xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 5ace19bd8395e8a98ff0bca0fd20ae3fac3e1d6f)
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 #include <net/netdev_rx_queue.h>
59 #include <linux/pci-tph.h>
60 
61 #include "bnxt_hsi.h"
62 #include "bnxt.h"
63 #include "bnxt_hwrm.h"
64 #include "bnxt_ulp.h"
65 #include "bnxt_sriov.h"
66 #include "bnxt_ethtool.h"
67 #include "bnxt_dcb.h"
68 #include "bnxt_xdp.h"
69 #include "bnxt_ptp.h"
70 #include "bnxt_vfr.h"
71 #include "bnxt_tc.h"
72 #include "bnxt_devlink.h"
73 #include "bnxt_debugfs.h"
74 #include "bnxt_coredump.h"
75 #include "bnxt_hwmon.h"
76 
77 #define BNXT_TX_TIMEOUT		(5 * HZ)
78 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
79 				 NETIF_MSG_TX_ERR)
80 
81 MODULE_IMPORT_NS("NETDEV_INTERNAL");
82 MODULE_LICENSE("GPL");
83 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
84 
85 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
86 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
87 
88 #define BNXT_TX_PUSH_THRESH 164
89 
90 /* indexed by enum board_idx */
91 static const struct {
92 	char *name;
93 } board_info[] = {
94 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
95 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
96 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
97 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
98 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
99 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
100 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
101 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
102 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
103 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
104 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
105 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
106 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
107 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
108 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
109 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
110 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
111 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
112 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
113 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
114 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
115 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
116 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
117 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
118 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
119 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
120 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
121 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
122 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
123 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
124 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
125 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
126 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
127 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
128 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
129 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
130 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
131 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
132 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
133 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
134 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
135 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
136 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
137 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
138 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
139 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
140 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
141 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
142 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
143 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
144 };
145 
146 static const struct pci_device_id bnxt_pci_tbl[] = {
147 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
148 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
149 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
150 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
151 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
152 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
153 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
155 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
166 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
168 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
169 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
170 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
171 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
172 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
173 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
174 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
178 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
179 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
180 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
181 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
182 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
183 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
188 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
189 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
190 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
191 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
193 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
194 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
195 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
196 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
197 #ifdef CONFIG_BNXT_SRIOV
198 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
199 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
200 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
201 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
202 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
204 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
206 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
207 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
208 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
209 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
210 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
211 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
212 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
214 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
215 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
216 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
217 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
218 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
219 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
220 #endif
221 	{ 0 }
222 };
223 
224 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
225 
226 static const u16 bnxt_vf_req_snif[] = {
227 	HWRM_FUNC_CFG,
228 	HWRM_FUNC_VF_CFG,
229 	HWRM_PORT_PHY_QCFG,
230 	HWRM_CFA_L2_FILTER_ALLOC,
231 };
232 
233 static const u16 bnxt_async_events_arr[] = {
234 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
235 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
236 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
237 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
238 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
239 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
240 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
241 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
242 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
243 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
244 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
245 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
246 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
247 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
248 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
249 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
250 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
251 };
252 
253 const u16 bnxt_bstore_to_trace[] = {
254 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
255 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
256 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
257 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
258 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
259 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
260 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
261 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
262 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
263 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
264 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
265 };
266 
267 static struct workqueue_struct *bnxt_pf_wq;
268 
269 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
270 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
271 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
272 
273 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
274 	.ports = {
275 		.src = 0,
276 		.dst = 0,
277 	},
278 	.addrs = {
279 		.v6addrs = {
280 			.src = BNXT_IPV6_MASK_NONE,
281 			.dst = BNXT_IPV6_MASK_NONE,
282 		},
283 	},
284 };
285 
286 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
287 	.ports = {
288 		.src = cpu_to_be16(0xffff),
289 		.dst = cpu_to_be16(0xffff),
290 	},
291 	.addrs = {
292 		.v6addrs = {
293 			.src = BNXT_IPV6_MASK_ALL,
294 			.dst = BNXT_IPV6_MASK_ALL,
295 		},
296 	},
297 };
298 
299 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
300 	.ports = {
301 		.src = cpu_to_be16(0xffff),
302 		.dst = cpu_to_be16(0xffff),
303 	},
304 	.addrs = {
305 		.v4addrs = {
306 			.src = cpu_to_be32(0xffffffff),
307 			.dst = cpu_to_be32(0xffffffff),
308 		},
309 	},
310 };
311 
312 static bool bnxt_vf_pciid(enum board_idx idx)
313 {
314 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
315 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
316 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
317 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF);
318 }
319 
320 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
321 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
322 
323 #define BNXT_DB_CQ(db, idx)						\
324 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
325 
326 #define BNXT_DB_NQ_P5(db, idx)						\
327 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
328 		    (db)->doorbell)
329 
330 #define BNXT_DB_NQ_P7(db, idx)						\
331 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
332 		    DB_RING_IDX(db, idx), (db)->doorbell)
333 
334 #define BNXT_DB_CQ_ARM(db, idx)						\
335 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
336 
337 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
338 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
339 		    DB_RING_IDX(db, idx), (db)->doorbell)
340 
341 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
342 {
343 	if (bp->flags & BNXT_FLAG_CHIP_P7)
344 		BNXT_DB_NQ_P7(db, idx);
345 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
346 		BNXT_DB_NQ_P5(db, idx);
347 	else
348 		BNXT_DB_CQ(db, idx);
349 }
350 
351 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
352 {
353 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
354 		BNXT_DB_NQ_ARM_P5(db, idx);
355 	else
356 		BNXT_DB_CQ_ARM(db, idx);
357 }
358 
359 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
360 {
361 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
362 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
363 			    DB_RING_IDX(db, idx), db->doorbell);
364 	else
365 		BNXT_DB_CQ(db, idx);
366 }
367 
368 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
369 {
370 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
371 		return;
372 
373 	if (BNXT_PF(bp))
374 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
375 	else
376 		schedule_delayed_work(&bp->fw_reset_task, delay);
377 }
378 
379 static void __bnxt_queue_sp_work(struct bnxt *bp)
380 {
381 	if (BNXT_PF(bp))
382 		queue_work(bnxt_pf_wq, &bp->sp_task);
383 	else
384 		schedule_work(&bp->sp_task);
385 }
386 
387 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
388 {
389 	set_bit(event, &bp->sp_event);
390 	__bnxt_queue_sp_work(bp);
391 }
392 
393 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
394 {
395 	if (!rxr->bnapi->in_reset) {
396 		rxr->bnapi->in_reset = true;
397 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
398 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
399 		else
400 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
401 		__bnxt_queue_sp_work(bp);
402 	}
403 	rxr->rx_next_cons = 0xffff;
404 }
405 
406 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
407 			  u16 curr)
408 {
409 	struct bnxt_napi *bnapi = txr->bnapi;
410 
411 	if (bnapi->tx_fault)
412 		return;
413 
414 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
415 		   txr->txq_index, txr->tx_hw_cons,
416 		   txr->tx_cons, txr->tx_prod, curr);
417 	WARN_ON_ONCE(1);
418 	bnapi->tx_fault = 1;
419 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
420 }
421 
422 const u16 bnxt_lhint_arr[] = {
423 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
424 	TX_BD_FLAGS_LHINT_512_TO_1023,
425 	TX_BD_FLAGS_LHINT_1024_TO_2047,
426 	TX_BD_FLAGS_LHINT_1024_TO_2047,
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 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
441 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
442 };
443 
444 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
445 {
446 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
447 
448 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
449 		return 0;
450 
451 	return md_dst->u.port_info.port_id;
452 }
453 
454 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
455 			     u16 prod)
456 {
457 	/* Sync BD data before updating doorbell */
458 	wmb();
459 	bnxt_db_write(bp, &txr->tx_db, prod);
460 	txr->kick_pending = 0;
461 }
462 
463 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
464 {
465 	struct bnxt *bp = netdev_priv(dev);
466 	struct tx_bd *txbd, *txbd0;
467 	struct tx_bd_ext *txbd1;
468 	struct netdev_queue *txq;
469 	int i;
470 	dma_addr_t mapping;
471 	unsigned int length, pad = 0;
472 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
473 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
474 	struct pci_dev *pdev = bp->pdev;
475 	u16 prod, last_frag, txts_prod;
476 	struct bnxt_tx_ring_info *txr;
477 	struct bnxt_sw_tx_bd *tx_buf;
478 	__le32 lflags = 0;
479 
480 	i = skb_get_queue_mapping(skb);
481 	if (unlikely(i >= bp->tx_nr_rings)) {
482 		dev_kfree_skb_any(skb);
483 		dev_core_stats_tx_dropped_inc(dev);
484 		return NETDEV_TX_OK;
485 	}
486 
487 	txq = netdev_get_tx_queue(dev, i);
488 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
489 	prod = txr->tx_prod;
490 
491 	free_size = bnxt_tx_avail(bp, txr);
492 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
493 		/* We must have raced with NAPI cleanup */
494 		if (net_ratelimit() && txr->kick_pending)
495 			netif_warn(bp, tx_err, dev,
496 				   "bnxt: ring busy w/ flush pending!\n");
497 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
498 					bp->tx_wake_thresh))
499 			return NETDEV_TX_BUSY;
500 	}
501 
502 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
503 		goto tx_free;
504 
505 	length = skb->len;
506 	len = skb_headlen(skb);
507 	last_frag = skb_shinfo(skb)->nr_frags;
508 
509 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
510 
511 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
512 	tx_buf->skb = skb;
513 	tx_buf->nr_frags = last_frag;
514 
515 	vlan_tag_flags = 0;
516 	cfa_action = bnxt_xmit_get_cfa_action(skb);
517 	if (skb_vlan_tag_present(skb)) {
518 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
519 				 skb_vlan_tag_get(skb);
520 		/* Currently supports 8021Q, 8021AD vlan offloads
521 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
522 		 */
523 		if (skb->vlan_proto == htons(ETH_P_8021Q))
524 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
525 	}
526 
527 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
528 	    ptp->tx_tstamp_en) {
529 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
530 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
531 			tx_buf->is_ts_pkt = 1;
532 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
533 		} else if (!skb_is_gso(skb)) {
534 			u16 seq_id, hdr_off;
535 
536 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
537 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
538 				if (vlan_tag_flags)
539 					hdr_off += VLAN_HLEN;
540 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
541 				tx_buf->is_ts_pkt = 1;
542 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
543 
544 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
545 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
546 				tx_buf->txts_prod = txts_prod;
547 			}
548 		}
549 	}
550 	if (unlikely(skb->no_fcs))
551 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
552 
553 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
554 	    !lflags) {
555 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
556 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
557 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
558 		void __iomem *db = txr->tx_db.doorbell;
559 		void *pdata = tx_push_buf->data;
560 		u64 *end;
561 		int j, push_len;
562 
563 		/* Set COAL_NOW to be ready quickly for the next push */
564 		tx_push->tx_bd_len_flags_type =
565 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
566 					TX_BD_TYPE_LONG_TX_BD |
567 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
568 					TX_BD_FLAGS_COAL_NOW |
569 					TX_BD_FLAGS_PACKET_END |
570 					(2 << TX_BD_FLAGS_BD_CNT_SHIFT));
571 
572 		if (skb->ip_summed == CHECKSUM_PARTIAL)
573 			tx_push1->tx_bd_hsize_lflags =
574 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
575 		else
576 			tx_push1->tx_bd_hsize_lflags = 0;
577 
578 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
579 		tx_push1->tx_bd_cfa_action =
580 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
581 
582 		end = pdata + length;
583 		end = PTR_ALIGN(end, 8) - 1;
584 		*end = 0;
585 
586 		skb_copy_from_linear_data(skb, pdata, len);
587 		pdata += len;
588 		for (j = 0; j < last_frag; j++) {
589 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
590 			void *fptr;
591 
592 			fptr = skb_frag_address_safe(frag);
593 			if (!fptr)
594 				goto normal_tx;
595 
596 			memcpy(pdata, fptr, skb_frag_size(frag));
597 			pdata += skb_frag_size(frag);
598 		}
599 
600 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
601 		txbd->tx_bd_haddr = txr->data_mapping;
602 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
603 		prod = NEXT_TX(prod);
604 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
605 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
606 		memcpy(txbd, tx_push1, sizeof(*txbd));
607 		prod = NEXT_TX(prod);
608 		tx_push->doorbell =
609 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
610 				    DB_RING_IDX(&txr->tx_db, prod));
611 		WRITE_ONCE(txr->tx_prod, prod);
612 
613 		tx_buf->is_push = 1;
614 		netdev_tx_sent_queue(txq, skb->len);
615 		wmb();	/* Sync is_push and byte queue before pushing data */
616 
617 		push_len = (length + sizeof(*tx_push) + 7) / 8;
618 		if (push_len > 16) {
619 			__iowrite64_copy(db, tx_push_buf, 16);
620 			__iowrite32_copy(db + 4, tx_push_buf + 1,
621 					 (push_len - 16) << 1);
622 		} else {
623 			__iowrite64_copy(db, tx_push_buf, push_len);
624 		}
625 
626 		goto tx_done;
627 	}
628 
629 normal_tx:
630 	if (length < BNXT_MIN_PKT_SIZE) {
631 		pad = BNXT_MIN_PKT_SIZE - length;
632 		if (skb_pad(skb, pad))
633 			/* SKB already freed. */
634 			goto tx_kick_pending;
635 		length = BNXT_MIN_PKT_SIZE;
636 	}
637 
638 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
639 
640 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
641 		goto tx_free;
642 
643 	dma_unmap_addr_set(tx_buf, mapping, mapping);
644 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
645 		((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
646 
647 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
648 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
649 
650 	prod = NEXT_TX(prod);
651 	txbd1 = (struct tx_bd_ext *)
652 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
653 
654 	txbd1->tx_bd_hsize_lflags = lflags;
655 	if (skb_is_gso(skb)) {
656 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
657 		u32 hdr_len;
658 
659 		if (skb->encapsulation) {
660 			if (udp_gso)
661 				hdr_len = skb_inner_transport_offset(skb) +
662 					  sizeof(struct udphdr);
663 			else
664 				hdr_len = skb_inner_tcp_all_headers(skb);
665 		} else if (udp_gso) {
666 			hdr_len = skb_transport_offset(skb) +
667 				  sizeof(struct udphdr);
668 		} else {
669 			hdr_len = skb_tcp_all_headers(skb);
670 		}
671 
672 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
673 					TX_BD_FLAGS_T_IPID |
674 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
675 		length = skb_shinfo(skb)->gso_size;
676 		txbd1->tx_bd_mss = cpu_to_le32(length);
677 		length += hdr_len;
678 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
679 		txbd1->tx_bd_hsize_lflags |=
680 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
681 		txbd1->tx_bd_mss = 0;
682 	}
683 
684 	length >>= 9;
685 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
686 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
687 				     skb->len);
688 		i = 0;
689 		goto tx_dma_error;
690 	}
691 	flags |= bnxt_lhint_arr[length];
692 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
693 
694 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
695 	txbd1->tx_bd_cfa_action =
696 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
697 	txbd0 = txbd;
698 	for (i = 0; i < last_frag; i++) {
699 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
700 
701 		prod = NEXT_TX(prod);
702 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
703 
704 		len = skb_frag_size(frag);
705 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
706 					   DMA_TO_DEVICE);
707 
708 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
709 			goto tx_dma_error;
710 
711 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
712 		dma_unmap_addr_set(tx_buf, mapping, mapping);
713 
714 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
715 
716 		flags = len << TX_BD_LEN_SHIFT;
717 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
718 	}
719 
720 	flags &= ~TX_BD_LEN;
721 	txbd->tx_bd_len_flags_type =
722 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
723 			    TX_BD_FLAGS_PACKET_END);
724 
725 	netdev_tx_sent_queue(txq, skb->len);
726 
727 	skb_tx_timestamp(skb);
728 
729 	prod = NEXT_TX(prod);
730 	WRITE_ONCE(txr->tx_prod, prod);
731 
732 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
733 		bnxt_txr_db_kick(bp, txr, prod);
734 	} else {
735 		if (free_size >= bp->tx_wake_thresh)
736 			txbd0->tx_bd_len_flags_type |=
737 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
738 		txr->kick_pending = 1;
739 	}
740 
741 tx_done:
742 
743 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
744 		if (netdev_xmit_more() && !tx_buf->is_push) {
745 			txbd0->tx_bd_len_flags_type &=
746 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
747 			bnxt_txr_db_kick(bp, txr, prod);
748 		}
749 
750 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
751 				   bp->tx_wake_thresh);
752 	}
753 	return NETDEV_TX_OK;
754 
755 tx_dma_error:
756 	last_frag = i;
757 
758 	/* start back at beginning and unmap skb */
759 	prod = txr->tx_prod;
760 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
761 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
762 			 skb_headlen(skb), DMA_TO_DEVICE);
763 	prod = NEXT_TX(prod);
764 
765 	/* unmap remaining mapped pages */
766 	for (i = 0; i < last_frag; i++) {
767 		prod = NEXT_TX(prod);
768 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
769 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
770 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
771 			       DMA_TO_DEVICE);
772 	}
773 
774 tx_free:
775 	dev_kfree_skb_any(skb);
776 tx_kick_pending:
777 	if (BNXT_TX_PTP_IS_SET(lflags)) {
778 		txr->tx_buf_ring[txr->tx_prod].is_ts_pkt = 0;
779 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
780 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
781 			/* set SKB to err so PTP worker will clean up */
782 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
783 	}
784 	if (txr->kick_pending)
785 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
786 	txr->tx_buf_ring[txr->tx_prod].skb = NULL;
787 	dev_core_stats_tx_dropped_inc(dev);
788 	return NETDEV_TX_OK;
789 }
790 
791 /* Returns true if some remaining TX packets not processed. */
792 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
793 			  int budget)
794 {
795 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
796 	struct pci_dev *pdev = bp->pdev;
797 	u16 hw_cons = txr->tx_hw_cons;
798 	unsigned int tx_bytes = 0;
799 	u16 cons = txr->tx_cons;
800 	int tx_pkts = 0;
801 	bool rc = false;
802 
803 	while (RING_TX(bp, cons) != hw_cons) {
804 		struct bnxt_sw_tx_bd *tx_buf;
805 		struct sk_buff *skb;
806 		bool is_ts_pkt;
807 		int j, last;
808 
809 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
810 		skb = tx_buf->skb;
811 
812 		if (unlikely(!skb)) {
813 			bnxt_sched_reset_txr(bp, txr, cons);
814 			return rc;
815 		}
816 
817 		is_ts_pkt = tx_buf->is_ts_pkt;
818 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
819 			rc = true;
820 			break;
821 		}
822 
823 		cons = NEXT_TX(cons);
824 		tx_pkts++;
825 		tx_bytes += skb->len;
826 		tx_buf->skb = NULL;
827 		tx_buf->is_ts_pkt = 0;
828 
829 		if (tx_buf->is_push) {
830 			tx_buf->is_push = 0;
831 			goto next_tx_int;
832 		}
833 
834 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
835 				 skb_headlen(skb), DMA_TO_DEVICE);
836 		last = tx_buf->nr_frags;
837 
838 		for (j = 0; j < last; j++) {
839 			cons = NEXT_TX(cons);
840 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
841 			dma_unmap_page(
842 				&pdev->dev,
843 				dma_unmap_addr(tx_buf, mapping),
844 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
845 				DMA_TO_DEVICE);
846 		}
847 		if (unlikely(is_ts_pkt)) {
848 			if (BNXT_CHIP_P5(bp)) {
849 				/* PTP worker takes ownership of the skb */
850 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
851 				skb = NULL;
852 			}
853 		}
854 
855 next_tx_int:
856 		cons = NEXT_TX(cons);
857 
858 		dev_consume_skb_any(skb);
859 	}
860 
861 	WRITE_ONCE(txr->tx_cons, cons);
862 
863 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
864 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
865 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
866 
867 	return rc;
868 }
869 
870 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
871 {
872 	struct bnxt_tx_ring_info *txr;
873 	bool more = false;
874 	int i;
875 
876 	bnxt_for_each_napi_tx(i, bnapi, txr) {
877 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
878 			more |= __bnxt_tx_int(bp, txr, budget);
879 	}
880 	if (!more)
881 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
882 }
883 
884 static bool bnxt_separate_head_pool(void)
885 {
886 	return PAGE_SIZE > BNXT_RX_PAGE_SIZE;
887 }
888 
889 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
890 					 struct bnxt_rx_ring_info *rxr,
891 					 unsigned int *offset,
892 					 gfp_t gfp)
893 {
894 	struct page *page;
895 
896 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
897 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
898 						BNXT_RX_PAGE_SIZE);
899 	} else {
900 		page = page_pool_dev_alloc_pages(rxr->page_pool);
901 		*offset = 0;
902 	}
903 	if (!page)
904 		return NULL;
905 
906 	*mapping = page_pool_get_dma_addr(page) + *offset;
907 	return page;
908 }
909 
910 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
911 				       struct bnxt_rx_ring_info *rxr,
912 				       gfp_t gfp)
913 {
914 	unsigned int offset;
915 	struct page *page;
916 
917 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
918 				    bp->rx_buf_size, gfp);
919 	if (!page)
920 		return NULL;
921 
922 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
923 	return page_address(page) + offset;
924 }
925 
926 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
927 		       u16 prod, gfp_t gfp)
928 {
929 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
930 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
931 	dma_addr_t mapping;
932 
933 	if (BNXT_RX_PAGE_MODE(bp)) {
934 		unsigned int offset;
935 		struct page *page =
936 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
937 
938 		if (!page)
939 			return -ENOMEM;
940 
941 		mapping += bp->rx_dma_offset;
942 		rx_buf->data = page;
943 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
944 	} else {
945 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
946 
947 		if (!data)
948 			return -ENOMEM;
949 
950 		rx_buf->data = data;
951 		rx_buf->data_ptr = data + bp->rx_offset;
952 	}
953 	rx_buf->mapping = mapping;
954 
955 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
956 	return 0;
957 }
958 
959 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
960 {
961 	u16 prod = rxr->rx_prod;
962 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
963 	struct bnxt *bp = rxr->bnapi->bp;
964 	struct rx_bd *cons_bd, *prod_bd;
965 
966 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
967 	cons_rx_buf = &rxr->rx_buf_ring[cons];
968 
969 	prod_rx_buf->data = data;
970 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
971 
972 	prod_rx_buf->mapping = cons_rx_buf->mapping;
973 
974 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
975 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
976 
977 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
978 }
979 
980 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
981 {
982 	u16 next, max = rxr->rx_agg_bmap_size;
983 
984 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
985 	if (next >= max)
986 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
987 	return next;
988 }
989 
990 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
991 				     struct bnxt_rx_ring_info *rxr,
992 				     u16 prod, gfp_t gfp)
993 {
994 	struct rx_bd *rxbd =
995 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
996 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
997 	struct page *page;
998 	dma_addr_t mapping;
999 	u16 sw_prod = rxr->rx_sw_agg_prod;
1000 	unsigned int offset = 0;
1001 
1002 	page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
1003 
1004 	if (!page)
1005 		return -ENOMEM;
1006 
1007 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1008 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1009 
1010 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1011 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1012 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1013 
1014 	rx_agg_buf->page = page;
1015 	rx_agg_buf->offset = offset;
1016 	rx_agg_buf->mapping = mapping;
1017 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1018 	rxbd->rx_bd_opaque = sw_prod;
1019 	return 0;
1020 }
1021 
1022 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1023 				       struct bnxt_cp_ring_info *cpr,
1024 				       u16 cp_cons, u16 curr)
1025 {
1026 	struct rx_agg_cmp *agg;
1027 
1028 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1029 	agg = (struct rx_agg_cmp *)
1030 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1031 	return agg;
1032 }
1033 
1034 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1035 					      struct bnxt_rx_ring_info *rxr,
1036 					      u16 agg_id, u16 curr)
1037 {
1038 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1039 
1040 	return &tpa_info->agg_arr[curr];
1041 }
1042 
1043 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1044 				   u16 start, u32 agg_bufs, bool tpa)
1045 {
1046 	struct bnxt_napi *bnapi = cpr->bnapi;
1047 	struct bnxt *bp = bnapi->bp;
1048 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1049 	u16 prod = rxr->rx_agg_prod;
1050 	u16 sw_prod = rxr->rx_sw_agg_prod;
1051 	bool p5_tpa = false;
1052 	u32 i;
1053 
1054 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1055 		p5_tpa = true;
1056 
1057 	for (i = 0; i < agg_bufs; i++) {
1058 		u16 cons;
1059 		struct rx_agg_cmp *agg;
1060 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1061 		struct rx_bd *prod_bd;
1062 		struct page *page;
1063 
1064 		if (p5_tpa)
1065 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1066 		else
1067 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1068 		cons = agg->rx_agg_cmp_opaque;
1069 		__clear_bit(cons, rxr->rx_agg_bmap);
1070 
1071 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1072 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1073 
1074 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1075 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1076 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1077 
1078 		/* It is possible for sw_prod to be equal to cons, so
1079 		 * set cons_rx_buf->page to NULL first.
1080 		 */
1081 		page = cons_rx_buf->page;
1082 		cons_rx_buf->page = NULL;
1083 		prod_rx_buf->page = page;
1084 		prod_rx_buf->offset = cons_rx_buf->offset;
1085 
1086 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1087 
1088 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1089 
1090 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1091 		prod_bd->rx_bd_opaque = sw_prod;
1092 
1093 		prod = NEXT_RX_AGG(prod);
1094 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1095 	}
1096 	rxr->rx_agg_prod = prod;
1097 	rxr->rx_sw_agg_prod = sw_prod;
1098 }
1099 
1100 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1101 					      struct bnxt_rx_ring_info *rxr,
1102 					      u16 cons, void *data, u8 *data_ptr,
1103 					      dma_addr_t dma_addr,
1104 					      unsigned int offset_and_len)
1105 {
1106 	unsigned int len = offset_and_len & 0xffff;
1107 	struct page *page = data;
1108 	u16 prod = rxr->rx_prod;
1109 	struct sk_buff *skb;
1110 	int err;
1111 
1112 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1113 	if (unlikely(err)) {
1114 		bnxt_reuse_rx_data(rxr, cons, data);
1115 		return NULL;
1116 	}
1117 	dma_addr -= bp->rx_dma_offset;
1118 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1119 				bp->rx_dir);
1120 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1121 	if (!skb) {
1122 		page_pool_recycle_direct(rxr->page_pool, page);
1123 		return NULL;
1124 	}
1125 	skb_mark_for_recycle(skb);
1126 	skb_reserve(skb, bp->rx_offset);
1127 	__skb_put(skb, len);
1128 
1129 	return skb;
1130 }
1131 
1132 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1133 					struct bnxt_rx_ring_info *rxr,
1134 					u16 cons, void *data, u8 *data_ptr,
1135 					dma_addr_t dma_addr,
1136 					unsigned int offset_and_len)
1137 {
1138 	unsigned int payload = offset_and_len >> 16;
1139 	unsigned int len = offset_and_len & 0xffff;
1140 	skb_frag_t *frag;
1141 	struct page *page = data;
1142 	u16 prod = rxr->rx_prod;
1143 	struct sk_buff *skb;
1144 	int off, err;
1145 
1146 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1147 	if (unlikely(err)) {
1148 		bnxt_reuse_rx_data(rxr, cons, data);
1149 		return NULL;
1150 	}
1151 	dma_addr -= bp->rx_dma_offset;
1152 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1153 				bp->rx_dir);
1154 
1155 	if (unlikely(!payload))
1156 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1157 
1158 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1159 	if (!skb) {
1160 		page_pool_recycle_direct(rxr->page_pool, page);
1161 		return NULL;
1162 	}
1163 
1164 	skb_mark_for_recycle(skb);
1165 	off = (void *)data_ptr - page_address(page);
1166 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1167 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1168 	       payload + NET_IP_ALIGN);
1169 
1170 	frag = &skb_shinfo(skb)->frags[0];
1171 	skb_frag_size_sub(frag, payload);
1172 	skb_frag_off_add(frag, payload);
1173 	skb->data_len -= payload;
1174 	skb->tail += payload;
1175 
1176 	return skb;
1177 }
1178 
1179 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1180 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1181 				   void *data, u8 *data_ptr,
1182 				   dma_addr_t dma_addr,
1183 				   unsigned int offset_and_len)
1184 {
1185 	u16 prod = rxr->rx_prod;
1186 	struct sk_buff *skb;
1187 	int err;
1188 
1189 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1190 	if (unlikely(err)) {
1191 		bnxt_reuse_rx_data(rxr, cons, data);
1192 		return NULL;
1193 	}
1194 
1195 	skb = napi_build_skb(data, bp->rx_buf_size);
1196 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1197 				bp->rx_dir);
1198 	if (!skb) {
1199 		page_pool_free_va(rxr->head_pool, data, true);
1200 		return NULL;
1201 	}
1202 
1203 	skb_mark_for_recycle(skb);
1204 	skb_reserve(skb, bp->rx_offset);
1205 	skb_put(skb, offset_and_len & 0xffff);
1206 	return skb;
1207 }
1208 
1209 static u32 __bnxt_rx_agg_pages(struct bnxt *bp,
1210 			       struct bnxt_cp_ring_info *cpr,
1211 			       struct skb_shared_info *shinfo,
1212 			       u16 idx, u32 agg_bufs, bool tpa,
1213 			       struct xdp_buff *xdp)
1214 {
1215 	struct bnxt_napi *bnapi = cpr->bnapi;
1216 	struct pci_dev *pdev = bp->pdev;
1217 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1218 	u16 prod = rxr->rx_agg_prod;
1219 	u32 i, total_frag_len = 0;
1220 	bool p5_tpa = false;
1221 
1222 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1223 		p5_tpa = true;
1224 
1225 	for (i = 0; i < agg_bufs; i++) {
1226 		skb_frag_t *frag = &shinfo->frags[i];
1227 		u16 cons, frag_len;
1228 		struct rx_agg_cmp *agg;
1229 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1230 		struct page *page;
1231 		dma_addr_t mapping;
1232 
1233 		if (p5_tpa)
1234 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1235 		else
1236 			agg = bnxt_get_agg(bp, cpr, idx, i);
1237 		cons = agg->rx_agg_cmp_opaque;
1238 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1239 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1240 
1241 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1242 		skb_frag_fill_page_desc(frag, cons_rx_buf->page,
1243 					cons_rx_buf->offset, frag_len);
1244 		shinfo->nr_frags = i + 1;
1245 		__clear_bit(cons, rxr->rx_agg_bmap);
1246 
1247 		/* It is possible for bnxt_alloc_rx_page() to allocate
1248 		 * a sw_prod index that equals the cons index, so we
1249 		 * need to clear the cons entry now.
1250 		 */
1251 		mapping = cons_rx_buf->mapping;
1252 		page = cons_rx_buf->page;
1253 		cons_rx_buf->page = NULL;
1254 
1255 		if (xdp && page_is_pfmemalloc(page))
1256 			xdp_buff_set_frag_pfmemalloc(xdp);
1257 
1258 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1259 			--shinfo->nr_frags;
1260 			cons_rx_buf->page = page;
1261 
1262 			/* Update prod since possibly some pages have been
1263 			 * allocated already.
1264 			 */
1265 			rxr->rx_agg_prod = prod;
1266 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1267 			return 0;
1268 		}
1269 
1270 		dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1271 					bp->rx_dir);
1272 
1273 		total_frag_len += frag_len;
1274 		prod = NEXT_RX_AGG(prod);
1275 	}
1276 	rxr->rx_agg_prod = prod;
1277 	return total_frag_len;
1278 }
1279 
1280 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp,
1281 					     struct bnxt_cp_ring_info *cpr,
1282 					     struct sk_buff *skb, u16 idx,
1283 					     u32 agg_bufs, bool tpa)
1284 {
1285 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1286 	u32 total_frag_len = 0;
1287 
1288 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx,
1289 					     agg_bufs, tpa, NULL);
1290 	if (!total_frag_len) {
1291 		skb_mark_for_recycle(skb);
1292 		dev_kfree_skb(skb);
1293 		return NULL;
1294 	}
1295 
1296 	skb->data_len += total_frag_len;
1297 	skb->len += total_frag_len;
1298 	skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs;
1299 	return skb;
1300 }
1301 
1302 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp,
1303 				 struct bnxt_cp_ring_info *cpr,
1304 				 struct xdp_buff *xdp, u16 idx,
1305 				 u32 agg_bufs, bool tpa)
1306 {
1307 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1308 	u32 total_frag_len = 0;
1309 
1310 	if (!xdp_buff_has_frags(xdp))
1311 		shinfo->nr_frags = 0;
1312 
1313 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo,
1314 					     idx, agg_bufs, tpa, xdp);
1315 	if (total_frag_len) {
1316 		xdp_buff_set_frags_flag(xdp);
1317 		shinfo->nr_frags = agg_bufs;
1318 		shinfo->xdp_frags_size = total_frag_len;
1319 	}
1320 	return total_frag_len;
1321 }
1322 
1323 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1324 			       u8 agg_bufs, u32 *raw_cons)
1325 {
1326 	u16 last;
1327 	struct rx_agg_cmp *agg;
1328 
1329 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1330 	last = RING_CMP(*raw_cons);
1331 	agg = (struct rx_agg_cmp *)
1332 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1333 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1334 }
1335 
1336 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1337 				      unsigned int len,
1338 				      dma_addr_t mapping)
1339 {
1340 	struct bnxt *bp = bnapi->bp;
1341 	struct pci_dev *pdev = bp->pdev;
1342 	struct sk_buff *skb;
1343 
1344 	skb = napi_alloc_skb(&bnapi->napi, len);
1345 	if (!skb)
1346 		return NULL;
1347 
1348 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1349 				bp->rx_dir);
1350 
1351 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1352 	       len + NET_IP_ALIGN);
1353 
1354 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1355 				   bp->rx_dir);
1356 
1357 	skb_put(skb, len);
1358 
1359 	return skb;
1360 }
1361 
1362 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1363 				     unsigned int len,
1364 				     dma_addr_t mapping)
1365 {
1366 	return bnxt_copy_data(bnapi, data, len, mapping);
1367 }
1368 
1369 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1370 				     struct xdp_buff *xdp,
1371 				     unsigned int len,
1372 				     dma_addr_t mapping)
1373 {
1374 	unsigned int metasize = 0;
1375 	u8 *data = xdp->data;
1376 	struct sk_buff *skb;
1377 
1378 	len = xdp->data_end - xdp->data_meta;
1379 	metasize = xdp->data - xdp->data_meta;
1380 	data = xdp->data_meta;
1381 
1382 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1383 	if (!skb)
1384 		return skb;
1385 
1386 	if (metasize) {
1387 		skb_metadata_set(skb, metasize);
1388 		__skb_pull(skb, metasize);
1389 	}
1390 
1391 	return skb;
1392 }
1393 
1394 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1395 			   u32 *raw_cons, void *cmp)
1396 {
1397 	struct rx_cmp *rxcmp = cmp;
1398 	u32 tmp_raw_cons = *raw_cons;
1399 	u8 cmp_type, agg_bufs = 0;
1400 
1401 	cmp_type = RX_CMP_TYPE(rxcmp);
1402 
1403 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1404 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1405 			    RX_CMP_AGG_BUFS) >>
1406 			   RX_CMP_AGG_BUFS_SHIFT;
1407 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1408 		struct rx_tpa_end_cmp *tpa_end = cmp;
1409 
1410 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1411 			return 0;
1412 
1413 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1414 	}
1415 
1416 	if (agg_bufs) {
1417 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1418 			return -EBUSY;
1419 	}
1420 	*raw_cons = tmp_raw_cons;
1421 	return 0;
1422 }
1423 
1424 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1425 {
1426 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1427 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1428 
1429 	if (test_bit(idx, map->agg_idx_bmap))
1430 		idx = find_first_zero_bit(map->agg_idx_bmap,
1431 					  BNXT_AGG_IDX_BMAP_SIZE);
1432 	__set_bit(idx, map->agg_idx_bmap);
1433 	map->agg_id_tbl[agg_id] = idx;
1434 	return idx;
1435 }
1436 
1437 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1438 {
1439 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1440 
1441 	__clear_bit(idx, map->agg_idx_bmap);
1442 }
1443 
1444 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1445 {
1446 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1447 
1448 	return map->agg_id_tbl[agg_id];
1449 }
1450 
1451 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1452 			      struct rx_tpa_start_cmp *tpa_start,
1453 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1454 {
1455 	tpa_info->cfa_code_valid = 1;
1456 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1457 	tpa_info->vlan_valid = 0;
1458 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1459 		tpa_info->vlan_valid = 1;
1460 		tpa_info->metadata =
1461 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1462 	}
1463 }
1464 
1465 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1466 				 struct rx_tpa_start_cmp *tpa_start,
1467 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1468 {
1469 	tpa_info->vlan_valid = 0;
1470 	if (TPA_START_VLAN_VALID(tpa_start)) {
1471 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1472 		u32 vlan_proto = ETH_P_8021Q;
1473 
1474 		tpa_info->vlan_valid = 1;
1475 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1476 			vlan_proto = ETH_P_8021AD;
1477 		tpa_info->metadata = vlan_proto << 16 |
1478 				     TPA_START_METADATA0_TCI(tpa_start1);
1479 	}
1480 }
1481 
1482 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1483 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1484 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1485 {
1486 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1487 	struct bnxt_tpa_info *tpa_info;
1488 	u16 cons, prod, agg_id;
1489 	struct rx_bd *prod_bd;
1490 	dma_addr_t mapping;
1491 
1492 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1493 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1494 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1495 	} else {
1496 		agg_id = TPA_START_AGG_ID(tpa_start);
1497 	}
1498 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1499 	prod = rxr->rx_prod;
1500 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1501 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1502 	tpa_info = &rxr->rx_tpa[agg_id];
1503 
1504 	if (unlikely(cons != rxr->rx_next_cons ||
1505 		     TPA_START_ERROR(tpa_start))) {
1506 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1507 			    cons, rxr->rx_next_cons,
1508 			    TPA_START_ERROR_CODE(tpa_start1));
1509 		bnxt_sched_reset_rxr(bp, rxr);
1510 		return;
1511 	}
1512 	prod_rx_buf->data = tpa_info->data;
1513 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1514 
1515 	mapping = tpa_info->mapping;
1516 	prod_rx_buf->mapping = mapping;
1517 
1518 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1519 
1520 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1521 
1522 	tpa_info->data = cons_rx_buf->data;
1523 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1524 	cons_rx_buf->data = NULL;
1525 	tpa_info->mapping = cons_rx_buf->mapping;
1526 
1527 	tpa_info->len =
1528 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1529 				RX_TPA_START_CMP_LEN_SHIFT;
1530 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1531 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1532 		tpa_info->gso_type = SKB_GSO_TCPV4;
1533 		if (TPA_START_IS_IPV6(tpa_start1))
1534 			tpa_info->gso_type = SKB_GSO_TCPV6;
1535 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1536 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1537 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1538 			tpa_info->gso_type = SKB_GSO_TCPV6;
1539 		tpa_info->rss_hash =
1540 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1541 	} else {
1542 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1543 		tpa_info->gso_type = 0;
1544 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1545 	}
1546 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1547 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1548 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1549 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1550 	else
1551 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1552 	tpa_info->agg_count = 0;
1553 
1554 	rxr->rx_prod = NEXT_RX(prod);
1555 	cons = RING_RX(bp, NEXT_RX(cons));
1556 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1557 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1558 
1559 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1560 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1561 	cons_rx_buf->data = NULL;
1562 }
1563 
1564 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1565 {
1566 	if (agg_bufs)
1567 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1568 }
1569 
1570 #ifdef CONFIG_INET
1571 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1572 {
1573 	struct udphdr *uh = NULL;
1574 
1575 	if (ip_proto == htons(ETH_P_IP)) {
1576 		struct iphdr *iph = (struct iphdr *)skb->data;
1577 
1578 		if (iph->protocol == IPPROTO_UDP)
1579 			uh = (struct udphdr *)(iph + 1);
1580 	} else {
1581 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1582 
1583 		if (iph->nexthdr == IPPROTO_UDP)
1584 			uh = (struct udphdr *)(iph + 1);
1585 	}
1586 	if (uh) {
1587 		if (uh->check)
1588 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1589 		else
1590 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1591 	}
1592 }
1593 #endif
1594 
1595 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1596 					   int payload_off, int tcp_ts,
1597 					   struct sk_buff *skb)
1598 {
1599 #ifdef CONFIG_INET
1600 	struct tcphdr *th;
1601 	int len, nw_off;
1602 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1603 	u32 hdr_info = tpa_info->hdr_info;
1604 	bool loopback = false;
1605 
1606 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1607 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1608 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1609 
1610 	/* If the packet is an internal loopback packet, the offsets will
1611 	 * have an extra 4 bytes.
1612 	 */
1613 	if (inner_mac_off == 4) {
1614 		loopback = true;
1615 	} else if (inner_mac_off > 4) {
1616 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1617 					    ETH_HLEN - 2));
1618 
1619 		/* We only support inner iPv4/ipv6.  If we don't see the
1620 		 * correct protocol ID, it must be a loopback packet where
1621 		 * the offsets are off by 4.
1622 		 */
1623 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1624 			loopback = true;
1625 	}
1626 	if (loopback) {
1627 		/* internal loopback packet, subtract all offsets by 4 */
1628 		inner_ip_off -= 4;
1629 		inner_mac_off -= 4;
1630 		outer_ip_off -= 4;
1631 	}
1632 
1633 	nw_off = inner_ip_off - ETH_HLEN;
1634 	skb_set_network_header(skb, nw_off);
1635 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1636 		struct ipv6hdr *iph = ipv6_hdr(skb);
1637 
1638 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1639 		len = skb->len - skb_transport_offset(skb);
1640 		th = tcp_hdr(skb);
1641 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1642 	} else {
1643 		struct iphdr *iph = ip_hdr(skb);
1644 
1645 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1646 		len = skb->len - skb_transport_offset(skb);
1647 		th = tcp_hdr(skb);
1648 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1649 	}
1650 
1651 	if (inner_mac_off) { /* tunnel */
1652 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1653 					    ETH_HLEN - 2));
1654 
1655 		bnxt_gro_tunnel(skb, proto);
1656 	}
1657 #endif
1658 	return skb;
1659 }
1660 
1661 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1662 					   int payload_off, int tcp_ts,
1663 					   struct sk_buff *skb)
1664 {
1665 #ifdef CONFIG_INET
1666 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1667 	u32 hdr_info = tpa_info->hdr_info;
1668 	int iphdr_len, nw_off;
1669 
1670 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1671 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1672 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1673 
1674 	nw_off = inner_ip_off - ETH_HLEN;
1675 	skb_set_network_header(skb, nw_off);
1676 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1677 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1678 	skb_set_transport_header(skb, nw_off + iphdr_len);
1679 
1680 	if (inner_mac_off) { /* tunnel */
1681 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1682 					    ETH_HLEN - 2));
1683 
1684 		bnxt_gro_tunnel(skb, proto);
1685 	}
1686 #endif
1687 	return skb;
1688 }
1689 
1690 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1691 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1692 
1693 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1694 					   int payload_off, int tcp_ts,
1695 					   struct sk_buff *skb)
1696 {
1697 #ifdef CONFIG_INET
1698 	struct tcphdr *th;
1699 	int len, nw_off, tcp_opt_len = 0;
1700 
1701 	if (tcp_ts)
1702 		tcp_opt_len = 12;
1703 
1704 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1705 		struct iphdr *iph;
1706 
1707 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1708 			 ETH_HLEN;
1709 		skb_set_network_header(skb, nw_off);
1710 		iph = ip_hdr(skb);
1711 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1712 		len = skb->len - skb_transport_offset(skb);
1713 		th = tcp_hdr(skb);
1714 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1715 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1716 		struct ipv6hdr *iph;
1717 
1718 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1719 			 ETH_HLEN;
1720 		skb_set_network_header(skb, nw_off);
1721 		iph = ipv6_hdr(skb);
1722 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1723 		len = skb->len - skb_transport_offset(skb);
1724 		th = tcp_hdr(skb);
1725 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1726 	} else {
1727 		dev_kfree_skb_any(skb);
1728 		return NULL;
1729 	}
1730 
1731 	if (nw_off) /* tunnel */
1732 		bnxt_gro_tunnel(skb, skb->protocol);
1733 #endif
1734 	return skb;
1735 }
1736 
1737 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1738 					   struct bnxt_tpa_info *tpa_info,
1739 					   struct rx_tpa_end_cmp *tpa_end,
1740 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1741 					   struct sk_buff *skb)
1742 {
1743 #ifdef CONFIG_INET
1744 	int payload_off;
1745 	u16 segs;
1746 
1747 	segs = TPA_END_TPA_SEGS(tpa_end);
1748 	if (segs == 1)
1749 		return skb;
1750 
1751 	NAPI_GRO_CB(skb)->count = segs;
1752 	skb_shinfo(skb)->gso_size =
1753 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1754 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1755 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1756 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1757 	else
1758 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1759 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1760 	if (likely(skb))
1761 		tcp_gro_complete(skb);
1762 #endif
1763 	return skb;
1764 }
1765 
1766 /* Given the cfa_code of a received packet determine which
1767  * netdev (vf-rep or PF) the packet is destined to.
1768  */
1769 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1770 {
1771 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1772 
1773 	/* if vf-rep dev is NULL, the must belongs to the PF */
1774 	return dev ? dev : bp->dev;
1775 }
1776 
1777 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1778 					   struct bnxt_cp_ring_info *cpr,
1779 					   u32 *raw_cons,
1780 					   struct rx_tpa_end_cmp *tpa_end,
1781 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1782 					   u8 *event)
1783 {
1784 	struct bnxt_napi *bnapi = cpr->bnapi;
1785 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1786 	struct net_device *dev = bp->dev;
1787 	u8 *data_ptr, agg_bufs;
1788 	unsigned int len;
1789 	struct bnxt_tpa_info *tpa_info;
1790 	dma_addr_t mapping;
1791 	struct sk_buff *skb;
1792 	u16 idx = 0, agg_id;
1793 	void *data;
1794 	bool gro;
1795 
1796 	if (unlikely(bnapi->in_reset)) {
1797 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1798 
1799 		if (rc < 0)
1800 			return ERR_PTR(-EBUSY);
1801 		return NULL;
1802 	}
1803 
1804 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1805 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1806 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1807 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1808 		tpa_info = &rxr->rx_tpa[agg_id];
1809 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1810 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1811 				    agg_bufs, tpa_info->agg_count);
1812 			agg_bufs = tpa_info->agg_count;
1813 		}
1814 		tpa_info->agg_count = 0;
1815 		*event |= BNXT_AGG_EVENT;
1816 		bnxt_free_agg_idx(rxr, agg_id);
1817 		idx = agg_id;
1818 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1819 	} else {
1820 		agg_id = TPA_END_AGG_ID(tpa_end);
1821 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1822 		tpa_info = &rxr->rx_tpa[agg_id];
1823 		idx = RING_CMP(*raw_cons);
1824 		if (agg_bufs) {
1825 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1826 				return ERR_PTR(-EBUSY);
1827 
1828 			*event |= BNXT_AGG_EVENT;
1829 			idx = NEXT_CMP(idx);
1830 		}
1831 		gro = !!TPA_END_GRO(tpa_end);
1832 	}
1833 	data = tpa_info->data;
1834 	data_ptr = tpa_info->data_ptr;
1835 	prefetch(data_ptr);
1836 	len = tpa_info->len;
1837 	mapping = tpa_info->mapping;
1838 
1839 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1840 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1841 		if (agg_bufs > MAX_SKB_FRAGS)
1842 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1843 				    agg_bufs, (int)MAX_SKB_FRAGS);
1844 		return NULL;
1845 	}
1846 
1847 	if (len <= bp->rx_copybreak) {
1848 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1849 		if (!skb) {
1850 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1851 			cpr->sw_stats->rx.rx_oom_discards += 1;
1852 			return NULL;
1853 		}
1854 	} else {
1855 		u8 *new_data;
1856 		dma_addr_t new_mapping;
1857 
1858 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1859 						GFP_ATOMIC);
1860 		if (!new_data) {
1861 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1862 			cpr->sw_stats->rx.rx_oom_discards += 1;
1863 			return NULL;
1864 		}
1865 
1866 		tpa_info->data = new_data;
1867 		tpa_info->data_ptr = new_data + bp->rx_offset;
1868 		tpa_info->mapping = new_mapping;
1869 
1870 		skb = napi_build_skb(data, bp->rx_buf_size);
1871 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1872 					bp->rx_buf_use_size, bp->rx_dir);
1873 
1874 		if (!skb) {
1875 			page_pool_free_va(rxr->head_pool, data, true);
1876 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1877 			cpr->sw_stats->rx.rx_oom_discards += 1;
1878 			return NULL;
1879 		}
1880 		skb_mark_for_recycle(skb);
1881 		skb_reserve(skb, bp->rx_offset);
1882 		skb_put(skb, len);
1883 	}
1884 
1885 	if (agg_bufs) {
1886 		skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true);
1887 		if (!skb) {
1888 			/* Page reuse already handled by bnxt_rx_pages(). */
1889 			cpr->sw_stats->rx.rx_oom_discards += 1;
1890 			return NULL;
1891 		}
1892 	}
1893 
1894 	if (tpa_info->cfa_code_valid)
1895 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1896 	skb->protocol = eth_type_trans(skb, dev);
1897 
1898 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1899 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1900 
1901 	if (tpa_info->vlan_valid &&
1902 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1903 		__be16 vlan_proto = htons(tpa_info->metadata >>
1904 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1905 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1906 
1907 		if (eth_type_vlan(vlan_proto)) {
1908 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1909 		} else {
1910 			dev_kfree_skb(skb);
1911 			return NULL;
1912 		}
1913 	}
1914 
1915 	skb_checksum_none_assert(skb);
1916 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1917 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1918 		skb->csum_level =
1919 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1920 	}
1921 
1922 	if (gro)
1923 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1924 
1925 	return skb;
1926 }
1927 
1928 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1929 			 struct rx_agg_cmp *rx_agg)
1930 {
1931 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1932 	struct bnxt_tpa_info *tpa_info;
1933 
1934 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1935 	tpa_info = &rxr->rx_tpa[agg_id];
1936 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1937 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1938 }
1939 
1940 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1941 			     struct sk_buff *skb)
1942 {
1943 	skb_mark_for_recycle(skb);
1944 
1945 	if (skb->dev != bp->dev) {
1946 		/* this packet belongs to a vf-rep */
1947 		bnxt_vf_rep_rx(bp, skb);
1948 		return;
1949 	}
1950 	skb_record_rx_queue(skb, bnapi->index);
1951 	napi_gro_receive(&bnapi->napi, skb);
1952 }
1953 
1954 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
1955 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
1956 {
1957 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1958 
1959 	if (BNXT_PTP_RX_TS_VALID(flags))
1960 		goto ts_valid;
1961 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
1962 		return false;
1963 
1964 ts_valid:
1965 	*cmpl_ts = ts;
1966 	return true;
1967 }
1968 
1969 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
1970 				    struct rx_cmp *rxcmp,
1971 				    struct rx_cmp_ext *rxcmp1)
1972 {
1973 	__be16 vlan_proto;
1974 	u16 vtag;
1975 
1976 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1977 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
1978 		u32 meta_data;
1979 
1980 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
1981 			return skb;
1982 
1983 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1984 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1985 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
1986 		if (eth_type_vlan(vlan_proto))
1987 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1988 		else
1989 			goto vlan_err;
1990 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
1991 		if (RX_CMP_VLAN_VALID(rxcmp)) {
1992 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
1993 
1994 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
1995 				vlan_proto = htons(ETH_P_8021Q);
1996 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
1997 				vlan_proto = htons(ETH_P_8021AD);
1998 			else
1999 				goto vlan_err;
2000 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
2001 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2002 		}
2003 	}
2004 	return skb;
2005 vlan_err:
2006 	dev_kfree_skb(skb);
2007 	return NULL;
2008 }
2009 
2010 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
2011 					   struct rx_cmp *rxcmp)
2012 {
2013 	u8 ext_op;
2014 
2015 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
2016 	switch (ext_op) {
2017 	case EXT_OP_INNER_4:
2018 	case EXT_OP_OUTER_4:
2019 	case EXT_OP_INNFL_3:
2020 	case EXT_OP_OUTFL_3:
2021 		return PKT_HASH_TYPE_L4;
2022 	default:
2023 		return PKT_HASH_TYPE_L3;
2024 	}
2025 }
2026 
2027 /* returns the following:
2028  * 1       - 1 packet successfully received
2029  * 0       - successful TPA_START, packet not completed yet
2030  * -EBUSY  - completion ring does not have all the agg buffers yet
2031  * -ENOMEM - packet aborted due to out of memory
2032  * -EIO    - packet aborted due to hw error indicated in BD
2033  */
2034 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2035 		       u32 *raw_cons, u8 *event)
2036 {
2037 	struct bnxt_napi *bnapi = cpr->bnapi;
2038 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2039 	struct net_device *dev = bp->dev;
2040 	struct rx_cmp *rxcmp;
2041 	struct rx_cmp_ext *rxcmp1;
2042 	u32 tmp_raw_cons = *raw_cons;
2043 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2044 	struct bnxt_sw_rx_bd *rx_buf;
2045 	unsigned int len;
2046 	u8 *data_ptr, agg_bufs, cmp_type;
2047 	bool xdp_active = false;
2048 	dma_addr_t dma_addr;
2049 	struct sk_buff *skb;
2050 	struct xdp_buff xdp;
2051 	u32 flags, misc;
2052 	u32 cmpl_ts;
2053 	void *data;
2054 	int rc = 0;
2055 
2056 	rxcmp = (struct rx_cmp *)
2057 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2058 
2059 	cmp_type = RX_CMP_TYPE(rxcmp);
2060 
2061 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2062 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2063 		goto next_rx_no_prod_no_len;
2064 	}
2065 
2066 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2067 	cp_cons = RING_CMP(tmp_raw_cons);
2068 	rxcmp1 = (struct rx_cmp_ext *)
2069 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2070 
2071 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2072 		return -EBUSY;
2073 
2074 	/* The valid test of the entry must be done first before
2075 	 * reading any further.
2076 	 */
2077 	dma_rmb();
2078 	prod = rxr->rx_prod;
2079 
2080 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2081 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2082 		bnxt_tpa_start(bp, rxr, cmp_type,
2083 			       (struct rx_tpa_start_cmp *)rxcmp,
2084 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2085 
2086 		*event |= BNXT_RX_EVENT;
2087 		goto next_rx_no_prod_no_len;
2088 
2089 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2090 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2091 				   (struct rx_tpa_end_cmp *)rxcmp,
2092 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2093 
2094 		if (IS_ERR(skb))
2095 			return -EBUSY;
2096 
2097 		rc = -ENOMEM;
2098 		if (likely(skb)) {
2099 			bnxt_deliver_skb(bp, bnapi, skb);
2100 			rc = 1;
2101 		}
2102 		*event |= BNXT_RX_EVENT;
2103 		goto next_rx_no_prod_no_len;
2104 	}
2105 
2106 	cons = rxcmp->rx_cmp_opaque;
2107 	if (unlikely(cons != rxr->rx_next_cons)) {
2108 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2109 
2110 		/* 0xffff is forced error, don't print it */
2111 		if (rxr->rx_next_cons != 0xffff)
2112 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2113 				    cons, rxr->rx_next_cons);
2114 		bnxt_sched_reset_rxr(bp, rxr);
2115 		if (rc1)
2116 			return rc1;
2117 		goto next_rx_no_prod_no_len;
2118 	}
2119 	rx_buf = &rxr->rx_buf_ring[cons];
2120 	data = rx_buf->data;
2121 	data_ptr = rx_buf->data_ptr;
2122 	prefetch(data_ptr);
2123 
2124 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2125 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2126 
2127 	if (agg_bufs) {
2128 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2129 			return -EBUSY;
2130 
2131 		cp_cons = NEXT_CMP(cp_cons);
2132 		*event |= BNXT_AGG_EVENT;
2133 	}
2134 	*event |= BNXT_RX_EVENT;
2135 
2136 	rx_buf->data = NULL;
2137 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2138 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2139 
2140 		bnxt_reuse_rx_data(rxr, cons, data);
2141 		if (agg_bufs)
2142 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2143 					       false);
2144 
2145 		rc = -EIO;
2146 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2147 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2148 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2149 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2150 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2151 						 rx_err);
2152 				bnxt_sched_reset_rxr(bp, rxr);
2153 			}
2154 		}
2155 		goto next_rx_no_len;
2156 	}
2157 
2158 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2159 	len = flags >> RX_CMP_LEN_SHIFT;
2160 	dma_addr = rx_buf->mapping;
2161 
2162 	if (bnxt_xdp_attached(bp, rxr)) {
2163 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2164 		if (agg_bufs) {
2165 			u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
2166 							     cp_cons, agg_bufs,
2167 							     false);
2168 			if (!frag_len)
2169 				goto oom_next_rx;
2170 		}
2171 		xdp_active = true;
2172 	}
2173 
2174 	if (xdp_active) {
2175 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2176 			rc = 1;
2177 			goto next_rx;
2178 		}
2179 	}
2180 
2181 	if (len <= bp->rx_copybreak) {
2182 		if (!xdp_active)
2183 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2184 		else
2185 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2186 		bnxt_reuse_rx_data(rxr, cons, data);
2187 		if (!skb) {
2188 			if (agg_bufs) {
2189 				if (!xdp_active)
2190 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2191 							       agg_bufs, false);
2192 				else
2193 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2194 			}
2195 			goto oom_next_rx;
2196 		}
2197 	} else {
2198 		u32 payload;
2199 
2200 		if (rx_buf->data_ptr == data_ptr)
2201 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2202 		else
2203 			payload = 0;
2204 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2205 				      payload | len);
2206 		if (!skb)
2207 			goto oom_next_rx;
2208 	}
2209 
2210 	if (agg_bufs) {
2211 		if (!xdp_active) {
2212 			skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
2213 			if (!skb)
2214 				goto oom_next_rx;
2215 		} else {
2216 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1);
2217 			if (!skb) {
2218 				/* we should be able to free the old skb here */
2219 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2220 				goto oom_next_rx;
2221 			}
2222 		}
2223 	}
2224 
2225 	if (RX_CMP_HASH_VALID(rxcmp)) {
2226 		enum pkt_hash_types type;
2227 
2228 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2229 			type = bnxt_rss_ext_op(bp, rxcmp);
2230 		} else {
2231 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2232 
2233 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2234 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2235 				type = PKT_HASH_TYPE_L4;
2236 			else
2237 				type = PKT_HASH_TYPE_L3;
2238 		}
2239 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2240 	}
2241 
2242 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2243 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2244 	skb->protocol = eth_type_trans(skb, dev);
2245 
2246 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2247 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2248 		if (!skb)
2249 			goto next_rx;
2250 	}
2251 
2252 	skb_checksum_none_assert(skb);
2253 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2254 		if (dev->features & NETIF_F_RXCSUM) {
2255 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2256 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2257 		}
2258 	} else {
2259 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2260 			if (dev->features & NETIF_F_RXCSUM)
2261 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2262 		}
2263 	}
2264 
2265 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2266 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2267 			u64 ns, ts;
2268 
2269 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2270 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2271 
2272 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2273 				memset(skb_hwtstamps(skb), 0,
2274 				       sizeof(*skb_hwtstamps(skb)));
2275 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2276 			}
2277 		}
2278 	}
2279 	bnxt_deliver_skb(bp, bnapi, skb);
2280 	rc = 1;
2281 
2282 next_rx:
2283 	cpr->rx_packets += 1;
2284 	cpr->rx_bytes += len;
2285 
2286 next_rx_no_len:
2287 	rxr->rx_prod = NEXT_RX(prod);
2288 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2289 
2290 next_rx_no_prod_no_len:
2291 	*raw_cons = tmp_raw_cons;
2292 
2293 	return rc;
2294 
2295 oom_next_rx:
2296 	cpr->sw_stats->rx.rx_oom_discards += 1;
2297 	rc = -ENOMEM;
2298 	goto next_rx;
2299 }
2300 
2301 /* In netpoll mode, if we are using a combined completion ring, we need to
2302  * discard the rx packets and recycle the buffers.
2303  */
2304 static int bnxt_force_rx_discard(struct bnxt *bp,
2305 				 struct bnxt_cp_ring_info *cpr,
2306 				 u32 *raw_cons, u8 *event)
2307 {
2308 	u32 tmp_raw_cons = *raw_cons;
2309 	struct rx_cmp_ext *rxcmp1;
2310 	struct rx_cmp *rxcmp;
2311 	u16 cp_cons;
2312 	u8 cmp_type;
2313 	int rc;
2314 
2315 	cp_cons = RING_CMP(tmp_raw_cons);
2316 	rxcmp = (struct rx_cmp *)
2317 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2318 
2319 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2320 	cp_cons = RING_CMP(tmp_raw_cons);
2321 	rxcmp1 = (struct rx_cmp_ext *)
2322 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2323 
2324 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2325 		return -EBUSY;
2326 
2327 	/* The valid test of the entry must be done first before
2328 	 * reading any further.
2329 	 */
2330 	dma_rmb();
2331 	cmp_type = RX_CMP_TYPE(rxcmp);
2332 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2333 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2334 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2335 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2336 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2337 		struct rx_tpa_end_cmp_ext *tpa_end1;
2338 
2339 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2340 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2341 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2342 	}
2343 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2344 	if (rc && rc != -EBUSY)
2345 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2346 	return rc;
2347 }
2348 
2349 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2350 {
2351 	struct bnxt_fw_health *fw_health = bp->fw_health;
2352 	u32 reg = fw_health->regs[reg_idx];
2353 	u32 reg_type, reg_off, val = 0;
2354 
2355 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2356 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2357 	switch (reg_type) {
2358 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2359 		pci_read_config_dword(bp->pdev, reg_off, &val);
2360 		break;
2361 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2362 		reg_off = fw_health->mapped_regs[reg_idx];
2363 		fallthrough;
2364 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2365 		val = readl(bp->bar0 + reg_off);
2366 		break;
2367 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2368 		val = readl(bp->bar1 + reg_off);
2369 		break;
2370 	}
2371 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2372 		val &= fw_health->fw_reset_inprog_reg_mask;
2373 	return val;
2374 }
2375 
2376 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2377 {
2378 	int i;
2379 
2380 	for (i = 0; i < bp->rx_nr_rings; i++) {
2381 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2382 		struct bnxt_ring_grp_info *grp_info;
2383 
2384 		grp_info = &bp->grp_info[grp_idx];
2385 		if (grp_info->agg_fw_ring_id == ring_id)
2386 			return grp_idx;
2387 	}
2388 	return INVALID_HW_RING_ID;
2389 }
2390 
2391 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2392 {
2393 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2394 
2395 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2396 		return link_info->force_link_speed2;
2397 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2398 		return link_info->force_pam4_link_speed;
2399 	return link_info->force_link_speed;
2400 }
2401 
2402 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2403 {
2404 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2405 
2406 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2407 		link_info->req_link_speed = link_info->force_link_speed2;
2408 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2409 		switch (link_info->req_link_speed) {
2410 		case BNXT_LINK_SPEED_50GB_PAM4:
2411 		case BNXT_LINK_SPEED_100GB_PAM4:
2412 		case BNXT_LINK_SPEED_200GB_PAM4:
2413 		case BNXT_LINK_SPEED_400GB_PAM4:
2414 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2415 			break;
2416 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2417 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2418 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2419 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2420 			break;
2421 		default:
2422 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2423 		}
2424 		return;
2425 	}
2426 	link_info->req_link_speed = link_info->force_link_speed;
2427 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2428 	if (link_info->force_pam4_link_speed) {
2429 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2430 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2431 	}
2432 }
2433 
2434 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2435 {
2436 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2437 
2438 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2439 		link_info->advertising = link_info->auto_link_speeds2;
2440 		return;
2441 	}
2442 	link_info->advertising = link_info->auto_link_speeds;
2443 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2444 }
2445 
2446 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2447 {
2448 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2449 
2450 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2451 		if (link_info->req_link_speed != link_info->force_link_speed2)
2452 			return true;
2453 		return false;
2454 	}
2455 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2456 	    link_info->req_link_speed != link_info->force_link_speed)
2457 		return true;
2458 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2459 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2460 		return true;
2461 	return false;
2462 }
2463 
2464 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2465 {
2466 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2467 
2468 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2469 		if (link_info->advertising != link_info->auto_link_speeds2)
2470 			return true;
2471 		return false;
2472 	}
2473 	if (link_info->advertising != link_info->auto_link_speeds ||
2474 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2475 		return true;
2476 	return false;
2477 }
2478 
2479 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2480 {
2481 	u32 flags = bp->ctx->ctx_arr[type].flags;
2482 
2483 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2484 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2485 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2486 }
2487 
2488 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2489 {
2490 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2491 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2492 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2493 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2494 	struct bnxt_bs_trace_info *bs_trace;
2495 	int last_pg;
2496 
2497 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2498 		return;
2499 
2500 	mem_size = ctxm->max_entries * ctxm->entry_size;
2501 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2502 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2503 
2504 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2505 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2506 
2507 	rmem = &ctx_pg[0].ring_mem;
2508 	bs_trace = &bp->bs_trace[trace_type];
2509 	bs_trace->ctx_type = ctxm->type;
2510 	bs_trace->trace_type = trace_type;
2511 	if (pages > MAX_CTX_PAGES) {
2512 		int last_pg_dir = rmem->nr_pages - 1;
2513 
2514 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2515 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2516 	} else {
2517 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2518 	}
2519 	bs_trace->magic_byte += magic_byte_offset;
2520 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2521 }
2522 
2523 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2524 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2525 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2526 
2527 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2528 	(((data2) &							\
2529 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2530 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2531 
2532 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2533 	((data2) &							\
2534 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2535 
2536 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2537 	(((data2) &							\
2538 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2539 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2540 
2541 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2542 	((data1) &							\
2543 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2544 
2545 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2546 	(((data1) &							\
2547 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2548 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2549 
2550 /* Return true if the workqueue has to be scheduled */
2551 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2552 {
2553 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2554 
2555 	switch (err_type) {
2556 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2557 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2558 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2559 		break;
2560 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2561 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2562 		break;
2563 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2564 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2565 		break;
2566 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2567 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2568 		char *threshold_type;
2569 		bool notify = false;
2570 		char *dir_str;
2571 
2572 		switch (type) {
2573 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2574 			threshold_type = "warning";
2575 			break;
2576 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2577 			threshold_type = "critical";
2578 			break;
2579 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2580 			threshold_type = "fatal";
2581 			break;
2582 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2583 			threshold_type = "shutdown";
2584 			break;
2585 		default:
2586 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2587 			return false;
2588 		}
2589 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2590 			dir_str = "above";
2591 			notify = true;
2592 		} else {
2593 			dir_str = "below";
2594 		}
2595 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2596 			    dir_str, threshold_type);
2597 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2598 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2599 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2600 		if (notify) {
2601 			bp->thermal_threshold_type = type;
2602 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2603 			return true;
2604 		}
2605 		return false;
2606 	}
2607 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2608 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2609 		break;
2610 	default:
2611 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2612 			   err_type);
2613 		break;
2614 	}
2615 	return false;
2616 }
2617 
2618 #define BNXT_GET_EVENT_PORT(data)	\
2619 	((data) &			\
2620 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2621 
2622 #define BNXT_EVENT_RING_TYPE(data2)	\
2623 	((data2) &			\
2624 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2625 
2626 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2627 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2628 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2629 
2630 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2631 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2632 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2633 
2634 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2635 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2636 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2637 
2638 #define BNXT_PHC_BITS	48
2639 
2640 static int bnxt_async_event_process(struct bnxt *bp,
2641 				    struct hwrm_async_event_cmpl *cmpl)
2642 {
2643 	u16 event_id = le16_to_cpu(cmpl->event_id);
2644 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2645 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2646 
2647 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2648 		   event_id, data1, data2);
2649 
2650 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2651 	switch (event_id) {
2652 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2653 		struct bnxt_link_info *link_info = &bp->link_info;
2654 
2655 		if (BNXT_VF(bp))
2656 			goto async_event_process_exit;
2657 
2658 		/* print unsupported speed warning in forced speed mode only */
2659 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2660 		    (data1 & 0x20000)) {
2661 			u16 fw_speed = bnxt_get_force_speed(link_info);
2662 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2663 
2664 			if (speed != SPEED_UNKNOWN)
2665 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2666 					    speed);
2667 		}
2668 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2669 	}
2670 		fallthrough;
2671 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2672 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2673 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2674 		fallthrough;
2675 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2676 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2677 		break;
2678 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2679 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2680 		break;
2681 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2682 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2683 
2684 		if (BNXT_VF(bp))
2685 			break;
2686 
2687 		if (bp->pf.port_id != port_id)
2688 			break;
2689 
2690 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2691 		break;
2692 	}
2693 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2694 		if (BNXT_PF(bp))
2695 			goto async_event_process_exit;
2696 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2697 		break;
2698 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2699 		char *type_str = "Solicited";
2700 
2701 		if (!bp->fw_health)
2702 			goto async_event_process_exit;
2703 
2704 		bp->fw_reset_timestamp = jiffies;
2705 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2706 		if (!bp->fw_reset_min_dsecs)
2707 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2708 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2709 		if (!bp->fw_reset_max_dsecs)
2710 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2711 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2712 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2713 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2714 			type_str = "Fatal";
2715 			bp->fw_health->fatalities++;
2716 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2717 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2718 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2719 			type_str = "Non-fatal";
2720 			bp->fw_health->survivals++;
2721 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2722 		}
2723 		netif_warn(bp, hw, bp->dev,
2724 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2725 			   type_str, data1, data2,
2726 			   bp->fw_reset_min_dsecs * 100,
2727 			   bp->fw_reset_max_dsecs * 100);
2728 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2729 		break;
2730 	}
2731 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2732 		struct bnxt_fw_health *fw_health = bp->fw_health;
2733 		char *status_desc = "healthy";
2734 		u32 status;
2735 
2736 		if (!fw_health)
2737 			goto async_event_process_exit;
2738 
2739 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2740 			fw_health->enabled = false;
2741 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2742 			break;
2743 		}
2744 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2745 		fw_health->tmr_multiplier =
2746 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2747 				     bp->current_interval * 10);
2748 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2749 		if (!fw_health->enabled)
2750 			fw_health->last_fw_heartbeat =
2751 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2752 		fw_health->last_fw_reset_cnt =
2753 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2754 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2755 		if (status != BNXT_FW_STATUS_HEALTHY)
2756 			status_desc = "unhealthy";
2757 		netif_info(bp, drv, bp->dev,
2758 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2759 			   fw_health->primary ? "primary" : "backup", status,
2760 			   status_desc, fw_health->last_fw_reset_cnt);
2761 		if (!fw_health->enabled) {
2762 			/* Make sure tmr_counter is set and visible to
2763 			 * bnxt_health_check() before setting enabled to true.
2764 			 */
2765 			smp_wmb();
2766 			fw_health->enabled = true;
2767 		}
2768 		goto async_event_process_exit;
2769 	}
2770 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2771 		netif_notice(bp, hw, bp->dev,
2772 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2773 			     data1, data2);
2774 		goto async_event_process_exit;
2775 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2776 		struct bnxt_rx_ring_info *rxr;
2777 		u16 grp_idx;
2778 
2779 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2780 			goto async_event_process_exit;
2781 
2782 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2783 			    BNXT_EVENT_RING_TYPE(data2), data1);
2784 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2785 			goto async_event_process_exit;
2786 
2787 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2788 		if (grp_idx == INVALID_HW_RING_ID) {
2789 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2790 				    data1);
2791 			goto async_event_process_exit;
2792 		}
2793 		rxr = bp->bnapi[grp_idx]->rx_ring;
2794 		bnxt_sched_reset_rxr(bp, rxr);
2795 		goto async_event_process_exit;
2796 	}
2797 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2798 		struct bnxt_fw_health *fw_health = bp->fw_health;
2799 
2800 		netif_notice(bp, hw, bp->dev,
2801 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2802 			     data1, data2);
2803 		if (fw_health) {
2804 			fw_health->echo_req_data1 = data1;
2805 			fw_health->echo_req_data2 = data2;
2806 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2807 			break;
2808 		}
2809 		goto async_event_process_exit;
2810 	}
2811 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2812 		bnxt_ptp_pps_event(bp, data1, data2);
2813 		goto async_event_process_exit;
2814 	}
2815 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2816 		if (bnxt_event_error_report(bp, data1, data2))
2817 			break;
2818 		goto async_event_process_exit;
2819 	}
2820 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2821 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2822 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2823 			if (BNXT_PTP_USE_RTC(bp)) {
2824 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2825 				unsigned long flags;
2826 				u64 ns;
2827 
2828 				if (!ptp)
2829 					goto async_event_process_exit;
2830 
2831 				bnxt_ptp_update_current_time(bp);
2832 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2833 				       BNXT_PHC_BITS) | ptp->current_time);
2834 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2835 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2836 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2837 			}
2838 			break;
2839 		}
2840 		goto async_event_process_exit;
2841 	}
2842 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2843 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2844 
2845 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2846 		goto async_event_process_exit;
2847 	}
2848 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2849 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2850 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2851 
2852 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2853 		goto async_event_process_exit;
2854 	}
2855 	default:
2856 		goto async_event_process_exit;
2857 	}
2858 	__bnxt_queue_sp_work(bp);
2859 async_event_process_exit:
2860 	bnxt_ulp_async_events(bp, cmpl);
2861 	return 0;
2862 }
2863 
2864 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2865 {
2866 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2867 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2868 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2869 				(struct hwrm_fwd_req_cmpl *)txcmp;
2870 
2871 	switch (cmpl_type) {
2872 	case CMPL_BASE_TYPE_HWRM_DONE:
2873 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2874 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2875 		break;
2876 
2877 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2878 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2879 
2880 		if ((vf_id < bp->pf.first_vf_id) ||
2881 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2882 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2883 				   vf_id);
2884 			return -EINVAL;
2885 		}
2886 
2887 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2888 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2889 		break;
2890 
2891 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2892 		bnxt_async_event_process(bp,
2893 					 (struct hwrm_async_event_cmpl *)txcmp);
2894 		break;
2895 
2896 	default:
2897 		break;
2898 	}
2899 
2900 	return 0;
2901 }
2902 
2903 static bool bnxt_vnic_is_active(struct bnxt *bp)
2904 {
2905 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
2906 
2907 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
2908 }
2909 
2910 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2911 {
2912 	struct bnxt_napi *bnapi = dev_instance;
2913 	struct bnxt *bp = bnapi->bp;
2914 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2915 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2916 
2917 	cpr->event_ctr++;
2918 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2919 	napi_schedule(&bnapi->napi);
2920 	return IRQ_HANDLED;
2921 }
2922 
2923 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2924 {
2925 	u32 raw_cons = cpr->cp_raw_cons;
2926 	u16 cons = RING_CMP(raw_cons);
2927 	struct tx_cmp *txcmp;
2928 
2929 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2930 
2931 	return TX_CMP_VALID(txcmp, raw_cons);
2932 }
2933 
2934 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2935 			    int budget)
2936 {
2937 	struct bnxt_napi *bnapi = cpr->bnapi;
2938 	u32 raw_cons = cpr->cp_raw_cons;
2939 	u32 cons;
2940 	int rx_pkts = 0;
2941 	u8 event = 0;
2942 	struct tx_cmp *txcmp;
2943 
2944 	cpr->has_more_work = 0;
2945 	cpr->had_work_done = 1;
2946 	while (1) {
2947 		u8 cmp_type;
2948 		int rc;
2949 
2950 		cons = RING_CMP(raw_cons);
2951 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2952 
2953 		if (!TX_CMP_VALID(txcmp, raw_cons))
2954 			break;
2955 
2956 		/* The valid test of the entry must be done first before
2957 		 * reading any further.
2958 		 */
2959 		dma_rmb();
2960 		cmp_type = TX_CMP_TYPE(txcmp);
2961 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
2962 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
2963 			u32 opaque = txcmp->tx_cmp_opaque;
2964 			struct bnxt_tx_ring_info *txr;
2965 			u16 tx_freed;
2966 
2967 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
2968 			event |= BNXT_TX_CMP_EVENT;
2969 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
2970 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
2971 			else
2972 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
2973 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
2974 				   bp->tx_ring_mask;
2975 			/* return full budget so NAPI will complete. */
2976 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
2977 				rx_pkts = budget;
2978 				raw_cons = NEXT_RAW_CMP(raw_cons);
2979 				if (budget)
2980 					cpr->has_more_work = 1;
2981 				break;
2982 			}
2983 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
2984 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
2985 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
2986 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2987 			if (likely(budget))
2988 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2989 			else
2990 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2991 							   &event);
2992 			if (likely(rc >= 0))
2993 				rx_pkts += rc;
2994 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2995 			 * the NAPI budget.  Otherwise, we may potentially loop
2996 			 * here forever if we consistently cannot allocate
2997 			 * buffers.
2998 			 */
2999 			else if (rc == -ENOMEM && budget)
3000 				rx_pkts++;
3001 			else if (rc == -EBUSY)	/* partial completion */
3002 				break;
3003 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3004 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3005 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3006 			bnxt_hwrm_handler(bp, txcmp);
3007 		}
3008 		raw_cons = NEXT_RAW_CMP(raw_cons);
3009 
3010 		if (rx_pkts && rx_pkts == budget) {
3011 			cpr->has_more_work = 1;
3012 			break;
3013 		}
3014 	}
3015 
3016 	if (event & BNXT_REDIRECT_EVENT) {
3017 		xdp_do_flush();
3018 		event &= ~BNXT_REDIRECT_EVENT;
3019 	}
3020 
3021 	if (event & BNXT_TX_EVENT) {
3022 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3023 		u16 prod = txr->tx_prod;
3024 
3025 		/* Sync BD data before updating doorbell */
3026 		wmb();
3027 
3028 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3029 		event &= ~BNXT_TX_EVENT;
3030 	}
3031 
3032 	cpr->cp_raw_cons = raw_cons;
3033 	bnapi->events |= event;
3034 	return rx_pkts;
3035 }
3036 
3037 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3038 				  int budget)
3039 {
3040 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3041 		bnapi->tx_int(bp, bnapi, budget);
3042 
3043 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3044 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3045 
3046 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3047 		bnapi->events &= ~BNXT_RX_EVENT;
3048 	}
3049 	if (bnapi->events & BNXT_AGG_EVENT) {
3050 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3051 
3052 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3053 		bnapi->events &= ~BNXT_AGG_EVENT;
3054 	}
3055 }
3056 
3057 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3058 			  int budget)
3059 {
3060 	struct bnxt_napi *bnapi = cpr->bnapi;
3061 	int rx_pkts;
3062 
3063 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3064 
3065 	/* ACK completion ring before freeing tx ring and producing new
3066 	 * buffers in rx/agg rings to prevent overflowing the completion
3067 	 * ring.
3068 	 */
3069 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3070 
3071 	__bnxt_poll_work_done(bp, bnapi, budget);
3072 	return rx_pkts;
3073 }
3074 
3075 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3076 {
3077 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3078 	struct bnxt *bp = bnapi->bp;
3079 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3080 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3081 	struct tx_cmp *txcmp;
3082 	struct rx_cmp_ext *rxcmp1;
3083 	u32 cp_cons, tmp_raw_cons;
3084 	u32 raw_cons = cpr->cp_raw_cons;
3085 	bool flush_xdp = false;
3086 	u32 rx_pkts = 0;
3087 	u8 event = 0;
3088 
3089 	while (1) {
3090 		int rc;
3091 
3092 		cp_cons = RING_CMP(raw_cons);
3093 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3094 
3095 		if (!TX_CMP_VALID(txcmp, raw_cons))
3096 			break;
3097 
3098 		/* The valid test of the entry must be done first before
3099 		 * reading any further.
3100 		 */
3101 		dma_rmb();
3102 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3103 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3104 			cp_cons = RING_CMP(tmp_raw_cons);
3105 			rxcmp1 = (struct rx_cmp_ext *)
3106 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3107 
3108 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3109 				break;
3110 
3111 			/* force an error to recycle the buffer */
3112 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3113 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3114 
3115 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3116 			if (likely(rc == -EIO) && budget)
3117 				rx_pkts++;
3118 			else if (rc == -EBUSY)	/* partial completion */
3119 				break;
3120 			if (event & BNXT_REDIRECT_EVENT)
3121 				flush_xdp = true;
3122 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3123 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3124 			bnxt_hwrm_handler(bp, txcmp);
3125 		} else {
3126 			netdev_err(bp->dev,
3127 				   "Invalid completion received on special ring\n");
3128 		}
3129 		raw_cons = NEXT_RAW_CMP(raw_cons);
3130 
3131 		if (rx_pkts == budget)
3132 			break;
3133 	}
3134 
3135 	cpr->cp_raw_cons = raw_cons;
3136 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3137 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3138 
3139 	if (event & BNXT_AGG_EVENT)
3140 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3141 	if (flush_xdp)
3142 		xdp_do_flush();
3143 
3144 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3145 		napi_complete_done(napi, rx_pkts);
3146 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3147 	}
3148 	return rx_pkts;
3149 }
3150 
3151 static int bnxt_poll(struct napi_struct *napi, int budget)
3152 {
3153 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3154 	struct bnxt *bp = bnapi->bp;
3155 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3156 	int work_done = 0;
3157 
3158 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3159 		napi_complete(napi);
3160 		return 0;
3161 	}
3162 	while (1) {
3163 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3164 
3165 		if (work_done >= budget) {
3166 			if (!budget)
3167 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3168 			break;
3169 		}
3170 
3171 		if (!bnxt_has_work(bp, cpr)) {
3172 			if (napi_complete_done(napi, work_done))
3173 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3174 			break;
3175 		}
3176 	}
3177 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3178 		struct dim_sample dim_sample = {};
3179 
3180 		dim_update_sample(cpr->event_ctr,
3181 				  cpr->rx_packets,
3182 				  cpr->rx_bytes,
3183 				  &dim_sample);
3184 		net_dim(&cpr->dim, &dim_sample);
3185 	}
3186 	return work_done;
3187 }
3188 
3189 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3190 {
3191 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3192 	int i, work_done = 0;
3193 
3194 	for (i = 0; i < cpr->cp_ring_count; i++) {
3195 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3196 
3197 		if (cpr2->had_nqe_notify) {
3198 			work_done += __bnxt_poll_work(bp, cpr2,
3199 						      budget - work_done);
3200 			cpr->has_more_work |= cpr2->has_more_work;
3201 		}
3202 	}
3203 	return work_done;
3204 }
3205 
3206 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3207 				 u64 dbr_type, int budget)
3208 {
3209 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3210 	int i;
3211 
3212 	for (i = 0; i < cpr->cp_ring_count; i++) {
3213 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3214 		struct bnxt_db_info *db;
3215 
3216 		if (cpr2->had_work_done) {
3217 			u32 tgl = 0;
3218 
3219 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3220 				cpr2->had_nqe_notify = 0;
3221 				tgl = cpr2->toggle;
3222 			}
3223 			db = &cpr2->cp_db;
3224 			bnxt_writeq(bp,
3225 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3226 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3227 				    db->doorbell);
3228 			cpr2->had_work_done = 0;
3229 		}
3230 	}
3231 	__bnxt_poll_work_done(bp, bnapi, budget);
3232 }
3233 
3234 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3235 {
3236 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3237 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3238 	struct bnxt_cp_ring_info *cpr_rx;
3239 	u32 raw_cons = cpr->cp_raw_cons;
3240 	struct bnxt *bp = bnapi->bp;
3241 	struct nqe_cn *nqcmp;
3242 	int work_done = 0;
3243 	u32 cons;
3244 
3245 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3246 		napi_complete(napi);
3247 		return 0;
3248 	}
3249 	if (cpr->has_more_work) {
3250 		cpr->has_more_work = 0;
3251 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3252 	}
3253 	while (1) {
3254 		u16 type;
3255 
3256 		cons = RING_CMP(raw_cons);
3257 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3258 
3259 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3260 			if (cpr->has_more_work)
3261 				break;
3262 
3263 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3264 					     budget);
3265 			cpr->cp_raw_cons = raw_cons;
3266 			if (napi_complete_done(napi, work_done))
3267 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3268 						  cpr->cp_raw_cons);
3269 			goto poll_done;
3270 		}
3271 
3272 		/* The valid test of the entry must be done first before
3273 		 * reading any further.
3274 		 */
3275 		dma_rmb();
3276 
3277 		type = le16_to_cpu(nqcmp->type);
3278 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3279 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3280 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3281 			struct bnxt_cp_ring_info *cpr2;
3282 
3283 			/* No more budget for RX work */
3284 			if (budget && work_done >= budget &&
3285 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3286 				break;
3287 
3288 			idx = BNXT_NQ_HDL_IDX(idx);
3289 			cpr2 = &cpr->cp_ring_arr[idx];
3290 			cpr2->had_nqe_notify = 1;
3291 			cpr2->toggle = NQE_CN_TOGGLE(type);
3292 			work_done += __bnxt_poll_work(bp, cpr2,
3293 						      budget - work_done);
3294 			cpr->has_more_work |= cpr2->has_more_work;
3295 		} else {
3296 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3297 		}
3298 		raw_cons = NEXT_RAW_CMP(raw_cons);
3299 	}
3300 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3301 	if (raw_cons != cpr->cp_raw_cons) {
3302 		cpr->cp_raw_cons = raw_cons;
3303 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3304 	}
3305 poll_done:
3306 	cpr_rx = &cpr->cp_ring_arr[0];
3307 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3308 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3309 		struct dim_sample dim_sample = {};
3310 
3311 		dim_update_sample(cpr->event_ctr,
3312 				  cpr_rx->rx_packets,
3313 				  cpr_rx->rx_bytes,
3314 				  &dim_sample);
3315 		net_dim(&cpr->dim, &dim_sample);
3316 	}
3317 	return work_done;
3318 }
3319 
3320 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp,
3321 				       struct bnxt_tx_ring_info *txr, int idx)
3322 {
3323 	int i, max_idx;
3324 	struct pci_dev *pdev = bp->pdev;
3325 
3326 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3327 
3328 	for (i = 0; i < max_idx;) {
3329 		struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i];
3330 		struct sk_buff *skb;
3331 		int j, last;
3332 
3333 		if (idx  < bp->tx_nr_rings_xdp &&
3334 		    tx_buf->action == XDP_REDIRECT) {
3335 			dma_unmap_single(&pdev->dev,
3336 					 dma_unmap_addr(tx_buf, mapping),
3337 					 dma_unmap_len(tx_buf, len),
3338 					 DMA_TO_DEVICE);
3339 			xdp_return_frame(tx_buf->xdpf);
3340 			tx_buf->action = 0;
3341 			tx_buf->xdpf = NULL;
3342 			i++;
3343 			continue;
3344 		}
3345 
3346 		skb = tx_buf->skb;
3347 		if (!skb) {
3348 			i++;
3349 			continue;
3350 		}
3351 
3352 		tx_buf->skb = NULL;
3353 
3354 		if (tx_buf->is_push) {
3355 			dev_kfree_skb(skb);
3356 			i += 2;
3357 			continue;
3358 		}
3359 
3360 		dma_unmap_single(&pdev->dev,
3361 				 dma_unmap_addr(tx_buf, mapping),
3362 				 skb_headlen(skb),
3363 				 DMA_TO_DEVICE);
3364 
3365 		last = tx_buf->nr_frags;
3366 		i += 2;
3367 		for (j = 0; j < last; j++, i++) {
3368 			int ring_idx = i & bp->tx_ring_mask;
3369 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
3370 
3371 			tx_buf = &txr->tx_buf_ring[ring_idx];
3372 			dma_unmap_page(&pdev->dev,
3373 				       dma_unmap_addr(tx_buf, mapping),
3374 				       skb_frag_size(frag), DMA_TO_DEVICE);
3375 		}
3376 		dev_kfree_skb(skb);
3377 	}
3378 	netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx));
3379 }
3380 
3381 static void bnxt_free_tx_skbs(struct bnxt *bp)
3382 {
3383 	int i;
3384 
3385 	if (!bp->tx_ring)
3386 		return;
3387 
3388 	for (i = 0; i < bp->tx_nr_rings; i++) {
3389 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3390 
3391 		if (!txr->tx_buf_ring)
3392 			continue;
3393 
3394 		bnxt_free_one_tx_ring_skbs(bp, txr, i);
3395 	}
3396 }
3397 
3398 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3399 {
3400 	int i, max_idx;
3401 
3402 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3403 
3404 	for (i = 0; i < max_idx; i++) {
3405 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3406 		void *data = rx_buf->data;
3407 
3408 		if (!data)
3409 			continue;
3410 
3411 		rx_buf->data = NULL;
3412 		if (BNXT_RX_PAGE_MODE(bp))
3413 			page_pool_recycle_direct(rxr->page_pool, data);
3414 		else
3415 			page_pool_free_va(rxr->head_pool, data, true);
3416 	}
3417 }
3418 
3419 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3420 {
3421 	int i, max_idx;
3422 
3423 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3424 
3425 	for (i = 0; i < max_idx; i++) {
3426 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3427 		struct page *page = rx_agg_buf->page;
3428 
3429 		if (!page)
3430 			continue;
3431 
3432 		rx_agg_buf->page = NULL;
3433 		__clear_bit(i, rxr->rx_agg_bmap);
3434 
3435 		page_pool_recycle_direct(rxr->page_pool, page);
3436 	}
3437 }
3438 
3439 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3440 					struct bnxt_rx_ring_info *rxr)
3441 {
3442 	int i;
3443 
3444 	for (i = 0; i < bp->max_tpa; i++) {
3445 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3446 		u8 *data = tpa_info->data;
3447 
3448 		if (!data)
3449 			continue;
3450 
3451 		tpa_info->data = NULL;
3452 		page_pool_free_va(rxr->head_pool, data, false);
3453 	}
3454 }
3455 
3456 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3457 				       struct bnxt_rx_ring_info *rxr)
3458 {
3459 	struct bnxt_tpa_idx_map *map;
3460 
3461 	if (!rxr->rx_tpa)
3462 		goto skip_rx_tpa_free;
3463 
3464 	bnxt_free_one_tpa_info_data(bp, rxr);
3465 
3466 skip_rx_tpa_free:
3467 	if (!rxr->rx_buf_ring)
3468 		goto skip_rx_buf_free;
3469 
3470 	bnxt_free_one_rx_ring(bp, rxr);
3471 
3472 skip_rx_buf_free:
3473 	if (!rxr->rx_agg_ring)
3474 		goto skip_rx_agg_free;
3475 
3476 	bnxt_free_one_rx_agg_ring(bp, rxr);
3477 
3478 skip_rx_agg_free:
3479 	map = rxr->rx_tpa_idx_map;
3480 	if (map)
3481 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3482 }
3483 
3484 static void bnxt_free_rx_skbs(struct bnxt *bp)
3485 {
3486 	int i;
3487 
3488 	if (!bp->rx_ring)
3489 		return;
3490 
3491 	for (i = 0; i < bp->rx_nr_rings; i++)
3492 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3493 }
3494 
3495 static void bnxt_free_skbs(struct bnxt *bp)
3496 {
3497 	bnxt_free_tx_skbs(bp);
3498 	bnxt_free_rx_skbs(bp);
3499 }
3500 
3501 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3502 {
3503 	u8 init_val = ctxm->init_value;
3504 	u16 offset = ctxm->init_offset;
3505 	u8 *p2 = p;
3506 	int i;
3507 
3508 	if (!init_val)
3509 		return;
3510 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3511 		memset(p, init_val, len);
3512 		return;
3513 	}
3514 	for (i = 0; i < len; i += ctxm->entry_size)
3515 		*(p2 + i + offset) = init_val;
3516 }
3517 
3518 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3519 			       void *buf, size_t offset, size_t head,
3520 			       size_t tail)
3521 {
3522 	int i, head_page, start_idx, source_offset;
3523 	size_t len, rem_len, total_len, max_bytes;
3524 
3525 	head_page = head / rmem->page_size;
3526 	source_offset = head % rmem->page_size;
3527 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3528 	if (!total_len)
3529 		total_len = MAX_CTX_BYTES;
3530 	start_idx = head_page % MAX_CTX_PAGES;
3531 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3532 		    source_offset;
3533 	total_len = min(total_len, max_bytes);
3534 	rem_len = total_len;
3535 
3536 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3537 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3538 		if (buf)
3539 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3540 			       len);
3541 		offset += len;
3542 		rem_len -= len;
3543 	}
3544 	return total_len;
3545 }
3546 
3547 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3548 {
3549 	struct pci_dev *pdev = bp->pdev;
3550 	int i;
3551 
3552 	if (!rmem->pg_arr)
3553 		goto skip_pages;
3554 
3555 	for (i = 0; i < rmem->nr_pages; i++) {
3556 		if (!rmem->pg_arr[i])
3557 			continue;
3558 
3559 		dma_free_coherent(&pdev->dev, rmem->page_size,
3560 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3561 
3562 		rmem->pg_arr[i] = NULL;
3563 	}
3564 skip_pages:
3565 	if (rmem->pg_tbl) {
3566 		size_t pg_tbl_size = rmem->nr_pages * 8;
3567 
3568 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3569 			pg_tbl_size = rmem->page_size;
3570 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3571 				  rmem->pg_tbl, rmem->pg_tbl_map);
3572 		rmem->pg_tbl = NULL;
3573 	}
3574 	if (rmem->vmem_size && *rmem->vmem) {
3575 		vfree(*rmem->vmem);
3576 		*rmem->vmem = NULL;
3577 	}
3578 }
3579 
3580 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3581 {
3582 	struct pci_dev *pdev = bp->pdev;
3583 	u64 valid_bit = 0;
3584 	int i;
3585 
3586 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3587 		valid_bit = PTU_PTE_VALID;
3588 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3589 		size_t pg_tbl_size = rmem->nr_pages * 8;
3590 
3591 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3592 			pg_tbl_size = rmem->page_size;
3593 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3594 						  &rmem->pg_tbl_map,
3595 						  GFP_KERNEL);
3596 		if (!rmem->pg_tbl)
3597 			return -ENOMEM;
3598 	}
3599 
3600 	for (i = 0; i < rmem->nr_pages; i++) {
3601 		u64 extra_bits = valid_bit;
3602 
3603 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3604 						     rmem->page_size,
3605 						     &rmem->dma_arr[i],
3606 						     GFP_KERNEL);
3607 		if (!rmem->pg_arr[i])
3608 			return -ENOMEM;
3609 
3610 		if (rmem->ctx_mem)
3611 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3612 					  rmem->page_size);
3613 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3614 			if (i == rmem->nr_pages - 2 &&
3615 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3616 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3617 			else if (i == rmem->nr_pages - 1 &&
3618 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3619 				extra_bits |= PTU_PTE_LAST;
3620 			rmem->pg_tbl[i] =
3621 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3622 		}
3623 	}
3624 
3625 	if (rmem->vmem_size) {
3626 		*rmem->vmem = vzalloc(rmem->vmem_size);
3627 		if (!(*rmem->vmem))
3628 			return -ENOMEM;
3629 	}
3630 	return 0;
3631 }
3632 
3633 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3634 				   struct bnxt_rx_ring_info *rxr)
3635 {
3636 	int i;
3637 
3638 	kfree(rxr->rx_tpa_idx_map);
3639 	rxr->rx_tpa_idx_map = NULL;
3640 	if (rxr->rx_tpa) {
3641 		for (i = 0; i < bp->max_tpa; i++) {
3642 			kfree(rxr->rx_tpa[i].agg_arr);
3643 			rxr->rx_tpa[i].agg_arr = NULL;
3644 		}
3645 	}
3646 	kfree(rxr->rx_tpa);
3647 	rxr->rx_tpa = NULL;
3648 }
3649 
3650 static void bnxt_free_tpa_info(struct bnxt *bp)
3651 {
3652 	int i;
3653 
3654 	for (i = 0; i < bp->rx_nr_rings; i++) {
3655 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3656 
3657 		bnxt_free_one_tpa_info(bp, rxr);
3658 	}
3659 }
3660 
3661 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3662 				   struct bnxt_rx_ring_info *rxr)
3663 {
3664 	struct rx_agg_cmp *agg;
3665 	int i;
3666 
3667 	rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3668 			      GFP_KERNEL);
3669 	if (!rxr->rx_tpa)
3670 		return -ENOMEM;
3671 
3672 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3673 		return 0;
3674 	for (i = 0; i < bp->max_tpa; i++) {
3675 		agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3676 		if (!agg)
3677 			return -ENOMEM;
3678 		rxr->rx_tpa[i].agg_arr = agg;
3679 	}
3680 	rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3681 				      GFP_KERNEL);
3682 	if (!rxr->rx_tpa_idx_map)
3683 		return -ENOMEM;
3684 
3685 	return 0;
3686 }
3687 
3688 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3689 {
3690 	int i, rc;
3691 
3692 	bp->max_tpa = MAX_TPA;
3693 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3694 		if (!bp->max_tpa_v2)
3695 			return 0;
3696 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3697 	}
3698 
3699 	for (i = 0; i < bp->rx_nr_rings; i++) {
3700 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3701 
3702 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3703 		if (rc)
3704 			return rc;
3705 	}
3706 	return 0;
3707 }
3708 
3709 static void bnxt_free_rx_rings(struct bnxt *bp)
3710 {
3711 	int i;
3712 
3713 	if (!bp->rx_ring)
3714 		return;
3715 
3716 	bnxt_free_tpa_info(bp);
3717 	for (i = 0; i < bp->rx_nr_rings; i++) {
3718 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3719 		struct bnxt_ring_struct *ring;
3720 
3721 		if (rxr->xdp_prog)
3722 			bpf_prog_put(rxr->xdp_prog);
3723 
3724 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3725 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3726 
3727 		page_pool_destroy(rxr->page_pool);
3728 		if (bnxt_separate_head_pool())
3729 			page_pool_destroy(rxr->head_pool);
3730 		rxr->page_pool = rxr->head_pool = NULL;
3731 
3732 		kfree(rxr->rx_agg_bmap);
3733 		rxr->rx_agg_bmap = NULL;
3734 
3735 		ring = &rxr->rx_ring_struct;
3736 		bnxt_free_ring(bp, &ring->ring_mem);
3737 
3738 		ring = &rxr->rx_agg_ring_struct;
3739 		bnxt_free_ring(bp, &ring->ring_mem);
3740 	}
3741 }
3742 
3743 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3744 				   struct bnxt_rx_ring_info *rxr,
3745 				   int numa_node)
3746 {
3747 	struct page_pool_params pp = { 0 };
3748 	struct page_pool *pool;
3749 
3750 	pp.pool_size = bp->rx_agg_ring_size;
3751 	if (BNXT_RX_PAGE_MODE(bp))
3752 		pp.pool_size += bp->rx_ring_size;
3753 	pp.nid = numa_node;
3754 	pp.napi = &rxr->bnapi->napi;
3755 	pp.netdev = bp->dev;
3756 	pp.dev = &bp->pdev->dev;
3757 	pp.dma_dir = bp->rx_dir;
3758 	pp.max_len = PAGE_SIZE;
3759 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3760 
3761 	pool = page_pool_create(&pp);
3762 	if (IS_ERR(pool))
3763 		return PTR_ERR(pool);
3764 	rxr->page_pool = pool;
3765 
3766 	if (bnxt_separate_head_pool()) {
3767 		pp.pool_size = max(bp->rx_ring_size, 1024);
3768 		pool = page_pool_create(&pp);
3769 		if (IS_ERR(pool))
3770 			goto err_destroy_pp;
3771 	}
3772 	rxr->head_pool = pool;
3773 
3774 	return 0;
3775 
3776 err_destroy_pp:
3777 	page_pool_destroy(rxr->page_pool);
3778 	rxr->page_pool = NULL;
3779 	return PTR_ERR(pool);
3780 }
3781 
3782 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3783 {
3784 	u16 mem_size;
3785 
3786 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3787 	mem_size = rxr->rx_agg_bmap_size / 8;
3788 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3789 	if (!rxr->rx_agg_bmap)
3790 		return -ENOMEM;
3791 
3792 	return 0;
3793 }
3794 
3795 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3796 {
3797 	int numa_node = dev_to_node(&bp->pdev->dev);
3798 	int i, rc = 0, agg_rings = 0, cpu;
3799 
3800 	if (!bp->rx_ring)
3801 		return -ENOMEM;
3802 
3803 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3804 		agg_rings = 1;
3805 
3806 	for (i = 0; i < bp->rx_nr_rings; i++) {
3807 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3808 		struct bnxt_ring_struct *ring;
3809 		int cpu_node;
3810 
3811 		ring = &rxr->rx_ring_struct;
3812 
3813 		cpu = cpumask_local_spread(i, numa_node);
3814 		cpu_node = cpu_to_node(cpu);
3815 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3816 			   i, cpu_node);
3817 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3818 		if (rc)
3819 			return rc;
3820 
3821 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3822 		if (rc < 0)
3823 			return rc;
3824 
3825 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3826 						MEM_TYPE_PAGE_POOL,
3827 						rxr->page_pool);
3828 		if (rc) {
3829 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3830 			return rc;
3831 		}
3832 
3833 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3834 		if (rc)
3835 			return rc;
3836 
3837 		ring->grp_idx = i;
3838 		if (agg_rings) {
3839 			ring = &rxr->rx_agg_ring_struct;
3840 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3841 			if (rc)
3842 				return rc;
3843 
3844 			ring->grp_idx = i;
3845 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3846 			if (rc)
3847 				return rc;
3848 		}
3849 	}
3850 	if (bp->flags & BNXT_FLAG_TPA)
3851 		rc = bnxt_alloc_tpa_info(bp);
3852 	return rc;
3853 }
3854 
3855 static void bnxt_free_tx_rings(struct bnxt *bp)
3856 {
3857 	int i;
3858 	struct pci_dev *pdev = bp->pdev;
3859 
3860 	if (!bp->tx_ring)
3861 		return;
3862 
3863 	for (i = 0; i < bp->tx_nr_rings; i++) {
3864 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3865 		struct bnxt_ring_struct *ring;
3866 
3867 		if (txr->tx_push) {
3868 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3869 					  txr->tx_push, txr->tx_push_mapping);
3870 			txr->tx_push = NULL;
3871 		}
3872 
3873 		ring = &txr->tx_ring_struct;
3874 
3875 		bnxt_free_ring(bp, &ring->ring_mem);
3876 	}
3877 }
3878 
3879 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3880 	((tc) * (bp)->tx_nr_rings_per_tc)
3881 
3882 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3883 	((tx) % (bp)->tx_nr_rings_per_tc)
3884 
3885 #define BNXT_RING_TO_TC(bp, tx)		\
3886 	((tx) / (bp)->tx_nr_rings_per_tc)
3887 
3888 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3889 {
3890 	int i, j, rc;
3891 	struct pci_dev *pdev = bp->pdev;
3892 
3893 	bp->tx_push_size = 0;
3894 	if (bp->tx_push_thresh) {
3895 		int push_size;
3896 
3897 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3898 					bp->tx_push_thresh);
3899 
3900 		if (push_size > 256) {
3901 			push_size = 0;
3902 			bp->tx_push_thresh = 0;
3903 		}
3904 
3905 		bp->tx_push_size = push_size;
3906 	}
3907 
3908 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3909 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3910 		struct bnxt_ring_struct *ring;
3911 		u8 qidx;
3912 
3913 		ring = &txr->tx_ring_struct;
3914 
3915 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3916 		if (rc)
3917 			return rc;
3918 
3919 		ring->grp_idx = txr->bnapi->index;
3920 		if (bp->tx_push_size) {
3921 			dma_addr_t mapping;
3922 
3923 			/* One pre-allocated DMA buffer to backup
3924 			 * TX push operation
3925 			 */
3926 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3927 						bp->tx_push_size,
3928 						&txr->tx_push_mapping,
3929 						GFP_KERNEL);
3930 
3931 			if (!txr->tx_push)
3932 				return -ENOMEM;
3933 
3934 			mapping = txr->tx_push_mapping +
3935 				sizeof(struct tx_push_bd);
3936 			txr->data_mapping = cpu_to_le64(mapping);
3937 		}
3938 		qidx = bp->tc_to_qidx[j];
3939 		ring->queue_id = bp->q_info[qidx].queue_id;
3940 		spin_lock_init(&txr->xdp_tx_lock);
3941 		if (i < bp->tx_nr_rings_xdp)
3942 			continue;
3943 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
3944 			j++;
3945 	}
3946 	return 0;
3947 }
3948 
3949 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3950 {
3951 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3952 
3953 	kfree(cpr->cp_desc_ring);
3954 	cpr->cp_desc_ring = NULL;
3955 	ring->ring_mem.pg_arr = NULL;
3956 	kfree(cpr->cp_desc_mapping);
3957 	cpr->cp_desc_mapping = NULL;
3958 	ring->ring_mem.dma_arr = NULL;
3959 }
3960 
3961 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3962 {
3963 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3964 	if (!cpr->cp_desc_ring)
3965 		return -ENOMEM;
3966 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3967 				       GFP_KERNEL);
3968 	if (!cpr->cp_desc_mapping)
3969 		return -ENOMEM;
3970 	return 0;
3971 }
3972 
3973 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3974 {
3975 	int i;
3976 
3977 	if (!bp->bnapi)
3978 		return;
3979 	for (i = 0; i < bp->cp_nr_rings; i++) {
3980 		struct bnxt_napi *bnapi = bp->bnapi[i];
3981 
3982 		if (!bnapi)
3983 			continue;
3984 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3985 	}
3986 }
3987 
3988 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3989 {
3990 	int i, n = bp->cp_nr_pages;
3991 
3992 	for (i = 0; i < bp->cp_nr_rings; i++) {
3993 		struct bnxt_napi *bnapi = bp->bnapi[i];
3994 		int rc;
3995 
3996 		if (!bnapi)
3997 			continue;
3998 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3999 		if (rc)
4000 			return rc;
4001 	}
4002 	return 0;
4003 }
4004 
4005 static void bnxt_free_cp_rings(struct bnxt *bp)
4006 {
4007 	int i;
4008 
4009 	if (!bp->bnapi)
4010 		return;
4011 
4012 	for (i = 0; i < bp->cp_nr_rings; i++) {
4013 		struct bnxt_napi *bnapi = bp->bnapi[i];
4014 		struct bnxt_cp_ring_info *cpr;
4015 		struct bnxt_ring_struct *ring;
4016 		int j;
4017 
4018 		if (!bnapi)
4019 			continue;
4020 
4021 		cpr = &bnapi->cp_ring;
4022 		ring = &cpr->cp_ring_struct;
4023 
4024 		bnxt_free_ring(bp, &ring->ring_mem);
4025 
4026 		if (!cpr->cp_ring_arr)
4027 			continue;
4028 
4029 		for (j = 0; j < cpr->cp_ring_count; j++) {
4030 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4031 
4032 			ring = &cpr2->cp_ring_struct;
4033 			bnxt_free_ring(bp, &ring->ring_mem);
4034 			bnxt_free_cp_arrays(cpr2);
4035 		}
4036 		kfree(cpr->cp_ring_arr);
4037 		cpr->cp_ring_arr = NULL;
4038 		cpr->cp_ring_count = 0;
4039 	}
4040 }
4041 
4042 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4043 				  struct bnxt_cp_ring_info *cpr)
4044 {
4045 	struct bnxt_ring_mem_info *rmem;
4046 	struct bnxt_ring_struct *ring;
4047 	int rc;
4048 
4049 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4050 	if (rc) {
4051 		bnxt_free_cp_arrays(cpr);
4052 		return -ENOMEM;
4053 	}
4054 	ring = &cpr->cp_ring_struct;
4055 	rmem = &ring->ring_mem;
4056 	rmem->nr_pages = bp->cp_nr_pages;
4057 	rmem->page_size = HW_CMPD_RING_SIZE;
4058 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4059 	rmem->dma_arr = cpr->cp_desc_mapping;
4060 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4061 	rc = bnxt_alloc_ring(bp, rmem);
4062 	if (rc) {
4063 		bnxt_free_ring(bp, rmem);
4064 		bnxt_free_cp_arrays(cpr);
4065 	}
4066 	return rc;
4067 }
4068 
4069 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4070 {
4071 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4072 	int i, j, rc, ulp_msix;
4073 	int tcs = bp->num_tc;
4074 
4075 	if (!tcs)
4076 		tcs = 1;
4077 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4078 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4079 		struct bnxt_napi *bnapi = bp->bnapi[i];
4080 		struct bnxt_cp_ring_info *cpr, *cpr2;
4081 		struct bnxt_ring_struct *ring;
4082 		int cp_count = 0, k;
4083 		int rx = 0, tx = 0;
4084 
4085 		if (!bnapi)
4086 			continue;
4087 
4088 		cpr = &bnapi->cp_ring;
4089 		cpr->bnapi = bnapi;
4090 		ring = &cpr->cp_ring_struct;
4091 
4092 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4093 		if (rc)
4094 			return rc;
4095 
4096 		ring->map_idx = ulp_msix + i;
4097 
4098 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4099 			continue;
4100 
4101 		if (i < bp->rx_nr_rings) {
4102 			cp_count++;
4103 			rx = 1;
4104 		}
4105 		if (i < bp->tx_nr_rings_xdp) {
4106 			cp_count++;
4107 			tx = 1;
4108 		} else if ((sh && i < bp->tx_nr_rings) ||
4109 			 (!sh && i >= bp->rx_nr_rings)) {
4110 			cp_count += tcs;
4111 			tx = 1;
4112 		}
4113 
4114 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
4115 					   GFP_KERNEL);
4116 		if (!cpr->cp_ring_arr)
4117 			return -ENOMEM;
4118 		cpr->cp_ring_count = cp_count;
4119 
4120 		for (k = 0; k < cp_count; k++) {
4121 			cpr2 = &cpr->cp_ring_arr[k];
4122 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4123 			if (rc)
4124 				return rc;
4125 			cpr2->bnapi = bnapi;
4126 			cpr2->sw_stats = cpr->sw_stats;
4127 			cpr2->cp_idx = k;
4128 			if (!k && rx) {
4129 				bp->rx_ring[i].rx_cpr = cpr2;
4130 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4131 			} else {
4132 				int n, tc = k - rx;
4133 
4134 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4135 				bp->tx_ring[n].tx_cpr = cpr2;
4136 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4137 			}
4138 		}
4139 		if (tx)
4140 			j++;
4141 	}
4142 	return 0;
4143 }
4144 
4145 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4146 				     struct bnxt_rx_ring_info *rxr)
4147 {
4148 	struct bnxt_ring_mem_info *rmem;
4149 	struct bnxt_ring_struct *ring;
4150 
4151 	ring = &rxr->rx_ring_struct;
4152 	rmem = &ring->ring_mem;
4153 	rmem->nr_pages = bp->rx_nr_pages;
4154 	rmem->page_size = HW_RXBD_RING_SIZE;
4155 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4156 	rmem->dma_arr = rxr->rx_desc_mapping;
4157 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4158 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4159 
4160 	ring = &rxr->rx_agg_ring_struct;
4161 	rmem = &ring->ring_mem;
4162 	rmem->nr_pages = bp->rx_agg_nr_pages;
4163 	rmem->page_size = HW_RXBD_RING_SIZE;
4164 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4165 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4166 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4167 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4168 }
4169 
4170 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4171 				      struct bnxt_rx_ring_info *rxr)
4172 {
4173 	struct bnxt_ring_mem_info *rmem;
4174 	struct bnxt_ring_struct *ring;
4175 	int i;
4176 
4177 	rxr->page_pool->p.napi = NULL;
4178 	rxr->page_pool = NULL;
4179 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4180 
4181 	ring = &rxr->rx_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 	ring = &rxr->rx_agg_ring_struct;
4192 	rmem = &ring->ring_mem;
4193 	rmem->pg_tbl = NULL;
4194 	rmem->pg_tbl_map = 0;
4195 	for (i = 0; i < rmem->nr_pages; i++) {
4196 		rmem->pg_arr[i] = NULL;
4197 		rmem->dma_arr[i] = 0;
4198 	}
4199 	*rmem->vmem = NULL;
4200 }
4201 
4202 static void bnxt_init_ring_struct(struct bnxt *bp)
4203 {
4204 	int i, j;
4205 
4206 	for (i = 0; i < bp->cp_nr_rings; i++) {
4207 		struct bnxt_napi *bnapi = bp->bnapi[i];
4208 		struct bnxt_ring_mem_info *rmem;
4209 		struct bnxt_cp_ring_info *cpr;
4210 		struct bnxt_rx_ring_info *rxr;
4211 		struct bnxt_tx_ring_info *txr;
4212 		struct bnxt_ring_struct *ring;
4213 
4214 		if (!bnapi)
4215 			continue;
4216 
4217 		cpr = &bnapi->cp_ring;
4218 		ring = &cpr->cp_ring_struct;
4219 		rmem = &ring->ring_mem;
4220 		rmem->nr_pages = bp->cp_nr_pages;
4221 		rmem->page_size = HW_CMPD_RING_SIZE;
4222 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4223 		rmem->dma_arr = cpr->cp_desc_mapping;
4224 		rmem->vmem_size = 0;
4225 
4226 		rxr = bnapi->rx_ring;
4227 		if (!rxr)
4228 			goto skip_rx;
4229 
4230 		ring = &rxr->rx_ring_struct;
4231 		rmem = &ring->ring_mem;
4232 		rmem->nr_pages = bp->rx_nr_pages;
4233 		rmem->page_size = HW_RXBD_RING_SIZE;
4234 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4235 		rmem->dma_arr = rxr->rx_desc_mapping;
4236 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4237 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4238 
4239 		ring = &rxr->rx_agg_ring_struct;
4240 		rmem = &ring->ring_mem;
4241 		rmem->nr_pages = bp->rx_agg_nr_pages;
4242 		rmem->page_size = HW_RXBD_RING_SIZE;
4243 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4244 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4245 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4246 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4247 
4248 skip_rx:
4249 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4250 			ring = &txr->tx_ring_struct;
4251 			rmem = &ring->ring_mem;
4252 			rmem->nr_pages = bp->tx_nr_pages;
4253 			rmem->page_size = HW_TXBD_RING_SIZE;
4254 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4255 			rmem->dma_arr = txr->tx_desc_mapping;
4256 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4257 			rmem->vmem = (void **)&txr->tx_buf_ring;
4258 		}
4259 	}
4260 }
4261 
4262 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4263 {
4264 	int i;
4265 	u32 prod;
4266 	struct rx_bd **rx_buf_ring;
4267 
4268 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4269 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4270 		int j;
4271 		struct rx_bd *rxbd;
4272 
4273 		rxbd = rx_buf_ring[i];
4274 		if (!rxbd)
4275 			continue;
4276 
4277 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4278 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4279 			rxbd->rx_bd_opaque = prod;
4280 		}
4281 	}
4282 }
4283 
4284 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4285 				       struct bnxt_rx_ring_info *rxr,
4286 				       int ring_nr)
4287 {
4288 	u32 prod;
4289 	int i;
4290 
4291 	prod = rxr->rx_prod;
4292 	for (i = 0; i < bp->rx_ring_size; i++) {
4293 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4294 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4295 				    ring_nr, i, bp->rx_ring_size);
4296 			break;
4297 		}
4298 		prod = NEXT_RX(prod);
4299 	}
4300 	rxr->rx_prod = prod;
4301 }
4302 
4303 static void bnxt_alloc_one_rx_ring_page(struct bnxt *bp,
4304 					struct bnxt_rx_ring_info *rxr,
4305 					int ring_nr)
4306 {
4307 	u32 prod;
4308 	int i;
4309 
4310 	prod = rxr->rx_agg_prod;
4311 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4312 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
4313 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4314 				    ring_nr, i, bp->rx_ring_size);
4315 			break;
4316 		}
4317 		prod = NEXT_RX_AGG(prod);
4318 	}
4319 	rxr->rx_agg_prod = prod;
4320 }
4321 
4322 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4323 					struct bnxt_rx_ring_info *rxr)
4324 {
4325 	dma_addr_t mapping;
4326 	u8 *data;
4327 	int i;
4328 
4329 	for (i = 0; i < bp->max_tpa; i++) {
4330 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4331 					    GFP_KERNEL);
4332 		if (!data)
4333 			return -ENOMEM;
4334 
4335 		rxr->rx_tpa[i].data = data;
4336 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4337 		rxr->rx_tpa[i].mapping = mapping;
4338 	}
4339 
4340 	return 0;
4341 }
4342 
4343 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4344 {
4345 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4346 	int rc;
4347 
4348 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4349 
4350 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4351 		return 0;
4352 
4353 	bnxt_alloc_one_rx_ring_page(bp, rxr, ring_nr);
4354 
4355 	if (rxr->rx_tpa) {
4356 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4357 		if (rc)
4358 			return rc;
4359 	}
4360 	return 0;
4361 }
4362 
4363 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4364 				       struct bnxt_rx_ring_info *rxr)
4365 {
4366 	struct bnxt_ring_struct *ring;
4367 	u32 type;
4368 
4369 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4370 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4371 
4372 	if (NET_IP_ALIGN == 2)
4373 		type |= RX_BD_FLAGS_SOP;
4374 
4375 	ring = &rxr->rx_ring_struct;
4376 	bnxt_init_rxbd_pages(ring, type);
4377 	ring->fw_ring_id = INVALID_HW_RING_ID;
4378 }
4379 
4380 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4381 					   struct bnxt_rx_ring_info *rxr)
4382 {
4383 	struct bnxt_ring_struct *ring;
4384 	u32 type;
4385 
4386 	ring = &rxr->rx_agg_ring_struct;
4387 	ring->fw_ring_id = INVALID_HW_RING_ID;
4388 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4389 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4390 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4391 
4392 		bnxt_init_rxbd_pages(ring, type);
4393 	}
4394 }
4395 
4396 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4397 {
4398 	struct bnxt_rx_ring_info *rxr;
4399 
4400 	rxr = &bp->rx_ring[ring_nr];
4401 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4402 
4403 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4404 			     &rxr->bnapi->napi);
4405 
4406 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4407 		bpf_prog_add(bp->xdp_prog, 1);
4408 		rxr->xdp_prog = bp->xdp_prog;
4409 	}
4410 
4411 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4412 
4413 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4414 }
4415 
4416 static void bnxt_init_cp_rings(struct bnxt *bp)
4417 {
4418 	int i, j;
4419 
4420 	for (i = 0; i < bp->cp_nr_rings; i++) {
4421 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4422 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4423 
4424 		ring->fw_ring_id = INVALID_HW_RING_ID;
4425 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4426 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4427 		if (!cpr->cp_ring_arr)
4428 			continue;
4429 		for (j = 0; j < cpr->cp_ring_count; j++) {
4430 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4431 
4432 			ring = &cpr2->cp_ring_struct;
4433 			ring->fw_ring_id = INVALID_HW_RING_ID;
4434 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4435 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4436 		}
4437 	}
4438 }
4439 
4440 static int bnxt_init_rx_rings(struct bnxt *bp)
4441 {
4442 	int i, rc = 0;
4443 
4444 	if (BNXT_RX_PAGE_MODE(bp)) {
4445 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4446 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4447 	} else {
4448 		bp->rx_offset = BNXT_RX_OFFSET;
4449 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4450 	}
4451 
4452 	for (i = 0; i < bp->rx_nr_rings; i++) {
4453 		rc = bnxt_init_one_rx_ring(bp, i);
4454 		if (rc)
4455 			break;
4456 	}
4457 
4458 	return rc;
4459 }
4460 
4461 static int bnxt_init_tx_rings(struct bnxt *bp)
4462 {
4463 	u16 i;
4464 
4465 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4466 				   BNXT_MIN_TX_DESC_CNT);
4467 
4468 	for (i = 0; i < bp->tx_nr_rings; i++) {
4469 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4470 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4471 
4472 		ring->fw_ring_id = INVALID_HW_RING_ID;
4473 
4474 		if (i >= bp->tx_nr_rings_xdp)
4475 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4476 					     NETDEV_QUEUE_TYPE_TX,
4477 					     &txr->bnapi->napi);
4478 	}
4479 
4480 	return 0;
4481 }
4482 
4483 static void bnxt_free_ring_grps(struct bnxt *bp)
4484 {
4485 	kfree(bp->grp_info);
4486 	bp->grp_info = NULL;
4487 }
4488 
4489 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4490 {
4491 	int i;
4492 
4493 	if (irq_re_init) {
4494 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4495 				       sizeof(struct bnxt_ring_grp_info),
4496 				       GFP_KERNEL);
4497 		if (!bp->grp_info)
4498 			return -ENOMEM;
4499 	}
4500 	for (i = 0; i < bp->cp_nr_rings; i++) {
4501 		if (irq_re_init)
4502 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4503 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4504 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4505 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4506 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4507 	}
4508 	return 0;
4509 }
4510 
4511 static void bnxt_free_vnics(struct bnxt *bp)
4512 {
4513 	kfree(bp->vnic_info);
4514 	bp->vnic_info = NULL;
4515 	bp->nr_vnics = 0;
4516 }
4517 
4518 static int bnxt_alloc_vnics(struct bnxt *bp)
4519 {
4520 	int num_vnics = 1;
4521 
4522 #ifdef CONFIG_RFS_ACCEL
4523 	if (bp->flags & BNXT_FLAG_RFS) {
4524 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4525 			num_vnics++;
4526 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4527 			num_vnics += bp->rx_nr_rings;
4528 	}
4529 #endif
4530 
4531 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4532 		num_vnics++;
4533 
4534 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4535 				GFP_KERNEL);
4536 	if (!bp->vnic_info)
4537 		return -ENOMEM;
4538 
4539 	bp->nr_vnics = num_vnics;
4540 	return 0;
4541 }
4542 
4543 static void bnxt_init_vnics(struct bnxt *bp)
4544 {
4545 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4546 	int i;
4547 
4548 	for (i = 0; i < bp->nr_vnics; i++) {
4549 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4550 		int j;
4551 
4552 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4553 		vnic->vnic_id = i;
4554 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4555 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4556 
4557 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4558 
4559 		if (bp->vnic_info[i].rss_hash_key) {
4560 			if (i == BNXT_VNIC_DEFAULT) {
4561 				u8 *key = (void *)vnic->rss_hash_key;
4562 				int k;
4563 
4564 				if (!bp->rss_hash_key_valid &&
4565 				    !bp->rss_hash_key_updated) {
4566 					get_random_bytes(bp->rss_hash_key,
4567 							 HW_HASH_KEY_SIZE);
4568 					bp->rss_hash_key_updated = true;
4569 				}
4570 
4571 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4572 				       HW_HASH_KEY_SIZE);
4573 
4574 				if (!bp->rss_hash_key_updated)
4575 					continue;
4576 
4577 				bp->rss_hash_key_updated = false;
4578 				bp->rss_hash_key_valid = true;
4579 
4580 				bp->toeplitz_prefix = 0;
4581 				for (k = 0; k < 8; k++) {
4582 					bp->toeplitz_prefix <<= 8;
4583 					bp->toeplitz_prefix |= key[k];
4584 				}
4585 			} else {
4586 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4587 				       HW_HASH_KEY_SIZE);
4588 			}
4589 		}
4590 	}
4591 }
4592 
4593 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4594 {
4595 	int pages;
4596 
4597 	pages = ring_size / desc_per_pg;
4598 
4599 	if (!pages)
4600 		return 1;
4601 
4602 	pages++;
4603 
4604 	while (pages & (pages - 1))
4605 		pages++;
4606 
4607 	return pages;
4608 }
4609 
4610 void bnxt_set_tpa_flags(struct bnxt *bp)
4611 {
4612 	bp->flags &= ~BNXT_FLAG_TPA;
4613 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4614 		return;
4615 	if (bp->dev->features & NETIF_F_LRO)
4616 		bp->flags |= BNXT_FLAG_LRO;
4617 	else if (bp->dev->features & NETIF_F_GRO_HW)
4618 		bp->flags |= BNXT_FLAG_GRO;
4619 }
4620 
4621 static void bnxt_init_ring_params(struct bnxt *bp)
4622 {
4623 	unsigned int rx_size;
4624 
4625 	bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK;
4626 	/* Try to fit 4 chunks into a 4k page */
4627 	rx_size = SZ_1K -
4628 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4629 	bp->dev->cfg->hds_thresh = max(BNXT_DEFAULT_RX_COPYBREAK, rx_size);
4630 }
4631 
4632 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4633  * be set on entry.
4634  */
4635 void bnxt_set_ring_params(struct bnxt *bp)
4636 {
4637 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4638 	u32 agg_factor = 0, agg_ring_size = 0;
4639 
4640 	/* 8 for CRC and VLAN */
4641 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4642 
4643 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4644 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4645 
4646 	ring_size = bp->rx_ring_size;
4647 	bp->rx_agg_ring_size = 0;
4648 	bp->rx_agg_nr_pages = 0;
4649 
4650 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4651 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4652 
4653 	bp->flags &= ~BNXT_FLAG_JUMBO;
4654 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4655 		u32 jumbo_factor;
4656 
4657 		bp->flags |= BNXT_FLAG_JUMBO;
4658 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4659 		if (jumbo_factor > agg_factor)
4660 			agg_factor = jumbo_factor;
4661 	}
4662 	if (agg_factor) {
4663 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4664 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4665 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4666 				    bp->rx_ring_size, ring_size);
4667 			bp->rx_ring_size = ring_size;
4668 		}
4669 		agg_ring_size = ring_size * agg_factor;
4670 
4671 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4672 							RX_DESC_CNT);
4673 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4674 			u32 tmp = agg_ring_size;
4675 
4676 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4677 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4678 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4679 				    tmp, agg_ring_size);
4680 		}
4681 		bp->rx_agg_ring_size = agg_ring_size;
4682 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4683 
4684 		if (BNXT_RX_PAGE_MODE(bp)) {
4685 			rx_space = PAGE_SIZE;
4686 			rx_size = PAGE_SIZE -
4687 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4688 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4689 		} else {
4690 			rx_size = max3(BNXT_DEFAULT_RX_COPYBREAK,
4691 				       bp->rx_copybreak,
4692 				       bp->dev->cfg_pending->hds_thresh);
4693 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4694 			rx_space = rx_size + NET_SKB_PAD +
4695 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4696 		}
4697 	}
4698 
4699 	bp->rx_buf_use_size = rx_size;
4700 	bp->rx_buf_size = rx_space;
4701 
4702 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4703 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4704 
4705 	ring_size = bp->tx_ring_size;
4706 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4707 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4708 
4709 	max_rx_cmpl = bp->rx_ring_size;
4710 	/* MAX TPA needs to be added because TPA_START completions are
4711 	 * immediately recycled, so the TPA completions are not bound by
4712 	 * the RX ring size.
4713 	 */
4714 	if (bp->flags & BNXT_FLAG_TPA)
4715 		max_rx_cmpl += bp->max_tpa;
4716 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4717 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4718 	bp->cp_ring_size = ring_size;
4719 
4720 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4721 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4722 		bp->cp_nr_pages = MAX_CP_PAGES;
4723 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4724 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4725 			    ring_size, bp->cp_ring_size);
4726 	}
4727 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4728 	bp->cp_ring_mask = bp->cp_bit - 1;
4729 }
4730 
4731 /* Changing allocation mode of RX rings.
4732  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4733  */
4734 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4735 {
4736 	struct net_device *dev = bp->dev;
4737 
4738 	if (page_mode) {
4739 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4740 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4741 
4742 		if (bp->xdp_prog->aux->xdp_has_frags)
4743 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4744 		else
4745 			dev->max_mtu =
4746 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4747 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4748 			bp->flags |= BNXT_FLAG_JUMBO;
4749 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4750 		} else {
4751 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4752 			bp->rx_skb_func = bnxt_rx_page_skb;
4753 		}
4754 		bp->rx_dir = DMA_BIDIRECTIONAL;
4755 	} else {
4756 		dev->max_mtu = bp->max_mtu;
4757 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4758 		bp->rx_dir = DMA_FROM_DEVICE;
4759 		bp->rx_skb_func = bnxt_rx_skb;
4760 	}
4761 }
4762 
4763 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4764 {
4765 	__bnxt_set_rx_skb_mode(bp, page_mode);
4766 
4767 	if (!page_mode) {
4768 		int rx, tx;
4769 
4770 		bnxt_get_max_rings(bp, &rx, &tx, true);
4771 		if (rx > 1) {
4772 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
4773 			bp->dev->hw_features |= NETIF_F_LRO;
4774 		}
4775 	}
4776 
4777 	/* Update LRO and GRO_HW availability */
4778 	netdev_update_features(bp->dev);
4779 }
4780 
4781 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4782 {
4783 	int i;
4784 	struct bnxt_vnic_info *vnic;
4785 	struct pci_dev *pdev = bp->pdev;
4786 
4787 	if (!bp->vnic_info)
4788 		return;
4789 
4790 	for (i = 0; i < bp->nr_vnics; i++) {
4791 		vnic = &bp->vnic_info[i];
4792 
4793 		kfree(vnic->fw_grp_ids);
4794 		vnic->fw_grp_ids = NULL;
4795 
4796 		kfree(vnic->uc_list);
4797 		vnic->uc_list = NULL;
4798 
4799 		if (vnic->mc_list) {
4800 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4801 					  vnic->mc_list, vnic->mc_list_mapping);
4802 			vnic->mc_list = NULL;
4803 		}
4804 
4805 		if (vnic->rss_table) {
4806 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4807 					  vnic->rss_table,
4808 					  vnic->rss_table_dma_addr);
4809 			vnic->rss_table = NULL;
4810 		}
4811 
4812 		vnic->rss_hash_key = NULL;
4813 		vnic->flags = 0;
4814 	}
4815 }
4816 
4817 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4818 {
4819 	int i, rc = 0, size;
4820 	struct bnxt_vnic_info *vnic;
4821 	struct pci_dev *pdev = bp->pdev;
4822 	int max_rings;
4823 
4824 	for (i = 0; i < bp->nr_vnics; i++) {
4825 		vnic = &bp->vnic_info[i];
4826 
4827 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4828 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4829 
4830 			if (mem_size > 0) {
4831 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4832 				if (!vnic->uc_list) {
4833 					rc = -ENOMEM;
4834 					goto out;
4835 				}
4836 			}
4837 		}
4838 
4839 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4840 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4841 			vnic->mc_list =
4842 				dma_alloc_coherent(&pdev->dev,
4843 						   vnic->mc_list_size,
4844 						   &vnic->mc_list_mapping,
4845 						   GFP_KERNEL);
4846 			if (!vnic->mc_list) {
4847 				rc = -ENOMEM;
4848 				goto out;
4849 			}
4850 		}
4851 
4852 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4853 			goto vnic_skip_grps;
4854 
4855 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4856 			max_rings = bp->rx_nr_rings;
4857 		else
4858 			max_rings = 1;
4859 
4860 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4861 		if (!vnic->fw_grp_ids) {
4862 			rc = -ENOMEM;
4863 			goto out;
4864 		}
4865 vnic_skip_grps:
4866 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4867 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4868 			continue;
4869 
4870 		/* Allocate rss table and hash key */
4871 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4872 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4873 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4874 
4875 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4876 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4877 						     vnic->rss_table_size,
4878 						     &vnic->rss_table_dma_addr,
4879 						     GFP_KERNEL);
4880 		if (!vnic->rss_table) {
4881 			rc = -ENOMEM;
4882 			goto out;
4883 		}
4884 
4885 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4886 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4887 	}
4888 	return 0;
4889 
4890 out:
4891 	return rc;
4892 }
4893 
4894 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4895 {
4896 	struct bnxt_hwrm_wait_token *token;
4897 
4898 	dma_pool_destroy(bp->hwrm_dma_pool);
4899 	bp->hwrm_dma_pool = NULL;
4900 
4901 	rcu_read_lock();
4902 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4903 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4904 	rcu_read_unlock();
4905 }
4906 
4907 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4908 {
4909 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4910 					    BNXT_HWRM_DMA_SIZE,
4911 					    BNXT_HWRM_DMA_ALIGN, 0);
4912 	if (!bp->hwrm_dma_pool)
4913 		return -ENOMEM;
4914 
4915 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4916 
4917 	return 0;
4918 }
4919 
4920 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4921 {
4922 	kfree(stats->hw_masks);
4923 	stats->hw_masks = NULL;
4924 	kfree(stats->sw_stats);
4925 	stats->sw_stats = NULL;
4926 	if (stats->hw_stats) {
4927 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4928 				  stats->hw_stats_map);
4929 		stats->hw_stats = NULL;
4930 	}
4931 }
4932 
4933 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4934 				bool alloc_masks)
4935 {
4936 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4937 					     &stats->hw_stats_map, GFP_KERNEL);
4938 	if (!stats->hw_stats)
4939 		return -ENOMEM;
4940 
4941 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4942 	if (!stats->sw_stats)
4943 		goto stats_mem_err;
4944 
4945 	if (alloc_masks) {
4946 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4947 		if (!stats->hw_masks)
4948 			goto stats_mem_err;
4949 	}
4950 	return 0;
4951 
4952 stats_mem_err:
4953 	bnxt_free_stats_mem(bp, stats);
4954 	return -ENOMEM;
4955 }
4956 
4957 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4958 {
4959 	int i;
4960 
4961 	for (i = 0; i < count; i++)
4962 		mask_arr[i] = mask;
4963 }
4964 
4965 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4966 {
4967 	int i;
4968 
4969 	for (i = 0; i < count; i++)
4970 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4971 }
4972 
4973 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4974 				    struct bnxt_stats_mem *stats)
4975 {
4976 	struct hwrm_func_qstats_ext_output *resp;
4977 	struct hwrm_func_qstats_ext_input *req;
4978 	__le64 *hw_masks;
4979 	int rc;
4980 
4981 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4982 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4983 		return -EOPNOTSUPP;
4984 
4985 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4986 	if (rc)
4987 		return rc;
4988 
4989 	req->fid = cpu_to_le16(0xffff);
4990 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4991 
4992 	resp = hwrm_req_hold(bp, req);
4993 	rc = hwrm_req_send(bp, req);
4994 	if (!rc) {
4995 		hw_masks = &resp->rx_ucast_pkts;
4996 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4997 	}
4998 	hwrm_req_drop(bp, req);
4999 	return rc;
5000 }
5001 
5002 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5003 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5004 
5005 static void bnxt_init_stats(struct bnxt *bp)
5006 {
5007 	struct bnxt_napi *bnapi = bp->bnapi[0];
5008 	struct bnxt_cp_ring_info *cpr;
5009 	struct bnxt_stats_mem *stats;
5010 	__le64 *rx_stats, *tx_stats;
5011 	int rc, rx_count, tx_count;
5012 	u64 *rx_masks, *tx_masks;
5013 	u64 mask;
5014 	u8 flags;
5015 
5016 	cpr = &bnapi->cp_ring;
5017 	stats = &cpr->stats;
5018 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5019 	if (rc) {
5020 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5021 			mask = (1ULL << 48) - 1;
5022 		else
5023 			mask = -1ULL;
5024 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5025 	}
5026 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5027 		stats = &bp->port_stats;
5028 		rx_stats = stats->hw_stats;
5029 		rx_masks = stats->hw_masks;
5030 		rx_count = sizeof(struct rx_port_stats) / 8;
5031 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5032 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5033 		tx_count = sizeof(struct tx_port_stats) / 8;
5034 
5035 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5036 		rc = bnxt_hwrm_port_qstats(bp, flags);
5037 		if (rc) {
5038 			mask = (1ULL << 40) - 1;
5039 
5040 			bnxt_fill_masks(rx_masks, mask, rx_count);
5041 			bnxt_fill_masks(tx_masks, mask, tx_count);
5042 		} else {
5043 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5044 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5045 			bnxt_hwrm_port_qstats(bp, 0);
5046 		}
5047 	}
5048 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5049 		stats = &bp->rx_port_stats_ext;
5050 		rx_stats = stats->hw_stats;
5051 		rx_masks = stats->hw_masks;
5052 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5053 		stats = &bp->tx_port_stats_ext;
5054 		tx_stats = stats->hw_stats;
5055 		tx_masks = stats->hw_masks;
5056 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5057 
5058 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5059 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5060 		if (rc) {
5061 			mask = (1ULL << 40) - 1;
5062 
5063 			bnxt_fill_masks(rx_masks, mask, rx_count);
5064 			if (tx_stats)
5065 				bnxt_fill_masks(tx_masks, mask, tx_count);
5066 		} else {
5067 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5068 			if (tx_stats)
5069 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5070 						   tx_count);
5071 			bnxt_hwrm_port_qstats_ext(bp, 0);
5072 		}
5073 	}
5074 }
5075 
5076 static void bnxt_free_port_stats(struct bnxt *bp)
5077 {
5078 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5079 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5080 
5081 	bnxt_free_stats_mem(bp, &bp->port_stats);
5082 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5083 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5084 }
5085 
5086 static void bnxt_free_ring_stats(struct bnxt *bp)
5087 {
5088 	int i;
5089 
5090 	if (!bp->bnapi)
5091 		return;
5092 
5093 	for (i = 0; i < bp->cp_nr_rings; i++) {
5094 		struct bnxt_napi *bnapi = bp->bnapi[i];
5095 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5096 
5097 		bnxt_free_stats_mem(bp, &cpr->stats);
5098 
5099 		kfree(cpr->sw_stats);
5100 		cpr->sw_stats = NULL;
5101 	}
5102 }
5103 
5104 static int bnxt_alloc_stats(struct bnxt *bp)
5105 {
5106 	u32 size, i;
5107 	int rc;
5108 
5109 	size = bp->hw_ring_stats_size;
5110 
5111 	for (i = 0; i < bp->cp_nr_rings; i++) {
5112 		struct bnxt_napi *bnapi = bp->bnapi[i];
5113 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5114 
5115 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
5116 		if (!cpr->sw_stats)
5117 			return -ENOMEM;
5118 
5119 		cpr->stats.len = size;
5120 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5121 		if (rc)
5122 			return rc;
5123 
5124 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5125 	}
5126 
5127 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5128 		return 0;
5129 
5130 	if (bp->port_stats.hw_stats)
5131 		goto alloc_ext_stats;
5132 
5133 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5134 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5135 	if (rc)
5136 		return rc;
5137 
5138 	bp->flags |= BNXT_FLAG_PORT_STATS;
5139 
5140 alloc_ext_stats:
5141 	/* Display extended statistics only if FW supports it */
5142 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5143 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5144 			return 0;
5145 
5146 	if (bp->rx_port_stats_ext.hw_stats)
5147 		goto alloc_tx_ext_stats;
5148 
5149 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5150 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5151 	/* Extended stats are optional */
5152 	if (rc)
5153 		return 0;
5154 
5155 alloc_tx_ext_stats:
5156 	if (bp->tx_port_stats_ext.hw_stats)
5157 		return 0;
5158 
5159 	if (bp->hwrm_spec_code >= 0x10902 ||
5160 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5161 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5162 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5163 		/* Extended stats are optional */
5164 		if (rc)
5165 			return 0;
5166 	}
5167 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5168 	return 0;
5169 }
5170 
5171 static void bnxt_clear_ring_indices(struct bnxt *bp)
5172 {
5173 	int i, j;
5174 
5175 	if (!bp->bnapi)
5176 		return;
5177 
5178 	for (i = 0; i < bp->cp_nr_rings; i++) {
5179 		struct bnxt_napi *bnapi = bp->bnapi[i];
5180 		struct bnxt_cp_ring_info *cpr;
5181 		struct bnxt_rx_ring_info *rxr;
5182 		struct bnxt_tx_ring_info *txr;
5183 
5184 		if (!bnapi)
5185 			continue;
5186 
5187 		cpr = &bnapi->cp_ring;
5188 		cpr->cp_raw_cons = 0;
5189 
5190 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5191 			txr->tx_prod = 0;
5192 			txr->tx_cons = 0;
5193 			txr->tx_hw_cons = 0;
5194 		}
5195 
5196 		rxr = bnapi->rx_ring;
5197 		if (rxr) {
5198 			rxr->rx_prod = 0;
5199 			rxr->rx_agg_prod = 0;
5200 			rxr->rx_sw_agg_prod = 0;
5201 			rxr->rx_next_cons = 0;
5202 		}
5203 		bnapi->events = 0;
5204 	}
5205 }
5206 
5207 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5208 {
5209 	u8 type = fltr->type, flags = fltr->flags;
5210 
5211 	INIT_LIST_HEAD(&fltr->list);
5212 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5213 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5214 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5215 }
5216 
5217 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5218 {
5219 	if (!list_empty(&fltr->list))
5220 		list_del_init(&fltr->list);
5221 }
5222 
5223 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5224 {
5225 	struct bnxt_filter_base *usr_fltr, *tmp;
5226 
5227 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5228 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5229 			continue;
5230 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5231 	}
5232 }
5233 
5234 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5235 {
5236 	hlist_del(&fltr->hash);
5237 	bnxt_del_one_usr_fltr(bp, fltr);
5238 	if (fltr->flags) {
5239 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5240 		bp->ntp_fltr_count--;
5241 	}
5242 	kfree(fltr);
5243 }
5244 
5245 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5246 {
5247 	int i;
5248 
5249 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
5250 	 * safe to delete the hash table.
5251 	 */
5252 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5253 		struct hlist_head *head;
5254 		struct hlist_node *tmp;
5255 		struct bnxt_ntuple_filter *fltr;
5256 
5257 		head = &bp->ntp_fltr_hash_tbl[i];
5258 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5259 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5260 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5261 				     !list_empty(&fltr->base.list)))
5262 				continue;
5263 			bnxt_del_fltr(bp, &fltr->base);
5264 		}
5265 	}
5266 	if (!all)
5267 		return;
5268 
5269 	bitmap_free(bp->ntp_fltr_bmap);
5270 	bp->ntp_fltr_bmap = NULL;
5271 	bp->ntp_fltr_count = 0;
5272 }
5273 
5274 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5275 {
5276 	int i, rc = 0;
5277 
5278 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5279 		return 0;
5280 
5281 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5282 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5283 
5284 	bp->ntp_fltr_count = 0;
5285 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5286 
5287 	if (!bp->ntp_fltr_bmap)
5288 		rc = -ENOMEM;
5289 
5290 	return rc;
5291 }
5292 
5293 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5294 {
5295 	int i;
5296 
5297 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5298 		struct hlist_head *head;
5299 		struct hlist_node *tmp;
5300 		struct bnxt_l2_filter *fltr;
5301 
5302 		head = &bp->l2_fltr_hash_tbl[i];
5303 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5304 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5305 				     !list_empty(&fltr->base.list)))
5306 				continue;
5307 			bnxt_del_fltr(bp, &fltr->base);
5308 		}
5309 	}
5310 }
5311 
5312 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5313 {
5314 	int i;
5315 
5316 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5317 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5318 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5319 }
5320 
5321 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5322 {
5323 	bnxt_free_vnic_attributes(bp);
5324 	bnxt_free_tx_rings(bp);
5325 	bnxt_free_rx_rings(bp);
5326 	bnxt_free_cp_rings(bp);
5327 	bnxt_free_all_cp_arrays(bp);
5328 	bnxt_free_ntp_fltrs(bp, false);
5329 	bnxt_free_l2_filters(bp, false);
5330 	if (irq_re_init) {
5331 		bnxt_free_ring_stats(bp);
5332 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5333 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5334 			bnxt_free_port_stats(bp);
5335 		bnxt_free_ring_grps(bp);
5336 		bnxt_free_vnics(bp);
5337 		kfree(bp->tx_ring_map);
5338 		bp->tx_ring_map = NULL;
5339 		kfree(bp->tx_ring);
5340 		bp->tx_ring = NULL;
5341 		kfree(bp->rx_ring);
5342 		bp->rx_ring = NULL;
5343 		kfree(bp->bnapi);
5344 		bp->bnapi = NULL;
5345 	} else {
5346 		bnxt_clear_ring_indices(bp);
5347 	}
5348 }
5349 
5350 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5351 {
5352 	int i, j, rc, size, arr_size;
5353 	void *bnapi;
5354 
5355 	if (irq_re_init) {
5356 		/* Allocate bnapi mem pointer array and mem block for
5357 		 * all queues
5358 		 */
5359 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5360 				bp->cp_nr_rings);
5361 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5362 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5363 		if (!bnapi)
5364 			return -ENOMEM;
5365 
5366 		bp->bnapi = bnapi;
5367 		bnapi += arr_size;
5368 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5369 			bp->bnapi[i] = bnapi;
5370 			bp->bnapi[i]->index = i;
5371 			bp->bnapi[i]->bp = bp;
5372 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5373 				struct bnxt_cp_ring_info *cpr =
5374 					&bp->bnapi[i]->cp_ring;
5375 
5376 				cpr->cp_ring_struct.ring_mem.flags =
5377 					BNXT_RMEM_RING_PTE_FLAG;
5378 			}
5379 		}
5380 
5381 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5382 				      sizeof(struct bnxt_rx_ring_info),
5383 				      GFP_KERNEL);
5384 		if (!bp->rx_ring)
5385 			return -ENOMEM;
5386 
5387 		for (i = 0; i < bp->rx_nr_rings; i++) {
5388 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5389 
5390 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5391 				rxr->rx_ring_struct.ring_mem.flags =
5392 					BNXT_RMEM_RING_PTE_FLAG;
5393 				rxr->rx_agg_ring_struct.ring_mem.flags =
5394 					BNXT_RMEM_RING_PTE_FLAG;
5395 			} else {
5396 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5397 			}
5398 			rxr->bnapi = bp->bnapi[i];
5399 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5400 		}
5401 
5402 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5403 				      sizeof(struct bnxt_tx_ring_info),
5404 				      GFP_KERNEL);
5405 		if (!bp->tx_ring)
5406 			return -ENOMEM;
5407 
5408 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5409 					  GFP_KERNEL);
5410 
5411 		if (!bp->tx_ring_map)
5412 			return -ENOMEM;
5413 
5414 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5415 			j = 0;
5416 		else
5417 			j = bp->rx_nr_rings;
5418 
5419 		for (i = 0; i < bp->tx_nr_rings; i++) {
5420 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5421 			struct bnxt_napi *bnapi2;
5422 
5423 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5424 				txr->tx_ring_struct.ring_mem.flags =
5425 					BNXT_RMEM_RING_PTE_FLAG;
5426 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5427 			if (i >= bp->tx_nr_rings_xdp) {
5428 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5429 
5430 				bnapi2 = bp->bnapi[k];
5431 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5432 				txr->tx_napi_idx =
5433 					BNXT_RING_TO_TC(bp, txr->txq_index);
5434 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5435 				bnapi2->tx_int = bnxt_tx_int;
5436 			} else {
5437 				bnapi2 = bp->bnapi[j];
5438 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5439 				bnapi2->tx_ring[0] = txr;
5440 				bnapi2->tx_int = bnxt_tx_int_xdp;
5441 				j++;
5442 			}
5443 			txr->bnapi = bnapi2;
5444 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5445 				txr->tx_cpr = &bnapi2->cp_ring;
5446 		}
5447 
5448 		rc = bnxt_alloc_stats(bp);
5449 		if (rc)
5450 			goto alloc_mem_err;
5451 		bnxt_init_stats(bp);
5452 
5453 		rc = bnxt_alloc_ntp_fltrs(bp);
5454 		if (rc)
5455 			goto alloc_mem_err;
5456 
5457 		rc = bnxt_alloc_vnics(bp);
5458 		if (rc)
5459 			goto alloc_mem_err;
5460 	}
5461 
5462 	rc = bnxt_alloc_all_cp_arrays(bp);
5463 	if (rc)
5464 		goto alloc_mem_err;
5465 
5466 	bnxt_init_ring_struct(bp);
5467 
5468 	rc = bnxt_alloc_rx_rings(bp);
5469 	if (rc)
5470 		goto alloc_mem_err;
5471 
5472 	rc = bnxt_alloc_tx_rings(bp);
5473 	if (rc)
5474 		goto alloc_mem_err;
5475 
5476 	rc = bnxt_alloc_cp_rings(bp);
5477 	if (rc)
5478 		goto alloc_mem_err;
5479 
5480 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5481 						  BNXT_VNIC_MCAST_FLAG |
5482 						  BNXT_VNIC_UCAST_FLAG;
5483 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5484 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5485 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5486 
5487 	rc = bnxt_alloc_vnic_attributes(bp);
5488 	if (rc)
5489 		goto alloc_mem_err;
5490 	return 0;
5491 
5492 alloc_mem_err:
5493 	bnxt_free_mem(bp, true);
5494 	return rc;
5495 }
5496 
5497 static void bnxt_disable_int(struct bnxt *bp)
5498 {
5499 	int i;
5500 
5501 	if (!bp->bnapi)
5502 		return;
5503 
5504 	for (i = 0; i < bp->cp_nr_rings; i++) {
5505 		struct bnxt_napi *bnapi = bp->bnapi[i];
5506 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5507 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5508 
5509 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5510 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5511 	}
5512 }
5513 
5514 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5515 {
5516 	struct bnxt_napi *bnapi = bp->bnapi[n];
5517 	struct bnxt_cp_ring_info *cpr;
5518 
5519 	cpr = &bnapi->cp_ring;
5520 	return cpr->cp_ring_struct.map_idx;
5521 }
5522 
5523 static void bnxt_disable_int_sync(struct bnxt *bp)
5524 {
5525 	int i;
5526 
5527 	if (!bp->irq_tbl)
5528 		return;
5529 
5530 	atomic_inc(&bp->intr_sem);
5531 
5532 	bnxt_disable_int(bp);
5533 	for (i = 0; i < bp->cp_nr_rings; i++) {
5534 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5535 
5536 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5537 	}
5538 }
5539 
5540 static void bnxt_enable_int(struct bnxt *bp)
5541 {
5542 	int i;
5543 
5544 	atomic_set(&bp->intr_sem, 0);
5545 	for (i = 0; i < bp->cp_nr_rings; i++) {
5546 		struct bnxt_napi *bnapi = bp->bnapi[i];
5547 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5548 
5549 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5550 	}
5551 }
5552 
5553 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5554 			    bool async_only)
5555 {
5556 	DECLARE_BITMAP(async_events_bmap, 256);
5557 	u32 *events = (u32 *)async_events_bmap;
5558 	struct hwrm_func_drv_rgtr_output *resp;
5559 	struct hwrm_func_drv_rgtr_input *req;
5560 	u32 flags;
5561 	int rc, i;
5562 
5563 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5564 	if (rc)
5565 		return rc;
5566 
5567 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5568 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5569 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5570 
5571 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5572 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5573 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5574 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5575 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5576 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5577 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5578 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5579 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5580 	req->flags = cpu_to_le32(flags);
5581 	req->ver_maj_8b = DRV_VER_MAJ;
5582 	req->ver_min_8b = DRV_VER_MIN;
5583 	req->ver_upd_8b = DRV_VER_UPD;
5584 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5585 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5586 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5587 
5588 	if (BNXT_PF(bp)) {
5589 		u32 data[8];
5590 		int i;
5591 
5592 		memset(data, 0, sizeof(data));
5593 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5594 			u16 cmd = bnxt_vf_req_snif[i];
5595 			unsigned int bit, idx;
5596 
5597 			idx = cmd / 32;
5598 			bit = cmd % 32;
5599 			data[idx] |= 1 << bit;
5600 		}
5601 
5602 		for (i = 0; i < 8; i++)
5603 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5604 
5605 		req->enables |=
5606 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5607 	}
5608 
5609 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5610 		req->flags |= cpu_to_le32(
5611 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5612 
5613 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5614 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5615 		u16 event_id = bnxt_async_events_arr[i];
5616 
5617 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5618 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5619 			continue;
5620 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5621 		    !bp->ptp_cfg)
5622 			continue;
5623 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5624 	}
5625 	if (bmap && bmap_size) {
5626 		for (i = 0; i < bmap_size; i++) {
5627 			if (test_bit(i, bmap))
5628 				__set_bit(i, async_events_bmap);
5629 		}
5630 	}
5631 	for (i = 0; i < 8; i++)
5632 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5633 
5634 	if (async_only)
5635 		req->enables =
5636 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5637 
5638 	resp = hwrm_req_hold(bp, req);
5639 	rc = hwrm_req_send(bp, req);
5640 	if (!rc) {
5641 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5642 		if (resp->flags &
5643 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5644 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5645 	}
5646 	hwrm_req_drop(bp, req);
5647 	return rc;
5648 }
5649 
5650 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5651 {
5652 	struct hwrm_func_drv_unrgtr_input *req;
5653 	int rc;
5654 
5655 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5656 		return 0;
5657 
5658 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5659 	if (rc)
5660 		return rc;
5661 	return hwrm_req_send(bp, req);
5662 }
5663 
5664 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5665 
5666 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5667 {
5668 	struct hwrm_tunnel_dst_port_free_input *req;
5669 	int rc;
5670 
5671 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5672 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5673 		return 0;
5674 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5675 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5676 		return 0;
5677 
5678 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5679 	if (rc)
5680 		return rc;
5681 
5682 	req->tunnel_type = tunnel_type;
5683 
5684 	switch (tunnel_type) {
5685 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5686 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5687 		bp->vxlan_port = 0;
5688 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5689 		break;
5690 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5691 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5692 		bp->nge_port = 0;
5693 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5694 		break;
5695 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5696 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5697 		bp->vxlan_gpe_port = 0;
5698 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5699 		break;
5700 	default:
5701 		break;
5702 	}
5703 
5704 	rc = hwrm_req_send(bp, req);
5705 	if (rc)
5706 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5707 			   rc);
5708 	if (bp->flags & BNXT_FLAG_TPA)
5709 		bnxt_set_tpa(bp, true);
5710 	return rc;
5711 }
5712 
5713 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5714 					   u8 tunnel_type)
5715 {
5716 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5717 	struct hwrm_tunnel_dst_port_alloc_input *req;
5718 	int rc;
5719 
5720 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5721 	if (rc)
5722 		return rc;
5723 
5724 	req->tunnel_type = tunnel_type;
5725 	req->tunnel_dst_port_val = port;
5726 
5727 	resp = hwrm_req_hold(bp, req);
5728 	rc = hwrm_req_send(bp, req);
5729 	if (rc) {
5730 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5731 			   rc);
5732 		goto err_out;
5733 	}
5734 
5735 	switch (tunnel_type) {
5736 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5737 		bp->vxlan_port = port;
5738 		bp->vxlan_fw_dst_port_id =
5739 			le16_to_cpu(resp->tunnel_dst_port_id);
5740 		break;
5741 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5742 		bp->nge_port = port;
5743 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5744 		break;
5745 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5746 		bp->vxlan_gpe_port = port;
5747 		bp->vxlan_gpe_fw_dst_port_id =
5748 			le16_to_cpu(resp->tunnel_dst_port_id);
5749 		break;
5750 	default:
5751 		break;
5752 	}
5753 	if (bp->flags & BNXT_FLAG_TPA)
5754 		bnxt_set_tpa(bp, true);
5755 
5756 err_out:
5757 	hwrm_req_drop(bp, req);
5758 	return rc;
5759 }
5760 
5761 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5762 {
5763 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5764 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5765 	int rc;
5766 
5767 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5768 	if (rc)
5769 		return rc;
5770 
5771 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5772 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5773 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5774 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5775 	}
5776 	req->mask = cpu_to_le32(vnic->rx_mask);
5777 	return hwrm_req_send_silent(bp, req);
5778 }
5779 
5780 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5781 {
5782 	if (!atomic_dec_and_test(&fltr->refcnt))
5783 		return;
5784 	spin_lock_bh(&bp->ntp_fltr_lock);
5785 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5786 		spin_unlock_bh(&bp->ntp_fltr_lock);
5787 		return;
5788 	}
5789 	hlist_del_rcu(&fltr->base.hash);
5790 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5791 	if (fltr->base.flags) {
5792 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5793 		bp->ntp_fltr_count--;
5794 	}
5795 	spin_unlock_bh(&bp->ntp_fltr_lock);
5796 	kfree_rcu(fltr, base.rcu);
5797 }
5798 
5799 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5800 						      struct bnxt_l2_key *key,
5801 						      u32 idx)
5802 {
5803 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5804 	struct bnxt_l2_filter *fltr;
5805 
5806 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5807 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5808 
5809 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5810 		    l2_key->vlan == key->vlan)
5811 			return fltr;
5812 	}
5813 	return NULL;
5814 }
5815 
5816 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5817 						    struct bnxt_l2_key *key,
5818 						    u32 idx)
5819 {
5820 	struct bnxt_l2_filter *fltr = NULL;
5821 
5822 	rcu_read_lock();
5823 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5824 	if (fltr)
5825 		atomic_inc(&fltr->refcnt);
5826 	rcu_read_unlock();
5827 	return fltr;
5828 }
5829 
5830 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5831 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5832 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5833 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5834 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5835 
5836 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5837 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5838 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5839 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5840 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5841 
5842 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5843 {
5844 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5845 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5846 			return sizeof(fkeys->addrs.v4addrs) +
5847 			       sizeof(fkeys->ports);
5848 
5849 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5850 			return sizeof(fkeys->addrs.v4addrs);
5851 	}
5852 
5853 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5854 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5855 			return sizeof(fkeys->addrs.v6addrs) +
5856 			       sizeof(fkeys->ports);
5857 
5858 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5859 			return sizeof(fkeys->addrs.v6addrs);
5860 	}
5861 
5862 	return 0;
5863 }
5864 
5865 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5866 			 const unsigned char *key)
5867 {
5868 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5869 	struct bnxt_ipv4_tuple tuple4;
5870 	struct bnxt_ipv6_tuple tuple6;
5871 	int i, j, len = 0;
5872 	u8 *four_tuple;
5873 
5874 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5875 	if (!len)
5876 		return 0;
5877 
5878 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5879 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5880 		tuple4.ports = fkeys->ports;
5881 		four_tuple = (unsigned char *)&tuple4;
5882 	} else {
5883 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5884 		tuple6.ports = fkeys->ports;
5885 		four_tuple = (unsigned char *)&tuple6;
5886 	}
5887 
5888 	for (i = 0, j = 8; i < len; i++, j++) {
5889 		u8 byte = four_tuple[i];
5890 		int bit;
5891 
5892 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5893 			if (byte & 0x80)
5894 				hash ^= prefix;
5895 		}
5896 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5897 	}
5898 
5899 	/* The valid part of the hash is in the upper 32 bits. */
5900 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5901 }
5902 
5903 #ifdef CONFIG_RFS_ACCEL
5904 static struct bnxt_l2_filter *
5905 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5906 {
5907 	struct bnxt_l2_filter *fltr;
5908 	u32 idx;
5909 
5910 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5911 	      BNXT_L2_FLTR_HASH_MASK;
5912 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5913 	return fltr;
5914 }
5915 #endif
5916 
5917 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5918 			       struct bnxt_l2_key *key, u32 idx)
5919 {
5920 	struct hlist_head *head;
5921 
5922 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5923 	fltr->l2_key.vlan = key->vlan;
5924 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5925 	if (fltr->base.flags) {
5926 		int bit_id;
5927 
5928 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5929 						 bp->max_fltr, 0);
5930 		if (bit_id < 0)
5931 			return -ENOMEM;
5932 		fltr->base.sw_id = (u16)bit_id;
5933 		bp->ntp_fltr_count++;
5934 	}
5935 	head = &bp->l2_fltr_hash_tbl[idx];
5936 	hlist_add_head_rcu(&fltr->base.hash, head);
5937 	bnxt_insert_usr_fltr(bp, &fltr->base);
5938 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5939 	atomic_set(&fltr->refcnt, 1);
5940 	return 0;
5941 }
5942 
5943 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
5944 						   struct bnxt_l2_key *key,
5945 						   gfp_t gfp)
5946 {
5947 	struct bnxt_l2_filter *fltr;
5948 	u32 idx;
5949 	int rc;
5950 
5951 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5952 	      BNXT_L2_FLTR_HASH_MASK;
5953 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5954 	if (fltr)
5955 		return fltr;
5956 
5957 	fltr = kzalloc(sizeof(*fltr), gfp);
5958 	if (!fltr)
5959 		return ERR_PTR(-ENOMEM);
5960 	spin_lock_bh(&bp->ntp_fltr_lock);
5961 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5962 	spin_unlock_bh(&bp->ntp_fltr_lock);
5963 	if (rc) {
5964 		bnxt_del_l2_filter(bp, fltr);
5965 		fltr = ERR_PTR(rc);
5966 	}
5967 	return fltr;
5968 }
5969 
5970 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
5971 						struct bnxt_l2_key *key,
5972 						u16 flags)
5973 {
5974 	struct bnxt_l2_filter *fltr;
5975 	u32 idx;
5976 	int rc;
5977 
5978 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5979 	      BNXT_L2_FLTR_HASH_MASK;
5980 	spin_lock_bh(&bp->ntp_fltr_lock);
5981 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5982 	if (fltr) {
5983 		fltr = ERR_PTR(-EEXIST);
5984 		goto l2_filter_exit;
5985 	}
5986 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
5987 	if (!fltr) {
5988 		fltr = ERR_PTR(-ENOMEM);
5989 		goto l2_filter_exit;
5990 	}
5991 	fltr->base.flags = flags;
5992 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5993 	if (rc) {
5994 		spin_unlock_bh(&bp->ntp_fltr_lock);
5995 		bnxt_del_l2_filter(bp, fltr);
5996 		return ERR_PTR(rc);
5997 	}
5998 
5999 l2_filter_exit:
6000 	spin_unlock_bh(&bp->ntp_fltr_lock);
6001 	return fltr;
6002 }
6003 
6004 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6005 {
6006 #ifdef CONFIG_BNXT_SRIOV
6007 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6008 
6009 	return vf->fw_fid;
6010 #else
6011 	return INVALID_HW_RING_ID;
6012 #endif
6013 }
6014 
6015 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6016 {
6017 	struct hwrm_cfa_l2_filter_free_input *req;
6018 	u16 target_id = 0xffff;
6019 	int rc;
6020 
6021 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6022 		struct bnxt_pf_info *pf = &bp->pf;
6023 
6024 		if (fltr->base.vf_idx >= pf->active_vfs)
6025 			return -EINVAL;
6026 
6027 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6028 		if (target_id == INVALID_HW_RING_ID)
6029 			return -EINVAL;
6030 	}
6031 
6032 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6033 	if (rc)
6034 		return rc;
6035 
6036 	req->target_id = cpu_to_le16(target_id);
6037 	req->l2_filter_id = fltr->base.filter_id;
6038 	return hwrm_req_send(bp, req);
6039 }
6040 
6041 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6042 {
6043 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6044 	struct hwrm_cfa_l2_filter_alloc_input *req;
6045 	u16 target_id = 0xffff;
6046 	int rc;
6047 
6048 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6049 		struct bnxt_pf_info *pf = &bp->pf;
6050 
6051 		if (fltr->base.vf_idx >= pf->active_vfs)
6052 			return -EINVAL;
6053 
6054 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6055 	}
6056 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6057 	if (rc)
6058 		return rc;
6059 
6060 	req->target_id = cpu_to_le16(target_id);
6061 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6062 
6063 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6064 		req->flags |=
6065 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6066 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6067 	req->enables =
6068 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6069 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6070 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6071 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6072 	eth_broadcast_addr(req->l2_addr_mask);
6073 
6074 	if (fltr->l2_key.vlan) {
6075 		req->enables |=
6076 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6077 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6078 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6079 		req->num_vlans = 1;
6080 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6081 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6082 	}
6083 
6084 	resp = hwrm_req_hold(bp, req);
6085 	rc = hwrm_req_send(bp, req);
6086 	if (!rc) {
6087 		fltr->base.filter_id = resp->l2_filter_id;
6088 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6089 	}
6090 	hwrm_req_drop(bp, req);
6091 	return rc;
6092 }
6093 
6094 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6095 				     struct bnxt_ntuple_filter *fltr)
6096 {
6097 	struct hwrm_cfa_ntuple_filter_free_input *req;
6098 	int rc;
6099 
6100 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6101 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6102 	if (rc)
6103 		return rc;
6104 
6105 	req->ntuple_filter_id = fltr->base.filter_id;
6106 	return hwrm_req_send(bp, req);
6107 }
6108 
6109 #define BNXT_NTP_FLTR_FLAGS					\
6110 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6111 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6112 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6113 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6114 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6115 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6116 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6117 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6118 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6119 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6120 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6121 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6122 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6123 
6124 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6125 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6126 
6127 void bnxt_fill_ipv6_mask(__be32 mask[4])
6128 {
6129 	int i;
6130 
6131 	for (i = 0; i < 4; i++)
6132 		mask[i] = cpu_to_be32(~0);
6133 }
6134 
6135 static void
6136 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6137 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6138 			  struct bnxt_ntuple_filter *fltr)
6139 {
6140 	u16 rxq = fltr->base.rxq;
6141 
6142 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6143 		struct ethtool_rxfh_context *ctx;
6144 		struct bnxt_rss_ctx *rss_ctx;
6145 		struct bnxt_vnic_info *vnic;
6146 
6147 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6148 			      fltr->base.fw_vnic_id);
6149 		if (ctx) {
6150 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6151 			vnic = &rss_ctx->vnic;
6152 
6153 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6154 		}
6155 		return;
6156 	}
6157 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6158 		struct bnxt_vnic_info *vnic;
6159 		u32 enables;
6160 
6161 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6162 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6163 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6164 		req->enables |= cpu_to_le32(enables);
6165 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6166 	} else {
6167 		u32 flags;
6168 
6169 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6170 		req->flags |= cpu_to_le32(flags);
6171 		req->dst_id = cpu_to_le16(rxq);
6172 	}
6173 }
6174 
6175 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6176 				      struct bnxt_ntuple_filter *fltr)
6177 {
6178 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6179 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6180 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6181 	struct flow_keys *keys = &fltr->fkeys;
6182 	struct bnxt_l2_filter *l2_fltr;
6183 	struct bnxt_vnic_info *vnic;
6184 	int rc;
6185 
6186 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6187 	if (rc)
6188 		return rc;
6189 
6190 	l2_fltr = fltr->l2_fltr;
6191 	req->l2_filter_id = l2_fltr->base.filter_id;
6192 
6193 	if (fltr->base.flags & BNXT_ACT_DROP) {
6194 		req->flags =
6195 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6196 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6197 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6198 	} else {
6199 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6200 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6201 	}
6202 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6203 
6204 	req->ethertype = htons(ETH_P_IP);
6205 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6206 	req->ip_protocol = keys->basic.ip_proto;
6207 
6208 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6209 		req->ethertype = htons(ETH_P_IPV6);
6210 		req->ip_addr_type =
6211 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6212 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6213 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6214 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6215 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6216 	} else {
6217 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6218 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6219 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6220 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6221 	}
6222 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6223 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6224 		req->tunnel_type =
6225 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6226 	}
6227 
6228 	req->src_port = keys->ports.src;
6229 	req->src_port_mask = masks->ports.src;
6230 	req->dst_port = keys->ports.dst;
6231 	req->dst_port_mask = masks->ports.dst;
6232 
6233 	resp = hwrm_req_hold(bp, req);
6234 	rc = hwrm_req_send(bp, req);
6235 	if (!rc)
6236 		fltr->base.filter_id = resp->ntuple_filter_id;
6237 	hwrm_req_drop(bp, req);
6238 	return rc;
6239 }
6240 
6241 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6242 				     const u8 *mac_addr)
6243 {
6244 	struct bnxt_l2_filter *fltr;
6245 	struct bnxt_l2_key key;
6246 	int rc;
6247 
6248 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6249 	key.vlan = 0;
6250 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6251 	if (IS_ERR(fltr))
6252 		return PTR_ERR(fltr);
6253 
6254 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6255 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6256 	if (rc)
6257 		bnxt_del_l2_filter(bp, fltr);
6258 	else
6259 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6260 	return rc;
6261 }
6262 
6263 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6264 {
6265 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6266 
6267 	/* Any associated ntuple filters will also be cleared by firmware. */
6268 	for (i = 0; i < num_of_vnics; i++) {
6269 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6270 
6271 		for (j = 0; j < vnic->uc_filter_count; j++) {
6272 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6273 
6274 			bnxt_hwrm_l2_filter_free(bp, fltr);
6275 			bnxt_del_l2_filter(bp, fltr);
6276 		}
6277 		vnic->uc_filter_count = 0;
6278 	}
6279 }
6280 
6281 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6282 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6283 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6284 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6285 
6286 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6287 					   struct hwrm_vnic_tpa_cfg_input *req)
6288 {
6289 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6290 
6291 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6292 		return;
6293 
6294 	if (bp->vxlan_port)
6295 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6296 	if (bp->vxlan_gpe_port)
6297 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6298 	if (bp->nge_port)
6299 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6300 
6301 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6302 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6303 }
6304 
6305 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6306 			   u32 tpa_flags)
6307 {
6308 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6309 	struct hwrm_vnic_tpa_cfg_input *req;
6310 	int rc;
6311 
6312 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6313 		return 0;
6314 
6315 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6316 	if (rc)
6317 		return rc;
6318 
6319 	if (tpa_flags) {
6320 		u16 mss = bp->dev->mtu - 40;
6321 		u32 nsegs, n, segs = 0, flags;
6322 
6323 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6324 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6325 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6326 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6327 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6328 		if (tpa_flags & BNXT_FLAG_GRO)
6329 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6330 
6331 		req->flags = cpu_to_le32(flags);
6332 
6333 		req->enables =
6334 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6335 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6336 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6337 
6338 		/* Number of segs are log2 units, and first packet is not
6339 		 * included as part of this units.
6340 		 */
6341 		if (mss <= BNXT_RX_PAGE_SIZE) {
6342 			n = BNXT_RX_PAGE_SIZE / mss;
6343 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6344 		} else {
6345 			n = mss / BNXT_RX_PAGE_SIZE;
6346 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6347 				n++;
6348 			nsegs = (MAX_SKB_FRAGS - n) / n;
6349 		}
6350 
6351 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6352 			segs = MAX_TPA_SEGS_P5;
6353 			max_aggs = bp->max_tpa;
6354 		} else {
6355 			segs = ilog2(nsegs);
6356 		}
6357 		req->max_agg_segs = cpu_to_le16(segs);
6358 		req->max_aggs = cpu_to_le16(max_aggs);
6359 
6360 		req->min_agg_len = cpu_to_le32(512);
6361 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6362 	}
6363 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6364 
6365 	return hwrm_req_send(bp, req);
6366 }
6367 
6368 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6369 {
6370 	struct bnxt_ring_grp_info *grp_info;
6371 
6372 	grp_info = &bp->grp_info[ring->grp_idx];
6373 	return grp_info->cp_fw_ring_id;
6374 }
6375 
6376 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6377 {
6378 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6379 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6380 	else
6381 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6382 }
6383 
6384 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6385 {
6386 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6387 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6388 	else
6389 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6390 }
6391 
6392 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6393 {
6394 	int entries;
6395 
6396 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6397 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6398 	else
6399 		entries = HW_HASH_INDEX_SIZE;
6400 
6401 	bp->rss_indir_tbl_entries = entries;
6402 	bp->rss_indir_tbl =
6403 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6404 	if (!bp->rss_indir_tbl)
6405 		return -ENOMEM;
6406 
6407 	return 0;
6408 }
6409 
6410 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6411 				 struct ethtool_rxfh_context *rss_ctx)
6412 {
6413 	u16 max_rings, max_entries, pad, i;
6414 	u32 *rss_indir_tbl;
6415 
6416 	if (!bp->rx_nr_rings)
6417 		return;
6418 
6419 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6420 		max_rings = bp->rx_nr_rings - 1;
6421 	else
6422 		max_rings = bp->rx_nr_rings;
6423 
6424 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6425 	if (rss_ctx)
6426 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6427 	else
6428 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6429 
6430 	for (i = 0; i < max_entries; i++)
6431 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6432 
6433 	pad = bp->rss_indir_tbl_entries - max_entries;
6434 	if (pad)
6435 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6436 }
6437 
6438 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6439 {
6440 	u32 i, tbl_size, max_ring = 0;
6441 
6442 	if (!bp->rss_indir_tbl)
6443 		return 0;
6444 
6445 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6446 	for (i = 0; i < tbl_size; i++)
6447 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6448 	return max_ring;
6449 }
6450 
6451 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6452 {
6453 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6454 		if (!rx_rings)
6455 			return 0;
6456 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6457 					       BNXT_RSS_TABLE_ENTRIES_P5);
6458 	}
6459 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6460 		return 2;
6461 	return 1;
6462 }
6463 
6464 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6465 {
6466 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6467 	u16 i, j;
6468 
6469 	/* Fill the RSS indirection table with ring group ids */
6470 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6471 		if (!no_rss)
6472 			j = bp->rss_indir_tbl[i];
6473 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6474 	}
6475 }
6476 
6477 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6478 				    struct bnxt_vnic_info *vnic)
6479 {
6480 	__le16 *ring_tbl = vnic->rss_table;
6481 	struct bnxt_rx_ring_info *rxr;
6482 	u16 tbl_size, i;
6483 
6484 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6485 
6486 	for (i = 0; i < tbl_size; i++) {
6487 		u16 ring_id, j;
6488 
6489 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6490 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6491 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6492 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6493 		else
6494 			j = bp->rss_indir_tbl[i];
6495 		rxr = &bp->rx_ring[j];
6496 
6497 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6498 		*ring_tbl++ = cpu_to_le16(ring_id);
6499 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6500 		*ring_tbl++ = cpu_to_le16(ring_id);
6501 	}
6502 }
6503 
6504 static void
6505 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6506 			 struct bnxt_vnic_info *vnic)
6507 {
6508 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6509 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6510 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6511 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6512 	} else {
6513 		bnxt_fill_hw_rss_tbl(bp, vnic);
6514 	}
6515 
6516 	if (bp->rss_hash_delta) {
6517 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6518 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6519 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6520 		else
6521 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6522 	} else {
6523 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6524 	}
6525 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6526 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6527 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6528 }
6529 
6530 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6531 				  bool set_rss)
6532 {
6533 	struct hwrm_vnic_rss_cfg_input *req;
6534 	int rc;
6535 
6536 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6537 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6538 		return 0;
6539 
6540 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6541 	if (rc)
6542 		return rc;
6543 
6544 	if (set_rss)
6545 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6546 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6547 	return hwrm_req_send(bp, req);
6548 }
6549 
6550 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6551 				     struct bnxt_vnic_info *vnic, bool set_rss)
6552 {
6553 	struct hwrm_vnic_rss_cfg_input *req;
6554 	dma_addr_t ring_tbl_map;
6555 	u32 i, nr_ctxs;
6556 	int rc;
6557 
6558 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6559 	if (rc)
6560 		return rc;
6561 
6562 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6563 	if (!set_rss)
6564 		return hwrm_req_send(bp, req);
6565 
6566 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6567 	ring_tbl_map = vnic->rss_table_dma_addr;
6568 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6569 
6570 	hwrm_req_hold(bp, req);
6571 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6572 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6573 		req->ring_table_pair_index = i;
6574 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6575 		rc = hwrm_req_send(bp, req);
6576 		if (rc)
6577 			goto exit;
6578 	}
6579 
6580 exit:
6581 	hwrm_req_drop(bp, req);
6582 	return rc;
6583 }
6584 
6585 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6586 {
6587 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6588 	struct hwrm_vnic_rss_qcfg_output *resp;
6589 	struct hwrm_vnic_rss_qcfg_input *req;
6590 
6591 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6592 		return;
6593 
6594 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6595 	/* all contexts configured to same hash_type, zero always exists */
6596 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6597 	resp = hwrm_req_hold(bp, req);
6598 	if (!hwrm_req_send(bp, req)) {
6599 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6600 		bp->rss_hash_delta = 0;
6601 	}
6602 	hwrm_req_drop(bp, req);
6603 }
6604 
6605 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6606 {
6607 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6608 	struct hwrm_vnic_plcmodes_cfg_input *req;
6609 	int rc;
6610 
6611 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6612 	if (rc)
6613 		return rc;
6614 
6615 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6616 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6617 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6618 
6619 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6620 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6621 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6622 		req->enables |=
6623 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6624 		req->hds_threshold = cpu_to_le16(hds_thresh);
6625 	}
6626 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6627 	return hwrm_req_send(bp, req);
6628 }
6629 
6630 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6631 					struct bnxt_vnic_info *vnic,
6632 					u16 ctx_idx)
6633 {
6634 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6635 
6636 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6637 		return;
6638 
6639 	req->rss_cos_lb_ctx_id =
6640 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6641 
6642 	hwrm_req_send(bp, req);
6643 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6644 }
6645 
6646 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6647 {
6648 	int i, j;
6649 
6650 	for (i = 0; i < bp->nr_vnics; i++) {
6651 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6652 
6653 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6654 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6655 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6656 		}
6657 	}
6658 	bp->rsscos_nr_ctxs = 0;
6659 }
6660 
6661 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6662 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6663 {
6664 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6665 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6666 	int rc;
6667 
6668 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6669 	if (rc)
6670 		return rc;
6671 
6672 	resp = hwrm_req_hold(bp, req);
6673 	rc = hwrm_req_send(bp, req);
6674 	if (!rc)
6675 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6676 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6677 	hwrm_req_drop(bp, req);
6678 
6679 	return rc;
6680 }
6681 
6682 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6683 {
6684 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6685 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6686 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6687 }
6688 
6689 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6690 {
6691 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6692 	struct hwrm_vnic_cfg_input *req;
6693 	unsigned int ring = 0, grp_idx;
6694 	u16 def_vlan = 0;
6695 	int rc;
6696 
6697 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6698 	if (rc)
6699 		return rc;
6700 
6701 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6702 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6703 
6704 		req->default_rx_ring_id =
6705 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6706 		req->default_cmpl_ring_id =
6707 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6708 		req->enables =
6709 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6710 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6711 		goto vnic_mru;
6712 	}
6713 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6714 	/* Only RSS support for now TBD: COS & LB */
6715 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6716 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6717 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6718 					   VNIC_CFG_REQ_ENABLES_MRU);
6719 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6720 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6721 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6722 					   VNIC_CFG_REQ_ENABLES_MRU);
6723 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6724 	} else {
6725 		req->rss_rule = cpu_to_le16(0xffff);
6726 	}
6727 
6728 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6729 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6730 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6731 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6732 	} else {
6733 		req->cos_rule = cpu_to_le16(0xffff);
6734 	}
6735 
6736 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6737 		ring = 0;
6738 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6739 		ring = vnic->vnic_id - 1;
6740 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6741 		ring = bp->rx_nr_rings - 1;
6742 
6743 	grp_idx = bp->rx_ring[ring].bnapi->index;
6744 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6745 	req->lb_rule = cpu_to_le16(0xffff);
6746 vnic_mru:
6747 	vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
6748 	req->mru = cpu_to_le16(vnic->mru);
6749 
6750 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6751 #ifdef CONFIG_BNXT_SRIOV
6752 	if (BNXT_VF(bp))
6753 		def_vlan = bp->vf.vlan;
6754 #endif
6755 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6756 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6757 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6758 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6759 
6760 	return hwrm_req_send(bp, req);
6761 }
6762 
6763 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6764 				    struct bnxt_vnic_info *vnic)
6765 {
6766 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6767 		struct hwrm_vnic_free_input *req;
6768 
6769 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6770 			return;
6771 
6772 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6773 
6774 		hwrm_req_send(bp, req);
6775 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6776 	}
6777 }
6778 
6779 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6780 {
6781 	u16 i;
6782 
6783 	for (i = 0; i < bp->nr_vnics; i++)
6784 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6785 }
6786 
6787 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6788 			 unsigned int start_rx_ring_idx,
6789 			 unsigned int nr_rings)
6790 {
6791 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6792 	struct hwrm_vnic_alloc_output *resp;
6793 	struct hwrm_vnic_alloc_input *req;
6794 	int rc;
6795 
6796 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6797 	if (rc)
6798 		return rc;
6799 
6800 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6801 		goto vnic_no_ring_grps;
6802 
6803 	/* map ring groups to this vnic */
6804 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6805 		grp_idx = bp->rx_ring[i].bnapi->index;
6806 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6807 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6808 				   j, nr_rings);
6809 			break;
6810 		}
6811 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6812 	}
6813 
6814 vnic_no_ring_grps:
6815 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6816 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6817 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6818 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6819 
6820 	resp = hwrm_req_hold(bp, req);
6821 	rc = hwrm_req_send(bp, req);
6822 	if (!rc)
6823 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6824 	hwrm_req_drop(bp, req);
6825 	return rc;
6826 }
6827 
6828 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6829 {
6830 	struct hwrm_vnic_qcaps_output *resp;
6831 	struct hwrm_vnic_qcaps_input *req;
6832 	int rc;
6833 
6834 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6835 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6836 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6837 	if (bp->hwrm_spec_code < 0x10600)
6838 		return 0;
6839 
6840 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6841 	if (rc)
6842 		return rc;
6843 
6844 	resp = hwrm_req_hold(bp, req);
6845 	rc = hwrm_req_send(bp, req);
6846 	if (!rc) {
6847 		u32 flags = le32_to_cpu(resp->flags);
6848 
6849 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6850 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6851 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6852 		if (flags &
6853 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6854 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6855 
6856 		/* Older P5 fw before EXT_HW_STATS support did not set
6857 		 * VLAN_STRIP_CAP properly.
6858 		 */
6859 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6860 		    (BNXT_CHIP_P5(bp) &&
6861 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6862 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6863 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6864 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6865 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6866 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6867 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6868 		if (bp->max_tpa_v2) {
6869 			if (BNXT_CHIP_P5(bp))
6870 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6871 			else
6872 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6873 		}
6874 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6875 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6876 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6877 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6878 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6879 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6880 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6881 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6882 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6883 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6884 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6885 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6886 	}
6887 	hwrm_req_drop(bp, req);
6888 	return rc;
6889 }
6890 
6891 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6892 {
6893 	struct hwrm_ring_grp_alloc_output *resp;
6894 	struct hwrm_ring_grp_alloc_input *req;
6895 	int rc;
6896 	u16 i;
6897 
6898 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6899 		return 0;
6900 
6901 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6902 	if (rc)
6903 		return rc;
6904 
6905 	resp = hwrm_req_hold(bp, req);
6906 	for (i = 0; i < bp->rx_nr_rings; i++) {
6907 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6908 
6909 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6910 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6911 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6912 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6913 
6914 		rc = hwrm_req_send(bp, req);
6915 
6916 		if (rc)
6917 			break;
6918 
6919 		bp->grp_info[grp_idx].fw_grp_id =
6920 			le32_to_cpu(resp->ring_group_id);
6921 	}
6922 	hwrm_req_drop(bp, req);
6923 	return rc;
6924 }
6925 
6926 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6927 {
6928 	struct hwrm_ring_grp_free_input *req;
6929 	u16 i;
6930 
6931 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6932 		return;
6933 
6934 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6935 		return;
6936 
6937 	hwrm_req_hold(bp, req);
6938 	for (i = 0; i < bp->cp_nr_rings; i++) {
6939 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
6940 			continue;
6941 		req->ring_group_id =
6942 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
6943 
6944 		hwrm_req_send(bp, req);
6945 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
6946 	}
6947 	hwrm_req_drop(bp, req);
6948 }
6949 
6950 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
6951 				       struct hwrm_ring_alloc_input *req,
6952 				       struct bnxt_ring_struct *ring)
6953 {
6954 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
6955 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
6956 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
6957 
6958 	if (ring_type == HWRM_RING_ALLOC_AGG) {
6959 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
6960 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
6961 		req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
6962 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
6963 	} else {
6964 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
6965 		if (NET_IP_ALIGN == 2)
6966 			req->flags =
6967 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
6968 	}
6969 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6970 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
6971 	req->enables |= cpu_to_le32(enables);
6972 }
6973 
6974 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
6975 				    struct bnxt_ring_struct *ring,
6976 				    u32 ring_type, u32 map_index)
6977 {
6978 	struct hwrm_ring_alloc_output *resp;
6979 	struct hwrm_ring_alloc_input *req;
6980 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
6981 	struct bnxt_ring_grp_info *grp_info;
6982 	int rc, err = 0;
6983 	u16 ring_id;
6984 
6985 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
6986 	if (rc)
6987 		goto exit;
6988 
6989 	req->enables = 0;
6990 	if (rmem->nr_pages > 1) {
6991 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
6992 		/* Page size is in log2 units */
6993 		req->page_size = BNXT_PAGE_SHIFT;
6994 		req->page_tbl_depth = 1;
6995 	} else {
6996 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
6997 	}
6998 	req->fbo = 0;
6999 	/* Association of ring index with doorbell index and MSIX number */
7000 	req->logical_id = cpu_to_le16(map_index);
7001 
7002 	switch (ring_type) {
7003 	case HWRM_RING_ALLOC_TX: {
7004 		struct bnxt_tx_ring_info *txr;
7005 		u16 flags = 0;
7006 
7007 		txr = container_of(ring, struct bnxt_tx_ring_info,
7008 				   tx_ring_struct);
7009 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7010 		/* Association of transmit ring with completion ring */
7011 		grp_info = &bp->grp_info[ring->grp_idx];
7012 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7013 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7014 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7015 		req->queue_id = cpu_to_le16(ring->queue_id);
7016 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7017 			req->cmpl_coal_cnt =
7018 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7019 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7020 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7021 		req->flags = cpu_to_le16(flags);
7022 		break;
7023 	}
7024 	case HWRM_RING_ALLOC_RX:
7025 	case HWRM_RING_ALLOC_AGG:
7026 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7027 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7028 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7029 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7030 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7031 			bnxt_set_rx_ring_params_p5(bp, ring_type, req, ring);
7032 		break;
7033 	case HWRM_RING_ALLOC_CMPL:
7034 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7035 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7036 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7037 			/* Association of cp ring with nq */
7038 			grp_info = &bp->grp_info[map_index];
7039 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7040 			req->cq_handle = cpu_to_le64(ring->handle);
7041 			req->enables |= cpu_to_le32(
7042 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7043 		} else {
7044 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7045 		}
7046 		break;
7047 	case HWRM_RING_ALLOC_NQ:
7048 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7049 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7050 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7051 		break;
7052 	default:
7053 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7054 			   ring_type);
7055 		return -1;
7056 	}
7057 
7058 	resp = hwrm_req_hold(bp, req);
7059 	rc = hwrm_req_send(bp, req);
7060 	err = le16_to_cpu(resp->error_code);
7061 	ring_id = le16_to_cpu(resp->ring_id);
7062 	hwrm_req_drop(bp, req);
7063 
7064 exit:
7065 	if (rc || err) {
7066 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7067 			   ring_type, rc, err);
7068 		return -EIO;
7069 	}
7070 	ring->fw_ring_id = ring_id;
7071 	return rc;
7072 }
7073 
7074 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7075 {
7076 	int rc;
7077 
7078 	if (BNXT_PF(bp)) {
7079 		struct hwrm_func_cfg_input *req;
7080 
7081 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7082 		if (rc)
7083 			return rc;
7084 
7085 		req->fid = cpu_to_le16(0xffff);
7086 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7087 		req->async_event_cr = cpu_to_le16(idx);
7088 		return hwrm_req_send(bp, req);
7089 	} else {
7090 		struct hwrm_func_vf_cfg_input *req;
7091 
7092 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7093 		if (rc)
7094 			return rc;
7095 
7096 		req->enables =
7097 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7098 		req->async_event_cr = cpu_to_le16(idx);
7099 		return hwrm_req_send(bp, req);
7100 	}
7101 }
7102 
7103 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7104 			     u32 ring_type)
7105 {
7106 	switch (ring_type) {
7107 	case HWRM_RING_ALLOC_TX:
7108 		db->db_ring_mask = bp->tx_ring_mask;
7109 		break;
7110 	case HWRM_RING_ALLOC_RX:
7111 		db->db_ring_mask = bp->rx_ring_mask;
7112 		break;
7113 	case HWRM_RING_ALLOC_AGG:
7114 		db->db_ring_mask = bp->rx_agg_ring_mask;
7115 		break;
7116 	case HWRM_RING_ALLOC_CMPL:
7117 	case HWRM_RING_ALLOC_NQ:
7118 		db->db_ring_mask = bp->cp_ring_mask;
7119 		break;
7120 	}
7121 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7122 		db->db_epoch_mask = db->db_ring_mask + 1;
7123 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7124 	}
7125 }
7126 
7127 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7128 			u32 map_idx, u32 xid)
7129 {
7130 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7131 		switch (ring_type) {
7132 		case HWRM_RING_ALLOC_TX:
7133 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7134 			break;
7135 		case HWRM_RING_ALLOC_RX:
7136 		case HWRM_RING_ALLOC_AGG:
7137 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7138 			break;
7139 		case HWRM_RING_ALLOC_CMPL:
7140 			db->db_key64 = DBR_PATH_L2;
7141 			break;
7142 		case HWRM_RING_ALLOC_NQ:
7143 			db->db_key64 = DBR_PATH_L2;
7144 			break;
7145 		}
7146 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7147 
7148 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7149 			db->db_key64 |= DBR_VALID;
7150 
7151 		db->doorbell = bp->bar1 + bp->db_offset;
7152 	} else {
7153 		db->doorbell = bp->bar1 + map_idx * 0x80;
7154 		switch (ring_type) {
7155 		case HWRM_RING_ALLOC_TX:
7156 			db->db_key32 = DB_KEY_TX;
7157 			break;
7158 		case HWRM_RING_ALLOC_RX:
7159 		case HWRM_RING_ALLOC_AGG:
7160 			db->db_key32 = DB_KEY_RX;
7161 			break;
7162 		case HWRM_RING_ALLOC_CMPL:
7163 			db->db_key32 = DB_KEY_CP;
7164 			break;
7165 		}
7166 	}
7167 	bnxt_set_db_mask(bp, db, ring_type);
7168 }
7169 
7170 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7171 				   struct bnxt_rx_ring_info *rxr)
7172 {
7173 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7174 	struct bnxt_napi *bnapi = rxr->bnapi;
7175 	u32 type = HWRM_RING_ALLOC_RX;
7176 	u32 map_idx = bnapi->index;
7177 	int rc;
7178 
7179 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7180 	if (rc)
7181 		return rc;
7182 
7183 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7184 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7185 
7186 	return 0;
7187 }
7188 
7189 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7190 				       struct bnxt_rx_ring_info *rxr)
7191 {
7192 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7193 	u32 type = HWRM_RING_ALLOC_AGG;
7194 	u32 grp_idx = ring->grp_idx;
7195 	u32 map_idx;
7196 	int rc;
7197 
7198 	map_idx = grp_idx + bp->rx_nr_rings;
7199 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7200 	if (rc)
7201 		return rc;
7202 
7203 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7204 		    ring->fw_ring_id);
7205 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7206 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7207 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7208 
7209 	return 0;
7210 }
7211 
7212 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7213 				      struct bnxt_cp_ring_info *cpr)
7214 {
7215 	const u32 type = HWRM_RING_ALLOC_CMPL;
7216 	struct bnxt_napi *bnapi = cpr->bnapi;
7217 	struct bnxt_ring_struct *ring;
7218 	u32 map_idx = bnapi->index;
7219 	int rc;
7220 
7221 	ring = &cpr->cp_ring_struct;
7222 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7223 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7224 	if (rc)
7225 		return rc;
7226 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7227 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7228 	return 0;
7229 }
7230 
7231 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7232 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7233 {
7234 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7235 	const u32 type = HWRM_RING_ALLOC_TX;
7236 	int rc;
7237 
7238 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, tx_idx);
7239 	if (rc)
7240 		return rc;
7241 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7242 	return 0;
7243 }
7244 
7245 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7246 {
7247 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7248 	int i, rc = 0;
7249 	u32 type;
7250 
7251 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7252 		type = HWRM_RING_ALLOC_NQ;
7253 	else
7254 		type = HWRM_RING_ALLOC_CMPL;
7255 	for (i = 0; i < bp->cp_nr_rings; i++) {
7256 		struct bnxt_napi *bnapi = bp->bnapi[i];
7257 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7258 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7259 		u32 map_idx = ring->map_idx;
7260 		unsigned int vector;
7261 
7262 		vector = bp->irq_tbl[map_idx].vector;
7263 		disable_irq_nosync(vector);
7264 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7265 		if (rc) {
7266 			enable_irq(vector);
7267 			goto err_out;
7268 		}
7269 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7270 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7271 		enable_irq(vector);
7272 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7273 
7274 		if (!i) {
7275 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7276 			if (rc)
7277 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7278 		}
7279 	}
7280 
7281 	for (i = 0; i < bp->tx_nr_rings; i++) {
7282 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7283 
7284 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7285 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7286 			if (rc)
7287 				goto err_out;
7288 		}
7289 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7290 		if (rc)
7291 			goto err_out;
7292 	}
7293 
7294 	for (i = 0; i < bp->rx_nr_rings; i++) {
7295 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7296 
7297 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7298 		if (rc)
7299 			goto err_out;
7300 		/* If we have agg rings, post agg buffers first. */
7301 		if (!agg_rings)
7302 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7303 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7304 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7305 			if (rc)
7306 				goto err_out;
7307 		}
7308 	}
7309 
7310 	if (agg_rings) {
7311 		for (i = 0; i < bp->rx_nr_rings; i++) {
7312 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7313 			if (rc)
7314 				goto err_out;
7315 		}
7316 	}
7317 err_out:
7318 	return rc;
7319 }
7320 
7321 static void bnxt_cancel_dim(struct bnxt *bp)
7322 {
7323 	int i;
7324 
7325 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7326 	 * if NAPI is enabled.
7327 	 */
7328 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7329 		return;
7330 
7331 	/* Make sure NAPI sees that the VNIC is disabled */
7332 	synchronize_net();
7333 	for (i = 0; i < bp->rx_nr_rings; i++) {
7334 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7335 		struct bnxt_napi *bnapi = rxr->bnapi;
7336 
7337 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7338 	}
7339 }
7340 
7341 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7342 				   struct bnxt_ring_struct *ring,
7343 				   u32 ring_type, int cmpl_ring_id)
7344 {
7345 	struct hwrm_ring_free_output *resp;
7346 	struct hwrm_ring_free_input *req;
7347 	u16 error_code = 0;
7348 	int rc;
7349 
7350 	if (BNXT_NO_FW_ACCESS(bp))
7351 		return 0;
7352 
7353 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7354 	if (rc)
7355 		goto exit;
7356 
7357 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7358 	req->ring_type = ring_type;
7359 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7360 
7361 	resp = hwrm_req_hold(bp, req);
7362 	rc = hwrm_req_send(bp, req);
7363 	error_code = le16_to_cpu(resp->error_code);
7364 	hwrm_req_drop(bp, req);
7365 exit:
7366 	if (rc || error_code) {
7367 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7368 			   ring_type, rc, error_code);
7369 		return -EIO;
7370 	}
7371 	return 0;
7372 }
7373 
7374 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7375 				   struct bnxt_tx_ring_info *txr,
7376 				   bool close_path)
7377 {
7378 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7379 	u32 cmpl_ring_id;
7380 
7381 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7382 		return;
7383 
7384 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7385 		       INVALID_HW_RING_ID;
7386 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7387 				cmpl_ring_id);
7388 	ring->fw_ring_id = INVALID_HW_RING_ID;
7389 }
7390 
7391 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7392 				   struct bnxt_rx_ring_info *rxr,
7393 				   bool close_path)
7394 {
7395 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7396 	u32 grp_idx = rxr->bnapi->index;
7397 	u32 cmpl_ring_id;
7398 
7399 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7400 		return;
7401 
7402 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7403 	hwrm_ring_free_send_msg(bp, ring,
7404 				RING_FREE_REQ_RING_TYPE_RX,
7405 				close_path ? cmpl_ring_id :
7406 				INVALID_HW_RING_ID);
7407 	ring->fw_ring_id = INVALID_HW_RING_ID;
7408 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7409 }
7410 
7411 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7412 				       struct bnxt_rx_ring_info *rxr,
7413 				       bool close_path)
7414 {
7415 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7416 	u32 grp_idx = rxr->bnapi->index;
7417 	u32 type, cmpl_ring_id;
7418 
7419 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7420 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7421 	else
7422 		type = RING_FREE_REQ_RING_TYPE_RX;
7423 
7424 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7425 		return;
7426 
7427 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7428 	hwrm_ring_free_send_msg(bp, ring, type,
7429 				close_path ? cmpl_ring_id :
7430 				INVALID_HW_RING_ID);
7431 	ring->fw_ring_id = INVALID_HW_RING_ID;
7432 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7433 }
7434 
7435 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7436 				   struct bnxt_cp_ring_info *cpr)
7437 {
7438 	struct bnxt_ring_struct *ring;
7439 
7440 	ring = &cpr->cp_ring_struct;
7441 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7442 		return;
7443 
7444 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7445 				INVALID_HW_RING_ID);
7446 	ring->fw_ring_id = INVALID_HW_RING_ID;
7447 }
7448 
7449 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7450 {
7451 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7452 	int i, size = ring->ring_mem.page_size;
7453 
7454 	cpr->cp_raw_cons = 0;
7455 	cpr->toggle = 0;
7456 
7457 	for (i = 0; i < bp->cp_nr_pages; i++)
7458 		if (cpr->cp_desc_ring[i])
7459 			memset(cpr->cp_desc_ring[i], 0, size);
7460 }
7461 
7462 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7463 {
7464 	u32 type;
7465 	int i;
7466 
7467 	if (!bp->bnapi)
7468 		return;
7469 
7470 	for (i = 0; i < bp->tx_nr_rings; i++)
7471 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7472 
7473 	bnxt_cancel_dim(bp);
7474 	for (i = 0; i < bp->rx_nr_rings; i++) {
7475 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7476 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7477 	}
7478 
7479 	/* The completion rings are about to be freed.  After that the
7480 	 * IRQ doorbell will not work anymore.  So we need to disable
7481 	 * IRQ here.
7482 	 */
7483 	bnxt_disable_int_sync(bp);
7484 
7485 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7486 		type = RING_FREE_REQ_RING_TYPE_NQ;
7487 	else
7488 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7489 	for (i = 0; i < bp->cp_nr_rings; i++) {
7490 		struct bnxt_napi *bnapi = bp->bnapi[i];
7491 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7492 		struct bnxt_ring_struct *ring;
7493 		int j;
7494 
7495 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7496 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7497 
7498 		ring = &cpr->cp_ring_struct;
7499 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7500 			hwrm_ring_free_send_msg(bp, ring, type,
7501 						INVALID_HW_RING_ID);
7502 			ring->fw_ring_id = INVALID_HW_RING_ID;
7503 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7504 		}
7505 	}
7506 }
7507 
7508 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7509 			     bool shared);
7510 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7511 			   bool shared);
7512 
7513 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7514 {
7515 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7516 	struct hwrm_func_qcfg_output *resp;
7517 	struct hwrm_func_qcfg_input *req;
7518 	int rc;
7519 
7520 	if (bp->hwrm_spec_code < 0x10601)
7521 		return 0;
7522 
7523 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7524 	if (rc)
7525 		return rc;
7526 
7527 	req->fid = cpu_to_le16(0xffff);
7528 	resp = hwrm_req_hold(bp, req);
7529 	rc = hwrm_req_send(bp, req);
7530 	if (rc) {
7531 		hwrm_req_drop(bp, req);
7532 		return rc;
7533 	}
7534 
7535 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7536 	if (BNXT_NEW_RM(bp)) {
7537 		u16 cp, stats;
7538 
7539 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7540 		hw_resc->resv_hw_ring_grps =
7541 			le32_to_cpu(resp->alloc_hw_ring_grps);
7542 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7543 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7544 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7545 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7546 		hw_resc->resv_irqs = cp;
7547 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7548 			int rx = hw_resc->resv_rx_rings;
7549 			int tx = hw_resc->resv_tx_rings;
7550 
7551 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7552 				rx >>= 1;
7553 			if (cp < (rx + tx)) {
7554 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7555 				if (rc)
7556 					goto get_rings_exit;
7557 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7558 					rx <<= 1;
7559 				hw_resc->resv_rx_rings = rx;
7560 				hw_resc->resv_tx_rings = tx;
7561 			}
7562 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7563 			hw_resc->resv_hw_ring_grps = rx;
7564 		}
7565 		hw_resc->resv_cp_rings = cp;
7566 		hw_resc->resv_stat_ctxs = stats;
7567 	}
7568 get_rings_exit:
7569 	hwrm_req_drop(bp, req);
7570 	return rc;
7571 }
7572 
7573 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7574 {
7575 	struct hwrm_func_qcfg_output *resp;
7576 	struct hwrm_func_qcfg_input *req;
7577 	int rc;
7578 
7579 	if (bp->hwrm_spec_code < 0x10601)
7580 		return 0;
7581 
7582 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7583 	if (rc)
7584 		return rc;
7585 
7586 	req->fid = cpu_to_le16(fid);
7587 	resp = hwrm_req_hold(bp, req);
7588 	rc = hwrm_req_send(bp, req);
7589 	if (!rc)
7590 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7591 
7592 	hwrm_req_drop(bp, req);
7593 	return rc;
7594 }
7595 
7596 static bool bnxt_rfs_supported(struct bnxt *bp);
7597 
7598 static struct hwrm_func_cfg_input *
7599 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7600 {
7601 	struct hwrm_func_cfg_input *req;
7602 	u32 enables = 0;
7603 
7604 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7605 		return NULL;
7606 
7607 	req->fid = cpu_to_le16(0xffff);
7608 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7609 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7610 	if (BNXT_NEW_RM(bp)) {
7611 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7612 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7613 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7614 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7615 			enables |= hwr->cp_p5 ?
7616 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7617 		} else {
7618 			enables |= hwr->cp ?
7619 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7620 			enables |= hwr->grp ?
7621 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7622 		}
7623 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7624 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7625 					  0;
7626 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7627 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7628 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7629 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7630 			req->num_msix = cpu_to_le16(hwr->cp);
7631 		} else {
7632 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7633 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7634 		}
7635 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7636 		req->num_vnics = cpu_to_le16(hwr->vnic);
7637 	}
7638 	req->enables = cpu_to_le32(enables);
7639 	return req;
7640 }
7641 
7642 static struct hwrm_func_vf_cfg_input *
7643 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7644 {
7645 	struct hwrm_func_vf_cfg_input *req;
7646 	u32 enables = 0;
7647 
7648 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7649 		return NULL;
7650 
7651 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7652 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7653 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7654 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7655 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7656 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7657 		enables |= hwr->cp_p5 ?
7658 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7659 	} else {
7660 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7661 		enables |= hwr->grp ?
7662 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7663 	}
7664 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7665 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7666 
7667 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7668 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7669 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7670 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7671 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7672 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7673 	} else {
7674 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7675 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7676 	}
7677 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7678 	req->num_vnics = cpu_to_le16(hwr->vnic);
7679 
7680 	req->enables = cpu_to_le32(enables);
7681 	return req;
7682 }
7683 
7684 static int
7685 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7686 {
7687 	struct hwrm_func_cfg_input *req;
7688 	int rc;
7689 
7690 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7691 	if (!req)
7692 		return -ENOMEM;
7693 
7694 	if (!req->enables) {
7695 		hwrm_req_drop(bp, req);
7696 		return 0;
7697 	}
7698 
7699 	rc = hwrm_req_send(bp, req);
7700 	if (rc)
7701 		return rc;
7702 
7703 	if (bp->hwrm_spec_code < 0x10601)
7704 		bp->hw_resc.resv_tx_rings = hwr->tx;
7705 
7706 	return bnxt_hwrm_get_rings(bp);
7707 }
7708 
7709 static int
7710 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7711 {
7712 	struct hwrm_func_vf_cfg_input *req;
7713 	int rc;
7714 
7715 	if (!BNXT_NEW_RM(bp)) {
7716 		bp->hw_resc.resv_tx_rings = hwr->tx;
7717 		return 0;
7718 	}
7719 
7720 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7721 	if (!req)
7722 		return -ENOMEM;
7723 
7724 	rc = hwrm_req_send(bp, req);
7725 	if (rc)
7726 		return rc;
7727 
7728 	return bnxt_hwrm_get_rings(bp);
7729 }
7730 
7731 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7732 {
7733 	if (BNXT_PF(bp))
7734 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7735 	else
7736 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7737 }
7738 
7739 int bnxt_nq_rings_in_use(struct bnxt *bp)
7740 {
7741 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7742 }
7743 
7744 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7745 {
7746 	int cp;
7747 
7748 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7749 		return bnxt_nq_rings_in_use(bp);
7750 
7751 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7752 	return cp;
7753 }
7754 
7755 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7756 {
7757 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7758 }
7759 
7760 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7761 {
7762 	if (!hwr->grp)
7763 		return 0;
7764 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7765 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7766 
7767 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7768 			rss_ctx *= hwr->vnic;
7769 		return rss_ctx;
7770 	}
7771 	if (BNXT_VF(bp))
7772 		return BNXT_VF_MAX_RSS_CTX;
7773 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7774 		return hwr->grp + 1;
7775 	return 1;
7776 }
7777 
7778 /* Check if a default RSS map needs to be setup.  This function is only
7779  * used on older firmware that does not require reserving RX rings.
7780  */
7781 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7782 {
7783 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7784 
7785 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7786 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7787 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7788 		if (!netif_is_rxfh_configured(bp->dev))
7789 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7790 	}
7791 }
7792 
7793 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7794 {
7795 	if (bp->flags & BNXT_FLAG_RFS) {
7796 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7797 			return 2 + bp->num_rss_ctx;
7798 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7799 			return rx_rings + 1;
7800 	}
7801 	return 1;
7802 }
7803 
7804 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7805 {
7806 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7807 	int cp = bnxt_cp_rings_in_use(bp);
7808 	int nq = bnxt_nq_rings_in_use(bp);
7809 	int rx = bp->rx_nr_rings, stat;
7810 	int vnic, grp = rx;
7811 
7812 	/* Old firmware does not need RX ring reservations but we still
7813 	 * need to setup a default RSS map when needed.  With new firmware
7814 	 * we go through RX ring reservations first and then set up the
7815 	 * RSS map for the successfully reserved RX rings when needed.
7816 	 */
7817 	if (!BNXT_NEW_RM(bp))
7818 		bnxt_check_rss_tbl_no_rmgr(bp);
7819 
7820 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7821 	    bp->hwrm_spec_code >= 0x10601)
7822 		return true;
7823 
7824 	if (!BNXT_NEW_RM(bp))
7825 		return false;
7826 
7827 	vnic = bnxt_get_total_vnics(bp, rx);
7828 
7829 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7830 		rx <<= 1;
7831 	stat = bnxt_get_func_stat_ctxs(bp);
7832 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7833 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7834 	    (hw_resc->resv_hw_ring_grps != grp &&
7835 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7836 		return true;
7837 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7838 	    hw_resc->resv_irqs != nq)
7839 		return true;
7840 	return false;
7841 }
7842 
7843 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7844 {
7845 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7846 
7847 	hwr->tx = hw_resc->resv_tx_rings;
7848 	if (BNXT_NEW_RM(bp)) {
7849 		hwr->rx = hw_resc->resv_rx_rings;
7850 		hwr->cp = hw_resc->resv_irqs;
7851 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7852 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7853 		hwr->grp = hw_resc->resv_hw_ring_grps;
7854 		hwr->vnic = hw_resc->resv_vnics;
7855 		hwr->stat = hw_resc->resv_stat_ctxs;
7856 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7857 	}
7858 }
7859 
7860 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7861 {
7862 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7863 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7864 }
7865 
7866 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7867 
7868 static int __bnxt_reserve_rings(struct bnxt *bp)
7869 {
7870 	struct bnxt_hw_rings hwr = {0};
7871 	int rx_rings, old_rx_rings, rc;
7872 	int cp = bp->cp_nr_rings;
7873 	int ulp_msix = 0;
7874 	bool sh = false;
7875 	int tx_cp;
7876 
7877 	if (!bnxt_need_reserve_rings(bp))
7878 		return 0;
7879 
7880 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7881 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7882 		if (!ulp_msix)
7883 			bnxt_set_ulp_stat_ctxs(bp, 0);
7884 
7885 		if (ulp_msix > bp->ulp_num_msix_want)
7886 			ulp_msix = bp->ulp_num_msix_want;
7887 		hwr.cp = cp + ulp_msix;
7888 	} else {
7889 		hwr.cp = bnxt_nq_rings_in_use(bp);
7890 	}
7891 
7892 	hwr.tx = bp->tx_nr_rings;
7893 	hwr.rx = bp->rx_nr_rings;
7894 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7895 		sh = true;
7896 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7897 		hwr.cp_p5 = hwr.rx + hwr.tx;
7898 
7899 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7900 
7901 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7902 		hwr.rx <<= 1;
7903 	hwr.grp = bp->rx_nr_rings;
7904 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7905 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7906 	old_rx_rings = bp->hw_resc.resv_rx_rings;
7907 
7908 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7909 	if (rc)
7910 		return rc;
7911 
7912 	bnxt_copy_reserved_rings(bp, &hwr);
7913 
7914 	rx_rings = hwr.rx;
7915 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7916 		if (hwr.rx >= 2) {
7917 			rx_rings = hwr.rx >> 1;
7918 		} else {
7919 			if (netif_running(bp->dev))
7920 				return -ENOMEM;
7921 
7922 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7923 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7924 			bp->dev->hw_features &= ~NETIF_F_LRO;
7925 			bp->dev->features &= ~NETIF_F_LRO;
7926 			bnxt_set_ring_params(bp);
7927 		}
7928 	}
7929 	rx_rings = min_t(int, rx_rings, hwr.grp);
7930 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7931 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7932 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7933 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7934 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7935 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7936 		hwr.rx = rx_rings << 1;
7937 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
7938 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
7939 	bp->tx_nr_rings = hwr.tx;
7940 
7941 	/* If we cannot reserve all the RX rings, reset the RSS map only
7942 	 * if absolutely necessary
7943 	 */
7944 	if (rx_rings != bp->rx_nr_rings) {
7945 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
7946 			    rx_rings, bp->rx_nr_rings);
7947 		if (netif_is_rxfh_configured(bp->dev) &&
7948 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
7949 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
7950 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
7951 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
7952 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
7953 		}
7954 	}
7955 	bp->rx_nr_rings = rx_rings;
7956 	bp->cp_nr_rings = hwr.cp;
7957 
7958 	if (!bnxt_rings_ok(bp, &hwr))
7959 		return -ENOMEM;
7960 
7961 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
7962 	    !netif_is_rxfh_configured(bp->dev))
7963 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7964 
7965 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
7966 		int resv_msix, resv_ctx, ulp_ctxs;
7967 		struct bnxt_hw_resc *hw_resc;
7968 
7969 		hw_resc = &bp->hw_resc;
7970 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
7971 		ulp_msix = min_t(int, resv_msix, ulp_msix);
7972 		bnxt_set_ulp_msix_num(bp, ulp_msix);
7973 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
7974 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
7975 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
7976 	}
7977 
7978 	return rc;
7979 }
7980 
7981 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7982 {
7983 	struct hwrm_func_vf_cfg_input *req;
7984 	u32 flags;
7985 
7986 	if (!BNXT_NEW_RM(bp))
7987 		return 0;
7988 
7989 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7990 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
7991 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7992 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7993 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7994 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
7995 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
7996 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7997 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7998 
7999 	req->flags = cpu_to_le32(flags);
8000 	return hwrm_req_send_silent(bp, req);
8001 }
8002 
8003 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8004 {
8005 	struct hwrm_func_cfg_input *req;
8006 	u32 flags;
8007 
8008 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8009 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8010 	if (BNXT_NEW_RM(bp)) {
8011 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8012 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8013 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8014 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8015 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8016 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8017 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8018 		else
8019 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8020 	}
8021 
8022 	req->flags = cpu_to_le32(flags);
8023 	return hwrm_req_send_silent(bp, req);
8024 }
8025 
8026 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8027 {
8028 	if (bp->hwrm_spec_code < 0x10801)
8029 		return 0;
8030 
8031 	if (BNXT_PF(bp))
8032 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8033 
8034 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8035 }
8036 
8037 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8038 {
8039 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8040 	struct hwrm_ring_aggint_qcaps_output *resp;
8041 	struct hwrm_ring_aggint_qcaps_input *req;
8042 	int rc;
8043 
8044 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8045 	coal_cap->num_cmpl_dma_aggr_max = 63;
8046 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8047 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8048 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8049 	coal_cap->int_lat_tmr_min_max = 65535;
8050 	coal_cap->int_lat_tmr_max_max = 65535;
8051 	coal_cap->num_cmpl_aggr_int_max = 65535;
8052 	coal_cap->timer_units = 80;
8053 
8054 	if (bp->hwrm_spec_code < 0x10902)
8055 		return;
8056 
8057 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8058 		return;
8059 
8060 	resp = hwrm_req_hold(bp, req);
8061 	rc = hwrm_req_send_silent(bp, req);
8062 	if (!rc) {
8063 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8064 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8065 		coal_cap->num_cmpl_dma_aggr_max =
8066 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8067 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8068 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8069 		coal_cap->cmpl_aggr_dma_tmr_max =
8070 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8071 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8072 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8073 		coal_cap->int_lat_tmr_min_max =
8074 			le16_to_cpu(resp->int_lat_tmr_min_max);
8075 		coal_cap->int_lat_tmr_max_max =
8076 			le16_to_cpu(resp->int_lat_tmr_max_max);
8077 		coal_cap->num_cmpl_aggr_int_max =
8078 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8079 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8080 	}
8081 	hwrm_req_drop(bp, req);
8082 }
8083 
8084 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8085 {
8086 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8087 
8088 	return usec * 1000 / coal_cap->timer_units;
8089 }
8090 
8091 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8092 	struct bnxt_coal *hw_coal,
8093 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8094 {
8095 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8096 	u16 val, tmr, max, flags = hw_coal->flags;
8097 	u32 cmpl_params = coal_cap->cmpl_params;
8098 
8099 	max = hw_coal->bufs_per_record * 128;
8100 	if (hw_coal->budget)
8101 		max = hw_coal->bufs_per_record * hw_coal->budget;
8102 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8103 
8104 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8105 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8106 
8107 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8108 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8109 
8110 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8111 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8112 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8113 
8114 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8115 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8116 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8117 
8118 	/* min timer set to 1/2 of interrupt timer */
8119 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8120 		val = tmr / 2;
8121 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8122 		req->int_lat_tmr_min = cpu_to_le16(val);
8123 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8124 	}
8125 
8126 	/* buf timer set to 1/4 of interrupt timer */
8127 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8128 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8129 
8130 	if (cmpl_params &
8131 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8132 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8133 		val = clamp_t(u16, tmr, 1,
8134 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8135 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8136 		req->enables |=
8137 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8138 	}
8139 
8140 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8141 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8142 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8143 	req->flags = cpu_to_le16(flags);
8144 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8145 }
8146 
8147 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8148 				   struct bnxt_coal *hw_coal)
8149 {
8150 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8151 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8152 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8153 	u32 nq_params = coal_cap->nq_params;
8154 	u16 tmr;
8155 	int rc;
8156 
8157 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8158 		return 0;
8159 
8160 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8161 	if (rc)
8162 		return rc;
8163 
8164 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8165 	req->flags =
8166 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8167 
8168 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8169 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8170 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8171 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8172 	return hwrm_req_send(bp, req);
8173 }
8174 
8175 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8176 {
8177 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8178 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8179 	struct bnxt_coal coal;
8180 	int rc;
8181 
8182 	/* Tick values in micro seconds.
8183 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8184 	 */
8185 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8186 
8187 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8188 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8189 
8190 	if (!bnapi->rx_ring)
8191 		return -ENODEV;
8192 
8193 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8194 	if (rc)
8195 		return rc;
8196 
8197 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8198 
8199 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8200 
8201 	return hwrm_req_send(bp, req_rx);
8202 }
8203 
8204 static int
8205 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8206 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8207 {
8208 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8209 
8210 	req->ring_id = cpu_to_le16(ring_id);
8211 	return hwrm_req_send(bp, req);
8212 }
8213 
8214 static int
8215 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8216 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8217 {
8218 	struct bnxt_tx_ring_info *txr;
8219 	int i, rc;
8220 
8221 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8222 		u16 ring_id;
8223 
8224 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8225 		req->ring_id = cpu_to_le16(ring_id);
8226 		rc = hwrm_req_send(bp, req);
8227 		if (rc)
8228 			return rc;
8229 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8230 			return 0;
8231 	}
8232 	return 0;
8233 }
8234 
8235 int bnxt_hwrm_set_coal(struct bnxt *bp)
8236 {
8237 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8238 	int i, rc;
8239 
8240 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8241 	if (rc)
8242 		return rc;
8243 
8244 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8245 	if (rc) {
8246 		hwrm_req_drop(bp, req_rx);
8247 		return rc;
8248 	}
8249 
8250 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8251 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8252 
8253 	hwrm_req_hold(bp, req_rx);
8254 	hwrm_req_hold(bp, req_tx);
8255 	for (i = 0; i < bp->cp_nr_rings; i++) {
8256 		struct bnxt_napi *bnapi = bp->bnapi[i];
8257 		struct bnxt_coal *hw_coal;
8258 
8259 		if (!bnapi->rx_ring)
8260 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8261 		else
8262 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8263 		if (rc)
8264 			break;
8265 
8266 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8267 			continue;
8268 
8269 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8270 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8271 			if (rc)
8272 				break;
8273 		}
8274 		if (bnapi->rx_ring)
8275 			hw_coal = &bp->rx_coal;
8276 		else
8277 			hw_coal = &bp->tx_coal;
8278 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8279 	}
8280 	hwrm_req_drop(bp, req_rx);
8281 	hwrm_req_drop(bp, req_tx);
8282 	return rc;
8283 }
8284 
8285 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8286 {
8287 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8288 	struct hwrm_stat_ctx_free_input *req;
8289 	int i;
8290 
8291 	if (!bp->bnapi)
8292 		return;
8293 
8294 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8295 		return;
8296 
8297 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8298 		return;
8299 	if (BNXT_FW_MAJ(bp) <= 20) {
8300 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8301 			hwrm_req_drop(bp, req);
8302 			return;
8303 		}
8304 		hwrm_req_hold(bp, req0);
8305 	}
8306 	hwrm_req_hold(bp, req);
8307 	for (i = 0; i < bp->cp_nr_rings; i++) {
8308 		struct bnxt_napi *bnapi = bp->bnapi[i];
8309 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8310 
8311 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8312 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8313 			if (req0) {
8314 				req0->stat_ctx_id = req->stat_ctx_id;
8315 				hwrm_req_send(bp, req0);
8316 			}
8317 			hwrm_req_send(bp, req);
8318 
8319 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8320 		}
8321 	}
8322 	hwrm_req_drop(bp, req);
8323 	if (req0)
8324 		hwrm_req_drop(bp, req0);
8325 }
8326 
8327 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8328 {
8329 	struct hwrm_stat_ctx_alloc_output *resp;
8330 	struct hwrm_stat_ctx_alloc_input *req;
8331 	int rc, i;
8332 
8333 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8334 		return 0;
8335 
8336 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8337 	if (rc)
8338 		return rc;
8339 
8340 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8341 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8342 
8343 	resp = hwrm_req_hold(bp, req);
8344 	for (i = 0; i < bp->cp_nr_rings; i++) {
8345 		struct bnxt_napi *bnapi = bp->bnapi[i];
8346 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8347 
8348 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8349 
8350 		rc = hwrm_req_send(bp, req);
8351 		if (rc)
8352 			break;
8353 
8354 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8355 
8356 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8357 	}
8358 	hwrm_req_drop(bp, req);
8359 	return rc;
8360 }
8361 
8362 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8363 {
8364 	struct hwrm_func_qcfg_output *resp;
8365 	struct hwrm_func_qcfg_input *req;
8366 	u16 flags;
8367 	int rc;
8368 
8369 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8370 	if (rc)
8371 		return rc;
8372 
8373 	req->fid = cpu_to_le16(0xffff);
8374 	resp = hwrm_req_hold(bp, req);
8375 	rc = hwrm_req_send(bp, req);
8376 	if (rc)
8377 		goto func_qcfg_exit;
8378 
8379 	flags = le16_to_cpu(resp->flags);
8380 #ifdef CONFIG_BNXT_SRIOV
8381 	if (BNXT_VF(bp)) {
8382 		struct bnxt_vf_info *vf = &bp->vf;
8383 
8384 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8385 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8386 			vf->flags |= BNXT_VF_TRUST;
8387 		else
8388 			vf->flags &= ~BNXT_VF_TRUST;
8389 	} else {
8390 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8391 	}
8392 #endif
8393 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8394 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8395 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8396 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8397 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8398 	}
8399 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8400 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8401 
8402 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8403 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8404 
8405 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8406 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8407 
8408 	switch (resp->port_partition_type) {
8409 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8410 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8411 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8412 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8413 		bp->port_partition_type = resp->port_partition_type;
8414 		break;
8415 	}
8416 	if (bp->hwrm_spec_code < 0x10707 ||
8417 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8418 		bp->br_mode = BRIDGE_MODE_VEB;
8419 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8420 		bp->br_mode = BRIDGE_MODE_VEPA;
8421 	else
8422 		bp->br_mode = BRIDGE_MODE_UNDEF;
8423 
8424 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8425 	if (!bp->max_mtu)
8426 		bp->max_mtu = BNXT_MAX_MTU;
8427 
8428 	if (bp->db_size)
8429 		goto func_qcfg_exit;
8430 
8431 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8432 	if (BNXT_CHIP_P5(bp)) {
8433 		if (BNXT_PF(bp))
8434 			bp->db_offset = DB_PF_OFFSET_P5;
8435 		else
8436 			bp->db_offset = DB_VF_OFFSET_P5;
8437 	}
8438 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8439 				 1024);
8440 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8441 	    bp->db_size <= bp->db_offset)
8442 		bp->db_size = pci_resource_len(bp->pdev, 2);
8443 
8444 func_qcfg_exit:
8445 	hwrm_req_drop(bp, req);
8446 	return rc;
8447 }
8448 
8449 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8450 				      u8 init_val, u8 init_offset,
8451 				      bool init_mask_set)
8452 {
8453 	ctxm->init_value = init_val;
8454 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8455 	if (init_mask_set)
8456 		ctxm->init_offset = init_offset * 4;
8457 	else
8458 		ctxm->init_value = 0;
8459 }
8460 
8461 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8462 {
8463 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8464 	u16 type;
8465 
8466 	for (type = 0; type < ctx_max; type++) {
8467 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8468 		int n = 1;
8469 
8470 		if (!ctxm->max_entries || ctxm->pg_info)
8471 			continue;
8472 
8473 		if (ctxm->instance_bmap)
8474 			n = hweight32(ctxm->instance_bmap);
8475 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8476 		if (!ctxm->pg_info)
8477 			return -ENOMEM;
8478 	}
8479 	return 0;
8480 }
8481 
8482 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8483 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8484 
8485 #define BNXT_CTX_INIT_VALID(flags)	\
8486 	(!!((flags) &			\
8487 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8488 
8489 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8490 {
8491 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8492 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8493 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8494 	u16 type;
8495 	int rc;
8496 
8497 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8498 	if (rc)
8499 		return rc;
8500 
8501 	if (!ctx) {
8502 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8503 		if (!ctx)
8504 			return -ENOMEM;
8505 		bp->ctx = ctx;
8506 	}
8507 
8508 	resp = hwrm_req_hold(bp, req);
8509 
8510 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8511 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8512 		u8 init_val, init_off, i;
8513 		u32 max_entries;
8514 		u16 entry_size;
8515 		__le32 *p;
8516 		u32 flags;
8517 
8518 		req->type = cpu_to_le16(type);
8519 		rc = hwrm_req_send(bp, req);
8520 		if (rc)
8521 			goto ctx_done;
8522 		flags = le32_to_cpu(resp->flags);
8523 		type = le16_to_cpu(resp->next_valid_type);
8524 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8525 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8526 			continue;
8527 		}
8528 		entry_size = le16_to_cpu(resp->entry_size);
8529 		max_entries = le32_to_cpu(resp->max_num_entries);
8530 		if (ctxm->mem_valid) {
8531 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8532 			    ctxm->entry_size != entry_size ||
8533 			    ctxm->max_entries != max_entries)
8534 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8535 			else
8536 				continue;
8537 		}
8538 		ctxm->type = le16_to_cpu(resp->type);
8539 		ctxm->entry_size = entry_size;
8540 		ctxm->flags = flags;
8541 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8542 		ctxm->entry_multiple = resp->entry_multiple;
8543 		ctxm->max_entries = max_entries;
8544 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8545 		init_val = resp->ctx_init_value;
8546 		init_off = resp->ctx_init_offset;
8547 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8548 					  BNXT_CTX_INIT_VALID(flags));
8549 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8550 					      BNXT_MAX_SPLIT_ENTRY);
8551 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8552 		     i++, p++)
8553 			ctxm->split[i] = le32_to_cpu(*p);
8554 	}
8555 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8556 
8557 ctx_done:
8558 	hwrm_req_drop(bp, req);
8559 	return rc;
8560 }
8561 
8562 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8563 {
8564 	struct hwrm_func_backing_store_qcaps_output *resp;
8565 	struct hwrm_func_backing_store_qcaps_input *req;
8566 	int rc;
8567 
8568 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8569 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8570 		return 0;
8571 
8572 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8573 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8574 
8575 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8576 	if (rc)
8577 		return rc;
8578 
8579 	resp = hwrm_req_hold(bp, req);
8580 	rc = hwrm_req_send_silent(bp, req);
8581 	if (!rc) {
8582 		struct bnxt_ctx_mem_type *ctxm;
8583 		struct bnxt_ctx_mem_info *ctx;
8584 		u8 init_val, init_idx = 0;
8585 		u16 init_mask;
8586 
8587 		ctx = bp->ctx;
8588 		if (!ctx) {
8589 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8590 			if (!ctx) {
8591 				rc = -ENOMEM;
8592 				goto ctx_err;
8593 			}
8594 			bp->ctx = ctx;
8595 		}
8596 		init_val = resp->ctx_kind_initializer;
8597 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8598 
8599 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8600 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8601 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8602 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8603 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8604 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8605 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8606 					  (init_mask & (1 << init_idx++)) != 0);
8607 
8608 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8609 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8610 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8611 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8612 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8613 					  (init_mask & (1 << init_idx++)) != 0);
8614 
8615 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8616 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8617 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8618 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8619 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8620 					  (init_mask & (1 << init_idx++)) != 0);
8621 
8622 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8623 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8624 		ctxm->max_entries = ctxm->vnic_entries +
8625 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8626 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8627 		bnxt_init_ctx_initializer(ctxm, init_val,
8628 					  resp->vnic_init_offset,
8629 					  (init_mask & (1 << init_idx++)) != 0);
8630 
8631 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8632 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8633 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8634 		bnxt_init_ctx_initializer(ctxm, init_val,
8635 					  resp->stat_init_offset,
8636 					  (init_mask & (1 << init_idx++)) != 0);
8637 
8638 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8639 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8640 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8641 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8642 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8643 		if (!ctxm->entry_multiple)
8644 			ctxm->entry_multiple = 1;
8645 
8646 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8647 
8648 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8649 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8650 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8651 		ctxm->mrav_num_entries_units =
8652 			le16_to_cpu(resp->mrav_num_entries_units);
8653 		bnxt_init_ctx_initializer(ctxm, init_val,
8654 					  resp->mrav_init_offset,
8655 					  (init_mask & (1 << init_idx++)) != 0);
8656 
8657 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8658 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8659 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8660 
8661 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8662 		if (!ctx->tqm_fp_rings_count)
8663 			ctx->tqm_fp_rings_count = bp->max_q;
8664 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8665 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8666 
8667 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8668 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8669 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8670 
8671 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8672 	} else {
8673 		rc = 0;
8674 	}
8675 ctx_err:
8676 	hwrm_req_drop(bp, req);
8677 	return rc;
8678 }
8679 
8680 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8681 				  __le64 *pg_dir)
8682 {
8683 	if (!rmem->nr_pages)
8684 		return;
8685 
8686 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8687 	if (rmem->depth >= 1) {
8688 		if (rmem->depth == 2)
8689 			*pg_attr |= 2;
8690 		else
8691 			*pg_attr |= 1;
8692 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8693 	} else {
8694 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8695 	}
8696 }
8697 
8698 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8699 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8700 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8701 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8702 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8703 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8704 
8705 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8706 {
8707 	struct hwrm_func_backing_store_cfg_input *req;
8708 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8709 	struct bnxt_ctx_pg_info *ctx_pg;
8710 	struct bnxt_ctx_mem_type *ctxm;
8711 	void **__req = (void **)&req;
8712 	u32 req_len = sizeof(*req);
8713 	__le32 *num_entries;
8714 	__le64 *pg_dir;
8715 	u32 flags = 0;
8716 	u8 *pg_attr;
8717 	u32 ena;
8718 	int rc;
8719 	int i;
8720 
8721 	if (!ctx)
8722 		return 0;
8723 
8724 	if (req_len > bp->hwrm_max_ext_req_len)
8725 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8726 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8727 	if (rc)
8728 		return rc;
8729 
8730 	req->enables = cpu_to_le32(enables);
8731 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8732 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8733 		ctx_pg = ctxm->pg_info;
8734 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8735 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8736 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8737 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8738 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8739 				      &req->qpc_pg_size_qpc_lvl,
8740 				      &req->qpc_page_dir);
8741 
8742 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8743 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8744 	}
8745 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8746 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8747 		ctx_pg = ctxm->pg_info;
8748 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8749 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8750 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8751 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8752 				      &req->srq_pg_size_srq_lvl,
8753 				      &req->srq_page_dir);
8754 	}
8755 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8756 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8757 		ctx_pg = ctxm->pg_info;
8758 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8759 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8760 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8761 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8762 				      &req->cq_pg_size_cq_lvl,
8763 				      &req->cq_page_dir);
8764 	}
8765 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8766 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8767 		ctx_pg = ctxm->pg_info;
8768 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8769 		req->vnic_num_ring_table_entries =
8770 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8771 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8772 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8773 				      &req->vnic_pg_size_vnic_lvl,
8774 				      &req->vnic_page_dir);
8775 	}
8776 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8777 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8778 		ctx_pg = ctxm->pg_info;
8779 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8780 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8781 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8782 				      &req->stat_pg_size_stat_lvl,
8783 				      &req->stat_page_dir);
8784 	}
8785 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8786 		u32 units;
8787 
8788 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8789 		ctx_pg = ctxm->pg_info;
8790 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8791 		units = ctxm->mrav_num_entries_units;
8792 		if (units) {
8793 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8794 			u32 entries;
8795 
8796 			num_mr = ctx_pg->entries - num_ah;
8797 			entries = ((num_mr / units) << 16) | (num_ah / units);
8798 			req->mrav_num_entries = cpu_to_le32(entries);
8799 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8800 		}
8801 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8802 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8803 				      &req->mrav_pg_size_mrav_lvl,
8804 				      &req->mrav_page_dir);
8805 	}
8806 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8807 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8808 		ctx_pg = ctxm->pg_info;
8809 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8810 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8811 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8812 				      &req->tim_pg_size_tim_lvl,
8813 				      &req->tim_page_dir);
8814 	}
8815 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8816 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8817 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8818 	     pg_dir = &req->tqm_sp_page_dir,
8819 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8820 	     ctx_pg = ctxm->pg_info;
8821 	     i < BNXT_MAX_TQM_RINGS;
8822 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8823 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8824 		if (!(enables & ena))
8825 			continue;
8826 
8827 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8828 		*num_entries = cpu_to_le32(ctx_pg->entries);
8829 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8830 	}
8831 	req->flags = cpu_to_le32(flags);
8832 	return hwrm_req_send(bp, req);
8833 }
8834 
8835 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8836 				  struct bnxt_ctx_pg_info *ctx_pg)
8837 {
8838 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8839 
8840 	rmem->page_size = BNXT_PAGE_SIZE;
8841 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8842 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8843 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8844 	if (rmem->depth >= 1)
8845 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8846 	return bnxt_alloc_ring(bp, rmem);
8847 }
8848 
8849 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8850 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8851 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8852 {
8853 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8854 	int rc;
8855 
8856 	if (!mem_size)
8857 		return -EINVAL;
8858 
8859 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8860 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8861 		ctx_pg->nr_pages = 0;
8862 		return -EINVAL;
8863 	}
8864 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8865 		int nr_tbls, i;
8866 
8867 		rmem->depth = 2;
8868 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8869 					     GFP_KERNEL);
8870 		if (!ctx_pg->ctx_pg_tbl)
8871 			return -ENOMEM;
8872 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8873 		rmem->nr_pages = nr_tbls;
8874 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8875 		if (rc)
8876 			return rc;
8877 		for (i = 0; i < nr_tbls; i++) {
8878 			struct bnxt_ctx_pg_info *pg_tbl;
8879 
8880 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8881 			if (!pg_tbl)
8882 				return -ENOMEM;
8883 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8884 			rmem = &pg_tbl->ring_mem;
8885 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8886 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8887 			rmem->depth = 1;
8888 			rmem->nr_pages = MAX_CTX_PAGES;
8889 			rmem->ctx_mem = ctxm;
8890 			if (i == (nr_tbls - 1)) {
8891 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8892 
8893 				if (rem)
8894 					rmem->nr_pages = rem;
8895 			}
8896 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8897 			if (rc)
8898 				break;
8899 		}
8900 	} else {
8901 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8902 		if (rmem->nr_pages > 1 || depth)
8903 			rmem->depth = 1;
8904 		rmem->ctx_mem = ctxm;
8905 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8906 	}
8907 	return rc;
8908 }
8909 
8910 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
8911 				    struct bnxt_ctx_pg_info *ctx_pg,
8912 				    void *buf, size_t offset, size_t head,
8913 				    size_t tail)
8914 {
8915 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8916 	size_t nr_pages = ctx_pg->nr_pages;
8917 	int page_size = rmem->page_size;
8918 	size_t len = 0, total_len = 0;
8919 	u16 depth = rmem->depth;
8920 
8921 	tail %= nr_pages * page_size;
8922 	do {
8923 		if (depth > 1) {
8924 			int i = head / (page_size * MAX_CTX_PAGES);
8925 			struct bnxt_ctx_pg_info *pg_tbl;
8926 
8927 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8928 			rmem = &pg_tbl->ring_mem;
8929 		}
8930 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
8931 		head += len;
8932 		offset += len;
8933 		total_len += len;
8934 		if (head >= nr_pages * page_size)
8935 			head = 0;
8936 	} while (head != tail);
8937 	return total_len;
8938 }
8939 
8940 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
8941 				  struct bnxt_ctx_pg_info *ctx_pg)
8942 {
8943 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8944 
8945 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
8946 	    ctx_pg->ctx_pg_tbl) {
8947 		int i, nr_tbls = rmem->nr_pages;
8948 
8949 		for (i = 0; i < nr_tbls; i++) {
8950 			struct bnxt_ctx_pg_info *pg_tbl;
8951 			struct bnxt_ring_mem_info *rmem2;
8952 
8953 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8954 			if (!pg_tbl)
8955 				continue;
8956 			rmem2 = &pg_tbl->ring_mem;
8957 			bnxt_free_ring(bp, rmem2);
8958 			ctx_pg->ctx_pg_arr[i] = NULL;
8959 			kfree(pg_tbl);
8960 			ctx_pg->ctx_pg_tbl[i] = NULL;
8961 		}
8962 		kfree(ctx_pg->ctx_pg_tbl);
8963 		ctx_pg->ctx_pg_tbl = NULL;
8964 	}
8965 	bnxt_free_ring(bp, rmem);
8966 	ctx_pg->nr_pages = 0;
8967 }
8968 
8969 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
8970 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
8971 				   u8 pg_lvl)
8972 {
8973 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8974 	int i, rc = 0, n = 1;
8975 	u32 mem_size;
8976 
8977 	if (!ctxm->entry_size || !ctx_pg)
8978 		return -EINVAL;
8979 	if (ctxm->instance_bmap)
8980 		n = hweight32(ctxm->instance_bmap);
8981 	if (ctxm->entry_multiple)
8982 		entries = roundup(entries, ctxm->entry_multiple);
8983 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
8984 	mem_size = entries * ctxm->entry_size;
8985 	for (i = 0; i < n && !rc; i++) {
8986 		ctx_pg[i].entries = entries;
8987 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
8988 					    ctxm->init_value ? ctxm : NULL);
8989 	}
8990 	if (!rc)
8991 		ctxm->mem_valid = 1;
8992 	return rc;
8993 }
8994 
8995 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
8996 					       struct bnxt_ctx_mem_type *ctxm,
8997 					       bool last)
8998 {
8999 	struct hwrm_func_backing_store_cfg_v2_input *req;
9000 	u32 instance_bmap = ctxm->instance_bmap;
9001 	int i, j, rc = 0, n = 1;
9002 	__le32 *p;
9003 
9004 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9005 		return 0;
9006 
9007 	if (instance_bmap)
9008 		n = hweight32(ctxm->instance_bmap);
9009 	else
9010 		instance_bmap = 1;
9011 
9012 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9013 	if (rc)
9014 		return rc;
9015 	hwrm_req_hold(bp, req);
9016 	req->type = cpu_to_le16(ctxm->type);
9017 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9018 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9019 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9020 		struct bnxt_bs_trace_info *bs_trace;
9021 		u32 enables;
9022 
9023 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9024 		req->enables = cpu_to_le32(enables);
9025 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9026 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9027 	}
9028 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9029 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9030 		p[i] = cpu_to_le32(ctxm->split[i]);
9031 	for (i = 0, j = 0; j < n && !rc; i++) {
9032 		struct bnxt_ctx_pg_info *ctx_pg;
9033 
9034 		if (!(instance_bmap & (1 << i)))
9035 			continue;
9036 		req->instance = cpu_to_le16(i);
9037 		ctx_pg = &ctxm->pg_info[j++];
9038 		if (!ctx_pg->entries)
9039 			continue;
9040 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9041 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9042 				      &req->page_size_pbl_level,
9043 				      &req->page_dir);
9044 		if (last && j == n)
9045 			req->flags =
9046 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9047 		rc = hwrm_req_send(bp, req);
9048 	}
9049 	hwrm_req_drop(bp, req);
9050 	return rc;
9051 }
9052 
9053 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
9054 {
9055 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9056 	struct bnxt_ctx_mem_type *ctxm;
9057 	u16 last_type = BNXT_CTX_INV;
9058 	int rc = 0;
9059 	u16 type;
9060 
9061 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) {
9062 		ctxm = &ctx->ctx_arr[type];
9063 		if (!bnxt_bs_trace_avail(bp, type))
9064 			continue;
9065 		if (!ctxm->mem_valid) {
9066 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9067 						     ctxm->max_entries, 1);
9068 			if (rc) {
9069 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9070 					    type);
9071 				continue;
9072 			}
9073 			bnxt_bs_trace_init(bp, ctxm);
9074 		}
9075 		last_type = type;
9076 	}
9077 
9078 	if (last_type == BNXT_CTX_INV) {
9079 		if (!ena)
9080 			return 0;
9081 		else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
9082 			last_type = BNXT_CTX_MAX - 1;
9083 		else
9084 			last_type = BNXT_CTX_L2_MAX - 1;
9085 	}
9086 	ctx->ctx_arr[last_type].last = 1;
9087 
9088 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9089 		ctxm = &ctx->ctx_arr[type];
9090 
9091 		if (!ctxm->mem_valid)
9092 			continue;
9093 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9094 		if (rc)
9095 			return rc;
9096 	}
9097 	return 0;
9098 }
9099 
9100 /**
9101  * __bnxt_copy_ctx_mem - copy host context memory
9102  * @bp: The driver context
9103  * @ctxm: The pointer to the context memory type
9104  * @buf: The destination buffer or NULL to just obtain the length
9105  * @offset: The buffer offset to copy the data to
9106  * @head: The head offset of context memory to copy from
9107  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9108  *
9109  * This function is called for debugging purposes to dump the host context
9110  * used by the chip.
9111  *
9112  * Return: Length of memory copied
9113  */
9114 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9115 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9116 				  size_t offset, size_t head, size_t tail)
9117 {
9118 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9119 	size_t len = 0, total_len = 0;
9120 	int i, n = 1;
9121 
9122 	if (!ctx_pg)
9123 		return 0;
9124 
9125 	if (ctxm->instance_bmap)
9126 		n = hweight32(ctxm->instance_bmap);
9127 	for (i = 0; i < n; i++) {
9128 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9129 					    tail);
9130 		offset += len;
9131 		total_len += len;
9132 	}
9133 	return total_len;
9134 }
9135 
9136 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9137 			 void *buf, size_t offset)
9138 {
9139 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9140 
9141 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9142 }
9143 
9144 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9145 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9146 {
9147 	struct bnxt_ctx_pg_info *ctx_pg;
9148 	int i, n = 1;
9149 
9150 	ctxm->last = 0;
9151 
9152 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9153 		return;
9154 
9155 	ctx_pg = ctxm->pg_info;
9156 	if (ctx_pg) {
9157 		if (ctxm->instance_bmap)
9158 			n = hweight32(ctxm->instance_bmap);
9159 		for (i = 0; i < n; i++)
9160 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9161 
9162 		kfree(ctx_pg);
9163 		ctxm->pg_info = NULL;
9164 		ctxm->mem_valid = 0;
9165 	}
9166 	memset(ctxm, 0, sizeof(*ctxm));
9167 }
9168 
9169 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9170 {
9171 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9172 	u16 type;
9173 
9174 	if (!ctx)
9175 		return;
9176 
9177 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9178 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9179 
9180 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9181 	if (force) {
9182 		kfree(ctx);
9183 		bp->ctx = NULL;
9184 	}
9185 }
9186 
9187 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9188 {
9189 	struct bnxt_ctx_mem_type *ctxm;
9190 	struct bnxt_ctx_mem_info *ctx;
9191 	u32 l2_qps, qp1_qps, max_qps;
9192 	u32 ena, entries_sp, entries;
9193 	u32 srqs, max_srqs, min;
9194 	u32 num_mr, num_ah;
9195 	u32 extra_srqs = 0;
9196 	u32 extra_qps = 0;
9197 	u32 fast_qpmd_qps;
9198 	u8 pg_lvl = 1;
9199 	int i, rc;
9200 
9201 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9202 	if (rc) {
9203 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9204 			   rc);
9205 		return rc;
9206 	}
9207 	ctx = bp->ctx;
9208 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9209 		return 0;
9210 
9211 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9212 	l2_qps = ctxm->qp_l2_entries;
9213 	qp1_qps = ctxm->qp_qp1_entries;
9214 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9215 	max_qps = ctxm->max_entries;
9216 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9217 	srqs = ctxm->srq_l2_entries;
9218 	max_srqs = ctxm->max_entries;
9219 	ena = 0;
9220 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9221 		pg_lvl = 2;
9222 		if (BNXT_SW_RES_LMT(bp)) {
9223 			extra_qps = max_qps - l2_qps - qp1_qps;
9224 			extra_srqs = max_srqs - srqs;
9225 		} else {
9226 			extra_qps = min_t(u32, 65536,
9227 					  max_qps - l2_qps - qp1_qps);
9228 			/* allocate extra qps if fw supports RoCE fast qp
9229 			 * destroy feature
9230 			 */
9231 			extra_qps += fast_qpmd_qps;
9232 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9233 		}
9234 		if (fast_qpmd_qps)
9235 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9236 	}
9237 
9238 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9239 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9240 				     pg_lvl);
9241 	if (rc)
9242 		return rc;
9243 
9244 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9245 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9246 	if (rc)
9247 		return rc;
9248 
9249 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9250 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9251 				     extra_qps * 2, pg_lvl);
9252 	if (rc)
9253 		return rc;
9254 
9255 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9256 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9257 	if (rc)
9258 		return rc;
9259 
9260 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9261 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9262 	if (rc)
9263 		return rc;
9264 
9265 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9266 		goto skip_rdma;
9267 
9268 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9269 	if (BNXT_SW_RES_LMT(bp) &&
9270 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9271 		num_ah = ctxm->mrav_av_entries;
9272 		num_mr = ctxm->max_entries - num_ah;
9273 	} else {
9274 		/* 128K extra is needed to accommodate static AH context
9275 		 * allocation by f/w.
9276 		 */
9277 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9278 		num_ah = min_t(u32, num_mr, 1024 * 128);
9279 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9280 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9281 			ctxm->mrav_av_entries = num_ah;
9282 	}
9283 
9284 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9285 	if (rc)
9286 		return rc;
9287 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9288 
9289 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9290 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9291 	if (rc)
9292 		return rc;
9293 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9294 
9295 skip_rdma:
9296 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9297 	min = ctxm->min_entries;
9298 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9299 		     2 * (extra_qps + qp1_qps) + min;
9300 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9301 	if (rc)
9302 		return rc;
9303 
9304 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9305 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9306 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9307 	if (rc)
9308 		return rc;
9309 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9310 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9311 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9312 
9313 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9314 		rc = bnxt_backing_store_cfg_v2(bp, ena);
9315 	else
9316 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9317 	if (rc) {
9318 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9319 			   rc);
9320 		return rc;
9321 	}
9322 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9323 	return 0;
9324 }
9325 
9326 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9327 {
9328 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9329 	u16 page_attr;
9330 	int rc;
9331 
9332 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9333 		return 0;
9334 
9335 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9336 	if (rc)
9337 		return rc;
9338 
9339 	if (BNXT_PAGE_SIZE == 0x2000)
9340 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9341 	else if (BNXT_PAGE_SIZE == 0x10000)
9342 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9343 	else
9344 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9345 	req->pg_size_lvl = cpu_to_le16(page_attr |
9346 				       bp->fw_crash_mem->ring_mem.depth);
9347 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9348 	req->size = cpu_to_le32(bp->fw_crash_len);
9349 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9350 	return hwrm_req_send(bp, req);
9351 }
9352 
9353 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9354 {
9355 	if (bp->fw_crash_mem) {
9356 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9357 		kfree(bp->fw_crash_mem);
9358 		bp->fw_crash_mem = NULL;
9359 	}
9360 }
9361 
9362 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9363 {
9364 	u32 mem_size = 0;
9365 	int rc;
9366 
9367 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9368 		return 0;
9369 
9370 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9371 	if (rc)
9372 		return rc;
9373 
9374 	mem_size = round_up(mem_size, 4);
9375 
9376 	/* keep and use the existing pages */
9377 	if (bp->fw_crash_mem &&
9378 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9379 		goto alloc_done;
9380 
9381 	if (bp->fw_crash_mem)
9382 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9383 	else
9384 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
9385 					   GFP_KERNEL);
9386 	if (!bp->fw_crash_mem)
9387 		return -ENOMEM;
9388 
9389 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9390 	if (rc) {
9391 		bnxt_free_crash_dump_mem(bp);
9392 		return rc;
9393 	}
9394 
9395 alloc_done:
9396 	bp->fw_crash_len = mem_size;
9397 	return 0;
9398 }
9399 
9400 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9401 {
9402 	struct hwrm_func_resource_qcaps_output *resp;
9403 	struct hwrm_func_resource_qcaps_input *req;
9404 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9405 	int rc;
9406 
9407 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9408 	if (rc)
9409 		return rc;
9410 
9411 	req->fid = cpu_to_le16(0xffff);
9412 	resp = hwrm_req_hold(bp, req);
9413 	rc = hwrm_req_send_silent(bp, req);
9414 	if (rc)
9415 		goto hwrm_func_resc_qcaps_exit;
9416 
9417 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9418 	if (!all)
9419 		goto hwrm_func_resc_qcaps_exit;
9420 
9421 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9422 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9423 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9424 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9425 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9426 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9427 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9428 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9429 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9430 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9431 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9432 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9433 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9434 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9435 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9436 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9437 
9438 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9439 		u16 max_msix = le16_to_cpu(resp->max_msix);
9440 
9441 		hw_resc->max_nqs = max_msix;
9442 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9443 	}
9444 
9445 	if (BNXT_PF(bp)) {
9446 		struct bnxt_pf_info *pf = &bp->pf;
9447 
9448 		pf->vf_resv_strategy =
9449 			le16_to_cpu(resp->vf_reservation_strategy);
9450 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9451 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9452 	}
9453 hwrm_func_resc_qcaps_exit:
9454 	hwrm_req_drop(bp, req);
9455 	return rc;
9456 }
9457 
9458 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9459 {
9460 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9461 	struct hwrm_port_mac_ptp_qcfg_input *req;
9462 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9463 	u8 flags;
9464 	int rc;
9465 
9466 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9467 		rc = -ENODEV;
9468 		goto no_ptp;
9469 	}
9470 
9471 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9472 	if (rc)
9473 		goto no_ptp;
9474 
9475 	req->port_id = cpu_to_le16(bp->pf.port_id);
9476 	resp = hwrm_req_hold(bp, req);
9477 	rc = hwrm_req_send(bp, req);
9478 	if (rc)
9479 		goto exit;
9480 
9481 	flags = resp->flags;
9482 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9483 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9484 		rc = -ENODEV;
9485 		goto exit;
9486 	}
9487 	if (!ptp) {
9488 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9489 		if (!ptp) {
9490 			rc = -ENOMEM;
9491 			goto exit;
9492 		}
9493 		ptp->bp = bp;
9494 		bp->ptp_cfg = ptp;
9495 	}
9496 
9497 	if (flags &
9498 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9499 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9500 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9501 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9502 	} else if (BNXT_CHIP_P5(bp)) {
9503 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9504 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9505 	} else {
9506 		rc = -ENODEV;
9507 		goto exit;
9508 	}
9509 	ptp->rtc_configured =
9510 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9511 	rc = bnxt_ptp_init(bp);
9512 	if (rc)
9513 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9514 exit:
9515 	hwrm_req_drop(bp, req);
9516 	if (!rc)
9517 		return 0;
9518 
9519 no_ptp:
9520 	bnxt_ptp_clear(bp);
9521 	kfree(ptp);
9522 	bp->ptp_cfg = NULL;
9523 	return rc;
9524 }
9525 
9526 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9527 {
9528 	struct hwrm_func_qcaps_output *resp;
9529 	struct hwrm_func_qcaps_input *req;
9530 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9531 	u32 flags, flags_ext, flags_ext2;
9532 	int rc;
9533 
9534 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9535 	if (rc)
9536 		return rc;
9537 
9538 	req->fid = cpu_to_le16(0xffff);
9539 	resp = hwrm_req_hold(bp, req);
9540 	rc = hwrm_req_send(bp, req);
9541 	if (rc)
9542 		goto hwrm_func_qcaps_exit;
9543 
9544 	flags = le32_to_cpu(resp->flags);
9545 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9546 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9547 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9548 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9549 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9550 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9551 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9552 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9553 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9554 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9555 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9556 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9557 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9558 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9559 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9560 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9561 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9562 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9563 
9564 	flags_ext = le32_to_cpu(resp->flags_ext);
9565 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9566 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9567 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9568 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9569 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9570 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9571 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9572 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9573 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9574 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9575 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9576 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9577 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9578 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9579 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9580 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9581 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9582 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9583 
9584 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9585 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9586 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9587 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9588 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9589 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9590 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9591 	if (flags_ext2 &
9592 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9593 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9594 	if (BNXT_PF(bp) &&
9595 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9596 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9597 
9598 	bp->tx_push_thresh = 0;
9599 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9600 	    BNXT_FW_MAJ(bp) > 217)
9601 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9602 
9603 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9604 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9605 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9606 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9607 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9608 	if (!hw_resc->max_hw_ring_grps)
9609 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9610 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9611 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9612 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9613 
9614 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9615 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9616 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9617 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9618 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9619 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9620 
9621 	if (BNXT_PF(bp)) {
9622 		struct bnxt_pf_info *pf = &bp->pf;
9623 
9624 		pf->fw_fid = le16_to_cpu(resp->fid);
9625 		pf->port_id = le16_to_cpu(resp->port_id);
9626 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9627 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9628 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9629 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9630 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9631 			bp->flags |= BNXT_FLAG_WOL_CAP;
9632 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9633 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9634 		} else {
9635 			bnxt_ptp_clear(bp);
9636 			kfree(bp->ptp_cfg);
9637 			bp->ptp_cfg = NULL;
9638 		}
9639 	} else {
9640 #ifdef CONFIG_BNXT_SRIOV
9641 		struct bnxt_vf_info *vf = &bp->vf;
9642 
9643 		vf->fw_fid = le16_to_cpu(resp->fid);
9644 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9645 #endif
9646 	}
9647 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9648 
9649 hwrm_func_qcaps_exit:
9650 	hwrm_req_drop(bp, req);
9651 	return rc;
9652 }
9653 
9654 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9655 {
9656 	struct hwrm_dbg_qcaps_output *resp;
9657 	struct hwrm_dbg_qcaps_input *req;
9658 	int rc;
9659 
9660 	bp->fw_dbg_cap = 0;
9661 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9662 		return;
9663 
9664 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9665 	if (rc)
9666 		return;
9667 
9668 	req->fid = cpu_to_le16(0xffff);
9669 	resp = hwrm_req_hold(bp, req);
9670 	rc = hwrm_req_send(bp, req);
9671 	if (rc)
9672 		goto hwrm_dbg_qcaps_exit;
9673 
9674 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9675 
9676 hwrm_dbg_qcaps_exit:
9677 	hwrm_req_drop(bp, req);
9678 }
9679 
9680 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9681 
9682 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9683 {
9684 	int rc;
9685 
9686 	rc = __bnxt_hwrm_func_qcaps(bp);
9687 	if (rc)
9688 		return rc;
9689 
9690 	bnxt_hwrm_dbg_qcaps(bp);
9691 
9692 	rc = bnxt_hwrm_queue_qportcfg(bp);
9693 	if (rc) {
9694 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9695 		return rc;
9696 	}
9697 	if (bp->hwrm_spec_code >= 0x10803) {
9698 		rc = bnxt_alloc_ctx_mem(bp);
9699 		if (rc)
9700 			return rc;
9701 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9702 		if (!rc)
9703 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9704 	}
9705 	return 0;
9706 }
9707 
9708 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9709 {
9710 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9711 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9712 	u32 flags;
9713 	int rc;
9714 
9715 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9716 		return 0;
9717 
9718 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9719 	if (rc)
9720 		return rc;
9721 
9722 	resp = hwrm_req_hold(bp, req);
9723 	rc = hwrm_req_send(bp, req);
9724 	if (rc)
9725 		goto hwrm_cfa_adv_qcaps_exit;
9726 
9727 	flags = le32_to_cpu(resp->flags);
9728 	if (flags &
9729 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9730 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9731 
9732 	if (flags &
9733 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9734 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9735 
9736 	if (flags &
9737 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9738 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9739 
9740 hwrm_cfa_adv_qcaps_exit:
9741 	hwrm_req_drop(bp, req);
9742 	return rc;
9743 }
9744 
9745 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9746 {
9747 	if (bp->fw_health)
9748 		return 0;
9749 
9750 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9751 	if (!bp->fw_health)
9752 		return -ENOMEM;
9753 
9754 	mutex_init(&bp->fw_health->lock);
9755 	return 0;
9756 }
9757 
9758 static int bnxt_alloc_fw_health(struct bnxt *bp)
9759 {
9760 	int rc;
9761 
9762 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9763 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9764 		return 0;
9765 
9766 	rc = __bnxt_alloc_fw_health(bp);
9767 	if (rc) {
9768 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9769 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9770 		return rc;
9771 	}
9772 
9773 	return 0;
9774 }
9775 
9776 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9777 {
9778 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9779 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9780 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9781 }
9782 
9783 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9784 {
9785 	struct bnxt_fw_health *fw_health = bp->fw_health;
9786 	u32 reg_type;
9787 
9788 	if (!fw_health)
9789 		return;
9790 
9791 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9792 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9793 		fw_health->status_reliable = false;
9794 
9795 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9796 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9797 		fw_health->resets_reliable = false;
9798 }
9799 
9800 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9801 {
9802 	void __iomem *hs;
9803 	u32 status_loc;
9804 	u32 reg_type;
9805 	u32 sig;
9806 
9807 	if (bp->fw_health)
9808 		bp->fw_health->status_reliable = false;
9809 
9810 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9811 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9812 
9813 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9814 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9815 		if (!bp->chip_num) {
9816 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9817 			bp->chip_num = readl(bp->bar0 +
9818 					     BNXT_FW_HEALTH_WIN_BASE +
9819 					     BNXT_GRC_REG_CHIP_NUM);
9820 		}
9821 		if (!BNXT_CHIP_P5_PLUS(bp))
9822 			return;
9823 
9824 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9825 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9826 	} else {
9827 		status_loc = readl(hs + offsetof(struct hcomm_status,
9828 						 fw_status_loc));
9829 	}
9830 
9831 	if (__bnxt_alloc_fw_health(bp)) {
9832 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9833 		return;
9834 	}
9835 
9836 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9837 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9838 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9839 		__bnxt_map_fw_health_reg(bp, status_loc);
9840 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9841 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9842 	}
9843 
9844 	bp->fw_health->status_reliable = true;
9845 }
9846 
9847 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9848 {
9849 	struct bnxt_fw_health *fw_health = bp->fw_health;
9850 	u32 reg_base = 0xffffffff;
9851 	int i;
9852 
9853 	bp->fw_health->status_reliable = false;
9854 	bp->fw_health->resets_reliable = false;
9855 	/* Only pre-map the monitoring GRC registers using window 3 */
9856 	for (i = 0; i < 4; i++) {
9857 		u32 reg = fw_health->regs[i];
9858 
9859 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9860 			continue;
9861 		if (reg_base == 0xffffffff)
9862 			reg_base = reg & BNXT_GRC_BASE_MASK;
9863 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9864 			return -ERANGE;
9865 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9866 	}
9867 	bp->fw_health->status_reliable = true;
9868 	bp->fw_health->resets_reliable = true;
9869 	if (reg_base == 0xffffffff)
9870 		return 0;
9871 
9872 	__bnxt_map_fw_health_reg(bp, reg_base);
9873 	return 0;
9874 }
9875 
9876 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9877 {
9878 	if (!bp->fw_health)
9879 		return;
9880 
9881 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9882 		bp->fw_health->status_reliable = true;
9883 		bp->fw_health->resets_reliable = true;
9884 	} else {
9885 		bnxt_try_map_fw_health_reg(bp);
9886 	}
9887 }
9888 
9889 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9890 {
9891 	struct bnxt_fw_health *fw_health = bp->fw_health;
9892 	struct hwrm_error_recovery_qcfg_output *resp;
9893 	struct hwrm_error_recovery_qcfg_input *req;
9894 	int rc, i;
9895 
9896 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9897 		return 0;
9898 
9899 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9900 	if (rc)
9901 		return rc;
9902 
9903 	resp = hwrm_req_hold(bp, req);
9904 	rc = hwrm_req_send(bp, req);
9905 	if (rc)
9906 		goto err_recovery_out;
9907 	fw_health->flags = le32_to_cpu(resp->flags);
9908 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9909 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9910 		rc = -EINVAL;
9911 		goto err_recovery_out;
9912 	}
9913 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9914 	fw_health->master_func_wait_dsecs =
9915 		le32_to_cpu(resp->master_func_wait_period);
9916 	fw_health->normal_func_wait_dsecs =
9917 		le32_to_cpu(resp->normal_func_wait_period);
9918 	fw_health->post_reset_wait_dsecs =
9919 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9920 	fw_health->post_reset_max_wait_dsecs =
9921 		le32_to_cpu(resp->max_bailout_time_after_reset);
9922 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9923 		le32_to_cpu(resp->fw_health_status_reg);
9924 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9925 		le32_to_cpu(resp->fw_heartbeat_reg);
9926 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9927 		le32_to_cpu(resp->fw_reset_cnt_reg);
9928 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9929 		le32_to_cpu(resp->reset_inprogress_reg);
9930 	fw_health->fw_reset_inprog_reg_mask =
9931 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9932 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9933 	if (fw_health->fw_reset_seq_cnt >= 16) {
9934 		rc = -EINVAL;
9935 		goto err_recovery_out;
9936 	}
9937 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9938 		fw_health->fw_reset_seq_regs[i] =
9939 			le32_to_cpu(resp->reset_reg[i]);
9940 		fw_health->fw_reset_seq_vals[i] =
9941 			le32_to_cpu(resp->reset_reg_val[i]);
9942 		fw_health->fw_reset_seq_delay_msec[i] =
9943 			resp->delay_after_reset[i];
9944 	}
9945 err_recovery_out:
9946 	hwrm_req_drop(bp, req);
9947 	if (!rc)
9948 		rc = bnxt_map_fw_health_regs(bp);
9949 	if (rc)
9950 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9951 	return rc;
9952 }
9953 
9954 static int bnxt_hwrm_func_reset(struct bnxt *bp)
9955 {
9956 	struct hwrm_func_reset_input *req;
9957 	int rc;
9958 
9959 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
9960 	if (rc)
9961 		return rc;
9962 
9963 	req->enables = 0;
9964 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
9965 	return hwrm_req_send(bp, req);
9966 }
9967 
9968 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
9969 {
9970 	struct hwrm_nvm_get_dev_info_output nvm_info;
9971 
9972 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
9973 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
9974 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
9975 			 nvm_info.nvm_cfg_ver_upd);
9976 }
9977 
9978 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
9979 {
9980 	struct hwrm_queue_qportcfg_output *resp;
9981 	struct hwrm_queue_qportcfg_input *req;
9982 	u8 i, j, *qptr;
9983 	bool no_rdma;
9984 	int rc = 0;
9985 
9986 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
9987 	if (rc)
9988 		return rc;
9989 
9990 	resp = hwrm_req_hold(bp, req);
9991 	rc = hwrm_req_send(bp, req);
9992 	if (rc)
9993 		goto qportcfg_exit;
9994 
9995 	if (!resp->max_configurable_queues) {
9996 		rc = -EINVAL;
9997 		goto qportcfg_exit;
9998 	}
9999 	bp->max_tc = resp->max_configurable_queues;
10000 	bp->max_lltc = resp->max_configurable_lossless_queues;
10001 	if (bp->max_tc > BNXT_MAX_QUEUE)
10002 		bp->max_tc = BNXT_MAX_QUEUE;
10003 
10004 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10005 	qptr = &resp->queue_id0;
10006 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10007 		bp->q_info[j].queue_id = *qptr;
10008 		bp->q_ids[i] = *qptr++;
10009 		bp->q_info[j].queue_profile = *qptr++;
10010 		bp->tc_to_qidx[j] = j;
10011 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10012 		    (no_rdma && BNXT_PF(bp)))
10013 			j++;
10014 	}
10015 	bp->max_q = bp->max_tc;
10016 	bp->max_tc = max_t(u8, j, 1);
10017 
10018 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10019 		bp->max_tc = 1;
10020 
10021 	if (bp->max_lltc > bp->max_tc)
10022 		bp->max_lltc = bp->max_tc;
10023 
10024 qportcfg_exit:
10025 	hwrm_req_drop(bp, req);
10026 	return rc;
10027 }
10028 
10029 static int bnxt_hwrm_poll(struct bnxt *bp)
10030 {
10031 	struct hwrm_ver_get_input *req;
10032 	int rc;
10033 
10034 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10035 	if (rc)
10036 		return rc;
10037 
10038 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10039 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10040 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10041 
10042 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10043 	rc = hwrm_req_send(bp, req);
10044 	return rc;
10045 }
10046 
10047 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10048 {
10049 	struct hwrm_ver_get_output *resp;
10050 	struct hwrm_ver_get_input *req;
10051 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10052 	u32 dev_caps_cfg, hwrm_ver;
10053 	int rc, len;
10054 
10055 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10056 	if (rc)
10057 		return rc;
10058 
10059 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10060 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10061 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10062 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10063 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10064 
10065 	resp = hwrm_req_hold(bp, req);
10066 	rc = hwrm_req_send(bp, req);
10067 	if (rc)
10068 		goto hwrm_ver_get_exit;
10069 
10070 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10071 
10072 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10073 			     resp->hwrm_intf_min_8b << 8 |
10074 			     resp->hwrm_intf_upd_8b;
10075 	if (resp->hwrm_intf_maj_8b < 1) {
10076 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10077 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10078 			    resp->hwrm_intf_upd_8b);
10079 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10080 	}
10081 
10082 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10083 			HWRM_VERSION_UPDATE;
10084 
10085 	if (bp->hwrm_spec_code > hwrm_ver)
10086 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10087 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10088 			 HWRM_VERSION_UPDATE);
10089 	else
10090 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10091 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10092 			 resp->hwrm_intf_upd_8b);
10093 
10094 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10095 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10096 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10097 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10098 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10099 		len = FW_VER_STR_LEN;
10100 	} else {
10101 		fw_maj = resp->hwrm_fw_maj_8b;
10102 		fw_min = resp->hwrm_fw_min_8b;
10103 		fw_bld = resp->hwrm_fw_bld_8b;
10104 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10105 		len = BC_HWRM_STR_LEN;
10106 	}
10107 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10108 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10109 		 fw_rsv);
10110 
10111 	if (strlen(resp->active_pkg_name)) {
10112 		int fw_ver_len = strlen(bp->fw_ver_str);
10113 
10114 		snprintf(bp->fw_ver_str + fw_ver_len,
10115 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10116 			 resp->active_pkg_name);
10117 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10118 	}
10119 
10120 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10121 	if (!bp->hwrm_cmd_timeout)
10122 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10123 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10124 	if (!bp->hwrm_cmd_max_timeout)
10125 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10126 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
10127 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
10128 			    bp->hwrm_cmd_max_timeout / 1000);
10129 
10130 	if (resp->hwrm_intf_maj_8b >= 1) {
10131 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10132 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10133 	}
10134 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10135 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10136 
10137 	bp->chip_num = le16_to_cpu(resp->chip_num);
10138 	bp->chip_rev = resp->chip_rev;
10139 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10140 	    !resp->chip_metal)
10141 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10142 
10143 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10144 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10145 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10146 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10147 
10148 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10149 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10150 
10151 	if (dev_caps_cfg &
10152 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10153 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10154 
10155 	if (dev_caps_cfg &
10156 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10157 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10158 
10159 	if (dev_caps_cfg &
10160 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10161 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10162 
10163 hwrm_ver_get_exit:
10164 	hwrm_req_drop(bp, req);
10165 	return rc;
10166 }
10167 
10168 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10169 {
10170 	struct hwrm_fw_set_time_input *req;
10171 	struct tm tm;
10172 	time64_t now = ktime_get_real_seconds();
10173 	int rc;
10174 
10175 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10176 	    bp->hwrm_spec_code < 0x10400)
10177 		return -EOPNOTSUPP;
10178 
10179 	time64_to_tm(now, 0, &tm);
10180 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10181 	if (rc)
10182 		return rc;
10183 
10184 	req->year = cpu_to_le16(1900 + tm.tm_year);
10185 	req->month = 1 + tm.tm_mon;
10186 	req->day = tm.tm_mday;
10187 	req->hour = tm.tm_hour;
10188 	req->minute = tm.tm_min;
10189 	req->second = tm.tm_sec;
10190 	return hwrm_req_send(bp, req);
10191 }
10192 
10193 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10194 {
10195 	u64 sw_tmp;
10196 
10197 	hw &= mask;
10198 	sw_tmp = (*sw & ~mask) | hw;
10199 	if (hw < (*sw & mask))
10200 		sw_tmp += mask + 1;
10201 	WRITE_ONCE(*sw, sw_tmp);
10202 }
10203 
10204 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10205 				    int count, bool ignore_zero)
10206 {
10207 	int i;
10208 
10209 	for (i = 0; i < count; i++) {
10210 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10211 
10212 		if (ignore_zero && !hw)
10213 			continue;
10214 
10215 		if (masks[i] == -1ULL)
10216 			sw_stats[i] = hw;
10217 		else
10218 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10219 	}
10220 }
10221 
10222 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10223 {
10224 	if (!stats->hw_stats)
10225 		return;
10226 
10227 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10228 				stats->hw_masks, stats->len / 8, false);
10229 }
10230 
10231 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10232 {
10233 	struct bnxt_stats_mem *ring0_stats;
10234 	bool ignore_zero = false;
10235 	int i;
10236 
10237 	/* Chip bug.  Counter intermittently becomes 0. */
10238 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10239 		ignore_zero = true;
10240 
10241 	for (i = 0; i < bp->cp_nr_rings; i++) {
10242 		struct bnxt_napi *bnapi = bp->bnapi[i];
10243 		struct bnxt_cp_ring_info *cpr;
10244 		struct bnxt_stats_mem *stats;
10245 
10246 		cpr = &bnapi->cp_ring;
10247 		stats = &cpr->stats;
10248 		if (!i)
10249 			ring0_stats = stats;
10250 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10251 					ring0_stats->hw_masks,
10252 					ring0_stats->len / 8, ignore_zero);
10253 	}
10254 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10255 		struct bnxt_stats_mem *stats = &bp->port_stats;
10256 		__le64 *hw_stats = stats->hw_stats;
10257 		u64 *sw_stats = stats->sw_stats;
10258 		u64 *masks = stats->hw_masks;
10259 		int cnt;
10260 
10261 		cnt = sizeof(struct rx_port_stats) / 8;
10262 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10263 
10264 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10265 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10266 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10267 		cnt = sizeof(struct tx_port_stats) / 8;
10268 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10269 	}
10270 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10271 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10272 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10273 	}
10274 }
10275 
10276 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10277 {
10278 	struct hwrm_port_qstats_input *req;
10279 	struct bnxt_pf_info *pf = &bp->pf;
10280 	int rc;
10281 
10282 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10283 		return 0;
10284 
10285 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10286 		return -EOPNOTSUPP;
10287 
10288 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10289 	if (rc)
10290 		return rc;
10291 
10292 	req->flags = flags;
10293 	req->port_id = cpu_to_le16(pf->port_id);
10294 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10295 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10296 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10297 	return hwrm_req_send(bp, req);
10298 }
10299 
10300 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10301 {
10302 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10303 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10304 	struct hwrm_port_qstats_ext_output *resp_qs;
10305 	struct hwrm_port_qstats_ext_input *req_qs;
10306 	struct bnxt_pf_info *pf = &bp->pf;
10307 	u32 tx_stat_size;
10308 	int rc;
10309 
10310 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10311 		return 0;
10312 
10313 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10314 		return -EOPNOTSUPP;
10315 
10316 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10317 	if (rc)
10318 		return rc;
10319 
10320 	req_qs->flags = flags;
10321 	req_qs->port_id = cpu_to_le16(pf->port_id);
10322 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10323 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10324 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10325 		       sizeof(struct tx_port_stats_ext) : 0;
10326 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10327 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10328 	resp_qs = hwrm_req_hold(bp, req_qs);
10329 	rc = hwrm_req_send(bp, req_qs);
10330 	if (!rc) {
10331 		bp->fw_rx_stats_ext_size =
10332 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10333 		if (BNXT_FW_MAJ(bp) < 220 &&
10334 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10335 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10336 
10337 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10338 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10339 	} else {
10340 		bp->fw_rx_stats_ext_size = 0;
10341 		bp->fw_tx_stats_ext_size = 0;
10342 	}
10343 	hwrm_req_drop(bp, req_qs);
10344 
10345 	if (flags)
10346 		return rc;
10347 
10348 	if (bp->fw_tx_stats_ext_size <=
10349 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10350 		bp->pri2cos_valid = 0;
10351 		return rc;
10352 	}
10353 
10354 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10355 	if (rc)
10356 		return rc;
10357 
10358 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10359 
10360 	resp_qc = hwrm_req_hold(bp, req_qc);
10361 	rc = hwrm_req_send(bp, req_qc);
10362 	if (!rc) {
10363 		u8 *pri2cos;
10364 		int i, j;
10365 
10366 		pri2cos = &resp_qc->pri0_cos_queue_id;
10367 		for (i = 0; i < 8; i++) {
10368 			u8 queue_id = pri2cos[i];
10369 			u8 queue_idx;
10370 
10371 			/* Per port queue IDs start from 0, 10, 20, etc */
10372 			queue_idx = queue_id % 10;
10373 			if (queue_idx > BNXT_MAX_QUEUE) {
10374 				bp->pri2cos_valid = false;
10375 				hwrm_req_drop(bp, req_qc);
10376 				return rc;
10377 			}
10378 			for (j = 0; j < bp->max_q; j++) {
10379 				if (bp->q_ids[j] == queue_id)
10380 					bp->pri2cos_idx[i] = queue_idx;
10381 			}
10382 		}
10383 		bp->pri2cos_valid = true;
10384 	}
10385 	hwrm_req_drop(bp, req_qc);
10386 
10387 	return rc;
10388 }
10389 
10390 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10391 {
10392 	bnxt_hwrm_tunnel_dst_port_free(bp,
10393 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10394 	bnxt_hwrm_tunnel_dst_port_free(bp,
10395 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10396 }
10397 
10398 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10399 {
10400 	int rc, i;
10401 	u32 tpa_flags = 0;
10402 
10403 	if (set_tpa)
10404 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10405 	else if (BNXT_NO_FW_ACCESS(bp))
10406 		return 0;
10407 	for (i = 0; i < bp->nr_vnics; i++) {
10408 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10409 		if (rc) {
10410 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10411 				   i, rc);
10412 			return rc;
10413 		}
10414 	}
10415 	return 0;
10416 }
10417 
10418 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10419 {
10420 	int i;
10421 
10422 	for (i = 0; i < bp->nr_vnics; i++)
10423 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10424 }
10425 
10426 static void bnxt_clear_vnic(struct bnxt *bp)
10427 {
10428 	if (!bp->vnic_info)
10429 		return;
10430 
10431 	bnxt_hwrm_clear_vnic_filter(bp);
10432 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10433 		/* clear all RSS setting before free vnic ctx */
10434 		bnxt_hwrm_clear_vnic_rss(bp);
10435 		bnxt_hwrm_vnic_ctx_free(bp);
10436 	}
10437 	/* before free the vnic, undo the vnic tpa settings */
10438 	if (bp->flags & BNXT_FLAG_TPA)
10439 		bnxt_set_tpa(bp, false);
10440 	bnxt_hwrm_vnic_free(bp);
10441 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10442 		bnxt_hwrm_vnic_ctx_free(bp);
10443 }
10444 
10445 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10446 				    bool irq_re_init)
10447 {
10448 	bnxt_clear_vnic(bp);
10449 	bnxt_hwrm_ring_free(bp, close_path);
10450 	bnxt_hwrm_ring_grp_free(bp);
10451 	if (irq_re_init) {
10452 		bnxt_hwrm_stat_ctx_free(bp);
10453 		bnxt_hwrm_free_tunnel_ports(bp);
10454 	}
10455 }
10456 
10457 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10458 {
10459 	struct hwrm_func_cfg_input *req;
10460 	u8 evb_mode;
10461 	int rc;
10462 
10463 	if (br_mode == BRIDGE_MODE_VEB)
10464 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10465 	else if (br_mode == BRIDGE_MODE_VEPA)
10466 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10467 	else
10468 		return -EINVAL;
10469 
10470 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10471 	if (rc)
10472 		return rc;
10473 
10474 	req->fid = cpu_to_le16(0xffff);
10475 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10476 	req->evb_mode = evb_mode;
10477 	return hwrm_req_send(bp, req);
10478 }
10479 
10480 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10481 {
10482 	struct hwrm_func_cfg_input *req;
10483 	int rc;
10484 
10485 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10486 		return 0;
10487 
10488 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10489 	if (rc)
10490 		return rc;
10491 
10492 	req->fid = cpu_to_le16(0xffff);
10493 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10494 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10495 	if (size == 128)
10496 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10497 
10498 	return hwrm_req_send(bp, req);
10499 }
10500 
10501 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10502 {
10503 	int rc;
10504 
10505 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10506 		goto skip_rss_ctx;
10507 
10508 	/* allocate context for vnic */
10509 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10510 	if (rc) {
10511 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10512 			   vnic->vnic_id, rc);
10513 		goto vnic_setup_err;
10514 	}
10515 	bp->rsscos_nr_ctxs++;
10516 
10517 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10518 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10519 		if (rc) {
10520 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10521 				   vnic->vnic_id, rc);
10522 			goto vnic_setup_err;
10523 		}
10524 		bp->rsscos_nr_ctxs++;
10525 	}
10526 
10527 skip_rss_ctx:
10528 	/* configure default vnic, ring grp */
10529 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10530 	if (rc) {
10531 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10532 			   vnic->vnic_id, rc);
10533 		goto vnic_setup_err;
10534 	}
10535 
10536 	/* Enable RSS hashing on vnic */
10537 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10538 	if (rc) {
10539 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10540 			   vnic->vnic_id, rc);
10541 		goto vnic_setup_err;
10542 	}
10543 
10544 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10545 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10546 		if (rc) {
10547 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10548 				   vnic->vnic_id, rc);
10549 		}
10550 	}
10551 
10552 vnic_setup_err:
10553 	return rc;
10554 }
10555 
10556 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10557 			  u8 valid)
10558 {
10559 	struct hwrm_vnic_update_input *req;
10560 	int rc;
10561 
10562 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10563 	if (rc)
10564 		return rc;
10565 
10566 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10567 
10568 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10569 		req->mru = cpu_to_le16(vnic->mru);
10570 
10571 	req->enables = cpu_to_le32(valid);
10572 
10573 	return hwrm_req_send(bp, req);
10574 }
10575 
10576 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10577 {
10578 	int rc;
10579 
10580 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10581 	if (rc) {
10582 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10583 			   vnic->vnic_id, rc);
10584 		return rc;
10585 	}
10586 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10587 	if (rc)
10588 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10589 			   vnic->vnic_id, rc);
10590 	return rc;
10591 }
10592 
10593 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10594 {
10595 	int rc, i, nr_ctxs;
10596 
10597 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10598 	for (i = 0; i < nr_ctxs; i++) {
10599 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10600 		if (rc) {
10601 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10602 				   vnic->vnic_id, i, rc);
10603 			break;
10604 		}
10605 		bp->rsscos_nr_ctxs++;
10606 	}
10607 	if (i < nr_ctxs)
10608 		return -ENOMEM;
10609 
10610 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10611 	if (rc)
10612 		return rc;
10613 
10614 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10615 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10616 		if (rc) {
10617 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10618 				   vnic->vnic_id, rc);
10619 		}
10620 	}
10621 	return rc;
10622 }
10623 
10624 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10625 {
10626 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10627 		return __bnxt_setup_vnic_p5(bp, vnic);
10628 	else
10629 		return __bnxt_setup_vnic(bp, vnic);
10630 }
10631 
10632 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10633 				     struct bnxt_vnic_info *vnic,
10634 				     u16 start_rx_ring_idx, int rx_rings)
10635 {
10636 	int rc;
10637 
10638 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10639 	if (rc) {
10640 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10641 			   vnic->vnic_id, rc);
10642 		return rc;
10643 	}
10644 	return bnxt_setup_vnic(bp, vnic);
10645 }
10646 
10647 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10648 {
10649 	struct bnxt_vnic_info *vnic;
10650 	int i, rc = 0;
10651 
10652 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10653 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10654 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10655 	}
10656 
10657 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10658 		return 0;
10659 
10660 	for (i = 0; i < bp->rx_nr_rings; i++) {
10661 		u16 vnic_id = i + 1;
10662 		u16 ring_id = i;
10663 
10664 		if (vnic_id >= bp->nr_vnics)
10665 			break;
10666 
10667 		vnic = &bp->vnic_info[vnic_id];
10668 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10669 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10670 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10671 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10672 			break;
10673 	}
10674 	return rc;
10675 }
10676 
10677 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10678 			  bool all)
10679 {
10680 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10681 	struct bnxt_filter_base *usr_fltr, *tmp;
10682 	struct bnxt_ntuple_filter *ntp_fltr;
10683 	int i;
10684 
10685 	if (netif_running(bp->dev)) {
10686 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10687 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10688 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10689 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10690 		}
10691 	}
10692 	if (!all)
10693 		return;
10694 
10695 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10696 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10697 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10698 			ntp_fltr = container_of(usr_fltr,
10699 						struct bnxt_ntuple_filter,
10700 						base);
10701 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10702 			bnxt_del_ntp_filter(bp, ntp_fltr);
10703 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10704 		}
10705 	}
10706 
10707 	if (vnic->rss_table)
10708 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10709 				  vnic->rss_table,
10710 				  vnic->rss_table_dma_addr);
10711 	bp->num_rss_ctx--;
10712 }
10713 
10714 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10715 {
10716 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10717 	struct ethtool_rxfh_context *ctx;
10718 	unsigned long context;
10719 
10720 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10721 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10722 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10723 
10724 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10725 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10726 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10727 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10728 				   rss_ctx->index);
10729 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10730 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10731 		}
10732 	}
10733 }
10734 
10735 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10736 {
10737 	struct ethtool_rxfh_context *ctx;
10738 	unsigned long context;
10739 
10740 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10741 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10742 
10743 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10744 	}
10745 }
10746 
10747 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10748 static bool bnxt_promisc_ok(struct bnxt *bp)
10749 {
10750 #ifdef CONFIG_BNXT_SRIOV
10751 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10752 		return false;
10753 #endif
10754 	return true;
10755 }
10756 
10757 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10758 {
10759 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10760 	unsigned int rc = 0;
10761 
10762 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10763 	if (rc) {
10764 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10765 			   rc);
10766 		return rc;
10767 	}
10768 
10769 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10770 	if (rc) {
10771 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10772 			   rc);
10773 		return rc;
10774 	}
10775 	return rc;
10776 }
10777 
10778 static int bnxt_cfg_rx_mode(struct bnxt *);
10779 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10780 
10781 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10782 {
10783 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10784 	int rc = 0;
10785 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10786 
10787 	if (irq_re_init) {
10788 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10789 		if (rc) {
10790 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10791 				   rc);
10792 			goto err_out;
10793 		}
10794 	}
10795 
10796 	rc = bnxt_hwrm_ring_alloc(bp);
10797 	if (rc) {
10798 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10799 		goto err_out;
10800 	}
10801 
10802 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10803 	if (rc) {
10804 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10805 		goto err_out;
10806 	}
10807 
10808 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10809 		rx_nr_rings--;
10810 
10811 	/* default vnic 0 */
10812 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10813 	if (rc) {
10814 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10815 		goto err_out;
10816 	}
10817 
10818 	if (BNXT_VF(bp))
10819 		bnxt_hwrm_func_qcfg(bp);
10820 
10821 	rc = bnxt_setup_vnic(bp, vnic);
10822 	if (rc)
10823 		goto err_out;
10824 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10825 		bnxt_hwrm_update_rss_hash_cfg(bp);
10826 
10827 	if (bp->flags & BNXT_FLAG_RFS) {
10828 		rc = bnxt_alloc_rfs_vnics(bp);
10829 		if (rc)
10830 			goto err_out;
10831 	}
10832 
10833 	if (bp->flags & BNXT_FLAG_TPA) {
10834 		rc = bnxt_set_tpa(bp, true);
10835 		if (rc)
10836 			goto err_out;
10837 	}
10838 
10839 	if (BNXT_VF(bp))
10840 		bnxt_update_vf_mac(bp);
10841 
10842 	/* Filter for default vnic 0 */
10843 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10844 	if (rc) {
10845 		if (BNXT_VF(bp) && rc == -ENODEV)
10846 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10847 		else
10848 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10849 		goto err_out;
10850 	}
10851 	vnic->uc_filter_count = 1;
10852 
10853 	vnic->rx_mask = 0;
10854 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10855 		goto skip_rx_mask;
10856 
10857 	if (bp->dev->flags & IFF_BROADCAST)
10858 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10859 
10860 	if (bp->dev->flags & IFF_PROMISC)
10861 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10862 
10863 	if (bp->dev->flags & IFF_ALLMULTI) {
10864 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10865 		vnic->mc_list_count = 0;
10866 	} else if (bp->dev->flags & IFF_MULTICAST) {
10867 		u32 mask = 0;
10868 
10869 		bnxt_mc_list_updated(bp, &mask);
10870 		vnic->rx_mask |= mask;
10871 	}
10872 
10873 	rc = bnxt_cfg_rx_mode(bp);
10874 	if (rc)
10875 		goto err_out;
10876 
10877 skip_rx_mask:
10878 	rc = bnxt_hwrm_set_coal(bp);
10879 	if (rc)
10880 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10881 				rc);
10882 
10883 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10884 		rc = bnxt_setup_nitroa0_vnic(bp);
10885 		if (rc)
10886 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10887 				   rc);
10888 	}
10889 
10890 	if (BNXT_VF(bp)) {
10891 		bnxt_hwrm_func_qcfg(bp);
10892 		netdev_update_features(bp->dev);
10893 	}
10894 
10895 	return 0;
10896 
10897 err_out:
10898 	bnxt_hwrm_resource_free(bp, 0, true);
10899 
10900 	return rc;
10901 }
10902 
10903 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10904 {
10905 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10906 	return 0;
10907 }
10908 
10909 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10910 {
10911 	bnxt_init_cp_rings(bp);
10912 	bnxt_init_rx_rings(bp);
10913 	bnxt_init_tx_rings(bp);
10914 	bnxt_init_ring_grps(bp, irq_re_init);
10915 	bnxt_init_vnics(bp);
10916 
10917 	return bnxt_init_chip(bp, irq_re_init);
10918 }
10919 
10920 static int bnxt_set_real_num_queues(struct bnxt *bp)
10921 {
10922 	int rc;
10923 	struct net_device *dev = bp->dev;
10924 
10925 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10926 					  bp->tx_nr_rings_xdp);
10927 	if (rc)
10928 		return rc;
10929 
10930 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10931 	if (rc)
10932 		return rc;
10933 
10934 #ifdef CONFIG_RFS_ACCEL
10935 	if (bp->flags & BNXT_FLAG_RFS)
10936 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
10937 #endif
10938 
10939 	return rc;
10940 }
10941 
10942 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10943 			     bool shared)
10944 {
10945 	int _rx = *rx, _tx = *tx;
10946 
10947 	if (shared) {
10948 		*rx = min_t(int, _rx, max);
10949 		*tx = min_t(int, _tx, max);
10950 	} else {
10951 		if (max < 2)
10952 			return -ENOMEM;
10953 
10954 		while (_rx + _tx > max) {
10955 			if (_rx > _tx && _rx > 1)
10956 				_rx--;
10957 			else if (_tx > 1)
10958 				_tx--;
10959 		}
10960 		*rx = _rx;
10961 		*tx = _tx;
10962 	}
10963 	return 0;
10964 }
10965 
10966 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
10967 {
10968 	return (tx - tx_xdp) / tx_sets + tx_xdp;
10969 }
10970 
10971 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
10972 {
10973 	int tcs = bp->num_tc;
10974 
10975 	if (!tcs)
10976 		tcs = 1;
10977 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
10978 }
10979 
10980 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
10981 {
10982 	int tcs = bp->num_tc;
10983 
10984 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
10985 	       bp->tx_nr_rings_xdp;
10986 }
10987 
10988 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10989 			   bool sh)
10990 {
10991 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
10992 
10993 	if (tx_cp != *tx) {
10994 		int tx_saved = tx_cp, rc;
10995 
10996 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
10997 		if (rc)
10998 			return rc;
10999 		if (tx_cp != tx_saved)
11000 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11001 		return 0;
11002 	}
11003 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11004 }
11005 
11006 static void bnxt_setup_msix(struct bnxt *bp)
11007 {
11008 	const int len = sizeof(bp->irq_tbl[0].name);
11009 	struct net_device *dev = bp->dev;
11010 	int tcs, i;
11011 
11012 	tcs = bp->num_tc;
11013 	if (tcs) {
11014 		int i, off, count;
11015 
11016 		for (i = 0; i < tcs; i++) {
11017 			count = bp->tx_nr_rings_per_tc;
11018 			off = BNXT_TC_TO_RING_BASE(bp, i);
11019 			netdev_set_tc_queue(dev, i, count, off);
11020 		}
11021 	}
11022 
11023 	for (i = 0; i < bp->cp_nr_rings; i++) {
11024 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11025 		char *attr;
11026 
11027 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11028 			attr = "TxRx";
11029 		else if (i < bp->rx_nr_rings)
11030 			attr = "rx";
11031 		else
11032 			attr = "tx";
11033 
11034 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11035 			 attr, i);
11036 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11037 	}
11038 }
11039 
11040 static int bnxt_init_int_mode(struct bnxt *bp);
11041 
11042 static int bnxt_change_msix(struct bnxt *bp, int total)
11043 {
11044 	struct msi_map map;
11045 	int i;
11046 
11047 	/* add MSIX to the end if needed */
11048 	for (i = bp->total_irqs; i < total; i++) {
11049 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11050 		if (map.index < 0)
11051 			return bp->total_irqs;
11052 		bp->irq_tbl[i].vector = map.virq;
11053 		bp->total_irqs++;
11054 	}
11055 
11056 	/* trim MSIX from the end if needed */
11057 	for (i = bp->total_irqs; i > total; i--) {
11058 		map.index = i - 1;
11059 		map.virq = bp->irq_tbl[i - 1].vector;
11060 		pci_msix_free_irq(bp->pdev, map);
11061 		bp->total_irqs--;
11062 	}
11063 	return bp->total_irqs;
11064 }
11065 
11066 static int bnxt_setup_int_mode(struct bnxt *bp)
11067 {
11068 	int rc;
11069 
11070 	if (!bp->irq_tbl) {
11071 		rc = bnxt_init_int_mode(bp);
11072 		if (rc || !bp->irq_tbl)
11073 			return rc ?: -ENODEV;
11074 	}
11075 
11076 	bnxt_setup_msix(bp);
11077 
11078 	rc = bnxt_set_real_num_queues(bp);
11079 	return rc;
11080 }
11081 
11082 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11083 {
11084 	return bp->hw_resc.max_rsscos_ctxs;
11085 }
11086 
11087 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11088 {
11089 	return bp->hw_resc.max_vnics;
11090 }
11091 
11092 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11093 {
11094 	return bp->hw_resc.max_stat_ctxs;
11095 }
11096 
11097 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11098 {
11099 	return bp->hw_resc.max_cp_rings;
11100 }
11101 
11102 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11103 {
11104 	unsigned int cp = bp->hw_resc.max_cp_rings;
11105 
11106 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11107 		cp -= bnxt_get_ulp_msix_num(bp);
11108 
11109 	return cp;
11110 }
11111 
11112 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11113 {
11114 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11115 
11116 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11117 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11118 
11119 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11120 }
11121 
11122 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11123 {
11124 	bp->hw_resc.max_irqs = max_irqs;
11125 }
11126 
11127 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11128 {
11129 	unsigned int cp;
11130 
11131 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11132 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11133 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11134 	else
11135 		return cp - bp->cp_nr_rings;
11136 }
11137 
11138 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11139 {
11140 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11141 }
11142 
11143 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11144 {
11145 	int max_irq = bnxt_get_max_func_irqs(bp);
11146 	int total_req = bp->cp_nr_rings + num;
11147 
11148 	if (max_irq < total_req) {
11149 		num = max_irq - bp->cp_nr_rings;
11150 		if (num <= 0)
11151 			return 0;
11152 	}
11153 	return num;
11154 }
11155 
11156 static int bnxt_get_num_msix(struct bnxt *bp)
11157 {
11158 	if (!BNXT_NEW_RM(bp))
11159 		return bnxt_get_max_func_irqs(bp);
11160 
11161 	return bnxt_nq_rings_in_use(bp);
11162 }
11163 
11164 static int bnxt_init_int_mode(struct bnxt *bp)
11165 {
11166 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11167 
11168 	total_vecs = bnxt_get_num_msix(bp);
11169 	max = bnxt_get_max_func_irqs(bp);
11170 	if (total_vecs > max)
11171 		total_vecs = max;
11172 
11173 	if (!total_vecs)
11174 		return 0;
11175 
11176 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11177 		min = 2;
11178 
11179 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11180 					   PCI_IRQ_MSIX);
11181 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11182 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11183 		rc = -ENODEV;
11184 		goto msix_setup_exit;
11185 	}
11186 
11187 	tbl_size = total_vecs;
11188 	if (pci_msix_can_alloc_dyn(bp->pdev))
11189 		tbl_size = max;
11190 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
11191 	if (bp->irq_tbl) {
11192 		for (i = 0; i < total_vecs; i++)
11193 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11194 
11195 		bp->total_irqs = total_vecs;
11196 		/* Trim rings based upon num of vectors allocated */
11197 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11198 				     total_vecs - ulp_msix, min == 1);
11199 		if (rc)
11200 			goto msix_setup_exit;
11201 
11202 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11203 		bp->cp_nr_rings = (min == 1) ?
11204 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11205 				  tx_cp + bp->rx_nr_rings;
11206 
11207 	} else {
11208 		rc = -ENOMEM;
11209 		goto msix_setup_exit;
11210 	}
11211 	return 0;
11212 
11213 msix_setup_exit:
11214 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11215 	kfree(bp->irq_tbl);
11216 	bp->irq_tbl = NULL;
11217 	pci_free_irq_vectors(bp->pdev);
11218 	return rc;
11219 }
11220 
11221 static void bnxt_clear_int_mode(struct bnxt *bp)
11222 {
11223 	pci_free_irq_vectors(bp->pdev);
11224 
11225 	kfree(bp->irq_tbl);
11226 	bp->irq_tbl = NULL;
11227 }
11228 
11229 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11230 {
11231 	bool irq_cleared = false;
11232 	bool irq_change = false;
11233 	int tcs = bp->num_tc;
11234 	int irqs_required;
11235 	int rc;
11236 
11237 	if (!bnxt_need_reserve_rings(bp))
11238 		return 0;
11239 
11240 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11241 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11242 
11243 		if (ulp_msix > bp->ulp_num_msix_want)
11244 			ulp_msix = bp->ulp_num_msix_want;
11245 		irqs_required = ulp_msix + bp->cp_nr_rings;
11246 	} else {
11247 		irqs_required = bnxt_get_num_msix(bp);
11248 	}
11249 
11250 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11251 		irq_change = true;
11252 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11253 			bnxt_ulp_irq_stop(bp);
11254 			bnxt_clear_int_mode(bp);
11255 			irq_cleared = true;
11256 		}
11257 	}
11258 	rc = __bnxt_reserve_rings(bp);
11259 	if (irq_cleared) {
11260 		if (!rc)
11261 			rc = bnxt_init_int_mode(bp);
11262 		bnxt_ulp_irq_restart(bp, rc);
11263 	} else if (irq_change && !rc) {
11264 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11265 			rc = -ENOSPC;
11266 	}
11267 	if (rc) {
11268 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11269 		return rc;
11270 	}
11271 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11272 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11273 		netdev_err(bp->dev, "tx ring reservation failure\n");
11274 		netdev_reset_tc(bp->dev);
11275 		bp->num_tc = 0;
11276 		if (bp->tx_nr_rings_xdp)
11277 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11278 		else
11279 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11280 		return -ENOMEM;
11281 	}
11282 	return 0;
11283 }
11284 
11285 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11286 {
11287 	struct bnxt_tx_ring_info *txr;
11288 	struct netdev_queue *txq;
11289 	struct bnxt_napi *bnapi;
11290 	int i;
11291 
11292 	bnapi = bp->bnapi[idx];
11293 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11294 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11295 		synchronize_net();
11296 
11297 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11298 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11299 			if (txq) {
11300 				__netif_tx_lock_bh(txq);
11301 				netif_tx_stop_queue(txq);
11302 				__netif_tx_unlock_bh(txq);
11303 			}
11304 		}
11305 
11306 		if (!bp->tph_mode)
11307 			continue;
11308 
11309 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11310 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11311 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11312 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11313 	}
11314 }
11315 
11316 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11317 {
11318 	struct bnxt_tx_ring_info *txr;
11319 	struct netdev_queue *txq;
11320 	struct bnxt_napi *bnapi;
11321 	int rc, i;
11322 
11323 	bnapi = bp->bnapi[idx];
11324 	/* All rings have been reserved and previously allocated.
11325 	 * Reallocating with the same parameters should never fail.
11326 	 */
11327 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11328 		if (!bp->tph_mode)
11329 			goto start_tx;
11330 
11331 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11332 		if (rc)
11333 			return rc;
11334 
11335 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11336 		if (rc)
11337 			return rc;
11338 
11339 		txr->tx_prod = 0;
11340 		txr->tx_cons = 0;
11341 		txr->tx_hw_cons = 0;
11342 start_tx:
11343 		WRITE_ONCE(txr->dev_state, 0);
11344 		synchronize_net();
11345 
11346 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11347 			continue;
11348 
11349 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11350 		if (txq)
11351 			netif_tx_start_queue(txq);
11352 	}
11353 
11354 	return 0;
11355 }
11356 
11357 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11358 				     const cpumask_t *mask)
11359 {
11360 	struct bnxt_irq *irq;
11361 	u16 tag;
11362 	int err;
11363 
11364 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11365 
11366 	if (!irq->bp->tph_mode)
11367 		return;
11368 
11369 	cpumask_copy(irq->cpu_mask, mask);
11370 
11371 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11372 		return;
11373 
11374 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11375 				cpumask_first(irq->cpu_mask), &tag))
11376 		return;
11377 
11378 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag))
11379 		return;
11380 
11381 	rtnl_lock();
11382 	if (netif_running(irq->bp->dev)) {
11383 		err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr);
11384 		if (err)
11385 			netdev_err(irq->bp->dev,
11386 				   "RX queue restart failed: err=%d\n", err);
11387 	}
11388 	rtnl_unlock();
11389 }
11390 
11391 static void bnxt_irq_affinity_release(struct kref *ref)
11392 {
11393 	struct irq_affinity_notify *notify =
11394 		container_of(ref, struct irq_affinity_notify, kref);
11395 	struct bnxt_irq *irq;
11396 
11397 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11398 
11399 	if (!irq->bp->tph_mode)
11400 		return;
11401 
11402 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11403 		netdev_err(irq->bp->dev,
11404 			   "Setting ST=0 for MSIX entry %d failed\n",
11405 			   irq->msix_nr);
11406 		return;
11407 	}
11408 }
11409 
11410 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11411 {
11412 	irq_set_affinity_notifier(irq->vector, NULL);
11413 }
11414 
11415 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11416 {
11417 	struct irq_affinity_notify *notify;
11418 
11419 	irq->bp = bp;
11420 
11421 	/* Nothing to do if TPH is not enabled */
11422 	if (!bp->tph_mode)
11423 		return;
11424 
11425 	/* Register IRQ affinity notifier */
11426 	notify = &irq->affinity_notify;
11427 	notify->irq = irq->vector;
11428 	notify->notify = bnxt_irq_affinity_notify;
11429 	notify->release = bnxt_irq_affinity_release;
11430 
11431 	irq_set_affinity_notifier(irq->vector, notify);
11432 }
11433 
11434 static void bnxt_free_irq(struct bnxt *bp)
11435 {
11436 	struct bnxt_irq *irq;
11437 	int i;
11438 
11439 #ifdef CONFIG_RFS_ACCEL
11440 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11441 	bp->dev->rx_cpu_rmap = NULL;
11442 #endif
11443 	if (!bp->irq_tbl || !bp->bnapi)
11444 		return;
11445 
11446 	for (i = 0; i < bp->cp_nr_rings; i++) {
11447 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11448 
11449 		irq = &bp->irq_tbl[map_idx];
11450 		if (irq->requested) {
11451 			if (irq->have_cpumask) {
11452 				irq_update_affinity_hint(irq->vector, NULL);
11453 				free_cpumask_var(irq->cpu_mask);
11454 				irq->have_cpumask = 0;
11455 			}
11456 
11457 			bnxt_release_irq_notifier(irq);
11458 
11459 			free_irq(irq->vector, bp->bnapi[i]);
11460 		}
11461 
11462 		irq->requested = 0;
11463 	}
11464 
11465 	/* Disable TPH support */
11466 	pcie_disable_tph(bp->pdev);
11467 	bp->tph_mode = 0;
11468 }
11469 
11470 static int bnxt_request_irq(struct bnxt *bp)
11471 {
11472 	int i, j, rc = 0;
11473 	unsigned long flags = 0;
11474 #ifdef CONFIG_RFS_ACCEL
11475 	struct cpu_rmap *rmap;
11476 #endif
11477 
11478 	rc = bnxt_setup_int_mode(bp);
11479 	if (rc) {
11480 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11481 			   rc);
11482 		return rc;
11483 	}
11484 #ifdef CONFIG_RFS_ACCEL
11485 	rmap = bp->dev->rx_cpu_rmap;
11486 #endif
11487 
11488 	/* Enable TPH support as part of IRQ request */
11489 	rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
11490 	if (!rc)
11491 		bp->tph_mode = PCI_TPH_ST_IV_MODE;
11492 
11493 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11494 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11495 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11496 
11497 #ifdef CONFIG_RFS_ACCEL
11498 		if (rmap && bp->bnapi[i]->rx_ring) {
11499 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11500 			if (rc)
11501 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11502 					    j);
11503 			j++;
11504 		}
11505 #endif
11506 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11507 				 bp->bnapi[i]);
11508 		if (rc)
11509 			break;
11510 
11511 		netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector);
11512 		irq->requested = 1;
11513 
11514 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11515 			int numa_node = dev_to_node(&bp->pdev->dev);
11516 			u16 tag;
11517 
11518 			irq->have_cpumask = 1;
11519 			irq->msix_nr = map_idx;
11520 			irq->ring_nr = i;
11521 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11522 					irq->cpu_mask);
11523 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11524 			if (rc) {
11525 				netdev_warn(bp->dev,
11526 					    "Update affinity hint failed, IRQ = %d\n",
11527 					    irq->vector);
11528 				break;
11529 			}
11530 
11531 			bnxt_register_irq_notifier(bp, irq);
11532 
11533 			/* Init ST table entry */
11534 			if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11535 						cpumask_first(irq->cpu_mask),
11536 						&tag))
11537 				continue;
11538 
11539 			pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag);
11540 		}
11541 	}
11542 	return rc;
11543 }
11544 
11545 static void bnxt_del_napi(struct bnxt *bp)
11546 {
11547 	int i;
11548 
11549 	if (!bp->bnapi)
11550 		return;
11551 
11552 	for (i = 0; i < bp->rx_nr_rings; i++)
11553 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11554 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11555 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11556 
11557 	for (i = 0; i < bp->cp_nr_rings; i++) {
11558 		struct bnxt_napi *bnapi = bp->bnapi[i];
11559 
11560 		__netif_napi_del(&bnapi->napi);
11561 	}
11562 	/* We called __netif_napi_del(), we need
11563 	 * to respect an RCU grace period before freeing napi structures.
11564 	 */
11565 	synchronize_net();
11566 }
11567 
11568 static void bnxt_init_napi(struct bnxt *bp)
11569 {
11570 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11571 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11572 	struct bnxt_napi *bnapi;
11573 	int i;
11574 
11575 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11576 		poll_fn = bnxt_poll_p5;
11577 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11578 		cp_nr_rings--;
11579 	for (i = 0; i < cp_nr_rings; i++) {
11580 		bnapi = bp->bnapi[i];
11581 		netif_napi_add_config(bp->dev, &bnapi->napi, poll_fn,
11582 				      bnapi->index);
11583 	}
11584 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11585 		bnapi = bp->bnapi[cp_nr_rings];
11586 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11587 	}
11588 }
11589 
11590 static void bnxt_disable_napi(struct bnxt *bp)
11591 {
11592 	int i;
11593 
11594 	if (!bp->bnapi ||
11595 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11596 		return;
11597 
11598 	for (i = 0; i < bp->cp_nr_rings; i++) {
11599 		struct bnxt_napi *bnapi = bp->bnapi[i];
11600 		struct bnxt_cp_ring_info *cpr;
11601 
11602 		cpr = &bnapi->cp_ring;
11603 		if (bnapi->tx_fault)
11604 			cpr->sw_stats->tx.tx_resets++;
11605 		if (bnapi->in_reset)
11606 			cpr->sw_stats->rx.rx_resets++;
11607 		napi_disable(&bnapi->napi);
11608 	}
11609 }
11610 
11611 static void bnxt_enable_napi(struct bnxt *bp)
11612 {
11613 	int i;
11614 
11615 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11616 	for (i = 0; i < bp->cp_nr_rings; i++) {
11617 		struct bnxt_napi *bnapi = bp->bnapi[i];
11618 		struct bnxt_cp_ring_info *cpr;
11619 
11620 		bnapi->tx_fault = 0;
11621 
11622 		cpr = &bnapi->cp_ring;
11623 		bnapi->in_reset = false;
11624 
11625 		if (bnapi->rx_ring) {
11626 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11627 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11628 		}
11629 		napi_enable(&bnapi->napi);
11630 	}
11631 }
11632 
11633 void bnxt_tx_disable(struct bnxt *bp)
11634 {
11635 	int i;
11636 	struct bnxt_tx_ring_info *txr;
11637 
11638 	if (bp->tx_ring) {
11639 		for (i = 0; i < bp->tx_nr_rings; i++) {
11640 			txr = &bp->tx_ring[i];
11641 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11642 		}
11643 	}
11644 	/* Make sure napi polls see @dev_state change */
11645 	synchronize_net();
11646 	/* Drop carrier first to prevent TX timeout */
11647 	netif_carrier_off(bp->dev);
11648 	/* Stop all TX queues */
11649 	netif_tx_disable(bp->dev);
11650 }
11651 
11652 void bnxt_tx_enable(struct bnxt *bp)
11653 {
11654 	int i;
11655 	struct bnxt_tx_ring_info *txr;
11656 
11657 	for (i = 0; i < bp->tx_nr_rings; i++) {
11658 		txr = &bp->tx_ring[i];
11659 		WRITE_ONCE(txr->dev_state, 0);
11660 	}
11661 	/* Make sure napi polls see @dev_state change */
11662 	synchronize_net();
11663 	netif_tx_wake_all_queues(bp->dev);
11664 	if (BNXT_LINK_IS_UP(bp))
11665 		netif_carrier_on(bp->dev);
11666 }
11667 
11668 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11669 {
11670 	u8 active_fec = link_info->active_fec_sig_mode &
11671 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11672 
11673 	switch (active_fec) {
11674 	default:
11675 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11676 		return "None";
11677 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11678 		return "Clause 74 BaseR";
11679 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11680 		return "Clause 91 RS(528,514)";
11681 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11682 		return "Clause 91 RS544_1XN";
11683 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11684 		return "Clause 91 RS(544,514)";
11685 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11686 		return "Clause 91 RS272_1XN";
11687 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11688 		return "Clause 91 RS(272,257)";
11689 	}
11690 }
11691 
11692 void bnxt_report_link(struct bnxt *bp)
11693 {
11694 	if (BNXT_LINK_IS_UP(bp)) {
11695 		const char *signal = "";
11696 		const char *flow_ctrl;
11697 		const char *duplex;
11698 		u32 speed;
11699 		u16 fec;
11700 
11701 		netif_carrier_on(bp->dev);
11702 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11703 		if (speed == SPEED_UNKNOWN) {
11704 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11705 			return;
11706 		}
11707 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11708 			duplex = "full";
11709 		else
11710 			duplex = "half";
11711 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11712 			flow_ctrl = "ON - receive & transmit";
11713 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11714 			flow_ctrl = "ON - transmit";
11715 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11716 			flow_ctrl = "ON - receive";
11717 		else
11718 			flow_ctrl = "none";
11719 		if (bp->link_info.phy_qcfg_resp.option_flags &
11720 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11721 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11722 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11723 			switch (sig_mode) {
11724 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11725 				signal = "(NRZ) ";
11726 				break;
11727 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11728 				signal = "(PAM4 56Gbps) ";
11729 				break;
11730 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11731 				signal = "(PAM4 112Gbps) ";
11732 				break;
11733 			default:
11734 				break;
11735 			}
11736 		}
11737 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11738 			    speed, signal, duplex, flow_ctrl);
11739 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11740 			netdev_info(bp->dev, "EEE is %s\n",
11741 				    bp->eee.eee_active ? "active" :
11742 							 "not active");
11743 		fec = bp->link_info.fec_cfg;
11744 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11745 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11746 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11747 				    bnxt_report_fec(&bp->link_info));
11748 	} else {
11749 		netif_carrier_off(bp->dev);
11750 		netdev_err(bp->dev, "NIC Link is Down\n");
11751 	}
11752 }
11753 
11754 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11755 {
11756 	if (!resp->supported_speeds_auto_mode &&
11757 	    !resp->supported_speeds_force_mode &&
11758 	    !resp->supported_pam4_speeds_auto_mode &&
11759 	    !resp->supported_pam4_speeds_force_mode &&
11760 	    !resp->supported_speeds2_auto_mode &&
11761 	    !resp->supported_speeds2_force_mode)
11762 		return true;
11763 	return false;
11764 }
11765 
11766 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11767 {
11768 	struct bnxt_link_info *link_info = &bp->link_info;
11769 	struct hwrm_port_phy_qcaps_output *resp;
11770 	struct hwrm_port_phy_qcaps_input *req;
11771 	int rc = 0;
11772 
11773 	if (bp->hwrm_spec_code < 0x10201)
11774 		return 0;
11775 
11776 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11777 	if (rc)
11778 		return rc;
11779 
11780 	resp = hwrm_req_hold(bp, req);
11781 	rc = hwrm_req_send(bp, req);
11782 	if (rc)
11783 		goto hwrm_phy_qcaps_exit;
11784 
11785 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11786 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11787 		struct ethtool_keee *eee = &bp->eee;
11788 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11789 
11790 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11791 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11792 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11793 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11794 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11795 	}
11796 
11797 	if (bp->hwrm_spec_code >= 0x10a01) {
11798 		if (bnxt_phy_qcaps_no_speed(resp)) {
11799 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11800 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11801 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11802 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11803 			netdev_info(bp->dev, "Ethernet link enabled\n");
11804 			/* Phy re-enabled, reprobe the speeds */
11805 			link_info->support_auto_speeds = 0;
11806 			link_info->support_pam4_auto_speeds = 0;
11807 			link_info->support_auto_speeds2 = 0;
11808 		}
11809 	}
11810 	if (resp->supported_speeds_auto_mode)
11811 		link_info->support_auto_speeds =
11812 			le16_to_cpu(resp->supported_speeds_auto_mode);
11813 	if (resp->supported_pam4_speeds_auto_mode)
11814 		link_info->support_pam4_auto_speeds =
11815 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11816 	if (resp->supported_speeds2_auto_mode)
11817 		link_info->support_auto_speeds2 =
11818 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11819 
11820 	bp->port_count = resp->port_cnt;
11821 
11822 hwrm_phy_qcaps_exit:
11823 	hwrm_req_drop(bp, req);
11824 	return rc;
11825 }
11826 
11827 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
11828 {
11829 	struct hwrm_port_mac_qcaps_output *resp;
11830 	struct hwrm_port_mac_qcaps_input *req;
11831 	int rc;
11832 
11833 	if (bp->hwrm_spec_code < 0x10a03)
11834 		return;
11835 
11836 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
11837 	if (rc)
11838 		return;
11839 
11840 	resp = hwrm_req_hold(bp, req);
11841 	rc = hwrm_req_send_silent(bp, req);
11842 	if (!rc)
11843 		bp->mac_flags = resp->flags;
11844 	hwrm_req_drop(bp, req);
11845 }
11846 
11847 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11848 {
11849 	u16 diff = advertising ^ supported;
11850 
11851 	return ((supported | diff) != supported);
11852 }
11853 
11854 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11855 {
11856 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11857 
11858 	/* Check if any advertised speeds are no longer supported. The caller
11859 	 * holds the link_lock mutex, so we can modify link_info settings.
11860 	 */
11861 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11862 		if (bnxt_support_dropped(link_info->advertising,
11863 					 link_info->support_auto_speeds2)) {
11864 			link_info->advertising = link_info->support_auto_speeds2;
11865 			return true;
11866 		}
11867 		return false;
11868 	}
11869 	if (bnxt_support_dropped(link_info->advertising,
11870 				 link_info->support_auto_speeds)) {
11871 		link_info->advertising = link_info->support_auto_speeds;
11872 		return true;
11873 	}
11874 	if (bnxt_support_dropped(link_info->advertising_pam4,
11875 				 link_info->support_pam4_auto_speeds)) {
11876 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
11877 		return true;
11878 	}
11879 	return false;
11880 }
11881 
11882 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
11883 {
11884 	struct bnxt_link_info *link_info = &bp->link_info;
11885 	struct hwrm_port_phy_qcfg_output *resp;
11886 	struct hwrm_port_phy_qcfg_input *req;
11887 	u8 link_state = link_info->link_state;
11888 	bool support_changed;
11889 	int rc;
11890 
11891 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
11892 	if (rc)
11893 		return rc;
11894 
11895 	resp = hwrm_req_hold(bp, req);
11896 	rc = hwrm_req_send(bp, req);
11897 	if (rc) {
11898 		hwrm_req_drop(bp, req);
11899 		if (BNXT_VF(bp) && rc == -ENODEV) {
11900 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
11901 			rc = 0;
11902 		}
11903 		return rc;
11904 	}
11905 
11906 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
11907 	link_info->phy_link_status = resp->link;
11908 	link_info->duplex = resp->duplex_cfg;
11909 	if (bp->hwrm_spec_code >= 0x10800)
11910 		link_info->duplex = resp->duplex_state;
11911 	link_info->pause = resp->pause;
11912 	link_info->auto_mode = resp->auto_mode;
11913 	link_info->auto_pause_setting = resp->auto_pause;
11914 	link_info->lp_pause = resp->link_partner_adv_pause;
11915 	link_info->force_pause_setting = resp->force_pause;
11916 	link_info->duplex_setting = resp->duplex_cfg;
11917 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
11918 		link_info->link_speed = le16_to_cpu(resp->link_speed);
11919 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
11920 			link_info->active_lanes = resp->active_lanes;
11921 	} else {
11922 		link_info->link_speed = 0;
11923 		link_info->active_lanes = 0;
11924 	}
11925 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
11926 	link_info->force_pam4_link_speed =
11927 		le16_to_cpu(resp->force_pam4_link_speed);
11928 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
11929 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
11930 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
11931 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
11932 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
11933 	link_info->auto_pam4_link_speeds =
11934 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
11935 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
11936 	link_info->lp_auto_link_speeds =
11937 		le16_to_cpu(resp->link_partner_adv_speeds);
11938 	link_info->lp_auto_pam4_link_speeds =
11939 		resp->link_partner_pam4_adv_speeds;
11940 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
11941 	link_info->phy_ver[0] = resp->phy_maj;
11942 	link_info->phy_ver[1] = resp->phy_min;
11943 	link_info->phy_ver[2] = resp->phy_bld;
11944 	link_info->media_type = resp->media_type;
11945 	link_info->phy_type = resp->phy_type;
11946 	link_info->transceiver = resp->xcvr_pkg_type;
11947 	link_info->phy_addr = resp->eee_config_phy_addr &
11948 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
11949 	link_info->module_status = resp->module_status;
11950 
11951 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
11952 		struct ethtool_keee *eee = &bp->eee;
11953 		u16 fw_speeds;
11954 
11955 		eee->eee_active = 0;
11956 		if (resp->eee_config_phy_addr &
11957 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
11958 			eee->eee_active = 1;
11959 			fw_speeds = le16_to_cpu(
11960 				resp->link_partner_adv_eee_link_speed_mask);
11961 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
11962 		}
11963 
11964 		/* Pull initial EEE config */
11965 		if (!chng_link_state) {
11966 			if (resp->eee_config_phy_addr &
11967 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
11968 				eee->eee_enabled = 1;
11969 
11970 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
11971 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
11972 
11973 			if (resp->eee_config_phy_addr &
11974 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
11975 				__le32 tmr;
11976 
11977 				eee->tx_lpi_enabled = 1;
11978 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
11979 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
11980 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
11981 			}
11982 		}
11983 	}
11984 
11985 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
11986 	if (bp->hwrm_spec_code >= 0x10504) {
11987 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
11988 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
11989 	}
11990 	/* TODO: need to add more logic to report VF link */
11991 	if (chng_link_state) {
11992 		if (link_info->phy_link_status == BNXT_LINK_LINK)
11993 			link_info->link_state = BNXT_LINK_STATE_UP;
11994 		else
11995 			link_info->link_state = BNXT_LINK_STATE_DOWN;
11996 		if (link_state != link_info->link_state)
11997 			bnxt_report_link(bp);
11998 	} else {
11999 		/* always link down if not require to update link state */
12000 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12001 	}
12002 	hwrm_req_drop(bp, req);
12003 
12004 	if (!BNXT_PHY_CFG_ABLE(bp))
12005 		return 0;
12006 
12007 	support_changed = bnxt_support_speed_dropped(link_info);
12008 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12009 		bnxt_hwrm_set_link_setting(bp, true, false);
12010 	return 0;
12011 }
12012 
12013 static void bnxt_get_port_module_status(struct bnxt *bp)
12014 {
12015 	struct bnxt_link_info *link_info = &bp->link_info;
12016 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12017 	u8 module_status;
12018 
12019 	if (bnxt_update_link(bp, true))
12020 		return;
12021 
12022 	module_status = link_info->module_status;
12023 	switch (module_status) {
12024 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12025 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12026 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12027 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12028 			    bp->pf.port_id);
12029 		if (bp->hwrm_spec_code >= 0x10201) {
12030 			netdev_warn(bp->dev, "Module part number %s\n",
12031 				    resp->phy_vendor_partnumber);
12032 		}
12033 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12034 			netdev_warn(bp->dev, "TX is disabled\n");
12035 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12036 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12037 	}
12038 }
12039 
12040 static void
12041 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12042 {
12043 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12044 		if (bp->hwrm_spec_code >= 0x10201)
12045 			req->auto_pause =
12046 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12047 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12048 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12049 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12050 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12051 		req->enables |=
12052 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12053 	} else {
12054 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12055 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12056 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12057 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12058 		req->enables |=
12059 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12060 		if (bp->hwrm_spec_code >= 0x10201) {
12061 			req->auto_pause = req->force_pause;
12062 			req->enables |= cpu_to_le32(
12063 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12064 		}
12065 	}
12066 }
12067 
12068 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12069 {
12070 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12071 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12072 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12073 			req->enables |=
12074 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12075 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12076 		} else if (bp->link_info.advertising) {
12077 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12078 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12079 		}
12080 		if (bp->link_info.advertising_pam4) {
12081 			req->enables |=
12082 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12083 			req->auto_link_pam4_speed_mask =
12084 				cpu_to_le16(bp->link_info.advertising_pam4);
12085 		}
12086 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12087 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12088 	} else {
12089 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12090 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12091 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12092 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12093 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12094 				   (u32)bp->link_info.req_link_speed);
12095 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12096 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12097 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12098 		} else {
12099 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12100 		}
12101 	}
12102 
12103 	/* tell chimp that the setting takes effect immediately */
12104 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12105 }
12106 
12107 int bnxt_hwrm_set_pause(struct bnxt *bp)
12108 {
12109 	struct hwrm_port_phy_cfg_input *req;
12110 	int rc;
12111 
12112 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12113 	if (rc)
12114 		return rc;
12115 
12116 	bnxt_hwrm_set_pause_common(bp, req);
12117 
12118 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12119 	    bp->link_info.force_link_chng)
12120 		bnxt_hwrm_set_link_common(bp, req);
12121 
12122 	rc = hwrm_req_send(bp, req);
12123 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12124 		/* since changing of pause setting doesn't trigger any link
12125 		 * change event, the driver needs to update the current pause
12126 		 * result upon successfully return of the phy_cfg command
12127 		 */
12128 		bp->link_info.pause =
12129 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12130 		bp->link_info.auto_pause_setting = 0;
12131 		if (!bp->link_info.force_link_chng)
12132 			bnxt_report_link(bp);
12133 	}
12134 	bp->link_info.force_link_chng = false;
12135 	return rc;
12136 }
12137 
12138 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12139 			      struct hwrm_port_phy_cfg_input *req)
12140 {
12141 	struct ethtool_keee *eee = &bp->eee;
12142 
12143 	if (eee->eee_enabled) {
12144 		u16 eee_speeds;
12145 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12146 
12147 		if (eee->tx_lpi_enabled)
12148 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12149 		else
12150 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12151 
12152 		req->flags |= cpu_to_le32(flags);
12153 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12154 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12155 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12156 	} else {
12157 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12158 	}
12159 }
12160 
12161 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12162 {
12163 	struct hwrm_port_phy_cfg_input *req;
12164 	int rc;
12165 
12166 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12167 	if (rc)
12168 		return rc;
12169 
12170 	if (set_pause)
12171 		bnxt_hwrm_set_pause_common(bp, req);
12172 
12173 	bnxt_hwrm_set_link_common(bp, req);
12174 
12175 	if (set_eee)
12176 		bnxt_hwrm_set_eee(bp, req);
12177 	return hwrm_req_send(bp, req);
12178 }
12179 
12180 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12181 {
12182 	struct hwrm_port_phy_cfg_input *req;
12183 	int rc;
12184 
12185 	if (!BNXT_SINGLE_PF(bp))
12186 		return 0;
12187 
12188 	if (pci_num_vf(bp->pdev) &&
12189 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12190 		return 0;
12191 
12192 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12193 	if (rc)
12194 		return rc;
12195 
12196 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12197 	rc = hwrm_req_send(bp, req);
12198 	if (!rc) {
12199 		mutex_lock(&bp->link_lock);
12200 		/* Device is not obliged link down in certain scenarios, even
12201 		 * when forced. Setting the state unknown is consistent with
12202 		 * driver startup and will force link state to be reported
12203 		 * during subsequent open based on PORT_PHY_QCFG.
12204 		 */
12205 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12206 		mutex_unlock(&bp->link_lock);
12207 	}
12208 	return rc;
12209 }
12210 
12211 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12212 {
12213 #ifdef CONFIG_TEE_BNXT_FW
12214 	int rc = tee_bnxt_fw_load();
12215 
12216 	if (rc)
12217 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12218 
12219 	return rc;
12220 #else
12221 	netdev_err(bp->dev, "OP-TEE not supported\n");
12222 	return -ENODEV;
12223 #endif
12224 }
12225 
12226 static int bnxt_try_recover_fw(struct bnxt *bp)
12227 {
12228 	if (bp->fw_health && bp->fw_health->status_reliable) {
12229 		int retry = 0, rc;
12230 		u32 sts;
12231 
12232 		do {
12233 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12234 			rc = bnxt_hwrm_poll(bp);
12235 			if (!BNXT_FW_IS_BOOTING(sts) &&
12236 			    !BNXT_FW_IS_RECOVERING(sts))
12237 				break;
12238 			retry++;
12239 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12240 
12241 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12242 			netdev_err(bp->dev,
12243 				   "Firmware not responding, status: 0x%x\n",
12244 				   sts);
12245 			rc = -ENODEV;
12246 		}
12247 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12248 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12249 			return bnxt_fw_reset_via_optee(bp);
12250 		}
12251 		return rc;
12252 	}
12253 
12254 	return -ENODEV;
12255 }
12256 
12257 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12258 {
12259 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12260 
12261 	if (!BNXT_NEW_RM(bp))
12262 		return; /* no resource reservations required */
12263 
12264 	hw_resc->resv_cp_rings = 0;
12265 	hw_resc->resv_stat_ctxs = 0;
12266 	hw_resc->resv_irqs = 0;
12267 	hw_resc->resv_tx_rings = 0;
12268 	hw_resc->resv_rx_rings = 0;
12269 	hw_resc->resv_hw_ring_grps = 0;
12270 	hw_resc->resv_vnics = 0;
12271 	hw_resc->resv_rsscos_ctxs = 0;
12272 	if (!fw_reset) {
12273 		bp->tx_nr_rings = 0;
12274 		bp->rx_nr_rings = 0;
12275 	}
12276 }
12277 
12278 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12279 {
12280 	int rc;
12281 
12282 	if (!BNXT_NEW_RM(bp))
12283 		return 0; /* no resource reservations required */
12284 
12285 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12286 	if (rc)
12287 		netdev_err(bp->dev, "resc_qcaps failed\n");
12288 
12289 	bnxt_clear_reservations(bp, fw_reset);
12290 
12291 	return rc;
12292 }
12293 
12294 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12295 {
12296 	struct hwrm_func_drv_if_change_output *resp;
12297 	struct hwrm_func_drv_if_change_input *req;
12298 	bool fw_reset = !bp->irq_tbl;
12299 	bool resc_reinit = false;
12300 	int rc, retry = 0;
12301 	u32 flags = 0;
12302 
12303 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12304 		return 0;
12305 
12306 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12307 	if (rc)
12308 		return rc;
12309 
12310 	if (up)
12311 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12312 	resp = hwrm_req_hold(bp, req);
12313 
12314 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12315 	while (retry < BNXT_FW_IF_RETRY) {
12316 		rc = hwrm_req_send(bp, req);
12317 		if (rc != -EAGAIN)
12318 			break;
12319 
12320 		msleep(50);
12321 		retry++;
12322 	}
12323 
12324 	if (rc == -EAGAIN) {
12325 		hwrm_req_drop(bp, req);
12326 		return rc;
12327 	} else if (!rc) {
12328 		flags = le32_to_cpu(resp->flags);
12329 	} else if (up) {
12330 		rc = bnxt_try_recover_fw(bp);
12331 		fw_reset = true;
12332 	}
12333 	hwrm_req_drop(bp, req);
12334 	if (rc)
12335 		return rc;
12336 
12337 	if (!up) {
12338 		bnxt_inv_fw_health_reg(bp);
12339 		return 0;
12340 	}
12341 
12342 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12343 		resc_reinit = true;
12344 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12345 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12346 		fw_reset = true;
12347 	else
12348 		bnxt_remap_fw_health_regs(bp);
12349 
12350 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12351 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12352 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12353 		return -ENODEV;
12354 	}
12355 	if (resc_reinit || fw_reset) {
12356 		if (fw_reset) {
12357 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12358 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12359 				bnxt_ulp_irq_stop(bp);
12360 			bnxt_free_ctx_mem(bp, false);
12361 			bnxt_dcb_free(bp);
12362 			rc = bnxt_fw_init_one(bp);
12363 			if (rc) {
12364 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12365 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12366 				return rc;
12367 			}
12368 			bnxt_clear_int_mode(bp);
12369 			rc = bnxt_init_int_mode(bp);
12370 			if (rc) {
12371 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12372 				netdev_err(bp->dev, "init int mode failed\n");
12373 				return rc;
12374 			}
12375 		}
12376 		rc = bnxt_cancel_reservations(bp, fw_reset);
12377 	}
12378 	return rc;
12379 }
12380 
12381 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12382 {
12383 	struct hwrm_port_led_qcaps_output *resp;
12384 	struct hwrm_port_led_qcaps_input *req;
12385 	struct bnxt_pf_info *pf = &bp->pf;
12386 	int rc;
12387 
12388 	bp->num_leds = 0;
12389 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12390 		return 0;
12391 
12392 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12393 	if (rc)
12394 		return rc;
12395 
12396 	req->port_id = cpu_to_le16(pf->port_id);
12397 	resp = hwrm_req_hold(bp, req);
12398 	rc = hwrm_req_send(bp, req);
12399 	if (rc) {
12400 		hwrm_req_drop(bp, req);
12401 		return rc;
12402 	}
12403 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12404 		int i;
12405 
12406 		bp->num_leds = resp->num_leds;
12407 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12408 						 bp->num_leds);
12409 		for (i = 0; i < bp->num_leds; i++) {
12410 			struct bnxt_led_info *led = &bp->leds[i];
12411 			__le16 caps = led->led_state_caps;
12412 
12413 			if (!led->led_group_id ||
12414 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12415 				bp->num_leds = 0;
12416 				break;
12417 			}
12418 		}
12419 	}
12420 	hwrm_req_drop(bp, req);
12421 	return 0;
12422 }
12423 
12424 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12425 {
12426 	struct hwrm_wol_filter_alloc_output *resp;
12427 	struct hwrm_wol_filter_alloc_input *req;
12428 	int rc;
12429 
12430 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12431 	if (rc)
12432 		return rc;
12433 
12434 	req->port_id = cpu_to_le16(bp->pf.port_id);
12435 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12436 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12437 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12438 
12439 	resp = hwrm_req_hold(bp, req);
12440 	rc = hwrm_req_send(bp, req);
12441 	if (!rc)
12442 		bp->wol_filter_id = resp->wol_filter_id;
12443 	hwrm_req_drop(bp, req);
12444 	return rc;
12445 }
12446 
12447 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12448 {
12449 	struct hwrm_wol_filter_free_input *req;
12450 	int rc;
12451 
12452 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12453 	if (rc)
12454 		return rc;
12455 
12456 	req->port_id = cpu_to_le16(bp->pf.port_id);
12457 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12458 	req->wol_filter_id = bp->wol_filter_id;
12459 
12460 	return hwrm_req_send(bp, req);
12461 }
12462 
12463 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12464 {
12465 	struct hwrm_wol_filter_qcfg_output *resp;
12466 	struct hwrm_wol_filter_qcfg_input *req;
12467 	u16 next_handle = 0;
12468 	int rc;
12469 
12470 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12471 	if (rc)
12472 		return rc;
12473 
12474 	req->port_id = cpu_to_le16(bp->pf.port_id);
12475 	req->handle = cpu_to_le16(handle);
12476 	resp = hwrm_req_hold(bp, req);
12477 	rc = hwrm_req_send(bp, req);
12478 	if (!rc) {
12479 		next_handle = le16_to_cpu(resp->next_handle);
12480 		if (next_handle != 0) {
12481 			if (resp->wol_type ==
12482 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12483 				bp->wol = 1;
12484 				bp->wol_filter_id = resp->wol_filter_id;
12485 			}
12486 		}
12487 	}
12488 	hwrm_req_drop(bp, req);
12489 	return next_handle;
12490 }
12491 
12492 static void bnxt_get_wol_settings(struct bnxt *bp)
12493 {
12494 	u16 handle = 0;
12495 
12496 	bp->wol = 0;
12497 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12498 		return;
12499 
12500 	do {
12501 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12502 	} while (handle && handle != 0xffff);
12503 }
12504 
12505 static bool bnxt_eee_config_ok(struct bnxt *bp)
12506 {
12507 	struct ethtool_keee *eee = &bp->eee;
12508 	struct bnxt_link_info *link_info = &bp->link_info;
12509 
12510 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12511 		return true;
12512 
12513 	if (eee->eee_enabled) {
12514 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12515 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12516 
12517 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12518 
12519 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12520 			eee->eee_enabled = 0;
12521 			return false;
12522 		}
12523 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12524 			linkmode_and(eee->advertised, advertising,
12525 				     eee->supported);
12526 			return false;
12527 		}
12528 	}
12529 	return true;
12530 }
12531 
12532 static int bnxt_update_phy_setting(struct bnxt *bp)
12533 {
12534 	int rc;
12535 	bool update_link = false;
12536 	bool update_pause = false;
12537 	bool update_eee = false;
12538 	struct bnxt_link_info *link_info = &bp->link_info;
12539 
12540 	rc = bnxt_update_link(bp, true);
12541 	if (rc) {
12542 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12543 			   rc);
12544 		return rc;
12545 	}
12546 	if (!BNXT_SINGLE_PF(bp))
12547 		return 0;
12548 
12549 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12550 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12551 	    link_info->req_flow_ctrl)
12552 		update_pause = true;
12553 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12554 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12555 		update_pause = true;
12556 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12557 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12558 			update_link = true;
12559 		if (bnxt_force_speed_updated(link_info))
12560 			update_link = true;
12561 		if (link_info->req_duplex != link_info->duplex_setting)
12562 			update_link = true;
12563 	} else {
12564 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12565 			update_link = true;
12566 		if (bnxt_auto_speed_updated(link_info))
12567 			update_link = true;
12568 	}
12569 
12570 	/* The last close may have shutdown the link, so need to call
12571 	 * PHY_CFG to bring it back up.
12572 	 */
12573 	if (!BNXT_LINK_IS_UP(bp))
12574 		update_link = true;
12575 
12576 	if (!bnxt_eee_config_ok(bp))
12577 		update_eee = true;
12578 
12579 	if (update_link)
12580 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12581 	else if (update_pause)
12582 		rc = bnxt_hwrm_set_pause(bp);
12583 	if (rc) {
12584 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12585 			   rc);
12586 		return rc;
12587 	}
12588 
12589 	return rc;
12590 }
12591 
12592 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12593 
12594 static int bnxt_reinit_after_abort(struct bnxt *bp)
12595 {
12596 	int rc;
12597 
12598 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12599 		return -EBUSY;
12600 
12601 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12602 		return -ENODEV;
12603 
12604 	rc = bnxt_fw_init_one(bp);
12605 	if (!rc) {
12606 		bnxt_clear_int_mode(bp);
12607 		rc = bnxt_init_int_mode(bp);
12608 		if (!rc) {
12609 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12610 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12611 		}
12612 	}
12613 	return rc;
12614 }
12615 
12616 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12617 {
12618 	struct bnxt_ntuple_filter *ntp_fltr;
12619 	struct bnxt_l2_filter *l2_fltr;
12620 
12621 	if (list_empty(&fltr->list))
12622 		return;
12623 
12624 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12625 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12626 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12627 		atomic_inc(&l2_fltr->refcnt);
12628 		ntp_fltr->l2_fltr = l2_fltr;
12629 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12630 			bnxt_del_ntp_filter(bp, ntp_fltr);
12631 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12632 				   fltr->sw_id);
12633 		}
12634 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12635 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12636 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12637 			bnxt_del_l2_filter(bp, l2_fltr);
12638 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12639 				   fltr->sw_id);
12640 		}
12641 	}
12642 }
12643 
12644 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12645 {
12646 	struct bnxt_filter_base *usr_fltr, *tmp;
12647 
12648 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12649 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12650 }
12651 
12652 static int bnxt_set_xps_mapping(struct bnxt *bp)
12653 {
12654 	int numa_node = dev_to_node(&bp->pdev->dev);
12655 	unsigned int q_idx, map_idx, cpu, i;
12656 	const struct cpumask *cpu_mask_ptr;
12657 	int nr_cpus = num_online_cpus();
12658 	cpumask_t *q_map;
12659 	int rc = 0;
12660 
12661 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12662 	if (!q_map)
12663 		return -ENOMEM;
12664 
12665 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12666 	 * Each TC has the same number of TX queues. The nth TX queue for each
12667 	 * TC will have the same CPU mask.
12668 	 */
12669 	for (i = 0; i < nr_cpus; i++) {
12670 		map_idx = i % bp->tx_nr_rings_per_tc;
12671 		cpu = cpumask_local_spread(i, numa_node);
12672 		cpu_mask_ptr = get_cpu_mask(cpu);
12673 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12674 	}
12675 
12676 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12677 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12678 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12679 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12680 		if (rc) {
12681 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12682 				    q_idx);
12683 			break;
12684 		}
12685 	}
12686 
12687 	kfree(q_map);
12688 
12689 	return rc;
12690 }
12691 
12692 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12693 {
12694 	int rc = 0;
12695 
12696 	netif_carrier_off(bp->dev);
12697 	if (irq_re_init) {
12698 		/* Reserve rings now if none were reserved at driver probe. */
12699 		rc = bnxt_init_dflt_ring_mode(bp);
12700 		if (rc) {
12701 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12702 			return rc;
12703 		}
12704 	}
12705 	rc = bnxt_reserve_rings(bp, irq_re_init);
12706 	if (rc)
12707 		return rc;
12708 
12709 	rc = bnxt_alloc_mem(bp, irq_re_init);
12710 	if (rc) {
12711 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12712 		goto open_err_free_mem;
12713 	}
12714 
12715 	if (irq_re_init) {
12716 		bnxt_init_napi(bp);
12717 		rc = bnxt_request_irq(bp);
12718 		if (rc) {
12719 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12720 			goto open_err_irq;
12721 		}
12722 	}
12723 
12724 	rc = bnxt_init_nic(bp, irq_re_init);
12725 	if (rc) {
12726 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12727 		goto open_err_irq;
12728 	}
12729 
12730 	bnxt_enable_napi(bp);
12731 	bnxt_debug_dev_init(bp);
12732 
12733 	if (link_re_init) {
12734 		mutex_lock(&bp->link_lock);
12735 		rc = bnxt_update_phy_setting(bp);
12736 		mutex_unlock(&bp->link_lock);
12737 		if (rc) {
12738 			netdev_warn(bp->dev, "failed to update phy settings\n");
12739 			if (BNXT_SINGLE_PF(bp)) {
12740 				bp->link_info.phy_retry = true;
12741 				bp->link_info.phy_retry_expires =
12742 					jiffies + 5 * HZ;
12743 			}
12744 		}
12745 	}
12746 
12747 	if (irq_re_init) {
12748 		udp_tunnel_nic_reset_ntf(bp->dev);
12749 		rc = bnxt_set_xps_mapping(bp);
12750 		if (rc)
12751 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12752 	}
12753 
12754 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12755 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12756 			static_branch_enable(&bnxt_xdp_locking_key);
12757 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12758 		static_branch_disable(&bnxt_xdp_locking_key);
12759 	}
12760 	set_bit(BNXT_STATE_OPEN, &bp->state);
12761 	bnxt_enable_int(bp);
12762 	/* Enable TX queues */
12763 	bnxt_tx_enable(bp);
12764 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12765 	/* Poll link status and check for SFP+ module status */
12766 	mutex_lock(&bp->link_lock);
12767 	bnxt_get_port_module_status(bp);
12768 	mutex_unlock(&bp->link_lock);
12769 
12770 	/* VF-reps may need to be re-opened after the PF is re-opened */
12771 	if (BNXT_PF(bp))
12772 		bnxt_vf_reps_open(bp);
12773 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
12774 		WRITE_ONCE(bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS);
12775 	bnxt_ptp_init_rtc(bp, true);
12776 	bnxt_ptp_cfg_tstamp_filters(bp);
12777 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12778 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12779 	bnxt_cfg_usr_fltrs(bp);
12780 	return 0;
12781 
12782 open_err_irq:
12783 	bnxt_del_napi(bp);
12784 
12785 open_err_free_mem:
12786 	bnxt_free_skbs(bp);
12787 	bnxt_free_irq(bp);
12788 	bnxt_free_mem(bp, true);
12789 	return rc;
12790 }
12791 
12792 /* rtnl_lock held */
12793 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12794 {
12795 	int rc = 0;
12796 
12797 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12798 		rc = -EIO;
12799 	if (!rc)
12800 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12801 	if (rc) {
12802 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12803 		dev_close(bp->dev);
12804 	}
12805 	return rc;
12806 }
12807 
12808 /* rtnl_lock held, open the NIC half way by allocating all resources, but
12809  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
12810  * self tests.
12811  */
12812 int bnxt_half_open_nic(struct bnxt *bp)
12813 {
12814 	int rc = 0;
12815 
12816 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12817 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12818 		rc = -ENODEV;
12819 		goto half_open_err;
12820 	}
12821 
12822 	rc = bnxt_alloc_mem(bp, true);
12823 	if (rc) {
12824 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12825 		goto half_open_err;
12826 	}
12827 	bnxt_init_napi(bp);
12828 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12829 	rc = bnxt_init_nic(bp, true);
12830 	if (rc) {
12831 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12832 		bnxt_del_napi(bp);
12833 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12834 		goto half_open_err;
12835 	}
12836 	return 0;
12837 
12838 half_open_err:
12839 	bnxt_free_skbs(bp);
12840 	bnxt_free_mem(bp, true);
12841 	dev_close(bp->dev);
12842 	return rc;
12843 }
12844 
12845 /* rtnl_lock held, this call can only be made after a previous successful
12846  * call to bnxt_half_open_nic().
12847  */
12848 void bnxt_half_close_nic(struct bnxt *bp)
12849 {
12850 	bnxt_hwrm_resource_free(bp, false, true);
12851 	bnxt_del_napi(bp);
12852 	bnxt_free_skbs(bp);
12853 	bnxt_free_mem(bp, true);
12854 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12855 }
12856 
12857 void bnxt_reenable_sriov(struct bnxt *bp)
12858 {
12859 	if (BNXT_PF(bp)) {
12860 		struct bnxt_pf_info *pf = &bp->pf;
12861 		int n = pf->active_vfs;
12862 
12863 		if (n)
12864 			bnxt_cfg_hw_sriov(bp, &n, true);
12865 	}
12866 }
12867 
12868 static int bnxt_open(struct net_device *dev)
12869 {
12870 	struct bnxt *bp = netdev_priv(dev);
12871 	int rc;
12872 
12873 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12874 		rc = bnxt_reinit_after_abort(bp);
12875 		if (rc) {
12876 			if (rc == -EBUSY)
12877 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
12878 			else
12879 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
12880 			return -ENODEV;
12881 		}
12882 	}
12883 
12884 	rc = bnxt_hwrm_if_change(bp, true);
12885 	if (rc)
12886 		return rc;
12887 
12888 	rc = __bnxt_open_nic(bp, true, true);
12889 	if (rc) {
12890 		bnxt_hwrm_if_change(bp, false);
12891 	} else {
12892 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
12893 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12894 				bnxt_queue_sp_work(bp,
12895 						   BNXT_RESTART_ULP_SP_EVENT);
12896 		}
12897 	}
12898 
12899 	return rc;
12900 }
12901 
12902 static bool bnxt_drv_busy(struct bnxt *bp)
12903 {
12904 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
12905 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
12906 }
12907 
12908 static void bnxt_get_ring_stats(struct bnxt *bp,
12909 				struct rtnl_link_stats64 *stats);
12910 
12911 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
12912 			     bool link_re_init)
12913 {
12914 	/* Close the VF-reps before closing PF */
12915 	if (BNXT_PF(bp))
12916 		bnxt_vf_reps_close(bp);
12917 
12918 	/* Change device state to avoid TX queue wake up's */
12919 	bnxt_tx_disable(bp);
12920 
12921 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12922 	smp_mb__after_atomic();
12923 	while (bnxt_drv_busy(bp))
12924 		msleep(20);
12925 
12926 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12927 		bnxt_clear_rss_ctxs(bp);
12928 	/* Flush rings and disable interrupts */
12929 	bnxt_shutdown_nic(bp, irq_re_init);
12930 
12931 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
12932 
12933 	bnxt_debug_dev_exit(bp);
12934 	bnxt_disable_napi(bp);
12935 	del_timer_sync(&bp->timer);
12936 	bnxt_free_skbs(bp);
12937 
12938 	/* Save ring stats before shutdown */
12939 	if (bp->bnapi && irq_re_init) {
12940 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
12941 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
12942 	}
12943 	if (irq_re_init) {
12944 		bnxt_free_irq(bp);
12945 		bnxt_del_napi(bp);
12946 	}
12947 	bnxt_free_mem(bp, irq_re_init);
12948 }
12949 
12950 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12951 {
12952 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12953 		/* If we get here, it means firmware reset is in progress
12954 		 * while we are trying to close.  We can safely proceed with
12955 		 * the close because we are holding rtnl_lock().  Some firmware
12956 		 * messages may fail as we proceed to close.  We set the
12957 		 * ABORT_ERR flag here so that the FW reset thread will later
12958 		 * abort when it gets the rtnl_lock() and sees the flag.
12959 		 */
12960 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
12961 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12962 	}
12963 
12964 #ifdef CONFIG_BNXT_SRIOV
12965 	if (bp->sriov_cfg) {
12966 		int rc;
12967 
12968 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
12969 						      !bp->sriov_cfg,
12970 						      BNXT_SRIOV_CFG_WAIT_TMO);
12971 		if (!rc)
12972 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
12973 		else if (rc < 0)
12974 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
12975 	}
12976 #endif
12977 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
12978 }
12979 
12980 static int bnxt_close(struct net_device *dev)
12981 {
12982 	struct bnxt *bp = netdev_priv(dev);
12983 
12984 	bnxt_close_nic(bp, true, true);
12985 	bnxt_hwrm_shutdown_link(bp);
12986 	bnxt_hwrm_if_change(bp, false);
12987 	return 0;
12988 }
12989 
12990 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
12991 				   u16 *val)
12992 {
12993 	struct hwrm_port_phy_mdio_read_output *resp;
12994 	struct hwrm_port_phy_mdio_read_input *req;
12995 	int rc;
12996 
12997 	if (bp->hwrm_spec_code < 0x10a00)
12998 		return -EOPNOTSUPP;
12999 
13000 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13001 	if (rc)
13002 		return rc;
13003 
13004 	req->port_id = cpu_to_le16(bp->pf.port_id);
13005 	req->phy_addr = phy_addr;
13006 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13007 	if (mdio_phy_id_is_c45(phy_addr)) {
13008 		req->cl45_mdio = 1;
13009 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13010 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13011 		req->reg_addr = cpu_to_le16(reg);
13012 	}
13013 
13014 	resp = hwrm_req_hold(bp, req);
13015 	rc = hwrm_req_send(bp, req);
13016 	if (!rc)
13017 		*val = le16_to_cpu(resp->reg_data);
13018 	hwrm_req_drop(bp, req);
13019 	return rc;
13020 }
13021 
13022 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13023 				    u16 val)
13024 {
13025 	struct hwrm_port_phy_mdio_write_input *req;
13026 	int rc;
13027 
13028 	if (bp->hwrm_spec_code < 0x10a00)
13029 		return -EOPNOTSUPP;
13030 
13031 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13032 	if (rc)
13033 		return rc;
13034 
13035 	req->port_id = cpu_to_le16(bp->pf.port_id);
13036 	req->phy_addr = phy_addr;
13037 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13038 	if (mdio_phy_id_is_c45(phy_addr)) {
13039 		req->cl45_mdio = 1;
13040 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13041 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13042 		req->reg_addr = cpu_to_le16(reg);
13043 	}
13044 	req->reg_data = cpu_to_le16(val);
13045 
13046 	return hwrm_req_send(bp, req);
13047 }
13048 
13049 /* rtnl_lock held */
13050 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13051 {
13052 	struct mii_ioctl_data *mdio = if_mii(ifr);
13053 	struct bnxt *bp = netdev_priv(dev);
13054 	int rc;
13055 
13056 	switch (cmd) {
13057 	case SIOCGMIIPHY:
13058 		mdio->phy_id = bp->link_info.phy_addr;
13059 
13060 		fallthrough;
13061 	case SIOCGMIIREG: {
13062 		u16 mii_regval = 0;
13063 
13064 		if (!netif_running(dev))
13065 			return -EAGAIN;
13066 
13067 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13068 					     &mii_regval);
13069 		mdio->val_out = mii_regval;
13070 		return rc;
13071 	}
13072 
13073 	case SIOCSMIIREG:
13074 		if (!netif_running(dev))
13075 			return -EAGAIN;
13076 
13077 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13078 						mdio->val_in);
13079 
13080 	case SIOCSHWTSTAMP:
13081 		return bnxt_hwtstamp_set(dev, ifr);
13082 
13083 	case SIOCGHWTSTAMP:
13084 		return bnxt_hwtstamp_get(dev, ifr);
13085 
13086 	default:
13087 		/* do nothing */
13088 		break;
13089 	}
13090 	return -EOPNOTSUPP;
13091 }
13092 
13093 static void bnxt_get_ring_stats(struct bnxt *bp,
13094 				struct rtnl_link_stats64 *stats)
13095 {
13096 	int i;
13097 
13098 	for (i = 0; i < bp->cp_nr_rings; i++) {
13099 		struct bnxt_napi *bnapi = bp->bnapi[i];
13100 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13101 		u64 *sw = cpr->stats.sw_stats;
13102 
13103 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13104 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13105 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13106 
13107 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13108 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13109 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13110 
13111 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13112 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13113 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13114 
13115 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13116 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13117 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13118 
13119 		stats->rx_missed_errors +=
13120 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13121 
13122 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13123 
13124 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13125 
13126 		stats->rx_dropped +=
13127 			cpr->sw_stats->rx.rx_netpoll_discards +
13128 			cpr->sw_stats->rx.rx_oom_discards;
13129 	}
13130 }
13131 
13132 static void bnxt_add_prev_stats(struct bnxt *bp,
13133 				struct rtnl_link_stats64 *stats)
13134 {
13135 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13136 
13137 	stats->rx_packets += prev_stats->rx_packets;
13138 	stats->tx_packets += prev_stats->tx_packets;
13139 	stats->rx_bytes += prev_stats->rx_bytes;
13140 	stats->tx_bytes += prev_stats->tx_bytes;
13141 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13142 	stats->multicast += prev_stats->multicast;
13143 	stats->rx_dropped += prev_stats->rx_dropped;
13144 	stats->tx_dropped += prev_stats->tx_dropped;
13145 }
13146 
13147 static void
13148 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13149 {
13150 	struct bnxt *bp = netdev_priv(dev);
13151 
13152 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13153 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13154 	 * we check the BNXT_STATE_OPEN flag.
13155 	 */
13156 	smp_mb__after_atomic();
13157 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13158 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13159 		*stats = bp->net_stats_prev;
13160 		return;
13161 	}
13162 
13163 	bnxt_get_ring_stats(bp, stats);
13164 	bnxt_add_prev_stats(bp, stats);
13165 
13166 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13167 		u64 *rx = bp->port_stats.sw_stats;
13168 		u64 *tx = bp->port_stats.sw_stats +
13169 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13170 
13171 		stats->rx_crc_errors =
13172 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13173 		stats->rx_frame_errors =
13174 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13175 		stats->rx_length_errors =
13176 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13177 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13178 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13179 		stats->rx_errors =
13180 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13181 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13182 		stats->collisions =
13183 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13184 		stats->tx_fifo_errors =
13185 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13186 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13187 	}
13188 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13189 }
13190 
13191 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
13192 					struct bnxt_total_ring_err_stats *stats,
13193 					struct bnxt_cp_ring_info *cpr)
13194 {
13195 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13196 	u64 *hw_stats = cpr->stats.sw_stats;
13197 
13198 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13199 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13200 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13201 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13202 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13203 	stats->rx_total_ring_discards +=
13204 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13205 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13206 	stats->tx_total_ring_discards +=
13207 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13208 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13209 }
13210 
13211 void bnxt_get_ring_err_stats(struct bnxt *bp,
13212 			     struct bnxt_total_ring_err_stats *stats)
13213 {
13214 	int i;
13215 
13216 	for (i = 0; i < bp->cp_nr_rings; i++)
13217 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13218 }
13219 
13220 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
13221 {
13222 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13223 	struct net_device *dev = bp->dev;
13224 	struct netdev_hw_addr *ha;
13225 	u8 *haddr;
13226 	int mc_count = 0;
13227 	bool update = false;
13228 	int off = 0;
13229 
13230 	netdev_for_each_mc_addr(ha, dev) {
13231 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13232 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13233 			vnic->mc_list_count = 0;
13234 			return false;
13235 		}
13236 		haddr = ha->addr;
13237 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13238 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13239 			update = true;
13240 		}
13241 		off += ETH_ALEN;
13242 		mc_count++;
13243 	}
13244 	if (mc_count)
13245 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13246 
13247 	if (mc_count != vnic->mc_list_count) {
13248 		vnic->mc_list_count = mc_count;
13249 		update = true;
13250 	}
13251 	return update;
13252 }
13253 
13254 static bool bnxt_uc_list_updated(struct bnxt *bp)
13255 {
13256 	struct net_device *dev = bp->dev;
13257 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13258 	struct netdev_hw_addr *ha;
13259 	int off = 0;
13260 
13261 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
13262 		return true;
13263 
13264 	netdev_for_each_uc_addr(ha, dev) {
13265 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13266 			return true;
13267 
13268 		off += ETH_ALEN;
13269 	}
13270 	return false;
13271 }
13272 
13273 static void bnxt_set_rx_mode(struct net_device *dev)
13274 {
13275 	struct bnxt *bp = netdev_priv(dev);
13276 	struct bnxt_vnic_info *vnic;
13277 	bool mc_update = false;
13278 	bool uc_update;
13279 	u32 mask;
13280 
13281 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13282 		return;
13283 
13284 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13285 	mask = vnic->rx_mask;
13286 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13287 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13288 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13289 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13290 
13291 	if (dev->flags & IFF_PROMISC)
13292 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13293 
13294 	uc_update = bnxt_uc_list_updated(bp);
13295 
13296 	if (dev->flags & IFF_BROADCAST)
13297 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13298 	if (dev->flags & IFF_ALLMULTI) {
13299 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13300 		vnic->mc_list_count = 0;
13301 	} else if (dev->flags & IFF_MULTICAST) {
13302 		mc_update = bnxt_mc_list_updated(bp, &mask);
13303 	}
13304 
13305 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13306 		vnic->rx_mask = mask;
13307 
13308 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13309 	}
13310 }
13311 
13312 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13313 {
13314 	struct net_device *dev = bp->dev;
13315 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13316 	struct netdev_hw_addr *ha;
13317 	int i, off = 0, rc;
13318 	bool uc_update;
13319 
13320 	netif_addr_lock_bh(dev);
13321 	uc_update = bnxt_uc_list_updated(bp);
13322 	netif_addr_unlock_bh(dev);
13323 
13324 	if (!uc_update)
13325 		goto skip_uc;
13326 
13327 	for (i = 1; i < vnic->uc_filter_count; i++) {
13328 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13329 
13330 		bnxt_hwrm_l2_filter_free(bp, fltr);
13331 		bnxt_del_l2_filter(bp, fltr);
13332 	}
13333 
13334 	vnic->uc_filter_count = 1;
13335 
13336 	netif_addr_lock_bh(dev);
13337 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13338 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13339 	} else {
13340 		netdev_for_each_uc_addr(ha, dev) {
13341 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13342 			off += ETH_ALEN;
13343 			vnic->uc_filter_count++;
13344 		}
13345 	}
13346 	netif_addr_unlock_bh(dev);
13347 
13348 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13349 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13350 		if (rc) {
13351 			if (BNXT_VF(bp) && rc == -ENODEV) {
13352 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13353 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13354 				else
13355 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13356 				rc = 0;
13357 			} else {
13358 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13359 			}
13360 			vnic->uc_filter_count = i;
13361 			return rc;
13362 		}
13363 	}
13364 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13365 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13366 
13367 skip_uc:
13368 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13369 	    !bnxt_promisc_ok(bp))
13370 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13371 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13372 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13373 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13374 			    rc);
13375 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13376 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13377 		vnic->mc_list_count = 0;
13378 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13379 	}
13380 	if (rc)
13381 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13382 			   rc);
13383 
13384 	return rc;
13385 }
13386 
13387 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13388 {
13389 #ifdef CONFIG_BNXT_SRIOV
13390 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13391 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13392 
13393 		/* No minimum rings were provisioned by the PF.  Don't
13394 		 * reserve rings by default when device is down.
13395 		 */
13396 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13397 			return true;
13398 
13399 		if (!netif_running(bp->dev))
13400 			return false;
13401 	}
13402 #endif
13403 	return true;
13404 }
13405 
13406 /* If the chip and firmware supports RFS */
13407 static bool bnxt_rfs_supported(struct bnxt *bp)
13408 {
13409 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13410 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13411 			return true;
13412 		return false;
13413 	}
13414 	/* 212 firmware is broken for aRFS */
13415 	if (BNXT_FW_MAJ(bp) == 212)
13416 		return false;
13417 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13418 		return true;
13419 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13420 		return true;
13421 	return false;
13422 }
13423 
13424 /* If runtime conditions support RFS */
13425 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13426 {
13427 	struct bnxt_hw_rings hwr = {0};
13428 	int max_vnics, max_rss_ctxs;
13429 
13430 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13431 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13432 		return bnxt_rfs_supported(bp);
13433 
13434 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13435 		return false;
13436 
13437 	hwr.grp = bp->rx_nr_rings;
13438 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13439 	if (new_rss_ctx)
13440 		hwr.vnic++;
13441 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13442 	max_vnics = bnxt_get_max_func_vnics(bp);
13443 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13444 
13445 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13446 		if (bp->rx_nr_rings > 1)
13447 			netdev_warn(bp->dev,
13448 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13449 				    min(max_rss_ctxs - 1, max_vnics - 1));
13450 		return false;
13451 	}
13452 
13453 	if (!BNXT_NEW_RM(bp))
13454 		return true;
13455 
13456 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13457 	 * issue that will mess up the default VNIC if we reduce the
13458 	 * reservations.
13459 	 */
13460 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13461 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13462 		return true;
13463 
13464 	bnxt_hwrm_reserve_rings(bp, &hwr);
13465 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13466 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13467 		return true;
13468 
13469 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13470 	hwr.vnic = 1;
13471 	hwr.rss_ctx = 0;
13472 	bnxt_hwrm_reserve_rings(bp, &hwr);
13473 	return false;
13474 }
13475 
13476 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13477 					   netdev_features_t features)
13478 {
13479 	struct bnxt *bp = netdev_priv(dev);
13480 	netdev_features_t vlan_features;
13481 
13482 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13483 		features &= ~NETIF_F_NTUPLE;
13484 
13485 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13486 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13487 
13488 	if (!(features & NETIF_F_GRO))
13489 		features &= ~NETIF_F_GRO_HW;
13490 
13491 	if (features & NETIF_F_GRO_HW)
13492 		features &= ~NETIF_F_LRO;
13493 
13494 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13495 	 * turned on or off together.
13496 	 */
13497 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13498 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13499 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13500 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13501 		else if (vlan_features)
13502 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13503 	}
13504 #ifdef CONFIG_BNXT_SRIOV
13505 	if (BNXT_VF(bp) && bp->vf.vlan)
13506 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13507 #endif
13508 	return features;
13509 }
13510 
13511 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13512 				bool link_re_init, u32 flags, bool update_tpa)
13513 {
13514 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13515 	bp->flags = flags;
13516 	if (update_tpa)
13517 		bnxt_set_ring_params(bp);
13518 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13519 }
13520 
13521 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13522 {
13523 	bool update_tpa = false, update_ntuple = false;
13524 	struct bnxt *bp = netdev_priv(dev);
13525 	u32 flags = bp->flags;
13526 	u32 changes;
13527 	int rc = 0;
13528 	bool re_init = false;
13529 
13530 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13531 	if (features & NETIF_F_GRO_HW)
13532 		flags |= BNXT_FLAG_GRO;
13533 	else if (features & NETIF_F_LRO)
13534 		flags |= BNXT_FLAG_LRO;
13535 
13536 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13537 		flags &= ~BNXT_FLAG_TPA;
13538 
13539 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13540 		flags |= BNXT_FLAG_STRIP_VLAN;
13541 
13542 	if (features & NETIF_F_NTUPLE)
13543 		flags |= BNXT_FLAG_RFS;
13544 	else
13545 		bnxt_clear_usr_fltrs(bp, true);
13546 
13547 	changes = flags ^ bp->flags;
13548 	if (changes & BNXT_FLAG_TPA) {
13549 		update_tpa = true;
13550 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13551 		    (flags & BNXT_FLAG_TPA) == 0 ||
13552 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13553 			re_init = true;
13554 	}
13555 
13556 	if (changes & ~BNXT_FLAG_TPA)
13557 		re_init = true;
13558 
13559 	if (changes & BNXT_FLAG_RFS)
13560 		update_ntuple = true;
13561 
13562 	if (flags != bp->flags) {
13563 		u32 old_flags = bp->flags;
13564 
13565 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13566 			bp->flags = flags;
13567 			if (update_tpa)
13568 				bnxt_set_ring_params(bp);
13569 			return rc;
13570 		}
13571 
13572 		if (update_ntuple)
13573 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13574 
13575 		if (re_init)
13576 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13577 
13578 		if (update_tpa) {
13579 			bp->flags = flags;
13580 			rc = bnxt_set_tpa(bp,
13581 					  (flags & BNXT_FLAG_TPA) ?
13582 					  true : false);
13583 			if (rc)
13584 				bp->flags = old_flags;
13585 		}
13586 	}
13587 	return rc;
13588 }
13589 
13590 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13591 			      u8 **nextp)
13592 {
13593 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13594 	struct hop_jumbo_hdr *jhdr;
13595 	int hdr_count = 0;
13596 	u8 *nexthdr;
13597 	int start;
13598 
13599 	/* Check that there are at most 2 IPv6 extension headers, no
13600 	 * fragment header, and each is <= 64 bytes.
13601 	 */
13602 	start = nw_off + sizeof(*ip6h);
13603 	nexthdr = &ip6h->nexthdr;
13604 	while (ipv6_ext_hdr(*nexthdr)) {
13605 		struct ipv6_opt_hdr *hp;
13606 		int hdrlen;
13607 
13608 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13609 		    *nexthdr == NEXTHDR_FRAGMENT)
13610 			return false;
13611 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13612 					  skb_headlen(skb), NULL);
13613 		if (!hp)
13614 			return false;
13615 		if (*nexthdr == NEXTHDR_AUTH)
13616 			hdrlen = ipv6_authlen(hp);
13617 		else
13618 			hdrlen = ipv6_optlen(hp);
13619 
13620 		if (hdrlen > 64)
13621 			return false;
13622 
13623 		/* The ext header may be a hop-by-hop header inserted for
13624 		 * big TCP purposes. This will be removed before sending
13625 		 * from NIC, so do not count it.
13626 		 */
13627 		if (*nexthdr == NEXTHDR_HOP) {
13628 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13629 				goto increment_hdr;
13630 
13631 			jhdr = (struct hop_jumbo_hdr *)hp;
13632 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13633 			    jhdr->nexthdr != IPPROTO_TCP)
13634 				goto increment_hdr;
13635 
13636 			goto next_hdr;
13637 		}
13638 increment_hdr:
13639 		hdr_count++;
13640 next_hdr:
13641 		nexthdr = &hp->nexthdr;
13642 		start += hdrlen;
13643 	}
13644 	if (nextp) {
13645 		/* Caller will check inner protocol */
13646 		if (skb->encapsulation) {
13647 			*nextp = nexthdr;
13648 			return true;
13649 		}
13650 		*nextp = NULL;
13651 	}
13652 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13653 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13654 }
13655 
13656 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13657 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13658 {
13659 	struct udphdr *uh = udp_hdr(skb);
13660 	__be16 udp_port = uh->dest;
13661 
13662 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13663 	    udp_port != bp->vxlan_gpe_port)
13664 		return false;
13665 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13666 		struct ethhdr *eh = inner_eth_hdr(skb);
13667 
13668 		switch (eh->h_proto) {
13669 		case htons(ETH_P_IP):
13670 			return true;
13671 		case htons(ETH_P_IPV6):
13672 			return bnxt_exthdr_check(bp, skb,
13673 						 skb_inner_network_offset(skb),
13674 						 NULL);
13675 		}
13676 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13677 		return true;
13678 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13679 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13680 					 NULL);
13681 	}
13682 	return false;
13683 }
13684 
13685 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13686 {
13687 	switch (l4_proto) {
13688 	case IPPROTO_UDP:
13689 		return bnxt_udp_tunl_check(bp, skb);
13690 	case IPPROTO_IPIP:
13691 		return true;
13692 	case IPPROTO_GRE: {
13693 		switch (skb->inner_protocol) {
13694 		default:
13695 			return false;
13696 		case htons(ETH_P_IP):
13697 			return true;
13698 		case htons(ETH_P_IPV6):
13699 			fallthrough;
13700 		}
13701 	}
13702 	case IPPROTO_IPV6:
13703 		/* Check ext headers of inner ipv6 */
13704 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13705 					 NULL);
13706 	}
13707 	return false;
13708 }
13709 
13710 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13711 					     struct net_device *dev,
13712 					     netdev_features_t features)
13713 {
13714 	struct bnxt *bp = netdev_priv(dev);
13715 	u8 *l4_proto;
13716 
13717 	features = vlan_features_check(skb, features);
13718 	switch (vlan_get_protocol(skb)) {
13719 	case htons(ETH_P_IP):
13720 		if (!skb->encapsulation)
13721 			return features;
13722 		l4_proto = &ip_hdr(skb)->protocol;
13723 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13724 			return features;
13725 		break;
13726 	case htons(ETH_P_IPV6):
13727 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13728 				       &l4_proto))
13729 			break;
13730 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13731 			return features;
13732 		break;
13733 	}
13734 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13735 }
13736 
13737 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13738 			 u32 *reg_buf)
13739 {
13740 	struct hwrm_dbg_read_direct_output *resp;
13741 	struct hwrm_dbg_read_direct_input *req;
13742 	__le32 *dbg_reg_buf;
13743 	dma_addr_t mapping;
13744 	int rc, i;
13745 
13746 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13747 	if (rc)
13748 		return rc;
13749 
13750 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13751 					 &mapping);
13752 	if (!dbg_reg_buf) {
13753 		rc = -ENOMEM;
13754 		goto dbg_rd_reg_exit;
13755 	}
13756 
13757 	req->host_dest_addr = cpu_to_le64(mapping);
13758 
13759 	resp = hwrm_req_hold(bp, req);
13760 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13761 	req->read_len32 = cpu_to_le32(num_words);
13762 
13763 	rc = hwrm_req_send(bp, req);
13764 	if (rc || resp->error_code) {
13765 		rc = -EIO;
13766 		goto dbg_rd_reg_exit;
13767 	}
13768 	for (i = 0; i < num_words; i++)
13769 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13770 
13771 dbg_rd_reg_exit:
13772 	hwrm_req_drop(bp, req);
13773 	return rc;
13774 }
13775 
13776 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13777 				       u32 ring_id, u32 *prod, u32 *cons)
13778 {
13779 	struct hwrm_dbg_ring_info_get_output *resp;
13780 	struct hwrm_dbg_ring_info_get_input *req;
13781 	int rc;
13782 
13783 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13784 	if (rc)
13785 		return rc;
13786 
13787 	req->ring_type = ring_type;
13788 	req->fw_ring_id = cpu_to_le32(ring_id);
13789 	resp = hwrm_req_hold(bp, req);
13790 	rc = hwrm_req_send(bp, req);
13791 	if (!rc) {
13792 		*prod = le32_to_cpu(resp->producer_index);
13793 		*cons = le32_to_cpu(resp->consumer_index);
13794 	}
13795 	hwrm_req_drop(bp, req);
13796 	return rc;
13797 }
13798 
13799 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13800 {
13801 	struct bnxt_tx_ring_info *txr;
13802 	int i = bnapi->index, j;
13803 
13804 	bnxt_for_each_napi_tx(j, bnapi, txr)
13805 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13806 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13807 			    txr->tx_cons);
13808 }
13809 
13810 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13811 {
13812 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13813 	int i = bnapi->index;
13814 
13815 	if (!rxr)
13816 		return;
13817 
13818 	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",
13819 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13820 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13821 		    rxr->rx_sw_agg_prod);
13822 }
13823 
13824 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13825 {
13826 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13827 	int i = bnapi->index;
13828 
13829 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13830 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13831 }
13832 
13833 static void bnxt_dbg_dump_states(struct bnxt *bp)
13834 {
13835 	int i;
13836 	struct bnxt_napi *bnapi;
13837 
13838 	for (i = 0; i < bp->cp_nr_rings; i++) {
13839 		bnapi = bp->bnapi[i];
13840 		if (netif_msg_drv(bp)) {
13841 			bnxt_dump_tx_sw_state(bnapi);
13842 			bnxt_dump_rx_sw_state(bnapi);
13843 			bnxt_dump_cp_sw_state(bnapi);
13844 		}
13845 	}
13846 }
13847 
13848 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13849 {
13850 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13851 	struct hwrm_ring_reset_input *req;
13852 	struct bnxt_napi *bnapi = rxr->bnapi;
13853 	struct bnxt_cp_ring_info *cpr;
13854 	u16 cp_ring_id;
13855 	int rc;
13856 
13857 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13858 	if (rc)
13859 		return rc;
13860 
13861 	cpr = &bnapi->cp_ring;
13862 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
13863 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
13864 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
13865 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
13866 	return hwrm_req_send_silent(bp, req);
13867 }
13868 
13869 static void bnxt_reset_task(struct bnxt *bp, bool silent)
13870 {
13871 	if (!silent)
13872 		bnxt_dbg_dump_states(bp);
13873 	if (netif_running(bp->dev)) {
13874 		bnxt_close_nic(bp, !silent, false);
13875 		bnxt_open_nic(bp, !silent, false);
13876 	}
13877 }
13878 
13879 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
13880 {
13881 	struct bnxt *bp = netdev_priv(dev);
13882 
13883 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
13884 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
13885 }
13886 
13887 static void bnxt_fw_health_check(struct bnxt *bp)
13888 {
13889 	struct bnxt_fw_health *fw_health = bp->fw_health;
13890 	struct pci_dev *pdev = bp->pdev;
13891 	u32 val;
13892 
13893 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13894 		return;
13895 
13896 	/* Make sure it is enabled before checking the tmr_counter. */
13897 	smp_rmb();
13898 	if (fw_health->tmr_counter) {
13899 		fw_health->tmr_counter--;
13900 		return;
13901 	}
13902 
13903 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13904 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
13905 		fw_health->arrests++;
13906 		goto fw_reset;
13907 	}
13908 
13909 	fw_health->last_fw_heartbeat = val;
13910 
13911 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13912 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
13913 		fw_health->discoveries++;
13914 		goto fw_reset;
13915 	}
13916 
13917 	fw_health->tmr_counter = fw_health->tmr_multiplier;
13918 	return;
13919 
13920 fw_reset:
13921 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
13922 }
13923 
13924 static void bnxt_timer(struct timer_list *t)
13925 {
13926 	struct bnxt *bp = from_timer(bp, t, timer);
13927 	struct net_device *dev = bp->dev;
13928 
13929 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
13930 		return;
13931 
13932 	if (atomic_read(&bp->intr_sem) != 0)
13933 		goto bnxt_restart_timer;
13934 
13935 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
13936 		bnxt_fw_health_check(bp);
13937 
13938 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
13939 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
13940 
13941 	if (bnxt_tc_flower_enabled(bp))
13942 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
13943 
13944 #ifdef CONFIG_RFS_ACCEL
13945 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
13946 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
13947 #endif /*CONFIG_RFS_ACCEL*/
13948 
13949 	if (bp->link_info.phy_retry) {
13950 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
13951 			bp->link_info.phy_retry = false;
13952 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
13953 		} else {
13954 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
13955 		}
13956 	}
13957 
13958 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13959 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13960 
13961 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
13962 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
13963 
13964 bnxt_restart_timer:
13965 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13966 }
13967 
13968 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
13969 {
13970 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
13971 	 * set.  If the device is being closed, bnxt_close() may be holding
13972 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
13973 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
13974 	 */
13975 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13976 	rtnl_lock();
13977 }
13978 
13979 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
13980 {
13981 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13982 	rtnl_unlock();
13983 }
13984 
13985 /* Only called from bnxt_sp_task() */
13986 static void bnxt_reset(struct bnxt *bp, bool silent)
13987 {
13988 	bnxt_rtnl_lock_sp(bp);
13989 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
13990 		bnxt_reset_task(bp, silent);
13991 	bnxt_rtnl_unlock_sp(bp);
13992 }
13993 
13994 /* Only called from bnxt_sp_task() */
13995 static void bnxt_rx_ring_reset(struct bnxt *bp)
13996 {
13997 	int i;
13998 
13999 	bnxt_rtnl_lock_sp(bp);
14000 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14001 		bnxt_rtnl_unlock_sp(bp);
14002 		return;
14003 	}
14004 	/* Disable and flush TPA before resetting the RX ring */
14005 	if (bp->flags & BNXT_FLAG_TPA)
14006 		bnxt_set_tpa(bp, false);
14007 	for (i = 0; i < bp->rx_nr_rings; i++) {
14008 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14009 		struct bnxt_cp_ring_info *cpr;
14010 		int rc;
14011 
14012 		if (!rxr->bnapi->in_reset)
14013 			continue;
14014 
14015 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14016 		if (rc) {
14017 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14018 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14019 			else
14020 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14021 					    rc);
14022 			bnxt_reset_task(bp, true);
14023 			break;
14024 		}
14025 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14026 		rxr->rx_prod = 0;
14027 		rxr->rx_agg_prod = 0;
14028 		rxr->rx_sw_agg_prod = 0;
14029 		rxr->rx_next_cons = 0;
14030 		rxr->bnapi->in_reset = false;
14031 		bnxt_alloc_one_rx_ring(bp, i);
14032 		cpr = &rxr->bnapi->cp_ring;
14033 		cpr->sw_stats->rx.rx_resets++;
14034 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14035 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14036 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14037 	}
14038 	if (bp->flags & BNXT_FLAG_TPA)
14039 		bnxt_set_tpa(bp, true);
14040 	bnxt_rtnl_unlock_sp(bp);
14041 }
14042 
14043 static void bnxt_fw_fatal_close(struct bnxt *bp)
14044 {
14045 	bnxt_tx_disable(bp);
14046 	bnxt_disable_napi(bp);
14047 	bnxt_disable_int_sync(bp);
14048 	bnxt_free_irq(bp);
14049 	bnxt_clear_int_mode(bp);
14050 	pci_disable_device(bp->pdev);
14051 }
14052 
14053 static void bnxt_fw_reset_close(struct bnxt *bp)
14054 {
14055 	/* When firmware is in fatal state, quiesce device and disable
14056 	 * bus master to prevent any potential bad DMAs before freeing
14057 	 * kernel memory.
14058 	 */
14059 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14060 		u16 val = 0;
14061 
14062 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14063 		if (val == 0xffff)
14064 			bp->fw_reset_min_dsecs = 0;
14065 		bnxt_fw_fatal_close(bp);
14066 	}
14067 	__bnxt_close_nic(bp, true, false);
14068 	bnxt_vf_reps_free(bp);
14069 	bnxt_clear_int_mode(bp);
14070 	bnxt_hwrm_func_drv_unrgtr(bp);
14071 	if (pci_is_enabled(bp->pdev))
14072 		pci_disable_device(bp->pdev);
14073 	bnxt_free_ctx_mem(bp, false);
14074 }
14075 
14076 static bool is_bnxt_fw_ok(struct bnxt *bp)
14077 {
14078 	struct bnxt_fw_health *fw_health = bp->fw_health;
14079 	bool no_heartbeat = false, has_reset = false;
14080 	u32 val;
14081 
14082 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14083 	if (val == fw_health->last_fw_heartbeat)
14084 		no_heartbeat = true;
14085 
14086 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14087 	if (val != fw_health->last_fw_reset_cnt)
14088 		has_reset = true;
14089 
14090 	if (!no_heartbeat && has_reset)
14091 		return true;
14092 
14093 	return false;
14094 }
14095 
14096 /* rtnl_lock is acquired before calling this function */
14097 static void bnxt_force_fw_reset(struct bnxt *bp)
14098 {
14099 	struct bnxt_fw_health *fw_health = bp->fw_health;
14100 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14101 	u32 wait_dsecs;
14102 
14103 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14104 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14105 		return;
14106 
14107 	/* we have to serialize with bnxt_refclk_read()*/
14108 	if (ptp) {
14109 		unsigned long flags;
14110 
14111 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14112 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14113 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14114 	} else {
14115 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14116 	}
14117 	bnxt_fw_reset_close(bp);
14118 	wait_dsecs = fw_health->master_func_wait_dsecs;
14119 	if (fw_health->primary) {
14120 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14121 			wait_dsecs = 0;
14122 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14123 	} else {
14124 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14125 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14126 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14127 	}
14128 
14129 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14130 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14131 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14132 }
14133 
14134 void bnxt_fw_exception(struct bnxt *bp)
14135 {
14136 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14137 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14138 	bnxt_ulp_stop(bp);
14139 	bnxt_rtnl_lock_sp(bp);
14140 	bnxt_force_fw_reset(bp);
14141 	bnxt_rtnl_unlock_sp(bp);
14142 }
14143 
14144 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14145  * < 0 on error.
14146  */
14147 static int bnxt_get_registered_vfs(struct bnxt *bp)
14148 {
14149 #ifdef CONFIG_BNXT_SRIOV
14150 	int rc;
14151 
14152 	if (!BNXT_PF(bp))
14153 		return 0;
14154 
14155 	rc = bnxt_hwrm_func_qcfg(bp);
14156 	if (rc) {
14157 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14158 		return rc;
14159 	}
14160 	if (bp->pf.registered_vfs)
14161 		return bp->pf.registered_vfs;
14162 	if (bp->sriov_cfg)
14163 		return 1;
14164 #endif
14165 	return 0;
14166 }
14167 
14168 void bnxt_fw_reset(struct bnxt *bp)
14169 {
14170 	bnxt_ulp_stop(bp);
14171 	bnxt_rtnl_lock_sp(bp);
14172 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14173 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14174 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14175 		int n = 0, tmo;
14176 
14177 		/* we have to serialize with bnxt_refclk_read()*/
14178 		if (ptp) {
14179 			unsigned long flags;
14180 
14181 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14182 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14183 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14184 		} else {
14185 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14186 		}
14187 		if (bp->pf.active_vfs &&
14188 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14189 			n = bnxt_get_registered_vfs(bp);
14190 		if (n < 0) {
14191 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14192 				   n);
14193 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14194 			dev_close(bp->dev);
14195 			goto fw_reset_exit;
14196 		} else if (n > 0) {
14197 			u16 vf_tmo_dsecs = n * 10;
14198 
14199 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14200 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14201 			bp->fw_reset_state =
14202 				BNXT_FW_RESET_STATE_POLL_VF;
14203 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14204 			goto fw_reset_exit;
14205 		}
14206 		bnxt_fw_reset_close(bp);
14207 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14208 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14209 			tmo = HZ / 10;
14210 		} else {
14211 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14212 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14213 		}
14214 		bnxt_queue_fw_reset_work(bp, tmo);
14215 	}
14216 fw_reset_exit:
14217 	bnxt_rtnl_unlock_sp(bp);
14218 }
14219 
14220 static void bnxt_chk_missed_irq(struct bnxt *bp)
14221 {
14222 	int i;
14223 
14224 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14225 		return;
14226 
14227 	for (i = 0; i < bp->cp_nr_rings; i++) {
14228 		struct bnxt_napi *bnapi = bp->bnapi[i];
14229 		struct bnxt_cp_ring_info *cpr;
14230 		u32 fw_ring_id;
14231 		int j;
14232 
14233 		if (!bnapi)
14234 			continue;
14235 
14236 		cpr = &bnapi->cp_ring;
14237 		for (j = 0; j < cpr->cp_ring_count; j++) {
14238 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14239 			u32 val[2];
14240 
14241 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14242 				continue;
14243 
14244 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14245 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14246 				continue;
14247 			}
14248 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14249 			bnxt_dbg_hwrm_ring_info_get(bp,
14250 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14251 				fw_ring_id, &val[0], &val[1]);
14252 			cpr->sw_stats->cmn.missed_irqs++;
14253 		}
14254 	}
14255 }
14256 
14257 static void bnxt_cfg_ntp_filters(struct bnxt *);
14258 
14259 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14260 {
14261 	struct bnxt_link_info *link_info = &bp->link_info;
14262 
14263 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14264 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14265 		if (bp->hwrm_spec_code >= 0x10201) {
14266 			if (link_info->auto_pause_setting &
14267 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14268 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14269 		} else {
14270 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14271 		}
14272 		bnxt_set_auto_speed(link_info);
14273 	} else {
14274 		bnxt_set_force_speed(link_info);
14275 		link_info->req_duplex = link_info->duplex_setting;
14276 	}
14277 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14278 		link_info->req_flow_ctrl =
14279 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14280 	else
14281 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14282 }
14283 
14284 static void bnxt_fw_echo_reply(struct bnxt *bp)
14285 {
14286 	struct bnxt_fw_health *fw_health = bp->fw_health;
14287 	struct hwrm_func_echo_response_input *req;
14288 	int rc;
14289 
14290 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14291 	if (rc)
14292 		return;
14293 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14294 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14295 	hwrm_req_send(bp, req);
14296 }
14297 
14298 static void bnxt_ulp_restart(struct bnxt *bp)
14299 {
14300 	bnxt_ulp_stop(bp);
14301 	bnxt_ulp_start(bp, 0);
14302 }
14303 
14304 static void bnxt_sp_task(struct work_struct *work)
14305 {
14306 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14307 
14308 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14309 	smp_mb__after_atomic();
14310 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14311 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14312 		return;
14313 	}
14314 
14315 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14316 		bnxt_ulp_restart(bp);
14317 		bnxt_reenable_sriov(bp);
14318 	}
14319 
14320 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14321 		bnxt_cfg_rx_mode(bp);
14322 
14323 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14324 		bnxt_cfg_ntp_filters(bp);
14325 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14326 		bnxt_hwrm_exec_fwd_req(bp);
14327 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14328 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14329 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14330 		bnxt_hwrm_port_qstats(bp, 0);
14331 		bnxt_hwrm_port_qstats_ext(bp, 0);
14332 		bnxt_accumulate_all_stats(bp);
14333 	}
14334 
14335 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14336 		int rc;
14337 
14338 		mutex_lock(&bp->link_lock);
14339 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14340 				       &bp->sp_event))
14341 			bnxt_hwrm_phy_qcaps(bp);
14342 
14343 		rc = bnxt_update_link(bp, true);
14344 		if (rc)
14345 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14346 				   rc);
14347 
14348 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14349 				       &bp->sp_event))
14350 			bnxt_init_ethtool_link_settings(bp);
14351 		mutex_unlock(&bp->link_lock);
14352 	}
14353 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14354 		int rc;
14355 
14356 		mutex_lock(&bp->link_lock);
14357 		rc = bnxt_update_phy_setting(bp);
14358 		mutex_unlock(&bp->link_lock);
14359 		if (rc) {
14360 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14361 		} else {
14362 			bp->link_info.phy_retry = false;
14363 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14364 		}
14365 	}
14366 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14367 		mutex_lock(&bp->link_lock);
14368 		bnxt_get_port_module_status(bp);
14369 		mutex_unlock(&bp->link_lock);
14370 	}
14371 
14372 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14373 		bnxt_tc_flow_stats_work(bp);
14374 
14375 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14376 		bnxt_chk_missed_irq(bp);
14377 
14378 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14379 		bnxt_fw_echo_reply(bp);
14380 
14381 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14382 		bnxt_hwmon_notify_event(bp);
14383 
14384 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14385 	 * must be the last functions to be called before exiting.
14386 	 */
14387 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14388 		bnxt_reset(bp, false);
14389 
14390 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14391 		bnxt_reset(bp, true);
14392 
14393 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14394 		bnxt_rx_ring_reset(bp);
14395 
14396 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14397 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14398 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14399 			bnxt_devlink_health_fw_report(bp);
14400 		else
14401 			bnxt_fw_reset(bp);
14402 	}
14403 
14404 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14405 		if (!is_bnxt_fw_ok(bp))
14406 			bnxt_devlink_health_fw_report(bp);
14407 	}
14408 
14409 	smp_mb__before_atomic();
14410 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14411 }
14412 
14413 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14414 				int *max_cp);
14415 
14416 /* Under rtnl_lock */
14417 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14418 		     int tx_xdp)
14419 {
14420 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14421 	struct bnxt_hw_rings hwr = {0};
14422 	int rx_rings = rx;
14423 	int rc;
14424 
14425 	if (tcs)
14426 		tx_sets = tcs;
14427 
14428 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14429 
14430 	if (max_rx < rx_rings)
14431 		return -ENOMEM;
14432 
14433 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14434 		rx_rings <<= 1;
14435 
14436 	hwr.rx = rx_rings;
14437 	hwr.tx = tx * tx_sets + tx_xdp;
14438 	if (max_tx < hwr.tx)
14439 		return -ENOMEM;
14440 
14441 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14442 
14443 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14444 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14445 	if (max_cp < hwr.cp)
14446 		return -ENOMEM;
14447 	hwr.stat = hwr.cp;
14448 	if (BNXT_NEW_RM(bp)) {
14449 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14450 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14451 		hwr.grp = rx;
14452 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14453 	}
14454 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14455 		hwr.cp_p5 = hwr.tx + rx;
14456 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14457 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14458 		if (!bnxt_ulp_registered(bp->edev)) {
14459 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14460 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14461 		}
14462 		if (hwr.cp > bp->total_irqs) {
14463 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14464 
14465 			if (total_msix < hwr.cp) {
14466 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14467 					    hwr.cp, total_msix);
14468 				rc = -ENOSPC;
14469 			}
14470 		}
14471 	}
14472 	return rc;
14473 }
14474 
14475 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14476 {
14477 	if (bp->bar2) {
14478 		pci_iounmap(pdev, bp->bar2);
14479 		bp->bar2 = NULL;
14480 	}
14481 
14482 	if (bp->bar1) {
14483 		pci_iounmap(pdev, bp->bar1);
14484 		bp->bar1 = NULL;
14485 	}
14486 
14487 	if (bp->bar0) {
14488 		pci_iounmap(pdev, bp->bar0);
14489 		bp->bar0 = NULL;
14490 	}
14491 }
14492 
14493 static void bnxt_cleanup_pci(struct bnxt *bp)
14494 {
14495 	bnxt_unmap_bars(bp, bp->pdev);
14496 	pci_release_regions(bp->pdev);
14497 	if (pci_is_enabled(bp->pdev))
14498 		pci_disable_device(bp->pdev);
14499 }
14500 
14501 static void bnxt_init_dflt_coal(struct bnxt *bp)
14502 {
14503 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14504 	struct bnxt_coal *coal;
14505 	u16 flags = 0;
14506 
14507 	if (coal_cap->cmpl_params &
14508 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14509 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14510 
14511 	/* Tick values in micro seconds.
14512 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14513 	 */
14514 	coal = &bp->rx_coal;
14515 	coal->coal_ticks = 10;
14516 	coal->coal_bufs = 30;
14517 	coal->coal_ticks_irq = 1;
14518 	coal->coal_bufs_irq = 2;
14519 	coal->idle_thresh = 50;
14520 	coal->bufs_per_record = 2;
14521 	coal->budget = 64;		/* NAPI budget */
14522 	coal->flags = flags;
14523 
14524 	coal = &bp->tx_coal;
14525 	coal->coal_ticks = 28;
14526 	coal->coal_bufs = 30;
14527 	coal->coal_ticks_irq = 2;
14528 	coal->coal_bufs_irq = 2;
14529 	coal->bufs_per_record = 1;
14530 	coal->flags = flags;
14531 
14532 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14533 }
14534 
14535 /* FW that pre-reserves 1 VNIC per function */
14536 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14537 {
14538 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14539 
14540 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14541 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14542 		return true;
14543 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14544 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14545 		return true;
14546 	return false;
14547 }
14548 
14549 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14550 {
14551 	int rc;
14552 
14553 	bp->fw_cap = 0;
14554 	rc = bnxt_hwrm_ver_get(bp);
14555 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14556 	 * so wait before continuing with recovery.
14557 	 */
14558 	if (rc)
14559 		msleep(100);
14560 	bnxt_try_map_fw_health_reg(bp);
14561 	if (rc) {
14562 		rc = bnxt_try_recover_fw(bp);
14563 		if (rc)
14564 			return rc;
14565 		rc = bnxt_hwrm_ver_get(bp);
14566 		if (rc)
14567 			return rc;
14568 	}
14569 
14570 	bnxt_nvm_cfg_ver_get(bp);
14571 
14572 	rc = bnxt_hwrm_func_reset(bp);
14573 	if (rc)
14574 		return -ENODEV;
14575 
14576 	bnxt_hwrm_fw_set_time(bp);
14577 	return 0;
14578 }
14579 
14580 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14581 {
14582 	int rc;
14583 
14584 	/* Get the MAX capabilities for this function */
14585 	rc = bnxt_hwrm_func_qcaps(bp);
14586 	if (rc) {
14587 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14588 			   rc);
14589 		return -ENODEV;
14590 	}
14591 
14592 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14593 	if (rc)
14594 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14595 			    rc);
14596 
14597 	if (bnxt_alloc_fw_health(bp)) {
14598 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14599 	} else {
14600 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14601 		if (rc)
14602 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14603 				    rc);
14604 	}
14605 
14606 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14607 	if (rc)
14608 		return -ENODEV;
14609 
14610 	rc = bnxt_alloc_crash_dump_mem(bp);
14611 	if (rc)
14612 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14613 			    rc);
14614 	if (!rc) {
14615 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14616 		if (rc) {
14617 			bnxt_free_crash_dump_mem(bp);
14618 			netdev_warn(bp->dev,
14619 				    "hwrm crash dump mem failure rc: %d\n", rc);
14620 		}
14621 	}
14622 
14623 	if (bnxt_fw_pre_resv_vnics(bp))
14624 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14625 
14626 	bnxt_hwrm_func_qcfg(bp);
14627 	bnxt_hwrm_vnic_qcaps(bp);
14628 	bnxt_hwrm_port_led_qcaps(bp);
14629 	bnxt_ethtool_init(bp);
14630 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14631 		__bnxt_hwrm_ptp_qcfg(bp);
14632 	bnxt_dcb_init(bp);
14633 	bnxt_hwmon_init(bp);
14634 	return 0;
14635 }
14636 
14637 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14638 {
14639 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14640 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14641 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14642 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14643 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14644 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14645 		bp->rss_hash_delta = bp->rss_hash_cfg;
14646 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14647 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14648 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14649 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14650 	}
14651 }
14652 
14653 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14654 {
14655 	struct net_device *dev = bp->dev;
14656 
14657 	dev->hw_features &= ~NETIF_F_NTUPLE;
14658 	dev->features &= ~NETIF_F_NTUPLE;
14659 	bp->flags &= ~BNXT_FLAG_RFS;
14660 	if (bnxt_rfs_supported(bp)) {
14661 		dev->hw_features |= NETIF_F_NTUPLE;
14662 		if (bnxt_rfs_capable(bp, false)) {
14663 			bp->flags |= BNXT_FLAG_RFS;
14664 			dev->features |= NETIF_F_NTUPLE;
14665 		}
14666 	}
14667 }
14668 
14669 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14670 {
14671 	struct pci_dev *pdev = bp->pdev;
14672 
14673 	bnxt_set_dflt_rss_hash_type(bp);
14674 	bnxt_set_dflt_rfs(bp);
14675 
14676 	bnxt_get_wol_settings(bp);
14677 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14678 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14679 	else
14680 		device_set_wakeup_capable(&pdev->dev, false);
14681 
14682 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14683 	bnxt_hwrm_coal_params_qcaps(bp);
14684 }
14685 
14686 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14687 
14688 int bnxt_fw_init_one(struct bnxt *bp)
14689 {
14690 	int rc;
14691 
14692 	rc = bnxt_fw_init_one_p1(bp);
14693 	if (rc) {
14694 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14695 		return rc;
14696 	}
14697 	rc = bnxt_fw_init_one_p2(bp);
14698 	if (rc) {
14699 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14700 		return rc;
14701 	}
14702 	rc = bnxt_probe_phy(bp, false);
14703 	if (rc)
14704 		return rc;
14705 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14706 	if (rc)
14707 		return rc;
14708 
14709 	bnxt_fw_init_one_p3(bp);
14710 	return 0;
14711 }
14712 
14713 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14714 {
14715 	struct bnxt_fw_health *fw_health = bp->fw_health;
14716 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14717 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14718 	u32 reg_type, reg_off, delay_msecs;
14719 
14720 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14721 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14722 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14723 	switch (reg_type) {
14724 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14725 		pci_write_config_dword(bp->pdev, reg_off, val);
14726 		break;
14727 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14728 		writel(reg_off & BNXT_GRC_BASE_MASK,
14729 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14730 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14731 		fallthrough;
14732 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14733 		writel(val, bp->bar0 + reg_off);
14734 		break;
14735 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14736 		writel(val, bp->bar1 + reg_off);
14737 		break;
14738 	}
14739 	if (delay_msecs) {
14740 		pci_read_config_dword(bp->pdev, 0, &val);
14741 		msleep(delay_msecs);
14742 	}
14743 }
14744 
14745 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14746 {
14747 	struct hwrm_func_qcfg_output *resp;
14748 	struct hwrm_func_qcfg_input *req;
14749 	bool result = true; /* firmware will enforce if unknown */
14750 
14751 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14752 		return result;
14753 
14754 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14755 		return result;
14756 
14757 	req->fid = cpu_to_le16(0xffff);
14758 	resp = hwrm_req_hold(bp, req);
14759 	if (!hwrm_req_send(bp, req))
14760 		result = !!(le16_to_cpu(resp->flags) &
14761 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14762 	hwrm_req_drop(bp, req);
14763 	return result;
14764 }
14765 
14766 static void bnxt_reset_all(struct bnxt *bp)
14767 {
14768 	struct bnxt_fw_health *fw_health = bp->fw_health;
14769 	int i, rc;
14770 
14771 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14772 		bnxt_fw_reset_via_optee(bp);
14773 		bp->fw_reset_timestamp = jiffies;
14774 		return;
14775 	}
14776 
14777 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14778 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14779 			bnxt_fw_reset_writel(bp, i);
14780 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14781 		struct hwrm_fw_reset_input *req;
14782 
14783 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14784 		if (!rc) {
14785 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14786 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14787 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14788 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14789 			rc = hwrm_req_send(bp, req);
14790 		}
14791 		if (rc != -ENODEV)
14792 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14793 	}
14794 	bp->fw_reset_timestamp = jiffies;
14795 }
14796 
14797 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14798 {
14799 	return time_after(jiffies, bp->fw_reset_timestamp +
14800 			  (bp->fw_reset_max_dsecs * HZ / 10));
14801 }
14802 
14803 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14804 {
14805 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14806 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14807 		bnxt_dl_health_fw_status_update(bp, false);
14808 	bp->fw_reset_state = 0;
14809 	dev_close(bp->dev);
14810 }
14811 
14812 static void bnxt_fw_reset_task(struct work_struct *work)
14813 {
14814 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14815 	int rc = 0;
14816 
14817 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14818 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14819 		return;
14820 	}
14821 
14822 	switch (bp->fw_reset_state) {
14823 	case BNXT_FW_RESET_STATE_POLL_VF: {
14824 		int n = bnxt_get_registered_vfs(bp);
14825 		int tmo;
14826 
14827 		if (n < 0) {
14828 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14829 				   n, jiffies_to_msecs(jiffies -
14830 				   bp->fw_reset_timestamp));
14831 			goto fw_reset_abort;
14832 		} else if (n > 0) {
14833 			if (bnxt_fw_reset_timeout(bp)) {
14834 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14835 				bp->fw_reset_state = 0;
14836 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14837 					   n);
14838 				goto ulp_start;
14839 			}
14840 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14841 			return;
14842 		}
14843 		bp->fw_reset_timestamp = jiffies;
14844 		rtnl_lock();
14845 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14846 			bnxt_fw_reset_abort(bp, rc);
14847 			rtnl_unlock();
14848 			goto ulp_start;
14849 		}
14850 		bnxt_fw_reset_close(bp);
14851 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14852 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14853 			tmo = HZ / 10;
14854 		} else {
14855 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14856 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14857 		}
14858 		rtnl_unlock();
14859 		bnxt_queue_fw_reset_work(bp, tmo);
14860 		return;
14861 	}
14862 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
14863 		u32 val;
14864 
14865 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14866 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
14867 		    !bnxt_fw_reset_timeout(bp)) {
14868 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14869 			return;
14870 		}
14871 
14872 		if (!bp->fw_health->primary) {
14873 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
14874 
14875 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14876 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14877 			return;
14878 		}
14879 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14880 	}
14881 		fallthrough;
14882 	case BNXT_FW_RESET_STATE_RESET_FW:
14883 		bnxt_reset_all(bp);
14884 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14885 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
14886 		return;
14887 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
14888 		bnxt_inv_fw_health_reg(bp);
14889 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
14890 		    !bp->fw_reset_min_dsecs) {
14891 			u16 val;
14892 
14893 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14894 			if (val == 0xffff) {
14895 				if (bnxt_fw_reset_timeout(bp)) {
14896 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
14897 					rc = -ETIMEDOUT;
14898 					goto fw_reset_abort;
14899 				}
14900 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
14901 				return;
14902 			}
14903 		}
14904 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14905 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
14906 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
14907 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
14908 			bnxt_dl_remote_reload(bp);
14909 		if (pci_enable_device(bp->pdev)) {
14910 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
14911 			rc = -ENODEV;
14912 			goto fw_reset_abort;
14913 		}
14914 		pci_set_master(bp->pdev);
14915 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
14916 		fallthrough;
14917 	case BNXT_FW_RESET_STATE_POLL_FW:
14918 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
14919 		rc = bnxt_hwrm_poll(bp);
14920 		if (rc) {
14921 			if (bnxt_fw_reset_timeout(bp)) {
14922 				netdev_err(bp->dev, "Firmware reset aborted\n");
14923 				goto fw_reset_abort_status;
14924 			}
14925 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14926 			return;
14927 		}
14928 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
14929 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
14930 		fallthrough;
14931 	case BNXT_FW_RESET_STATE_OPENING:
14932 		while (!rtnl_trylock()) {
14933 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14934 			return;
14935 		}
14936 		rc = bnxt_open(bp->dev);
14937 		if (rc) {
14938 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
14939 			bnxt_fw_reset_abort(bp, rc);
14940 			rtnl_unlock();
14941 			goto ulp_start;
14942 		}
14943 
14944 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
14945 		    bp->fw_health->enabled) {
14946 			bp->fw_health->last_fw_reset_cnt =
14947 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14948 		}
14949 		bp->fw_reset_state = 0;
14950 		/* Make sure fw_reset_state is 0 before clearing the flag */
14951 		smp_mb__before_atomic();
14952 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14953 		bnxt_ptp_reapply_pps(bp);
14954 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
14955 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
14956 			bnxt_dl_health_fw_recovery_done(bp);
14957 			bnxt_dl_health_fw_status_update(bp, true);
14958 		}
14959 		rtnl_unlock();
14960 		bnxt_ulp_start(bp, 0);
14961 		bnxt_reenable_sriov(bp);
14962 		rtnl_lock();
14963 		bnxt_vf_reps_alloc(bp);
14964 		bnxt_vf_reps_open(bp);
14965 		rtnl_unlock();
14966 		break;
14967 	}
14968 	return;
14969 
14970 fw_reset_abort_status:
14971 	if (bp->fw_health->status_reliable ||
14972 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
14973 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14974 
14975 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
14976 	}
14977 fw_reset_abort:
14978 	rtnl_lock();
14979 	bnxt_fw_reset_abort(bp, rc);
14980 	rtnl_unlock();
14981 ulp_start:
14982 	bnxt_ulp_start(bp, rc);
14983 }
14984 
14985 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
14986 {
14987 	int rc;
14988 	struct bnxt *bp = netdev_priv(dev);
14989 
14990 	SET_NETDEV_DEV(dev, &pdev->dev);
14991 
14992 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
14993 	rc = pci_enable_device(pdev);
14994 	if (rc) {
14995 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
14996 		goto init_err;
14997 	}
14998 
14999 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15000 		dev_err(&pdev->dev,
15001 			"Cannot find PCI device base address, aborting\n");
15002 		rc = -ENODEV;
15003 		goto init_err_disable;
15004 	}
15005 
15006 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15007 	if (rc) {
15008 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15009 		goto init_err_disable;
15010 	}
15011 
15012 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
15013 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
15014 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15015 		rc = -EIO;
15016 		goto init_err_release;
15017 	}
15018 
15019 	pci_set_master(pdev);
15020 
15021 	bp->dev = dev;
15022 	bp->pdev = pdev;
15023 
15024 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15025 	 * determines the BAR size.
15026 	 */
15027 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15028 	if (!bp->bar0) {
15029 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15030 		rc = -ENOMEM;
15031 		goto init_err_release;
15032 	}
15033 
15034 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15035 	if (!bp->bar2) {
15036 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15037 		rc = -ENOMEM;
15038 		goto init_err_release;
15039 	}
15040 
15041 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15042 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15043 
15044 	spin_lock_init(&bp->ntp_fltr_lock);
15045 #if BITS_PER_LONG == 32
15046 	spin_lock_init(&bp->db_lock);
15047 #endif
15048 
15049 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15050 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15051 
15052 	timer_setup(&bp->timer, bnxt_timer, 0);
15053 	bp->current_interval = BNXT_TIMER_INTERVAL;
15054 
15055 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15056 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15057 
15058 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15059 	return 0;
15060 
15061 init_err_release:
15062 	bnxt_unmap_bars(bp, pdev);
15063 	pci_release_regions(pdev);
15064 
15065 init_err_disable:
15066 	pci_disable_device(pdev);
15067 
15068 init_err:
15069 	return rc;
15070 }
15071 
15072 /* rtnl_lock held */
15073 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15074 {
15075 	struct sockaddr *addr = p;
15076 	struct bnxt *bp = netdev_priv(dev);
15077 	int rc = 0;
15078 
15079 	if (!is_valid_ether_addr(addr->sa_data))
15080 		return -EADDRNOTAVAIL;
15081 
15082 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15083 		return 0;
15084 
15085 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15086 	if (rc)
15087 		return rc;
15088 
15089 	eth_hw_addr_set(dev, addr->sa_data);
15090 	bnxt_clear_usr_fltrs(bp, true);
15091 	if (netif_running(dev)) {
15092 		bnxt_close_nic(bp, false, false);
15093 		rc = bnxt_open_nic(bp, false, false);
15094 	}
15095 
15096 	return rc;
15097 }
15098 
15099 /* rtnl_lock held */
15100 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15101 {
15102 	struct bnxt *bp = netdev_priv(dev);
15103 
15104 	if (netif_running(dev))
15105 		bnxt_close_nic(bp, true, false);
15106 
15107 	WRITE_ONCE(dev->mtu, new_mtu);
15108 
15109 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15110 	 * program is attached.  We need to set the AGG rings settings and
15111 	 * rx_skb_func accordingly.
15112 	 */
15113 	if (READ_ONCE(bp->xdp_prog))
15114 		bnxt_set_rx_skb_mode(bp, true);
15115 
15116 	bnxt_set_ring_params(bp);
15117 
15118 	if (netif_running(dev))
15119 		return bnxt_open_nic(bp, true, false);
15120 
15121 	return 0;
15122 }
15123 
15124 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15125 {
15126 	struct bnxt *bp = netdev_priv(dev);
15127 	bool sh = false;
15128 	int rc, tx_cp;
15129 
15130 	if (tc > bp->max_tc) {
15131 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15132 			   tc, bp->max_tc);
15133 		return -EINVAL;
15134 	}
15135 
15136 	if (bp->num_tc == tc)
15137 		return 0;
15138 
15139 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15140 		sh = true;
15141 
15142 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15143 			      sh, tc, bp->tx_nr_rings_xdp);
15144 	if (rc)
15145 		return rc;
15146 
15147 	/* Needs to close the device and do hw resource re-allocations */
15148 	if (netif_running(bp->dev))
15149 		bnxt_close_nic(bp, true, false);
15150 
15151 	if (tc) {
15152 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15153 		netdev_set_num_tc(dev, tc);
15154 		bp->num_tc = tc;
15155 	} else {
15156 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15157 		netdev_reset_tc(dev);
15158 		bp->num_tc = 0;
15159 	}
15160 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15161 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15162 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15163 			       tx_cp + bp->rx_nr_rings;
15164 
15165 	if (netif_running(bp->dev))
15166 		return bnxt_open_nic(bp, true, false);
15167 
15168 	return 0;
15169 }
15170 
15171 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15172 				  void *cb_priv)
15173 {
15174 	struct bnxt *bp = cb_priv;
15175 
15176 	if (!bnxt_tc_flower_enabled(bp) ||
15177 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15178 		return -EOPNOTSUPP;
15179 
15180 	switch (type) {
15181 	case TC_SETUP_CLSFLOWER:
15182 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15183 	default:
15184 		return -EOPNOTSUPP;
15185 	}
15186 }
15187 
15188 LIST_HEAD(bnxt_block_cb_list);
15189 
15190 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15191 			 void *type_data)
15192 {
15193 	struct bnxt *bp = netdev_priv(dev);
15194 
15195 	switch (type) {
15196 	case TC_SETUP_BLOCK:
15197 		return flow_block_cb_setup_simple(type_data,
15198 						  &bnxt_block_cb_list,
15199 						  bnxt_setup_tc_block_cb,
15200 						  bp, bp, true);
15201 	case TC_SETUP_QDISC_MQPRIO: {
15202 		struct tc_mqprio_qopt *mqprio = type_data;
15203 
15204 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15205 
15206 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15207 	}
15208 	default:
15209 		return -EOPNOTSUPP;
15210 	}
15211 }
15212 
15213 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15214 			    const struct sk_buff *skb)
15215 {
15216 	struct bnxt_vnic_info *vnic;
15217 
15218 	if (skb)
15219 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15220 
15221 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15222 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15223 }
15224 
15225 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15226 			   u32 idx)
15227 {
15228 	struct hlist_head *head;
15229 	int bit_id;
15230 
15231 	spin_lock_bh(&bp->ntp_fltr_lock);
15232 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15233 	if (bit_id < 0) {
15234 		spin_unlock_bh(&bp->ntp_fltr_lock);
15235 		return -ENOMEM;
15236 	}
15237 
15238 	fltr->base.sw_id = (u16)bit_id;
15239 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15240 	fltr->base.flags |= BNXT_ACT_RING_DST;
15241 	head = &bp->ntp_fltr_hash_tbl[idx];
15242 	hlist_add_head_rcu(&fltr->base.hash, head);
15243 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15244 	bnxt_insert_usr_fltr(bp, &fltr->base);
15245 	bp->ntp_fltr_count++;
15246 	spin_unlock_bh(&bp->ntp_fltr_lock);
15247 	return 0;
15248 }
15249 
15250 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15251 			    struct bnxt_ntuple_filter *f2)
15252 {
15253 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15254 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15255 	struct flow_keys *keys1 = &f1->fkeys;
15256 	struct flow_keys *keys2 = &f2->fkeys;
15257 
15258 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15259 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15260 		return false;
15261 
15262 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15263 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15264 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15265 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15266 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15267 			return false;
15268 	} else {
15269 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15270 				     &keys2->addrs.v6addrs.src) ||
15271 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15272 				     &masks2->addrs.v6addrs.src) ||
15273 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15274 				     &keys2->addrs.v6addrs.dst) ||
15275 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15276 				     &masks2->addrs.v6addrs.dst))
15277 			return false;
15278 	}
15279 
15280 	return keys1->ports.src == keys2->ports.src &&
15281 	       masks1->ports.src == masks2->ports.src &&
15282 	       keys1->ports.dst == keys2->ports.dst &&
15283 	       masks1->ports.dst == masks2->ports.dst &&
15284 	       keys1->control.flags == keys2->control.flags &&
15285 	       f1->l2_fltr == f2->l2_fltr;
15286 }
15287 
15288 struct bnxt_ntuple_filter *
15289 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15290 				struct bnxt_ntuple_filter *fltr, u32 idx)
15291 {
15292 	struct bnxt_ntuple_filter *f;
15293 	struct hlist_head *head;
15294 
15295 	head = &bp->ntp_fltr_hash_tbl[idx];
15296 	hlist_for_each_entry_rcu(f, head, base.hash) {
15297 		if (bnxt_fltr_match(f, fltr))
15298 			return f;
15299 	}
15300 	return NULL;
15301 }
15302 
15303 #ifdef CONFIG_RFS_ACCEL
15304 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15305 			      u16 rxq_index, u32 flow_id)
15306 {
15307 	struct bnxt *bp = netdev_priv(dev);
15308 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15309 	struct flow_keys *fkeys;
15310 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15311 	struct bnxt_l2_filter *l2_fltr;
15312 	int rc = 0, idx;
15313 	u32 flags;
15314 
15315 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15316 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15317 		atomic_inc(&l2_fltr->refcnt);
15318 	} else {
15319 		struct bnxt_l2_key key;
15320 
15321 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15322 		key.vlan = 0;
15323 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15324 		if (!l2_fltr)
15325 			return -EINVAL;
15326 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15327 			bnxt_del_l2_filter(bp, l2_fltr);
15328 			return -EINVAL;
15329 		}
15330 	}
15331 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
15332 	if (!new_fltr) {
15333 		bnxt_del_l2_filter(bp, l2_fltr);
15334 		return -ENOMEM;
15335 	}
15336 
15337 	fkeys = &new_fltr->fkeys;
15338 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15339 		rc = -EPROTONOSUPPORT;
15340 		goto err_free;
15341 	}
15342 
15343 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15344 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15345 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15346 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15347 		rc = -EPROTONOSUPPORT;
15348 		goto err_free;
15349 	}
15350 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15351 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15352 		if (bp->hwrm_spec_code < 0x10601) {
15353 			rc = -EPROTONOSUPPORT;
15354 			goto err_free;
15355 		}
15356 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15357 	}
15358 	flags = fkeys->control.flags;
15359 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15360 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15361 		rc = -EPROTONOSUPPORT;
15362 		goto err_free;
15363 	}
15364 	new_fltr->l2_fltr = l2_fltr;
15365 
15366 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15367 	rcu_read_lock();
15368 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15369 	if (fltr) {
15370 		rc = fltr->base.sw_id;
15371 		rcu_read_unlock();
15372 		goto err_free;
15373 	}
15374 	rcu_read_unlock();
15375 
15376 	new_fltr->flow_id = flow_id;
15377 	new_fltr->base.rxq = rxq_index;
15378 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15379 	if (!rc) {
15380 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15381 		return new_fltr->base.sw_id;
15382 	}
15383 
15384 err_free:
15385 	bnxt_del_l2_filter(bp, l2_fltr);
15386 	kfree(new_fltr);
15387 	return rc;
15388 }
15389 #endif
15390 
15391 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15392 {
15393 	spin_lock_bh(&bp->ntp_fltr_lock);
15394 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15395 		spin_unlock_bh(&bp->ntp_fltr_lock);
15396 		return;
15397 	}
15398 	hlist_del_rcu(&fltr->base.hash);
15399 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15400 	bp->ntp_fltr_count--;
15401 	spin_unlock_bh(&bp->ntp_fltr_lock);
15402 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15403 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15404 	kfree_rcu(fltr, base.rcu);
15405 }
15406 
15407 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15408 {
15409 #ifdef CONFIG_RFS_ACCEL
15410 	int i;
15411 
15412 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15413 		struct hlist_head *head;
15414 		struct hlist_node *tmp;
15415 		struct bnxt_ntuple_filter *fltr;
15416 		int rc;
15417 
15418 		head = &bp->ntp_fltr_hash_tbl[i];
15419 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15420 			bool del = false;
15421 
15422 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15423 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15424 					continue;
15425 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15426 							fltr->flow_id,
15427 							fltr->base.sw_id)) {
15428 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15429 									 fltr);
15430 					del = true;
15431 				}
15432 			} else {
15433 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15434 								       fltr);
15435 				if (rc)
15436 					del = true;
15437 				else
15438 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15439 			}
15440 
15441 			if (del)
15442 				bnxt_del_ntp_filter(bp, fltr);
15443 		}
15444 	}
15445 #endif
15446 }
15447 
15448 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15449 				    unsigned int entry, struct udp_tunnel_info *ti)
15450 {
15451 	struct bnxt *bp = netdev_priv(netdev);
15452 	unsigned int cmd;
15453 
15454 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15455 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15456 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15457 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15458 	else
15459 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15460 
15461 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15462 }
15463 
15464 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15465 				      unsigned int entry, struct udp_tunnel_info *ti)
15466 {
15467 	struct bnxt *bp = netdev_priv(netdev);
15468 	unsigned int cmd;
15469 
15470 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15471 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15472 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15473 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15474 	else
15475 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15476 
15477 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15478 }
15479 
15480 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15481 	.set_port	= bnxt_udp_tunnel_set_port,
15482 	.unset_port	= bnxt_udp_tunnel_unset_port,
15483 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15484 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15485 	.tables		= {
15486 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15487 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15488 	},
15489 }, bnxt_udp_tunnels_p7 = {
15490 	.set_port	= bnxt_udp_tunnel_set_port,
15491 	.unset_port	= bnxt_udp_tunnel_unset_port,
15492 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15493 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15494 	.tables		= {
15495 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15496 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15497 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15498 	},
15499 };
15500 
15501 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15502 			       struct net_device *dev, u32 filter_mask,
15503 			       int nlflags)
15504 {
15505 	struct bnxt *bp = netdev_priv(dev);
15506 
15507 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15508 				       nlflags, filter_mask, NULL);
15509 }
15510 
15511 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15512 			       u16 flags, struct netlink_ext_ack *extack)
15513 {
15514 	struct bnxt *bp = netdev_priv(dev);
15515 	struct nlattr *attr, *br_spec;
15516 	int rem, rc = 0;
15517 
15518 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15519 		return -EOPNOTSUPP;
15520 
15521 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15522 	if (!br_spec)
15523 		return -EINVAL;
15524 
15525 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15526 		u16 mode;
15527 
15528 		mode = nla_get_u16(attr);
15529 		if (mode == bp->br_mode)
15530 			break;
15531 
15532 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15533 		if (!rc)
15534 			bp->br_mode = mode;
15535 		break;
15536 	}
15537 	return rc;
15538 }
15539 
15540 int bnxt_get_port_parent_id(struct net_device *dev,
15541 			    struct netdev_phys_item_id *ppid)
15542 {
15543 	struct bnxt *bp = netdev_priv(dev);
15544 
15545 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15546 		return -EOPNOTSUPP;
15547 
15548 	/* The PF and it's VF-reps only support the switchdev framework */
15549 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15550 		return -EOPNOTSUPP;
15551 
15552 	ppid->id_len = sizeof(bp->dsn);
15553 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15554 
15555 	return 0;
15556 }
15557 
15558 static const struct net_device_ops bnxt_netdev_ops = {
15559 	.ndo_open		= bnxt_open,
15560 	.ndo_start_xmit		= bnxt_start_xmit,
15561 	.ndo_stop		= bnxt_close,
15562 	.ndo_get_stats64	= bnxt_get_stats64,
15563 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15564 	.ndo_eth_ioctl		= bnxt_ioctl,
15565 	.ndo_validate_addr	= eth_validate_addr,
15566 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15567 	.ndo_change_mtu		= bnxt_change_mtu,
15568 	.ndo_fix_features	= bnxt_fix_features,
15569 	.ndo_set_features	= bnxt_set_features,
15570 	.ndo_features_check	= bnxt_features_check,
15571 	.ndo_tx_timeout		= bnxt_tx_timeout,
15572 #ifdef CONFIG_BNXT_SRIOV
15573 	.ndo_get_vf_config	= bnxt_get_vf_config,
15574 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15575 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15576 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15577 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15578 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15579 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15580 #endif
15581 	.ndo_setup_tc           = bnxt_setup_tc,
15582 #ifdef CONFIG_RFS_ACCEL
15583 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15584 #endif
15585 	.ndo_bpf		= bnxt_xdp,
15586 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15587 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15588 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15589 };
15590 
15591 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15592 				    struct netdev_queue_stats_rx *stats)
15593 {
15594 	struct bnxt *bp = netdev_priv(dev);
15595 	struct bnxt_cp_ring_info *cpr;
15596 	u64 *sw;
15597 
15598 	cpr = &bp->bnapi[i]->cp_ring;
15599 	sw = cpr->stats.sw_stats;
15600 
15601 	stats->packets = 0;
15602 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15603 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15604 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15605 
15606 	stats->bytes = 0;
15607 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15608 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15609 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15610 
15611 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15612 }
15613 
15614 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15615 				    struct netdev_queue_stats_tx *stats)
15616 {
15617 	struct bnxt *bp = netdev_priv(dev);
15618 	struct bnxt_napi *bnapi;
15619 	u64 *sw;
15620 
15621 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15622 	sw = bnapi->cp_ring.stats.sw_stats;
15623 
15624 	stats->packets = 0;
15625 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15626 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15627 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15628 
15629 	stats->bytes = 0;
15630 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15631 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15632 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15633 }
15634 
15635 static void bnxt_get_base_stats(struct net_device *dev,
15636 				struct netdev_queue_stats_rx *rx,
15637 				struct netdev_queue_stats_tx *tx)
15638 {
15639 	struct bnxt *bp = netdev_priv(dev);
15640 
15641 	rx->packets = bp->net_stats_prev.rx_packets;
15642 	rx->bytes = bp->net_stats_prev.rx_bytes;
15643 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15644 
15645 	tx->packets = bp->net_stats_prev.tx_packets;
15646 	tx->bytes = bp->net_stats_prev.tx_bytes;
15647 }
15648 
15649 static const struct netdev_stat_ops bnxt_stat_ops = {
15650 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15651 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15652 	.get_base_stats		= bnxt_get_base_stats,
15653 };
15654 
15655 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15656 {
15657 	struct bnxt_rx_ring_info *rxr, *clone;
15658 	struct bnxt *bp = netdev_priv(dev);
15659 	struct bnxt_ring_struct *ring;
15660 	int rc;
15661 
15662 	rxr = &bp->rx_ring[idx];
15663 	clone = qmem;
15664 	memcpy(clone, rxr, sizeof(*rxr));
15665 	bnxt_init_rx_ring_struct(bp, clone);
15666 	bnxt_reset_rx_ring_struct(bp, clone);
15667 
15668 	clone->rx_prod = 0;
15669 	clone->rx_agg_prod = 0;
15670 	clone->rx_sw_agg_prod = 0;
15671 	clone->rx_next_cons = 0;
15672 
15673 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15674 	if (rc)
15675 		return rc;
15676 
15677 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15678 	if (rc < 0)
15679 		goto err_page_pool_destroy;
15680 
15681 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15682 					MEM_TYPE_PAGE_POOL,
15683 					clone->page_pool);
15684 	if (rc)
15685 		goto err_rxq_info_unreg;
15686 
15687 	ring = &clone->rx_ring_struct;
15688 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15689 	if (rc)
15690 		goto err_free_rx_ring;
15691 
15692 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15693 		ring = &clone->rx_agg_ring_struct;
15694 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15695 		if (rc)
15696 			goto err_free_rx_agg_ring;
15697 
15698 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15699 		if (rc)
15700 			goto err_free_rx_agg_ring;
15701 	}
15702 
15703 	if (bp->flags & BNXT_FLAG_TPA) {
15704 		rc = bnxt_alloc_one_tpa_info(bp, clone);
15705 		if (rc)
15706 			goto err_free_tpa_info;
15707 	}
15708 
15709 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15710 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15711 
15712 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15713 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15714 		bnxt_alloc_one_rx_ring_page(bp, clone, idx);
15715 	if (bp->flags & BNXT_FLAG_TPA)
15716 		bnxt_alloc_one_tpa_info_data(bp, clone);
15717 
15718 	return 0;
15719 
15720 err_free_tpa_info:
15721 	bnxt_free_one_tpa_info(bp, clone);
15722 err_free_rx_agg_ring:
15723 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15724 err_free_rx_ring:
15725 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15726 err_rxq_info_unreg:
15727 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15728 err_page_pool_destroy:
15729 	page_pool_destroy(clone->page_pool);
15730 	if (bnxt_separate_head_pool())
15731 		page_pool_destroy(clone->head_pool);
15732 	clone->page_pool = NULL;
15733 	clone->head_pool = NULL;
15734 	return rc;
15735 }
15736 
15737 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15738 {
15739 	struct bnxt_rx_ring_info *rxr = qmem;
15740 	struct bnxt *bp = netdev_priv(dev);
15741 	struct bnxt_ring_struct *ring;
15742 
15743 	bnxt_free_one_rx_ring_skbs(bp, rxr);
15744 
15745 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15746 
15747 	page_pool_destroy(rxr->page_pool);
15748 	if (bnxt_separate_head_pool())
15749 		page_pool_destroy(rxr->head_pool);
15750 	rxr->page_pool = NULL;
15751 	rxr->head_pool = NULL;
15752 
15753 	ring = &rxr->rx_ring_struct;
15754 	bnxt_free_ring(bp, &ring->ring_mem);
15755 
15756 	ring = &rxr->rx_agg_ring_struct;
15757 	bnxt_free_ring(bp, &ring->ring_mem);
15758 
15759 	kfree(rxr->rx_agg_bmap);
15760 	rxr->rx_agg_bmap = NULL;
15761 }
15762 
15763 static void bnxt_copy_rx_ring(struct bnxt *bp,
15764 			      struct bnxt_rx_ring_info *dst,
15765 			      struct bnxt_rx_ring_info *src)
15766 {
15767 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15768 	struct bnxt_ring_struct *dst_ring, *src_ring;
15769 	int i;
15770 
15771 	dst_ring = &dst->rx_ring_struct;
15772 	dst_rmem = &dst_ring->ring_mem;
15773 	src_ring = &src->rx_ring_struct;
15774 	src_rmem = &src_ring->ring_mem;
15775 
15776 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15777 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15778 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15779 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15780 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15781 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15782 
15783 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15784 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15785 	*dst_rmem->vmem = *src_rmem->vmem;
15786 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15787 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15788 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15789 	}
15790 
15791 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15792 		return;
15793 
15794 	dst_ring = &dst->rx_agg_ring_struct;
15795 	dst_rmem = &dst_ring->ring_mem;
15796 	src_ring = &src->rx_agg_ring_struct;
15797 	src_rmem = &src_ring->ring_mem;
15798 
15799 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15800 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15801 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15802 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15803 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15804 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15805 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15806 
15807 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15808 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15809 	*dst_rmem->vmem = *src_rmem->vmem;
15810 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15811 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15812 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15813 	}
15814 
15815 	dst->rx_agg_bmap = src->rx_agg_bmap;
15816 }
15817 
15818 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15819 {
15820 	struct bnxt *bp = netdev_priv(dev);
15821 	struct bnxt_rx_ring_info *rxr, *clone;
15822 	struct bnxt_cp_ring_info *cpr;
15823 	struct bnxt_vnic_info *vnic;
15824 	struct bnxt_napi *bnapi;
15825 	int i, rc;
15826 
15827 	rxr = &bp->rx_ring[idx];
15828 	clone = qmem;
15829 
15830 	rxr->rx_prod = clone->rx_prod;
15831 	rxr->rx_agg_prod = clone->rx_agg_prod;
15832 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
15833 	rxr->rx_next_cons = clone->rx_next_cons;
15834 	rxr->rx_tpa = clone->rx_tpa;
15835 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
15836 	rxr->page_pool = clone->page_pool;
15837 	rxr->head_pool = clone->head_pool;
15838 	rxr->xdp_rxq = clone->xdp_rxq;
15839 
15840 	bnxt_copy_rx_ring(bp, rxr, clone);
15841 
15842 	bnapi = rxr->bnapi;
15843 	cpr = &bnapi->cp_ring;
15844 
15845 	/* All rings have been reserved and previously allocated.
15846 	 * Reallocating with the same parameters should never fail.
15847 	 */
15848 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
15849 	if (rc)
15850 		goto err_reset;
15851 
15852 	if (bp->tph_mode) {
15853 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
15854 		if (rc)
15855 			goto err_reset;
15856 	}
15857 
15858 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
15859 	if (rc)
15860 		goto err_reset;
15861 
15862 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
15863 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15864 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
15865 
15866 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
15867 		rc = bnxt_tx_queue_start(bp, idx);
15868 		if (rc)
15869 			goto err_reset;
15870 	}
15871 
15872 	napi_enable(&bnapi->napi);
15873 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
15874 
15875 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15876 		vnic = &bp->vnic_info[i];
15877 
15878 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
15879 		if (rc) {
15880 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
15881 				   vnic->vnic_id, rc);
15882 			return rc;
15883 		}
15884 		vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
15885 		bnxt_hwrm_vnic_update(bp, vnic,
15886 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15887 	}
15888 
15889 	return 0;
15890 
15891 err_reset:
15892 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
15893 		   rc);
15894 	napi_enable(&bnapi->napi);
15895 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
15896 	bnxt_reset_task(bp, true);
15897 	return rc;
15898 }
15899 
15900 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
15901 {
15902 	struct bnxt *bp = netdev_priv(dev);
15903 	struct bnxt_rx_ring_info *rxr;
15904 	struct bnxt_cp_ring_info *cpr;
15905 	struct bnxt_vnic_info *vnic;
15906 	struct bnxt_napi *bnapi;
15907 	int i;
15908 
15909 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15910 		vnic = &bp->vnic_info[i];
15911 		vnic->mru = 0;
15912 		bnxt_hwrm_vnic_update(bp, vnic,
15913 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15914 	}
15915 	/* Make sure NAPI sees that the VNIC is disabled */
15916 	synchronize_net();
15917 	rxr = &bp->rx_ring[idx];
15918 	bnapi = rxr->bnapi;
15919 	cpr = &bnapi->cp_ring;
15920 	cancel_work_sync(&cpr->dim.work);
15921 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15922 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
15923 	page_pool_disable_direct_recycling(rxr->page_pool);
15924 	if (bnxt_separate_head_pool())
15925 		page_pool_disable_direct_recycling(rxr->head_pool);
15926 
15927 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15928 		bnxt_tx_queue_stop(bp, idx);
15929 
15930 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
15931 	 * completion is handled in NAPI to guarantee no more DMA on that ring
15932 	 * after seeing the completion.
15933 	 */
15934 	napi_disable(&bnapi->napi);
15935 
15936 	if (bp->tph_mode) {
15937 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
15938 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
15939 	}
15940 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
15941 
15942 	memcpy(qmem, rxr, sizeof(*rxr));
15943 	bnxt_init_rx_ring_struct(bp, qmem);
15944 
15945 	return 0;
15946 }
15947 
15948 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
15949 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
15950 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
15951 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
15952 	.ndo_queue_start	= bnxt_queue_start,
15953 	.ndo_queue_stop		= bnxt_queue_stop,
15954 };
15955 
15956 static void bnxt_remove_one(struct pci_dev *pdev)
15957 {
15958 	struct net_device *dev = pci_get_drvdata(pdev);
15959 	struct bnxt *bp = netdev_priv(dev);
15960 
15961 	if (BNXT_PF(bp))
15962 		bnxt_sriov_disable(bp);
15963 
15964 	bnxt_rdma_aux_device_del(bp);
15965 
15966 	bnxt_ptp_clear(bp);
15967 	unregister_netdev(dev);
15968 
15969 	bnxt_rdma_aux_device_uninit(bp);
15970 
15971 	bnxt_free_l2_filters(bp, true);
15972 	bnxt_free_ntp_fltrs(bp, true);
15973 	WARN_ON(bp->num_rss_ctx);
15974 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15975 	/* Flush any pending tasks */
15976 	cancel_work_sync(&bp->sp_task);
15977 	cancel_delayed_work_sync(&bp->fw_reset_task);
15978 	bp->sp_event = 0;
15979 
15980 	bnxt_dl_fw_reporters_destroy(bp);
15981 	bnxt_dl_unregister(bp);
15982 	bnxt_shutdown_tc(bp);
15983 
15984 	bnxt_clear_int_mode(bp);
15985 	bnxt_hwrm_func_drv_unrgtr(bp);
15986 	bnxt_free_hwrm_resources(bp);
15987 	bnxt_hwmon_uninit(bp);
15988 	bnxt_ethtool_free(bp);
15989 	bnxt_dcb_free(bp);
15990 	kfree(bp->ptp_cfg);
15991 	bp->ptp_cfg = NULL;
15992 	kfree(bp->fw_health);
15993 	bp->fw_health = NULL;
15994 	bnxt_cleanup_pci(bp);
15995 	bnxt_free_ctx_mem(bp, true);
15996 	bnxt_free_crash_dump_mem(bp);
15997 	kfree(bp->rss_indir_tbl);
15998 	bp->rss_indir_tbl = NULL;
15999 	bnxt_free_port_stats(bp);
16000 	free_netdev(dev);
16001 }
16002 
16003 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16004 {
16005 	int rc = 0;
16006 	struct bnxt_link_info *link_info = &bp->link_info;
16007 
16008 	bp->phy_flags = 0;
16009 	rc = bnxt_hwrm_phy_qcaps(bp);
16010 	if (rc) {
16011 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16012 			   rc);
16013 		return rc;
16014 	}
16015 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16016 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16017 	else
16018 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16019 
16020 	bp->mac_flags = 0;
16021 	bnxt_hwrm_mac_qcaps(bp);
16022 
16023 	if (!fw_dflt)
16024 		return 0;
16025 
16026 	mutex_lock(&bp->link_lock);
16027 	rc = bnxt_update_link(bp, false);
16028 	if (rc) {
16029 		mutex_unlock(&bp->link_lock);
16030 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16031 			   rc);
16032 		return rc;
16033 	}
16034 
16035 	/* Older firmware does not have supported_auto_speeds, so assume
16036 	 * that all supported speeds can be autonegotiated.
16037 	 */
16038 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16039 		link_info->support_auto_speeds = link_info->support_speeds;
16040 
16041 	bnxt_init_ethtool_link_settings(bp);
16042 	mutex_unlock(&bp->link_lock);
16043 	return 0;
16044 }
16045 
16046 static int bnxt_get_max_irq(struct pci_dev *pdev)
16047 {
16048 	u16 ctrl;
16049 
16050 	if (!pdev->msix_cap)
16051 		return 1;
16052 
16053 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16054 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16055 }
16056 
16057 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16058 				int *max_cp)
16059 {
16060 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16061 	int max_ring_grps = 0, max_irq;
16062 
16063 	*max_tx = hw_resc->max_tx_rings;
16064 	*max_rx = hw_resc->max_rx_rings;
16065 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16066 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16067 			bnxt_get_ulp_msix_num_in_use(bp),
16068 			hw_resc->max_stat_ctxs -
16069 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16070 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16071 		*max_cp = min_t(int, *max_cp, max_irq);
16072 	max_ring_grps = hw_resc->max_hw_ring_grps;
16073 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16074 		*max_cp -= 1;
16075 		*max_rx -= 2;
16076 	}
16077 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16078 		*max_rx >>= 1;
16079 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16080 		int rc;
16081 
16082 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16083 		if (rc) {
16084 			*max_rx = 0;
16085 			*max_tx = 0;
16086 		}
16087 		/* On P5 chips, max_cp output param should be available NQs */
16088 		*max_cp = max_irq;
16089 	}
16090 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16091 }
16092 
16093 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16094 {
16095 	int rx, tx, cp;
16096 
16097 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16098 	*max_rx = rx;
16099 	*max_tx = tx;
16100 	if (!rx || !tx || !cp)
16101 		return -ENOMEM;
16102 
16103 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16104 }
16105 
16106 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16107 			       bool shared)
16108 {
16109 	int rc;
16110 
16111 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16112 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16113 		/* Not enough rings, try disabling agg rings. */
16114 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16115 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16116 		if (rc) {
16117 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16118 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16119 			return rc;
16120 		}
16121 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16122 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16123 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16124 		bnxt_set_ring_params(bp);
16125 	}
16126 
16127 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16128 		int max_cp, max_stat, max_irq;
16129 
16130 		/* Reserve minimum resources for RoCE */
16131 		max_cp = bnxt_get_max_func_cp_rings(bp);
16132 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16133 		max_irq = bnxt_get_max_func_irqs(bp);
16134 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16135 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16136 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16137 			return 0;
16138 
16139 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16140 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16141 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16142 		max_cp = min_t(int, max_cp, max_irq);
16143 		max_cp = min_t(int, max_cp, max_stat);
16144 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16145 		if (rc)
16146 			rc = 0;
16147 	}
16148 	return rc;
16149 }
16150 
16151 /* In initial default shared ring setting, each shared ring must have a
16152  * RX/TX ring pair.
16153  */
16154 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16155 {
16156 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16157 	bp->rx_nr_rings = bp->cp_nr_rings;
16158 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16159 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
16160 }
16161 
16162 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16163 {
16164 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16165 	int avail_msix;
16166 
16167 	if (!bnxt_can_reserve_rings(bp))
16168 		return 0;
16169 
16170 	if (sh)
16171 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16172 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16173 	/* Reduce default rings on multi-port cards so that total default
16174 	 * rings do not exceed CPU count.
16175 	 */
16176 	if (bp->port_count > 1) {
16177 		int max_rings =
16178 			max_t(int, num_online_cpus() / bp->port_count, 1);
16179 
16180 		dflt_rings = min_t(int, dflt_rings, max_rings);
16181 	}
16182 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16183 	if (rc)
16184 		return rc;
16185 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16186 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16187 	if (sh)
16188 		bnxt_trim_dflt_sh_rings(bp);
16189 	else
16190 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16191 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
16192 
16193 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16194 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16195 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16196 
16197 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16198 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16199 	}
16200 
16201 	rc = __bnxt_reserve_rings(bp);
16202 	if (rc && rc != -ENODEV)
16203 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16204 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16205 	if (sh)
16206 		bnxt_trim_dflt_sh_rings(bp);
16207 
16208 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16209 	if (bnxt_need_reserve_rings(bp)) {
16210 		rc = __bnxt_reserve_rings(bp);
16211 		if (rc && rc != -ENODEV)
16212 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16213 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16214 	}
16215 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16216 		bp->rx_nr_rings++;
16217 		bp->cp_nr_rings++;
16218 	}
16219 	if (rc) {
16220 		bp->tx_nr_rings = 0;
16221 		bp->rx_nr_rings = 0;
16222 	}
16223 	return rc;
16224 }
16225 
16226 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16227 {
16228 	int rc;
16229 
16230 	if (bp->tx_nr_rings)
16231 		return 0;
16232 
16233 	bnxt_ulp_irq_stop(bp);
16234 	bnxt_clear_int_mode(bp);
16235 	rc = bnxt_set_dflt_rings(bp, true);
16236 	if (rc) {
16237 		if (BNXT_VF(bp) && rc == -ENODEV)
16238 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16239 		else
16240 			netdev_err(bp->dev, "Not enough rings available.\n");
16241 		goto init_dflt_ring_err;
16242 	}
16243 	rc = bnxt_init_int_mode(bp);
16244 	if (rc)
16245 		goto init_dflt_ring_err;
16246 
16247 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16248 
16249 	bnxt_set_dflt_rfs(bp);
16250 
16251 init_dflt_ring_err:
16252 	bnxt_ulp_irq_restart(bp, rc);
16253 	return rc;
16254 }
16255 
16256 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
16257 {
16258 	int rc;
16259 
16260 	ASSERT_RTNL();
16261 	bnxt_hwrm_func_qcaps(bp);
16262 
16263 	if (netif_running(bp->dev))
16264 		__bnxt_close_nic(bp, true, false);
16265 
16266 	bnxt_ulp_irq_stop(bp);
16267 	bnxt_clear_int_mode(bp);
16268 	rc = bnxt_init_int_mode(bp);
16269 	bnxt_ulp_irq_restart(bp, rc);
16270 
16271 	if (netif_running(bp->dev)) {
16272 		if (rc)
16273 			dev_close(bp->dev);
16274 		else
16275 			rc = bnxt_open_nic(bp, true, false);
16276 	}
16277 
16278 	return rc;
16279 }
16280 
16281 static int bnxt_init_mac_addr(struct bnxt *bp)
16282 {
16283 	int rc = 0;
16284 
16285 	if (BNXT_PF(bp)) {
16286 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16287 	} else {
16288 #ifdef CONFIG_BNXT_SRIOV
16289 		struct bnxt_vf_info *vf = &bp->vf;
16290 		bool strict_approval = true;
16291 
16292 		if (is_valid_ether_addr(vf->mac_addr)) {
16293 			/* overwrite netdev dev_addr with admin VF MAC */
16294 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16295 			/* Older PF driver or firmware may not approve this
16296 			 * correctly.
16297 			 */
16298 			strict_approval = false;
16299 		} else {
16300 			eth_hw_addr_random(bp->dev);
16301 		}
16302 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16303 #endif
16304 	}
16305 	return rc;
16306 }
16307 
16308 static void bnxt_vpd_read_info(struct bnxt *bp)
16309 {
16310 	struct pci_dev *pdev = bp->pdev;
16311 	unsigned int vpd_size, kw_len;
16312 	int pos, size;
16313 	u8 *vpd_data;
16314 
16315 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16316 	if (IS_ERR(vpd_data)) {
16317 		pci_warn(pdev, "Unable to read VPD\n");
16318 		return;
16319 	}
16320 
16321 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16322 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16323 	if (pos < 0)
16324 		goto read_sn;
16325 
16326 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16327 	memcpy(bp->board_partno, &vpd_data[pos], size);
16328 
16329 read_sn:
16330 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16331 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16332 					   &kw_len);
16333 	if (pos < 0)
16334 		goto exit;
16335 
16336 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16337 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16338 exit:
16339 	kfree(vpd_data);
16340 }
16341 
16342 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16343 {
16344 	struct pci_dev *pdev = bp->pdev;
16345 	u64 qword;
16346 
16347 	qword = pci_get_dsn(pdev);
16348 	if (!qword) {
16349 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16350 		return -EOPNOTSUPP;
16351 	}
16352 
16353 	put_unaligned_le64(qword, dsn);
16354 
16355 	bp->flags |= BNXT_FLAG_DSN_VALID;
16356 	return 0;
16357 }
16358 
16359 static int bnxt_map_db_bar(struct bnxt *bp)
16360 {
16361 	if (!bp->db_size)
16362 		return -ENODEV;
16363 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16364 	if (!bp->bar1)
16365 		return -ENOMEM;
16366 	return 0;
16367 }
16368 
16369 void bnxt_print_device_info(struct bnxt *bp)
16370 {
16371 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16372 		    board_info[bp->board_idx].name,
16373 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16374 
16375 	pcie_print_link_status(bp->pdev);
16376 }
16377 
16378 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16379 {
16380 	struct bnxt_hw_resc *hw_resc;
16381 	struct net_device *dev;
16382 	struct bnxt *bp;
16383 	int rc, max_irqs;
16384 
16385 	if (pci_is_bridge(pdev))
16386 		return -ENODEV;
16387 
16388 	if (!pdev->msix_cap) {
16389 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16390 		return -ENODEV;
16391 	}
16392 
16393 	/* Clear any pending DMA transactions from crash kernel
16394 	 * while loading driver in capture kernel.
16395 	 */
16396 	if (is_kdump_kernel()) {
16397 		pci_clear_master(pdev);
16398 		pcie_flr(pdev);
16399 	}
16400 
16401 	max_irqs = bnxt_get_max_irq(pdev);
16402 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16403 				 max_irqs);
16404 	if (!dev)
16405 		return -ENOMEM;
16406 
16407 	bp = netdev_priv(dev);
16408 	bp->board_idx = ent->driver_data;
16409 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16410 	bnxt_set_max_func_irqs(bp, max_irqs);
16411 
16412 	if (bnxt_vf_pciid(bp->board_idx))
16413 		bp->flags |= BNXT_FLAG_VF;
16414 
16415 	/* No devlink port registration in case of a VF */
16416 	if (BNXT_PF(bp))
16417 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16418 
16419 	rc = bnxt_init_board(pdev, dev);
16420 	if (rc < 0)
16421 		goto init_err_free;
16422 
16423 	dev->netdev_ops = &bnxt_netdev_ops;
16424 	dev->stat_ops = &bnxt_stat_ops;
16425 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16426 	dev->ethtool_ops = &bnxt_ethtool_ops;
16427 	pci_set_drvdata(pdev, dev);
16428 
16429 	rc = bnxt_alloc_hwrm_resources(bp);
16430 	if (rc)
16431 		goto init_err_pci_clean;
16432 
16433 	mutex_init(&bp->hwrm_cmd_lock);
16434 	mutex_init(&bp->link_lock);
16435 
16436 	rc = bnxt_fw_init_one_p1(bp);
16437 	if (rc)
16438 		goto init_err_pci_clean;
16439 
16440 	if (BNXT_PF(bp))
16441 		bnxt_vpd_read_info(bp);
16442 
16443 	if (BNXT_CHIP_P5_PLUS(bp)) {
16444 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16445 		if (BNXT_CHIP_P7(bp))
16446 			bp->flags |= BNXT_FLAG_CHIP_P7;
16447 	}
16448 
16449 	rc = bnxt_alloc_rss_indir_tbl(bp);
16450 	if (rc)
16451 		goto init_err_pci_clean;
16452 
16453 	rc = bnxt_fw_init_one_p2(bp);
16454 	if (rc)
16455 		goto init_err_pci_clean;
16456 
16457 	rc = bnxt_map_db_bar(bp);
16458 	if (rc) {
16459 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16460 			rc);
16461 		goto init_err_pci_clean;
16462 	}
16463 
16464 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16465 			   NETIF_F_TSO | NETIF_F_TSO6 |
16466 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16467 			   NETIF_F_GSO_IPXIP4 |
16468 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16469 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16470 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16471 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16472 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16473 
16474 	if (BNXT_SUPPORTS_TPA(bp))
16475 		dev->hw_features |= NETIF_F_LRO;
16476 
16477 	dev->hw_enc_features =
16478 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16479 			NETIF_F_TSO | NETIF_F_TSO6 |
16480 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16481 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16482 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16483 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16484 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16485 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16486 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16487 	else
16488 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16489 
16490 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16491 				    NETIF_F_GSO_GRE_CSUM;
16492 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16493 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16494 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16495 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16496 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16497 	if (BNXT_SUPPORTS_TPA(bp))
16498 		dev->hw_features |= NETIF_F_GRO_HW;
16499 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16500 	if (dev->features & NETIF_F_GRO_HW)
16501 		dev->features &= ~NETIF_F_LRO;
16502 	dev->priv_flags |= IFF_UNICAST_FLT;
16503 
16504 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16505 	if (bp->tso_max_segs)
16506 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16507 
16508 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16509 			    NETDEV_XDP_ACT_RX_SG;
16510 
16511 #ifdef CONFIG_BNXT_SRIOV
16512 	init_waitqueue_head(&bp->sriov_cfg_wait);
16513 #endif
16514 	if (BNXT_SUPPORTS_TPA(bp)) {
16515 		bp->gro_func = bnxt_gro_func_5730x;
16516 		if (BNXT_CHIP_P4(bp))
16517 			bp->gro_func = bnxt_gro_func_5731x;
16518 		else if (BNXT_CHIP_P5_PLUS(bp))
16519 			bp->gro_func = bnxt_gro_func_5750x;
16520 	}
16521 	if (!BNXT_CHIP_P4_PLUS(bp))
16522 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16523 
16524 	rc = bnxt_init_mac_addr(bp);
16525 	if (rc) {
16526 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16527 		rc = -EADDRNOTAVAIL;
16528 		goto init_err_pci_clean;
16529 	}
16530 
16531 	if (BNXT_PF(bp)) {
16532 		/* Read the adapter's DSN to use as the eswitch switch_id */
16533 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16534 	}
16535 
16536 	/* MTU range: 60 - FW defined max */
16537 	dev->min_mtu = ETH_ZLEN;
16538 	dev->max_mtu = bp->max_mtu;
16539 
16540 	rc = bnxt_probe_phy(bp, true);
16541 	if (rc)
16542 		goto init_err_pci_clean;
16543 
16544 	hw_resc = &bp->hw_resc;
16545 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16546 		       BNXT_L2_FLTR_MAX_FLTR;
16547 	/* Older firmware may not report these filters properly */
16548 	if (bp->max_fltr < BNXT_MAX_FLTR)
16549 		bp->max_fltr = BNXT_MAX_FLTR;
16550 	bnxt_init_l2_fltr_tbl(bp);
16551 	__bnxt_set_rx_skb_mode(bp, false);
16552 	bnxt_set_tpa_flags(bp);
16553 	bnxt_init_ring_params(bp);
16554 	bnxt_set_ring_params(bp);
16555 	bnxt_rdma_aux_device_init(bp);
16556 	rc = bnxt_set_dflt_rings(bp, true);
16557 	if (rc) {
16558 		if (BNXT_VF(bp) && rc == -ENODEV) {
16559 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16560 		} else {
16561 			netdev_err(bp->dev, "Not enough rings available.\n");
16562 			rc = -ENOMEM;
16563 		}
16564 		goto init_err_pci_clean;
16565 	}
16566 
16567 	bnxt_fw_init_one_p3(bp);
16568 
16569 	bnxt_init_dflt_coal(bp);
16570 
16571 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16572 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16573 
16574 	rc = bnxt_init_int_mode(bp);
16575 	if (rc)
16576 		goto init_err_pci_clean;
16577 
16578 	/* No TC has been set yet and rings may have been trimmed due to
16579 	 * limited MSIX, so we re-initialize the TX rings per TC.
16580 	 */
16581 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16582 
16583 	if (BNXT_PF(bp)) {
16584 		if (!bnxt_pf_wq) {
16585 			bnxt_pf_wq =
16586 				create_singlethread_workqueue("bnxt_pf_wq");
16587 			if (!bnxt_pf_wq) {
16588 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16589 				rc = -ENOMEM;
16590 				goto init_err_pci_clean;
16591 			}
16592 		}
16593 		rc = bnxt_init_tc(bp);
16594 		if (rc)
16595 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16596 				   rc);
16597 	}
16598 
16599 	bnxt_inv_fw_health_reg(bp);
16600 	rc = bnxt_dl_register(bp);
16601 	if (rc)
16602 		goto init_err_dl;
16603 
16604 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16605 
16606 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16607 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16608 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16609 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16610 
16611 	rc = register_netdev(dev);
16612 	if (rc)
16613 		goto init_err_cleanup;
16614 
16615 	bnxt_dl_fw_reporters_create(bp);
16616 
16617 	bnxt_rdma_aux_device_add(bp);
16618 
16619 	bnxt_print_device_info(bp);
16620 
16621 	pci_save_state(pdev);
16622 
16623 	return 0;
16624 init_err_cleanup:
16625 	bnxt_rdma_aux_device_uninit(bp);
16626 	bnxt_dl_unregister(bp);
16627 init_err_dl:
16628 	bnxt_shutdown_tc(bp);
16629 	bnxt_clear_int_mode(bp);
16630 
16631 init_err_pci_clean:
16632 	bnxt_hwrm_func_drv_unrgtr(bp);
16633 	bnxt_free_hwrm_resources(bp);
16634 	bnxt_hwmon_uninit(bp);
16635 	bnxt_ethtool_free(bp);
16636 	bnxt_ptp_clear(bp);
16637 	kfree(bp->ptp_cfg);
16638 	bp->ptp_cfg = NULL;
16639 	kfree(bp->fw_health);
16640 	bp->fw_health = NULL;
16641 	bnxt_cleanup_pci(bp);
16642 	bnxt_free_ctx_mem(bp, true);
16643 	bnxt_free_crash_dump_mem(bp);
16644 	kfree(bp->rss_indir_tbl);
16645 	bp->rss_indir_tbl = NULL;
16646 
16647 init_err_free:
16648 	free_netdev(dev);
16649 	return rc;
16650 }
16651 
16652 static void bnxt_shutdown(struct pci_dev *pdev)
16653 {
16654 	struct net_device *dev = pci_get_drvdata(pdev);
16655 	struct bnxt *bp;
16656 
16657 	if (!dev)
16658 		return;
16659 
16660 	rtnl_lock();
16661 	bp = netdev_priv(dev);
16662 	if (!bp)
16663 		goto shutdown_exit;
16664 
16665 	if (netif_running(dev))
16666 		dev_close(dev);
16667 
16668 	bnxt_ptp_clear(bp);
16669 	bnxt_clear_int_mode(bp);
16670 	pci_disable_device(pdev);
16671 
16672 	if (system_state == SYSTEM_POWER_OFF) {
16673 		pci_wake_from_d3(pdev, bp->wol);
16674 		pci_set_power_state(pdev, PCI_D3hot);
16675 	}
16676 
16677 shutdown_exit:
16678 	rtnl_unlock();
16679 }
16680 
16681 #ifdef CONFIG_PM_SLEEP
16682 static int bnxt_suspend(struct device *device)
16683 {
16684 	struct net_device *dev = dev_get_drvdata(device);
16685 	struct bnxt *bp = netdev_priv(dev);
16686 	int rc = 0;
16687 
16688 	bnxt_ulp_stop(bp);
16689 
16690 	rtnl_lock();
16691 	if (netif_running(dev)) {
16692 		netif_device_detach(dev);
16693 		rc = bnxt_close(dev);
16694 	}
16695 	bnxt_hwrm_func_drv_unrgtr(bp);
16696 	bnxt_ptp_clear(bp);
16697 	pci_disable_device(bp->pdev);
16698 	bnxt_free_ctx_mem(bp, false);
16699 	rtnl_unlock();
16700 	return rc;
16701 }
16702 
16703 static int bnxt_resume(struct device *device)
16704 {
16705 	struct net_device *dev = dev_get_drvdata(device);
16706 	struct bnxt *bp = netdev_priv(dev);
16707 	int rc = 0;
16708 
16709 	rtnl_lock();
16710 	rc = pci_enable_device(bp->pdev);
16711 	if (rc) {
16712 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16713 			   rc);
16714 		goto resume_exit;
16715 	}
16716 	pci_set_master(bp->pdev);
16717 	if (bnxt_hwrm_ver_get(bp)) {
16718 		rc = -ENODEV;
16719 		goto resume_exit;
16720 	}
16721 	rc = bnxt_hwrm_func_reset(bp);
16722 	if (rc) {
16723 		rc = -EBUSY;
16724 		goto resume_exit;
16725 	}
16726 
16727 	rc = bnxt_hwrm_func_qcaps(bp);
16728 	if (rc)
16729 		goto resume_exit;
16730 
16731 	bnxt_clear_reservations(bp, true);
16732 
16733 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16734 		rc = -ENODEV;
16735 		goto resume_exit;
16736 	}
16737 	if (bp->fw_crash_mem)
16738 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16739 
16740 	if (bnxt_ptp_init(bp)) {
16741 		kfree(bp->ptp_cfg);
16742 		bp->ptp_cfg = NULL;
16743 	}
16744 	bnxt_get_wol_settings(bp);
16745 	if (netif_running(dev)) {
16746 		rc = bnxt_open(dev);
16747 		if (!rc)
16748 			netif_device_attach(dev);
16749 	}
16750 
16751 resume_exit:
16752 	rtnl_unlock();
16753 	bnxt_ulp_start(bp, rc);
16754 	if (!rc)
16755 		bnxt_reenable_sriov(bp);
16756 	return rc;
16757 }
16758 
16759 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16760 #define BNXT_PM_OPS (&bnxt_pm_ops)
16761 
16762 #else
16763 
16764 #define BNXT_PM_OPS NULL
16765 
16766 #endif /* CONFIG_PM_SLEEP */
16767 
16768 /**
16769  * bnxt_io_error_detected - called when PCI error is detected
16770  * @pdev: Pointer to PCI device
16771  * @state: The current pci connection state
16772  *
16773  * This function is called after a PCI bus error affecting
16774  * this device has been detected.
16775  */
16776 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16777 					       pci_channel_state_t state)
16778 {
16779 	struct net_device *netdev = pci_get_drvdata(pdev);
16780 	struct bnxt *bp = netdev_priv(netdev);
16781 	bool abort = false;
16782 
16783 	netdev_info(netdev, "PCI I/O error detected\n");
16784 
16785 	bnxt_ulp_stop(bp);
16786 
16787 	rtnl_lock();
16788 	netif_device_detach(netdev);
16789 
16790 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16791 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16792 		abort = true;
16793 	}
16794 
16795 	if (abort || state == pci_channel_io_perm_failure) {
16796 		rtnl_unlock();
16797 		return PCI_ERS_RESULT_DISCONNECT;
16798 	}
16799 
16800 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16801 	 * so we disable bus master to prevent any potential bad DMAs before
16802 	 * freeing kernel memory.
16803 	 */
16804 	if (state == pci_channel_io_frozen) {
16805 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16806 		bnxt_fw_fatal_close(bp);
16807 	}
16808 
16809 	if (netif_running(netdev))
16810 		__bnxt_close_nic(bp, true, true);
16811 
16812 	if (pci_is_enabled(pdev))
16813 		pci_disable_device(pdev);
16814 	bnxt_free_ctx_mem(bp, false);
16815 	rtnl_unlock();
16816 
16817 	/* Request a slot slot reset. */
16818 	return PCI_ERS_RESULT_NEED_RESET;
16819 }
16820 
16821 /**
16822  * bnxt_io_slot_reset - called after the pci bus has been reset.
16823  * @pdev: Pointer to PCI device
16824  *
16825  * Restart the card from scratch, as if from a cold-boot.
16826  * At this point, the card has experienced a hard reset,
16827  * followed by fixups by BIOS, and has its config space
16828  * set up identically to what it was at cold boot.
16829  */
16830 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
16831 {
16832 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
16833 	struct net_device *netdev = pci_get_drvdata(pdev);
16834 	struct bnxt *bp = netdev_priv(netdev);
16835 	int retry = 0;
16836 	int err = 0;
16837 	int off;
16838 
16839 	netdev_info(bp->dev, "PCI Slot Reset\n");
16840 
16841 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16842 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
16843 		msleep(900);
16844 
16845 	rtnl_lock();
16846 
16847 	if (pci_enable_device(pdev)) {
16848 		dev_err(&pdev->dev,
16849 			"Cannot re-enable PCI device after reset.\n");
16850 	} else {
16851 		pci_set_master(pdev);
16852 		/* Upon fatal error, our device internal logic that latches to
16853 		 * BAR value is getting reset and will restore only upon
16854 		 * rewriting the BARs.
16855 		 *
16856 		 * As pci_restore_state() does not re-write the BARs if the
16857 		 * value is same as saved value earlier, driver needs to
16858 		 * write the BARs to 0 to force restore, in case of fatal error.
16859 		 */
16860 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
16861 				       &bp->state)) {
16862 			for (off = PCI_BASE_ADDRESS_0;
16863 			     off <= PCI_BASE_ADDRESS_5; off += 4)
16864 				pci_write_config_dword(bp->pdev, off, 0);
16865 		}
16866 		pci_restore_state(pdev);
16867 		pci_save_state(pdev);
16868 
16869 		bnxt_inv_fw_health_reg(bp);
16870 		bnxt_try_map_fw_health_reg(bp);
16871 
16872 		/* In some PCIe AER scenarios, firmware may take up to
16873 		 * 10 seconds to become ready in the worst case.
16874 		 */
16875 		do {
16876 			err = bnxt_try_recover_fw(bp);
16877 			if (!err)
16878 				break;
16879 			retry++;
16880 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
16881 
16882 		if (err) {
16883 			dev_err(&pdev->dev, "Firmware not ready\n");
16884 			goto reset_exit;
16885 		}
16886 
16887 		err = bnxt_hwrm_func_reset(bp);
16888 		if (!err)
16889 			result = PCI_ERS_RESULT_RECOVERED;
16890 
16891 		bnxt_ulp_irq_stop(bp);
16892 		bnxt_clear_int_mode(bp);
16893 		err = bnxt_init_int_mode(bp);
16894 		bnxt_ulp_irq_restart(bp, err);
16895 	}
16896 
16897 reset_exit:
16898 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16899 	bnxt_clear_reservations(bp, true);
16900 	rtnl_unlock();
16901 
16902 	return result;
16903 }
16904 
16905 /**
16906  * bnxt_io_resume - called when traffic can start flowing again.
16907  * @pdev: Pointer to PCI device
16908  *
16909  * This callback is called when the error recovery driver tells
16910  * us that its OK to resume normal operation.
16911  */
16912 static void bnxt_io_resume(struct pci_dev *pdev)
16913 {
16914 	struct net_device *netdev = pci_get_drvdata(pdev);
16915 	struct bnxt *bp = netdev_priv(netdev);
16916 	int err;
16917 
16918 	netdev_info(bp->dev, "PCI Slot Resume\n");
16919 	rtnl_lock();
16920 
16921 	err = bnxt_hwrm_func_qcaps(bp);
16922 	if (!err) {
16923 		if (netif_running(netdev))
16924 			err = bnxt_open(netdev);
16925 		else
16926 			err = bnxt_reserve_rings(bp, true);
16927 	}
16928 
16929 	if (!err)
16930 		netif_device_attach(netdev);
16931 
16932 	rtnl_unlock();
16933 	bnxt_ulp_start(bp, err);
16934 	if (!err)
16935 		bnxt_reenable_sriov(bp);
16936 }
16937 
16938 static const struct pci_error_handlers bnxt_err_handler = {
16939 	.error_detected	= bnxt_io_error_detected,
16940 	.slot_reset	= bnxt_io_slot_reset,
16941 	.resume		= bnxt_io_resume
16942 };
16943 
16944 static struct pci_driver bnxt_pci_driver = {
16945 	.name		= DRV_MODULE_NAME,
16946 	.id_table	= bnxt_pci_tbl,
16947 	.probe		= bnxt_init_one,
16948 	.remove		= bnxt_remove_one,
16949 	.shutdown	= bnxt_shutdown,
16950 	.driver.pm	= BNXT_PM_OPS,
16951 	.err_handler	= &bnxt_err_handler,
16952 #if defined(CONFIG_BNXT_SRIOV)
16953 	.sriov_configure = bnxt_sriov_configure,
16954 #endif
16955 };
16956 
16957 static int __init bnxt_init(void)
16958 {
16959 	int err;
16960 
16961 	bnxt_debug_init();
16962 	err = pci_register_driver(&bnxt_pci_driver);
16963 	if (err) {
16964 		bnxt_debug_exit();
16965 		return err;
16966 	}
16967 
16968 	return 0;
16969 }
16970 
16971 static void __exit bnxt_exit(void)
16972 {
16973 	pci_unregister_driver(&bnxt_pci_driver);
16974 	if (bnxt_pf_wq)
16975 		destroy_workqueue(bnxt_pf_wq);
16976 	bnxt_debug_exit();
16977 }
16978 
16979 module_init(bnxt_init);
16980 module_exit(bnxt_exit);
16981