xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision da0e2197645c8e01bb6080c7a2b86d9a56cc64a9)
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 #include <linux/bnxt/hsi.h>
62 
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 	[NETXTREME_E_P7_VF_HV] = { "Broadcom BCM5760X Virtual Function for Hyper-V" },
146 };
147 
148 static const struct pci_device_id bnxt_pci_tbl[] = {
149 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
150 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
151 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
152 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
153 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
157 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
166 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
168 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
169 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
170 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
171 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
172 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
173 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
176 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
178 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
179 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
180 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
181 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
182 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
183 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
188 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
189 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
190 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
191 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
193 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
194 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
195 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
196 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
197 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
198 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
199 #ifdef CONFIG_BNXT_SRIOV
200 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
201 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
202 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
204 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
206 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
207 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
208 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
209 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
210 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
211 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
212 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
214 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
215 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
216 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
217 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
218 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
219 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
220 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
221 	{ PCI_VDEVICE(BROADCOM, 0x181b), .driver_data = NETXTREME_E_P7_VF_HV },
222 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
223 #endif
224 	{ 0 }
225 };
226 
227 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
228 
229 static const u16 bnxt_vf_req_snif[] = {
230 	HWRM_FUNC_CFG,
231 	HWRM_FUNC_VF_CFG,
232 	HWRM_PORT_PHY_QCFG,
233 	HWRM_CFA_L2_FILTER_ALLOC,
234 };
235 
236 static const u16 bnxt_async_events_arr[] = {
237 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
238 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
239 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
240 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
241 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
242 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
243 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
244 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
245 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
246 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
247 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
248 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
249 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
250 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
251 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
252 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
253 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
254 };
255 
256 const u16 bnxt_bstore_to_trace[] = {
257 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
258 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
259 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
260 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
261 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
262 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
263 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
264 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
265 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
266 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
267 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
268 };
269 
270 static struct workqueue_struct *bnxt_pf_wq;
271 
272 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
273 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
274 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
275 
276 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
277 	.ports = {
278 		.src = 0,
279 		.dst = 0,
280 	},
281 	.addrs = {
282 		.v6addrs = {
283 			.src = BNXT_IPV6_MASK_NONE,
284 			.dst = BNXT_IPV6_MASK_NONE,
285 		},
286 	},
287 };
288 
289 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
290 	.ports = {
291 		.src = cpu_to_be16(0xffff),
292 		.dst = cpu_to_be16(0xffff),
293 	},
294 	.addrs = {
295 		.v6addrs = {
296 			.src = BNXT_IPV6_MASK_ALL,
297 			.dst = BNXT_IPV6_MASK_ALL,
298 		},
299 	},
300 };
301 
302 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
303 	.ports = {
304 		.src = cpu_to_be16(0xffff),
305 		.dst = cpu_to_be16(0xffff),
306 	},
307 	.addrs = {
308 		.v4addrs = {
309 			.src = cpu_to_be32(0xffffffff),
310 			.dst = cpu_to_be32(0xffffffff),
311 		},
312 	},
313 };
314 
315 static bool bnxt_vf_pciid(enum board_idx idx)
316 {
317 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
318 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
319 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
320 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF ||
321 		idx == NETXTREME_E_P7_VF_HV);
322 }
323 
324 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
325 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
326 
327 #define BNXT_DB_CQ(db, idx)						\
328 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
329 
330 #define BNXT_DB_NQ_P5(db, idx)						\
331 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
332 		    (db)->doorbell)
333 
334 #define BNXT_DB_NQ_P7(db, idx)						\
335 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
336 		    DB_RING_IDX(db, idx), (db)->doorbell)
337 
338 #define BNXT_DB_CQ_ARM(db, idx)						\
339 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
340 
341 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
342 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
343 		    DB_RING_IDX(db, idx), (db)->doorbell)
344 
345 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
346 {
347 	if (bp->flags & BNXT_FLAG_CHIP_P7)
348 		BNXT_DB_NQ_P7(db, idx);
349 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
350 		BNXT_DB_NQ_P5(db, idx);
351 	else
352 		BNXT_DB_CQ(db, idx);
353 }
354 
355 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
356 {
357 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
358 		BNXT_DB_NQ_ARM_P5(db, idx);
359 	else
360 		BNXT_DB_CQ_ARM(db, idx);
361 }
362 
363 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
364 {
365 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
366 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
367 			    DB_RING_IDX(db, idx), db->doorbell);
368 	else
369 		BNXT_DB_CQ(db, idx);
370 }
371 
372 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
373 {
374 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
375 		return;
376 
377 	if (BNXT_PF(bp))
378 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
379 	else
380 		schedule_delayed_work(&bp->fw_reset_task, delay);
381 }
382 
383 static void __bnxt_queue_sp_work(struct bnxt *bp)
384 {
385 	if (BNXT_PF(bp))
386 		queue_work(bnxt_pf_wq, &bp->sp_task);
387 	else
388 		schedule_work(&bp->sp_task);
389 }
390 
391 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
392 {
393 	set_bit(event, &bp->sp_event);
394 	__bnxt_queue_sp_work(bp);
395 }
396 
397 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
398 {
399 	if (!rxr->bnapi->in_reset) {
400 		rxr->bnapi->in_reset = true;
401 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
402 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
403 		else
404 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
405 		__bnxt_queue_sp_work(bp);
406 	}
407 	rxr->rx_next_cons = 0xffff;
408 }
409 
410 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
411 			  u16 curr)
412 {
413 	struct bnxt_napi *bnapi = txr->bnapi;
414 
415 	if (bnapi->tx_fault)
416 		return;
417 
418 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
419 		   txr->txq_index, txr->tx_hw_cons,
420 		   txr->tx_cons, txr->tx_prod, curr);
421 	WARN_ON_ONCE(1);
422 	bnapi->tx_fault = 1;
423 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
424 }
425 
426 const u16 bnxt_lhint_arr[] = {
427 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
428 	TX_BD_FLAGS_LHINT_512_TO_1023,
429 	TX_BD_FLAGS_LHINT_1024_TO_2047,
430 	TX_BD_FLAGS_LHINT_1024_TO_2047,
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 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
444 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
445 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
446 };
447 
448 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
449 {
450 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
451 
452 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
453 		return 0;
454 
455 	return md_dst->u.port_info.port_id;
456 }
457 
458 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
459 			     u16 prod)
460 {
461 	/* Sync BD data before updating doorbell */
462 	wmb();
463 	bnxt_db_write(bp, &txr->tx_db, prod);
464 	txr->kick_pending = 0;
465 }
466 
467 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
468 {
469 	struct bnxt *bp = netdev_priv(dev);
470 	struct tx_bd *txbd, *txbd0;
471 	struct tx_bd_ext *txbd1;
472 	struct netdev_queue *txq;
473 	int i;
474 	dma_addr_t mapping;
475 	unsigned int length, pad = 0;
476 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
477 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
478 	struct pci_dev *pdev = bp->pdev;
479 	u16 prod, last_frag, txts_prod;
480 	struct bnxt_tx_ring_info *txr;
481 	struct bnxt_sw_tx_bd *tx_buf;
482 	__le32 lflags = 0;
483 	skb_frag_t *frag;
484 
485 	i = skb_get_queue_mapping(skb);
486 	if (unlikely(i >= bp->tx_nr_rings)) {
487 		dev_kfree_skb_any(skb);
488 		dev_core_stats_tx_dropped_inc(dev);
489 		return NETDEV_TX_OK;
490 	}
491 
492 	txq = netdev_get_tx_queue(dev, i);
493 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
494 	prod = txr->tx_prod;
495 
496 #if (MAX_SKB_FRAGS > TX_MAX_FRAGS)
497 	if (skb_shinfo(skb)->nr_frags > TX_MAX_FRAGS) {
498 		netdev_warn_once(dev, "SKB has too many (%d) fragments, max supported is %d.  SKB will be linearized.\n",
499 				 skb_shinfo(skb)->nr_frags, TX_MAX_FRAGS);
500 		if (skb_linearize(skb)) {
501 			dev_kfree_skb_any(skb);
502 			dev_core_stats_tx_dropped_inc(dev);
503 			return NETDEV_TX_OK;
504 		}
505 	}
506 #endif
507 	free_size = bnxt_tx_avail(bp, txr);
508 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
509 		/* We must have raced with NAPI cleanup */
510 		if (net_ratelimit() && txr->kick_pending)
511 			netif_warn(bp, tx_err, dev,
512 				   "bnxt: ring busy w/ flush pending!\n");
513 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
514 					bp->tx_wake_thresh))
515 			return NETDEV_TX_BUSY;
516 	}
517 
518 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
519 		goto tx_free;
520 
521 	length = skb->len;
522 	len = skb_headlen(skb);
523 	last_frag = skb_shinfo(skb)->nr_frags;
524 
525 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
526 
527 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
528 	tx_buf->skb = skb;
529 	tx_buf->nr_frags = last_frag;
530 
531 	vlan_tag_flags = 0;
532 	cfa_action = bnxt_xmit_get_cfa_action(skb);
533 	if (skb_vlan_tag_present(skb)) {
534 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
535 				 skb_vlan_tag_get(skb);
536 		/* Currently supports 8021Q, 8021AD vlan offloads
537 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
538 		 */
539 		if (skb->vlan_proto == htons(ETH_P_8021Q))
540 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
541 	}
542 
543 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
544 	    ptp->tx_tstamp_en) {
545 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
546 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
547 			tx_buf->is_ts_pkt = 1;
548 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
549 		} else if (!skb_is_gso(skb)) {
550 			u16 seq_id, hdr_off;
551 
552 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
553 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
554 				if (vlan_tag_flags)
555 					hdr_off += VLAN_HLEN;
556 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
557 				tx_buf->is_ts_pkt = 1;
558 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
559 
560 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
561 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
562 				tx_buf->txts_prod = txts_prod;
563 			}
564 		}
565 	}
566 	if (unlikely(skb->no_fcs))
567 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
568 
569 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
570 	    skb_frags_readable(skb) && !lflags) {
571 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
572 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
573 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
574 		void __iomem *db = txr->tx_db.doorbell;
575 		void *pdata = tx_push_buf->data;
576 		u64 *end;
577 		int j, push_len;
578 
579 		/* Set COAL_NOW to be ready quickly for the next push */
580 		tx_push->tx_bd_len_flags_type =
581 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
582 					TX_BD_TYPE_LONG_TX_BD |
583 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
584 					TX_BD_FLAGS_COAL_NOW |
585 					TX_BD_FLAGS_PACKET_END |
586 					TX_BD_CNT(2));
587 
588 		if (skb->ip_summed == CHECKSUM_PARTIAL)
589 			tx_push1->tx_bd_hsize_lflags =
590 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
591 		else
592 			tx_push1->tx_bd_hsize_lflags = 0;
593 
594 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
595 		tx_push1->tx_bd_cfa_action =
596 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
597 
598 		end = pdata + length;
599 		end = PTR_ALIGN(end, 8) - 1;
600 		*end = 0;
601 
602 		skb_copy_from_linear_data(skb, pdata, len);
603 		pdata += len;
604 		for (j = 0; j < last_frag; j++) {
605 			void *fptr;
606 
607 			frag = &skb_shinfo(skb)->frags[j];
608 			fptr = skb_frag_address_safe(frag);
609 			if (!fptr)
610 				goto normal_tx;
611 
612 			memcpy(pdata, fptr, skb_frag_size(frag));
613 			pdata += skb_frag_size(frag);
614 		}
615 
616 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
617 		txbd->tx_bd_haddr = txr->data_mapping;
618 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
619 		prod = NEXT_TX(prod);
620 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
621 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
622 		memcpy(txbd, tx_push1, sizeof(*txbd));
623 		prod = NEXT_TX(prod);
624 		tx_push->doorbell =
625 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
626 				    DB_RING_IDX(&txr->tx_db, prod));
627 		WRITE_ONCE(txr->tx_prod, prod);
628 
629 		tx_buf->is_push = 1;
630 		netdev_tx_sent_queue(txq, skb->len);
631 		wmb();	/* Sync is_push and byte queue before pushing data */
632 
633 		push_len = (length + sizeof(*tx_push) + 7) / 8;
634 		if (push_len > 16) {
635 			__iowrite64_copy(db, tx_push_buf, 16);
636 			__iowrite32_copy(db + 4, tx_push_buf + 1,
637 					 (push_len - 16) << 1);
638 		} else {
639 			__iowrite64_copy(db, tx_push_buf, push_len);
640 		}
641 
642 		goto tx_done;
643 	}
644 
645 normal_tx:
646 	if (length < BNXT_MIN_PKT_SIZE) {
647 		pad = BNXT_MIN_PKT_SIZE - length;
648 		if (skb_pad(skb, pad))
649 			/* SKB already freed. */
650 			goto tx_kick_pending;
651 		length = BNXT_MIN_PKT_SIZE;
652 	}
653 
654 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
655 
656 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
657 		goto tx_free;
658 
659 	dma_unmap_addr_set(tx_buf, mapping, mapping);
660 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
661 		TX_BD_CNT(last_frag + 2);
662 
663 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
664 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
665 
666 	prod = NEXT_TX(prod);
667 	txbd1 = (struct tx_bd_ext *)
668 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
669 
670 	txbd1->tx_bd_hsize_lflags = lflags;
671 	if (skb_is_gso(skb)) {
672 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
673 		u32 hdr_len;
674 
675 		if (skb->encapsulation) {
676 			if (udp_gso)
677 				hdr_len = skb_inner_transport_offset(skb) +
678 					  sizeof(struct udphdr);
679 			else
680 				hdr_len = skb_inner_tcp_all_headers(skb);
681 		} else if (udp_gso) {
682 			hdr_len = skb_transport_offset(skb) +
683 				  sizeof(struct udphdr);
684 		} else {
685 			hdr_len = skb_tcp_all_headers(skb);
686 		}
687 
688 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
689 					TX_BD_FLAGS_T_IPID |
690 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
691 		length = skb_shinfo(skb)->gso_size;
692 		txbd1->tx_bd_mss = cpu_to_le32(length);
693 		length += hdr_len;
694 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
695 		txbd1->tx_bd_hsize_lflags |=
696 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
697 		txbd1->tx_bd_mss = 0;
698 	}
699 
700 	length >>= 9;
701 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
702 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
703 				     skb->len);
704 		i = 0;
705 		goto tx_dma_error;
706 	}
707 	flags |= bnxt_lhint_arr[length];
708 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
709 
710 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
711 	txbd1->tx_bd_cfa_action =
712 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
713 	txbd0 = txbd;
714 	for (i = 0; i < last_frag; i++) {
715 		frag = &skb_shinfo(skb)->frags[i];
716 		prod = NEXT_TX(prod);
717 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
718 
719 		len = skb_frag_size(frag);
720 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
721 					   DMA_TO_DEVICE);
722 
723 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
724 			goto tx_dma_error;
725 
726 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
727 		netmem_dma_unmap_addr_set(skb_frag_netmem(frag), tx_buf,
728 					  mapping, mapping);
729 
730 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
731 
732 		flags = len << TX_BD_LEN_SHIFT;
733 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
734 	}
735 
736 	flags &= ~TX_BD_LEN;
737 	txbd->tx_bd_len_flags_type =
738 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
739 			    TX_BD_FLAGS_PACKET_END);
740 
741 	netdev_tx_sent_queue(txq, skb->len);
742 
743 	skb_tx_timestamp(skb);
744 
745 	prod = NEXT_TX(prod);
746 	WRITE_ONCE(txr->tx_prod, prod);
747 
748 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
749 		bnxt_txr_db_kick(bp, txr, prod);
750 	} else {
751 		if (free_size >= bp->tx_wake_thresh)
752 			txbd0->tx_bd_len_flags_type |=
753 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
754 		txr->kick_pending = 1;
755 	}
756 
757 tx_done:
758 
759 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
760 		if (netdev_xmit_more() && !tx_buf->is_push) {
761 			txbd0->tx_bd_len_flags_type &=
762 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
763 			bnxt_txr_db_kick(bp, txr, prod);
764 		}
765 
766 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
767 				   bp->tx_wake_thresh);
768 	}
769 	return NETDEV_TX_OK;
770 
771 tx_dma_error:
772 	last_frag = i;
773 
774 	/* start back at beginning and unmap skb */
775 	prod = txr->tx_prod;
776 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
777 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
778 			 skb_headlen(skb), DMA_TO_DEVICE);
779 	prod = NEXT_TX(prod);
780 
781 	/* unmap remaining mapped pages */
782 	for (i = 0; i < last_frag; i++) {
783 		prod = NEXT_TX(prod);
784 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
785 		frag = &skb_shinfo(skb)->frags[i];
786 		netmem_dma_unmap_page_attrs(&pdev->dev,
787 					    dma_unmap_addr(tx_buf, mapping),
788 					    skb_frag_size(frag),
789 					    DMA_TO_DEVICE, 0);
790 	}
791 
792 tx_free:
793 	dev_kfree_skb_any(skb);
794 tx_kick_pending:
795 	if (BNXT_TX_PTP_IS_SET(lflags)) {
796 		txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].is_ts_pkt = 0;
797 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
798 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
799 			/* set SKB to err so PTP worker will clean up */
800 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
801 	}
802 	if (txr->kick_pending)
803 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
804 	txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].skb = NULL;
805 	dev_core_stats_tx_dropped_inc(dev);
806 	return NETDEV_TX_OK;
807 }
808 
809 /* Returns true if some remaining TX packets not processed. */
810 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
811 			  int budget)
812 {
813 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
814 	struct pci_dev *pdev = bp->pdev;
815 	u16 hw_cons = txr->tx_hw_cons;
816 	unsigned int tx_bytes = 0;
817 	u16 cons = txr->tx_cons;
818 	skb_frag_t *frag;
819 	int tx_pkts = 0;
820 	bool rc = false;
821 
822 	while (RING_TX(bp, cons) != hw_cons) {
823 		struct bnxt_sw_tx_bd *tx_buf;
824 		struct sk_buff *skb;
825 		bool is_ts_pkt;
826 		int j, last;
827 
828 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
829 		skb = tx_buf->skb;
830 
831 		if (unlikely(!skb)) {
832 			bnxt_sched_reset_txr(bp, txr, cons);
833 			return rc;
834 		}
835 
836 		is_ts_pkt = tx_buf->is_ts_pkt;
837 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
838 			rc = true;
839 			break;
840 		}
841 
842 		cons = NEXT_TX(cons);
843 		tx_pkts++;
844 		tx_bytes += skb->len;
845 		tx_buf->skb = NULL;
846 		tx_buf->is_ts_pkt = 0;
847 
848 		if (tx_buf->is_push) {
849 			tx_buf->is_push = 0;
850 			goto next_tx_int;
851 		}
852 
853 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
854 				 skb_headlen(skb), DMA_TO_DEVICE);
855 		last = tx_buf->nr_frags;
856 
857 		for (j = 0; j < last; j++) {
858 			frag = &skb_shinfo(skb)->frags[j];
859 			cons = NEXT_TX(cons);
860 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
861 			netmem_dma_unmap_page_attrs(&pdev->dev,
862 						    dma_unmap_addr(tx_buf,
863 								   mapping),
864 						    skb_frag_size(frag),
865 						    DMA_TO_DEVICE, 0);
866 		}
867 		if (unlikely(is_ts_pkt)) {
868 			if (BNXT_CHIP_P5(bp)) {
869 				/* PTP worker takes ownership of the skb */
870 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
871 				skb = NULL;
872 			}
873 		}
874 
875 next_tx_int:
876 		cons = NEXT_TX(cons);
877 
878 		dev_consume_skb_any(skb);
879 	}
880 
881 	WRITE_ONCE(txr->tx_cons, cons);
882 
883 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
884 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
885 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
886 
887 	return rc;
888 }
889 
890 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
891 {
892 	struct bnxt_tx_ring_info *txr;
893 	bool more = false;
894 	int i;
895 
896 	bnxt_for_each_napi_tx(i, bnapi, txr) {
897 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
898 			more |= __bnxt_tx_int(bp, txr, budget);
899 	}
900 	if (!more)
901 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
902 }
903 
904 static bool bnxt_separate_head_pool(struct bnxt_rx_ring_info *rxr)
905 {
906 	return rxr->need_head_pool || PAGE_SIZE > BNXT_RX_PAGE_SIZE;
907 }
908 
909 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
910 					 struct bnxt_rx_ring_info *rxr,
911 					 unsigned int *offset,
912 					 gfp_t gfp)
913 {
914 	struct page *page;
915 
916 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
917 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
918 						BNXT_RX_PAGE_SIZE);
919 	} else {
920 		page = page_pool_dev_alloc_pages(rxr->page_pool);
921 		*offset = 0;
922 	}
923 	if (!page)
924 		return NULL;
925 
926 	*mapping = page_pool_get_dma_addr(page) + *offset;
927 	return page;
928 }
929 
930 static netmem_ref __bnxt_alloc_rx_netmem(struct bnxt *bp, dma_addr_t *mapping,
931 					 struct bnxt_rx_ring_info *rxr,
932 					 unsigned int *offset,
933 					 gfp_t gfp)
934 {
935 	netmem_ref netmem;
936 
937 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
938 		netmem = page_pool_alloc_frag_netmem(rxr->page_pool, offset, BNXT_RX_PAGE_SIZE, gfp);
939 	} else {
940 		netmem = page_pool_alloc_netmems(rxr->page_pool, gfp);
941 		*offset = 0;
942 	}
943 	if (!netmem)
944 		return 0;
945 
946 	*mapping = page_pool_get_dma_addr_netmem(netmem) + *offset;
947 	return netmem;
948 }
949 
950 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
951 				       struct bnxt_rx_ring_info *rxr,
952 				       gfp_t gfp)
953 {
954 	unsigned int offset;
955 	struct page *page;
956 
957 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
958 				    bp->rx_buf_size, gfp);
959 	if (!page)
960 		return NULL;
961 
962 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
963 	return page_address(page) + offset;
964 }
965 
966 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
967 		       u16 prod, gfp_t gfp)
968 {
969 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
970 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
971 	dma_addr_t mapping;
972 
973 	if (BNXT_RX_PAGE_MODE(bp)) {
974 		unsigned int offset;
975 		struct page *page =
976 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
977 
978 		if (!page)
979 			return -ENOMEM;
980 
981 		mapping += bp->rx_dma_offset;
982 		rx_buf->data = page;
983 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
984 	} else {
985 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
986 
987 		if (!data)
988 			return -ENOMEM;
989 
990 		rx_buf->data = data;
991 		rx_buf->data_ptr = data + bp->rx_offset;
992 	}
993 	rx_buf->mapping = mapping;
994 
995 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
996 	return 0;
997 }
998 
999 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
1000 {
1001 	u16 prod = rxr->rx_prod;
1002 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1003 	struct bnxt *bp = rxr->bnapi->bp;
1004 	struct rx_bd *cons_bd, *prod_bd;
1005 
1006 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1007 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1008 
1009 	prod_rx_buf->data = data;
1010 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
1011 
1012 	prod_rx_buf->mapping = cons_rx_buf->mapping;
1013 
1014 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1015 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
1016 
1017 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
1018 }
1019 
1020 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1021 {
1022 	u16 next, max = rxr->rx_agg_bmap_size;
1023 
1024 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
1025 	if (next >= max)
1026 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
1027 	return next;
1028 }
1029 
1030 static int bnxt_alloc_rx_netmem(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1031 				u16 prod, gfp_t gfp)
1032 {
1033 	struct rx_bd *rxbd =
1034 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1035 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
1036 	u16 sw_prod = rxr->rx_sw_agg_prod;
1037 	unsigned int offset = 0;
1038 	dma_addr_t mapping;
1039 	netmem_ref netmem;
1040 
1041 	netmem = __bnxt_alloc_rx_netmem(bp, &mapping, rxr, &offset, gfp);
1042 	if (!netmem)
1043 		return -ENOMEM;
1044 
1045 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1046 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1047 
1048 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1049 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1050 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1051 
1052 	rx_agg_buf->netmem = netmem;
1053 	rx_agg_buf->offset = offset;
1054 	rx_agg_buf->mapping = mapping;
1055 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1056 	rxbd->rx_bd_opaque = sw_prod;
1057 	return 0;
1058 }
1059 
1060 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1061 				       struct bnxt_cp_ring_info *cpr,
1062 				       u16 cp_cons, u16 curr)
1063 {
1064 	struct rx_agg_cmp *agg;
1065 
1066 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1067 	agg = (struct rx_agg_cmp *)
1068 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1069 	return agg;
1070 }
1071 
1072 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1073 					      struct bnxt_rx_ring_info *rxr,
1074 					      u16 agg_id, u16 curr)
1075 {
1076 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1077 
1078 	return &tpa_info->agg_arr[curr];
1079 }
1080 
1081 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1082 				   u16 start, u32 agg_bufs, bool tpa)
1083 {
1084 	struct bnxt_napi *bnapi = cpr->bnapi;
1085 	struct bnxt *bp = bnapi->bp;
1086 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1087 	u16 prod = rxr->rx_agg_prod;
1088 	u16 sw_prod = rxr->rx_sw_agg_prod;
1089 	bool p5_tpa = false;
1090 	u32 i;
1091 
1092 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1093 		p5_tpa = true;
1094 
1095 	for (i = 0; i < agg_bufs; i++) {
1096 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1097 		struct rx_agg_cmp *agg;
1098 		struct rx_bd *prod_bd;
1099 		netmem_ref netmem;
1100 		u16 cons;
1101 
1102 		if (p5_tpa)
1103 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1104 		else
1105 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1106 		cons = agg->rx_agg_cmp_opaque;
1107 		__clear_bit(cons, rxr->rx_agg_bmap);
1108 
1109 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1110 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1111 
1112 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1113 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1114 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1115 
1116 		/* It is possible for sw_prod to be equal to cons, so
1117 		 * set cons_rx_buf->netmem to 0 first.
1118 		 */
1119 		netmem = cons_rx_buf->netmem;
1120 		cons_rx_buf->netmem = 0;
1121 		prod_rx_buf->netmem = netmem;
1122 		prod_rx_buf->offset = cons_rx_buf->offset;
1123 
1124 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1125 
1126 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1127 
1128 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1129 		prod_bd->rx_bd_opaque = sw_prod;
1130 
1131 		prod = NEXT_RX_AGG(prod);
1132 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1133 	}
1134 	rxr->rx_agg_prod = prod;
1135 	rxr->rx_sw_agg_prod = sw_prod;
1136 }
1137 
1138 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1139 					      struct bnxt_rx_ring_info *rxr,
1140 					      u16 cons, void *data, u8 *data_ptr,
1141 					      dma_addr_t dma_addr,
1142 					      unsigned int offset_and_len)
1143 {
1144 	unsigned int len = offset_and_len & 0xffff;
1145 	struct page *page = data;
1146 	u16 prod = rxr->rx_prod;
1147 	struct sk_buff *skb;
1148 	int err;
1149 
1150 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1151 	if (unlikely(err)) {
1152 		bnxt_reuse_rx_data(rxr, cons, data);
1153 		return NULL;
1154 	}
1155 	dma_addr -= bp->rx_dma_offset;
1156 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1157 				bp->rx_dir);
1158 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1159 	if (!skb) {
1160 		page_pool_recycle_direct(rxr->page_pool, page);
1161 		return NULL;
1162 	}
1163 	skb_mark_for_recycle(skb);
1164 	skb_reserve(skb, bp->rx_offset);
1165 	__skb_put(skb, len);
1166 
1167 	return skb;
1168 }
1169 
1170 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1171 					struct bnxt_rx_ring_info *rxr,
1172 					u16 cons, void *data, u8 *data_ptr,
1173 					dma_addr_t dma_addr,
1174 					unsigned int offset_and_len)
1175 {
1176 	unsigned int payload = offset_and_len >> 16;
1177 	unsigned int len = offset_and_len & 0xffff;
1178 	skb_frag_t *frag;
1179 	struct page *page = data;
1180 	u16 prod = rxr->rx_prod;
1181 	struct sk_buff *skb;
1182 	int off, err;
1183 
1184 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1185 	if (unlikely(err)) {
1186 		bnxt_reuse_rx_data(rxr, cons, data);
1187 		return NULL;
1188 	}
1189 	dma_addr -= bp->rx_dma_offset;
1190 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1191 				bp->rx_dir);
1192 
1193 	if (unlikely(!payload))
1194 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1195 
1196 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1197 	if (!skb) {
1198 		page_pool_recycle_direct(rxr->page_pool, page);
1199 		return NULL;
1200 	}
1201 
1202 	skb_mark_for_recycle(skb);
1203 	off = (void *)data_ptr - page_address(page);
1204 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1205 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1206 	       payload + NET_IP_ALIGN);
1207 
1208 	frag = &skb_shinfo(skb)->frags[0];
1209 	skb_frag_size_sub(frag, payload);
1210 	skb_frag_off_add(frag, payload);
1211 	skb->data_len -= payload;
1212 	skb->tail += payload;
1213 
1214 	return skb;
1215 }
1216 
1217 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1218 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1219 				   void *data, u8 *data_ptr,
1220 				   dma_addr_t dma_addr,
1221 				   unsigned int offset_and_len)
1222 {
1223 	u16 prod = rxr->rx_prod;
1224 	struct sk_buff *skb;
1225 	int err;
1226 
1227 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1228 	if (unlikely(err)) {
1229 		bnxt_reuse_rx_data(rxr, cons, data);
1230 		return NULL;
1231 	}
1232 
1233 	skb = napi_build_skb(data, bp->rx_buf_size);
1234 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1235 				bp->rx_dir);
1236 	if (!skb) {
1237 		page_pool_free_va(rxr->head_pool, data, true);
1238 		return NULL;
1239 	}
1240 
1241 	skb_mark_for_recycle(skb);
1242 	skb_reserve(skb, bp->rx_offset);
1243 	skb_put(skb, offset_and_len & 0xffff);
1244 	return skb;
1245 }
1246 
1247 static u32 __bnxt_rx_agg_netmems(struct bnxt *bp,
1248 				 struct bnxt_cp_ring_info *cpr,
1249 				 u16 idx, u32 agg_bufs, bool tpa,
1250 				 struct sk_buff *skb,
1251 				 struct xdp_buff *xdp)
1252 {
1253 	struct bnxt_napi *bnapi = cpr->bnapi;
1254 	struct skb_shared_info *shinfo;
1255 	struct bnxt_rx_ring_info *rxr;
1256 	u32 i, total_frag_len = 0;
1257 	bool p5_tpa = false;
1258 	u16 prod;
1259 
1260 	rxr = bnapi->rx_ring;
1261 	prod = rxr->rx_agg_prod;
1262 
1263 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1264 		p5_tpa = true;
1265 
1266 	if (skb)
1267 		shinfo = skb_shinfo(skb);
1268 	else
1269 		shinfo = xdp_get_shared_info_from_buff(xdp);
1270 
1271 	for (i = 0; i < agg_bufs; i++) {
1272 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1273 		struct rx_agg_cmp *agg;
1274 		u16 cons, frag_len;
1275 		netmem_ref netmem;
1276 
1277 		if (p5_tpa)
1278 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1279 		else
1280 			agg = bnxt_get_agg(bp, cpr, idx, i);
1281 		cons = agg->rx_agg_cmp_opaque;
1282 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1283 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1284 
1285 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1286 		if (skb) {
1287 			skb_add_rx_frag_netmem(skb, i, cons_rx_buf->netmem,
1288 					       cons_rx_buf->offset,
1289 					       frag_len, BNXT_RX_PAGE_SIZE);
1290 		} else {
1291 			skb_frag_t *frag = &shinfo->frags[i];
1292 
1293 			skb_frag_fill_netmem_desc(frag, cons_rx_buf->netmem,
1294 						  cons_rx_buf->offset,
1295 						  frag_len);
1296 			shinfo->nr_frags = i + 1;
1297 		}
1298 		__clear_bit(cons, rxr->rx_agg_bmap);
1299 
1300 		/* It is possible for bnxt_alloc_rx_netmem() to allocate
1301 		 * a sw_prod index that equals the cons index, so we
1302 		 * need to clear the cons entry now.
1303 		 */
1304 		netmem = cons_rx_buf->netmem;
1305 		cons_rx_buf->netmem = 0;
1306 
1307 		if (xdp && netmem_is_pfmemalloc(netmem))
1308 			xdp_buff_set_frag_pfmemalloc(xdp);
1309 
1310 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_ATOMIC) != 0) {
1311 			if (skb) {
1312 				skb->len -= frag_len;
1313 				skb->data_len -= frag_len;
1314 				skb->truesize -= BNXT_RX_PAGE_SIZE;
1315 			}
1316 
1317 			--shinfo->nr_frags;
1318 			cons_rx_buf->netmem = netmem;
1319 
1320 			/* Update prod since possibly some netmems have been
1321 			 * allocated already.
1322 			 */
1323 			rxr->rx_agg_prod = prod;
1324 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1325 			return 0;
1326 		}
1327 
1328 		page_pool_dma_sync_netmem_for_cpu(rxr->page_pool, netmem, 0,
1329 						  BNXT_RX_PAGE_SIZE);
1330 
1331 		total_frag_len += frag_len;
1332 		prod = NEXT_RX_AGG(prod);
1333 	}
1334 	rxr->rx_agg_prod = prod;
1335 	return total_frag_len;
1336 }
1337 
1338 static struct sk_buff *bnxt_rx_agg_netmems_skb(struct bnxt *bp,
1339 					       struct bnxt_cp_ring_info *cpr,
1340 					       struct sk_buff *skb, u16 idx,
1341 					       u32 agg_bufs, bool tpa)
1342 {
1343 	u32 total_frag_len = 0;
1344 
1345 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1346 					       skb, NULL);
1347 	if (!total_frag_len) {
1348 		skb_mark_for_recycle(skb);
1349 		dev_kfree_skb(skb);
1350 		return NULL;
1351 	}
1352 
1353 	return skb;
1354 }
1355 
1356 static u32 bnxt_rx_agg_netmems_xdp(struct bnxt *bp,
1357 				   struct bnxt_cp_ring_info *cpr,
1358 				   struct xdp_buff *xdp, u16 idx,
1359 				   u32 agg_bufs, bool tpa)
1360 {
1361 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1362 	u32 total_frag_len = 0;
1363 
1364 	if (!xdp_buff_has_frags(xdp))
1365 		shinfo->nr_frags = 0;
1366 
1367 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1368 					       NULL, xdp);
1369 	if (total_frag_len) {
1370 		xdp_buff_set_frags_flag(xdp);
1371 		shinfo->nr_frags = agg_bufs;
1372 		shinfo->xdp_frags_size = total_frag_len;
1373 	}
1374 	return total_frag_len;
1375 }
1376 
1377 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1378 			       u8 agg_bufs, u32 *raw_cons)
1379 {
1380 	u16 last;
1381 	struct rx_agg_cmp *agg;
1382 
1383 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1384 	last = RING_CMP(*raw_cons);
1385 	agg = (struct rx_agg_cmp *)
1386 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1387 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1388 }
1389 
1390 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1391 				      unsigned int len,
1392 				      dma_addr_t mapping)
1393 {
1394 	struct bnxt *bp = bnapi->bp;
1395 	struct pci_dev *pdev = bp->pdev;
1396 	struct sk_buff *skb;
1397 
1398 	skb = napi_alloc_skb(&bnapi->napi, len);
1399 	if (!skb)
1400 		return NULL;
1401 
1402 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1403 				bp->rx_dir);
1404 
1405 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1406 	       len + NET_IP_ALIGN);
1407 
1408 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1409 				   bp->rx_dir);
1410 
1411 	skb_put(skb, len);
1412 
1413 	return skb;
1414 }
1415 
1416 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1417 				     unsigned int len,
1418 				     dma_addr_t mapping)
1419 {
1420 	return bnxt_copy_data(bnapi, data, len, mapping);
1421 }
1422 
1423 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1424 				     struct xdp_buff *xdp,
1425 				     unsigned int len,
1426 				     dma_addr_t mapping)
1427 {
1428 	unsigned int metasize = 0;
1429 	u8 *data = xdp->data;
1430 	struct sk_buff *skb;
1431 
1432 	len = xdp->data_end - xdp->data_meta;
1433 	metasize = xdp->data - xdp->data_meta;
1434 	data = xdp->data_meta;
1435 
1436 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1437 	if (!skb)
1438 		return skb;
1439 
1440 	if (metasize) {
1441 		skb_metadata_set(skb, metasize);
1442 		__skb_pull(skb, metasize);
1443 	}
1444 
1445 	return skb;
1446 }
1447 
1448 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1449 			   u32 *raw_cons, void *cmp)
1450 {
1451 	struct rx_cmp *rxcmp = cmp;
1452 	u32 tmp_raw_cons = *raw_cons;
1453 	u8 cmp_type, agg_bufs = 0;
1454 
1455 	cmp_type = RX_CMP_TYPE(rxcmp);
1456 
1457 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1458 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1459 			    RX_CMP_AGG_BUFS) >>
1460 			   RX_CMP_AGG_BUFS_SHIFT;
1461 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1462 		struct rx_tpa_end_cmp *tpa_end = cmp;
1463 
1464 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1465 			return 0;
1466 
1467 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1468 	}
1469 
1470 	if (agg_bufs) {
1471 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1472 			return -EBUSY;
1473 	}
1474 	*raw_cons = tmp_raw_cons;
1475 	return 0;
1476 }
1477 
1478 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1479 {
1480 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1481 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1482 
1483 	if (test_bit(idx, map->agg_idx_bmap))
1484 		idx = find_first_zero_bit(map->agg_idx_bmap,
1485 					  BNXT_AGG_IDX_BMAP_SIZE);
1486 	__set_bit(idx, map->agg_idx_bmap);
1487 	map->agg_id_tbl[agg_id] = idx;
1488 	return idx;
1489 }
1490 
1491 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1492 {
1493 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1494 
1495 	__clear_bit(idx, map->agg_idx_bmap);
1496 }
1497 
1498 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1499 {
1500 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1501 
1502 	return map->agg_id_tbl[agg_id];
1503 }
1504 
1505 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1506 			      struct rx_tpa_start_cmp *tpa_start,
1507 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1508 {
1509 	tpa_info->cfa_code_valid = 1;
1510 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1511 	tpa_info->vlan_valid = 0;
1512 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1513 		tpa_info->vlan_valid = 1;
1514 		tpa_info->metadata =
1515 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1516 	}
1517 }
1518 
1519 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1520 				 struct rx_tpa_start_cmp *tpa_start,
1521 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1522 {
1523 	tpa_info->vlan_valid = 0;
1524 	if (TPA_START_VLAN_VALID(tpa_start)) {
1525 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1526 		u32 vlan_proto = ETH_P_8021Q;
1527 
1528 		tpa_info->vlan_valid = 1;
1529 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1530 			vlan_proto = ETH_P_8021AD;
1531 		tpa_info->metadata = vlan_proto << 16 |
1532 				     TPA_START_METADATA0_TCI(tpa_start1);
1533 	}
1534 }
1535 
1536 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1537 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1538 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1539 {
1540 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1541 	struct bnxt_tpa_info *tpa_info;
1542 	u16 cons, prod, agg_id;
1543 	struct rx_bd *prod_bd;
1544 	dma_addr_t mapping;
1545 
1546 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1547 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1548 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1549 	} else {
1550 		agg_id = TPA_START_AGG_ID(tpa_start);
1551 	}
1552 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1553 	prod = rxr->rx_prod;
1554 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1555 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1556 	tpa_info = &rxr->rx_tpa[agg_id];
1557 
1558 	if (unlikely(cons != rxr->rx_next_cons ||
1559 		     TPA_START_ERROR(tpa_start))) {
1560 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1561 			    cons, rxr->rx_next_cons,
1562 			    TPA_START_ERROR_CODE(tpa_start1));
1563 		bnxt_sched_reset_rxr(bp, rxr);
1564 		return;
1565 	}
1566 	prod_rx_buf->data = tpa_info->data;
1567 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1568 
1569 	mapping = tpa_info->mapping;
1570 	prod_rx_buf->mapping = mapping;
1571 
1572 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1573 
1574 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1575 
1576 	tpa_info->data = cons_rx_buf->data;
1577 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1578 	cons_rx_buf->data = NULL;
1579 	tpa_info->mapping = cons_rx_buf->mapping;
1580 
1581 	tpa_info->len =
1582 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1583 				RX_TPA_START_CMP_LEN_SHIFT;
1584 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1585 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1586 		tpa_info->gso_type = SKB_GSO_TCPV4;
1587 		if (TPA_START_IS_IPV6(tpa_start1))
1588 			tpa_info->gso_type = SKB_GSO_TCPV6;
1589 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1590 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1591 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1592 			tpa_info->gso_type = SKB_GSO_TCPV6;
1593 		tpa_info->rss_hash =
1594 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1595 	} else {
1596 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1597 		tpa_info->gso_type = 0;
1598 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1599 	}
1600 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1601 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1602 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1603 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1604 	else
1605 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1606 	tpa_info->agg_count = 0;
1607 
1608 	rxr->rx_prod = NEXT_RX(prod);
1609 	cons = RING_RX(bp, NEXT_RX(cons));
1610 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1611 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1612 
1613 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1614 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1615 	cons_rx_buf->data = NULL;
1616 }
1617 
1618 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1619 {
1620 	if (agg_bufs)
1621 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1622 }
1623 
1624 #ifdef CONFIG_INET
1625 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1626 {
1627 	struct udphdr *uh = NULL;
1628 
1629 	if (ip_proto == htons(ETH_P_IP)) {
1630 		struct iphdr *iph = (struct iphdr *)skb->data;
1631 
1632 		if (iph->protocol == IPPROTO_UDP)
1633 			uh = (struct udphdr *)(iph + 1);
1634 	} else {
1635 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1636 
1637 		if (iph->nexthdr == IPPROTO_UDP)
1638 			uh = (struct udphdr *)(iph + 1);
1639 	}
1640 	if (uh) {
1641 		if (uh->check)
1642 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1643 		else
1644 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1645 	}
1646 }
1647 #endif
1648 
1649 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1650 					   int payload_off, int tcp_ts,
1651 					   struct sk_buff *skb)
1652 {
1653 #ifdef CONFIG_INET
1654 	struct tcphdr *th;
1655 	int len, nw_off;
1656 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1657 	u32 hdr_info = tpa_info->hdr_info;
1658 	bool loopback = false;
1659 
1660 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1661 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1662 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1663 
1664 	/* If the packet is an internal loopback packet, the offsets will
1665 	 * have an extra 4 bytes.
1666 	 */
1667 	if (inner_mac_off == 4) {
1668 		loopback = true;
1669 	} else if (inner_mac_off > 4) {
1670 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1671 					    ETH_HLEN - 2));
1672 
1673 		/* We only support inner iPv4/ipv6.  If we don't see the
1674 		 * correct protocol ID, it must be a loopback packet where
1675 		 * the offsets are off by 4.
1676 		 */
1677 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1678 			loopback = true;
1679 	}
1680 	if (loopback) {
1681 		/* internal loopback packet, subtract all offsets by 4 */
1682 		inner_ip_off -= 4;
1683 		inner_mac_off -= 4;
1684 		outer_ip_off -= 4;
1685 	}
1686 
1687 	nw_off = inner_ip_off - ETH_HLEN;
1688 	skb_set_network_header(skb, nw_off);
1689 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1690 		struct ipv6hdr *iph = ipv6_hdr(skb);
1691 
1692 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1693 		len = skb->len - skb_transport_offset(skb);
1694 		th = tcp_hdr(skb);
1695 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1696 	} else {
1697 		struct iphdr *iph = ip_hdr(skb);
1698 
1699 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1700 		len = skb->len - skb_transport_offset(skb);
1701 		th = tcp_hdr(skb);
1702 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1703 	}
1704 
1705 	if (inner_mac_off) { /* tunnel */
1706 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1707 					    ETH_HLEN - 2));
1708 
1709 		bnxt_gro_tunnel(skb, proto);
1710 	}
1711 #endif
1712 	return skb;
1713 }
1714 
1715 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1716 					   int payload_off, int tcp_ts,
1717 					   struct sk_buff *skb)
1718 {
1719 #ifdef CONFIG_INET
1720 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1721 	u32 hdr_info = tpa_info->hdr_info;
1722 	int iphdr_len, nw_off;
1723 
1724 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1725 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1726 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1727 
1728 	nw_off = inner_ip_off - ETH_HLEN;
1729 	skb_set_network_header(skb, nw_off);
1730 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1731 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1732 	skb_set_transport_header(skb, nw_off + iphdr_len);
1733 
1734 	if (inner_mac_off) { /* tunnel */
1735 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1736 					    ETH_HLEN - 2));
1737 
1738 		bnxt_gro_tunnel(skb, proto);
1739 	}
1740 #endif
1741 	return skb;
1742 }
1743 
1744 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1745 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1746 
1747 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1748 					   int payload_off, int tcp_ts,
1749 					   struct sk_buff *skb)
1750 {
1751 #ifdef CONFIG_INET
1752 	struct tcphdr *th;
1753 	int len, nw_off, tcp_opt_len = 0;
1754 
1755 	if (tcp_ts)
1756 		tcp_opt_len = 12;
1757 
1758 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1759 		struct iphdr *iph;
1760 
1761 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1762 			 ETH_HLEN;
1763 		skb_set_network_header(skb, nw_off);
1764 		iph = ip_hdr(skb);
1765 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1766 		len = skb->len - skb_transport_offset(skb);
1767 		th = tcp_hdr(skb);
1768 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1769 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1770 		struct ipv6hdr *iph;
1771 
1772 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1773 			 ETH_HLEN;
1774 		skb_set_network_header(skb, nw_off);
1775 		iph = ipv6_hdr(skb);
1776 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1777 		len = skb->len - skb_transport_offset(skb);
1778 		th = tcp_hdr(skb);
1779 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1780 	} else {
1781 		dev_kfree_skb_any(skb);
1782 		return NULL;
1783 	}
1784 
1785 	if (nw_off) /* tunnel */
1786 		bnxt_gro_tunnel(skb, skb->protocol);
1787 #endif
1788 	return skb;
1789 }
1790 
1791 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1792 					   struct bnxt_tpa_info *tpa_info,
1793 					   struct rx_tpa_end_cmp *tpa_end,
1794 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1795 					   struct sk_buff *skb)
1796 {
1797 #ifdef CONFIG_INET
1798 	int payload_off;
1799 	u16 segs;
1800 
1801 	segs = TPA_END_TPA_SEGS(tpa_end);
1802 	if (segs == 1)
1803 		return skb;
1804 
1805 	NAPI_GRO_CB(skb)->count = segs;
1806 	skb_shinfo(skb)->gso_size =
1807 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1808 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1809 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1810 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1811 	else
1812 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1813 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1814 	if (likely(skb))
1815 		tcp_gro_complete(skb);
1816 #endif
1817 	return skb;
1818 }
1819 
1820 /* Given the cfa_code of a received packet determine which
1821  * netdev (vf-rep or PF) the packet is destined to.
1822  */
1823 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1824 {
1825 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1826 
1827 	/* if vf-rep dev is NULL, it must belong to the PF */
1828 	return dev ? dev : bp->dev;
1829 }
1830 
1831 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1832 					   struct bnxt_cp_ring_info *cpr,
1833 					   u32 *raw_cons,
1834 					   struct rx_tpa_end_cmp *tpa_end,
1835 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1836 					   u8 *event)
1837 {
1838 	struct bnxt_napi *bnapi = cpr->bnapi;
1839 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1840 	struct net_device *dev = bp->dev;
1841 	u8 *data_ptr, agg_bufs;
1842 	unsigned int len;
1843 	struct bnxt_tpa_info *tpa_info;
1844 	dma_addr_t mapping;
1845 	struct sk_buff *skb;
1846 	u16 idx = 0, agg_id;
1847 	void *data;
1848 	bool gro;
1849 
1850 	if (unlikely(bnapi->in_reset)) {
1851 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1852 
1853 		if (rc < 0)
1854 			return ERR_PTR(-EBUSY);
1855 		return NULL;
1856 	}
1857 
1858 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1859 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1860 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1861 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1862 		tpa_info = &rxr->rx_tpa[agg_id];
1863 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1864 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1865 				    agg_bufs, tpa_info->agg_count);
1866 			agg_bufs = tpa_info->agg_count;
1867 		}
1868 		tpa_info->agg_count = 0;
1869 		*event |= BNXT_AGG_EVENT;
1870 		bnxt_free_agg_idx(rxr, agg_id);
1871 		idx = agg_id;
1872 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1873 	} else {
1874 		agg_id = TPA_END_AGG_ID(tpa_end);
1875 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1876 		tpa_info = &rxr->rx_tpa[agg_id];
1877 		idx = RING_CMP(*raw_cons);
1878 		if (agg_bufs) {
1879 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1880 				return ERR_PTR(-EBUSY);
1881 
1882 			*event |= BNXT_AGG_EVENT;
1883 			idx = NEXT_CMP(idx);
1884 		}
1885 		gro = !!TPA_END_GRO(tpa_end);
1886 	}
1887 	data = tpa_info->data;
1888 	data_ptr = tpa_info->data_ptr;
1889 	prefetch(data_ptr);
1890 	len = tpa_info->len;
1891 	mapping = tpa_info->mapping;
1892 
1893 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1894 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1895 		if (agg_bufs > MAX_SKB_FRAGS)
1896 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1897 				    agg_bufs, (int)MAX_SKB_FRAGS);
1898 		return NULL;
1899 	}
1900 
1901 	if (len <= bp->rx_copybreak) {
1902 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1903 		if (!skb) {
1904 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1905 			cpr->sw_stats->rx.rx_oom_discards += 1;
1906 			return NULL;
1907 		}
1908 	} else {
1909 		u8 *new_data;
1910 		dma_addr_t new_mapping;
1911 
1912 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1913 						GFP_ATOMIC);
1914 		if (!new_data) {
1915 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1916 			cpr->sw_stats->rx.rx_oom_discards += 1;
1917 			return NULL;
1918 		}
1919 
1920 		tpa_info->data = new_data;
1921 		tpa_info->data_ptr = new_data + bp->rx_offset;
1922 		tpa_info->mapping = new_mapping;
1923 
1924 		skb = napi_build_skb(data, bp->rx_buf_size);
1925 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1926 					bp->rx_buf_use_size, bp->rx_dir);
1927 
1928 		if (!skb) {
1929 			page_pool_free_va(rxr->head_pool, data, true);
1930 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1931 			cpr->sw_stats->rx.rx_oom_discards += 1;
1932 			return NULL;
1933 		}
1934 		skb_mark_for_recycle(skb);
1935 		skb_reserve(skb, bp->rx_offset);
1936 		skb_put(skb, len);
1937 	}
1938 
1939 	if (agg_bufs) {
1940 		skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs,
1941 					      true);
1942 		if (!skb) {
1943 			/* Page reuse already handled by bnxt_rx_pages(). */
1944 			cpr->sw_stats->rx.rx_oom_discards += 1;
1945 			return NULL;
1946 		}
1947 	}
1948 
1949 	if (tpa_info->cfa_code_valid)
1950 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1951 	skb->protocol = eth_type_trans(skb, dev);
1952 
1953 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1954 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1955 
1956 	if (tpa_info->vlan_valid &&
1957 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1958 		__be16 vlan_proto = htons(tpa_info->metadata >>
1959 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1960 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1961 
1962 		if (eth_type_vlan(vlan_proto)) {
1963 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1964 		} else {
1965 			dev_kfree_skb(skb);
1966 			return NULL;
1967 		}
1968 	}
1969 
1970 	skb_checksum_none_assert(skb);
1971 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1972 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1973 		skb->csum_level =
1974 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1975 	}
1976 
1977 	if (gro)
1978 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1979 
1980 	return skb;
1981 }
1982 
1983 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1984 			 struct rx_agg_cmp *rx_agg)
1985 {
1986 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1987 	struct bnxt_tpa_info *tpa_info;
1988 
1989 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1990 	tpa_info = &rxr->rx_tpa[agg_id];
1991 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1992 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1993 }
1994 
1995 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1996 			     struct sk_buff *skb)
1997 {
1998 	skb_mark_for_recycle(skb);
1999 
2000 	if (skb->dev != bp->dev) {
2001 		/* this packet belongs to a vf-rep */
2002 		bnxt_vf_rep_rx(bp, skb);
2003 		return;
2004 	}
2005 	skb_record_rx_queue(skb, bnapi->index);
2006 	napi_gro_receive(&bnapi->napi, skb);
2007 }
2008 
2009 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
2010 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
2011 {
2012 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
2013 
2014 	if (BNXT_PTP_RX_TS_VALID(flags))
2015 		goto ts_valid;
2016 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
2017 		return false;
2018 
2019 ts_valid:
2020 	*cmpl_ts = ts;
2021 	return true;
2022 }
2023 
2024 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
2025 				    struct rx_cmp *rxcmp,
2026 				    struct rx_cmp_ext *rxcmp1)
2027 {
2028 	__be16 vlan_proto;
2029 	u16 vtag;
2030 
2031 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
2032 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
2033 		u32 meta_data;
2034 
2035 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
2036 			return skb;
2037 
2038 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
2039 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2040 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
2041 		if (eth_type_vlan(vlan_proto))
2042 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2043 		else
2044 			goto vlan_err;
2045 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2046 		if (RX_CMP_VLAN_VALID(rxcmp)) {
2047 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
2048 
2049 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
2050 				vlan_proto = htons(ETH_P_8021Q);
2051 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
2052 				vlan_proto = htons(ETH_P_8021AD);
2053 			else
2054 				goto vlan_err;
2055 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
2056 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2057 		}
2058 	}
2059 	return skb;
2060 vlan_err:
2061 	skb_mark_for_recycle(skb);
2062 	dev_kfree_skb(skb);
2063 	return NULL;
2064 }
2065 
2066 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
2067 					   struct rx_cmp *rxcmp)
2068 {
2069 	u8 ext_op;
2070 
2071 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
2072 	switch (ext_op) {
2073 	case EXT_OP_INNER_4:
2074 	case EXT_OP_OUTER_4:
2075 	case EXT_OP_INNFL_3:
2076 	case EXT_OP_OUTFL_3:
2077 		return PKT_HASH_TYPE_L4;
2078 	default:
2079 		return PKT_HASH_TYPE_L3;
2080 	}
2081 }
2082 
2083 /* returns the following:
2084  * 1       - 1 packet successfully received
2085  * 0       - successful TPA_START, packet not completed yet
2086  * -EBUSY  - completion ring does not have all the agg buffers yet
2087  * -ENOMEM - packet aborted due to out of memory
2088  * -EIO    - packet aborted due to hw error indicated in BD
2089  */
2090 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2091 		       u32 *raw_cons, u8 *event)
2092 {
2093 	struct bnxt_napi *bnapi = cpr->bnapi;
2094 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2095 	struct net_device *dev = bp->dev;
2096 	struct rx_cmp *rxcmp;
2097 	struct rx_cmp_ext *rxcmp1;
2098 	u32 tmp_raw_cons = *raw_cons;
2099 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2100 	struct skb_shared_info *sinfo;
2101 	struct bnxt_sw_rx_bd *rx_buf;
2102 	unsigned int len;
2103 	u8 *data_ptr, agg_bufs, cmp_type;
2104 	bool xdp_active = false;
2105 	dma_addr_t dma_addr;
2106 	struct sk_buff *skb;
2107 	struct xdp_buff xdp;
2108 	u32 flags, misc;
2109 	u32 cmpl_ts;
2110 	void *data;
2111 	int rc = 0;
2112 
2113 	rxcmp = (struct rx_cmp *)
2114 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2115 
2116 	cmp_type = RX_CMP_TYPE(rxcmp);
2117 
2118 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2119 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2120 		goto next_rx_no_prod_no_len;
2121 	}
2122 
2123 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2124 	cp_cons = RING_CMP(tmp_raw_cons);
2125 	rxcmp1 = (struct rx_cmp_ext *)
2126 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2127 
2128 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2129 		return -EBUSY;
2130 
2131 	/* The valid test of the entry must be done first before
2132 	 * reading any further.
2133 	 */
2134 	dma_rmb();
2135 	prod = rxr->rx_prod;
2136 
2137 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2138 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2139 		bnxt_tpa_start(bp, rxr, cmp_type,
2140 			       (struct rx_tpa_start_cmp *)rxcmp,
2141 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2142 
2143 		*event |= BNXT_RX_EVENT;
2144 		goto next_rx_no_prod_no_len;
2145 
2146 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2147 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2148 				   (struct rx_tpa_end_cmp *)rxcmp,
2149 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2150 
2151 		if (IS_ERR(skb))
2152 			return -EBUSY;
2153 
2154 		rc = -ENOMEM;
2155 		if (likely(skb)) {
2156 			bnxt_deliver_skb(bp, bnapi, skb);
2157 			rc = 1;
2158 		}
2159 		*event |= BNXT_RX_EVENT;
2160 		goto next_rx_no_prod_no_len;
2161 	}
2162 
2163 	cons = rxcmp->rx_cmp_opaque;
2164 	if (unlikely(cons != rxr->rx_next_cons)) {
2165 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2166 
2167 		/* 0xffff is forced error, don't print it */
2168 		if (rxr->rx_next_cons != 0xffff)
2169 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2170 				    cons, rxr->rx_next_cons);
2171 		bnxt_sched_reset_rxr(bp, rxr);
2172 		if (rc1)
2173 			return rc1;
2174 		goto next_rx_no_prod_no_len;
2175 	}
2176 	rx_buf = &rxr->rx_buf_ring[cons];
2177 	data = rx_buf->data;
2178 	data_ptr = rx_buf->data_ptr;
2179 	prefetch(data_ptr);
2180 
2181 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2182 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2183 
2184 	if (agg_bufs) {
2185 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2186 			return -EBUSY;
2187 
2188 		cp_cons = NEXT_CMP(cp_cons);
2189 		*event |= BNXT_AGG_EVENT;
2190 	}
2191 	*event |= BNXT_RX_EVENT;
2192 
2193 	rx_buf->data = NULL;
2194 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2195 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2196 
2197 		bnxt_reuse_rx_data(rxr, cons, data);
2198 		if (agg_bufs)
2199 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2200 					       false);
2201 
2202 		rc = -EIO;
2203 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2204 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2205 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2206 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2207 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2208 						 rx_err);
2209 				bnxt_sched_reset_rxr(bp, rxr);
2210 			}
2211 		}
2212 		goto next_rx_no_len;
2213 	}
2214 
2215 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2216 	len = flags >> RX_CMP_LEN_SHIFT;
2217 	dma_addr = rx_buf->mapping;
2218 
2219 	if (bnxt_xdp_attached(bp, rxr)) {
2220 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2221 		if (agg_bufs) {
2222 			u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr, &xdp,
2223 							       cp_cons,
2224 							       agg_bufs,
2225 							       false);
2226 			if (!frag_len)
2227 				goto oom_next_rx;
2228 
2229 		}
2230 		xdp_active = true;
2231 	}
2232 
2233 	if (xdp_active) {
2234 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2235 			rc = 1;
2236 			goto next_rx;
2237 		}
2238 		if (xdp_buff_has_frags(&xdp)) {
2239 			sinfo = xdp_get_shared_info_from_buff(&xdp);
2240 			agg_bufs = sinfo->nr_frags;
2241 		} else {
2242 			agg_bufs = 0;
2243 		}
2244 	}
2245 
2246 	if (len <= bp->rx_copybreak) {
2247 		if (!xdp_active)
2248 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2249 		else
2250 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2251 		bnxt_reuse_rx_data(rxr, cons, data);
2252 		if (!skb) {
2253 			if (agg_bufs) {
2254 				if (!xdp_active)
2255 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2256 							       agg_bufs, false);
2257 				else
2258 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2259 			}
2260 			goto oom_next_rx;
2261 		}
2262 	} else {
2263 		u32 payload;
2264 
2265 		if (rx_buf->data_ptr == data_ptr)
2266 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2267 		else
2268 			payload = 0;
2269 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2270 				      payload | len);
2271 		if (!skb)
2272 			goto oom_next_rx;
2273 	}
2274 
2275 	if (agg_bufs) {
2276 		if (!xdp_active) {
2277 			skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons,
2278 						      agg_bufs, false);
2279 			if (!skb)
2280 				goto oom_next_rx;
2281 		} else {
2282 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs,
2283 						 rxr->page_pool, &xdp);
2284 			if (!skb) {
2285 				/* we should be able to free the old skb here */
2286 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2287 				goto oom_next_rx;
2288 			}
2289 		}
2290 	}
2291 
2292 	if (RX_CMP_HASH_VALID(rxcmp)) {
2293 		enum pkt_hash_types type;
2294 
2295 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2296 			type = bnxt_rss_ext_op(bp, rxcmp);
2297 		} else {
2298 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2299 
2300 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2301 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2302 				type = PKT_HASH_TYPE_L4;
2303 			else
2304 				type = PKT_HASH_TYPE_L3;
2305 		}
2306 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2307 	}
2308 
2309 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2310 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2311 	skb->protocol = eth_type_trans(skb, dev);
2312 
2313 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2314 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2315 		if (!skb)
2316 			goto next_rx;
2317 	}
2318 
2319 	skb_checksum_none_assert(skb);
2320 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2321 		if (dev->features & NETIF_F_RXCSUM) {
2322 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2323 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2324 		}
2325 	} else {
2326 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2327 			if (dev->features & NETIF_F_RXCSUM)
2328 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2329 		}
2330 	}
2331 
2332 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2333 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2334 			u64 ns, ts;
2335 
2336 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2337 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2338 
2339 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2340 				memset(skb_hwtstamps(skb), 0,
2341 				       sizeof(*skb_hwtstamps(skb)));
2342 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2343 			}
2344 		}
2345 	}
2346 	bnxt_deliver_skb(bp, bnapi, skb);
2347 	rc = 1;
2348 
2349 next_rx:
2350 	cpr->rx_packets += 1;
2351 	cpr->rx_bytes += len;
2352 
2353 next_rx_no_len:
2354 	rxr->rx_prod = NEXT_RX(prod);
2355 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2356 
2357 next_rx_no_prod_no_len:
2358 	*raw_cons = tmp_raw_cons;
2359 
2360 	return rc;
2361 
2362 oom_next_rx:
2363 	cpr->sw_stats->rx.rx_oom_discards += 1;
2364 	rc = -ENOMEM;
2365 	goto next_rx;
2366 }
2367 
2368 /* In netpoll mode, if we are using a combined completion ring, we need to
2369  * discard the rx packets and recycle the buffers.
2370  */
2371 static int bnxt_force_rx_discard(struct bnxt *bp,
2372 				 struct bnxt_cp_ring_info *cpr,
2373 				 u32 *raw_cons, u8 *event)
2374 {
2375 	u32 tmp_raw_cons = *raw_cons;
2376 	struct rx_cmp_ext *rxcmp1;
2377 	struct rx_cmp *rxcmp;
2378 	u16 cp_cons;
2379 	u8 cmp_type;
2380 	int rc;
2381 
2382 	cp_cons = RING_CMP(tmp_raw_cons);
2383 	rxcmp = (struct rx_cmp *)
2384 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2385 
2386 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2387 	cp_cons = RING_CMP(tmp_raw_cons);
2388 	rxcmp1 = (struct rx_cmp_ext *)
2389 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2390 
2391 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2392 		return -EBUSY;
2393 
2394 	/* The valid test of the entry must be done first before
2395 	 * reading any further.
2396 	 */
2397 	dma_rmb();
2398 	cmp_type = RX_CMP_TYPE(rxcmp);
2399 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2400 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2401 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2402 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2403 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2404 		struct rx_tpa_end_cmp_ext *tpa_end1;
2405 
2406 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2407 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2408 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2409 	}
2410 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2411 	if (rc && rc != -EBUSY)
2412 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2413 	return rc;
2414 }
2415 
2416 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2417 {
2418 	struct bnxt_fw_health *fw_health = bp->fw_health;
2419 	u32 reg = fw_health->regs[reg_idx];
2420 	u32 reg_type, reg_off, val = 0;
2421 
2422 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2423 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2424 	switch (reg_type) {
2425 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2426 		pci_read_config_dword(bp->pdev, reg_off, &val);
2427 		break;
2428 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2429 		reg_off = fw_health->mapped_regs[reg_idx];
2430 		fallthrough;
2431 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2432 		val = readl(bp->bar0 + reg_off);
2433 		break;
2434 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2435 		val = readl(bp->bar1 + reg_off);
2436 		break;
2437 	}
2438 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2439 		val &= fw_health->fw_reset_inprog_reg_mask;
2440 	return val;
2441 }
2442 
2443 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2444 {
2445 	int i;
2446 
2447 	for (i = 0; i < bp->rx_nr_rings; i++) {
2448 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2449 		struct bnxt_ring_grp_info *grp_info;
2450 
2451 		grp_info = &bp->grp_info[grp_idx];
2452 		if (grp_info->agg_fw_ring_id == ring_id)
2453 			return grp_idx;
2454 	}
2455 	return INVALID_HW_RING_ID;
2456 }
2457 
2458 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2459 {
2460 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2461 
2462 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2463 		return link_info->force_link_speed2;
2464 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2465 		return link_info->force_pam4_link_speed;
2466 	return link_info->force_link_speed;
2467 }
2468 
2469 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2470 {
2471 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2472 
2473 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2474 		link_info->req_link_speed = link_info->force_link_speed2;
2475 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2476 		switch (link_info->req_link_speed) {
2477 		case BNXT_LINK_SPEED_50GB_PAM4:
2478 		case BNXT_LINK_SPEED_100GB_PAM4:
2479 		case BNXT_LINK_SPEED_200GB_PAM4:
2480 		case BNXT_LINK_SPEED_400GB_PAM4:
2481 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2482 			break;
2483 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2484 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2485 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2486 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2487 			break;
2488 		default:
2489 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2490 		}
2491 		return;
2492 	}
2493 	link_info->req_link_speed = link_info->force_link_speed;
2494 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2495 	if (link_info->force_pam4_link_speed) {
2496 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2497 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2498 	}
2499 }
2500 
2501 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2502 {
2503 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2504 
2505 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2506 		link_info->advertising = link_info->auto_link_speeds2;
2507 		return;
2508 	}
2509 	link_info->advertising = link_info->auto_link_speeds;
2510 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2511 }
2512 
2513 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2514 {
2515 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2516 
2517 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2518 		if (link_info->req_link_speed != link_info->force_link_speed2)
2519 			return true;
2520 		return false;
2521 	}
2522 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2523 	    link_info->req_link_speed != link_info->force_link_speed)
2524 		return true;
2525 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2526 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2527 		return true;
2528 	return false;
2529 }
2530 
2531 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2532 {
2533 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2534 
2535 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2536 		if (link_info->advertising != link_info->auto_link_speeds2)
2537 			return true;
2538 		return false;
2539 	}
2540 	if (link_info->advertising != link_info->auto_link_speeds ||
2541 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2542 		return true;
2543 	return false;
2544 }
2545 
2546 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2547 {
2548 	u32 flags = bp->ctx->ctx_arr[type].flags;
2549 
2550 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2551 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2552 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2553 }
2554 
2555 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2556 {
2557 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2558 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2559 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2560 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2561 	struct bnxt_bs_trace_info *bs_trace;
2562 	int last_pg;
2563 
2564 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2565 		return;
2566 
2567 	mem_size = ctxm->max_entries * ctxm->entry_size;
2568 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2569 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2570 
2571 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2572 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2573 
2574 	rmem = &ctx_pg[0].ring_mem;
2575 	bs_trace = &bp->bs_trace[trace_type];
2576 	bs_trace->ctx_type = ctxm->type;
2577 	bs_trace->trace_type = trace_type;
2578 	if (pages > MAX_CTX_PAGES) {
2579 		int last_pg_dir = rmem->nr_pages - 1;
2580 
2581 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2582 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2583 	} else {
2584 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2585 	}
2586 	bs_trace->magic_byte += magic_byte_offset;
2587 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2588 }
2589 
2590 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2591 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2592 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2593 
2594 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2595 	(((data2) &							\
2596 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2597 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2598 
2599 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2600 	((data2) &							\
2601 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2602 
2603 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2604 	(((data2) &							\
2605 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2606 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2607 
2608 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2609 	((data1) &							\
2610 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2611 
2612 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2613 	(((data1) &							\
2614 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2615 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2616 
2617 /* Return true if the workqueue has to be scheduled */
2618 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2619 {
2620 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2621 
2622 	switch (err_type) {
2623 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2624 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2625 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2626 		break;
2627 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2628 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2629 		break;
2630 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2631 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2632 		break;
2633 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2634 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2635 		char *threshold_type;
2636 		bool notify = false;
2637 		char *dir_str;
2638 
2639 		switch (type) {
2640 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2641 			threshold_type = "warning";
2642 			break;
2643 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2644 			threshold_type = "critical";
2645 			break;
2646 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2647 			threshold_type = "fatal";
2648 			break;
2649 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2650 			threshold_type = "shutdown";
2651 			break;
2652 		default:
2653 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2654 			return false;
2655 		}
2656 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2657 			dir_str = "above";
2658 			notify = true;
2659 		} else {
2660 			dir_str = "below";
2661 		}
2662 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2663 			    dir_str, threshold_type);
2664 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2665 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2666 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2667 		if (notify) {
2668 			bp->thermal_threshold_type = type;
2669 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2670 			return true;
2671 		}
2672 		return false;
2673 	}
2674 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2675 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2676 		break;
2677 	default:
2678 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2679 			   err_type);
2680 		break;
2681 	}
2682 	return false;
2683 }
2684 
2685 #define BNXT_GET_EVENT_PORT(data)	\
2686 	((data) &			\
2687 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2688 
2689 #define BNXT_EVENT_RING_TYPE(data2)	\
2690 	((data2) &			\
2691 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2692 
2693 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2694 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2695 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2696 
2697 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2698 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2699 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2700 
2701 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2702 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2703 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2704 
2705 #define BNXT_PHC_BITS	48
2706 
2707 static int bnxt_async_event_process(struct bnxt *bp,
2708 				    struct hwrm_async_event_cmpl *cmpl)
2709 {
2710 	u16 event_id = le16_to_cpu(cmpl->event_id);
2711 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2712 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2713 
2714 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2715 		   event_id, data1, data2);
2716 
2717 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2718 	switch (event_id) {
2719 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2720 		struct bnxt_link_info *link_info = &bp->link_info;
2721 
2722 		if (BNXT_VF(bp))
2723 			goto async_event_process_exit;
2724 
2725 		/* print unsupported speed warning in forced speed mode only */
2726 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2727 		    (data1 & 0x20000)) {
2728 			u16 fw_speed = bnxt_get_force_speed(link_info);
2729 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2730 
2731 			if (speed != SPEED_UNKNOWN)
2732 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2733 					    speed);
2734 		}
2735 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2736 	}
2737 		fallthrough;
2738 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2739 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2740 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2741 		fallthrough;
2742 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2743 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2744 		break;
2745 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2746 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2747 		break;
2748 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2749 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2750 
2751 		if (BNXT_VF(bp))
2752 			break;
2753 
2754 		if (bp->pf.port_id != port_id)
2755 			break;
2756 
2757 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2758 		break;
2759 	}
2760 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2761 		if (BNXT_PF(bp))
2762 			goto async_event_process_exit;
2763 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2764 		break;
2765 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2766 		char *type_str = "Solicited";
2767 
2768 		if (!bp->fw_health)
2769 			goto async_event_process_exit;
2770 
2771 		bp->fw_reset_timestamp = jiffies;
2772 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2773 		if (!bp->fw_reset_min_dsecs)
2774 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2775 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2776 		if (!bp->fw_reset_max_dsecs)
2777 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2778 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2779 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2780 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2781 			type_str = "Fatal";
2782 			bp->fw_health->fatalities++;
2783 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2784 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2785 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2786 			type_str = "Non-fatal";
2787 			bp->fw_health->survivals++;
2788 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2789 		}
2790 		netif_warn(bp, hw, bp->dev,
2791 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2792 			   type_str, data1, data2,
2793 			   bp->fw_reset_min_dsecs * 100,
2794 			   bp->fw_reset_max_dsecs * 100);
2795 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2796 		break;
2797 	}
2798 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2799 		struct bnxt_fw_health *fw_health = bp->fw_health;
2800 		char *status_desc = "healthy";
2801 		u32 status;
2802 
2803 		if (!fw_health)
2804 			goto async_event_process_exit;
2805 
2806 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2807 			fw_health->enabled = false;
2808 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2809 			break;
2810 		}
2811 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2812 		fw_health->tmr_multiplier =
2813 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2814 				     bp->current_interval * 10);
2815 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2816 		if (!fw_health->enabled)
2817 			fw_health->last_fw_heartbeat =
2818 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2819 		fw_health->last_fw_reset_cnt =
2820 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2821 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2822 		if (status != BNXT_FW_STATUS_HEALTHY)
2823 			status_desc = "unhealthy";
2824 		netif_info(bp, drv, bp->dev,
2825 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2826 			   fw_health->primary ? "primary" : "backup", status,
2827 			   status_desc, fw_health->last_fw_reset_cnt);
2828 		if (!fw_health->enabled) {
2829 			/* Make sure tmr_counter is set and visible to
2830 			 * bnxt_health_check() before setting enabled to true.
2831 			 */
2832 			smp_wmb();
2833 			fw_health->enabled = true;
2834 		}
2835 		goto async_event_process_exit;
2836 	}
2837 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2838 		netif_notice(bp, hw, bp->dev,
2839 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2840 			     data1, data2);
2841 		goto async_event_process_exit;
2842 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2843 		struct bnxt_rx_ring_info *rxr;
2844 		u16 grp_idx;
2845 
2846 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2847 			goto async_event_process_exit;
2848 
2849 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2850 			    BNXT_EVENT_RING_TYPE(data2), data1);
2851 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2852 			goto async_event_process_exit;
2853 
2854 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2855 		if (grp_idx == INVALID_HW_RING_ID) {
2856 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2857 				    data1);
2858 			goto async_event_process_exit;
2859 		}
2860 		rxr = bp->bnapi[grp_idx]->rx_ring;
2861 		bnxt_sched_reset_rxr(bp, rxr);
2862 		goto async_event_process_exit;
2863 	}
2864 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2865 		struct bnxt_fw_health *fw_health = bp->fw_health;
2866 
2867 		netif_notice(bp, hw, bp->dev,
2868 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2869 			     data1, data2);
2870 		if (fw_health) {
2871 			fw_health->echo_req_data1 = data1;
2872 			fw_health->echo_req_data2 = data2;
2873 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2874 			break;
2875 		}
2876 		goto async_event_process_exit;
2877 	}
2878 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2879 		bnxt_ptp_pps_event(bp, data1, data2);
2880 		goto async_event_process_exit;
2881 	}
2882 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2883 		if (bnxt_event_error_report(bp, data1, data2))
2884 			break;
2885 		goto async_event_process_exit;
2886 	}
2887 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2888 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2889 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2890 			if (BNXT_PTP_USE_RTC(bp)) {
2891 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2892 				unsigned long flags;
2893 				u64 ns;
2894 
2895 				if (!ptp)
2896 					goto async_event_process_exit;
2897 
2898 				bnxt_ptp_update_current_time(bp);
2899 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2900 				       BNXT_PHC_BITS) | ptp->current_time);
2901 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2902 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2903 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2904 			}
2905 			break;
2906 		}
2907 		goto async_event_process_exit;
2908 	}
2909 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2910 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2911 
2912 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2913 		goto async_event_process_exit;
2914 	}
2915 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2916 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2917 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2918 
2919 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2920 		goto async_event_process_exit;
2921 	}
2922 	default:
2923 		goto async_event_process_exit;
2924 	}
2925 	__bnxt_queue_sp_work(bp);
2926 async_event_process_exit:
2927 	bnxt_ulp_async_events(bp, cmpl);
2928 	return 0;
2929 }
2930 
2931 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2932 {
2933 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2934 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2935 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2936 				(struct hwrm_fwd_req_cmpl *)txcmp;
2937 
2938 	switch (cmpl_type) {
2939 	case CMPL_BASE_TYPE_HWRM_DONE:
2940 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2941 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2942 		break;
2943 
2944 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2945 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2946 
2947 		if ((vf_id < bp->pf.first_vf_id) ||
2948 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2949 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2950 				   vf_id);
2951 			return -EINVAL;
2952 		}
2953 
2954 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2955 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2956 		break;
2957 
2958 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2959 		bnxt_async_event_process(bp,
2960 					 (struct hwrm_async_event_cmpl *)txcmp);
2961 		break;
2962 
2963 	default:
2964 		break;
2965 	}
2966 
2967 	return 0;
2968 }
2969 
2970 static bool bnxt_vnic_is_active(struct bnxt *bp)
2971 {
2972 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
2973 
2974 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
2975 }
2976 
2977 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2978 {
2979 	struct bnxt_napi *bnapi = dev_instance;
2980 	struct bnxt *bp = bnapi->bp;
2981 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2982 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2983 
2984 	cpr->event_ctr++;
2985 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2986 	napi_schedule(&bnapi->napi);
2987 	return IRQ_HANDLED;
2988 }
2989 
2990 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2991 {
2992 	u32 raw_cons = cpr->cp_raw_cons;
2993 	u16 cons = RING_CMP(raw_cons);
2994 	struct tx_cmp *txcmp;
2995 
2996 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2997 
2998 	return TX_CMP_VALID(txcmp, raw_cons);
2999 }
3000 
3001 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3002 			    int budget)
3003 {
3004 	struct bnxt_napi *bnapi = cpr->bnapi;
3005 	u32 raw_cons = cpr->cp_raw_cons;
3006 	bool flush_xdp = false;
3007 	u32 cons;
3008 	int rx_pkts = 0;
3009 	u8 event = 0;
3010 	struct tx_cmp *txcmp;
3011 
3012 	cpr->has_more_work = 0;
3013 	cpr->had_work_done = 1;
3014 	while (1) {
3015 		u8 cmp_type;
3016 		int rc;
3017 
3018 		cons = RING_CMP(raw_cons);
3019 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3020 
3021 		if (!TX_CMP_VALID(txcmp, raw_cons))
3022 			break;
3023 
3024 		/* The valid test of the entry must be done first before
3025 		 * reading any further.
3026 		 */
3027 		dma_rmb();
3028 		cmp_type = TX_CMP_TYPE(txcmp);
3029 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
3030 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
3031 			u32 opaque = txcmp->tx_cmp_opaque;
3032 			struct bnxt_tx_ring_info *txr;
3033 			u16 tx_freed;
3034 
3035 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
3036 			event |= BNXT_TX_CMP_EVENT;
3037 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
3038 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
3039 			else
3040 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
3041 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
3042 				   bp->tx_ring_mask;
3043 			/* return full budget so NAPI will complete. */
3044 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
3045 				rx_pkts = budget;
3046 				raw_cons = NEXT_RAW_CMP(raw_cons);
3047 				if (budget)
3048 					cpr->has_more_work = 1;
3049 				break;
3050 			}
3051 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
3052 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
3053 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
3054 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
3055 			if (likely(budget))
3056 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3057 			else
3058 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
3059 							   &event);
3060 			if (event & BNXT_REDIRECT_EVENT)
3061 				flush_xdp = true;
3062 			if (likely(rc >= 0))
3063 				rx_pkts += rc;
3064 			/* Increment rx_pkts when rc is -ENOMEM to count towards
3065 			 * the NAPI budget.  Otherwise, we may potentially loop
3066 			 * here forever if we consistently cannot allocate
3067 			 * buffers.
3068 			 */
3069 			else if (rc == -ENOMEM && budget)
3070 				rx_pkts++;
3071 			else if (rc == -EBUSY)	/* partial completion */
3072 				break;
3073 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3074 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3075 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3076 			bnxt_hwrm_handler(bp, txcmp);
3077 		}
3078 		raw_cons = NEXT_RAW_CMP(raw_cons);
3079 
3080 		if (rx_pkts && rx_pkts == budget) {
3081 			cpr->has_more_work = 1;
3082 			break;
3083 		}
3084 	}
3085 
3086 	if (flush_xdp) {
3087 		xdp_do_flush();
3088 		event &= ~BNXT_REDIRECT_EVENT;
3089 	}
3090 
3091 	if (event & BNXT_TX_EVENT) {
3092 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3093 		u16 prod = txr->tx_prod;
3094 
3095 		/* Sync BD data before updating doorbell */
3096 		wmb();
3097 
3098 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3099 		event &= ~BNXT_TX_EVENT;
3100 	}
3101 
3102 	cpr->cp_raw_cons = raw_cons;
3103 	bnapi->events |= event;
3104 	return rx_pkts;
3105 }
3106 
3107 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3108 				  int budget)
3109 {
3110 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3111 		bnapi->tx_int(bp, bnapi, budget);
3112 
3113 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3114 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3115 
3116 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3117 		bnapi->events &= ~BNXT_RX_EVENT;
3118 	}
3119 	if (bnapi->events & BNXT_AGG_EVENT) {
3120 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3121 
3122 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3123 		bnapi->events &= ~BNXT_AGG_EVENT;
3124 	}
3125 }
3126 
3127 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3128 			  int budget)
3129 {
3130 	struct bnxt_napi *bnapi = cpr->bnapi;
3131 	int rx_pkts;
3132 
3133 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3134 
3135 	/* ACK completion ring before freeing tx ring and producing new
3136 	 * buffers in rx/agg rings to prevent overflowing the completion
3137 	 * ring.
3138 	 */
3139 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3140 
3141 	__bnxt_poll_work_done(bp, bnapi, budget);
3142 	return rx_pkts;
3143 }
3144 
3145 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3146 {
3147 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3148 	struct bnxt *bp = bnapi->bp;
3149 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3150 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3151 	struct tx_cmp *txcmp;
3152 	struct rx_cmp_ext *rxcmp1;
3153 	u32 cp_cons, tmp_raw_cons;
3154 	u32 raw_cons = cpr->cp_raw_cons;
3155 	bool flush_xdp = false;
3156 	u32 rx_pkts = 0;
3157 	u8 event = 0;
3158 
3159 	while (1) {
3160 		int rc;
3161 
3162 		cp_cons = RING_CMP(raw_cons);
3163 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3164 
3165 		if (!TX_CMP_VALID(txcmp, raw_cons))
3166 			break;
3167 
3168 		/* The valid test of the entry must be done first before
3169 		 * reading any further.
3170 		 */
3171 		dma_rmb();
3172 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3173 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3174 			cp_cons = RING_CMP(tmp_raw_cons);
3175 			rxcmp1 = (struct rx_cmp_ext *)
3176 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3177 
3178 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3179 				break;
3180 
3181 			/* force an error to recycle the buffer */
3182 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3183 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3184 
3185 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3186 			if (likely(rc == -EIO) && budget)
3187 				rx_pkts++;
3188 			else if (rc == -EBUSY)	/* partial completion */
3189 				break;
3190 			if (event & BNXT_REDIRECT_EVENT)
3191 				flush_xdp = true;
3192 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3193 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3194 			bnxt_hwrm_handler(bp, txcmp);
3195 		} else {
3196 			netdev_err(bp->dev,
3197 				   "Invalid completion received on special ring\n");
3198 		}
3199 		raw_cons = NEXT_RAW_CMP(raw_cons);
3200 
3201 		if (rx_pkts == budget)
3202 			break;
3203 	}
3204 
3205 	cpr->cp_raw_cons = raw_cons;
3206 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3207 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3208 
3209 	if (event & BNXT_AGG_EVENT)
3210 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3211 	if (flush_xdp)
3212 		xdp_do_flush();
3213 
3214 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3215 		napi_complete_done(napi, rx_pkts);
3216 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3217 	}
3218 	return rx_pkts;
3219 }
3220 
3221 static int bnxt_poll(struct napi_struct *napi, int budget)
3222 {
3223 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3224 	struct bnxt *bp = bnapi->bp;
3225 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3226 	int work_done = 0;
3227 
3228 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3229 		napi_complete(napi);
3230 		return 0;
3231 	}
3232 	while (1) {
3233 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3234 
3235 		if (work_done >= budget) {
3236 			if (!budget)
3237 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3238 			break;
3239 		}
3240 
3241 		if (!bnxt_has_work(bp, cpr)) {
3242 			if (napi_complete_done(napi, work_done))
3243 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3244 			break;
3245 		}
3246 	}
3247 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3248 		struct dim_sample dim_sample = {};
3249 
3250 		dim_update_sample(cpr->event_ctr,
3251 				  cpr->rx_packets,
3252 				  cpr->rx_bytes,
3253 				  &dim_sample);
3254 		net_dim(&cpr->dim, &dim_sample);
3255 	}
3256 	return work_done;
3257 }
3258 
3259 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3260 {
3261 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3262 	int i, work_done = 0;
3263 
3264 	for (i = 0; i < cpr->cp_ring_count; i++) {
3265 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3266 
3267 		if (cpr2->had_nqe_notify) {
3268 			work_done += __bnxt_poll_work(bp, cpr2,
3269 						      budget - work_done);
3270 			cpr->has_more_work |= cpr2->has_more_work;
3271 		}
3272 	}
3273 	return work_done;
3274 }
3275 
3276 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3277 				 u64 dbr_type, int budget)
3278 {
3279 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3280 	int i;
3281 
3282 	for (i = 0; i < cpr->cp_ring_count; i++) {
3283 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3284 		struct bnxt_db_info *db;
3285 
3286 		if (cpr2->had_work_done) {
3287 			u32 tgl = 0;
3288 
3289 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3290 				cpr2->had_nqe_notify = 0;
3291 				tgl = cpr2->toggle;
3292 			}
3293 			db = &cpr2->cp_db;
3294 			bnxt_writeq(bp,
3295 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3296 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3297 				    db->doorbell);
3298 			cpr2->had_work_done = 0;
3299 		}
3300 	}
3301 	__bnxt_poll_work_done(bp, bnapi, budget);
3302 }
3303 
3304 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3305 {
3306 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3307 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3308 	struct bnxt_cp_ring_info *cpr_rx;
3309 	u32 raw_cons = cpr->cp_raw_cons;
3310 	struct bnxt *bp = bnapi->bp;
3311 	struct nqe_cn *nqcmp;
3312 	int work_done = 0;
3313 	u32 cons;
3314 
3315 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3316 		napi_complete(napi);
3317 		return 0;
3318 	}
3319 	if (cpr->has_more_work) {
3320 		cpr->has_more_work = 0;
3321 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3322 	}
3323 	while (1) {
3324 		u16 type;
3325 
3326 		cons = RING_CMP(raw_cons);
3327 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3328 
3329 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3330 			if (cpr->has_more_work)
3331 				break;
3332 
3333 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3334 					     budget);
3335 			cpr->cp_raw_cons = raw_cons;
3336 			if (napi_complete_done(napi, work_done))
3337 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3338 						  cpr->cp_raw_cons);
3339 			goto poll_done;
3340 		}
3341 
3342 		/* The valid test of the entry must be done first before
3343 		 * reading any further.
3344 		 */
3345 		dma_rmb();
3346 
3347 		type = le16_to_cpu(nqcmp->type);
3348 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3349 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3350 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3351 			struct bnxt_cp_ring_info *cpr2;
3352 
3353 			/* No more budget for RX work */
3354 			if (budget && work_done >= budget &&
3355 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3356 				break;
3357 
3358 			idx = BNXT_NQ_HDL_IDX(idx);
3359 			cpr2 = &cpr->cp_ring_arr[idx];
3360 			cpr2->had_nqe_notify = 1;
3361 			cpr2->toggle = NQE_CN_TOGGLE(type);
3362 			work_done += __bnxt_poll_work(bp, cpr2,
3363 						      budget - work_done);
3364 			cpr->has_more_work |= cpr2->has_more_work;
3365 		} else {
3366 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3367 		}
3368 		raw_cons = NEXT_RAW_CMP(raw_cons);
3369 	}
3370 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3371 	if (raw_cons != cpr->cp_raw_cons) {
3372 		cpr->cp_raw_cons = raw_cons;
3373 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3374 	}
3375 poll_done:
3376 	cpr_rx = &cpr->cp_ring_arr[0];
3377 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3378 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3379 		struct dim_sample dim_sample = {};
3380 
3381 		dim_update_sample(cpr->event_ctr,
3382 				  cpr_rx->rx_packets,
3383 				  cpr_rx->rx_bytes,
3384 				  &dim_sample);
3385 		net_dim(&cpr->dim, &dim_sample);
3386 	}
3387 	return work_done;
3388 }
3389 
3390 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp,
3391 				       struct bnxt_tx_ring_info *txr, int idx)
3392 {
3393 	int i, max_idx;
3394 	struct pci_dev *pdev = bp->pdev;
3395 
3396 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3397 
3398 	for (i = 0; i < max_idx;) {
3399 		struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i];
3400 		struct sk_buff *skb;
3401 		int j, last;
3402 
3403 		if (idx  < bp->tx_nr_rings_xdp &&
3404 		    tx_buf->action == XDP_REDIRECT) {
3405 			dma_unmap_single(&pdev->dev,
3406 					 dma_unmap_addr(tx_buf, mapping),
3407 					 dma_unmap_len(tx_buf, len),
3408 					 DMA_TO_DEVICE);
3409 			xdp_return_frame(tx_buf->xdpf);
3410 			tx_buf->action = 0;
3411 			tx_buf->xdpf = NULL;
3412 			i++;
3413 			continue;
3414 		}
3415 
3416 		skb = tx_buf->skb;
3417 		if (!skb) {
3418 			i++;
3419 			continue;
3420 		}
3421 
3422 		tx_buf->skb = NULL;
3423 
3424 		if (tx_buf->is_push) {
3425 			dev_kfree_skb(skb);
3426 			i += 2;
3427 			continue;
3428 		}
3429 
3430 		dma_unmap_single(&pdev->dev,
3431 				 dma_unmap_addr(tx_buf, mapping),
3432 				 skb_headlen(skb),
3433 				 DMA_TO_DEVICE);
3434 
3435 		last = tx_buf->nr_frags;
3436 		i += 2;
3437 		for (j = 0; j < last; j++, i++) {
3438 			int ring_idx = i & bp->tx_ring_mask;
3439 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
3440 
3441 			tx_buf = &txr->tx_buf_ring[ring_idx];
3442 			netmem_dma_unmap_page_attrs(&pdev->dev,
3443 						    dma_unmap_addr(tx_buf,
3444 								   mapping),
3445 						    skb_frag_size(frag),
3446 						    DMA_TO_DEVICE, 0);
3447 		}
3448 		dev_kfree_skb(skb);
3449 	}
3450 	netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx));
3451 }
3452 
3453 static void bnxt_free_tx_skbs(struct bnxt *bp)
3454 {
3455 	int i;
3456 
3457 	if (!bp->tx_ring)
3458 		return;
3459 
3460 	for (i = 0; i < bp->tx_nr_rings; i++) {
3461 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3462 
3463 		if (!txr->tx_buf_ring)
3464 			continue;
3465 
3466 		bnxt_free_one_tx_ring_skbs(bp, txr, i);
3467 	}
3468 
3469 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
3470 		bnxt_ptp_free_txts_skbs(bp->ptp_cfg);
3471 }
3472 
3473 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3474 {
3475 	int i, max_idx;
3476 
3477 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3478 
3479 	for (i = 0; i < max_idx; i++) {
3480 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3481 		void *data = rx_buf->data;
3482 
3483 		if (!data)
3484 			continue;
3485 
3486 		rx_buf->data = NULL;
3487 		if (BNXT_RX_PAGE_MODE(bp))
3488 			page_pool_recycle_direct(rxr->page_pool, data);
3489 		else
3490 			page_pool_free_va(rxr->head_pool, data, true);
3491 	}
3492 }
3493 
3494 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3495 {
3496 	int i, max_idx;
3497 
3498 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3499 
3500 	for (i = 0; i < max_idx; i++) {
3501 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3502 		netmem_ref netmem = rx_agg_buf->netmem;
3503 
3504 		if (!netmem)
3505 			continue;
3506 
3507 		rx_agg_buf->netmem = 0;
3508 		__clear_bit(i, rxr->rx_agg_bmap);
3509 
3510 		page_pool_recycle_direct_netmem(rxr->page_pool, netmem);
3511 	}
3512 }
3513 
3514 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3515 					struct bnxt_rx_ring_info *rxr)
3516 {
3517 	int i;
3518 
3519 	for (i = 0; i < bp->max_tpa; i++) {
3520 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3521 		u8 *data = tpa_info->data;
3522 
3523 		if (!data)
3524 			continue;
3525 
3526 		tpa_info->data = NULL;
3527 		page_pool_free_va(rxr->head_pool, data, false);
3528 	}
3529 }
3530 
3531 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3532 				       struct bnxt_rx_ring_info *rxr)
3533 {
3534 	struct bnxt_tpa_idx_map *map;
3535 
3536 	if (!rxr->rx_tpa)
3537 		goto skip_rx_tpa_free;
3538 
3539 	bnxt_free_one_tpa_info_data(bp, rxr);
3540 
3541 skip_rx_tpa_free:
3542 	if (!rxr->rx_buf_ring)
3543 		goto skip_rx_buf_free;
3544 
3545 	bnxt_free_one_rx_ring(bp, rxr);
3546 
3547 skip_rx_buf_free:
3548 	if (!rxr->rx_agg_ring)
3549 		goto skip_rx_agg_free;
3550 
3551 	bnxt_free_one_rx_agg_ring(bp, rxr);
3552 
3553 skip_rx_agg_free:
3554 	map = rxr->rx_tpa_idx_map;
3555 	if (map)
3556 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3557 }
3558 
3559 static void bnxt_free_rx_skbs(struct bnxt *bp)
3560 {
3561 	int i;
3562 
3563 	if (!bp->rx_ring)
3564 		return;
3565 
3566 	for (i = 0; i < bp->rx_nr_rings; i++)
3567 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3568 }
3569 
3570 static void bnxt_free_skbs(struct bnxt *bp)
3571 {
3572 	bnxt_free_tx_skbs(bp);
3573 	bnxt_free_rx_skbs(bp);
3574 }
3575 
3576 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3577 {
3578 	u8 init_val = ctxm->init_value;
3579 	u16 offset = ctxm->init_offset;
3580 	u8 *p2 = p;
3581 	int i;
3582 
3583 	if (!init_val)
3584 		return;
3585 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3586 		memset(p, init_val, len);
3587 		return;
3588 	}
3589 	for (i = 0; i < len; i += ctxm->entry_size)
3590 		*(p2 + i + offset) = init_val;
3591 }
3592 
3593 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3594 			       void *buf, size_t offset, size_t head,
3595 			       size_t tail)
3596 {
3597 	int i, head_page, start_idx, source_offset;
3598 	size_t len, rem_len, total_len, max_bytes;
3599 
3600 	head_page = head / rmem->page_size;
3601 	source_offset = head % rmem->page_size;
3602 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3603 	if (!total_len)
3604 		total_len = MAX_CTX_BYTES;
3605 	start_idx = head_page % MAX_CTX_PAGES;
3606 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3607 		    source_offset;
3608 	total_len = min(total_len, max_bytes);
3609 	rem_len = total_len;
3610 
3611 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3612 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3613 		if (buf)
3614 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3615 			       len);
3616 		offset += len;
3617 		rem_len -= len;
3618 	}
3619 	return total_len;
3620 }
3621 
3622 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3623 {
3624 	struct pci_dev *pdev = bp->pdev;
3625 	int i;
3626 
3627 	if (!rmem->pg_arr)
3628 		goto skip_pages;
3629 
3630 	for (i = 0; i < rmem->nr_pages; i++) {
3631 		if (!rmem->pg_arr[i])
3632 			continue;
3633 
3634 		dma_free_coherent(&pdev->dev, rmem->page_size,
3635 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3636 
3637 		rmem->pg_arr[i] = NULL;
3638 	}
3639 skip_pages:
3640 	if (rmem->pg_tbl) {
3641 		size_t pg_tbl_size = rmem->nr_pages * 8;
3642 
3643 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3644 			pg_tbl_size = rmem->page_size;
3645 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3646 				  rmem->pg_tbl, rmem->pg_tbl_map);
3647 		rmem->pg_tbl = NULL;
3648 	}
3649 	if (rmem->vmem_size && *rmem->vmem) {
3650 		vfree(*rmem->vmem);
3651 		*rmem->vmem = NULL;
3652 	}
3653 }
3654 
3655 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3656 {
3657 	struct pci_dev *pdev = bp->pdev;
3658 	u64 valid_bit = 0;
3659 	int i;
3660 
3661 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3662 		valid_bit = PTU_PTE_VALID;
3663 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3664 		size_t pg_tbl_size = rmem->nr_pages * 8;
3665 
3666 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3667 			pg_tbl_size = rmem->page_size;
3668 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3669 						  &rmem->pg_tbl_map,
3670 						  GFP_KERNEL);
3671 		if (!rmem->pg_tbl)
3672 			return -ENOMEM;
3673 	}
3674 
3675 	for (i = 0; i < rmem->nr_pages; i++) {
3676 		u64 extra_bits = valid_bit;
3677 
3678 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3679 						     rmem->page_size,
3680 						     &rmem->dma_arr[i],
3681 						     GFP_KERNEL);
3682 		if (!rmem->pg_arr[i])
3683 			return -ENOMEM;
3684 
3685 		if (rmem->ctx_mem)
3686 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3687 					  rmem->page_size);
3688 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3689 			if (i == rmem->nr_pages - 2 &&
3690 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3691 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3692 			else if (i == rmem->nr_pages - 1 &&
3693 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3694 				extra_bits |= PTU_PTE_LAST;
3695 			rmem->pg_tbl[i] =
3696 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3697 		}
3698 	}
3699 
3700 	if (rmem->vmem_size) {
3701 		*rmem->vmem = vzalloc(rmem->vmem_size);
3702 		if (!(*rmem->vmem))
3703 			return -ENOMEM;
3704 	}
3705 	return 0;
3706 }
3707 
3708 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3709 				   struct bnxt_rx_ring_info *rxr)
3710 {
3711 	int i;
3712 
3713 	kfree(rxr->rx_tpa_idx_map);
3714 	rxr->rx_tpa_idx_map = NULL;
3715 	if (rxr->rx_tpa) {
3716 		for (i = 0; i < bp->max_tpa; i++) {
3717 			kfree(rxr->rx_tpa[i].agg_arr);
3718 			rxr->rx_tpa[i].agg_arr = NULL;
3719 		}
3720 	}
3721 	kfree(rxr->rx_tpa);
3722 	rxr->rx_tpa = NULL;
3723 }
3724 
3725 static void bnxt_free_tpa_info(struct bnxt *bp)
3726 {
3727 	int i;
3728 
3729 	for (i = 0; i < bp->rx_nr_rings; i++) {
3730 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3731 
3732 		bnxt_free_one_tpa_info(bp, rxr);
3733 	}
3734 }
3735 
3736 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3737 				   struct bnxt_rx_ring_info *rxr)
3738 {
3739 	struct rx_agg_cmp *agg;
3740 	int i;
3741 
3742 	rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3743 			      GFP_KERNEL);
3744 	if (!rxr->rx_tpa)
3745 		return -ENOMEM;
3746 
3747 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3748 		return 0;
3749 	for (i = 0; i < bp->max_tpa; i++) {
3750 		agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3751 		if (!agg)
3752 			return -ENOMEM;
3753 		rxr->rx_tpa[i].agg_arr = agg;
3754 	}
3755 	rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3756 				      GFP_KERNEL);
3757 	if (!rxr->rx_tpa_idx_map)
3758 		return -ENOMEM;
3759 
3760 	return 0;
3761 }
3762 
3763 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3764 {
3765 	int i, rc;
3766 
3767 	bp->max_tpa = MAX_TPA;
3768 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3769 		if (!bp->max_tpa_v2)
3770 			return 0;
3771 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3772 	}
3773 
3774 	for (i = 0; i < bp->rx_nr_rings; i++) {
3775 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3776 
3777 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3778 		if (rc)
3779 			return rc;
3780 	}
3781 	return 0;
3782 }
3783 
3784 static void bnxt_free_rx_rings(struct bnxt *bp)
3785 {
3786 	int i;
3787 
3788 	if (!bp->rx_ring)
3789 		return;
3790 
3791 	bnxt_free_tpa_info(bp);
3792 	for (i = 0; i < bp->rx_nr_rings; i++) {
3793 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3794 		struct bnxt_ring_struct *ring;
3795 
3796 		if (rxr->xdp_prog)
3797 			bpf_prog_put(rxr->xdp_prog);
3798 
3799 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3800 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3801 
3802 		page_pool_destroy(rxr->page_pool);
3803 		page_pool_destroy(rxr->head_pool);
3804 		rxr->page_pool = rxr->head_pool = NULL;
3805 
3806 		kfree(rxr->rx_agg_bmap);
3807 		rxr->rx_agg_bmap = NULL;
3808 
3809 		ring = &rxr->rx_ring_struct;
3810 		bnxt_free_ring(bp, &ring->ring_mem);
3811 
3812 		ring = &rxr->rx_agg_ring_struct;
3813 		bnxt_free_ring(bp, &ring->ring_mem);
3814 	}
3815 }
3816 
3817 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3818 				   struct bnxt_rx_ring_info *rxr,
3819 				   int numa_node)
3820 {
3821 	const unsigned int agg_size_fac = PAGE_SIZE / BNXT_RX_PAGE_SIZE;
3822 	const unsigned int rx_size_fac = PAGE_SIZE / SZ_4K;
3823 	struct page_pool_params pp = { 0 };
3824 	struct page_pool *pool;
3825 
3826 	pp.pool_size = bp->rx_agg_ring_size / agg_size_fac;
3827 	if (BNXT_RX_PAGE_MODE(bp))
3828 		pp.pool_size += bp->rx_ring_size / rx_size_fac;
3829 	pp.nid = numa_node;
3830 	pp.netdev = bp->dev;
3831 	pp.dev = &bp->pdev->dev;
3832 	pp.dma_dir = bp->rx_dir;
3833 	pp.max_len = PAGE_SIZE;
3834 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV |
3835 		   PP_FLAG_ALLOW_UNREADABLE_NETMEM;
3836 	pp.queue_idx = rxr->bnapi->index;
3837 
3838 	pool = page_pool_create(&pp);
3839 	if (IS_ERR(pool))
3840 		return PTR_ERR(pool);
3841 	rxr->page_pool = pool;
3842 
3843 	rxr->need_head_pool = page_pool_is_unreadable(pool);
3844 	if (bnxt_separate_head_pool(rxr)) {
3845 		pp.pool_size = min(bp->rx_ring_size / rx_size_fac, 1024);
3846 		pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3847 		pool = page_pool_create(&pp);
3848 		if (IS_ERR(pool))
3849 			goto err_destroy_pp;
3850 	} else {
3851 		page_pool_get(pool);
3852 	}
3853 	rxr->head_pool = pool;
3854 
3855 	return 0;
3856 
3857 err_destroy_pp:
3858 	page_pool_destroy(rxr->page_pool);
3859 	rxr->page_pool = NULL;
3860 	return PTR_ERR(pool);
3861 }
3862 
3863 static void bnxt_enable_rx_page_pool(struct bnxt_rx_ring_info *rxr)
3864 {
3865 	page_pool_enable_direct_recycling(rxr->head_pool, &rxr->bnapi->napi);
3866 	page_pool_enable_direct_recycling(rxr->page_pool, &rxr->bnapi->napi);
3867 }
3868 
3869 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3870 {
3871 	u16 mem_size;
3872 
3873 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3874 	mem_size = rxr->rx_agg_bmap_size / 8;
3875 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3876 	if (!rxr->rx_agg_bmap)
3877 		return -ENOMEM;
3878 
3879 	return 0;
3880 }
3881 
3882 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3883 {
3884 	int numa_node = dev_to_node(&bp->pdev->dev);
3885 	int i, rc = 0, agg_rings = 0, cpu;
3886 
3887 	if (!bp->rx_ring)
3888 		return -ENOMEM;
3889 
3890 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3891 		agg_rings = 1;
3892 
3893 	for (i = 0; i < bp->rx_nr_rings; i++) {
3894 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3895 		struct bnxt_ring_struct *ring;
3896 		int cpu_node;
3897 
3898 		ring = &rxr->rx_ring_struct;
3899 
3900 		cpu = cpumask_local_spread(i, numa_node);
3901 		cpu_node = cpu_to_node(cpu);
3902 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3903 			   i, cpu_node);
3904 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3905 		if (rc)
3906 			return rc;
3907 		bnxt_enable_rx_page_pool(rxr);
3908 
3909 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3910 		if (rc < 0)
3911 			return rc;
3912 
3913 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3914 						MEM_TYPE_PAGE_POOL,
3915 						rxr->page_pool);
3916 		if (rc) {
3917 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3918 			return rc;
3919 		}
3920 
3921 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3922 		if (rc)
3923 			return rc;
3924 
3925 		ring->grp_idx = i;
3926 		if (agg_rings) {
3927 			ring = &rxr->rx_agg_ring_struct;
3928 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3929 			if (rc)
3930 				return rc;
3931 
3932 			ring->grp_idx = i;
3933 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3934 			if (rc)
3935 				return rc;
3936 		}
3937 	}
3938 	if (bp->flags & BNXT_FLAG_TPA)
3939 		rc = bnxt_alloc_tpa_info(bp);
3940 	return rc;
3941 }
3942 
3943 static void bnxt_free_tx_rings(struct bnxt *bp)
3944 {
3945 	int i;
3946 	struct pci_dev *pdev = bp->pdev;
3947 
3948 	if (!bp->tx_ring)
3949 		return;
3950 
3951 	for (i = 0; i < bp->tx_nr_rings; i++) {
3952 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3953 		struct bnxt_ring_struct *ring;
3954 
3955 		if (txr->tx_push) {
3956 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3957 					  txr->tx_push, txr->tx_push_mapping);
3958 			txr->tx_push = NULL;
3959 		}
3960 
3961 		ring = &txr->tx_ring_struct;
3962 
3963 		bnxt_free_ring(bp, &ring->ring_mem);
3964 	}
3965 }
3966 
3967 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3968 	((tc) * (bp)->tx_nr_rings_per_tc)
3969 
3970 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3971 	((tx) % (bp)->tx_nr_rings_per_tc)
3972 
3973 #define BNXT_RING_TO_TC(bp, tx)		\
3974 	((tx) / (bp)->tx_nr_rings_per_tc)
3975 
3976 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3977 {
3978 	int i, j, rc;
3979 	struct pci_dev *pdev = bp->pdev;
3980 
3981 	bp->tx_push_size = 0;
3982 	if (bp->tx_push_thresh) {
3983 		int push_size;
3984 
3985 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3986 					bp->tx_push_thresh);
3987 
3988 		if (push_size > 256) {
3989 			push_size = 0;
3990 			bp->tx_push_thresh = 0;
3991 		}
3992 
3993 		bp->tx_push_size = push_size;
3994 	}
3995 
3996 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3997 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3998 		struct bnxt_ring_struct *ring;
3999 		u8 qidx;
4000 
4001 		ring = &txr->tx_ring_struct;
4002 
4003 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4004 		if (rc)
4005 			return rc;
4006 
4007 		ring->grp_idx = txr->bnapi->index;
4008 		if (bp->tx_push_size) {
4009 			dma_addr_t mapping;
4010 
4011 			/* One pre-allocated DMA buffer to backup
4012 			 * TX push operation
4013 			 */
4014 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
4015 						bp->tx_push_size,
4016 						&txr->tx_push_mapping,
4017 						GFP_KERNEL);
4018 
4019 			if (!txr->tx_push)
4020 				return -ENOMEM;
4021 
4022 			mapping = txr->tx_push_mapping +
4023 				sizeof(struct tx_push_bd);
4024 			txr->data_mapping = cpu_to_le64(mapping);
4025 		}
4026 		qidx = bp->tc_to_qidx[j];
4027 		ring->queue_id = bp->q_info[qidx].queue_id;
4028 		spin_lock_init(&txr->xdp_tx_lock);
4029 		if (i < bp->tx_nr_rings_xdp)
4030 			continue;
4031 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
4032 			j++;
4033 	}
4034 	return 0;
4035 }
4036 
4037 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
4038 {
4039 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4040 
4041 	kfree(cpr->cp_desc_ring);
4042 	cpr->cp_desc_ring = NULL;
4043 	ring->ring_mem.pg_arr = NULL;
4044 	kfree(cpr->cp_desc_mapping);
4045 	cpr->cp_desc_mapping = NULL;
4046 	ring->ring_mem.dma_arr = NULL;
4047 }
4048 
4049 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
4050 {
4051 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
4052 	if (!cpr->cp_desc_ring)
4053 		return -ENOMEM;
4054 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
4055 				       GFP_KERNEL);
4056 	if (!cpr->cp_desc_mapping)
4057 		return -ENOMEM;
4058 	return 0;
4059 }
4060 
4061 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
4062 {
4063 	int i;
4064 
4065 	if (!bp->bnapi)
4066 		return;
4067 	for (i = 0; i < bp->cp_nr_rings; i++) {
4068 		struct bnxt_napi *bnapi = bp->bnapi[i];
4069 
4070 		if (!bnapi)
4071 			continue;
4072 		bnxt_free_cp_arrays(&bnapi->cp_ring);
4073 	}
4074 }
4075 
4076 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
4077 {
4078 	int i, n = bp->cp_nr_pages;
4079 
4080 	for (i = 0; i < bp->cp_nr_rings; i++) {
4081 		struct bnxt_napi *bnapi = bp->bnapi[i];
4082 		int rc;
4083 
4084 		if (!bnapi)
4085 			continue;
4086 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
4087 		if (rc)
4088 			return rc;
4089 	}
4090 	return 0;
4091 }
4092 
4093 static void bnxt_free_cp_rings(struct bnxt *bp)
4094 {
4095 	int i;
4096 
4097 	if (!bp->bnapi)
4098 		return;
4099 
4100 	for (i = 0; i < bp->cp_nr_rings; i++) {
4101 		struct bnxt_napi *bnapi = bp->bnapi[i];
4102 		struct bnxt_cp_ring_info *cpr;
4103 		struct bnxt_ring_struct *ring;
4104 		int j;
4105 
4106 		if (!bnapi)
4107 			continue;
4108 
4109 		cpr = &bnapi->cp_ring;
4110 		ring = &cpr->cp_ring_struct;
4111 
4112 		bnxt_free_ring(bp, &ring->ring_mem);
4113 
4114 		if (!cpr->cp_ring_arr)
4115 			continue;
4116 
4117 		for (j = 0; j < cpr->cp_ring_count; j++) {
4118 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4119 
4120 			ring = &cpr2->cp_ring_struct;
4121 			bnxt_free_ring(bp, &ring->ring_mem);
4122 			bnxt_free_cp_arrays(cpr2);
4123 		}
4124 		kfree(cpr->cp_ring_arr);
4125 		cpr->cp_ring_arr = NULL;
4126 		cpr->cp_ring_count = 0;
4127 	}
4128 }
4129 
4130 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4131 				  struct bnxt_cp_ring_info *cpr)
4132 {
4133 	struct bnxt_ring_mem_info *rmem;
4134 	struct bnxt_ring_struct *ring;
4135 	int rc;
4136 
4137 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4138 	if (rc) {
4139 		bnxt_free_cp_arrays(cpr);
4140 		return -ENOMEM;
4141 	}
4142 	ring = &cpr->cp_ring_struct;
4143 	rmem = &ring->ring_mem;
4144 	rmem->nr_pages = bp->cp_nr_pages;
4145 	rmem->page_size = HW_CMPD_RING_SIZE;
4146 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4147 	rmem->dma_arr = cpr->cp_desc_mapping;
4148 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4149 	rc = bnxt_alloc_ring(bp, rmem);
4150 	if (rc) {
4151 		bnxt_free_ring(bp, rmem);
4152 		bnxt_free_cp_arrays(cpr);
4153 	}
4154 	return rc;
4155 }
4156 
4157 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4158 {
4159 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4160 	int i, j, rc, ulp_msix;
4161 	int tcs = bp->num_tc;
4162 
4163 	if (!tcs)
4164 		tcs = 1;
4165 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4166 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4167 		struct bnxt_napi *bnapi = bp->bnapi[i];
4168 		struct bnxt_cp_ring_info *cpr, *cpr2;
4169 		struct bnxt_ring_struct *ring;
4170 		int cp_count = 0, k;
4171 		int rx = 0, tx = 0;
4172 
4173 		if (!bnapi)
4174 			continue;
4175 
4176 		cpr = &bnapi->cp_ring;
4177 		cpr->bnapi = bnapi;
4178 		ring = &cpr->cp_ring_struct;
4179 
4180 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4181 		if (rc)
4182 			return rc;
4183 
4184 		ring->map_idx = ulp_msix + i;
4185 
4186 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4187 			continue;
4188 
4189 		if (i < bp->rx_nr_rings) {
4190 			cp_count++;
4191 			rx = 1;
4192 		}
4193 		if (i < bp->tx_nr_rings_xdp) {
4194 			cp_count++;
4195 			tx = 1;
4196 		} else if ((sh && i < bp->tx_nr_rings) ||
4197 			 (!sh && i >= bp->rx_nr_rings)) {
4198 			cp_count += tcs;
4199 			tx = 1;
4200 		}
4201 
4202 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
4203 					   GFP_KERNEL);
4204 		if (!cpr->cp_ring_arr)
4205 			return -ENOMEM;
4206 		cpr->cp_ring_count = cp_count;
4207 
4208 		for (k = 0; k < cp_count; k++) {
4209 			cpr2 = &cpr->cp_ring_arr[k];
4210 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4211 			if (rc)
4212 				return rc;
4213 			cpr2->bnapi = bnapi;
4214 			cpr2->sw_stats = cpr->sw_stats;
4215 			cpr2->cp_idx = k;
4216 			if (!k && rx) {
4217 				bp->rx_ring[i].rx_cpr = cpr2;
4218 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4219 			} else {
4220 				int n, tc = k - rx;
4221 
4222 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4223 				bp->tx_ring[n].tx_cpr = cpr2;
4224 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4225 			}
4226 		}
4227 		if (tx)
4228 			j++;
4229 	}
4230 	return 0;
4231 }
4232 
4233 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4234 				     struct bnxt_rx_ring_info *rxr)
4235 {
4236 	struct bnxt_ring_mem_info *rmem;
4237 	struct bnxt_ring_struct *ring;
4238 
4239 	ring = &rxr->rx_ring_struct;
4240 	rmem = &ring->ring_mem;
4241 	rmem->nr_pages = bp->rx_nr_pages;
4242 	rmem->page_size = HW_RXBD_RING_SIZE;
4243 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4244 	rmem->dma_arr = rxr->rx_desc_mapping;
4245 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4246 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4247 
4248 	ring = &rxr->rx_agg_ring_struct;
4249 	rmem = &ring->ring_mem;
4250 	rmem->nr_pages = bp->rx_agg_nr_pages;
4251 	rmem->page_size = HW_RXBD_RING_SIZE;
4252 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4253 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4254 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4255 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4256 }
4257 
4258 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4259 				      struct bnxt_rx_ring_info *rxr)
4260 {
4261 	struct bnxt_ring_mem_info *rmem;
4262 	struct bnxt_ring_struct *ring;
4263 	int i;
4264 
4265 	rxr->page_pool->p.napi = NULL;
4266 	rxr->page_pool = NULL;
4267 	rxr->head_pool->p.napi = NULL;
4268 	rxr->head_pool = NULL;
4269 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4270 
4271 	ring = &rxr->rx_ring_struct;
4272 	rmem = &ring->ring_mem;
4273 	rmem->pg_tbl = NULL;
4274 	rmem->pg_tbl_map = 0;
4275 	for (i = 0; i < rmem->nr_pages; i++) {
4276 		rmem->pg_arr[i] = NULL;
4277 		rmem->dma_arr[i] = 0;
4278 	}
4279 	*rmem->vmem = NULL;
4280 
4281 	ring = &rxr->rx_agg_ring_struct;
4282 	rmem = &ring->ring_mem;
4283 	rmem->pg_tbl = NULL;
4284 	rmem->pg_tbl_map = 0;
4285 	for (i = 0; i < rmem->nr_pages; i++) {
4286 		rmem->pg_arr[i] = NULL;
4287 		rmem->dma_arr[i] = 0;
4288 	}
4289 	*rmem->vmem = NULL;
4290 }
4291 
4292 static void bnxt_init_ring_struct(struct bnxt *bp)
4293 {
4294 	int i, j;
4295 
4296 	for (i = 0; i < bp->cp_nr_rings; i++) {
4297 		struct bnxt_napi *bnapi = bp->bnapi[i];
4298 		struct bnxt_ring_mem_info *rmem;
4299 		struct bnxt_cp_ring_info *cpr;
4300 		struct bnxt_rx_ring_info *rxr;
4301 		struct bnxt_tx_ring_info *txr;
4302 		struct bnxt_ring_struct *ring;
4303 
4304 		if (!bnapi)
4305 			continue;
4306 
4307 		cpr = &bnapi->cp_ring;
4308 		ring = &cpr->cp_ring_struct;
4309 		rmem = &ring->ring_mem;
4310 		rmem->nr_pages = bp->cp_nr_pages;
4311 		rmem->page_size = HW_CMPD_RING_SIZE;
4312 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4313 		rmem->dma_arr = cpr->cp_desc_mapping;
4314 		rmem->vmem_size = 0;
4315 
4316 		rxr = bnapi->rx_ring;
4317 		if (!rxr)
4318 			goto skip_rx;
4319 
4320 		ring = &rxr->rx_ring_struct;
4321 		rmem = &ring->ring_mem;
4322 		rmem->nr_pages = bp->rx_nr_pages;
4323 		rmem->page_size = HW_RXBD_RING_SIZE;
4324 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4325 		rmem->dma_arr = rxr->rx_desc_mapping;
4326 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4327 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4328 
4329 		ring = &rxr->rx_agg_ring_struct;
4330 		rmem = &ring->ring_mem;
4331 		rmem->nr_pages = bp->rx_agg_nr_pages;
4332 		rmem->page_size = HW_RXBD_RING_SIZE;
4333 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4334 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4335 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4336 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4337 
4338 skip_rx:
4339 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4340 			ring = &txr->tx_ring_struct;
4341 			rmem = &ring->ring_mem;
4342 			rmem->nr_pages = bp->tx_nr_pages;
4343 			rmem->page_size = HW_TXBD_RING_SIZE;
4344 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4345 			rmem->dma_arr = txr->tx_desc_mapping;
4346 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4347 			rmem->vmem = (void **)&txr->tx_buf_ring;
4348 		}
4349 	}
4350 }
4351 
4352 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4353 {
4354 	int i;
4355 	u32 prod;
4356 	struct rx_bd **rx_buf_ring;
4357 
4358 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4359 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4360 		int j;
4361 		struct rx_bd *rxbd;
4362 
4363 		rxbd = rx_buf_ring[i];
4364 		if (!rxbd)
4365 			continue;
4366 
4367 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4368 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4369 			rxbd->rx_bd_opaque = prod;
4370 		}
4371 	}
4372 }
4373 
4374 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4375 				       struct bnxt_rx_ring_info *rxr,
4376 				       int ring_nr)
4377 {
4378 	u32 prod;
4379 	int i;
4380 
4381 	prod = rxr->rx_prod;
4382 	for (i = 0; i < bp->rx_ring_size; i++) {
4383 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4384 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4385 				    ring_nr, i, bp->rx_ring_size);
4386 			break;
4387 		}
4388 		prod = NEXT_RX(prod);
4389 	}
4390 	rxr->rx_prod = prod;
4391 }
4392 
4393 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp,
4394 					  struct bnxt_rx_ring_info *rxr,
4395 					  int ring_nr)
4396 {
4397 	u32 prod;
4398 	int i;
4399 
4400 	prod = rxr->rx_agg_prod;
4401 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4402 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) {
4403 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4404 				    ring_nr, i, bp->rx_ring_size);
4405 			break;
4406 		}
4407 		prod = NEXT_RX_AGG(prod);
4408 	}
4409 	rxr->rx_agg_prod = prod;
4410 }
4411 
4412 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4413 					struct bnxt_rx_ring_info *rxr)
4414 {
4415 	dma_addr_t mapping;
4416 	u8 *data;
4417 	int i;
4418 
4419 	for (i = 0; i < bp->max_tpa; i++) {
4420 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4421 					    GFP_KERNEL);
4422 		if (!data)
4423 			return -ENOMEM;
4424 
4425 		rxr->rx_tpa[i].data = data;
4426 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4427 		rxr->rx_tpa[i].mapping = mapping;
4428 	}
4429 
4430 	return 0;
4431 }
4432 
4433 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4434 {
4435 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4436 	int rc;
4437 
4438 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4439 
4440 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4441 		return 0;
4442 
4443 	bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr);
4444 
4445 	if (rxr->rx_tpa) {
4446 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4447 		if (rc)
4448 			return rc;
4449 	}
4450 	return 0;
4451 }
4452 
4453 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4454 				       struct bnxt_rx_ring_info *rxr)
4455 {
4456 	struct bnxt_ring_struct *ring;
4457 	u32 type;
4458 
4459 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4460 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4461 
4462 	if (NET_IP_ALIGN == 2)
4463 		type |= RX_BD_FLAGS_SOP;
4464 
4465 	ring = &rxr->rx_ring_struct;
4466 	bnxt_init_rxbd_pages(ring, type);
4467 	ring->fw_ring_id = INVALID_HW_RING_ID;
4468 }
4469 
4470 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4471 					   struct bnxt_rx_ring_info *rxr)
4472 {
4473 	struct bnxt_ring_struct *ring;
4474 	u32 type;
4475 
4476 	ring = &rxr->rx_agg_ring_struct;
4477 	ring->fw_ring_id = INVALID_HW_RING_ID;
4478 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4479 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4480 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4481 
4482 		bnxt_init_rxbd_pages(ring, type);
4483 	}
4484 }
4485 
4486 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4487 {
4488 	struct bnxt_rx_ring_info *rxr;
4489 
4490 	rxr = &bp->rx_ring[ring_nr];
4491 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4492 
4493 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4494 			     &rxr->bnapi->napi);
4495 
4496 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4497 		bpf_prog_add(bp->xdp_prog, 1);
4498 		rxr->xdp_prog = bp->xdp_prog;
4499 	}
4500 
4501 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4502 
4503 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4504 }
4505 
4506 static void bnxt_init_cp_rings(struct bnxt *bp)
4507 {
4508 	int i, j;
4509 
4510 	for (i = 0; i < bp->cp_nr_rings; i++) {
4511 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4512 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4513 
4514 		ring->fw_ring_id = INVALID_HW_RING_ID;
4515 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4516 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4517 		if (!cpr->cp_ring_arr)
4518 			continue;
4519 		for (j = 0; j < cpr->cp_ring_count; j++) {
4520 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4521 
4522 			ring = &cpr2->cp_ring_struct;
4523 			ring->fw_ring_id = INVALID_HW_RING_ID;
4524 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4525 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4526 		}
4527 	}
4528 }
4529 
4530 static int bnxt_init_rx_rings(struct bnxt *bp)
4531 {
4532 	int i, rc = 0;
4533 
4534 	if (BNXT_RX_PAGE_MODE(bp)) {
4535 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4536 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4537 	} else {
4538 		bp->rx_offset = BNXT_RX_OFFSET;
4539 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4540 	}
4541 
4542 	for (i = 0; i < bp->rx_nr_rings; i++) {
4543 		rc = bnxt_init_one_rx_ring(bp, i);
4544 		if (rc)
4545 			break;
4546 	}
4547 
4548 	return rc;
4549 }
4550 
4551 static int bnxt_init_tx_rings(struct bnxt *bp)
4552 {
4553 	u16 i;
4554 
4555 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4556 				   BNXT_MIN_TX_DESC_CNT);
4557 
4558 	for (i = 0; i < bp->tx_nr_rings; i++) {
4559 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4560 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4561 
4562 		ring->fw_ring_id = INVALID_HW_RING_ID;
4563 
4564 		if (i >= bp->tx_nr_rings_xdp)
4565 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4566 					     NETDEV_QUEUE_TYPE_TX,
4567 					     &txr->bnapi->napi);
4568 	}
4569 
4570 	return 0;
4571 }
4572 
4573 static void bnxt_free_ring_grps(struct bnxt *bp)
4574 {
4575 	kfree(bp->grp_info);
4576 	bp->grp_info = NULL;
4577 }
4578 
4579 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4580 {
4581 	int i;
4582 
4583 	if (irq_re_init) {
4584 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4585 				       sizeof(struct bnxt_ring_grp_info),
4586 				       GFP_KERNEL);
4587 		if (!bp->grp_info)
4588 			return -ENOMEM;
4589 	}
4590 	for (i = 0; i < bp->cp_nr_rings; i++) {
4591 		if (irq_re_init)
4592 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4593 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4594 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4595 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4596 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4597 	}
4598 	return 0;
4599 }
4600 
4601 static void bnxt_free_vnics(struct bnxt *bp)
4602 {
4603 	kfree(bp->vnic_info);
4604 	bp->vnic_info = NULL;
4605 	bp->nr_vnics = 0;
4606 }
4607 
4608 static int bnxt_alloc_vnics(struct bnxt *bp)
4609 {
4610 	int num_vnics = 1;
4611 
4612 #ifdef CONFIG_RFS_ACCEL
4613 	if (bp->flags & BNXT_FLAG_RFS) {
4614 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4615 			num_vnics++;
4616 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4617 			num_vnics += bp->rx_nr_rings;
4618 	}
4619 #endif
4620 
4621 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4622 		num_vnics++;
4623 
4624 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4625 				GFP_KERNEL);
4626 	if (!bp->vnic_info)
4627 		return -ENOMEM;
4628 
4629 	bp->nr_vnics = num_vnics;
4630 	return 0;
4631 }
4632 
4633 static void bnxt_init_vnics(struct bnxt *bp)
4634 {
4635 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4636 	int i;
4637 
4638 	for (i = 0; i < bp->nr_vnics; i++) {
4639 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4640 		int j;
4641 
4642 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4643 		vnic->vnic_id = i;
4644 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4645 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4646 
4647 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4648 
4649 		if (bp->vnic_info[i].rss_hash_key) {
4650 			if (i == BNXT_VNIC_DEFAULT) {
4651 				u8 *key = (void *)vnic->rss_hash_key;
4652 				int k;
4653 
4654 				if (!bp->rss_hash_key_valid &&
4655 				    !bp->rss_hash_key_updated) {
4656 					get_random_bytes(bp->rss_hash_key,
4657 							 HW_HASH_KEY_SIZE);
4658 					bp->rss_hash_key_updated = true;
4659 				}
4660 
4661 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4662 				       HW_HASH_KEY_SIZE);
4663 
4664 				if (!bp->rss_hash_key_updated)
4665 					continue;
4666 
4667 				bp->rss_hash_key_updated = false;
4668 				bp->rss_hash_key_valid = true;
4669 
4670 				bp->toeplitz_prefix = 0;
4671 				for (k = 0; k < 8; k++) {
4672 					bp->toeplitz_prefix <<= 8;
4673 					bp->toeplitz_prefix |= key[k];
4674 				}
4675 			} else {
4676 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4677 				       HW_HASH_KEY_SIZE);
4678 			}
4679 		}
4680 	}
4681 }
4682 
4683 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4684 {
4685 	int pages;
4686 
4687 	pages = ring_size / desc_per_pg;
4688 
4689 	if (!pages)
4690 		return 1;
4691 
4692 	pages++;
4693 
4694 	while (pages & (pages - 1))
4695 		pages++;
4696 
4697 	return pages;
4698 }
4699 
4700 void bnxt_set_tpa_flags(struct bnxt *bp)
4701 {
4702 	bp->flags &= ~BNXT_FLAG_TPA;
4703 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4704 		return;
4705 	if (bp->dev->features & NETIF_F_LRO)
4706 		bp->flags |= BNXT_FLAG_LRO;
4707 	else if (bp->dev->features & NETIF_F_GRO_HW)
4708 		bp->flags |= BNXT_FLAG_GRO;
4709 }
4710 
4711 static void bnxt_init_ring_params(struct bnxt *bp)
4712 {
4713 	unsigned int rx_size;
4714 
4715 	bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK;
4716 	/* Try to fit 4 chunks into a 4k page */
4717 	rx_size = SZ_1K -
4718 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4719 	bp->dev->cfg->hds_thresh = max(BNXT_DEFAULT_RX_COPYBREAK, rx_size);
4720 }
4721 
4722 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4723  * be set on entry.
4724  */
4725 void bnxt_set_ring_params(struct bnxt *bp)
4726 {
4727 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4728 	u32 agg_factor = 0, agg_ring_size = 0;
4729 
4730 	/* 8 for CRC and VLAN */
4731 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4732 
4733 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4734 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4735 
4736 	ring_size = bp->rx_ring_size;
4737 	bp->rx_agg_ring_size = 0;
4738 	bp->rx_agg_nr_pages = 0;
4739 
4740 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4741 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4742 
4743 	bp->flags &= ~BNXT_FLAG_JUMBO;
4744 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4745 		u32 jumbo_factor;
4746 
4747 		bp->flags |= BNXT_FLAG_JUMBO;
4748 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4749 		if (jumbo_factor > agg_factor)
4750 			agg_factor = jumbo_factor;
4751 	}
4752 	if (agg_factor) {
4753 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4754 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4755 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4756 				    bp->rx_ring_size, ring_size);
4757 			bp->rx_ring_size = ring_size;
4758 		}
4759 		agg_ring_size = ring_size * agg_factor;
4760 
4761 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4762 							RX_DESC_CNT);
4763 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4764 			u32 tmp = agg_ring_size;
4765 
4766 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4767 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4768 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4769 				    tmp, agg_ring_size);
4770 		}
4771 		bp->rx_agg_ring_size = agg_ring_size;
4772 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4773 
4774 		if (BNXT_RX_PAGE_MODE(bp)) {
4775 			rx_space = PAGE_SIZE;
4776 			rx_size = PAGE_SIZE -
4777 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4778 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4779 		} else {
4780 			rx_size = max3(BNXT_DEFAULT_RX_COPYBREAK,
4781 				       bp->rx_copybreak,
4782 				       bp->dev->cfg_pending->hds_thresh);
4783 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4784 			rx_space = rx_size + NET_SKB_PAD +
4785 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4786 		}
4787 	}
4788 
4789 	bp->rx_buf_use_size = rx_size;
4790 	bp->rx_buf_size = rx_space;
4791 
4792 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4793 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4794 
4795 	ring_size = bp->tx_ring_size;
4796 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4797 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4798 
4799 	max_rx_cmpl = bp->rx_ring_size;
4800 	/* MAX TPA needs to be added because TPA_START completions are
4801 	 * immediately recycled, so the TPA completions are not bound by
4802 	 * the RX ring size.
4803 	 */
4804 	if (bp->flags & BNXT_FLAG_TPA)
4805 		max_rx_cmpl += bp->max_tpa;
4806 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4807 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4808 	bp->cp_ring_size = ring_size;
4809 
4810 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4811 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4812 		bp->cp_nr_pages = MAX_CP_PAGES;
4813 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4814 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4815 			    ring_size, bp->cp_ring_size);
4816 	}
4817 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4818 	bp->cp_ring_mask = bp->cp_bit - 1;
4819 }
4820 
4821 /* Changing allocation mode of RX rings.
4822  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4823  */
4824 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4825 {
4826 	struct net_device *dev = bp->dev;
4827 
4828 	if (page_mode) {
4829 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4830 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4831 
4832 		if (bp->xdp_prog->aux->xdp_has_frags)
4833 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4834 		else
4835 			dev->max_mtu =
4836 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4837 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4838 			bp->flags |= BNXT_FLAG_JUMBO;
4839 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4840 		} else {
4841 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4842 			bp->rx_skb_func = bnxt_rx_page_skb;
4843 		}
4844 		bp->rx_dir = DMA_BIDIRECTIONAL;
4845 	} else {
4846 		dev->max_mtu = bp->max_mtu;
4847 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4848 		bp->rx_dir = DMA_FROM_DEVICE;
4849 		bp->rx_skb_func = bnxt_rx_skb;
4850 	}
4851 }
4852 
4853 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4854 {
4855 	__bnxt_set_rx_skb_mode(bp, page_mode);
4856 
4857 	if (!page_mode) {
4858 		int rx, tx;
4859 
4860 		bnxt_get_max_rings(bp, &rx, &tx, true);
4861 		if (rx > 1) {
4862 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
4863 			bp->dev->hw_features |= NETIF_F_LRO;
4864 		}
4865 	}
4866 
4867 	/* Update LRO and GRO_HW availability */
4868 	netdev_update_features(bp->dev);
4869 }
4870 
4871 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4872 {
4873 	int i;
4874 	struct bnxt_vnic_info *vnic;
4875 	struct pci_dev *pdev = bp->pdev;
4876 
4877 	if (!bp->vnic_info)
4878 		return;
4879 
4880 	for (i = 0; i < bp->nr_vnics; i++) {
4881 		vnic = &bp->vnic_info[i];
4882 
4883 		kfree(vnic->fw_grp_ids);
4884 		vnic->fw_grp_ids = NULL;
4885 
4886 		kfree(vnic->uc_list);
4887 		vnic->uc_list = NULL;
4888 
4889 		if (vnic->mc_list) {
4890 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4891 					  vnic->mc_list, vnic->mc_list_mapping);
4892 			vnic->mc_list = NULL;
4893 		}
4894 
4895 		if (vnic->rss_table) {
4896 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4897 					  vnic->rss_table,
4898 					  vnic->rss_table_dma_addr);
4899 			vnic->rss_table = NULL;
4900 		}
4901 
4902 		vnic->rss_hash_key = NULL;
4903 		vnic->flags = 0;
4904 	}
4905 }
4906 
4907 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4908 {
4909 	int i, rc = 0, size;
4910 	struct bnxt_vnic_info *vnic;
4911 	struct pci_dev *pdev = bp->pdev;
4912 	int max_rings;
4913 
4914 	for (i = 0; i < bp->nr_vnics; i++) {
4915 		vnic = &bp->vnic_info[i];
4916 
4917 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4918 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4919 
4920 			if (mem_size > 0) {
4921 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4922 				if (!vnic->uc_list) {
4923 					rc = -ENOMEM;
4924 					goto out;
4925 				}
4926 			}
4927 		}
4928 
4929 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4930 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4931 			vnic->mc_list =
4932 				dma_alloc_coherent(&pdev->dev,
4933 						   vnic->mc_list_size,
4934 						   &vnic->mc_list_mapping,
4935 						   GFP_KERNEL);
4936 			if (!vnic->mc_list) {
4937 				rc = -ENOMEM;
4938 				goto out;
4939 			}
4940 		}
4941 
4942 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4943 			goto vnic_skip_grps;
4944 
4945 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4946 			max_rings = bp->rx_nr_rings;
4947 		else
4948 			max_rings = 1;
4949 
4950 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4951 		if (!vnic->fw_grp_ids) {
4952 			rc = -ENOMEM;
4953 			goto out;
4954 		}
4955 vnic_skip_grps:
4956 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4957 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4958 			continue;
4959 
4960 		/* Allocate rss table and hash key */
4961 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4962 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4963 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4964 
4965 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4966 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4967 						     vnic->rss_table_size,
4968 						     &vnic->rss_table_dma_addr,
4969 						     GFP_KERNEL);
4970 		if (!vnic->rss_table) {
4971 			rc = -ENOMEM;
4972 			goto out;
4973 		}
4974 
4975 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4976 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4977 	}
4978 	return 0;
4979 
4980 out:
4981 	return rc;
4982 }
4983 
4984 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4985 {
4986 	struct bnxt_hwrm_wait_token *token;
4987 
4988 	dma_pool_destroy(bp->hwrm_dma_pool);
4989 	bp->hwrm_dma_pool = NULL;
4990 
4991 	rcu_read_lock();
4992 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4993 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4994 	rcu_read_unlock();
4995 }
4996 
4997 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4998 {
4999 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
5000 					    BNXT_HWRM_DMA_SIZE,
5001 					    BNXT_HWRM_DMA_ALIGN, 0);
5002 	if (!bp->hwrm_dma_pool)
5003 		return -ENOMEM;
5004 
5005 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
5006 
5007 	return 0;
5008 }
5009 
5010 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
5011 {
5012 	kfree(stats->hw_masks);
5013 	stats->hw_masks = NULL;
5014 	kfree(stats->sw_stats);
5015 	stats->sw_stats = NULL;
5016 	if (stats->hw_stats) {
5017 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
5018 				  stats->hw_stats_map);
5019 		stats->hw_stats = NULL;
5020 	}
5021 }
5022 
5023 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
5024 				bool alloc_masks)
5025 {
5026 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
5027 					     &stats->hw_stats_map, GFP_KERNEL);
5028 	if (!stats->hw_stats)
5029 		return -ENOMEM;
5030 
5031 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
5032 	if (!stats->sw_stats)
5033 		goto stats_mem_err;
5034 
5035 	if (alloc_masks) {
5036 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
5037 		if (!stats->hw_masks)
5038 			goto stats_mem_err;
5039 	}
5040 	return 0;
5041 
5042 stats_mem_err:
5043 	bnxt_free_stats_mem(bp, stats);
5044 	return -ENOMEM;
5045 }
5046 
5047 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
5048 {
5049 	int i;
5050 
5051 	for (i = 0; i < count; i++)
5052 		mask_arr[i] = mask;
5053 }
5054 
5055 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
5056 {
5057 	int i;
5058 
5059 	for (i = 0; i < count; i++)
5060 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
5061 }
5062 
5063 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
5064 				    struct bnxt_stats_mem *stats)
5065 {
5066 	struct hwrm_func_qstats_ext_output *resp;
5067 	struct hwrm_func_qstats_ext_input *req;
5068 	__le64 *hw_masks;
5069 	int rc;
5070 
5071 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
5072 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5073 		return -EOPNOTSUPP;
5074 
5075 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
5076 	if (rc)
5077 		return rc;
5078 
5079 	req->fid = cpu_to_le16(0xffff);
5080 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5081 
5082 	resp = hwrm_req_hold(bp, req);
5083 	rc = hwrm_req_send(bp, req);
5084 	if (!rc) {
5085 		hw_masks = &resp->rx_ucast_pkts;
5086 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
5087 	}
5088 	hwrm_req_drop(bp, req);
5089 	return rc;
5090 }
5091 
5092 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5093 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5094 
5095 static void bnxt_init_stats(struct bnxt *bp)
5096 {
5097 	struct bnxt_napi *bnapi = bp->bnapi[0];
5098 	struct bnxt_cp_ring_info *cpr;
5099 	struct bnxt_stats_mem *stats;
5100 	__le64 *rx_stats, *tx_stats;
5101 	int rc, rx_count, tx_count;
5102 	u64 *rx_masks, *tx_masks;
5103 	u64 mask;
5104 	u8 flags;
5105 
5106 	cpr = &bnapi->cp_ring;
5107 	stats = &cpr->stats;
5108 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5109 	if (rc) {
5110 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5111 			mask = (1ULL << 48) - 1;
5112 		else
5113 			mask = -1ULL;
5114 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5115 	}
5116 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5117 		stats = &bp->port_stats;
5118 		rx_stats = stats->hw_stats;
5119 		rx_masks = stats->hw_masks;
5120 		rx_count = sizeof(struct rx_port_stats) / 8;
5121 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5122 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5123 		tx_count = sizeof(struct tx_port_stats) / 8;
5124 
5125 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5126 		rc = bnxt_hwrm_port_qstats(bp, flags);
5127 		if (rc) {
5128 			mask = (1ULL << 40) - 1;
5129 
5130 			bnxt_fill_masks(rx_masks, mask, rx_count);
5131 			bnxt_fill_masks(tx_masks, mask, tx_count);
5132 		} else {
5133 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5134 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5135 			bnxt_hwrm_port_qstats(bp, 0);
5136 		}
5137 	}
5138 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5139 		stats = &bp->rx_port_stats_ext;
5140 		rx_stats = stats->hw_stats;
5141 		rx_masks = stats->hw_masks;
5142 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5143 		stats = &bp->tx_port_stats_ext;
5144 		tx_stats = stats->hw_stats;
5145 		tx_masks = stats->hw_masks;
5146 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5147 
5148 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5149 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5150 		if (rc) {
5151 			mask = (1ULL << 40) - 1;
5152 
5153 			bnxt_fill_masks(rx_masks, mask, rx_count);
5154 			if (tx_stats)
5155 				bnxt_fill_masks(tx_masks, mask, tx_count);
5156 		} else {
5157 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5158 			if (tx_stats)
5159 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5160 						   tx_count);
5161 			bnxt_hwrm_port_qstats_ext(bp, 0);
5162 		}
5163 	}
5164 }
5165 
5166 static void bnxt_free_port_stats(struct bnxt *bp)
5167 {
5168 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5169 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5170 
5171 	bnxt_free_stats_mem(bp, &bp->port_stats);
5172 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5173 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5174 }
5175 
5176 static void bnxt_free_ring_stats(struct bnxt *bp)
5177 {
5178 	int i;
5179 
5180 	if (!bp->bnapi)
5181 		return;
5182 
5183 	for (i = 0; i < bp->cp_nr_rings; i++) {
5184 		struct bnxt_napi *bnapi = bp->bnapi[i];
5185 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5186 
5187 		bnxt_free_stats_mem(bp, &cpr->stats);
5188 
5189 		kfree(cpr->sw_stats);
5190 		cpr->sw_stats = NULL;
5191 	}
5192 }
5193 
5194 static int bnxt_alloc_stats(struct bnxt *bp)
5195 {
5196 	u32 size, i;
5197 	int rc;
5198 
5199 	size = bp->hw_ring_stats_size;
5200 
5201 	for (i = 0; i < bp->cp_nr_rings; i++) {
5202 		struct bnxt_napi *bnapi = bp->bnapi[i];
5203 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5204 
5205 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
5206 		if (!cpr->sw_stats)
5207 			return -ENOMEM;
5208 
5209 		cpr->stats.len = size;
5210 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5211 		if (rc)
5212 			return rc;
5213 
5214 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5215 	}
5216 
5217 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5218 		return 0;
5219 
5220 	if (bp->port_stats.hw_stats)
5221 		goto alloc_ext_stats;
5222 
5223 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5224 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5225 	if (rc)
5226 		return rc;
5227 
5228 	bp->flags |= BNXT_FLAG_PORT_STATS;
5229 
5230 alloc_ext_stats:
5231 	/* Display extended statistics only if FW supports it */
5232 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5233 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5234 			return 0;
5235 
5236 	if (bp->rx_port_stats_ext.hw_stats)
5237 		goto alloc_tx_ext_stats;
5238 
5239 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5240 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5241 	/* Extended stats are optional */
5242 	if (rc)
5243 		return 0;
5244 
5245 alloc_tx_ext_stats:
5246 	if (bp->tx_port_stats_ext.hw_stats)
5247 		return 0;
5248 
5249 	if (bp->hwrm_spec_code >= 0x10902 ||
5250 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5251 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5252 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5253 		/* Extended stats are optional */
5254 		if (rc)
5255 			return 0;
5256 	}
5257 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5258 	return 0;
5259 }
5260 
5261 static void bnxt_clear_ring_indices(struct bnxt *bp)
5262 {
5263 	int i, j;
5264 
5265 	if (!bp->bnapi)
5266 		return;
5267 
5268 	for (i = 0; i < bp->cp_nr_rings; i++) {
5269 		struct bnxt_napi *bnapi = bp->bnapi[i];
5270 		struct bnxt_cp_ring_info *cpr;
5271 		struct bnxt_rx_ring_info *rxr;
5272 		struct bnxt_tx_ring_info *txr;
5273 
5274 		if (!bnapi)
5275 			continue;
5276 
5277 		cpr = &bnapi->cp_ring;
5278 		cpr->cp_raw_cons = 0;
5279 
5280 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5281 			txr->tx_prod = 0;
5282 			txr->tx_cons = 0;
5283 			txr->tx_hw_cons = 0;
5284 		}
5285 
5286 		rxr = bnapi->rx_ring;
5287 		if (rxr) {
5288 			rxr->rx_prod = 0;
5289 			rxr->rx_agg_prod = 0;
5290 			rxr->rx_sw_agg_prod = 0;
5291 			rxr->rx_next_cons = 0;
5292 		}
5293 		bnapi->events = 0;
5294 	}
5295 }
5296 
5297 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5298 {
5299 	u8 type = fltr->type, flags = fltr->flags;
5300 
5301 	INIT_LIST_HEAD(&fltr->list);
5302 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5303 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5304 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5305 }
5306 
5307 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5308 {
5309 	if (!list_empty(&fltr->list))
5310 		list_del_init(&fltr->list);
5311 }
5312 
5313 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5314 {
5315 	struct bnxt_filter_base *usr_fltr, *tmp;
5316 
5317 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5318 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5319 			continue;
5320 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5321 	}
5322 }
5323 
5324 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5325 {
5326 	hlist_del(&fltr->hash);
5327 	bnxt_del_one_usr_fltr(bp, fltr);
5328 	if (fltr->flags) {
5329 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5330 		bp->ntp_fltr_count--;
5331 	}
5332 	kfree(fltr);
5333 }
5334 
5335 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5336 {
5337 	int i;
5338 
5339 	netdev_assert_locked_or_invisible(bp->dev);
5340 
5341 	/* Under netdev instance lock and all our NAPIs have been disabled.
5342 	 * It's safe to delete the hash table.
5343 	 */
5344 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5345 		struct hlist_head *head;
5346 		struct hlist_node *tmp;
5347 		struct bnxt_ntuple_filter *fltr;
5348 
5349 		head = &bp->ntp_fltr_hash_tbl[i];
5350 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5351 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5352 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5353 				     !list_empty(&fltr->base.list)))
5354 				continue;
5355 			bnxt_del_fltr(bp, &fltr->base);
5356 		}
5357 	}
5358 	if (!all)
5359 		return;
5360 
5361 	bitmap_free(bp->ntp_fltr_bmap);
5362 	bp->ntp_fltr_bmap = NULL;
5363 	bp->ntp_fltr_count = 0;
5364 }
5365 
5366 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5367 {
5368 	int i, rc = 0;
5369 
5370 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5371 		return 0;
5372 
5373 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5374 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5375 
5376 	bp->ntp_fltr_count = 0;
5377 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5378 
5379 	if (!bp->ntp_fltr_bmap)
5380 		rc = -ENOMEM;
5381 
5382 	return rc;
5383 }
5384 
5385 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5386 {
5387 	int i;
5388 
5389 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5390 		struct hlist_head *head;
5391 		struct hlist_node *tmp;
5392 		struct bnxt_l2_filter *fltr;
5393 
5394 		head = &bp->l2_fltr_hash_tbl[i];
5395 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5396 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5397 				     !list_empty(&fltr->base.list)))
5398 				continue;
5399 			bnxt_del_fltr(bp, &fltr->base);
5400 		}
5401 	}
5402 }
5403 
5404 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5405 {
5406 	int i;
5407 
5408 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5409 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5410 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5411 }
5412 
5413 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5414 {
5415 	bnxt_free_vnic_attributes(bp);
5416 	bnxt_free_tx_rings(bp);
5417 	bnxt_free_rx_rings(bp);
5418 	bnxt_free_cp_rings(bp);
5419 	bnxt_free_all_cp_arrays(bp);
5420 	bnxt_free_ntp_fltrs(bp, false);
5421 	bnxt_free_l2_filters(bp, false);
5422 	if (irq_re_init) {
5423 		bnxt_free_ring_stats(bp);
5424 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5425 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5426 			bnxt_free_port_stats(bp);
5427 		bnxt_free_ring_grps(bp);
5428 		bnxt_free_vnics(bp);
5429 		kfree(bp->tx_ring_map);
5430 		bp->tx_ring_map = NULL;
5431 		kfree(bp->tx_ring);
5432 		bp->tx_ring = NULL;
5433 		kfree(bp->rx_ring);
5434 		bp->rx_ring = NULL;
5435 		kfree(bp->bnapi);
5436 		bp->bnapi = NULL;
5437 	} else {
5438 		bnxt_clear_ring_indices(bp);
5439 	}
5440 }
5441 
5442 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5443 {
5444 	int i, j, rc, size, arr_size;
5445 	void *bnapi;
5446 
5447 	if (irq_re_init) {
5448 		/* Allocate bnapi mem pointer array and mem block for
5449 		 * all queues
5450 		 */
5451 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5452 				bp->cp_nr_rings);
5453 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5454 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5455 		if (!bnapi)
5456 			return -ENOMEM;
5457 
5458 		bp->bnapi = bnapi;
5459 		bnapi += arr_size;
5460 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5461 			bp->bnapi[i] = bnapi;
5462 			bp->bnapi[i]->index = i;
5463 			bp->bnapi[i]->bp = bp;
5464 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5465 				struct bnxt_cp_ring_info *cpr =
5466 					&bp->bnapi[i]->cp_ring;
5467 
5468 				cpr->cp_ring_struct.ring_mem.flags =
5469 					BNXT_RMEM_RING_PTE_FLAG;
5470 			}
5471 		}
5472 
5473 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5474 				      sizeof(struct bnxt_rx_ring_info),
5475 				      GFP_KERNEL);
5476 		if (!bp->rx_ring)
5477 			return -ENOMEM;
5478 
5479 		for (i = 0; i < bp->rx_nr_rings; i++) {
5480 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5481 
5482 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5483 				rxr->rx_ring_struct.ring_mem.flags =
5484 					BNXT_RMEM_RING_PTE_FLAG;
5485 				rxr->rx_agg_ring_struct.ring_mem.flags =
5486 					BNXT_RMEM_RING_PTE_FLAG;
5487 			} else {
5488 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5489 			}
5490 			rxr->bnapi = bp->bnapi[i];
5491 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5492 		}
5493 
5494 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5495 				      sizeof(struct bnxt_tx_ring_info),
5496 				      GFP_KERNEL);
5497 		if (!bp->tx_ring)
5498 			return -ENOMEM;
5499 
5500 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5501 					  GFP_KERNEL);
5502 
5503 		if (!bp->tx_ring_map)
5504 			return -ENOMEM;
5505 
5506 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5507 			j = 0;
5508 		else
5509 			j = bp->rx_nr_rings;
5510 
5511 		for (i = 0; i < bp->tx_nr_rings; i++) {
5512 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5513 			struct bnxt_napi *bnapi2;
5514 
5515 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5516 				txr->tx_ring_struct.ring_mem.flags =
5517 					BNXT_RMEM_RING_PTE_FLAG;
5518 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5519 			if (i >= bp->tx_nr_rings_xdp) {
5520 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5521 
5522 				bnapi2 = bp->bnapi[k];
5523 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5524 				txr->tx_napi_idx =
5525 					BNXT_RING_TO_TC(bp, txr->txq_index);
5526 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5527 				bnapi2->tx_int = bnxt_tx_int;
5528 			} else {
5529 				bnapi2 = bp->bnapi[j];
5530 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5531 				bnapi2->tx_ring[0] = txr;
5532 				bnapi2->tx_int = bnxt_tx_int_xdp;
5533 				j++;
5534 			}
5535 			txr->bnapi = bnapi2;
5536 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5537 				txr->tx_cpr = &bnapi2->cp_ring;
5538 		}
5539 
5540 		rc = bnxt_alloc_stats(bp);
5541 		if (rc)
5542 			goto alloc_mem_err;
5543 		bnxt_init_stats(bp);
5544 
5545 		rc = bnxt_alloc_ntp_fltrs(bp);
5546 		if (rc)
5547 			goto alloc_mem_err;
5548 
5549 		rc = bnxt_alloc_vnics(bp);
5550 		if (rc)
5551 			goto alloc_mem_err;
5552 	}
5553 
5554 	rc = bnxt_alloc_all_cp_arrays(bp);
5555 	if (rc)
5556 		goto alloc_mem_err;
5557 
5558 	bnxt_init_ring_struct(bp);
5559 
5560 	rc = bnxt_alloc_rx_rings(bp);
5561 	if (rc)
5562 		goto alloc_mem_err;
5563 
5564 	rc = bnxt_alloc_tx_rings(bp);
5565 	if (rc)
5566 		goto alloc_mem_err;
5567 
5568 	rc = bnxt_alloc_cp_rings(bp);
5569 	if (rc)
5570 		goto alloc_mem_err;
5571 
5572 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5573 						  BNXT_VNIC_MCAST_FLAG |
5574 						  BNXT_VNIC_UCAST_FLAG;
5575 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5576 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5577 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5578 
5579 	rc = bnxt_alloc_vnic_attributes(bp);
5580 	if (rc)
5581 		goto alloc_mem_err;
5582 	return 0;
5583 
5584 alloc_mem_err:
5585 	bnxt_free_mem(bp, true);
5586 	return rc;
5587 }
5588 
5589 static void bnxt_disable_int(struct bnxt *bp)
5590 {
5591 	int i;
5592 
5593 	if (!bp->bnapi)
5594 		return;
5595 
5596 	for (i = 0; i < bp->cp_nr_rings; i++) {
5597 		struct bnxt_napi *bnapi = bp->bnapi[i];
5598 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5599 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5600 
5601 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5602 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5603 	}
5604 }
5605 
5606 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5607 {
5608 	struct bnxt_napi *bnapi = bp->bnapi[n];
5609 	struct bnxt_cp_ring_info *cpr;
5610 
5611 	cpr = &bnapi->cp_ring;
5612 	return cpr->cp_ring_struct.map_idx;
5613 }
5614 
5615 static void bnxt_disable_int_sync(struct bnxt *bp)
5616 {
5617 	int i;
5618 
5619 	if (!bp->irq_tbl)
5620 		return;
5621 
5622 	atomic_inc(&bp->intr_sem);
5623 
5624 	bnxt_disable_int(bp);
5625 	for (i = 0; i < bp->cp_nr_rings; i++) {
5626 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5627 
5628 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5629 	}
5630 }
5631 
5632 static void bnxt_enable_int(struct bnxt *bp)
5633 {
5634 	int i;
5635 
5636 	atomic_set(&bp->intr_sem, 0);
5637 	for (i = 0; i < bp->cp_nr_rings; i++) {
5638 		struct bnxt_napi *bnapi = bp->bnapi[i];
5639 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5640 
5641 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5642 	}
5643 }
5644 
5645 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5646 			    bool async_only)
5647 {
5648 	DECLARE_BITMAP(async_events_bmap, 256);
5649 	u32 *events = (u32 *)async_events_bmap;
5650 	struct hwrm_func_drv_rgtr_output *resp;
5651 	struct hwrm_func_drv_rgtr_input *req;
5652 	u32 flags;
5653 	int rc, i;
5654 
5655 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5656 	if (rc)
5657 		return rc;
5658 
5659 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5660 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5661 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5662 
5663 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5664 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5665 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5666 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5667 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5668 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5669 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5670 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5671 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5672 	req->flags = cpu_to_le32(flags);
5673 	req->ver_maj_8b = DRV_VER_MAJ;
5674 	req->ver_min_8b = DRV_VER_MIN;
5675 	req->ver_upd_8b = DRV_VER_UPD;
5676 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5677 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5678 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5679 
5680 	if (BNXT_PF(bp)) {
5681 		u32 data[8];
5682 		int i;
5683 
5684 		memset(data, 0, sizeof(data));
5685 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5686 			u16 cmd = bnxt_vf_req_snif[i];
5687 			unsigned int bit, idx;
5688 
5689 			idx = cmd / 32;
5690 			bit = cmd % 32;
5691 			data[idx] |= 1 << bit;
5692 		}
5693 
5694 		for (i = 0; i < 8; i++)
5695 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5696 
5697 		req->enables |=
5698 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5699 	}
5700 
5701 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5702 		req->flags |= cpu_to_le32(
5703 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5704 
5705 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5706 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5707 		u16 event_id = bnxt_async_events_arr[i];
5708 
5709 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5710 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5711 			continue;
5712 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5713 		    !bp->ptp_cfg)
5714 			continue;
5715 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5716 	}
5717 	if (bmap && bmap_size) {
5718 		for (i = 0; i < bmap_size; i++) {
5719 			if (test_bit(i, bmap))
5720 				__set_bit(i, async_events_bmap);
5721 		}
5722 	}
5723 	for (i = 0; i < 8; i++)
5724 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5725 
5726 	if (async_only)
5727 		req->enables =
5728 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5729 
5730 	resp = hwrm_req_hold(bp, req);
5731 	rc = hwrm_req_send(bp, req);
5732 	if (!rc) {
5733 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5734 		if (resp->flags &
5735 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5736 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5737 	}
5738 	hwrm_req_drop(bp, req);
5739 	return rc;
5740 }
5741 
5742 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5743 {
5744 	struct hwrm_func_drv_unrgtr_input *req;
5745 	int rc;
5746 
5747 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5748 		return 0;
5749 
5750 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5751 	if (rc)
5752 		return rc;
5753 	return hwrm_req_send(bp, req);
5754 }
5755 
5756 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5757 
5758 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5759 {
5760 	struct hwrm_tunnel_dst_port_free_input *req;
5761 	int rc;
5762 
5763 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5764 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5765 		return 0;
5766 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5767 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5768 		return 0;
5769 
5770 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5771 	if (rc)
5772 		return rc;
5773 
5774 	req->tunnel_type = tunnel_type;
5775 
5776 	switch (tunnel_type) {
5777 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5778 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5779 		bp->vxlan_port = 0;
5780 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5781 		break;
5782 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5783 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5784 		bp->nge_port = 0;
5785 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5786 		break;
5787 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5788 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5789 		bp->vxlan_gpe_port = 0;
5790 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5791 		break;
5792 	default:
5793 		break;
5794 	}
5795 
5796 	rc = hwrm_req_send(bp, req);
5797 	if (rc)
5798 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5799 			   rc);
5800 	if (bp->flags & BNXT_FLAG_TPA)
5801 		bnxt_set_tpa(bp, true);
5802 	return rc;
5803 }
5804 
5805 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5806 					   u8 tunnel_type)
5807 {
5808 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5809 	struct hwrm_tunnel_dst_port_alloc_input *req;
5810 	int rc;
5811 
5812 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5813 	if (rc)
5814 		return rc;
5815 
5816 	req->tunnel_type = tunnel_type;
5817 	req->tunnel_dst_port_val = port;
5818 
5819 	resp = hwrm_req_hold(bp, req);
5820 	rc = hwrm_req_send(bp, req);
5821 	if (rc) {
5822 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5823 			   rc);
5824 		goto err_out;
5825 	}
5826 
5827 	switch (tunnel_type) {
5828 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5829 		bp->vxlan_port = port;
5830 		bp->vxlan_fw_dst_port_id =
5831 			le16_to_cpu(resp->tunnel_dst_port_id);
5832 		break;
5833 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5834 		bp->nge_port = port;
5835 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5836 		break;
5837 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5838 		bp->vxlan_gpe_port = port;
5839 		bp->vxlan_gpe_fw_dst_port_id =
5840 			le16_to_cpu(resp->tunnel_dst_port_id);
5841 		break;
5842 	default:
5843 		break;
5844 	}
5845 	if (bp->flags & BNXT_FLAG_TPA)
5846 		bnxt_set_tpa(bp, true);
5847 
5848 err_out:
5849 	hwrm_req_drop(bp, req);
5850 	return rc;
5851 }
5852 
5853 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5854 {
5855 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5856 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5857 	int rc;
5858 
5859 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5860 	if (rc)
5861 		return rc;
5862 
5863 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5864 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5865 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5866 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5867 	}
5868 	req->mask = cpu_to_le32(vnic->rx_mask);
5869 	return hwrm_req_send_silent(bp, req);
5870 }
5871 
5872 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5873 {
5874 	if (!atomic_dec_and_test(&fltr->refcnt))
5875 		return;
5876 	spin_lock_bh(&bp->ntp_fltr_lock);
5877 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5878 		spin_unlock_bh(&bp->ntp_fltr_lock);
5879 		return;
5880 	}
5881 	hlist_del_rcu(&fltr->base.hash);
5882 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5883 	if (fltr->base.flags) {
5884 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5885 		bp->ntp_fltr_count--;
5886 	}
5887 	spin_unlock_bh(&bp->ntp_fltr_lock);
5888 	kfree_rcu(fltr, base.rcu);
5889 }
5890 
5891 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5892 						      struct bnxt_l2_key *key,
5893 						      u32 idx)
5894 {
5895 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5896 	struct bnxt_l2_filter *fltr;
5897 
5898 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5899 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5900 
5901 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5902 		    l2_key->vlan == key->vlan)
5903 			return fltr;
5904 	}
5905 	return NULL;
5906 }
5907 
5908 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5909 						    struct bnxt_l2_key *key,
5910 						    u32 idx)
5911 {
5912 	struct bnxt_l2_filter *fltr = NULL;
5913 
5914 	rcu_read_lock();
5915 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5916 	if (fltr)
5917 		atomic_inc(&fltr->refcnt);
5918 	rcu_read_unlock();
5919 	return fltr;
5920 }
5921 
5922 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5923 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5924 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5925 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5926 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5927 
5928 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5929 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5930 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5931 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5932 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5933 
5934 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5935 {
5936 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5937 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5938 			return sizeof(fkeys->addrs.v4addrs) +
5939 			       sizeof(fkeys->ports);
5940 
5941 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5942 			return sizeof(fkeys->addrs.v4addrs);
5943 	}
5944 
5945 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5946 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5947 			return sizeof(fkeys->addrs.v6addrs) +
5948 			       sizeof(fkeys->ports);
5949 
5950 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5951 			return sizeof(fkeys->addrs.v6addrs);
5952 	}
5953 
5954 	return 0;
5955 }
5956 
5957 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5958 			 const unsigned char *key)
5959 {
5960 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5961 	struct bnxt_ipv4_tuple tuple4;
5962 	struct bnxt_ipv6_tuple tuple6;
5963 	int i, j, len = 0;
5964 	u8 *four_tuple;
5965 
5966 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5967 	if (!len)
5968 		return 0;
5969 
5970 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5971 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5972 		tuple4.ports = fkeys->ports;
5973 		four_tuple = (unsigned char *)&tuple4;
5974 	} else {
5975 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5976 		tuple6.ports = fkeys->ports;
5977 		four_tuple = (unsigned char *)&tuple6;
5978 	}
5979 
5980 	for (i = 0, j = 8; i < len; i++, j++) {
5981 		u8 byte = four_tuple[i];
5982 		int bit;
5983 
5984 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5985 			if (byte & 0x80)
5986 				hash ^= prefix;
5987 		}
5988 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5989 	}
5990 
5991 	/* The valid part of the hash is in the upper 32 bits. */
5992 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5993 }
5994 
5995 #ifdef CONFIG_RFS_ACCEL
5996 static struct bnxt_l2_filter *
5997 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5998 {
5999 	struct bnxt_l2_filter *fltr;
6000 	u32 idx;
6001 
6002 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6003 	      BNXT_L2_FLTR_HASH_MASK;
6004 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6005 	return fltr;
6006 }
6007 #endif
6008 
6009 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
6010 			       struct bnxt_l2_key *key, u32 idx)
6011 {
6012 	struct hlist_head *head;
6013 
6014 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
6015 	fltr->l2_key.vlan = key->vlan;
6016 	fltr->base.type = BNXT_FLTR_TYPE_L2;
6017 	if (fltr->base.flags) {
6018 		int bit_id;
6019 
6020 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6021 						 bp->max_fltr, 0);
6022 		if (bit_id < 0)
6023 			return -ENOMEM;
6024 		fltr->base.sw_id = (u16)bit_id;
6025 		bp->ntp_fltr_count++;
6026 	}
6027 	head = &bp->l2_fltr_hash_tbl[idx];
6028 	hlist_add_head_rcu(&fltr->base.hash, head);
6029 	bnxt_insert_usr_fltr(bp, &fltr->base);
6030 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
6031 	atomic_set(&fltr->refcnt, 1);
6032 	return 0;
6033 }
6034 
6035 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
6036 						   struct bnxt_l2_key *key,
6037 						   gfp_t gfp)
6038 {
6039 	struct bnxt_l2_filter *fltr;
6040 	u32 idx;
6041 	int rc;
6042 
6043 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6044 	      BNXT_L2_FLTR_HASH_MASK;
6045 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6046 	if (fltr)
6047 		return fltr;
6048 
6049 	fltr = kzalloc(sizeof(*fltr), gfp);
6050 	if (!fltr)
6051 		return ERR_PTR(-ENOMEM);
6052 	spin_lock_bh(&bp->ntp_fltr_lock);
6053 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6054 	spin_unlock_bh(&bp->ntp_fltr_lock);
6055 	if (rc) {
6056 		bnxt_del_l2_filter(bp, fltr);
6057 		fltr = ERR_PTR(rc);
6058 	}
6059 	return fltr;
6060 }
6061 
6062 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
6063 						struct bnxt_l2_key *key,
6064 						u16 flags)
6065 {
6066 	struct bnxt_l2_filter *fltr;
6067 	u32 idx;
6068 	int rc;
6069 
6070 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6071 	      BNXT_L2_FLTR_HASH_MASK;
6072 	spin_lock_bh(&bp->ntp_fltr_lock);
6073 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6074 	if (fltr) {
6075 		fltr = ERR_PTR(-EEXIST);
6076 		goto l2_filter_exit;
6077 	}
6078 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
6079 	if (!fltr) {
6080 		fltr = ERR_PTR(-ENOMEM);
6081 		goto l2_filter_exit;
6082 	}
6083 	fltr->base.flags = flags;
6084 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6085 	if (rc) {
6086 		spin_unlock_bh(&bp->ntp_fltr_lock);
6087 		bnxt_del_l2_filter(bp, fltr);
6088 		return ERR_PTR(rc);
6089 	}
6090 
6091 l2_filter_exit:
6092 	spin_unlock_bh(&bp->ntp_fltr_lock);
6093 	return fltr;
6094 }
6095 
6096 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6097 {
6098 #ifdef CONFIG_BNXT_SRIOV
6099 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6100 
6101 	return vf->fw_fid;
6102 #else
6103 	return INVALID_HW_RING_ID;
6104 #endif
6105 }
6106 
6107 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6108 {
6109 	struct hwrm_cfa_l2_filter_free_input *req;
6110 	u16 target_id = 0xffff;
6111 	int rc;
6112 
6113 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6114 		struct bnxt_pf_info *pf = &bp->pf;
6115 
6116 		if (fltr->base.vf_idx >= pf->active_vfs)
6117 			return -EINVAL;
6118 
6119 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6120 		if (target_id == INVALID_HW_RING_ID)
6121 			return -EINVAL;
6122 	}
6123 
6124 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6125 	if (rc)
6126 		return rc;
6127 
6128 	req->target_id = cpu_to_le16(target_id);
6129 	req->l2_filter_id = fltr->base.filter_id;
6130 	return hwrm_req_send(bp, req);
6131 }
6132 
6133 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6134 {
6135 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6136 	struct hwrm_cfa_l2_filter_alloc_input *req;
6137 	u16 target_id = 0xffff;
6138 	int rc;
6139 
6140 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6141 		struct bnxt_pf_info *pf = &bp->pf;
6142 
6143 		if (fltr->base.vf_idx >= pf->active_vfs)
6144 			return -EINVAL;
6145 
6146 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6147 	}
6148 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6149 	if (rc)
6150 		return rc;
6151 
6152 	req->target_id = cpu_to_le16(target_id);
6153 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6154 
6155 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6156 		req->flags |=
6157 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6158 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6159 	req->enables =
6160 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6161 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6162 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6163 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6164 	eth_broadcast_addr(req->l2_addr_mask);
6165 
6166 	if (fltr->l2_key.vlan) {
6167 		req->enables |=
6168 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6169 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6170 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6171 		req->num_vlans = 1;
6172 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6173 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6174 	}
6175 
6176 	resp = hwrm_req_hold(bp, req);
6177 	rc = hwrm_req_send(bp, req);
6178 	if (!rc) {
6179 		fltr->base.filter_id = resp->l2_filter_id;
6180 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6181 	}
6182 	hwrm_req_drop(bp, req);
6183 	return rc;
6184 }
6185 
6186 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6187 				     struct bnxt_ntuple_filter *fltr)
6188 {
6189 	struct hwrm_cfa_ntuple_filter_free_input *req;
6190 	int rc;
6191 
6192 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6193 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6194 	if (rc)
6195 		return rc;
6196 
6197 	req->ntuple_filter_id = fltr->base.filter_id;
6198 	return hwrm_req_send(bp, req);
6199 }
6200 
6201 #define BNXT_NTP_FLTR_FLAGS					\
6202 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6203 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6204 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6205 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6206 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6207 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6208 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6209 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6210 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6211 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6212 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6213 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6214 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6215 
6216 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6217 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6218 
6219 void bnxt_fill_ipv6_mask(__be32 mask[4])
6220 {
6221 	int i;
6222 
6223 	for (i = 0; i < 4; i++)
6224 		mask[i] = cpu_to_be32(~0);
6225 }
6226 
6227 static void
6228 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6229 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6230 			  struct bnxt_ntuple_filter *fltr)
6231 {
6232 	u16 rxq = fltr->base.rxq;
6233 
6234 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6235 		struct ethtool_rxfh_context *ctx;
6236 		struct bnxt_rss_ctx *rss_ctx;
6237 		struct bnxt_vnic_info *vnic;
6238 
6239 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6240 			      fltr->base.fw_vnic_id);
6241 		if (ctx) {
6242 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6243 			vnic = &rss_ctx->vnic;
6244 
6245 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6246 		}
6247 		return;
6248 	}
6249 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6250 		struct bnxt_vnic_info *vnic;
6251 		u32 enables;
6252 
6253 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6254 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6255 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6256 		req->enables |= cpu_to_le32(enables);
6257 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6258 	} else {
6259 		u32 flags;
6260 
6261 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6262 		req->flags |= cpu_to_le32(flags);
6263 		req->dst_id = cpu_to_le16(rxq);
6264 	}
6265 }
6266 
6267 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6268 				      struct bnxt_ntuple_filter *fltr)
6269 {
6270 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6271 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6272 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6273 	struct flow_keys *keys = &fltr->fkeys;
6274 	struct bnxt_l2_filter *l2_fltr;
6275 	struct bnxt_vnic_info *vnic;
6276 	int rc;
6277 
6278 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6279 	if (rc)
6280 		return rc;
6281 
6282 	l2_fltr = fltr->l2_fltr;
6283 	req->l2_filter_id = l2_fltr->base.filter_id;
6284 
6285 	if (fltr->base.flags & BNXT_ACT_DROP) {
6286 		req->flags =
6287 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6288 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6289 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6290 	} else {
6291 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6292 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6293 	}
6294 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6295 
6296 	req->ethertype = htons(ETH_P_IP);
6297 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6298 	req->ip_protocol = keys->basic.ip_proto;
6299 
6300 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6301 		req->ethertype = htons(ETH_P_IPV6);
6302 		req->ip_addr_type =
6303 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6304 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6305 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6306 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6307 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6308 	} else {
6309 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6310 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6311 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6312 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6313 	}
6314 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6315 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6316 		req->tunnel_type =
6317 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6318 	}
6319 
6320 	req->src_port = keys->ports.src;
6321 	req->src_port_mask = masks->ports.src;
6322 	req->dst_port = keys->ports.dst;
6323 	req->dst_port_mask = masks->ports.dst;
6324 
6325 	resp = hwrm_req_hold(bp, req);
6326 	rc = hwrm_req_send(bp, req);
6327 	if (!rc)
6328 		fltr->base.filter_id = resp->ntuple_filter_id;
6329 	hwrm_req_drop(bp, req);
6330 	return rc;
6331 }
6332 
6333 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6334 				     const u8 *mac_addr)
6335 {
6336 	struct bnxt_l2_filter *fltr;
6337 	struct bnxt_l2_key key;
6338 	int rc;
6339 
6340 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6341 	key.vlan = 0;
6342 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6343 	if (IS_ERR(fltr))
6344 		return PTR_ERR(fltr);
6345 
6346 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6347 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6348 	if (rc)
6349 		bnxt_del_l2_filter(bp, fltr);
6350 	else
6351 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6352 	return rc;
6353 }
6354 
6355 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6356 {
6357 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6358 
6359 	/* Any associated ntuple filters will also be cleared by firmware. */
6360 	for (i = 0; i < num_of_vnics; i++) {
6361 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6362 
6363 		for (j = 0; j < vnic->uc_filter_count; j++) {
6364 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6365 
6366 			bnxt_hwrm_l2_filter_free(bp, fltr);
6367 			bnxt_del_l2_filter(bp, fltr);
6368 		}
6369 		vnic->uc_filter_count = 0;
6370 	}
6371 }
6372 
6373 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6374 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6375 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6376 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6377 
6378 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6379 					   struct hwrm_vnic_tpa_cfg_input *req)
6380 {
6381 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6382 
6383 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6384 		return;
6385 
6386 	if (bp->vxlan_port)
6387 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6388 	if (bp->vxlan_gpe_port)
6389 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6390 	if (bp->nge_port)
6391 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6392 
6393 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6394 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6395 }
6396 
6397 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6398 			   u32 tpa_flags)
6399 {
6400 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6401 	struct hwrm_vnic_tpa_cfg_input *req;
6402 	int rc;
6403 
6404 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6405 		return 0;
6406 
6407 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6408 	if (rc)
6409 		return rc;
6410 
6411 	if (tpa_flags) {
6412 		u16 mss = bp->dev->mtu - 40;
6413 		u32 nsegs, n, segs = 0, flags;
6414 
6415 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6416 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6417 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6418 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6419 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6420 		if (tpa_flags & BNXT_FLAG_GRO)
6421 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6422 
6423 		req->flags = cpu_to_le32(flags);
6424 
6425 		req->enables =
6426 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6427 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6428 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6429 
6430 		/* Number of segs are log2 units, and first packet is not
6431 		 * included as part of this units.
6432 		 */
6433 		if (mss <= BNXT_RX_PAGE_SIZE) {
6434 			n = BNXT_RX_PAGE_SIZE / mss;
6435 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6436 		} else {
6437 			n = mss / BNXT_RX_PAGE_SIZE;
6438 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6439 				n++;
6440 			nsegs = (MAX_SKB_FRAGS - n) / n;
6441 		}
6442 
6443 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6444 			segs = MAX_TPA_SEGS_P5;
6445 			max_aggs = bp->max_tpa;
6446 		} else {
6447 			segs = ilog2(nsegs);
6448 		}
6449 		req->max_agg_segs = cpu_to_le16(segs);
6450 		req->max_aggs = cpu_to_le16(max_aggs);
6451 
6452 		req->min_agg_len = cpu_to_le32(512);
6453 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6454 	}
6455 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6456 
6457 	return hwrm_req_send(bp, req);
6458 }
6459 
6460 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6461 {
6462 	struct bnxt_ring_grp_info *grp_info;
6463 
6464 	grp_info = &bp->grp_info[ring->grp_idx];
6465 	return grp_info->cp_fw_ring_id;
6466 }
6467 
6468 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6469 {
6470 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6471 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6472 	else
6473 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6474 }
6475 
6476 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6477 {
6478 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6479 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6480 	else
6481 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6482 }
6483 
6484 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6485 {
6486 	int entries;
6487 
6488 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6489 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6490 	else
6491 		entries = HW_HASH_INDEX_SIZE;
6492 
6493 	bp->rss_indir_tbl_entries = entries;
6494 	bp->rss_indir_tbl =
6495 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6496 	if (!bp->rss_indir_tbl)
6497 		return -ENOMEM;
6498 
6499 	return 0;
6500 }
6501 
6502 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6503 				 struct ethtool_rxfh_context *rss_ctx)
6504 {
6505 	u16 max_rings, max_entries, pad, i;
6506 	u32 *rss_indir_tbl;
6507 
6508 	if (!bp->rx_nr_rings)
6509 		return;
6510 
6511 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6512 		max_rings = bp->rx_nr_rings - 1;
6513 	else
6514 		max_rings = bp->rx_nr_rings;
6515 
6516 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6517 	if (rss_ctx)
6518 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6519 	else
6520 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6521 
6522 	for (i = 0; i < max_entries; i++)
6523 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6524 
6525 	pad = bp->rss_indir_tbl_entries - max_entries;
6526 	if (pad)
6527 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6528 }
6529 
6530 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6531 {
6532 	u32 i, tbl_size, max_ring = 0;
6533 
6534 	if (!bp->rss_indir_tbl)
6535 		return 0;
6536 
6537 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6538 	for (i = 0; i < tbl_size; i++)
6539 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6540 	return max_ring;
6541 }
6542 
6543 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6544 {
6545 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6546 		if (!rx_rings)
6547 			return 0;
6548 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6549 					       BNXT_RSS_TABLE_ENTRIES_P5);
6550 	}
6551 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6552 		return 2;
6553 	return 1;
6554 }
6555 
6556 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6557 {
6558 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6559 	u16 i, j;
6560 
6561 	/* Fill the RSS indirection table with ring group ids */
6562 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6563 		if (!no_rss)
6564 			j = bp->rss_indir_tbl[i];
6565 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6566 	}
6567 }
6568 
6569 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6570 				    struct bnxt_vnic_info *vnic)
6571 {
6572 	__le16 *ring_tbl = vnic->rss_table;
6573 	struct bnxt_rx_ring_info *rxr;
6574 	u16 tbl_size, i;
6575 
6576 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6577 
6578 	for (i = 0; i < tbl_size; i++) {
6579 		u16 ring_id, j;
6580 
6581 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6582 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6583 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6584 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6585 		else
6586 			j = bp->rss_indir_tbl[i];
6587 		rxr = &bp->rx_ring[j];
6588 
6589 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6590 		*ring_tbl++ = cpu_to_le16(ring_id);
6591 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6592 		*ring_tbl++ = cpu_to_le16(ring_id);
6593 	}
6594 }
6595 
6596 static void
6597 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6598 			 struct bnxt_vnic_info *vnic)
6599 {
6600 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6601 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6602 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6603 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6604 	} else {
6605 		bnxt_fill_hw_rss_tbl(bp, vnic);
6606 	}
6607 
6608 	if (bp->rss_hash_delta) {
6609 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6610 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6611 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6612 		else
6613 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6614 	} else {
6615 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6616 	}
6617 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6618 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6619 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6620 }
6621 
6622 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6623 				  bool set_rss)
6624 {
6625 	struct hwrm_vnic_rss_cfg_input *req;
6626 	int rc;
6627 
6628 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6629 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6630 		return 0;
6631 
6632 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6633 	if (rc)
6634 		return rc;
6635 
6636 	if (set_rss)
6637 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6638 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6639 	return hwrm_req_send(bp, req);
6640 }
6641 
6642 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6643 				     struct bnxt_vnic_info *vnic, bool set_rss)
6644 {
6645 	struct hwrm_vnic_rss_cfg_input *req;
6646 	dma_addr_t ring_tbl_map;
6647 	u32 i, nr_ctxs;
6648 	int rc;
6649 
6650 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6651 	if (rc)
6652 		return rc;
6653 
6654 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6655 	if (!set_rss)
6656 		return hwrm_req_send(bp, req);
6657 
6658 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6659 	ring_tbl_map = vnic->rss_table_dma_addr;
6660 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6661 
6662 	hwrm_req_hold(bp, req);
6663 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6664 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6665 		req->ring_table_pair_index = i;
6666 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6667 		rc = hwrm_req_send(bp, req);
6668 		if (rc)
6669 			goto exit;
6670 	}
6671 
6672 exit:
6673 	hwrm_req_drop(bp, req);
6674 	return rc;
6675 }
6676 
6677 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6678 {
6679 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6680 	struct hwrm_vnic_rss_qcfg_output *resp;
6681 	struct hwrm_vnic_rss_qcfg_input *req;
6682 
6683 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6684 		return;
6685 
6686 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6687 	/* all contexts configured to same hash_type, zero always exists */
6688 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6689 	resp = hwrm_req_hold(bp, req);
6690 	if (!hwrm_req_send(bp, req)) {
6691 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6692 		bp->rss_hash_delta = 0;
6693 	}
6694 	hwrm_req_drop(bp, req);
6695 }
6696 
6697 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6698 {
6699 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6700 	struct hwrm_vnic_plcmodes_cfg_input *req;
6701 	int rc;
6702 
6703 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6704 	if (rc)
6705 		return rc;
6706 
6707 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6708 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6709 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6710 
6711 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6712 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6713 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6714 		req->enables |=
6715 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6716 		req->hds_threshold = cpu_to_le16(hds_thresh);
6717 	}
6718 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6719 	return hwrm_req_send(bp, req);
6720 }
6721 
6722 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6723 					struct bnxt_vnic_info *vnic,
6724 					u16 ctx_idx)
6725 {
6726 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6727 
6728 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6729 		return;
6730 
6731 	req->rss_cos_lb_ctx_id =
6732 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6733 
6734 	hwrm_req_send(bp, req);
6735 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6736 }
6737 
6738 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6739 {
6740 	int i, j;
6741 
6742 	for (i = 0; i < bp->nr_vnics; i++) {
6743 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6744 
6745 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6746 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6747 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6748 		}
6749 	}
6750 	bp->rsscos_nr_ctxs = 0;
6751 }
6752 
6753 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6754 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6755 {
6756 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6757 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6758 	int rc;
6759 
6760 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6761 	if (rc)
6762 		return rc;
6763 
6764 	resp = hwrm_req_hold(bp, req);
6765 	rc = hwrm_req_send(bp, req);
6766 	if (!rc)
6767 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6768 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6769 	hwrm_req_drop(bp, req);
6770 
6771 	return rc;
6772 }
6773 
6774 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6775 {
6776 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6777 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6778 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6779 }
6780 
6781 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6782 {
6783 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6784 	struct hwrm_vnic_cfg_input *req;
6785 	unsigned int ring = 0, grp_idx;
6786 	u16 def_vlan = 0;
6787 	int rc;
6788 
6789 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6790 	if (rc)
6791 		return rc;
6792 
6793 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6794 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6795 
6796 		req->default_rx_ring_id =
6797 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6798 		req->default_cmpl_ring_id =
6799 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6800 		req->enables =
6801 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6802 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6803 		goto vnic_mru;
6804 	}
6805 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6806 	/* Only RSS support for now TBD: COS & LB */
6807 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6808 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6809 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6810 					   VNIC_CFG_REQ_ENABLES_MRU);
6811 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6812 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6813 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6814 					   VNIC_CFG_REQ_ENABLES_MRU);
6815 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6816 	} else {
6817 		req->rss_rule = cpu_to_le16(0xffff);
6818 	}
6819 
6820 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6821 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6822 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6823 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6824 	} else {
6825 		req->cos_rule = cpu_to_le16(0xffff);
6826 	}
6827 
6828 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6829 		ring = 0;
6830 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6831 		ring = vnic->vnic_id - 1;
6832 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6833 		ring = bp->rx_nr_rings - 1;
6834 
6835 	grp_idx = bp->rx_ring[ring].bnapi->index;
6836 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6837 	req->lb_rule = cpu_to_le16(0xffff);
6838 vnic_mru:
6839 	vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
6840 	req->mru = cpu_to_le16(vnic->mru);
6841 
6842 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6843 #ifdef CONFIG_BNXT_SRIOV
6844 	if (BNXT_VF(bp))
6845 		def_vlan = bp->vf.vlan;
6846 #endif
6847 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6848 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6849 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6850 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6851 
6852 	return hwrm_req_send(bp, req);
6853 }
6854 
6855 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6856 				    struct bnxt_vnic_info *vnic)
6857 {
6858 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6859 		struct hwrm_vnic_free_input *req;
6860 
6861 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6862 			return;
6863 
6864 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6865 
6866 		hwrm_req_send(bp, req);
6867 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6868 	}
6869 }
6870 
6871 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6872 {
6873 	u16 i;
6874 
6875 	for (i = 0; i < bp->nr_vnics; i++)
6876 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6877 }
6878 
6879 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6880 			 unsigned int start_rx_ring_idx,
6881 			 unsigned int nr_rings)
6882 {
6883 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6884 	struct hwrm_vnic_alloc_output *resp;
6885 	struct hwrm_vnic_alloc_input *req;
6886 	int rc;
6887 
6888 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6889 	if (rc)
6890 		return rc;
6891 
6892 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6893 		goto vnic_no_ring_grps;
6894 
6895 	/* map ring groups to this vnic */
6896 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6897 		grp_idx = bp->rx_ring[i].bnapi->index;
6898 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6899 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6900 				   j, nr_rings);
6901 			break;
6902 		}
6903 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6904 	}
6905 
6906 vnic_no_ring_grps:
6907 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6908 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6909 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6910 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6911 
6912 	resp = hwrm_req_hold(bp, req);
6913 	rc = hwrm_req_send(bp, req);
6914 	if (!rc)
6915 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6916 	hwrm_req_drop(bp, req);
6917 	return rc;
6918 }
6919 
6920 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6921 {
6922 	struct hwrm_vnic_qcaps_output *resp;
6923 	struct hwrm_vnic_qcaps_input *req;
6924 	int rc;
6925 
6926 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6927 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6928 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6929 	if (bp->hwrm_spec_code < 0x10600)
6930 		return 0;
6931 
6932 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6933 	if (rc)
6934 		return rc;
6935 
6936 	resp = hwrm_req_hold(bp, req);
6937 	rc = hwrm_req_send(bp, req);
6938 	if (!rc) {
6939 		u32 flags = le32_to_cpu(resp->flags);
6940 
6941 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6942 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6943 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6944 		if (flags &
6945 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6946 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6947 
6948 		/* Older P5 fw before EXT_HW_STATS support did not set
6949 		 * VLAN_STRIP_CAP properly.
6950 		 */
6951 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6952 		    (BNXT_CHIP_P5(bp) &&
6953 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6954 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6955 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6956 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6957 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6958 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6959 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6960 		if (bp->max_tpa_v2) {
6961 			if (BNXT_CHIP_P5(bp))
6962 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6963 			else
6964 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6965 		}
6966 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6967 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6968 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6969 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6970 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6971 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6972 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6973 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6974 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6975 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6976 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPV6_FLOW_LABEL_CAP)
6977 			bp->rss_cap |= BNXT_RSS_CAP_IPV6_FLOW_LABEL_RSS_CAP;
6978 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6979 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6980 	}
6981 	hwrm_req_drop(bp, req);
6982 	return rc;
6983 }
6984 
6985 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6986 {
6987 	struct hwrm_ring_grp_alloc_output *resp;
6988 	struct hwrm_ring_grp_alloc_input *req;
6989 	int rc;
6990 	u16 i;
6991 
6992 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6993 		return 0;
6994 
6995 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6996 	if (rc)
6997 		return rc;
6998 
6999 	resp = hwrm_req_hold(bp, req);
7000 	for (i = 0; i < bp->rx_nr_rings; i++) {
7001 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
7002 
7003 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
7004 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
7005 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
7006 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
7007 
7008 		rc = hwrm_req_send(bp, req);
7009 
7010 		if (rc)
7011 			break;
7012 
7013 		bp->grp_info[grp_idx].fw_grp_id =
7014 			le32_to_cpu(resp->ring_group_id);
7015 	}
7016 	hwrm_req_drop(bp, req);
7017 	return rc;
7018 }
7019 
7020 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
7021 {
7022 	struct hwrm_ring_grp_free_input *req;
7023 	u16 i;
7024 
7025 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7026 		return;
7027 
7028 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
7029 		return;
7030 
7031 	hwrm_req_hold(bp, req);
7032 	for (i = 0; i < bp->cp_nr_rings; i++) {
7033 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
7034 			continue;
7035 		req->ring_group_id =
7036 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
7037 
7038 		hwrm_req_send(bp, req);
7039 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
7040 	}
7041 	hwrm_req_drop(bp, req);
7042 }
7043 
7044 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
7045 				       struct hwrm_ring_alloc_input *req,
7046 				       struct bnxt_ring_struct *ring)
7047 {
7048 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
7049 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
7050 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
7051 
7052 	if (ring_type == HWRM_RING_ALLOC_AGG) {
7053 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7054 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7055 		req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
7056 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
7057 	} else {
7058 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
7059 		if (NET_IP_ALIGN == 2)
7060 			req->flags =
7061 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
7062 	}
7063 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7064 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7065 	req->enables |= cpu_to_le32(enables);
7066 }
7067 
7068 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7069 				    struct bnxt_ring_struct *ring,
7070 				    u32 ring_type, u32 map_index)
7071 {
7072 	struct hwrm_ring_alloc_output *resp;
7073 	struct hwrm_ring_alloc_input *req;
7074 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7075 	struct bnxt_ring_grp_info *grp_info;
7076 	int rc, err = 0;
7077 	u16 ring_id;
7078 
7079 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7080 	if (rc)
7081 		goto exit;
7082 
7083 	req->enables = 0;
7084 	if (rmem->nr_pages > 1) {
7085 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7086 		/* Page size is in log2 units */
7087 		req->page_size = BNXT_PAGE_SHIFT;
7088 		req->page_tbl_depth = 1;
7089 	} else {
7090 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7091 	}
7092 	req->fbo = 0;
7093 	/* Association of ring index with doorbell index and MSIX number */
7094 	req->logical_id = cpu_to_le16(map_index);
7095 
7096 	switch (ring_type) {
7097 	case HWRM_RING_ALLOC_TX: {
7098 		struct bnxt_tx_ring_info *txr;
7099 		u16 flags = 0;
7100 
7101 		txr = container_of(ring, struct bnxt_tx_ring_info,
7102 				   tx_ring_struct);
7103 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7104 		/* Association of transmit ring with completion ring */
7105 		grp_info = &bp->grp_info[ring->grp_idx];
7106 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7107 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7108 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7109 		req->queue_id = cpu_to_le16(ring->queue_id);
7110 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7111 			req->cmpl_coal_cnt =
7112 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7113 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7114 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7115 		req->flags = cpu_to_le16(flags);
7116 		break;
7117 	}
7118 	case HWRM_RING_ALLOC_RX:
7119 	case HWRM_RING_ALLOC_AGG:
7120 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7121 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7122 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7123 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7124 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7125 			bnxt_set_rx_ring_params_p5(bp, ring_type, req, ring);
7126 		break;
7127 	case HWRM_RING_ALLOC_CMPL:
7128 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7129 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7130 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7131 			/* Association of cp ring with nq */
7132 			grp_info = &bp->grp_info[map_index];
7133 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7134 			req->cq_handle = cpu_to_le64(ring->handle);
7135 			req->enables |= cpu_to_le32(
7136 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7137 		} else {
7138 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7139 		}
7140 		break;
7141 	case HWRM_RING_ALLOC_NQ:
7142 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7143 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7144 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7145 		break;
7146 	default:
7147 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7148 			   ring_type);
7149 		return -EINVAL;
7150 	}
7151 
7152 	resp = hwrm_req_hold(bp, req);
7153 	rc = hwrm_req_send(bp, req);
7154 	err = le16_to_cpu(resp->error_code);
7155 	ring_id = le16_to_cpu(resp->ring_id);
7156 	hwrm_req_drop(bp, req);
7157 
7158 exit:
7159 	if (rc || err) {
7160 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7161 			   ring_type, rc, err);
7162 		return -EIO;
7163 	}
7164 	ring->fw_ring_id = ring_id;
7165 	return rc;
7166 }
7167 
7168 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7169 {
7170 	int rc;
7171 
7172 	if (BNXT_PF(bp)) {
7173 		struct hwrm_func_cfg_input *req;
7174 
7175 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7176 		if (rc)
7177 			return rc;
7178 
7179 		req->fid = cpu_to_le16(0xffff);
7180 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7181 		req->async_event_cr = cpu_to_le16(idx);
7182 		return hwrm_req_send(bp, req);
7183 	} else {
7184 		struct hwrm_func_vf_cfg_input *req;
7185 
7186 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7187 		if (rc)
7188 			return rc;
7189 
7190 		req->enables =
7191 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7192 		req->async_event_cr = cpu_to_le16(idx);
7193 		return hwrm_req_send(bp, req);
7194 	}
7195 }
7196 
7197 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7198 			     u32 ring_type)
7199 {
7200 	switch (ring_type) {
7201 	case HWRM_RING_ALLOC_TX:
7202 		db->db_ring_mask = bp->tx_ring_mask;
7203 		break;
7204 	case HWRM_RING_ALLOC_RX:
7205 		db->db_ring_mask = bp->rx_ring_mask;
7206 		break;
7207 	case HWRM_RING_ALLOC_AGG:
7208 		db->db_ring_mask = bp->rx_agg_ring_mask;
7209 		break;
7210 	case HWRM_RING_ALLOC_CMPL:
7211 	case HWRM_RING_ALLOC_NQ:
7212 		db->db_ring_mask = bp->cp_ring_mask;
7213 		break;
7214 	}
7215 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7216 		db->db_epoch_mask = db->db_ring_mask + 1;
7217 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7218 	}
7219 }
7220 
7221 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7222 			u32 map_idx, u32 xid)
7223 {
7224 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7225 		switch (ring_type) {
7226 		case HWRM_RING_ALLOC_TX:
7227 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7228 			break;
7229 		case HWRM_RING_ALLOC_RX:
7230 		case HWRM_RING_ALLOC_AGG:
7231 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7232 			break;
7233 		case HWRM_RING_ALLOC_CMPL:
7234 			db->db_key64 = DBR_PATH_L2;
7235 			break;
7236 		case HWRM_RING_ALLOC_NQ:
7237 			db->db_key64 = DBR_PATH_L2;
7238 			break;
7239 		}
7240 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7241 
7242 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7243 			db->db_key64 |= DBR_VALID;
7244 
7245 		db->doorbell = bp->bar1 + bp->db_offset;
7246 	} else {
7247 		db->doorbell = bp->bar1 + map_idx * 0x80;
7248 		switch (ring_type) {
7249 		case HWRM_RING_ALLOC_TX:
7250 			db->db_key32 = DB_KEY_TX;
7251 			break;
7252 		case HWRM_RING_ALLOC_RX:
7253 		case HWRM_RING_ALLOC_AGG:
7254 			db->db_key32 = DB_KEY_RX;
7255 			break;
7256 		case HWRM_RING_ALLOC_CMPL:
7257 			db->db_key32 = DB_KEY_CP;
7258 			break;
7259 		}
7260 	}
7261 	bnxt_set_db_mask(bp, db, ring_type);
7262 }
7263 
7264 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7265 				   struct bnxt_rx_ring_info *rxr)
7266 {
7267 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7268 	struct bnxt_napi *bnapi = rxr->bnapi;
7269 	u32 type = HWRM_RING_ALLOC_RX;
7270 	u32 map_idx = bnapi->index;
7271 	int rc;
7272 
7273 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7274 	if (rc)
7275 		return rc;
7276 
7277 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7278 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7279 
7280 	return 0;
7281 }
7282 
7283 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7284 				       struct bnxt_rx_ring_info *rxr)
7285 {
7286 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7287 	u32 type = HWRM_RING_ALLOC_AGG;
7288 	u32 grp_idx = ring->grp_idx;
7289 	u32 map_idx;
7290 	int rc;
7291 
7292 	map_idx = grp_idx + bp->rx_nr_rings;
7293 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7294 	if (rc)
7295 		return rc;
7296 
7297 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7298 		    ring->fw_ring_id);
7299 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7300 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7301 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7302 
7303 	return 0;
7304 }
7305 
7306 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7307 				      struct bnxt_cp_ring_info *cpr)
7308 {
7309 	const u32 type = HWRM_RING_ALLOC_CMPL;
7310 	struct bnxt_napi *bnapi = cpr->bnapi;
7311 	struct bnxt_ring_struct *ring;
7312 	u32 map_idx = bnapi->index;
7313 	int rc;
7314 
7315 	ring = &cpr->cp_ring_struct;
7316 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7317 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7318 	if (rc)
7319 		return rc;
7320 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7321 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7322 	return 0;
7323 }
7324 
7325 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7326 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7327 {
7328 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7329 	const u32 type = HWRM_RING_ALLOC_TX;
7330 	int rc;
7331 
7332 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, tx_idx);
7333 	if (rc)
7334 		return rc;
7335 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7336 	return 0;
7337 }
7338 
7339 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7340 {
7341 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7342 	int i, rc = 0;
7343 	u32 type;
7344 
7345 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7346 		type = HWRM_RING_ALLOC_NQ;
7347 	else
7348 		type = HWRM_RING_ALLOC_CMPL;
7349 	for (i = 0; i < bp->cp_nr_rings; i++) {
7350 		struct bnxt_napi *bnapi = bp->bnapi[i];
7351 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7352 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7353 		u32 map_idx = ring->map_idx;
7354 		unsigned int vector;
7355 
7356 		vector = bp->irq_tbl[map_idx].vector;
7357 		disable_irq_nosync(vector);
7358 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7359 		if (rc) {
7360 			enable_irq(vector);
7361 			goto err_out;
7362 		}
7363 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7364 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7365 		enable_irq(vector);
7366 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7367 
7368 		if (!i) {
7369 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7370 			if (rc)
7371 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7372 		}
7373 	}
7374 
7375 	for (i = 0; i < bp->tx_nr_rings; i++) {
7376 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7377 
7378 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7379 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7380 			if (rc)
7381 				goto err_out;
7382 		}
7383 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7384 		if (rc)
7385 			goto err_out;
7386 	}
7387 
7388 	for (i = 0; i < bp->rx_nr_rings; i++) {
7389 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7390 
7391 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7392 		if (rc)
7393 			goto err_out;
7394 		/* If we have agg rings, post agg buffers first. */
7395 		if (!agg_rings)
7396 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7397 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7398 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7399 			if (rc)
7400 				goto err_out;
7401 		}
7402 	}
7403 
7404 	if (agg_rings) {
7405 		for (i = 0; i < bp->rx_nr_rings; i++) {
7406 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7407 			if (rc)
7408 				goto err_out;
7409 		}
7410 	}
7411 err_out:
7412 	return rc;
7413 }
7414 
7415 static void bnxt_cancel_dim(struct bnxt *bp)
7416 {
7417 	int i;
7418 
7419 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7420 	 * if NAPI is enabled.
7421 	 */
7422 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7423 		return;
7424 
7425 	/* Make sure NAPI sees that the VNIC is disabled */
7426 	synchronize_net();
7427 	for (i = 0; i < bp->rx_nr_rings; i++) {
7428 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7429 		struct bnxt_napi *bnapi = rxr->bnapi;
7430 
7431 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7432 	}
7433 }
7434 
7435 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7436 				   struct bnxt_ring_struct *ring,
7437 				   u32 ring_type, int cmpl_ring_id)
7438 {
7439 	struct hwrm_ring_free_output *resp;
7440 	struct hwrm_ring_free_input *req;
7441 	u16 error_code = 0;
7442 	int rc;
7443 
7444 	if (BNXT_NO_FW_ACCESS(bp))
7445 		return 0;
7446 
7447 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7448 	if (rc)
7449 		goto exit;
7450 
7451 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7452 	req->ring_type = ring_type;
7453 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7454 
7455 	resp = hwrm_req_hold(bp, req);
7456 	rc = hwrm_req_send(bp, req);
7457 	error_code = le16_to_cpu(resp->error_code);
7458 	hwrm_req_drop(bp, req);
7459 exit:
7460 	if (rc || error_code) {
7461 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7462 			   ring_type, rc, error_code);
7463 		return -EIO;
7464 	}
7465 	return 0;
7466 }
7467 
7468 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7469 				   struct bnxt_tx_ring_info *txr,
7470 				   bool close_path)
7471 {
7472 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7473 	u32 cmpl_ring_id;
7474 
7475 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7476 		return;
7477 
7478 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7479 		       INVALID_HW_RING_ID;
7480 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7481 				cmpl_ring_id);
7482 	ring->fw_ring_id = INVALID_HW_RING_ID;
7483 }
7484 
7485 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7486 				   struct bnxt_rx_ring_info *rxr,
7487 				   bool close_path)
7488 {
7489 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7490 	u32 grp_idx = rxr->bnapi->index;
7491 	u32 cmpl_ring_id;
7492 
7493 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7494 		return;
7495 
7496 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7497 	hwrm_ring_free_send_msg(bp, ring,
7498 				RING_FREE_REQ_RING_TYPE_RX,
7499 				close_path ? cmpl_ring_id :
7500 				INVALID_HW_RING_ID);
7501 	ring->fw_ring_id = INVALID_HW_RING_ID;
7502 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7503 }
7504 
7505 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7506 				       struct bnxt_rx_ring_info *rxr,
7507 				       bool close_path)
7508 {
7509 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7510 	u32 grp_idx = rxr->bnapi->index;
7511 	u32 type, cmpl_ring_id;
7512 
7513 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7514 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7515 	else
7516 		type = RING_FREE_REQ_RING_TYPE_RX;
7517 
7518 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7519 		return;
7520 
7521 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7522 	hwrm_ring_free_send_msg(bp, ring, type,
7523 				close_path ? cmpl_ring_id :
7524 				INVALID_HW_RING_ID);
7525 	ring->fw_ring_id = INVALID_HW_RING_ID;
7526 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7527 }
7528 
7529 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7530 				   struct bnxt_cp_ring_info *cpr)
7531 {
7532 	struct bnxt_ring_struct *ring;
7533 
7534 	ring = &cpr->cp_ring_struct;
7535 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7536 		return;
7537 
7538 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7539 				INVALID_HW_RING_ID);
7540 	ring->fw_ring_id = INVALID_HW_RING_ID;
7541 }
7542 
7543 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7544 {
7545 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7546 	int i, size = ring->ring_mem.page_size;
7547 
7548 	cpr->cp_raw_cons = 0;
7549 	cpr->toggle = 0;
7550 
7551 	for (i = 0; i < bp->cp_nr_pages; i++)
7552 		if (cpr->cp_desc_ring[i])
7553 			memset(cpr->cp_desc_ring[i], 0, size);
7554 }
7555 
7556 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7557 {
7558 	u32 type;
7559 	int i;
7560 
7561 	if (!bp->bnapi)
7562 		return;
7563 
7564 	for (i = 0; i < bp->tx_nr_rings; i++)
7565 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7566 
7567 	bnxt_cancel_dim(bp);
7568 	for (i = 0; i < bp->rx_nr_rings; i++) {
7569 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7570 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7571 	}
7572 
7573 	/* The completion rings are about to be freed.  After that the
7574 	 * IRQ doorbell will not work anymore.  So we need to disable
7575 	 * IRQ here.
7576 	 */
7577 	bnxt_disable_int_sync(bp);
7578 
7579 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7580 		type = RING_FREE_REQ_RING_TYPE_NQ;
7581 	else
7582 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7583 	for (i = 0; i < bp->cp_nr_rings; i++) {
7584 		struct bnxt_napi *bnapi = bp->bnapi[i];
7585 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7586 		struct bnxt_ring_struct *ring;
7587 		int j;
7588 
7589 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7590 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7591 
7592 		ring = &cpr->cp_ring_struct;
7593 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7594 			hwrm_ring_free_send_msg(bp, ring, type,
7595 						INVALID_HW_RING_ID);
7596 			ring->fw_ring_id = INVALID_HW_RING_ID;
7597 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7598 		}
7599 	}
7600 }
7601 
7602 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7603 			     bool shared);
7604 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7605 			   bool shared);
7606 
7607 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7608 {
7609 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7610 	struct hwrm_func_qcfg_output *resp;
7611 	struct hwrm_func_qcfg_input *req;
7612 	int rc;
7613 
7614 	if (bp->hwrm_spec_code < 0x10601)
7615 		return 0;
7616 
7617 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7618 	if (rc)
7619 		return rc;
7620 
7621 	req->fid = cpu_to_le16(0xffff);
7622 	resp = hwrm_req_hold(bp, req);
7623 	rc = hwrm_req_send(bp, req);
7624 	if (rc) {
7625 		hwrm_req_drop(bp, req);
7626 		return rc;
7627 	}
7628 
7629 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7630 	if (BNXT_NEW_RM(bp)) {
7631 		u16 cp, stats;
7632 
7633 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7634 		hw_resc->resv_hw_ring_grps =
7635 			le32_to_cpu(resp->alloc_hw_ring_grps);
7636 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7637 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7638 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7639 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7640 		hw_resc->resv_irqs = cp;
7641 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7642 			int rx = hw_resc->resv_rx_rings;
7643 			int tx = hw_resc->resv_tx_rings;
7644 
7645 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7646 				rx >>= 1;
7647 			if (cp < (rx + tx)) {
7648 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7649 				if (rc)
7650 					goto get_rings_exit;
7651 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7652 					rx <<= 1;
7653 				hw_resc->resv_rx_rings = rx;
7654 				hw_resc->resv_tx_rings = tx;
7655 			}
7656 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7657 			hw_resc->resv_hw_ring_grps = rx;
7658 		}
7659 		hw_resc->resv_cp_rings = cp;
7660 		hw_resc->resv_stat_ctxs = stats;
7661 	}
7662 get_rings_exit:
7663 	hwrm_req_drop(bp, req);
7664 	return rc;
7665 }
7666 
7667 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7668 {
7669 	struct hwrm_func_qcfg_output *resp;
7670 	struct hwrm_func_qcfg_input *req;
7671 	int rc;
7672 
7673 	if (bp->hwrm_spec_code < 0x10601)
7674 		return 0;
7675 
7676 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7677 	if (rc)
7678 		return rc;
7679 
7680 	req->fid = cpu_to_le16(fid);
7681 	resp = hwrm_req_hold(bp, req);
7682 	rc = hwrm_req_send(bp, req);
7683 	if (!rc)
7684 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7685 
7686 	hwrm_req_drop(bp, req);
7687 	return rc;
7688 }
7689 
7690 static bool bnxt_rfs_supported(struct bnxt *bp);
7691 
7692 static struct hwrm_func_cfg_input *
7693 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7694 {
7695 	struct hwrm_func_cfg_input *req;
7696 	u32 enables = 0;
7697 
7698 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7699 		return NULL;
7700 
7701 	req->fid = cpu_to_le16(0xffff);
7702 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7703 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7704 	if (BNXT_NEW_RM(bp)) {
7705 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7706 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7707 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7708 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7709 			enables |= hwr->cp_p5 ?
7710 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7711 		} else {
7712 			enables |= hwr->cp ?
7713 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7714 			enables |= hwr->grp ?
7715 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7716 		}
7717 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7718 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7719 					  0;
7720 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7721 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7722 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7723 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7724 			req->num_msix = cpu_to_le16(hwr->cp);
7725 		} else {
7726 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7727 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7728 		}
7729 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7730 		req->num_vnics = cpu_to_le16(hwr->vnic);
7731 	}
7732 	req->enables = cpu_to_le32(enables);
7733 	return req;
7734 }
7735 
7736 static struct hwrm_func_vf_cfg_input *
7737 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7738 {
7739 	struct hwrm_func_vf_cfg_input *req;
7740 	u32 enables = 0;
7741 
7742 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7743 		return NULL;
7744 
7745 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7746 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7747 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7748 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7749 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7750 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7751 		enables |= hwr->cp_p5 ?
7752 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7753 	} else {
7754 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7755 		enables |= hwr->grp ?
7756 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7757 	}
7758 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7759 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7760 
7761 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7762 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7763 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7764 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7765 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7766 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7767 	} else {
7768 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7769 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7770 	}
7771 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7772 	req->num_vnics = cpu_to_le16(hwr->vnic);
7773 
7774 	req->enables = cpu_to_le32(enables);
7775 	return req;
7776 }
7777 
7778 static int
7779 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7780 {
7781 	struct hwrm_func_cfg_input *req;
7782 	int rc;
7783 
7784 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7785 	if (!req)
7786 		return -ENOMEM;
7787 
7788 	if (!req->enables) {
7789 		hwrm_req_drop(bp, req);
7790 		return 0;
7791 	}
7792 
7793 	rc = hwrm_req_send(bp, req);
7794 	if (rc)
7795 		return rc;
7796 
7797 	if (bp->hwrm_spec_code < 0x10601)
7798 		bp->hw_resc.resv_tx_rings = hwr->tx;
7799 
7800 	return bnxt_hwrm_get_rings(bp);
7801 }
7802 
7803 static int
7804 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7805 {
7806 	struct hwrm_func_vf_cfg_input *req;
7807 	int rc;
7808 
7809 	if (!BNXT_NEW_RM(bp)) {
7810 		bp->hw_resc.resv_tx_rings = hwr->tx;
7811 		return 0;
7812 	}
7813 
7814 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7815 	if (!req)
7816 		return -ENOMEM;
7817 
7818 	rc = hwrm_req_send(bp, req);
7819 	if (rc)
7820 		return rc;
7821 
7822 	return bnxt_hwrm_get_rings(bp);
7823 }
7824 
7825 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7826 {
7827 	if (BNXT_PF(bp))
7828 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7829 	else
7830 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7831 }
7832 
7833 int bnxt_nq_rings_in_use(struct bnxt *bp)
7834 {
7835 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7836 }
7837 
7838 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7839 {
7840 	int cp;
7841 
7842 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7843 		return bnxt_nq_rings_in_use(bp);
7844 
7845 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7846 	return cp;
7847 }
7848 
7849 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7850 {
7851 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7852 }
7853 
7854 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7855 {
7856 	if (!hwr->grp)
7857 		return 0;
7858 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7859 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7860 
7861 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7862 			rss_ctx *= hwr->vnic;
7863 		return rss_ctx;
7864 	}
7865 	if (BNXT_VF(bp))
7866 		return BNXT_VF_MAX_RSS_CTX;
7867 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7868 		return hwr->grp + 1;
7869 	return 1;
7870 }
7871 
7872 /* Check if a default RSS map needs to be setup.  This function is only
7873  * used on older firmware that does not require reserving RX rings.
7874  */
7875 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7876 {
7877 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7878 
7879 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7880 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7881 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7882 		if (!netif_is_rxfh_configured(bp->dev))
7883 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7884 	}
7885 }
7886 
7887 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7888 {
7889 	if (bp->flags & BNXT_FLAG_RFS) {
7890 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7891 			return 2 + bp->num_rss_ctx;
7892 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7893 			return rx_rings + 1;
7894 	}
7895 	return 1;
7896 }
7897 
7898 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7899 {
7900 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7901 	int cp = bnxt_cp_rings_in_use(bp);
7902 	int nq = bnxt_nq_rings_in_use(bp);
7903 	int rx = bp->rx_nr_rings, stat;
7904 	int vnic, grp = rx;
7905 
7906 	/* Old firmware does not need RX ring reservations but we still
7907 	 * need to setup a default RSS map when needed.  With new firmware
7908 	 * we go through RX ring reservations first and then set up the
7909 	 * RSS map for the successfully reserved RX rings when needed.
7910 	 */
7911 	if (!BNXT_NEW_RM(bp))
7912 		bnxt_check_rss_tbl_no_rmgr(bp);
7913 
7914 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7915 	    bp->hwrm_spec_code >= 0x10601)
7916 		return true;
7917 
7918 	if (!BNXT_NEW_RM(bp))
7919 		return false;
7920 
7921 	vnic = bnxt_get_total_vnics(bp, rx);
7922 
7923 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7924 		rx <<= 1;
7925 	stat = bnxt_get_func_stat_ctxs(bp);
7926 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7927 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7928 	    (hw_resc->resv_hw_ring_grps != grp &&
7929 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7930 		return true;
7931 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7932 	    hw_resc->resv_irqs != nq)
7933 		return true;
7934 	return false;
7935 }
7936 
7937 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7938 {
7939 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7940 
7941 	hwr->tx = hw_resc->resv_tx_rings;
7942 	if (BNXT_NEW_RM(bp)) {
7943 		hwr->rx = hw_resc->resv_rx_rings;
7944 		hwr->cp = hw_resc->resv_irqs;
7945 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7946 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7947 		hwr->grp = hw_resc->resv_hw_ring_grps;
7948 		hwr->vnic = hw_resc->resv_vnics;
7949 		hwr->stat = hw_resc->resv_stat_ctxs;
7950 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7951 	}
7952 }
7953 
7954 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7955 {
7956 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7957 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7958 }
7959 
7960 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7961 
7962 static int __bnxt_reserve_rings(struct bnxt *bp)
7963 {
7964 	struct bnxt_hw_rings hwr = {0};
7965 	int rx_rings, old_rx_rings, rc;
7966 	int cp = bp->cp_nr_rings;
7967 	int ulp_msix = 0;
7968 	bool sh = false;
7969 	int tx_cp;
7970 
7971 	if (!bnxt_need_reserve_rings(bp))
7972 		return 0;
7973 
7974 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7975 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7976 		if (!ulp_msix)
7977 			bnxt_set_ulp_stat_ctxs(bp, 0);
7978 
7979 		if (ulp_msix > bp->ulp_num_msix_want)
7980 			ulp_msix = bp->ulp_num_msix_want;
7981 		hwr.cp = cp + ulp_msix;
7982 	} else {
7983 		hwr.cp = bnxt_nq_rings_in_use(bp);
7984 	}
7985 
7986 	hwr.tx = bp->tx_nr_rings;
7987 	hwr.rx = bp->rx_nr_rings;
7988 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7989 		sh = true;
7990 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7991 		hwr.cp_p5 = hwr.rx + hwr.tx;
7992 
7993 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7994 
7995 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7996 		hwr.rx <<= 1;
7997 	hwr.grp = bp->rx_nr_rings;
7998 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7999 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
8000 	old_rx_rings = bp->hw_resc.resv_rx_rings;
8001 
8002 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
8003 	if (rc)
8004 		return rc;
8005 
8006 	bnxt_copy_reserved_rings(bp, &hwr);
8007 
8008 	rx_rings = hwr.rx;
8009 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8010 		if (hwr.rx >= 2) {
8011 			rx_rings = hwr.rx >> 1;
8012 		} else {
8013 			if (netif_running(bp->dev))
8014 				return -ENOMEM;
8015 
8016 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
8017 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
8018 			bp->dev->hw_features &= ~NETIF_F_LRO;
8019 			bp->dev->features &= ~NETIF_F_LRO;
8020 			bnxt_set_ring_params(bp);
8021 		}
8022 	}
8023 	rx_rings = min_t(int, rx_rings, hwr.grp);
8024 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
8025 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
8026 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
8027 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
8028 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
8029 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8030 		hwr.rx = rx_rings << 1;
8031 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
8032 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
8033 	bp->tx_nr_rings = hwr.tx;
8034 
8035 	/* If we cannot reserve all the RX rings, reset the RSS map only
8036 	 * if absolutely necessary
8037 	 */
8038 	if (rx_rings != bp->rx_nr_rings) {
8039 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
8040 			    rx_rings, bp->rx_nr_rings);
8041 		if (netif_is_rxfh_configured(bp->dev) &&
8042 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
8043 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
8044 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
8045 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
8046 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
8047 		}
8048 	}
8049 	bp->rx_nr_rings = rx_rings;
8050 	bp->cp_nr_rings = hwr.cp;
8051 
8052 	if (!bnxt_rings_ok(bp, &hwr))
8053 		return -ENOMEM;
8054 
8055 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
8056 	    !netif_is_rxfh_configured(bp->dev))
8057 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8058 
8059 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
8060 		int resv_msix, resv_ctx, ulp_ctxs;
8061 		struct bnxt_hw_resc *hw_resc;
8062 
8063 		hw_resc = &bp->hw_resc;
8064 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
8065 		ulp_msix = min_t(int, resv_msix, ulp_msix);
8066 		bnxt_set_ulp_msix_num(bp, ulp_msix);
8067 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8068 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8069 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8070 	}
8071 
8072 	return rc;
8073 }
8074 
8075 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8076 {
8077 	struct hwrm_func_vf_cfg_input *req;
8078 	u32 flags;
8079 
8080 	if (!BNXT_NEW_RM(bp))
8081 		return 0;
8082 
8083 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8084 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8085 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8086 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8087 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8088 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8089 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8090 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8091 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8092 
8093 	req->flags = cpu_to_le32(flags);
8094 	return hwrm_req_send_silent(bp, req);
8095 }
8096 
8097 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8098 {
8099 	struct hwrm_func_cfg_input *req;
8100 	u32 flags;
8101 
8102 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8103 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8104 	if (BNXT_NEW_RM(bp)) {
8105 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8106 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8107 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8108 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8109 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8110 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8111 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8112 		else
8113 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8114 	}
8115 
8116 	req->flags = cpu_to_le32(flags);
8117 	return hwrm_req_send_silent(bp, req);
8118 }
8119 
8120 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8121 {
8122 	if (bp->hwrm_spec_code < 0x10801)
8123 		return 0;
8124 
8125 	if (BNXT_PF(bp))
8126 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8127 
8128 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8129 }
8130 
8131 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8132 {
8133 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8134 	struct hwrm_ring_aggint_qcaps_output *resp;
8135 	struct hwrm_ring_aggint_qcaps_input *req;
8136 	int rc;
8137 
8138 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8139 	coal_cap->num_cmpl_dma_aggr_max = 63;
8140 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8141 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8142 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8143 	coal_cap->int_lat_tmr_min_max = 65535;
8144 	coal_cap->int_lat_tmr_max_max = 65535;
8145 	coal_cap->num_cmpl_aggr_int_max = 65535;
8146 	coal_cap->timer_units = 80;
8147 
8148 	if (bp->hwrm_spec_code < 0x10902)
8149 		return;
8150 
8151 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8152 		return;
8153 
8154 	resp = hwrm_req_hold(bp, req);
8155 	rc = hwrm_req_send_silent(bp, req);
8156 	if (!rc) {
8157 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8158 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8159 		coal_cap->num_cmpl_dma_aggr_max =
8160 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8161 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8162 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8163 		coal_cap->cmpl_aggr_dma_tmr_max =
8164 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8165 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8166 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8167 		coal_cap->int_lat_tmr_min_max =
8168 			le16_to_cpu(resp->int_lat_tmr_min_max);
8169 		coal_cap->int_lat_tmr_max_max =
8170 			le16_to_cpu(resp->int_lat_tmr_max_max);
8171 		coal_cap->num_cmpl_aggr_int_max =
8172 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8173 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8174 	}
8175 	hwrm_req_drop(bp, req);
8176 }
8177 
8178 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8179 {
8180 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8181 
8182 	return usec * 1000 / coal_cap->timer_units;
8183 }
8184 
8185 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8186 	struct bnxt_coal *hw_coal,
8187 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8188 {
8189 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8190 	u16 val, tmr, max, flags = hw_coal->flags;
8191 	u32 cmpl_params = coal_cap->cmpl_params;
8192 
8193 	max = hw_coal->bufs_per_record * 128;
8194 	if (hw_coal->budget)
8195 		max = hw_coal->bufs_per_record * hw_coal->budget;
8196 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8197 
8198 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8199 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8200 
8201 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8202 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8203 
8204 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8205 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8206 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8207 
8208 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8209 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8210 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8211 
8212 	/* min timer set to 1/2 of interrupt timer */
8213 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8214 		val = tmr / 2;
8215 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8216 		req->int_lat_tmr_min = cpu_to_le16(val);
8217 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8218 	}
8219 
8220 	/* buf timer set to 1/4 of interrupt timer */
8221 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8222 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8223 
8224 	if (cmpl_params &
8225 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8226 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8227 		val = clamp_t(u16, tmr, 1,
8228 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8229 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8230 		req->enables |=
8231 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8232 	}
8233 
8234 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8235 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8236 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8237 	req->flags = cpu_to_le16(flags);
8238 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8239 }
8240 
8241 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8242 				   struct bnxt_coal *hw_coal)
8243 {
8244 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8245 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8246 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8247 	u32 nq_params = coal_cap->nq_params;
8248 	u16 tmr;
8249 	int rc;
8250 
8251 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8252 		return 0;
8253 
8254 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8255 	if (rc)
8256 		return rc;
8257 
8258 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8259 	req->flags =
8260 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8261 
8262 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8263 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8264 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8265 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8266 	return hwrm_req_send(bp, req);
8267 }
8268 
8269 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8270 {
8271 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8272 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8273 	struct bnxt_coal coal;
8274 	int rc;
8275 
8276 	/* Tick values in micro seconds.
8277 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8278 	 */
8279 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8280 
8281 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8282 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8283 
8284 	if (!bnapi->rx_ring)
8285 		return -ENODEV;
8286 
8287 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8288 	if (rc)
8289 		return rc;
8290 
8291 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8292 
8293 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8294 
8295 	return hwrm_req_send(bp, req_rx);
8296 }
8297 
8298 static int
8299 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8300 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8301 {
8302 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8303 
8304 	req->ring_id = cpu_to_le16(ring_id);
8305 	return hwrm_req_send(bp, req);
8306 }
8307 
8308 static int
8309 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8310 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8311 {
8312 	struct bnxt_tx_ring_info *txr;
8313 	int i, rc;
8314 
8315 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8316 		u16 ring_id;
8317 
8318 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8319 		req->ring_id = cpu_to_le16(ring_id);
8320 		rc = hwrm_req_send(bp, req);
8321 		if (rc)
8322 			return rc;
8323 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8324 			return 0;
8325 	}
8326 	return 0;
8327 }
8328 
8329 int bnxt_hwrm_set_coal(struct bnxt *bp)
8330 {
8331 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8332 	int i, rc;
8333 
8334 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8335 	if (rc)
8336 		return rc;
8337 
8338 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8339 	if (rc) {
8340 		hwrm_req_drop(bp, req_rx);
8341 		return rc;
8342 	}
8343 
8344 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8345 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8346 
8347 	hwrm_req_hold(bp, req_rx);
8348 	hwrm_req_hold(bp, req_tx);
8349 	for (i = 0; i < bp->cp_nr_rings; i++) {
8350 		struct bnxt_napi *bnapi = bp->bnapi[i];
8351 		struct bnxt_coal *hw_coal;
8352 
8353 		if (!bnapi->rx_ring)
8354 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8355 		else
8356 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8357 		if (rc)
8358 			break;
8359 
8360 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8361 			continue;
8362 
8363 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8364 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8365 			if (rc)
8366 				break;
8367 		}
8368 		if (bnapi->rx_ring)
8369 			hw_coal = &bp->rx_coal;
8370 		else
8371 			hw_coal = &bp->tx_coal;
8372 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8373 	}
8374 	hwrm_req_drop(bp, req_rx);
8375 	hwrm_req_drop(bp, req_tx);
8376 	return rc;
8377 }
8378 
8379 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8380 {
8381 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8382 	struct hwrm_stat_ctx_free_input *req;
8383 	int i;
8384 
8385 	if (!bp->bnapi)
8386 		return;
8387 
8388 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8389 		return;
8390 
8391 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8392 		return;
8393 	if (BNXT_FW_MAJ(bp) <= 20) {
8394 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8395 			hwrm_req_drop(bp, req);
8396 			return;
8397 		}
8398 		hwrm_req_hold(bp, req0);
8399 	}
8400 	hwrm_req_hold(bp, req);
8401 	for (i = 0; i < bp->cp_nr_rings; i++) {
8402 		struct bnxt_napi *bnapi = bp->bnapi[i];
8403 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8404 
8405 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8406 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8407 			if (req0) {
8408 				req0->stat_ctx_id = req->stat_ctx_id;
8409 				hwrm_req_send(bp, req0);
8410 			}
8411 			hwrm_req_send(bp, req);
8412 
8413 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8414 		}
8415 	}
8416 	hwrm_req_drop(bp, req);
8417 	if (req0)
8418 		hwrm_req_drop(bp, req0);
8419 }
8420 
8421 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8422 {
8423 	struct hwrm_stat_ctx_alloc_output *resp;
8424 	struct hwrm_stat_ctx_alloc_input *req;
8425 	int rc, i;
8426 
8427 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8428 		return 0;
8429 
8430 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8431 	if (rc)
8432 		return rc;
8433 
8434 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8435 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8436 
8437 	resp = hwrm_req_hold(bp, req);
8438 	for (i = 0; i < bp->cp_nr_rings; i++) {
8439 		struct bnxt_napi *bnapi = bp->bnapi[i];
8440 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8441 
8442 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8443 
8444 		rc = hwrm_req_send(bp, req);
8445 		if (rc)
8446 			break;
8447 
8448 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8449 
8450 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8451 	}
8452 	hwrm_req_drop(bp, req);
8453 	return rc;
8454 }
8455 
8456 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8457 {
8458 	struct hwrm_func_qcfg_output *resp;
8459 	struct hwrm_func_qcfg_input *req;
8460 	u16 flags;
8461 	int rc;
8462 
8463 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8464 	if (rc)
8465 		return rc;
8466 
8467 	req->fid = cpu_to_le16(0xffff);
8468 	resp = hwrm_req_hold(bp, req);
8469 	rc = hwrm_req_send(bp, req);
8470 	if (rc)
8471 		goto func_qcfg_exit;
8472 
8473 	flags = le16_to_cpu(resp->flags);
8474 #ifdef CONFIG_BNXT_SRIOV
8475 	if (BNXT_VF(bp)) {
8476 		struct bnxt_vf_info *vf = &bp->vf;
8477 
8478 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8479 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8480 			vf->flags |= BNXT_VF_TRUST;
8481 		else
8482 			vf->flags &= ~BNXT_VF_TRUST;
8483 	} else {
8484 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8485 	}
8486 #endif
8487 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8488 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8489 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8490 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8491 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8492 	}
8493 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8494 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8495 
8496 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8497 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8498 
8499 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8500 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8501 
8502 	switch (resp->port_partition_type) {
8503 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8504 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8505 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8506 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8507 		bp->port_partition_type = resp->port_partition_type;
8508 		break;
8509 	}
8510 	if (bp->hwrm_spec_code < 0x10707 ||
8511 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8512 		bp->br_mode = BRIDGE_MODE_VEB;
8513 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8514 		bp->br_mode = BRIDGE_MODE_VEPA;
8515 	else
8516 		bp->br_mode = BRIDGE_MODE_UNDEF;
8517 
8518 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8519 	if (!bp->max_mtu)
8520 		bp->max_mtu = BNXT_MAX_MTU;
8521 
8522 	if (bp->db_size)
8523 		goto func_qcfg_exit;
8524 
8525 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8526 	if (BNXT_CHIP_P5(bp)) {
8527 		if (BNXT_PF(bp))
8528 			bp->db_offset = DB_PF_OFFSET_P5;
8529 		else
8530 			bp->db_offset = DB_VF_OFFSET_P5;
8531 	}
8532 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8533 				 1024);
8534 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8535 	    bp->db_size <= bp->db_offset)
8536 		bp->db_size = pci_resource_len(bp->pdev, 2);
8537 
8538 func_qcfg_exit:
8539 	hwrm_req_drop(bp, req);
8540 	return rc;
8541 }
8542 
8543 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8544 				      u8 init_val, u8 init_offset,
8545 				      bool init_mask_set)
8546 {
8547 	ctxm->init_value = init_val;
8548 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8549 	if (init_mask_set)
8550 		ctxm->init_offset = init_offset * 4;
8551 	else
8552 		ctxm->init_value = 0;
8553 }
8554 
8555 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8556 {
8557 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8558 	u16 type;
8559 
8560 	for (type = 0; type < ctx_max; type++) {
8561 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8562 		int n = 1;
8563 
8564 		if (!ctxm->max_entries || ctxm->pg_info)
8565 			continue;
8566 
8567 		if (ctxm->instance_bmap)
8568 			n = hweight32(ctxm->instance_bmap);
8569 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8570 		if (!ctxm->pg_info)
8571 			return -ENOMEM;
8572 	}
8573 	return 0;
8574 }
8575 
8576 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8577 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8578 
8579 #define BNXT_CTX_INIT_VALID(flags)	\
8580 	(!!((flags) &			\
8581 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8582 
8583 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8584 {
8585 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8586 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8587 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8588 	u16 type;
8589 	int rc;
8590 
8591 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8592 	if (rc)
8593 		return rc;
8594 
8595 	if (!ctx) {
8596 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8597 		if (!ctx)
8598 			return -ENOMEM;
8599 		bp->ctx = ctx;
8600 	}
8601 
8602 	resp = hwrm_req_hold(bp, req);
8603 
8604 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8605 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8606 		u8 init_val, init_off, i;
8607 		u32 max_entries;
8608 		u16 entry_size;
8609 		__le32 *p;
8610 		u32 flags;
8611 
8612 		req->type = cpu_to_le16(type);
8613 		rc = hwrm_req_send(bp, req);
8614 		if (rc)
8615 			goto ctx_done;
8616 		flags = le32_to_cpu(resp->flags);
8617 		type = le16_to_cpu(resp->next_valid_type);
8618 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8619 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8620 			continue;
8621 		}
8622 		entry_size = le16_to_cpu(resp->entry_size);
8623 		max_entries = le32_to_cpu(resp->max_num_entries);
8624 		if (ctxm->mem_valid) {
8625 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8626 			    ctxm->entry_size != entry_size ||
8627 			    ctxm->max_entries != max_entries)
8628 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8629 			else
8630 				continue;
8631 		}
8632 		ctxm->type = le16_to_cpu(resp->type);
8633 		ctxm->entry_size = entry_size;
8634 		ctxm->flags = flags;
8635 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8636 		ctxm->entry_multiple = resp->entry_multiple;
8637 		ctxm->max_entries = max_entries;
8638 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8639 		init_val = resp->ctx_init_value;
8640 		init_off = resp->ctx_init_offset;
8641 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8642 					  BNXT_CTX_INIT_VALID(flags));
8643 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8644 					      BNXT_MAX_SPLIT_ENTRY);
8645 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8646 		     i++, p++)
8647 			ctxm->split[i] = le32_to_cpu(*p);
8648 	}
8649 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8650 
8651 ctx_done:
8652 	hwrm_req_drop(bp, req);
8653 	return rc;
8654 }
8655 
8656 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8657 {
8658 	struct hwrm_func_backing_store_qcaps_output *resp;
8659 	struct hwrm_func_backing_store_qcaps_input *req;
8660 	int rc;
8661 
8662 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8663 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8664 		return 0;
8665 
8666 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8667 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8668 
8669 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8670 	if (rc)
8671 		return rc;
8672 
8673 	resp = hwrm_req_hold(bp, req);
8674 	rc = hwrm_req_send_silent(bp, req);
8675 	if (!rc) {
8676 		struct bnxt_ctx_mem_type *ctxm;
8677 		struct bnxt_ctx_mem_info *ctx;
8678 		u8 init_val, init_idx = 0;
8679 		u16 init_mask;
8680 
8681 		ctx = bp->ctx;
8682 		if (!ctx) {
8683 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8684 			if (!ctx) {
8685 				rc = -ENOMEM;
8686 				goto ctx_err;
8687 			}
8688 			bp->ctx = ctx;
8689 		}
8690 		init_val = resp->ctx_kind_initializer;
8691 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8692 
8693 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8694 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8695 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8696 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8697 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8698 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8699 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8700 					  (init_mask & (1 << init_idx++)) != 0);
8701 
8702 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8703 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8704 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8705 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8706 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8707 					  (init_mask & (1 << init_idx++)) != 0);
8708 
8709 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8710 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8711 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8712 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8713 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8714 					  (init_mask & (1 << init_idx++)) != 0);
8715 
8716 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8717 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8718 		ctxm->max_entries = ctxm->vnic_entries +
8719 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8720 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8721 		bnxt_init_ctx_initializer(ctxm, init_val,
8722 					  resp->vnic_init_offset,
8723 					  (init_mask & (1 << init_idx++)) != 0);
8724 
8725 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8726 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8727 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8728 		bnxt_init_ctx_initializer(ctxm, init_val,
8729 					  resp->stat_init_offset,
8730 					  (init_mask & (1 << init_idx++)) != 0);
8731 
8732 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8733 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8734 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8735 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8736 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8737 		if (!ctxm->entry_multiple)
8738 			ctxm->entry_multiple = 1;
8739 
8740 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8741 
8742 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8743 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8744 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8745 		ctxm->mrav_num_entries_units =
8746 			le16_to_cpu(resp->mrav_num_entries_units);
8747 		bnxt_init_ctx_initializer(ctxm, init_val,
8748 					  resp->mrav_init_offset,
8749 					  (init_mask & (1 << init_idx++)) != 0);
8750 
8751 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8752 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8753 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8754 
8755 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8756 		if (!ctx->tqm_fp_rings_count)
8757 			ctx->tqm_fp_rings_count = bp->max_q;
8758 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8759 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8760 
8761 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8762 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8763 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8764 
8765 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8766 	} else {
8767 		rc = 0;
8768 	}
8769 ctx_err:
8770 	hwrm_req_drop(bp, req);
8771 	return rc;
8772 }
8773 
8774 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8775 				  __le64 *pg_dir)
8776 {
8777 	if (!rmem->nr_pages)
8778 		return;
8779 
8780 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8781 	if (rmem->depth >= 1) {
8782 		if (rmem->depth == 2)
8783 			*pg_attr |= 2;
8784 		else
8785 			*pg_attr |= 1;
8786 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8787 	} else {
8788 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8789 	}
8790 }
8791 
8792 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8793 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8794 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8795 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8796 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8797 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8798 
8799 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8800 {
8801 	struct hwrm_func_backing_store_cfg_input *req;
8802 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8803 	struct bnxt_ctx_pg_info *ctx_pg;
8804 	struct bnxt_ctx_mem_type *ctxm;
8805 	void **__req = (void **)&req;
8806 	u32 req_len = sizeof(*req);
8807 	__le32 *num_entries;
8808 	__le64 *pg_dir;
8809 	u32 flags = 0;
8810 	u8 *pg_attr;
8811 	u32 ena;
8812 	int rc;
8813 	int i;
8814 
8815 	if (!ctx)
8816 		return 0;
8817 
8818 	if (req_len > bp->hwrm_max_ext_req_len)
8819 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8820 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8821 	if (rc)
8822 		return rc;
8823 
8824 	req->enables = cpu_to_le32(enables);
8825 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8826 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8827 		ctx_pg = ctxm->pg_info;
8828 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8829 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8830 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8831 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8832 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8833 				      &req->qpc_pg_size_qpc_lvl,
8834 				      &req->qpc_page_dir);
8835 
8836 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8837 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8838 	}
8839 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8840 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8841 		ctx_pg = ctxm->pg_info;
8842 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8843 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8844 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8845 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8846 				      &req->srq_pg_size_srq_lvl,
8847 				      &req->srq_page_dir);
8848 	}
8849 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8850 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8851 		ctx_pg = ctxm->pg_info;
8852 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8853 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8854 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8855 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8856 				      &req->cq_pg_size_cq_lvl,
8857 				      &req->cq_page_dir);
8858 	}
8859 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8860 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8861 		ctx_pg = ctxm->pg_info;
8862 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8863 		req->vnic_num_ring_table_entries =
8864 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8865 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8866 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8867 				      &req->vnic_pg_size_vnic_lvl,
8868 				      &req->vnic_page_dir);
8869 	}
8870 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8871 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8872 		ctx_pg = ctxm->pg_info;
8873 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8874 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8875 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8876 				      &req->stat_pg_size_stat_lvl,
8877 				      &req->stat_page_dir);
8878 	}
8879 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8880 		u32 units;
8881 
8882 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8883 		ctx_pg = ctxm->pg_info;
8884 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8885 		units = ctxm->mrav_num_entries_units;
8886 		if (units) {
8887 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8888 			u32 entries;
8889 
8890 			num_mr = ctx_pg->entries - num_ah;
8891 			entries = ((num_mr / units) << 16) | (num_ah / units);
8892 			req->mrav_num_entries = cpu_to_le32(entries);
8893 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8894 		}
8895 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8896 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8897 				      &req->mrav_pg_size_mrav_lvl,
8898 				      &req->mrav_page_dir);
8899 	}
8900 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8901 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8902 		ctx_pg = ctxm->pg_info;
8903 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8904 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8905 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8906 				      &req->tim_pg_size_tim_lvl,
8907 				      &req->tim_page_dir);
8908 	}
8909 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8910 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8911 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8912 	     pg_dir = &req->tqm_sp_page_dir,
8913 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8914 	     ctx_pg = ctxm->pg_info;
8915 	     i < BNXT_MAX_TQM_RINGS;
8916 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8917 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8918 		if (!(enables & ena))
8919 			continue;
8920 
8921 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8922 		*num_entries = cpu_to_le32(ctx_pg->entries);
8923 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8924 	}
8925 	req->flags = cpu_to_le32(flags);
8926 	return hwrm_req_send(bp, req);
8927 }
8928 
8929 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8930 				  struct bnxt_ctx_pg_info *ctx_pg)
8931 {
8932 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8933 
8934 	rmem->page_size = BNXT_PAGE_SIZE;
8935 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8936 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8937 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8938 	if (rmem->depth >= 1)
8939 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8940 	return bnxt_alloc_ring(bp, rmem);
8941 }
8942 
8943 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8944 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8945 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8946 {
8947 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8948 	int rc;
8949 
8950 	if (!mem_size)
8951 		return -EINVAL;
8952 
8953 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8954 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8955 		ctx_pg->nr_pages = 0;
8956 		return -EINVAL;
8957 	}
8958 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8959 		int nr_tbls, i;
8960 
8961 		rmem->depth = 2;
8962 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8963 					     GFP_KERNEL);
8964 		if (!ctx_pg->ctx_pg_tbl)
8965 			return -ENOMEM;
8966 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8967 		rmem->nr_pages = nr_tbls;
8968 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8969 		if (rc)
8970 			return rc;
8971 		for (i = 0; i < nr_tbls; i++) {
8972 			struct bnxt_ctx_pg_info *pg_tbl;
8973 
8974 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8975 			if (!pg_tbl)
8976 				return -ENOMEM;
8977 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8978 			rmem = &pg_tbl->ring_mem;
8979 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8980 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8981 			rmem->depth = 1;
8982 			rmem->nr_pages = MAX_CTX_PAGES;
8983 			rmem->ctx_mem = ctxm;
8984 			if (i == (nr_tbls - 1)) {
8985 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8986 
8987 				if (rem)
8988 					rmem->nr_pages = rem;
8989 			}
8990 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8991 			if (rc)
8992 				break;
8993 		}
8994 	} else {
8995 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8996 		if (rmem->nr_pages > 1 || depth)
8997 			rmem->depth = 1;
8998 		rmem->ctx_mem = ctxm;
8999 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9000 	}
9001 	return rc;
9002 }
9003 
9004 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
9005 				    struct bnxt_ctx_pg_info *ctx_pg,
9006 				    void *buf, size_t offset, size_t head,
9007 				    size_t tail)
9008 {
9009 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9010 	size_t nr_pages = ctx_pg->nr_pages;
9011 	int page_size = rmem->page_size;
9012 	size_t len = 0, total_len = 0;
9013 	u16 depth = rmem->depth;
9014 
9015 	tail %= nr_pages * page_size;
9016 	do {
9017 		if (depth > 1) {
9018 			int i = head / (page_size * MAX_CTX_PAGES);
9019 			struct bnxt_ctx_pg_info *pg_tbl;
9020 
9021 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9022 			rmem = &pg_tbl->ring_mem;
9023 		}
9024 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
9025 		head += len;
9026 		offset += len;
9027 		total_len += len;
9028 		if (head >= nr_pages * page_size)
9029 			head = 0;
9030 	} while (head != tail);
9031 	return total_len;
9032 }
9033 
9034 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
9035 				  struct bnxt_ctx_pg_info *ctx_pg)
9036 {
9037 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9038 
9039 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
9040 	    ctx_pg->ctx_pg_tbl) {
9041 		int i, nr_tbls = rmem->nr_pages;
9042 
9043 		for (i = 0; i < nr_tbls; i++) {
9044 			struct bnxt_ctx_pg_info *pg_tbl;
9045 			struct bnxt_ring_mem_info *rmem2;
9046 
9047 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9048 			if (!pg_tbl)
9049 				continue;
9050 			rmem2 = &pg_tbl->ring_mem;
9051 			bnxt_free_ring(bp, rmem2);
9052 			ctx_pg->ctx_pg_arr[i] = NULL;
9053 			kfree(pg_tbl);
9054 			ctx_pg->ctx_pg_tbl[i] = NULL;
9055 		}
9056 		kfree(ctx_pg->ctx_pg_tbl);
9057 		ctx_pg->ctx_pg_tbl = NULL;
9058 	}
9059 	bnxt_free_ring(bp, rmem);
9060 	ctx_pg->nr_pages = 0;
9061 }
9062 
9063 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
9064 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
9065 				   u8 pg_lvl)
9066 {
9067 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9068 	int i, rc = 0, n = 1;
9069 	u32 mem_size;
9070 
9071 	if (!ctxm->entry_size || !ctx_pg)
9072 		return -EINVAL;
9073 	if (ctxm->instance_bmap)
9074 		n = hweight32(ctxm->instance_bmap);
9075 	if (ctxm->entry_multiple)
9076 		entries = roundup(entries, ctxm->entry_multiple);
9077 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9078 	mem_size = entries * ctxm->entry_size;
9079 	for (i = 0; i < n && !rc; i++) {
9080 		ctx_pg[i].entries = entries;
9081 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9082 					    ctxm->init_value ? ctxm : NULL);
9083 	}
9084 	if (!rc)
9085 		ctxm->mem_valid = 1;
9086 	return rc;
9087 }
9088 
9089 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9090 					       struct bnxt_ctx_mem_type *ctxm,
9091 					       bool last)
9092 {
9093 	struct hwrm_func_backing_store_cfg_v2_input *req;
9094 	u32 instance_bmap = ctxm->instance_bmap;
9095 	int i, j, rc = 0, n = 1;
9096 	__le32 *p;
9097 
9098 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9099 		return 0;
9100 
9101 	if (instance_bmap)
9102 		n = hweight32(ctxm->instance_bmap);
9103 	else
9104 		instance_bmap = 1;
9105 
9106 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9107 	if (rc)
9108 		return rc;
9109 	hwrm_req_hold(bp, req);
9110 	req->type = cpu_to_le16(ctxm->type);
9111 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9112 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9113 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9114 		struct bnxt_bs_trace_info *bs_trace;
9115 		u32 enables;
9116 
9117 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9118 		req->enables = cpu_to_le32(enables);
9119 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9120 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9121 	}
9122 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9123 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9124 		p[i] = cpu_to_le32(ctxm->split[i]);
9125 	for (i = 0, j = 0; j < n && !rc; i++) {
9126 		struct bnxt_ctx_pg_info *ctx_pg;
9127 
9128 		if (!(instance_bmap & (1 << i)))
9129 			continue;
9130 		req->instance = cpu_to_le16(i);
9131 		ctx_pg = &ctxm->pg_info[j++];
9132 		if (!ctx_pg->entries)
9133 			continue;
9134 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9135 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9136 				      &req->page_size_pbl_level,
9137 				      &req->page_dir);
9138 		if (last && j == n)
9139 			req->flags =
9140 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9141 		rc = hwrm_req_send(bp, req);
9142 	}
9143 	hwrm_req_drop(bp, req);
9144 	return rc;
9145 }
9146 
9147 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
9148 {
9149 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9150 	struct bnxt_ctx_mem_type *ctxm;
9151 	u16 last_type = BNXT_CTX_INV;
9152 	int rc = 0;
9153 	u16 type;
9154 
9155 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) {
9156 		ctxm = &ctx->ctx_arr[type];
9157 		if (!bnxt_bs_trace_avail(bp, type))
9158 			continue;
9159 		if (!ctxm->mem_valid) {
9160 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9161 						     ctxm->max_entries, 1);
9162 			if (rc) {
9163 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9164 					    type);
9165 				continue;
9166 			}
9167 			bnxt_bs_trace_init(bp, ctxm);
9168 		}
9169 		last_type = type;
9170 	}
9171 
9172 	if (last_type == BNXT_CTX_INV) {
9173 		if (!ena)
9174 			return 0;
9175 		else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
9176 			last_type = BNXT_CTX_MAX - 1;
9177 		else
9178 			last_type = BNXT_CTX_L2_MAX - 1;
9179 	}
9180 	ctx->ctx_arr[last_type].last = 1;
9181 
9182 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9183 		ctxm = &ctx->ctx_arr[type];
9184 
9185 		if (!ctxm->mem_valid)
9186 			continue;
9187 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9188 		if (rc)
9189 			return rc;
9190 	}
9191 	return 0;
9192 }
9193 
9194 /**
9195  * __bnxt_copy_ctx_mem - copy host context memory
9196  * @bp: The driver context
9197  * @ctxm: The pointer to the context memory type
9198  * @buf: The destination buffer or NULL to just obtain the length
9199  * @offset: The buffer offset to copy the data to
9200  * @head: The head offset of context memory to copy from
9201  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9202  *
9203  * This function is called for debugging purposes to dump the host context
9204  * used by the chip.
9205  *
9206  * Return: Length of memory copied
9207  */
9208 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9209 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9210 				  size_t offset, size_t head, size_t tail)
9211 {
9212 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9213 	size_t len = 0, total_len = 0;
9214 	int i, n = 1;
9215 
9216 	if (!ctx_pg)
9217 		return 0;
9218 
9219 	if (ctxm->instance_bmap)
9220 		n = hweight32(ctxm->instance_bmap);
9221 	for (i = 0; i < n; i++) {
9222 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9223 					    tail);
9224 		offset += len;
9225 		total_len += len;
9226 	}
9227 	return total_len;
9228 }
9229 
9230 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9231 			 void *buf, size_t offset)
9232 {
9233 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9234 
9235 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9236 }
9237 
9238 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9239 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9240 {
9241 	struct bnxt_ctx_pg_info *ctx_pg;
9242 	int i, n = 1;
9243 
9244 	ctxm->last = 0;
9245 
9246 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9247 		return;
9248 
9249 	ctx_pg = ctxm->pg_info;
9250 	if (ctx_pg) {
9251 		if (ctxm->instance_bmap)
9252 			n = hweight32(ctxm->instance_bmap);
9253 		for (i = 0; i < n; i++)
9254 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9255 
9256 		kfree(ctx_pg);
9257 		ctxm->pg_info = NULL;
9258 		ctxm->mem_valid = 0;
9259 	}
9260 	memset(ctxm, 0, sizeof(*ctxm));
9261 }
9262 
9263 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9264 {
9265 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9266 	u16 type;
9267 
9268 	if (!ctx)
9269 		return;
9270 
9271 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9272 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9273 
9274 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9275 	if (force) {
9276 		kfree(ctx);
9277 		bp->ctx = NULL;
9278 	}
9279 }
9280 
9281 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9282 {
9283 	struct bnxt_ctx_mem_type *ctxm;
9284 	struct bnxt_ctx_mem_info *ctx;
9285 	u32 l2_qps, qp1_qps, max_qps;
9286 	u32 ena, entries_sp, entries;
9287 	u32 srqs, max_srqs, min;
9288 	u32 num_mr, num_ah;
9289 	u32 extra_srqs = 0;
9290 	u32 extra_qps = 0;
9291 	u32 fast_qpmd_qps;
9292 	u8 pg_lvl = 1;
9293 	int i, rc;
9294 
9295 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9296 	if (rc) {
9297 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9298 			   rc);
9299 		return rc;
9300 	}
9301 	ctx = bp->ctx;
9302 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9303 		return 0;
9304 
9305 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9306 	l2_qps = ctxm->qp_l2_entries;
9307 	qp1_qps = ctxm->qp_qp1_entries;
9308 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9309 	max_qps = ctxm->max_entries;
9310 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9311 	srqs = ctxm->srq_l2_entries;
9312 	max_srqs = ctxm->max_entries;
9313 	ena = 0;
9314 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9315 		pg_lvl = 2;
9316 		if (BNXT_SW_RES_LMT(bp)) {
9317 			extra_qps = max_qps - l2_qps - qp1_qps;
9318 			extra_srqs = max_srqs - srqs;
9319 		} else {
9320 			extra_qps = min_t(u32, 65536,
9321 					  max_qps - l2_qps - qp1_qps);
9322 			/* allocate extra qps if fw supports RoCE fast qp
9323 			 * destroy feature
9324 			 */
9325 			extra_qps += fast_qpmd_qps;
9326 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9327 		}
9328 		if (fast_qpmd_qps)
9329 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9330 	}
9331 
9332 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9333 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9334 				     pg_lvl);
9335 	if (rc)
9336 		return rc;
9337 
9338 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9339 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9340 	if (rc)
9341 		return rc;
9342 
9343 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9344 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9345 				     extra_qps * 2, pg_lvl);
9346 	if (rc)
9347 		return rc;
9348 
9349 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9350 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9351 	if (rc)
9352 		return rc;
9353 
9354 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9355 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9356 	if (rc)
9357 		return rc;
9358 
9359 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9360 		goto skip_rdma;
9361 
9362 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9363 	if (BNXT_SW_RES_LMT(bp) &&
9364 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9365 		num_ah = ctxm->mrav_av_entries;
9366 		num_mr = ctxm->max_entries - num_ah;
9367 	} else {
9368 		/* 128K extra is needed to accommodate static AH context
9369 		 * allocation by f/w.
9370 		 */
9371 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9372 		num_ah = min_t(u32, num_mr, 1024 * 128);
9373 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9374 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9375 			ctxm->mrav_av_entries = num_ah;
9376 	}
9377 
9378 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9379 	if (rc)
9380 		return rc;
9381 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9382 
9383 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9384 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9385 	if (rc)
9386 		return rc;
9387 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9388 
9389 skip_rdma:
9390 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9391 	min = ctxm->min_entries;
9392 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9393 		     2 * (extra_qps + qp1_qps) + min;
9394 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9395 	if (rc)
9396 		return rc;
9397 
9398 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9399 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9400 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9401 	if (rc)
9402 		return rc;
9403 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9404 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9405 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9406 
9407 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9408 		rc = bnxt_backing_store_cfg_v2(bp, ena);
9409 	else
9410 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9411 	if (rc) {
9412 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9413 			   rc);
9414 		return rc;
9415 	}
9416 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9417 	return 0;
9418 }
9419 
9420 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9421 {
9422 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9423 	u16 page_attr;
9424 	int rc;
9425 
9426 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9427 		return 0;
9428 
9429 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9430 	if (rc)
9431 		return rc;
9432 
9433 	if (BNXT_PAGE_SIZE == 0x2000)
9434 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9435 	else if (BNXT_PAGE_SIZE == 0x10000)
9436 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9437 	else
9438 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9439 	req->pg_size_lvl = cpu_to_le16(page_attr |
9440 				       bp->fw_crash_mem->ring_mem.depth);
9441 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9442 	req->size = cpu_to_le32(bp->fw_crash_len);
9443 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9444 	return hwrm_req_send(bp, req);
9445 }
9446 
9447 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9448 {
9449 	if (bp->fw_crash_mem) {
9450 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9451 		kfree(bp->fw_crash_mem);
9452 		bp->fw_crash_mem = NULL;
9453 	}
9454 }
9455 
9456 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9457 {
9458 	u32 mem_size = 0;
9459 	int rc;
9460 
9461 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9462 		return 0;
9463 
9464 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9465 	if (rc)
9466 		return rc;
9467 
9468 	mem_size = round_up(mem_size, 4);
9469 
9470 	/* keep and use the existing pages */
9471 	if (bp->fw_crash_mem &&
9472 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9473 		goto alloc_done;
9474 
9475 	if (bp->fw_crash_mem)
9476 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9477 	else
9478 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
9479 					   GFP_KERNEL);
9480 	if (!bp->fw_crash_mem)
9481 		return -ENOMEM;
9482 
9483 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9484 	if (rc) {
9485 		bnxt_free_crash_dump_mem(bp);
9486 		return rc;
9487 	}
9488 
9489 alloc_done:
9490 	bp->fw_crash_len = mem_size;
9491 	return 0;
9492 }
9493 
9494 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9495 {
9496 	struct hwrm_func_resource_qcaps_output *resp;
9497 	struct hwrm_func_resource_qcaps_input *req;
9498 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9499 	int rc;
9500 
9501 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9502 	if (rc)
9503 		return rc;
9504 
9505 	req->fid = cpu_to_le16(0xffff);
9506 	resp = hwrm_req_hold(bp, req);
9507 	rc = hwrm_req_send_silent(bp, req);
9508 	if (rc)
9509 		goto hwrm_func_resc_qcaps_exit;
9510 
9511 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9512 	if (!all)
9513 		goto hwrm_func_resc_qcaps_exit;
9514 
9515 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9516 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9517 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9518 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9519 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9520 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9521 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9522 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9523 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9524 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9525 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9526 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9527 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9528 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9529 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9530 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9531 
9532 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9533 		u16 max_msix = le16_to_cpu(resp->max_msix);
9534 
9535 		hw_resc->max_nqs = max_msix;
9536 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9537 	}
9538 
9539 	if (BNXT_PF(bp)) {
9540 		struct bnxt_pf_info *pf = &bp->pf;
9541 
9542 		pf->vf_resv_strategy =
9543 			le16_to_cpu(resp->vf_reservation_strategy);
9544 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9545 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9546 	}
9547 hwrm_func_resc_qcaps_exit:
9548 	hwrm_req_drop(bp, req);
9549 	return rc;
9550 }
9551 
9552 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9553 {
9554 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9555 	struct hwrm_port_mac_ptp_qcfg_input *req;
9556 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9557 	u8 flags;
9558 	int rc;
9559 
9560 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9561 		rc = -ENODEV;
9562 		goto no_ptp;
9563 	}
9564 
9565 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9566 	if (rc)
9567 		goto no_ptp;
9568 
9569 	req->port_id = cpu_to_le16(bp->pf.port_id);
9570 	resp = hwrm_req_hold(bp, req);
9571 	rc = hwrm_req_send(bp, req);
9572 	if (rc)
9573 		goto exit;
9574 
9575 	flags = resp->flags;
9576 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9577 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9578 		rc = -ENODEV;
9579 		goto exit;
9580 	}
9581 	if (!ptp) {
9582 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9583 		if (!ptp) {
9584 			rc = -ENOMEM;
9585 			goto exit;
9586 		}
9587 		ptp->bp = bp;
9588 		bp->ptp_cfg = ptp;
9589 	}
9590 
9591 	if (flags &
9592 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9593 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9594 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9595 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9596 	} else if (BNXT_CHIP_P5(bp)) {
9597 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9598 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9599 	} else {
9600 		rc = -ENODEV;
9601 		goto exit;
9602 	}
9603 	ptp->rtc_configured =
9604 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9605 	rc = bnxt_ptp_init(bp);
9606 	if (rc)
9607 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9608 exit:
9609 	hwrm_req_drop(bp, req);
9610 	if (!rc)
9611 		return 0;
9612 
9613 no_ptp:
9614 	bnxt_ptp_clear(bp);
9615 	kfree(ptp);
9616 	bp->ptp_cfg = NULL;
9617 	return rc;
9618 }
9619 
9620 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9621 {
9622 	struct hwrm_func_qcaps_output *resp;
9623 	struct hwrm_func_qcaps_input *req;
9624 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9625 	u32 flags, flags_ext, flags_ext2;
9626 	int rc;
9627 
9628 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9629 	if (rc)
9630 		return rc;
9631 
9632 	req->fid = cpu_to_le16(0xffff);
9633 	resp = hwrm_req_hold(bp, req);
9634 	rc = hwrm_req_send(bp, req);
9635 	if (rc)
9636 		goto hwrm_func_qcaps_exit;
9637 
9638 	flags = le32_to_cpu(resp->flags);
9639 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9640 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9641 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9642 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9643 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9644 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9645 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9646 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9647 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9648 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9649 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9650 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9651 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9652 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9653 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9654 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9655 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9656 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9657 
9658 	flags_ext = le32_to_cpu(resp->flags_ext);
9659 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9660 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9661 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9662 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9663 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9664 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9665 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9666 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9667 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9668 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9669 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9670 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9671 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9672 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9673 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9674 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9675 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9676 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9677 
9678 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9679 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9680 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9681 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9682 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9683 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9684 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9685 	if (flags_ext2 &
9686 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9687 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9688 	if (BNXT_PF(bp) &&
9689 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9690 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9691 
9692 	bp->tx_push_thresh = 0;
9693 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9694 	    BNXT_FW_MAJ(bp) > 217)
9695 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9696 
9697 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9698 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9699 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9700 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9701 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9702 	if (!hw_resc->max_hw_ring_grps)
9703 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9704 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9705 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9706 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9707 
9708 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9709 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9710 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9711 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9712 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9713 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9714 
9715 	if (BNXT_PF(bp)) {
9716 		struct bnxt_pf_info *pf = &bp->pf;
9717 
9718 		pf->fw_fid = le16_to_cpu(resp->fid);
9719 		pf->port_id = le16_to_cpu(resp->port_id);
9720 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9721 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9722 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9723 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9724 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9725 			bp->flags |= BNXT_FLAG_WOL_CAP;
9726 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9727 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9728 		} else {
9729 			bnxt_ptp_clear(bp);
9730 			kfree(bp->ptp_cfg);
9731 			bp->ptp_cfg = NULL;
9732 		}
9733 	} else {
9734 #ifdef CONFIG_BNXT_SRIOV
9735 		struct bnxt_vf_info *vf = &bp->vf;
9736 
9737 		vf->fw_fid = le16_to_cpu(resp->fid);
9738 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9739 #endif
9740 	}
9741 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9742 
9743 hwrm_func_qcaps_exit:
9744 	hwrm_req_drop(bp, req);
9745 	return rc;
9746 }
9747 
9748 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9749 {
9750 	struct hwrm_dbg_qcaps_output *resp;
9751 	struct hwrm_dbg_qcaps_input *req;
9752 	int rc;
9753 
9754 	bp->fw_dbg_cap = 0;
9755 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9756 		return;
9757 
9758 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9759 	if (rc)
9760 		return;
9761 
9762 	req->fid = cpu_to_le16(0xffff);
9763 	resp = hwrm_req_hold(bp, req);
9764 	rc = hwrm_req_send(bp, req);
9765 	if (rc)
9766 		goto hwrm_dbg_qcaps_exit;
9767 
9768 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9769 
9770 hwrm_dbg_qcaps_exit:
9771 	hwrm_req_drop(bp, req);
9772 }
9773 
9774 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9775 
9776 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9777 {
9778 	int rc;
9779 
9780 	rc = __bnxt_hwrm_func_qcaps(bp);
9781 	if (rc)
9782 		return rc;
9783 
9784 	bnxt_hwrm_dbg_qcaps(bp);
9785 
9786 	rc = bnxt_hwrm_queue_qportcfg(bp);
9787 	if (rc) {
9788 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9789 		return rc;
9790 	}
9791 	if (bp->hwrm_spec_code >= 0x10803) {
9792 		rc = bnxt_alloc_ctx_mem(bp);
9793 		if (rc)
9794 			return rc;
9795 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9796 		if (!rc)
9797 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9798 	}
9799 	return 0;
9800 }
9801 
9802 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9803 {
9804 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9805 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9806 	u32 flags;
9807 	int rc;
9808 
9809 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9810 		return 0;
9811 
9812 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9813 	if (rc)
9814 		return rc;
9815 
9816 	resp = hwrm_req_hold(bp, req);
9817 	rc = hwrm_req_send(bp, req);
9818 	if (rc)
9819 		goto hwrm_cfa_adv_qcaps_exit;
9820 
9821 	flags = le32_to_cpu(resp->flags);
9822 	if (flags &
9823 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9824 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9825 
9826 	if (flags &
9827 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9828 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9829 
9830 	if (flags &
9831 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9832 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9833 
9834 hwrm_cfa_adv_qcaps_exit:
9835 	hwrm_req_drop(bp, req);
9836 	return rc;
9837 }
9838 
9839 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9840 {
9841 	if (bp->fw_health)
9842 		return 0;
9843 
9844 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9845 	if (!bp->fw_health)
9846 		return -ENOMEM;
9847 
9848 	mutex_init(&bp->fw_health->lock);
9849 	return 0;
9850 }
9851 
9852 static int bnxt_alloc_fw_health(struct bnxt *bp)
9853 {
9854 	int rc;
9855 
9856 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9857 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9858 		return 0;
9859 
9860 	rc = __bnxt_alloc_fw_health(bp);
9861 	if (rc) {
9862 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9863 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9864 		return rc;
9865 	}
9866 
9867 	return 0;
9868 }
9869 
9870 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9871 {
9872 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9873 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9874 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9875 }
9876 
9877 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9878 {
9879 	struct bnxt_fw_health *fw_health = bp->fw_health;
9880 	u32 reg_type;
9881 
9882 	if (!fw_health)
9883 		return;
9884 
9885 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9886 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9887 		fw_health->status_reliable = false;
9888 
9889 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9890 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9891 		fw_health->resets_reliable = false;
9892 }
9893 
9894 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9895 {
9896 	void __iomem *hs;
9897 	u32 status_loc;
9898 	u32 reg_type;
9899 	u32 sig;
9900 
9901 	if (bp->fw_health)
9902 		bp->fw_health->status_reliable = false;
9903 
9904 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9905 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9906 
9907 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9908 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9909 		if (!bp->chip_num) {
9910 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9911 			bp->chip_num = readl(bp->bar0 +
9912 					     BNXT_FW_HEALTH_WIN_BASE +
9913 					     BNXT_GRC_REG_CHIP_NUM);
9914 		}
9915 		if (!BNXT_CHIP_P5_PLUS(bp))
9916 			return;
9917 
9918 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9919 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9920 	} else {
9921 		status_loc = readl(hs + offsetof(struct hcomm_status,
9922 						 fw_status_loc));
9923 	}
9924 
9925 	if (__bnxt_alloc_fw_health(bp)) {
9926 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9927 		return;
9928 	}
9929 
9930 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9931 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9932 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9933 		__bnxt_map_fw_health_reg(bp, status_loc);
9934 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9935 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9936 	}
9937 
9938 	bp->fw_health->status_reliable = true;
9939 }
9940 
9941 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9942 {
9943 	struct bnxt_fw_health *fw_health = bp->fw_health;
9944 	u32 reg_base = 0xffffffff;
9945 	int i;
9946 
9947 	bp->fw_health->status_reliable = false;
9948 	bp->fw_health->resets_reliable = false;
9949 	/* Only pre-map the monitoring GRC registers using window 3 */
9950 	for (i = 0; i < 4; i++) {
9951 		u32 reg = fw_health->regs[i];
9952 
9953 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9954 			continue;
9955 		if (reg_base == 0xffffffff)
9956 			reg_base = reg & BNXT_GRC_BASE_MASK;
9957 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9958 			return -ERANGE;
9959 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9960 	}
9961 	bp->fw_health->status_reliable = true;
9962 	bp->fw_health->resets_reliable = true;
9963 	if (reg_base == 0xffffffff)
9964 		return 0;
9965 
9966 	__bnxt_map_fw_health_reg(bp, reg_base);
9967 	return 0;
9968 }
9969 
9970 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9971 {
9972 	if (!bp->fw_health)
9973 		return;
9974 
9975 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9976 		bp->fw_health->status_reliable = true;
9977 		bp->fw_health->resets_reliable = true;
9978 	} else {
9979 		bnxt_try_map_fw_health_reg(bp);
9980 	}
9981 }
9982 
9983 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9984 {
9985 	struct bnxt_fw_health *fw_health = bp->fw_health;
9986 	struct hwrm_error_recovery_qcfg_output *resp;
9987 	struct hwrm_error_recovery_qcfg_input *req;
9988 	int rc, i;
9989 
9990 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9991 		return 0;
9992 
9993 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9994 	if (rc)
9995 		return rc;
9996 
9997 	resp = hwrm_req_hold(bp, req);
9998 	rc = hwrm_req_send(bp, req);
9999 	if (rc)
10000 		goto err_recovery_out;
10001 	fw_health->flags = le32_to_cpu(resp->flags);
10002 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
10003 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
10004 		rc = -EINVAL;
10005 		goto err_recovery_out;
10006 	}
10007 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
10008 	fw_health->master_func_wait_dsecs =
10009 		le32_to_cpu(resp->master_func_wait_period);
10010 	fw_health->normal_func_wait_dsecs =
10011 		le32_to_cpu(resp->normal_func_wait_period);
10012 	fw_health->post_reset_wait_dsecs =
10013 		le32_to_cpu(resp->master_func_wait_period_after_reset);
10014 	fw_health->post_reset_max_wait_dsecs =
10015 		le32_to_cpu(resp->max_bailout_time_after_reset);
10016 	fw_health->regs[BNXT_FW_HEALTH_REG] =
10017 		le32_to_cpu(resp->fw_health_status_reg);
10018 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
10019 		le32_to_cpu(resp->fw_heartbeat_reg);
10020 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
10021 		le32_to_cpu(resp->fw_reset_cnt_reg);
10022 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
10023 		le32_to_cpu(resp->reset_inprogress_reg);
10024 	fw_health->fw_reset_inprog_reg_mask =
10025 		le32_to_cpu(resp->reset_inprogress_reg_mask);
10026 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
10027 	if (fw_health->fw_reset_seq_cnt >= 16) {
10028 		rc = -EINVAL;
10029 		goto err_recovery_out;
10030 	}
10031 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
10032 		fw_health->fw_reset_seq_regs[i] =
10033 			le32_to_cpu(resp->reset_reg[i]);
10034 		fw_health->fw_reset_seq_vals[i] =
10035 			le32_to_cpu(resp->reset_reg_val[i]);
10036 		fw_health->fw_reset_seq_delay_msec[i] =
10037 			resp->delay_after_reset[i];
10038 	}
10039 err_recovery_out:
10040 	hwrm_req_drop(bp, req);
10041 	if (!rc)
10042 		rc = bnxt_map_fw_health_regs(bp);
10043 	if (rc)
10044 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10045 	return rc;
10046 }
10047 
10048 static int bnxt_hwrm_func_reset(struct bnxt *bp)
10049 {
10050 	struct hwrm_func_reset_input *req;
10051 	int rc;
10052 
10053 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
10054 	if (rc)
10055 		return rc;
10056 
10057 	req->enables = 0;
10058 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
10059 	return hwrm_req_send(bp, req);
10060 }
10061 
10062 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
10063 {
10064 	struct hwrm_nvm_get_dev_info_output nvm_info;
10065 
10066 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
10067 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10068 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10069 			 nvm_info.nvm_cfg_ver_upd);
10070 }
10071 
10072 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10073 {
10074 	struct hwrm_queue_qportcfg_output *resp;
10075 	struct hwrm_queue_qportcfg_input *req;
10076 	u8 i, j, *qptr;
10077 	bool no_rdma;
10078 	int rc = 0;
10079 
10080 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10081 	if (rc)
10082 		return rc;
10083 
10084 	resp = hwrm_req_hold(bp, req);
10085 	rc = hwrm_req_send(bp, req);
10086 	if (rc)
10087 		goto qportcfg_exit;
10088 
10089 	if (!resp->max_configurable_queues) {
10090 		rc = -EINVAL;
10091 		goto qportcfg_exit;
10092 	}
10093 	bp->max_tc = resp->max_configurable_queues;
10094 	bp->max_lltc = resp->max_configurable_lossless_queues;
10095 	if (bp->max_tc > BNXT_MAX_QUEUE)
10096 		bp->max_tc = BNXT_MAX_QUEUE;
10097 
10098 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10099 	qptr = &resp->queue_id0;
10100 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10101 		bp->q_info[j].queue_id = *qptr;
10102 		bp->q_ids[i] = *qptr++;
10103 		bp->q_info[j].queue_profile = *qptr++;
10104 		bp->tc_to_qidx[j] = j;
10105 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10106 		    (no_rdma && BNXT_PF(bp)))
10107 			j++;
10108 	}
10109 	bp->max_q = bp->max_tc;
10110 	bp->max_tc = max_t(u8, j, 1);
10111 
10112 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10113 		bp->max_tc = 1;
10114 
10115 	if (bp->max_lltc > bp->max_tc)
10116 		bp->max_lltc = bp->max_tc;
10117 
10118 qportcfg_exit:
10119 	hwrm_req_drop(bp, req);
10120 	return rc;
10121 }
10122 
10123 static int bnxt_hwrm_poll(struct bnxt *bp)
10124 {
10125 	struct hwrm_ver_get_input *req;
10126 	int rc;
10127 
10128 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10129 	if (rc)
10130 		return rc;
10131 
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 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10137 	rc = hwrm_req_send(bp, req);
10138 	return rc;
10139 }
10140 
10141 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10142 {
10143 	struct hwrm_ver_get_output *resp;
10144 	struct hwrm_ver_get_input *req;
10145 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10146 	u32 dev_caps_cfg, hwrm_ver;
10147 	int rc, len, max_tmo_secs;
10148 
10149 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10150 	if (rc)
10151 		return rc;
10152 
10153 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10154 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10155 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10156 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10157 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10158 
10159 	resp = hwrm_req_hold(bp, req);
10160 	rc = hwrm_req_send(bp, req);
10161 	if (rc)
10162 		goto hwrm_ver_get_exit;
10163 
10164 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10165 
10166 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10167 			     resp->hwrm_intf_min_8b << 8 |
10168 			     resp->hwrm_intf_upd_8b;
10169 	if (resp->hwrm_intf_maj_8b < 1) {
10170 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10171 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10172 			    resp->hwrm_intf_upd_8b);
10173 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10174 	}
10175 
10176 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10177 			HWRM_VERSION_UPDATE;
10178 
10179 	if (bp->hwrm_spec_code > hwrm_ver)
10180 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10181 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10182 			 HWRM_VERSION_UPDATE);
10183 	else
10184 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10185 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10186 			 resp->hwrm_intf_upd_8b);
10187 
10188 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10189 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10190 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10191 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10192 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10193 		len = FW_VER_STR_LEN;
10194 	} else {
10195 		fw_maj = resp->hwrm_fw_maj_8b;
10196 		fw_min = resp->hwrm_fw_min_8b;
10197 		fw_bld = resp->hwrm_fw_bld_8b;
10198 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10199 		len = BC_HWRM_STR_LEN;
10200 	}
10201 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10202 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10203 		 fw_rsv);
10204 
10205 	if (strlen(resp->active_pkg_name)) {
10206 		int fw_ver_len = strlen(bp->fw_ver_str);
10207 
10208 		snprintf(bp->fw_ver_str + fw_ver_len,
10209 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10210 			 resp->active_pkg_name);
10211 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10212 	}
10213 
10214 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10215 	if (!bp->hwrm_cmd_timeout)
10216 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10217 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10218 	if (!bp->hwrm_cmd_max_timeout)
10219 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10220 	max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000;
10221 #ifdef CONFIG_DETECT_HUNG_TASK
10222 	if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT ||
10223 	    max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) {
10224 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n",
10225 			    max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT);
10226 	}
10227 #endif
10228 
10229 	if (resp->hwrm_intf_maj_8b >= 1) {
10230 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10231 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10232 	}
10233 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10234 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10235 
10236 	bp->chip_num = le16_to_cpu(resp->chip_num);
10237 	bp->chip_rev = resp->chip_rev;
10238 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10239 	    !resp->chip_metal)
10240 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10241 
10242 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10243 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10244 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10245 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10246 
10247 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10248 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10249 
10250 	if (dev_caps_cfg &
10251 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10252 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10253 
10254 	if (dev_caps_cfg &
10255 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10256 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10257 
10258 	if (dev_caps_cfg &
10259 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10260 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10261 
10262 hwrm_ver_get_exit:
10263 	hwrm_req_drop(bp, req);
10264 	return rc;
10265 }
10266 
10267 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10268 {
10269 	struct hwrm_fw_set_time_input *req;
10270 	struct tm tm;
10271 	time64_t now = ktime_get_real_seconds();
10272 	int rc;
10273 
10274 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10275 	    bp->hwrm_spec_code < 0x10400)
10276 		return -EOPNOTSUPP;
10277 
10278 	time64_to_tm(now, 0, &tm);
10279 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10280 	if (rc)
10281 		return rc;
10282 
10283 	req->year = cpu_to_le16(1900 + tm.tm_year);
10284 	req->month = 1 + tm.tm_mon;
10285 	req->day = tm.tm_mday;
10286 	req->hour = tm.tm_hour;
10287 	req->minute = tm.tm_min;
10288 	req->second = tm.tm_sec;
10289 	return hwrm_req_send(bp, req);
10290 }
10291 
10292 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10293 {
10294 	u64 sw_tmp;
10295 
10296 	hw &= mask;
10297 	sw_tmp = (*sw & ~mask) | hw;
10298 	if (hw < (*sw & mask))
10299 		sw_tmp += mask + 1;
10300 	WRITE_ONCE(*sw, sw_tmp);
10301 }
10302 
10303 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10304 				    int count, bool ignore_zero)
10305 {
10306 	int i;
10307 
10308 	for (i = 0; i < count; i++) {
10309 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10310 
10311 		if (ignore_zero && !hw)
10312 			continue;
10313 
10314 		if (masks[i] == -1ULL)
10315 			sw_stats[i] = hw;
10316 		else
10317 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10318 	}
10319 }
10320 
10321 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10322 {
10323 	if (!stats->hw_stats)
10324 		return;
10325 
10326 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10327 				stats->hw_masks, stats->len / 8, false);
10328 }
10329 
10330 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10331 {
10332 	struct bnxt_stats_mem *ring0_stats;
10333 	bool ignore_zero = false;
10334 	int i;
10335 
10336 	/* Chip bug.  Counter intermittently becomes 0. */
10337 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10338 		ignore_zero = true;
10339 
10340 	for (i = 0; i < bp->cp_nr_rings; i++) {
10341 		struct bnxt_napi *bnapi = bp->bnapi[i];
10342 		struct bnxt_cp_ring_info *cpr;
10343 		struct bnxt_stats_mem *stats;
10344 
10345 		cpr = &bnapi->cp_ring;
10346 		stats = &cpr->stats;
10347 		if (!i)
10348 			ring0_stats = stats;
10349 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10350 					ring0_stats->hw_masks,
10351 					ring0_stats->len / 8, ignore_zero);
10352 	}
10353 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10354 		struct bnxt_stats_mem *stats = &bp->port_stats;
10355 		__le64 *hw_stats = stats->hw_stats;
10356 		u64 *sw_stats = stats->sw_stats;
10357 		u64 *masks = stats->hw_masks;
10358 		int cnt;
10359 
10360 		cnt = sizeof(struct rx_port_stats) / 8;
10361 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10362 
10363 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10364 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10365 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10366 		cnt = sizeof(struct tx_port_stats) / 8;
10367 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10368 	}
10369 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10370 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10371 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10372 	}
10373 }
10374 
10375 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10376 {
10377 	struct hwrm_port_qstats_input *req;
10378 	struct bnxt_pf_info *pf = &bp->pf;
10379 	int rc;
10380 
10381 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10382 		return 0;
10383 
10384 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10385 		return -EOPNOTSUPP;
10386 
10387 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10388 	if (rc)
10389 		return rc;
10390 
10391 	req->flags = flags;
10392 	req->port_id = cpu_to_le16(pf->port_id);
10393 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10394 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10395 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10396 	return hwrm_req_send(bp, req);
10397 }
10398 
10399 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10400 {
10401 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10402 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10403 	struct hwrm_port_qstats_ext_output *resp_qs;
10404 	struct hwrm_port_qstats_ext_input *req_qs;
10405 	struct bnxt_pf_info *pf = &bp->pf;
10406 	u32 tx_stat_size;
10407 	int rc;
10408 
10409 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10410 		return 0;
10411 
10412 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10413 		return -EOPNOTSUPP;
10414 
10415 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10416 	if (rc)
10417 		return rc;
10418 
10419 	req_qs->flags = flags;
10420 	req_qs->port_id = cpu_to_le16(pf->port_id);
10421 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10422 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10423 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10424 		       sizeof(struct tx_port_stats_ext) : 0;
10425 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10426 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10427 	resp_qs = hwrm_req_hold(bp, req_qs);
10428 	rc = hwrm_req_send(bp, req_qs);
10429 	if (!rc) {
10430 		bp->fw_rx_stats_ext_size =
10431 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10432 		if (BNXT_FW_MAJ(bp) < 220 &&
10433 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10434 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10435 
10436 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10437 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10438 	} else {
10439 		bp->fw_rx_stats_ext_size = 0;
10440 		bp->fw_tx_stats_ext_size = 0;
10441 	}
10442 	hwrm_req_drop(bp, req_qs);
10443 
10444 	if (flags)
10445 		return rc;
10446 
10447 	if (bp->fw_tx_stats_ext_size <=
10448 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10449 		bp->pri2cos_valid = 0;
10450 		return rc;
10451 	}
10452 
10453 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10454 	if (rc)
10455 		return rc;
10456 
10457 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10458 
10459 	resp_qc = hwrm_req_hold(bp, req_qc);
10460 	rc = hwrm_req_send(bp, req_qc);
10461 	if (!rc) {
10462 		u8 *pri2cos;
10463 		int i, j;
10464 
10465 		pri2cos = &resp_qc->pri0_cos_queue_id;
10466 		for (i = 0; i < 8; i++) {
10467 			u8 queue_id = pri2cos[i];
10468 			u8 queue_idx;
10469 
10470 			/* Per port queue IDs start from 0, 10, 20, etc */
10471 			queue_idx = queue_id % 10;
10472 			if (queue_idx > BNXT_MAX_QUEUE) {
10473 				bp->pri2cos_valid = false;
10474 				hwrm_req_drop(bp, req_qc);
10475 				return rc;
10476 			}
10477 			for (j = 0; j < bp->max_q; j++) {
10478 				if (bp->q_ids[j] == queue_id)
10479 					bp->pri2cos_idx[i] = queue_idx;
10480 			}
10481 		}
10482 		bp->pri2cos_valid = true;
10483 	}
10484 	hwrm_req_drop(bp, req_qc);
10485 
10486 	return rc;
10487 }
10488 
10489 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10490 {
10491 	bnxt_hwrm_tunnel_dst_port_free(bp,
10492 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10493 	bnxt_hwrm_tunnel_dst_port_free(bp,
10494 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10495 }
10496 
10497 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10498 {
10499 	int rc, i;
10500 	u32 tpa_flags = 0;
10501 
10502 	if (set_tpa)
10503 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10504 	else if (BNXT_NO_FW_ACCESS(bp))
10505 		return 0;
10506 	for (i = 0; i < bp->nr_vnics; i++) {
10507 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10508 		if (rc) {
10509 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10510 				   i, rc);
10511 			return rc;
10512 		}
10513 	}
10514 	return 0;
10515 }
10516 
10517 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10518 {
10519 	int i;
10520 
10521 	for (i = 0; i < bp->nr_vnics; i++)
10522 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10523 }
10524 
10525 static void bnxt_clear_vnic(struct bnxt *bp)
10526 {
10527 	if (!bp->vnic_info)
10528 		return;
10529 
10530 	bnxt_hwrm_clear_vnic_filter(bp);
10531 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10532 		/* clear all RSS setting before free vnic ctx */
10533 		bnxt_hwrm_clear_vnic_rss(bp);
10534 		bnxt_hwrm_vnic_ctx_free(bp);
10535 	}
10536 	/* before free the vnic, undo the vnic tpa settings */
10537 	if (bp->flags & BNXT_FLAG_TPA)
10538 		bnxt_set_tpa(bp, false);
10539 	bnxt_hwrm_vnic_free(bp);
10540 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10541 		bnxt_hwrm_vnic_ctx_free(bp);
10542 }
10543 
10544 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10545 				    bool irq_re_init)
10546 {
10547 	bnxt_clear_vnic(bp);
10548 	bnxt_hwrm_ring_free(bp, close_path);
10549 	bnxt_hwrm_ring_grp_free(bp);
10550 	if (irq_re_init) {
10551 		bnxt_hwrm_stat_ctx_free(bp);
10552 		bnxt_hwrm_free_tunnel_ports(bp);
10553 	}
10554 }
10555 
10556 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10557 {
10558 	struct hwrm_func_cfg_input *req;
10559 	u8 evb_mode;
10560 	int rc;
10561 
10562 	if (br_mode == BRIDGE_MODE_VEB)
10563 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10564 	else if (br_mode == BRIDGE_MODE_VEPA)
10565 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10566 	else
10567 		return -EINVAL;
10568 
10569 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10570 	if (rc)
10571 		return rc;
10572 
10573 	req->fid = cpu_to_le16(0xffff);
10574 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10575 	req->evb_mode = evb_mode;
10576 	return hwrm_req_send(bp, req);
10577 }
10578 
10579 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10580 {
10581 	struct hwrm_func_cfg_input *req;
10582 	int rc;
10583 
10584 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10585 		return 0;
10586 
10587 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10588 	if (rc)
10589 		return rc;
10590 
10591 	req->fid = cpu_to_le16(0xffff);
10592 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10593 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10594 	if (size == 128)
10595 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10596 
10597 	return hwrm_req_send(bp, req);
10598 }
10599 
10600 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10601 {
10602 	int rc;
10603 
10604 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10605 		goto skip_rss_ctx;
10606 
10607 	/* allocate context for vnic */
10608 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10609 	if (rc) {
10610 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10611 			   vnic->vnic_id, rc);
10612 		goto vnic_setup_err;
10613 	}
10614 	bp->rsscos_nr_ctxs++;
10615 
10616 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10617 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10618 		if (rc) {
10619 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10620 				   vnic->vnic_id, rc);
10621 			goto vnic_setup_err;
10622 		}
10623 		bp->rsscos_nr_ctxs++;
10624 	}
10625 
10626 skip_rss_ctx:
10627 	/* configure default vnic, ring grp */
10628 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10629 	if (rc) {
10630 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10631 			   vnic->vnic_id, rc);
10632 		goto vnic_setup_err;
10633 	}
10634 
10635 	/* Enable RSS hashing on vnic */
10636 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10637 	if (rc) {
10638 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10639 			   vnic->vnic_id, rc);
10640 		goto vnic_setup_err;
10641 	}
10642 
10643 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10644 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10645 		if (rc) {
10646 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10647 				   vnic->vnic_id, rc);
10648 		}
10649 	}
10650 
10651 vnic_setup_err:
10652 	return rc;
10653 }
10654 
10655 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10656 			  u8 valid)
10657 {
10658 	struct hwrm_vnic_update_input *req;
10659 	int rc;
10660 
10661 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10662 	if (rc)
10663 		return rc;
10664 
10665 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10666 
10667 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10668 		req->mru = cpu_to_le16(vnic->mru);
10669 
10670 	req->enables = cpu_to_le32(valid);
10671 
10672 	return hwrm_req_send(bp, req);
10673 }
10674 
10675 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10676 {
10677 	int rc;
10678 
10679 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10680 	if (rc) {
10681 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10682 			   vnic->vnic_id, rc);
10683 		return rc;
10684 	}
10685 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10686 	if (rc)
10687 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10688 			   vnic->vnic_id, rc);
10689 	return rc;
10690 }
10691 
10692 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10693 {
10694 	int rc, i, nr_ctxs;
10695 
10696 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10697 	for (i = 0; i < nr_ctxs; i++) {
10698 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10699 		if (rc) {
10700 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10701 				   vnic->vnic_id, i, rc);
10702 			break;
10703 		}
10704 		bp->rsscos_nr_ctxs++;
10705 	}
10706 	if (i < nr_ctxs)
10707 		return -ENOMEM;
10708 
10709 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10710 	if (rc)
10711 		return rc;
10712 
10713 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10714 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10715 		if (rc) {
10716 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10717 				   vnic->vnic_id, rc);
10718 		}
10719 	}
10720 	return rc;
10721 }
10722 
10723 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10724 {
10725 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10726 		return __bnxt_setup_vnic_p5(bp, vnic);
10727 	else
10728 		return __bnxt_setup_vnic(bp, vnic);
10729 }
10730 
10731 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10732 				     struct bnxt_vnic_info *vnic,
10733 				     u16 start_rx_ring_idx, int rx_rings)
10734 {
10735 	int rc;
10736 
10737 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10738 	if (rc) {
10739 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10740 			   vnic->vnic_id, rc);
10741 		return rc;
10742 	}
10743 	return bnxt_setup_vnic(bp, vnic);
10744 }
10745 
10746 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10747 {
10748 	struct bnxt_vnic_info *vnic;
10749 	int i, rc = 0;
10750 
10751 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10752 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10753 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10754 	}
10755 
10756 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10757 		return 0;
10758 
10759 	for (i = 0; i < bp->rx_nr_rings; i++) {
10760 		u16 vnic_id = i + 1;
10761 		u16 ring_id = i;
10762 
10763 		if (vnic_id >= bp->nr_vnics)
10764 			break;
10765 
10766 		vnic = &bp->vnic_info[vnic_id];
10767 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10768 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10769 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10770 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10771 			break;
10772 	}
10773 	return rc;
10774 }
10775 
10776 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10777 			  bool all)
10778 {
10779 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10780 	struct bnxt_filter_base *usr_fltr, *tmp;
10781 	struct bnxt_ntuple_filter *ntp_fltr;
10782 	int i;
10783 
10784 	if (netif_running(bp->dev)) {
10785 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10786 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10787 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10788 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10789 		}
10790 	}
10791 	if (!all)
10792 		return;
10793 
10794 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10795 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10796 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10797 			ntp_fltr = container_of(usr_fltr,
10798 						struct bnxt_ntuple_filter,
10799 						base);
10800 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10801 			bnxt_del_ntp_filter(bp, ntp_fltr);
10802 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10803 		}
10804 	}
10805 
10806 	if (vnic->rss_table)
10807 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10808 				  vnic->rss_table,
10809 				  vnic->rss_table_dma_addr);
10810 	bp->num_rss_ctx--;
10811 }
10812 
10813 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10814 				  int rxr_id)
10815 {
10816 	u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
10817 	int i, vnic_rx;
10818 
10819 	/* Ntuple VNIC always has all the rx rings. Any change of ring id
10820 	 * must be updated because a future filter may use it.
10821 	 */
10822 	if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
10823 		return true;
10824 
10825 	for (i = 0; i < tbl_size; i++) {
10826 		if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
10827 			vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
10828 		else
10829 			vnic_rx = bp->rss_indir_tbl[i];
10830 
10831 		if (rxr_id == vnic_rx)
10832 			return true;
10833 	}
10834 
10835 	return false;
10836 }
10837 
10838 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10839 				u16 mru, int rxr_id)
10840 {
10841 	int rc;
10842 
10843 	if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id))
10844 		return 0;
10845 
10846 	if (mru) {
10847 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10848 		if (rc) {
10849 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10850 				   vnic->vnic_id, rc);
10851 			return rc;
10852 		}
10853 	}
10854 	vnic->mru = mru;
10855 	bnxt_hwrm_vnic_update(bp, vnic,
10856 			      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
10857 
10858 	return 0;
10859 }
10860 
10861 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id)
10862 {
10863 	struct ethtool_rxfh_context *ctx;
10864 	unsigned long context;
10865 	int rc;
10866 
10867 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10868 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10869 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10870 
10871 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id);
10872 		if (rc)
10873 			return rc;
10874 	}
10875 
10876 	return 0;
10877 }
10878 
10879 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10880 {
10881 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10882 	struct ethtool_rxfh_context *ctx;
10883 	unsigned long context;
10884 
10885 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10886 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10887 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10888 
10889 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10890 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10891 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10892 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10893 				   rss_ctx->index);
10894 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10895 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10896 		}
10897 	}
10898 }
10899 
10900 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10901 {
10902 	struct ethtool_rxfh_context *ctx;
10903 	unsigned long context;
10904 
10905 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10906 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10907 
10908 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10909 	}
10910 }
10911 
10912 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10913 static bool bnxt_promisc_ok(struct bnxt *bp)
10914 {
10915 #ifdef CONFIG_BNXT_SRIOV
10916 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10917 		return false;
10918 #endif
10919 	return true;
10920 }
10921 
10922 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10923 {
10924 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10925 	unsigned int rc = 0;
10926 
10927 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10928 	if (rc) {
10929 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10930 			   rc);
10931 		return rc;
10932 	}
10933 
10934 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10935 	if (rc) {
10936 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10937 			   rc);
10938 		return rc;
10939 	}
10940 	return rc;
10941 }
10942 
10943 static int bnxt_cfg_rx_mode(struct bnxt *);
10944 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10945 
10946 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10947 {
10948 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10949 	int rc = 0;
10950 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10951 
10952 	if (irq_re_init) {
10953 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10954 		if (rc) {
10955 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10956 				   rc);
10957 			goto err_out;
10958 		}
10959 	}
10960 
10961 	rc = bnxt_hwrm_ring_alloc(bp);
10962 	if (rc) {
10963 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10964 		goto err_out;
10965 	}
10966 
10967 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10968 	if (rc) {
10969 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10970 		goto err_out;
10971 	}
10972 
10973 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10974 		rx_nr_rings--;
10975 
10976 	/* default vnic 0 */
10977 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10978 	if (rc) {
10979 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10980 		goto err_out;
10981 	}
10982 
10983 	if (BNXT_VF(bp))
10984 		bnxt_hwrm_func_qcfg(bp);
10985 
10986 	rc = bnxt_setup_vnic(bp, vnic);
10987 	if (rc)
10988 		goto err_out;
10989 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10990 		bnxt_hwrm_update_rss_hash_cfg(bp);
10991 
10992 	if (bp->flags & BNXT_FLAG_RFS) {
10993 		rc = bnxt_alloc_rfs_vnics(bp);
10994 		if (rc)
10995 			goto err_out;
10996 	}
10997 
10998 	if (bp->flags & BNXT_FLAG_TPA) {
10999 		rc = bnxt_set_tpa(bp, true);
11000 		if (rc)
11001 			goto err_out;
11002 	}
11003 
11004 	if (BNXT_VF(bp))
11005 		bnxt_update_vf_mac(bp);
11006 
11007 	/* Filter for default vnic 0 */
11008 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
11009 	if (rc) {
11010 		if (BNXT_VF(bp) && rc == -ENODEV)
11011 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
11012 		else
11013 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
11014 		goto err_out;
11015 	}
11016 	vnic->uc_filter_count = 1;
11017 
11018 	vnic->rx_mask = 0;
11019 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
11020 		goto skip_rx_mask;
11021 
11022 	if (bp->dev->flags & IFF_BROADCAST)
11023 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11024 
11025 	if (bp->dev->flags & IFF_PROMISC)
11026 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11027 
11028 	if (bp->dev->flags & IFF_ALLMULTI) {
11029 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11030 		vnic->mc_list_count = 0;
11031 	} else if (bp->dev->flags & IFF_MULTICAST) {
11032 		u32 mask = 0;
11033 
11034 		bnxt_mc_list_updated(bp, &mask);
11035 		vnic->rx_mask |= mask;
11036 	}
11037 
11038 	rc = bnxt_cfg_rx_mode(bp);
11039 	if (rc)
11040 		goto err_out;
11041 
11042 skip_rx_mask:
11043 	rc = bnxt_hwrm_set_coal(bp);
11044 	if (rc)
11045 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
11046 				rc);
11047 
11048 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11049 		rc = bnxt_setup_nitroa0_vnic(bp);
11050 		if (rc)
11051 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
11052 				   rc);
11053 	}
11054 
11055 	if (BNXT_VF(bp)) {
11056 		bnxt_hwrm_func_qcfg(bp);
11057 		netdev_update_features(bp->dev);
11058 	}
11059 
11060 	return 0;
11061 
11062 err_out:
11063 	bnxt_hwrm_resource_free(bp, 0, true);
11064 
11065 	return rc;
11066 }
11067 
11068 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
11069 {
11070 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
11071 	return 0;
11072 }
11073 
11074 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
11075 {
11076 	bnxt_init_cp_rings(bp);
11077 	bnxt_init_rx_rings(bp);
11078 	bnxt_init_tx_rings(bp);
11079 	bnxt_init_ring_grps(bp, irq_re_init);
11080 	bnxt_init_vnics(bp);
11081 
11082 	return bnxt_init_chip(bp, irq_re_init);
11083 }
11084 
11085 static int bnxt_set_real_num_queues(struct bnxt *bp)
11086 {
11087 	int rc;
11088 	struct net_device *dev = bp->dev;
11089 
11090 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
11091 					  bp->tx_nr_rings_xdp);
11092 	if (rc)
11093 		return rc;
11094 
11095 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
11096 	if (rc)
11097 		return rc;
11098 
11099 #ifdef CONFIG_RFS_ACCEL
11100 	if (bp->flags & BNXT_FLAG_RFS)
11101 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
11102 #endif
11103 
11104 	return rc;
11105 }
11106 
11107 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11108 			     bool shared)
11109 {
11110 	int _rx = *rx, _tx = *tx;
11111 
11112 	if (shared) {
11113 		*rx = min_t(int, _rx, max);
11114 		*tx = min_t(int, _tx, max);
11115 	} else {
11116 		if (max < 2)
11117 			return -ENOMEM;
11118 
11119 		while (_rx + _tx > max) {
11120 			if (_rx > _tx && _rx > 1)
11121 				_rx--;
11122 			else if (_tx > 1)
11123 				_tx--;
11124 		}
11125 		*rx = _rx;
11126 		*tx = _tx;
11127 	}
11128 	return 0;
11129 }
11130 
11131 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
11132 {
11133 	return (tx - tx_xdp) / tx_sets + tx_xdp;
11134 }
11135 
11136 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
11137 {
11138 	int tcs = bp->num_tc;
11139 
11140 	if (!tcs)
11141 		tcs = 1;
11142 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11143 }
11144 
11145 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11146 {
11147 	int tcs = bp->num_tc;
11148 
11149 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11150 	       bp->tx_nr_rings_xdp;
11151 }
11152 
11153 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11154 			   bool sh)
11155 {
11156 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11157 
11158 	if (tx_cp != *tx) {
11159 		int tx_saved = tx_cp, rc;
11160 
11161 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11162 		if (rc)
11163 			return rc;
11164 		if (tx_cp != tx_saved)
11165 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11166 		return 0;
11167 	}
11168 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11169 }
11170 
11171 static void bnxt_setup_msix(struct bnxt *bp)
11172 {
11173 	const int len = sizeof(bp->irq_tbl[0].name);
11174 	struct net_device *dev = bp->dev;
11175 	int tcs, i;
11176 
11177 	tcs = bp->num_tc;
11178 	if (tcs) {
11179 		int i, off, count;
11180 
11181 		for (i = 0; i < tcs; i++) {
11182 			count = bp->tx_nr_rings_per_tc;
11183 			off = BNXT_TC_TO_RING_BASE(bp, i);
11184 			netdev_set_tc_queue(dev, i, count, off);
11185 		}
11186 	}
11187 
11188 	for (i = 0; i < bp->cp_nr_rings; i++) {
11189 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11190 		char *attr;
11191 
11192 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11193 			attr = "TxRx";
11194 		else if (i < bp->rx_nr_rings)
11195 			attr = "rx";
11196 		else
11197 			attr = "tx";
11198 
11199 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11200 			 attr, i);
11201 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11202 	}
11203 }
11204 
11205 static int bnxt_init_int_mode(struct bnxt *bp);
11206 
11207 static int bnxt_change_msix(struct bnxt *bp, int total)
11208 {
11209 	struct msi_map map;
11210 	int i;
11211 
11212 	/* add MSIX to the end if needed */
11213 	for (i = bp->total_irqs; i < total; i++) {
11214 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11215 		if (map.index < 0)
11216 			return bp->total_irqs;
11217 		bp->irq_tbl[i].vector = map.virq;
11218 		bp->total_irqs++;
11219 	}
11220 
11221 	/* trim MSIX from the end if needed */
11222 	for (i = bp->total_irqs; i > total; i--) {
11223 		map.index = i - 1;
11224 		map.virq = bp->irq_tbl[i - 1].vector;
11225 		pci_msix_free_irq(bp->pdev, map);
11226 		bp->total_irqs--;
11227 	}
11228 	return bp->total_irqs;
11229 }
11230 
11231 static int bnxt_setup_int_mode(struct bnxt *bp)
11232 {
11233 	int rc;
11234 
11235 	if (!bp->irq_tbl) {
11236 		rc = bnxt_init_int_mode(bp);
11237 		if (rc || !bp->irq_tbl)
11238 			return rc ?: -ENODEV;
11239 	}
11240 
11241 	bnxt_setup_msix(bp);
11242 
11243 	rc = bnxt_set_real_num_queues(bp);
11244 	return rc;
11245 }
11246 
11247 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11248 {
11249 	return bp->hw_resc.max_rsscos_ctxs;
11250 }
11251 
11252 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11253 {
11254 	return bp->hw_resc.max_vnics;
11255 }
11256 
11257 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11258 {
11259 	return bp->hw_resc.max_stat_ctxs;
11260 }
11261 
11262 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11263 {
11264 	return bp->hw_resc.max_cp_rings;
11265 }
11266 
11267 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11268 {
11269 	unsigned int cp = bp->hw_resc.max_cp_rings;
11270 
11271 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11272 		cp -= bnxt_get_ulp_msix_num(bp);
11273 
11274 	return cp;
11275 }
11276 
11277 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11278 {
11279 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11280 
11281 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11282 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11283 
11284 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11285 }
11286 
11287 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11288 {
11289 	bp->hw_resc.max_irqs = max_irqs;
11290 }
11291 
11292 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11293 {
11294 	unsigned int cp;
11295 
11296 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11297 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11298 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11299 	else
11300 		return cp - bp->cp_nr_rings;
11301 }
11302 
11303 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11304 {
11305 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11306 }
11307 
11308 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11309 {
11310 	int max_irq = bnxt_get_max_func_irqs(bp);
11311 	int total_req = bp->cp_nr_rings + num;
11312 
11313 	if (max_irq < total_req) {
11314 		num = max_irq - bp->cp_nr_rings;
11315 		if (num <= 0)
11316 			return 0;
11317 	}
11318 	return num;
11319 }
11320 
11321 static int bnxt_get_num_msix(struct bnxt *bp)
11322 {
11323 	if (!BNXT_NEW_RM(bp))
11324 		return bnxt_get_max_func_irqs(bp);
11325 
11326 	return bnxt_nq_rings_in_use(bp);
11327 }
11328 
11329 static int bnxt_init_int_mode(struct bnxt *bp)
11330 {
11331 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11332 
11333 	total_vecs = bnxt_get_num_msix(bp);
11334 	max = bnxt_get_max_func_irqs(bp);
11335 	if (total_vecs > max)
11336 		total_vecs = max;
11337 
11338 	if (!total_vecs)
11339 		return 0;
11340 
11341 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11342 		min = 2;
11343 
11344 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11345 					   PCI_IRQ_MSIX);
11346 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11347 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11348 		rc = -ENODEV;
11349 		goto msix_setup_exit;
11350 	}
11351 
11352 	tbl_size = total_vecs;
11353 	if (pci_msix_can_alloc_dyn(bp->pdev))
11354 		tbl_size = max;
11355 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
11356 	if (bp->irq_tbl) {
11357 		for (i = 0; i < total_vecs; i++)
11358 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11359 
11360 		bp->total_irqs = total_vecs;
11361 		/* Trim rings based upon num of vectors allocated */
11362 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11363 				     total_vecs - ulp_msix, min == 1);
11364 		if (rc)
11365 			goto msix_setup_exit;
11366 
11367 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11368 		bp->cp_nr_rings = (min == 1) ?
11369 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11370 				  tx_cp + bp->rx_nr_rings;
11371 
11372 	} else {
11373 		rc = -ENOMEM;
11374 		goto msix_setup_exit;
11375 	}
11376 	return 0;
11377 
11378 msix_setup_exit:
11379 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11380 	kfree(bp->irq_tbl);
11381 	bp->irq_tbl = NULL;
11382 	pci_free_irq_vectors(bp->pdev);
11383 	return rc;
11384 }
11385 
11386 static void bnxt_clear_int_mode(struct bnxt *bp)
11387 {
11388 	pci_free_irq_vectors(bp->pdev);
11389 
11390 	kfree(bp->irq_tbl);
11391 	bp->irq_tbl = NULL;
11392 }
11393 
11394 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11395 {
11396 	bool irq_cleared = false;
11397 	bool irq_change = false;
11398 	int tcs = bp->num_tc;
11399 	int irqs_required;
11400 	int rc;
11401 
11402 	if (!bnxt_need_reserve_rings(bp))
11403 		return 0;
11404 
11405 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11406 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11407 
11408 		if (ulp_msix > bp->ulp_num_msix_want)
11409 			ulp_msix = bp->ulp_num_msix_want;
11410 		irqs_required = ulp_msix + bp->cp_nr_rings;
11411 	} else {
11412 		irqs_required = bnxt_get_num_msix(bp);
11413 	}
11414 
11415 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11416 		irq_change = true;
11417 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11418 			bnxt_ulp_irq_stop(bp);
11419 			bnxt_clear_int_mode(bp);
11420 			irq_cleared = true;
11421 		}
11422 	}
11423 	rc = __bnxt_reserve_rings(bp);
11424 	if (irq_cleared) {
11425 		if (!rc)
11426 			rc = bnxt_init_int_mode(bp);
11427 		bnxt_ulp_irq_restart(bp, rc);
11428 	} else if (irq_change && !rc) {
11429 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11430 			rc = -ENOSPC;
11431 	}
11432 	if (rc) {
11433 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11434 		return rc;
11435 	}
11436 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11437 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11438 		netdev_err(bp->dev, "tx ring reservation failure\n");
11439 		netdev_reset_tc(bp->dev);
11440 		bp->num_tc = 0;
11441 		if (bp->tx_nr_rings_xdp)
11442 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11443 		else
11444 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11445 		return -ENOMEM;
11446 	}
11447 	return 0;
11448 }
11449 
11450 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11451 {
11452 	struct bnxt_tx_ring_info *txr;
11453 	struct netdev_queue *txq;
11454 	struct bnxt_napi *bnapi;
11455 	int i;
11456 
11457 	bnapi = bp->bnapi[idx];
11458 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11459 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11460 		synchronize_net();
11461 
11462 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11463 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11464 			if (txq) {
11465 				__netif_tx_lock_bh(txq);
11466 				netif_tx_stop_queue(txq);
11467 				__netif_tx_unlock_bh(txq);
11468 			}
11469 		}
11470 
11471 		if (!bp->tph_mode)
11472 			continue;
11473 
11474 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11475 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11476 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11477 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11478 	}
11479 }
11480 
11481 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11482 {
11483 	struct bnxt_tx_ring_info *txr;
11484 	struct netdev_queue *txq;
11485 	struct bnxt_napi *bnapi;
11486 	int rc, i;
11487 
11488 	bnapi = bp->bnapi[idx];
11489 	/* All rings have been reserved and previously allocated.
11490 	 * Reallocating with the same parameters should never fail.
11491 	 */
11492 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11493 		if (!bp->tph_mode)
11494 			goto start_tx;
11495 
11496 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11497 		if (rc)
11498 			return rc;
11499 
11500 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11501 		if (rc)
11502 			return rc;
11503 
11504 		txr->tx_prod = 0;
11505 		txr->tx_cons = 0;
11506 		txr->tx_hw_cons = 0;
11507 start_tx:
11508 		WRITE_ONCE(txr->dev_state, 0);
11509 		synchronize_net();
11510 
11511 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11512 			continue;
11513 
11514 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11515 		if (txq)
11516 			netif_tx_start_queue(txq);
11517 	}
11518 
11519 	return 0;
11520 }
11521 
11522 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11523 				     const cpumask_t *mask)
11524 {
11525 	struct bnxt_irq *irq;
11526 	u16 tag;
11527 	int err;
11528 
11529 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11530 
11531 	if (!irq->bp->tph_mode)
11532 		return;
11533 
11534 	cpumask_copy(irq->cpu_mask, mask);
11535 
11536 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11537 		return;
11538 
11539 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11540 				cpumask_first(irq->cpu_mask), &tag))
11541 		return;
11542 
11543 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag))
11544 		return;
11545 
11546 	netdev_lock(irq->bp->dev);
11547 	if (netif_running(irq->bp->dev)) {
11548 		err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr);
11549 		if (err)
11550 			netdev_err(irq->bp->dev,
11551 				   "RX queue restart failed: err=%d\n", err);
11552 	}
11553 	netdev_unlock(irq->bp->dev);
11554 }
11555 
11556 static void bnxt_irq_affinity_release(struct kref *ref)
11557 {
11558 	struct irq_affinity_notify *notify =
11559 		container_of(ref, struct irq_affinity_notify, kref);
11560 	struct bnxt_irq *irq;
11561 
11562 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11563 
11564 	if (!irq->bp->tph_mode)
11565 		return;
11566 
11567 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11568 		netdev_err(irq->bp->dev,
11569 			   "Setting ST=0 for MSIX entry %d failed\n",
11570 			   irq->msix_nr);
11571 		return;
11572 	}
11573 }
11574 
11575 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11576 {
11577 	irq_set_affinity_notifier(irq->vector, NULL);
11578 }
11579 
11580 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11581 {
11582 	struct irq_affinity_notify *notify;
11583 
11584 	irq->bp = bp;
11585 
11586 	/* Nothing to do if TPH is not enabled */
11587 	if (!bp->tph_mode)
11588 		return;
11589 
11590 	/* Register IRQ affinity notifier */
11591 	notify = &irq->affinity_notify;
11592 	notify->irq = irq->vector;
11593 	notify->notify = bnxt_irq_affinity_notify;
11594 	notify->release = bnxt_irq_affinity_release;
11595 
11596 	irq_set_affinity_notifier(irq->vector, notify);
11597 }
11598 
11599 static void bnxt_free_irq(struct bnxt *bp)
11600 {
11601 	struct bnxt_irq *irq;
11602 	int i;
11603 
11604 #ifdef CONFIG_RFS_ACCEL
11605 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11606 	bp->dev->rx_cpu_rmap = NULL;
11607 #endif
11608 	if (!bp->irq_tbl || !bp->bnapi)
11609 		return;
11610 
11611 	for (i = 0; i < bp->cp_nr_rings; i++) {
11612 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11613 
11614 		irq = &bp->irq_tbl[map_idx];
11615 		if (irq->requested) {
11616 			if (irq->have_cpumask) {
11617 				irq_update_affinity_hint(irq->vector, NULL);
11618 				free_cpumask_var(irq->cpu_mask);
11619 				irq->have_cpumask = 0;
11620 			}
11621 
11622 			bnxt_release_irq_notifier(irq);
11623 
11624 			free_irq(irq->vector, bp->bnapi[i]);
11625 		}
11626 
11627 		irq->requested = 0;
11628 	}
11629 
11630 	/* Disable TPH support */
11631 	pcie_disable_tph(bp->pdev);
11632 	bp->tph_mode = 0;
11633 }
11634 
11635 static int bnxt_request_irq(struct bnxt *bp)
11636 {
11637 	struct cpu_rmap *rmap = NULL;
11638 	int i, j, rc = 0;
11639 	unsigned long flags = 0;
11640 
11641 	rc = bnxt_setup_int_mode(bp);
11642 	if (rc) {
11643 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11644 			   rc);
11645 		return rc;
11646 	}
11647 #ifdef CONFIG_RFS_ACCEL
11648 	rmap = bp->dev->rx_cpu_rmap;
11649 #endif
11650 
11651 	/* Enable TPH support as part of IRQ request */
11652 	rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
11653 	if (!rc)
11654 		bp->tph_mode = PCI_TPH_ST_IV_MODE;
11655 
11656 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11657 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11658 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11659 
11660 		if (IS_ENABLED(CONFIG_RFS_ACCEL) &&
11661 		    rmap && bp->bnapi[i]->rx_ring) {
11662 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11663 			if (rc)
11664 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11665 					    j);
11666 			j++;
11667 		}
11668 
11669 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11670 				 bp->bnapi[i]);
11671 		if (rc)
11672 			break;
11673 
11674 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
11675 		irq->requested = 1;
11676 
11677 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11678 			int numa_node = dev_to_node(&bp->pdev->dev);
11679 			u16 tag;
11680 
11681 			irq->have_cpumask = 1;
11682 			irq->msix_nr = map_idx;
11683 			irq->ring_nr = i;
11684 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11685 					irq->cpu_mask);
11686 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11687 			if (rc) {
11688 				netdev_warn(bp->dev,
11689 					    "Update affinity hint failed, IRQ = %d\n",
11690 					    irq->vector);
11691 				break;
11692 			}
11693 
11694 			bnxt_register_irq_notifier(bp, irq);
11695 
11696 			/* Init ST table entry */
11697 			if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11698 						cpumask_first(irq->cpu_mask),
11699 						&tag))
11700 				continue;
11701 
11702 			pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag);
11703 		}
11704 	}
11705 	return rc;
11706 }
11707 
11708 static void bnxt_del_napi(struct bnxt *bp)
11709 {
11710 	int i;
11711 
11712 	if (!bp->bnapi)
11713 		return;
11714 
11715 	for (i = 0; i < bp->rx_nr_rings; i++)
11716 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11717 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11718 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11719 
11720 	for (i = 0; i < bp->cp_nr_rings; i++) {
11721 		struct bnxt_napi *bnapi = bp->bnapi[i];
11722 
11723 		__netif_napi_del_locked(&bnapi->napi);
11724 	}
11725 	/* We called __netif_napi_del_locked(), we need
11726 	 * to respect an RCU grace period before freeing napi structures.
11727 	 */
11728 	synchronize_net();
11729 }
11730 
11731 static void bnxt_init_napi(struct bnxt *bp)
11732 {
11733 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11734 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11735 	struct bnxt_napi *bnapi;
11736 	int i;
11737 
11738 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11739 		poll_fn = bnxt_poll_p5;
11740 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11741 		cp_nr_rings--;
11742 
11743 	set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11744 
11745 	for (i = 0; i < cp_nr_rings; i++) {
11746 		bnapi = bp->bnapi[i];
11747 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
11748 					     bnapi->index);
11749 	}
11750 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11751 		bnapi = bp->bnapi[cp_nr_rings];
11752 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11753 	}
11754 }
11755 
11756 static void bnxt_disable_napi(struct bnxt *bp)
11757 {
11758 	int i;
11759 
11760 	if (!bp->bnapi ||
11761 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11762 		return;
11763 
11764 	for (i = 0; i < bp->cp_nr_rings; i++) {
11765 		struct bnxt_napi *bnapi = bp->bnapi[i];
11766 		struct bnxt_cp_ring_info *cpr;
11767 
11768 		cpr = &bnapi->cp_ring;
11769 		if (bnapi->tx_fault)
11770 			cpr->sw_stats->tx.tx_resets++;
11771 		if (bnapi->in_reset)
11772 			cpr->sw_stats->rx.rx_resets++;
11773 		napi_disable_locked(&bnapi->napi);
11774 	}
11775 }
11776 
11777 static void bnxt_enable_napi(struct bnxt *bp)
11778 {
11779 	int i;
11780 
11781 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11782 	for (i = 0; i < bp->cp_nr_rings; i++) {
11783 		struct bnxt_napi *bnapi = bp->bnapi[i];
11784 		struct bnxt_cp_ring_info *cpr;
11785 
11786 		bnapi->tx_fault = 0;
11787 
11788 		cpr = &bnapi->cp_ring;
11789 		bnapi->in_reset = false;
11790 
11791 		if (bnapi->rx_ring) {
11792 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11793 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11794 		}
11795 		napi_enable_locked(&bnapi->napi);
11796 	}
11797 }
11798 
11799 void bnxt_tx_disable(struct bnxt *bp)
11800 {
11801 	int i;
11802 	struct bnxt_tx_ring_info *txr;
11803 
11804 	if (bp->tx_ring) {
11805 		for (i = 0; i < bp->tx_nr_rings; i++) {
11806 			txr = &bp->tx_ring[i];
11807 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11808 		}
11809 	}
11810 	/* Make sure napi polls see @dev_state change */
11811 	synchronize_net();
11812 	/* Drop carrier first to prevent TX timeout */
11813 	netif_carrier_off(bp->dev);
11814 	/* Stop all TX queues */
11815 	netif_tx_disable(bp->dev);
11816 }
11817 
11818 void bnxt_tx_enable(struct bnxt *bp)
11819 {
11820 	int i;
11821 	struct bnxt_tx_ring_info *txr;
11822 
11823 	for (i = 0; i < bp->tx_nr_rings; i++) {
11824 		txr = &bp->tx_ring[i];
11825 		WRITE_ONCE(txr->dev_state, 0);
11826 	}
11827 	/* Make sure napi polls see @dev_state change */
11828 	synchronize_net();
11829 	netif_tx_wake_all_queues(bp->dev);
11830 	if (BNXT_LINK_IS_UP(bp))
11831 		netif_carrier_on(bp->dev);
11832 }
11833 
11834 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11835 {
11836 	u8 active_fec = link_info->active_fec_sig_mode &
11837 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11838 
11839 	switch (active_fec) {
11840 	default:
11841 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11842 		return "None";
11843 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11844 		return "Clause 74 BaseR";
11845 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11846 		return "Clause 91 RS(528,514)";
11847 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11848 		return "Clause 91 RS544_1XN";
11849 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11850 		return "Clause 91 RS(544,514)";
11851 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11852 		return "Clause 91 RS272_1XN";
11853 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11854 		return "Clause 91 RS(272,257)";
11855 	}
11856 }
11857 
11858 void bnxt_report_link(struct bnxt *bp)
11859 {
11860 	if (BNXT_LINK_IS_UP(bp)) {
11861 		const char *signal = "";
11862 		const char *flow_ctrl;
11863 		const char *duplex;
11864 		u32 speed;
11865 		u16 fec;
11866 
11867 		netif_carrier_on(bp->dev);
11868 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11869 		if (speed == SPEED_UNKNOWN) {
11870 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11871 			return;
11872 		}
11873 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11874 			duplex = "full";
11875 		else
11876 			duplex = "half";
11877 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11878 			flow_ctrl = "ON - receive & transmit";
11879 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11880 			flow_ctrl = "ON - transmit";
11881 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11882 			flow_ctrl = "ON - receive";
11883 		else
11884 			flow_ctrl = "none";
11885 		if (bp->link_info.phy_qcfg_resp.option_flags &
11886 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11887 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11888 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11889 			switch (sig_mode) {
11890 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11891 				signal = "(NRZ) ";
11892 				break;
11893 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11894 				signal = "(PAM4 56Gbps) ";
11895 				break;
11896 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11897 				signal = "(PAM4 112Gbps) ";
11898 				break;
11899 			default:
11900 				break;
11901 			}
11902 		}
11903 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11904 			    speed, signal, duplex, flow_ctrl);
11905 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11906 			netdev_info(bp->dev, "EEE is %s\n",
11907 				    bp->eee.eee_active ? "active" :
11908 							 "not active");
11909 		fec = bp->link_info.fec_cfg;
11910 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11911 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11912 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11913 				    bnxt_report_fec(&bp->link_info));
11914 	} else {
11915 		netif_carrier_off(bp->dev);
11916 		netdev_err(bp->dev, "NIC Link is Down\n");
11917 	}
11918 }
11919 
11920 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11921 {
11922 	if (!resp->supported_speeds_auto_mode &&
11923 	    !resp->supported_speeds_force_mode &&
11924 	    !resp->supported_pam4_speeds_auto_mode &&
11925 	    !resp->supported_pam4_speeds_force_mode &&
11926 	    !resp->supported_speeds2_auto_mode &&
11927 	    !resp->supported_speeds2_force_mode)
11928 		return true;
11929 	return false;
11930 }
11931 
11932 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11933 {
11934 	struct bnxt_link_info *link_info = &bp->link_info;
11935 	struct hwrm_port_phy_qcaps_output *resp;
11936 	struct hwrm_port_phy_qcaps_input *req;
11937 	int rc = 0;
11938 
11939 	if (bp->hwrm_spec_code < 0x10201)
11940 		return 0;
11941 
11942 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11943 	if (rc)
11944 		return rc;
11945 
11946 	resp = hwrm_req_hold(bp, req);
11947 	rc = hwrm_req_send(bp, req);
11948 	if (rc)
11949 		goto hwrm_phy_qcaps_exit;
11950 
11951 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11952 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11953 		struct ethtool_keee *eee = &bp->eee;
11954 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11955 
11956 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11957 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11958 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11959 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11960 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11961 	}
11962 
11963 	if (bp->hwrm_spec_code >= 0x10a01) {
11964 		if (bnxt_phy_qcaps_no_speed(resp)) {
11965 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11966 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11967 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11968 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11969 			netdev_info(bp->dev, "Ethernet link enabled\n");
11970 			/* Phy re-enabled, reprobe the speeds */
11971 			link_info->support_auto_speeds = 0;
11972 			link_info->support_pam4_auto_speeds = 0;
11973 			link_info->support_auto_speeds2 = 0;
11974 		}
11975 	}
11976 	if (resp->supported_speeds_auto_mode)
11977 		link_info->support_auto_speeds =
11978 			le16_to_cpu(resp->supported_speeds_auto_mode);
11979 	if (resp->supported_pam4_speeds_auto_mode)
11980 		link_info->support_pam4_auto_speeds =
11981 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11982 	if (resp->supported_speeds2_auto_mode)
11983 		link_info->support_auto_speeds2 =
11984 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11985 
11986 	bp->port_count = resp->port_cnt;
11987 
11988 hwrm_phy_qcaps_exit:
11989 	hwrm_req_drop(bp, req);
11990 	return rc;
11991 }
11992 
11993 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
11994 {
11995 	struct hwrm_port_mac_qcaps_output *resp;
11996 	struct hwrm_port_mac_qcaps_input *req;
11997 	int rc;
11998 
11999 	if (bp->hwrm_spec_code < 0x10a03)
12000 		return;
12001 
12002 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
12003 	if (rc)
12004 		return;
12005 
12006 	resp = hwrm_req_hold(bp, req);
12007 	rc = hwrm_req_send_silent(bp, req);
12008 	if (!rc)
12009 		bp->mac_flags = resp->flags;
12010 	hwrm_req_drop(bp, req);
12011 }
12012 
12013 static bool bnxt_support_dropped(u16 advertising, u16 supported)
12014 {
12015 	u16 diff = advertising ^ supported;
12016 
12017 	return ((supported | diff) != supported);
12018 }
12019 
12020 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
12021 {
12022 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
12023 
12024 	/* Check if any advertised speeds are no longer supported. The caller
12025 	 * holds the link_lock mutex, so we can modify link_info settings.
12026 	 */
12027 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12028 		if (bnxt_support_dropped(link_info->advertising,
12029 					 link_info->support_auto_speeds2)) {
12030 			link_info->advertising = link_info->support_auto_speeds2;
12031 			return true;
12032 		}
12033 		return false;
12034 	}
12035 	if (bnxt_support_dropped(link_info->advertising,
12036 				 link_info->support_auto_speeds)) {
12037 		link_info->advertising = link_info->support_auto_speeds;
12038 		return true;
12039 	}
12040 	if (bnxt_support_dropped(link_info->advertising_pam4,
12041 				 link_info->support_pam4_auto_speeds)) {
12042 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
12043 		return true;
12044 	}
12045 	return false;
12046 }
12047 
12048 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
12049 {
12050 	struct bnxt_link_info *link_info = &bp->link_info;
12051 	struct hwrm_port_phy_qcfg_output *resp;
12052 	struct hwrm_port_phy_qcfg_input *req;
12053 	u8 link_state = link_info->link_state;
12054 	bool support_changed;
12055 	int rc;
12056 
12057 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
12058 	if (rc)
12059 		return rc;
12060 
12061 	resp = hwrm_req_hold(bp, req);
12062 	rc = hwrm_req_send(bp, req);
12063 	if (rc) {
12064 		hwrm_req_drop(bp, req);
12065 		if (BNXT_VF(bp) && rc == -ENODEV) {
12066 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
12067 			rc = 0;
12068 		}
12069 		return rc;
12070 	}
12071 
12072 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
12073 	link_info->phy_link_status = resp->link;
12074 	link_info->duplex = resp->duplex_cfg;
12075 	if (bp->hwrm_spec_code >= 0x10800)
12076 		link_info->duplex = resp->duplex_state;
12077 	link_info->pause = resp->pause;
12078 	link_info->auto_mode = resp->auto_mode;
12079 	link_info->auto_pause_setting = resp->auto_pause;
12080 	link_info->lp_pause = resp->link_partner_adv_pause;
12081 	link_info->force_pause_setting = resp->force_pause;
12082 	link_info->duplex_setting = resp->duplex_cfg;
12083 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
12084 		link_info->link_speed = le16_to_cpu(resp->link_speed);
12085 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
12086 			link_info->active_lanes = resp->active_lanes;
12087 	} else {
12088 		link_info->link_speed = 0;
12089 		link_info->active_lanes = 0;
12090 	}
12091 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
12092 	link_info->force_pam4_link_speed =
12093 		le16_to_cpu(resp->force_pam4_link_speed);
12094 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
12095 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
12096 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
12097 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
12098 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
12099 	link_info->auto_pam4_link_speeds =
12100 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
12101 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
12102 	link_info->lp_auto_link_speeds =
12103 		le16_to_cpu(resp->link_partner_adv_speeds);
12104 	link_info->lp_auto_pam4_link_speeds =
12105 		resp->link_partner_pam4_adv_speeds;
12106 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
12107 	link_info->phy_ver[0] = resp->phy_maj;
12108 	link_info->phy_ver[1] = resp->phy_min;
12109 	link_info->phy_ver[2] = resp->phy_bld;
12110 	link_info->media_type = resp->media_type;
12111 	link_info->phy_type = resp->phy_type;
12112 	link_info->transceiver = resp->xcvr_pkg_type;
12113 	link_info->phy_addr = resp->eee_config_phy_addr &
12114 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
12115 	link_info->module_status = resp->module_status;
12116 
12117 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
12118 		struct ethtool_keee *eee = &bp->eee;
12119 		u16 fw_speeds;
12120 
12121 		eee->eee_active = 0;
12122 		if (resp->eee_config_phy_addr &
12123 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
12124 			eee->eee_active = 1;
12125 			fw_speeds = le16_to_cpu(
12126 				resp->link_partner_adv_eee_link_speed_mask);
12127 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
12128 		}
12129 
12130 		/* Pull initial EEE config */
12131 		if (!chng_link_state) {
12132 			if (resp->eee_config_phy_addr &
12133 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
12134 				eee->eee_enabled = 1;
12135 
12136 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
12137 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
12138 
12139 			if (resp->eee_config_phy_addr &
12140 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12141 				__le32 tmr;
12142 
12143 				eee->tx_lpi_enabled = 1;
12144 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12145 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12146 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12147 			}
12148 		}
12149 	}
12150 
12151 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12152 	if (bp->hwrm_spec_code >= 0x10504) {
12153 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12154 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12155 	}
12156 	/* TODO: need to add more logic to report VF link */
12157 	if (chng_link_state) {
12158 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12159 			link_info->link_state = BNXT_LINK_STATE_UP;
12160 		else
12161 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12162 		if (link_state != link_info->link_state)
12163 			bnxt_report_link(bp);
12164 	} else {
12165 		/* always link down if not require to update link state */
12166 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12167 	}
12168 	hwrm_req_drop(bp, req);
12169 
12170 	if (!BNXT_PHY_CFG_ABLE(bp))
12171 		return 0;
12172 
12173 	support_changed = bnxt_support_speed_dropped(link_info);
12174 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12175 		bnxt_hwrm_set_link_setting(bp, true, false);
12176 	return 0;
12177 }
12178 
12179 static void bnxt_get_port_module_status(struct bnxt *bp)
12180 {
12181 	struct bnxt_link_info *link_info = &bp->link_info;
12182 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12183 	u8 module_status;
12184 
12185 	if (bnxt_update_link(bp, true))
12186 		return;
12187 
12188 	module_status = link_info->module_status;
12189 	switch (module_status) {
12190 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12191 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12192 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12193 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12194 			    bp->pf.port_id);
12195 		if (bp->hwrm_spec_code >= 0x10201) {
12196 			netdev_warn(bp->dev, "Module part number %s\n",
12197 				    resp->phy_vendor_partnumber);
12198 		}
12199 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12200 			netdev_warn(bp->dev, "TX is disabled\n");
12201 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12202 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12203 	}
12204 }
12205 
12206 static void
12207 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12208 {
12209 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12210 		if (bp->hwrm_spec_code >= 0x10201)
12211 			req->auto_pause =
12212 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12213 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12214 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12215 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12216 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12217 		req->enables |=
12218 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12219 	} else {
12220 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12221 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12222 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12223 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12224 		req->enables |=
12225 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12226 		if (bp->hwrm_spec_code >= 0x10201) {
12227 			req->auto_pause = req->force_pause;
12228 			req->enables |= cpu_to_le32(
12229 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12230 		}
12231 	}
12232 }
12233 
12234 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12235 {
12236 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12237 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12238 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12239 			req->enables |=
12240 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12241 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12242 		} else if (bp->link_info.advertising) {
12243 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12244 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12245 		}
12246 		if (bp->link_info.advertising_pam4) {
12247 			req->enables |=
12248 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12249 			req->auto_link_pam4_speed_mask =
12250 				cpu_to_le16(bp->link_info.advertising_pam4);
12251 		}
12252 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12253 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12254 	} else {
12255 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12256 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12257 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12258 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12259 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12260 				   (u32)bp->link_info.req_link_speed);
12261 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12262 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12263 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12264 		} else {
12265 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12266 		}
12267 	}
12268 
12269 	/* tell chimp that the setting takes effect immediately */
12270 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12271 }
12272 
12273 int bnxt_hwrm_set_pause(struct bnxt *bp)
12274 {
12275 	struct hwrm_port_phy_cfg_input *req;
12276 	int rc;
12277 
12278 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12279 	if (rc)
12280 		return rc;
12281 
12282 	bnxt_hwrm_set_pause_common(bp, req);
12283 
12284 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12285 	    bp->link_info.force_link_chng)
12286 		bnxt_hwrm_set_link_common(bp, req);
12287 
12288 	rc = hwrm_req_send(bp, req);
12289 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12290 		/* since changing of pause setting doesn't trigger any link
12291 		 * change event, the driver needs to update the current pause
12292 		 * result upon successfully return of the phy_cfg command
12293 		 */
12294 		bp->link_info.pause =
12295 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12296 		bp->link_info.auto_pause_setting = 0;
12297 		if (!bp->link_info.force_link_chng)
12298 			bnxt_report_link(bp);
12299 	}
12300 	bp->link_info.force_link_chng = false;
12301 	return rc;
12302 }
12303 
12304 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12305 			      struct hwrm_port_phy_cfg_input *req)
12306 {
12307 	struct ethtool_keee *eee = &bp->eee;
12308 
12309 	if (eee->eee_enabled) {
12310 		u16 eee_speeds;
12311 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12312 
12313 		if (eee->tx_lpi_enabled)
12314 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12315 		else
12316 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12317 
12318 		req->flags |= cpu_to_le32(flags);
12319 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12320 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12321 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12322 	} else {
12323 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12324 	}
12325 }
12326 
12327 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12328 {
12329 	struct hwrm_port_phy_cfg_input *req;
12330 	int rc;
12331 
12332 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12333 	if (rc)
12334 		return rc;
12335 
12336 	if (set_pause)
12337 		bnxt_hwrm_set_pause_common(bp, req);
12338 
12339 	bnxt_hwrm_set_link_common(bp, req);
12340 
12341 	if (set_eee)
12342 		bnxt_hwrm_set_eee(bp, req);
12343 	return hwrm_req_send(bp, req);
12344 }
12345 
12346 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12347 {
12348 	struct hwrm_port_phy_cfg_input *req;
12349 	int rc;
12350 
12351 	if (!BNXT_SINGLE_PF(bp))
12352 		return 0;
12353 
12354 	if (pci_num_vf(bp->pdev) &&
12355 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12356 		return 0;
12357 
12358 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12359 	if (rc)
12360 		return rc;
12361 
12362 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12363 	rc = hwrm_req_send(bp, req);
12364 	if (!rc) {
12365 		mutex_lock(&bp->link_lock);
12366 		/* Device is not obliged link down in certain scenarios, even
12367 		 * when forced. Setting the state unknown is consistent with
12368 		 * driver startup and will force link state to be reported
12369 		 * during subsequent open based on PORT_PHY_QCFG.
12370 		 */
12371 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12372 		mutex_unlock(&bp->link_lock);
12373 	}
12374 	return rc;
12375 }
12376 
12377 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12378 {
12379 #ifdef CONFIG_TEE_BNXT_FW
12380 	int rc = tee_bnxt_fw_load();
12381 
12382 	if (rc)
12383 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12384 
12385 	return rc;
12386 #else
12387 	netdev_err(bp->dev, "OP-TEE not supported\n");
12388 	return -ENODEV;
12389 #endif
12390 }
12391 
12392 static int bnxt_try_recover_fw(struct bnxt *bp)
12393 {
12394 	if (bp->fw_health && bp->fw_health->status_reliable) {
12395 		int retry = 0, rc;
12396 		u32 sts;
12397 
12398 		do {
12399 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12400 			rc = bnxt_hwrm_poll(bp);
12401 			if (!BNXT_FW_IS_BOOTING(sts) &&
12402 			    !BNXT_FW_IS_RECOVERING(sts))
12403 				break;
12404 			retry++;
12405 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12406 
12407 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12408 			netdev_err(bp->dev,
12409 				   "Firmware not responding, status: 0x%x\n",
12410 				   sts);
12411 			rc = -ENODEV;
12412 		}
12413 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12414 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12415 			return bnxt_fw_reset_via_optee(bp);
12416 		}
12417 		return rc;
12418 	}
12419 
12420 	return -ENODEV;
12421 }
12422 
12423 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12424 {
12425 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12426 
12427 	if (!BNXT_NEW_RM(bp))
12428 		return; /* no resource reservations required */
12429 
12430 	hw_resc->resv_cp_rings = 0;
12431 	hw_resc->resv_stat_ctxs = 0;
12432 	hw_resc->resv_irqs = 0;
12433 	hw_resc->resv_tx_rings = 0;
12434 	hw_resc->resv_rx_rings = 0;
12435 	hw_resc->resv_hw_ring_grps = 0;
12436 	hw_resc->resv_vnics = 0;
12437 	hw_resc->resv_rsscos_ctxs = 0;
12438 	if (!fw_reset) {
12439 		bp->tx_nr_rings = 0;
12440 		bp->rx_nr_rings = 0;
12441 	}
12442 }
12443 
12444 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12445 {
12446 	int rc;
12447 
12448 	if (!BNXT_NEW_RM(bp))
12449 		return 0; /* no resource reservations required */
12450 
12451 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12452 	if (rc)
12453 		netdev_err(bp->dev, "resc_qcaps failed\n");
12454 
12455 	bnxt_clear_reservations(bp, fw_reset);
12456 
12457 	return rc;
12458 }
12459 
12460 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12461 {
12462 	struct hwrm_func_drv_if_change_output *resp;
12463 	struct hwrm_func_drv_if_change_input *req;
12464 	bool resc_reinit = false;
12465 	bool caps_change = false;
12466 	int rc, retry = 0;
12467 	bool fw_reset;
12468 	u32 flags = 0;
12469 
12470 	fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT);
12471 	bp->fw_reset_state = 0;
12472 
12473 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12474 		return 0;
12475 
12476 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12477 	if (rc)
12478 		return rc;
12479 
12480 	if (up)
12481 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12482 	resp = hwrm_req_hold(bp, req);
12483 
12484 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12485 	while (retry < BNXT_FW_IF_RETRY) {
12486 		rc = hwrm_req_send(bp, req);
12487 		if (rc != -EAGAIN)
12488 			break;
12489 
12490 		msleep(50);
12491 		retry++;
12492 	}
12493 
12494 	if (rc == -EAGAIN) {
12495 		hwrm_req_drop(bp, req);
12496 		return rc;
12497 	} else if (!rc) {
12498 		flags = le32_to_cpu(resp->flags);
12499 	} else if (up) {
12500 		rc = bnxt_try_recover_fw(bp);
12501 		fw_reset = true;
12502 	}
12503 	hwrm_req_drop(bp, req);
12504 	if (rc)
12505 		return rc;
12506 
12507 	if (!up) {
12508 		bnxt_inv_fw_health_reg(bp);
12509 		return 0;
12510 	}
12511 
12512 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12513 		resc_reinit = true;
12514 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12515 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12516 		fw_reset = true;
12517 	else
12518 		bnxt_remap_fw_health_regs(bp);
12519 
12520 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12521 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12522 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12523 		return -ENODEV;
12524 	}
12525 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12526 		caps_change = true;
12527 
12528 	if (resc_reinit || fw_reset || caps_change) {
12529 		if (fw_reset || caps_change) {
12530 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12531 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12532 				bnxt_ulp_irq_stop(bp);
12533 			bnxt_free_ctx_mem(bp, false);
12534 			bnxt_dcb_free(bp);
12535 			rc = bnxt_fw_init_one(bp);
12536 			if (rc) {
12537 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12538 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12539 				return rc;
12540 			}
12541 			/* IRQ will be initialized later in bnxt_request_irq()*/
12542 			bnxt_clear_int_mode(bp);
12543 		}
12544 		rc = bnxt_cancel_reservations(bp, fw_reset);
12545 	}
12546 	return rc;
12547 }
12548 
12549 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12550 {
12551 	struct hwrm_port_led_qcaps_output *resp;
12552 	struct hwrm_port_led_qcaps_input *req;
12553 	struct bnxt_pf_info *pf = &bp->pf;
12554 	int rc;
12555 
12556 	bp->num_leds = 0;
12557 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12558 		return 0;
12559 
12560 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12561 	if (rc)
12562 		return rc;
12563 
12564 	req->port_id = cpu_to_le16(pf->port_id);
12565 	resp = hwrm_req_hold(bp, req);
12566 	rc = hwrm_req_send(bp, req);
12567 	if (rc) {
12568 		hwrm_req_drop(bp, req);
12569 		return rc;
12570 	}
12571 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12572 		int i;
12573 
12574 		bp->num_leds = resp->num_leds;
12575 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12576 						 bp->num_leds);
12577 		for (i = 0; i < bp->num_leds; i++) {
12578 			struct bnxt_led_info *led = &bp->leds[i];
12579 			__le16 caps = led->led_state_caps;
12580 
12581 			if (!led->led_group_id ||
12582 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12583 				bp->num_leds = 0;
12584 				break;
12585 			}
12586 		}
12587 	}
12588 	hwrm_req_drop(bp, req);
12589 	return 0;
12590 }
12591 
12592 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12593 {
12594 	struct hwrm_wol_filter_alloc_output *resp;
12595 	struct hwrm_wol_filter_alloc_input *req;
12596 	int rc;
12597 
12598 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12599 	if (rc)
12600 		return rc;
12601 
12602 	req->port_id = cpu_to_le16(bp->pf.port_id);
12603 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12604 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12605 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12606 
12607 	resp = hwrm_req_hold(bp, req);
12608 	rc = hwrm_req_send(bp, req);
12609 	if (!rc)
12610 		bp->wol_filter_id = resp->wol_filter_id;
12611 	hwrm_req_drop(bp, req);
12612 	return rc;
12613 }
12614 
12615 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12616 {
12617 	struct hwrm_wol_filter_free_input *req;
12618 	int rc;
12619 
12620 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12621 	if (rc)
12622 		return rc;
12623 
12624 	req->port_id = cpu_to_le16(bp->pf.port_id);
12625 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12626 	req->wol_filter_id = bp->wol_filter_id;
12627 
12628 	return hwrm_req_send(bp, req);
12629 }
12630 
12631 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12632 {
12633 	struct hwrm_wol_filter_qcfg_output *resp;
12634 	struct hwrm_wol_filter_qcfg_input *req;
12635 	u16 next_handle = 0;
12636 	int rc;
12637 
12638 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12639 	if (rc)
12640 		return rc;
12641 
12642 	req->port_id = cpu_to_le16(bp->pf.port_id);
12643 	req->handle = cpu_to_le16(handle);
12644 	resp = hwrm_req_hold(bp, req);
12645 	rc = hwrm_req_send(bp, req);
12646 	if (!rc) {
12647 		next_handle = le16_to_cpu(resp->next_handle);
12648 		if (next_handle != 0) {
12649 			if (resp->wol_type ==
12650 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12651 				bp->wol = 1;
12652 				bp->wol_filter_id = resp->wol_filter_id;
12653 			}
12654 		}
12655 	}
12656 	hwrm_req_drop(bp, req);
12657 	return next_handle;
12658 }
12659 
12660 static void bnxt_get_wol_settings(struct bnxt *bp)
12661 {
12662 	u16 handle = 0;
12663 
12664 	bp->wol = 0;
12665 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12666 		return;
12667 
12668 	do {
12669 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12670 	} while (handle && handle != 0xffff);
12671 }
12672 
12673 static bool bnxt_eee_config_ok(struct bnxt *bp)
12674 {
12675 	struct ethtool_keee *eee = &bp->eee;
12676 	struct bnxt_link_info *link_info = &bp->link_info;
12677 
12678 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12679 		return true;
12680 
12681 	if (eee->eee_enabled) {
12682 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12683 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12684 
12685 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12686 
12687 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12688 			eee->eee_enabled = 0;
12689 			return false;
12690 		}
12691 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12692 			linkmode_and(eee->advertised, advertising,
12693 				     eee->supported);
12694 			return false;
12695 		}
12696 	}
12697 	return true;
12698 }
12699 
12700 static int bnxt_update_phy_setting(struct bnxt *bp)
12701 {
12702 	int rc;
12703 	bool update_link = false;
12704 	bool update_pause = false;
12705 	bool update_eee = false;
12706 	struct bnxt_link_info *link_info = &bp->link_info;
12707 
12708 	rc = bnxt_update_link(bp, true);
12709 	if (rc) {
12710 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12711 			   rc);
12712 		return rc;
12713 	}
12714 	if (!BNXT_SINGLE_PF(bp))
12715 		return 0;
12716 
12717 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12718 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12719 	    link_info->req_flow_ctrl)
12720 		update_pause = true;
12721 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12722 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12723 		update_pause = true;
12724 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12725 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12726 			update_link = true;
12727 		if (bnxt_force_speed_updated(link_info))
12728 			update_link = true;
12729 		if (link_info->req_duplex != link_info->duplex_setting)
12730 			update_link = true;
12731 	} else {
12732 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12733 			update_link = true;
12734 		if (bnxt_auto_speed_updated(link_info))
12735 			update_link = true;
12736 	}
12737 
12738 	/* The last close may have shutdown the link, so need to call
12739 	 * PHY_CFG to bring it back up.
12740 	 */
12741 	if (!BNXT_LINK_IS_UP(bp))
12742 		update_link = true;
12743 
12744 	if (!bnxt_eee_config_ok(bp))
12745 		update_eee = true;
12746 
12747 	if (update_link)
12748 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12749 	else if (update_pause)
12750 		rc = bnxt_hwrm_set_pause(bp);
12751 	if (rc) {
12752 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12753 			   rc);
12754 		return rc;
12755 	}
12756 
12757 	return rc;
12758 }
12759 
12760 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12761 
12762 static int bnxt_reinit_after_abort(struct bnxt *bp)
12763 {
12764 	int rc;
12765 
12766 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12767 		return -EBUSY;
12768 
12769 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12770 		return -ENODEV;
12771 
12772 	rc = bnxt_fw_init_one(bp);
12773 	if (!rc) {
12774 		bnxt_clear_int_mode(bp);
12775 		rc = bnxt_init_int_mode(bp);
12776 		if (!rc) {
12777 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12778 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12779 		}
12780 	}
12781 	return rc;
12782 }
12783 
12784 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12785 {
12786 	struct bnxt_ntuple_filter *ntp_fltr;
12787 	struct bnxt_l2_filter *l2_fltr;
12788 
12789 	if (list_empty(&fltr->list))
12790 		return;
12791 
12792 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12793 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12794 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12795 		atomic_inc(&l2_fltr->refcnt);
12796 		ntp_fltr->l2_fltr = l2_fltr;
12797 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12798 			bnxt_del_ntp_filter(bp, ntp_fltr);
12799 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12800 				   fltr->sw_id);
12801 		}
12802 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12803 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12804 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12805 			bnxt_del_l2_filter(bp, l2_fltr);
12806 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12807 				   fltr->sw_id);
12808 		}
12809 	}
12810 }
12811 
12812 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12813 {
12814 	struct bnxt_filter_base *usr_fltr, *tmp;
12815 
12816 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12817 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12818 }
12819 
12820 static int bnxt_set_xps_mapping(struct bnxt *bp)
12821 {
12822 	int numa_node = dev_to_node(&bp->pdev->dev);
12823 	unsigned int q_idx, map_idx, cpu, i;
12824 	const struct cpumask *cpu_mask_ptr;
12825 	int nr_cpus = num_online_cpus();
12826 	cpumask_t *q_map;
12827 	int rc = 0;
12828 
12829 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12830 	if (!q_map)
12831 		return -ENOMEM;
12832 
12833 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12834 	 * Each TC has the same number of TX queues. The nth TX queue for each
12835 	 * TC will have the same CPU mask.
12836 	 */
12837 	for (i = 0; i < nr_cpus; i++) {
12838 		map_idx = i % bp->tx_nr_rings_per_tc;
12839 		cpu = cpumask_local_spread(i, numa_node);
12840 		cpu_mask_ptr = get_cpu_mask(cpu);
12841 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12842 	}
12843 
12844 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12845 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12846 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12847 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12848 		if (rc) {
12849 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12850 				    q_idx);
12851 			break;
12852 		}
12853 	}
12854 
12855 	kfree(q_map);
12856 
12857 	return rc;
12858 }
12859 
12860 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12861 {
12862 	int rc = 0;
12863 
12864 	netif_carrier_off(bp->dev);
12865 	if (irq_re_init) {
12866 		/* Reserve rings now if none were reserved at driver probe. */
12867 		rc = bnxt_init_dflt_ring_mode(bp);
12868 		if (rc) {
12869 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12870 			return rc;
12871 		}
12872 	}
12873 	rc = bnxt_reserve_rings(bp, irq_re_init);
12874 	if (rc)
12875 		return rc;
12876 
12877 	rc = bnxt_alloc_mem(bp, irq_re_init);
12878 	if (rc) {
12879 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12880 		goto open_err_free_mem;
12881 	}
12882 
12883 	if (irq_re_init) {
12884 		bnxt_init_napi(bp);
12885 		rc = bnxt_request_irq(bp);
12886 		if (rc) {
12887 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12888 			goto open_err_irq;
12889 		}
12890 	}
12891 
12892 	rc = bnxt_init_nic(bp, irq_re_init);
12893 	if (rc) {
12894 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12895 		goto open_err_irq;
12896 	}
12897 
12898 	bnxt_enable_napi(bp);
12899 	bnxt_debug_dev_init(bp);
12900 
12901 	if (link_re_init) {
12902 		mutex_lock(&bp->link_lock);
12903 		rc = bnxt_update_phy_setting(bp);
12904 		mutex_unlock(&bp->link_lock);
12905 		if (rc) {
12906 			netdev_warn(bp->dev, "failed to update phy settings\n");
12907 			if (BNXT_SINGLE_PF(bp)) {
12908 				bp->link_info.phy_retry = true;
12909 				bp->link_info.phy_retry_expires =
12910 					jiffies + 5 * HZ;
12911 			}
12912 		}
12913 	}
12914 
12915 	if (irq_re_init) {
12916 		udp_tunnel_nic_reset_ntf(bp->dev);
12917 		rc = bnxt_set_xps_mapping(bp);
12918 		if (rc)
12919 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12920 	}
12921 
12922 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12923 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12924 			static_branch_enable(&bnxt_xdp_locking_key);
12925 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12926 		static_branch_disable(&bnxt_xdp_locking_key);
12927 	}
12928 	set_bit(BNXT_STATE_OPEN, &bp->state);
12929 	bnxt_enable_int(bp);
12930 	/* Enable TX queues */
12931 	bnxt_tx_enable(bp);
12932 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12933 	/* Poll link status and check for SFP+ module status */
12934 	mutex_lock(&bp->link_lock);
12935 	bnxt_get_port_module_status(bp);
12936 	mutex_unlock(&bp->link_lock);
12937 
12938 	/* VF-reps may need to be re-opened after the PF is re-opened */
12939 	if (BNXT_PF(bp))
12940 		bnxt_vf_reps_open(bp);
12941 	bnxt_ptp_init_rtc(bp, true);
12942 	bnxt_ptp_cfg_tstamp_filters(bp);
12943 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12944 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12945 	bnxt_cfg_usr_fltrs(bp);
12946 	return 0;
12947 
12948 open_err_irq:
12949 	bnxt_del_napi(bp);
12950 
12951 open_err_free_mem:
12952 	bnxt_free_skbs(bp);
12953 	bnxt_free_irq(bp);
12954 	bnxt_free_mem(bp, true);
12955 	return rc;
12956 }
12957 
12958 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12959 {
12960 	int rc = 0;
12961 
12962 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12963 		rc = -EIO;
12964 	if (!rc)
12965 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12966 	if (rc) {
12967 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12968 		netif_close(bp->dev);
12969 	}
12970 	return rc;
12971 }
12972 
12973 /* netdev instance lock held, open the NIC half way by allocating all
12974  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
12975  * for offline self tests.
12976  */
12977 int bnxt_half_open_nic(struct bnxt *bp)
12978 {
12979 	int rc = 0;
12980 
12981 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12982 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12983 		rc = -ENODEV;
12984 		goto half_open_err;
12985 	}
12986 
12987 	rc = bnxt_alloc_mem(bp, true);
12988 	if (rc) {
12989 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12990 		goto half_open_err;
12991 	}
12992 	bnxt_init_napi(bp);
12993 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12994 	rc = bnxt_init_nic(bp, true);
12995 	if (rc) {
12996 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12997 		bnxt_del_napi(bp);
12998 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12999 		goto half_open_err;
13000 	}
13001 	return 0;
13002 
13003 half_open_err:
13004 	bnxt_free_skbs(bp);
13005 	bnxt_free_mem(bp, true);
13006 	netif_close(bp->dev);
13007 	return rc;
13008 }
13009 
13010 /* netdev instance lock held, this call can only be made after a previous
13011  * successful call to bnxt_half_open_nic().
13012  */
13013 void bnxt_half_close_nic(struct bnxt *bp)
13014 {
13015 	bnxt_hwrm_resource_free(bp, false, true);
13016 	bnxt_del_napi(bp);
13017 	bnxt_free_skbs(bp);
13018 	bnxt_free_mem(bp, true);
13019 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13020 }
13021 
13022 void bnxt_reenable_sriov(struct bnxt *bp)
13023 {
13024 	if (BNXT_PF(bp)) {
13025 		struct bnxt_pf_info *pf = &bp->pf;
13026 		int n = pf->active_vfs;
13027 
13028 		if (n)
13029 			bnxt_cfg_hw_sriov(bp, &n, true);
13030 	}
13031 }
13032 
13033 static int bnxt_open(struct net_device *dev)
13034 {
13035 	struct bnxt *bp = netdev_priv(dev);
13036 	int rc;
13037 
13038 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13039 		rc = bnxt_reinit_after_abort(bp);
13040 		if (rc) {
13041 			if (rc == -EBUSY)
13042 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
13043 			else
13044 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
13045 			return -ENODEV;
13046 		}
13047 	}
13048 
13049 	rc = bnxt_hwrm_if_change(bp, true);
13050 	if (rc)
13051 		return rc;
13052 
13053 	rc = __bnxt_open_nic(bp, true, true);
13054 	if (rc) {
13055 		bnxt_hwrm_if_change(bp, false);
13056 	} else {
13057 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
13058 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13059 				bnxt_queue_sp_work(bp,
13060 						   BNXT_RESTART_ULP_SP_EVENT);
13061 		}
13062 	}
13063 
13064 	return rc;
13065 }
13066 
13067 static bool bnxt_drv_busy(struct bnxt *bp)
13068 {
13069 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
13070 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
13071 }
13072 
13073 static void bnxt_get_ring_stats(struct bnxt *bp,
13074 				struct rtnl_link_stats64 *stats);
13075 
13076 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
13077 			     bool link_re_init)
13078 {
13079 	/* Close the VF-reps before closing PF */
13080 	if (BNXT_PF(bp))
13081 		bnxt_vf_reps_close(bp);
13082 
13083 	/* Change device state to avoid TX queue wake up's */
13084 	bnxt_tx_disable(bp);
13085 
13086 	clear_bit(BNXT_STATE_OPEN, &bp->state);
13087 	smp_mb__after_atomic();
13088 	while (bnxt_drv_busy(bp))
13089 		msleep(20);
13090 
13091 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13092 		bnxt_clear_rss_ctxs(bp);
13093 	/* Flush rings and disable interrupts */
13094 	bnxt_shutdown_nic(bp, irq_re_init);
13095 
13096 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
13097 
13098 	bnxt_debug_dev_exit(bp);
13099 	bnxt_disable_napi(bp);
13100 	timer_delete_sync(&bp->timer);
13101 	bnxt_free_skbs(bp);
13102 
13103 	/* Save ring stats before shutdown */
13104 	if (bp->bnapi && irq_re_init) {
13105 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
13106 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
13107 	}
13108 	if (irq_re_init) {
13109 		bnxt_free_irq(bp);
13110 		bnxt_del_napi(bp);
13111 	}
13112 	bnxt_free_mem(bp, irq_re_init);
13113 }
13114 
13115 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13116 {
13117 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13118 		/* If we get here, it means firmware reset is in progress
13119 		 * while we are trying to close.  We can safely proceed with
13120 		 * the close because we are holding netdev instance lock.
13121 		 * Some firmware messages may fail as we proceed to close.
13122 		 * We set the ABORT_ERR flag here so that the FW reset thread
13123 		 * will later abort when it gets the netdev instance lock
13124 		 * and sees the flag.
13125 		 */
13126 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
13127 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13128 	}
13129 
13130 #ifdef CONFIG_BNXT_SRIOV
13131 	if (bp->sriov_cfg) {
13132 		int rc;
13133 
13134 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
13135 						      !bp->sriov_cfg,
13136 						      BNXT_SRIOV_CFG_WAIT_TMO);
13137 		if (!rc)
13138 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13139 		else if (rc < 0)
13140 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13141 	}
13142 #endif
13143 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13144 }
13145 
13146 static int bnxt_close(struct net_device *dev)
13147 {
13148 	struct bnxt *bp = netdev_priv(dev);
13149 
13150 	bnxt_close_nic(bp, true, true);
13151 	bnxt_hwrm_shutdown_link(bp);
13152 	bnxt_hwrm_if_change(bp, false);
13153 	return 0;
13154 }
13155 
13156 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13157 				   u16 *val)
13158 {
13159 	struct hwrm_port_phy_mdio_read_output *resp;
13160 	struct hwrm_port_phy_mdio_read_input *req;
13161 	int rc;
13162 
13163 	if (bp->hwrm_spec_code < 0x10a00)
13164 		return -EOPNOTSUPP;
13165 
13166 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13167 	if (rc)
13168 		return rc;
13169 
13170 	req->port_id = cpu_to_le16(bp->pf.port_id);
13171 	req->phy_addr = phy_addr;
13172 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13173 	if (mdio_phy_id_is_c45(phy_addr)) {
13174 		req->cl45_mdio = 1;
13175 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13176 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13177 		req->reg_addr = cpu_to_le16(reg);
13178 	}
13179 
13180 	resp = hwrm_req_hold(bp, req);
13181 	rc = hwrm_req_send(bp, req);
13182 	if (!rc)
13183 		*val = le16_to_cpu(resp->reg_data);
13184 	hwrm_req_drop(bp, req);
13185 	return rc;
13186 }
13187 
13188 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13189 				    u16 val)
13190 {
13191 	struct hwrm_port_phy_mdio_write_input *req;
13192 	int rc;
13193 
13194 	if (bp->hwrm_spec_code < 0x10a00)
13195 		return -EOPNOTSUPP;
13196 
13197 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13198 	if (rc)
13199 		return rc;
13200 
13201 	req->port_id = cpu_to_le16(bp->pf.port_id);
13202 	req->phy_addr = phy_addr;
13203 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13204 	if (mdio_phy_id_is_c45(phy_addr)) {
13205 		req->cl45_mdio = 1;
13206 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13207 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13208 		req->reg_addr = cpu_to_le16(reg);
13209 	}
13210 	req->reg_data = cpu_to_le16(val);
13211 
13212 	return hwrm_req_send(bp, req);
13213 }
13214 
13215 /* netdev instance lock held */
13216 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13217 {
13218 	struct mii_ioctl_data *mdio = if_mii(ifr);
13219 	struct bnxt *bp = netdev_priv(dev);
13220 	int rc;
13221 
13222 	switch (cmd) {
13223 	case SIOCGMIIPHY:
13224 		mdio->phy_id = bp->link_info.phy_addr;
13225 
13226 		fallthrough;
13227 	case SIOCGMIIREG: {
13228 		u16 mii_regval = 0;
13229 
13230 		if (!netif_running(dev))
13231 			return -EAGAIN;
13232 
13233 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13234 					     &mii_regval);
13235 		mdio->val_out = mii_regval;
13236 		return rc;
13237 	}
13238 
13239 	case SIOCSMIIREG:
13240 		if (!netif_running(dev))
13241 			return -EAGAIN;
13242 
13243 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13244 						mdio->val_in);
13245 
13246 	case SIOCSHWTSTAMP:
13247 		return bnxt_hwtstamp_set(dev, ifr);
13248 
13249 	case SIOCGHWTSTAMP:
13250 		return bnxt_hwtstamp_get(dev, ifr);
13251 
13252 	default:
13253 		/* do nothing */
13254 		break;
13255 	}
13256 	return -EOPNOTSUPP;
13257 }
13258 
13259 static void bnxt_get_ring_stats(struct bnxt *bp,
13260 				struct rtnl_link_stats64 *stats)
13261 {
13262 	int i;
13263 
13264 	for (i = 0; i < bp->cp_nr_rings; i++) {
13265 		struct bnxt_napi *bnapi = bp->bnapi[i];
13266 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13267 		u64 *sw = cpr->stats.sw_stats;
13268 
13269 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13270 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13271 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13272 
13273 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13274 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13275 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13276 
13277 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13278 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13279 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13280 
13281 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13282 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13283 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13284 
13285 		stats->rx_missed_errors +=
13286 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13287 
13288 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13289 
13290 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13291 
13292 		stats->rx_dropped +=
13293 			cpr->sw_stats->rx.rx_netpoll_discards +
13294 			cpr->sw_stats->rx.rx_oom_discards;
13295 	}
13296 }
13297 
13298 static void bnxt_add_prev_stats(struct bnxt *bp,
13299 				struct rtnl_link_stats64 *stats)
13300 {
13301 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13302 
13303 	stats->rx_packets += prev_stats->rx_packets;
13304 	stats->tx_packets += prev_stats->tx_packets;
13305 	stats->rx_bytes += prev_stats->rx_bytes;
13306 	stats->tx_bytes += prev_stats->tx_bytes;
13307 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13308 	stats->multicast += prev_stats->multicast;
13309 	stats->rx_dropped += prev_stats->rx_dropped;
13310 	stats->tx_dropped += prev_stats->tx_dropped;
13311 }
13312 
13313 static void
13314 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13315 {
13316 	struct bnxt *bp = netdev_priv(dev);
13317 
13318 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13319 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13320 	 * we check the BNXT_STATE_OPEN flag.
13321 	 */
13322 	smp_mb__after_atomic();
13323 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13324 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13325 		*stats = bp->net_stats_prev;
13326 		return;
13327 	}
13328 
13329 	bnxt_get_ring_stats(bp, stats);
13330 	bnxt_add_prev_stats(bp, stats);
13331 
13332 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13333 		u64 *rx = bp->port_stats.sw_stats;
13334 		u64 *tx = bp->port_stats.sw_stats +
13335 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13336 
13337 		stats->rx_crc_errors =
13338 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13339 		stats->rx_frame_errors =
13340 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13341 		stats->rx_length_errors =
13342 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13343 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13344 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13345 		stats->rx_errors =
13346 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13347 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13348 		stats->collisions =
13349 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13350 		stats->tx_fifo_errors =
13351 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13352 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13353 	}
13354 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13355 }
13356 
13357 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
13358 					struct bnxt_total_ring_err_stats *stats,
13359 					struct bnxt_cp_ring_info *cpr)
13360 {
13361 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13362 	u64 *hw_stats = cpr->stats.sw_stats;
13363 
13364 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13365 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13366 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13367 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13368 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13369 	stats->rx_total_ring_discards +=
13370 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13371 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13372 	stats->tx_total_ring_discards +=
13373 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13374 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13375 }
13376 
13377 void bnxt_get_ring_err_stats(struct bnxt *bp,
13378 			     struct bnxt_total_ring_err_stats *stats)
13379 {
13380 	int i;
13381 
13382 	for (i = 0; i < bp->cp_nr_rings; i++)
13383 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13384 }
13385 
13386 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
13387 {
13388 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13389 	struct net_device *dev = bp->dev;
13390 	struct netdev_hw_addr *ha;
13391 	u8 *haddr;
13392 	int mc_count = 0;
13393 	bool update = false;
13394 	int off = 0;
13395 
13396 	netdev_for_each_mc_addr(ha, dev) {
13397 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13398 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13399 			vnic->mc_list_count = 0;
13400 			return false;
13401 		}
13402 		haddr = ha->addr;
13403 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13404 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13405 			update = true;
13406 		}
13407 		off += ETH_ALEN;
13408 		mc_count++;
13409 	}
13410 	if (mc_count)
13411 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13412 
13413 	if (mc_count != vnic->mc_list_count) {
13414 		vnic->mc_list_count = mc_count;
13415 		update = true;
13416 	}
13417 	return update;
13418 }
13419 
13420 static bool bnxt_uc_list_updated(struct bnxt *bp)
13421 {
13422 	struct net_device *dev = bp->dev;
13423 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13424 	struct netdev_hw_addr *ha;
13425 	int off = 0;
13426 
13427 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
13428 		return true;
13429 
13430 	netdev_for_each_uc_addr(ha, dev) {
13431 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13432 			return true;
13433 
13434 		off += ETH_ALEN;
13435 	}
13436 	return false;
13437 }
13438 
13439 static void bnxt_set_rx_mode(struct net_device *dev)
13440 {
13441 	struct bnxt *bp = netdev_priv(dev);
13442 	struct bnxt_vnic_info *vnic;
13443 	bool mc_update = false;
13444 	bool uc_update;
13445 	u32 mask;
13446 
13447 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13448 		return;
13449 
13450 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13451 	mask = vnic->rx_mask;
13452 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13453 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13454 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13455 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13456 
13457 	if (dev->flags & IFF_PROMISC)
13458 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13459 
13460 	uc_update = bnxt_uc_list_updated(bp);
13461 
13462 	if (dev->flags & IFF_BROADCAST)
13463 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13464 	if (dev->flags & IFF_ALLMULTI) {
13465 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13466 		vnic->mc_list_count = 0;
13467 	} else if (dev->flags & IFF_MULTICAST) {
13468 		mc_update = bnxt_mc_list_updated(bp, &mask);
13469 	}
13470 
13471 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13472 		vnic->rx_mask = mask;
13473 
13474 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13475 	}
13476 }
13477 
13478 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13479 {
13480 	struct net_device *dev = bp->dev;
13481 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13482 	struct netdev_hw_addr *ha;
13483 	int i, off = 0, rc;
13484 	bool uc_update;
13485 
13486 	netif_addr_lock_bh(dev);
13487 	uc_update = bnxt_uc_list_updated(bp);
13488 	netif_addr_unlock_bh(dev);
13489 
13490 	if (!uc_update)
13491 		goto skip_uc;
13492 
13493 	for (i = 1; i < vnic->uc_filter_count; i++) {
13494 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13495 
13496 		bnxt_hwrm_l2_filter_free(bp, fltr);
13497 		bnxt_del_l2_filter(bp, fltr);
13498 	}
13499 
13500 	vnic->uc_filter_count = 1;
13501 
13502 	netif_addr_lock_bh(dev);
13503 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13504 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13505 	} else {
13506 		netdev_for_each_uc_addr(ha, dev) {
13507 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13508 			off += ETH_ALEN;
13509 			vnic->uc_filter_count++;
13510 		}
13511 	}
13512 	netif_addr_unlock_bh(dev);
13513 
13514 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13515 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13516 		if (rc) {
13517 			if (BNXT_VF(bp) && rc == -ENODEV) {
13518 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13519 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13520 				else
13521 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13522 				rc = 0;
13523 			} else {
13524 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13525 			}
13526 			vnic->uc_filter_count = i;
13527 			return rc;
13528 		}
13529 	}
13530 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13531 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13532 
13533 skip_uc:
13534 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13535 	    !bnxt_promisc_ok(bp))
13536 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13537 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13538 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13539 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13540 			    rc);
13541 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13542 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13543 		vnic->mc_list_count = 0;
13544 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13545 	}
13546 	if (rc)
13547 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13548 			   rc);
13549 
13550 	return rc;
13551 }
13552 
13553 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13554 {
13555 #ifdef CONFIG_BNXT_SRIOV
13556 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13557 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13558 
13559 		/* No minimum rings were provisioned by the PF.  Don't
13560 		 * reserve rings by default when device is down.
13561 		 */
13562 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13563 			return true;
13564 
13565 		if (!netif_running(bp->dev))
13566 			return false;
13567 	}
13568 #endif
13569 	return true;
13570 }
13571 
13572 /* If the chip and firmware supports RFS */
13573 static bool bnxt_rfs_supported(struct bnxt *bp)
13574 {
13575 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13576 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13577 			return true;
13578 		return false;
13579 	}
13580 	/* 212 firmware is broken for aRFS */
13581 	if (BNXT_FW_MAJ(bp) == 212)
13582 		return false;
13583 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13584 		return true;
13585 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13586 		return true;
13587 	return false;
13588 }
13589 
13590 /* If runtime conditions support RFS */
13591 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13592 {
13593 	struct bnxt_hw_rings hwr = {0};
13594 	int max_vnics, max_rss_ctxs;
13595 
13596 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13597 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13598 		return bnxt_rfs_supported(bp);
13599 
13600 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13601 		return false;
13602 
13603 	hwr.grp = bp->rx_nr_rings;
13604 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13605 	if (new_rss_ctx)
13606 		hwr.vnic++;
13607 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13608 	max_vnics = bnxt_get_max_func_vnics(bp);
13609 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13610 
13611 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13612 		if (bp->rx_nr_rings > 1)
13613 			netdev_warn(bp->dev,
13614 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13615 				    min(max_rss_ctxs - 1, max_vnics - 1));
13616 		return false;
13617 	}
13618 
13619 	if (!BNXT_NEW_RM(bp))
13620 		return true;
13621 
13622 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13623 	 * issue that will mess up the default VNIC if we reduce the
13624 	 * reservations.
13625 	 */
13626 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13627 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13628 		return true;
13629 
13630 	bnxt_hwrm_reserve_rings(bp, &hwr);
13631 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13632 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13633 		return true;
13634 
13635 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13636 	hwr.vnic = 1;
13637 	hwr.rss_ctx = 0;
13638 	bnxt_hwrm_reserve_rings(bp, &hwr);
13639 	return false;
13640 }
13641 
13642 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13643 					   netdev_features_t features)
13644 {
13645 	struct bnxt *bp = netdev_priv(dev);
13646 	netdev_features_t vlan_features;
13647 
13648 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13649 		features &= ~NETIF_F_NTUPLE;
13650 
13651 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13652 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13653 
13654 	if (!(features & NETIF_F_GRO))
13655 		features &= ~NETIF_F_GRO_HW;
13656 
13657 	if (features & NETIF_F_GRO_HW)
13658 		features &= ~NETIF_F_LRO;
13659 
13660 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13661 	 * turned on or off together.
13662 	 */
13663 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13664 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13665 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13666 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13667 		else if (vlan_features)
13668 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13669 	}
13670 #ifdef CONFIG_BNXT_SRIOV
13671 	if (BNXT_VF(bp) && bp->vf.vlan)
13672 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13673 #endif
13674 	return features;
13675 }
13676 
13677 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13678 				bool link_re_init, u32 flags, bool update_tpa)
13679 {
13680 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13681 	bp->flags = flags;
13682 	if (update_tpa)
13683 		bnxt_set_ring_params(bp);
13684 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13685 }
13686 
13687 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13688 {
13689 	bool update_tpa = false, update_ntuple = false;
13690 	struct bnxt *bp = netdev_priv(dev);
13691 	u32 flags = bp->flags;
13692 	u32 changes;
13693 	int rc = 0;
13694 	bool re_init = false;
13695 
13696 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13697 	if (features & NETIF_F_GRO_HW)
13698 		flags |= BNXT_FLAG_GRO;
13699 	else if (features & NETIF_F_LRO)
13700 		flags |= BNXT_FLAG_LRO;
13701 
13702 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13703 		flags &= ~BNXT_FLAG_TPA;
13704 
13705 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13706 		flags |= BNXT_FLAG_STRIP_VLAN;
13707 
13708 	if (features & NETIF_F_NTUPLE)
13709 		flags |= BNXT_FLAG_RFS;
13710 	else
13711 		bnxt_clear_usr_fltrs(bp, true);
13712 
13713 	changes = flags ^ bp->flags;
13714 	if (changes & BNXT_FLAG_TPA) {
13715 		update_tpa = true;
13716 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13717 		    (flags & BNXT_FLAG_TPA) == 0 ||
13718 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13719 			re_init = true;
13720 	}
13721 
13722 	if (changes & ~BNXT_FLAG_TPA)
13723 		re_init = true;
13724 
13725 	if (changes & BNXT_FLAG_RFS)
13726 		update_ntuple = true;
13727 
13728 	if (flags != bp->flags) {
13729 		u32 old_flags = bp->flags;
13730 
13731 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13732 			bp->flags = flags;
13733 			if (update_tpa)
13734 				bnxt_set_ring_params(bp);
13735 			return rc;
13736 		}
13737 
13738 		if (update_ntuple)
13739 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13740 
13741 		if (re_init)
13742 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13743 
13744 		if (update_tpa) {
13745 			bp->flags = flags;
13746 			rc = bnxt_set_tpa(bp,
13747 					  (flags & BNXT_FLAG_TPA) ?
13748 					  true : false);
13749 			if (rc)
13750 				bp->flags = old_flags;
13751 		}
13752 	}
13753 	return rc;
13754 }
13755 
13756 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13757 			      u8 **nextp)
13758 {
13759 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13760 	struct hop_jumbo_hdr *jhdr;
13761 	int hdr_count = 0;
13762 	u8 *nexthdr;
13763 	int start;
13764 
13765 	/* Check that there are at most 2 IPv6 extension headers, no
13766 	 * fragment header, and each is <= 64 bytes.
13767 	 */
13768 	start = nw_off + sizeof(*ip6h);
13769 	nexthdr = &ip6h->nexthdr;
13770 	while (ipv6_ext_hdr(*nexthdr)) {
13771 		struct ipv6_opt_hdr *hp;
13772 		int hdrlen;
13773 
13774 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13775 		    *nexthdr == NEXTHDR_FRAGMENT)
13776 			return false;
13777 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13778 					  skb_headlen(skb), NULL);
13779 		if (!hp)
13780 			return false;
13781 		if (*nexthdr == NEXTHDR_AUTH)
13782 			hdrlen = ipv6_authlen(hp);
13783 		else
13784 			hdrlen = ipv6_optlen(hp);
13785 
13786 		if (hdrlen > 64)
13787 			return false;
13788 
13789 		/* The ext header may be a hop-by-hop header inserted for
13790 		 * big TCP purposes. This will be removed before sending
13791 		 * from NIC, so do not count it.
13792 		 */
13793 		if (*nexthdr == NEXTHDR_HOP) {
13794 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13795 				goto increment_hdr;
13796 
13797 			jhdr = (struct hop_jumbo_hdr *)hp;
13798 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13799 			    jhdr->nexthdr != IPPROTO_TCP)
13800 				goto increment_hdr;
13801 
13802 			goto next_hdr;
13803 		}
13804 increment_hdr:
13805 		hdr_count++;
13806 next_hdr:
13807 		nexthdr = &hp->nexthdr;
13808 		start += hdrlen;
13809 	}
13810 	if (nextp) {
13811 		/* Caller will check inner protocol */
13812 		if (skb->encapsulation) {
13813 			*nextp = nexthdr;
13814 			return true;
13815 		}
13816 		*nextp = NULL;
13817 	}
13818 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13819 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13820 }
13821 
13822 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13823 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13824 {
13825 	struct udphdr *uh = udp_hdr(skb);
13826 	__be16 udp_port = uh->dest;
13827 
13828 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13829 	    udp_port != bp->vxlan_gpe_port)
13830 		return false;
13831 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13832 		struct ethhdr *eh = inner_eth_hdr(skb);
13833 
13834 		switch (eh->h_proto) {
13835 		case htons(ETH_P_IP):
13836 			return true;
13837 		case htons(ETH_P_IPV6):
13838 			return bnxt_exthdr_check(bp, skb,
13839 						 skb_inner_network_offset(skb),
13840 						 NULL);
13841 		}
13842 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13843 		return true;
13844 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13845 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13846 					 NULL);
13847 	}
13848 	return false;
13849 }
13850 
13851 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13852 {
13853 	switch (l4_proto) {
13854 	case IPPROTO_UDP:
13855 		return bnxt_udp_tunl_check(bp, skb);
13856 	case IPPROTO_IPIP:
13857 		return true;
13858 	case IPPROTO_GRE: {
13859 		switch (skb->inner_protocol) {
13860 		default:
13861 			return false;
13862 		case htons(ETH_P_IP):
13863 			return true;
13864 		case htons(ETH_P_IPV6):
13865 			fallthrough;
13866 		}
13867 	}
13868 	case IPPROTO_IPV6:
13869 		/* Check ext headers of inner ipv6 */
13870 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13871 					 NULL);
13872 	}
13873 	return false;
13874 }
13875 
13876 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13877 					     struct net_device *dev,
13878 					     netdev_features_t features)
13879 {
13880 	struct bnxt *bp = netdev_priv(dev);
13881 	u8 *l4_proto;
13882 
13883 	features = vlan_features_check(skb, features);
13884 	switch (vlan_get_protocol(skb)) {
13885 	case htons(ETH_P_IP):
13886 		if (!skb->encapsulation)
13887 			return features;
13888 		l4_proto = &ip_hdr(skb)->protocol;
13889 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13890 			return features;
13891 		break;
13892 	case htons(ETH_P_IPV6):
13893 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13894 				       &l4_proto))
13895 			break;
13896 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13897 			return features;
13898 		break;
13899 	}
13900 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13901 }
13902 
13903 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13904 			 u32 *reg_buf)
13905 {
13906 	struct hwrm_dbg_read_direct_output *resp;
13907 	struct hwrm_dbg_read_direct_input *req;
13908 	__le32 *dbg_reg_buf;
13909 	dma_addr_t mapping;
13910 	int rc, i;
13911 
13912 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13913 	if (rc)
13914 		return rc;
13915 
13916 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13917 					 &mapping);
13918 	if (!dbg_reg_buf) {
13919 		rc = -ENOMEM;
13920 		goto dbg_rd_reg_exit;
13921 	}
13922 
13923 	req->host_dest_addr = cpu_to_le64(mapping);
13924 
13925 	resp = hwrm_req_hold(bp, req);
13926 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13927 	req->read_len32 = cpu_to_le32(num_words);
13928 
13929 	rc = hwrm_req_send(bp, req);
13930 	if (rc || resp->error_code) {
13931 		rc = -EIO;
13932 		goto dbg_rd_reg_exit;
13933 	}
13934 	for (i = 0; i < num_words; i++)
13935 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13936 
13937 dbg_rd_reg_exit:
13938 	hwrm_req_drop(bp, req);
13939 	return rc;
13940 }
13941 
13942 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13943 				       u32 ring_id, u32 *prod, u32 *cons)
13944 {
13945 	struct hwrm_dbg_ring_info_get_output *resp;
13946 	struct hwrm_dbg_ring_info_get_input *req;
13947 	int rc;
13948 
13949 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13950 	if (rc)
13951 		return rc;
13952 
13953 	req->ring_type = ring_type;
13954 	req->fw_ring_id = cpu_to_le32(ring_id);
13955 	resp = hwrm_req_hold(bp, req);
13956 	rc = hwrm_req_send(bp, req);
13957 	if (!rc) {
13958 		*prod = le32_to_cpu(resp->producer_index);
13959 		*cons = le32_to_cpu(resp->consumer_index);
13960 	}
13961 	hwrm_req_drop(bp, req);
13962 	return rc;
13963 }
13964 
13965 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13966 {
13967 	struct bnxt_tx_ring_info *txr;
13968 	int i = bnapi->index, j;
13969 
13970 	bnxt_for_each_napi_tx(j, bnapi, txr)
13971 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13972 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13973 			    txr->tx_cons);
13974 }
13975 
13976 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13977 {
13978 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13979 	int i = bnapi->index;
13980 
13981 	if (!rxr)
13982 		return;
13983 
13984 	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",
13985 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13986 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13987 		    rxr->rx_sw_agg_prod);
13988 }
13989 
13990 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13991 {
13992 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13993 	int i = bnapi->index;
13994 
13995 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13996 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13997 }
13998 
13999 static void bnxt_dbg_dump_states(struct bnxt *bp)
14000 {
14001 	int i;
14002 	struct bnxt_napi *bnapi;
14003 
14004 	for (i = 0; i < bp->cp_nr_rings; i++) {
14005 		bnapi = bp->bnapi[i];
14006 		if (netif_msg_drv(bp)) {
14007 			bnxt_dump_tx_sw_state(bnapi);
14008 			bnxt_dump_rx_sw_state(bnapi);
14009 			bnxt_dump_cp_sw_state(bnapi);
14010 		}
14011 	}
14012 }
14013 
14014 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
14015 {
14016 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
14017 	struct hwrm_ring_reset_input *req;
14018 	struct bnxt_napi *bnapi = rxr->bnapi;
14019 	struct bnxt_cp_ring_info *cpr;
14020 	u16 cp_ring_id;
14021 	int rc;
14022 
14023 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
14024 	if (rc)
14025 		return rc;
14026 
14027 	cpr = &bnapi->cp_ring;
14028 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
14029 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
14030 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
14031 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
14032 	return hwrm_req_send_silent(bp, req);
14033 }
14034 
14035 static void bnxt_reset_task(struct bnxt *bp, bool silent)
14036 {
14037 	if (!silent)
14038 		bnxt_dbg_dump_states(bp);
14039 	if (netif_running(bp->dev)) {
14040 		bnxt_close_nic(bp, !silent, false);
14041 		bnxt_open_nic(bp, !silent, false);
14042 	}
14043 }
14044 
14045 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
14046 {
14047 	struct bnxt *bp = netdev_priv(dev);
14048 
14049 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
14050 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
14051 }
14052 
14053 static void bnxt_fw_health_check(struct bnxt *bp)
14054 {
14055 	struct bnxt_fw_health *fw_health = bp->fw_health;
14056 	struct pci_dev *pdev = bp->pdev;
14057 	u32 val;
14058 
14059 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14060 		return;
14061 
14062 	/* Make sure it is enabled before checking the tmr_counter. */
14063 	smp_rmb();
14064 	if (fw_health->tmr_counter) {
14065 		fw_health->tmr_counter--;
14066 		return;
14067 	}
14068 
14069 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14070 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
14071 		fw_health->arrests++;
14072 		goto fw_reset;
14073 	}
14074 
14075 	fw_health->last_fw_heartbeat = val;
14076 
14077 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14078 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
14079 		fw_health->discoveries++;
14080 		goto fw_reset;
14081 	}
14082 
14083 	fw_health->tmr_counter = fw_health->tmr_multiplier;
14084 	return;
14085 
14086 fw_reset:
14087 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
14088 }
14089 
14090 static void bnxt_timer(struct timer_list *t)
14091 {
14092 	struct bnxt *bp = timer_container_of(bp, t, timer);
14093 	struct net_device *dev = bp->dev;
14094 
14095 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
14096 		return;
14097 
14098 	if (atomic_read(&bp->intr_sem) != 0)
14099 		goto bnxt_restart_timer;
14100 
14101 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
14102 		bnxt_fw_health_check(bp);
14103 
14104 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
14105 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
14106 
14107 	if (bnxt_tc_flower_enabled(bp))
14108 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
14109 
14110 #ifdef CONFIG_RFS_ACCEL
14111 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
14112 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14113 #endif /*CONFIG_RFS_ACCEL*/
14114 
14115 	if (bp->link_info.phy_retry) {
14116 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
14117 			bp->link_info.phy_retry = false;
14118 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
14119 		} else {
14120 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
14121 		}
14122 	}
14123 
14124 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
14125 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
14126 
14127 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
14128 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
14129 
14130 bnxt_restart_timer:
14131 	mod_timer(&bp->timer, jiffies + bp->current_interval);
14132 }
14133 
14134 static void bnxt_lock_sp(struct bnxt *bp)
14135 {
14136 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
14137 	 * set.  If the device is being closed, bnxt_close() may be holding
14138 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14139 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14140 	 * instance lock.
14141 	 */
14142 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14143 	netdev_lock(bp->dev);
14144 }
14145 
14146 static void bnxt_unlock_sp(struct bnxt *bp)
14147 {
14148 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14149 	netdev_unlock(bp->dev);
14150 }
14151 
14152 /* Only called from bnxt_sp_task() */
14153 static void bnxt_reset(struct bnxt *bp, bool silent)
14154 {
14155 	bnxt_lock_sp(bp);
14156 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14157 		bnxt_reset_task(bp, silent);
14158 	bnxt_unlock_sp(bp);
14159 }
14160 
14161 /* Only called from bnxt_sp_task() */
14162 static void bnxt_rx_ring_reset(struct bnxt *bp)
14163 {
14164 	int i;
14165 
14166 	bnxt_lock_sp(bp);
14167 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14168 		bnxt_unlock_sp(bp);
14169 		return;
14170 	}
14171 	/* Disable and flush TPA before resetting the RX ring */
14172 	if (bp->flags & BNXT_FLAG_TPA)
14173 		bnxt_set_tpa(bp, false);
14174 	for (i = 0; i < bp->rx_nr_rings; i++) {
14175 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14176 		struct bnxt_cp_ring_info *cpr;
14177 		int rc;
14178 
14179 		if (!rxr->bnapi->in_reset)
14180 			continue;
14181 
14182 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14183 		if (rc) {
14184 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14185 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14186 			else
14187 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14188 					    rc);
14189 			bnxt_reset_task(bp, true);
14190 			break;
14191 		}
14192 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14193 		rxr->rx_prod = 0;
14194 		rxr->rx_agg_prod = 0;
14195 		rxr->rx_sw_agg_prod = 0;
14196 		rxr->rx_next_cons = 0;
14197 		rxr->bnapi->in_reset = false;
14198 		bnxt_alloc_one_rx_ring(bp, i);
14199 		cpr = &rxr->bnapi->cp_ring;
14200 		cpr->sw_stats->rx.rx_resets++;
14201 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14202 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14203 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14204 	}
14205 	if (bp->flags & BNXT_FLAG_TPA)
14206 		bnxt_set_tpa(bp, true);
14207 	bnxt_unlock_sp(bp);
14208 }
14209 
14210 static void bnxt_fw_fatal_close(struct bnxt *bp)
14211 {
14212 	bnxt_tx_disable(bp);
14213 	bnxt_disable_napi(bp);
14214 	bnxt_disable_int_sync(bp);
14215 	bnxt_free_irq(bp);
14216 	bnxt_clear_int_mode(bp);
14217 	pci_disable_device(bp->pdev);
14218 }
14219 
14220 static void bnxt_fw_reset_close(struct bnxt *bp)
14221 {
14222 	/* When firmware is in fatal state, quiesce device and disable
14223 	 * bus master to prevent any potential bad DMAs before freeing
14224 	 * kernel memory.
14225 	 */
14226 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14227 		u16 val = 0;
14228 
14229 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14230 		if (val == 0xffff)
14231 			bp->fw_reset_min_dsecs = 0;
14232 		bnxt_fw_fatal_close(bp);
14233 	}
14234 	__bnxt_close_nic(bp, true, false);
14235 	bnxt_vf_reps_free(bp);
14236 	bnxt_clear_int_mode(bp);
14237 	bnxt_hwrm_func_drv_unrgtr(bp);
14238 	if (pci_is_enabled(bp->pdev))
14239 		pci_disable_device(bp->pdev);
14240 	bnxt_free_ctx_mem(bp, false);
14241 }
14242 
14243 static bool is_bnxt_fw_ok(struct bnxt *bp)
14244 {
14245 	struct bnxt_fw_health *fw_health = bp->fw_health;
14246 	bool no_heartbeat = false, has_reset = false;
14247 	u32 val;
14248 
14249 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14250 	if (val == fw_health->last_fw_heartbeat)
14251 		no_heartbeat = true;
14252 
14253 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14254 	if (val != fw_health->last_fw_reset_cnt)
14255 		has_reset = true;
14256 
14257 	if (!no_heartbeat && has_reset)
14258 		return true;
14259 
14260 	return false;
14261 }
14262 
14263 /* netdev instance lock is acquired before calling this function */
14264 static void bnxt_force_fw_reset(struct bnxt *bp)
14265 {
14266 	struct bnxt_fw_health *fw_health = bp->fw_health;
14267 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14268 	u32 wait_dsecs;
14269 
14270 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14271 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14272 		return;
14273 
14274 	/* we have to serialize with bnxt_refclk_read()*/
14275 	if (ptp) {
14276 		unsigned long flags;
14277 
14278 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14279 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14280 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14281 	} else {
14282 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14283 	}
14284 	bnxt_fw_reset_close(bp);
14285 	wait_dsecs = fw_health->master_func_wait_dsecs;
14286 	if (fw_health->primary) {
14287 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14288 			wait_dsecs = 0;
14289 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14290 	} else {
14291 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14292 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14293 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14294 	}
14295 
14296 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14297 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14298 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14299 }
14300 
14301 void bnxt_fw_exception(struct bnxt *bp)
14302 {
14303 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14304 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14305 	bnxt_ulp_stop(bp);
14306 	bnxt_lock_sp(bp);
14307 	bnxt_force_fw_reset(bp);
14308 	bnxt_unlock_sp(bp);
14309 }
14310 
14311 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14312  * < 0 on error.
14313  */
14314 static int bnxt_get_registered_vfs(struct bnxt *bp)
14315 {
14316 #ifdef CONFIG_BNXT_SRIOV
14317 	int rc;
14318 
14319 	if (!BNXT_PF(bp))
14320 		return 0;
14321 
14322 	rc = bnxt_hwrm_func_qcfg(bp);
14323 	if (rc) {
14324 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14325 		return rc;
14326 	}
14327 	if (bp->pf.registered_vfs)
14328 		return bp->pf.registered_vfs;
14329 	if (bp->sriov_cfg)
14330 		return 1;
14331 #endif
14332 	return 0;
14333 }
14334 
14335 void bnxt_fw_reset(struct bnxt *bp)
14336 {
14337 	bnxt_ulp_stop(bp);
14338 	bnxt_lock_sp(bp);
14339 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14340 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14341 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14342 		int n = 0, tmo;
14343 
14344 		/* we have to serialize with bnxt_refclk_read()*/
14345 		if (ptp) {
14346 			unsigned long flags;
14347 
14348 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14349 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14350 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14351 		} else {
14352 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14353 		}
14354 		if (bp->pf.active_vfs &&
14355 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14356 			n = bnxt_get_registered_vfs(bp);
14357 		if (n < 0) {
14358 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14359 				   n);
14360 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14361 			netif_close(bp->dev);
14362 			goto fw_reset_exit;
14363 		} else if (n > 0) {
14364 			u16 vf_tmo_dsecs = n * 10;
14365 
14366 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14367 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14368 			bp->fw_reset_state =
14369 				BNXT_FW_RESET_STATE_POLL_VF;
14370 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14371 			goto fw_reset_exit;
14372 		}
14373 		bnxt_fw_reset_close(bp);
14374 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14375 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14376 			tmo = HZ / 10;
14377 		} else {
14378 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14379 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14380 		}
14381 		bnxt_queue_fw_reset_work(bp, tmo);
14382 	}
14383 fw_reset_exit:
14384 	bnxt_unlock_sp(bp);
14385 }
14386 
14387 static void bnxt_chk_missed_irq(struct bnxt *bp)
14388 {
14389 	int i;
14390 
14391 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14392 		return;
14393 
14394 	for (i = 0; i < bp->cp_nr_rings; i++) {
14395 		struct bnxt_napi *bnapi = bp->bnapi[i];
14396 		struct bnxt_cp_ring_info *cpr;
14397 		u32 fw_ring_id;
14398 		int j;
14399 
14400 		if (!bnapi)
14401 			continue;
14402 
14403 		cpr = &bnapi->cp_ring;
14404 		for (j = 0; j < cpr->cp_ring_count; j++) {
14405 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14406 			u32 val[2];
14407 
14408 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14409 				continue;
14410 
14411 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14412 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14413 				continue;
14414 			}
14415 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14416 			bnxt_dbg_hwrm_ring_info_get(bp,
14417 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14418 				fw_ring_id, &val[0], &val[1]);
14419 			cpr->sw_stats->cmn.missed_irqs++;
14420 		}
14421 	}
14422 }
14423 
14424 static void bnxt_cfg_ntp_filters(struct bnxt *);
14425 
14426 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14427 {
14428 	struct bnxt_link_info *link_info = &bp->link_info;
14429 
14430 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14431 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14432 		if (bp->hwrm_spec_code >= 0x10201) {
14433 			if (link_info->auto_pause_setting &
14434 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14435 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14436 		} else {
14437 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14438 		}
14439 		bnxt_set_auto_speed(link_info);
14440 	} else {
14441 		bnxt_set_force_speed(link_info);
14442 		link_info->req_duplex = link_info->duplex_setting;
14443 	}
14444 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14445 		link_info->req_flow_ctrl =
14446 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14447 	else
14448 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14449 }
14450 
14451 static void bnxt_fw_echo_reply(struct bnxt *bp)
14452 {
14453 	struct bnxt_fw_health *fw_health = bp->fw_health;
14454 	struct hwrm_func_echo_response_input *req;
14455 	int rc;
14456 
14457 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14458 	if (rc)
14459 		return;
14460 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14461 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14462 	hwrm_req_send(bp, req);
14463 }
14464 
14465 static void bnxt_ulp_restart(struct bnxt *bp)
14466 {
14467 	bnxt_ulp_stop(bp);
14468 	bnxt_ulp_start(bp, 0);
14469 }
14470 
14471 static void bnxt_sp_task(struct work_struct *work)
14472 {
14473 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14474 
14475 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14476 	smp_mb__after_atomic();
14477 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14478 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14479 		return;
14480 	}
14481 
14482 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14483 		bnxt_ulp_restart(bp);
14484 		bnxt_reenable_sriov(bp);
14485 	}
14486 
14487 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14488 		bnxt_cfg_rx_mode(bp);
14489 
14490 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14491 		bnxt_cfg_ntp_filters(bp);
14492 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14493 		bnxt_hwrm_exec_fwd_req(bp);
14494 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14495 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14496 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14497 		bnxt_hwrm_port_qstats(bp, 0);
14498 		bnxt_hwrm_port_qstats_ext(bp, 0);
14499 		bnxt_accumulate_all_stats(bp);
14500 	}
14501 
14502 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14503 		int rc;
14504 
14505 		mutex_lock(&bp->link_lock);
14506 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14507 				       &bp->sp_event))
14508 			bnxt_hwrm_phy_qcaps(bp);
14509 
14510 		rc = bnxt_update_link(bp, true);
14511 		if (rc)
14512 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14513 				   rc);
14514 
14515 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14516 				       &bp->sp_event))
14517 			bnxt_init_ethtool_link_settings(bp);
14518 		mutex_unlock(&bp->link_lock);
14519 	}
14520 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14521 		int rc;
14522 
14523 		mutex_lock(&bp->link_lock);
14524 		rc = bnxt_update_phy_setting(bp);
14525 		mutex_unlock(&bp->link_lock);
14526 		if (rc) {
14527 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14528 		} else {
14529 			bp->link_info.phy_retry = false;
14530 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14531 		}
14532 	}
14533 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14534 		mutex_lock(&bp->link_lock);
14535 		bnxt_get_port_module_status(bp);
14536 		mutex_unlock(&bp->link_lock);
14537 	}
14538 
14539 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14540 		bnxt_tc_flow_stats_work(bp);
14541 
14542 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14543 		bnxt_chk_missed_irq(bp);
14544 
14545 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14546 		bnxt_fw_echo_reply(bp);
14547 
14548 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14549 		bnxt_hwmon_notify_event(bp);
14550 
14551 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14552 	 * must be the last functions to be called before exiting.
14553 	 */
14554 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14555 		bnxt_reset(bp, false);
14556 
14557 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14558 		bnxt_reset(bp, true);
14559 
14560 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14561 		bnxt_rx_ring_reset(bp);
14562 
14563 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14564 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14565 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14566 			bnxt_devlink_health_fw_report(bp);
14567 		else
14568 			bnxt_fw_reset(bp);
14569 	}
14570 
14571 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14572 		if (!is_bnxt_fw_ok(bp))
14573 			bnxt_devlink_health_fw_report(bp);
14574 	}
14575 
14576 	smp_mb__before_atomic();
14577 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14578 }
14579 
14580 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14581 				int *max_cp);
14582 
14583 /* Under netdev instance lock */
14584 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14585 		     int tx_xdp)
14586 {
14587 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14588 	struct bnxt_hw_rings hwr = {0};
14589 	int rx_rings = rx;
14590 	int rc;
14591 
14592 	if (tcs)
14593 		tx_sets = tcs;
14594 
14595 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14596 
14597 	if (max_rx < rx_rings)
14598 		return -ENOMEM;
14599 
14600 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14601 		rx_rings <<= 1;
14602 
14603 	hwr.rx = rx_rings;
14604 	hwr.tx = tx * tx_sets + tx_xdp;
14605 	if (max_tx < hwr.tx)
14606 		return -ENOMEM;
14607 
14608 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14609 
14610 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14611 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14612 	if (max_cp < hwr.cp)
14613 		return -ENOMEM;
14614 	hwr.stat = hwr.cp;
14615 	if (BNXT_NEW_RM(bp)) {
14616 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14617 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14618 		hwr.grp = rx;
14619 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14620 	}
14621 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14622 		hwr.cp_p5 = hwr.tx + rx;
14623 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14624 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14625 		if (!bnxt_ulp_registered(bp->edev)) {
14626 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14627 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14628 		}
14629 		if (hwr.cp > bp->total_irqs) {
14630 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14631 
14632 			if (total_msix < hwr.cp) {
14633 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14634 					    hwr.cp, total_msix);
14635 				rc = -ENOSPC;
14636 			}
14637 		}
14638 	}
14639 	return rc;
14640 }
14641 
14642 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14643 {
14644 	if (bp->bar2) {
14645 		pci_iounmap(pdev, bp->bar2);
14646 		bp->bar2 = NULL;
14647 	}
14648 
14649 	if (bp->bar1) {
14650 		pci_iounmap(pdev, bp->bar1);
14651 		bp->bar1 = NULL;
14652 	}
14653 
14654 	if (bp->bar0) {
14655 		pci_iounmap(pdev, bp->bar0);
14656 		bp->bar0 = NULL;
14657 	}
14658 }
14659 
14660 static void bnxt_cleanup_pci(struct bnxt *bp)
14661 {
14662 	bnxt_unmap_bars(bp, bp->pdev);
14663 	pci_release_regions(bp->pdev);
14664 	if (pci_is_enabled(bp->pdev))
14665 		pci_disable_device(bp->pdev);
14666 }
14667 
14668 static void bnxt_init_dflt_coal(struct bnxt *bp)
14669 {
14670 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14671 	struct bnxt_coal *coal;
14672 	u16 flags = 0;
14673 
14674 	if (coal_cap->cmpl_params &
14675 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14676 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14677 
14678 	/* Tick values in micro seconds.
14679 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14680 	 */
14681 	coal = &bp->rx_coal;
14682 	coal->coal_ticks = 10;
14683 	coal->coal_bufs = 30;
14684 	coal->coal_ticks_irq = 1;
14685 	coal->coal_bufs_irq = 2;
14686 	coal->idle_thresh = 50;
14687 	coal->bufs_per_record = 2;
14688 	coal->budget = 64;		/* NAPI budget */
14689 	coal->flags = flags;
14690 
14691 	coal = &bp->tx_coal;
14692 	coal->coal_ticks = 28;
14693 	coal->coal_bufs = 30;
14694 	coal->coal_ticks_irq = 2;
14695 	coal->coal_bufs_irq = 2;
14696 	coal->bufs_per_record = 1;
14697 	coal->flags = flags;
14698 
14699 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14700 }
14701 
14702 /* FW that pre-reserves 1 VNIC per function */
14703 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14704 {
14705 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14706 
14707 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14708 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14709 		return true;
14710 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14711 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14712 		return true;
14713 	return false;
14714 }
14715 
14716 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14717 {
14718 	int rc;
14719 
14720 	bp->fw_cap = 0;
14721 	rc = bnxt_hwrm_ver_get(bp);
14722 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14723 	 * so wait before continuing with recovery.
14724 	 */
14725 	if (rc)
14726 		msleep(100);
14727 	bnxt_try_map_fw_health_reg(bp);
14728 	if (rc) {
14729 		rc = bnxt_try_recover_fw(bp);
14730 		if (rc)
14731 			return rc;
14732 		rc = bnxt_hwrm_ver_get(bp);
14733 		if (rc)
14734 			return rc;
14735 	}
14736 
14737 	bnxt_nvm_cfg_ver_get(bp);
14738 
14739 	rc = bnxt_hwrm_func_reset(bp);
14740 	if (rc)
14741 		return -ENODEV;
14742 
14743 	bnxt_hwrm_fw_set_time(bp);
14744 	return 0;
14745 }
14746 
14747 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14748 {
14749 	int rc;
14750 
14751 	/* Get the MAX capabilities for this function */
14752 	rc = bnxt_hwrm_func_qcaps(bp);
14753 	if (rc) {
14754 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14755 			   rc);
14756 		return -ENODEV;
14757 	}
14758 
14759 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14760 	if (rc)
14761 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14762 			    rc);
14763 
14764 	if (bnxt_alloc_fw_health(bp)) {
14765 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14766 	} else {
14767 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14768 		if (rc)
14769 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14770 				    rc);
14771 	}
14772 
14773 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14774 	if (rc)
14775 		return -ENODEV;
14776 
14777 	rc = bnxt_alloc_crash_dump_mem(bp);
14778 	if (rc)
14779 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14780 			    rc);
14781 	if (!rc) {
14782 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14783 		if (rc) {
14784 			bnxt_free_crash_dump_mem(bp);
14785 			netdev_warn(bp->dev,
14786 				    "hwrm crash dump mem failure rc: %d\n", rc);
14787 		}
14788 	}
14789 
14790 	if (bnxt_fw_pre_resv_vnics(bp))
14791 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14792 
14793 	bnxt_hwrm_func_qcfg(bp);
14794 	bnxt_hwrm_vnic_qcaps(bp);
14795 	bnxt_hwrm_port_led_qcaps(bp);
14796 	bnxt_ethtool_init(bp);
14797 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14798 		__bnxt_hwrm_ptp_qcfg(bp);
14799 	bnxt_dcb_init(bp);
14800 	bnxt_hwmon_init(bp);
14801 	return 0;
14802 }
14803 
14804 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14805 {
14806 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14807 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14808 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14809 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14810 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14811 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14812 		bp->rss_hash_delta = bp->rss_hash_cfg;
14813 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14814 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14815 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14816 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14817 	}
14818 }
14819 
14820 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14821 {
14822 	struct net_device *dev = bp->dev;
14823 
14824 	dev->hw_features &= ~NETIF_F_NTUPLE;
14825 	dev->features &= ~NETIF_F_NTUPLE;
14826 	bp->flags &= ~BNXT_FLAG_RFS;
14827 	if (bnxt_rfs_supported(bp)) {
14828 		dev->hw_features |= NETIF_F_NTUPLE;
14829 		if (bnxt_rfs_capable(bp, false)) {
14830 			bp->flags |= BNXT_FLAG_RFS;
14831 			dev->features |= NETIF_F_NTUPLE;
14832 		}
14833 	}
14834 }
14835 
14836 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14837 {
14838 	struct pci_dev *pdev = bp->pdev;
14839 
14840 	bnxt_set_dflt_rss_hash_type(bp);
14841 	bnxt_set_dflt_rfs(bp);
14842 
14843 	bnxt_get_wol_settings(bp);
14844 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14845 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14846 	else
14847 		device_set_wakeup_capable(&pdev->dev, false);
14848 
14849 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14850 	bnxt_hwrm_coal_params_qcaps(bp);
14851 }
14852 
14853 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14854 
14855 int bnxt_fw_init_one(struct bnxt *bp)
14856 {
14857 	int rc;
14858 
14859 	rc = bnxt_fw_init_one_p1(bp);
14860 	if (rc) {
14861 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14862 		return rc;
14863 	}
14864 	rc = bnxt_fw_init_one_p2(bp);
14865 	if (rc) {
14866 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14867 		return rc;
14868 	}
14869 	rc = bnxt_probe_phy(bp, false);
14870 	if (rc)
14871 		return rc;
14872 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14873 	if (rc)
14874 		return rc;
14875 
14876 	bnxt_fw_init_one_p3(bp);
14877 	return 0;
14878 }
14879 
14880 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14881 {
14882 	struct bnxt_fw_health *fw_health = bp->fw_health;
14883 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14884 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14885 	u32 reg_type, reg_off, delay_msecs;
14886 
14887 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14888 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14889 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14890 	switch (reg_type) {
14891 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14892 		pci_write_config_dword(bp->pdev, reg_off, val);
14893 		break;
14894 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14895 		writel(reg_off & BNXT_GRC_BASE_MASK,
14896 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14897 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14898 		fallthrough;
14899 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14900 		writel(val, bp->bar0 + reg_off);
14901 		break;
14902 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14903 		writel(val, bp->bar1 + reg_off);
14904 		break;
14905 	}
14906 	if (delay_msecs) {
14907 		pci_read_config_dword(bp->pdev, 0, &val);
14908 		msleep(delay_msecs);
14909 	}
14910 }
14911 
14912 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14913 {
14914 	struct hwrm_func_qcfg_output *resp;
14915 	struct hwrm_func_qcfg_input *req;
14916 	bool result = true; /* firmware will enforce if unknown */
14917 
14918 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14919 		return result;
14920 
14921 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14922 		return result;
14923 
14924 	req->fid = cpu_to_le16(0xffff);
14925 	resp = hwrm_req_hold(bp, req);
14926 	if (!hwrm_req_send(bp, req))
14927 		result = !!(le16_to_cpu(resp->flags) &
14928 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14929 	hwrm_req_drop(bp, req);
14930 	return result;
14931 }
14932 
14933 static void bnxt_reset_all(struct bnxt *bp)
14934 {
14935 	struct bnxt_fw_health *fw_health = bp->fw_health;
14936 	int i, rc;
14937 
14938 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14939 		bnxt_fw_reset_via_optee(bp);
14940 		bp->fw_reset_timestamp = jiffies;
14941 		return;
14942 	}
14943 
14944 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14945 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14946 			bnxt_fw_reset_writel(bp, i);
14947 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14948 		struct hwrm_fw_reset_input *req;
14949 
14950 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14951 		if (!rc) {
14952 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14953 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14954 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14955 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14956 			rc = hwrm_req_send(bp, req);
14957 		}
14958 		if (rc != -ENODEV)
14959 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14960 	}
14961 	bp->fw_reset_timestamp = jiffies;
14962 }
14963 
14964 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14965 {
14966 	return time_after(jiffies, bp->fw_reset_timestamp +
14967 			  (bp->fw_reset_max_dsecs * HZ / 10));
14968 }
14969 
14970 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14971 {
14972 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14973 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14974 		bnxt_dl_health_fw_status_update(bp, false);
14975 	bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT;
14976 	netif_close(bp->dev);
14977 }
14978 
14979 static void bnxt_fw_reset_task(struct work_struct *work)
14980 {
14981 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14982 	int rc = 0;
14983 
14984 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14985 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14986 		return;
14987 	}
14988 
14989 	switch (bp->fw_reset_state) {
14990 	case BNXT_FW_RESET_STATE_POLL_VF: {
14991 		int n = bnxt_get_registered_vfs(bp);
14992 		int tmo;
14993 
14994 		if (n < 0) {
14995 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14996 				   n, jiffies_to_msecs(jiffies -
14997 				   bp->fw_reset_timestamp));
14998 			goto fw_reset_abort;
14999 		} else if (n > 0) {
15000 			if (bnxt_fw_reset_timeout(bp)) {
15001 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15002 				bp->fw_reset_state = 0;
15003 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
15004 					   n);
15005 				goto ulp_start;
15006 			}
15007 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15008 			return;
15009 		}
15010 		bp->fw_reset_timestamp = jiffies;
15011 		netdev_lock(bp->dev);
15012 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
15013 			bnxt_fw_reset_abort(bp, rc);
15014 			netdev_unlock(bp->dev);
15015 			goto ulp_start;
15016 		}
15017 		bnxt_fw_reset_close(bp);
15018 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15019 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
15020 			tmo = HZ / 10;
15021 		} else {
15022 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15023 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
15024 		}
15025 		netdev_unlock(bp->dev);
15026 		bnxt_queue_fw_reset_work(bp, tmo);
15027 		return;
15028 	}
15029 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
15030 		u32 val;
15031 
15032 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15033 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
15034 		    !bnxt_fw_reset_timeout(bp)) {
15035 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15036 			return;
15037 		}
15038 
15039 		if (!bp->fw_health->primary) {
15040 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
15041 
15042 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15043 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
15044 			return;
15045 		}
15046 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
15047 	}
15048 		fallthrough;
15049 	case BNXT_FW_RESET_STATE_RESET_FW:
15050 		bnxt_reset_all(bp);
15051 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15052 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
15053 		return;
15054 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
15055 		bnxt_inv_fw_health_reg(bp);
15056 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
15057 		    !bp->fw_reset_min_dsecs) {
15058 			u16 val;
15059 
15060 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
15061 			if (val == 0xffff) {
15062 				if (bnxt_fw_reset_timeout(bp)) {
15063 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
15064 					rc = -ETIMEDOUT;
15065 					goto fw_reset_abort;
15066 				}
15067 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
15068 				return;
15069 			}
15070 		}
15071 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
15072 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
15073 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
15074 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
15075 			bnxt_dl_remote_reload(bp);
15076 		if (pci_enable_device(bp->pdev)) {
15077 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
15078 			rc = -ENODEV;
15079 			goto fw_reset_abort;
15080 		}
15081 		pci_set_master(bp->pdev);
15082 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
15083 		fallthrough;
15084 	case BNXT_FW_RESET_STATE_POLL_FW:
15085 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
15086 		rc = bnxt_hwrm_poll(bp);
15087 		if (rc) {
15088 			if (bnxt_fw_reset_timeout(bp)) {
15089 				netdev_err(bp->dev, "Firmware reset aborted\n");
15090 				goto fw_reset_abort_status;
15091 			}
15092 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15093 			return;
15094 		}
15095 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
15096 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
15097 		fallthrough;
15098 	case BNXT_FW_RESET_STATE_OPENING:
15099 		while (!netdev_trylock(bp->dev)) {
15100 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15101 			return;
15102 		}
15103 		rc = bnxt_open(bp->dev);
15104 		if (rc) {
15105 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
15106 			bnxt_fw_reset_abort(bp, rc);
15107 			netdev_unlock(bp->dev);
15108 			goto ulp_start;
15109 		}
15110 
15111 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
15112 		    bp->fw_health->enabled) {
15113 			bp->fw_health->last_fw_reset_cnt =
15114 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
15115 		}
15116 		bp->fw_reset_state = 0;
15117 		/* Make sure fw_reset_state is 0 before clearing the flag */
15118 		smp_mb__before_atomic();
15119 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15120 		bnxt_ptp_reapply_pps(bp);
15121 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
15122 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
15123 			bnxt_dl_health_fw_recovery_done(bp);
15124 			bnxt_dl_health_fw_status_update(bp, true);
15125 		}
15126 		netdev_unlock(bp->dev);
15127 		bnxt_ulp_start(bp, 0);
15128 		bnxt_reenable_sriov(bp);
15129 		netdev_lock(bp->dev);
15130 		bnxt_vf_reps_alloc(bp);
15131 		bnxt_vf_reps_open(bp);
15132 		netdev_unlock(bp->dev);
15133 		break;
15134 	}
15135 	return;
15136 
15137 fw_reset_abort_status:
15138 	if (bp->fw_health->status_reliable ||
15139 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15140 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15141 
15142 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15143 	}
15144 fw_reset_abort:
15145 	netdev_lock(bp->dev);
15146 	bnxt_fw_reset_abort(bp, rc);
15147 	netdev_unlock(bp->dev);
15148 ulp_start:
15149 	bnxt_ulp_start(bp, rc);
15150 }
15151 
15152 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15153 {
15154 	int rc;
15155 	struct bnxt *bp = netdev_priv(dev);
15156 
15157 	SET_NETDEV_DEV(dev, &pdev->dev);
15158 
15159 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15160 	rc = pci_enable_device(pdev);
15161 	if (rc) {
15162 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15163 		goto init_err;
15164 	}
15165 
15166 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15167 		dev_err(&pdev->dev,
15168 			"Cannot find PCI device base address, aborting\n");
15169 		rc = -ENODEV;
15170 		goto init_err_disable;
15171 	}
15172 
15173 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15174 	if (rc) {
15175 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15176 		goto init_err_disable;
15177 	}
15178 
15179 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
15180 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
15181 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15182 		rc = -EIO;
15183 		goto init_err_release;
15184 	}
15185 
15186 	pci_set_master(pdev);
15187 
15188 	bp->dev = dev;
15189 	bp->pdev = pdev;
15190 
15191 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15192 	 * determines the BAR size.
15193 	 */
15194 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15195 	if (!bp->bar0) {
15196 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15197 		rc = -ENOMEM;
15198 		goto init_err_release;
15199 	}
15200 
15201 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15202 	if (!bp->bar2) {
15203 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15204 		rc = -ENOMEM;
15205 		goto init_err_release;
15206 	}
15207 
15208 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15209 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15210 
15211 	spin_lock_init(&bp->ntp_fltr_lock);
15212 #if BITS_PER_LONG == 32
15213 	spin_lock_init(&bp->db_lock);
15214 #endif
15215 
15216 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15217 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15218 
15219 	timer_setup(&bp->timer, bnxt_timer, 0);
15220 	bp->current_interval = BNXT_TIMER_INTERVAL;
15221 
15222 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15223 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15224 
15225 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15226 	return 0;
15227 
15228 init_err_release:
15229 	bnxt_unmap_bars(bp, pdev);
15230 	pci_release_regions(pdev);
15231 
15232 init_err_disable:
15233 	pci_disable_device(pdev);
15234 
15235 init_err:
15236 	return rc;
15237 }
15238 
15239 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15240 {
15241 	struct sockaddr *addr = p;
15242 	struct bnxt *bp = netdev_priv(dev);
15243 	int rc = 0;
15244 
15245 	netdev_assert_locked(dev);
15246 
15247 	if (!is_valid_ether_addr(addr->sa_data))
15248 		return -EADDRNOTAVAIL;
15249 
15250 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15251 		return 0;
15252 
15253 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15254 	if (rc)
15255 		return rc;
15256 
15257 	eth_hw_addr_set(dev, addr->sa_data);
15258 	bnxt_clear_usr_fltrs(bp, true);
15259 	if (netif_running(dev)) {
15260 		bnxt_close_nic(bp, false, false);
15261 		rc = bnxt_open_nic(bp, false, false);
15262 	}
15263 
15264 	return rc;
15265 }
15266 
15267 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15268 {
15269 	struct bnxt *bp = netdev_priv(dev);
15270 
15271 	netdev_assert_locked(dev);
15272 
15273 	if (netif_running(dev))
15274 		bnxt_close_nic(bp, true, false);
15275 
15276 	WRITE_ONCE(dev->mtu, new_mtu);
15277 
15278 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15279 	 * program is attached.  We need to set the AGG rings settings and
15280 	 * rx_skb_func accordingly.
15281 	 */
15282 	if (READ_ONCE(bp->xdp_prog))
15283 		bnxt_set_rx_skb_mode(bp, true);
15284 
15285 	bnxt_set_ring_params(bp);
15286 
15287 	if (netif_running(dev))
15288 		return bnxt_open_nic(bp, true, false);
15289 
15290 	return 0;
15291 }
15292 
15293 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15294 {
15295 	struct bnxt *bp = netdev_priv(dev);
15296 	bool sh = false;
15297 	int rc, tx_cp;
15298 
15299 	if (tc > bp->max_tc) {
15300 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15301 			   tc, bp->max_tc);
15302 		return -EINVAL;
15303 	}
15304 
15305 	if (bp->num_tc == tc)
15306 		return 0;
15307 
15308 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15309 		sh = true;
15310 
15311 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15312 			      sh, tc, bp->tx_nr_rings_xdp);
15313 	if (rc)
15314 		return rc;
15315 
15316 	/* Needs to close the device and do hw resource re-allocations */
15317 	if (netif_running(bp->dev))
15318 		bnxt_close_nic(bp, true, false);
15319 
15320 	if (tc) {
15321 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15322 		netdev_set_num_tc(dev, tc);
15323 		bp->num_tc = tc;
15324 	} else {
15325 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15326 		netdev_reset_tc(dev);
15327 		bp->num_tc = 0;
15328 	}
15329 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15330 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15331 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15332 			       tx_cp + bp->rx_nr_rings;
15333 
15334 	if (netif_running(bp->dev))
15335 		return bnxt_open_nic(bp, true, false);
15336 
15337 	return 0;
15338 }
15339 
15340 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15341 				  void *cb_priv)
15342 {
15343 	struct bnxt *bp = cb_priv;
15344 
15345 	if (!bnxt_tc_flower_enabled(bp) ||
15346 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15347 		return -EOPNOTSUPP;
15348 
15349 	switch (type) {
15350 	case TC_SETUP_CLSFLOWER:
15351 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15352 	default:
15353 		return -EOPNOTSUPP;
15354 	}
15355 }
15356 
15357 LIST_HEAD(bnxt_block_cb_list);
15358 
15359 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15360 			 void *type_data)
15361 {
15362 	struct bnxt *bp = netdev_priv(dev);
15363 
15364 	switch (type) {
15365 	case TC_SETUP_BLOCK:
15366 		return flow_block_cb_setup_simple(type_data,
15367 						  &bnxt_block_cb_list,
15368 						  bnxt_setup_tc_block_cb,
15369 						  bp, bp, true);
15370 	case TC_SETUP_QDISC_MQPRIO: {
15371 		struct tc_mqprio_qopt *mqprio = type_data;
15372 
15373 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15374 
15375 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15376 	}
15377 	default:
15378 		return -EOPNOTSUPP;
15379 	}
15380 }
15381 
15382 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15383 			    const struct sk_buff *skb)
15384 {
15385 	struct bnxt_vnic_info *vnic;
15386 
15387 	if (skb)
15388 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15389 
15390 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15391 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15392 }
15393 
15394 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15395 			   u32 idx)
15396 {
15397 	struct hlist_head *head;
15398 	int bit_id;
15399 
15400 	spin_lock_bh(&bp->ntp_fltr_lock);
15401 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15402 	if (bit_id < 0) {
15403 		spin_unlock_bh(&bp->ntp_fltr_lock);
15404 		return -ENOMEM;
15405 	}
15406 
15407 	fltr->base.sw_id = (u16)bit_id;
15408 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15409 	fltr->base.flags |= BNXT_ACT_RING_DST;
15410 	head = &bp->ntp_fltr_hash_tbl[idx];
15411 	hlist_add_head_rcu(&fltr->base.hash, head);
15412 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15413 	bnxt_insert_usr_fltr(bp, &fltr->base);
15414 	bp->ntp_fltr_count++;
15415 	spin_unlock_bh(&bp->ntp_fltr_lock);
15416 	return 0;
15417 }
15418 
15419 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15420 			    struct bnxt_ntuple_filter *f2)
15421 {
15422 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15423 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15424 	struct flow_keys *keys1 = &f1->fkeys;
15425 	struct flow_keys *keys2 = &f2->fkeys;
15426 
15427 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15428 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15429 		return false;
15430 
15431 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15432 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15433 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15434 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15435 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15436 			return false;
15437 	} else {
15438 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15439 				     &keys2->addrs.v6addrs.src) ||
15440 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15441 				     &masks2->addrs.v6addrs.src) ||
15442 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15443 				     &keys2->addrs.v6addrs.dst) ||
15444 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15445 				     &masks2->addrs.v6addrs.dst))
15446 			return false;
15447 	}
15448 
15449 	return keys1->ports.src == keys2->ports.src &&
15450 	       masks1->ports.src == masks2->ports.src &&
15451 	       keys1->ports.dst == keys2->ports.dst &&
15452 	       masks1->ports.dst == masks2->ports.dst &&
15453 	       keys1->control.flags == keys2->control.flags &&
15454 	       f1->l2_fltr == f2->l2_fltr;
15455 }
15456 
15457 struct bnxt_ntuple_filter *
15458 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15459 				struct bnxt_ntuple_filter *fltr, u32 idx)
15460 {
15461 	struct bnxt_ntuple_filter *f;
15462 	struct hlist_head *head;
15463 
15464 	head = &bp->ntp_fltr_hash_tbl[idx];
15465 	hlist_for_each_entry_rcu(f, head, base.hash) {
15466 		if (bnxt_fltr_match(f, fltr))
15467 			return f;
15468 	}
15469 	return NULL;
15470 }
15471 
15472 #ifdef CONFIG_RFS_ACCEL
15473 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15474 			      u16 rxq_index, u32 flow_id)
15475 {
15476 	struct bnxt *bp = netdev_priv(dev);
15477 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15478 	struct flow_keys *fkeys;
15479 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15480 	struct bnxt_l2_filter *l2_fltr;
15481 	int rc = 0, idx;
15482 	u32 flags;
15483 
15484 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15485 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15486 		atomic_inc(&l2_fltr->refcnt);
15487 	} else {
15488 		struct bnxt_l2_key key;
15489 
15490 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15491 		key.vlan = 0;
15492 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15493 		if (!l2_fltr)
15494 			return -EINVAL;
15495 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15496 			bnxt_del_l2_filter(bp, l2_fltr);
15497 			return -EINVAL;
15498 		}
15499 	}
15500 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
15501 	if (!new_fltr) {
15502 		bnxt_del_l2_filter(bp, l2_fltr);
15503 		return -ENOMEM;
15504 	}
15505 
15506 	fkeys = &new_fltr->fkeys;
15507 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15508 		rc = -EPROTONOSUPPORT;
15509 		goto err_free;
15510 	}
15511 
15512 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15513 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15514 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15515 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15516 		rc = -EPROTONOSUPPORT;
15517 		goto err_free;
15518 	}
15519 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15520 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15521 		if (bp->hwrm_spec_code < 0x10601) {
15522 			rc = -EPROTONOSUPPORT;
15523 			goto err_free;
15524 		}
15525 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15526 	}
15527 	flags = fkeys->control.flags;
15528 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15529 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15530 		rc = -EPROTONOSUPPORT;
15531 		goto err_free;
15532 	}
15533 	new_fltr->l2_fltr = l2_fltr;
15534 
15535 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15536 	rcu_read_lock();
15537 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15538 	if (fltr) {
15539 		rc = fltr->base.sw_id;
15540 		rcu_read_unlock();
15541 		goto err_free;
15542 	}
15543 	rcu_read_unlock();
15544 
15545 	new_fltr->flow_id = flow_id;
15546 	new_fltr->base.rxq = rxq_index;
15547 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15548 	if (!rc) {
15549 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15550 		return new_fltr->base.sw_id;
15551 	}
15552 
15553 err_free:
15554 	bnxt_del_l2_filter(bp, l2_fltr);
15555 	kfree(new_fltr);
15556 	return rc;
15557 }
15558 #endif
15559 
15560 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15561 {
15562 	spin_lock_bh(&bp->ntp_fltr_lock);
15563 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15564 		spin_unlock_bh(&bp->ntp_fltr_lock);
15565 		return;
15566 	}
15567 	hlist_del_rcu(&fltr->base.hash);
15568 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15569 	bp->ntp_fltr_count--;
15570 	spin_unlock_bh(&bp->ntp_fltr_lock);
15571 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15572 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15573 	kfree_rcu(fltr, base.rcu);
15574 }
15575 
15576 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15577 {
15578 #ifdef CONFIG_RFS_ACCEL
15579 	int i;
15580 
15581 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15582 		struct hlist_head *head;
15583 		struct hlist_node *tmp;
15584 		struct bnxt_ntuple_filter *fltr;
15585 		int rc;
15586 
15587 		head = &bp->ntp_fltr_hash_tbl[i];
15588 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15589 			bool del = false;
15590 
15591 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15592 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15593 					continue;
15594 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15595 							fltr->flow_id,
15596 							fltr->base.sw_id)) {
15597 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15598 									 fltr);
15599 					del = true;
15600 				}
15601 			} else {
15602 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15603 								       fltr);
15604 				if (rc)
15605 					del = true;
15606 				else
15607 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15608 			}
15609 
15610 			if (del)
15611 				bnxt_del_ntp_filter(bp, fltr);
15612 		}
15613 	}
15614 #endif
15615 }
15616 
15617 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15618 				    unsigned int entry, struct udp_tunnel_info *ti)
15619 {
15620 	struct bnxt *bp = netdev_priv(netdev);
15621 	unsigned int cmd;
15622 
15623 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15624 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15625 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15626 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15627 	else
15628 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15629 
15630 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15631 }
15632 
15633 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15634 				      unsigned int entry, struct udp_tunnel_info *ti)
15635 {
15636 	struct bnxt *bp = netdev_priv(netdev);
15637 	unsigned int cmd;
15638 
15639 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15640 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15641 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15642 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15643 	else
15644 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15645 
15646 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15647 }
15648 
15649 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15650 	.set_port	= bnxt_udp_tunnel_set_port,
15651 	.unset_port	= bnxt_udp_tunnel_unset_port,
15652 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15653 	.tables		= {
15654 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15655 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15656 	},
15657 }, bnxt_udp_tunnels_p7 = {
15658 	.set_port	= bnxt_udp_tunnel_set_port,
15659 	.unset_port	= bnxt_udp_tunnel_unset_port,
15660 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15661 	.tables		= {
15662 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15663 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15664 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15665 	},
15666 };
15667 
15668 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15669 			       struct net_device *dev, u32 filter_mask,
15670 			       int nlflags)
15671 {
15672 	struct bnxt *bp = netdev_priv(dev);
15673 
15674 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15675 				       nlflags, filter_mask, NULL);
15676 }
15677 
15678 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15679 			       u16 flags, struct netlink_ext_ack *extack)
15680 {
15681 	struct bnxt *bp = netdev_priv(dev);
15682 	struct nlattr *attr, *br_spec;
15683 	int rem, rc = 0;
15684 
15685 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15686 		return -EOPNOTSUPP;
15687 
15688 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15689 	if (!br_spec)
15690 		return -EINVAL;
15691 
15692 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15693 		u16 mode;
15694 
15695 		mode = nla_get_u16(attr);
15696 		if (mode == bp->br_mode)
15697 			break;
15698 
15699 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15700 		if (!rc)
15701 			bp->br_mode = mode;
15702 		break;
15703 	}
15704 	return rc;
15705 }
15706 
15707 int bnxt_get_port_parent_id(struct net_device *dev,
15708 			    struct netdev_phys_item_id *ppid)
15709 {
15710 	struct bnxt *bp = netdev_priv(dev);
15711 
15712 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15713 		return -EOPNOTSUPP;
15714 
15715 	/* The PF and it's VF-reps only support the switchdev framework */
15716 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15717 		return -EOPNOTSUPP;
15718 
15719 	ppid->id_len = sizeof(bp->dsn);
15720 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15721 
15722 	return 0;
15723 }
15724 
15725 static const struct net_device_ops bnxt_netdev_ops = {
15726 	.ndo_open		= bnxt_open,
15727 	.ndo_start_xmit		= bnxt_start_xmit,
15728 	.ndo_stop		= bnxt_close,
15729 	.ndo_get_stats64	= bnxt_get_stats64,
15730 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15731 	.ndo_eth_ioctl		= bnxt_ioctl,
15732 	.ndo_validate_addr	= eth_validate_addr,
15733 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15734 	.ndo_change_mtu		= bnxt_change_mtu,
15735 	.ndo_fix_features	= bnxt_fix_features,
15736 	.ndo_set_features	= bnxt_set_features,
15737 	.ndo_features_check	= bnxt_features_check,
15738 	.ndo_tx_timeout		= bnxt_tx_timeout,
15739 #ifdef CONFIG_BNXT_SRIOV
15740 	.ndo_get_vf_config	= bnxt_get_vf_config,
15741 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15742 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15743 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15744 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15745 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15746 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15747 #endif
15748 	.ndo_setup_tc           = bnxt_setup_tc,
15749 #ifdef CONFIG_RFS_ACCEL
15750 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15751 #endif
15752 	.ndo_bpf		= bnxt_xdp,
15753 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15754 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15755 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15756 };
15757 
15758 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15759 				    struct netdev_queue_stats_rx *stats)
15760 {
15761 	struct bnxt *bp = netdev_priv(dev);
15762 	struct bnxt_cp_ring_info *cpr;
15763 	u64 *sw;
15764 
15765 	if (!bp->bnapi)
15766 		return;
15767 
15768 	cpr = &bp->bnapi[i]->cp_ring;
15769 	sw = cpr->stats.sw_stats;
15770 
15771 	stats->packets = 0;
15772 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15773 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15774 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15775 
15776 	stats->bytes = 0;
15777 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15778 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15779 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15780 
15781 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15782 }
15783 
15784 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15785 				    struct netdev_queue_stats_tx *stats)
15786 {
15787 	struct bnxt *bp = netdev_priv(dev);
15788 	struct bnxt_napi *bnapi;
15789 	u64 *sw;
15790 
15791 	if (!bp->tx_ring)
15792 		return;
15793 
15794 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15795 	sw = bnapi->cp_ring.stats.sw_stats;
15796 
15797 	stats->packets = 0;
15798 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15799 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15800 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15801 
15802 	stats->bytes = 0;
15803 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15804 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15805 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15806 }
15807 
15808 static void bnxt_get_base_stats(struct net_device *dev,
15809 				struct netdev_queue_stats_rx *rx,
15810 				struct netdev_queue_stats_tx *tx)
15811 {
15812 	struct bnxt *bp = netdev_priv(dev);
15813 
15814 	rx->packets = bp->net_stats_prev.rx_packets;
15815 	rx->bytes = bp->net_stats_prev.rx_bytes;
15816 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15817 
15818 	tx->packets = bp->net_stats_prev.tx_packets;
15819 	tx->bytes = bp->net_stats_prev.tx_bytes;
15820 }
15821 
15822 static const struct netdev_stat_ops bnxt_stat_ops = {
15823 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15824 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15825 	.get_base_stats		= bnxt_get_base_stats,
15826 };
15827 
15828 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15829 {
15830 	struct bnxt_rx_ring_info *rxr, *clone;
15831 	struct bnxt *bp = netdev_priv(dev);
15832 	struct bnxt_ring_struct *ring;
15833 	int rc;
15834 
15835 	if (!bp->rx_ring)
15836 		return -ENETDOWN;
15837 
15838 	rxr = &bp->rx_ring[idx];
15839 	clone = qmem;
15840 	memcpy(clone, rxr, sizeof(*rxr));
15841 	bnxt_init_rx_ring_struct(bp, clone);
15842 	bnxt_reset_rx_ring_struct(bp, clone);
15843 
15844 	clone->rx_prod = 0;
15845 	clone->rx_agg_prod = 0;
15846 	clone->rx_sw_agg_prod = 0;
15847 	clone->rx_next_cons = 0;
15848 	clone->need_head_pool = false;
15849 
15850 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15851 	if (rc)
15852 		return rc;
15853 
15854 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15855 	if (rc < 0)
15856 		goto err_page_pool_destroy;
15857 
15858 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15859 					MEM_TYPE_PAGE_POOL,
15860 					clone->page_pool);
15861 	if (rc)
15862 		goto err_rxq_info_unreg;
15863 
15864 	ring = &clone->rx_ring_struct;
15865 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15866 	if (rc)
15867 		goto err_free_rx_ring;
15868 
15869 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15870 		ring = &clone->rx_agg_ring_struct;
15871 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15872 		if (rc)
15873 			goto err_free_rx_agg_ring;
15874 
15875 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15876 		if (rc)
15877 			goto err_free_rx_agg_ring;
15878 	}
15879 
15880 	if (bp->flags & BNXT_FLAG_TPA) {
15881 		rc = bnxt_alloc_one_tpa_info(bp, clone);
15882 		if (rc)
15883 			goto err_free_tpa_info;
15884 	}
15885 
15886 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15887 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15888 
15889 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15890 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15891 		bnxt_alloc_one_rx_ring_netmem(bp, clone, idx);
15892 	if (bp->flags & BNXT_FLAG_TPA)
15893 		bnxt_alloc_one_tpa_info_data(bp, clone);
15894 
15895 	return 0;
15896 
15897 err_free_tpa_info:
15898 	bnxt_free_one_tpa_info(bp, clone);
15899 err_free_rx_agg_ring:
15900 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15901 err_free_rx_ring:
15902 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15903 err_rxq_info_unreg:
15904 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15905 err_page_pool_destroy:
15906 	page_pool_destroy(clone->page_pool);
15907 	page_pool_destroy(clone->head_pool);
15908 	clone->page_pool = NULL;
15909 	clone->head_pool = NULL;
15910 	return rc;
15911 }
15912 
15913 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15914 {
15915 	struct bnxt_rx_ring_info *rxr = qmem;
15916 	struct bnxt *bp = netdev_priv(dev);
15917 	struct bnxt_ring_struct *ring;
15918 
15919 	bnxt_free_one_rx_ring_skbs(bp, rxr);
15920 	bnxt_free_one_tpa_info(bp, rxr);
15921 
15922 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15923 
15924 	page_pool_destroy(rxr->page_pool);
15925 	page_pool_destroy(rxr->head_pool);
15926 	rxr->page_pool = NULL;
15927 	rxr->head_pool = NULL;
15928 
15929 	ring = &rxr->rx_ring_struct;
15930 	bnxt_free_ring(bp, &ring->ring_mem);
15931 
15932 	ring = &rxr->rx_agg_ring_struct;
15933 	bnxt_free_ring(bp, &ring->ring_mem);
15934 
15935 	kfree(rxr->rx_agg_bmap);
15936 	rxr->rx_agg_bmap = NULL;
15937 }
15938 
15939 static void bnxt_copy_rx_ring(struct bnxt *bp,
15940 			      struct bnxt_rx_ring_info *dst,
15941 			      struct bnxt_rx_ring_info *src)
15942 {
15943 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15944 	struct bnxt_ring_struct *dst_ring, *src_ring;
15945 	int i;
15946 
15947 	dst_ring = &dst->rx_ring_struct;
15948 	dst_rmem = &dst_ring->ring_mem;
15949 	src_ring = &src->rx_ring_struct;
15950 	src_rmem = &src_ring->ring_mem;
15951 
15952 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15953 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15954 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15955 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15956 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15957 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15958 
15959 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15960 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15961 	*dst_rmem->vmem = *src_rmem->vmem;
15962 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15963 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15964 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15965 	}
15966 
15967 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15968 		return;
15969 
15970 	dst_ring = &dst->rx_agg_ring_struct;
15971 	dst_rmem = &dst_ring->ring_mem;
15972 	src_ring = &src->rx_agg_ring_struct;
15973 	src_rmem = &src_ring->ring_mem;
15974 
15975 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15976 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15977 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15978 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15979 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15980 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15981 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15982 
15983 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15984 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15985 	*dst_rmem->vmem = *src_rmem->vmem;
15986 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15987 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15988 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15989 	}
15990 
15991 	dst->rx_agg_bmap = src->rx_agg_bmap;
15992 }
15993 
15994 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15995 {
15996 	struct bnxt *bp = netdev_priv(dev);
15997 	struct bnxt_rx_ring_info *rxr, *clone;
15998 	struct bnxt_cp_ring_info *cpr;
15999 	struct bnxt_vnic_info *vnic;
16000 	struct bnxt_napi *bnapi;
16001 	int i, rc;
16002 	u16 mru;
16003 
16004 	rxr = &bp->rx_ring[idx];
16005 	clone = qmem;
16006 
16007 	rxr->rx_prod = clone->rx_prod;
16008 	rxr->rx_agg_prod = clone->rx_agg_prod;
16009 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
16010 	rxr->rx_next_cons = clone->rx_next_cons;
16011 	rxr->rx_tpa = clone->rx_tpa;
16012 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
16013 	rxr->page_pool = clone->page_pool;
16014 	rxr->head_pool = clone->head_pool;
16015 	rxr->xdp_rxq = clone->xdp_rxq;
16016 	rxr->need_head_pool = clone->need_head_pool;
16017 
16018 	bnxt_copy_rx_ring(bp, rxr, clone);
16019 
16020 	bnapi = rxr->bnapi;
16021 	cpr = &bnapi->cp_ring;
16022 
16023 	/* All rings have been reserved and previously allocated.
16024 	 * Reallocating with the same parameters should never fail.
16025 	 */
16026 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
16027 	if (rc)
16028 		goto err_reset;
16029 
16030 	if (bp->tph_mode) {
16031 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
16032 		if (rc)
16033 			goto err_reset;
16034 	}
16035 
16036 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
16037 	if (rc)
16038 		goto err_reset;
16039 
16040 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
16041 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16042 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
16043 
16044 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
16045 		rc = bnxt_tx_queue_start(bp, idx);
16046 		if (rc)
16047 			goto err_reset;
16048 	}
16049 
16050 	bnxt_enable_rx_page_pool(rxr);
16051 	napi_enable_locked(&bnapi->napi);
16052 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16053 
16054 	mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
16055 	for (i = 0; i < bp->nr_vnics; i++) {
16056 		vnic = &bp->vnic_info[i];
16057 
16058 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx);
16059 		if (rc)
16060 			return rc;
16061 	}
16062 	return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx);
16063 
16064 err_reset:
16065 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
16066 		   rc);
16067 	napi_enable_locked(&bnapi->napi);
16068 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16069 	bnxt_reset_task(bp, true);
16070 	return rc;
16071 }
16072 
16073 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
16074 {
16075 	struct bnxt *bp = netdev_priv(dev);
16076 	struct bnxt_rx_ring_info *rxr;
16077 	struct bnxt_cp_ring_info *cpr;
16078 	struct bnxt_vnic_info *vnic;
16079 	struct bnxt_napi *bnapi;
16080 	int i;
16081 
16082 	for (i = 0; i < bp->nr_vnics; i++) {
16083 		vnic = &bp->vnic_info[i];
16084 
16085 		bnxt_set_vnic_mru_p5(bp, vnic, 0, idx);
16086 	}
16087 	bnxt_set_rss_ctx_vnic_mru(bp, 0, idx);
16088 	/* Make sure NAPI sees that the VNIC is disabled */
16089 	synchronize_net();
16090 	rxr = &bp->rx_ring[idx];
16091 	bnapi = rxr->bnapi;
16092 	cpr = &bnapi->cp_ring;
16093 	cancel_work_sync(&cpr->dim.work);
16094 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
16095 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
16096 	page_pool_disable_direct_recycling(rxr->page_pool);
16097 	if (bnxt_separate_head_pool(rxr))
16098 		page_pool_disable_direct_recycling(rxr->head_pool);
16099 
16100 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
16101 		bnxt_tx_queue_stop(bp, idx);
16102 
16103 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
16104 	 * completion is handled in NAPI to guarantee no more DMA on that ring
16105 	 * after seeing the completion.
16106 	 */
16107 	napi_disable_locked(&bnapi->napi);
16108 
16109 	if (bp->tph_mode) {
16110 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
16111 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
16112 	}
16113 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
16114 
16115 	memcpy(qmem, rxr, sizeof(*rxr));
16116 	bnxt_init_rx_ring_struct(bp, qmem);
16117 
16118 	return 0;
16119 }
16120 
16121 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
16122 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
16123 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
16124 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
16125 	.ndo_queue_start	= bnxt_queue_start,
16126 	.ndo_queue_stop		= bnxt_queue_stop,
16127 };
16128 
16129 static void bnxt_remove_one(struct pci_dev *pdev)
16130 {
16131 	struct net_device *dev = pci_get_drvdata(pdev);
16132 	struct bnxt *bp = netdev_priv(dev);
16133 
16134 	if (BNXT_PF(bp))
16135 		bnxt_sriov_disable(bp);
16136 
16137 	bnxt_rdma_aux_device_del(bp);
16138 
16139 	unregister_netdev(dev);
16140 	bnxt_ptp_clear(bp);
16141 
16142 	bnxt_rdma_aux_device_uninit(bp);
16143 
16144 	bnxt_free_l2_filters(bp, true);
16145 	bnxt_free_ntp_fltrs(bp, true);
16146 	WARN_ON(bp->num_rss_ctx);
16147 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16148 	/* Flush any pending tasks */
16149 	cancel_work_sync(&bp->sp_task);
16150 	cancel_delayed_work_sync(&bp->fw_reset_task);
16151 	bp->sp_event = 0;
16152 
16153 	bnxt_dl_fw_reporters_destroy(bp);
16154 	bnxt_dl_unregister(bp);
16155 	bnxt_shutdown_tc(bp);
16156 
16157 	bnxt_clear_int_mode(bp);
16158 	bnxt_hwrm_func_drv_unrgtr(bp);
16159 	bnxt_free_hwrm_resources(bp);
16160 	bnxt_hwmon_uninit(bp);
16161 	bnxt_ethtool_free(bp);
16162 	bnxt_dcb_free(bp);
16163 	kfree(bp->ptp_cfg);
16164 	bp->ptp_cfg = NULL;
16165 	kfree(bp->fw_health);
16166 	bp->fw_health = NULL;
16167 	bnxt_cleanup_pci(bp);
16168 	bnxt_free_ctx_mem(bp, true);
16169 	bnxt_free_crash_dump_mem(bp);
16170 	kfree(bp->rss_indir_tbl);
16171 	bp->rss_indir_tbl = NULL;
16172 	bnxt_free_port_stats(bp);
16173 	free_netdev(dev);
16174 }
16175 
16176 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16177 {
16178 	int rc = 0;
16179 	struct bnxt_link_info *link_info = &bp->link_info;
16180 
16181 	bp->phy_flags = 0;
16182 	rc = bnxt_hwrm_phy_qcaps(bp);
16183 	if (rc) {
16184 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16185 			   rc);
16186 		return rc;
16187 	}
16188 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16189 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16190 	else
16191 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16192 
16193 	bp->mac_flags = 0;
16194 	bnxt_hwrm_mac_qcaps(bp);
16195 
16196 	if (!fw_dflt)
16197 		return 0;
16198 
16199 	mutex_lock(&bp->link_lock);
16200 	rc = bnxt_update_link(bp, false);
16201 	if (rc) {
16202 		mutex_unlock(&bp->link_lock);
16203 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16204 			   rc);
16205 		return rc;
16206 	}
16207 
16208 	/* Older firmware does not have supported_auto_speeds, so assume
16209 	 * that all supported speeds can be autonegotiated.
16210 	 */
16211 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16212 		link_info->support_auto_speeds = link_info->support_speeds;
16213 
16214 	bnxt_init_ethtool_link_settings(bp);
16215 	mutex_unlock(&bp->link_lock);
16216 	return 0;
16217 }
16218 
16219 static int bnxt_get_max_irq(struct pci_dev *pdev)
16220 {
16221 	u16 ctrl;
16222 
16223 	if (!pdev->msix_cap)
16224 		return 1;
16225 
16226 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16227 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16228 }
16229 
16230 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16231 				int *max_cp)
16232 {
16233 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16234 	int max_ring_grps = 0, max_irq;
16235 
16236 	*max_tx = hw_resc->max_tx_rings;
16237 	*max_rx = hw_resc->max_rx_rings;
16238 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16239 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16240 			bnxt_get_ulp_msix_num_in_use(bp),
16241 			hw_resc->max_stat_ctxs -
16242 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16243 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16244 		*max_cp = min_t(int, *max_cp, max_irq);
16245 	max_ring_grps = hw_resc->max_hw_ring_grps;
16246 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16247 		*max_cp -= 1;
16248 		*max_rx -= 2;
16249 	}
16250 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16251 		*max_rx >>= 1;
16252 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16253 		int rc;
16254 
16255 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16256 		if (rc) {
16257 			*max_rx = 0;
16258 			*max_tx = 0;
16259 		}
16260 		/* On P5 chips, max_cp output param should be available NQs */
16261 		*max_cp = max_irq;
16262 	}
16263 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16264 }
16265 
16266 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16267 {
16268 	int rx, tx, cp;
16269 
16270 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16271 	*max_rx = rx;
16272 	*max_tx = tx;
16273 	if (!rx || !tx || !cp)
16274 		return -ENOMEM;
16275 
16276 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16277 }
16278 
16279 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16280 			       bool shared)
16281 {
16282 	int rc;
16283 
16284 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16285 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16286 		/* Not enough rings, try disabling agg rings. */
16287 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16288 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16289 		if (rc) {
16290 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16291 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16292 			return rc;
16293 		}
16294 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16295 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16296 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16297 		bnxt_set_ring_params(bp);
16298 	}
16299 
16300 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16301 		int max_cp, max_stat, max_irq;
16302 
16303 		/* Reserve minimum resources for RoCE */
16304 		max_cp = bnxt_get_max_func_cp_rings(bp);
16305 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16306 		max_irq = bnxt_get_max_func_irqs(bp);
16307 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16308 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16309 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16310 			return 0;
16311 
16312 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16313 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16314 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16315 		max_cp = min_t(int, max_cp, max_irq);
16316 		max_cp = min_t(int, max_cp, max_stat);
16317 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16318 		if (rc)
16319 			rc = 0;
16320 	}
16321 	return rc;
16322 }
16323 
16324 /* In initial default shared ring setting, each shared ring must have a
16325  * RX/TX ring pair.
16326  */
16327 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16328 {
16329 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16330 	bp->rx_nr_rings = bp->cp_nr_rings;
16331 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16332 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
16333 }
16334 
16335 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16336 {
16337 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16338 	int avail_msix;
16339 
16340 	if (!bnxt_can_reserve_rings(bp))
16341 		return 0;
16342 
16343 	if (sh)
16344 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16345 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16346 	/* Reduce default rings on multi-port cards so that total default
16347 	 * rings do not exceed CPU count.
16348 	 */
16349 	if (bp->port_count > 1) {
16350 		int max_rings =
16351 			max_t(int, num_online_cpus() / bp->port_count, 1);
16352 
16353 		dflt_rings = min_t(int, dflt_rings, max_rings);
16354 	}
16355 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16356 	if (rc)
16357 		return rc;
16358 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16359 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16360 	if (sh)
16361 		bnxt_trim_dflt_sh_rings(bp);
16362 	else
16363 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16364 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
16365 
16366 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16367 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16368 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16369 
16370 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16371 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16372 	}
16373 
16374 	rc = __bnxt_reserve_rings(bp);
16375 	if (rc && rc != -ENODEV)
16376 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16377 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16378 	if (sh)
16379 		bnxt_trim_dflt_sh_rings(bp);
16380 
16381 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16382 	if (bnxt_need_reserve_rings(bp)) {
16383 		rc = __bnxt_reserve_rings(bp);
16384 		if (rc && rc != -ENODEV)
16385 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16386 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16387 	}
16388 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16389 		bp->rx_nr_rings++;
16390 		bp->cp_nr_rings++;
16391 	}
16392 	if (rc) {
16393 		bp->tx_nr_rings = 0;
16394 		bp->rx_nr_rings = 0;
16395 	}
16396 	return rc;
16397 }
16398 
16399 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16400 {
16401 	int rc;
16402 
16403 	if (bp->tx_nr_rings)
16404 		return 0;
16405 
16406 	bnxt_ulp_irq_stop(bp);
16407 	bnxt_clear_int_mode(bp);
16408 	rc = bnxt_set_dflt_rings(bp, true);
16409 	if (rc) {
16410 		if (BNXT_VF(bp) && rc == -ENODEV)
16411 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16412 		else
16413 			netdev_err(bp->dev, "Not enough rings available.\n");
16414 		goto init_dflt_ring_err;
16415 	}
16416 	rc = bnxt_init_int_mode(bp);
16417 	if (rc)
16418 		goto init_dflt_ring_err;
16419 
16420 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16421 
16422 	bnxt_set_dflt_rfs(bp);
16423 
16424 init_dflt_ring_err:
16425 	bnxt_ulp_irq_restart(bp, rc);
16426 	return rc;
16427 }
16428 
16429 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
16430 {
16431 	int rc;
16432 
16433 	netdev_ops_assert_locked(bp->dev);
16434 	bnxt_hwrm_func_qcaps(bp);
16435 
16436 	if (netif_running(bp->dev))
16437 		__bnxt_close_nic(bp, true, false);
16438 
16439 	bnxt_ulp_irq_stop(bp);
16440 	bnxt_clear_int_mode(bp);
16441 	rc = bnxt_init_int_mode(bp);
16442 	bnxt_ulp_irq_restart(bp, rc);
16443 
16444 	if (netif_running(bp->dev)) {
16445 		if (rc)
16446 			netif_close(bp->dev);
16447 		else
16448 			rc = bnxt_open_nic(bp, true, false);
16449 	}
16450 
16451 	return rc;
16452 }
16453 
16454 static int bnxt_init_mac_addr(struct bnxt *bp)
16455 {
16456 	int rc = 0;
16457 
16458 	if (BNXT_PF(bp)) {
16459 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16460 	} else {
16461 #ifdef CONFIG_BNXT_SRIOV
16462 		struct bnxt_vf_info *vf = &bp->vf;
16463 		bool strict_approval = true;
16464 
16465 		if (is_valid_ether_addr(vf->mac_addr)) {
16466 			/* overwrite netdev dev_addr with admin VF MAC */
16467 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16468 			/* Older PF driver or firmware may not approve this
16469 			 * correctly.
16470 			 */
16471 			strict_approval = false;
16472 		} else {
16473 			eth_hw_addr_random(bp->dev);
16474 		}
16475 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16476 #endif
16477 	}
16478 	return rc;
16479 }
16480 
16481 static void bnxt_vpd_read_info(struct bnxt *bp)
16482 {
16483 	struct pci_dev *pdev = bp->pdev;
16484 	unsigned int vpd_size, kw_len;
16485 	int pos, size;
16486 	u8 *vpd_data;
16487 
16488 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16489 	if (IS_ERR(vpd_data)) {
16490 		pci_warn(pdev, "Unable to read VPD\n");
16491 		return;
16492 	}
16493 
16494 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16495 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16496 	if (pos < 0)
16497 		goto read_sn;
16498 
16499 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16500 	memcpy(bp->board_partno, &vpd_data[pos], size);
16501 
16502 read_sn:
16503 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16504 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16505 					   &kw_len);
16506 	if (pos < 0)
16507 		goto exit;
16508 
16509 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16510 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16511 exit:
16512 	kfree(vpd_data);
16513 }
16514 
16515 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16516 {
16517 	struct pci_dev *pdev = bp->pdev;
16518 	u64 qword;
16519 
16520 	qword = pci_get_dsn(pdev);
16521 	if (!qword) {
16522 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16523 		return -EOPNOTSUPP;
16524 	}
16525 
16526 	put_unaligned_le64(qword, dsn);
16527 
16528 	bp->flags |= BNXT_FLAG_DSN_VALID;
16529 	return 0;
16530 }
16531 
16532 static int bnxt_map_db_bar(struct bnxt *bp)
16533 {
16534 	if (!bp->db_size)
16535 		return -ENODEV;
16536 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16537 	if (!bp->bar1)
16538 		return -ENOMEM;
16539 	return 0;
16540 }
16541 
16542 void bnxt_print_device_info(struct bnxt *bp)
16543 {
16544 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16545 		    board_info[bp->board_idx].name,
16546 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16547 
16548 	pcie_print_link_status(bp->pdev);
16549 }
16550 
16551 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16552 {
16553 	struct bnxt_hw_resc *hw_resc;
16554 	struct net_device *dev;
16555 	struct bnxt *bp;
16556 	int rc, max_irqs;
16557 
16558 	if (pci_is_bridge(pdev))
16559 		return -ENODEV;
16560 
16561 	if (!pdev->msix_cap) {
16562 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16563 		return -ENODEV;
16564 	}
16565 
16566 	/* Clear any pending DMA transactions from crash kernel
16567 	 * while loading driver in capture kernel.
16568 	 */
16569 	if (is_kdump_kernel()) {
16570 		pci_clear_master(pdev);
16571 		pcie_flr(pdev);
16572 	}
16573 
16574 	max_irqs = bnxt_get_max_irq(pdev);
16575 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16576 				 max_irqs);
16577 	if (!dev)
16578 		return -ENOMEM;
16579 
16580 	bp = netdev_priv(dev);
16581 	bp->board_idx = ent->driver_data;
16582 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16583 	bnxt_set_max_func_irqs(bp, max_irqs);
16584 
16585 	if (bnxt_vf_pciid(bp->board_idx))
16586 		bp->flags |= BNXT_FLAG_VF;
16587 
16588 	/* No devlink port registration in case of a VF */
16589 	if (BNXT_PF(bp))
16590 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16591 
16592 	rc = bnxt_init_board(pdev, dev);
16593 	if (rc < 0)
16594 		goto init_err_free;
16595 
16596 	dev->netdev_ops = &bnxt_netdev_ops;
16597 	dev->stat_ops = &bnxt_stat_ops;
16598 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16599 	dev->ethtool_ops = &bnxt_ethtool_ops;
16600 	pci_set_drvdata(pdev, dev);
16601 
16602 	rc = bnxt_alloc_hwrm_resources(bp);
16603 	if (rc)
16604 		goto init_err_pci_clean;
16605 
16606 	mutex_init(&bp->hwrm_cmd_lock);
16607 	mutex_init(&bp->link_lock);
16608 
16609 	rc = bnxt_fw_init_one_p1(bp);
16610 	if (rc)
16611 		goto init_err_pci_clean;
16612 
16613 	if (BNXT_PF(bp))
16614 		bnxt_vpd_read_info(bp);
16615 
16616 	if (BNXT_CHIP_P5_PLUS(bp)) {
16617 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16618 		if (BNXT_CHIP_P7(bp))
16619 			bp->flags |= BNXT_FLAG_CHIP_P7;
16620 	}
16621 
16622 	rc = bnxt_alloc_rss_indir_tbl(bp);
16623 	if (rc)
16624 		goto init_err_pci_clean;
16625 
16626 	rc = bnxt_fw_init_one_p2(bp);
16627 	if (rc)
16628 		goto init_err_pci_clean;
16629 
16630 	rc = bnxt_map_db_bar(bp);
16631 	if (rc) {
16632 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16633 			rc);
16634 		goto init_err_pci_clean;
16635 	}
16636 
16637 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16638 			   NETIF_F_TSO | NETIF_F_TSO6 |
16639 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16640 			   NETIF_F_GSO_IPXIP4 |
16641 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16642 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16643 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16644 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16645 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16646 
16647 	if (BNXT_SUPPORTS_TPA(bp))
16648 		dev->hw_features |= NETIF_F_LRO;
16649 
16650 	dev->hw_enc_features =
16651 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16652 			NETIF_F_TSO | NETIF_F_TSO6 |
16653 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16654 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16655 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16656 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16657 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16658 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16659 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16660 	else
16661 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16662 
16663 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16664 				    NETIF_F_GSO_GRE_CSUM;
16665 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16666 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16667 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16668 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16669 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16670 	if (BNXT_SUPPORTS_TPA(bp))
16671 		dev->hw_features |= NETIF_F_GRO_HW;
16672 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16673 	if (dev->features & NETIF_F_GRO_HW)
16674 		dev->features &= ~NETIF_F_LRO;
16675 	dev->priv_flags |= IFF_UNICAST_FLT;
16676 
16677 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16678 	if (bp->tso_max_segs)
16679 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16680 
16681 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16682 			    NETDEV_XDP_ACT_RX_SG;
16683 
16684 #ifdef CONFIG_BNXT_SRIOV
16685 	init_waitqueue_head(&bp->sriov_cfg_wait);
16686 #endif
16687 	if (BNXT_SUPPORTS_TPA(bp)) {
16688 		bp->gro_func = bnxt_gro_func_5730x;
16689 		if (BNXT_CHIP_P4(bp))
16690 			bp->gro_func = bnxt_gro_func_5731x;
16691 		else if (BNXT_CHIP_P5_PLUS(bp))
16692 			bp->gro_func = bnxt_gro_func_5750x;
16693 	}
16694 	if (!BNXT_CHIP_P4_PLUS(bp))
16695 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16696 
16697 	rc = bnxt_init_mac_addr(bp);
16698 	if (rc) {
16699 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16700 		rc = -EADDRNOTAVAIL;
16701 		goto init_err_pci_clean;
16702 	}
16703 
16704 	if (BNXT_PF(bp)) {
16705 		/* Read the adapter's DSN to use as the eswitch switch_id */
16706 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16707 	}
16708 
16709 	/* MTU range: 60 - FW defined max */
16710 	dev->min_mtu = ETH_ZLEN;
16711 	dev->max_mtu = bp->max_mtu;
16712 
16713 	rc = bnxt_probe_phy(bp, true);
16714 	if (rc)
16715 		goto init_err_pci_clean;
16716 
16717 	hw_resc = &bp->hw_resc;
16718 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16719 		       BNXT_L2_FLTR_MAX_FLTR;
16720 	/* Older firmware may not report these filters properly */
16721 	if (bp->max_fltr < BNXT_MAX_FLTR)
16722 		bp->max_fltr = BNXT_MAX_FLTR;
16723 	bnxt_init_l2_fltr_tbl(bp);
16724 	__bnxt_set_rx_skb_mode(bp, false);
16725 	bnxt_set_tpa_flags(bp);
16726 	bnxt_init_ring_params(bp);
16727 	bnxt_set_ring_params(bp);
16728 	bnxt_rdma_aux_device_init(bp);
16729 	rc = bnxt_set_dflt_rings(bp, true);
16730 	if (rc) {
16731 		if (BNXT_VF(bp) && rc == -ENODEV) {
16732 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16733 		} else {
16734 			netdev_err(bp->dev, "Not enough rings available.\n");
16735 			rc = -ENOMEM;
16736 		}
16737 		goto init_err_pci_clean;
16738 	}
16739 
16740 	bnxt_fw_init_one_p3(bp);
16741 
16742 	bnxt_init_dflt_coal(bp);
16743 
16744 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16745 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16746 
16747 	rc = bnxt_init_int_mode(bp);
16748 	if (rc)
16749 		goto init_err_pci_clean;
16750 
16751 	/* No TC has been set yet and rings may have been trimmed due to
16752 	 * limited MSIX, so we re-initialize the TX rings per TC.
16753 	 */
16754 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16755 
16756 	if (BNXT_PF(bp)) {
16757 		if (!bnxt_pf_wq) {
16758 			bnxt_pf_wq =
16759 				create_singlethread_workqueue("bnxt_pf_wq");
16760 			if (!bnxt_pf_wq) {
16761 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16762 				rc = -ENOMEM;
16763 				goto init_err_pci_clean;
16764 			}
16765 		}
16766 		rc = bnxt_init_tc(bp);
16767 		if (rc)
16768 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16769 				   rc);
16770 	}
16771 
16772 	bnxt_inv_fw_health_reg(bp);
16773 	rc = bnxt_dl_register(bp);
16774 	if (rc)
16775 		goto init_err_dl;
16776 
16777 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16778 
16779 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16780 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16781 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16782 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16783 	dev->request_ops_lock = true;
16784 	dev->netmem_tx = true;
16785 
16786 	rc = register_netdev(dev);
16787 	if (rc)
16788 		goto init_err_cleanup;
16789 
16790 	bnxt_dl_fw_reporters_create(bp);
16791 
16792 	bnxt_rdma_aux_device_add(bp);
16793 
16794 	bnxt_print_device_info(bp);
16795 
16796 	pci_save_state(pdev);
16797 
16798 	return 0;
16799 init_err_cleanup:
16800 	bnxt_rdma_aux_device_uninit(bp);
16801 	bnxt_dl_unregister(bp);
16802 init_err_dl:
16803 	bnxt_shutdown_tc(bp);
16804 	bnxt_clear_int_mode(bp);
16805 
16806 init_err_pci_clean:
16807 	bnxt_hwrm_func_drv_unrgtr(bp);
16808 	bnxt_free_hwrm_resources(bp);
16809 	bnxt_hwmon_uninit(bp);
16810 	bnxt_ethtool_free(bp);
16811 	bnxt_ptp_clear(bp);
16812 	kfree(bp->ptp_cfg);
16813 	bp->ptp_cfg = NULL;
16814 	kfree(bp->fw_health);
16815 	bp->fw_health = NULL;
16816 	bnxt_cleanup_pci(bp);
16817 	bnxt_free_ctx_mem(bp, true);
16818 	bnxt_free_crash_dump_mem(bp);
16819 	kfree(bp->rss_indir_tbl);
16820 	bp->rss_indir_tbl = NULL;
16821 
16822 init_err_free:
16823 	free_netdev(dev);
16824 	return rc;
16825 }
16826 
16827 static void bnxt_shutdown(struct pci_dev *pdev)
16828 {
16829 	struct net_device *dev = pci_get_drvdata(pdev);
16830 	struct bnxt *bp;
16831 
16832 	if (!dev)
16833 		return;
16834 
16835 	rtnl_lock();
16836 	netdev_lock(dev);
16837 	bp = netdev_priv(dev);
16838 	if (!bp)
16839 		goto shutdown_exit;
16840 
16841 	if (netif_running(dev))
16842 		netif_close(dev);
16843 
16844 	bnxt_ptp_clear(bp);
16845 	bnxt_clear_int_mode(bp);
16846 	pci_disable_device(pdev);
16847 
16848 	if (system_state == SYSTEM_POWER_OFF) {
16849 		pci_wake_from_d3(pdev, bp->wol);
16850 		pci_set_power_state(pdev, PCI_D3hot);
16851 	}
16852 
16853 shutdown_exit:
16854 	netdev_unlock(dev);
16855 	rtnl_unlock();
16856 }
16857 
16858 #ifdef CONFIG_PM_SLEEP
16859 static int bnxt_suspend(struct device *device)
16860 {
16861 	struct net_device *dev = dev_get_drvdata(device);
16862 	struct bnxt *bp = netdev_priv(dev);
16863 	int rc = 0;
16864 
16865 	bnxt_ulp_stop(bp);
16866 
16867 	netdev_lock(dev);
16868 	if (netif_running(dev)) {
16869 		netif_device_detach(dev);
16870 		rc = bnxt_close(dev);
16871 	}
16872 	bnxt_hwrm_func_drv_unrgtr(bp);
16873 	bnxt_ptp_clear(bp);
16874 	pci_disable_device(bp->pdev);
16875 	bnxt_free_ctx_mem(bp, false);
16876 	netdev_unlock(dev);
16877 	return rc;
16878 }
16879 
16880 static int bnxt_resume(struct device *device)
16881 {
16882 	struct net_device *dev = dev_get_drvdata(device);
16883 	struct bnxt *bp = netdev_priv(dev);
16884 	int rc = 0;
16885 
16886 	netdev_lock(dev);
16887 	rc = pci_enable_device(bp->pdev);
16888 	if (rc) {
16889 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16890 			   rc);
16891 		goto resume_exit;
16892 	}
16893 	pci_set_master(bp->pdev);
16894 	if (bnxt_hwrm_ver_get(bp)) {
16895 		rc = -ENODEV;
16896 		goto resume_exit;
16897 	}
16898 	rc = bnxt_hwrm_func_reset(bp);
16899 	if (rc) {
16900 		rc = -EBUSY;
16901 		goto resume_exit;
16902 	}
16903 
16904 	rc = bnxt_hwrm_func_qcaps(bp);
16905 	if (rc)
16906 		goto resume_exit;
16907 
16908 	bnxt_clear_reservations(bp, true);
16909 
16910 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16911 		rc = -ENODEV;
16912 		goto resume_exit;
16913 	}
16914 	if (bp->fw_crash_mem)
16915 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16916 
16917 	if (bnxt_ptp_init(bp)) {
16918 		kfree(bp->ptp_cfg);
16919 		bp->ptp_cfg = NULL;
16920 	}
16921 	bnxt_get_wol_settings(bp);
16922 	if (netif_running(dev)) {
16923 		rc = bnxt_open(dev);
16924 		if (!rc)
16925 			netif_device_attach(dev);
16926 	}
16927 
16928 resume_exit:
16929 	netdev_unlock(bp->dev);
16930 	bnxt_ulp_start(bp, rc);
16931 	if (!rc)
16932 		bnxt_reenable_sriov(bp);
16933 	return rc;
16934 }
16935 
16936 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16937 #define BNXT_PM_OPS (&bnxt_pm_ops)
16938 
16939 #else
16940 
16941 #define BNXT_PM_OPS NULL
16942 
16943 #endif /* CONFIG_PM_SLEEP */
16944 
16945 /**
16946  * bnxt_io_error_detected - called when PCI error is detected
16947  * @pdev: Pointer to PCI device
16948  * @state: The current pci connection state
16949  *
16950  * This function is called after a PCI bus error affecting
16951  * this device has been detected.
16952  */
16953 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16954 					       pci_channel_state_t state)
16955 {
16956 	struct net_device *netdev = pci_get_drvdata(pdev);
16957 	struct bnxt *bp = netdev_priv(netdev);
16958 	bool abort = false;
16959 
16960 	netdev_info(netdev, "PCI I/O error detected\n");
16961 
16962 	bnxt_ulp_stop(bp);
16963 
16964 	netdev_lock(netdev);
16965 	netif_device_detach(netdev);
16966 
16967 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16968 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16969 		abort = true;
16970 	}
16971 
16972 	if (abort || state == pci_channel_io_perm_failure) {
16973 		netdev_unlock(netdev);
16974 		return PCI_ERS_RESULT_DISCONNECT;
16975 	}
16976 
16977 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16978 	 * so we disable bus master to prevent any potential bad DMAs before
16979 	 * freeing kernel memory.
16980 	 */
16981 	if (state == pci_channel_io_frozen) {
16982 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16983 		bnxt_fw_fatal_close(bp);
16984 	}
16985 
16986 	if (netif_running(netdev))
16987 		__bnxt_close_nic(bp, true, true);
16988 
16989 	if (pci_is_enabled(pdev))
16990 		pci_disable_device(pdev);
16991 	bnxt_free_ctx_mem(bp, false);
16992 	netdev_unlock(netdev);
16993 
16994 	/* Request a slot reset. */
16995 	return PCI_ERS_RESULT_NEED_RESET;
16996 }
16997 
16998 /**
16999  * bnxt_io_slot_reset - called after the pci bus has been reset.
17000  * @pdev: Pointer to PCI device
17001  *
17002  * Restart the card from scratch, as if from a cold-boot.
17003  * At this point, the card has experienced a hard reset,
17004  * followed by fixups by BIOS, and has its config space
17005  * set up identically to what it was at cold boot.
17006  */
17007 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
17008 {
17009 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
17010 	struct net_device *netdev = pci_get_drvdata(pdev);
17011 	struct bnxt *bp = netdev_priv(netdev);
17012 	int retry = 0;
17013 	int err = 0;
17014 	int off;
17015 
17016 	netdev_info(bp->dev, "PCI Slot Reset\n");
17017 
17018 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
17019 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
17020 		msleep(900);
17021 
17022 	netdev_lock(netdev);
17023 
17024 	if (pci_enable_device(pdev)) {
17025 		dev_err(&pdev->dev,
17026 			"Cannot re-enable PCI device after reset.\n");
17027 	} else {
17028 		pci_set_master(pdev);
17029 		/* Upon fatal error, our device internal logic that latches to
17030 		 * BAR value is getting reset and will restore only upon
17031 		 * rewriting the BARs.
17032 		 *
17033 		 * As pci_restore_state() does not re-write the BARs if the
17034 		 * value is same as saved value earlier, driver needs to
17035 		 * write the BARs to 0 to force restore, in case of fatal error.
17036 		 */
17037 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
17038 				       &bp->state)) {
17039 			for (off = PCI_BASE_ADDRESS_0;
17040 			     off <= PCI_BASE_ADDRESS_5; off += 4)
17041 				pci_write_config_dword(bp->pdev, off, 0);
17042 		}
17043 		pci_restore_state(pdev);
17044 		pci_save_state(pdev);
17045 
17046 		bnxt_inv_fw_health_reg(bp);
17047 		bnxt_try_map_fw_health_reg(bp);
17048 
17049 		/* In some PCIe AER scenarios, firmware may take up to
17050 		 * 10 seconds to become ready in the worst case.
17051 		 */
17052 		do {
17053 			err = bnxt_try_recover_fw(bp);
17054 			if (!err)
17055 				break;
17056 			retry++;
17057 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
17058 
17059 		if (err) {
17060 			dev_err(&pdev->dev, "Firmware not ready\n");
17061 			goto reset_exit;
17062 		}
17063 
17064 		err = bnxt_hwrm_func_reset(bp);
17065 		if (!err)
17066 			result = PCI_ERS_RESULT_RECOVERED;
17067 
17068 		/* IRQ will be initialized later in bnxt_io_resume */
17069 		bnxt_ulp_irq_stop(bp);
17070 		bnxt_clear_int_mode(bp);
17071 	}
17072 
17073 reset_exit:
17074 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
17075 	bnxt_clear_reservations(bp, true);
17076 	netdev_unlock(netdev);
17077 
17078 	return result;
17079 }
17080 
17081 /**
17082  * bnxt_io_resume - called when traffic can start flowing again.
17083  * @pdev: Pointer to PCI device
17084  *
17085  * This callback is called when the error recovery driver tells
17086  * us that its OK to resume normal operation.
17087  */
17088 static void bnxt_io_resume(struct pci_dev *pdev)
17089 {
17090 	struct net_device *netdev = pci_get_drvdata(pdev);
17091 	struct bnxt *bp = netdev_priv(netdev);
17092 	int err;
17093 
17094 	netdev_info(bp->dev, "PCI Slot Resume\n");
17095 	netdev_lock(netdev);
17096 
17097 	err = bnxt_hwrm_func_qcaps(bp);
17098 	if (!err) {
17099 		if (netif_running(netdev)) {
17100 			err = bnxt_open(netdev);
17101 		} else {
17102 			err = bnxt_reserve_rings(bp, true);
17103 			if (!err)
17104 				err = bnxt_init_int_mode(bp);
17105 		}
17106 	}
17107 
17108 	if (!err)
17109 		netif_device_attach(netdev);
17110 
17111 	netdev_unlock(netdev);
17112 	bnxt_ulp_start(bp, err);
17113 	if (!err)
17114 		bnxt_reenable_sriov(bp);
17115 }
17116 
17117 static const struct pci_error_handlers bnxt_err_handler = {
17118 	.error_detected	= bnxt_io_error_detected,
17119 	.slot_reset	= bnxt_io_slot_reset,
17120 	.resume		= bnxt_io_resume
17121 };
17122 
17123 static struct pci_driver bnxt_pci_driver = {
17124 	.name		= DRV_MODULE_NAME,
17125 	.id_table	= bnxt_pci_tbl,
17126 	.probe		= bnxt_init_one,
17127 	.remove		= bnxt_remove_one,
17128 	.shutdown	= bnxt_shutdown,
17129 	.driver.pm	= BNXT_PM_OPS,
17130 	.err_handler	= &bnxt_err_handler,
17131 #if defined(CONFIG_BNXT_SRIOV)
17132 	.sriov_configure = bnxt_sriov_configure,
17133 #endif
17134 };
17135 
17136 static int __init bnxt_init(void)
17137 {
17138 	int err;
17139 
17140 	bnxt_debug_init();
17141 	err = pci_register_driver(&bnxt_pci_driver);
17142 	if (err) {
17143 		bnxt_debug_exit();
17144 		return err;
17145 	}
17146 
17147 	return 0;
17148 }
17149 
17150 static void __exit bnxt_exit(void)
17151 {
17152 	pci_unregister_driver(&bnxt_pci_driver);
17153 	if (bnxt_pf_wq)
17154 		destroy_workqueue(bnxt_pf_wq);
17155 	bnxt_debug_exit();
17156 }
17157 
17158 module_init(bnxt_init);
17159 module_exit(bnxt_exit);
17160