xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision bfb4a6c721517a11b277e8841f8a7a64b1b14b72)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_lock.h>
58 #include <net/netdev_queues.h>
59 #include <net/netdev_rx_queue.h>
60 #include <linux/pci-tph.h>
61 
62 #include "bnxt_hsi.h"
63 #include "bnxt.h"
64 #include "bnxt_hwrm.h"
65 #include "bnxt_ulp.h"
66 #include "bnxt_sriov.h"
67 #include "bnxt_ethtool.h"
68 #include "bnxt_dcb.h"
69 #include "bnxt_xdp.h"
70 #include "bnxt_ptp.h"
71 #include "bnxt_vfr.h"
72 #include "bnxt_tc.h"
73 #include "bnxt_devlink.h"
74 #include "bnxt_debugfs.h"
75 #include "bnxt_coredump.h"
76 #include "bnxt_hwmon.h"
77 
78 #define BNXT_TX_TIMEOUT		(5 * HZ)
79 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
80 				 NETIF_MSG_TX_ERR)
81 
82 MODULE_IMPORT_NS("NETDEV_INTERNAL");
83 MODULE_LICENSE("GPL");
84 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
85 
86 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
87 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
88 
89 #define BNXT_TX_PUSH_THRESH 164
90 
91 /* indexed by enum board_idx */
92 static const struct {
93 	char *name;
94 } board_info[] = {
95 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
96 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
97 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
98 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
99 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
100 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
101 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
102 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
103 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
104 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
105 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
106 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
108 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
109 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
110 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
111 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
112 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
113 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
114 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
115 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
116 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
117 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
118 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
119 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
120 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
121 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
122 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
123 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
124 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
125 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
126 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
127 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
128 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
129 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
130 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
131 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
132 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
133 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
134 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
135 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
136 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
137 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
138 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
139 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
140 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
141 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
142 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
143 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
144 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
145 };
146 
147 static const struct pci_device_id bnxt_pci_tbl[] = {
148 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
149 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
150 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
151 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
152 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
153 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
156 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
166 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
168 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
169 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
170 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
171 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
172 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
173 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
175 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
178 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
179 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
180 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
181 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
182 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
183 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
188 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
189 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
190 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
191 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
193 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
194 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
195 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
196 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
197 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
198 #ifdef CONFIG_BNXT_SRIOV
199 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
200 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
201 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
202 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
203 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
205 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
206 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
207 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
208 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
209 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
210 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
211 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
212 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
214 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
215 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
216 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
217 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
218 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
219 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
220 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
221 #endif
222 	{ 0 }
223 };
224 
225 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
226 
227 static const u16 bnxt_vf_req_snif[] = {
228 	HWRM_FUNC_CFG,
229 	HWRM_FUNC_VF_CFG,
230 	HWRM_PORT_PHY_QCFG,
231 	HWRM_CFA_L2_FILTER_ALLOC,
232 };
233 
234 static const u16 bnxt_async_events_arr[] = {
235 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
236 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
237 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
238 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
239 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
240 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
241 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
242 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
243 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
244 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
245 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
246 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
247 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
248 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
249 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
250 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
251 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
252 };
253 
254 const u16 bnxt_bstore_to_trace[] = {
255 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
256 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
257 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
258 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
259 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
260 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
261 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
262 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
263 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
264 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
265 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
266 };
267 
268 static struct workqueue_struct *bnxt_pf_wq;
269 
270 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
271 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
272 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
273 
274 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
275 	.ports = {
276 		.src = 0,
277 		.dst = 0,
278 	},
279 	.addrs = {
280 		.v6addrs = {
281 			.src = BNXT_IPV6_MASK_NONE,
282 			.dst = BNXT_IPV6_MASK_NONE,
283 		},
284 	},
285 };
286 
287 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
288 	.ports = {
289 		.src = cpu_to_be16(0xffff),
290 		.dst = cpu_to_be16(0xffff),
291 	},
292 	.addrs = {
293 		.v6addrs = {
294 			.src = BNXT_IPV6_MASK_ALL,
295 			.dst = BNXT_IPV6_MASK_ALL,
296 		},
297 	},
298 };
299 
300 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
301 	.ports = {
302 		.src = cpu_to_be16(0xffff),
303 		.dst = cpu_to_be16(0xffff),
304 	},
305 	.addrs = {
306 		.v4addrs = {
307 			.src = cpu_to_be32(0xffffffff),
308 			.dst = cpu_to_be32(0xffffffff),
309 		},
310 	},
311 };
312 
313 static bool bnxt_vf_pciid(enum board_idx idx)
314 {
315 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
316 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
317 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
318 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF);
319 }
320 
321 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
322 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
323 
324 #define BNXT_DB_CQ(db, idx)						\
325 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
326 
327 #define BNXT_DB_NQ_P5(db, idx)						\
328 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
329 		    (db)->doorbell)
330 
331 #define BNXT_DB_NQ_P7(db, idx)						\
332 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
333 		    DB_RING_IDX(db, idx), (db)->doorbell)
334 
335 #define BNXT_DB_CQ_ARM(db, idx)						\
336 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
337 
338 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
339 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
340 		    DB_RING_IDX(db, idx), (db)->doorbell)
341 
342 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
343 {
344 	if (bp->flags & BNXT_FLAG_CHIP_P7)
345 		BNXT_DB_NQ_P7(db, idx);
346 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
347 		BNXT_DB_NQ_P5(db, idx);
348 	else
349 		BNXT_DB_CQ(db, idx);
350 }
351 
352 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
353 {
354 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
355 		BNXT_DB_NQ_ARM_P5(db, idx);
356 	else
357 		BNXT_DB_CQ_ARM(db, idx);
358 }
359 
360 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
361 {
362 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
363 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
364 			    DB_RING_IDX(db, idx), db->doorbell);
365 	else
366 		BNXT_DB_CQ(db, idx);
367 }
368 
369 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
370 {
371 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
372 		return;
373 
374 	if (BNXT_PF(bp))
375 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
376 	else
377 		schedule_delayed_work(&bp->fw_reset_task, delay);
378 }
379 
380 static void __bnxt_queue_sp_work(struct bnxt *bp)
381 {
382 	if (BNXT_PF(bp))
383 		queue_work(bnxt_pf_wq, &bp->sp_task);
384 	else
385 		schedule_work(&bp->sp_task);
386 }
387 
388 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
389 {
390 	set_bit(event, &bp->sp_event);
391 	__bnxt_queue_sp_work(bp);
392 }
393 
394 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
395 {
396 	if (!rxr->bnapi->in_reset) {
397 		rxr->bnapi->in_reset = true;
398 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
399 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
400 		else
401 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
402 		__bnxt_queue_sp_work(bp);
403 	}
404 	rxr->rx_next_cons = 0xffff;
405 }
406 
407 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
408 			  u16 curr)
409 {
410 	struct bnxt_napi *bnapi = txr->bnapi;
411 
412 	if (bnapi->tx_fault)
413 		return;
414 
415 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
416 		   txr->txq_index, txr->tx_hw_cons,
417 		   txr->tx_cons, txr->tx_prod, curr);
418 	WARN_ON_ONCE(1);
419 	bnapi->tx_fault = 1;
420 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
421 }
422 
423 const u16 bnxt_lhint_arr[] = {
424 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
425 	TX_BD_FLAGS_LHINT_512_TO_1023,
426 	TX_BD_FLAGS_LHINT_1024_TO_2047,
427 	TX_BD_FLAGS_LHINT_1024_TO_2047,
428 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
429 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
430 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
431 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
432 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
433 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
434 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
435 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
436 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
437 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
438 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
439 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
440 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
441 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
442 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
443 };
444 
445 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
446 {
447 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
448 
449 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
450 		return 0;
451 
452 	return md_dst->u.port_info.port_id;
453 }
454 
455 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
456 			     u16 prod)
457 {
458 	/* Sync BD data before updating doorbell */
459 	wmb();
460 	bnxt_db_write(bp, &txr->tx_db, prod);
461 	txr->kick_pending = 0;
462 }
463 
464 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
465 {
466 	struct bnxt *bp = netdev_priv(dev);
467 	struct tx_bd *txbd, *txbd0;
468 	struct tx_bd_ext *txbd1;
469 	struct netdev_queue *txq;
470 	int i;
471 	dma_addr_t mapping;
472 	unsigned int length, pad = 0;
473 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
474 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
475 	struct pci_dev *pdev = bp->pdev;
476 	u16 prod, last_frag, txts_prod;
477 	struct bnxt_tx_ring_info *txr;
478 	struct bnxt_sw_tx_bd *tx_buf;
479 	__le32 lflags = 0;
480 	skb_frag_t *frag;
481 
482 	i = skb_get_queue_mapping(skb);
483 	if (unlikely(i >= bp->tx_nr_rings)) {
484 		dev_kfree_skb_any(skb);
485 		dev_core_stats_tx_dropped_inc(dev);
486 		return NETDEV_TX_OK;
487 	}
488 
489 	txq = netdev_get_tx_queue(dev, i);
490 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
491 	prod = txr->tx_prod;
492 
493 #if (MAX_SKB_FRAGS > TX_MAX_FRAGS)
494 	if (skb_shinfo(skb)->nr_frags > TX_MAX_FRAGS) {
495 		netdev_warn_once(dev, "SKB has too many (%d) fragments, max supported is %d.  SKB will be linearized.\n",
496 				 skb_shinfo(skb)->nr_frags, TX_MAX_FRAGS);
497 		if (skb_linearize(skb)) {
498 			dev_kfree_skb_any(skb);
499 			dev_core_stats_tx_dropped_inc(dev);
500 			return NETDEV_TX_OK;
501 		}
502 	}
503 #endif
504 	free_size = bnxt_tx_avail(bp, txr);
505 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
506 		/* We must have raced with NAPI cleanup */
507 		if (net_ratelimit() && txr->kick_pending)
508 			netif_warn(bp, tx_err, dev,
509 				   "bnxt: ring busy w/ flush pending!\n");
510 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
511 					bp->tx_wake_thresh))
512 			return NETDEV_TX_BUSY;
513 	}
514 
515 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
516 		goto tx_free;
517 
518 	length = skb->len;
519 	len = skb_headlen(skb);
520 	last_frag = skb_shinfo(skb)->nr_frags;
521 
522 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
523 
524 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
525 	tx_buf->skb = skb;
526 	tx_buf->nr_frags = last_frag;
527 
528 	vlan_tag_flags = 0;
529 	cfa_action = bnxt_xmit_get_cfa_action(skb);
530 	if (skb_vlan_tag_present(skb)) {
531 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
532 				 skb_vlan_tag_get(skb);
533 		/* Currently supports 8021Q, 8021AD vlan offloads
534 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
535 		 */
536 		if (skb->vlan_proto == htons(ETH_P_8021Q))
537 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
538 	}
539 
540 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
541 	    ptp->tx_tstamp_en) {
542 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
543 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
544 			tx_buf->is_ts_pkt = 1;
545 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
546 		} else if (!skb_is_gso(skb)) {
547 			u16 seq_id, hdr_off;
548 
549 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
550 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
551 				if (vlan_tag_flags)
552 					hdr_off += VLAN_HLEN;
553 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
554 				tx_buf->is_ts_pkt = 1;
555 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
556 
557 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
558 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
559 				tx_buf->txts_prod = txts_prod;
560 			}
561 		}
562 	}
563 	if (unlikely(skb->no_fcs))
564 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
565 
566 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
567 	    skb_frags_readable(skb) && !lflags) {
568 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
569 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
570 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
571 		void __iomem *db = txr->tx_db.doorbell;
572 		void *pdata = tx_push_buf->data;
573 		u64 *end;
574 		int j, push_len;
575 
576 		/* Set COAL_NOW to be ready quickly for the next push */
577 		tx_push->tx_bd_len_flags_type =
578 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
579 					TX_BD_TYPE_LONG_TX_BD |
580 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
581 					TX_BD_FLAGS_COAL_NOW |
582 					TX_BD_FLAGS_PACKET_END |
583 					TX_BD_CNT(2));
584 
585 		if (skb->ip_summed == CHECKSUM_PARTIAL)
586 			tx_push1->tx_bd_hsize_lflags =
587 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
588 		else
589 			tx_push1->tx_bd_hsize_lflags = 0;
590 
591 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
592 		tx_push1->tx_bd_cfa_action =
593 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
594 
595 		end = pdata + length;
596 		end = PTR_ALIGN(end, 8) - 1;
597 		*end = 0;
598 
599 		skb_copy_from_linear_data(skb, pdata, len);
600 		pdata += len;
601 		for (j = 0; j < last_frag; j++) {
602 			void *fptr;
603 
604 			frag = &skb_shinfo(skb)->frags[j];
605 			fptr = skb_frag_address_safe(frag);
606 			if (!fptr)
607 				goto normal_tx;
608 
609 			memcpy(pdata, fptr, skb_frag_size(frag));
610 			pdata += skb_frag_size(frag);
611 		}
612 
613 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
614 		txbd->tx_bd_haddr = txr->data_mapping;
615 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
616 		prod = NEXT_TX(prod);
617 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
618 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
619 		memcpy(txbd, tx_push1, sizeof(*txbd));
620 		prod = NEXT_TX(prod);
621 		tx_push->doorbell =
622 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
623 				    DB_RING_IDX(&txr->tx_db, prod));
624 		WRITE_ONCE(txr->tx_prod, prod);
625 
626 		tx_buf->is_push = 1;
627 		netdev_tx_sent_queue(txq, skb->len);
628 		wmb();	/* Sync is_push and byte queue before pushing data */
629 
630 		push_len = (length + sizeof(*tx_push) + 7) / 8;
631 		if (push_len > 16) {
632 			__iowrite64_copy(db, tx_push_buf, 16);
633 			__iowrite32_copy(db + 4, tx_push_buf + 1,
634 					 (push_len - 16) << 1);
635 		} else {
636 			__iowrite64_copy(db, tx_push_buf, push_len);
637 		}
638 
639 		goto tx_done;
640 	}
641 
642 normal_tx:
643 	if (length < BNXT_MIN_PKT_SIZE) {
644 		pad = BNXT_MIN_PKT_SIZE - length;
645 		if (skb_pad(skb, pad))
646 			/* SKB already freed. */
647 			goto tx_kick_pending;
648 		length = BNXT_MIN_PKT_SIZE;
649 	}
650 
651 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
652 
653 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
654 		goto tx_free;
655 
656 	dma_unmap_addr_set(tx_buf, mapping, mapping);
657 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
658 		TX_BD_CNT(last_frag + 2);
659 
660 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
661 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
662 
663 	prod = NEXT_TX(prod);
664 	txbd1 = (struct tx_bd_ext *)
665 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
666 
667 	txbd1->tx_bd_hsize_lflags = lflags;
668 	if (skb_is_gso(skb)) {
669 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
670 		u32 hdr_len;
671 
672 		if (skb->encapsulation) {
673 			if (udp_gso)
674 				hdr_len = skb_inner_transport_offset(skb) +
675 					  sizeof(struct udphdr);
676 			else
677 				hdr_len = skb_inner_tcp_all_headers(skb);
678 		} else if (udp_gso) {
679 			hdr_len = skb_transport_offset(skb) +
680 				  sizeof(struct udphdr);
681 		} else {
682 			hdr_len = skb_tcp_all_headers(skb);
683 		}
684 
685 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
686 					TX_BD_FLAGS_T_IPID |
687 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
688 		length = skb_shinfo(skb)->gso_size;
689 		txbd1->tx_bd_mss = cpu_to_le32(length);
690 		length += hdr_len;
691 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
692 		txbd1->tx_bd_hsize_lflags |=
693 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
694 		txbd1->tx_bd_mss = 0;
695 	}
696 
697 	length >>= 9;
698 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
699 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
700 				     skb->len);
701 		i = 0;
702 		goto tx_dma_error;
703 	}
704 	flags |= bnxt_lhint_arr[length];
705 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
706 
707 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
708 	txbd1->tx_bd_cfa_action =
709 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
710 	txbd0 = txbd;
711 	for (i = 0; i < last_frag; i++) {
712 		frag = &skb_shinfo(skb)->frags[i];
713 		prod = NEXT_TX(prod);
714 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
715 
716 		len = skb_frag_size(frag);
717 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
718 					   DMA_TO_DEVICE);
719 
720 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
721 			goto tx_dma_error;
722 
723 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
724 		netmem_dma_unmap_addr_set(skb_frag_netmem(frag), tx_buf,
725 					  mapping, mapping);
726 
727 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
728 
729 		flags = len << TX_BD_LEN_SHIFT;
730 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
731 	}
732 
733 	flags &= ~TX_BD_LEN;
734 	txbd->tx_bd_len_flags_type =
735 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
736 			    TX_BD_FLAGS_PACKET_END);
737 
738 	netdev_tx_sent_queue(txq, skb->len);
739 
740 	skb_tx_timestamp(skb);
741 
742 	prod = NEXT_TX(prod);
743 	WRITE_ONCE(txr->tx_prod, prod);
744 
745 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
746 		bnxt_txr_db_kick(bp, txr, prod);
747 	} else {
748 		if (free_size >= bp->tx_wake_thresh)
749 			txbd0->tx_bd_len_flags_type |=
750 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
751 		txr->kick_pending = 1;
752 	}
753 
754 tx_done:
755 
756 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
757 		if (netdev_xmit_more() && !tx_buf->is_push) {
758 			txbd0->tx_bd_len_flags_type &=
759 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
760 			bnxt_txr_db_kick(bp, txr, prod);
761 		}
762 
763 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
764 				   bp->tx_wake_thresh);
765 	}
766 	return NETDEV_TX_OK;
767 
768 tx_dma_error:
769 	last_frag = i;
770 
771 	/* start back at beginning and unmap skb */
772 	prod = txr->tx_prod;
773 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
774 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
775 			 skb_headlen(skb), DMA_TO_DEVICE);
776 	prod = NEXT_TX(prod);
777 
778 	/* unmap remaining mapped pages */
779 	for (i = 0; i < last_frag; i++) {
780 		prod = NEXT_TX(prod);
781 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
782 		frag = &skb_shinfo(skb)->frags[i];
783 		netmem_dma_unmap_page_attrs(&pdev->dev,
784 					    dma_unmap_addr(tx_buf, mapping),
785 					    skb_frag_size(frag),
786 					    DMA_TO_DEVICE, 0);
787 	}
788 
789 tx_free:
790 	dev_kfree_skb_any(skb);
791 tx_kick_pending:
792 	if (BNXT_TX_PTP_IS_SET(lflags)) {
793 		txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].is_ts_pkt = 0;
794 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
795 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
796 			/* set SKB to err so PTP worker will clean up */
797 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
798 	}
799 	if (txr->kick_pending)
800 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
801 	txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].skb = NULL;
802 	dev_core_stats_tx_dropped_inc(dev);
803 	return NETDEV_TX_OK;
804 }
805 
806 /* Returns true if some remaining TX packets not processed. */
807 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
808 			  int budget)
809 {
810 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
811 	struct pci_dev *pdev = bp->pdev;
812 	u16 hw_cons = txr->tx_hw_cons;
813 	unsigned int tx_bytes = 0;
814 	u16 cons = txr->tx_cons;
815 	skb_frag_t *frag;
816 	int tx_pkts = 0;
817 	bool rc = false;
818 
819 	while (RING_TX(bp, cons) != hw_cons) {
820 		struct bnxt_sw_tx_bd *tx_buf;
821 		struct sk_buff *skb;
822 		bool is_ts_pkt;
823 		int j, last;
824 
825 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
826 		skb = tx_buf->skb;
827 
828 		if (unlikely(!skb)) {
829 			bnxt_sched_reset_txr(bp, txr, cons);
830 			return rc;
831 		}
832 
833 		is_ts_pkt = tx_buf->is_ts_pkt;
834 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
835 			rc = true;
836 			break;
837 		}
838 
839 		cons = NEXT_TX(cons);
840 		tx_pkts++;
841 		tx_bytes += skb->len;
842 		tx_buf->skb = NULL;
843 		tx_buf->is_ts_pkt = 0;
844 
845 		if (tx_buf->is_push) {
846 			tx_buf->is_push = 0;
847 			goto next_tx_int;
848 		}
849 
850 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
851 				 skb_headlen(skb), DMA_TO_DEVICE);
852 		last = tx_buf->nr_frags;
853 
854 		for (j = 0; j < last; j++) {
855 			frag = &skb_shinfo(skb)->frags[j];
856 			cons = NEXT_TX(cons);
857 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
858 			netmem_dma_unmap_page_attrs(&pdev->dev,
859 						    dma_unmap_addr(tx_buf,
860 								   mapping),
861 						    skb_frag_size(frag),
862 						    DMA_TO_DEVICE, 0);
863 		}
864 		if (unlikely(is_ts_pkt)) {
865 			if (BNXT_CHIP_P5(bp)) {
866 				/* PTP worker takes ownership of the skb */
867 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
868 				skb = NULL;
869 			}
870 		}
871 
872 next_tx_int:
873 		cons = NEXT_TX(cons);
874 
875 		dev_consume_skb_any(skb);
876 	}
877 
878 	WRITE_ONCE(txr->tx_cons, cons);
879 
880 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
881 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
882 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
883 
884 	return rc;
885 }
886 
887 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
888 {
889 	struct bnxt_tx_ring_info *txr;
890 	bool more = false;
891 	int i;
892 
893 	bnxt_for_each_napi_tx(i, bnapi, txr) {
894 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
895 			more |= __bnxt_tx_int(bp, txr, budget);
896 	}
897 	if (!more)
898 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
899 }
900 
901 static bool bnxt_separate_head_pool(struct bnxt_rx_ring_info *rxr)
902 {
903 	return rxr->need_head_pool || PAGE_SIZE > BNXT_RX_PAGE_SIZE;
904 }
905 
906 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
907 					 struct bnxt_rx_ring_info *rxr,
908 					 unsigned int *offset,
909 					 gfp_t gfp)
910 {
911 	struct page *page;
912 
913 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
914 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
915 						BNXT_RX_PAGE_SIZE);
916 	} else {
917 		page = page_pool_dev_alloc_pages(rxr->page_pool);
918 		*offset = 0;
919 	}
920 	if (!page)
921 		return NULL;
922 
923 	*mapping = page_pool_get_dma_addr(page) + *offset;
924 	return page;
925 }
926 
927 static netmem_ref __bnxt_alloc_rx_netmem(struct bnxt *bp, dma_addr_t *mapping,
928 					 struct bnxt_rx_ring_info *rxr,
929 					 gfp_t gfp)
930 {
931 	netmem_ref netmem;
932 
933 	netmem = page_pool_alloc_netmems(rxr->page_pool, gfp);
934 	if (!netmem)
935 		return 0;
936 
937 	*mapping = page_pool_get_dma_addr_netmem(netmem);
938 	return netmem;
939 }
940 
941 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
942 				       struct bnxt_rx_ring_info *rxr,
943 				       gfp_t gfp)
944 {
945 	unsigned int offset;
946 	struct page *page;
947 
948 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
949 				    bp->rx_buf_size, gfp);
950 	if (!page)
951 		return NULL;
952 
953 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
954 	return page_address(page) + offset;
955 }
956 
957 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
958 		       u16 prod, gfp_t gfp)
959 {
960 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
961 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
962 	dma_addr_t mapping;
963 
964 	if (BNXT_RX_PAGE_MODE(bp)) {
965 		unsigned int offset;
966 		struct page *page =
967 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
968 
969 		if (!page)
970 			return -ENOMEM;
971 
972 		mapping += bp->rx_dma_offset;
973 		rx_buf->data = page;
974 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
975 	} else {
976 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
977 
978 		if (!data)
979 			return -ENOMEM;
980 
981 		rx_buf->data = data;
982 		rx_buf->data_ptr = data + bp->rx_offset;
983 	}
984 	rx_buf->mapping = mapping;
985 
986 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
987 	return 0;
988 }
989 
990 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
991 {
992 	u16 prod = rxr->rx_prod;
993 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
994 	struct bnxt *bp = rxr->bnapi->bp;
995 	struct rx_bd *cons_bd, *prod_bd;
996 
997 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
998 	cons_rx_buf = &rxr->rx_buf_ring[cons];
999 
1000 	prod_rx_buf->data = data;
1001 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
1002 
1003 	prod_rx_buf->mapping = cons_rx_buf->mapping;
1004 
1005 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1006 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
1007 
1008 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
1009 }
1010 
1011 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1012 {
1013 	u16 next, max = rxr->rx_agg_bmap_size;
1014 
1015 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
1016 	if (next >= max)
1017 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
1018 	return next;
1019 }
1020 
1021 static int bnxt_alloc_rx_netmem(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1022 				u16 prod, gfp_t gfp)
1023 {
1024 	struct rx_bd *rxbd =
1025 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1026 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
1027 	u16 sw_prod = rxr->rx_sw_agg_prod;
1028 	unsigned int offset = 0;
1029 	dma_addr_t mapping;
1030 	netmem_ref netmem;
1031 
1032 	netmem = __bnxt_alloc_rx_netmem(bp, &mapping, rxr, gfp);
1033 	if (!netmem)
1034 		return -ENOMEM;
1035 
1036 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1037 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1038 
1039 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1040 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1041 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1042 
1043 	rx_agg_buf->netmem = netmem;
1044 	rx_agg_buf->offset = offset;
1045 	rx_agg_buf->mapping = mapping;
1046 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1047 	rxbd->rx_bd_opaque = sw_prod;
1048 	return 0;
1049 }
1050 
1051 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1052 				       struct bnxt_cp_ring_info *cpr,
1053 				       u16 cp_cons, u16 curr)
1054 {
1055 	struct rx_agg_cmp *agg;
1056 
1057 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1058 	agg = (struct rx_agg_cmp *)
1059 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1060 	return agg;
1061 }
1062 
1063 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1064 					      struct bnxt_rx_ring_info *rxr,
1065 					      u16 agg_id, u16 curr)
1066 {
1067 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1068 
1069 	return &tpa_info->agg_arr[curr];
1070 }
1071 
1072 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1073 				   u16 start, u32 agg_bufs, bool tpa)
1074 {
1075 	struct bnxt_napi *bnapi = cpr->bnapi;
1076 	struct bnxt *bp = bnapi->bp;
1077 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1078 	u16 prod = rxr->rx_agg_prod;
1079 	u16 sw_prod = rxr->rx_sw_agg_prod;
1080 	bool p5_tpa = false;
1081 	u32 i;
1082 
1083 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1084 		p5_tpa = true;
1085 
1086 	for (i = 0; i < agg_bufs; i++) {
1087 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1088 		struct rx_agg_cmp *agg;
1089 		struct rx_bd *prod_bd;
1090 		netmem_ref netmem;
1091 		u16 cons;
1092 
1093 		if (p5_tpa)
1094 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1095 		else
1096 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1097 		cons = agg->rx_agg_cmp_opaque;
1098 		__clear_bit(cons, rxr->rx_agg_bmap);
1099 
1100 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1101 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1102 
1103 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1104 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1105 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1106 
1107 		/* It is possible for sw_prod to be equal to cons, so
1108 		 * set cons_rx_buf->netmem to 0 first.
1109 		 */
1110 		netmem = cons_rx_buf->netmem;
1111 		cons_rx_buf->netmem = 0;
1112 		prod_rx_buf->netmem = netmem;
1113 		prod_rx_buf->offset = cons_rx_buf->offset;
1114 
1115 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1116 
1117 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1118 
1119 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1120 		prod_bd->rx_bd_opaque = sw_prod;
1121 
1122 		prod = NEXT_RX_AGG(prod);
1123 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1124 	}
1125 	rxr->rx_agg_prod = prod;
1126 	rxr->rx_sw_agg_prod = sw_prod;
1127 }
1128 
1129 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1130 					      struct bnxt_rx_ring_info *rxr,
1131 					      u16 cons, void *data, u8 *data_ptr,
1132 					      dma_addr_t dma_addr,
1133 					      unsigned int offset_and_len)
1134 {
1135 	unsigned int len = offset_and_len & 0xffff;
1136 	struct page *page = data;
1137 	u16 prod = rxr->rx_prod;
1138 	struct sk_buff *skb;
1139 	int err;
1140 
1141 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1142 	if (unlikely(err)) {
1143 		bnxt_reuse_rx_data(rxr, cons, data);
1144 		return NULL;
1145 	}
1146 	dma_addr -= bp->rx_dma_offset;
1147 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1148 				bp->rx_dir);
1149 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1150 	if (!skb) {
1151 		page_pool_recycle_direct(rxr->page_pool, page);
1152 		return NULL;
1153 	}
1154 	skb_mark_for_recycle(skb);
1155 	skb_reserve(skb, bp->rx_offset);
1156 	__skb_put(skb, len);
1157 
1158 	return skb;
1159 }
1160 
1161 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1162 					struct bnxt_rx_ring_info *rxr,
1163 					u16 cons, void *data, u8 *data_ptr,
1164 					dma_addr_t dma_addr,
1165 					unsigned int offset_and_len)
1166 {
1167 	unsigned int payload = offset_and_len >> 16;
1168 	unsigned int len = offset_and_len & 0xffff;
1169 	skb_frag_t *frag;
1170 	struct page *page = data;
1171 	u16 prod = rxr->rx_prod;
1172 	struct sk_buff *skb;
1173 	int off, err;
1174 
1175 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1176 	if (unlikely(err)) {
1177 		bnxt_reuse_rx_data(rxr, cons, data);
1178 		return NULL;
1179 	}
1180 	dma_addr -= bp->rx_dma_offset;
1181 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1182 				bp->rx_dir);
1183 
1184 	if (unlikely(!payload))
1185 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1186 
1187 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1188 	if (!skb) {
1189 		page_pool_recycle_direct(rxr->page_pool, page);
1190 		return NULL;
1191 	}
1192 
1193 	skb_mark_for_recycle(skb);
1194 	off = (void *)data_ptr - page_address(page);
1195 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1196 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1197 	       payload + NET_IP_ALIGN);
1198 
1199 	frag = &skb_shinfo(skb)->frags[0];
1200 	skb_frag_size_sub(frag, payload);
1201 	skb_frag_off_add(frag, payload);
1202 	skb->data_len -= payload;
1203 	skb->tail += payload;
1204 
1205 	return skb;
1206 }
1207 
1208 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1209 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1210 				   void *data, u8 *data_ptr,
1211 				   dma_addr_t dma_addr,
1212 				   unsigned int offset_and_len)
1213 {
1214 	u16 prod = rxr->rx_prod;
1215 	struct sk_buff *skb;
1216 	int err;
1217 
1218 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1219 	if (unlikely(err)) {
1220 		bnxt_reuse_rx_data(rxr, cons, data);
1221 		return NULL;
1222 	}
1223 
1224 	skb = napi_build_skb(data, bp->rx_buf_size);
1225 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1226 				bp->rx_dir);
1227 	if (!skb) {
1228 		page_pool_free_va(rxr->head_pool, data, true);
1229 		return NULL;
1230 	}
1231 
1232 	skb_mark_for_recycle(skb);
1233 	skb_reserve(skb, bp->rx_offset);
1234 	skb_put(skb, offset_and_len & 0xffff);
1235 	return skb;
1236 }
1237 
1238 static u32 __bnxt_rx_agg_netmems(struct bnxt *bp,
1239 				 struct bnxt_cp_ring_info *cpr,
1240 				 u16 idx, u32 agg_bufs, bool tpa,
1241 				 struct sk_buff *skb,
1242 				 struct xdp_buff *xdp)
1243 {
1244 	struct bnxt_napi *bnapi = cpr->bnapi;
1245 	struct skb_shared_info *shinfo;
1246 	struct bnxt_rx_ring_info *rxr;
1247 	u32 i, total_frag_len = 0;
1248 	bool p5_tpa = false;
1249 	u16 prod;
1250 
1251 	rxr = bnapi->rx_ring;
1252 	prod = rxr->rx_agg_prod;
1253 
1254 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1255 		p5_tpa = true;
1256 
1257 	if (skb)
1258 		shinfo = skb_shinfo(skb);
1259 	else
1260 		shinfo = xdp_get_shared_info_from_buff(xdp);
1261 
1262 	for (i = 0; i < agg_bufs; i++) {
1263 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1264 		struct rx_agg_cmp *agg;
1265 		u16 cons, frag_len;
1266 		netmem_ref netmem;
1267 
1268 		if (p5_tpa)
1269 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1270 		else
1271 			agg = bnxt_get_agg(bp, cpr, idx, i);
1272 		cons = agg->rx_agg_cmp_opaque;
1273 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1274 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1275 
1276 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1277 		if (skb) {
1278 			skb_add_rx_frag_netmem(skb, i, cons_rx_buf->netmem,
1279 					       cons_rx_buf->offset,
1280 					       frag_len, BNXT_RX_PAGE_SIZE);
1281 		} else {
1282 			skb_frag_t *frag = &shinfo->frags[i];
1283 
1284 			skb_frag_fill_netmem_desc(frag, cons_rx_buf->netmem,
1285 						  cons_rx_buf->offset,
1286 						  frag_len);
1287 			shinfo->nr_frags = i + 1;
1288 		}
1289 		__clear_bit(cons, rxr->rx_agg_bmap);
1290 
1291 		/* It is possible for bnxt_alloc_rx_netmem() to allocate
1292 		 * a sw_prod index that equals the cons index, so we
1293 		 * need to clear the cons entry now.
1294 		 */
1295 		netmem = cons_rx_buf->netmem;
1296 		cons_rx_buf->netmem = 0;
1297 
1298 		if (xdp && netmem_is_pfmemalloc(netmem))
1299 			xdp_buff_set_frag_pfmemalloc(xdp);
1300 
1301 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_ATOMIC) != 0) {
1302 			if (skb) {
1303 				skb->len -= frag_len;
1304 				skb->data_len -= frag_len;
1305 				skb->truesize -= BNXT_RX_PAGE_SIZE;
1306 			}
1307 
1308 			--shinfo->nr_frags;
1309 			cons_rx_buf->netmem = netmem;
1310 
1311 			/* Update prod since possibly some netmems have been
1312 			 * allocated already.
1313 			 */
1314 			rxr->rx_agg_prod = prod;
1315 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1316 			return 0;
1317 		}
1318 
1319 		page_pool_dma_sync_netmem_for_cpu(rxr->page_pool, netmem, 0,
1320 						  BNXT_RX_PAGE_SIZE);
1321 
1322 		total_frag_len += frag_len;
1323 		prod = NEXT_RX_AGG(prod);
1324 	}
1325 	rxr->rx_agg_prod = prod;
1326 	return total_frag_len;
1327 }
1328 
1329 static struct sk_buff *bnxt_rx_agg_netmems_skb(struct bnxt *bp,
1330 					       struct bnxt_cp_ring_info *cpr,
1331 					       struct sk_buff *skb, u16 idx,
1332 					       u32 agg_bufs, bool tpa)
1333 {
1334 	u32 total_frag_len = 0;
1335 
1336 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1337 					       skb, NULL);
1338 	if (!total_frag_len) {
1339 		skb_mark_for_recycle(skb);
1340 		dev_kfree_skb(skb);
1341 		return NULL;
1342 	}
1343 
1344 	return skb;
1345 }
1346 
1347 static u32 bnxt_rx_agg_netmems_xdp(struct bnxt *bp,
1348 				   struct bnxt_cp_ring_info *cpr,
1349 				   struct xdp_buff *xdp, u16 idx,
1350 				   u32 agg_bufs, bool tpa)
1351 {
1352 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1353 	u32 total_frag_len = 0;
1354 
1355 	if (!xdp_buff_has_frags(xdp))
1356 		shinfo->nr_frags = 0;
1357 
1358 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1359 					       NULL, xdp);
1360 	if (total_frag_len) {
1361 		xdp_buff_set_frags_flag(xdp);
1362 		shinfo->nr_frags = agg_bufs;
1363 		shinfo->xdp_frags_size = total_frag_len;
1364 	}
1365 	return total_frag_len;
1366 }
1367 
1368 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1369 			       u8 agg_bufs, u32 *raw_cons)
1370 {
1371 	u16 last;
1372 	struct rx_agg_cmp *agg;
1373 
1374 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1375 	last = RING_CMP(*raw_cons);
1376 	agg = (struct rx_agg_cmp *)
1377 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1378 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1379 }
1380 
1381 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1382 				      unsigned int len,
1383 				      dma_addr_t mapping)
1384 {
1385 	struct bnxt *bp = bnapi->bp;
1386 	struct pci_dev *pdev = bp->pdev;
1387 	struct sk_buff *skb;
1388 
1389 	skb = napi_alloc_skb(&bnapi->napi, len);
1390 	if (!skb)
1391 		return NULL;
1392 
1393 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1394 				bp->rx_dir);
1395 
1396 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1397 	       len + NET_IP_ALIGN);
1398 
1399 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1400 				   bp->rx_dir);
1401 
1402 	skb_put(skb, len);
1403 
1404 	return skb;
1405 }
1406 
1407 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1408 				     unsigned int len,
1409 				     dma_addr_t mapping)
1410 {
1411 	return bnxt_copy_data(bnapi, data, len, mapping);
1412 }
1413 
1414 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1415 				     struct xdp_buff *xdp,
1416 				     unsigned int len,
1417 				     dma_addr_t mapping)
1418 {
1419 	unsigned int metasize = 0;
1420 	u8 *data = xdp->data;
1421 	struct sk_buff *skb;
1422 
1423 	len = xdp->data_end - xdp->data_meta;
1424 	metasize = xdp->data - xdp->data_meta;
1425 	data = xdp->data_meta;
1426 
1427 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1428 	if (!skb)
1429 		return skb;
1430 
1431 	if (metasize) {
1432 		skb_metadata_set(skb, metasize);
1433 		__skb_pull(skb, metasize);
1434 	}
1435 
1436 	return skb;
1437 }
1438 
1439 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1440 			   u32 *raw_cons, void *cmp)
1441 {
1442 	struct rx_cmp *rxcmp = cmp;
1443 	u32 tmp_raw_cons = *raw_cons;
1444 	u8 cmp_type, agg_bufs = 0;
1445 
1446 	cmp_type = RX_CMP_TYPE(rxcmp);
1447 
1448 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1449 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1450 			    RX_CMP_AGG_BUFS) >>
1451 			   RX_CMP_AGG_BUFS_SHIFT;
1452 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1453 		struct rx_tpa_end_cmp *tpa_end = cmp;
1454 
1455 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1456 			return 0;
1457 
1458 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1459 	}
1460 
1461 	if (agg_bufs) {
1462 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1463 			return -EBUSY;
1464 	}
1465 	*raw_cons = tmp_raw_cons;
1466 	return 0;
1467 }
1468 
1469 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1470 {
1471 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1472 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1473 
1474 	if (test_bit(idx, map->agg_idx_bmap))
1475 		idx = find_first_zero_bit(map->agg_idx_bmap,
1476 					  BNXT_AGG_IDX_BMAP_SIZE);
1477 	__set_bit(idx, map->agg_idx_bmap);
1478 	map->agg_id_tbl[agg_id] = idx;
1479 	return idx;
1480 }
1481 
1482 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1483 {
1484 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1485 
1486 	__clear_bit(idx, map->agg_idx_bmap);
1487 }
1488 
1489 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1490 {
1491 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1492 
1493 	return map->agg_id_tbl[agg_id];
1494 }
1495 
1496 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1497 			      struct rx_tpa_start_cmp *tpa_start,
1498 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1499 {
1500 	tpa_info->cfa_code_valid = 1;
1501 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1502 	tpa_info->vlan_valid = 0;
1503 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1504 		tpa_info->vlan_valid = 1;
1505 		tpa_info->metadata =
1506 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1507 	}
1508 }
1509 
1510 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1511 				 struct rx_tpa_start_cmp *tpa_start,
1512 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1513 {
1514 	tpa_info->vlan_valid = 0;
1515 	if (TPA_START_VLAN_VALID(tpa_start)) {
1516 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1517 		u32 vlan_proto = ETH_P_8021Q;
1518 
1519 		tpa_info->vlan_valid = 1;
1520 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1521 			vlan_proto = ETH_P_8021AD;
1522 		tpa_info->metadata = vlan_proto << 16 |
1523 				     TPA_START_METADATA0_TCI(tpa_start1);
1524 	}
1525 }
1526 
1527 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1528 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1529 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1530 {
1531 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1532 	struct bnxt_tpa_info *tpa_info;
1533 	u16 cons, prod, agg_id;
1534 	struct rx_bd *prod_bd;
1535 	dma_addr_t mapping;
1536 
1537 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1538 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1539 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1540 	} else {
1541 		agg_id = TPA_START_AGG_ID(tpa_start);
1542 	}
1543 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1544 	prod = rxr->rx_prod;
1545 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1546 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1547 	tpa_info = &rxr->rx_tpa[agg_id];
1548 
1549 	if (unlikely(cons != rxr->rx_next_cons ||
1550 		     TPA_START_ERROR(tpa_start))) {
1551 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1552 			    cons, rxr->rx_next_cons,
1553 			    TPA_START_ERROR_CODE(tpa_start1));
1554 		bnxt_sched_reset_rxr(bp, rxr);
1555 		return;
1556 	}
1557 	prod_rx_buf->data = tpa_info->data;
1558 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1559 
1560 	mapping = tpa_info->mapping;
1561 	prod_rx_buf->mapping = mapping;
1562 
1563 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1564 
1565 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1566 
1567 	tpa_info->data = cons_rx_buf->data;
1568 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1569 	cons_rx_buf->data = NULL;
1570 	tpa_info->mapping = cons_rx_buf->mapping;
1571 
1572 	tpa_info->len =
1573 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1574 				RX_TPA_START_CMP_LEN_SHIFT;
1575 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1576 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1577 		tpa_info->gso_type = SKB_GSO_TCPV4;
1578 		if (TPA_START_IS_IPV6(tpa_start1))
1579 			tpa_info->gso_type = SKB_GSO_TCPV6;
1580 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1581 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1582 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1583 			tpa_info->gso_type = SKB_GSO_TCPV6;
1584 		tpa_info->rss_hash =
1585 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1586 	} else {
1587 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1588 		tpa_info->gso_type = 0;
1589 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1590 	}
1591 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1592 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1593 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1594 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1595 	else
1596 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1597 	tpa_info->agg_count = 0;
1598 
1599 	rxr->rx_prod = NEXT_RX(prod);
1600 	cons = RING_RX(bp, NEXT_RX(cons));
1601 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1602 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1603 
1604 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1605 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1606 	cons_rx_buf->data = NULL;
1607 }
1608 
1609 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1610 {
1611 	if (agg_bufs)
1612 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1613 }
1614 
1615 #ifdef CONFIG_INET
1616 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1617 {
1618 	struct udphdr *uh = NULL;
1619 
1620 	if (ip_proto == htons(ETH_P_IP)) {
1621 		struct iphdr *iph = (struct iphdr *)skb->data;
1622 
1623 		if (iph->protocol == IPPROTO_UDP)
1624 			uh = (struct udphdr *)(iph + 1);
1625 	} else {
1626 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1627 
1628 		if (iph->nexthdr == IPPROTO_UDP)
1629 			uh = (struct udphdr *)(iph + 1);
1630 	}
1631 	if (uh) {
1632 		if (uh->check)
1633 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1634 		else
1635 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1636 	}
1637 }
1638 #endif
1639 
1640 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1641 					   int payload_off, int tcp_ts,
1642 					   struct sk_buff *skb)
1643 {
1644 #ifdef CONFIG_INET
1645 	struct tcphdr *th;
1646 	int len, nw_off;
1647 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1648 	u32 hdr_info = tpa_info->hdr_info;
1649 	bool loopback = false;
1650 
1651 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1652 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1653 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1654 
1655 	/* If the packet is an internal loopback packet, the offsets will
1656 	 * have an extra 4 bytes.
1657 	 */
1658 	if (inner_mac_off == 4) {
1659 		loopback = true;
1660 	} else if (inner_mac_off > 4) {
1661 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1662 					    ETH_HLEN - 2));
1663 
1664 		/* We only support inner iPv4/ipv6.  If we don't see the
1665 		 * correct protocol ID, it must be a loopback packet where
1666 		 * the offsets are off by 4.
1667 		 */
1668 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1669 			loopback = true;
1670 	}
1671 	if (loopback) {
1672 		/* internal loopback packet, subtract all offsets by 4 */
1673 		inner_ip_off -= 4;
1674 		inner_mac_off -= 4;
1675 		outer_ip_off -= 4;
1676 	}
1677 
1678 	nw_off = inner_ip_off - ETH_HLEN;
1679 	skb_set_network_header(skb, nw_off);
1680 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1681 		struct ipv6hdr *iph = ipv6_hdr(skb);
1682 
1683 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1684 		len = skb->len - skb_transport_offset(skb);
1685 		th = tcp_hdr(skb);
1686 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1687 	} else {
1688 		struct iphdr *iph = ip_hdr(skb);
1689 
1690 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1691 		len = skb->len - skb_transport_offset(skb);
1692 		th = tcp_hdr(skb);
1693 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1694 	}
1695 
1696 	if (inner_mac_off) { /* tunnel */
1697 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1698 					    ETH_HLEN - 2));
1699 
1700 		bnxt_gro_tunnel(skb, proto);
1701 	}
1702 #endif
1703 	return skb;
1704 }
1705 
1706 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1707 					   int payload_off, int tcp_ts,
1708 					   struct sk_buff *skb)
1709 {
1710 #ifdef CONFIG_INET
1711 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1712 	u32 hdr_info = tpa_info->hdr_info;
1713 	int iphdr_len, nw_off;
1714 
1715 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1716 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1717 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1718 
1719 	nw_off = inner_ip_off - ETH_HLEN;
1720 	skb_set_network_header(skb, nw_off);
1721 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1722 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1723 	skb_set_transport_header(skb, nw_off + iphdr_len);
1724 
1725 	if (inner_mac_off) { /* tunnel */
1726 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1727 					    ETH_HLEN - 2));
1728 
1729 		bnxt_gro_tunnel(skb, proto);
1730 	}
1731 #endif
1732 	return skb;
1733 }
1734 
1735 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1736 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1737 
1738 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1739 					   int payload_off, int tcp_ts,
1740 					   struct sk_buff *skb)
1741 {
1742 #ifdef CONFIG_INET
1743 	struct tcphdr *th;
1744 	int len, nw_off, tcp_opt_len = 0;
1745 
1746 	if (tcp_ts)
1747 		tcp_opt_len = 12;
1748 
1749 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1750 		struct iphdr *iph;
1751 
1752 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1753 			 ETH_HLEN;
1754 		skb_set_network_header(skb, nw_off);
1755 		iph = ip_hdr(skb);
1756 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1757 		len = skb->len - skb_transport_offset(skb);
1758 		th = tcp_hdr(skb);
1759 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1760 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1761 		struct ipv6hdr *iph;
1762 
1763 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1764 			 ETH_HLEN;
1765 		skb_set_network_header(skb, nw_off);
1766 		iph = ipv6_hdr(skb);
1767 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1768 		len = skb->len - skb_transport_offset(skb);
1769 		th = tcp_hdr(skb);
1770 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1771 	} else {
1772 		dev_kfree_skb_any(skb);
1773 		return NULL;
1774 	}
1775 
1776 	if (nw_off) /* tunnel */
1777 		bnxt_gro_tunnel(skb, skb->protocol);
1778 #endif
1779 	return skb;
1780 }
1781 
1782 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1783 					   struct bnxt_tpa_info *tpa_info,
1784 					   struct rx_tpa_end_cmp *tpa_end,
1785 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1786 					   struct sk_buff *skb)
1787 {
1788 #ifdef CONFIG_INET
1789 	int payload_off;
1790 	u16 segs;
1791 
1792 	segs = TPA_END_TPA_SEGS(tpa_end);
1793 	if (segs == 1)
1794 		return skb;
1795 
1796 	NAPI_GRO_CB(skb)->count = segs;
1797 	skb_shinfo(skb)->gso_size =
1798 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1799 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1800 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1801 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1802 	else
1803 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1804 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1805 	if (likely(skb))
1806 		tcp_gro_complete(skb);
1807 #endif
1808 	return skb;
1809 }
1810 
1811 /* Given the cfa_code of a received packet determine which
1812  * netdev (vf-rep or PF) the packet is destined to.
1813  */
1814 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1815 {
1816 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1817 
1818 	/* if vf-rep dev is NULL, it must belong to the PF */
1819 	return dev ? dev : bp->dev;
1820 }
1821 
1822 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1823 					   struct bnxt_cp_ring_info *cpr,
1824 					   u32 *raw_cons,
1825 					   struct rx_tpa_end_cmp *tpa_end,
1826 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1827 					   u8 *event)
1828 {
1829 	struct bnxt_napi *bnapi = cpr->bnapi;
1830 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1831 	struct net_device *dev = bp->dev;
1832 	u8 *data_ptr, agg_bufs;
1833 	unsigned int len;
1834 	struct bnxt_tpa_info *tpa_info;
1835 	dma_addr_t mapping;
1836 	struct sk_buff *skb;
1837 	u16 idx = 0, agg_id;
1838 	void *data;
1839 	bool gro;
1840 
1841 	if (unlikely(bnapi->in_reset)) {
1842 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1843 
1844 		if (rc < 0)
1845 			return ERR_PTR(-EBUSY);
1846 		return NULL;
1847 	}
1848 
1849 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1850 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1851 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1852 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1853 		tpa_info = &rxr->rx_tpa[agg_id];
1854 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1855 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1856 				    agg_bufs, tpa_info->agg_count);
1857 			agg_bufs = tpa_info->agg_count;
1858 		}
1859 		tpa_info->agg_count = 0;
1860 		*event |= BNXT_AGG_EVENT;
1861 		bnxt_free_agg_idx(rxr, agg_id);
1862 		idx = agg_id;
1863 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1864 	} else {
1865 		agg_id = TPA_END_AGG_ID(tpa_end);
1866 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1867 		tpa_info = &rxr->rx_tpa[agg_id];
1868 		idx = RING_CMP(*raw_cons);
1869 		if (agg_bufs) {
1870 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1871 				return ERR_PTR(-EBUSY);
1872 
1873 			*event |= BNXT_AGG_EVENT;
1874 			idx = NEXT_CMP(idx);
1875 		}
1876 		gro = !!TPA_END_GRO(tpa_end);
1877 	}
1878 	data = tpa_info->data;
1879 	data_ptr = tpa_info->data_ptr;
1880 	prefetch(data_ptr);
1881 	len = tpa_info->len;
1882 	mapping = tpa_info->mapping;
1883 
1884 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1885 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1886 		if (agg_bufs > MAX_SKB_FRAGS)
1887 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1888 				    agg_bufs, (int)MAX_SKB_FRAGS);
1889 		return NULL;
1890 	}
1891 
1892 	if (len <= bp->rx_copybreak) {
1893 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1894 		if (!skb) {
1895 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1896 			cpr->sw_stats->rx.rx_oom_discards += 1;
1897 			return NULL;
1898 		}
1899 	} else {
1900 		u8 *new_data;
1901 		dma_addr_t new_mapping;
1902 
1903 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1904 						GFP_ATOMIC);
1905 		if (!new_data) {
1906 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1907 			cpr->sw_stats->rx.rx_oom_discards += 1;
1908 			return NULL;
1909 		}
1910 
1911 		tpa_info->data = new_data;
1912 		tpa_info->data_ptr = new_data + bp->rx_offset;
1913 		tpa_info->mapping = new_mapping;
1914 
1915 		skb = napi_build_skb(data, bp->rx_buf_size);
1916 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1917 					bp->rx_buf_use_size, bp->rx_dir);
1918 
1919 		if (!skb) {
1920 			page_pool_free_va(rxr->head_pool, data, true);
1921 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1922 			cpr->sw_stats->rx.rx_oom_discards += 1;
1923 			return NULL;
1924 		}
1925 		skb_mark_for_recycle(skb);
1926 		skb_reserve(skb, bp->rx_offset);
1927 		skb_put(skb, len);
1928 	}
1929 
1930 	if (agg_bufs) {
1931 		skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs,
1932 					      true);
1933 		if (!skb) {
1934 			/* Page reuse already handled by bnxt_rx_pages(). */
1935 			cpr->sw_stats->rx.rx_oom_discards += 1;
1936 			return NULL;
1937 		}
1938 	}
1939 
1940 	if (tpa_info->cfa_code_valid)
1941 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1942 	skb->protocol = eth_type_trans(skb, dev);
1943 
1944 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1945 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1946 
1947 	if (tpa_info->vlan_valid &&
1948 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1949 		__be16 vlan_proto = htons(tpa_info->metadata >>
1950 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1951 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1952 
1953 		if (eth_type_vlan(vlan_proto)) {
1954 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1955 		} else {
1956 			dev_kfree_skb(skb);
1957 			return NULL;
1958 		}
1959 	}
1960 
1961 	skb_checksum_none_assert(skb);
1962 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1963 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1964 		skb->csum_level =
1965 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1966 	}
1967 
1968 	if (gro)
1969 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1970 
1971 	return skb;
1972 }
1973 
1974 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1975 			 struct rx_agg_cmp *rx_agg)
1976 {
1977 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1978 	struct bnxt_tpa_info *tpa_info;
1979 
1980 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1981 	tpa_info = &rxr->rx_tpa[agg_id];
1982 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1983 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1984 }
1985 
1986 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1987 			     struct sk_buff *skb)
1988 {
1989 	skb_mark_for_recycle(skb);
1990 
1991 	if (skb->dev != bp->dev) {
1992 		/* this packet belongs to a vf-rep */
1993 		bnxt_vf_rep_rx(bp, skb);
1994 		return;
1995 	}
1996 	skb_record_rx_queue(skb, bnapi->index);
1997 	napi_gro_receive(&bnapi->napi, skb);
1998 }
1999 
2000 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
2001 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
2002 {
2003 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
2004 
2005 	if (BNXT_PTP_RX_TS_VALID(flags))
2006 		goto ts_valid;
2007 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
2008 		return false;
2009 
2010 ts_valid:
2011 	*cmpl_ts = ts;
2012 	return true;
2013 }
2014 
2015 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
2016 				    struct rx_cmp *rxcmp,
2017 				    struct rx_cmp_ext *rxcmp1)
2018 {
2019 	__be16 vlan_proto;
2020 	u16 vtag;
2021 
2022 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
2023 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
2024 		u32 meta_data;
2025 
2026 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
2027 			return skb;
2028 
2029 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
2030 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2031 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
2032 		if (eth_type_vlan(vlan_proto))
2033 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2034 		else
2035 			goto vlan_err;
2036 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2037 		if (RX_CMP_VLAN_VALID(rxcmp)) {
2038 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
2039 
2040 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
2041 				vlan_proto = htons(ETH_P_8021Q);
2042 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
2043 				vlan_proto = htons(ETH_P_8021AD);
2044 			else
2045 				goto vlan_err;
2046 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
2047 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2048 		}
2049 	}
2050 	return skb;
2051 vlan_err:
2052 	skb_mark_for_recycle(skb);
2053 	dev_kfree_skb(skb);
2054 	return NULL;
2055 }
2056 
2057 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
2058 					   struct rx_cmp *rxcmp)
2059 {
2060 	u8 ext_op;
2061 
2062 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
2063 	switch (ext_op) {
2064 	case EXT_OP_INNER_4:
2065 	case EXT_OP_OUTER_4:
2066 	case EXT_OP_INNFL_3:
2067 	case EXT_OP_OUTFL_3:
2068 		return PKT_HASH_TYPE_L4;
2069 	default:
2070 		return PKT_HASH_TYPE_L3;
2071 	}
2072 }
2073 
2074 /* returns the following:
2075  * 1       - 1 packet successfully received
2076  * 0       - successful TPA_START, packet not completed yet
2077  * -EBUSY  - completion ring does not have all the agg buffers yet
2078  * -ENOMEM - packet aborted due to out of memory
2079  * -EIO    - packet aborted due to hw error indicated in BD
2080  */
2081 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2082 		       u32 *raw_cons, u8 *event)
2083 {
2084 	struct bnxt_napi *bnapi = cpr->bnapi;
2085 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2086 	struct net_device *dev = bp->dev;
2087 	struct rx_cmp *rxcmp;
2088 	struct rx_cmp_ext *rxcmp1;
2089 	u32 tmp_raw_cons = *raw_cons;
2090 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2091 	struct skb_shared_info *sinfo;
2092 	struct bnxt_sw_rx_bd *rx_buf;
2093 	unsigned int len;
2094 	u8 *data_ptr, agg_bufs, cmp_type;
2095 	bool xdp_active = false;
2096 	dma_addr_t dma_addr;
2097 	struct sk_buff *skb;
2098 	struct xdp_buff xdp;
2099 	u32 flags, misc;
2100 	u32 cmpl_ts;
2101 	void *data;
2102 	int rc = 0;
2103 
2104 	rxcmp = (struct rx_cmp *)
2105 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2106 
2107 	cmp_type = RX_CMP_TYPE(rxcmp);
2108 
2109 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2110 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2111 		goto next_rx_no_prod_no_len;
2112 	}
2113 
2114 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2115 	cp_cons = RING_CMP(tmp_raw_cons);
2116 	rxcmp1 = (struct rx_cmp_ext *)
2117 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2118 
2119 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2120 		return -EBUSY;
2121 
2122 	/* The valid test of the entry must be done first before
2123 	 * reading any further.
2124 	 */
2125 	dma_rmb();
2126 	prod = rxr->rx_prod;
2127 
2128 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2129 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2130 		bnxt_tpa_start(bp, rxr, cmp_type,
2131 			       (struct rx_tpa_start_cmp *)rxcmp,
2132 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2133 
2134 		*event |= BNXT_RX_EVENT;
2135 		goto next_rx_no_prod_no_len;
2136 
2137 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2138 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2139 				   (struct rx_tpa_end_cmp *)rxcmp,
2140 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2141 
2142 		if (IS_ERR(skb))
2143 			return -EBUSY;
2144 
2145 		rc = -ENOMEM;
2146 		if (likely(skb)) {
2147 			bnxt_deliver_skb(bp, bnapi, skb);
2148 			rc = 1;
2149 		}
2150 		*event |= BNXT_RX_EVENT;
2151 		goto next_rx_no_prod_no_len;
2152 	}
2153 
2154 	cons = rxcmp->rx_cmp_opaque;
2155 	if (unlikely(cons != rxr->rx_next_cons)) {
2156 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2157 
2158 		/* 0xffff is forced error, don't print it */
2159 		if (rxr->rx_next_cons != 0xffff)
2160 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2161 				    cons, rxr->rx_next_cons);
2162 		bnxt_sched_reset_rxr(bp, rxr);
2163 		if (rc1)
2164 			return rc1;
2165 		goto next_rx_no_prod_no_len;
2166 	}
2167 	rx_buf = &rxr->rx_buf_ring[cons];
2168 	data = rx_buf->data;
2169 	data_ptr = rx_buf->data_ptr;
2170 	prefetch(data_ptr);
2171 
2172 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2173 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2174 
2175 	if (agg_bufs) {
2176 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2177 			return -EBUSY;
2178 
2179 		cp_cons = NEXT_CMP(cp_cons);
2180 		*event |= BNXT_AGG_EVENT;
2181 	}
2182 	*event |= BNXT_RX_EVENT;
2183 
2184 	rx_buf->data = NULL;
2185 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2186 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2187 
2188 		bnxt_reuse_rx_data(rxr, cons, data);
2189 		if (agg_bufs)
2190 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2191 					       false);
2192 
2193 		rc = -EIO;
2194 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2195 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2196 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2197 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2198 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2199 						 rx_err);
2200 				bnxt_sched_reset_rxr(bp, rxr);
2201 			}
2202 		}
2203 		goto next_rx_no_len;
2204 	}
2205 
2206 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2207 	len = flags >> RX_CMP_LEN_SHIFT;
2208 	dma_addr = rx_buf->mapping;
2209 
2210 	if (bnxt_xdp_attached(bp, rxr)) {
2211 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2212 		if (agg_bufs) {
2213 			u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr, &xdp,
2214 							       cp_cons,
2215 							       agg_bufs,
2216 							       false);
2217 			if (!frag_len)
2218 				goto oom_next_rx;
2219 
2220 		}
2221 		xdp_active = true;
2222 	}
2223 
2224 	if (xdp_active) {
2225 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2226 			rc = 1;
2227 			goto next_rx;
2228 		}
2229 		if (xdp_buff_has_frags(&xdp)) {
2230 			sinfo = xdp_get_shared_info_from_buff(&xdp);
2231 			agg_bufs = sinfo->nr_frags;
2232 		} else {
2233 			agg_bufs = 0;
2234 		}
2235 	}
2236 
2237 	if (len <= bp->rx_copybreak) {
2238 		if (!xdp_active)
2239 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2240 		else
2241 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2242 		bnxt_reuse_rx_data(rxr, cons, data);
2243 		if (!skb) {
2244 			if (agg_bufs) {
2245 				if (!xdp_active)
2246 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2247 							       agg_bufs, false);
2248 				else
2249 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2250 			}
2251 			goto oom_next_rx;
2252 		}
2253 	} else {
2254 		u32 payload;
2255 
2256 		if (rx_buf->data_ptr == data_ptr)
2257 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2258 		else
2259 			payload = 0;
2260 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2261 				      payload | len);
2262 		if (!skb)
2263 			goto oom_next_rx;
2264 	}
2265 
2266 	if (agg_bufs) {
2267 		if (!xdp_active) {
2268 			skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons,
2269 						      agg_bufs, false);
2270 			if (!skb)
2271 				goto oom_next_rx;
2272 		} else {
2273 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs,
2274 						 rxr->page_pool, &xdp);
2275 			if (!skb) {
2276 				/* we should be able to free the old skb here */
2277 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2278 				goto oom_next_rx;
2279 			}
2280 		}
2281 	}
2282 
2283 	if (RX_CMP_HASH_VALID(rxcmp)) {
2284 		enum pkt_hash_types type;
2285 
2286 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2287 			type = bnxt_rss_ext_op(bp, rxcmp);
2288 		} else {
2289 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2290 
2291 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2292 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2293 				type = PKT_HASH_TYPE_L4;
2294 			else
2295 				type = PKT_HASH_TYPE_L3;
2296 		}
2297 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2298 	}
2299 
2300 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2301 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2302 	skb->protocol = eth_type_trans(skb, dev);
2303 
2304 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2305 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2306 		if (!skb)
2307 			goto next_rx;
2308 	}
2309 
2310 	skb_checksum_none_assert(skb);
2311 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2312 		if (dev->features & NETIF_F_RXCSUM) {
2313 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2314 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2315 		}
2316 	} else {
2317 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2318 			if (dev->features & NETIF_F_RXCSUM)
2319 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2320 		}
2321 	}
2322 
2323 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2324 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2325 			u64 ns, ts;
2326 
2327 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2328 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2329 
2330 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2331 				memset(skb_hwtstamps(skb), 0,
2332 				       sizeof(*skb_hwtstamps(skb)));
2333 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2334 			}
2335 		}
2336 	}
2337 	bnxt_deliver_skb(bp, bnapi, skb);
2338 	rc = 1;
2339 
2340 next_rx:
2341 	cpr->rx_packets += 1;
2342 	cpr->rx_bytes += len;
2343 
2344 next_rx_no_len:
2345 	rxr->rx_prod = NEXT_RX(prod);
2346 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2347 
2348 next_rx_no_prod_no_len:
2349 	*raw_cons = tmp_raw_cons;
2350 
2351 	return rc;
2352 
2353 oom_next_rx:
2354 	cpr->sw_stats->rx.rx_oom_discards += 1;
2355 	rc = -ENOMEM;
2356 	goto next_rx;
2357 }
2358 
2359 /* In netpoll mode, if we are using a combined completion ring, we need to
2360  * discard the rx packets and recycle the buffers.
2361  */
2362 static int bnxt_force_rx_discard(struct bnxt *bp,
2363 				 struct bnxt_cp_ring_info *cpr,
2364 				 u32 *raw_cons, u8 *event)
2365 {
2366 	u32 tmp_raw_cons = *raw_cons;
2367 	struct rx_cmp_ext *rxcmp1;
2368 	struct rx_cmp *rxcmp;
2369 	u16 cp_cons;
2370 	u8 cmp_type;
2371 	int rc;
2372 
2373 	cp_cons = RING_CMP(tmp_raw_cons);
2374 	rxcmp = (struct rx_cmp *)
2375 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2376 
2377 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2378 	cp_cons = RING_CMP(tmp_raw_cons);
2379 	rxcmp1 = (struct rx_cmp_ext *)
2380 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2381 
2382 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2383 		return -EBUSY;
2384 
2385 	/* The valid test of the entry must be done first before
2386 	 * reading any further.
2387 	 */
2388 	dma_rmb();
2389 	cmp_type = RX_CMP_TYPE(rxcmp);
2390 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2391 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2392 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2393 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2394 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2395 		struct rx_tpa_end_cmp_ext *tpa_end1;
2396 
2397 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2398 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2399 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2400 	}
2401 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2402 	if (rc && rc != -EBUSY)
2403 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2404 	return rc;
2405 }
2406 
2407 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2408 {
2409 	struct bnxt_fw_health *fw_health = bp->fw_health;
2410 	u32 reg = fw_health->regs[reg_idx];
2411 	u32 reg_type, reg_off, val = 0;
2412 
2413 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2414 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2415 	switch (reg_type) {
2416 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2417 		pci_read_config_dword(bp->pdev, reg_off, &val);
2418 		break;
2419 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2420 		reg_off = fw_health->mapped_regs[reg_idx];
2421 		fallthrough;
2422 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2423 		val = readl(bp->bar0 + reg_off);
2424 		break;
2425 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2426 		val = readl(bp->bar1 + reg_off);
2427 		break;
2428 	}
2429 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2430 		val &= fw_health->fw_reset_inprog_reg_mask;
2431 	return val;
2432 }
2433 
2434 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2435 {
2436 	int i;
2437 
2438 	for (i = 0; i < bp->rx_nr_rings; i++) {
2439 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2440 		struct bnxt_ring_grp_info *grp_info;
2441 
2442 		grp_info = &bp->grp_info[grp_idx];
2443 		if (grp_info->agg_fw_ring_id == ring_id)
2444 			return grp_idx;
2445 	}
2446 	return INVALID_HW_RING_ID;
2447 }
2448 
2449 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2450 {
2451 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2452 
2453 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2454 		return link_info->force_link_speed2;
2455 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2456 		return link_info->force_pam4_link_speed;
2457 	return link_info->force_link_speed;
2458 }
2459 
2460 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2461 {
2462 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2463 
2464 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2465 		link_info->req_link_speed = link_info->force_link_speed2;
2466 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2467 		switch (link_info->req_link_speed) {
2468 		case BNXT_LINK_SPEED_50GB_PAM4:
2469 		case BNXT_LINK_SPEED_100GB_PAM4:
2470 		case BNXT_LINK_SPEED_200GB_PAM4:
2471 		case BNXT_LINK_SPEED_400GB_PAM4:
2472 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2473 			break;
2474 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2475 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2476 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2477 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2478 			break;
2479 		default:
2480 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2481 		}
2482 		return;
2483 	}
2484 	link_info->req_link_speed = link_info->force_link_speed;
2485 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2486 	if (link_info->force_pam4_link_speed) {
2487 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2488 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2489 	}
2490 }
2491 
2492 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2493 {
2494 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2495 
2496 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2497 		link_info->advertising = link_info->auto_link_speeds2;
2498 		return;
2499 	}
2500 	link_info->advertising = link_info->auto_link_speeds;
2501 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2502 }
2503 
2504 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2505 {
2506 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2507 
2508 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2509 		if (link_info->req_link_speed != link_info->force_link_speed2)
2510 			return true;
2511 		return false;
2512 	}
2513 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2514 	    link_info->req_link_speed != link_info->force_link_speed)
2515 		return true;
2516 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2517 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2518 		return true;
2519 	return false;
2520 }
2521 
2522 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2523 {
2524 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2525 
2526 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2527 		if (link_info->advertising != link_info->auto_link_speeds2)
2528 			return true;
2529 		return false;
2530 	}
2531 	if (link_info->advertising != link_info->auto_link_speeds ||
2532 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2533 		return true;
2534 	return false;
2535 }
2536 
2537 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2538 {
2539 	u32 flags = bp->ctx->ctx_arr[type].flags;
2540 
2541 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2542 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2543 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2544 }
2545 
2546 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2547 {
2548 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2549 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2550 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2551 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2552 	struct bnxt_bs_trace_info *bs_trace;
2553 	int last_pg;
2554 
2555 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2556 		return;
2557 
2558 	mem_size = ctxm->max_entries * ctxm->entry_size;
2559 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2560 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2561 
2562 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2563 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2564 
2565 	rmem = &ctx_pg[0].ring_mem;
2566 	bs_trace = &bp->bs_trace[trace_type];
2567 	bs_trace->ctx_type = ctxm->type;
2568 	bs_trace->trace_type = trace_type;
2569 	if (pages > MAX_CTX_PAGES) {
2570 		int last_pg_dir = rmem->nr_pages - 1;
2571 
2572 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2573 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2574 	} else {
2575 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2576 	}
2577 	bs_trace->magic_byte += magic_byte_offset;
2578 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2579 }
2580 
2581 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2582 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2583 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2584 
2585 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2586 	(((data2) &							\
2587 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2588 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2589 
2590 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2591 	((data2) &							\
2592 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2593 
2594 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2595 	(((data2) &							\
2596 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2597 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2598 
2599 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2600 	((data1) &							\
2601 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2602 
2603 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2604 	(((data1) &							\
2605 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2606 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2607 
2608 /* Return true if the workqueue has to be scheduled */
2609 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2610 {
2611 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2612 
2613 	switch (err_type) {
2614 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2615 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2616 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2617 		break;
2618 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2619 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2620 		break;
2621 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2622 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2623 		break;
2624 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2625 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2626 		char *threshold_type;
2627 		bool notify = false;
2628 		char *dir_str;
2629 
2630 		switch (type) {
2631 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2632 			threshold_type = "warning";
2633 			break;
2634 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2635 			threshold_type = "critical";
2636 			break;
2637 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2638 			threshold_type = "fatal";
2639 			break;
2640 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2641 			threshold_type = "shutdown";
2642 			break;
2643 		default:
2644 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2645 			return false;
2646 		}
2647 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2648 			dir_str = "above";
2649 			notify = true;
2650 		} else {
2651 			dir_str = "below";
2652 		}
2653 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2654 			    dir_str, threshold_type);
2655 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2656 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2657 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2658 		if (notify) {
2659 			bp->thermal_threshold_type = type;
2660 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2661 			return true;
2662 		}
2663 		return false;
2664 	}
2665 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2666 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2667 		break;
2668 	default:
2669 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2670 			   err_type);
2671 		break;
2672 	}
2673 	return false;
2674 }
2675 
2676 #define BNXT_GET_EVENT_PORT(data)	\
2677 	((data) &			\
2678 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2679 
2680 #define BNXT_EVENT_RING_TYPE(data2)	\
2681 	((data2) &			\
2682 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2683 
2684 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2685 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2686 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2687 
2688 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2689 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2690 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2691 
2692 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2693 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2694 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2695 
2696 #define BNXT_PHC_BITS	48
2697 
2698 static int bnxt_async_event_process(struct bnxt *bp,
2699 				    struct hwrm_async_event_cmpl *cmpl)
2700 {
2701 	u16 event_id = le16_to_cpu(cmpl->event_id);
2702 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2703 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2704 
2705 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2706 		   event_id, data1, data2);
2707 
2708 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2709 	switch (event_id) {
2710 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2711 		struct bnxt_link_info *link_info = &bp->link_info;
2712 
2713 		if (BNXT_VF(bp))
2714 			goto async_event_process_exit;
2715 
2716 		/* print unsupported speed warning in forced speed mode only */
2717 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2718 		    (data1 & 0x20000)) {
2719 			u16 fw_speed = bnxt_get_force_speed(link_info);
2720 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2721 
2722 			if (speed != SPEED_UNKNOWN)
2723 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2724 					    speed);
2725 		}
2726 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2727 	}
2728 		fallthrough;
2729 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2730 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2731 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2732 		fallthrough;
2733 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2734 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2735 		break;
2736 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2737 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2738 		break;
2739 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2740 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2741 
2742 		if (BNXT_VF(bp))
2743 			break;
2744 
2745 		if (bp->pf.port_id != port_id)
2746 			break;
2747 
2748 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2749 		break;
2750 	}
2751 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2752 		if (BNXT_PF(bp))
2753 			goto async_event_process_exit;
2754 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2755 		break;
2756 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2757 		char *type_str = "Solicited";
2758 
2759 		if (!bp->fw_health)
2760 			goto async_event_process_exit;
2761 
2762 		bp->fw_reset_timestamp = jiffies;
2763 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2764 		if (!bp->fw_reset_min_dsecs)
2765 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2766 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2767 		if (!bp->fw_reset_max_dsecs)
2768 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2769 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2770 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2771 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2772 			type_str = "Fatal";
2773 			bp->fw_health->fatalities++;
2774 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2775 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2776 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2777 			type_str = "Non-fatal";
2778 			bp->fw_health->survivals++;
2779 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2780 		}
2781 		netif_warn(bp, hw, bp->dev,
2782 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2783 			   type_str, data1, data2,
2784 			   bp->fw_reset_min_dsecs * 100,
2785 			   bp->fw_reset_max_dsecs * 100);
2786 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2787 		break;
2788 	}
2789 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2790 		struct bnxt_fw_health *fw_health = bp->fw_health;
2791 		char *status_desc = "healthy";
2792 		u32 status;
2793 
2794 		if (!fw_health)
2795 			goto async_event_process_exit;
2796 
2797 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2798 			fw_health->enabled = false;
2799 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2800 			break;
2801 		}
2802 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2803 		fw_health->tmr_multiplier =
2804 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2805 				     bp->current_interval * 10);
2806 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2807 		if (!fw_health->enabled)
2808 			fw_health->last_fw_heartbeat =
2809 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2810 		fw_health->last_fw_reset_cnt =
2811 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2812 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2813 		if (status != BNXT_FW_STATUS_HEALTHY)
2814 			status_desc = "unhealthy";
2815 		netif_info(bp, drv, bp->dev,
2816 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2817 			   fw_health->primary ? "primary" : "backup", status,
2818 			   status_desc, fw_health->last_fw_reset_cnt);
2819 		if (!fw_health->enabled) {
2820 			/* Make sure tmr_counter is set and visible to
2821 			 * bnxt_health_check() before setting enabled to true.
2822 			 */
2823 			smp_wmb();
2824 			fw_health->enabled = true;
2825 		}
2826 		goto async_event_process_exit;
2827 	}
2828 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2829 		netif_notice(bp, hw, bp->dev,
2830 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2831 			     data1, data2);
2832 		goto async_event_process_exit;
2833 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2834 		struct bnxt_rx_ring_info *rxr;
2835 		u16 grp_idx;
2836 
2837 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2838 			goto async_event_process_exit;
2839 
2840 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2841 			    BNXT_EVENT_RING_TYPE(data2), data1);
2842 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2843 			goto async_event_process_exit;
2844 
2845 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2846 		if (grp_idx == INVALID_HW_RING_ID) {
2847 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2848 				    data1);
2849 			goto async_event_process_exit;
2850 		}
2851 		rxr = bp->bnapi[grp_idx]->rx_ring;
2852 		bnxt_sched_reset_rxr(bp, rxr);
2853 		goto async_event_process_exit;
2854 	}
2855 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2856 		struct bnxt_fw_health *fw_health = bp->fw_health;
2857 
2858 		netif_notice(bp, hw, bp->dev,
2859 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2860 			     data1, data2);
2861 		if (fw_health) {
2862 			fw_health->echo_req_data1 = data1;
2863 			fw_health->echo_req_data2 = data2;
2864 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2865 			break;
2866 		}
2867 		goto async_event_process_exit;
2868 	}
2869 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2870 		bnxt_ptp_pps_event(bp, data1, data2);
2871 		goto async_event_process_exit;
2872 	}
2873 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2874 		if (bnxt_event_error_report(bp, data1, data2))
2875 			break;
2876 		goto async_event_process_exit;
2877 	}
2878 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2879 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2880 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2881 			if (BNXT_PTP_USE_RTC(bp)) {
2882 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2883 				unsigned long flags;
2884 				u64 ns;
2885 
2886 				if (!ptp)
2887 					goto async_event_process_exit;
2888 
2889 				bnxt_ptp_update_current_time(bp);
2890 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2891 				       BNXT_PHC_BITS) | ptp->current_time);
2892 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2893 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2894 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2895 			}
2896 			break;
2897 		}
2898 		goto async_event_process_exit;
2899 	}
2900 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2901 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2902 
2903 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2904 		goto async_event_process_exit;
2905 	}
2906 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2907 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2908 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2909 
2910 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2911 		goto async_event_process_exit;
2912 	}
2913 	default:
2914 		goto async_event_process_exit;
2915 	}
2916 	__bnxt_queue_sp_work(bp);
2917 async_event_process_exit:
2918 	bnxt_ulp_async_events(bp, cmpl);
2919 	return 0;
2920 }
2921 
2922 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2923 {
2924 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2925 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2926 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2927 				(struct hwrm_fwd_req_cmpl *)txcmp;
2928 
2929 	switch (cmpl_type) {
2930 	case CMPL_BASE_TYPE_HWRM_DONE:
2931 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2932 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2933 		break;
2934 
2935 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2936 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2937 
2938 		if ((vf_id < bp->pf.first_vf_id) ||
2939 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2940 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2941 				   vf_id);
2942 			return -EINVAL;
2943 		}
2944 
2945 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2946 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2947 		break;
2948 
2949 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2950 		bnxt_async_event_process(bp,
2951 					 (struct hwrm_async_event_cmpl *)txcmp);
2952 		break;
2953 
2954 	default:
2955 		break;
2956 	}
2957 
2958 	return 0;
2959 }
2960 
2961 static bool bnxt_vnic_is_active(struct bnxt *bp)
2962 {
2963 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
2964 
2965 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
2966 }
2967 
2968 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2969 {
2970 	struct bnxt_napi *bnapi = dev_instance;
2971 	struct bnxt *bp = bnapi->bp;
2972 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2973 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2974 
2975 	cpr->event_ctr++;
2976 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2977 	napi_schedule(&bnapi->napi);
2978 	return IRQ_HANDLED;
2979 }
2980 
2981 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2982 {
2983 	u32 raw_cons = cpr->cp_raw_cons;
2984 	u16 cons = RING_CMP(raw_cons);
2985 	struct tx_cmp *txcmp;
2986 
2987 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2988 
2989 	return TX_CMP_VALID(txcmp, raw_cons);
2990 }
2991 
2992 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2993 			    int budget)
2994 {
2995 	struct bnxt_napi *bnapi = cpr->bnapi;
2996 	u32 raw_cons = cpr->cp_raw_cons;
2997 	u32 cons;
2998 	int rx_pkts = 0;
2999 	u8 event = 0;
3000 	struct tx_cmp *txcmp;
3001 
3002 	cpr->has_more_work = 0;
3003 	cpr->had_work_done = 1;
3004 	while (1) {
3005 		u8 cmp_type;
3006 		int rc;
3007 
3008 		cons = RING_CMP(raw_cons);
3009 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3010 
3011 		if (!TX_CMP_VALID(txcmp, raw_cons))
3012 			break;
3013 
3014 		/* The valid test of the entry must be done first before
3015 		 * reading any further.
3016 		 */
3017 		dma_rmb();
3018 		cmp_type = TX_CMP_TYPE(txcmp);
3019 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
3020 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
3021 			u32 opaque = txcmp->tx_cmp_opaque;
3022 			struct bnxt_tx_ring_info *txr;
3023 			u16 tx_freed;
3024 
3025 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
3026 			event |= BNXT_TX_CMP_EVENT;
3027 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
3028 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
3029 			else
3030 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
3031 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
3032 				   bp->tx_ring_mask;
3033 			/* return full budget so NAPI will complete. */
3034 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
3035 				rx_pkts = budget;
3036 				raw_cons = NEXT_RAW_CMP(raw_cons);
3037 				if (budget)
3038 					cpr->has_more_work = 1;
3039 				break;
3040 			}
3041 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
3042 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
3043 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
3044 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
3045 			if (likely(budget))
3046 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3047 			else
3048 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
3049 							   &event);
3050 			if (likely(rc >= 0))
3051 				rx_pkts += rc;
3052 			/* Increment rx_pkts when rc is -ENOMEM to count towards
3053 			 * the NAPI budget.  Otherwise, we may potentially loop
3054 			 * here forever if we consistently cannot allocate
3055 			 * buffers.
3056 			 */
3057 			else if (rc == -ENOMEM && budget)
3058 				rx_pkts++;
3059 			else if (rc == -EBUSY)	/* partial completion */
3060 				break;
3061 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3062 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3063 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3064 			bnxt_hwrm_handler(bp, txcmp);
3065 		}
3066 		raw_cons = NEXT_RAW_CMP(raw_cons);
3067 
3068 		if (rx_pkts && rx_pkts == budget) {
3069 			cpr->has_more_work = 1;
3070 			break;
3071 		}
3072 	}
3073 
3074 	if (event & BNXT_REDIRECT_EVENT) {
3075 		xdp_do_flush();
3076 		event &= ~BNXT_REDIRECT_EVENT;
3077 	}
3078 
3079 	if (event & BNXT_TX_EVENT) {
3080 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3081 		u16 prod = txr->tx_prod;
3082 
3083 		/* Sync BD data before updating doorbell */
3084 		wmb();
3085 
3086 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3087 		event &= ~BNXT_TX_EVENT;
3088 	}
3089 
3090 	cpr->cp_raw_cons = raw_cons;
3091 	bnapi->events |= event;
3092 	return rx_pkts;
3093 }
3094 
3095 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3096 				  int budget)
3097 {
3098 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3099 		bnapi->tx_int(bp, bnapi, budget);
3100 
3101 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3102 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3103 
3104 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3105 		bnapi->events &= ~BNXT_RX_EVENT;
3106 	}
3107 	if (bnapi->events & BNXT_AGG_EVENT) {
3108 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3109 
3110 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3111 		bnapi->events &= ~BNXT_AGG_EVENT;
3112 	}
3113 }
3114 
3115 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3116 			  int budget)
3117 {
3118 	struct bnxt_napi *bnapi = cpr->bnapi;
3119 	int rx_pkts;
3120 
3121 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3122 
3123 	/* ACK completion ring before freeing tx ring and producing new
3124 	 * buffers in rx/agg rings to prevent overflowing the completion
3125 	 * ring.
3126 	 */
3127 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3128 
3129 	__bnxt_poll_work_done(bp, bnapi, budget);
3130 	return rx_pkts;
3131 }
3132 
3133 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3134 {
3135 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3136 	struct bnxt *bp = bnapi->bp;
3137 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3138 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3139 	struct tx_cmp *txcmp;
3140 	struct rx_cmp_ext *rxcmp1;
3141 	u32 cp_cons, tmp_raw_cons;
3142 	u32 raw_cons = cpr->cp_raw_cons;
3143 	bool flush_xdp = false;
3144 	u32 rx_pkts = 0;
3145 	u8 event = 0;
3146 
3147 	while (1) {
3148 		int rc;
3149 
3150 		cp_cons = RING_CMP(raw_cons);
3151 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3152 
3153 		if (!TX_CMP_VALID(txcmp, raw_cons))
3154 			break;
3155 
3156 		/* The valid test of the entry must be done first before
3157 		 * reading any further.
3158 		 */
3159 		dma_rmb();
3160 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3161 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3162 			cp_cons = RING_CMP(tmp_raw_cons);
3163 			rxcmp1 = (struct rx_cmp_ext *)
3164 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3165 
3166 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3167 				break;
3168 
3169 			/* force an error to recycle the buffer */
3170 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3171 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3172 
3173 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3174 			if (likely(rc == -EIO) && budget)
3175 				rx_pkts++;
3176 			else if (rc == -EBUSY)	/* partial completion */
3177 				break;
3178 			if (event & BNXT_REDIRECT_EVENT)
3179 				flush_xdp = true;
3180 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3181 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3182 			bnxt_hwrm_handler(bp, txcmp);
3183 		} else {
3184 			netdev_err(bp->dev,
3185 				   "Invalid completion received on special ring\n");
3186 		}
3187 		raw_cons = NEXT_RAW_CMP(raw_cons);
3188 
3189 		if (rx_pkts == budget)
3190 			break;
3191 	}
3192 
3193 	cpr->cp_raw_cons = raw_cons;
3194 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3195 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3196 
3197 	if (event & BNXT_AGG_EVENT)
3198 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3199 	if (flush_xdp)
3200 		xdp_do_flush();
3201 
3202 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3203 		napi_complete_done(napi, rx_pkts);
3204 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3205 	}
3206 	return rx_pkts;
3207 }
3208 
3209 static int bnxt_poll(struct napi_struct *napi, int budget)
3210 {
3211 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3212 	struct bnxt *bp = bnapi->bp;
3213 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3214 	int work_done = 0;
3215 
3216 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3217 		napi_complete(napi);
3218 		return 0;
3219 	}
3220 	while (1) {
3221 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3222 
3223 		if (work_done >= budget) {
3224 			if (!budget)
3225 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3226 			break;
3227 		}
3228 
3229 		if (!bnxt_has_work(bp, cpr)) {
3230 			if (napi_complete_done(napi, work_done))
3231 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3232 			break;
3233 		}
3234 	}
3235 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3236 		struct dim_sample dim_sample = {};
3237 
3238 		dim_update_sample(cpr->event_ctr,
3239 				  cpr->rx_packets,
3240 				  cpr->rx_bytes,
3241 				  &dim_sample);
3242 		net_dim(&cpr->dim, &dim_sample);
3243 	}
3244 	return work_done;
3245 }
3246 
3247 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3248 {
3249 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3250 	int i, work_done = 0;
3251 
3252 	for (i = 0; i < cpr->cp_ring_count; i++) {
3253 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3254 
3255 		if (cpr2->had_nqe_notify) {
3256 			work_done += __bnxt_poll_work(bp, cpr2,
3257 						      budget - work_done);
3258 			cpr->has_more_work |= cpr2->has_more_work;
3259 		}
3260 	}
3261 	return work_done;
3262 }
3263 
3264 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3265 				 u64 dbr_type, int budget)
3266 {
3267 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3268 	int i;
3269 
3270 	for (i = 0; i < cpr->cp_ring_count; i++) {
3271 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3272 		struct bnxt_db_info *db;
3273 
3274 		if (cpr2->had_work_done) {
3275 			u32 tgl = 0;
3276 
3277 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3278 				cpr2->had_nqe_notify = 0;
3279 				tgl = cpr2->toggle;
3280 			}
3281 			db = &cpr2->cp_db;
3282 			bnxt_writeq(bp,
3283 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3284 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3285 				    db->doorbell);
3286 			cpr2->had_work_done = 0;
3287 		}
3288 	}
3289 	__bnxt_poll_work_done(bp, bnapi, budget);
3290 }
3291 
3292 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3293 {
3294 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3295 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3296 	struct bnxt_cp_ring_info *cpr_rx;
3297 	u32 raw_cons = cpr->cp_raw_cons;
3298 	struct bnxt *bp = bnapi->bp;
3299 	struct nqe_cn *nqcmp;
3300 	int work_done = 0;
3301 	u32 cons;
3302 
3303 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3304 		napi_complete(napi);
3305 		return 0;
3306 	}
3307 	if (cpr->has_more_work) {
3308 		cpr->has_more_work = 0;
3309 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3310 	}
3311 	while (1) {
3312 		u16 type;
3313 
3314 		cons = RING_CMP(raw_cons);
3315 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3316 
3317 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3318 			if (cpr->has_more_work)
3319 				break;
3320 
3321 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3322 					     budget);
3323 			cpr->cp_raw_cons = raw_cons;
3324 			if (napi_complete_done(napi, work_done))
3325 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3326 						  cpr->cp_raw_cons);
3327 			goto poll_done;
3328 		}
3329 
3330 		/* The valid test of the entry must be done first before
3331 		 * reading any further.
3332 		 */
3333 		dma_rmb();
3334 
3335 		type = le16_to_cpu(nqcmp->type);
3336 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3337 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3338 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3339 			struct bnxt_cp_ring_info *cpr2;
3340 
3341 			/* No more budget for RX work */
3342 			if (budget && work_done >= budget &&
3343 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3344 				break;
3345 
3346 			idx = BNXT_NQ_HDL_IDX(idx);
3347 			cpr2 = &cpr->cp_ring_arr[idx];
3348 			cpr2->had_nqe_notify = 1;
3349 			cpr2->toggle = NQE_CN_TOGGLE(type);
3350 			work_done += __bnxt_poll_work(bp, cpr2,
3351 						      budget - work_done);
3352 			cpr->has_more_work |= cpr2->has_more_work;
3353 		} else {
3354 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3355 		}
3356 		raw_cons = NEXT_RAW_CMP(raw_cons);
3357 	}
3358 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3359 	if (raw_cons != cpr->cp_raw_cons) {
3360 		cpr->cp_raw_cons = raw_cons;
3361 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3362 	}
3363 poll_done:
3364 	cpr_rx = &cpr->cp_ring_arr[0];
3365 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3366 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3367 		struct dim_sample dim_sample = {};
3368 
3369 		dim_update_sample(cpr->event_ctr,
3370 				  cpr_rx->rx_packets,
3371 				  cpr_rx->rx_bytes,
3372 				  &dim_sample);
3373 		net_dim(&cpr->dim, &dim_sample);
3374 	}
3375 	return work_done;
3376 }
3377 
3378 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp,
3379 				       struct bnxt_tx_ring_info *txr, int idx)
3380 {
3381 	int i, max_idx;
3382 	struct pci_dev *pdev = bp->pdev;
3383 
3384 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3385 
3386 	for (i = 0; i < max_idx;) {
3387 		struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i];
3388 		struct sk_buff *skb;
3389 		int j, last;
3390 
3391 		if (idx  < bp->tx_nr_rings_xdp &&
3392 		    tx_buf->action == XDP_REDIRECT) {
3393 			dma_unmap_single(&pdev->dev,
3394 					 dma_unmap_addr(tx_buf, mapping),
3395 					 dma_unmap_len(tx_buf, len),
3396 					 DMA_TO_DEVICE);
3397 			xdp_return_frame(tx_buf->xdpf);
3398 			tx_buf->action = 0;
3399 			tx_buf->xdpf = NULL;
3400 			i++;
3401 			continue;
3402 		}
3403 
3404 		skb = tx_buf->skb;
3405 		if (!skb) {
3406 			i++;
3407 			continue;
3408 		}
3409 
3410 		tx_buf->skb = NULL;
3411 
3412 		if (tx_buf->is_push) {
3413 			dev_kfree_skb(skb);
3414 			i += 2;
3415 			continue;
3416 		}
3417 
3418 		dma_unmap_single(&pdev->dev,
3419 				 dma_unmap_addr(tx_buf, mapping),
3420 				 skb_headlen(skb),
3421 				 DMA_TO_DEVICE);
3422 
3423 		last = tx_buf->nr_frags;
3424 		i += 2;
3425 		for (j = 0; j < last; j++, i++) {
3426 			int ring_idx = i & bp->tx_ring_mask;
3427 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
3428 
3429 			tx_buf = &txr->tx_buf_ring[ring_idx];
3430 			netmem_dma_unmap_page_attrs(&pdev->dev,
3431 						    dma_unmap_addr(tx_buf,
3432 								   mapping),
3433 						    skb_frag_size(frag),
3434 						    DMA_TO_DEVICE, 0);
3435 		}
3436 		dev_kfree_skb(skb);
3437 	}
3438 	netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx));
3439 }
3440 
3441 static void bnxt_free_tx_skbs(struct bnxt *bp)
3442 {
3443 	int i;
3444 
3445 	if (!bp->tx_ring)
3446 		return;
3447 
3448 	for (i = 0; i < bp->tx_nr_rings; i++) {
3449 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3450 
3451 		if (!txr->tx_buf_ring)
3452 			continue;
3453 
3454 		bnxt_free_one_tx_ring_skbs(bp, txr, i);
3455 	}
3456 
3457 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
3458 		bnxt_ptp_free_txts_skbs(bp->ptp_cfg);
3459 }
3460 
3461 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3462 {
3463 	int i, max_idx;
3464 
3465 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3466 
3467 	for (i = 0; i < max_idx; i++) {
3468 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3469 		void *data = rx_buf->data;
3470 
3471 		if (!data)
3472 			continue;
3473 
3474 		rx_buf->data = NULL;
3475 		if (BNXT_RX_PAGE_MODE(bp))
3476 			page_pool_recycle_direct(rxr->page_pool, data);
3477 		else
3478 			page_pool_free_va(rxr->head_pool, data, true);
3479 	}
3480 }
3481 
3482 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3483 {
3484 	int i, max_idx;
3485 
3486 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3487 
3488 	for (i = 0; i < max_idx; i++) {
3489 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3490 		netmem_ref netmem = rx_agg_buf->netmem;
3491 
3492 		if (!netmem)
3493 			continue;
3494 
3495 		rx_agg_buf->netmem = 0;
3496 		__clear_bit(i, rxr->rx_agg_bmap);
3497 
3498 		page_pool_recycle_direct_netmem(rxr->page_pool, netmem);
3499 	}
3500 }
3501 
3502 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3503 					struct bnxt_rx_ring_info *rxr)
3504 {
3505 	int i;
3506 
3507 	for (i = 0; i < bp->max_tpa; i++) {
3508 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3509 		u8 *data = tpa_info->data;
3510 
3511 		if (!data)
3512 			continue;
3513 
3514 		tpa_info->data = NULL;
3515 		page_pool_free_va(rxr->head_pool, data, false);
3516 	}
3517 }
3518 
3519 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3520 				       struct bnxt_rx_ring_info *rxr)
3521 {
3522 	struct bnxt_tpa_idx_map *map;
3523 
3524 	if (!rxr->rx_tpa)
3525 		goto skip_rx_tpa_free;
3526 
3527 	bnxt_free_one_tpa_info_data(bp, rxr);
3528 
3529 skip_rx_tpa_free:
3530 	if (!rxr->rx_buf_ring)
3531 		goto skip_rx_buf_free;
3532 
3533 	bnxt_free_one_rx_ring(bp, rxr);
3534 
3535 skip_rx_buf_free:
3536 	if (!rxr->rx_agg_ring)
3537 		goto skip_rx_agg_free;
3538 
3539 	bnxt_free_one_rx_agg_ring(bp, rxr);
3540 
3541 skip_rx_agg_free:
3542 	map = rxr->rx_tpa_idx_map;
3543 	if (map)
3544 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3545 }
3546 
3547 static void bnxt_free_rx_skbs(struct bnxt *bp)
3548 {
3549 	int i;
3550 
3551 	if (!bp->rx_ring)
3552 		return;
3553 
3554 	for (i = 0; i < bp->rx_nr_rings; i++)
3555 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3556 }
3557 
3558 static void bnxt_free_skbs(struct bnxt *bp)
3559 {
3560 	bnxt_free_tx_skbs(bp);
3561 	bnxt_free_rx_skbs(bp);
3562 }
3563 
3564 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3565 {
3566 	u8 init_val = ctxm->init_value;
3567 	u16 offset = ctxm->init_offset;
3568 	u8 *p2 = p;
3569 	int i;
3570 
3571 	if (!init_val)
3572 		return;
3573 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3574 		memset(p, init_val, len);
3575 		return;
3576 	}
3577 	for (i = 0; i < len; i += ctxm->entry_size)
3578 		*(p2 + i + offset) = init_val;
3579 }
3580 
3581 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3582 			       void *buf, size_t offset, size_t head,
3583 			       size_t tail)
3584 {
3585 	int i, head_page, start_idx, source_offset;
3586 	size_t len, rem_len, total_len, max_bytes;
3587 
3588 	head_page = head / rmem->page_size;
3589 	source_offset = head % rmem->page_size;
3590 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3591 	if (!total_len)
3592 		total_len = MAX_CTX_BYTES;
3593 	start_idx = head_page % MAX_CTX_PAGES;
3594 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3595 		    source_offset;
3596 	total_len = min(total_len, max_bytes);
3597 	rem_len = total_len;
3598 
3599 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3600 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3601 		if (buf)
3602 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3603 			       len);
3604 		offset += len;
3605 		rem_len -= len;
3606 	}
3607 	return total_len;
3608 }
3609 
3610 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3611 {
3612 	struct pci_dev *pdev = bp->pdev;
3613 	int i;
3614 
3615 	if (!rmem->pg_arr)
3616 		goto skip_pages;
3617 
3618 	for (i = 0; i < rmem->nr_pages; i++) {
3619 		if (!rmem->pg_arr[i])
3620 			continue;
3621 
3622 		dma_free_coherent(&pdev->dev, rmem->page_size,
3623 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3624 
3625 		rmem->pg_arr[i] = NULL;
3626 	}
3627 skip_pages:
3628 	if (rmem->pg_tbl) {
3629 		size_t pg_tbl_size = rmem->nr_pages * 8;
3630 
3631 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3632 			pg_tbl_size = rmem->page_size;
3633 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3634 				  rmem->pg_tbl, rmem->pg_tbl_map);
3635 		rmem->pg_tbl = NULL;
3636 	}
3637 	if (rmem->vmem_size && *rmem->vmem) {
3638 		vfree(*rmem->vmem);
3639 		*rmem->vmem = NULL;
3640 	}
3641 }
3642 
3643 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3644 {
3645 	struct pci_dev *pdev = bp->pdev;
3646 	u64 valid_bit = 0;
3647 	int i;
3648 
3649 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3650 		valid_bit = PTU_PTE_VALID;
3651 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3652 		size_t pg_tbl_size = rmem->nr_pages * 8;
3653 
3654 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3655 			pg_tbl_size = rmem->page_size;
3656 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3657 						  &rmem->pg_tbl_map,
3658 						  GFP_KERNEL);
3659 		if (!rmem->pg_tbl)
3660 			return -ENOMEM;
3661 	}
3662 
3663 	for (i = 0; i < rmem->nr_pages; i++) {
3664 		u64 extra_bits = valid_bit;
3665 
3666 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3667 						     rmem->page_size,
3668 						     &rmem->dma_arr[i],
3669 						     GFP_KERNEL);
3670 		if (!rmem->pg_arr[i])
3671 			return -ENOMEM;
3672 
3673 		if (rmem->ctx_mem)
3674 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3675 					  rmem->page_size);
3676 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3677 			if (i == rmem->nr_pages - 2 &&
3678 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3679 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3680 			else if (i == rmem->nr_pages - 1 &&
3681 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3682 				extra_bits |= PTU_PTE_LAST;
3683 			rmem->pg_tbl[i] =
3684 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3685 		}
3686 	}
3687 
3688 	if (rmem->vmem_size) {
3689 		*rmem->vmem = vzalloc(rmem->vmem_size);
3690 		if (!(*rmem->vmem))
3691 			return -ENOMEM;
3692 	}
3693 	return 0;
3694 }
3695 
3696 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3697 				   struct bnxt_rx_ring_info *rxr)
3698 {
3699 	int i;
3700 
3701 	kfree(rxr->rx_tpa_idx_map);
3702 	rxr->rx_tpa_idx_map = NULL;
3703 	if (rxr->rx_tpa) {
3704 		for (i = 0; i < bp->max_tpa; i++) {
3705 			kfree(rxr->rx_tpa[i].agg_arr);
3706 			rxr->rx_tpa[i].agg_arr = NULL;
3707 		}
3708 	}
3709 	kfree(rxr->rx_tpa);
3710 	rxr->rx_tpa = NULL;
3711 }
3712 
3713 static void bnxt_free_tpa_info(struct bnxt *bp)
3714 {
3715 	int i;
3716 
3717 	for (i = 0; i < bp->rx_nr_rings; i++) {
3718 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3719 
3720 		bnxt_free_one_tpa_info(bp, rxr);
3721 	}
3722 }
3723 
3724 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3725 				   struct bnxt_rx_ring_info *rxr)
3726 {
3727 	struct rx_agg_cmp *agg;
3728 	int i;
3729 
3730 	rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3731 			      GFP_KERNEL);
3732 	if (!rxr->rx_tpa)
3733 		return -ENOMEM;
3734 
3735 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3736 		return 0;
3737 	for (i = 0; i < bp->max_tpa; i++) {
3738 		agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3739 		if (!agg)
3740 			return -ENOMEM;
3741 		rxr->rx_tpa[i].agg_arr = agg;
3742 	}
3743 	rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3744 				      GFP_KERNEL);
3745 	if (!rxr->rx_tpa_idx_map)
3746 		return -ENOMEM;
3747 
3748 	return 0;
3749 }
3750 
3751 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3752 {
3753 	int i, rc;
3754 
3755 	bp->max_tpa = MAX_TPA;
3756 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3757 		if (!bp->max_tpa_v2)
3758 			return 0;
3759 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3760 	}
3761 
3762 	for (i = 0; i < bp->rx_nr_rings; i++) {
3763 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3764 
3765 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3766 		if (rc)
3767 			return rc;
3768 	}
3769 	return 0;
3770 }
3771 
3772 static void bnxt_free_rx_rings(struct bnxt *bp)
3773 {
3774 	int i;
3775 
3776 	if (!bp->rx_ring)
3777 		return;
3778 
3779 	bnxt_free_tpa_info(bp);
3780 	for (i = 0; i < bp->rx_nr_rings; i++) {
3781 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3782 		struct bnxt_ring_struct *ring;
3783 
3784 		if (rxr->xdp_prog)
3785 			bpf_prog_put(rxr->xdp_prog);
3786 
3787 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3788 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3789 
3790 		page_pool_destroy(rxr->page_pool);
3791 		if (bnxt_separate_head_pool(rxr))
3792 			page_pool_destroy(rxr->head_pool);
3793 		rxr->page_pool = rxr->head_pool = NULL;
3794 
3795 		kfree(rxr->rx_agg_bmap);
3796 		rxr->rx_agg_bmap = NULL;
3797 
3798 		ring = &rxr->rx_ring_struct;
3799 		bnxt_free_ring(bp, &ring->ring_mem);
3800 
3801 		ring = &rxr->rx_agg_ring_struct;
3802 		bnxt_free_ring(bp, &ring->ring_mem);
3803 	}
3804 }
3805 
3806 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3807 				   struct bnxt_rx_ring_info *rxr,
3808 				   int numa_node)
3809 {
3810 	struct page_pool_params pp = { 0 };
3811 	struct page_pool *pool;
3812 
3813 	pp.pool_size = bp->rx_agg_ring_size;
3814 	if (BNXT_RX_PAGE_MODE(bp))
3815 		pp.pool_size += bp->rx_ring_size;
3816 	pp.nid = numa_node;
3817 	pp.napi = &rxr->bnapi->napi;
3818 	pp.netdev = bp->dev;
3819 	pp.dev = &bp->pdev->dev;
3820 	pp.dma_dir = bp->rx_dir;
3821 	pp.max_len = PAGE_SIZE;
3822 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV |
3823 		   PP_FLAG_ALLOW_UNREADABLE_NETMEM;
3824 	pp.queue_idx = rxr->bnapi->index;
3825 
3826 	pool = page_pool_create(&pp);
3827 	if (IS_ERR(pool))
3828 		return PTR_ERR(pool);
3829 	rxr->page_pool = pool;
3830 
3831 	rxr->need_head_pool = page_pool_is_unreadable(pool);
3832 	if (bnxt_separate_head_pool(rxr)) {
3833 		pp.pool_size = max(bp->rx_ring_size, 1024);
3834 		pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3835 		pool = page_pool_create(&pp);
3836 		if (IS_ERR(pool))
3837 			goto err_destroy_pp;
3838 	}
3839 	rxr->head_pool = pool;
3840 
3841 	return 0;
3842 
3843 err_destroy_pp:
3844 	page_pool_destroy(rxr->page_pool);
3845 	rxr->page_pool = NULL;
3846 	return PTR_ERR(pool);
3847 }
3848 
3849 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3850 {
3851 	u16 mem_size;
3852 
3853 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3854 	mem_size = rxr->rx_agg_bmap_size / 8;
3855 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3856 	if (!rxr->rx_agg_bmap)
3857 		return -ENOMEM;
3858 
3859 	return 0;
3860 }
3861 
3862 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3863 {
3864 	int numa_node = dev_to_node(&bp->pdev->dev);
3865 	int i, rc = 0, agg_rings = 0, cpu;
3866 
3867 	if (!bp->rx_ring)
3868 		return -ENOMEM;
3869 
3870 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3871 		agg_rings = 1;
3872 
3873 	for (i = 0; i < bp->rx_nr_rings; i++) {
3874 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3875 		struct bnxt_ring_struct *ring;
3876 		int cpu_node;
3877 
3878 		ring = &rxr->rx_ring_struct;
3879 
3880 		cpu = cpumask_local_spread(i, numa_node);
3881 		cpu_node = cpu_to_node(cpu);
3882 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3883 			   i, cpu_node);
3884 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3885 		if (rc)
3886 			return rc;
3887 
3888 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3889 		if (rc < 0)
3890 			return rc;
3891 
3892 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3893 						MEM_TYPE_PAGE_POOL,
3894 						rxr->page_pool);
3895 		if (rc) {
3896 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3897 			return rc;
3898 		}
3899 
3900 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3901 		if (rc)
3902 			return rc;
3903 
3904 		ring->grp_idx = i;
3905 		if (agg_rings) {
3906 			ring = &rxr->rx_agg_ring_struct;
3907 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3908 			if (rc)
3909 				return rc;
3910 
3911 			ring->grp_idx = i;
3912 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3913 			if (rc)
3914 				return rc;
3915 		}
3916 	}
3917 	if (bp->flags & BNXT_FLAG_TPA)
3918 		rc = bnxt_alloc_tpa_info(bp);
3919 	return rc;
3920 }
3921 
3922 static void bnxt_free_tx_rings(struct bnxt *bp)
3923 {
3924 	int i;
3925 	struct pci_dev *pdev = bp->pdev;
3926 
3927 	if (!bp->tx_ring)
3928 		return;
3929 
3930 	for (i = 0; i < bp->tx_nr_rings; i++) {
3931 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3932 		struct bnxt_ring_struct *ring;
3933 
3934 		if (txr->tx_push) {
3935 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3936 					  txr->tx_push, txr->tx_push_mapping);
3937 			txr->tx_push = NULL;
3938 		}
3939 
3940 		ring = &txr->tx_ring_struct;
3941 
3942 		bnxt_free_ring(bp, &ring->ring_mem);
3943 	}
3944 }
3945 
3946 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3947 	((tc) * (bp)->tx_nr_rings_per_tc)
3948 
3949 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3950 	((tx) % (bp)->tx_nr_rings_per_tc)
3951 
3952 #define BNXT_RING_TO_TC(bp, tx)		\
3953 	((tx) / (bp)->tx_nr_rings_per_tc)
3954 
3955 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3956 {
3957 	int i, j, rc;
3958 	struct pci_dev *pdev = bp->pdev;
3959 
3960 	bp->tx_push_size = 0;
3961 	if (bp->tx_push_thresh) {
3962 		int push_size;
3963 
3964 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3965 					bp->tx_push_thresh);
3966 
3967 		if (push_size > 256) {
3968 			push_size = 0;
3969 			bp->tx_push_thresh = 0;
3970 		}
3971 
3972 		bp->tx_push_size = push_size;
3973 	}
3974 
3975 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3976 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3977 		struct bnxt_ring_struct *ring;
3978 		u8 qidx;
3979 
3980 		ring = &txr->tx_ring_struct;
3981 
3982 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3983 		if (rc)
3984 			return rc;
3985 
3986 		ring->grp_idx = txr->bnapi->index;
3987 		if (bp->tx_push_size) {
3988 			dma_addr_t mapping;
3989 
3990 			/* One pre-allocated DMA buffer to backup
3991 			 * TX push operation
3992 			 */
3993 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3994 						bp->tx_push_size,
3995 						&txr->tx_push_mapping,
3996 						GFP_KERNEL);
3997 
3998 			if (!txr->tx_push)
3999 				return -ENOMEM;
4000 
4001 			mapping = txr->tx_push_mapping +
4002 				sizeof(struct tx_push_bd);
4003 			txr->data_mapping = cpu_to_le64(mapping);
4004 		}
4005 		qidx = bp->tc_to_qidx[j];
4006 		ring->queue_id = bp->q_info[qidx].queue_id;
4007 		spin_lock_init(&txr->xdp_tx_lock);
4008 		if (i < bp->tx_nr_rings_xdp)
4009 			continue;
4010 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
4011 			j++;
4012 	}
4013 	return 0;
4014 }
4015 
4016 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
4017 {
4018 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4019 
4020 	kfree(cpr->cp_desc_ring);
4021 	cpr->cp_desc_ring = NULL;
4022 	ring->ring_mem.pg_arr = NULL;
4023 	kfree(cpr->cp_desc_mapping);
4024 	cpr->cp_desc_mapping = NULL;
4025 	ring->ring_mem.dma_arr = NULL;
4026 }
4027 
4028 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
4029 {
4030 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
4031 	if (!cpr->cp_desc_ring)
4032 		return -ENOMEM;
4033 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
4034 				       GFP_KERNEL);
4035 	if (!cpr->cp_desc_mapping)
4036 		return -ENOMEM;
4037 	return 0;
4038 }
4039 
4040 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
4041 {
4042 	int i;
4043 
4044 	if (!bp->bnapi)
4045 		return;
4046 	for (i = 0; i < bp->cp_nr_rings; i++) {
4047 		struct bnxt_napi *bnapi = bp->bnapi[i];
4048 
4049 		if (!bnapi)
4050 			continue;
4051 		bnxt_free_cp_arrays(&bnapi->cp_ring);
4052 	}
4053 }
4054 
4055 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
4056 {
4057 	int i, n = bp->cp_nr_pages;
4058 
4059 	for (i = 0; i < bp->cp_nr_rings; i++) {
4060 		struct bnxt_napi *bnapi = bp->bnapi[i];
4061 		int rc;
4062 
4063 		if (!bnapi)
4064 			continue;
4065 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
4066 		if (rc)
4067 			return rc;
4068 	}
4069 	return 0;
4070 }
4071 
4072 static void bnxt_free_cp_rings(struct bnxt *bp)
4073 {
4074 	int i;
4075 
4076 	if (!bp->bnapi)
4077 		return;
4078 
4079 	for (i = 0; i < bp->cp_nr_rings; i++) {
4080 		struct bnxt_napi *bnapi = bp->bnapi[i];
4081 		struct bnxt_cp_ring_info *cpr;
4082 		struct bnxt_ring_struct *ring;
4083 		int j;
4084 
4085 		if (!bnapi)
4086 			continue;
4087 
4088 		cpr = &bnapi->cp_ring;
4089 		ring = &cpr->cp_ring_struct;
4090 
4091 		bnxt_free_ring(bp, &ring->ring_mem);
4092 
4093 		if (!cpr->cp_ring_arr)
4094 			continue;
4095 
4096 		for (j = 0; j < cpr->cp_ring_count; j++) {
4097 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4098 
4099 			ring = &cpr2->cp_ring_struct;
4100 			bnxt_free_ring(bp, &ring->ring_mem);
4101 			bnxt_free_cp_arrays(cpr2);
4102 		}
4103 		kfree(cpr->cp_ring_arr);
4104 		cpr->cp_ring_arr = NULL;
4105 		cpr->cp_ring_count = 0;
4106 	}
4107 }
4108 
4109 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4110 				  struct bnxt_cp_ring_info *cpr)
4111 {
4112 	struct bnxt_ring_mem_info *rmem;
4113 	struct bnxt_ring_struct *ring;
4114 	int rc;
4115 
4116 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4117 	if (rc) {
4118 		bnxt_free_cp_arrays(cpr);
4119 		return -ENOMEM;
4120 	}
4121 	ring = &cpr->cp_ring_struct;
4122 	rmem = &ring->ring_mem;
4123 	rmem->nr_pages = bp->cp_nr_pages;
4124 	rmem->page_size = HW_CMPD_RING_SIZE;
4125 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4126 	rmem->dma_arr = cpr->cp_desc_mapping;
4127 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4128 	rc = bnxt_alloc_ring(bp, rmem);
4129 	if (rc) {
4130 		bnxt_free_ring(bp, rmem);
4131 		bnxt_free_cp_arrays(cpr);
4132 	}
4133 	return rc;
4134 }
4135 
4136 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4137 {
4138 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4139 	int i, j, rc, ulp_msix;
4140 	int tcs = bp->num_tc;
4141 
4142 	if (!tcs)
4143 		tcs = 1;
4144 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4145 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4146 		struct bnxt_napi *bnapi = bp->bnapi[i];
4147 		struct bnxt_cp_ring_info *cpr, *cpr2;
4148 		struct bnxt_ring_struct *ring;
4149 		int cp_count = 0, k;
4150 		int rx = 0, tx = 0;
4151 
4152 		if (!bnapi)
4153 			continue;
4154 
4155 		cpr = &bnapi->cp_ring;
4156 		cpr->bnapi = bnapi;
4157 		ring = &cpr->cp_ring_struct;
4158 
4159 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4160 		if (rc)
4161 			return rc;
4162 
4163 		ring->map_idx = ulp_msix + i;
4164 
4165 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4166 			continue;
4167 
4168 		if (i < bp->rx_nr_rings) {
4169 			cp_count++;
4170 			rx = 1;
4171 		}
4172 		if (i < bp->tx_nr_rings_xdp) {
4173 			cp_count++;
4174 			tx = 1;
4175 		} else if ((sh && i < bp->tx_nr_rings) ||
4176 			 (!sh && i >= bp->rx_nr_rings)) {
4177 			cp_count += tcs;
4178 			tx = 1;
4179 		}
4180 
4181 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
4182 					   GFP_KERNEL);
4183 		if (!cpr->cp_ring_arr)
4184 			return -ENOMEM;
4185 		cpr->cp_ring_count = cp_count;
4186 
4187 		for (k = 0; k < cp_count; k++) {
4188 			cpr2 = &cpr->cp_ring_arr[k];
4189 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4190 			if (rc)
4191 				return rc;
4192 			cpr2->bnapi = bnapi;
4193 			cpr2->sw_stats = cpr->sw_stats;
4194 			cpr2->cp_idx = k;
4195 			if (!k && rx) {
4196 				bp->rx_ring[i].rx_cpr = cpr2;
4197 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4198 			} else {
4199 				int n, tc = k - rx;
4200 
4201 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4202 				bp->tx_ring[n].tx_cpr = cpr2;
4203 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4204 			}
4205 		}
4206 		if (tx)
4207 			j++;
4208 	}
4209 	return 0;
4210 }
4211 
4212 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4213 				     struct bnxt_rx_ring_info *rxr)
4214 {
4215 	struct bnxt_ring_mem_info *rmem;
4216 	struct bnxt_ring_struct *ring;
4217 
4218 	ring = &rxr->rx_ring_struct;
4219 	rmem = &ring->ring_mem;
4220 	rmem->nr_pages = bp->rx_nr_pages;
4221 	rmem->page_size = HW_RXBD_RING_SIZE;
4222 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4223 	rmem->dma_arr = rxr->rx_desc_mapping;
4224 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4225 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4226 
4227 	ring = &rxr->rx_agg_ring_struct;
4228 	rmem = &ring->ring_mem;
4229 	rmem->nr_pages = bp->rx_agg_nr_pages;
4230 	rmem->page_size = HW_RXBD_RING_SIZE;
4231 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4232 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4233 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4234 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4235 }
4236 
4237 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4238 				      struct bnxt_rx_ring_info *rxr)
4239 {
4240 	struct bnxt_ring_mem_info *rmem;
4241 	struct bnxt_ring_struct *ring;
4242 	int i;
4243 
4244 	rxr->page_pool->p.napi = NULL;
4245 	rxr->page_pool = NULL;
4246 	rxr->head_pool->p.napi = NULL;
4247 	rxr->head_pool = NULL;
4248 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4249 
4250 	ring = &rxr->rx_ring_struct;
4251 	rmem = &ring->ring_mem;
4252 	rmem->pg_tbl = NULL;
4253 	rmem->pg_tbl_map = 0;
4254 	for (i = 0; i < rmem->nr_pages; i++) {
4255 		rmem->pg_arr[i] = NULL;
4256 		rmem->dma_arr[i] = 0;
4257 	}
4258 	*rmem->vmem = NULL;
4259 
4260 	ring = &rxr->rx_agg_ring_struct;
4261 	rmem = &ring->ring_mem;
4262 	rmem->pg_tbl = NULL;
4263 	rmem->pg_tbl_map = 0;
4264 	for (i = 0; i < rmem->nr_pages; i++) {
4265 		rmem->pg_arr[i] = NULL;
4266 		rmem->dma_arr[i] = 0;
4267 	}
4268 	*rmem->vmem = NULL;
4269 }
4270 
4271 static void bnxt_init_ring_struct(struct bnxt *bp)
4272 {
4273 	int i, j;
4274 
4275 	for (i = 0; i < bp->cp_nr_rings; i++) {
4276 		struct bnxt_napi *bnapi = bp->bnapi[i];
4277 		struct bnxt_ring_mem_info *rmem;
4278 		struct bnxt_cp_ring_info *cpr;
4279 		struct bnxt_rx_ring_info *rxr;
4280 		struct bnxt_tx_ring_info *txr;
4281 		struct bnxt_ring_struct *ring;
4282 
4283 		if (!bnapi)
4284 			continue;
4285 
4286 		cpr = &bnapi->cp_ring;
4287 		ring = &cpr->cp_ring_struct;
4288 		rmem = &ring->ring_mem;
4289 		rmem->nr_pages = bp->cp_nr_pages;
4290 		rmem->page_size = HW_CMPD_RING_SIZE;
4291 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4292 		rmem->dma_arr = cpr->cp_desc_mapping;
4293 		rmem->vmem_size = 0;
4294 
4295 		rxr = bnapi->rx_ring;
4296 		if (!rxr)
4297 			goto skip_rx;
4298 
4299 		ring = &rxr->rx_ring_struct;
4300 		rmem = &ring->ring_mem;
4301 		rmem->nr_pages = bp->rx_nr_pages;
4302 		rmem->page_size = HW_RXBD_RING_SIZE;
4303 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4304 		rmem->dma_arr = rxr->rx_desc_mapping;
4305 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4306 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4307 
4308 		ring = &rxr->rx_agg_ring_struct;
4309 		rmem = &ring->ring_mem;
4310 		rmem->nr_pages = bp->rx_agg_nr_pages;
4311 		rmem->page_size = HW_RXBD_RING_SIZE;
4312 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4313 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4314 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4315 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4316 
4317 skip_rx:
4318 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4319 			ring = &txr->tx_ring_struct;
4320 			rmem = &ring->ring_mem;
4321 			rmem->nr_pages = bp->tx_nr_pages;
4322 			rmem->page_size = HW_TXBD_RING_SIZE;
4323 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4324 			rmem->dma_arr = txr->tx_desc_mapping;
4325 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4326 			rmem->vmem = (void **)&txr->tx_buf_ring;
4327 		}
4328 	}
4329 }
4330 
4331 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4332 {
4333 	int i;
4334 	u32 prod;
4335 	struct rx_bd **rx_buf_ring;
4336 
4337 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4338 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4339 		int j;
4340 		struct rx_bd *rxbd;
4341 
4342 		rxbd = rx_buf_ring[i];
4343 		if (!rxbd)
4344 			continue;
4345 
4346 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4347 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4348 			rxbd->rx_bd_opaque = prod;
4349 		}
4350 	}
4351 }
4352 
4353 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4354 				       struct bnxt_rx_ring_info *rxr,
4355 				       int ring_nr)
4356 {
4357 	u32 prod;
4358 	int i;
4359 
4360 	prod = rxr->rx_prod;
4361 	for (i = 0; i < bp->rx_ring_size; i++) {
4362 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4363 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4364 				    ring_nr, i, bp->rx_ring_size);
4365 			break;
4366 		}
4367 		prod = NEXT_RX(prod);
4368 	}
4369 	rxr->rx_prod = prod;
4370 }
4371 
4372 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp,
4373 					  struct bnxt_rx_ring_info *rxr,
4374 					  int ring_nr)
4375 {
4376 	u32 prod;
4377 	int i;
4378 
4379 	prod = rxr->rx_agg_prod;
4380 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4381 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) {
4382 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4383 				    ring_nr, i, bp->rx_ring_size);
4384 			break;
4385 		}
4386 		prod = NEXT_RX_AGG(prod);
4387 	}
4388 	rxr->rx_agg_prod = prod;
4389 }
4390 
4391 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4392 					struct bnxt_rx_ring_info *rxr)
4393 {
4394 	dma_addr_t mapping;
4395 	u8 *data;
4396 	int i;
4397 
4398 	for (i = 0; i < bp->max_tpa; i++) {
4399 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4400 					    GFP_KERNEL);
4401 		if (!data)
4402 			return -ENOMEM;
4403 
4404 		rxr->rx_tpa[i].data = data;
4405 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4406 		rxr->rx_tpa[i].mapping = mapping;
4407 	}
4408 
4409 	return 0;
4410 }
4411 
4412 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4413 {
4414 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4415 	int rc;
4416 
4417 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4418 
4419 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4420 		return 0;
4421 
4422 	bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr);
4423 
4424 	if (rxr->rx_tpa) {
4425 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4426 		if (rc)
4427 			return rc;
4428 	}
4429 	return 0;
4430 }
4431 
4432 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4433 				       struct bnxt_rx_ring_info *rxr)
4434 {
4435 	struct bnxt_ring_struct *ring;
4436 	u32 type;
4437 
4438 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4439 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4440 
4441 	if (NET_IP_ALIGN == 2)
4442 		type |= RX_BD_FLAGS_SOP;
4443 
4444 	ring = &rxr->rx_ring_struct;
4445 	bnxt_init_rxbd_pages(ring, type);
4446 	ring->fw_ring_id = INVALID_HW_RING_ID;
4447 }
4448 
4449 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4450 					   struct bnxt_rx_ring_info *rxr)
4451 {
4452 	struct bnxt_ring_struct *ring;
4453 	u32 type;
4454 
4455 	ring = &rxr->rx_agg_ring_struct;
4456 	ring->fw_ring_id = INVALID_HW_RING_ID;
4457 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4458 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4459 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4460 
4461 		bnxt_init_rxbd_pages(ring, type);
4462 	}
4463 }
4464 
4465 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4466 {
4467 	struct bnxt_rx_ring_info *rxr;
4468 
4469 	rxr = &bp->rx_ring[ring_nr];
4470 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4471 
4472 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4473 			     &rxr->bnapi->napi);
4474 
4475 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4476 		bpf_prog_add(bp->xdp_prog, 1);
4477 		rxr->xdp_prog = bp->xdp_prog;
4478 	}
4479 
4480 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4481 
4482 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4483 }
4484 
4485 static void bnxt_init_cp_rings(struct bnxt *bp)
4486 {
4487 	int i, j;
4488 
4489 	for (i = 0; i < bp->cp_nr_rings; i++) {
4490 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4491 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4492 
4493 		ring->fw_ring_id = INVALID_HW_RING_ID;
4494 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4495 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4496 		if (!cpr->cp_ring_arr)
4497 			continue;
4498 		for (j = 0; j < cpr->cp_ring_count; j++) {
4499 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4500 
4501 			ring = &cpr2->cp_ring_struct;
4502 			ring->fw_ring_id = INVALID_HW_RING_ID;
4503 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4504 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4505 		}
4506 	}
4507 }
4508 
4509 static int bnxt_init_rx_rings(struct bnxt *bp)
4510 {
4511 	int i, rc = 0;
4512 
4513 	if (BNXT_RX_PAGE_MODE(bp)) {
4514 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4515 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4516 	} else {
4517 		bp->rx_offset = BNXT_RX_OFFSET;
4518 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4519 	}
4520 
4521 	for (i = 0; i < bp->rx_nr_rings; i++) {
4522 		rc = bnxt_init_one_rx_ring(bp, i);
4523 		if (rc)
4524 			break;
4525 	}
4526 
4527 	return rc;
4528 }
4529 
4530 static int bnxt_init_tx_rings(struct bnxt *bp)
4531 {
4532 	u16 i;
4533 
4534 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4535 				   BNXT_MIN_TX_DESC_CNT);
4536 
4537 	for (i = 0; i < bp->tx_nr_rings; i++) {
4538 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4539 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4540 
4541 		ring->fw_ring_id = INVALID_HW_RING_ID;
4542 
4543 		if (i >= bp->tx_nr_rings_xdp)
4544 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4545 					     NETDEV_QUEUE_TYPE_TX,
4546 					     &txr->bnapi->napi);
4547 	}
4548 
4549 	return 0;
4550 }
4551 
4552 static void bnxt_free_ring_grps(struct bnxt *bp)
4553 {
4554 	kfree(bp->grp_info);
4555 	bp->grp_info = NULL;
4556 }
4557 
4558 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4559 {
4560 	int i;
4561 
4562 	if (irq_re_init) {
4563 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4564 				       sizeof(struct bnxt_ring_grp_info),
4565 				       GFP_KERNEL);
4566 		if (!bp->grp_info)
4567 			return -ENOMEM;
4568 	}
4569 	for (i = 0; i < bp->cp_nr_rings; i++) {
4570 		if (irq_re_init)
4571 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4572 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4573 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4574 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4575 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4576 	}
4577 	return 0;
4578 }
4579 
4580 static void bnxt_free_vnics(struct bnxt *bp)
4581 {
4582 	kfree(bp->vnic_info);
4583 	bp->vnic_info = NULL;
4584 	bp->nr_vnics = 0;
4585 }
4586 
4587 static int bnxt_alloc_vnics(struct bnxt *bp)
4588 {
4589 	int num_vnics = 1;
4590 
4591 #ifdef CONFIG_RFS_ACCEL
4592 	if (bp->flags & BNXT_FLAG_RFS) {
4593 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4594 			num_vnics++;
4595 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4596 			num_vnics += bp->rx_nr_rings;
4597 	}
4598 #endif
4599 
4600 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4601 		num_vnics++;
4602 
4603 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4604 				GFP_KERNEL);
4605 	if (!bp->vnic_info)
4606 		return -ENOMEM;
4607 
4608 	bp->nr_vnics = num_vnics;
4609 	return 0;
4610 }
4611 
4612 static void bnxt_init_vnics(struct bnxt *bp)
4613 {
4614 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4615 	int i;
4616 
4617 	for (i = 0; i < bp->nr_vnics; i++) {
4618 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4619 		int j;
4620 
4621 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4622 		vnic->vnic_id = i;
4623 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4624 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4625 
4626 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4627 
4628 		if (bp->vnic_info[i].rss_hash_key) {
4629 			if (i == BNXT_VNIC_DEFAULT) {
4630 				u8 *key = (void *)vnic->rss_hash_key;
4631 				int k;
4632 
4633 				if (!bp->rss_hash_key_valid &&
4634 				    !bp->rss_hash_key_updated) {
4635 					get_random_bytes(bp->rss_hash_key,
4636 							 HW_HASH_KEY_SIZE);
4637 					bp->rss_hash_key_updated = true;
4638 				}
4639 
4640 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4641 				       HW_HASH_KEY_SIZE);
4642 
4643 				if (!bp->rss_hash_key_updated)
4644 					continue;
4645 
4646 				bp->rss_hash_key_updated = false;
4647 				bp->rss_hash_key_valid = true;
4648 
4649 				bp->toeplitz_prefix = 0;
4650 				for (k = 0; k < 8; k++) {
4651 					bp->toeplitz_prefix <<= 8;
4652 					bp->toeplitz_prefix |= key[k];
4653 				}
4654 			} else {
4655 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4656 				       HW_HASH_KEY_SIZE);
4657 			}
4658 		}
4659 	}
4660 }
4661 
4662 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4663 {
4664 	int pages;
4665 
4666 	pages = ring_size / desc_per_pg;
4667 
4668 	if (!pages)
4669 		return 1;
4670 
4671 	pages++;
4672 
4673 	while (pages & (pages - 1))
4674 		pages++;
4675 
4676 	return pages;
4677 }
4678 
4679 void bnxt_set_tpa_flags(struct bnxt *bp)
4680 {
4681 	bp->flags &= ~BNXT_FLAG_TPA;
4682 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4683 		return;
4684 	if (bp->dev->features & NETIF_F_LRO)
4685 		bp->flags |= BNXT_FLAG_LRO;
4686 	else if (bp->dev->features & NETIF_F_GRO_HW)
4687 		bp->flags |= BNXT_FLAG_GRO;
4688 }
4689 
4690 static void bnxt_init_ring_params(struct bnxt *bp)
4691 {
4692 	unsigned int rx_size;
4693 
4694 	bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK;
4695 	/* Try to fit 4 chunks into a 4k page */
4696 	rx_size = SZ_1K -
4697 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4698 	bp->dev->cfg->hds_thresh = max(BNXT_DEFAULT_RX_COPYBREAK, rx_size);
4699 }
4700 
4701 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4702  * be set on entry.
4703  */
4704 void bnxt_set_ring_params(struct bnxt *bp)
4705 {
4706 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4707 	u32 agg_factor = 0, agg_ring_size = 0;
4708 
4709 	/* 8 for CRC and VLAN */
4710 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4711 
4712 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4713 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4714 
4715 	ring_size = bp->rx_ring_size;
4716 	bp->rx_agg_ring_size = 0;
4717 	bp->rx_agg_nr_pages = 0;
4718 
4719 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4720 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4721 
4722 	bp->flags &= ~BNXT_FLAG_JUMBO;
4723 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4724 		u32 jumbo_factor;
4725 
4726 		bp->flags |= BNXT_FLAG_JUMBO;
4727 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4728 		if (jumbo_factor > agg_factor)
4729 			agg_factor = jumbo_factor;
4730 	}
4731 	if (agg_factor) {
4732 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4733 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4734 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4735 				    bp->rx_ring_size, ring_size);
4736 			bp->rx_ring_size = ring_size;
4737 		}
4738 		agg_ring_size = ring_size * agg_factor;
4739 
4740 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4741 							RX_DESC_CNT);
4742 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4743 			u32 tmp = agg_ring_size;
4744 
4745 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4746 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4747 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4748 				    tmp, agg_ring_size);
4749 		}
4750 		bp->rx_agg_ring_size = agg_ring_size;
4751 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4752 
4753 		if (BNXT_RX_PAGE_MODE(bp)) {
4754 			rx_space = PAGE_SIZE;
4755 			rx_size = PAGE_SIZE -
4756 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4757 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4758 		} else {
4759 			rx_size = max3(BNXT_DEFAULT_RX_COPYBREAK,
4760 				       bp->rx_copybreak,
4761 				       bp->dev->cfg_pending->hds_thresh);
4762 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4763 			rx_space = rx_size + NET_SKB_PAD +
4764 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4765 		}
4766 	}
4767 
4768 	bp->rx_buf_use_size = rx_size;
4769 	bp->rx_buf_size = rx_space;
4770 
4771 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4772 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4773 
4774 	ring_size = bp->tx_ring_size;
4775 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4776 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4777 
4778 	max_rx_cmpl = bp->rx_ring_size;
4779 	/* MAX TPA needs to be added because TPA_START completions are
4780 	 * immediately recycled, so the TPA completions are not bound by
4781 	 * the RX ring size.
4782 	 */
4783 	if (bp->flags & BNXT_FLAG_TPA)
4784 		max_rx_cmpl += bp->max_tpa;
4785 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4786 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4787 	bp->cp_ring_size = ring_size;
4788 
4789 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4790 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4791 		bp->cp_nr_pages = MAX_CP_PAGES;
4792 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4793 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4794 			    ring_size, bp->cp_ring_size);
4795 	}
4796 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4797 	bp->cp_ring_mask = bp->cp_bit - 1;
4798 }
4799 
4800 /* Changing allocation mode of RX rings.
4801  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4802  */
4803 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4804 {
4805 	struct net_device *dev = bp->dev;
4806 
4807 	if (page_mode) {
4808 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4809 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4810 
4811 		if (bp->xdp_prog->aux->xdp_has_frags)
4812 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4813 		else
4814 			dev->max_mtu =
4815 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4816 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4817 			bp->flags |= BNXT_FLAG_JUMBO;
4818 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4819 		} else {
4820 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4821 			bp->rx_skb_func = bnxt_rx_page_skb;
4822 		}
4823 		bp->rx_dir = DMA_BIDIRECTIONAL;
4824 	} else {
4825 		dev->max_mtu = bp->max_mtu;
4826 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4827 		bp->rx_dir = DMA_FROM_DEVICE;
4828 		bp->rx_skb_func = bnxt_rx_skb;
4829 	}
4830 }
4831 
4832 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4833 {
4834 	__bnxt_set_rx_skb_mode(bp, page_mode);
4835 
4836 	if (!page_mode) {
4837 		int rx, tx;
4838 
4839 		bnxt_get_max_rings(bp, &rx, &tx, true);
4840 		if (rx > 1) {
4841 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
4842 			bp->dev->hw_features |= NETIF_F_LRO;
4843 		}
4844 	}
4845 
4846 	/* Update LRO and GRO_HW availability */
4847 	netdev_update_features(bp->dev);
4848 }
4849 
4850 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4851 {
4852 	int i;
4853 	struct bnxt_vnic_info *vnic;
4854 	struct pci_dev *pdev = bp->pdev;
4855 
4856 	if (!bp->vnic_info)
4857 		return;
4858 
4859 	for (i = 0; i < bp->nr_vnics; i++) {
4860 		vnic = &bp->vnic_info[i];
4861 
4862 		kfree(vnic->fw_grp_ids);
4863 		vnic->fw_grp_ids = NULL;
4864 
4865 		kfree(vnic->uc_list);
4866 		vnic->uc_list = NULL;
4867 
4868 		if (vnic->mc_list) {
4869 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4870 					  vnic->mc_list, vnic->mc_list_mapping);
4871 			vnic->mc_list = NULL;
4872 		}
4873 
4874 		if (vnic->rss_table) {
4875 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4876 					  vnic->rss_table,
4877 					  vnic->rss_table_dma_addr);
4878 			vnic->rss_table = NULL;
4879 		}
4880 
4881 		vnic->rss_hash_key = NULL;
4882 		vnic->flags = 0;
4883 	}
4884 }
4885 
4886 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4887 {
4888 	int i, rc = 0, size;
4889 	struct bnxt_vnic_info *vnic;
4890 	struct pci_dev *pdev = bp->pdev;
4891 	int max_rings;
4892 
4893 	for (i = 0; i < bp->nr_vnics; i++) {
4894 		vnic = &bp->vnic_info[i];
4895 
4896 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4897 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4898 
4899 			if (mem_size > 0) {
4900 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4901 				if (!vnic->uc_list) {
4902 					rc = -ENOMEM;
4903 					goto out;
4904 				}
4905 			}
4906 		}
4907 
4908 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4909 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4910 			vnic->mc_list =
4911 				dma_alloc_coherent(&pdev->dev,
4912 						   vnic->mc_list_size,
4913 						   &vnic->mc_list_mapping,
4914 						   GFP_KERNEL);
4915 			if (!vnic->mc_list) {
4916 				rc = -ENOMEM;
4917 				goto out;
4918 			}
4919 		}
4920 
4921 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4922 			goto vnic_skip_grps;
4923 
4924 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4925 			max_rings = bp->rx_nr_rings;
4926 		else
4927 			max_rings = 1;
4928 
4929 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4930 		if (!vnic->fw_grp_ids) {
4931 			rc = -ENOMEM;
4932 			goto out;
4933 		}
4934 vnic_skip_grps:
4935 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4936 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4937 			continue;
4938 
4939 		/* Allocate rss table and hash key */
4940 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4941 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4942 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4943 
4944 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4945 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4946 						     vnic->rss_table_size,
4947 						     &vnic->rss_table_dma_addr,
4948 						     GFP_KERNEL);
4949 		if (!vnic->rss_table) {
4950 			rc = -ENOMEM;
4951 			goto out;
4952 		}
4953 
4954 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4955 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4956 	}
4957 	return 0;
4958 
4959 out:
4960 	return rc;
4961 }
4962 
4963 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4964 {
4965 	struct bnxt_hwrm_wait_token *token;
4966 
4967 	dma_pool_destroy(bp->hwrm_dma_pool);
4968 	bp->hwrm_dma_pool = NULL;
4969 
4970 	rcu_read_lock();
4971 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4972 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4973 	rcu_read_unlock();
4974 }
4975 
4976 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4977 {
4978 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4979 					    BNXT_HWRM_DMA_SIZE,
4980 					    BNXT_HWRM_DMA_ALIGN, 0);
4981 	if (!bp->hwrm_dma_pool)
4982 		return -ENOMEM;
4983 
4984 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4985 
4986 	return 0;
4987 }
4988 
4989 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4990 {
4991 	kfree(stats->hw_masks);
4992 	stats->hw_masks = NULL;
4993 	kfree(stats->sw_stats);
4994 	stats->sw_stats = NULL;
4995 	if (stats->hw_stats) {
4996 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4997 				  stats->hw_stats_map);
4998 		stats->hw_stats = NULL;
4999 	}
5000 }
5001 
5002 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
5003 				bool alloc_masks)
5004 {
5005 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
5006 					     &stats->hw_stats_map, GFP_KERNEL);
5007 	if (!stats->hw_stats)
5008 		return -ENOMEM;
5009 
5010 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
5011 	if (!stats->sw_stats)
5012 		goto stats_mem_err;
5013 
5014 	if (alloc_masks) {
5015 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
5016 		if (!stats->hw_masks)
5017 			goto stats_mem_err;
5018 	}
5019 	return 0;
5020 
5021 stats_mem_err:
5022 	bnxt_free_stats_mem(bp, stats);
5023 	return -ENOMEM;
5024 }
5025 
5026 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
5027 {
5028 	int i;
5029 
5030 	for (i = 0; i < count; i++)
5031 		mask_arr[i] = mask;
5032 }
5033 
5034 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
5035 {
5036 	int i;
5037 
5038 	for (i = 0; i < count; i++)
5039 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
5040 }
5041 
5042 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
5043 				    struct bnxt_stats_mem *stats)
5044 {
5045 	struct hwrm_func_qstats_ext_output *resp;
5046 	struct hwrm_func_qstats_ext_input *req;
5047 	__le64 *hw_masks;
5048 	int rc;
5049 
5050 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
5051 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5052 		return -EOPNOTSUPP;
5053 
5054 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
5055 	if (rc)
5056 		return rc;
5057 
5058 	req->fid = cpu_to_le16(0xffff);
5059 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5060 
5061 	resp = hwrm_req_hold(bp, req);
5062 	rc = hwrm_req_send(bp, req);
5063 	if (!rc) {
5064 		hw_masks = &resp->rx_ucast_pkts;
5065 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
5066 	}
5067 	hwrm_req_drop(bp, req);
5068 	return rc;
5069 }
5070 
5071 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5072 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5073 
5074 static void bnxt_init_stats(struct bnxt *bp)
5075 {
5076 	struct bnxt_napi *bnapi = bp->bnapi[0];
5077 	struct bnxt_cp_ring_info *cpr;
5078 	struct bnxt_stats_mem *stats;
5079 	__le64 *rx_stats, *tx_stats;
5080 	int rc, rx_count, tx_count;
5081 	u64 *rx_masks, *tx_masks;
5082 	u64 mask;
5083 	u8 flags;
5084 
5085 	cpr = &bnapi->cp_ring;
5086 	stats = &cpr->stats;
5087 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5088 	if (rc) {
5089 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5090 			mask = (1ULL << 48) - 1;
5091 		else
5092 			mask = -1ULL;
5093 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5094 	}
5095 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5096 		stats = &bp->port_stats;
5097 		rx_stats = stats->hw_stats;
5098 		rx_masks = stats->hw_masks;
5099 		rx_count = sizeof(struct rx_port_stats) / 8;
5100 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5101 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5102 		tx_count = sizeof(struct tx_port_stats) / 8;
5103 
5104 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5105 		rc = bnxt_hwrm_port_qstats(bp, flags);
5106 		if (rc) {
5107 			mask = (1ULL << 40) - 1;
5108 
5109 			bnxt_fill_masks(rx_masks, mask, rx_count);
5110 			bnxt_fill_masks(tx_masks, mask, tx_count);
5111 		} else {
5112 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5113 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5114 			bnxt_hwrm_port_qstats(bp, 0);
5115 		}
5116 	}
5117 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5118 		stats = &bp->rx_port_stats_ext;
5119 		rx_stats = stats->hw_stats;
5120 		rx_masks = stats->hw_masks;
5121 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5122 		stats = &bp->tx_port_stats_ext;
5123 		tx_stats = stats->hw_stats;
5124 		tx_masks = stats->hw_masks;
5125 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5126 
5127 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5128 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5129 		if (rc) {
5130 			mask = (1ULL << 40) - 1;
5131 
5132 			bnxt_fill_masks(rx_masks, mask, rx_count);
5133 			if (tx_stats)
5134 				bnxt_fill_masks(tx_masks, mask, tx_count);
5135 		} else {
5136 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5137 			if (tx_stats)
5138 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5139 						   tx_count);
5140 			bnxt_hwrm_port_qstats_ext(bp, 0);
5141 		}
5142 	}
5143 }
5144 
5145 static void bnxt_free_port_stats(struct bnxt *bp)
5146 {
5147 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5148 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5149 
5150 	bnxt_free_stats_mem(bp, &bp->port_stats);
5151 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5152 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5153 }
5154 
5155 static void bnxt_free_ring_stats(struct bnxt *bp)
5156 {
5157 	int i;
5158 
5159 	if (!bp->bnapi)
5160 		return;
5161 
5162 	for (i = 0; i < bp->cp_nr_rings; i++) {
5163 		struct bnxt_napi *bnapi = bp->bnapi[i];
5164 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5165 
5166 		bnxt_free_stats_mem(bp, &cpr->stats);
5167 
5168 		kfree(cpr->sw_stats);
5169 		cpr->sw_stats = NULL;
5170 	}
5171 }
5172 
5173 static int bnxt_alloc_stats(struct bnxt *bp)
5174 {
5175 	u32 size, i;
5176 	int rc;
5177 
5178 	size = bp->hw_ring_stats_size;
5179 
5180 	for (i = 0; i < bp->cp_nr_rings; i++) {
5181 		struct bnxt_napi *bnapi = bp->bnapi[i];
5182 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5183 
5184 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
5185 		if (!cpr->sw_stats)
5186 			return -ENOMEM;
5187 
5188 		cpr->stats.len = size;
5189 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5190 		if (rc)
5191 			return rc;
5192 
5193 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5194 	}
5195 
5196 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5197 		return 0;
5198 
5199 	if (bp->port_stats.hw_stats)
5200 		goto alloc_ext_stats;
5201 
5202 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5203 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5204 	if (rc)
5205 		return rc;
5206 
5207 	bp->flags |= BNXT_FLAG_PORT_STATS;
5208 
5209 alloc_ext_stats:
5210 	/* Display extended statistics only if FW supports it */
5211 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5212 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5213 			return 0;
5214 
5215 	if (bp->rx_port_stats_ext.hw_stats)
5216 		goto alloc_tx_ext_stats;
5217 
5218 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5219 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5220 	/* Extended stats are optional */
5221 	if (rc)
5222 		return 0;
5223 
5224 alloc_tx_ext_stats:
5225 	if (bp->tx_port_stats_ext.hw_stats)
5226 		return 0;
5227 
5228 	if (bp->hwrm_spec_code >= 0x10902 ||
5229 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5230 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5231 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5232 		/* Extended stats are optional */
5233 		if (rc)
5234 			return 0;
5235 	}
5236 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5237 	return 0;
5238 }
5239 
5240 static void bnxt_clear_ring_indices(struct bnxt *bp)
5241 {
5242 	int i, j;
5243 
5244 	if (!bp->bnapi)
5245 		return;
5246 
5247 	for (i = 0; i < bp->cp_nr_rings; i++) {
5248 		struct bnxt_napi *bnapi = bp->bnapi[i];
5249 		struct bnxt_cp_ring_info *cpr;
5250 		struct bnxt_rx_ring_info *rxr;
5251 		struct bnxt_tx_ring_info *txr;
5252 
5253 		if (!bnapi)
5254 			continue;
5255 
5256 		cpr = &bnapi->cp_ring;
5257 		cpr->cp_raw_cons = 0;
5258 
5259 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5260 			txr->tx_prod = 0;
5261 			txr->tx_cons = 0;
5262 			txr->tx_hw_cons = 0;
5263 		}
5264 
5265 		rxr = bnapi->rx_ring;
5266 		if (rxr) {
5267 			rxr->rx_prod = 0;
5268 			rxr->rx_agg_prod = 0;
5269 			rxr->rx_sw_agg_prod = 0;
5270 			rxr->rx_next_cons = 0;
5271 		}
5272 		bnapi->events = 0;
5273 	}
5274 }
5275 
5276 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5277 {
5278 	u8 type = fltr->type, flags = fltr->flags;
5279 
5280 	INIT_LIST_HEAD(&fltr->list);
5281 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5282 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5283 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5284 }
5285 
5286 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5287 {
5288 	if (!list_empty(&fltr->list))
5289 		list_del_init(&fltr->list);
5290 }
5291 
5292 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5293 {
5294 	struct bnxt_filter_base *usr_fltr, *tmp;
5295 
5296 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5297 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5298 			continue;
5299 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5300 	}
5301 }
5302 
5303 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5304 {
5305 	hlist_del(&fltr->hash);
5306 	bnxt_del_one_usr_fltr(bp, fltr);
5307 	if (fltr->flags) {
5308 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5309 		bp->ntp_fltr_count--;
5310 	}
5311 	kfree(fltr);
5312 }
5313 
5314 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5315 {
5316 	int i;
5317 
5318 	netdev_assert_locked(bp->dev);
5319 
5320 	/* Under netdev instance lock and all our NAPIs have been disabled.
5321 	 * It's safe to delete the hash table.
5322 	 */
5323 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5324 		struct hlist_head *head;
5325 		struct hlist_node *tmp;
5326 		struct bnxt_ntuple_filter *fltr;
5327 
5328 		head = &bp->ntp_fltr_hash_tbl[i];
5329 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5330 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5331 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5332 				     !list_empty(&fltr->base.list)))
5333 				continue;
5334 			bnxt_del_fltr(bp, &fltr->base);
5335 		}
5336 	}
5337 	if (!all)
5338 		return;
5339 
5340 	bitmap_free(bp->ntp_fltr_bmap);
5341 	bp->ntp_fltr_bmap = NULL;
5342 	bp->ntp_fltr_count = 0;
5343 }
5344 
5345 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5346 {
5347 	int i, rc = 0;
5348 
5349 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5350 		return 0;
5351 
5352 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5353 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5354 
5355 	bp->ntp_fltr_count = 0;
5356 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5357 
5358 	if (!bp->ntp_fltr_bmap)
5359 		rc = -ENOMEM;
5360 
5361 	return rc;
5362 }
5363 
5364 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5365 {
5366 	int i;
5367 
5368 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5369 		struct hlist_head *head;
5370 		struct hlist_node *tmp;
5371 		struct bnxt_l2_filter *fltr;
5372 
5373 		head = &bp->l2_fltr_hash_tbl[i];
5374 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5375 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5376 				     !list_empty(&fltr->base.list)))
5377 				continue;
5378 			bnxt_del_fltr(bp, &fltr->base);
5379 		}
5380 	}
5381 }
5382 
5383 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5384 {
5385 	int i;
5386 
5387 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5388 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5389 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5390 }
5391 
5392 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5393 {
5394 	bnxt_free_vnic_attributes(bp);
5395 	bnxt_free_tx_rings(bp);
5396 	bnxt_free_rx_rings(bp);
5397 	bnxt_free_cp_rings(bp);
5398 	bnxt_free_all_cp_arrays(bp);
5399 	bnxt_free_ntp_fltrs(bp, false);
5400 	bnxt_free_l2_filters(bp, false);
5401 	if (irq_re_init) {
5402 		bnxt_free_ring_stats(bp);
5403 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5404 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5405 			bnxt_free_port_stats(bp);
5406 		bnxt_free_ring_grps(bp);
5407 		bnxt_free_vnics(bp);
5408 		kfree(bp->tx_ring_map);
5409 		bp->tx_ring_map = NULL;
5410 		kfree(bp->tx_ring);
5411 		bp->tx_ring = NULL;
5412 		kfree(bp->rx_ring);
5413 		bp->rx_ring = NULL;
5414 		kfree(bp->bnapi);
5415 		bp->bnapi = NULL;
5416 	} else {
5417 		bnxt_clear_ring_indices(bp);
5418 	}
5419 }
5420 
5421 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5422 {
5423 	int i, j, rc, size, arr_size;
5424 	void *bnapi;
5425 
5426 	if (irq_re_init) {
5427 		/* Allocate bnapi mem pointer array and mem block for
5428 		 * all queues
5429 		 */
5430 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5431 				bp->cp_nr_rings);
5432 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5433 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5434 		if (!bnapi)
5435 			return -ENOMEM;
5436 
5437 		bp->bnapi = bnapi;
5438 		bnapi += arr_size;
5439 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5440 			bp->bnapi[i] = bnapi;
5441 			bp->bnapi[i]->index = i;
5442 			bp->bnapi[i]->bp = bp;
5443 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5444 				struct bnxt_cp_ring_info *cpr =
5445 					&bp->bnapi[i]->cp_ring;
5446 
5447 				cpr->cp_ring_struct.ring_mem.flags =
5448 					BNXT_RMEM_RING_PTE_FLAG;
5449 			}
5450 		}
5451 
5452 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5453 				      sizeof(struct bnxt_rx_ring_info),
5454 				      GFP_KERNEL);
5455 		if (!bp->rx_ring)
5456 			return -ENOMEM;
5457 
5458 		for (i = 0; i < bp->rx_nr_rings; i++) {
5459 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5460 
5461 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5462 				rxr->rx_ring_struct.ring_mem.flags =
5463 					BNXT_RMEM_RING_PTE_FLAG;
5464 				rxr->rx_agg_ring_struct.ring_mem.flags =
5465 					BNXT_RMEM_RING_PTE_FLAG;
5466 			} else {
5467 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5468 			}
5469 			rxr->bnapi = bp->bnapi[i];
5470 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5471 		}
5472 
5473 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5474 				      sizeof(struct bnxt_tx_ring_info),
5475 				      GFP_KERNEL);
5476 		if (!bp->tx_ring)
5477 			return -ENOMEM;
5478 
5479 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5480 					  GFP_KERNEL);
5481 
5482 		if (!bp->tx_ring_map)
5483 			return -ENOMEM;
5484 
5485 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5486 			j = 0;
5487 		else
5488 			j = bp->rx_nr_rings;
5489 
5490 		for (i = 0; i < bp->tx_nr_rings; i++) {
5491 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5492 			struct bnxt_napi *bnapi2;
5493 
5494 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5495 				txr->tx_ring_struct.ring_mem.flags =
5496 					BNXT_RMEM_RING_PTE_FLAG;
5497 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5498 			if (i >= bp->tx_nr_rings_xdp) {
5499 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5500 
5501 				bnapi2 = bp->bnapi[k];
5502 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5503 				txr->tx_napi_idx =
5504 					BNXT_RING_TO_TC(bp, txr->txq_index);
5505 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5506 				bnapi2->tx_int = bnxt_tx_int;
5507 			} else {
5508 				bnapi2 = bp->bnapi[j];
5509 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5510 				bnapi2->tx_ring[0] = txr;
5511 				bnapi2->tx_int = bnxt_tx_int_xdp;
5512 				j++;
5513 			}
5514 			txr->bnapi = bnapi2;
5515 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5516 				txr->tx_cpr = &bnapi2->cp_ring;
5517 		}
5518 
5519 		rc = bnxt_alloc_stats(bp);
5520 		if (rc)
5521 			goto alloc_mem_err;
5522 		bnxt_init_stats(bp);
5523 
5524 		rc = bnxt_alloc_ntp_fltrs(bp);
5525 		if (rc)
5526 			goto alloc_mem_err;
5527 
5528 		rc = bnxt_alloc_vnics(bp);
5529 		if (rc)
5530 			goto alloc_mem_err;
5531 	}
5532 
5533 	rc = bnxt_alloc_all_cp_arrays(bp);
5534 	if (rc)
5535 		goto alloc_mem_err;
5536 
5537 	bnxt_init_ring_struct(bp);
5538 
5539 	rc = bnxt_alloc_rx_rings(bp);
5540 	if (rc)
5541 		goto alloc_mem_err;
5542 
5543 	rc = bnxt_alloc_tx_rings(bp);
5544 	if (rc)
5545 		goto alloc_mem_err;
5546 
5547 	rc = bnxt_alloc_cp_rings(bp);
5548 	if (rc)
5549 		goto alloc_mem_err;
5550 
5551 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5552 						  BNXT_VNIC_MCAST_FLAG |
5553 						  BNXT_VNIC_UCAST_FLAG;
5554 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5555 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5556 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5557 
5558 	rc = bnxt_alloc_vnic_attributes(bp);
5559 	if (rc)
5560 		goto alloc_mem_err;
5561 	return 0;
5562 
5563 alloc_mem_err:
5564 	bnxt_free_mem(bp, true);
5565 	return rc;
5566 }
5567 
5568 static void bnxt_disable_int(struct bnxt *bp)
5569 {
5570 	int i;
5571 
5572 	if (!bp->bnapi)
5573 		return;
5574 
5575 	for (i = 0; i < bp->cp_nr_rings; i++) {
5576 		struct bnxt_napi *bnapi = bp->bnapi[i];
5577 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5578 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5579 
5580 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5581 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5582 	}
5583 }
5584 
5585 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5586 {
5587 	struct bnxt_napi *bnapi = bp->bnapi[n];
5588 	struct bnxt_cp_ring_info *cpr;
5589 
5590 	cpr = &bnapi->cp_ring;
5591 	return cpr->cp_ring_struct.map_idx;
5592 }
5593 
5594 static void bnxt_disable_int_sync(struct bnxt *bp)
5595 {
5596 	int i;
5597 
5598 	if (!bp->irq_tbl)
5599 		return;
5600 
5601 	atomic_inc(&bp->intr_sem);
5602 
5603 	bnxt_disable_int(bp);
5604 	for (i = 0; i < bp->cp_nr_rings; i++) {
5605 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5606 
5607 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5608 	}
5609 }
5610 
5611 static void bnxt_enable_int(struct bnxt *bp)
5612 {
5613 	int i;
5614 
5615 	atomic_set(&bp->intr_sem, 0);
5616 	for (i = 0; i < bp->cp_nr_rings; i++) {
5617 		struct bnxt_napi *bnapi = bp->bnapi[i];
5618 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5619 
5620 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5621 	}
5622 }
5623 
5624 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5625 			    bool async_only)
5626 {
5627 	DECLARE_BITMAP(async_events_bmap, 256);
5628 	u32 *events = (u32 *)async_events_bmap;
5629 	struct hwrm_func_drv_rgtr_output *resp;
5630 	struct hwrm_func_drv_rgtr_input *req;
5631 	u32 flags;
5632 	int rc, i;
5633 
5634 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5635 	if (rc)
5636 		return rc;
5637 
5638 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5639 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5640 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5641 
5642 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5643 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5644 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5645 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5646 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5647 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5648 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5649 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5650 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5651 	req->flags = cpu_to_le32(flags);
5652 	req->ver_maj_8b = DRV_VER_MAJ;
5653 	req->ver_min_8b = DRV_VER_MIN;
5654 	req->ver_upd_8b = DRV_VER_UPD;
5655 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5656 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5657 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5658 
5659 	if (BNXT_PF(bp)) {
5660 		u32 data[8];
5661 		int i;
5662 
5663 		memset(data, 0, sizeof(data));
5664 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5665 			u16 cmd = bnxt_vf_req_snif[i];
5666 			unsigned int bit, idx;
5667 
5668 			idx = cmd / 32;
5669 			bit = cmd % 32;
5670 			data[idx] |= 1 << bit;
5671 		}
5672 
5673 		for (i = 0; i < 8; i++)
5674 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5675 
5676 		req->enables |=
5677 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5678 	}
5679 
5680 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5681 		req->flags |= cpu_to_le32(
5682 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5683 
5684 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5685 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5686 		u16 event_id = bnxt_async_events_arr[i];
5687 
5688 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5689 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5690 			continue;
5691 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5692 		    !bp->ptp_cfg)
5693 			continue;
5694 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5695 	}
5696 	if (bmap && bmap_size) {
5697 		for (i = 0; i < bmap_size; i++) {
5698 			if (test_bit(i, bmap))
5699 				__set_bit(i, async_events_bmap);
5700 		}
5701 	}
5702 	for (i = 0; i < 8; i++)
5703 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5704 
5705 	if (async_only)
5706 		req->enables =
5707 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5708 
5709 	resp = hwrm_req_hold(bp, req);
5710 	rc = hwrm_req_send(bp, req);
5711 	if (!rc) {
5712 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5713 		if (resp->flags &
5714 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5715 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5716 	}
5717 	hwrm_req_drop(bp, req);
5718 	return rc;
5719 }
5720 
5721 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5722 {
5723 	struct hwrm_func_drv_unrgtr_input *req;
5724 	int rc;
5725 
5726 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5727 		return 0;
5728 
5729 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5730 	if (rc)
5731 		return rc;
5732 	return hwrm_req_send(bp, req);
5733 }
5734 
5735 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5736 
5737 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5738 {
5739 	struct hwrm_tunnel_dst_port_free_input *req;
5740 	int rc;
5741 
5742 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5743 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5744 		return 0;
5745 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5746 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5747 		return 0;
5748 
5749 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5750 	if (rc)
5751 		return rc;
5752 
5753 	req->tunnel_type = tunnel_type;
5754 
5755 	switch (tunnel_type) {
5756 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5757 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5758 		bp->vxlan_port = 0;
5759 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5760 		break;
5761 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5762 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5763 		bp->nge_port = 0;
5764 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5765 		break;
5766 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5767 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5768 		bp->vxlan_gpe_port = 0;
5769 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5770 		break;
5771 	default:
5772 		break;
5773 	}
5774 
5775 	rc = hwrm_req_send(bp, req);
5776 	if (rc)
5777 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5778 			   rc);
5779 	if (bp->flags & BNXT_FLAG_TPA)
5780 		bnxt_set_tpa(bp, true);
5781 	return rc;
5782 }
5783 
5784 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5785 					   u8 tunnel_type)
5786 {
5787 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5788 	struct hwrm_tunnel_dst_port_alloc_input *req;
5789 	int rc;
5790 
5791 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5792 	if (rc)
5793 		return rc;
5794 
5795 	req->tunnel_type = tunnel_type;
5796 	req->tunnel_dst_port_val = port;
5797 
5798 	resp = hwrm_req_hold(bp, req);
5799 	rc = hwrm_req_send(bp, req);
5800 	if (rc) {
5801 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5802 			   rc);
5803 		goto err_out;
5804 	}
5805 
5806 	switch (tunnel_type) {
5807 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5808 		bp->vxlan_port = port;
5809 		bp->vxlan_fw_dst_port_id =
5810 			le16_to_cpu(resp->tunnel_dst_port_id);
5811 		break;
5812 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5813 		bp->nge_port = port;
5814 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5815 		break;
5816 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5817 		bp->vxlan_gpe_port = port;
5818 		bp->vxlan_gpe_fw_dst_port_id =
5819 			le16_to_cpu(resp->tunnel_dst_port_id);
5820 		break;
5821 	default:
5822 		break;
5823 	}
5824 	if (bp->flags & BNXT_FLAG_TPA)
5825 		bnxt_set_tpa(bp, true);
5826 
5827 err_out:
5828 	hwrm_req_drop(bp, req);
5829 	return rc;
5830 }
5831 
5832 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5833 {
5834 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5835 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5836 	int rc;
5837 
5838 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5839 	if (rc)
5840 		return rc;
5841 
5842 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5843 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5844 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5845 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5846 	}
5847 	req->mask = cpu_to_le32(vnic->rx_mask);
5848 	return hwrm_req_send_silent(bp, req);
5849 }
5850 
5851 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5852 {
5853 	if (!atomic_dec_and_test(&fltr->refcnt))
5854 		return;
5855 	spin_lock_bh(&bp->ntp_fltr_lock);
5856 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5857 		spin_unlock_bh(&bp->ntp_fltr_lock);
5858 		return;
5859 	}
5860 	hlist_del_rcu(&fltr->base.hash);
5861 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5862 	if (fltr->base.flags) {
5863 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5864 		bp->ntp_fltr_count--;
5865 	}
5866 	spin_unlock_bh(&bp->ntp_fltr_lock);
5867 	kfree_rcu(fltr, base.rcu);
5868 }
5869 
5870 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5871 						      struct bnxt_l2_key *key,
5872 						      u32 idx)
5873 {
5874 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5875 	struct bnxt_l2_filter *fltr;
5876 
5877 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5878 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5879 
5880 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5881 		    l2_key->vlan == key->vlan)
5882 			return fltr;
5883 	}
5884 	return NULL;
5885 }
5886 
5887 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5888 						    struct bnxt_l2_key *key,
5889 						    u32 idx)
5890 {
5891 	struct bnxt_l2_filter *fltr = NULL;
5892 
5893 	rcu_read_lock();
5894 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5895 	if (fltr)
5896 		atomic_inc(&fltr->refcnt);
5897 	rcu_read_unlock();
5898 	return fltr;
5899 }
5900 
5901 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5902 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5903 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5904 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5905 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5906 
5907 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5908 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5909 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5910 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5911 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5912 
5913 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5914 {
5915 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5916 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5917 			return sizeof(fkeys->addrs.v4addrs) +
5918 			       sizeof(fkeys->ports);
5919 
5920 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5921 			return sizeof(fkeys->addrs.v4addrs);
5922 	}
5923 
5924 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5925 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5926 			return sizeof(fkeys->addrs.v6addrs) +
5927 			       sizeof(fkeys->ports);
5928 
5929 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5930 			return sizeof(fkeys->addrs.v6addrs);
5931 	}
5932 
5933 	return 0;
5934 }
5935 
5936 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5937 			 const unsigned char *key)
5938 {
5939 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5940 	struct bnxt_ipv4_tuple tuple4;
5941 	struct bnxt_ipv6_tuple tuple6;
5942 	int i, j, len = 0;
5943 	u8 *four_tuple;
5944 
5945 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5946 	if (!len)
5947 		return 0;
5948 
5949 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5950 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5951 		tuple4.ports = fkeys->ports;
5952 		four_tuple = (unsigned char *)&tuple4;
5953 	} else {
5954 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5955 		tuple6.ports = fkeys->ports;
5956 		four_tuple = (unsigned char *)&tuple6;
5957 	}
5958 
5959 	for (i = 0, j = 8; i < len; i++, j++) {
5960 		u8 byte = four_tuple[i];
5961 		int bit;
5962 
5963 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5964 			if (byte & 0x80)
5965 				hash ^= prefix;
5966 		}
5967 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5968 	}
5969 
5970 	/* The valid part of the hash is in the upper 32 bits. */
5971 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5972 }
5973 
5974 #ifdef CONFIG_RFS_ACCEL
5975 static struct bnxt_l2_filter *
5976 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5977 {
5978 	struct bnxt_l2_filter *fltr;
5979 	u32 idx;
5980 
5981 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5982 	      BNXT_L2_FLTR_HASH_MASK;
5983 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5984 	return fltr;
5985 }
5986 #endif
5987 
5988 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5989 			       struct bnxt_l2_key *key, u32 idx)
5990 {
5991 	struct hlist_head *head;
5992 
5993 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5994 	fltr->l2_key.vlan = key->vlan;
5995 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5996 	if (fltr->base.flags) {
5997 		int bit_id;
5998 
5999 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6000 						 bp->max_fltr, 0);
6001 		if (bit_id < 0)
6002 			return -ENOMEM;
6003 		fltr->base.sw_id = (u16)bit_id;
6004 		bp->ntp_fltr_count++;
6005 	}
6006 	head = &bp->l2_fltr_hash_tbl[idx];
6007 	hlist_add_head_rcu(&fltr->base.hash, head);
6008 	bnxt_insert_usr_fltr(bp, &fltr->base);
6009 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
6010 	atomic_set(&fltr->refcnt, 1);
6011 	return 0;
6012 }
6013 
6014 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
6015 						   struct bnxt_l2_key *key,
6016 						   gfp_t gfp)
6017 {
6018 	struct bnxt_l2_filter *fltr;
6019 	u32 idx;
6020 	int rc;
6021 
6022 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6023 	      BNXT_L2_FLTR_HASH_MASK;
6024 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6025 	if (fltr)
6026 		return fltr;
6027 
6028 	fltr = kzalloc(sizeof(*fltr), gfp);
6029 	if (!fltr)
6030 		return ERR_PTR(-ENOMEM);
6031 	spin_lock_bh(&bp->ntp_fltr_lock);
6032 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6033 	spin_unlock_bh(&bp->ntp_fltr_lock);
6034 	if (rc) {
6035 		bnxt_del_l2_filter(bp, fltr);
6036 		fltr = ERR_PTR(rc);
6037 	}
6038 	return fltr;
6039 }
6040 
6041 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
6042 						struct bnxt_l2_key *key,
6043 						u16 flags)
6044 {
6045 	struct bnxt_l2_filter *fltr;
6046 	u32 idx;
6047 	int rc;
6048 
6049 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6050 	      BNXT_L2_FLTR_HASH_MASK;
6051 	spin_lock_bh(&bp->ntp_fltr_lock);
6052 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6053 	if (fltr) {
6054 		fltr = ERR_PTR(-EEXIST);
6055 		goto l2_filter_exit;
6056 	}
6057 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
6058 	if (!fltr) {
6059 		fltr = ERR_PTR(-ENOMEM);
6060 		goto l2_filter_exit;
6061 	}
6062 	fltr->base.flags = flags;
6063 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6064 	if (rc) {
6065 		spin_unlock_bh(&bp->ntp_fltr_lock);
6066 		bnxt_del_l2_filter(bp, fltr);
6067 		return ERR_PTR(rc);
6068 	}
6069 
6070 l2_filter_exit:
6071 	spin_unlock_bh(&bp->ntp_fltr_lock);
6072 	return fltr;
6073 }
6074 
6075 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6076 {
6077 #ifdef CONFIG_BNXT_SRIOV
6078 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6079 
6080 	return vf->fw_fid;
6081 #else
6082 	return INVALID_HW_RING_ID;
6083 #endif
6084 }
6085 
6086 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6087 {
6088 	struct hwrm_cfa_l2_filter_free_input *req;
6089 	u16 target_id = 0xffff;
6090 	int rc;
6091 
6092 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6093 		struct bnxt_pf_info *pf = &bp->pf;
6094 
6095 		if (fltr->base.vf_idx >= pf->active_vfs)
6096 			return -EINVAL;
6097 
6098 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6099 		if (target_id == INVALID_HW_RING_ID)
6100 			return -EINVAL;
6101 	}
6102 
6103 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6104 	if (rc)
6105 		return rc;
6106 
6107 	req->target_id = cpu_to_le16(target_id);
6108 	req->l2_filter_id = fltr->base.filter_id;
6109 	return hwrm_req_send(bp, req);
6110 }
6111 
6112 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6113 {
6114 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6115 	struct hwrm_cfa_l2_filter_alloc_input *req;
6116 	u16 target_id = 0xffff;
6117 	int rc;
6118 
6119 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6120 		struct bnxt_pf_info *pf = &bp->pf;
6121 
6122 		if (fltr->base.vf_idx >= pf->active_vfs)
6123 			return -EINVAL;
6124 
6125 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6126 	}
6127 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6128 	if (rc)
6129 		return rc;
6130 
6131 	req->target_id = cpu_to_le16(target_id);
6132 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6133 
6134 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6135 		req->flags |=
6136 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6137 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6138 	req->enables =
6139 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6140 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6141 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6142 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6143 	eth_broadcast_addr(req->l2_addr_mask);
6144 
6145 	if (fltr->l2_key.vlan) {
6146 		req->enables |=
6147 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6148 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6149 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6150 		req->num_vlans = 1;
6151 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6152 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6153 	}
6154 
6155 	resp = hwrm_req_hold(bp, req);
6156 	rc = hwrm_req_send(bp, req);
6157 	if (!rc) {
6158 		fltr->base.filter_id = resp->l2_filter_id;
6159 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6160 	}
6161 	hwrm_req_drop(bp, req);
6162 	return rc;
6163 }
6164 
6165 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6166 				     struct bnxt_ntuple_filter *fltr)
6167 {
6168 	struct hwrm_cfa_ntuple_filter_free_input *req;
6169 	int rc;
6170 
6171 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6172 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6173 	if (rc)
6174 		return rc;
6175 
6176 	req->ntuple_filter_id = fltr->base.filter_id;
6177 	return hwrm_req_send(bp, req);
6178 }
6179 
6180 #define BNXT_NTP_FLTR_FLAGS					\
6181 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6182 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6183 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6184 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6185 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6186 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6187 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6188 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6189 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6190 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6191 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6192 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6193 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6194 
6195 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6196 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6197 
6198 void bnxt_fill_ipv6_mask(__be32 mask[4])
6199 {
6200 	int i;
6201 
6202 	for (i = 0; i < 4; i++)
6203 		mask[i] = cpu_to_be32(~0);
6204 }
6205 
6206 static void
6207 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6208 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6209 			  struct bnxt_ntuple_filter *fltr)
6210 {
6211 	u16 rxq = fltr->base.rxq;
6212 
6213 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6214 		struct ethtool_rxfh_context *ctx;
6215 		struct bnxt_rss_ctx *rss_ctx;
6216 		struct bnxt_vnic_info *vnic;
6217 
6218 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6219 			      fltr->base.fw_vnic_id);
6220 		if (ctx) {
6221 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6222 			vnic = &rss_ctx->vnic;
6223 
6224 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6225 		}
6226 		return;
6227 	}
6228 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6229 		struct bnxt_vnic_info *vnic;
6230 		u32 enables;
6231 
6232 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6233 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6234 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6235 		req->enables |= cpu_to_le32(enables);
6236 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6237 	} else {
6238 		u32 flags;
6239 
6240 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6241 		req->flags |= cpu_to_le32(flags);
6242 		req->dst_id = cpu_to_le16(rxq);
6243 	}
6244 }
6245 
6246 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6247 				      struct bnxt_ntuple_filter *fltr)
6248 {
6249 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6250 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6251 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6252 	struct flow_keys *keys = &fltr->fkeys;
6253 	struct bnxt_l2_filter *l2_fltr;
6254 	struct bnxt_vnic_info *vnic;
6255 	int rc;
6256 
6257 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6258 	if (rc)
6259 		return rc;
6260 
6261 	l2_fltr = fltr->l2_fltr;
6262 	req->l2_filter_id = l2_fltr->base.filter_id;
6263 
6264 	if (fltr->base.flags & BNXT_ACT_DROP) {
6265 		req->flags =
6266 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6267 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6268 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6269 	} else {
6270 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6271 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6272 	}
6273 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6274 
6275 	req->ethertype = htons(ETH_P_IP);
6276 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6277 	req->ip_protocol = keys->basic.ip_proto;
6278 
6279 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6280 		req->ethertype = htons(ETH_P_IPV6);
6281 		req->ip_addr_type =
6282 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6283 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6284 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6285 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6286 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6287 	} else {
6288 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6289 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6290 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6291 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6292 	}
6293 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6294 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6295 		req->tunnel_type =
6296 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6297 	}
6298 
6299 	req->src_port = keys->ports.src;
6300 	req->src_port_mask = masks->ports.src;
6301 	req->dst_port = keys->ports.dst;
6302 	req->dst_port_mask = masks->ports.dst;
6303 
6304 	resp = hwrm_req_hold(bp, req);
6305 	rc = hwrm_req_send(bp, req);
6306 	if (!rc)
6307 		fltr->base.filter_id = resp->ntuple_filter_id;
6308 	hwrm_req_drop(bp, req);
6309 	return rc;
6310 }
6311 
6312 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6313 				     const u8 *mac_addr)
6314 {
6315 	struct bnxt_l2_filter *fltr;
6316 	struct bnxt_l2_key key;
6317 	int rc;
6318 
6319 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6320 	key.vlan = 0;
6321 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6322 	if (IS_ERR(fltr))
6323 		return PTR_ERR(fltr);
6324 
6325 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6326 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6327 	if (rc)
6328 		bnxt_del_l2_filter(bp, fltr);
6329 	else
6330 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6331 	return rc;
6332 }
6333 
6334 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6335 {
6336 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6337 
6338 	/* Any associated ntuple filters will also be cleared by firmware. */
6339 	for (i = 0; i < num_of_vnics; i++) {
6340 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6341 
6342 		for (j = 0; j < vnic->uc_filter_count; j++) {
6343 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6344 
6345 			bnxt_hwrm_l2_filter_free(bp, fltr);
6346 			bnxt_del_l2_filter(bp, fltr);
6347 		}
6348 		vnic->uc_filter_count = 0;
6349 	}
6350 }
6351 
6352 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6353 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6354 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6355 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6356 
6357 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6358 					   struct hwrm_vnic_tpa_cfg_input *req)
6359 {
6360 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6361 
6362 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6363 		return;
6364 
6365 	if (bp->vxlan_port)
6366 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6367 	if (bp->vxlan_gpe_port)
6368 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6369 	if (bp->nge_port)
6370 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6371 
6372 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6373 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6374 }
6375 
6376 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6377 			   u32 tpa_flags)
6378 {
6379 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6380 	struct hwrm_vnic_tpa_cfg_input *req;
6381 	int rc;
6382 
6383 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6384 		return 0;
6385 
6386 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6387 	if (rc)
6388 		return rc;
6389 
6390 	if (tpa_flags) {
6391 		u16 mss = bp->dev->mtu - 40;
6392 		u32 nsegs, n, segs = 0, flags;
6393 
6394 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6395 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6396 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6397 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6398 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6399 		if (tpa_flags & BNXT_FLAG_GRO)
6400 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6401 
6402 		req->flags = cpu_to_le32(flags);
6403 
6404 		req->enables =
6405 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6406 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6407 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6408 
6409 		/* Number of segs are log2 units, and first packet is not
6410 		 * included as part of this units.
6411 		 */
6412 		if (mss <= BNXT_RX_PAGE_SIZE) {
6413 			n = BNXT_RX_PAGE_SIZE / mss;
6414 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6415 		} else {
6416 			n = mss / BNXT_RX_PAGE_SIZE;
6417 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6418 				n++;
6419 			nsegs = (MAX_SKB_FRAGS - n) / n;
6420 		}
6421 
6422 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6423 			segs = MAX_TPA_SEGS_P5;
6424 			max_aggs = bp->max_tpa;
6425 		} else {
6426 			segs = ilog2(nsegs);
6427 		}
6428 		req->max_agg_segs = cpu_to_le16(segs);
6429 		req->max_aggs = cpu_to_le16(max_aggs);
6430 
6431 		req->min_agg_len = cpu_to_le32(512);
6432 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6433 	}
6434 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6435 
6436 	return hwrm_req_send(bp, req);
6437 }
6438 
6439 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6440 {
6441 	struct bnxt_ring_grp_info *grp_info;
6442 
6443 	grp_info = &bp->grp_info[ring->grp_idx];
6444 	return grp_info->cp_fw_ring_id;
6445 }
6446 
6447 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6448 {
6449 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6450 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6451 	else
6452 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6453 }
6454 
6455 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6456 {
6457 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6458 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6459 	else
6460 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6461 }
6462 
6463 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6464 {
6465 	int entries;
6466 
6467 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6468 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6469 	else
6470 		entries = HW_HASH_INDEX_SIZE;
6471 
6472 	bp->rss_indir_tbl_entries = entries;
6473 	bp->rss_indir_tbl =
6474 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6475 	if (!bp->rss_indir_tbl)
6476 		return -ENOMEM;
6477 
6478 	return 0;
6479 }
6480 
6481 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6482 				 struct ethtool_rxfh_context *rss_ctx)
6483 {
6484 	u16 max_rings, max_entries, pad, i;
6485 	u32 *rss_indir_tbl;
6486 
6487 	if (!bp->rx_nr_rings)
6488 		return;
6489 
6490 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6491 		max_rings = bp->rx_nr_rings - 1;
6492 	else
6493 		max_rings = bp->rx_nr_rings;
6494 
6495 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6496 	if (rss_ctx)
6497 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6498 	else
6499 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6500 
6501 	for (i = 0; i < max_entries; i++)
6502 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6503 
6504 	pad = bp->rss_indir_tbl_entries - max_entries;
6505 	if (pad)
6506 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6507 }
6508 
6509 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6510 {
6511 	u32 i, tbl_size, max_ring = 0;
6512 
6513 	if (!bp->rss_indir_tbl)
6514 		return 0;
6515 
6516 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6517 	for (i = 0; i < tbl_size; i++)
6518 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6519 	return max_ring;
6520 }
6521 
6522 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6523 {
6524 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6525 		if (!rx_rings)
6526 			return 0;
6527 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6528 					       BNXT_RSS_TABLE_ENTRIES_P5);
6529 	}
6530 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6531 		return 2;
6532 	return 1;
6533 }
6534 
6535 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6536 {
6537 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6538 	u16 i, j;
6539 
6540 	/* Fill the RSS indirection table with ring group ids */
6541 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6542 		if (!no_rss)
6543 			j = bp->rss_indir_tbl[i];
6544 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6545 	}
6546 }
6547 
6548 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6549 				    struct bnxt_vnic_info *vnic)
6550 {
6551 	__le16 *ring_tbl = vnic->rss_table;
6552 	struct bnxt_rx_ring_info *rxr;
6553 	u16 tbl_size, i;
6554 
6555 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6556 
6557 	for (i = 0; i < tbl_size; i++) {
6558 		u16 ring_id, j;
6559 
6560 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6561 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6562 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6563 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6564 		else
6565 			j = bp->rss_indir_tbl[i];
6566 		rxr = &bp->rx_ring[j];
6567 
6568 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6569 		*ring_tbl++ = cpu_to_le16(ring_id);
6570 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6571 		*ring_tbl++ = cpu_to_le16(ring_id);
6572 	}
6573 }
6574 
6575 static void
6576 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6577 			 struct bnxt_vnic_info *vnic)
6578 {
6579 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6580 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6581 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6582 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6583 	} else {
6584 		bnxt_fill_hw_rss_tbl(bp, vnic);
6585 	}
6586 
6587 	if (bp->rss_hash_delta) {
6588 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6589 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6590 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6591 		else
6592 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6593 	} else {
6594 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6595 	}
6596 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6597 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6598 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6599 }
6600 
6601 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6602 				  bool set_rss)
6603 {
6604 	struct hwrm_vnic_rss_cfg_input *req;
6605 	int rc;
6606 
6607 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6608 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6609 		return 0;
6610 
6611 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6612 	if (rc)
6613 		return rc;
6614 
6615 	if (set_rss)
6616 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6617 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6618 	return hwrm_req_send(bp, req);
6619 }
6620 
6621 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6622 				     struct bnxt_vnic_info *vnic, bool set_rss)
6623 {
6624 	struct hwrm_vnic_rss_cfg_input *req;
6625 	dma_addr_t ring_tbl_map;
6626 	u32 i, nr_ctxs;
6627 	int rc;
6628 
6629 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6630 	if (rc)
6631 		return rc;
6632 
6633 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6634 	if (!set_rss)
6635 		return hwrm_req_send(bp, req);
6636 
6637 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6638 	ring_tbl_map = vnic->rss_table_dma_addr;
6639 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6640 
6641 	hwrm_req_hold(bp, req);
6642 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6643 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6644 		req->ring_table_pair_index = i;
6645 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6646 		rc = hwrm_req_send(bp, req);
6647 		if (rc)
6648 			goto exit;
6649 	}
6650 
6651 exit:
6652 	hwrm_req_drop(bp, req);
6653 	return rc;
6654 }
6655 
6656 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6657 {
6658 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6659 	struct hwrm_vnic_rss_qcfg_output *resp;
6660 	struct hwrm_vnic_rss_qcfg_input *req;
6661 
6662 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6663 		return;
6664 
6665 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6666 	/* all contexts configured to same hash_type, zero always exists */
6667 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6668 	resp = hwrm_req_hold(bp, req);
6669 	if (!hwrm_req_send(bp, req)) {
6670 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6671 		bp->rss_hash_delta = 0;
6672 	}
6673 	hwrm_req_drop(bp, req);
6674 }
6675 
6676 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6677 {
6678 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6679 	struct hwrm_vnic_plcmodes_cfg_input *req;
6680 	int rc;
6681 
6682 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6683 	if (rc)
6684 		return rc;
6685 
6686 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6687 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6688 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6689 
6690 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6691 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6692 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6693 		req->enables |=
6694 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6695 		req->hds_threshold = cpu_to_le16(hds_thresh);
6696 	}
6697 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6698 	return hwrm_req_send(bp, req);
6699 }
6700 
6701 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6702 					struct bnxt_vnic_info *vnic,
6703 					u16 ctx_idx)
6704 {
6705 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6706 
6707 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6708 		return;
6709 
6710 	req->rss_cos_lb_ctx_id =
6711 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6712 
6713 	hwrm_req_send(bp, req);
6714 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6715 }
6716 
6717 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6718 {
6719 	int i, j;
6720 
6721 	for (i = 0; i < bp->nr_vnics; i++) {
6722 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6723 
6724 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6725 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6726 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6727 		}
6728 	}
6729 	bp->rsscos_nr_ctxs = 0;
6730 }
6731 
6732 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6733 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6734 {
6735 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6736 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6737 	int rc;
6738 
6739 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6740 	if (rc)
6741 		return rc;
6742 
6743 	resp = hwrm_req_hold(bp, req);
6744 	rc = hwrm_req_send(bp, req);
6745 	if (!rc)
6746 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6747 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6748 	hwrm_req_drop(bp, req);
6749 
6750 	return rc;
6751 }
6752 
6753 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6754 {
6755 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6756 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6757 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6758 }
6759 
6760 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6761 {
6762 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6763 	struct hwrm_vnic_cfg_input *req;
6764 	unsigned int ring = 0, grp_idx;
6765 	u16 def_vlan = 0;
6766 	int rc;
6767 
6768 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6769 	if (rc)
6770 		return rc;
6771 
6772 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6773 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6774 
6775 		req->default_rx_ring_id =
6776 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6777 		req->default_cmpl_ring_id =
6778 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6779 		req->enables =
6780 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6781 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6782 		goto vnic_mru;
6783 	}
6784 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6785 	/* Only RSS support for now TBD: COS & LB */
6786 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6787 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6788 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6789 					   VNIC_CFG_REQ_ENABLES_MRU);
6790 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6791 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6792 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6793 					   VNIC_CFG_REQ_ENABLES_MRU);
6794 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6795 	} else {
6796 		req->rss_rule = cpu_to_le16(0xffff);
6797 	}
6798 
6799 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6800 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6801 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6802 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6803 	} else {
6804 		req->cos_rule = cpu_to_le16(0xffff);
6805 	}
6806 
6807 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6808 		ring = 0;
6809 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6810 		ring = vnic->vnic_id - 1;
6811 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6812 		ring = bp->rx_nr_rings - 1;
6813 
6814 	grp_idx = bp->rx_ring[ring].bnapi->index;
6815 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6816 	req->lb_rule = cpu_to_le16(0xffff);
6817 vnic_mru:
6818 	vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
6819 	req->mru = cpu_to_le16(vnic->mru);
6820 
6821 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6822 #ifdef CONFIG_BNXT_SRIOV
6823 	if (BNXT_VF(bp))
6824 		def_vlan = bp->vf.vlan;
6825 #endif
6826 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6827 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6828 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6829 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6830 
6831 	return hwrm_req_send(bp, req);
6832 }
6833 
6834 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6835 				    struct bnxt_vnic_info *vnic)
6836 {
6837 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6838 		struct hwrm_vnic_free_input *req;
6839 
6840 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6841 			return;
6842 
6843 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6844 
6845 		hwrm_req_send(bp, req);
6846 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6847 	}
6848 }
6849 
6850 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6851 {
6852 	u16 i;
6853 
6854 	for (i = 0; i < bp->nr_vnics; i++)
6855 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6856 }
6857 
6858 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6859 			 unsigned int start_rx_ring_idx,
6860 			 unsigned int nr_rings)
6861 {
6862 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6863 	struct hwrm_vnic_alloc_output *resp;
6864 	struct hwrm_vnic_alloc_input *req;
6865 	int rc;
6866 
6867 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6868 	if (rc)
6869 		return rc;
6870 
6871 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6872 		goto vnic_no_ring_grps;
6873 
6874 	/* map ring groups to this vnic */
6875 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6876 		grp_idx = bp->rx_ring[i].bnapi->index;
6877 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6878 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6879 				   j, nr_rings);
6880 			break;
6881 		}
6882 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6883 	}
6884 
6885 vnic_no_ring_grps:
6886 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6887 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6888 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6889 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6890 
6891 	resp = hwrm_req_hold(bp, req);
6892 	rc = hwrm_req_send(bp, req);
6893 	if (!rc)
6894 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6895 	hwrm_req_drop(bp, req);
6896 	return rc;
6897 }
6898 
6899 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6900 {
6901 	struct hwrm_vnic_qcaps_output *resp;
6902 	struct hwrm_vnic_qcaps_input *req;
6903 	int rc;
6904 
6905 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6906 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6907 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6908 	if (bp->hwrm_spec_code < 0x10600)
6909 		return 0;
6910 
6911 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6912 	if (rc)
6913 		return rc;
6914 
6915 	resp = hwrm_req_hold(bp, req);
6916 	rc = hwrm_req_send(bp, req);
6917 	if (!rc) {
6918 		u32 flags = le32_to_cpu(resp->flags);
6919 
6920 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6921 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6922 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6923 		if (flags &
6924 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6925 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6926 
6927 		/* Older P5 fw before EXT_HW_STATS support did not set
6928 		 * VLAN_STRIP_CAP properly.
6929 		 */
6930 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6931 		    (BNXT_CHIP_P5(bp) &&
6932 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6933 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6934 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6935 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6936 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6937 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6938 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6939 		if (bp->max_tpa_v2) {
6940 			if (BNXT_CHIP_P5(bp))
6941 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6942 			else
6943 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6944 		}
6945 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6946 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6947 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6948 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6949 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6950 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6951 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6952 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6953 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6954 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6955 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6956 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6957 	}
6958 	hwrm_req_drop(bp, req);
6959 	return rc;
6960 }
6961 
6962 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6963 {
6964 	struct hwrm_ring_grp_alloc_output *resp;
6965 	struct hwrm_ring_grp_alloc_input *req;
6966 	int rc;
6967 	u16 i;
6968 
6969 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6970 		return 0;
6971 
6972 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6973 	if (rc)
6974 		return rc;
6975 
6976 	resp = hwrm_req_hold(bp, req);
6977 	for (i = 0; i < bp->rx_nr_rings; i++) {
6978 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6979 
6980 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6981 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6982 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6983 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6984 
6985 		rc = hwrm_req_send(bp, req);
6986 
6987 		if (rc)
6988 			break;
6989 
6990 		bp->grp_info[grp_idx].fw_grp_id =
6991 			le32_to_cpu(resp->ring_group_id);
6992 	}
6993 	hwrm_req_drop(bp, req);
6994 	return rc;
6995 }
6996 
6997 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6998 {
6999 	struct hwrm_ring_grp_free_input *req;
7000 	u16 i;
7001 
7002 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7003 		return;
7004 
7005 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
7006 		return;
7007 
7008 	hwrm_req_hold(bp, req);
7009 	for (i = 0; i < bp->cp_nr_rings; i++) {
7010 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
7011 			continue;
7012 		req->ring_group_id =
7013 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
7014 
7015 		hwrm_req_send(bp, req);
7016 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
7017 	}
7018 	hwrm_req_drop(bp, req);
7019 }
7020 
7021 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
7022 				       struct hwrm_ring_alloc_input *req,
7023 				       struct bnxt_ring_struct *ring)
7024 {
7025 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
7026 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
7027 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
7028 
7029 	if (ring_type == HWRM_RING_ALLOC_AGG) {
7030 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7031 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7032 		req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
7033 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
7034 	} else {
7035 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
7036 		if (NET_IP_ALIGN == 2)
7037 			req->flags =
7038 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
7039 	}
7040 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7041 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7042 	req->enables |= cpu_to_le32(enables);
7043 }
7044 
7045 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7046 				    struct bnxt_ring_struct *ring,
7047 				    u32 ring_type, u32 map_index)
7048 {
7049 	struct hwrm_ring_alloc_output *resp;
7050 	struct hwrm_ring_alloc_input *req;
7051 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7052 	struct bnxt_ring_grp_info *grp_info;
7053 	int rc, err = 0;
7054 	u16 ring_id;
7055 
7056 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7057 	if (rc)
7058 		goto exit;
7059 
7060 	req->enables = 0;
7061 	if (rmem->nr_pages > 1) {
7062 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7063 		/* Page size is in log2 units */
7064 		req->page_size = BNXT_PAGE_SHIFT;
7065 		req->page_tbl_depth = 1;
7066 	} else {
7067 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7068 	}
7069 	req->fbo = 0;
7070 	/* Association of ring index with doorbell index and MSIX number */
7071 	req->logical_id = cpu_to_le16(map_index);
7072 
7073 	switch (ring_type) {
7074 	case HWRM_RING_ALLOC_TX: {
7075 		struct bnxt_tx_ring_info *txr;
7076 		u16 flags = 0;
7077 
7078 		txr = container_of(ring, struct bnxt_tx_ring_info,
7079 				   tx_ring_struct);
7080 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7081 		/* Association of transmit ring with completion ring */
7082 		grp_info = &bp->grp_info[ring->grp_idx];
7083 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7084 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7085 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7086 		req->queue_id = cpu_to_le16(ring->queue_id);
7087 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7088 			req->cmpl_coal_cnt =
7089 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7090 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7091 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7092 		req->flags = cpu_to_le16(flags);
7093 		break;
7094 	}
7095 	case HWRM_RING_ALLOC_RX:
7096 	case HWRM_RING_ALLOC_AGG:
7097 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7098 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7099 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7100 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7101 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7102 			bnxt_set_rx_ring_params_p5(bp, ring_type, req, ring);
7103 		break;
7104 	case HWRM_RING_ALLOC_CMPL:
7105 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7106 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7107 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7108 			/* Association of cp ring with nq */
7109 			grp_info = &bp->grp_info[map_index];
7110 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7111 			req->cq_handle = cpu_to_le64(ring->handle);
7112 			req->enables |= cpu_to_le32(
7113 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7114 		} else {
7115 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7116 		}
7117 		break;
7118 	case HWRM_RING_ALLOC_NQ:
7119 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7120 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7121 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7122 		break;
7123 	default:
7124 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7125 			   ring_type);
7126 		return -EINVAL;
7127 	}
7128 
7129 	resp = hwrm_req_hold(bp, req);
7130 	rc = hwrm_req_send(bp, req);
7131 	err = le16_to_cpu(resp->error_code);
7132 	ring_id = le16_to_cpu(resp->ring_id);
7133 	hwrm_req_drop(bp, req);
7134 
7135 exit:
7136 	if (rc || err) {
7137 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7138 			   ring_type, rc, err);
7139 		return -EIO;
7140 	}
7141 	ring->fw_ring_id = ring_id;
7142 	return rc;
7143 }
7144 
7145 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7146 {
7147 	int rc;
7148 
7149 	if (BNXT_PF(bp)) {
7150 		struct hwrm_func_cfg_input *req;
7151 
7152 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7153 		if (rc)
7154 			return rc;
7155 
7156 		req->fid = cpu_to_le16(0xffff);
7157 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7158 		req->async_event_cr = cpu_to_le16(idx);
7159 		return hwrm_req_send(bp, req);
7160 	} else {
7161 		struct hwrm_func_vf_cfg_input *req;
7162 
7163 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7164 		if (rc)
7165 			return rc;
7166 
7167 		req->enables =
7168 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7169 		req->async_event_cr = cpu_to_le16(idx);
7170 		return hwrm_req_send(bp, req);
7171 	}
7172 }
7173 
7174 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7175 			     u32 ring_type)
7176 {
7177 	switch (ring_type) {
7178 	case HWRM_RING_ALLOC_TX:
7179 		db->db_ring_mask = bp->tx_ring_mask;
7180 		break;
7181 	case HWRM_RING_ALLOC_RX:
7182 		db->db_ring_mask = bp->rx_ring_mask;
7183 		break;
7184 	case HWRM_RING_ALLOC_AGG:
7185 		db->db_ring_mask = bp->rx_agg_ring_mask;
7186 		break;
7187 	case HWRM_RING_ALLOC_CMPL:
7188 	case HWRM_RING_ALLOC_NQ:
7189 		db->db_ring_mask = bp->cp_ring_mask;
7190 		break;
7191 	}
7192 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7193 		db->db_epoch_mask = db->db_ring_mask + 1;
7194 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7195 	}
7196 }
7197 
7198 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7199 			u32 map_idx, u32 xid)
7200 {
7201 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7202 		switch (ring_type) {
7203 		case HWRM_RING_ALLOC_TX:
7204 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7205 			break;
7206 		case HWRM_RING_ALLOC_RX:
7207 		case HWRM_RING_ALLOC_AGG:
7208 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7209 			break;
7210 		case HWRM_RING_ALLOC_CMPL:
7211 			db->db_key64 = DBR_PATH_L2;
7212 			break;
7213 		case HWRM_RING_ALLOC_NQ:
7214 			db->db_key64 = DBR_PATH_L2;
7215 			break;
7216 		}
7217 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7218 
7219 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7220 			db->db_key64 |= DBR_VALID;
7221 
7222 		db->doorbell = bp->bar1 + bp->db_offset;
7223 	} else {
7224 		db->doorbell = bp->bar1 + map_idx * 0x80;
7225 		switch (ring_type) {
7226 		case HWRM_RING_ALLOC_TX:
7227 			db->db_key32 = DB_KEY_TX;
7228 			break;
7229 		case HWRM_RING_ALLOC_RX:
7230 		case HWRM_RING_ALLOC_AGG:
7231 			db->db_key32 = DB_KEY_RX;
7232 			break;
7233 		case HWRM_RING_ALLOC_CMPL:
7234 			db->db_key32 = DB_KEY_CP;
7235 			break;
7236 		}
7237 	}
7238 	bnxt_set_db_mask(bp, db, ring_type);
7239 }
7240 
7241 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7242 				   struct bnxt_rx_ring_info *rxr)
7243 {
7244 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7245 	struct bnxt_napi *bnapi = rxr->bnapi;
7246 	u32 type = HWRM_RING_ALLOC_RX;
7247 	u32 map_idx = bnapi->index;
7248 	int rc;
7249 
7250 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7251 	if (rc)
7252 		return rc;
7253 
7254 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7255 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7256 
7257 	return 0;
7258 }
7259 
7260 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7261 				       struct bnxt_rx_ring_info *rxr)
7262 {
7263 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7264 	u32 type = HWRM_RING_ALLOC_AGG;
7265 	u32 grp_idx = ring->grp_idx;
7266 	u32 map_idx;
7267 	int rc;
7268 
7269 	map_idx = grp_idx + bp->rx_nr_rings;
7270 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7271 	if (rc)
7272 		return rc;
7273 
7274 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7275 		    ring->fw_ring_id);
7276 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7277 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7278 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7279 
7280 	return 0;
7281 }
7282 
7283 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7284 				      struct bnxt_cp_ring_info *cpr)
7285 {
7286 	const u32 type = HWRM_RING_ALLOC_CMPL;
7287 	struct bnxt_napi *bnapi = cpr->bnapi;
7288 	struct bnxt_ring_struct *ring;
7289 	u32 map_idx = bnapi->index;
7290 	int rc;
7291 
7292 	ring = &cpr->cp_ring_struct;
7293 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7294 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7295 	if (rc)
7296 		return rc;
7297 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7298 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7299 	return 0;
7300 }
7301 
7302 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7303 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7304 {
7305 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7306 	const u32 type = HWRM_RING_ALLOC_TX;
7307 	int rc;
7308 
7309 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, tx_idx);
7310 	if (rc)
7311 		return rc;
7312 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7313 	return 0;
7314 }
7315 
7316 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7317 {
7318 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7319 	int i, rc = 0;
7320 	u32 type;
7321 
7322 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7323 		type = HWRM_RING_ALLOC_NQ;
7324 	else
7325 		type = HWRM_RING_ALLOC_CMPL;
7326 	for (i = 0; i < bp->cp_nr_rings; i++) {
7327 		struct bnxt_napi *bnapi = bp->bnapi[i];
7328 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7329 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7330 		u32 map_idx = ring->map_idx;
7331 		unsigned int vector;
7332 
7333 		vector = bp->irq_tbl[map_idx].vector;
7334 		disable_irq_nosync(vector);
7335 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7336 		if (rc) {
7337 			enable_irq(vector);
7338 			goto err_out;
7339 		}
7340 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7341 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7342 		enable_irq(vector);
7343 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7344 
7345 		if (!i) {
7346 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7347 			if (rc)
7348 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7349 		}
7350 	}
7351 
7352 	for (i = 0; i < bp->tx_nr_rings; i++) {
7353 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7354 
7355 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7356 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7357 			if (rc)
7358 				goto err_out;
7359 		}
7360 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7361 		if (rc)
7362 			goto err_out;
7363 	}
7364 
7365 	for (i = 0; i < bp->rx_nr_rings; i++) {
7366 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7367 
7368 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7369 		if (rc)
7370 			goto err_out;
7371 		/* If we have agg rings, post agg buffers first. */
7372 		if (!agg_rings)
7373 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7374 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7375 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7376 			if (rc)
7377 				goto err_out;
7378 		}
7379 	}
7380 
7381 	if (agg_rings) {
7382 		for (i = 0; i < bp->rx_nr_rings; i++) {
7383 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7384 			if (rc)
7385 				goto err_out;
7386 		}
7387 	}
7388 err_out:
7389 	return rc;
7390 }
7391 
7392 static void bnxt_cancel_dim(struct bnxt *bp)
7393 {
7394 	int i;
7395 
7396 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7397 	 * if NAPI is enabled.
7398 	 */
7399 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7400 		return;
7401 
7402 	/* Make sure NAPI sees that the VNIC is disabled */
7403 	synchronize_net();
7404 	for (i = 0; i < bp->rx_nr_rings; i++) {
7405 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7406 		struct bnxt_napi *bnapi = rxr->bnapi;
7407 
7408 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7409 	}
7410 }
7411 
7412 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7413 				   struct bnxt_ring_struct *ring,
7414 				   u32 ring_type, int cmpl_ring_id)
7415 {
7416 	struct hwrm_ring_free_output *resp;
7417 	struct hwrm_ring_free_input *req;
7418 	u16 error_code = 0;
7419 	int rc;
7420 
7421 	if (BNXT_NO_FW_ACCESS(bp))
7422 		return 0;
7423 
7424 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7425 	if (rc)
7426 		goto exit;
7427 
7428 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7429 	req->ring_type = ring_type;
7430 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7431 
7432 	resp = hwrm_req_hold(bp, req);
7433 	rc = hwrm_req_send(bp, req);
7434 	error_code = le16_to_cpu(resp->error_code);
7435 	hwrm_req_drop(bp, req);
7436 exit:
7437 	if (rc || error_code) {
7438 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7439 			   ring_type, rc, error_code);
7440 		return -EIO;
7441 	}
7442 	return 0;
7443 }
7444 
7445 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7446 				   struct bnxt_tx_ring_info *txr,
7447 				   bool close_path)
7448 {
7449 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7450 	u32 cmpl_ring_id;
7451 
7452 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7453 		return;
7454 
7455 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7456 		       INVALID_HW_RING_ID;
7457 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7458 				cmpl_ring_id);
7459 	ring->fw_ring_id = INVALID_HW_RING_ID;
7460 }
7461 
7462 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7463 				   struct bnxt_rx_ring_info *rxr,
7464 				   bool close_path)
7465 {
7466 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7467 	u32 grp_idx = rxr->bnapi->index;
7468 	u32 cmpl_ring_id;
7469 
7470 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7471 		return;
7472 
7473 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7474 	hwrm_ring_free_send_msg(bp, ring,
7475 				RING_FREE_REQ_RING_TYPE_RX,
7476 				close_path ? cmpl_ring_id :
7477 				INVALID_HW_RING_ID);
7478 	ring->fw_ring_id = INVALID_HW_RING_ID;
7479 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7480 }
7481 
7482 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7483 				       struct bnxt_rx_ring_info *rxr,
7484 				       bool close_path)
7485 {
7486 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7487 	u32 grp_idx = rxr->bnapi->index;
7488 	u32 type, cmpl_ring_id;
7489 
7490 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7491 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7492 	else
7493 		type = RING_FREE_REQ_RING_TYPE_RX;
7494 
7495 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7496 		return;
7497 
7498 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7499 	hwrm_ring_free_send_msg(bp, ring, type,
7500 				close_path ? cmpl_ring_id :
7501 				INVALID_HW_RING_ID);
7502 	ring->fw_ring_id = INVALID_HW_RING_ID;
7503 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7504 }
7505 
7506 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7507 				   struct bnxt_cp_ring_info *cpr)
7508 {
7509 	struct bnxt_ring_struct *ring;
7510 
7511 	ring = &cpr->cp_ring_struct;
7512 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7513 		return;
7514 
7515 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7516 				INVALID_HW_RING_ID);
7517 	ring->fw_ring_id = INVALID_HW_RING_ID;
7518 }
7519 
7520 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7521 {
7522 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7523 	int i, size = ring->ring_mem.page_size;
7524 
7525 	cpr->cp_raw_cons = 0;
7526 	cpr->toggle = 0;
7527 
7528 	for (i = 0; i < bp->cp_nr_pages; i++)
7529 		if (cpr->cp_desc_ring[i])
7530 			memset(cpr->cp_desc_ring[i], 0, size);
7531 }
7532 
7533 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7534 {
7535 	u32 type;
7536 	int i;
7537 
7538 	if (!bp->bnapi)
7539 		return;
7540 
7541 	for (i = 0; i < bp->tx_nr_rings; i++)
7542 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7543 
7544 	bnxt_cancel_dim(bp);
7545 	for (i = 0; i < bp->rx_nr_rings; i++) {
7546 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7547 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7548 	}
7549 
7550 	/* The completion rings are about to be freed.  After that the
7551 	 * IRQ doorbell will not work anymore.  So we need to disable
7552 	 * IRQ here.
7553 	 */
7554 	bnxt_disable_int_sync(bp);
7555 
7556 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7557 		type = RING_FREE_REQ_RING_TYPE_NQ;
7558 	else
7559 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7560 	for (i = 0; i < bp->cp_nr_rings; i++) {
7561 		struct bnxt_napi *bnapi = bp->bnapi[i];
7562 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7563 		struct bnxt_ring_struct *ring;
7564 		int j;
7565 
7566 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7567 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7568 
7569 		ring = &cpr->cp_ring_struct;
7570 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7571 			hwrm_ring_free_send_msg(bp, ring, type,
7572 						INVALID_HW_RING_ID);
7573 			ring->fw_ring_id = INVALID_HW_RING_ID;
7574 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7575 		}
7576 	}
7577 }
7578 
7579 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7580 			     bool shared);
7581 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7582 			   bool shared);
7583 
7584 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7585 {
7586 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7587 	struct hwrm_func_qcfg_output *resp;
7588 	struct hwrm_func_qcfg_input *req;
7589 	int rc;
7590 
7591 	if (bp->hwrm_spec_code < 0x10601)
7592 		return 0;
7593 
7594 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7595 	if (rc)
7596 		return rc;
7597 
7598 	req->fid = cpu_to_le16(0xffff);
7599 	resp = hwrm_req_hold(bp, req);
7600 	rc = hwrm_req_send(bp, req);
7601 	if (rc) {
7602 		hwrm_req_drop(bp, req);
7603 		return rc;
7604 	}
7605 
7606 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7607 	if (BNXT_NEW_RM(bp)) {
7608 		u16 cp, stats;
7609 
7610 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7611 		hw_resc->resv_hw_ring_grps =
7612 			le32_to_cpu(resp->alloc_hw_ring_grps);
7613 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7614 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7615 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7616 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7617 		hw_resc->resv_irqs = cp;
7618 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7619 			int rx = hw_resc->resv_rx_rings;
7620 			int tx = hw_resc->resv_tx_rings;
7621 
7622 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7623 				rx >>= 1;
7624 			if (cp < (rx + tx)) {
7625 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7626 				if (rc)
7627 					goto get_rings_exit;
7628 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7629 					rx <<= 1;
7630 				hw_resc->resv_rx_rings = rx;
7631 				hw_resc->resv_tx_rings = tx;
7632 			}
7633 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7634 			hw_resc->resv_hw_ring_grps = rx;
7635 		}
7636 		hw_resc->resv_cp_rings = cp;
7637 		hw_resc->resv_stat_ctxs = stats;
7638 	}
7639 get_rings_exit:
7640 	hwrm_req_drop(bp, req);
7641 	return rc;
7642 }
7643 
7644 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7645 {
7646 	struct hwrm_func_qcfg_output *resp;
7647 	struct hwrm_func_qcfg_input *req;
7648 	int rc;
7649 
7650 	if (bp->hwrm_spec_code < 0x10601)
7651 		return 0;
7652 
7653 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7654 	if (rc)
7655 		return rc;
7656 
7657 	req->fid = cpu_to_le16(fid);
7658 	resp = hwrm_req_hold(bp, req);
7659 	rc = hwrm_req_send(bp, req);
7660 	if (!rc)
7661 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7662 
7663 	hwrm_req_drop(bp, req);
7664 	return rc;
7665 }
7666 
7667 static bool bnxt_rfs_supported(struct bnxt *bp);
7668 
7669 static struct hwrm_func_cfg_input *
7670 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7671 {
7672 	struct hwrm_func_cfg_input *req;
7673 	u32 enables = 0;
7674 
7675 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7676 		return NULL;
7677 
7678 	req->fid = cpu_to_le16(0xffff);
7679 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7680 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7681 	if (BNXT_NEW_RM(bp)) {
7682 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7683 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7684 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7685 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7686 			enables |= hwr->cp_p5 ?
7687 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7688 		} else {
7689 			enables |= hwr->cp ?
7690 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7691 			enables |= hwr->grp ?
7692 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7693 		}
7694 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7695 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7696 					  0;
7697 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7698 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7699 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7700 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7701 			req->num_msix = cpu_to_le16(hwr->cp);
7702 		} else {
7703 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7704 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7705 		}
7706 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7707 		req->num_vnics = cpu_to_le16(hwr->vnic);
7708 	}
7709 	req->enables = cpu_to_le32(enables);
7710 	return req;
7711 }
7712 
7713 static struct hwrm_func_vf_cfg_input *
7714 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7715 {
7716 	struct hwrm_func_vf_cfg_input *req;
7717 	u32 enables = 0;
7718 
7719 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7720 		return NULL;
7721 
7722 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7723 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7724 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7725 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7726 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7727 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7728 		enables |= hwr->cp_p5 ?
7729 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7730 	} else {
7731 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7732 		enables |= hwr->grp ?
7733 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7734 	}
7735 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7736 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7737 
7738 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7739 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7740 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7741 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7742 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7743 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7744 	} else {
7745 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7746 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7747 	}
7748 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7749 	req->num_vnics = cpu_to_le16(hwr->vnic);
7750 
7751 	req->enables = cpu_to_le32(enables);
7752 	return req;
7753 }
7754 
7755 static int
7756 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7757 {
7758 	struct hwrm_func_cfg_input *req;
7759 	int rc;
7760 
7761 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7762 	if (!req)
7763 		return -ENOMEM;
7764 
7765 	if (!req->enables) {
7766 		hwrm_req_drop(bp, req);
7767 		return 0;
7768 	}
7769 
7770 	rc = hwrm_req_send(bp, req);
7771 	if (rc)
7772 		return rc;
7773 
7774 	if (bp->hwrm_spec_code < 0x10601)
7775 		bp->hw_resc.resv_tx_rings = hwr->tx;
7776 
7777 	return bnxt_hwrm_get_rings(bp);
7778 }
7779 
7780 static int
7781 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7782 {
7783 	struct hwrm_func_vf_cfg_input *req;
7784 	int rc;
7785 
7786 	if (!BNXT_NEW_RM(bp)) {
7787 		bp->hw_resc.resv_tx_rings = hwr->tx;
7788 		return 0;
7789 	}
7790 
7791 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7792 	if (!req)
7793 		return -ENOMEM;
7794 
7795 	rc = hwrm_req_send(bp, req);
7796 	if (rc)
7797 		return rc;
7798 
7799 	return bnxt_hwrm_get_rings(bp);
7800 }
7801 
7802 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7803 {
7804 	if (BNXT_PF(bp))
7805 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7806 	else
7807 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7808 }
7809 
7810 int bnxt_nq_rings_in_use(struct bnxt *bp)
7811 {
7812 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7813 }
7814 
7815 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7816 {
7817 	int cp;
7818 
7819 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7820 		return bnxt_nq_rings_in_use(bp);
7821 
7822 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7823 	return cp;
7824 }
7825 
7826 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7827 {
7828 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7829 }
7830 
7831 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7832 {
7833 	if (!hwr->grp)
7834 		return 0;
7835 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7836 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7837 
7838 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7839 			rss_ctx *= hwr->vnic;
7840 		return rss_ctx;
7841 	}
7842 	if (BNXT_VF(bp))
7843 		return BNXT_VF_MAX_RSS_CTX;
7844 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7845 		return hwr->grp + 1;
7846 	return 1;
7847 }
7848 
7849 /* Check if a default RSS map needs to be setup.  This function is only
7850  * used on older firmware that does not require reserving RX rings.
7851  */
7852 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7853 {
7854 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7855 
7856 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7857 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7858 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7859 		if (!netif_is_rxfh_configured(bp->dev))
7860 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7861 	}
7862 }
7863 
7864 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7865 {
7866 	if (bp->flags & BNXT_FLAG_RFS) {
7867 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7868 			return 2 + bp->num_rss_ctx;
7869 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7870 			return rx_rings + 1;
7871 	}
7872 	return 1;
7873 }
7874 
7875 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7876 {
7877 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7878 	int cp = bnxt_cp_rings_in_use(bp);
7879 	int nq = bnxt_nq_rings_in_use(bp);
7880 	int rx = bp->rx_nr_rings, stat;
7881 	int vnic, grp = rx;
7882 
7883 	/* Old firmware does not need RX ring reservations but we still
7884 	 * need to setup a default RSS map when needed.  With new firmware
7885 	 * we go through RX ring reservations first and then set up the
7886 	 * RSS map for the successfully reserved RX rings when needed.
7887 	 */
7888 	if (!BNXT_NEW_RM(bp))
7889 		bnxt_check_rss_tbl_no_rmgr(bp);
7890 
7891 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7892 	    bp->hwrm_spec_code >= 0x10601)
7893 		return true;
7894 
7895 	if (!BNXT_NEW_RM(bp))
7896 		return false;
7897 
7898 	vnic = bnxt_get_total_vnics(bp, rx);
7899 
7900 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7901 		rx <<= 1;
7902 	stat = bnxt_get_func_stat_ctxs(bp);
7903 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7904 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7905 	    (hw_resc->resv_hw_ring_grps != grp &&
7906 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7907 		return true;
7908 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7909 	    hw_resc->resv_irqs != nq)
7910 		return true;
7911 	return false;
7912 }
7913 
7914 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7915 {
7916 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7917 
7918 	hwr->tx = hw_resc->resv_tx_rings;
7919 	if (BNXT_NEW_RM(bp)) {
7920 		hwr->rx = hw_resc->resv_rx_rings;
7921 		hwr->cp = hw_resc->resv_irqs;
7922 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7923 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7924 		hwr->grp = hw_resc->resv_hw_ring_grps;
7925 		hwr->vnic = hw_resc->resv_vnics;
7926 		hwr->stat = hw_resc->resv_stat_ctxs;
7927 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7928 	}
7929 }
7930 
7931 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7932 {
7933 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7934 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7935 }
7936 
7937 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7938 
7939 static int __bnxt_reserve_rings(struct bnxt *bp)
7940 {
7941 	struct bnxt_hw_rings hwr = {0};
7942 	int rx_rings, old_rx_rings, rc;
7943 	int cp = bp->cp_nr_rings;
7944 	int ulp_msix = 0;
7945 	bool sh = false;
7946 	int tx_cp;
7947 
7948 	if (!bnxt_need_reserve_rings(bp))
7949 		return 0;
7950 
7951 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7952 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7953 		if (!ulp_msix)
7954 			bnxt_set_ulp_stat_ctxs(bp, 0);
7955 
7956 		if (ulp_msix > bp->ulp_num_msix_want)
7957 			ulp_msix = bp->ulp_num_msix_want;
7958 		hwr.cp = cp + ulp_msix;
7959 	} else {
7960 		hwr.cp = bnxt_nq_rings_in_use(bp);
7961 	}
7962 
7963 	hwr.tx = bp->tx_nr_rings;
7964 	hwr.rx = bp->rx_nr_rings;
7965 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7966 		sh = true;
7967 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7968 		hwr.cp_p5 = hwr.rx + hwr.tx;
7969 
7970 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7971 
7972 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7973 		hwr.rx <<= 1;
7974 	hwr.grp = bp->rx_nr_rings;
7975 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7976 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7977 	old_rx_rings = bp->hw_resc.resv_rx_rings;
7978 
7979 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7980 	if (rc)
7981 		return rc;
7982 
7983 	bnxt_copy_reserved_rings(bp, &hwr);
7984 
7985 	rx_rings = hwr.rx;
7986 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7987 		if (hwr.rx >= 2) {
7988 			rx_rings = hwr.rx >> 1;
7989 		} else {
7990 			if (netif_running(bp->dev))
7991 				return -ENOMEM;
7992 
7993 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7994 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7995 			bp->dev->hw_features &= ~NETIF_F_LRO;
7996 			bp->dev->features &= ~NETIF_F_LRO;
7997 			bnxt_set_ring_params(bp);
7998 		}
7999 	}
8000 	rx_rings = min_t(int, rx_rings, hwr.grp);
8001 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
8002 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
8003 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
8004 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
8005 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
8006 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8007 		hwr.rx = rx_rings << 1;
8008 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
8009 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
8010 	bp->tx_nr_rings = hwr.tx;
8011 
8012 	/* If we cannot reserve all the RX rings, reset the RSS map only
8013 	 * if absolutely necessary
8014 	 */
8015 	if (rx_rings != bp->rx_nr_rings) {
8016 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
8017 			    rx_rings, bp->rx_nr_rings);
8018 		if (netif_is_rxfh_configured(bp->dev) &&
8019 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
8020 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
8021 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
8022 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
8023 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
8024 		}
8025 	}
8026 	bp->rx_nr_rings = rx_rings;
8027 	bp->cp_nr_rings = hwr.cp;
8028 
8029 	if (!bnxt_rings_ok(bp, &hwr))
8030 		return -ENOMEM;
8031 
8032 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
8033 	    !netif_is_rxfh_configured(bp->dev))
8034 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8035 
8036 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
8037 		int resv_msix, resv_ctx, ulp_ctxs;
8038 		struct bnxt_hw_resc *hw_resc;
8039 
8040 		hw_resc = &bp->hw_resc;
8041 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
8042 		ulp_msix = min_t(int, resv_msix, ulp_msix);
8043 		bnxt_set_ulp_msix_num(bp, ulp_msix);
8044 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8045 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8046 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8047 	}
8048 
8049 	return rc;
8050 }
8051 
8052 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8053 {
8054 	struct hwrm_func_vf_cfg_input *req;
8055 	u32 flags;
8056 
8057 	if (!BNXT_NEW_RM(bp))
8058 		return 0;
8059 
8060 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8061 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8062 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8063 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8064 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8065 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8066 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8067 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8068 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8069 
8070 	req->flags = cpu_to_le32(flags);
8071 	return hwrm_req_send_silent(bp, req);
8072 }
8073 
8074 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8075 {
8076 	struct hwrm_func_cfg_input *req;
8077 	u32 flags;
8078 
8079 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8080 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8081 	if (BNXT_NEW_RM(bp)) {
8082 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8083 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8084 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8085 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8086 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8087 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8088 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8089 		else
8090 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8091 	}
8092 
8093 	req->flags = cpu_to_le32(flags);
8094 	return hwrm_req_send_silent(bp, req);
8095 }
8096 
8097 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8098 {
8099 	if (bp->hwrm_spec_code < 0x10801)
8100 		return 0;
8101 
8102 	if (BNXT_PF(bp))
8103 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8104 
8105 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8106 }
8107 
8108 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8109 {
8110 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8111 	struct hwrm_ring_aggint_qcaps_output *resp;
8112 	struct hwrm_ring_aggint_qcaps_input *req;
8113 	int rc;
8114 
8115 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8116 	coal_cap->num_cmpl_dma_aggr_max = 63;
8117 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8118 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8119 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8120 	coal_cap->int_lat_tmr_min_max = 65535;
8121 	coal_cap->int_lat_tmr_max_max = 65535;
8122 	coal_cap->num_cmpl_aggr_int_max = 65535;
8123 	coal_cap->timer_units = 80;
8124 
8125 	if (bp->hwrm_spec_code < 0x10902)
8126 		return;
8127 
8128 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8129 		return;
8130 
8131 	resp = hwrm_req_hold(bp, req);
8132 	rc = hwrm_req_send_silent(bp, req);
8133 	if (!rc) {
8134 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8135 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8136 		coal_cap->num_cmpl_dma_aggr_max =
8137 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8138 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8139 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8140 		coal_cap->cmpl_aggr_dma_tmr_max =
8141 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8142 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8143 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8144 		coal_cap->int_lat_tmr_min_max =
8145 			le16_to_cpu(resp->int_lat_tmr_min_max);
8146 		coal_cap->int_lat_tmr_max_max =
8147 			le16_to_cpu(resp->int_lat_tmr_max_max);
8148 		coal_cap->num_cmpl_aggr_int_max =
8149 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8150 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8151 	}
8152 	hwrm_req_drop(bp, req);
8153 }
8154 
8155 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8156 {
8157 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8158 
8159 	return usec * 1000 / coal_cap->timer_units;
8160 }
8161 
8162 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8163 	struct bnxt_coal *hw_coal,
8164 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8165 {
8166 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8167 	u16 val, tmr, max, flags = hw_coal->flags;
8168 	u32 cmpl_params = coal_cap->cmpl_params;
8169 
8170 	max = hw_coal->bufs_per_record * 128;
8171 	if (hw_coal->budget)
8172 		max = hw_coal->bufs_per_record * hw_coal->budget;
8173 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8174 
8175 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8176 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8177 
8178 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8179 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8180 
8181 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8182 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8183 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8184 
8185 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8186 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8187 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8188 
8189 	/* min timer set to 1/2 of interrupt timer */
8190 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8191 		val = tmr / 2;
8192 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8193 		req->int_lat_tmr_min = cpu_to_le16(val);
8194 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8195 	}
8196 
8197 	/* buf timer set to 1/4 of interrupt timer */
8198 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8199 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8200 
8201 	if (cmpl_params &
8202 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8203 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8204 		val = clamp_t(u16, tmr, 1,
8205 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8206 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8207 		req->enables |=
8208 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8209 	}
8210 
8211 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8212 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8213 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8214 	req->flags = cpu_to_le16(flags);
8215 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8216 }
8217 
8218 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8219 				   struct bnxt_coal *hw_coal)
8220 {
8221 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8222 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8223 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8224 	u32 nq_params = coal_cap->nq_params;
8225 	u16 tmr;
8226 	int rc;
8227 
8228 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8229 		return 0;
8230 
8231 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8232 	if (rc)
8233 		return rc;
8234 
8235 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8236 	req->flags =
8237 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8238 
8239 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8240 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8241 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8242 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8243 	return hwrm_req_send(bp, req);
8244 }
8245 
8246 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8247 {
8248 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8249 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8250 	struct bnxt_coal coal;
8251 	int rc;
8252 
8253 	/* Tick values in micro seconds.
8254 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8255 	 */
8256 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8257 
8258 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8259 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8260 
8261 	if (!bnapi->rx_ring)
8262 		return -ENODEV;
8263 
8264 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8265 	if (rc)
8266 		return rc;
8267 
8268 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8269 
8270 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8271 
8272 	return hwrm_req_send(bp, req_rx);
8273 }
8274 
8275 static int
8276 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8277 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8278 {
8279 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8280 
8281 	req->ring_id = cpu_to_le16(ring_id);
8282 	return hwrm_req_send(bp, req);
8283 }
8284 
8285 static int
8286 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8287 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8288 {
8289 	struct bnxt_tx_ring_info *txr;
8290 	int i, rc;
8291 
8292 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8293 		u16 ring_id;
8294 
8295 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8296 		req->ring_id = cpu_to_le16(ring_id);
8297 		rc = hwrm_req_send(bp, req);
8298 		if (rc)
8299 			return rc;
8300 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8301 			return 0;
8302 	}
8303 	return 0;
8304 }
8305 
8306 int bnxt_hwrm_set_coal(struct bnxt *bp)
8307 {
8308 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8309 	int i, rc;
8310 
8311 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8312 	if (rc)
8313 		return rc;
8314 
8315 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8316 	if (rc) {
8317 		hwrm_req_drop(bp, req_rx);
8318 		return rc;
8319 	}
8320 
8321 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8322 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8323 
8324 	hwrm_req_hold(bp, req_rx);
8325 	hwrm_req_hold(bp, req_tx);
8326 	for (i = 0; i < bp->cp_nr_rings; i++) {
8327 		struct bnxt_napi *bnapi = bp->bnapi[i];
8328 		struct bnxt_coal *hw_coal;
8329 
8330 		if (!bnapi->rx_ring)
8331 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8332 		else
8333 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8334 		if (rc)
8335 			break;
8336 
8337 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8338 			continue;
8339 
8340 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8341 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8342 			if (rc)
8343 				break;
8344 		}
8345 		if (bnapi->rx_ring)
8346 			hw_coal = &bp->rx_coal;
8347 		else
8348 			hw_coal = &bp->tx_coal;
8349 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8350 	}
8351 	hwrm_req_drop(bp, req_rx);
8352 	hwrm_req_drop(bp, req_tx);
8353 	return rc;
8354 }
8355 
8356 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8357 {
8358 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8359 	struct hwrm_stat_ctx_free_input *req;
8360 	int i;
8361 
8362 	if (!bp->bnapi)
8363 		return;
8364 
8365 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8366 		return;
8367 
8368 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8369 		return;
8370 	if (BNXT_FW_MAJ(bp) <= 20) {
8371 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8372 			hwrm_req_drop(bp, req);
8373 			return;
8374 		}
8375 		hwrm_req_hold(bp, req0);
8376 	}
8377 	hwrm_req_hold(bp, req);
8378 	for (i = 0; i < bp->cp_nr_rings; i++) {
8379 		struct bnxt_napi *bnapi = bp->bnapi[i];
8380 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8381 
8382 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8383 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8384 			if (req0) {
8385 				req0->stat_ctx_id = req->stat_ctx_id;
8386 				hwrm_req_send(bp, req0);
8387 			}
8388 			hwrm_req_send(bp, req);
8389 
8390 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8391 		}
8392 	}
8393 	hwrm_req_drop(bp, req);
8394 	if (req0)
8395 		hwrm_req_drop(bp, req0);
8396 }
8397 
8398 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8399 {
8400 	struct hwrm_stat_ctx_alloc_output *resp;
8401 	struct hwrm_stat_ctx_alloc_input *req;
8402 	int rc, i;
8403 
8404 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8405 		return 0;
8406 
8407 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8408 	if (rc)
8409 		return rc;
8410 
8411 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8412 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8413 
8414 	resp = hwrm_req_hold(bp, req);
8415 	for (i = 0; i < bp->cp_nr_rings; i++) {
8416 		struct bnxt_napi *bnapi = bp->bnapi[i];
8417 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8418 
8419 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8420 
8421 		rc = hwrm_req_send(bp, req);
8422 		if (rc)
8423 			break;
8424 
8425 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8426 
8427 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8428 	}
8429 	hwrm_req_drop(bp, req);
8430 	return rc;
8431 }
8432 
8433 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8434 {
8435 	struct hwrm_func_qcfg_output *resp;
8436 	struct hwrm_func_qcfg_input *req;
8437 	u16 flags;
8438 	int rc;
8439 
8440 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8441 	if (rc)
8442 		return rc;
8443 
8444 	req->fid = cpu_to_le16(0xffff);
8445 	resp = hwrm_req_hold(bp, req);
8446 	rc = hwrm_req_send(bp, req);
8447 	if (rc)
8448 		goto func_qcfg_exit;
8449 
8450 	flags = le16_to_cpu(resp->flags);
8451 #ifdef CONFIG_BNXT_SRIOV
8452 	if (BNXT_VF(bp)) {
8453 		struct bnxt_vf_info *vf = &bp->vf;
8454 
8455 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8456 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8457 			vf->flags |= BNXT_VF_TRUST;
8458 		else
8459 			vf->flags &= ~BNXT_VF_TRUST;
8460 	} else {
8461 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8462 	}
8463 #endif
8464 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8465 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8466 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8467 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8468 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8469 	}
8470 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8471 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8472 
8473 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8474 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8475 
8476 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8477 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8478 
8479 	switch (resp->port_partition_type) {
8480 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8481 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8482 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8483 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8484 		bp->port_partition_type = resp->port_partition_type;
8485 		break;
8486 	}
8487 	if (bp->hwrm_spec_code < 0x10707 ||
8488 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8489 		bp->br_mode = BRIDGE_MODE_VEB;
8490 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8491 		bp->br_mode = BRIDGE_MODE_VEPA;
8492 	else
8493 		bp->br_mode = BRIDGE_MODE_UNDEF;
8494 
8495 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8496 	if (!bp->max_mtu)
8497 		bp->max_mtu = BNXT_MAX_MTU;
8498 
8499 	if (bp->db_size)
8500 		goto func_qcfg_exit;
8501 
8502 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8503 	if (BNXT_CHIP_P5(bp)) {
8504 		if (BNXT_PF(bp))
8505 			bp->db_offset = DB_PF_OFFSET_P5;
8506 		else
8507 			bp->db_offset = DB_VF_OFFSET_P5;
8508 	}
8509 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8510 				 1024);
8511 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8512 	    bp->db_size <= bp->db_offset)
8513 		bp->db_size = pci_resource_len(bp->pdev, 2);
8514 
8515 func_qcfg_exit:
8516 	hwrm_req_drop(bp, req);
8517 	return rc;
8518 }
8519 
8520 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8521 				      u8 init_val, u8 init_offset,
8522 				      bool init_mask_set)
8523 {
8524 	ctxm->init_value = init_val;
8525 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8526 	if (init_mask_set)
8527 		ctxm->init_offset = init_offset * 4;
8528 	else
8529 		ctxm->init_value = 0;
8530 }
8531 
8532 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8533 {
8534 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8535 	u16 type;
8536 
8537 	for (type = 0; type < ctx_max; type++) {
8538 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8539 		int n = 1;
8540 
8541 		if (!ctxm->max_entries || ctxm->pg_info)
8542 			continue;
8543 
8544 		if (ctxm->instance_bmap)
8545 			n = hweight32(ctxm->instance_bmap);
8546 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8547 		if (!ctxm->pg_info)
8548 			return -ENOMEM;
8549 	}
8550 	return 0;
8551 }
8552 
8553 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8554 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8555 
8556 #define BNXT_CTX_INIT_VALID(flags)	\
8557 	(!!((flags) &			\
8558 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8559 
8560 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8561 {
8562 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8563 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8564 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8565 	u16 type;
8566 	int rc;
8567 
8568 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8569 	if (rc)
8570 		return rc;
8571 
8572 	if (!ctx) {
8573 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8574 		if (!ctx)
8575 			return -ENOMEM;
8576 		bp->ctx = ctx;
8577 	}
8578 
8579 	resp = hwrm_req_hold(bp, req);
8580 
8581 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8582 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8583 		u8 init_val, init_off, i;
8584 		u32 max_entries;
8585 		u16 entry_size;
8586 		__le32 *p;
8587 		u32 flags;
8588 
8589 		req->type = cpu_to_le16(type);
8590 		rc = hwrm_req_send(bp, req);
8591 		if (rc)
8592 			goto ctx_done;
8593 		flags = le32_to_cpu(resp->flags);
8594 		type = le16_to_cpu(resp->next_valid_type);
8595 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8596 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8597 			continue;
8598 		}
8599 		entry_size = le16_to_cpu(resp->entry_size);
8600 		max_entries = le32_to_cpu(resp->max_num_entries);
8601 		if (ctxm->mem_valid) {
8602 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8603 			    ctxm->entry_size != entry_size ||
8604 			    ctxm->max_entries != max_entries)
8605 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8606 			else
8607 				continue;
8608 		}
8609 		ctxm->type = le16_to_cpu(resp->type);
8610 		ctxm->entry_size = entry_size;
8611 		ctxm->flags = flags;
8612 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8613 		ctxm->entry_multiple = resp->entry_multiple;
8614 		ctxm->max_entries = max_entries;
8615 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8616 		init_val = resp->ctx_init_value;
8617 		init_off = resp->ctx_init_offset;
8618 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8619 					  BNXT_CTX_INIT_VALID(flags));
8620 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8621 					      BNXT_MAX_SPLIT_ENTRY);
8622 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8623 		     i++, p++)
8624 			ctxm->split[i] = le32_to_cpu(*p);
8625 	}
8626 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8627 
8628 ctx_done:
8629 	hwrm_req_drop(bp, req);
8630 	return rc;
8631 }
8632 
8633 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8634 {
8635 	struct hwrm_func_backing_store_qcaps_output *resp;
8636 	struct hwrm_func_backing_store_qcaps_input *req;
8637 	int rc;
8638 
8639 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8640 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8641 		return 0;
8642 
8643 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8644 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8645 
8646 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8647 	if (rc)
8648 		return rc;
8649 
8650 	resp = hwrm_req_hold(bp, req);
8651 	rc = hwrm_req_send_silent(bp, req);
8652 	if (!rc) {
8653 		struct bnxt_ctx_mem_type *ctxm;
8654 		struct bnxt_ctx_mem_info *ctx;
8655 		u8 init_val, init_idx = 0;
8656 		u16 init_mask;
8657 
8658 		ctx = bp->ctx;
8659 		if (!ctx) {
8660 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8661 			if (!ctx) {
8662 				rc = -ENOMEM;
8663 				goto ctx_err;
8664 			}
8665 			bp->ctx = ctx;
8666 		}
8667 		init_val = resp->ctx_kind_initializer;
8668 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8669 
8670 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8671 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8672 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8673 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8674 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8675 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8676 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8677 					  (init_mask & (1 << init_idx++)) != 0);
8678 
8679 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8680 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8681 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8682 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8683 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8684 					  (init_mask & (1 << init_idx++)) != 0);
8685 
8686 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8687 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8688 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8689 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8690 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8691 					  (init_mask & (1 << init_idx++)) != 0);
8692 
8693 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8694 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8695 		ctxm->max_entries = ctxm->vnic_entries +
8696 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8697 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8698 		bnxt_init_ctx_initializer(ctxm, init_val,
8699 					  resp->vnic_init_offset,
8700 					  (init_mask & (1 << init_idx++)) != 0);
8701 
8702 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8703 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8704 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8705 		bnxt_init_ctx_initializer(ctxm, init_val,
8706 					  resp->stat_init_offset,
8707 					  (init_mask & (1 << init_idx++)) != 0);
8708 
8709 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8710 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8711 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8712 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8713 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8714 		if (!ctxm->entry_multiple)
8715 			ctxm->entry_multiple = 1;
8716 
8717 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8718 
8719 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8720 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8721 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8722 		ctxm->mrav_num_entries_units =
8723 			le16_to_cpu(resp->mrav_num_entries_units);
8724 		bnxt_init_ctx_initializer(ctxm, init_val,
8725 					  resp->mrav_init_offset,
8726 					  (init_mask & (1 << init_idx++)) != 0);
8727 
8728 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8729 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8730 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8731 
8732 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8733 		if (!ctx->tqm_fp_rings_count)
8734 			ctx->tqm_fp_rings_count = bp->max_q;
8735 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8736 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8737 
8738 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8739 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8740 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8741 
8742 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8743 	} else {
8744 		rc = 0;
8745 	}
8746 ctx_err:
8747 	hwrm_req_drop(bp, req);
8748 	return rc;
8749 }
8750 
8751 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8752 				  __le64 *pg_dir)
8753 {
8754 	if (!rmem->nr_pages)
8755 		return;
8756 
8757 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8758 	if (rmem->depth >= 1) {
8759 		if (rmem->depth == 2)
8760 			*pg_attr |= 2;
8761 		else
8762 			*pg_attr |= 1;
8763 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8764 	} else {
8765 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8766 	}
8767 }
8768 
8769 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8770 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8771 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8772 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8773 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8774 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8775 
8776 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8777 {
8778 	struct hwrm_func_backing_store_cfg_input *req;
8779 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8780 	struct bnxt_ctx_pg_info *ctx_pg;
8781 	struct bnxt_ctx_mem_type *ctxm;
8782 	void **__req = (void **)&req;
8783 	u32 req_len = sizeof(*req);
8784 	__le32 *num_entries;
8785 	__le64 *pg_dir;
8786 	u32 flags = 0;
8787 	u8 *pg_attr;
8788 	u32 ena;
8789 	int rc;
8790 	int i;
8791 
8792 	if (!ctx)
8793 		return 0;
8794 
8795 	if (req_len > bp->hwrm_max_ext_req_len)
8796 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8797 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8798 	if (rc)
8799 		return rc;
8800 
8801 	req->enables = cpu_to_le32(enables);
8802 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8803 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8804 		ctx_pg = ctxm->pg_info;
8805 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8806 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8807 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8808 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8809 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8810 				      &req->qpc_pg_size_qpc_lvl,
8811 				      &req->qpc_page_dir);
8812 
8813 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8814 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8815 	}
8816 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8817 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8818 		ctx_pg = ctxm->pg_info;
8819 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8820 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8821 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8822 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8823 				      &req->srq_pg_size_srq_lvl,
8824 				      &req->srq_page_dir);
8825 	}
8826 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8827 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8828 		ctx_pg = ctxm->pg_info;
8829 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8830 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8831 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8832 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8833 				      &req->cq_pg_size_cq_lvl,
8834 				      &req->cq_page_dir);
8835 	}
8836 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8837 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8838 		ctx_pg = ctxm->pg_info;
8839 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8840 		req->vnic_num_ring_table_entries =
8841 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8842 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8843 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8844 				      &req->vnic_pg_size_vnic_lvl,
8845 				      &req->vnic_page_dir);
8846 	}
8847 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8848 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8849 		ctx_pg = ctxm->pg_info;
8850 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8851 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8852 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8853 				      &req->stat_pg_size_stat_lvl,
8854 				      &req->stat_page_dir);
8855 	}
8856 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8857 		u32 units;
8858 
8859 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8860 		ctx_pg = ctxm->pg_info;
8861 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8862 		units = ctxm->mrav_num_entries_units;
8863 		if (units) {
8864 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8865 			u32 entries;
8866 
8867 			num_mr = ctx_pg->entries - num_ah;
8868 			entries = ((num_mr / units) << 16) | (num_ah / units);
8869 			req->mrav_num_entries = cpu_to_le32(entries);
8870 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8871 		}
8872 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8873 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8874 				      &req->mrav_pg_size_mrav_lvl,
8875 				      &req->mrav_page_dir);
8876 	}
8877 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8878 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8879 		ctx_pg = ctxm->pg_info;
8880 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8881 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8882 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8883 				      &req->tim_pg_size_tim_lvl,
8884 				      &req->tim_page_dir);
8885 	}
8886 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8887 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8888 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8889 	     pg_dir = &req->tqm_sp_page_dir,
8890 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8891 	     ctx_pg = ctxm->pg_info;
8892 	     i < BNXT_MAX_TQM_RINGS;
8893 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8894 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8895 		if (!(enables & ena))
8896 			continue;
8897 
8898 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8899 		*num_entries = cpu_to_le32(ctx_pg->entries);
8900 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8901 	}
8902 	req->flags = cpu_to_le32(flags);
8903 	return hwrm_req_send(bp, req);
8904 }
8905 
8906 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8907 				  struct bnxt_ctx_pg_info *ctx_pg)
8908 {
8909 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8910 
8911 	rmem->page_size = BNXT_PAGE_SIZE;
8912 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8913 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8914 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8915 	if (rmem->depth >= 1)
8916 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8917 	return bnxt_alloc_ring(bp, rmem);
8918 }
8919 
8920 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8921 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8922 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8923 {
8924 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8925 	int rc;
8926 
8927 	if (!mem_size)
8928 		return -EINVAL;
8929 
8930 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8931 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8932 		ctx_pg->nr_pages = 0;
8933 		return -EINVAL;
8934 	}
8935 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8936 		int nr_tbls, i;
8937 
8938 		rmem->depth = 2;
8939 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8940 					     GFP_KERNEL);
8941 		if (!ctx_pg->ctx_pg_tbl)
8942 			return -ENOMEM;
8943 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8944 		rmem->nr_pages = nr_tbls;
8945 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8946 		if (rc)
8947 			return rc;
8948 		for (i = 0; i < nr_tbls; i++) {
8949 			struct bnxt_ctx_pg_info *pg_tbl;
8950 
8951 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8952 			if (!pg_tbl)
8953 				return -ENOMEM;
8954 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8955 			rmem = &pg_tbl->ring_mem;
8956 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8957 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8958 			rmem->depth = 1;
8959 			rmem->nr_pages = MAX_CTX_PAGES;
8960 			rmem->ctx_mem = ctxm;
8961 			if (i == (nr_tbls - 1)) {
8962 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8963 
8964 				if (rem)
8965 					rmem->nr_pages = rem;
8966 			}
8967 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8968 			if (rc)
8969 				break;
8970 		}
8971 	} else {
8972 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8973 		if (rmem->nr_pages > 1 || depth)
8974 			rmem->depth = 1;
8975 		rmem->ctx_mem = ctxm;
8976 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8977 	}
8978 	return rc;
8979 }
8980 
8981 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
8982 				    struct bnxt_ctx_pg_info *ctx_pg,
8983 				    void *buf, size_t offset, size_t head,
8984 				    size_t tail)
8985 {
8986 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8987 	size_t nr_pages = ctx_pg->nr_pages;
8988 	int page_size = rmem->page_size;
8989 	size_t len = 0, total_len = 0;
8990 	u16 depth = rmem->depth;
8991 
8992 	tail %= nr_pages * page_size;
8993 	do {
8994 		if (depth > 1) {
8995 			int i = head / (page_size * MAX_CTX_PAGES);
8996 			struct bnxt_ctx_pg_info *pg_tbl;
8997 
8998 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8999 			rmem = &pg_tbl->ring_mem;
9000 		}
9001 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
9002 		head += len;
9003 		offset += len;
9004 		total_len += len;
9005 		if (head >= nr_pages * page_size)
9006 			head = 0;
9007 	} while (head != tail);
9008 	return total_len;
9009 }
9010 
9011 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
9012 				  struct bnxt_ctx_pg_info *ctx_pg)
9013 {
9014 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9015 
9016 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
9017 	    ctx_pg->ctx_pg_tbl) {
9018 		int i, nr_tbls = rmem->nr_pages;
9019 
9020 		for (i = 0; i < nr_tbls; i++) {
9021 			struct bnxt_ctx_pg_info *pg_tbl;
9022 			struct bnxt_ring_mem_info *rmem2;
9023 
9024 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9025 			if (!pg_tbl)
9026 				continue;
9027 			rmem2 = &pg_tbl->ring_mem;
9028 			bnxt_free_ring(bp, rmem2);
9029 			ctx_pg->ctx_pg_arr[i] = NULL;
9030 			kfree(pg_tbl);
9031 			ctx_pg->ctx_pg_tbl[i] = NULL;
9032 		}
9033 		kfree(ctx_pg->ctx_pg_tbl);
9034 		ctx_pg->ctx_pg_tbl = NULL;
9035 	}
9036 	bnxt_free_ring(bp, rmem);
9037 	ctx_pg->nr_pages = 0;
9038 }
9039 
9040 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
9041 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
9042 				   u8 pg_lvl)
9043 {
9044 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9045 	int i, rc = 0, n = 1;
9046 	u32 mem_size;
9047 
9048 	if (!ctxm->entry_size || !ctx_pg)
9049 		return -EINVAL;
9050 	if (ctxm->instance_bmap)
9051 		n = hweight32(ctxm->instance_bmap);
9052 	if (ctxm->entry_multiple)
9053 		entries = roundup(entries, ctxm->entry_multiple);
9054 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9055 	mem_size = entries * ctxm->entry_size;
9056 	for (i = 0; i < n && !rc; i++) {
9057 		ctx_pg[i].entries = entries;
9058 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9059 					    ctxm->init_value ? ctxm : NULL);
9060 	}
9061 	if (!rc)
9062 		ctxm->mem_valid = 1;
9063 	return rc;
9064 }
9065 
9066 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9067 					       struct bnxt_ctx_mem_type *ctxm,
9068 					       bool last)
9069 {
9070 	struct hwrm_func_backing_store_cfg_v2_input *req;
9071 	u32 instance_bmap = ctxm->instance_bmap;
9072 	int i, j, rc = 0, n = 1;
9073 	__le32 *p;
9074 
9075 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9076 		return 0;
9077 
9078 	if (instance_bmap)
9079 		n = hweight32(ctxm->instance_bmap);
9080 	else
9081 		instance_bmap = 1;
9082 
9083 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9084 	if (rc)
9085 		return rc;
9086 	hwrm_req_hold(bp, req);
9087 	req->type = cpu_to_le16(ctxm->type);
9088 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9089 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9090 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9091 		struct bnxt_bs_trace_info *bs_trace;
9092 		u32 enables;
9093 
9094 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9095 		req->enables = cpu_to_le32(enables);
9096 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9097 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9098 	}
9099 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9100 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9101 		p[i] = cpu_to_le32(ctxm->split[i]);
9102 	for (i = 0, j = 0; j < n && !rc; i++) {
9103 		struct bnxt_ctx_pg_info *ctx_pg;
9104 
9105 		if (!(instance_bmap & (1 << i)))
9106 			continue;
9107 		req->instance = cpu_to_le16(i);
9108 		ctx_pg = &ctxm->pg_info[j++];
9109 		if (!ctx_pg->entries)
9110 			continue;
9111 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9112 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9113 				      &req->page_size_pbl_level,
9114 				      &req->page_dir);
9115 		if (last && j == n)
9116 			req->flags =
9117 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9118 		rc = hwrm_req_send(bp, req);
9119 	}
9120 	hwrm_req_drop(bp, req);
9121 	return rc;
9122 }
9123 
9124 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
9125 {
9126 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9127 	struct bnxt_ctx_mem_type *ctxm;
9128 	u16 last_type = BNXT_CTX_INV;
9129 	int rc = 0;
9130 	u16 type;
9131 
9132 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) {
9133 		ctxm = &ctx->ctx_arr[type];
9134 		if (!bnxt_bs_trace_avail(bp, type))
9135 			continue;
9136 		if (!ctxm->mem_valid) {
9137 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9138 						     ctxm->max_entries, 1);
9139 			if (rc) {
9140 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9141 					    type);
9142 				continue;
9143 			}
9144 			bnxt_bs_trace_init(bp, ctxm);
9145 		}
9146 		last_type = type;
9147 	}
9148 
9149 	if (last_type == BNXT_CTX_INV) {
9150 		if (!ena)
9151 			return 0;
9152 		else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
9153 			last_type = BNXT_CTX_MAX - 1;
9154 		else
9155 			last_type = BNXT_CTX_L2_MAX - 1;
9156 	}
9157 	ctx->ctx_arr[last_type].last = 1;
9158 
9159 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9160 		ctxm = &ctx->ctx_arr[type];
9161 
9162 		if (!ctxm->mem_valid)
9163 			continue;
9164 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9165 		if (rc)
9166 			return rc;
9167 	}
9168 	return 0;
9169 }
9170 
9171 /**
9172  * __bnxt_copy_ctx_mem - copy host context memory
9173  * @bp: The driver context
9174  * @ctxm: The pointer to the context memory type
9175  * @buf: The destination buffer or NULL to just obtain the length
9176  * @offset: The buffer offset to copy the data to
9177  * @head: The head offset of context memory to copy from
9178  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9179  *
9180  * This function is called for debugging purposes to dump the host context
9181  * used by the chip.
9182  *
9183  * Return: Length of memory copied
9184  */
9185 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9186 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9187 				  size_t offset, size_t head, size_t tail)
9188 {
9189 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9190 	size_t len = 0, total_len = 0;
9191 	int i, n = 1;
9192 
9193 	if (!ctx_pg)
9194 		return 0;
9195 
9196 	if (ctxm->instance_bmap)
9197 		n = hweight32(ctxm->instance_bmap);
9198 	for (i = 0; i < n; i++) {
9199 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9200 					    tail);
9201 		offset += len;
9202 		total_len += len;
9203 	}
9204 	return total_len;
9205 }
9206 
9207 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9208 			 void *buf, size_t offset)
9209 {
9210 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9211 
9212 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9213 }
9214 
9215 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9216 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9217 {
9218 	struct bnxt_ctx_pg_info *ctx_pg;
9219 	int i, n = 1;
9220 
9221 	ctxm->last = 0;
9222 
9223 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9224 		return;
9225 
9226 	ctx_pg = ctxm->pg_info;
9227 	if (ctx_pg) {
9228 		if (ctxm->instance_bmap)
9229 			n = hweight32(ctxm->instance_bmap);
9230 		for (i = 0; i < n; i++)
9231 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9232 
9233 		kfree(ctx_pg);
9234 		ctxm->pg_info = NULL;
9235 		ctxm->mem_valid = 0;
9236 	}
9237 	memset(ctxm, 0, sizeof(*ctxm));
9238 }
9239 
9240 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9241 {
9242 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9243 	u16 type;
9244 
9245 	if (!ctx)
9246 		return;
9247 
9248 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9249 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9250 
9251 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9252 	if (force) {
9253 		kfree(ctx);
9254 		bp->ctx = NULL;
9255 	}
9256 }
9257 
9258 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9259 {
9260 	struct bnxt_ctx_mem_type *ctxm;
9261 	struct bnxt_ctx_mem_info *ctx;
9262 	u32 l2_qps, qp1_qps, max_qps;
9263 	u32 ena, entries_sp, entries;
9264 	u32 srqs, max_srqs, min;
9265 	u32 num_mr, num_ah;
9266 	u32 extra_srqs = 0;
9267 	u32 extra_qps = 0;
9268 	u32 fast_qpmd_qps;
9269 	u8 pg_lvl = 1;
9270 	int i, rc;
9271 
9272 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9273 	if (rc) {
9274 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9275 			   rc);
9276 		return rc;
9277 	}
9278 	ctx = bp->ctx;
9279 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9280 		return 0;
9281 
9282 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9283 	l2_qps = ctxm->qp_l2_entries;
9284 	qp1_qps = ctxm->qp_qp1_entries;
9285 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9286 	max_qps = ctxm->max_entries;
9287 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9288 	srqs = ctxm->srq_l2_entries;
9289 	max_srqs = ctxm->max_entries;
9290 	ena = 0;
9291 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9292 		pg_lvl = 2;
9293 		if (BNXT_SW_RES_LMT(bp)) {
9294 			extra_qps = max_qps - l2_qps - qp1_qps;
9295 			extra_srqs = max_srqs - srqs;
9296 		} else {
9297 			extra_qps = min_t(u32, 65536,
9298 					  max_qps - l2_qps - qp1_qps);
9299 			/* allocate extra qps if fw supports RoCE fast qp
9300 			 * destroy feature
9301 			 */
9302 			extra_qps += fast_qpmd_qps;
9303 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9304 		}
9305 		if (fast_qpmd_qps)
9306 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9307 	}
9308 
9309 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9310 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9311 				     pg_lvl);
9312 	if (rc)
9313 		return rc;
9314 
9315 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9316 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9317 	if (rc)
9318 		return rc;
9319 
9320 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9321 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9322 				     extra_qps * 2, pg_lvl);
9323 	if (rc)
9324 		return rc;
9325 
9326 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9327 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9328 	if (rc)
9329 		return rc;
9330 
9331 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9332 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9333 	if (rc)
9334 		return rc;
9335 
9336 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9337 		goto skip_rdma;
9338 
9339 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9340 	if (BNXT_SW_RES_LMT(bp) &&
9341 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9342 		num_ah = ctxm->mrav_av_entries;
9343 		num_mr = ctxm->max_entries - num_ah;
9344 	} else {
9345 		/* 128K extra is needed to accommodate static AH context
9346 		 * allocation by f/w.
9347 		 */
9348 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9349 		num_ah = min_t(u32, num_mr, 1024 * 128);
9350 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9351 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9352 			ctxm->mrav_av_entries = num_ah;
9353 	}
9354 
9355 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9356 	if (rc)
9357 		return rc;
9358 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9359 
9360 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9361 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9362 	if (rc)
9363 		return rc;
9364 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9365 
9366 skip_rdma:
9367 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9368 	min = ctxm->min_entries;
9369 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9370 		     2 * (extra_qps + qp1_qps) + min;
9371 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9372 	if (rc)
9373 		return rc;
9374 
9375 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9376 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9377 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9378 	if (rc)
9379 		return rc;
9380 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9381 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9382 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9383 
9384 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9385 		rc = bnxt_backing_store_cfg_v2(bp, ena);
9386 	else
9387 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9388 	if (rc) {
9389 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9390 			   rc);
9391 		return rc;
9392 	}
9393 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9394 	return 0;
9395 }
9396 
9397 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9398 {
9399 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9400 	u16 page_attr;
9401 	int rc;
9402 
9403 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9404 		return 0;
9405 
9406 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9407 	if (rc)
9408 		return rc;
9409 
9410 	if (BNXT_PAGE_SIZE == 0x2000)
9411 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9412 	else if (BNXT_PAGE_SIZE == 0x10000)
9413 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9414 	else
9415 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9416 	req->pg_size_lvl = cpu_to_le16(page_attr |
9417 				       bp->fw_crash_mem->ring_mem.depth);
9418 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9419 	req->size = cpu_to_le32(bp->fw_crash_len);
9420 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9421 	return hwrm_req_send(bp, req);
9422 }
9423 
9424 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9425 {
9426 	if (bp->fw_crash_mem) {
9427 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9428 		kfree(bp->fw_crash_mem);
9429 		bp->fw_crash_mem = NULL;
9430 	}
9431 }
9432 
9433 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9434 {
9435 	u32 mem_size = 0;
9436 	int rc;
9437 
9438 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9439 		return 0;
9440 
9441 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9442 	if (rc)
9443 		return rc;
9444 
9445 	mem_size = round_up(mem_size, 4);
9446 
9447 	/* keep and use the existing pages */
9448 	if (bp->fw_crash_mem &&
9449 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9450 		goto alloc_done;
9451 
9452 	if (bp->fw_crash_mem)
9453 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9454 	else
9455 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
9456 					   GFP_KERNEL);
9457 	if (!bp->fw_crash_mem)
9458 		return -ENOMEM;
9459 
9460 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9461 	if (rc) {
9462 		bnxt_free_crash_dump_mem(bp);
9463 		return rc;
9464 	}
9465 
9466 alloc_done:
9467 	bp->fw_crash_len = mem_size;
9468 	return 0;
9469 }
9470 
9471 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9472 {
9473 	struct hwrm_func_resource_qcaps_output *resp;
9474 	struct hwrm_func_resource_qcaps_input *req;
9475 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9476 	int rc;
9477 
9478 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9479 	if (rc)
9480 		return rc;
9481 
9482 	req->fid = cpu_to_le16(0xffff);
9483 	resp = hwrm_req_hold(bp, req);
9484 	rc = hwrm_req_send_silent(bp, req);
9485 	if (rc)
9486 		goto hwrm_func_resc_qcaps_exit;
9487 
9488 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9489 	if (!all)
9490 		goto hwrm_func_resc_qcaps_exit;
9491 
9492 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9493 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9494 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9495 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9496 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9497 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9498 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9499 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9500 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9501 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9502 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9503 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9504 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9505 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9506 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9507 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9508 
9509 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9510 		u16 max_msix = le16_to_cpu(resp->max_msix);
9511 
9512 		hw_resc->max_nqs = max_msix;
9513 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9514 	}
9515 
9516 	if (BNXT_PF(bp)) {
9517 		struct bnxt_pf_info *pf = &bp->pf;
9518 
9519 		pf->vf_resv_strategy =
9520 			le16_to_cpu(resp->vf_reservation_strategy);
9521 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9522 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9523 	}
9524 hwrm_func_resc_qcaps_exit:
9525 	hwrm_req_drop(bp, req);
9526 	return rc;
9527 }
9528 
9529 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9530 {
9531 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9532 	struct hwrm_port_mac_ptp_qcfg_input *req;
9533 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9534 	u8 flags;
9535 	int rc;
9536 
9537 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9538 		rc = -ENODEV;
9539 		goto no_ptp;
9540 	}
9541 
9542 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9543 	if (rc)
9544 		goto no_ptp;
9545 
9546 	req->port_id = cpu_to_le16(bp->pf.port_id);
9547 	resp = hwrm_req_hold(bp, req);
9548 	rc = hwrm_req_send(bp, req);
9549 	if (rc)
9550 		goto exit;
9551 
9552 	flags = resp->flags;
9553 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9554 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9555 		rc = -ENODEV;
9556 		goto exit;
9557 	}
9558 	if (!ptp) {
9559 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9560 		if (!ptp) {
9561 			rc = -ENOMEM;
9562 			goto exit;
9563 		}
9564 		ptp->bp = bp;
9565 		bp->ptp_cfg = ptp;
9566 	}
9567 
9568 	if (flags &
9569 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9570 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9571 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9572 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9573 	} else if (BNXT_CHIP_P5(bp)) {
9574 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9575 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9576 	} else {
9577 		rc = -ENODEV;
9578 		goto exit;
9579 	}
9580 	ptp->rtc_configured =
9581 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9582 	rc = bnxt_ptp_init(bp);
9583 	if (rc)
9584 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9585 exit:
9586 	hwrm_req_drop(bp, req);
9587 	if (!rc)
9588 		return 0;
9589 
9590 no_ptp:
9591 	bnxt_ptp_clear(bp);
9592 	kfree(ptp);
9593 	bp->ptp_cfg = NULL;
9594 	return rc;
9595 }
9596 
9597 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9598 {
9599 	struct hwrm_func_qcaps_output *resp;
9600 	struct hwrm_func_qcaps_input *req;
9601 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9602 	u32 flags, flags_ext, flags_ext2;
9603 	int rc;
9604 
9605 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9606 	if (rc)
9607 		return rc;
9608 
9609 	req->fid = cpu_to_le16(0xffff);
9610 	resp = hwrm_req_hold(bp, req);
9611 	rc = hwrm_req_send(bp, req);
9612 	if (rc)
9613 		goto hwrm_func_qcaps_exit;
9614 
9615 	flags = le32_to_cpu(resp->flags);
9616 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9617 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9618 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9619 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9620 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9621 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9622 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9623 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9624 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9625 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9626 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9627 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9628 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9629 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9630 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9631 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9632 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9633 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9634 
9635 	flags_ext = le32_to_cpu(resp->flags_ext);
9636 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9637 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9638 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9639 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9640 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9641 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9642 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9643 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9644 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9645 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9646 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9647 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9648 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9649 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9650 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9651 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9652 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9653 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9654 
9655 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9656 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9657 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9658 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9659 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9660 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9661 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9662 	if (flags_ext2 &
9663 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9664 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9665 	if (BNXT_PF(bp) &&
9666 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9667 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9668 
9669 	bp->tx_push_thresh = 0;
9670 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9671 	    BNXT_FW_MAJ(bp) > 217)
9672 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9673 
9674 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9675 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9676 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9677 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9678 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9679 	if (!hw_resc->max_hw_ring_grps)
9680 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9681 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9682 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9683 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9684 
9685 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9686 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9687 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9688 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9689 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9690 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9691 
9692 	if (BNXT_PF(bp)) {
9693 		struct bnxt_pf_info *pf = &bp->pf;
9694 
9695 		pf->fw_fid = le16_to_cpu(resp->fid);
9696 		pf->port_id = le16_to_cpu(resp->port_id);
9697 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9698 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9699 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9700 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9701 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9702 			bp->flags |= BNXT_FLAG_WOL_CAP;
9703 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9704 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9705 		} else {
9706 			bnxt_ptp_clear(bp);
9707 			kfree(bp->ptp_cfg);
9708 			bp->ptp_cfg = NULL;
9709 		}
9710 	} else {
9711 #ifdef CONFIG_BNXT_SRIOV
9712 		struct bnxt_vf_info *vf = &bp->vf;
9713 
9714 		vf->fw_fid = le16_to_cpu(resp->fid);
9715 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9716 #endif
9717 	}
9718 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9719 
9720 hwrm_func_qcaps_exit:
9721 	hwrm_req_drop(bp, req);
9722 	return rc;
9723 }
9724 
9725 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9726 {
9727 	struct hwrm_dbg_qcaps_output *resp;
9728 	struct hwrm_dbg_qcaps_input *req;
9729 	int rc;
9730 
9731 	bp->fw_dbg_cap = 0;
9732 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9733 		return;
9734 
9735 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9736 	if (rc)
9737 		return;
9738 
9739 	req->fid = cpu_to_le16(0xffff);
9740 	resp = hwrm_req_hold(bp, req);
9741 	rc = hwrm_req_send(bp, req);
9742 	if (rc)
9743 		goto hwrm_dbg_qcaps_exit;
9744 
9745 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9746 
9747 hwrm_dbg_qcaps_exit:
9748 	hwrm_req_drop(bp, req);
9749 }
9750 
9751 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9752 
9753 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9754 {
9755 	int rc;
9756 
9757 	rc = __bnxt_hwrm_func_qcaps(bp);
9758 	if (rc)
9759 		return rc;
9760 
9761 	bnxt_hwrm_dbg_qcaps(bp);
9762 
9763 	rc = bnxt_hwrm_queue_qportcfg(bp);
9764 	if (rc) {
9765 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9766 		return rc;
9767 	}
9768 	if (bp->hwrm_spec_code >= 0x10803) {
9769 		rc = bnxt_alloc_ctx_mem(bp);
9770 		if (rc)
9771 			return rc;
9772 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9773 		if (!rc)
9774 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9775 	}
9776 	return 0;
9777 }
9778 
9779 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9780 {
9781 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9782 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9783 	u32 flags;
9784 	int rc;
9785 
9786 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9787 		return 0;
9788 
9789 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9790 	if (rc)
9791 		return rc;
9792 
9793 	resp = hwrm_req_hold(bp, req);
9794 	rc = hwrm_req_send(bp, req);
9795 	if (rc)
9796 		goto hwrm_cfa_adv_qcaps_exit;
9797 
9798 	flags = le32_to_cpu(resp->flags);
9799 	if (flags &
9800 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9801 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9802 
9803 	if (flags &
9804 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9805 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9806 
9807 	if (flags &
9808 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9809 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9810 
9811 hwrm_cfa_adv_qcaps_exit:
9812 	hwrm_req_drop(bp, req);
9813 	return rc;
9814 }
9815 
9816 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9817 {
9818 	if (bp->fw_health)
9819 		return 0;
9820 
9821 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9822 	if (!bp->fw_health)
9823 		return -ENOMEM;
9824 
9825 	mutex_init(&bp->fw_health->lock);
9826 	return 0;
9827 }
9828 
9829 static int bnxt_alloc_fw_health(struct bnxt *bp)
9830 {
9831 	int rc;
9832 
9833 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9834 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9835 		return 0;
9836 
9837 	rc = __bnxt_alloc_fw_health(bp);
9838 	if (rc) {
9839 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9840 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9841 		return rc;
9842 	}
9843 
9844 	return 0;
9845 }
9846 
9847 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9848 {
9849 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9850 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9851 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9852 }
9853 
9854 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9855 {
9856 	struct bnxt_fw_health *fw_health = bp->fw_health;
9857 	u32 reg_type;
9858 
9859 	if (!fw_health)
9860 		return;
9861 
9862 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9863 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9864 		fw_health->status_reliable = false;
9865 
9866 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9867 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9868 		fw_health->resets_reliable = false;
9869 }
9870 
9871 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9872 {
9873 	void __iomem *hs;
9874 	u32 status_loc;
9875 	u32 reg_type;
9876 	u32 sig;
9877 
9878 	if (bp->fw_health)
9879 		bp->fw_health->status_reliable = false;
9880 
9881 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9882 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9883 
9884 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9885 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9886 		if (!bp->chip_num) {
9887 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9888 			bp->chip_num = readl(bp->bar0 +
9889 					     BNXT_FW_HEALTH_WIN_BASE +
9890 					     BNXT_GRC_REG_CHIP_NUM);
9891 		}
9892 		if (!BNXT_CHIP_P5_PLUS(bp))
9893 			return;
9894 
9895 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9896 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9897 	} else {
9898 		status_loc = readl(hs + offsetof(struct hcomm_status,
9899 						 fw_status_loc));
9900 	}
9901 
9902 	if (__bnxt_alloc_fw_health(bp)) {
9903 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9904 		return;
9905 	}
9906 
9907 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9908 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9909 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9910 		__bnxt_map_fw_health_reg(bp, status_loc);
9911 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9912 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9913 	}
9914 
9915 	bp->fw_health->status_reliable = true;
9916 }
9917 
9918 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9919 {
9920 	struct bnxt_fw_health *fw_health = bp->fw_health;
9921 	u32 reg_base = 0xffffffff;
9922 	int i;
9923 
9924 	bp->fw_health->status_reliable = false;
9925 	bp->fw_health->resets_reliable = false;
9926 	/* Only pre-map the monitoring GRC registers using window 3 */
9927 	for (i = 0; i < 4; i++) {
9928 		u32 reg = fw_health->regs[i];
9929 
9930 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9931 			continue;
9932 		if (reg_base == 0xffffffff)
9933 			reg_base = reg & BNXT_GRC_BASE_MASK;
9934 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9935 			return -ERANGE;
9936 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9937 	}
9938 	bp->fw_health->status_reliable = true;
9939 	bp->fw_health->resets_reliable = true;
9940 	if (reg_base == 0xffffffff)
9941 		return 0;
9942 
9943 	__bnxt_map_fw_health_reg(bp, reg_base);
9944 	return 0;
9945 }
9946 
9947 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9948 {
9949 	if (!bp->fw_health)
9950 		return;
9951 
9952 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9953 		bp->fw_health->status_reliable = true;
9954 		bp->fw_health->resets_reliable = true;
9955 	} else {
9956 		bnxt_try_map_fw_health_reg(bp);
9957 	}
9958 }
9959 
9960 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9961 {
9962 	struct bnxt_fw_health *fw_health = bp->fw_health;
9963 	struct hwrm_error_recovery_qcfg_output *resp;
9964 	struct hwrm_error_recovery_qcfg_input *req;
9965 	int rc, i;
9966 
9967 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9968 		return 0;
9969 
9970 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9971 	if (rc)
9972 		return rc;
9973 
9974 	resp = hwrm_req_hold(bp, req);
9975 	rc = hwrm_req_send(bp, req);
9976 	if (rc)
9977 		goto err_recovery_out;
9978 	fw_health->flags = le32_to_cpu(resp->flags);
9979 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9980 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9981 		rc = -EINVAL;
9982 		goto err_recovery_out;
9983 	}
9984 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9985 	fw_health->master_func_wait_dsecs =
9986 		le32_to_cpu(resp->master_func_wait_period);
9987 	fw_health->normal_func_wait_dsecs =
9988 		le32_to_cpu(resp->normal_func_wait_period);
9989 	fw_health->post_reset_wait_dsecs =
9990 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9991 	fw_health->post_reset_max_wait_dsecs =
9992 		le32_to_cpu(resp->max_bailout_time_after_reset);
9993 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9994 		le32_to_cpu(resp->fw_health_status_reg);
9995 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9996 		le32_to_cpu(resp->fw_heartbeat_reg);
9997 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9998 		le32_to_cpu(resp->fw_reset_cnt_reg);
9999 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
10000 		le32_to_cpu(resp->reset_inprogress_reg);
10001 	fw_health->fw_reset_inprog_reg_mask =
10002 		le32_to_cpu(resp->reset_inprogress_reg_mask);
10003 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
10004 	if (fw_health->fw_reset_seq_cnt >= 16) {
10005 		rc = -EINVAL;
10006 		goto err_recovery_out;
10007 	}
10008 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
10009 		fw_health->fw_reset_seq_regs[i] =
10010 			le32_to_cpu(resp->reset_reg[i]);
10011 		fw_health->fw_reset_seq_vals[i] =
10012 			le32_to_cpu(resp->reset_reg_val[i]);
10013 		fw_health->fw_reset_seq_delay_msec[i] =
10014 			resp->delay_after_reset[i];
10015 	}
10016 err_recovery_out:
10017 	hwrm_req_drop(bp, req);
10018 	if (!rc)
10019 		rc = bnxt_map_fw_health_regs(bp);
10020 	if (rc)
10021 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10022 	return rc;
10023 }
10024 
10025 static int bnxt_hwrm_func_reset(struct bnxt *bp)
10026 {
10027 	struct hwrm_func_reset_input *req;
10028 	int rc;
10029 
10030 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
10031 	if (rc)
10032 		return rc;
10033 
10034 	req->enables = 0;
10035 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
10036 	return hwrm_req_send(bp, req);
10037 }
10038 
10039 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
10040 {
10041 	struct hwrm_nvm_get_dev_info_output nvm_info;
10042 
10043 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
10044 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10045 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10046 			 nvm_info.nvm_cfg_ver_upd);
10047 }
10048 
10049 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10050 {
10051 	struct hwrm_queue_qportcfg_output *resp;
10052 	struct hwrm_queue_qportcfg_input *req;
10053 	u8 i, j, *qptr;
10054 	bool no_rdma;
10055 	int rc = 0;
10056 
10057 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10058 	if (rc)
10059 		return rc;
10060 
10061 	resp = hwrm_req_hold(bp, req);
10062 	rc = hwrm_req_send(bp, req);
10063 	if (rc)
10064 		goto qportcfg_exit;
10065 
10066 	if (!resp->max_configurable_queues) {
10067 		rc = -EINVAL;
10068 		goto qportcfg_exit;
10069 	}
10070 	bp->max_tc = resp->max_configurable_queues;
10071 	bp->max_lltc = resp->max_configurable_lossless_queues;
10072 	if (bp->max_tc > BNXT_MAX_QUEUE)
10073 		bp->max_tc = BNXT_MAX_QUEUE;
10074 
10075 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10076 	qptr = &resp->queue_id0;
10077 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10078 		bp->q_info[j].queue_id = *qptr;
10079 		bp->q_ids[i] = *qptr++;
10080 		bp->q_info[j].queue_profile = *qptr++;
10081 		bp->tc_to_qidx[j] = j;
10082 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10083 		    (no_rdma && BNXT_PF(bp)))
10084 			j++;
10085 	}
10086 	bp->max_q = bp->max_tc;
10087 	bp->max_tc = max_t(u8, j, 1);
10088 
10089 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10090 		bp->max_tc = 1;
10091 
10092 	if (bp->max_lltc > bp->max_tc)
10093 		bp->max_lltc = bp->max_tc;
10094 
10095 qportcfg_exit:
10096 	hwrm_req_drop(bp, req);
10097 	return rc;
10098 }
10099 
10100 static int bnxt_hwrm_poll(struct bnxt *bp)
10101 {
10102 	struct hwrm_ver_get_input *req;
10103 	int rc;
10104 
10105 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10106 	if (rc)
10107 		return rc;
10108 
10109 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10110 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10111 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10112 
10113 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10114 	rc = hwrm_req_send(bp, req);
10115 	return rc;
10116 }
10117 
10118 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10119 {
10120 	struct hwrm_ver_get_output *resp;
10121 	struct hwrm_ver_get_input *req;
10122 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10123 	u32 dev_caps_cfg, hwrm_ver;
10124 	int rc, len, max_tmo_secs;
10125 
10126 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10127 	if (rc)
10128 		return rc;
10129 
10130 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10131 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10132 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10133 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10134 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10135 
10136 	resp = hwrm_req_hold(bp, req);
10137 	rc = hwrm_req_send(bp, req);
10138 	if (rc)
10139 		goto hwrm_ver_get_exit;
10140 
10141 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10142 
10143 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10144 			     resp->hwrm_intf_min_8b << 8 |
10145 			     resp->hwrm_intf_upd_8b;
10146 	if (resp->hwrm_intf_maj_8b < 1) {
10147 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10148 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10149 			    resp->hwrm_intf_upd_8b);
10150 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10151 	}
10152 
10153 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10154 			HWRM_VERSION_UPDATE;
10155 
10156 	if (bp->hwrm_spec_code > hwrm_ver)
10157 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10158 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10159 			 HWRM_VERSION_UPDATE);
10160 	else
10161 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10162 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10163 			 resp->hwrm_intf_upd_8b);
10164 
10165 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10166 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10167 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10168 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10169 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10170 		len = FW_VER_STR_LEN;
10171 	} else {
10172 		fw_maj = resp->hwrm_fw_maj_8b;
10173 		fw_min = resp->hwrm_fw_min_8b;
10174 		fw_bld = resp->hwrm_fw_bld_8b;
10175 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10176 		len = BC_HWRM_STR_LEN;
10177 	}
10178 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10179 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10180 		 fw_rsv);
10181 
10182 	if (strlen(resp->active_pkg_name)) {
10183 		int fw_ver_len = strlen(bp->fw_ver_str);
10184 
10185 		snprintf(bp->fw_ver_str + fw_ver_len,
10186 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10187 			 resp->active_pkg_name);
10188 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10189 	}
10190 
10191 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10192 	if (!bp->hwrm_cmd_timeout)
10193 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10194 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10195 	if (!bp->hwrm_cmd_max_timeout)
10196 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10197 	max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000;
10198 #ifdef CONFIG_DETECT_HUNG_TASK
10199 	if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT ||
10200 	    max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) {
10201 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n",
10202 			    max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT);
10203 	}
10204 #endif
10205 
10206 	if (resp->hwrm_intf_maj_8b >= 1) {
10207 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10208 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10209 	}
10210 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10211 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10212 
10213 	bp->chip_num = le16_to_cpu(resp->chip_num);
10214 	bp->chip_rev = resp->chip_rev;
10215 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10216 	    !resp->chip_metal)
10217 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10218 
10219 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10220 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10221 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10222 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10223 
10224 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10225 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10226 
10227 	if (dev_caps_cfg &
10228 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10229 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10230 
10231 	if (dev_caps_cfg &
10232 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10233 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10234 
10235 	if (dev_caps_cfg &
10236 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10237 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10238 
10239 hwrm_ver_get_exit:
10240 	hwrm_req_drop(bp, req);
10241 	return rc;
10242 }
10243 
10244 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10245 {
10246 	struct hwrm_fw_set_time_input *req;
10247 	struct tm tm;
10248 	time64_t now = ktime_get_real_seconds();
10249 	int rc;
10250 
10251 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10252 	    bp->hwrm_spec_code < 0x10400)
10253 		return -EOPNOTSUPP;
10254 
10255 	time64_to_tm(now, 0, &tm);
10256 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10257 	if (rc)
10258 		return rc;
10259 
10260 	req->year = cpu_to_le16(1900 + tm.tm_year);
10261 	req->month = 1 + tm.tm_mon;
10262 	req->day = tm.tm_mday;
10263 	req->hour = tm.tm_hour;
10264 	req->minute = tm.tm_min;
10265 	req->second = tm.tm_sec;
10266 	return hwrm_req_send(bp, req);
10267 }
10268 
10269 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10270 {
10271 	u64 sw_tmp;
10272 
10273 	hw &= mask;
10274 	sw_tmp = (*sw & ~mask) | hw;
10275 	if (hw < (*sw & mask))
10276 		sw_tmp += mask + 1;
10277 	WRITE_ONCE(*sw, sw_tmp);
10278 }
10279 
10280 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10281 				    int count, bool ignore_zero)
10282 {
10283 	int i;
10284 
10285 	for (i = 0; i < count; i++) {
10286 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10287 
10288 		if (ignore_zero && !hw)
10289 			continue;
10290 
10291 		if (masks[i] == -1ULL)
10292 			sw_stats[i] = hw;
10293 		else
10294 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10295 	}
10296 }
10297 
10298 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10299 {
10300 	if (!stats->hw_stats)
10301 		return;
10302 
10303 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10304 				stats->hw_masks, stats->len / 8, false);
10305 }
10306 
10307 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10308 {
10309 	struct bnxt_stats_mem *ring0_stats;
10310 	bool ignore_zero = false;
10311 	int i;
10312 
10313 	/* Chip bug.  Counter intermittently becomes 0. */
10314 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10315 		ignore_zero = true;
10316 
10317 	for (i = 0; i < bp->cp_nr_rings; i++) {
10318 		struct bnxt_napi *bnapi = bp->bnapi[i];
10319 		struct bnxt_cp_ring_info *cpr;
10320 		struct bnxt_stats_mem *stats;
10321 
10322 		cpr = &bnapi->cp_ring;
10323 		stats = &cpr->stats;
10324 		if (!i)
10325 			ring0_stats = stats;
10326 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10327 					ring0_stats->hw_masks,
10328 					ring0_stats->len / 8, ignore_zero);
10329 	}
10330 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10331 		struct bnxt_stats_mem *stats = &bp->port_stats;
10332 		__le64 *hw_stats = stats->hw_stats;
10333 		u64 *sw_stats = stats->sw_stats;
10334 		u64 *masks = stats->hw_masks;
10335 		int cnt;
10336 
10337 		cnt = sizeof(struct rx_port_stats) / 8;
10338 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10339 
10340 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10341 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10342 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10343 		cnt = sizeof(struct tx_port_stats) / 8;
10344 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10345 	}
10346 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10347 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10348 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10349 	}
10350 }
10351 
10352 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10353 {
10354 	struct hwrm_port_qstats_input *req;
10355 	struct bnxt_pf_info *pf = &bp->pf;
10356 	int rc;
10357 
10358 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10359 		return 0;
10360 
10361 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10362 		return -EOPNOTSUPP;
10363 
10364 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10365 	if (rc)
10366 		return rc;
10367 
10368 	req->flags = flags;
10369 	req->port_id = cpu_to_le16(pf->port_id);
10370 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10371 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10372 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10373 	return hwrm_req_send(bp, req);
10374 }
10375 
10376 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10377 {
10378 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10379 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10380 	struct hwrm_port_qstats_ext_output *resp_qs;
10381 	struct hwrm_port_qstats_ext_input *req_qs;
10382 	struct bnxt_pf_info *pf = &bp->pf;
10383 	u32 tx_stat_size;
10384 	int rc;
10385 
10386 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10387 		return 0;
10388 
10389 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10390 		return -EOPNOTSUPP;
10391 
10392 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10393 	if (rc)
10394 		return rc;
10395 
10396 	req_qs->flags = flags;
10397 	req_qs->port_id = cpu_to_le16(pf->port_id);
10398 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10399 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10400 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10401 		       sizeof(struct tx_port_stats_ext) : 0;
10402 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10403 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10404 	resp_qs = hwrm_req_hold(bp, req_qs);
10405 	rc = hwrm_req_send(bp, req_qs);
10406 	if (!rc) {
10407 		bp->fw_rx_stats_ext_size =
10408 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10409 		if (BNXT_FW_MAJ(bp) < 220 &&
10410 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10411 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10412 
10413 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10414 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10415 	} else {
10416 		bp->fw_rx_stats_ext_size = 0;
10417 		bp->fw_tx_stats_ext_size = 0;
10418 	}
10419 	hwrm_req_drop(bp, req_qs);
10420 
10421 	if (flags)
10422 		return rc;
10423 
10424 	if (bp->fw_tx_stats_ext_size <=
10425 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10426 		bp->pri2cos_valid = 0;
10427 		return rc;
10428 	}
10429 
10430 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10431 	if (rc)
10432 		return rc;
10433 
10434 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10435 
10436 	resp_qc = hwrm_req_hold(bp, req_qc);
10437 	rc = hwrm_req_send(bp, req_qc);
10438 	if (!rc) {
10439 		u8 *pri2cos;
10440 		int i, j;
10441 
10442 		pri2cos = &resp_qc->pri0_cos_queue_id;
10443 		for (i = 0; i < 8; i++) {
10444 			u8 queue_id = pri2cos[i];
10445 			u8 queue_idx;
10446 
10447 			/* Per port queue IDs start from 0, 10, 20, etc */
10448 			queue_idx = queue_id % 10;
10449 			if (queue_idx > BNXT_MAX_QUEUE) {
10450 				bp->pri2cos_valid = false;
10451 				hwrm_req_drop(bp, req_qc);
10452 				return rc;
10453 			}
10454 			for (j = 0; j < bp->max_q; j++) {
10455 				if (bp->q_ids[j] == queue_id)
10456 					bp->pri2cos_idx[i] = queue_idx;
10457 			}
10458 		}
10459 		bp->pri2cos_valid = true;
10460 	}
10461 	hwrm_req_drop(bp, req_qc);
10462 
10463 	return rc;
10464 }
10465 
10466 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10467 {
10468 	bnxt_hwrm_tunnel_dst_port_free(bp,
10469 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10470 	bnxt_hwrm_tunnel_dst_port_free(bp,
10471 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10472 }
10473 
10474 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10475 {
10476 	int rc, i;
10477 	u32 tpa_flags = 0;
10478 
10479 	if (set_tpa)
10480 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10481 	else if (BNXT_NO_FW_ACCESS(bp))
10482 		return 0;
10483 	for (i = 0; i < bp->nr_vnics; i++) {
10484 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10485 		if (rc) {
10486 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10487 				   i, rc);
10488 			return rc;
10489 		}
10490 	}
10491 	return 0;
10492 }
10493 
10494 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10495 {
10496 	int i;
10497 
10498 	for (i = 0; i < bp->nr_vnics; i++)
10499 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10500 }
10501 
10502 static void bnxt_clear_vnic(struct bnxt *bp)
10503 {
10504 	if (!bp->vnic_info)
10505 		return;
10506 
10507 	bnxt_hwrm_clear_vnic_filter(bp);
10508 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10509 		/* clear all RSS setting before free vnic ctx */
10510 		bnxt_hwrm_clear_vnic_rss(bp);
10511 		bnxt_hwrm_vnic_ctx_free(bp);
10512 	}
10513 	/* before free the vnic, undo the vnic tpa settings */
10514 	if (bp->flags & BNXT_FLAG_TPA)
10515 		bnxt_set_tpa(bp, false);
10516 	bnxt_hwrm_vnic_free(bp);
10517 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10518 		bnxt_hwrm_vnic_ctx_free(bp);
10519 }
10520 
10521 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10522 				    bool irq_re_init)
10523 {
10524 	bnxt_clear_vnic(bp);
10525 	bnxt_hwrm_ring_free(bp, close_path);
10526 	bnxt_hwrm_ring_grp_free(bp);
10527 	if (irq_re_init) {
10528 		bnxt_hwrm_stat_ctx_free(bp);
10529 		bnxt_hwrm_free_tunnel_ports(bp);
10530 	}
10531 }
10532 
10533 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10534 {
10535 	struct hwrm_func_cfg_input *req;
10536 	u8 evb_mode;
10537 	int rc;
10538 
10539 	if (br_mode == BRIDGE_MODE_VEB)
10540 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10541 	else if (br_mode == BRIDGE_MODE_VEPA)
10542 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10543 	else
10544 		return -EINVAL;
10545 
10546 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10547 	if (rc)
10548 		return rc;
10549 
10550 	req->fid = cpu_to_le16(0xffff);
10551 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10552 	req->evb_mode = evb_mode;
10553 	return hwrm_req_send(bp, req);
10554 }
10555 
10556 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10557 {
10558 	struct hwrm_func_cfg_input *req;
10559 	int rc;
10560 
10561 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10562 		return 0;
10563 
10564 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10565 	if (rc)
10566 		return rc;
10567 
10568 	req->fid = cpu_to_le16(0xffff);
10569 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10570 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10571 	if (size == 128)
10572 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10573 
10574 	return hwrm_req_send(bp, req);
10575 }
10576 
10577 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10578 {
10579 	int rc;
10580 
10581 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10582 		goto skip_rss_ctx;
10583 
10584 	/* allocate context for vnic */
10585 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10586 	if (rc) {
10587 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10588 			   vnic->vnic_id, rc);
10589 		goto vnic_setup_err;
10590 	}
10591 	bp->rsscos_nr_ctxs++;
10592 
10593 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10594 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10595 		if (rc) {
10596 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10597 				   vnic->vnic_id, rc);
10598 			goto vnic_setup_err;
10599 		}
10600 		bp->rsscos_nr_ctxs++;
10601 	}
10602 
10603 skip_rss_ctx:
10604 	/* configure default vnic, ring grp */
10605 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10606 	if (rc) {
10607 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10608 			   vnic->vnic_id, rc);
10609 		goto vnic_setup_err;
10610 	}
10611 
10612 	/* Enable RSS hashing on vnic */
10613 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10614 	if (rc) {
10615 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10616 			   vnic->vnic_id, rc);
10617 		goto vnic_setup_err;
10618 	}
10619 
10620 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10621 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10622 		if (rc) {
10623 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10624 				   vnic->vnic_id, rc);
10625 		}
10626 	}
10627 
10628 vnic_setup_err:
10629 	return rc;
10630 }
10631 
10632 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10633 			  u8 valid)
10634 {
10635 	struct hwrm_vnic_update_input *req;
10636 	int rc;
10637 
10638 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10639 	if (rc)
10640 		return rc;
10641 
10642 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10643 
10644 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10645 		req->mru = cpu_to_le16(vnic->mru);
10646 
10647 	req->enables = cpu_to_le32(valid);
10648 
10649 	return hwrm_req_send(bp, req);
10650 }
10651 
10652 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10653 {
10654 	int rc;
10655 
10656 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10657 	if (rc) {
10658 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10659 			   vnic->vnic_id, rc);
10660 		return rc;
10661 	}
10662 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10663 	if (rc)
10664 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10665 			   vnic->vnic_id, rc);
10666 	return rc;
10667 }
10668 
10669 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10670 {
10671 	int rc, i, nr_ctxs;
10672 
10673 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10674 	for (i = 0; i < nr_ctxs; i++) {
10675 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10676 		if (rc) {
10677 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10678 				   vnic->vnic_id, i, rc);
10679 			break;
10680 		}
10681 		bp->rsscos_nr_ctxs++;
10682 	}
10683 	if (i < nr_ctxs)
10684 		return -ENOMEM;
10685 
10686 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10687 	if (rc)
10688 		return rc;
10689 
10690 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10691 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10692 		if (rc) {
10693 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10694 				   vnic->vnic_id, rc);
10695 		}
10696 	}
10697 	return rc;
10698 }
10699 
10700 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10701 {
10702 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10703 		return __bnxt_setup_vnic_p5(bp, vnic);
10704 	else
10705 		return __bnxt_setup_vnic(bp, vnic);
10706 }
10707 
10708 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10709 				     struct bnxt_vnic_info *vnic,
10710 				     u16 start_rx_ring_idx, int rx_rings)
10711 {
10712 	int rc;
10713 
10714 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10715 	if (rc) {
10716 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10717 			   vnic->vnic_id, rc);
10718 		return rc;
10719 	}
10720 	return bnxt_setup_vnic(bp, vnic);
10721 }
10722 
10723 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10724 {
10725 	struct bnxt_vnic_info *vnic;
10726 	int i, rc = 0;
10727 
10728 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10729 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10730 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10731 	}
10732 
10733 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10734 		return 0;
10735 
10736 	for (i = 0; i < bp->rx_nr_rings; i++) {
10737 		u16 vnic_id = i + 1;
10738 		u16 ring_id = i;
10739 
10740 		if (vnic_id >= bp->nr_vnics)
10741 			break;
10742 
10743 		vnic = &bp->vnic_info[vnic_id];
10744 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10745 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10746 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10747 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10748 			break;
10749 	}
10750 	return rc;
10751 }
10752 
10753 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10754 			  bool all)
10755 {
10756 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10757 	struct bnxt_filter_base *usr_fltr, *tmp;
10758 	struct bnxt_ntuple_filter *ntp_fltr;
10759 	int i;
10760 
10761 	if (netif_running(bp->dev)) {
10762 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10763 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10764 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10765 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10766 		}
10767 	}
10768 	if (!all)
10769 		return;
10770 
10771 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10772 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10773 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10774 			ntp_fltr = container_of(usr_fltr,
10775 						struct bnxt_ntuple_filter,
10776 						base);
10777 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10778 			bnxt_del_ntp_filter(bp, ntp_fltr);
10779 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10780 		}
10781 	}
10782 
10783 	if (vnic->rss_table)
10784 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10785 				  vnic->rss_table,
10786 				  vnic->rss_table_dma_addr);
10787 	bp->num_rss_ctx--;
10788 }
10789 
10790 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10791 				  int rxr_id)
10792 {
10793 	u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
10794 	int i, vnic_rx;
10795 
10796 	/* Ntuple VNIC always has all the rx rings. Any change of ring id
10797 	 * must be updated because a future filter may use it.
10798 	 */
10799 	if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
10800 		return true;
10801 
10802 	for (i = 0; i < tbl_size; i++) {
10803 		if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
10804 			vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
10805 		else
10806 			vnic_rx = bp->rss_indir_tbl[i];
10807 
10808 		if (rxr_id == vnic_rx)
10809 			return true;
10810 	}
10811 
10812 	return false;
10813 }
10814 
10815 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10816 				u16 mru, int rxr_id)
10817 {
10818 	int rc;
10819 
10820 	if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id))
10821 		return 0;
10822 
10823 	if (mru) {
10824 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10825 		if (rc) {
10826 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10827 				   vnic->vnic_id, rc);
10828 			return rc;
10829 		}
10830 	}
10831 	vnic->mru = mru;
10832 	bnxt_hwrm_vnic_update(bp, vnic,
10833 			      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
10834 
10835 	return 0;
10836 }
10837 
10838 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id)
10839 {
10840 	struct ethtool_rxfh_context *ctx;
10841 	unsigned long context;
10842 	int rc;
10843 
10844 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10845 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10846 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10847 
10848 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id);
10849 		if (rc)
10850 			return rc;
10851 	}
10852 
10853 	return 0;
10854 }
10855 
10856 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10857 {
10858 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10859 	struct ethtool_rxfh_context *ctx;
10860 	unsigned long context;
10861 
10862 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10863 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10864 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10865 
10866 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10867 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10868 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10869 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10870 				   rss_ctx->index);
10871 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10872 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10873 		}
10874 	}
10875 }
10876 
10877 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10878 {
10879 	struct ethtool_rxfh_context *ctx;
10880 	unsigned long context;
10881 
10882 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10883 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10884 
10885 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10886 	}
10887 }
10888 
10889 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10890 static bool bnxt_promisc_ok(struct bnxt *bp)
10891 {
10892 #ifdef CONFIG_BNXT_SRIOV
10893 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10894 		return false;
10895 #endif
10896 	return true;
10897 }
10898 
10899 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10900 {
10901 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10902 	unsigned int rc = 0;
10903 
10904 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10905 	if (rc) {
10906 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10907 			   rc);
10908 		return rc;
10909 	}
10910 
10911 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10912 	if (rc) {
10913 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10914 			   rc);
10915 		return rc;
10916 	}
10917 	return rc;
10918 }
10919 
10920 static int bnxt_cfg_rx_mode(struct bnxt *);
10921 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10922 
10923 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10924 {
10925 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10926 	int rc = 0;
10927 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10928 
10929 	if (irq_re_init) {
10930 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10931 		if (rc) {
10932 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10933 				   rc);
10934 			goto err_out;
10935 		}
10936 	}
10937 
10938 	rc = bnxt_hwrm_ring_alloc(bp);
10939 	if (rc) {
10940 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10941 		goto err_out;
10942 	}
10943 
10944 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10945 	if (rc) {
10946 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10947 		goto err_out;
10948 	}
10949 
10950 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10951 		rx_nr_rings--;
10952 
10953 	/* default vnic 0 */
10954 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10955 	if (rc) {
10956 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10957 		goto err_out;
10958 	}
10959 
10960 	if (BNXT_VF(bp))
10961 		bnxt_hwrm_func_qcfg(bp);
10962 
10963 	rc = bnxt_setup_vnic(bp, vnic);
10964 	if (rc)
10965 		goto err_out;
10966 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10967 		bnxt_hwrm_update_rss_hash_cfg(bp);
10968 
10969 	if (bp->flags & BNXT_FLAG_RFS) {
10970 		rc = bnxt_alloc_rfs_vnics(bp);
10971 		if (rc)
10972 			goto err_out;
10973 	}
10974 
10975 	if (bp->flags & BNXT_FLAG_TPA) {
10976 		rc = bnxt_set_tpa(bp, true);
10977 		if (rc)
10978 			goto err_out;
10979 	}
10980 
10981 	if (BNXT_VF(bp))
10982 		bnxt_update_vf_mac(bp);
10983 
10984 	/* Filter for default vnic 0 */
10985 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10986 	if (rc) {
10987 		if (BNXT_VF(bp) && rc == -ENODEV)
10988 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10989 		else
10990 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10991 		goto err_out;
10992 	}
10993 	vnic->uc_filter_count = 1;
10994 
10995 	vnic->rx_mask = 0;
10996 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10997 		goto skip_rx_mask;
10998 
10999 	if (bp->dev->flags & IFF_BROADCAST)
11000 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11001 
11002 	if (bp->dev->flags & IFF_PROMISC)
11003 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11004 
11005 	if (bp->dev->flags & IFF_ALLMULTI) {
11006 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11007 		vnic->mc_list_count = 0;
11008 	} else if (bp->dev->flags & IFF_MULTICAST) {
11009 		u32 mask = 0;
11010 
11011 		bnxt_mc_list_updated(bp, &mask);
11012 		vnic->rx_mask |= mask;
11013 	}
11014 
11015 	rc = bnxt_cfg_rx_mode(bp);
11016 	if (rc)
11017 		goto err_out;
11018 
11019 skip_rx_mask:
11020 	rc = bnxt_hwrm_set_coal(bp);
11021 	if (rc)
11022 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
11023 				rc);
11024 
11025 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11026 		rc = bnxt_setup_nitroa0_vnic(bp);
11027 		if (rc)
11028 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
11029 				   rc);
11030 	}
11031 
11032 	if (BNXT_VF(bp)) {
11033 		bnxt_hwrm_func_qcfg(bp);
11034 		netdev_update_features(bp->dev);
11035 	}
11036 
11037 	return 0;
11038 
11039 err_out:
11040 	bnxt_hwrm_resource_free(bp, 0, true);
11041 
11042 	return rc;
11043 }
11044 
11045 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
11046 {
11047 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
11048 	return 0;
11049 }
11050 
11051 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
11052 {
11053 	bnxt_init_cp_rings(bp);
11054 	bnxt_init_rx_rings(bp);
11055 	bnxt_init_tx_rings(bp);
11056 	bnxt_init_ring_grps(bp, irq_re_init);
11057 	bnxt_init_vnics(bp);
11058 
11059 	return bnxt_init_chip(bp, irq_re_init);
11060 }
11061 
11062 static int bnxt_set_real_num_queues(struct bnxt *bp)
11063 {
11064 	int rc;
11065 	struct net_device *dev = bp->dev;
11066 
11067 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
11068 					  bp->tx_nr_rings_xdp);
11069 	if (rc)
11070 		return rc;
11071 
11072 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
11073 	if (rc)
11074 		return rc;
11075 
11076 #ifdef CONFIG_RFS_ACCEL
11077 	if (bp->flags & BNXT_FLAG_RFS)
11078 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
11079 #endif
11080 
11081 	return rc;
11082 }
11083 
11084 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11085 			     bool shared)
11086 {
11087 	int _rx = *rx, _tx = *tx;
11088 
11089 	if (shared) {
11090 		*rx = min_t(int, _rx, max);
11091 		*tx = min_t(int, _tx, max);
11092 	} else {
11093 		if (max < 2)
11094 			return -ENOMEM;
11095 
11096 		while (_rx + _tx > max) {
11097 			if (_rx > _tx && _rx > 1)
11098 				_rx--;
11099 			else if (_tx > 1)
11100 				_tx--;
11101 		}
11102 		*rx = _rx;
11103 		*tx = _tx;
11104 	}
11105 	return 0;
11106 }
11107 
11108 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
11109 {
11110 	return (tx - tx_xdp) / tx_sets + tx_xdp;
11111 }
11112 
11113 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
11114 {
11115 	int tcs = bp->num_tc;
11116 
11117 	if (!tcs)
11118 		tcs = 1;
11119 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11120 }
11121 
11122 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11123 {
11124 	int tcs = bp->num_tc;
11125 
11126 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11127 	       bp->tx_nr_rings_xdp;
11128 }
11129 
11130 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11131 			   bool sh)
11132 {
11133 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11134 
11135 	if (tx_cp != *tx) {
11136 		int tx_saved = tx_cp, rc;
11137 
11138 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11139 		if (rc)
11140 			return rc;
11141 		if (tx_cp != tx_saved)
11142 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11143 		return 0;
11144 	}
11145 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11146 }
11147 
11148 static void bnxt_setup_msix(struct bnxt *bp)
11149 {
11150 	const int len = sizeof(bp->irq_tbl[0].name);
11151 	struct net_device *dev = bp->dev;
11152 	int tcs, i;
11153 
11154 	tcs = bp->num_tc;
11155 	if (tcs) {
11156 		int i, off, count;
11157 
11158 		for (i = 0; i < tcs; i++) {
11159 			count = bp->tx_nr_rings_per_tc;
11160 			off = BNXT_TC_TO_RING_BASE(bp, i);
11161 			netdev_set_tc_queue(dev, i, count, off);
11162 		}
11163 	}
11164 
11165 	for (i = 0; i < bp->cp_nr_rings; i++) {
11166 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11167 		char *attr;
11168 
11169 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11170 			attr = "TxRx";
11171 		else if (i < bp->rx_nr_rings)
11172 			attr = "rx";
11173 		else
11174 			attr = "tx";
11175 
11176 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11177 			 attr, i);
11178 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11179 	}
11180 }
11181 
11182 static int bnxt_init_int_mode(struct bnxt *bp);
11183 
11184 static int bnxt_change_msix(struct bnxt *bp, int total)
11185 {
11186 	struct msi_map map;
11187 	int i;
11188 
11189 	/* add MSIX to the end if needed */
11190 	for (i = bp->total_irqs; i < total; i++) {
11191 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11192 		if (map.index < 0)
11193 			return bp->total_irqs;
11194 		bp->irq_tbl[i].vector = map.virq;
11195 		bp->total_irqs++;
11196 	}
11197 
11198 	/* trim MSIX from the end if needed */
11199 	for (i = bp->total_irqs; i > total; i--) {
11200 		map.index = i - 1;
11201 		map.virq = bp->irq_tbl[i - 1].vector;
11202 		pci_msix_free_irq(bp->pdev, map);
11203 		bp->total_irqs--;
11204 	}
11205 	return bp->total_irqs;
11206 }
11207 
11208 static int bnxt_setup_int_mode(struct bnxt *bp)
11209 {
11210 	int rc;
11211 
11212 	if (!bp->irq_tbl) {
11213 		rc = bnxt_init_int_mode(bp);
11214 		if (rc || !bp->irq_tbl)
11215 			return rc ?: -ENODEV;
11216 	}
11217 
11218 	bnxt_setup_msix(bp);
11219 
11220 	rc = bnxt_set_real_num_queues(bp);
11221 	return rc;
11222 }
11223 
11224 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11225 {
11226 	return bp->hw_resc.max_rsscos_ctxs;
11227 }
11228 
11229 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11230 {
11231 	return bp->hw_resc.max_vnics;
11232 }
11233 
11234 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11235 {
11236 	return bp->hw_resc.max_stat_ctxs;
11237 }
11238 
11239 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11240 {
11241 	return bp->hw_resc.max_cp_rings;
11242 }
11243 
11244 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11245 {
11246 	unsigned int cp = bp->hw_resc.max_cp_rings;
11247 
11248 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11249 		cp -= bnxt_get_ulp_msix_num(bp);
11250 
11251 	return cp;
11252 }
11253 
11254 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11255 {
11256 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11257 
11258 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11259 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11260 
11261 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11262 }
11263 
11264 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11265 {
11266 	bp->hw_resc.max_irqs = max_irqs;
11267 }
11268 
11269 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11270 {
11271 	unsigned int cp;
11272 
11273 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11274 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11275 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11276 	else
11277 		return cp - bp->cp_nr_rings;
11278 }
11279 
11280 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11281 {
11282 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11283 }
11284 
11285 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11286 {
11287 	int max_irq = bnxt_get_max_func_irqs(bp);
11288 	int total_req = bp->cp_nr_rings + num;
11289 
11290 	if (max_irq < total_req) {
11291 		num = max_irq - bp->cp_nr_rings;
11292 		if (num <= 0)
11293 			return 0;
11294 	}
11295 	return num;
11296 }
11297 
11298 static int bnxt_get_num_msix(struct bnxt *bp)
11299 {
11300 	if (!BNXT_NEW_RM(bp))
11301 		return bnxt_get_max_func_irqs(bp);
11302 
11303 	return bnxt_nq_rings_in_use(bp);
11304 }
11305 
11306 static int bnxt_init_int_mode(struct bnxt *bp)
11307 {
11308 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11309 
11310 	total_vecs = bnxt_get_num_msix(bp);
11311 	max = bnxt_get_max_func_irqs(bp);
11312 	if (total_vecs > max)
11313 		total_vecs = max;
11314 
11315 	if (!total_vecs)
11316 		return 0;
11317 
11318 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11319 		min = 2;
11320 
11321 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11322 					   PCI_IRQ_MSIX);
11323 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11324 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11325 		rc = -ENODEV;
11326 		goto msix_setup_exit;
11327 	}
11328 
11329 	tbl_size = total_vecs;
11330 	if (pci_msix_can_alloc_dyn(bp->pdev))
11331 		tbl_size = max;
11332 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
11333 	if (bp->irq_tbl) {
11334 		for (i = 0; i < total_vecs; i++)
11335 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11336 
11337 		bp->total_irqs = total_vecs;
11338 		/* Trim rings based upon num of vectors allocated */
11339 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11340 				     total_vecs - ulp_msix, min == 1);
11341 		if (rc)
11342 			goto msix_setup_exit;
11343 
11344 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11345 		bp->cp_nr_rings = (min == 1) ?
11346 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11347 				  tx_cp + bp->rx_nr_rings;
11348 
11349 	} else {
11350 		rc = -ENOMEM;
11351 		goto msix_setup_exit;
11352 	}
11353 	return 0;
11354 
11355 msix_setup_exit:
11356 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11357 	kfree(bp->irq_tbl);
11358 	bp->irq_tbl = NULL;
11359 	pci_free_irq_vectors(bp->pdev);
11360 	return rc;
11361 }
11362 
11363 static void bnxt_clear_int_mode(struct bnxt *bp)
11364 {
11365 	pci_free_irq_vectors(bp->pdev);
11366 
11367 	kfree(bp->irq_tbl);
11368 	bp->irq_tbl = NULL;
11369 }
11370 
11371 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11372 {
11373 	bool irq_cleared = false;
11374 	bool irq_change = false;
11375 	int tcs = bp->num_tc;
11376 	int irqs_required;
11377 	int rc;
11378 
11379 	if (!bnxt_need_reserve_rings(bp))
11380 		return 0;
11381 
11382 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11383 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11384 
11385 		if (ulp_msix > bp->ulp_num_msix_want)
11386 			ulp_msix = bp->ulp_num_msix_want;
11387 		irqs_required = ulp_msix + bp->cp_nr_rings;
11388 	} else {
11389 		irqs_required = bnxt_get_num_msix(bp);
11390 	}
11391 
11392 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11393 		irq_change = true;
11394 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11395 			bnxt_ulp_irq_stop(bp);
11396 			bnxt_clear_int_mode(bp);
11397 			irq_cleared = true;
11398 		}
11399 	}
11400 	rc = __bnxt_reserve_rings(bp);
11401 	if (irq_cleared) {
11402 		if (!rc)
11403 			rc = bnxt_init_int_mode(bp);
11404 		bnxt_ulp_irq_restart(bp, rc);
11405 	} else if (irq_change && !rc) {
11406 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11407 			rc = -ENOSPC;
11408 	}
11409 	if (rc) {
11410 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11411 		return rc;
11412 	}
11413 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11414 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11415 		netdev_err(bp->dev, "tx ring reservation failure\n");
11416 		netdev_reset_tc(bp->dev);
11417 		bp->num_tc = 0;
11418 		if (bp->tx_nr_rings_xdp)
11419 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11420 		else
11421 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11422 		return -ENOMEM;
11423 	}
11424 	return 0;
11425 }
11426 
11427 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11428 {
11429 	struct bnxt_tx_ring_info *txr;
11430 	struct netdev_queue *txq;
11431 	struct bnxt_napi *bnapi;
11432 	int i;
11433 
11434 	bnapi = bp->bnapi[idx];
11435 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11436 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11437 		synchronize_net();
11438 
11439 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11440 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11441 			if (txq) {
11442 				__netif_tx_lock_bh(txq);
11443 				netif_tx_stop_queue(txq);
11444 				__netif_tx_unlock_bh(txq);
11445 			}
11446 		}
11447 
11448 		if (!bp->tph_mode)
11449 			continue;
11450 
11451 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11452 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11453 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11454 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11455 	}
11456 }
11457 
11458 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11459 {
11460 	struct bnxt_tx_ring_info *txr;
11461 	struct netdev_queue *txq;
11462 	struct bnxt_napi *bnapi;
11463 	int rc, i;
11464 
11465 	bnapi = bp->bnapi[idx];
11466 	/* All rings have been reserved and previously allocated.
11467 	 * Reallocating with the same parameters should never fail.
11468 	 */
11469 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11470 		if (!bp->tph_mode)
11471 			goto start_tx;
11472 
11473 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11474 		if (rc)
11475 			return rc;
11476 
11477 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11478 		if (rc)
11479 			return rc;
11480 
11481 		txr->tx_prod = 0;
11482 		txr->tx_cons = 0;
11483 		txr->tx_hw_cons = 0;
11484 start_tx:
11485 		WRITE_ONCE(txr->dev_state, 0);
11486 		synchronize_net();
11487 
11488 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11489 			continue;
11490 
11491 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11492 		if (txq)
11493 			netif_tx_start_queue(txq);
11494 	}
11495 
11496 	return 0;
11497 }
11498 
11499 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11500 				     const cpumask_t *mask)
11501 {
11502 	struct bnxt_irq *irq;
11503 	u16 tag;
11504 	int err;
11505 
11506 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11507 
11508 	if (!irq->bp->tph_mode)
11509 		return;
11510 
11511 	cpumask_copy(irq->cpu_mask, mask);
11512 
11513 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11514 		return;
11515 
11516 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11517 				cpumask_first(irq->cpu_mask), &tag))
11518 		return;
11519 
11520 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag))
11521 		return;
11522 
11523 	netdev_lock(irq->bp->dev);
11524 	if (netif_running(irq->bp->dev)) {
11525 		err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr);
11526 		if (err)
11527 			netdev_err(irq->bp->dev,
11528 				   "RX queue restart failed: err=%d\n", err);
11529 	}
11530 	netdev_unlock(irq->bp->dev);
11531 }
11532 
11533 static void bnxt_irq_affinity_release(struct kref *ref)
11534 {
11535 	struct irq_affinity_notify *notify =
11536 		container_of(ref, struct irq_affinity_notify, kref);
11537 	struct bnxt_irq *irq;
11538 
11539 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11540 
11541 	if (!irq->bp->tph_mode)
11542 		return;
11543 
11544 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11545 		netdev_err(irq->bp->dev,
11546 			   "Setting ST=0 for MSIX entry %d failed\n",
11547 			   irq->msix_nr);
11548 		return;
11549 	}
11550 }
11551 
11552 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11553 {
11554 	irq_set_affinity_notifier(irq->vector, NULL);
11555 }
11556 
11557 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11558 {
11559 	struct irq_affinity_notify *notify;
11560 
11561 	irq->bp = bp;
11562 
11563 	/* Nothing to do if TPH is not enabled */
11564 	if (!bp->tph_mode)
11565 		return;
11566 
11567 	/* Register IRQ affinity notifier */
11568 	notify = &irq->affinity_notify;
11569 	notify->irq = irq->vector;
11570 	notify->notify = bnxt_irq_affinity_notify;
11571 	notify->release = bnxt_irq_affinity_release;
11572 
11573 	irq_set_affinity_notifier(irq->vector, notify);
11574 }
11575 
11576 static void bnxt_free_irq(struct bnxt *bp)
11577 {
11578 	struct bnxt_irq *irq;
11579 	int i;
11580 
11581 #ifdef CONFIG_RFS_ACCEL
11582 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11583 	bp->dev->rx_cpu_rmap = NULL;
11584 #endif
11585 	if (!bp->irq_tbl || !bp->bnapi)
11586 		return;
11587 
11588 	for (i = 0; i < bp->cp_nr_rings; i++) {
11589 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11590 
11591 		irq = &bp->irq_tbl[map_idx];
11592 		if (irq->requested) {
11593 			if (irq->have_cpumask) {
11594 				irq_update_affinity_hint(irq->vector, NULL);
11595 				free_cpumask_var(irq->cpu_mask);
11596 				irq->have_cpumask = 0;
11597 			}
11598 
11599 			bnxt_release_irq_notifier(irq);
11600 
11601 			free_irq(irq->vector, bp->bnapi[i]);
11602 		}
11603 
11604 		irq->requested = 0;
11605 	}
11606 
11607 	/* Disable TPH support */
11608 	pcie_disable_tph(bp->pdev);
11609 	bp->tph_mode = 0;
11610 }
11611 
11612 static int bnxt_request_irq(struct bnxt *bp)
11613 {
11614 	int i, j, rc = 0;
11615 	unsigned long flags = 0;
11616 #ifdef CONFIG_RFS_ACCEL
11617 	struct cpu_rmap *rmap;
11618 #endif
11619 
11620 	rc = bnxt_setup_int_mode(bp);
11621 	if (rc) {
11622 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11623 			   rc);
11624 		return rc;
11625 	}
11626 #ifdef CONFIG_RFS_ACCEL
11627 	rmap = bp->dev->rx_cpu_rmap;
11628 #endif
11629 
11630 	/* Enable TPH support as part of IRQ request */
11631 	rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
11632 	if (!rc)
11633 		bp->tph_mode = PCI_TPH_ST_IV_MODE;
11634 
11635 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11636 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11637 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11638 
11639 #ifdef CONFIG_RFS_ACCEL
11640 		if (rmap && bp->bnapi[i]->rx_ring) {
11641 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11642 			if (rc)
11643 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11644 					    j);
11645 			j++;
11646 		}
11647 #endif
11648 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11649 				 bp->bnapi[i]);
11650 		if (rc)
11651 			break;
11652 
11653 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
11654 		irq->requested = 1;
11655 
11656 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11657 			int numa_node = dev_to_node(&bp->pdev->dev);
11658 			u16 tag;
11659 
11660 			irq->have_cpumask = 1;
11661 			irq->msix_nr = map_idx;
11662 			irq->ring_nr = i;
11663 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11664 					irq->cpu_mask);
11665 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11666 			if (rc) {
11667 				netdev_warn(bp->dev,
11668 					    "Update affinity hint failed, IRQ = %d\n",
11669 					    irq->vector);
11670 				break;
11671 			}
11672 
11673 			bnxt_register_irq_notifier(bp, irq);
11674 
11675 			/* Init ST table entry */
11676 			if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11677 						cpumask_first(irq->cpu_mask),
11678 						&tag))
11679 				continue;
11680 
11681 			pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag);
11682 		}
11683 	}
11684 	return rc;
11685 }
11686 
11687 static void bnxt_del_napi(struct bnxt *bp)
11688 {
11689 	int i;
11690 
11691 	if (!bp->bnapi)
11692 		return;
11693 
11694 	for (i = 0; i < bp->rx_nr_rings; i++)
11695 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11696 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11697 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11698 
11699 	for (i = 0; i < bp->cp_nr_rings; i++) {
11700 		struct bnxt_napi *bnapi = bp->bnapi[i];
11701 
11702 		__netif_napi_del_locked(&bnapi->napi);
11703 	}
11704 	/* We called __netif_napi_del_locked(), we need
11705 	 * to respect an RCU grace period before freeing napi structures.
11706 	 */
11707 	synchronize_net();
11708 }
11709 
11710 static void bnxt_init_napi(struct bnxt *bp)
11711 {
11712 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11713 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11714 	struct bnxt_napi *bnapi;
11715 	int i;
11716 
11717 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11718 		poll_fn = bnxt_poll_p5;
11719 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11720 		cp_nr_rings--;
11721 
11722 	set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11723 
11724 	for (i = 0; i < cp_nr_rings; i++) {
11725 		bnapi = bp->bnapi[i];
11726 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
11727 					     bnapi->index);
11728 	}
11729 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11730 		bnapi = bp->bnapi[cp_nr_rings];
11731 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11732 	}
11733 }
11734 
11735 static void bnxt_disable_napi(struct bnxt *bp)
11736 {
11737 	int i;
11738 
11739 	if (!bp->bnapi ||
11740 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11741 		return;
11742 
11743 	for (i = 0; i < bp->cp_nr_rings; i++) {
11744 		struct bnxt_napi *bnapi = bp->bnapi[i];
11745 		struct bnxt_cp_ring_info *cpr;
11746 
11747 		cpr = &bnapi->cp_ring;
11748 		if (bnapi->tx_fault)
11749 			cpr->sw_stats->tx.tx_resets++;
11750 		if (bnapi->in_reset)
11751 			cpr->sw_stats->rx.rx_resets++;
11752 		napi_disable_locked(&bnapi->napi);
11753 	}
11754 }
11755 
11756 static void bnxt_enable_napi(struct bnxt *bp)
11757 {
11758 	int i;
11759 
11760 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11761 	for (i = 0; i < bp->cp_nr_rings; i++) {
11762 		struct bnxt_napi *bnapi = bp->bnapi[i];
11763 		struct bnxt_cp_ring_info *cpr;
11764 
11765 		bnapi->tx_fault = 0;
11766 
11767 		cpr = &bnapi->cp_ring;
11768 		bnapi->in_reset = false;
11769 
11770 		if (bnapi->rx_ring) {
11771 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11772 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11773 		}
11774 		napi_enable_locked(&bnapi->napi);
11775 	}
11776 }
11777 
11778 void bnxt_tx_disable(struct bnxt *bp)
11779 {
11780 	int i;
11781 	struct bnxt_tx_ring_info *txr;
11782 
11783 	if (bp->tx_ring) {
11784 		for (i = 0; i < bp->tx_nr_rings; i++) {
11785 			txr = &bp->tx_ring[i];
11786 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11787 		}
11788 	}
11789 	/* Make sure napi polls see @dev_state change */
11790 	synchronize_net();
11791 	/* Drop carrier first to prevent TX timeout */
11792 	netif_carrier_off(bp->dev);
11793 	/* Stop all TX queues */
11794 	netif_tx_disable(bp->dev);
11795 }
11796 
11797 void bnxt_tx_enable(struct bnxt *bp)
11798 {
11799 	int i;
11800 	struct bnxt_tx_ring_info *txr;
11801 
11802 	for (i = 0; i < bp->tx_nr_rings; i++) {
11803 		txr = &bp->tx_ring[i];
11804 		WRITE_ONCE(txr->dev_state, 0);
11805 	}
11806 	/* Make sure napi polls see @dev_state change */
11807 	synchronize_net();
11808 	netif_tx_wake_all_queues(bp->dev);
11809 	if (BNXT_LINK_IS_UP(bp))
11810 		netif_carrier_on(bp->dev);
11811 }
11812 
11813 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11814 {
11815 	u8 active_fec = link_info->active_fec_sig_mode &
11816 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11817 
11818 	switch (active_fec) {
11819 	default:
11820 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11821 		return "None";
11822 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11823 		return "Clause 74 BaseR";
11824 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11825 		return "Clause 91 RS(528,514)";
11826 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11827 		return "Clause 91 RS544_1XN";
11828 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11829 		return "Clause 91 RS(544,514)";
11830 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11831 		return "Clause 91 RS272_1XN";
11832 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11833 		return "Clause 91 RS(272,257)";
11834 	}
11835 }
11836 
11837 void bnxt_report_link(struct bnxt *bp)
11838 {
11839 	if (BNXT_LINK_IS_UP(bp)) {
11840 		const char *signal = "";
11841 		const char *flow_ctrl;
11842 		const char *duplex;
11843 		u32 speed;
11844 		u16 fec;
11845 
11846 		netif_carrier_on(bp->dev);
11847 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11848 		if (speed == SPEED_UNKNOWN) {
11849 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11850 			return;
11851 		}
11852 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11853 			duplex = "full";
11854 		else
11855 			duplex = "half";
11856 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11857 			flow_ctrl = "ON - receive & transmit";
11858 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11859 			flow_ctrl = "ON - transmit";
11860 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11861 			flow_ctrl = "ON - receive";
11862 		else
11863 			flow_ctrl = "none";
11864 		if (bp->link_info.phy_qcfg_resp.option_flags &
11865 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11866 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11867 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11868 			switch (sig_mode) {
11869 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11870 				signal = "(NRZ) ";
11871 				break;
11872 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11873 				signal = "(PAM4 56Gbps) ";
11874 				break;
11875 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11876 				signal = "(PAM4 112Gbps) ";
11877 				break;
11878 			default:
11879 				break;
11880 			}
11881 		}
11882 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11883 			    speed, signal, duplex, flow_ctrl);
11884 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11885 			netdev_info(bp->dev, "EEE is %s\n",
11886 				    bp->eee.eee_active ? "active" :
11887 							 "not active");
11888 		fec = bp->link_info.fec_cfg;
11889 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11890 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11891 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11892 				    bnxt_report_fec(&bp->link_info));
11893 	} else {
11894 		netif_carrier_off(bp->dev);
11895 		netdev_err(bp->dev, "NIC Link is Down\n");
11896 	}
11897 }
11898 
11899 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11900 {
11901 	if (!resp->supported_speeds_auto_mode &&
11902 	    !resp->supported_speeds_force_mode &&
11903 	    !resp->supported_pam4_speeds_auto_mode &&
11904 	    !resp->supported_pam4_speeds_force_mode &&
11905 	    !resp->supported_speeds2_auto_mode &&
11906 	    !resp->supported_speeds2_force_mode)
11907 		return true;
11908 	return false;
11909 }
11910 
11911 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11912 {
11913 	struct bnxt_link_info *link_info = &bp->link_info;
11914 	struct hwrm_port_phy_qcaps_output *resp;
11915 	struct hwrm_port_phy_qcaps_input *req;
11916 	int rc = 0;
11917 
11918 	if (bp->hwrm_spec_code < 0x10201)
11919 		return 0;
11920 
11921 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11922 	if (rc)
11923 		return rc;
11924 
11925 	resp = hwrm_req_hold(bp, req);
11926 	rc = hwrm_req_send(bp, req);
11927 	if (rc)
11928 		goto hwrm_phy_qcaps_exit;
11929 
11930 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11931 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11932 		struct ethtool_keee *eee = &bp->eee;
11933 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11934 
11935 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11936 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11937 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11938 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11939 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11940 	}
11941 
11942 	if (bp->hwrm_spec_code >= 0x10a01) {
11943 		if (bnxt_phy_qcaps_no_speed(resp)) {
11944 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11945 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11946 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11947 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11948 			netdev_info(bp->dev, "Ethernet link enabled\n");
11949 			/* Phy re-enabled, reprobe the speeds */
11950 			link_info->support_auto_speeds = 0;
11951 			link_info->support_pam4_auto_speeds = 0;
11952 			link_info->support_auto_speeds2 = 0;
11953 		}
11954 	}
11955 	if (resp->supported_speeds_auto_mode)
11956 		link_info->support_auto_speeds =
11957 			le16_to_cpu(resp->supported_speeds_auto_mode);
11958 	if (resp->supported_pam4_speeds_auto_mode)
11959 		link_info->support_pam4_auto_speeds =
11960 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11961 	if (resp->supported_speeds2_auto_mode)
11962 		link_info->support_auto_speeds2 =
11963 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11964 
11965 	bp->port_count = resp->port_cnt;
11966 
11967 hwrm_phy_qcaps_exit:
11968 	hwrm_req_drop(bp, req);
11969 	return rc;
11970 }
11971 
11972 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
11973 {
11974 	struct hwrm_port_mac_qcaps_output *resp;
11975 	struct hwrm_port_mac_qcaps_input *req;
11976 	int rc;
11977 
11978 	if (bp->hwrm_spec_code < 0x10a03)
11979 		return;
11980 
11981 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
11982 	if (rc)
11983 		return;
11984 
11985 	resp = hwrm_req_hold(bp, req);
11986 	rc = hwrm_req_send_silent(bp, req);
11987 	if (!rc)
11988 		bp->mac_flags = resp->flags;
11989 	hwrm_req_drop(bp, req);
11990 }
11991 
11992 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11993 {
11994 	u16 diff = advertising ^ supported;
11995 
11996 	return ((supported | diff) != supported);
11997 }
11998 
11999 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
12000 {
12001 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
12002 
12003 	/* Check if any advertised speeds are no longer supported. The caller
12004 	 * holds the link_lock mutex, so we can modify link_info settings.
12005 	 */
12006 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12007 		if (bnxt_support_dropped(link_info->advertising,
12008 					 link_info->support_auto_speeds2)) {
12009 			link_info->advertising = link_info->support_auto_speeds2;
12010 			return true;
12011 		}
12012 		return false;
12013 	}
12014 	if (bnxt_support_dropped(link_info->advertising,
12015 				 link_info->support_auto_speeds)) {
12016 		link_info->advertising = link_info->support_auto_speeds;
12017 		return true;
12018 	}
12019 	if (bnxt_support_dropped(link_info->advertising_pam4,
12020 				 link_info->support_pam4_auto_speeds)) {
12021 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
12022 		return true;
12023 	}
12024 	return false;
12025 }
12026 
12027 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
12028 {
12029 	struct bnxt_link_info *link_info = &bp->link_info;
12030 	struct hwrm_port_phy_qcfg_output *resp;
12031 	struct hwrm_port_phy_qcfg_input *req;
12032 	u8 link_state = link_info->link_state;
12033 	bool support_changed;
12034 	int rc;
12035 
12036 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
12037 	if (rc)
12038 		return rc;
12039 
12040 	resp = hwrm_req_hold(bp, req);
12041 	rc = hwrm_req_send(bp, req);
12042 	if (rc) {
12043 		hwrm_req_drop(bp, req);
12044 		if (BNXT_VF(bp) && rc == -ENODEV) {
12045 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
12046 			rc = 0;
12047 		}
12048 		return rc;
12049 	}
12050 
12051 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
12052 	link_info->phy_link_status = resp->link;
12053 	link_info->duplex = resp->duplex_cfg;
12054 	if (bp->hwrm_spec_code >= 0x10800)
12055 		link_info->duplex = resp->duplex_state;
12056 	link_info->pause = resp->pause;
12057 	link_info->auto_mode = resp->auto_mode;
12058 	link_info->auto_pause_setting = resp->auto_pause;
12059 	link_info->lp_pause = resp->link_partner_adv_pause;
12060 	link_info->force_pause_setting = resp->force_pause;
12061 	link_info->duplex_setting = resp->duplex_cfg;
12062 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
12063 		link_info->link_speed = le16_to_cpu(resp->link_speed);
12064 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
12065 			link_info->active_lanes = resp->active_lanes;
12066 	} else {
12067 		link_info->link_speed = 0;
12068 		link_info->active_lanes = 0;
12069 	}
12070 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
12071 	link_info->force_pam4_link_speed =
12072 		le16_to_cpu(resp->force_pam4_link_speed);
12073 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
12074 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
12075 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
12076 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
12077 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
12078 	link_info->auto_pam4_link_speeds =
12079 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
12080 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
12081 	link_info->lp_auto_link_speeds =
12082 		le16_to_cpu(resp->link_partner_adv_speeds);
12083 	link_info->lp_auto_pam4_link_speeds =
12084 		resp->link_partner_pam4_adv_speeds;
12085 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
12086 	link_info->phy_ver[0] = resp->phy_maj;
12087 	link_info->phy_ver[1] = resp->phy_min;
12088 	link_info->phy_ver[2] = resp->phy_bld;
12089 	link_info->media_type = resp->media_type;
12090 	link_info->phy_type = resp->phy_type;
12091 	link_info->transceiver = resp->xcvr_pkg_type;
12092 	link_info->phy_addr = resp->eee_config_phy_addr &
12093 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
12094 	link_info->module_status = resp->module_status;
12095 
12096 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
12097 		struct ethtool_keee *eee = &bp->eee;
12098 		u16 fw_speeds;
12099 
12100 		eee->eee_active = 0;
12101 		if (resp->eee_config_phy_addr &
12102 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
12103 			eee->eee_active = 1;
12104 			fw_speeds = le16_to_cpu(
12105 				resp->link_partner_adv_eee_link_speed_mask);
12106 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
12107 		}
12108 
12109 		/* Pull initial EEE config */
12110 		if (!chng_link_state) {
12111 			if (resp->eee_config_phy_addr &
12112 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
12113 				eee->eee_enabled = 1;
12114 
12115 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
12116 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
12117 
12118 			if (resp->eee_config_phy_addr &
12119 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12120 				__le32 tmr;
12121 
12122 				eee->tx_lpi_enabled = 1;
12123 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12124 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12125 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12126 			}
12127 		}
12128 	}
12129 
12130 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12131 	if (bp->hwrm_spec_code >= 0x10504) {
12132 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12133 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12134 	}
12135 	/* TODO: need to add more logic to report VF link */
12136 	if (chng_link_state) {
12137 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12138 			link_info->link_state = BNXT_LINK_STATE_UP;
12139 		else
12140 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12141 		if (link_state != link_info->link_state)
12142 			bnxt_report_link(bp);
12143 	} else {
12144 		/* always link down if not require to update link state */
12145 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12146 	}
12147 	hwrm_req_drop(bp, req);
12148 
12149 	if (!BNXT_PHY_CFG_ABLE(bp))
12150 		return 0;
12151 
12152 	support_changed = bnxt_support_speed_dropped(link_info);
12153 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12154 		bnxt_hwrm_set_link_setting(bp, true, false);
12155 	return 0;
12156 }
12157 
12158 static void bnxt_get_port_module_status(struct bnxt *bp)
12159 {
12160 	struct bnxt_link_info *link_info = &bp->link_info;
12161 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12162 	u8 module_status;
12163 
12164 	if (bnxt_update_link(bp, true))
12165 		return;
12166 
12167 	module_status = link_info->module_status;
12168 	switch (module_status) {
12169 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12170 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12171 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12172 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12173 			    bp->pf.port_id);
12174 		if (bp->hwrm_spec_code >= 0x10201) {
12175 			netdev_warn(bp->dev, "Module part number %s\n",
12176 				    resp->phy_vendor_partnumber);
12177 		}
12178 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12179 			netdev_warn(bp->dev, "TX is disabled\n");
12180 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12181 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12182 	}
12183 }
12184 
12185 static void
12186 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12187 {
12188 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12189 		if (bp->hwrm_spec_code >= 0x10201)
12190 			req->auto_pause =
12191 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12192 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12193 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12194 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12195 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12196 		req->enables |=
12197 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12198 	} else {
12199 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12200 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12201 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12202 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12203 		req->enables |=
12204 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12205 		if (bp->hwrm_spec_code >= 0x10201) {
12206 			req->auto_pause = req->force_pause;
12207 			req->enables |= cpu_to_le32(
12208 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12209 		}
12210 	}
12211 }
12212 
12213 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12214 {
12215 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12216 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12217 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12218 			req->enables |=
12219 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12220 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12221 		} else if (bp->link_info.advertising) {
12222 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12223 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12224 		}
12225 		if (bp->link_info.advertising_pam4) {
12226 			req->enables |=
12227 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12228 			req->auto_link_pam4_speed_mask =
12229 				cpu_to_le16(bp->link_info.advertising_pam4);
12230 		}
12231 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12232 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12233 	} else {
12234 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12235 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12236 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12237 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12238 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12239 				   (u32)bp->link_info.req_link_speed);
12240 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12241 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12242 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12243 		} else {
12244 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12245 		}
12246 	}
12247 
12248 	/* tell chimp that the setting takes effect immediately */
12249 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12250 }
12251 
12252 int bnxt_hwrm_set_pause(struct bnxt *bp)
12253 {
12254 	struct hwrm_port_phy_cfg_input *req;
12255 	int rc;
12256 
12257 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12258 	if (rc)
12259 		return rc;
12260 
12261 	bnxt_hwrm_set_pause_common(bp, req);
12262 
12263 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12264 	    bp->link_info.force_link_chng)
12265 		bnxt_hwrm_set_link_common(bp, req);
12266 
12267 	rc = hwrm_req_send(bp, req);
12268 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12269 		/* since changing of pause setting doesn't trigger any link
12270 		 * change event, the driver needs to update the current pause
12271 		 * result upon successfully return of the phy_cfg command
12272 		 */
12273 		bp->link_info.pause =
12274 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12275 		bp->link_info.auto_pause_setting = 0;
12276 		if (!bp->link_info.force_link_chng)
12277 			bnxt_report_link(bp);
12278 	}
12279 	bp->link_info.force_link_chng = false;
12280 	return rc;
12281 }
12282 
12283 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12284 			      struct hwrm_port_phy_cfg_input *req)
12285 {
12286 	struct ethtool_keee *eee = &bp->eee;
12287 
12288 	if (eee->eee_enabled) {
12289 		u16 eee_speeds;
12290 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12291 
12292 		if (eee->tx_lpi_enabled)
12293 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12294 		else
12295 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12296 
12297 		req->flags |= cpu_to_le32(flags);
12298 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12299 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12300 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12301 	} else {
12302 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12303 	}
12304 }
12305 
12306 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12307 {
12308 	struct hwrm_port_phy_cfg_input *req;
12309 	int rc;
12310 
12311 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12312 	if (rc)
12313 		return rc;
12314 
12315 	if (set_pause)
12316 		bnxt_hwrm_set_pause_common(bp, req);
12317 
12318 	bnxt_hwrm_set_link_common(bp, req);
12319 
12320 	if (set_eee)
12321 		bnxt_hwrm_set_eee(bp, req);
12322 	return hwrm_req_send(bp, req);
12323 }
12324 
12325 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12326 {
12327 	struct hwrm_port_phy_cfg_input *req;
12328 	int rc;
12329 
12330 	if (!BNXT_SINGLE_PF(bp))
12331 		return 0;
12332 
12333 	if (pci_num_vf(bp->pdev) &&
12334 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12335 		return 0;
12336 
12337 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12338 	if (rc)
12339 		return rc;
12340 
12341 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12342 	rc = hwrm_req_send(bp, req);
12343 	if (!rc) {
12344 		mutex_lock(&bp->link_lock);
12345 		/* Device is not obliged link down in certain scenarios, even
12346 		 * when forced. Setting the state unknown is consistent with
12347 		 * driver startup and will force link state to be reported
12348 		 * during subsequent open based on PORT_PHY_QCFG.
12349 		 */
12350 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12351 		mutex_unlock(&bp->link_lock);
12352 	}
12353 	return rc;
12354 }
12355 
12356 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12357 {
12358 #ifdef CONFIG_TEE_BNXT_FW
12359 	int rc = tee_bnxt_fw_load();
12360 
12361 	if (rc)
12362 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12363 
12364 	return rc;
12365 #else
12366 	netdev_err(bp->dev, "OP-TEE not supported\n");
12367 	return -ENODEV;
12368 #endif
12369 }
12370 
12371 static int bnxt_try_recover_fw(struct bnxt *bp)
12372 {
12373 	if (bp->fw_health && bp->fw_health->status_reliable) {
12374 		int retry = 0, rc;
12375 		u32 sts;
12376 
12377 		do {
12378 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12379 			rc = bnxt_hwrm_poll(bp);
12380 			if (!BNXT_FW_IS_BOOTING(sts) &&
12381 			    !BNXT_FW_IS_RECOVERING(sts))
12382 				break;
12383 			retry++;
12384 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12385 
12386 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12387 			netdev_err(bp->dev,
12388 				   "Firmware not responding, status: 0x%x\n",
12389 				   sts);
12390 			rc = -ENODEV;
12391 		}
12392 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12393 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12394 			return bnxt_fw_reset_via_optee(bp);
12395 		}
12396 		return rc;
12397 	}
12398 
12399 	return -ENODEV;
12400 }
12401 
12402 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12403 {
12404 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12405 
12406 	if (!BNXT_NEW_RM(bp))
12407 		return; /* no resource reservations required */
12408 
12409 	hw_resc->resv_cp_rings = 0;
12410 	hw_resc->resv_stat_ctxs = 0;
12411 	hw_resc->resv_irqs = 0;
12412 	hw_resc->resv_tx_rings = 0;
12413 	hw_resc->resv_rx_rings = 0;
12414 	hw_resc->resv_hw_ring_grps = 0;
12415 	hw_resc->resv_vnics = 0;
12416 	hw_resc->resv_rsscos_ctxs = 0;
12417 	if (!fw_reset) {
12418 		bp->tx_nr_rings = 0;
12419 		bp->rx_nr_rings = 0;
12420 	}
12421 }
12422 
12423 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12424 {
12425 	int rc;
12426 
12427 	if (!BNXT_NEW_RM(bp))
12428 		return 0; /* no resource reservations required */
12429 
12430 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12431 	if (rc)
12432 		netdev_err(bp->dev, "resc_qcaps failed\n");
12433 
12434 	bnxt_clear_reservations(bp, fw_reset);
12435 
12436 	return rc;
12437 }
12438 
12439 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12440 {
12441 	struct hwrm_func_drv_if_change_output *resp;
12442 	struct hwrm_func_drv_if_change_input *req;
12443 	bool resc_reinit = false;
12444 	bool caps_change = false;
12445 	int rc, retry = 0;
12446 	bool fw_reset;
12447 	u32 flags = 0;
12448 
12449 	fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT);
12450 	bp->fw_reset_state = 0;
12451 
12452 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12453 		return 0;
12454 
12455 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12456 	if (rc)
12457 		return rc;
12458 
12459 	if (up)
12460 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12461 	resp = hwrm_req_hold(bp, req);
12462 
12463 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12464 	while (retry < BNXT_FW_IF_RETRY) {
12465 		rc = hwrm_req_send(bp, req);
12466 		if (rc != -EAGAIN)
12467 			break;
12468 
12469 		msleep(50);
12470 		retry++;
12471 	}
12472 
12473 	if (rc == -EAGAIN) {
12474 		hwrm_req_drop(bp, req);
12475 		return rc;
12476 	} else if (!rc) {
12477 		flags = le32_to_cpu(resp->flags);
12478 	} else if (up) {
12479 		rc = bnxt_try_recover_fw(bp);
12480 		fw_reset = true;
12481 	}
12482 	hwrm_req_drop(bp, req);
12483 	if (rc)
12484 		return rc;
12485 
12486 	if (!up) {
12487 		bnxt_inv_fw_health_reg(bp);
12488 		return 0;
12489 	}
12490 
12491 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12492 		resc_reinit = true;
12493 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12494 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12495 		fw_reset = true;
12496 	else
12497 		bnxt_remap_fw_health_regs(bp);
12498 
12499 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12500 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12501 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12502 		return -ENODEV;
12503 	}
12504 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12505 		caps_change = true;
12506 
12507 	if (resc_reinit || fw_reset || caps_change) {
12508 		if (fw_reset || caps_change) {
12509 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12510 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12511 				bnxt_ulp_irq_stop(bp);
12512 			bnxt_free_ctx_mem(bp, false);
12513 			bnxt_dcb_free(bp);
12514 			rc = bnxt_fw_init_one(bp);
12515 			if (rc) {
12516 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12517 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12518 				return rc;
12519 			}
12520 			/* IRQ will be initialized later in bnxt_request_irq()*/
12521 			bnxt_clear_int_mode(bp);
12522 		}
12523 		rc = bnxt_cancel_reservations(bp, fw_reset);
12524 	}
12525 	return rc;
12526 }
12527 
12528 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12529 {
12530 	struct hwrm_port_led_qcaps_output *resp;
12531 	struct hwrm_port_led_qcaps_input *req;
12532 	struct bnxt_pf_info *pf = &bp->pf;
12533 	int rc;
12534 
12535 	bp->num_leds = 0;
12536 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12537 		return 0;
12538 
12539 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12540 	if (rc)
12541 		return rc;
12542 
12543 	req->port_id = cpu_to_le16(pf->port_id);
12544 	resp = hwrm_req_hold(bp, req);
12545 	rc = hwrm_req_send(bp, req);
12546 	if (rc) {
12547 		hwrm_req_drop(bp, req);
12548 		return rc;
12549 	}
12550 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12551 		int i;
12552 
12553 		bp->num_leds = resp->num_leds;
12554 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12555 						 bp->num_leds);
12556 		for (i = 0; i < bp->num_leds; i++) {
12557 			struct bnxt_led_info *led = &bp->leds[i];
12558 			__le16 caps = led->led_state_caps;
12559 
12560 			if (!led->led_group_id ||
12561 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12562 				bp->num_leds = 0;
12563 				break;
12564 			}
12565 		}
12566 	}
12567 	hwrm_req_drop(bp, req);
12568 	return 0;
12569 }
12570 
12571 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12572 {
12573 	struct hwrm_wol_filter_alloc_output *resp;
12574 	struct hwrm_wol_filter_alloc_input *req;
12575 	int rc;
12576 
12577 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12578 	if (rc)
12579 		return rc;
12580 
12581 	req->port_id = cpu_to_le16(bp->pf.port_id);
12582 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12583 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12584 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12585 
12586 	resp = hwrm_req_hold(bp, req);
12587 	rc = hwrm_req_send(bp, req);
12588 	if (!rc)
12589 		bp->wol_filter_id = resp->wol_filter_id;
12590 	hwrm_req_drop(bp, req);
12591 	return rc;
12592 }
12593 
12594 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12595 {
12596 	struct hwrm_wol_filter_free_input *req;
12597 	int rc;
12598 
12599 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12600 	if (rc)
12601 		return rc;
12602 
12603 	req->port_id = cpu_to_le16(bp->pf.port_id);
12604 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12605 	req->wol_filter_id = bp->wol_filter_id;
12606 
12607 	return hwrm_req_send(bp, req);
12608 }
12609 
12610 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12611 {
12612 	struct hwrm_wol_filter_qcfg_output *resp;
12613 	struct hwrm_wol_filter_qcfg_input *req;
12614 	u16 next_handle = 0;
12615 	int rc;
12616 
12617 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12618 	if (rc)
12619 		return rc;
12620 
12621 	req->port_id = cpu_to_le16(bp->pf.port_id);
12622 	req->handle = cpu_to_le16(handle);
12623 	resp = hwrm_req_hold(bp, req);
12624 	rc = hwrm_req_send(bp, req);
12625 	if (!rc) {
12626 		next_handle = le16_to_cpu(resp->next_handle);
12627 		if (next_handle != 0) {
12628 			if (resp->wol_type ==
12629 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12630 				bp->wol = 1;
12631 				bp->wol_filter_id = resp->wol_filter_id;
12632 			}
12633 		}
12634 	}
12635 	hwrm_req_drop(bp, req);
12636 	return next_handle;
12637 }
12638 
12639 static void bnxt_get_wol_settings(struct bnxt *bp)
12640 {
12641 	u16 handle = 0;
12642 
12643 	bp->wol = 0;
12644 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12645 		return;
12646 
12647 	do {
12648 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12649 	} while (handle && handle != 0xffff);
12650 }
12651 
12652 static bool bnxt_eee_config_ok(struct bnxt *bp)
12653 {
12654 	struct ethtool_keee *eee = &bp->eee;
12655 	struct bnxt_link_info *link_info = &bp->link_info;
12656 
12657 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12658 		return true;
12659 
12660 	if (eee->eee_enabled) {
12661 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12662 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12663 
12664 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12665 
12666 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12667 			eee->eee_enabled = 0;
12668 			return false;
12669 		}
12670 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12671 			linkmode_and(eee->advertised, advertising,
12672 				     eee->supported);
12673 			return false;
12674 		}
12675 	}
12676 	return true;
12677 }
12678 
12679 static int bnxt_update_phy_setting(struct bnxt *bp)
12680 {
12681 	int rc;
12682 	bool update_link = false;
12683 	bool update_pause = false;
12684 	bool update_eee = false;
12685 	struct bnxt_link_info *link_info = &bp->link_info;
12686 
12687 	rc = bnxt_update_link(bp, true);
12688 	if (rc) {
12689 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12690 			   rc);
12691 		return rc;
12692 	}
12693 	if (!BNXT_SINGLE_PF(bp))
12694 		return 0;
12695 
12696 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12697 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12698 	    link_info->req_flow_ctrl)
12699 		update_pause = true;
12700 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12701 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12702 		update_pause = true;
12703 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12704 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12705 			update_link = true;
12706 		if (bnxt_force_speed_updated(link_info))
12707 			update_link = true;
12708 		if (link_info->req_duplex != link_info->duplex_setting)
12709 			update_link = true;
12710 	} else {
12711 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12712 			update_link = true;
12713 		if (bnxt_auto_speed_updated(link_info))
12714 			update_link = true;
12715 	}
12716 
12717 	/* The last close may have shutdown the link, so need to call
12718 	 * PHY_CFG to bring it back up.
12719 	 */
12720 	if (!BNXT_LINK_IS_UP(bp))
12721 		update_link = true;
12722 
12723 	if (!bnxt_eee_config_ok(bp))
12724 		update_eee = true;
12725 
12726 	if (update_link)
12727 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12728 	else if (update_pause)
12729 		rc = bnxt_hwrm_set_pause(bp);
12730 	if (rc) {
12731 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12732 			   rc);
12733 		return rc;
12734 	}
12735 
12736 	return rc;
12737 }
12738 
12739 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12740 
12741 static int bnxt_reinit_after_abort(struct bnxt *bp)
12742 {
12743 	int rc;
12744 
12745 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12746 		return -EBUSY;
12747 
12748 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12749 		return -ENODEV;
12750 
12751 	rc = bnxt_fw_init_one(bp);
12752 	if (!rc) {
12753 		bnxt_clear_int_mode(bp);
12754 		rc = bnxt_init_int_mode(bp);
12755 		if (!rc) {
12756 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12757 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12758 		}
12759 	}
12760 	return rc;
12761 }
12762 
12763 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12764 {
12765 	struct bnxt_ntuple_filter *ntp_fltr;
12766 	struct bnxt_l2_filter *l2_fltr;
12767 
12768 	if (list_empty(&fltr->list))
12769 		return;
12770 
12771 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12772 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12773 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12774 		atomic_inc(&l2_fltr->refcnt);
12775 		ntp_fltr->l2_fltr = l2_fltr;
12776 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12777 			bnxt_del_ntp_filter(bp, ntp_fltr);
12778 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12779 				   fltr->sw_id);
12780 		}
12781 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12782 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12783 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12784 			bnxt_del_l2_filter(bp, l2_fltr);
12785 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12786 				   fltr->sw_id);
12787 		}
12788 	}
12789 }
12790 
12791 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12792 {
12793 	struct bnxt_filter_base *usr_fltr, *tmp;
12794 
12795 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12796 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12797 }
12798 
12799 static int bnxt_set_xps_mapping(struct bnxt *bp)
12800 {
12801 	int numa_node = dev_to_node(&bp->pdev->dev);
12802 	unsigned int q_idx, map_idx, cpu, i;
12803 	const struct cpumask *cpu_mask_ptr;
12804 	int nr_cpus = num_online_cpus();
12805 	cpumask_t *q_map;
12806 	int rc = 0;
12807 
12808 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12809 	if (!q_map)
12810 		return -ENOMEM;
12811 
12812 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12813 	 * Each TC has the same number of TX queues. The nth TX queue for each
12814 	 * TC will have the same CPU mask.
12815 	 */
12816 	for (i = 0; i < nr_cpus; i++) {
12817 		map_idx = i % bp->tx_nr_rings_per_tc;
12818 		cpu = cpumask_local_spread(i, numa_node);
12819 		cpu_mask_ptr = get_cpu_mask(cpu);
12820 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12821 	}
12822 
12823 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12824 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12825 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12826 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12827 		if (rc) {
12828 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12829 				    q_idx);
12830 			break;
12831 		}
12832 	}
12833 
12834 	kfree(q_map);
12835 
12836 	return rc;
12837 }
12838 
12839 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12840 {
12841 	int rc = 0;
12842 
12843 	netif_carrier_off(bp->dev);
12844 	if (irq_re_init) {
12845 		/* Reserve rings now if none were reserved at driver probe. */
12846 		rc = bnxt_init_dflt_ring_mode(bp);
12847 		if (rc) {
12848 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12849 			return rc;
12850 		}
12851 	}
12852 	rc = bnxt_reserve_rings(bp, irq_re_init);
12853 	if (rc)
12854 		return rc;
12855 
12856 	rc = bnxt_alloc_mem(bp, irq_re_init);
12857 	if (rc) {
12858 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12859 		goto open_err_free_mem;
12860 	}
12861 
12862 	if (irq_re_init) {
12863 		bnxt_init_napi(bp);
12864 		rc = bnxt_request_irq(bp);
12865 		if (rc) {
12866 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12867 			goto open_err_irq;
12868 		}
12869 	}
12870 
12871 	rc = bnxt_init_nic(bp, irq_re_init);
12872 	if (rc) {
12873 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12874 		goto open_err_irq;
12875 	}
12876 
12877 	bnxt_enable_napi(bp);
12878 	bnxt_debug_dev_init(bp);
12879 
12880 	if (link_re_init) {
12881 		mutex_lock(&bp->link_lock);
12882 		rc = bnxt_update_phy_setting(bp);
12883 		mutex_unlock(&bp->link_lock);
12884 		if (rc) {
12885 			netdev_warn(bp->dev, "failed to update phy settings\n");
12886 			if (BNXT_SINGLE_PF(bp)) {
12887 				bp->link_info.phy_retry = true;
12888 				bp->link_info.phy_retry_expires =
12889 					jiffies + 5 * HZ;
12890 			}
12891 		}
12892 	}
12893 
12894 	if (irq_re_init) {
12895 		udp_tunnel_nic_reset_ntf(bp->dev);
12896 		rc = bnxt_set_xps_mapping(bp);
12897 		if (rc)
12898 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12899 	}
12900 
12901 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12902 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12903 			static_branch_enable(&bnxt_xdp_locking_key);
12904 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12905 		static_branch_disable(&bnxt_xdp_locking_key);
12906 	}
12907 	set_bit(BNXT_STATE_OPEN, &bp->state);
12908 	bnxt_enable_int(bp);
12909 	/* Enable TX queues */
12910 	bnxt_tx_enable(bp);
12911 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12912 	/* Poll link status and check for SFP+ module status */
12913 	mutex_lock(&bp->link_lock);
12914 	bnxt_get_port_module_status(bp);
12915 	mutex_unlock(&bp->link_lock);
12916 
12917 	/* VF-reps may need to be re-opened after the PF is re-opened */
12918 	if (BNXT_PF(bp))
12919 		bnxt_vf_reps_open(bp);
12920 	bnxt_ptp_init_rtc(bp, true);
12921 	bnxt_ptp_cfg_tstamp_filters(bp);
12922 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12923 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12924 	bnxt_cfg_usr_fltrs(bp);
12925 	return 0;
12926 
12927 open_err_irq:
12928 	bnxt_del_napi(bp);
12929 
12930 open_err_free_mem:
12931 	bnxt_free_skbs(bp);
12932 	bnxt_free_irq(bp);
12933 	bnxt_free_mem(bp, true);
12934 	return rc;
12935 }
12936 
12937 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12938 {
12939 	int rc = 0;
12940 
12941 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12942 		rc = -EIO;
12943 	if (!rc)
12944 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12945 	if (rc) {
12946 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12947 		netif_close(bp->dev);
12948 	}
12949 	return rc;
12950 }
12951 
12952 /* netdev instance lock held, open the NIC half way by allocating all
12953  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
12954  * for offline self tests.
12955  */
12956 int bnxt_half_open_nic(struct bnxt *bp)
12957 {
12958 	int rc = 0;
12959 
12960 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12961 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12962 		rc = -ENODEV;
12963 		goto half_open_err;
12964 	}
12965 
12966 	rc = bnxt_alloc_mem(bp, true);
12967 	if (rc) {
12968 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12969 		goto half_open_err;
12970 	}
12971 	bnxt_init_napi(bp);
12972 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12973 	rc = bnxt_init_nic(bp, true);
12974 	if (rc) {
12975 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12976 		bnxt_del_napi(bp);
12977 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12978 		goto half_open_err;
12979 	}
12980 	return 0;
12981 
12982 half_open_err:
12983 	bnxt_free_skbs(bp);
12984 	bnxt_free_mem(bp, true);
12985 	netif_close(bp->dev);
12986 	return rc;
12987 }
12988 
12989 /* netdev instance lock held, this call can only be made after a previous
12990  * successful call to bnxt_half_open_nic().
12991  */
12992 void bnxt_half_close_nic(struct bnxt *bp)
12993 {
12994 	bnxt_hwrm_resource_free(bp, false, true);
12995 	bnxt_del_napi(bp);
12996 	bnxt_free_skbs(bp);
12997 	bnxt_free_mem(bp, true);
12998 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12999 }
13000 
13001 void bnxt_reenable_sriov(struct bnxt *bp)
13002 {
13003 	if (BNXT_PF(bp)) {
13004 		struct bnxt_pf_info *pf = &bp->pf;
13005 		int n = pf->active_vfs;
13006 
13007 		if (n)
13008 			bnxt_cfg_hw_sriov(bp, &n, true);
13009 	}
13010 }
13011 
13012 static int bnxt_open(struct net_device *dev)
13013 {
13014 	struct bnxt *bp = netdev_priv(dev);
13015 	int rc;
13016 
13017 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13018 		rc = bnxt_reinit_after_abort(bp);
13019 		if (rc) {
13020 			if (rc == -EBUSY)
13021 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
13022 			else
13023 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
13024 			return -ENODEV;
13025 		}
13026 	}
13027 
13028 	rc = bnxt_hwrm_if_change(bp, true);
13029 	if (rc)
13030 		return rc;
13031 
13032 	rc = __bnxt_open_nic(bp, true, true);
13033 	if (rc) {
13034 		bnxt_hwrm_if_change(bp, false);
13035 	} else {
13036 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
13037 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13038 				bnxt_queue_sp_work(bp,
13039 						   BNXT_RESTART_ULP_SP_EVENT);
13040 		}
13041 	}
13042 
13043 	return rc;
13044 }
13045 
13046 static bool bnxt_drv_busy(struct bnxt *bp)
13047 {
13048 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
13049 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
13050 }
13051 
13052 static void bnxt_get_ring_stats(struct bnxt *bp,
13053 				struct rtnl_link_stats64 *stats);
13054 
13055 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
13056 			     bool link_re_init)
13057 {
13058 	/* Close the VF-reps before closing PF */
13059 	if (BNXT_PF(bp))
13060 		bnxt_vf_reps_close(bp);
13061 
13062 	/* Change device state to avoid TX queue wake up's */
13063 	bnxt_tx_disable(bp);
13064 
13065 	clear_bit(BNXT_STATE_OPEN, &bp->state);
13066 	smp_mb__after_atomic();
13067 	while (bnxt_drv_busy(bp))
13068 		msleep(20);
13069 
13070 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13071 		bnxt_clear_rss_ctxs(bp);
13072 	/* Flush rings and disable interrupts */
13073 	bnxt_shutdown_nic(bp, irq_re_init);
13074 
13075 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
13076 
13077 	bnxt_debug_dev_exit(bp);
13078 	bnxt_disable_napi(bp);
13079 	timer_delete_sync(&bp->timer);
13080 	bnxt_free_skbs(bp);
13081 
13082 	/* Save ring stats before shutdown */
13083 	if (bp->bnapi && irq_re_init) {
13084 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
13085 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
13086 	}
13087 	if (irq_re_init) {
13088 		bnxt_free_irq(bp);
13089 		bnxt_del_napi(bp);
13090 	}
13091 	bnxt_free_mem(bp, irq_re_init);
13092 }
13093 
13094 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13095 {
13096 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13097 		/* If we get here, it means firmware reset is in progress
13098 		 * while we are trying to close.  We can safely proceed with
13099 		 * the close because we are holding netdev instance lock.
13100 		 * Some firmware messages may fail as we proceed to close.
13101 		 * We set the ABORT_ERR flag here so that the FW reset thread
13102 		 * will later abort when it gets the netdev instance lock
13103 		 * and sees the flag.
13104 		 */
13105 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
13106 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13107 	}
13108 
13109 #ifdef CONFIG_BNXT_SRIOV
13110 	if (bp->sriov_cfg) {
13111 		int rc;
13112 
13113 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
13114 						      !bp->sriov_cfg,
13115 						      BNXT_SRIOV_CFG_WAIT_TMO);
13116 		if (!rc)
13117 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13118 		else if (rc < 0)
13119 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13120 	}
13121 #endif
13122 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13123 }
13124 
13125 static int bnxt_close(struct net_device *dev)
13126 {
13127 	struct bnxt *bp = netdev_priv(dev);
13128 
13129 	bnxt_close_nic(bp, true, true);
13130 	bnxt_hwrm_shutdown_link(bp);
13131 	bnxt_hwrm_if_change(bp, false);
13132 	return 0;
13133 }
13134 
13135 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13136 				   u16 *val)
13137 {
13138 	struct hwrm_port_phy_mdio_read_output *resp;
13139 	struct hwrm_port_phy_mdio_read_input *req;
13140 	int rc;
13141 
13142 	if (bp->hwrm_spec_code < 0x10a00)
13143 		return -EOPNOTSUPP;
13144 
13145 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13146 	if (rc)
13147 		return rc;
13148 
13149 	req->port_id = cpu_to_le16(bp->pf.port_id);
13150 	req->phy_addr = phy_addr;
13151 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13152 	if (mdio_phy_id_is_c45(phy_addr)) {
13153 		req->cl45_mdio = 1;
13154 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13155 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13156 		req->reg_addr = cpu_to_le16(reg);
13157 	}
13158 
13159 	resp = hwrm_req_hold(bp, req);
13160 	rc = hwrm_req_send(bp, req);
13161 	if (!rc)
13162 		*val = le16_to_cpu(resp->reg_data);
13163 	hwrm_req_drop(bp, req);
13164 	return rc;
13165 }
13166 
13167 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13168 				    u16 val)
13169 {
13170 	struct hwrm_port_phy_mdio_write_input *req;
13171 	int rc;
13172 
13173 	if (bp->hwrm_spec_code < 0x10a00)
13174 		return -EOPNOTSUPP;
13175 
13176 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13177 	if (rc)
13178 		return rc;
13179 
13180 	req->port_id = cpu_to_le16(bp->pf.port_id);
13181 	req->phy_addr = phy_addr;
13182 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13183 	if (mdio_phy_id_is_c45(phy_addr)) {
13184 		req->cl45_mdio = 1;
13185 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13186 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13187 		req->reg_addr = cpu_to_le16(reg);
13188 	}
13189 	req->reg_data = cpu_to_le16(val);
13190 
13191 	return hwrm_req_send(bp, req);
13192 }
13193 
13194 /* netdev instance lock held */
13195 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13196 {
13197 	struct mii_ioctl_data *mdio = if_mii(ifr);
13198 	struct bnxt *bp = netdev_priv(dev);
13199 	int rc;
13200 
13201 	switch (cmd) {
13202 	case SIOCGMIIPHY:
13203 		mdio->phy_id = bp->link_info.phy_addr;
13204 
13205 		fallthrough;
13206 	case SIOCGMIIREG: {
13207 		u16 mii_regval = 0;
13208 
13209 		if (!netif_running(dev))
13210 			return -EAGAIN;
13211 
13212 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13213 					     &mii_regval);
13214 		mdio->val_out = mii_regval;
13215 		return rc;
13216 	}
13217 
13218 	case SIOCSMIIREG:
13219 		if (!netif_running(dev))
13220 			return -EAGAIN;
13221 
13222 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13223 						mdio->val_in);
13224 
13225 	case SIOCSHWTSTAMP:
13226 		return bnxt_hwtstamp_set(dev, ifr);
13227 
13228 	case SIOCGHWTSTAMP:
13229 		return bnxt_hwtstamp_get(dev, ifr);
13230 
13231 	default:
13232 		/* do nothing */
13233 		break;
13234 	}
13235 	return -EOPNOTSUPP;
13236 }
13237 
13238 static void bnxt_get_ring_stats(struct bnxt *bp,
13239 				struct rtnl_link_stats64 *stats)
13240 {
13241 	int i;
13242 
13243 	for (i = 0; i < bp->cp_nr_rings; i++) {
13244 		struct bnxt_napi *bnapi = bp->bnapi[i];
13245 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13246 		u64 *sw = cpr->stats.sw_stats;
13247 
13248 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13249 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13250 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13251 
13252 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13253 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13254 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13255 
13256 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13257 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13258 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13259 
13260 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13261 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13262 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13263 
13264 		stats->rx_missed_errors +=
13265 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13266 
13267 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13268 
13269 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13270 
13271 		stats->rx_dropped +=
13272 			cpr->sw_stats->rx.rx_netpoll_discards +
13273 			cpr->sw_stats->rx.rx_oom_discards;
13274 	}
13275 }
13276 
13277 static void bnxt_add_prev_stats(struct bnxt *bp,
13278 				struct rtnl_link_stats64 *stats)
13279 {
13280 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13281 
13282 	stats->rx_packets += prev_stats->rx_packets;
13283 	stats->tx_packets += prev_stats->tx_packets;
13284 	stats->rx_bytes += prev_stats->rx_bytes;
13285 	stats->tx_bytes += prev_stats->tx_bytes;
13286 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13287 	stats->multicast += prev_stats->multicast;
13288 	stats->rx_dropped += prev_stats->rx_dropped;
13289 	stats->tx_dropped += prev_stats->tx_dropped;
13290 }
13291 
13292 static void
13293 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13294 {
13295 	struct bnxt *bp = netdev_priv(dev);
13296 
13297 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13298 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13299 	 * we check the BNXT_STATE_OPEN flag.
13300 	 */
13301 	smp_mb__after_atomic();
13302 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13303 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13304 		*stats = bp->net_stats_prev;
13305 		return;
13306 	}
13307 
13308 	bnxt_get_ring_stats(bp, stats);
13309 	bnxt_add_prev_stats(bp, stats);
13310 
13311 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13312 		u64 *rx = bp->port_stats.sw_stats;
13313 		u64 *tx = bp->port_stats.sw_stats +
13314 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13315 
13316 		stats->rx_crc_errors =
13317 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13318 		stats->rx_frame_errors =
13319 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13320 		stats->rx_length_errors =
13321 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13322 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13323 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13324 		stats->rx_errors =
13325 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13326 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13327 		stats->collisions =
13328 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13329 		stats->tx_fifo_errors =
13330 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13331 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13332 	}
13333 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13334 }
13335 
13336 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
13337 					struct bnxt_total_ring_err_stats *stats,
13338 					struct bnxt_cp_ring_info *cpr)
13339 {
13340 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13341 	u64 *hw_stats = cpr->stats.sw_stats;
13342 
13343 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13344 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13345 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13346 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13347 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13348 	stats->rx_total_ring_discards +=
13349 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13350 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13351 	stats->tx_total_ring_discards +=
13352 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13353 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13354 }
13355 
13356 void bnxt_get_ring_err_stats(struct bnxt *bp,
13357 			     struct bnxt_total_ring_err_stats *stats)
13358 {
13359 	int i;
13360 
13361 	for (i = 0; i < bp->cp_nr_rings; i++)
13362 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13363 }
13364 
13365 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
13366 {
13367 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13368 	struct net_device *dev = bp->dev;
13369 	struct netdev_hw_addr *ha;
13370 	u8 *haddr;
13371 	int mc_count = 0;
13372 	bool update = false;
13373 	int off = 0;
13374 
13375 	netdev_for_each_mc_addr(ha, dev) {
13376 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13377 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13378 			vnic->mc_list_count = 0;
13379 			return false;
13380 		}
13381 		haddr = ha->addr;
13382 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13383 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13384 			update = true;
13385 		}
13386 		off += ETH_ALEN;
13387 		mc_count++;
13388 	}
13389 	if (mc_count)
13390 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13391 
13392 	if (mc_count != vnic->mc_list_count) {
13393 		vnic->mc_list_count = mc_count;
13394 		update = true;
13395 	}
13396 	return update;
13397 }
13398 
13399 static bool bnxt_uc_list_updated(struct bnxt *bp)
13400 {
13401 	struct net_device *dev = bp->dev;
13402 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13403 	struct netdev_hw_addr *ha;
13404 	int off = 0;
13405 
13406 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
13407 		return true;
13408 
13409 	netdev_for_each_uc_addr(ha, dev) {
13410 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13411 			return true;
13412 
13413 		off += ETH_ALEN;
13414 	}
13415 	return false;
13416 }
13417 
13418 static void bnxt_set_rx_mode(struct net_device *dev)
13419 {
13420 	struct bnxt *bp = netdev_priv(dev);
13421 	struct bnxt_vnic_info *vnic;
13422 	bool mc_update = false;
13423 	bool uc_update;
13424 	u32 mask;
13425 
13426 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13427 		return;
13428 
13429 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13430 	mask = vnic->rx_mask;
13431 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13432 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13433 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13434 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13435 
13436 	if (dev->flags & IFF_PROMISC)
13437 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13438 
13439 	uc_update = bnxt_uc_list_updated(bp);
13440 
13441 	if (dev->flags & IFF_BROADCAST)
13442 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13443 	if (dev->flags & IFF_ALLMULTI) {
13444 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13445 		vnic->mc_list_count = 0;
13446 	} else if (dev->flags & IFF_MULTICAST) {
13447 		mc_update = bnxt_mc_list_updated(bp, &mask);
13448 	}
13449 
13450 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13451 		vnic->rx_mask = mask;
13452 
13453 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13454 	}
13455 }
13456 
13457 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13458 {
13459 	struct net_device *dev = bp->dev;
13460 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13461 	struct netdev_hw_addr *ha;
13462 	int i, off = 0, rc;
13463 	bool uc_update;
13464 
13465 	netif_addr_lock_bh(dev);
13466 	uc_update = bnxt_uc_list_updated(bp);
13467 	netif_addr_unlock_bh(dev);
13468 
13469 	if (!uc_update)
13470 		goto skip_uc;
13471 
13472 	for (i = 1; i < vnic->uc_filter_count; i++) {
13473 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13474 
13475 		bnxt_hwrm_l2_filter_free(bp, fltr);
13476 		bnxt_del_l2_filter(bp, fltr);
13477 	}
13478 
13479 	vnic->uc_filter_count = 1;
13480 
13481 	netif_addr_lock_bh(dev);
13482 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13483 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13484 	} else {
13485 		netdev_for_each_uc_addr(ha, dev) {
13486 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13487 			off += ETH_ALEN;
13488 			vnic->uc_filter_count++;
13489 		}
13490 	}
13491 	netif_addr_unlock_bh(dev);
13492 
13493 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13494 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13495 		if (rc) {
13496 			if (BNXT_VF(bp) && rc == -ENODEV) {
13497 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13498 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13499 				else
13500 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13501 				rc = 0;
13502 			} else {
13503 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13504 			}
13505 			vnic->uc_filter_count = i;
13506 			return rc;
13507 		}
13508 	}
13509 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13510 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13511 
13512 skip_uc:
13513 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13514 	    !bnxt_promisc_ok(bp))
13515 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13516 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13517 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13518 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13519 			    rc);
13520 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13521 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13522 		vnic->mc_list_count = 0;
13523 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13524 	}
13525 	if (rc)
13526 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13527 			   rc);
13528 
13529 	return rc;
13530 }
13531 
13532 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13533 {
13534 #ifdef CONFIG_BNXT_SRIOV
13535 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13536 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13537 
13538 		/* No minimum rings were provisioned by the PF.  Don't
13539 		 * reserve rings by default when device is down.
13540 		 */
13541 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13542 			return true;
13543 
13544 		if (!netif_running(bp->dev))
13545 			return false;
13546 	}
13547 #endif
13548 	return true;
13549 }
13550 
13551 /* If the chip and firmware supports RFS */
13552 static bool bnxt_rfs_supported(struct bnxt *bp)
13553 {
13554 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13555 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13556 			return true;
13557 		return false;
13558 	}
13559 	/* 212 firmware is broken for aRFS */
13560 	if (BNXT_FW_MAJ(bp) == 212)
13561 		return false;
13562 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13563 		return true;
13564 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13565 		return true;
13566 	return false;
13567 }
13568 
13569 /* If runtime conditions support RFS */
13570 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13571 {
13572 	struct bnxt_hw_rings hwr = {0};
13573 	int max_vnics, max_rss_ctxs;
13574 
13575 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13576 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13577 		return bnxt_rfs_supported(bp);
13578 
13579 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13580 		return false;
13581 
13582 	hwr.grp = bp->rx_nr_rings;
13583 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13584 	if (new_rss_ctx)
13585 		hwr.vnic++;
13586 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13587 	max_vnics = bnxt_get_max_func_vnics(bp);
13588 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13589 
13590 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13591 		if (bp->rx_nr_rings > 1)
13592 			netdev_warn(bp->dev,
13593 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13594 				    min(max_rss_ctxs - 1, max_vnics - 1));
13595 		return false;
13596 	}
13597 
13598 	if (!BNXT_NEW_RM(bp))
13599 		return true;
13600 
13601 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13602 	 * issue that will mess up the default VNIC if we reduce the
13603 	 * reservations.
13604 	 */
13605 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13606 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13607 		return true;
13608 
13609 	bnxt_hwrm_reserve_rings(bp, &hwr);
13610 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13611 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13612 		return true;
13613 
13614 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13615 	hwr.vnic = 1;
13616 	hwr.rss_ctx = 0;
13617 	bnxt_hwrm_reserve_rings(bp, &hwr);
13618 	return false;
13619 }
13620 
13621 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13622 					   netdev_features_t features)
13623 {
13624 	struct bnxt *bp = netdev_priv(dev);
13625 	netdev_features_t vlan_features;
13626 
13627 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13628 		features &= ~NETIF_F_NTUPLE;
13629 
13630 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13631 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13632 
13633 	if (!(features & NETIF_F_GRO))
13634 		features &= ~NETIF_F_GRO_HW;
13635 
13636 	if (features & NETIF_F_GRO_HW)
13637 		features &= ~NETIF_F_LRO;
13638 
13639 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13640 	 * turned on or off together.
13641 	 */
13642 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13643 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13644 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13645 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13646 		else if (vlan_features)
13647 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13648 	}
13649 #ifdef CONFIG_BNXT_SRIOV
13650 	if (BNXT_VF(bp) && bp->vf.vlan)
13651 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13652 #endif
13653 	return features;
13654 }
13655 
13656 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13657 				bool link_re_init, u32 flags, bool update_tpa)
13658 {
13659 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13660 	bp->flags = flags;
13661 	if (update_tpa)
13662 		bnxt_set_ring_params(bp);
13663 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13664 }
13665 
13666 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13667 {
13668 	bool update_tpa = false, update_ntuple = false;
13669 	struct bnxt *bp = netdev_priv(dev);
13670 	u32 flags = bp->flags;
13671 	u32 changes;
13672 	int rc = 0;
13673 	bool re_init = false;
13674 
13675 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13676 	if (features & NETIF_F_GRO_HW)
13677 		flags |= BNXT_FLAG_GRO;
13678 	else if (features & NETIF_F_LRO)
13679 		flags |= BNXT_FLAG_LRO;
13680 
13681 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13682 		flags &= ~BNXT_FLAG_TPA;
13683 
13684 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13685 		flags |= BNXT_FLAG_STRIP_VLAN;
13686 
13687 	if (features & NETIF_F_NTUPLE)
13688 		flags |= BNXT_FLAG_RFS;
13689 	else
13690 		bnxt_clear_usr_fltrs(bp, true);
13691 
13692 	changes = flags ^ bp->flags;
13693 	if (changes & BNXT_FLAG_TPA) {
13694 		update_tpa = true;
13695 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13696 		    (flags & BNXT_FLAG_TPA) == 0 ||
13697 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13698 			re_init = true;
13699 	}
13700 
13701 	if (changes & ~BNXT_FLAG_TPA)
13702 		re_init = true;
13703 
13704 	if (changes & BNXT_FLAG_RFS)
13705 		update_ntuple = true;
13706 
13707 	if (flags != bp->flags) {
13708 		u32 old_flags = bp->flags;
13709 
13710 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13711 			bp->flags = flags;
13712 			if (update_tpa)
13713 				bnxt_set_ring_params(bp);
13714 			return rc;
13715 		}
13716 
13717 		if (update_ntuple)
13718 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13719 
13720 		if (re_init)
13721 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13722 
13723 		if (update_tpa) {
13724 			bp->flags = flags;
13725 			rc = bnxt_set_tpa(bp,
13726 					  (flags & BNXT_FLAG_TPA) ?
13727 					  true : false);
13728 			if (rc)
13729 				bp->flags = old_flags;
13730 		}
13731 	}
13732 	return rc;
13733 }
13734 
13735 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13736 			      u8 **nextp)
13737 {
13738 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13739 	struct hop_jumbo_hdr *jhdr;
13740 	int hdr_count = 0;
13741 	u8 *nexthdr;
13742 	int start;
13743 
13744 	/* Check that there are at most 2 IPv6 extension headers, no
13745 	 * fragment header, and each is <= 64 bytes.
13746 	 */
13747 	start = nw_off + sizeof(*ip6h);
13748 	nexthdr = &ip6h->nexthdr;
13749 	while (ipv6_ext_hdr(*nexthdr)) {
13750 		struct ipv6_opt_hdr *hp;
13751 		int hdrlen;
13752 
13753 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13754 		    *nexthdr == NEXTHDR_FRAGMENT)
13755 			return false;
13756 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13757 					  skb_headlen(skb), NULL);
13758 		if (!hp)
13759 			return false;
13760 		if (*nexthdr == NEXTHDR_AUTH)
13761 			hdrlen = ipv6_authlen(hp);
13762 		else
13763 			hdrlen = ipv6_optlen(hp);
13764 
13765 		if (hdrlen > 64)
13766 			return false;
13767 
13768 		/* The ext header may be a hop-by-hop header inserted for
13769 		 * big TCP purposes. This will be removed before sending
13770 		 * from NIC, so do not count it.
13771 		 */
13772 		if (*nexthdr == NEXTHDR_HOP) {
13773 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13774 				goto increment_hdr;
13775 
13776 			jhdr = (struct hop_jumbo_hdr *)hp;
13777 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13778 			    jhdr->nexthdr != IPPROTO_TCP)
13779 				goto increment_hdr;
13780 
13781 			goto next_hdr;
13782 		}
13783 increment_hdr:
13784 		hdr_count++;
13785 next_hdr:
13786 		nexthdr = &hp->nexthdr;
13787 		start += hdrlen;
13788 	}
13789 	if (nextp) {
13790 		/* Caller will check inner protocol */
13791 		if (skb->encapsulation) {
13792 			*nextp = nexthdr;
13793 			return true;
13794 		}
13795 		*nextp = NULL;
13796 	}
13797 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13798 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13799 }
13800 
13801 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13802 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13803 {
13804 	struct udphdr *uh = udp_hdr(skb);
13805 	__be16 udp_port = uh->dest;
13806 
13807 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13808 	    udp_port != bp->vxlan_gpe_port)
13809 		return false;
13810 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13811 		struct ethhdr *eh = inner_eth_hdr(skb);
13812 
13813 		switch (eh->h_proto) {
13814 		case htons(ETH_P_IP):
13815 			return true;
13816 		case htons(ETH_P_IPV6):
13817 			return bnxt_exthdr_check(bp, skb,
13818 						 skb_inner_network_offset(skb),
13819 						 NULL);
13820 		}
13821 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13822 		return true;
13823 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13824 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13825 					 NULL);
13826 	}
13827 	return false;
13828 }
13829 
13830 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13831 {
13832 	switch (l4_proto) {
13833 	case IPPROTO_UDP:
13834 		return bnxt_udp_tunl_check(bp, skb);
13835 	case IPPROTO_IPIP:
13836 		return true;
13837 	case IPPROTO_GRE: {
13838 		switch (skb->inner_protocol) {
13839 		default:
13840 			return false;
13841 		case htons(ETH_P_IP):
13842 			return true;
13843 		case htons(ETH_P_IPV6):
13844 			fallthrough;
13845 		}
13846 	}
13847 	case IPPROTO_IPV6:
13848 		/* Check ext headers of inner ipv6 */
13849 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13850 					 NULL);
13851 	}
13852 	return false;
13853 }
13854 
13855 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13856 					     struct net_device *dev,
13857 					     netdev_features_t features)
13858 {
13859 	struct bnxt *bp = netdev_priv(dev);
13860 	u8 *l4_proto;
13861 
13862 	features = vlan_features_check(skb, features);
13863 	switch (vlan_get_protocol(skb)) {
13864 	case htons(ETH_P_IP):
13865 		if (!skb->encapsulation)
13866 			return features;
13867 		l4_proto = &ip_hdr(skb)->protocol;
13868 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13869 			return features;
13870 		break;
13871 	case htons(ETH_P_IPV6):
13872 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13873 				       &l4_proto))
13874 			break;
13875 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13876 			return features;
13877 		break;
13878 	}
13879 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13880 }
13881 
13882 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13883 			 u32 *reg_buf)
13884 {
13885 	struct hwrm_dbg_read_direct_output *resp;
13886 	struct hwrm_dbg_read_direct_input *req;
13887 	__le32 *dbg_reg_buf;
13888 	dma_addr_t mapping;
13889 	int rc, i;
13890 
13891 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13892 	if (rc)
13893 		return rc;
13894 
13895 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13896 					 &mapping);
13897 	if (!dbg_reg_buf) {
13898 		rc = -ENOMEM;
13899 		goto dbg_rd_reg_exit;
13900 	}
13901 
13902 	req->host_dest_addr = cpu_to_le64(mapping);
13903 
13904 	resp = hwrm_req_hold(bp, req);
13905 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13906 	req->read_len32 = cpu_to_le32(num_words);
13907 
13908 	rc = hwrm_req_send(bp, req);
13909 	if (rc || resp->error_code) {
13910 		rc = -EIO;
13911 		goto dbg_rd_reg_exit;
13912 	}
13913 	for (i = 0; i < num_words; i++)
13914 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13915 
13916 dbg_rd_reg_exit:
13917 	hwrm_req_drop(bp, req);
13918 	return rc;
13919 }
13920 
13921 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13922 				       u32 ring_id, u32 *prod, u32 *cons)
13923 {
13924 	struct hwrm_dbg_ring_info_get_output *resp;
13925 	struct hwrm_dbg_ring_info_get_input *req;
13926 	int rc;
13927 
13928 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13929 	if (rc)
13930 		return rc;
13931 
13932 	req->ring_type = ring_type;
13933 	req->fw_ring_id = cpu_to_le32(ring_id);
13934 	resp = hwrm_req_hold(bp, req);
13935 	rc = hwrm_req_send(bp, req);
13936 	if (!rc) {
13937 		*prod = le32_to_cpu(resp->producer_index);
13938 		*cons = le32_to_cpu(resp->consumer_index);
13939 	}
13940 	hwrm_req_drop(bp, req);
13941 	return rc;
13942 }
13943 
13944 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13945 {
13946 	struct bnxt_tx_ring_info *txr;
13947 	int i = bnapi->index, j;
13948 
13949 	bnxt_for_each_napi_tx(j, bnapi, txr)
13950 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13951 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13952 			    txr->tx_cons);
13953 }
13954 
13955 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13956 {
13957 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13958 	int i = bnapi->index;
13959 
13960 	if (!rxr)
13961 		return;
13962 
13963 	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",
13964 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13965 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13966 		    rxr->rx_sw_agg_prod);
13967 }
13968 
13969 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13970 {
13971 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13972 	int i = bnapi->index;
13973 
13974 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13975 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13976 }
13977 
13978 static void bnxt_dbg_dump_states(struct bnxt *bp)
13979 {
13980 	int i;
13981 	struct bnxt_napi *bnapi;
13982 
13983 	for (i = 0; i < bp->cp_nr_rings; i++) {
13984 		bnapi = bp->bnapi[i];
13985 		if (netif_msg_drv(bp)) {
13986 			bnxt_dump_tx_sw_state(bnapi);
13987 			bnxt_dump_rx_sw_state(bnapi);
13988 			bnxt_dump_cp_sw_state(bnapi);
13989 		}
13990 	}
13991 }
13992 
13993 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13994 {
13995 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13996 	struct hwrm_ring_reset_input *req;
13997 	struct bnxt_napi *bnapi = rxr->bnapi;
13998 	struct bnxt_cp_ring_info *cpr;
13999 	u16 cp_ring_id;
14000 	int rc;
14001 
14002 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
14003 	if (rc)
14004 		return rc;
14005 
14006 	cpr = &bnapi->cp_ring;
14007 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
14008 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
14009 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
14010 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
14011 	return hwrm_req_send_silent(bp, req);
14012 }
14013 
14014 static void bnxt_reset_task(struct bnxt *bp, bool silent)
14015 {
14016 	if (!silent)
14017 		bnxt_dbg_dump_states(bp);
14018 	if (netif_running(bp->dev)) {
14019 		bnxt_close_nic(bp, !silent, false);
14020 		bnxt_open_nic(bp, !silent, false);
14021 	}
14022 }
14023 
14024 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
14025 {
14026 	struct bnxt *bp = netdev_priv(dev);
14027 
14028 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
14029 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
14030 }
14031 
14032 static void bnxt_fw_health_check(struct bnxt *bp)
14033 {
14034 	struct bnxt_fw_health *fw_health = bp->fw_health;
14035 	struct pci_dev *pdev = bp->pdev;
14036 	u32 val;
14037 
14038 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14039 		return;
14040 
14041 	/* Make sure it is enabled before checking the tmr_counter. */
14042 	smp_rmb();
14043 	if (fw_health->tmr_counter) {
14044 		fw_health->tmr_counter--;
14045 		return;
14046 	}
14047 
14048 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14049 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
14050 		fw_health->arrests++;
14051 		goto fw_reset;
14052 	}
14053 
14054 	fw_health->last_fw_heartbeat = val;
14055 
14056 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14057 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
14058 		fw_health->discoveries++;
14059 		goto fw_reset;
14060 	}
14061 
14062 	fw_health->tmr_counter = fw_health->tmr_multiplier;
14063 	return;
14064 
14065 fw_reset:
14066 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
14067 }
14068 
14069 static void bnxt_timer(struct timer_list *t)
14070 {
14071 	struct bnxt *bp = timer_container_of(bp, t, timer);
14072 	struct net_device *dev = bp->dev;
14073 
14074 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
14075 		return;
14076 
14077 	if (atomic_read(&bp->intr_sem) != 0)
14078 		goto bnxt_restart_timer;
14079 
14080 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
14081 		bnxt_fw_health_check(bp);
14082 
14083 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
14084 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
14085 
14086 	if (bnxt_tc_flower_enabled(bp))
14087 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
14088 
14089 #ifdef CONFIG_RFS_ACCEL
14090 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
14091 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14092 #endif /*CONFIG_RFS_ACCEL*/
14093 
14094 	if (bp->link_info.phy_retry) {
14095 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
14096 			bp->link_info.phy_retry = false;
14097 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
14098 		} else {
14099 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
14100 		}
14101 	}
14102 
14103 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
14104 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
14105 
14106 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
14107 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
14108 
14109 bnxt_restart_timer:
14110 	mod_timer(&bp->timer, jiffies + bp->current_interval);
14111 }
14112 
14113 static void bnxt_lock_sp(struct bnxt *bp)
14114 {
14115 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
14116 	 * set.  If the device is being closed, bnxt_close() may be holding
14117 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14118 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14119 	 * instance lock.
14120 	 */
14121 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14122 	netdev_lock(bp->dev);
14123 }
14124 
14125 static void bnxt_unlock_sp(struct bnxt *bp)
14126 {
14127 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14128 	netdev_unlock(bp->dev);
14129 }
14130 
14131 /* Only called from bnxt_sp_task() */
14132 static void bnxt_reset(struct bnxt *bp, bool silent)
14133 {
14134 	bnxt_lock_sp(bp);
14135 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14136 		bnxt_reset_task(bp, silent);
14137 	bnxt_unlock_sp(bp);
14138 }
14139 
14140 /* Only called from bnxt_sp_task() */
14141 static void bnxt_rx_ring_reset(struct bnxt *bp)
14142 {
14143 	int i;
14144 
14145 	bnxt_lock_sp(bp);
14146 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14147 		bnxt_unlock_sp(bp);
14148 		return;
14149 	}
14150 	/* Disable and flush TPA before resetting the RX ring */
14151 	if (bp->flags & BNXT_FLAG_TPA)
14152 		bnxt_set_tpa(bp, false);
14153 	for (i = 0; i < bp->rx_nr_rings; i++) {
14154 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14155 		struct bnxt_cp_ring_info *cpr;
14156 		int rc;
14157 
14158 		if (!rxr->bnapi->in_reset)
14159 			continue;
14160 
14161 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14162 		if (rc) {
14163 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14164 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14165 			else
14166 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14167 					    rc);
14168 			bnxt_reset_task(bp, true);
14169 			break;
14170 		}
14171 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14172 		rxr->rx_prod = 0;
14173 		rxr->rx_agg_prod = 0;
14174 		rxr->rx_sw_agg_prod = 0;
14175 		rxr->rx_next_cons = 0;
14176 		rxr->bnapi->in_reset = false;
14177 		bnxt_alloc_one_rx_ring(bp, i);
14178 		cpr = &rxr->bnapi->cp_ring;
14179 		cpr->sw_stats->rx.rx_resets++;
14180 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14181 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14182 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14183 	}
14184 	if (bp->flags & BNXT_FLAG_TPA)
14185 		bnxt_set_tpa(bp, true);
14186 	bnxt_unlock_sp(bp);
14187 }
14188 
14189 static void bnxt_fw_fatal_close(struct bnxt *bp)
14190 {
14191 	bnxt_tx_disable(bp);
14192 	bnxt_disable_napi(bp);
14193 	bnxt_disable_int_sync(bp);
14194 	bnxt_free_irq(bp);
14195 	bnxt_clear_int_mode(bp);
14196 	pci_disable_device(bp->pdev);
14197 }
14198 
14199 static void bnxt_fw_reset_close(struct bnxt *bp)
14200 {
14201 	/* When firmware is in fatal state, quiesce device and disable
14202 	 * bus master to prevent any potential bad DMAs before freeing
14203 	 * kernel memory.
14204 	 */
14205 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14206 		u16 val = 0;
14207 
14208 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14209 		if (val == 0xffff)
14210 			bp->fw_reset_min_dsecs = 0;
14211 		bnxt_fw_fatal_close(bp);
14212 	}
14213 	__bnxt_close_nic(bp, true, false);
14214 	bnxt_vf_reps_free(bp);
14215 	bnxt_clear_int_mode(bp);
14216 	bnxt_hwrm_func_drv_unrgtr(bp);
14217 	if (pci_is_enabled(bp->pdev))
14218 		pci_disable_device(bp->pdev);
14219 	bnxt_free_ctx_mem(bp, false);
14220 }
14221 
14222 static bool is_bnxt_fw_ok(struct bnxt *bp)
14223 {
14224 	struct bnxt_fw_health *fw_health = bp->fw_health;
14225 	bool no_heartbeat = false, has_reset = false;
14226 	u32 val;
14227 
14228 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14229 	if (val == fw_health->last_fw_heartbeat)
14230 		no_heartbeat = true;
14231 
14232 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14233 	if (val != fw_health->last_fw_reset_cnt)
14234 		has_reset = true;
14235 
14236 	if (!no_heartbeat && has_reset)
14237 		return true;
14238 
14239 	return false;
14240 }
14241 
14242 /* netdev instance lock is acquired before calling this function */
14243 static void bnxt_force_fw_reset(struct bnxt *bp)
14244 {
14245 	struct bnxt_fw_health *fw_health = bp->fw_health;
14246 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14247 	u32 wait_dsecs;
14248 
14249 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14250 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14251 		return;
14252 
14253 	/* we have to serialize with bnxt_refclk_read()*/
14254 	if (ptp) {
14255 		unsigned long flags;
14256 
14257 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14258 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14259 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14260 	} else {
14261 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14262 	}
14263 	bnxt_fw_reset_close(bp);
14264 	wait_dsecs = fw_health->master_func_wait_dsecs;
14265 	if (fw_health->primary) {
14266 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14267 			wait_dsecs = 0;
14268 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14269 	} else {
14270 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14271 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14272 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14273 	}
14274 
14275 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14276 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14277 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14278 }
14279 
14280 void bnxt_fw_exception(struct bnxt *bp)
14281 {
14282 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14283 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14284 	bnxt_ulp_stop(bp);
14285 	bnxt_lock_sp(bp);
14286 	bnxt_force_fw_reset(bp);
14287 	bnxt_unlock_sp(bp);
14288 }
14289 
14290 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14291  * < 0 on error.
14292  */
14293 static int bnxt_get_registered_vfs(struct bnxt *bp)
14294 {
14295 #ifdef CONFIG_BNXT_SRIOV
14296 	int rc;
14297 
14298 	if (!BNXT_PF(bp))
14299 		return 0;
14300 
14301 	rc = bnxt_hwrm_func_qcfg(bp);
14302 	if (rc) {
14303 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14304 		return rc;
14305 	}
14306 	if (bp->pf.registered_vfs)
14307 		return bp->pf.registered_vfs;
14308 	if (bp->sriov_cfg)
14309 		return 1;
14310 #endif
14311 	return 0;
14312 }
14313 
14314 void bnxt_fw_reset(struct bnxt *bp)
14315 {
14316 	bnxt_ulp_stop(bp);
14317 	bnxt_lock_sp(bp);
14318 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14319 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14320 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14321 		int n = 0, tmo;
14322 
14323 		/* we have to serialize with bnxt_refclk_read()*/
14324 		if (ptp) {
14325 			unsigned long flags;
14326 
14327 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14328 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14329 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14330 		} else {
14331 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14332 		}
14333 		if (bp->pf.active_vfs &&
14334 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14335 			n = bnxt_get_registered_vfs(bp);
14336 		if (n < 0) {
14337 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14338 				   n);
14339 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14340 			netif_close(bp->dev);
14341 			goto fw_reset_exit;
14342 		} else if (n > 0) {
14343 			u16 vf_tmo_dsecs = n * 10;
14344 
14345 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14346 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14347 			bp->fw_reset_state =
14348 				BNXT_FW_RESET_STATE_POLL_VF;
14349 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14350 			goto fw_reset_exit;
14351 		}
14352 		bnxt_fw_reset_close(bp);
14353 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14354 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14355 			tmo = HZ / 10;
14356 		} else {
14357 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14358 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14359 		}
14360 		bnxt_queue_fw_reset_work(bp, tmo);
14361 	}
14362 fw_reset_exit:
14363 	bnxt_unlock_sp(bp);
14364 }
14365 
14366 static void bnxt_chk_missed_irq(struct bnxt *bp)
14367 {
14368 	int i;
14369 
14370 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14371 		return;
14372 
14373 	for (i = 0; i < bp->cp_nr_rings; i++) {
14374 		struct bnxt_napi *bnapi = bp->bnapi[i];
14375 		struct bnxt_cp_ring_info *cpr;
14376 		u32 fw_ring_id;
14377 		int j;
14378 
14379 		if (!bnapi)
14380 			continue;
14381 
14382 		cpr = &bnapi->cp_ring;
14383 		for (j = 0; j < cpr->cp_ring_count; j++) {
14384 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14385 			u32 val[2];
14386 
14387 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14388 				continue;
14389 
14390 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14391 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14392 				continue;
14393 			}
14394 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14395 			bnxt_dbg_hwrm_ring_info_get(bp,
14396 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14397 				fw_ring_id, &val[0], &val[1]);
14398 			cpr->sw_stats->cmn.missed_irqs++;
14399 		}
14400 	}
14401 }
14402 
14403 static void bnxt_cfg_ntp_filters(struct bnxt *);
14404 
14405 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14406 {
14407 	struct bnxt_link_info *link_info = &bp->link_info;
14408 
14409 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14410 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14411 		if (bp->hwrm_spec_code >= 0x10201) {
14412 			if (link_info->auto_pause_setting &
14413 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14414 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14415 		} else {
14416 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14417 		}
14418 		bnxt_set_auto_speed(link_info);
14419 	} else {
14420 		bnxt_set_force_speed(link_info);
14421 		link_info->req_duplex = link_info->duplex_setting;
14422 	}
14423 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14424 		link_info->req_flow_ctrl =
14425 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14426 	else
14427 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14428 }
14429 
14430 static void bnxt_fw_echo_reply(struct bnxt *bp)
14431 {
14432 	struct bnxt_fw_health *fw_health = bp->fw_health;
14433 	struct hwrm_func_echo_response_input *req;
14434 	int rc;
14435 
14436 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14437 	if (rc)
14438 		return;
14439 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14440 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14441 	hwrm_req_send(bp, req);
14442 }
14443 
14444 static void bnxt_ulp_restart(struct bnxt *bp)
14445 {
14446 	bnxt_ulp_stop(bp);
14447 	bnxt_ulp_start(bp, 0);
14448 }
14449 
14450 static void bnxt_sp_task(struct work_struct *work)
14451 {
14452 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14453 
14454 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14455 	smp_mb__after_atomic();
14456 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14457 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14458 		return;
14459 	}
14460 
14461 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14462 		bnxt_ulp_restart(bp);
14463 		bnxt_reenable_sriov(bp);
14464 	}
14465 
14466 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14467 		bnxt_cfg_rx_mode(bp);
14468 
14469 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14470 		bnxt_cfg_ntp_filters(bp);
14471 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14472 		bnxt_hwrm_exec_fwd_req(bp);
14473 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14474 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14475 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14476 		bnxt_hwrm_port_qstats(bp, 0);
14477 		bnxt_hwrm_port_qstats_ext(bp, 0);
14478 		bnxt_accumulate_all_stats(bp);
14479 	}
14480 
14481 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14482 		int rc;
14483 
14484 		mutex_lock(&bp->link_lock);
14485 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14486 				       &bp->sp_event))
14487 			bnxt_hwrm_phy_qcaps(bp);
14488 
14489 		rc = bnxt_update_link(bp, true);
14490 		if (rc)
14491 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14492 				   rc);
14493 
14494 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14495 				       &bp->sp_event))
14496 			bnxt_init_ethtool_link_settings(bp);
14497 		mutex_unlock(&bp->link_lock);
14498 	}
14499 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14500 		int rc;
14501 
14502 		mutex_lock(&bp->link_lock);
14503 		rc = bnxt_update_phy_setting(bp);
14504 		mutex_unlock(&bp->link_lock);
14505 		if (rc) {
14506 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14507 		} else {
14508 			bp->link_info.phy_retry = false;
14509 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14510 		}
14511 	}
14512 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14513 		mutex_lock(&bp->link_lock);
14514 		bnxt_get_port_module_status(bp);
14515 		mutex_unlock(&bp->link_lock);
14516 	}
14517 
14518 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14519 		bnxt_tc_flow_stats_work(bp);
14520 
14521 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14522 		bnxt_chk_missed_irq(bp);
14523 
14524 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14525 		bnxt_fw_echo_reply(bp);
14526 
14527 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14528 		bnxt_hwmon_notify_event(bp);
14529 
14530 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14531 	 * must be the last functions to be called before exiting.
14532 	 */
14533 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14534 		bnxt_reset(bp, false);
14535 
14536 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14537 		bnxt_reset(bp, true);
14538 
14539 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14540 		bnxt_rx_ring_reset(bp);
14541 
14542 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14543 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14544 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14545 			bnxt_devlink_health_fw_report(bp);
14546 		else
14547 			bnxt_fw_reset(bp);
14548 	}
14549 
14550 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14551 		if (!is_bnxt_fw_ok(bp))
14552 			bnxt_devlink_health_fw_report(bp);
14553 	}
14554 
14555 	smp_mb__before_atomic();
14556 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14557 }
14558 
14559 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14560 				int *max_cp);
14561 
14562 /* Under netdev instance lock */
14563 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14564 		     int tx_xdp)
14565 {
14566 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14567 	struct bnxt_hw_rings hwr = {0};
14568 	int rx_rings = rx;
14569 	int rc;
14570 
14571 	if (tcs)
14572 		tx_sets = tcs;
14573 
14574 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14575 
14576 	if (max_rx < rx_rings)
14577 		return -ENOMEM;
14578 
14579 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14580 		rx_rings <<= 1;
14581 
14582 	hwr.rx = rx_rings;
14583 	hwr.tx = tx * tx_sets + tx_xdp;
14584 	if (max_tx < hwr.tx)
14585 		return -ENOMEM;
14586 
14587 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14588 
14589 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14590 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14591 	if (max_cp < hwr.cp)
14592 		return -ENOMEM;
14593 	hwr.stat = hwr.cp;
14594 	if (BNXT_NEW_RM(bp)) {
14595 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14596 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14597 		hwr.grp = rx;
14598 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14599 	}
14600 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14601 		hwr.cp_p5 = hwr.tx + rx;
14602 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14603 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14604 		if (!bnxt_ulp_registered(bp->edev)) {
14605 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14606 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14607 		}
14608 		if (hwr.cp > bp->total_irqs) {
14609 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14610 
14611 			if (total_msix < hwr.cp) {
14612 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14613 					    hwr.cp, total_msix);
14614 				rc = -ENOSPC;
14615 			}
14616 		}
14617 	}
14618 	return rc;
14619 }
14620 
14621 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14622 {
14623 	if (bp->bar2) {
14624 		pci_iounmap(pdev, bp->bar2);
14625 		bp->bar2 = NULL;
14626 	}
14627 
14628 	if (bp->bar1) {
14629 		pci_iounmap(pdev, bp->bar1);
14630 		bp->bar1 = NULL;
14631 	}
14632 
14633 	if (bp->bar0) {
14634 		pci_iounmap(pdev, bp->bar0);
14635 		bp->bar0 = NULL;
14636 	}
14637 }
14638 
14639 static void bnxt_cleanup_pci(struct bnxt *bp)
14640 {
14641 	bnxt_unmap_bars(bp, bp->pdev);
14642 	pci_release_regions(bp->pdev);
14643 	if (pci_is_enabled(bp->pdev))
14644 		pci_disable_device(bp->pdev);
14645 }
14646 
14647 static void bnxt_init_dflt_coal(struct bnxt *bp)
14648 {
14649 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14650 	struct bnxt_coal *coal;
14651 	u16 flags = 0;
14652 
14653 	if (coal_cap->cmpl_params &
14654 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14655 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14656 
14657 	/* Tick values in micro seconds.
14658 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14659 	 */
14660 	coal = &bp->rx_coal;
14661 	coal->coal_ticks = 10;
14662 	coal->coal_bufs = 30;
14663 	coal->coal_ticks_irq = 1;
14664 	coal->coal_bufs_irq = 2;
14665 	coal->idle_thresh = 50;
14666 	coal->bufs_per_record = 2;
14667 	coal->budget = 64;		/* NAPI budget */
14668 	coal->flags = flags;
14669 
14670 	coal = &bp->tx_coal;
14671 	coal->coal_ticks = 28;
14672 	coal->coal_bufs = 30;
14673 	coal->coal_ticks_irq = 2;
14674 	coal->coal_bufs_irq = 2;
14675 	coal->bufs_per_record = 1;
14676 	coal->flags = flags;
14677 
14678 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14679 }
14680 
14681 /* FW that pre-reserves 1 VNIC per function */
14682 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14683 {
14684 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14685 
14686 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14687 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14688 		return true;
14689 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14690 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14691 		return true;
14692 	return false;
14693 }
14694 
14695 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14696 {
14697 	int rc;
14698 
14699 	bp->fw_cap = 0;
14700 	rc = bnxt_hwrm_ver_get(bp);
14701 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14702 	 * so wait before continuing with recovery.
14703 	 */
14704 	if (rc)
14705 		msleep(100);
14706 	bnxt_try_map_fw_health_reg(bp);
14707 	if (rc) {
14708 		rc = bnxt_try_recover_fw(bp);
14709 		if (rc)
14710 			return rc;
14711 		rc = bnxt_hwrm_ver_get(bp);
14712 		if (rc)
14713 			return rc;
14714 	}
14715 
14716 	bnxt_nvm_cfg_ver_get(bp);
14717 
14718 	rc = bnxt_hwrm_func_reset(bp);
14719 	if (rc)
14720 		return -ENODEV;
14721 
14722 	bnxt_hwrm_fw_set_time(bp);
14723 	return 0;
14724 }
14725 
14726 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14727 {
14728 	int rc;
14729 
14730 	/* Get the MAX capabilities for this function */
14731 	rc = bnxt_hwrm_func_qcaps(bp);
14732 	if (rc) {
14733 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14734 			   rc);
14735 		return -ENODEV;
14736 	}
14737 
14738 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14739 	if (rc)
14740 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14741 			    rc);
14742 
14743 	if (bnxt_alloc_fw_health(bp)) {
14744 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14745 	} else {
14746 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14747 		if (rc)
14748 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14749 				    rc);
14750 	}
14751 
14752 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14753 	if (rc)
14754 		return -ENODEV;
14755 
14756 	rc = bnxt_alloc_crash_dump_mem(bp);
14757 	if (rc)
14758 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14759 			    rc);
14760 	if (!rc) {
14761 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14762 		if (rc) {
14763 			bnxt_free_crash_dump_mem(bp);
14764 			netdev_warn(bp->dev,
14765 				    "hwrm crash dump mem failure rc: %d\n", rc);
14766 		}
14767 	}
14768 
14769 	if (bnxt_fw_pre_resv_vnics(bp))
14770 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14771 
14772 	bnxt_hwrm_func_qcfg(bp);
14773 	bnxt_hwrm_vnic_qcaps(bp);
14774 	bnxt_hwrm_port_led_qcaps(bp);
14775 	bnxt_ethtool_init(bp);
14776 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14777 		__bnxt_hwrm_ptp_qcfg(bp);
14778 	bnxt_dcb_init(bp);
14779 	bnxt_hwmon_init(bp);
14780 	return 0;
14781 }
14782 
14783 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14784 {
14785 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14786 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14787 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14788 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14789 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14790 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14791 		bp->rss_hash_delta = bp->rss_hash_cfg;
14792 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14793 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14794 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14795 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14796 	}
14797 }
14798 
14799 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14800 {
14801 	struct net_device *dev = bp->dev;
14802 
14803 	dev->hw_features &= ~NETIF_F_NTUPLE;
14804 	dev->features &= ~NETIF_F_NTUPLE;
14805 	bp->flags &= ~BNXT_FLAG_RFS;
14806 	if (bnxt_rfs_supported(bp)) {
14807 		dev->hw_features |= NETIF_F_NTUPLE;
14808 		if (bnxt_rfs_capable(bp, false)) {
14809 			bp->flags |= BNXT_FLAG_RFS;
14810 			dev->features |= NETIF_F_NTUPLE;
14811 		}
14812 	}
14813 }
14814 
14815 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14816 {
14817 	struct pci_dev *pdev = bp->pdev;
14818 
14819 	bnxt_set_dflt_rss_hash_type(bp);
14820 	bnxt_set_dflt_rfs(bp);
14821 
14822 	bnxt_get_wol_settings(bp);
14823 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14824 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14825 	else
14826 		device_set_wakeup_capable(&pdev->dev, false);
14827 
14828 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14829 	bnxt_hwrm_coal_params_qcaps(bp);
14830 }
14831 
14832 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14833 
14834 int bnxt_fw_init_one(struct bnxt *bp)
14835 {
14836 	int rc;
14837 
14838 	rc = bnxt_fw_init_one_p1(bp);
14839 	if (rc) {
14840 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14841 		return rc;
14842 	}
14843 	rc = bnxt_fw_init_one_p2(bp);
14844 	if (rc) {
14845 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14846 		return rc;
14847 	}
14848 	rc = bnxt_probe_phy(bp, false);
14849 	if (rc)
14850 		return rc;
14851 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14852 	if (rc)
14853 		return rc;
14854 
14855 	bnxt_fw_init_one_p3(bp);
14856 	return 0;
14857 }
14858 
14859 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14860 {
14861 	struct bnxt_fw_health *fw_health = bp->fw_health;
14862 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14863 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14864 	u32 reg_type, reg_off, delay_msecs;
14865 
14866 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14867 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14868 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14869 	switch (reg_type) {
14870 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14871 		pci_write_config_dword(bp->pdev, reg_off, val);
14872 		break;
14873 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14874 		writel(reg_off & BNXT_GRC_BASE_MASK,
14875 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14876 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14877 		fallthrough;
14878 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14879 		writel(val, bp->bar0 + reg_off);
14880 		break;
14881 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14882 		writel(val, bp->bar1 + reg_off);
14883 		break;
14884 	}
14885 	if (delay_msecs) {
14886 		pci_read_config_dword(bp->pdev, 0, &val);
14887 		msleep(delay_msecs);
14888 	}
14889 }
14890 
14891 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14892 {
14893 	struct hwrm_func_qcfg_output *resp;
14894 	struct hwrm_func_qcfg_input *req;
14895 	bool result = true; /* firmware will enforce if unknown */
14896 
14897 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14898 		return result;
14899 
14900 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14901 		return result;
14902 
14903 	req->fid = cpu_to_le16(0xffff);
14904 	resp = hwrm_req_hold(bp, req);
14905 	if (!hwrm_req_send(bp, req))
14906 		result = !!(le16_to_cpu(resp->flags) &
14907 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14908 	hwrm_req_drop(bp, req);
14909 	return result;
14910 }
14911 
14912 static void bnxt_reset_all(struct bnxt *bp)
14913 {
14914 	struct bnxt_fw_health *fw_health = bp->fw_health;
14915 	int i, rc;
14916 
14917 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14918 		bnxt_fw_reset_via_optee(bp);
14919 		bp->fw_reset_timestamp = jiffies;
14920 		return;
14921 	}
14922 
14923 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14924 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14925 			bnxt_fw_reset_writel(bp, i);
14926 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14927 		struct hwrm_fw_reset_input *req;
14928 
14929 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14930 		if (!rc) {
14931 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14932 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14933 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14934 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14935 			rc = hwrm_req_send(bp, req);
14936 		}
14937 		if (rc != -ENODEV)
14938 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14939 	}
14940 	bp->fw_reset_timestamp = jiffies;
14941 }
14942 
14943 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14944 {
14945 	return time_after(jiffies, bp->fw_reset_timestamp +
14946 			  (bp->fw_reset_max_dsecs * HZ / 10));
14947 }
14948 
14949 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14950 {
14951 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14952 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14953 		bnxt_dl_health_fw_status_update(bp, false);
14954 	bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT;
14955 	netif_close(bp->dev);
14956 }
14957 
14958 static void bnxt_fw_reset_task(struct work_struct *work)
14959 {
14960 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14961 	int rc = 0;
14962 
14963 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14964 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14965 		return;
14966 	}
14967 
14968 	switch (bp->fw_reset_state) {
14969 	case BNXT_FW_RESET_STATE_POLL_VF: {
14970 		int n = bnxt_get_registered_vfs(bp);
14971 		int tmo;
14972 
14973 		if (n < 0) {
14974 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14975 				   n, jiffies_to_msecs(jiffies -
14976 				   bp->fw_reset_timestamp));
14977 			goto fw_reset_abort;
14978 		} else if (n > 0) {
14979 			if (bnxt_fw_reset_timeout(bp)) {
14980 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14981 				bp->fw_reset_state = 0;
14982 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14983 					   n);
14984 				goto ulp_start;
14985 			}
14986 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14987 			return;
14988 		}
14989 		bp->fw_reset_timestamp = jiffies;
14990 		netdev_lock(bp->dev);
14991 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14992 			bnxt_fw_reset_abort(bp, rc);
14993 			netdev_unlock(bp->dev);
14994 			goto ulp_start;
14995 		}
14996 		bnxt_fw_reset_close(bp);
14997 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14998 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14999 			tmo = HZ / 10;
15000 		} else {
15001 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15002 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
15003 		}
15004 		netdev_unlock(bp->dev);
15005 		bnxt_queue_fw_reset_work(bp, tmo);
15006 		return;
15007 	}
15008 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
15009 		u32 val;
15010 
15011 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15012 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
15013 		    !bnxt_fw_reset_timeout(bp)) {
15014 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15015 			return;
15016 		}
15017 
15018 		if (!bp->fw_health->primary) {
15019 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
15020 
15021 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15022 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
15023 			return;
15024 		}
15025 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
15026 	}
15027 		fallthrough;
15028 	case BNXT_FW_RESET_STATE_RESET_FW:
15029 		bnxt_reset_all(bp);
15030 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15031 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
15032 		return;
15033 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
15034 		bnxt_inv_fw_health_reg(bp);
15035 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
15036 		    !bp->fw_reset_min_dsecs) {
15037 			u16 val;
15038 
15039 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
15040 			if (val == 0xffff) {
15041 				if (bnxt_fw_reset_timeout(bp)) {
15042 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
15043 					rc = -ETIMEDOUT;
15044 					goto fw_reset_abort;
15045 				}
15046 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
15047 				return;
15048 			}
15049 		}
15050 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
15051 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
15052 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
15053 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
15054 			bnxt_dl_remote_reload(bp);
15055 		if (pci_enable_device(bp->pdev)) {
15056 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
15057 			rc = -ENODEV;
15058 			goto fw_reset_abort;
15059 		}
15060 		pci_set_master(bp->pdev);
15061 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
15062 		fallthrough;
15063 	case BNXT_FW_RESET_STATE_POLL_FW:
15064 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
15065 		rc = bnxt_hwrm_poll(bp);
15066 		if (rc) {
15067 			if (bnxt_fw_reset_timeout(bp)) {
15068 				netdev_err(bp->dev, "Firmware reset aborted\n");
15069 				goto fw_reset_abort_status;
15070 			}
15071 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15072 			return;
15073 		}
15074 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
15075 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
15076 		fallthrough;
15077 	case BNXT_FW_RESET_STATE_OPENING:
15078 		while (!netdev_trylock(bp->dev)) {
15079 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15080 			return;
15081 		}
15082 		rc = bnxt_open(bp->dev);
15083 		if (rc) {
15084 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
15085 			bnxt_fw_reset_abort(bp, rc);
15086 			netdev_unlock(bp->dev);
15087 			goto ulp_start;
15088 		}
15089 
15090 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
15091 		    bp->fw_health->enabled) {
15092 			bp->fw_health->last_fw_reset_cnt =
15093 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
15094 		}
15095 		bp->fw_reset_state = 0;
15096 		/* Make sure fw_reset_state is 0 before clearing the flag */
15097 		smp_mb__before_atomic();
15098 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15099 		bnxt_ptp_reapply_pps(bp);
15100 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
15101 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
15102 			bnxt_dl_health_fw_recovery_done(bp);
15103 			bnxt_dl_health_fw_status_update(bp, true);
15104 		}
15105 		netdev_unlock(bp->dev);
15106 		bnxt_ulp_start(bp, 0);
15107 		bnxt_reenable_sriov(bp);
15108 		netdev_lock(bp->dev);
15109 		bnxt_vf_reps_alloc(bp);
15110 		bnxt_vf_reps_open(bp);
15111 		netdev_unlock(bp->dev);
15112 		break;
15113 	}
15114 	return;
15115 
15116 fw_reset_abort_status:
15117 	if (bp->fw_health->status_reliable ||
15118 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15119 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15120 
15121 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15122 	}
15123 fw_reset_abort:
15124 	netdev_lock(bp->dev);
15125 	bnxt_fw_reset_abort(bp, rc);
15126 	netdev_unlock(bp->dev);
15127 ulp_start:
15128 	bnxt_ulp_start(bp, rc);
15129 }
15130 
15131 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15132 {
15133 	int rc;
15134 	struct bnxt *bp = netdev_priv(dev);
15135 
15136 	SET_NETDEV_DEV(dev, &pdev->dev);
15137 
15138 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15139 	rc = pci_enable_device(pdev);
15140 	if (rc) {
15141 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15142 		goto init_err;
15143 	}
15144 
15145 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15146 		dev_err(&pdev->dev,
15147 			"Cannot find PCI device base address, aborting\n");
15148 		rc = -ENODEV;
15149 		goto init_err_disable;
15150 	}
15151 
15152 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15153 	if (rc) {
15154 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15155 		goto init_err_disable;
15156 	}
15157 
15158 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
15159 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
15160 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15161 		rc = -EIO;
15162 		goto init_err_release;
15163 	}
15164 
15165 	pci_set_master(pdev);
15166 
15167 	bp->dev = dev;
15168 	bp->pdev = pdev;
15169 
15170 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15171 	 * determines the BAR size.
15172 	 */
15173 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15174 	if (!bp->bar0) {
15175 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15176 		rc = -ENOMEM;
15177 		goto init_err_release;
15178 	}
15179 
15180 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15181 	if (!bp->bar2) {
15182 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15183 		rc = -ENOMEM;
15184 		goto init_err_release;
15185 	}
15186 
15187 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15188 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15189 
15190 	spin_lock_init(&bp->ntp_fltr_lock);
15191 #if BITS_PER_LONG == 32
15192 	spin_lock_init(&bp->db_lock);
15193 #endif
15194 
15195 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15196 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15197 
15198 	timer_setup(&bp->timer, bnxt_timer, 0);
15199 	bp->current_interval = BNXT_TIMER_INTERVAL;
15200 
15201 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15202 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15203 
15204 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15205 	return 0;
15206 
15207 init_err_release:
15208 	bnxt_unmap_bars(bp, pdev);
15209 	pci_release_regions(pdev);
15210 
15211 init_err_disable:
15212 	pci_disable_device(pdev);
15213 
15214 init_err:
15215 	return rc;
15216 }
15217 
15218 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15219 {
15220 	struct sockaddr *addr = p;
15221 	struct bnxt *bp = netdev_priv(dev);
15222 	int rc = 0;
15223 
15224 	netdev_assert_locked(dev);
15225 
15226 	if (!is_valid_ether_addr(addr->sa_data))
15227 		return -EADDRNOTAVAIL;
15228 
15229 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15230 		return 0;
15231 
15232 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15233 	if (rc)
15234 		return rc;
15235 
15236 	eth_hw_addr_set(dev, addr->sa_data);
15237 	bnxt_clear_usr_fltrs(bp, true);
15238 	if (netif_running(dev)) {
15239 		bnxt_close_nic(bp, false, false);
15240 		rc = bnxt_open_nic(bp, false, false);
15241 	}
15242 
15243 	return rc;
15244 }
15245 
15246 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15247 {
15248 	struct bnxt *bp = netdev_priv(dev);
15249 
15250 	netdev_assert_locked(dev);
15251 
15252 	if (netif_running(dev))
15253 		bnxt_close_nic(bp, true, false);
15254 
15255 	WRITE_ONCE(dev->mtu, new_mtu);
15256 
15257 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15258 	 * program is attached.  We need to set the AGG rings settings and
15259 	 * rx_skb_func accordingly.
15260 	 */
15261 	if (READ_ONCE(bp->xdp_prog))
15262 		bnxt_set_rx_skb_mode(bp, true);
15263 
15264 	bnxt_set_ring_params(bp);
15265 
15266 	if (netif_running(dev))
15267 		return bnxt_open_nic(bp, true, false);
15268 
15269 	return 0;
15270 }
15271 
15272 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15273 {
15274 	struct bnxt *bp = netdev_priv(dev);
15275 	bool sh = false;
15276 	int rc, tx_cp;
15277 
15278 	if (tc > bp->max_tc) {
15279 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15280 			   tc, bp->max_tc);
15281 		return -EINVAL;
15282 	}
15283 
15284 	if (bp->num_tc == tc)
15285 		return 0;
15286 
15287 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15288 		sh = true;
15289 
15290 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15291 			      sh, tc, bp->tx_nr_rings_xdp);
15292 	if (rc)
15293 		return rc;
15294 
15295 	/* Needs to close the device and do hw resource re-allocations */
15296 	if (netif_running(bp->dev))
15297 		bnxt_close_nic(bp, true, false);
15298 
15299 	if (tc) {
15300 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15301 		netdev_set_num_tc(dev, tc);
15302 		bp->num_tc = tc;
15303 	} else {
15304 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15305 		netdev_reset_tc(dev);
15306 		bp->num_tc = 0;
15307 	}
15308 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15309 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15310 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15311 			       tx_cp + bp->rx_nr_rings;
15312 
15313 	if (netif_running(bp->dev))
15314 		return bnxt_open_nic(bp, true, false);
15315 
15316 	return 0;
15317 }
15318 
15319 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15320 				  void *cb_priv)
15321 {
15322 	struct bnxt *bp = cb_priv;
15323 
15324 	if (!bnxt_tc_flower_enabled(bp) ||
15325 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15326 		return -EOPNOTSUPP;
15327 
15328 	switch (type) {
15329 	case TC_SETUP_CLSFLOWER:
15330 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15331 	default:
15332 		return -EOPNOTSUPP;
15333 	}
15334 }
15335 
15336 LIST_HEAD(bnxt_block_cb_list);
15337 
15338 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15339 			 void *type_data)
15340 {
15341 	struct bnxt *bp = netdev_priv(dev);
15342 
15343 	switch (type) {
15344 	case TC_SETUP_BLOCK:
15345 		return flow_block_cb_setup_simple(type_data,
15346 						  &bnxt_block_cb_list,
15347 						  bnxt_setup_tc_block_cb,
15348 						  bp, bp, true);
15349 	case TC_SETUP_QDISC_MQPRIO: {
15350 		struct tc_mqprio_qopt *mqprio = type_data;
15351 
15352 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15353 
15354 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15355 	}
15356 	default:
15357 		return -EOPNOTSUPP;
15358 	}
15359 }
15360 
15361 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15362 			    const struct sk_buff *skb)
15363 {
15364 	struct bnxt_vnic_info *vnic;
15365 
15366 	if (skb)
15367 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15368 
15369 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15370 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15371 }
15372 
15373 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15374 			   u32 idx)
15375 {
15376 	struct hlist_head *head;
15377 	int bit_id;
15378 
15379 	spin_lock_bh(&bp->ntp_fltr_lock);
15380 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15381 	if (bit_id < 0) {
15382 		spin_unlock_bh(&bp->ntp_fltr_lock);
15383 		return -ENOMEM;
15384 	}
15385 
15386 	fltr->base.sw_id = (u16)bit_id;
15387 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15388 	fltr->base.flags |= BNXT_ACT_RING_DST;
15389 	head = &bp->ntp_fltr_hash_tbl[idx];
15390 	hlist_add_head_rcu(&fltr->base.hash, head);
15391 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15392 	bnxt_insert_usr_fltr(bp, &fltr->base);
15393 	bp->ntp_fltr_count++;
15394 	spin_unlock_bh(&bp->ntp_fltr_lock);
15395 	return 0;
15396 }
15397 
15398 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15399 			    struct bnxt_ntuple_filter *f2)
15400 {
15401 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15402 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15403 	struct flow_keys *keys1 = &f1->fkeys;
15404 	struct flow_keys *keys2 = &f2->fkeys;
15405 
15406 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15407 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15408 		return false;
15409 
15410 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15411 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15412 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15413 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15414 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15415 			return false;
15416 	} else {
15417 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15418 				     &keys2->addrs.v6addrs.src) ||
15419 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15420 				     &masks2->addrs.v6addrs.src) ||
15421 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15422 				     &keys2->addrs.v6addrs.dst) ||
15423 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15424 				     &masks2->addrs.v6addrs.dst))
15425 			return false;
15426 	}
15427 
15428 	return keys1->ports.src == keys2->ports.src &&
15429 	       masks1->ports.src == masks2->ports.src &&
15430 	       keys1->ports.dst == keys2->ports.dst &&
15431 	       masks1->ports.dst == masks2->ports.dst &&
15432 	       keys1->control.flags == keys2->control.flags &&
15433 	       f1->l2_fltr == f2->l2_fltr;
15434 }
15435 
15436 struct bnxt_ntuple_filter *
15437 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15438 				struct bnxt_ntuple_filter *fltr, u32 idx)
15439 {
15440 	struct bnxt_ntuple_filter *f;
15441 	struct hlist_head *head;
15442 
15443 	head = &bp->ntp_fltr_hash_tbl[idx];
15444 	hlist_for_each_entry_rcu(f, head, base.hash) {
15445 		if (bnxt_fltr_match(f, fltr))
15446 			return f;
15447 	}
15448 	return NULL;
15449 }
15450 
15451 #ifdef CONFIG_RFS_ACCEL
15452 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15453 			      u16 rxq_index, u32 flow_id)
15454 {
15455 	struct bnxt *bp = netdev_priv(dev);
15456 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15457 	struct flow_keys *fkeys;
15458 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15459 	struct bnxt_l2_filter *l2_fltr;
15460 	int rc = 0, idx;
15461 	u32 flags;
15462 
15463 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15464 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15465 		atomic_inc(&l2_fltr->refcnt);
15466 	} else {
15467 		struct bnxt_l2_key key;
15468 
15469 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15470 		key.vlan = 0;
15471 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15472 		if (!l2_fltr)
15473 			return -EINVAL;
15474 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15475 			bnxt_del_l2_filter(bp, l2_fltr);
15476 			return -EINVAL;
15477 		}
15478 	}
15479 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
15480 	if (!new_fltr) {
15481 		bnxt_del_l2_filter(bp, l2_fltr);
15482 		return -ENOMEM;
15483 	}
15484 
15485 	fkeys = &new_fltr->fkeys;
15486 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15487 		rc = -EPROTONOSUPPORT;
15488 		goto err_free;
15489 	}
15490 
15491 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15492 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15493 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15494 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15495 		rc = -EPROTONOSUPPORT;
15496 		goto err_free;
15497 	}
15498 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15499 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15500 		if (bp->hwrm_spec_code < 0x10601) {
15501 			rc = -EPROTONOSUPPORT;
15502 			goto err_free;
15503 		}
15504 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15505 	}
15506 	flags = fkeys->control.flags;
15507 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15508 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15509 		rc = -EPROTONOSUPPORT;
15510 		goto err_free;
15511 	}
15512 	new_fltr->l2_fltr = l2_fltr;
15513 
15514 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15515 	rcu_read_lock();
15516 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15517 	if (fltr) {
15518 		rc = fltr->base.sw_id;
15519 		rcu_read_unlock();
15520 		goto err_free;
15521 	}
15522 	rcu_read_unlock();
15523 
15524 	new_fltr->flow_id = flow_id;
15525 	new_fltr->base.rxq = rxq_index;
15526 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15527 	if (!rc) {
15528 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15529 		return new_fltr->base.sw_id;
15530 	}
15531 
15532 err_free:
15533 	bnxt_del_l2_filter(bp, l2_fltr);
15534 	kfree(new_fltr);
15535 	return rc;
15536 }
15537 #endif
15538 
15539 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15540 {
15541 	spin_lock_bh(&bp->ntp_fltr_lock);
15542 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15543 		spin_unlock_bh(&bp->ntp_fltr_lock);
15544 		return;
15545 	}
15546 	hlist_del_rcu(&fltr->base.hash);
15547 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15548 	bp->ntp_fltr_count--;
15549 	spin_unlock_bh(&bp->ntp_fltr_lock);
15550 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15551 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15552 	kfree_rcu(fltr, base.rcu);
15553 }
15554 
15555 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15556 {
15557 #ifdef CONFIG_RFS_ACCEL
15558 	int i;
15559 
15560 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15561 		struct hlist_head *head;
15562 		struct hlist_node *tmp;
15563 		struct bnxt_ntuple_filter *fltr;
15564 		int rc;
15565 
15566 		head = &bp->ntp_fltr_hash_tbl[i];
15567 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15568 			bool del = false;
15569 
15570 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15571 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15572 					continue;
15573 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15574 							fltr->flow_id,
15575 							fltr->base.sw_id)) {
15576 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15577 									 fltr);
15578 					del = true;
15579 				}
15580 			} else {
15581 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15582 								       fltr);
15583 				if (rc)
15584 					del = true;
15585 				else
15586 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15587 			}
15588 
15589 			if (del)
15590 				bnxt_del_ntp_filter(bp, fltr);
15591 		}
15592 	}
15593 #endif
15594 }
15595 
15596 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15597 				    unsigned int entry, struct udp_tunnel_info *ti)
15598 {
15599 	struct bnxt *bp = netdev_priv(netdev);
15600 	unsigned int cmd;
15601 
15602 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15603 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15604 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15605 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15606 	else
15607 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15608 
15609 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15610 }
15611 
15612 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15613 				      unsigned int entry, struct udp_tunnel_info *ti)
15614 {
15615 	struct bnxt *bp = netdev_priv(netdev);
15616 	unsigned int cmd;
15617 
15618 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15619 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15620 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15621 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15622 	else
15623 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15624 
15625 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15626 }
15627 
15628 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15629 	.set_port	= bnxt_udp_tunnel_set_port,
15630 	.unset_port	= bnxt_udp_tunnel_unset_port,
15631 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15632 	.tables		= {
15633 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15634 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15635 	},
15636 }, bnxt_udp_tunnels_p7 = {
15637 	.set_port	= bnxt_udp_tunnel_set_port,
15638 	.unset_port	= bnxt_udp_tunnel_unset_port,
15639 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15640 	.tables		= {
15641 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15642 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15643 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15644 	},
15645 };
15646 
15647 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15648 			       struct net_device *dev, u32 filter_mask,
15649 			       int nlflags)
15650 {
15651 	struct bnxt *bp = netdev_priv(dev);
15652 
15653 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15654 				       nlflags, filter_mask, NULL);
15655 }
15656 
15657 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15658 			       u16 flags, struct netlink_ext_ack *extack)
15659 {
15660 	struct bnxt *bp = netdev_priv(dev);
15661 	struct nlattr *attr, *br_spec;
15662 	int rem, rc = 0;
15663 
15664 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15665 		return -EOPNOTSUPP;
15666 
15667 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15668 	if (!br_spec)
15669 		return -EINVAL;
15670 
15671 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15672 		u16 mode;
15673 
15674 		mode = nla_get_u16(attr);
15675 		if (mode == bp->br_mode)
15676 			break;
15677 
15678 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15679 		if (!rc)
15680 			bp->br_mode = mode;
15681 		break;
15682 	}
15683 	return rc;
15684 }
15685 
15686 int bnxt_get_port_parent_id(struct net_device *dev,
15687 			    struct netdev_phys_item_id *ppid)
15688 {
15689 	struct bnxt *bp = netdev_priv(dev);
15690 
15691 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15692 		return -EOPNOTSUPP;
15693 
15694 	/* The PF and it's VF-reps only support the switchdev framework */
15695 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15696 		return -EOPNOTSUPP;
15697 
15698 	ppid->id_len = sizeof(bp->dsn);
15699 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15700 
15701 	return 0;
15702 }
15703 
15704 static const struct net_device_ops bnxt_netdev_ops = {
15705 	.ndo_open		= bnxt_open,
15706 	.ndo_start_xmit		= bnxt_start_xmit,
15707 	.ndo_stop		= bnxt_close,
15708 	.ndo_get_stats64	= bnxt_get_stats64,
15709 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15710 	.ndo_eth_ioctl		= bnxt_ioctl,
15711 	.ndo_validate_addr	= eth_validate_addr,
15712 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15713 	.ndo_change_mtu		= bnxt_change_mtu,
15714 	.ndo_fix_features	= bnxt_fix_features,
15715 	.ndo_set_features	= bnxt_set_features,
15716 	.ndo_features_check	= bnxt_features_check,
15717 	.ndo_tx_timeout		= bnxt_tx_timeout,
15718 #ifdef CONFIG_BNXT_SRIOV
15719 	.ndo_get_vf_config	= bnxt_get_vf_config,
15720 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15721 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15722 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15723 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15724 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15725 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15726 #endif
15727 	.ndo_setup_tc           = bnxt_setup_tc,
15728 #ifdef CONFIG_RFS_ACCEL
15729 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15730 #endif
15731 	.ndo_bpf		= bnxt_xdp,
15732 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15733 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15734 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15735 };
15736 
15737 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15738 				    struct netdev_queue_stats_rx *stats)
15739 {
15740 	struct bnxt *bp = netdev_priv(dev);
15741 	struct bnxt_cp_ring_info *cpr;
15742 	u64 *sw;
15743 
15744 	if (!bp->bnapi)
15745 		return;
15746 
15747 	cpr = &bp->bnapi[i]->cp_ring;
15748 	sw = cpr->stats.sw_stats;
15749 
15750 	stats->packets = 0;
15751 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15752 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15753 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15754 
15755 	stats->bytes = 0;
15756 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15757 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15758 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15759 
15760 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15761 }
15762 
15763 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15764 				    struct netdev_queue_stats_tx *stats)
15765 {
15766 	struct bnxt *bp = netdev_priv(dev);
15767 	struct bnxt_napi *bnapi;
15768 	u64 *sw;
15769 
15770 	if (!bp->tx_ring)
15771 		return;
15772 
15773 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15774 	sw = bnapi->cp_ring.stats.sw_stats;
15775 
15776 	stats->packets = 0;
15777 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15778 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15779 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15780 
15781 	stats->bytes = 0;
15782 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15783 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15784 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15785 }
15786 
15787 static void bnxt_get_base_stats(struct net_device *dev,
15788 				struct netdev_queue_stats_rx *rx,
15789 				struct netdev_queue_stats_tx *tx)
15790 {
15791 	struct bnxt *bp = netdev_priv(dev);
15792 
15793 	rx->packets = bp->net_stats_prev.rx_packets;
15794 	rx->bytes = bp->net_stats_prev.rx_bytes;
15795 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15796 
15797 	tx->packets = bp->net_stats_prev.tx_packets;
15798 	tx->bytes = bp->net_stats_prev.tx_bytes;
15799 }
15800 
15801 static const struct netdev_stat_ops bnxt_stat_ops = {
15802 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15803 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15804 	.get_base_stats		= bnxt_get_base_stats,
15805 };
15806 
15807 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15808 {
15809 	struct bnxt_rx_ring_info *rxr, *clone;
15810 	struct bnxt *bp = netdev_priv(dev);
15811 	struct bnxt_ring_struct *ring;
15812 	int rc;
15813 
15814 	if (!bp->rx_ring)
15815 		return -ENETDOWN;
15816 
15817 	rxr = &bp->rx_ring[idx];
15818 	clone = qmem;
15819 	memcpy(clone, rxr, sizeof(*rxr));
15820 	bnxt_init_rx_ring_struct(bp, clone);
15821 	bnxt_reset_rx_ring_struct(bp, clone);
15822 
15823 	clone->rx_prod = 0;
15824 	clone->rx_agg_prod = 0;
15825 	clone->rx_sw_agg_prod = 0;
15826 	clone->rx_next_cons = 0;
15827 	clone->need_head_pool = false;
15828 
15829 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15830 	if (rc)
15831 		return rc;
15832 
15833 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15834 	if (rc < 0)
15835 		goto err_page_pool_destroy;
15836 
15837 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15838 					MEM_TYPE_PAGE_POOL,
15839 					clone->page_pool);
15840 	if (rc)
15841 		goto err_rxq_info_unreg;
15842 
15843 	ring = &clone->rx_ring_struct;
15844 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15845 	if (rc)
15846 		goto err_free_rx_ring;
15847 
15848 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15849 		ring = &clone->rx_agg_ring_struct;
15850 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15851 		if (rc)
15852 			goto err_free_rx_agg_ring;
15853 
15854 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15855 		if (rc)
15856 			goto err_free_rx_agg_ring;
15857 	}
15858 
15859 	if (bp->flags & BNXT_FLAG_TPA) {
15860 		rc = bnxt_alloc_one_tpa_info(bp, clone);
15861 		if (rc)
15862 			goto err_free_tpa_info;
15863 	}
15864 
15865 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15866 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15867 
15868 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15869 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15870 		bnxt_alloc_one_rx_ring_netmem(bp, clone, idx);
15871 	if (bp->flags & BNXT_FLAG_TPA)
15872 		bnxt_alloc_one_tpa_info_data(bp, clone);
15873 
15874 	return 0;
15875 
15876 err_free_tpa_info:
15877 	bnxt_free_one_tpa_info(bp, clone);
15878 err_free_rx_agg_ring:
15879 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15880 err_free_rx_ring:
15881 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15882 err_rxq_info_unreg:
15883 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15884 err_page_pool_destroy:
15885 	page_pool_destroy(clone->page_pool);
15886 	if (bnxt_separate_head_pool(clone))
15887 		page_pool_destroy(clone->head_pool);
15888 	clone->page_pool = NULL;
15889 	clone->head_pool = NULL;
15890 	return rc;
15891 }
15892 
15893 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15894 {
15895 	struct bnxt_rx_ring_info *rxr = qmem;
15896 	struct bnxt *bp = netdev_priv(dev);
15897 	struct bnxt_ring_struct *ring;
15898 
15899 	bnxt_free_one_rx_ring_skbs(bp, rxr);
15900 	bnxt_free_one_tpa_info(bp, rxr);
15901 
15902 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15903 
15904 	page_pool_destroy(rxr->page_pool);
15905 	if (bnxt_separate_head_pool(rxr))
15906 		page_pool_destroy(rxr->head_pool);
15907 	rxr->page_pool = NULL;
15908 	rxr->head_pool = NULL;
15909 
15910 	ring = &rxr->rx_ring_struct;
15911 	bnxt_free_ring(bp, &ring->ring_mem);
15912 
15913 	ring = &rxr->rx_agg_ring_struct;
15914 	bnxt_free_ring(bp, &ring->ring_mem);
15915 
15916 	kfree(rxr->rx_agg_bmap);
15917 	rxr->rx_agg_bmap = NULL;
15918 }
15919 
15920 static void bnxt_copy_rx_ring(struct bnxt *bp,
15921 			      struct bnxt_rx_ring_info *dst,
15922 			      struct bnxt_rx_ring_info *src)
15923 {
15924 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15925 	struct bnxt_ring_struct *dst_ring, *src_ring;
15926 	int i;
15927 
15928 	dst_ring = &dst->rx_ring_struct;
15929 	dst_rmem = &dst_ring->ring_mem;
15930 	src_ring = &src->rx_ring_struct;
15931 	src_rmem = &src_ring->ring_mem;
15932 
15933 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15934 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15935 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15936 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15937 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15938 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15939 
15940 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15941 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15942 	*dst_rmem->vmem = *src_rmem->vmem;
15943 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15944 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15945 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15946 	}
15947 
15948 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15949 		return;
15950 
15951 	dst_ring = &dst->rx_agg_ring_struct;
15952 	dst_rmem = &dst_ring->ring_mem;
15953 	src_ring = &src->rx_agg_ring_struct;
15954 	src_rmem = &src_ring->ring_mem;
15955 
15956 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15957 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15958 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15959 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15960 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15961 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15962 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15963 
15964 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15965 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15966 	*dst_rmem->vmem = *src_rmem->vmem;
15967 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15968 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15969 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15970 	}
15971 
15972 	dst->rx_agg_bmap = src->rx_agg_bmap;
15973 }
15974 
15975 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15976 {
15977 	struct bnxt *bp = netdev_priv(dev);
15978 	struct bnxt_rx_ring_info *rxr, *clone;
15979 	struct bnxt_cp_ring_info *cpr;
15980 	struct bnxt_vnic_info *vnic;
15981 	struct bnxt_napi *bnapi;
15982 	int i, rc;
15983 	u16 mru;
15984 
15985 	rxr = &bp->rx_ring[idx];
15986 	clone = qmem;
15987 
15988 	rxr->rx_prod = clone->rx_prod;
15989 	rxr->rx_agg_prod = clone->rx_agg_prod;
15990 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
15991 	rxr->rx_next_cons = clone->rx_next_cons;
15992 	rxr->rx_tpa = clone->rx_tpa;
15993 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
15994 	rxr->page_pool = clone->page_pool;
15995 	rxr->head_pool = clone->head_pool;
15996 	rxr->xdp_rxq = clone->xdp_rxq;
15997 	rxr->need_head_pool = clone->need_head_pool;
15998 
15999 	bnxt_copy_rx_ring(bp, rxr, clone);
16000 
16001 	bnapi = rxr->bnapi;
16002 	cpr = &bnapi->cp_ring;
16003 
16004 	/* All rings have been reserved and previously allocated.
16005 	 * Reallocating with the same parameters should never fail.
16006 	 */
16007 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
16008 	if (rc)
16009 		goto err_reset;
16010 
16011 	if (bp->tph_mode) {
16012 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
16013 		if (rc)
16014 			goto err_reset;
16015 	}
16016 
16017 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
16018 	if (rc)
16019 		goto err_reset;
16020 
16021 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
16022 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16023 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
16024 
16025 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
16026 		rc = bnxt_tx_queue_start(bp, idx);
16027 		if (rc)
16028 			goto err_reset;
16029 	}
16030 
16031 	napi_enable_locked(&bnapi->napi);
16032 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16033 
16034 	mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
16035 	for (i = 0; i < bp->nr_vnics; i++) {
16036 		vnic = &bp->vnic_info[i];
16037 
16038 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx);
16039 		if (rc)
16040 			return rc;
16041 	}
16042 	return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx);
16043 
16044 err_reset:
16045 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
16046 		   rc);
16047 	napi_enable_locked(&bnapi->napi);
16048 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16049 	bnxt_reset_task(bp, true);
16050 	return rc;
16051 }
16052 
16053 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
16054 {
16055 	struct bnxt *bp = netdev_priv(dev);
16056 	struct bnxt_rx_ring_info *rxr;
16057 	struct bnxt_cp_ring_info *cpr;
16058 	struct bnxt_vnic_info *vnic;
16059 	struct bnxt_napi *bnapi;
16060 	int i;
16061 
16062 	for (i = 0; i < bp->nr_vnics; i++) {
16063 		vnic = &bp->vnic_info[i];
16064 
16065 		bnxt_set_vnic_mru_p5(bp, vnic, 0, idx);
16066 	}
16067 	bnxt_set_rss_ctx_vnic_mru(bp, 0, idx);
16068 	/* Make sure NAPI sees that the VNIC is disabled */
16069 	synchronize_net();
16070 	rxr = &bp->rx_ring[idx];
16071 	bnapi = rxr->bnapi;
16072 	cpr = &bnapi->cp_ring;
16073 	cancel_work_sync(&cpr->dim.work);
16074 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
16075 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
16076 	page_pool_disable_direct_recycling(rxr->page_pool);
16077 	if (bnxt_separate_head_pool(rxr))
16078 		page_pool_disable_direct_recycling(rxr->head_pool);
16079 
16080 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
16081 		bnxt_tx_queue_stop(bp, idx);
16082 
16083 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
16084 	 * completion is handled in NAPI to guarantee no more DMA on that ring
16085 	 * after seeing the completion.
16086 	 */
16087 	napi_disable_locked(&bnapi->napi);
16088 
16089 	if (bp->tph_mode) {
16090 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
16091 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
16092 	}
16093 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
16094 
16095 	memcpy(qmem, rxr, sizeof(*rxr));
16096 	bnxt_init_rx_ring_struct(bp, qmem);
16097 
16098 	return 0;
16099 }
16100 
16101 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
16102 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
16103 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
16104 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
16105 	.ndo_queue_start	= bnxt_queue_start,
16106 	.ndo_queue_stop		= bnxt_queue_stop,
16107 };
16108 
16109 static void bnxt_remove_one(struct pci_dev *pdev)
16110 {
16111 	struct net_device *dev = pci_get_drvdata(pdev);
16112 	struct bnxt *bp = netdev_priv(dev);
16113 
16114 	if (BNXT_PF(bp))
16115 		bnxt_sriov_disable(bp);
16116 
16117 	bnxt_rdma_aux_device_del(bp);
16118 
16119 	unregister_netdev(dev);
16120 	bnxt_ptp_clear(bp);
16121 
16122 	bnxt_rdma_aux_device_uninit(bp);
16123 
16124 	bnxt_free_l2_filters(bp, true);
16125 	bnxt_free_ntp_fltrs(bp, true);
16126 	WARN_ON(bp->num_rss_ctx);
16127 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16128 	/* Flush any pending tasks */
16129 	cancel_work_sync(&bp->sp_task);
16130 	cancel_delayed_work_sync(&bp->fw_reset_task);
16131 	bp->sp_event = 0;
16132 
16133 	bnxt_dl_fw_reporters_destroy(bp);
16134 	bnxt_dl_unregister(bp);
16135 	bnxt_shutdown_tc(bp);
16136 
16137 	bnxt_clear_int_mode(bp);
16138 	bnxt_hwrm_func_drv_unrgtr(bp);
16139 	bnxt_free_hwrm_resources(bp);
16140 	bnxt_hwmon_uninit(bp);
16141 	bnxt_ethtool_free(bp);
16142 	bnxt_dcb_free(bp);
16143 	kfree(bp->ptp_cfg);
16144 	bp->ptp_cfg = NULL;
16145 	kfree(bp->fw_health);
16146 	bp->fw_health = NULL;
16147 	bnxt_cleanup_pci(bp);
16148 	bnxt_free_ctx_mem(bp, true);
16149 	bnxt_free_crash_dump_mem(bp);
16150 	kfree(bp->rss_indir_tbl);
16151 	bp->rss_indir_tbl = NULL;
16152 	bnxt_free_port_stats(bp);
16153 	free_netdev(dev);
16154 }
16155 
16156 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16157 {
16158 	int rc = 0;
16159 	struct bnxt_link_info *link_info = &bp->link_info;
16160 
16161 	bp->phy_flags = 0;
16162 	rc = bnxt_hwrm_phy_qcaps(bp);
16163 	if (rc) {
16164 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16165 			   rc);
16166 		return rc;
16167 	}
16168 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16169 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16170 	else
16171 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16172 
16173 	bp->mac_flags = 0;
16174 	bnxt_hwrm_mac_qcaps(bp);
16175 
16176 	if (!fw_dflt)
16177 		return 0;
16178 
16179 	mutex_lock(&bp->link_lock);
16180 	rc = bnxt_update_link(bp, false);
16181 	if (rc) {
16182 		mutex_unlock(&bp->link_lock);
16183 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16184 			   rc);
16185 		return rc;
16186 	}
16187 
16188 	/* Older firmware does not have supported_auto_speeds, so assume
16189 	 * that all supported speeds can be autonegotiated.
16190 	 */
16191 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16192 		link_info->support_auto_speeds = link_info->support_speeds;
16193 
16194 	bnxt_init_ethtool_link_settings(bp);
16195 	mutex_unlock(&bp->link_lock);
16196 	return 0;
16197 }
16198 
16199 static int bnxt_get_max_irq(struct pci_dev *pdev)
16200 {
16201 	u16 ctrl;
16202 
16203 	if (!pdev->msix_cap)
16204 		return 1;
16205 
16206 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16207 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16208 }
16209 
16210 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16211 				int *max_cp)
16212 {
16213 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16214 	int max_ring_grps = 0, max_irq;
16215 
16216 	*max_tx = hw_resc->max_tx_rings;
16217 	*max_rx = hw_resc->max_rx_rings;
16218 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16219 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16220 			bnxt_get_ulp_msix_num_in_use(bp),
16221 			hw_resc->max_stat_ctxs -
16222 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16223 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16224 		*max_cp = min_t(int, *max_cp, max_irq);
16225 	max_ring_grps = hw_resc->max_hw_ring_grps;
16226 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16227 		*max_cp -= 1;
16228 		*max_rx -= 2;
16229 	}
16230 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16231 		*max_rx >>= 1;
16232 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16233 		int rc;
16234 
16235 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16236 		if (rc) {
16237 			*max_rx = 0;
16238 			*max_tx = 0;
16239 		}
16240 		/* On P5 chips, max_cp output param should be available NQs */
16241 		*max_cp = max_irq;
16242 	}
16243 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16244 }
16245 
16246 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16247 {
16248 	int rx, tx, cp;
16249 
16250 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16251 	*max_rx = rx;
16252 	*max_tx = tx;
16253 	if (!rx || !tx || !cp)
16254 		return -ENOMEM;
16255 
16256 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16257 }
16258 
16259 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16260 			       bool shared)
16261 {
16262 	int rc;
16263 
16264 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16265 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16266 		/* Not enough rings, try disabling agg rings. */
16267 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16268 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16269 		if (rc) {
16270 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16271 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16272 			return rc;
16273 		}
16274 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16275 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16276 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16277 		bnxt_set_ring_params(bp);
16278 	}
16279 
16280 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16281 		int max_cp, max_stat, max_irq;
16282 
16283 		/* Reserve minimum resources for RoCE */
16284 		max_cp = bnxt_get_max_func_cp_rings(bp);
16285 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16286 		max_irq = bnxt_get_max_func_irqs(bp);
16287 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16288 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16289 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16290 			return 0;
16291 
16292 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16293 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16294 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16295 		max_cp = min_t(int, max_cp, max_irq);
16296 		max_cp = min_t(int, max_cp, max_stat);
16297 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16298 		if (rc)
16299 			rc = 0;
16300 	}
16301 	return rc;
16302 }
16303 
16304 /* In initial default shared ring setting, each shared ring must have a
16305  * RX/TX ring pair.
16306  */
16307 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16308 {
16309 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16310 	bp->rx_nr_rings = bp->cp_nr_rings;
16311 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16312 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
16313 }
16314 
16315 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16316 {
16317 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16318 	int avail_msix;
16319 
16320 	if (!bnxt_can_reserve_rings(bp))
16321 		return 0;
16322 
16323 	if (sh)
16324 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16325 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16326 	/* Reduce default rings on multi-port cards so that total default
16327 	 * rings do not exceed CPU count.
16328 	 */
16329 	if (bp->port_count > 1) {
16330 		int max_rings =
16331 			max_t(int, num_online_cpus() / bp->port_count, 1);
16332 
16333 		dflt_rings = min_t(int, dflt_rings, max_rings);
16334 	}
16335 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16336 	if (rc)
16337 		return rc;
16338 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16339 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16340 	if (sh)
16341 		bnxt_trim_dflt_sh_rings(bp);
16342 	else
16343 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16344 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
16345 
16346 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16347 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16348 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16349 
16350 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16351 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16352 	}
16353 
16354 	rc = __bnxt_reserve_rings(bp);
16355 	if (rc && rc != -ENODEV)
16356 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16357 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16358 	if (sh)
16359 		bnxt_trim_dflt_sh_rings(bp);
16360 
16361 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16362 	if (bnxt_need_reserve_rings(bp)) {
16363 		rc = __bnxt_reserve_rings(bp);
16364 		if (rc && rc != -ENODEV)
16365 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16366 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16367 	}
16368 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16369 		bp->rx_nr_rings++;
16370 		bp->cp_nr_rings++;
16371 	}
16372 	if (rc) {
16373 		bp->tx_nr_rings = 0;
16374 		bp->rx_nr_rings = 0;
16375 	}
16376 	return rc;
16377 }
16378 
16379 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16380 {
16381 	int rc;
16382 
16383 	if (bp->tx_nr_rings)
16384 		return 0;
16385 
16386 	bnxt_ulp_irq_stop(bp);
16387 	bnxt_clear_int_mode(bp);
16388 	rc = bnxt_set_dflt_rings(bp, true);
16389 	if (rc) {
16390 		if (BNXT_VF(bp) && rc == -ENODEV)
16391 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16392 		else
16393 			netdev_err(bp->dev, "Not enough rings available.\n");
16394 		goto init_dflt_ring_err;
16395 	}
16396 	rc = bnxt_init_int_mode(bp);
16397 	if (rc)
16398 		goto init_dflt_ring_err;
16399 
16400 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16401 
16402 	bnxt_set_dflt_rfs(bp);
16403 
16404 init_dflt_ring_err:
16405 	bnxt_ulp_irq_restart(bp, rc);
16406 	return rc;
16407 }
16408 
16409 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
16410 {
16411 	int rc;
16412 
16413 	netdev_ops_assert_locked(bp->dev);
16414 	bnxt_hwrm_func_qcaps(bp);
16415 
16416 	if (netif_running(bp->dev))
16417 		__bnxt_close_nic(bp, true, false);
16418 
16419 	bnxt_ulp_irq_stop(bp);
16420 	bnxt_clear_int_mode(bp);
16421 	rc = bnxt_init_int_mode(bp);
16422 	bnxt_ulp_irq_restart(bp, rc);
16423 
16424 	if (netif_running(bp->dev)) {
16425 		if (rc)
16426 			netif_close(bp->dev);
16427 		else
16428 			rc = bnxt_open_nic(bp, true, false);
16429 	}
16430 
16431 	return rc;
16432 }
16433 
16434 static int bnxt_init_mac_addr(struct bnxt *bp)
16435 {
16436 	int rc = 0;
16437 
16438 	if (BNXT_PF(bp)) {
16439 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16440 	} else {
16441 #ifdef CONFIG_BNXT_SRIOV
16442 		struct bnxt_vf_info *vf = &bp->vf;
16443 		bool strict_approval = true;
16444 
16445 		if (is_valid_ether_addr(vf->mac_addr)) {
16446 			/* overwrite netdev dev_addr with admin VF MAC */
16447 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16448 			/* Older PF driver or firmware may not approve this
16449 			 * correctly.
16450 			 */
16451 			strict_approval = false;
16452 		} else {
16453 			eth_hw_addr_random(bp->dev);
16454 		}
16455 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16456 #endif
16457 	}
16458 	return rc;
16459 }
16460 
16461 static void bnxt_vpd_read_info(struct bnxt *bp)
16462 {
16463 	struct pci_dev *pdev = bp->pdev;
16464 	unsigned int vpd_size, kw_len;
16465 	int pos, size;
16466 	u8 *vpd_data;
16467 
16468 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16469 	if (IS_ERR(vpd_data)) {
16470 		pci_warn(pdev, "Unable to read VPD\n");
16471 		return;
16472 	}
16473 
16474 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16475 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16476 	if (pos < 0)
16477 		goto read_sn;
16478 
16479 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16480 	memcpy(bp->board_partno, &vpd_data[pos], size);
16481 
16482 read_sn:
16483 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16484 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16485 					   &kw_len);
16486 	if (pos < 0)
16487 		goto exit;
16488 
16489 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16490 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16491 exit:
16492 	kfree(vpd_data);
16493 }
16494 
16495 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16496 {
16497 	struct pci_dev *pdev = bp->pdev;
16498 	u64 qword;
16499 
16500 	qword = pci_get_dsn(pdev);
16501 	if (!qword) {
16502 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16503 		return -EOPNOTSUPP;
16504 	}
16505 
16506 	put_unaligned_le64(qword, dsn);
16507 
16508 	bp->flags |= BNXT_FLAG_DSN_VALID;
16509 	return 0;
16510 }
16511 
16512 static int bnxt_map_db_bar(struct bnxt *bp)
16513 {
16514 	if (!bp->db_size)
16515 		return -ENODEV;
16516 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16517 	if (!bp->bar1)
16518 		return -ENOMEM;
16519 	return 0;
16520 }
16521 
16522 void bnxt_print_device_info(struct bnxt *bp)
16523 {
16524 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16525 		    board_info[bp->board_idx].name,
16526 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16527 
16528 	pcie_print_link_status(bp->pdev);
16529 }
16530 
16531 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16532 {
16533 	struct bnxt_hw_resc *hw_resc;
16534 	struct net_device *dev;
16535 	struct bnxt *bp;
16536 	int rc, max_irqs;
16537 
16538 	if (pci_is_bridge(pdev))
16539 		return -ENODEV;
16540 
16541 	if (!pdev->msix_cap) {
16542 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16543 		return -ENODEV;
16544 	}
16545 
16546 	/* Clear any pending DMA transactions from crash kernel
16547 	 * while loading driver in capture kernel.
16548 	 */
16549 	if (is_kdump_kernel()) {
16550 		pci_clear_master(pdev);
16551 		pcie_flr(pdev);
16552 	}
16553 
16554 	max_irqs = bnxt_get_max_irq(pdev);
16555 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16556 				 max_irqs);
16557 	if (!dev)
16558 		return -ENOMEM;
16559 
16560 	bp = netdev_priv(dev);
16561 	bp->board_idx = ent->driver_data;
16562 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16563 	bnxt_set_max_func_irqs(bp, max_irqs);
16564 
16565 	if (bnxt_vf_pciid(bp->board_idx))
16566 		bp->flags |= BNXT_FLAG_VF;
16567 
16568 	/* No devlink port registration in case of a VF */
16569 	if (BNXT_PF(bp))
16570 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16571 
16572 	rc = bnxt_init_board(pdev, dev);
16573 	if (rc < 0)
16574 		goto init_err_free;
16575 
16576 	dev->netdev_ops = &bnxt_netdev_ops;
16577 	dev->stat_ops = &bnxt_stat_ops;
16578 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16579 	dev->ethtool_ops = &bnxt_ethtool_ops;
16580 	pci_set_drvdata(pdev, dev);
16581 
16582 	rc = bnxt_alloc_hwrm_resources(bp);
16583 	if (rc)
16584 		goto init_err_pci_clean;
16585 
16586 	mutex_init(&bp->hwrm_cmd_lock);
16587 	mutex_init(&bp->link_lock);
16588 
16589 	rc = bnxt_fw_init_one_p1(bp);
16590 	if (rc)
16591 		goto init_err_pci_clean;
16592 
16593 	if (BNXT_PF(bp))
16594 		bnxt_vpd_read_info(bp);
16595 
16596 	if (BNXT_CHIP_P5_PLUS(bp)) {
16597 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16598 		if (BNXT_CHIP_P7(bp))
16599 			bp->flags |= BNXT_FLAG_CHIP_P7;
16600 	}
16601 
16602 	rc = bnxt_alloc_rss_indir_tbl(bp);
16603 	if (rc)
16604 		goto init_err_pci_clean;
16605 
16606 	rc = bnxt_fw_init_one_p2(bp);
16607 	if (rc)
16608 		goto init_err_pci_clean;
16609 
16610 	rc = bnxt_map_db_bar(bp);
16611 	if (rc) {
16612 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16613 			rc);
16614 		goto init_err_pci_clean;
16615 	}
16616 
16617 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16618 			   NETIF_F_TSO | NETIF_F_TSO6 |
16619 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16620 			   NETIF_F_GSO_IPXIP4 |
16621 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16622 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16623 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16624 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16625 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16626 
16627 	if (BNXT_SUPPORTS_TPA(bp))
16628 		dev->hw_features |= NETIF_F_LRO;
16629 
16630 	dev->hw_enc_features =
16631 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16632 			NETIF_F_TSO | NETIF_F_TSO6 |
16633 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16634 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16635 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16636 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16637 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16638 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16639 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16640 	else
16641 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16642 
16643 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16644 				    NETIF_F_GSO_GRE_CSUM;
16645 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16646 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16647 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16648 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16649 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16650 	if (BNXT_SUPPORTS_TPA(bp))
16651 		dev->hw_features |= NETIF_F_GRO_HW;
16652 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16653 	if (dev->features & NETIF_F_GRO_HW)
16654 		dev->features &= ~NETIF_F_LRO;
16655 	dev->priv_flags |= IFF_UNICAST_FLT;
16656 
16657 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16658 	if (bp->tso_max_segs)
16659 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16660 
16661 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16662 			    NETDEV_XDP_ACT_RX_SG;
16663 
16664 #ifdef CONFIG_BNXT_SRIOV
16665 	init_waitqueue_head(&bp->sriov_cfg_wait);
16666 #endif
16667 	if (BNXT_SUPPORTS_TPA(bp)) {
16668 		bp->gro_func = bnxt_gro_func_5730x;
16669 		if (BNXT_CHIP_P4(bp))
16670 			bp->gro_func = bnxt_gro_func_5731x;
16671 		else if (BNXT_CHIP_P5_PLUS(bp))
16672 			bp->gro_func = bnxt_gro_func_5750x;
16673 	}
16674 	if (!BNXT_CHIP_P4_PLUS(bp))
16675 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16676 
16677 	rc = bnxt_init_mac_addr(bp);
16678 	if (rc) {
16679 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16680 		rc = -EADDRNOTAVAIL;
16681 		goto init_err_pci_clean;
16682 	}
16683 
16684 	if (BNXT_PF(bp)) {
16685 		/* Read the adapter's DSN to use as the eswitch switch_id */
16686 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16687 	}
16688 
16689 	/* MTU range: 60 - FW defined max */
16690 	dev->min_mtu = ETH_ZLEN;
16691 	dev->max_mtu = bp->max_mtu;
16692 
16693 	rc = bnxt_probe_phy(bp, true);
16694 	if (rc)
16695 		goto init_err_pci_clean;
16696 
16697 	hw_resc = &bp->hw_resc;
16698 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16699 		       BNXT_L2_FLTR_MAX_FLTR;
16700 	/* Older firmware may not report these filters properly */
16701 	if (bp->max_fltr < BNXT_MAX_FLTR)
16702 		bp->max_fltr = BNXT_MAX_FLTR;
16703 	bnxt_init_l2_fltr_tbl(bp);
16704 	__bnxt_set_rx_skb_mode(bp, false);
16705 	bnxt_set_tpa_flags(bp);
16706 	bnxt_init_ring_params(bp);
16707 	bnxt_set_ring_params(bp);
16708 	bnxt_rdma_aux_device_init(bp);
16709 	rc = bnxt_set_dflt_rings(bp, true);
16710 	if (rc) {
16711 		if (BNXT_VF(bp) && rc == -ENODEV) {
16712 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16713 		} else {
16714 			netdev_err(bp->dev, "Not enough rings available.\n");
16715 			rc = -ENOMEM;
16716 		}
16717 		goto init_err_pci_clean;
16718 	}
16719 
16720 	bnxt_fw_init_one_p3(bp);
16721 
16722 	bnxt_init_dflt_coal(bp);
16723 
16724 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16725 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16726 
16727 	rc = bnxt_init_int_mode(bp);
16728 	if (rc)
16729 		goto init_err_pci_clean;
16730 
16731 	/* No TC has been set yet and rings may have been trimmed due to
16732 	 * limited MSIX, so we re-initialize the TX rings per TC.
16733 	 */
16734 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16735 
16736 	if (BNXT_PF(bp)) {
16737 		if (!bnxt_pf_wq) {
16738 			bnxt_pf_wq =
16739 				create_singlethread_workqueue("bnxt_pf_wq");
16740 			if (!bnxt_pf_wq) {
16741 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16742 				rc = -ENOMEM;
16743 				goto init_err_pci_clean;
16744 			}
16745 		}
16746 		rc = bnxt_init_tc(bp);
16747 		if (rc)
16748 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16749 				   rc);
16750 	}
16751 
16752 	bnxt_inv_fw_health_reg(bp);
16753 	rc = bnxt_dl_register(bp);
16754 	if (rc)
16755 		goto init_err_dl;
16756 
16757 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16758 
16759 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16760 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16761 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16762 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16763 	dev->request_ops_lock = true;
16764 	dev->netmem_tx = true;
16765 
16766 	rc = register_netdev(dev);
16767 	if (rc)
16768 		goto init_err_cleanup;
16769 
16770 	bnxt_dl_fw_reporters_create(bp);
16771 
16772 	bnxt_rdma_aux_device_add(bp);
16773 
16774 	bnxt_print_device_info(bp);
16775 
16776 	pci_save_state(pdev);
16777 
16778 	return 0;
16779 init_err_cleanup:
16780 	bnxt_rdma_aux_device_uninit(bp);
16781 	bnxt_dl_unregister(bp);
16782 init_err_dl:
16783 	bnxt_shutdown_tc(bp);
16784 	bnxt_clear_int_mode(bp);
16785 
16786 init_err_pci_clean:
16787 	bnxt_hwrm_func_drv_unrgtr(bp);
16788 	bnxt_free_hwrm_resources(bp);
16789 	bnxt_hwmon_uninit(bp);
16790 	bnxt_ethtool_free(bp);
16791 	bnxt_ptp_clear(bp);
16792 	kfree(bp->ptp_cfg);
16793 	bp->ptp_cfg = NULL;
16794 	kfree(bp->fw_health);
16795 	bp->fw_health = NULL;
16796 	bnxt_cleanup_pci(bp);
16797 	bnxt_free_ctx_mem(bp, true);
16798 	bnxt_free_crash_dump_mem(bp);
16799 	kfree(bp->rss_indir_tbl);
16800 	bp->rss_indir_tbl = NULL;
16801 
16802 init_err_free:
16803 	free_netdev(dev);
16804 	return rc;
16805 }
16806 
16807 static void bnxt_shutdown(struct pci_dev *pdev)
16808 {
16809 	struct net_device *dev = pci_get_drvdata(pdev);
16810 	struct bnxt *bp;
16811 
16812 	if (!dev)
16813 		return;
16814 
16815 	rtnl_lock();
16816 	netdev_lock(dev);
16817 	bp = netdev_priv(dev);
16818 	if (!bp)
16819 		goto shutdown_exit;
16820 
16821 	if (netif_running(dev))
16822 		netif_close(dev);
16823 
16824 	bnxt_ptp_clear(bp);
16825 	bnxt_clear_int_mode(bp);
16826 	pci_disable_device(pdev);
16827 
16828 	if (system_state == SYSTEM_POWER_OFF) {
16829 		pci_wake_from_d3(pdev, bp->wol);
16830 		pci_set_power_state(pdev, PCI_D3hot);
16831 	}
16832 
16833 shutdown_exit:
16834 	netdev_unlock(dev);
16835 	rtnl_unlock();
16836 }
16837 
16838 #ifdef CONFIG_PM_SLEEP
16839 static int bnxt_suspend(struct device *device)
16840 {
16841 	struct net_device *dev = dev_get_drvdata(device);
16842 	struct bnxt *bp = netdev_priv(dev);
16843 	int rc = 0;
16844 
16845 	bnxt_ulp_stop(bp);
16846 
16847 	netdev_lock(dev);
16848 	if (netif_running(dev)) {
16849 		netif_device_detach(dev);
16850 		rc = bnxt_close(dev);
16851 	}
16852 	bnxt_hwrm_func_drv_unrgtr(bp);
16853 	bnxt_ptp_clear(bp);
16854 	pci_disable_device(bp->pdev);
16855 	bnxt_free_ctx_mem(bp, false);
16856 	netdev_unlock(dev);
16857 	return rc;
16858 }
16859 
16860 static int bnxt_resume(struct device *device)
16861 {
16862 	struct net_device *dev = dev_get_drvdata(device);
16863 	struct bnxt *bp = netdev_priv(dev);
16864 	int rc = 0;
16865 
16866 	netdev_lock(dev);
16867 	rc = pci_enable_device(bp->pdev);
16868 	if (rc) {
16869 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16870 			   rc);
16871 		goto resume_exit;
16872 	}
16873 	pci_set_master(bp->pdev);
16874 	if (bnxt_hwrm_ver_get(bp)) {
16875 		rc = -ENODEV;
16876 		goto resume_exit;
16877 	}
16878 	rc = bnxt_hwrm_func_reset(bp);
16879 	if (rc) {
16880 		rc = -EBUSY;
16881 		goto resume_exit;
16882 	}
16883 
16884 	rc = bnxt_hwrm_func_qcaps(bp);
16885 	if (rc)
16886 		goto resume_exit;
16887 
16888 	bnxt_clear_reservations(bp, true);
16889 
16890 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16891 		rc = -ENODEV;
16892 		goto resume_exit;
16893 	}
16894 	if (bp->fw_crash_mem)
16895 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16896 
16897 	if (bnxt_ptp_init(bp)) {
16898 		kfree(bp->ptp_cfg);
16899 		bp->ptp_cfg = NULL;
16900 	}
16901 	bnxt_get_wol_settings(bp);
16902 	if (netif_running(dev)) {
16903 		rc = bnxt_open(dev);
16904 		if (!rc)
16905 			netif_device_attach(dev);
16906 	}
16907 
16908 resume_exit:
16909 	netdev_unlock(bp->dev);
16910 	bnxt_ulp_start(bp, rc);
16911 	if (!rc)
16912 		bnxt_reenable_sriov(bp);
16913 	return rc;
16914 }
16915 
16916 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16917 #define BNXT_PM_OPS (&bnxt_pm_ops)
16918 
16919 #else
16920 
16921 #define BNXT_PM_OPS NULL
16922 
16923 #endif /* CONFIG_PM_SLEEP */
16924 
16925 /**
16926  * bnxt_io_error_detected - called when PCI error is detected
16927  * @pdev: Pointer to PCI device
16928  * @state: The current pci connection state
16929  *
16930  * This function is called after a PCI bus error affecting
16931  * this device has been detected.
16932  */
16933 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16934 					       pci_channel_state_t state)
16935 {
16936 	struct net_device *netdev = pci_get_drvdata(pdev);
16937 	struct bnxt *bp = netdev_priv(netdev);
16938 	bool abort = false;
16939 
16940 	netdev_info(netdev, "PCI I/O error detected\n");
16941 
16942 	bnxt_ulp_stop(bp);
16943 
16944 	netdev_lock(netdev);
16945 	netif_device_detach(netdev);
16946 
16947 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16948 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16949 		abort = true;
16950 	}
16951 
16952 	if (abort || state == pci_channel_io_perm_failure) {
16953 		netdev_unlock(netdev);
16954 		return PCI_ERS_RESULT_DISCONNECT;
16955 	}
16956 
16957 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16958 	 * so we disable bus master to prevent any potential bad DMAs before
16959 	 * freeing kernel memory.
16960 	 */
16961 	if (state == pci_channel_io_frozen) {
16962 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16963 		bnxt_fw_fatal_close(bp);
16964 	}
16965 
16966 	if (netif_running(netdev))
16967 		__bnxt_close_nic(bp, true, true);
16968 
16969 	if (pci_is_enabled(pdev))
16970 		pci_disable_device(pdev);
16971 	bnxt_free_ctx_mem(bp, false);
16972 	netdev_unlock(netdev);
16973 
16974 	/* Request a slot slot reset. */
16975 	return PCI_ERS_RESULT_NEED_RESET;
16976 }
16977 
16978 /**
16979  * bnxt_io_slot_reset - called after the pci bus has been reset.
16980  * @pdev: Pointer to PCI device
16981  *
16982  * Restart the card from scratch, as if from a cold-boot.
16983  * At this point, the card has experienced a hard reset,
16984  * followed by fixups by BIOS, and has its config space
16985  * set up identically to what it was at cold boot.
16986  */
16987 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
16988 {
16989 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
16990 	struct net_device *netdev = pci_get_drvdata(pdev);
16991 	struct bnxt *bp = netdev_priv(netdev);
16992 	int retry = 0;
16993 	int err = 0;
16994 	int off;
16995 
16996 	netdev_info(bp->dev, "PCI Slot Reset\n");
16997 
16998 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16999 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
17000 		msleep(900);
17001 
17002 	netdev_lock(netdev);
17003 
17004 	if (pci_enable_device(pdev)) {
17005 		dev_err(&pdev->dev,
17006 			"Cannot re-enable PCI device after reset.\n");
17007 	} else {
17008 		pci_set_master(pdev);
17009 		/* Upon fatal error, our device internal logic that latches to
17010 		 * BAR value is getting reset and will restore only upon
17011 		 * rewriting the BARs.
17012 		 *
17013 		 * As pci_restore_state() does not re-write the BARs if the
17014 		 * value is same as saved value earlier, driver needs to
17015 		 * write the BARs to 0 to force restore, in case of fatal error.
17016 		 */
17017 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
17018 				       &bp->state)) {
17019 			for (off = PCI_BASE_ADDRESS_0;
17020 			     off <= PCI_BASE_ADDRESS_5; off += 4)
17021 				pci_write_config_dword(bp->pdev, off, 0);
17022 		}
17023 		pci_restore_state(pdev);
17024 		pci_save_state(pdev);
17025 
17026 		bnxt_inv_fw_health_reg(bp);
17027 		bnxt_try_map_fw_health_reg(bp);
17028 
17029 		/* In some PCIe AER scenarios, firmware may take up to
17030 		 * 10 seconds to become ready in the worst case.
17031 		 */
17032 		do {
17033 			err = bnxt_try_recover_fw(bp);
17034 			if (!err)
17035 				break;
17036 			retry++;
17037 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
17038 
17039 		if (err) {
17040 			dev_err(&pdev->dev, "Firmware not ready\n");
17041 			goto reset_exit;
17042 		}
17043 
17044 		err = bnxt_hwrm_func_reset(bp);
17045 		if (!err)
17046 			result = PCI_ERS_RESULT_RECOVERED;
17047 
17048 		/* IRQ will be initialized later in bnxt_io_resume */
17049 		bnxt_ulp_irq_stop(bp);
17050 		bnxt_clear_int_mode(bp);
17051 	}
17052 
17053 reset_exit:
17054 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
17055 	bnxt_clear_reservations(bp, true);
17056 	netdev_unlock(netdev);
17057 
17058 	return result;
17059 }
17060 
17061 /**
17062  * bnxt_io_resume - called when traffic can start flowing again.
17063  * @pdev: Pointer to PCI device
17064  *
17065  * This callback is called when the error recovery driver tells
17066  * us that its OK to resume normal operation.
17067  */
17068 static void bnxt_io_resume(struct pci_dev *pdev)
17069 {
17070 	struct net_device *netdev = pci_get_drvdata(pdev);
17071 	struct bnxt *bp = netdev_priv(netdev);
17072 	int err;
17073 
17074 	netdev_info(bp->dev, "PCI Slot Resume\n");
17075 	netdev_lock(netdev);
17076 
17077 	err = bnxt_hwrm_func_qcaps(bp);
17078 	if (!err) {
17079 		if (netif_running(netdev)) {
17080 			err = bnxt_open(netdev);
17081 		} else {
17082 			err = bnxt_reserve_rings(bp, true);
17083 			if (!err)
17084 				err = bnxt_init_int_mode(bp);
17085 		}
17086 	}
17087 
17088 	if (!err)
17089 		netif_device_attach(netdev);
17090 
17091 	netdev_unlock(netdev);
17092 	bnxt_ulp_start(bp, err);
17093 	if (!err)
17094 		bnxt_reenable_sriov(bp);
17095 }
17096 
17097 static const struct pci_error_handlers bnxt_err_handler = {
17098 	.error_detected	= bnxt_io_error_detected,
17099 	.slot_reset	= bnxt_io_slot_reset,
17100 	.resume		= bnxt_io_resume
17101 };
17102 
17103 static struct pci_driver bnxt_pci_driver = {
17104 	.name		= DRV_MODULE_NAME,
17105 	.id_table	= bnxt_pci_tbl,
17106 	.probe		= bnxt_init_one,
17107 	.remove		= bnxt_remove_one,
17108 	.shutdown	= bnxt_shutdown,
17109 	.driver.pm	= BNXT_PM_OPS,
17110 	.err_handler	= &bnxt_err_handler,
17111 #if defined(CONFIG_BNXT_SRIOV)
17112 	.sriov_configure = bnxt_sriov_configure,
17113 #endif
17114 };
17115 
17116 static int __init bnxt_init(void)
17117 {
17118 	int err;
17119 
17120 	bnxt_debug_init();
17121 	err = pci_register_driver(&bnxt_pci_driver);
17122 	if (err) {
17123 		bnxt_debug_exit();
17124 		return err;
17125 	}
17126 
17127 	return 0;
17128 }
17129 
17130 static void __exit bnxt_exit(void)
17131 {
17132 	pci_unregister_driver(&bnxt_pci_driver);
17133 	if (bnxt_pf_wq)
17134 		destroy_workqueue(bnxt_pf_wq);
17135 	bnxt_debug_exit();
17136 }
17137 
17138 module_init(bnxt_init);
17139 module_exit(bnxt_exit);
17140