xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 364b88b54fc260d22ee318bae862c2d7d8c31dc6)
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 #include <linux/bnxt/ulp.h>
63 
64 #include "bnxt.h"
65 #include "bnxt_hwrm.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 #include "bnxt_gso.h"
78 #include <net/tso.h>
79 
80 #define BNXT_TX_TIMEOUT		(5 * HZ)
81 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
82 				 NETIF_MSG_TX_ERR)
83 
84 MODULE_IMPORT_NS("NETDEV_INTERNAL");
85 MODULE_LICENSE("GPL");
86 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
87 
88 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
89 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
90 
91 #define BNXT_TX_PUSH_THRESH 164
92 
93 /* indexed by enum board_idx */
94 static const struct {
95 	char *name;
96 } board_info[] = {
97 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
98 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
99 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
100 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
101 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
102 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
103 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
104 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
105 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
106 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
108 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
109 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
110 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
111 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
112 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
113 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
114 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
115 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
116 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
117 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
118 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
119 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
120 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
121 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
122 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
123 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
124 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
125 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
126 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
127 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
128 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
129 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
130 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
131 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
132 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
133 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
134 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
135 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
136 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
137 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
138 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
139 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
140 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
141 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
142 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
143 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
144 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
145 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
146 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
147 	[NETXTREME_E_P7_VF_HV] = { "Broadcom BCM5760X Virtual Function for Hyper-V" },
148 };
149 
150 static const struct pci_device_id bnxt_pci_tbl[] = {
151 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
152 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
153 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
155 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
159 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
166 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
168 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
169 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
170 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
171 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
172 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
173 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
174 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
175 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
178 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
179 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
180 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
181 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
182 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
183 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
184 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
185 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
188 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
189 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
190 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
191 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
192 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
193 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
194 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
195 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
196 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
197 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
198 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
199 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
200 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
201 #ifdef CONFIG_BNXT_SRIOV
202 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
203 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
206 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
207 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
208 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
209 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
210 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
211 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
212 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
214 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
215 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
216 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
217 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
218 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
219 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
220 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
221 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
222 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
223 	{ PCI_VDEVICE(BROADCOM, 0x181b), .driver_data = NETXTREME_E_P7_VF_HV },
224 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
225 #endif
226 	{ 0 }
227 };
228 
229 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
230 
231 static const u16 bnxt_vf_req_snif[] = {
232 	HWRM_FUNC_CFG,
233 	HWRM_FUNC_VF_CFG,
234 	HWRM_PORT_PHY_QCFG,
235 	HWRM_CFA_L2_FILTER_ALLOC,
236 };
237 
238 static const u16 bnxt_async_events_arr[] = {
239 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
240 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
241 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
242 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
243 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
244 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
245 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
246 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
247 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
248 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
249 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
250 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
251 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
252 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
253 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
254 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
255 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
256 };
257 
258 const u16 bnxt_bstore_to_trace[] = {
259 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
260 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
261 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
262 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
263 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
264 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
265 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
266 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
267 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
268 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
269 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
270 	[BNXT_CTX_KONG]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_AFM_KONG_HWRM_TRACE,
271 	[BNXT_CTX_QPC]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ERR_QPC_TRACE,
272 };
273 
274 static struct workqueue_struct *bnxt_pf_wq;
275 
276 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
277 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
278 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
279 
280 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
281 	.ports = {
282 		.src = 0,
283 		.dst = 0,
284 	},
285 	.addrs = {
286 		.v6addrs = {
287 			.src = BNXT_IPV6_MASK_NONE,
288 			.dst = BNXT_IPV6_MASK_NONE,
289 		},
290 	},
291 };
292 
293 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
294 	.ports = {
295 		.src = cpu_to_be16(0xffff),
296 		.dst = cpu_to_be16(0xffff),
297 	},
298 	.addrs = {
299 		.v6addrs = {
300 			.src = BNXT_IPV6_MASK_ALL,
301 			.dst = BNXT_IPV6_MASK_ALL,
302 		},
303 	},
304 };
305 
306 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
307 	.ports = {
308 		.src = cpu_to_be16(0xffff),
309 		.dst = cpu_to_be16(0xffff),
310 	},
311 	.addrs = {
312 		.v4addrs = {
313 			.src = cpu_to_be32(0xffffffff),
314 			.dst = cpu_to_be32(0xffffffff),
315 		},
316 	},
317 };
318 
319 static bool bnxt_vf_pciid(enum board_idx idx)
320 {
321 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
322 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
323 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
324 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF ||
325 		idx == NETXTREME_E_P7_VF_HV);
326 }
327 
328 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
329 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
330 
331 #define BNXT_DB_CQ(db, idx)						\
332 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
333 
334 #define BNXT_DB_NQ_P5(db, idx)						\
335 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
336 		    (db)->doorbell)
337 
338 #define BNXT_DB_NQ_P7(db, idx)						\
339 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
340 		    DB_RING_IDX(db, idx), (db)->doorbell)
341 
342 #define BNXT_DB_CQ_ARM(db, idx)						\
343 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
344 
345 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
346 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
347 		    DB_RING_IDX(db, idx), (db)->doorbell)
348 
349 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
350 {
351 	if (bp->flags & BNXT_FLAG_CHIP_P7)
352 		BNXT_DB_NQ_P7(db, idx);
353 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
354 		BNXT_DB_NQ_P5(db, idx);
355 	else
356 		BNXT_DB_CQ(db, idx);
357 }
358 
359 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
360 {
361 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
362 		BNXT_DB_NQ_ARM_P5(db, idx);
363 	else
364 		BNXT_DB_CQ_ARM(db, idx);
365 }
366 
367 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
368 {
369 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
370 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
371 			    DB_RING_IDX(db, idx), db->doorbell);
372 	else
373 		BNXT_DB_CQ(db, idx);
374 }
375 
376 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
377 {
378 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
379 		return;
380 
381 	if (BNXT_PF(bp))
382 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
383 	else
384 		schedule_delayed_work(&bp->fw_reset_task, delay);
385 }
386 
387 static void __bnxt_queue_sp_work(struct bnxt *bp)
388 {
389 	if (BNXT_PF(bp))
390 		queue_work(bnxt_pf_wq, &bp->sp_task);
391 	else
392 		schedule_work(&bp->sp_task);
393 }
394 
395 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
396 {
397 	set_bit(event, &bp->sp_event);
398 	__bnxt_queue_sp_work(bp);
399 }
400 
401 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
402 {
403 	if (!rxr->bnapi->in_reset) {
404 		rxr->bnapi->in_reset = true;
405 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
406 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
407 		else
408 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
409 		__bnxt_queue_sp_work(bp);
410 	}
411 	rxr->rx_next_cons = 0xffff;
412 }
413 
414 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
415 			  u16 curr)
416 {
417 	struct bnxt_napi *bnapi = txr->bnapi;
418 
419 	if (bnapi->tx_fault)
420 		return;
421 
422 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
423 		   txr->txq_index, txr->tx_hw_cons,
424 		   txr->tx_cons, txr->tx_prod, curr);
425 	WARN_ON_ONCE(1);
426 	bnapi->tx_fault = 1;
427 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
428 }
429 
430 const u16 bnxt_lhint_arr[] = {
431 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
432 	TX_BD_FLAGS_LHINT_512_TO_1023,
433 	TX_BD_FLAGS_LHINT_1024_TO_2047,
434 	TX_BD_FLAGS_LHINT_1024_TO_2047,
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 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
447 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
448 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
449 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
450 };
451 
452 u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
453 {
454 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
455 
456 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
457 		return 0;
458 
459 	return md_dst->u.port_info.port_id;
460 }
461 
462 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
463 			     u16 prod)
464 {
465 	/* If the most recent BD has its completion suppressed, unset the bit
466 	 * so that a completion is generated, otherwise nothing is left to
467 	 * clean the ring and wake the queue.
468 	 */
469 	if (txr->kick_txbd0) {
470 		txr->kick_txbd0->tx_bd_len_flags_type &=
471 			cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
472 		txr->kick_txbd0 = NULL;
473 	}
474 
475 	/* Sync BD data before updating doorbell */
476 	wmb();
477 	bnxt_db_write(bp, &txr->tx_db, prod);
478 	txr->kick_pending = 0;
479 }
480 
481 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
482 {
483 	struct bnxt *bp = netdev_priv(dev);
484 	struct tx_bd *txbd, *txbd0;
485 	struct tx_bd_ext *txbd1;
486 	struct netdev_queue *txq;
487 	int i;
488 	dma_addr_t mapping;
489 	unsigned int length;
490 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
491 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
492 	struct pci_dev *pdev = bp->pdev;
493 	u16 prod, last_frag, txts_prod;
494 	struct bnxt_tx_ring_info *txr;
495 	struct bnxt_sw_tx_bd *tx_buf;
496 	__le32 lflags = 0;
497 	skb_frag_t *frag;
498 
499 	i = skb_get_queue_mapping(skb);
500 	if (unlikely(i >= bp->tx_nr_rings)) {
501 		dev_kfree_skb_any(skb);
502 		dev_core_stats_tx_dropped_inc(dev);
503 		return NETDEV_TX_OK;
504 	}
505 
506 	txq = netdev_get_tx_queue(dev, i);
507 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
508 	prod = txr->tx_prod;
509 
510 #if (MAX_SKB_FRAGS > TX_MAX_FRAGS)
511 	if (skb_shinfo(skb)->nr_frags > TX_MAX_FRAGS) {
512 		netdev_warn_once(dev, "SKB has too many (%d) fragments, max supported is %d.  SKB will be linearized.\n",
513 				 skb_shinfo(skb)->nr_frags, TX_MAX_FRAGS);
514 		if (skb_linearize(skb))
515 			goto tx_free;
516 	}
517 #endif
518 	if (skb_is_gso(skb) &&
519 	    (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) &&
520 	    !(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) {
521 		int rc = bnxt_sw_udp_gso_xmit(bp, txr, txq, skb);
522 
523 		/* if SW USO queued a packet, the doorbell will be written
524 		 * below and there is no reason to track the last BD with
525 		 * suppressed completions
526 		 */
527 		if (rc > 0)
528 			txr->kick_txbd0 = NULL;
529 
530 		/* if a packet was queued by SW USO or a doorbell was pending
531 		 * from a previous xmit that was deferred, write the doorbell.
532 		 */
533 		if (rc > 0 || txr->kick_pending)
534 			bnxt_txr_db_kick(bp, txr, txr->tx_prod);
535 
536 		return rc < 0 ? NETDEV_TX_BUSY : NETDEV_TX_OK;
537 	}
538 
539 	/* Pad after the SW USO branch: bnxt_sw_udp_gso_xmit() would
540 	 * otherwise account the padding as UDP payload.
541 	 * Must be done before skb_shinfo(skb)->nr_frags is sampled,
542 	 * because skb_put_padto() might linearize the skb.
543 	 */
544 	if (skb_put_padto(skb, BNXT_MIN_PKT_SIZE)) {
545 		/* SKB already freed. */
546 		goto tx_kick_pending;
547 	}
548 
549 	free_size = bnxt_tx_avail(bp, txr);
550 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
551 		/* We must have raced with NAPI cleanup */
552 		if (net_ratelimit() && txr->kick_pending)
553 			netif_warn(bp, tx_err, dev,
554 				   "bnxt: ring busy w/ flush pending!\n");
555 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
556 					bp->tx_wake_thresh))
557 			return NETDEV_TX_BUSY;
558 	}
559 
560 	length = skb->len;
561 	len = skb_headlen(skb);
562 	last_frag = skb_shinfo(skb)->nr_frags;
563 
564 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
565 
566 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
567 	tx_buf->skb = skb;
568 	tx_buf->nr_frags = last_frag;
569 
570 	vlan_tag_flags = 0;
571 	cfa_action = bnxt_xmit_get_cfa_action(skb);
572 	if (skb_vlan_tag_present(skb)) {
573 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
574 				 skb_vlan_tag_get(skb);
575 		/* Currently supports 8021Q, 8021AD vlan offloads
576 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
577 		 */
578 		if (skb->vlan_proto == htons(ETH_P_8021Q))
579 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
580 	}
581 
582 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
583 	    ptp->tx_tstamp_en) {
584 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
585 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
586 			tx_buf->is_ts_pkt = 1;
587 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
588 		} else if (!skb_is_gso(skb)) {
589 			u16 seq_id, hdr_off;
590 
591 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
592 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
593 				if (vlan_tag_flags)
594 					hdr_off += VLAN_HLEN;
595 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
596 				tx_buf->is_ts_pkt = 1;
597 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
598 
599 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
600 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
601 				tx_buf->txts_prod = txts_prod;
602 			}
603 		}
604 	}
605 	if (unlikely(skb->no_fcs))
606 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
607 
608 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
609 	    skb_frags_readable(skb) && !lflags) {
610 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
611 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
612 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
613 		void __iomem *db = txr->tx_db.doorbell;
614 		void *pdata = tx_push_buf->data;
615 		u64 *end;
616 		int j, push_len;
617 
618 		/* Set COAL_NOW to be ready quickly for the next push */
619 		tx_push->tx_bd_len_flags_type =
620 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
621 					TX_BD_TYPE_LONG_TX_BD |
622 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
623 					TX_BD_FLAGS_COAL_NOW |
624 					TX_BD_FLAGS_PACKET_END |
625 					TX_BD_CNT(2));
626 
627 		if (skb->ip_summed == CHECKSUM_PARTIAL)
628 			tx_push1->tx_bd_hsize_lflags =
629 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
630 		else
631 			tx_push1->tx_bd_hsize_lflags = 0;
632 
633 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
634 		tx_push1->tx_bd_cfa_action =
635 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
636 
637 		end = pdata + length;
638 		end = PTR_ALIGN(end, 8) - 1;
639 		*end = 0;
640 
641 		skb_copy_from_linear_data(skb, pdata, len);
642 		pdata += len;
643 		for (j = 0; j < last_frag; j++) {
644 			void *fptr;
645 
646 			frag = &skb_shinfo(skb)->frags[j];
647 			fptr = skb_frag_address_safe(frag);
648 			if (!fptr)
649 				goto normal_tx;
650 
651 			memcpy(pdata, fptr, skb_frag_size(frag));
652 			pdata += skb_frag_size(frag);
653 		}
654 
655 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
656 		txbd->tx_bd_haddr = txr->data_mapping;
657 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
658 		prod = NEXT_TX(prod);
659 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
660 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
661 		memcpy(txbd, tx_push1, sizeof(*txbd));
662 		prod = NEXT_TX(prod);
663 		tx_push->doorbell =
664 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
665 				    DB_RING_IDX(&txr->tx_db, prod));
666 		WRITE_ONCE(txr->tx_prod, prod);
667 
668 		tx_buf->is_push = 1;
669 		netdev_tx_sent_queue(txq, skb->len);
670 		wmb();	/* Sync is_push and byte queue before pushing data */
671 
672 		push_len = (length + sizeof(*tx_push) + 7) / 8;
673 		if (push_len > 16) {
674 			__iowrite64_copy(db, tx_push_buf, 16);
675 			__iowrite32_copy(db + 4, tx_push_buf + 1,
676 					 (push_len - 16) << 1);
677 		} else {
678 			__iowrite64_copy(db, tx_push_buf, push_len);
679 		}
680 
681 		goto tx_done;
682 	}
683 
684 normal_tx:
685 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
686 
687 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
688 		goto tx_free;
689 
690 	dma_unmap_addr_set(tx_buf, mapping, mapping);
691 	dma_unmap_len_set(tx_buf, len, len);
692 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
693 		TX_BD_CNT(last_frag + 2);
694 
695 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
696 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
697 
698 	prod = NEXT_TX(prod);
699 	txbd1 = bnxt_init_ext_bd(bp, txr, prod, lflags, vlan_tag_flags,
700 				 cfa_action);
701 
702 	if (skb_is_gso(skb)) {
703 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
704 		u32 hdr_len;
705 
706 		if (skb->encapsulation) {
707 			if (udp_gso)
708 				hdr_len = skb_inner_transport_offset(skb) +
709 					  sizeof(struct udphdr);
710 			else
711 				hdr_len = skb_inner_tcp_all_headers(skb);
712 		} else if (udp_gso) {
713 			hdr_len = skb_transport_offset(skb) +
714 				  sizeof(struct udphdr);
715 		} else {
716 			hdr_len = skb_tcp_all_headers(skb);
717 		}
718 
719 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
720 					TX_BD_FLAGS_T_IPID |
721 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
722 		length = skb_shinfo(skb)->gso_size;
723 		txbd1->tx_bd_mss = cpu_to_le32(length);
724 		length += hdr_len;
725 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
726 		txbd1->tx_bd_hsize_lflags |=
727 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
728 	}
729 
730 	length >>= 9;
731 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
732 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
733 				     skb->len);
734 		i = 0;
735 		goto tx_dma_error;
736 	}
737 	flags |= bnxt_lhint_arr[length];
738 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
739 
740 	txbd0 = txbd;
741 	for (i = 0; i < last_frag; i++) {
742 		frag = &skb_shinfo(skb)->frags[i];
743 		prod = NEXT_TX(prod);
744 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
745 
746 		len = skb_frag_size(frag);
747 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
748 					   DMA_TO_DEVICE);
749 
750 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
751 			goto tx_dma_error;
752 
753 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
754 		netmem_dma_unmap_addr_set(skb_frag_netmem(frag), tx_buf,
755 					  mapping, mapping);
756 		dma_unmap_len_set(tx_buf, len, len);
757 
758 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
759 
760 		flags = len << TX_BD_LEN_SHIFT;
761 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
762 	}
763 
764 	txbd->tx_bd_len_flags_type |= cpu_to_le32(TX_BD_FLAGS_PACKET_END);
765 
766 	netdev_tx_sent_queue(txq, skb->len);
767 
768 	skb_tx_timestamp(skb);
769 
770 	prod = NEXT_TX(prod);
771 	WRITE_ONCE(txr->tx_prod, prod);
772 
773 	txr->kick_txbd0 = NULL;
774 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
775 		bnxt_txr_db_kick(bp, txr, prod);
776 	} else {
777 		if (free_size >= bp->tx_wake_thresh) {
778 			txbd0->tx_bd_len_flags_type |=
779 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
780 			txr->kick_txbd0 = txbd0;
781 		}
782 		txr->kick_pending = 1;
783 	}
784 
785 tx_done:
786 
787 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
788 		if (txr->kick_pending)
789 			bnxt_txr_db_kick(bp, txr, prod);
790 
791 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
792 				   bp->tx_wake_thresh);
793 	}
794 	return NETDEV_TX_OK;
795 
796 tx_dma_error:
797 	last_frag = i;
798 
799 	/* start back at beginning and unmap skb */
800 	prod = txr->tx_prod;
801 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
802 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
803 			 skb_headlen(skb), DMA_TO_DEVICE);
804 	prod = NEXT_TX(prod);
805 
806 	/* unmap remaining mapped pages */
807 	for (i = 0; i < last_frag; i++) {
808 		prod = NEXT_TX(prod);
809 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
810 		frag = &skb_shinfo(skb)->frags[i];
811 		netmem_dma_unmap_page_attrs(&pdev->dev,
812 					    dma_unmap_addr(tx_buf, mapping),
813 					    skb_frag_size(frag),
814 					    DMA_TO_DEVICE, 0);
815 	}
816 
817 tx_free:
818 	dev_kfree_skb_any(skb);
819 tx_kick_pending:
820 	if (BNXT_TX_PTP_IS_SET(lflags)) {
821 		txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].is_ts_pkt = 0;
822 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
823 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
824 			/* set SKB to err so PTP worker will clean up */
825 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
826 	}
827 	if (txr->kick_pending)
828 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
829 	txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].skb = NULL;
830 	dev_core_stats_tx_dropped_inc(dev);
831 	return NETDEV_TX_OK;
832 }
833 
834 /* Returns true if some remaining TX packets not processed. */
835 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
836 			  int budget)
837 {
838 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
839 	struct pci_dev *pdev = bp->pdev;
840 	u16 hw_cons = txr->tx_hw_cons;
841 	unsigned int tx_bytes = 0;
842 	u16 cons = txr->tx_cons;
843 	unsigned int dma_len;
844 	dma_addr_t dma_addr;
845 	int tx_pkts = 0;
846 	bool rc = false;
847 
848 	while (RING_TX(bp, cons) != hw_cons) {
849 		struct bnxt_sw_tx_bd *tx_buf, *head_buf;
850 		struct sk_buff *skb;
851 		bool is_ts_pkt;
852 		int j, last;
853 
854 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
855 		head_buf = tx_buf;
856 		skb = tx_buf->skb;
857 
858 		if (unlikely(!skb)) {
859 			bnxt_sched_reset_txr(bp, txr, cons);
860 			return rc;
861 		}
862 
863 		is_ts_pkt = tx_buf->is_ts_pkt;
864 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
865 			rc = true;
866 			break;
867 		}
868 
869 		cons = NEXT_TX(cons);
870 		tx_pkts++;
871 		tx_bytes += skb->len;
872 		tx_buf->skb = NULL;
873 		tx_buf->is_ts_pkt = 0;
874 
875 		if (tx_buf->is_push) {
876 			tx_buf->is_push = 0;
877 			goto next_tx_int;
878 		}
879 
880 		if (dma_unmap_len(tx_buf, len)) {
881 			dma_addr = dma_unmap_addr(tx_buf, mapping);
882 			dma_len = dma_unmap_len(tx_buf, len);
883 
884 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
885 					 DMA_TO_DEVICE);
886 		}
887 
888 		last = tx_buf->nr_frags;
889 
890 		for (j = 0; j < last; j++) {
891 			cons = NEXT_TX(cons);
892 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
893 			if (dma_unmap_len(tx_buf, len)) {
894 				dma_addr = dma_unmap_addr(tx_buf, mapping);
895 				dma_len = dma_unmap_len(tx_buf, len);
896 
897 				netmem_dma_unmap_page_attrs(&pdev->dev,
898 							    dma_addr, dma_len,
899 							    DMA_TO_DEVICE, 0);
900 			}
901 		}
902 
903 		if (unlikely(head_buf->is_sw_gso)) {
904 			u16 inline_cons = txr->tx_inline_cons + 1;
905 
906 			WRITE_ONCE(txr->tx_inline_cons, inline_cons);
907 			if (head_buf->is_sw_gso == BNXT_SW_GSO_LAST) {
908 				tso_dma_map_complete(&pdev->dev,
909 						     &head_buf->sw_gso_cstate);
910 			} else {
911 				tx_pkts--;
912 				tx_bytes -= skb->len;
913 				skb = NULL;
914 			}
915 			head_buf->is_sw_gso = 0;
916 		}
917 
918 		if (unlikely(is_ts_pkt)) {
919 			if (BNXT_CHIP_P5(bp)) {
920 				/* PTP worker takes ownership of the skb */
921 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
922 				skb = NULL;
923 			}
924 		}
925 
926 next_tx_int:
927 		cons = NEXT_TX(cons);
928 
929 		napi_consume_skb(skb, budget);
930 	}
931 
932 	WRITE_ONCE(txr->tx_cons, cons);
933 
934 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
935 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
936 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
937 
938 	return rc;
939 }
940 
941 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
942 {
943 	struct bnxt_tx_ring_info *txr;
944 	bool more = false;
945 	int i;
946 
947 	bnxt_for_each_napi_tx(i, bnapi, txr) {
948 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
949 			more |= __bnxt_tx_int(bp, txr, budget);
950 	}
951 	if (!more)
952 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
953 }
954 
955 static bool bnxt_separate_head_pool(struct bnxt_rx_ring_info *rxr)
956 {
957 	return rxr->need_head_pool || rxr->rx_page_size < PAGE_SIZE;
958 }
959 
960 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
961 					 struct bnxt_rx_ring_info *rxr,
962 					 unsigned int *offset,
963 					 gfp_t gfp)
964 {
965 	struct page *page;
966 
967 	if (rxr->rx_page_size < PAGE_SIZE) {
968 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
969 						rxr->rx_page_size);
970 	} else {
971 		page = page_pool_dev_alloc_pages(rxr->page_pool);
972 		*offset = 0;
973 	}
974 	if (!page)
975 		return NULL;
976 
977 	*mapping = page_pool_get_dma_addr(page) + *offset;
978 	return page;
979 }
980 
981 static netmem_ref __bnxt_alloc_rx_netmem(struct bnxt *bp, dma_addr_t *mapping,
982 					 struct bnxt_rx_ring_info *rxr,
983 					 unsigned int *offset,
984 					 gfp_t gfp)
985 {
986 	netmem_ref netmem;
987 
988 	if (rxr->rx_page_size < PAGE_SIZE) {
989 		netmem = page_pool_alloc_frag_netmem(rxr->page_pool, offset,
990 						     rxr->rx_page_size, gfp);
991 	} else {
992 		netmem = page_pool_alloc_netmems(rxr->page_pool, gfp);
993 		*offset = 0;
994 	}
995 	if (!netmem)
996 		return 0;
997 
998 	*mapping = page_pool_get_dma_addr_netmem(netmem) + *offset;
999 	return netmem;
1000 }
1001 
1002 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
1003 				       struct bnxt_rx_ring_info *rxr,
1004 				       gfp_t gfp)
1005 {
1006 	unsigned int offset;
1007 	struct page *page;
1008 
1009 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
1010 				    bp->rx_buf_size, gfp);
1011 	if (!page)
1012 		return NULL;
1013 
1014 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
1015 	return page_address(page) + offset;
1016 }
1017 
1018 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1019 		       u16 prod, gfp_t gfp)
1020 {
1021 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1022 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1023 	dma_addr_t mapping;
1024 
1025 	if (BNXT_RX_PAGE_MODE(bp)) {
1026 		unsigned int offset;
1027 		struct page *page =
1028 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
1029 
1030 		if (!page)
1031 			return -ENOMEM;
1032 
1033 		mapping += bp->rx_dma_offset;
1034 		rx_buf->data = page;
1035 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
1036 		rx_buf->offset = offset;
1037 	} else {
1038 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
1039 
1040 		if (!data)
1041 			return -ENOMEM;
1042 
1043 		rx_buf->data = data;
1044 		rx_buf->data_ptr = data + bp->rx_offset;
1045 		rx_buf->offset = 0;
1046 	}
1047 	rx_buf->mapping = mapping;
1048 
1049 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1050 	return 0;
1051 }
1052 
1053 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
1054 {
1055 	u16 prod = rxr->rx_prod;
1056 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1057 	struct bnxt *bp = rxr->bnapi->bp;
1058 	struct rx_bd *cons_bd, *prod_bd;
1059 
1060 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1061 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1062 
1063 	prod_rx_buf->data = data;
1064 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
1065 
1066 	prod_rx_buf->mapping = cons_rx_buf->mapping;
1067 	prod_rx_buf->offset = cons_rx_buf->offset;
1068 
1069 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1070 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
1071 
1072 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
1073 }
1074 
1075 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1076 {
1077 	u16 next, max = rxr->rx_agg_bmap_size;
1078 
1079 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
1080 	if (next >= max)
1081 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
1082 	return next;
1083 }
1084 
1085 static int bnxt_alloc_rx_netmem(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1086 				u16 prod, gfp_t gfp)
1087 {
1088 	struct rx_bd *rxbd =
1089 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1090 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
1091 	u16 sw_prod = rxr->rx_sw_agg_prod;
1092 	unsigned int offset = 0;
1093 	dma_addr_t mapping;
1094 	netmem_ref netmem;
1095 
1096 	netmem = __bnxt_alloc_rx_netmem(bp, &mapping, rxr, &offset, gfp);
1097 	if (!netmem)
1098 		return -ENOMEM;
1099 
1100 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1101 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1102 
1103 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1104 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1105 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1106 
1107 	rx_agg_buf->netmem = netmem;
1108 	rx_agg_buf->offset = offset;
1109 	rx_agg_buf->mapping = mapping;
1110 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1111 	rxbd->rx_bd_opaque = sw_prod;
1112 	return 0;
1113 }
1114 
1115 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1116 				       struct bnxt_cp_ring_info *cpr,
1117 				       u16 cp_cons, u16 curr)
1118 {
1119 	struct rx_agg_cmp *agg;
1120 
1121 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1122 	agg = (struct rx_agg_cmp *)
1123 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1124 	return agg;
1125 }
1126 
1127 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1128 					      struct bnxt_rx_ring_info *rxr,
1129 					      u16 agg_id, u16 curr)
1130 {
1131 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1132 
1133 	return &tpa_info->agg_arr[curr];
1134 }
1135 
1136 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1137 				   u16 start, u32 agg_bufs, bool tpa)
1138 {
1139 	struct bnxt_napi *bnapi = cpr->bnapi;
1140 	struct bnxt *bp = bnapi->bp;
1141 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1142 	u16 prod = rxr->rx_agg_prod;
1143 	u16 sw_prod = rxr->rx_sw_agg_prod;
1144 	bool p5_tpa = false;
1145 	u32 i;
1146 
1147 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1148 		p5_tpa = true;
1149 
1150 	for (i = 0; i < agg_bufs; i++) {
1151 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1152 		struct rx_agg_cmp *agg;
1153 		struct rx_bd *prod_bd;
1154 		netmem_ref netmem;
1155 		u16 cons;
1156 
1157 		if (p5_tpa)
1158 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1159 		else
1160 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1161 		cons = agg->rx_agg_cmp_opaque;
1162 		__clear_bit(cons, rxr->rx_agg_bmap);
1163 
1164 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1165 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1166 
1167 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1168 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1169 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1170 
1171 		/* It is possible for sw_prod to be equal to cons, so
1172 		 * set cons_rx_buf->netmem to 0 first.
1173 		 */
1174 		netmem = cons_rx_buf->netmem;
1175 		cons_rx_buf->netmem = 0;
1176 		prod_rx_buf->netmem = netmem;
1177 		prod_rx_buf->offset = cons_rx_buf->offset;
1178 
1179 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1180 
1181 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1182 
1183 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1184 		prod_bd->rx_bd_opaque = sw_prod;
1185 
1186 		prod = NEXT_RX_AGG(prod);
1187 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1188 	}
1189 	rxr->rx_agg_prod = prod;
1190 	rxr->rx_sw_agg_prod = sw_prod;
1191 }
1192 
1193 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1194 					      struct bnxt_rx_ring_info *rxr,
1195 					      u16 cons, void *data, u8 *data_ptr,
1196 					      dma_addr_t dma_addr,
1197 					      unsigned int offset_and_len)
1198 {
1199 	unsigned int len = offset_and_len & 0xffff;
1200 	struct page *page = data;
1201 	u16 prod = rxr->rx_prod;
1202 	struct sk_buff *skb;
1203 	void *frag_start;
1204 	int err;
1205 
1206 	frag_start = page_address(page) + rxr->rx_buf_ring[cons].offset;
1207 
1208 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1209 	if (unlikely(err)) {
1210 		bnxt_reuse_rx_data(rxr, cons, data);
1211 		return NULL;
1212 	}
1213 	dma_addr -= bp->rx_dma_offset;
1214 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size,
1215 				bp->rx_dir);
1216 	skb = napi_build_skb(frag_start, rxr->rx_page_size);
1217 	if (!skb) {
1218 		struct page *new_page;
1219 
1220 		new_page = rxr->rx_buf_ring[RING_RX(bp, prod)].data;
1221 		/* The caller may still need the XDP frags from shinfo at the
1222 		 * end of the head page. Release the new buffer instead and
1223 		 * put the head back on the ring.
1224 		 */
1225 		page_pool_recycle_direct(rxr->page_pool, new_page);
1226 		bnxt_reuse_rx_data(rxr, cons, data);
1227 		return NULL;
1228 	}
1229 	skb_mark_for_recycle(skb);
1230 	skb_reserve(skb, data_ptr - (u8 *)frag_start);
1231 	__skb_put(skb, len);
1232 
1233 	return skb;
1234 }
1235 
1236 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1237 					struct bnxt_rx_ring_info *rxr,
1238 					u16 cons, void *data, u8 *data_ptr,
1239 					dma_addr_t dma_addr,
1240 					unsigned int offset_and_len)
1241 {
1242 	unsigned int payload = offset_and_len >> 16;
1243 	unsigned int len = offset_and_len & 0xffff;
1244 	skb_frag_t *frag;
1245 	struct page *page = data;
1246 	u16 prod = rxr->rx_prod;
1247 	struct sk_buff *skb;
1248 	int off, err;
1249 
1250 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1251 	if (unlikely(err)) {
1252 		bnxt_reuse_rx_data(rxr, cons, data);
1253 		return NULL;
1254 	}
1255 	dma_addr -= bp->rx_dma_offset;
1256 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size,
1257 				bp->rx_dir);
1258 
1259 	if (unlikely(!payload))
1260 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1261 
1262 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1263 	if (!skb) {
1264 		page_pool_recycle_direct(rxr->page_pool, page);
1265 		return NULL;
1266 	}
1267 
1268 	skb_mark_for_recycle(skb);
1269 	off = (void *)data_ptr - page_address(page);
1270 	skb_add_rx_frag(skb, 0, page, off, len, rxr->rx_page_size);
1271 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1272 	       payload + NET_IP_ALIGN);
1273 
1274 	frag = &skb_shinfo(skb)->frags[0];
1275 	skb_frag_size_sub(frag, payload);
1276 	skb_frag_off_add(frag, payload);
1277 	skb->data_len -= payload;
1278 	skb->tail += payload;
1279 
1280 	return skb;
1281 }
1282 
1283 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1284 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1285 				   void *data, u8 *data_ptr,
1286 				   dma_addr_t dma_addr,
1287 				   unsigned int offset_and_len)
1288 {
1289 	u16 prod = rxr->rx_prod;
1290 	struct sk_buff *skb;
1291 	int err;
1292 
1293 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1294 	if (unlikely(err)) {
1295 		bnxt_reuse_rx_data(rxr, cons, data);
1296 		return NULL;
1297 	}
1298 
1299 	skb = napi_build_skb(data, bp->rx_buf_size);
1300 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1301 				bp->rx_dir);
1302 	if (!skb) {
1303 		page_pool_free_va(rxr->head_pool, data, true);
1304 		return NULL;
1305 	}
1306 
1307 	skb_mark_for_recycle(skb);
1308 	skb_reserve(skb, bp->rx_offset);
1309 	skb_put(skb, offset_and_len & 0xffff);
1310 	return skb;
1311 }
1312 
1313 static u32 __bnxt_rx_agg_netmems(struct bnxt *bp,
1314 				 struct bnxt_cp_ring_info *cpr,
1315 				 u16 idx, u32 agg_bufs, bool tpa,
1316 				 struct sk_buff *skb,
1317 				 struct xdp_buff *xdp)
1318 {
1319 	struct bnxt_napi *bnapi = cpr->bnapi;
1320 	struct skb_shared_info *shinfo;
1321 	struct bnxt_rx_ring_info *rxr;
1322 	u32 i, total_frag_len = 0;
1323 	bool p5_tpa = false;
1324 	u16 prod;
1325 
1326 	rxr = bnapi->rx_ring;
1327 	prod = rxr->rx_agg_prod;
1328 
1329 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1330 		p5_tpa = true;
1331 
1332 	if (skb)
1333 		shinfo = skb_shinfo(skb);
1334 	else
1335 		shinfo = xdp_get_shared_info_from_buff(xdp);
1336 
1337 	for (i = 0; i < agg_bufs; i++) {
1338 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1339 		struct rx_agg_cmp *agg;
1340 		u16 cons, frag_len;
1341 		netmem_ref netmem;
1342 
1343 		if (p5_tpa)
1344 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1345 		else
1346 			agg = bnxt_get_agg(bp, cpr, idx, i);
1347 		cons = agg->rx_agg_cmp_opaque;
1348 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1349 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1350 
1351 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1352 		if (skb) {
1353 			skb_add_rx_frag_netmem(skb, i, cons_rx_buf->netmem,
1354 					       cons_rx_buf->offset,
1355 					       frag_len, rxr->rx_page_size);
1356 		} else {
1357 			skb_frag_t *frag = &shinfo->frags[i];
1358 
1359 			skb_frag_fill_netmem_desc(frag, cons_rx_buf->netmem,
1360 						  cons_rx_buf->offset,
1361 						  frag_len);
1362 			shinfo->nr_frags = i + 1;
1363 		}
1364 		__clear_bit(cons, rxr->rx_agg_bmap);
1365 
1366 		/* It is possible for bnxt_alloc_rx_netmem() to allocate
1367 		 * a sw_prod index that equals the cons index, so we
1368 		 * need to clear the cons entry now.
1369 		 */
1370 		netmem = cons_rx_buf->netmem;
1371 		cons_rx_buf->netmem = 0;
1372 
1373 		if (xdp && netmem_is_pfmemalloc(netmem))
1374 			xdp_buff_set_frag_pfmemalloc(xdp);
1375 
1376 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_ATOMIC) != 0) {
1377 			if (skb) {
1378 				skb->len -= frag_len;
1379 				skb->data_len -= frag_len;
1380 				skb->truesize -= rxr->rx_page_size;
1381 			}
1382 
1383 			--shinfo->nr_frags;
1384 			cons_rx_buf->netmem = netmem;
1385 
1386 			/* Update prod since possibly some netmems have been
1387 			 * allocated already.
1388 			 */
1389 			rxr->rx_agg_prod = prod;
1390 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1391 			return 0;
1392 		}
1393 
1394 		page_pool_dma_sync_netmem_for_cpu(rxr->page_pool, netmem, 0,
1395 						  rxr->rx_page_size);
1396 
1397 		total_frag_len += frag_len;
1398 		prod = NEXT_RX_AGG(prod);
1399 	}
1400 	rxr->rx_agg_prod = prod;
1401 	return total_frag_len;
1402 }
1403 
1404 static struct sk_buff *bnxt_rx_agg_netmems_skb(struct bnxt *bp,
1405 					       struct bnxt_cp_ring_info *cpr,
1406 					       struct sk_buff *skb, u16 idx,
1407 					       u32 agg_bufs, bool tpa)
1408 {
1409 	u32 total_frag_len = 0;
1410 
1411 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1412 					       skb, NULL);
1413 	if (!total_frag_len) {
1414 		skb_mark_for_recycle(skb);
1415 		dev_kfree_skb(skb);
1416 		return NULL;
1417 	}
1418 
1419 	return skb;
1420 }
1421 
1422 static u32 bnxt_rx_agg_netmems_xdp(struct bnxt *bp,
1423 				   struct bnxt_cp_ring_info *cpr,
1424 				   struct xdp_buff *xdp, u16 idx,
1425 				   u32 agg_bufs, bool tpa)
1426 {
1427 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1428 	u32 total_frag_len = 0;
1429 
1430 	if (!xdp_buff_has_frags(xdp)) {
1431 		xdp_buff_set_frags_flag(xdp);
1432 		shinfo->nr_frags = 0;
1433 	}
1434 
1435 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1436 					       NULL, xdp);
1437 	if (!total_frag_len) {
1438 		bnxt_xdp_buff_frags_free(cpr->bnapi->rx_ring, xdp);
1439 		return 0;
1440 	}
1441 	shinfo->nr_frags = agg_bufs;
1442 	shinfo->xdp_frags_size = total_frag_len;
1443 	return total_frag_len;
1444 }
1445 
1446 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1447 			       u8 agg_bufs, u32 *raw_cons)
1448 {
1449 	u16 last;
1450 	struct rx_agg_cmp *agg;
1451 
1452 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1453 	last = RING_CMP(*raw_cons);
1454 	agg = (struct rx_agg_cmp *)
1455 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1456 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1457 }
1458 
1459 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1460 				      unsigned int len,
1461 				      dma_addr_t mapping)
1462 {
1463 	struct bnxt *bp = bnapi->bp;
1464 	struct pci_dev *pdev = bp->pdev;
1465 	struct sk_buff *skb;
1466 
1467 	skb = napi_alloc_skb(&bnapi->napi, len);
1468 	if (!skb)
1469 		return NULL;
1470 
1471 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1472 				bp->rx_dir);
1473 
1474 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1475 	       len + NET_IP_ALIGN);
1476 
1477 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1478 				   bp->rx_dir);
1479 
1480 	skb_put(skb, len);
1481 
1482 	return skb;
1483 }
1484 
1485 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1486 				     unsigned int len,
1487 				     dma_addr_t mapping)
1488 {
1489 	return bnxt_copy_data(bnapi, data, len, mapping);
1490 }
1491 
1492 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1493 				     struct xdp_buff *xdp,
1494 				     unsigned int len,
1495 				     dma_addr_t mapping)
1496 {
1497 	unsigned int metasize = 0;
1498 	u8 *data = xdp->data;
1499 	struct sk_buff *skb;
1500 
1501 	len = xdp->data_end - xdp->data_meta;
1502 	metasize = xdp->data - xdp->data_meta;
1503 	data = xdp->data_meta;
1504 
1505 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1506 	if (!skb)
1507 		return skb;
1508 
1509 	if (metasize) {
1510 		skb_metadata_set(skb, metasize);
1511 		__skb_pull(skb, metasize);
1512 	}
1513 
1514 	if (unlikely(xdp_buff_has_frags(xdp))) {
1515 		struct skb_shared_info *sinfo;
1516 
1517 		sinfo = xdp_get_shared_info_from_buff(xdp);
1518 		memcpy(skb_shinfo(skb)->frags, sinfo->frags,
1519 		       sinfo->nr_frags * sizeof(skb_frag_t));
1520 	}
1521 
1522 	return skb;
1523 }
1524 
1525 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1526 			   u32 *raw_cons, void *cmp)
1527 {
1528 	struct rx_cmp *rxcmp = cmp;
1529 	u32 tmp_raw_cons = *raw_cons;
1530 	u8 cmp_type, agg_bufs = 0;
1531 
1532 	cmp_type = RX_CMP_TYPE(rxcmp);
1533 
1534 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1535 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1536 			    RX_CMP_AGG_BUFS) >>
1537 			   RX_CMP_AGG_BUFS_SHIFT;
1538 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1539 		struct rx_tpa_end_cmp *tpa_end = cmp;
1540 
1541 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1542 			return 0;
1543 
1544 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1545 	}
1546 
1547 	if (agg_bufs) {
1548 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1549 			return -EBUSY;
1550 	}
1551 	*raw_cons = tmp_raw_cons;
1552 	return 0;
1553 }
1554 
1555 static u16 bnxt_alloc_agg_idx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1556 			      u16 agg_id)
1557 {
1558 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1559 	u16 idx = agg_id & (bp->max_tpa_roundup_size - 1);
1560 
1561 	if (test_bit(idx, map->agg_idx_bmap)) {
1562 		idx = find_first_zero_bit(map->agg_idx_bmap,
1563 					  bp->max_tpa_roundup_size);
1564 		if (idx >= bp->max_tpa_roundup_size)
1565 			return INVALID_HW_RING_ID;
1566 	}
1567 	__set_bit(idx, map->agg_idx_bmap);
1568 	map->agg_id_tbl[agg_id] = idx;
1569 	return idx;
1570 }
1571 
1572 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1573 {
1574 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1575 
1576 	__clear_bit(idx, map->agg_idx_bmap);
1577 }
1578 
1579 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1580 {
1581 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1582 
1583 	return map->agg_id_tbl[agg_id];
1584 }
1585 
1586 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1587 			      struct rx_tpa_start_cmp *tpa_start,
1588 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1589 {
1590 	tpa_info->cfa_code_valid = 1;
1591 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1592 	tpa_info->vlan_valid = 0;
1593 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1594 		tpa_info->vlan_valid = 1;
1595 		tpa_info->metadata =
1596 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1597 	}
1598 }
1599 
1600 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1601 				 struct rx_tpa_start_cmp *tpa_start,
1602 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1603 {
1604 	tpa_info->vlan_valid = 0;
1605 	if (TPA_START_VLAN_VALID(tpa_start)) {
1606 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1607 		u32 vlan_proto = ETH_P_8021Q;
1608 
1609 		tpa_info->vlan_valid = 1;
1610 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1611 			vlan_proto = ETH_P_8021AD;
1612 		tpa_info->metadata = vlan_proto << 16 |
1613 				     TPA_START_METADATA0_TCI(tpa_start1);
1614 	}
1615 }
1616 
1617 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1618 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1619 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1620 {
1621 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1622 	struct bnxt_tpa_info *tpa_info;
1623 	u16 cons, prod, agg_id;
1624 	struct rx_bd *prod_bd;
1625 	dma_addr_t mapping;
1626 
1627 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1628 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1629 		agg_id = bnxt_alloc_agg_idx(bp, rxr, agg_id);
1630 		if (unlikely(agg_id == INVALID_HW_RING_ID)) {
1631 			netdev_warn(bp->dev, "Unable to allocate agg ID for ring %d, agg 0x%x\n",
1632 				    rxr->bnapi->index,
1633 				    TPA_START_AGG_ID_P5(tpa_start));
1634 			bnxt_sched_reset_rxr(bp, rxr);
1635 			return;
1636 		}
1637 	} else {
1638 		agg_id = TPA_START_AGG_ID(tpa_start);
1639 	}
1640 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1641 	prod = rxr->rx_prod;
1642 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1643 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1644 	tpa_info = &rxr->rx_tpa[agg_id];
1645 
1646 	if (unlikely(cons != rxr->rx_next_cons ||
1647 		     TPA_START_ERROR(tpa_start))) {
1648 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1649 			    cons, rxr->rx_next_cons,
1650 			    TPA_START_ERROR_CODE(tpa_start1));
1651 		bnxt_sched_reset_rxr(bp, rxr);
1652 		return;
1653 	}
1654 	prod_rx_buf->data = tpa_info->data;
1655 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1656 
1657 	mapping = tpa_info->mapping;
1658 	prod_rx_buf->mapping = mapping;
1659 
1660 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1661 
1662 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1663 
1664 	tpa_info->data = cons_rx_buf->data;
1665 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1666 	cons_rx_buf->data = NULL;
1667 	tpa_info->mapping = cons_rx_buf->mapping;
1668 
1669 	tpa_info->len =
1670 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1671 				RX_TPA_START_CMP_LEN_SHIFT;
1672 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1673 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1674 		tpa_info->gso_type = SKB_GSO_TCPV4;
1675 		if (TPA_START_IS_IPV6(tpa_start1))
1676 			tpa_info->gso_type = SKB_GSO_TCPV6;
1677 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1678 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1679 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1680 			tpa_info->gso_type = SKB_GSO_TCPV6;
1681 		tpa_info->rss_hash =
1682 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1683 	} else {
1684 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1685 		tpa_info->gso_type = 0;
1686 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1687 	}
1688 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1689 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1690 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1691 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1692 	else
1693 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1694 	tpa_info->agg_count = 0;
1695 
1696 	rxr->rx_prod = NEXT_RX(prod);
1697 	cons = RING_RX(bp, NEXT_RX(cons));
1698 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1699 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1700 
1701 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1702 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1703 	cons_rx_buf->data = NULL;
1704 }
1705 
1706 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1707 {
1708 	if (agg_bufs)
1709 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1710 }
1711 
1712 #ifdef CONFIG_INET
1713 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1714 {
1715 	struct udphdr *uh = NULL;
1716 
1717 	if (ip_proto == htons(ETH_P_IP)) {
1718 		struct iphdr *iph = (struct iphdr *)skb->data;
1719 
1720 		if (iph->protocol == IPPROTO_UDP)
1721 			uh = (struct udphdr *)(iph + 1);
1722 	} else {
1723 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1724 
1725 		if (iph->nexthdr == IPPROTO_UDP)
1726 			uh = (struct udphdr *)(iph + 1);
1727 	}
1728 	if (uh) {
1729 		if (uh->check)
1730 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1731 		else
1732 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1733 	}
1734 }
1735 #endif
1736 
1737 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1738 					   int payload_off, int tcp_ts,
1739 					   struct sk_buff *skb)
1740 {
1741 #ifdef CONFIG_INET
1742 	struct tcphdr *th;
1743 	int len, nw_off;
1744 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1745 	u32 hdr_info = tpa_info->hdr_info;
1746 	bool loopback = false;
1747 
1748 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1749 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1750 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1751 
1752 	/* If the packet is an internal loopback packet, the offsets will
1753 	 * have an extra 4 bytes.
1754 	 */
1755 	if (inner_mac_off == 4) {
1756 		loopback = true;
1757 	} else if (inner_mac_off > 4) {
1758 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1759 					    ETH_HLEN - 2));
1760 
1761 		/* We only support inner iPv4/ipv6.  If we don't see the
1762 		 * correct protocol ID, it must be a loopback packet where
1763 		 * the offsets are off by 4.
1764 		 */
1765 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1766 			loopback = true;
1767 	}
1768 	if (loopback) {
1769 		/* internal loopback packet, subtract all offsets by 4 */
1770 		inner_ip_off -= 4;
1771 		inner_mac_off -= 4;
1772 		outer_ip_off -= 4;
1773 	}
1774 
1775 	nw_off = inner_ip_off - ETH_HLEN;
1776 	skb_set_network_header(skb, nw_off);
1777 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1778 		struct ipv6hdr *iph = ipv6_hdr(skb);
1779 
1780 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1781 		len = skb->len - skb_transport_offset(skb);
1782 		th = tcp_hdr(skb);
1783 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1784 	} else {
1785 		struct iphdr *iph = ip_hdr(skb);
1786 
1787 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1788 		len = skb->len - skb_transport_offset(skb);
1789 		th = tcp_hdr(skb);
1790 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1791 	}
1792 
1793 	if (inner_mac_off) { /* tunnel */
1794 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1795 					    ETH_HLEN - 2));
1796 
1797 		bnxt_gro_tunnel(skb, proto);
1798 	}
1799 #endif
1800 	return skb;
1801 }
1802 
1803 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1804 					   int payload_off, int tcp_ts,
1805 					   struct sk_buff *skb)
1806 {
1807 #ifdef CONFIG_INET
1808 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1809 	u32 hdr_info = tpa_info->hdr_info;
1810 	int iphdr_len, nw_off;
1811 
1812 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1813 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1814 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1815 
1816 	nw_off = inner_ip_off - ETH_HLEN;
1817 	skb_set_network_header(skb, nw_off);
1818 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1819 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1820 	skb_set_transport_header(skb, nw_off + iphdr_len);
1821 
1822 	if (inner_mac_off) { /* tunnel */
1823 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1824 					    ETH_HLEN - 2));
1825 
1826 		bnxt_gro_tunnel(skb, proto);
1827 	}
1828 #endif
1829 	return skb;
1830 }
1831 
1832 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1833 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1834 
1835 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1836 					   int payload_off, int tcp_ts,
1837 					   struct sk_buff *skb)
1838 {
1839 #ifdef CONFIG_INET
1840 	struct tcphdr *th;
1841 	int len, nw_off, tcp_opt_len = 0;
1842 
1843 	if (tcp_ts)
1844 		tcp_opt_len = 12;
1845 
1846 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1847 		struct iphdr *iph;
1848 
1849 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1850 			 ETH_HLEN;
1851 		skb_set_network_header(skb, nw_off);
1852 		iph = ip_hdr(skb);
1853 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1854 		len = skb->len - skb_transport_offset(skb);
1855 		th = tcp_hdr(skb);
1856 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1857 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1858 		struct ipv6hdr *iph;
1859 
1860 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1861 			 ETH_HLEN;
1862 		skb_set_network_header(skb, nw_off);
1863 		iph = ipv6_hdr(skb);
1864 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1865 		len = skb->len - skb_transport_offset(skb);
1866 		th = tcp_hdr(skb);
1867 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1868 	} else {
1869 		dev_kfree_skb_any(skb);
1870 		return NULL;
1871 	}
1872 
1873 	if (nw_off) /* tunnel */
1874 		bnxt_gro_tunnel(skb, skb->protocol);
1875 #endif
1876 	return skb;
1877 }
1878 
1879 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1880 					   struct bnxt_tpa_info *tpa_info,
1881 					   struct rx_tpa_end_cmp *tpa_end,
1882 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1883 					   struct sk_buff *skb,
1884 					   struct bnxt_rx_sw_stats *rx_stats)
1885 {
1886 #ifdef CONFIG_INET
1887 	int payload_off;
1888 	u16 segs;
1889 
1890 	segs = TPA_END_TPA_SEGS(tpa_end);
1891 	if (segs == 1)
1892 		return skb;
1893 
1894 	rx_stats->rx_hw_gro_packets++;
1895 	rx_stats->rx_hw_gro_wire_packets += segs;
1896 
1897 	NAPI_GRO_CB(skb)->count = segs;
1898 	skb_shinfo(skb)->gso_size =
1899 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1900 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1901 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1902 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1903 	else
1904 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1905 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1906 	if (likely(skb))
1907 		tcp_gro_complete(skb);
1908 #endif
1909 	return skb;
1910 }
1911 
1912 /* Given the cfa_code of a received packet determine which
1913  * netdev (vf-rep or PF) the packet is destined to.
1914  */
1915 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1916 {
1917 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1918 
1919 	/* if vf-rep dev is NULL, it must belong to the PF */
1920 	return dev ? dev : bp->dev;
1921 }
1922 
1923 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1924 					   struct bnxt_cp_ring_info *cpr,
1925 					   u32 *raw_cons,
1926 					   struct rx_tpa_end_cmp *tpa_end,
1927 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1928 					   u8 *event)
1929 {
1930 	struct bnxt_napi *bnapi = cpr->bnapi;
1931 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1932 	struct net_device *dev = bp->dev;
1933 	u8 *data_ptr, agg_bufs;
1934 	unsigned int len;
1935 	struct bnxt_tpa_info *tpa_info;
1936 	dma_addr_t mapping;
1937 	struct sk_buff *skb;
1938 	u16 idx = 0, agg_id;
1939 	void *data;
1940 	bool gro;
1941 
1942 	if (unlikely(bnapi->in_reset)) {
1943 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1944 
1945 		if (rc < 0)
1946 			return ERR_PTR(-EBUSY);
1947 		return NULL;
1948 	}
1949 
1950 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1951 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1952 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1953 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1954 		tpa_info = &rxr->rx_tpa[agg_id];
1955 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1956 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1957 				    agg_bufs, tpa_info->agg_count);
1958 			agg_bufs = tpa_info->agg_count;
1959 		}
1960 		tpa_info->agg_count = 0;
1961 		*event |= BNXT_AGG_EVENT;
1962 		bnxt_free_agg_idx(rxr, agg_id);
1963 		idx = agg_id;
1964 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1965 	} else {
1966 		agg_id = TPA_END_AGG_ID(tpa_end);
1967 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1968 		tpa_info = &rxr->rx_tpa[agg_id];
1969 		idx = RING_CMP(*raw_cons);
1970 		if (agg_bufs) {
1971 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1972 				return ERR_PTR(-EBUSY);
1973 
1974 			*event |= BNXT_AGG_EVENT;
1975 			idx = NEXT_CMP(idx);
1976 		}
1977 		gro = !!TPA_END_GRO(tpa_end);
1978 	}
1979 	data = tpa_info->data;
1980 	data_ptr = tpa_info->data_ptr;
1981 	prefetch(data_ptr);
1982 	len = tpa_info->len;
1983 	mapping = tpa_info->mapping;
1984 
1985 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1986 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1987 		if (agg_bufs > MAX_SKB_FRAGS)
1988 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1989 				    agg_bufs, (int)MAX_SKB_FRAGS);
1990 		return NULL;
1991 	}
1992 
1993 	if (len <= bp->rx_copybreak) {
1994 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1995 		if (!skb) {
1996 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1997 			cpr->sw_stats->rx.rx_oom_discards += 1;
1998 			return NULL;
1999 		}
2000 	} else {
2001 		u8 *new_data;
2002 		dma_addr_t new_mapping;
2003 
2004 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
2005 						GFP_ATOMIC);
2006 		if (!new_data) {
2007 			bnxt_abort_tpa(cpr, idx, agg_bufs);
2008 			cpr->sw_stats->rx.rx_oom_discards += 1;
2009 			return NULL;
2010 		}
2011 
2012 		tpa_info->data = new_data;
2013 		tpa_info->data_ptr = new_data + bp->rx_offset;
2014 		tpa_info->mapping = new_mapping;
2015 
2016 		skb = napi_build_skb(data, bp->rx_buf_size);
2017 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
2018 					bp->rx_buf_use_size, bp->rx_dir);
2019 
2020 		if (!skb) {
2021 			page_pool_free_va(rxr->head_pool, data, true);
2022 			bnxt_abort_tpa(cpr, idx, agg_bufs);
2023 			cpr->sw_stats->rx.rx_oom_discards += 1;
2024 			return NULL;
2025 		}
2026 		skb_mark_for_recycle(skb);
2027 		skb_reserve(skb, bp->rx_offset);
2028 		skb_put(skb, len);
2029 	}
2030 
2031 	if (agg_bufs) {
2032 		skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs,
2033 					      true);
2034 		if (!skb) {
2035 			/* Page reuse already handled by bnxt_rx_pages(). */
2036 			cpr->sw_stats->rx.rx_oom_discards += 1;
2037 			return NULL;
2038 		}
2039 	}
2040 
2041 	if (tpa_info->cfa_code_valid)
2042 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
2043 	skb->protocol = eth_type_trans(skb, dev);
2044 
2045 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
2046 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
2047 
2048 	if (tpa_info->vlan_valid &&
2049 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
2050 		__be16 vlan_proto = htons(tpa_info->metadata >>
2051 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
2052 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2053 
2054 		if (eth_type_vlan(vlan_proto)) {
2055 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2056 		} else {
2057 			dev_kfree_skb(skb);
2058 			return NULL;
2059 		}
2060 	}
2061 
2062 	skb_checksum_none_assert(skb);
2063 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
2064 		skb->ip_summed = CHECKSUM_UNNECESSARY;
2065 		skb->csum_level =
2066 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
2067 	}
2068 
2069 	if (gro)
2070 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb,
2071 				   &cpr->sw_stats->rx);
2072 
2073 	return skb;
2074 }
2075 
2076 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
2077 			 struct rx_agg_cmp *rx_agg)
2078 {
2079 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
2080 	struct bnxt_tpa_info *tpa_info;
2081 
2082 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
2083 	tpa_info = &rxr->rx_tpa[agg_id];
2084 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
2085 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
2086 }
2087 
2088 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
2089 			     struct sk_buff *skb)
2090 {
2091 	skb_mark_for_recycle(skb);
2092 
2093 	if (skb->dev != bp->dev) {
2094 		/* this packet belongs to a vf-rep */
2095 		bnxt_vf_rep_rx(bp, skb);
2096 		return;
2097 	}
2098 	skb_record_rx_queue(skb, bnapi->index);
2099 	napi_gro_receive(&bnapi->napi, skb);
2100 }
2101 
2102 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
2103 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
2104 {
2105 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
2106 
2107 	if (BNXT_PTP_RX_TS_VALID(flags))
2108 		goto ts_valid;
2109 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
2110 		return false;
2111 
2112 ts_valid:
2113 	*cmpl_ts = ts;
2114 	return true;
2115 }
2116 
2117 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
2118 				    struct rx_cmp *rxcmp,
2119 				    struct rx_cmp_ext *rxcmp1)
2120 {
2121 	__be16 vlan_proto;
2122 	u16 vtag;
2123 
2124 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
2125 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
2126 		u32 meta_data;
2127 
2128 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
2129 			return skb;
2130 
2131 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
2132 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2133 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
2134 		if (eth_type_vlan(vlan_proto))
2135 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2136 		else
2137 			goto vlan_err;
2138 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2139 		if (RX_CMP_VLAN_VALID(rxcmp)) {
2140 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
2141 
2142 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
2143 				vlan_proto = htons(ETH_P_8021Q);
2144 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
2145 				vlan_proto = htons(ETH_P_8021AD);
2146 			else
2147 				goto vlan_err;
2148 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
2149 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2150 		}
2151 	}
2152 	return skb;
2153 vlan_err:
2154 	skb_mark_for_recycle(skb);
2155 	dev_kfree_skb(skb);
2156 	return NULL;
2157 }
2158 
2159 /* returns the following:
2160  * 1       - 1 packet successfully received
2161  * 0       - successful TPA_START, packet not completed yet
2162  * -EBUSY  - completion ring does not have all the agg buffers yet
2163  * -ENOMEM - packet aborted due to out of memory
2164  * -EIO    - packet aborted due to hw error indicated in BD
2165  */
2166 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2167 		       u32 *raw_cons, u8 *event)
2168 {
2169 	struct bnxt_napi *bnapi = cpr->bnapi;
2170 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2171 	struct net_device *dev = bp->dev;
2172 	struct rx_cmp *rxcmp;
2173 	struct rx_cmp_ext *rxcmp1;
2174 	u32 tmp_raw_cons = *raw_cons;
2175 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2176 	struct skb_shared_info *sinfo;
2177 	struct bnxt_xdp_buff bnxt_xdp;
2178 	struct bnxt_sw_rx_bd *rx_buf;
2179 	unsigned int len;
2180 	u8 *data_ptr, agg_bufs, cmp_type;
2181 	bool xdp_active = false;
2182 	dma_addr_t dma_addr;
2183 	struct sk_buff *skb;
2184 	u32 flags, misc;
2185 	u32 cmpl_ts;
2186 	void *data;
2187 	int rc = 0;
2188 
2189 	rxcmp = (struct rx_cmp *)
2190 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2191 
2192 	cmp_type = RX_CMP_TYPE(rxcmp);
2193 
2194 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2195 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2196 		goto next_rx_no_prod_no_len;
2197 	}
2198 
2199 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2200 	cp_cons = RING_CMP(tmp_raw_cons);
2201 	rxcmp1 = (struct rx_cmp_ext *)
2202 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2203 
2204 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2205 		return -EBUSY;
2206 
2207 	/* The valid test of the entry must be done first before
2208 	 * reading any further.
2209 	 */
2210 	dma_rmb();
2211 	prod = rxr->rx_prod;
2212 
2213 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2214 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2215 		bnxt_tpa_start(bp, rxr, cmp_type,
2216 			       (struct rx_tpa_start_cmp *)rxcmp,
2217 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2218 
2219 		*event |= BNXT_RX_EVENT;
2220 		goto next_rx_no_prod_no_len;
2221 
2222 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2223 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2224 				   (struct rx_tpa_end_cmp *)rxcmp,
2225 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2226 
2227 		if (IS_ERR(skb))
2228 			return -EBUSY;
2229 
2230 		rc = -ENOMEM;
2231 		if (likely(skb)) {
2232 			bnxt_deliver_skb(bp, bnapi, skb);
2233 			rc = 1;
2234 		}
2235 		*event |= BNXT_RX_EVENT;
2236 		goto next_rx_no_prod_no_len;
2237 	}
2238 
2239 	cons = rxcmp->rx_cmp_opaque;
2240 	if (unlikely(cons != rxr->rx_next_cons)) {
2241 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2242 
2243 		/* 0xffff is forced error, don't print it */
2244 		if (rxr->rx_next_cons != 0xffff)
2245 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2246 				    cons, rxr->rx_next_cons);
2247 		bnxt_sched_reset_rxr(bp, rxr);
2248 		if (rc1)
2249 			return rc1;
2250 		goto next_rx_no_prod_no_len;
2251 	}
2252 	rx_buf = &rxr->rx_buf_ring[cons];
2253 	data = rx_buf->data;
2254 	data_ptr = rx_buf->data_ptr;
2255 	prefetch(data_ptr);
2256 
2257 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2258 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2259 
2260 	if (agg_bufs) {
2261 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2262 			return -EBUSY;
2263 
2264 		cp_cons = NEXT_CMP(cp_cons);
2265 		*event |= BNXT_AGG_EVENT;
2266 	}
2267 	*event |= BNXT_RX_EVENT;
2268 
2269 	rx_buf->data = NULL;
2270 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2271 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2272 
2273 		bnxt_reuse_rx_data(rxr, cons, data);
2274 		if (agg_bufs)
2275 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2276 					       false);
2277 
2278 		rc = -EIO;
2279 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2280 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2281 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2282 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2283 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2284 						 rx_err);
2285 				bnxt_sched_reset_rxr(bp, rxr);
2286 			}
2287 		}
2288 		goto next_rx_no_len;
2289 	}
2290 
2291 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2292 	len = flags >> RX_CMP_LEN_SHIFT;
2293 	dma_addr = rx_buf->mapping;
2294 
2295 	if (bnxt_xdp_attached(bp, rxr)) {
2296 		bnxt_xdp.rxcmp = rxcmp;
2297 		bnxt_xdp.rxcmp1 = rxcmp1;
2298 		bnxt_xdp.cmp_type = cmp_type;
2299 
2300 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &bnxt_xdp.xdp);
2301 		if (agg_bufs) {
2302 			u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr,
2303 							       &bnxt_xdp.xdp,
2304 							       cp_cons,
2305 							       agg_bufs,
2306 							       false);
2307 			if (!frag_len) {
2308 				bnxt_reuse_rx_data(rxr, cons, data);
2309 				goto oom_next_rx;
2310 			}
2311 
2312 		}
2313 		xdp_active = true;
2314 	}
2315 
2316 	if (xdp_active) {
2317 		if (bnxt_rx_xdp(bp, rxr, cons, &bnxt_xdp.xdp, data, &data_ptr,
2318 				&len, event)) {
2319 			rc = 1;
2320 			goto next_rx;
2321 		}
2322 		if (xdp_buff_has_frags(&bnxt_xdp.xdp)) {
2323 			sinfo = xdp_get_shared_info_from_buff(&bnxt_xdp.xdp);
2324 			agg_bufs = sinfo->nr_frags;
2325 		} else {
2326 			agg_bufs = 0;
2327 		}
2328 	}
2329 
2330 	if (len <= bp->rx_copybreak) {
2331 		if (!xdp_active)
2332 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2333 		else
2334 			skb = bnxt_copy_xdp(bnapi, &bnxt_xdp.xdp, len,
2335 					    dma_addr);
2336 		bnxt_reuse_rx_data(rxr, cons, data);
2337 		if (!skb)
2338 			goto oom_next_rx_agg;
2339 	} else {
2340 		u32 payload;
2341 
2342 		if (rx_buf->data_ptr == data_ptr)
2343 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2344 		else
2345 			payload = 0;
2346 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2347 				      payload | len);
2348 		if (!skb)
2349 			goto oom_next_rx_agg;
2350 	}
2351 
2352 	if (agg_bufs) {
2353 		if (!xdp_active) {
2354 			skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons,
2355 						      agg_bufs, false);
2356 			if (!skb)
2357 				goto oom_next_rx;
2358 		} else {
2359 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs,
2360 						 rxr, &bnxt_xdp.xdp);
2361 			if (!skb) {
2362 				/* we should be able to free the old skb here */
2363 				bnxt_xdp_buff_frags_free(rxr, &bnxt_xdp.xdp);
2364 				goto oom_next_rx;
2365 			}
2366 		}
2367 	}
2368 
2369 	if (RX_CMP_HASH_VALID(rxcmp)) {
2370 		enum pkt_hash_types type;
2371 
2372 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2373 			type = bnxt_rss_ext_op(bp, rxcmp);
2374 		} else {
2375 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2376 
2377 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2378 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2379 				type = PKT_HASH_TYPE_L4;
2380 			else
2381 				type = PKT_HASH_TYPE_L3;
2382 		}
2383 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2384 	}
2385 
2386 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2387 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2388 	skb->protocol = eth_type_trans(skb, dev);
2389 
2390 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2391 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2392 		if (!skb)
2393 			goto next_rx;
2394 	}
2395 
2396 	skb_checksum_none_assert(skb);
2397 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2398 		if (dev->features & NETIF_F_RXCSUM) {
2399 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2400 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2401 		}
2402 	} else {
2403 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2404 			if (dev->features & NETIF_F_RXCSUM)
2405 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2406 		}
2407 	}
2408 
2409 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2410 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2411 			u64 ns, ts;
2412 
2413 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2414 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2415 
2416 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2417 				memset(skb_hwtstamps(skb), 0,
2418 				       sizeof(*skb_hwtstamps(skb)));
2419 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2420 			}
2421 		}
2422 	}
2423 	bnxt_deliver_skb(bp, bnapi, skb);
2424 	rc = 1;
2425 
2426 next_rx:
2427 	cpr->rx_packets += 1;
2428 	cpr->rx_bytes += len;
2429 
2430 next_rx_no_len:
2431 	rxr->rx_prod = NEXT_RX(prod);
2432 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2433 
2434 next_rx_no_prod_no_len:
2435 	*raw_cons = tmp_raw_cons;
2436 
2437 	return rc;
2438 
2439 oom_next_rx_agg:
2440 	if (agg_bufs) {
2441 		if (!xdp_active)
2442 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2443 					       false);
2444 		else
2445 			bnxt_xdp_buff_frags_free(rxr, &bnxt_xdp.xdp);
2446 	}
2447 oom_next_rx:
2448 	cpr->sw_stats->rx.rx_oom_discards += 1;
2449 	rc = -ENOMEM;
2450 	goto next_rx;
2451 }
2452 
2453 /* In netpoll mode, if we are using a combined completion ring, we need to
2454  * discard the rx packets and recycle the buffers.
2455  */
2456 static int bnxt_force_rx_discard(struct bnxt *bp,
2457 				 struct bnxt_cp_ring_info *cpr,
2458 				 u32 *raw_cons, u8 *event)
2459 {
2460 	u32 tmp_raw_cons = *raw_cons;
2461 	struct rx_cmp_ext *rxcmp1;
2462 	struct rx_cmp *rxcmp;
2463 	u16 cp_cons;
2464 	u8 cmp_type;
2465 	int rc;
2466 
2467 	cp_cons = RING_CMP(tmp_raw_cons);
2468 	rxcmp = (struct rx_cmp *)
2469 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2470 
2471 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2472 	cp_cons = RING_CMP(tmp_raw_cons);
2473 	rxcmp1 = (struct rx_cmp_ext *)
2474 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2475 
2476 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2477 		return -EBUSY;
2478 
2479 	/* The valid test of the entry must be done first before
2480 	 * reading any further.
2481 	 */
2482 	dma_rmb();
2483 	cmp_type = RX_CMP_TYPE(rxcmp);
2484 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2485 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2486 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2487 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2488 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2489 		struct rx_tpa_end_cmp_ext *tpa_end1;
2490 
2491 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2492 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2493 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2494 	}
2495 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2496 	if (rc && rc != -EBUSY)
2497 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2498 	return rc;
2499 }
2500 
2501 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2502 {
2503 	struct bnxt_fw_health *fw_health = bp->fw_health;
2504 	u32 reg = fw_health->regs[reg_idx];
2505 	u32 reg_type, reg_off, val = 0;
2506 
2507 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2508 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2509 	switch (reg_type) {
2510 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2511 		pci_read_config_dword(bp->pdev, reg_off, &val);
2512 		break;
2513 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2514 		reg_off = fw_health->mapped_regs[reg_idx];
2515 		fallthrough;
2516 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2517 		val = readl(bp->bar0 + reg_off);
2518 		break;
2519 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2520 		val = readl(bp->bar1 + reg_off);
2521 		break;
2522 	}
2523 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2524 		val &= fw_health->fw_reset_inprog_reg_mask;
2525 	return val;
2526 }
2527 
2528 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2529 {
2530 	int i;
2531 
2532 	for (i = 0; i < bp->rx_nr_rings; i++) {
2533 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2534 		struct bnxt_ring_grp_info *grp_info;
2535 
2536 		grp_info = &bp->grp_info[grp_idx];
2537 		if (grp_info->agg_fw_ring_id == ring_id)
2538 			return grp_idx;
2539 	}
2540 	return INVALID_HW_RING_ID;
2541 }
2542 
2543 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2544 {
2545 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2546 
2547 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2548 		return link_info->force_link_speed2;
2549 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2550 		return link_info->force_pam4_link_speed;
2551 	return link_info->force_link_speed;
2552 }
2553 
2554 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2555 {
2556 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2557 
2558 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2559 		link_info->req_link_speed = link_info->force_link_speed2;
2560 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2561 		switch (link_info->req_link_speed) {
2562 		case BNXT_LINK_SPEED_50GB_PAM4:
2563 		case BNXT_LINK_SPEED_100GB_PAM4:
2564 		case BNXT_LINK_SPEED_200GB_PAM4:
2565 		case BNXT_LINK_SPEED_400GB_PAM4:
2566 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2567 			break;
2568 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2569 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2570 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2571 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2572 			break;
2573 		default:
2574 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2575 		}
2576 		return;
2577 	}
2578 	link_info->req_link_speed = link_info->force_link_speed;
2579 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2580 	if (link_info->force_pam4_link_speed) {
2581 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2582 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2583 	}
2584 }
2585 
2586 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2587 {
2588 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2589 
2590 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2591 		link_info->advertising = link_info->auto_link_speeds2;
2592 		return;
2593 	}
2594 	link_info->advertising = link_info->auto_link_speeds;
2595 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2596 }
2597 
2598 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2599 {
2600 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2601 
2602 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2603 		if (link_info->req_link_speed != link_info->force_link_speed2)
2604 			return true;
2605 		return false;
2606 	}
2607 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2608 	    link_info->req_link_speed != link_info->force_link_speed)
2609 		return true;
2610 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2611 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2612 		return true;
2613 	return false;
2614 }
2615 
2616 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2617 {
2618 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2619 
2620 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2621 		if (link_info->advertising != link_info->auto_link_speeds2)
2622 			return true;
2623 		return false;
2624 	}
2625 	if (link_info->advertising != link_info->auto_link_speeds ||
2626 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2627 		return true;
2628 	return false;
2629 }
2630 
2631 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2632 {
2633 	u32 flags = bp->ctx->ctx_arr[type].flags;
2634 
2635 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2636 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2637 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2638 }
2639 
2640 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2641 {
2642 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2643 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2644 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2645 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2646 	struct bnxt_bs_trace_info *bs_trace;
2647 	int last_pg;
2648 
2649 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2650 		return;
2651 
2652 	mem_size = ctxm->max_entries * ctxm->entry_size;
2653 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2654 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2655 
2656 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2657 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2658 
2659 	rmem = &ctx_pg[0].ring_mem;
2660 	bs_trace = &bp->bs_trace[trace_type];
2661 	bs_trace->ctx_type = ctxm->type;
2662 	bs_trace->trace_type = trace_type;
2663 	if (pages > MAX_CTX_PAGES) {
2664 		int last_pg_dir = rmem->nr_pages - 1;
2665 
2666 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2667 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2668 	} else {
2669 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2670 	}
2671 	bs_trace->magic_byte += magic_byte_offset;
2672 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2673 }
2674 
2675 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2676 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2677 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2678 
2679 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2680 	(((data2) &							\
2681 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2682 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2683 
2684 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2685 	((data2) &							\
2686 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2687 
2688 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2689 	(((data2) &							\
2690 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2691 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2692 
2693 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2694 	((data1) &							\
2695 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2696 
2697 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2698 	(((data1) &							\
2699 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2700 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2701 
2702 /* Return true if the workqueue has to be scheduled */
2703 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2704 {
2705 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2706 
2707 	switch (err_type) {
2708 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2709 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2710 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2711 		break;
2712 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2713 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2714 		break;
2715 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2716 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2717 		break;
2718 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2719 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2720 		char *threshold_type;
2721 		bool notify = false;
2722 		char *dir_str;
2723 
2724 		switch (type) {
2725 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2726 			threshold_type = "warning";
2727 			break;
2728 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2729 			threshold_type = "critical";
2730 			break;
2731 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2732 			threshold_type = "fatal";
2733 			break;
2734 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2735 			threshold_type = "shutdown";
2736 			break;
2737 		default:
2738 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2739 			return false;
2740 		}
2741 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2742 			dir_str = "above";
2743 			notify = true;
2744 		} else {
2745 			dir_str = "below";
2746 		}
2747 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2748 			    dir_str, threshold_type);
2749 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2750 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2751 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2752 		if (notify) {
2753 			bp->thermal_threshold_type = type;
2754 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2755 			return true;
2756 		}
2757 		return false;
2758 	}
2759 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2760 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2761 		break;
2762 	default:
2763 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2764 			   err_type);
2765 		break;
2766 	}
2767 	return false;
2768 }
2769 
2770 #define BNXT_GET_EVENT_PORT(data)	\
2771 	((data) &			\
2772 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2773 
2774 #define BNXT_EVENT_RING_TYPE(data2)	\
2775 	((data2) &			\
2776 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2777 
2778 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2779 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2780 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2781 
2782 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2783 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2784 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2785 
2786 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2787 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2788 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2789 
2790 #define BNXT_PHC_BITS	48
2791 
2792 static int bnxt_async_event_process(struct bnxt *bp,
2793 				    struct hwrm_async_event_cmpl *cmpl)
2794 {
2795 	u16 event_id = le16_to_cpu(cmpl->event_id);
2796 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2797 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2798 
2799 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2800 		   event_id, data1, data2);
2801 
2802 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2803 	switch (event_id) {
2804 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2805 		struct bnxt_link_info *link_info = &bp->link_info;
2806 
2807 		if (BNXT_VF(bp))
2808 			goto async_event_process_exit;
2809 
2810 		/* print unsupported speed warning in forced speed mode only */
2811 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2812 		    (data1 & 0x20000)) {
2813 			u16 fw_speed = bnxt_get_force_speed(link_info);
2814 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2815 
2816 			if (speed != SPEED_UNKNOWN)
2817 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2818 					    speed);
2819 		}
2820 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2821 	}
2822 		fallthrough;
2823 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2824 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2825 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2826 		fallthrough;
2827 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2828 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2829 		break;
2830 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2831 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2832 		break;
2833 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2834 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2835 
2836 		if (BNXT_VF(bp))
2837 			break;
2838 
2839 		if (bp->pf.port_id != port_id)
2840 			break;
2841 
2842 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2843 		break;
2844 	}
2845 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2846 		if (BNXT_PF(bp))
2847 			goto async_event_process_exit;
2848 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2849 		break;
2850 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2851 		char *type_str = "Solicited";
2852 
2853 		if (!bp->fw_health)
2854 			goto async_event_process_exit;
2855 
2856 		bp->fw_reset_timestamp = jiffies;
2857 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2858 		if (!bp->fw_reset_min_dsecs)
2859 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2860 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2861 		if (!bp->fw_reset_max_dsecs)
2862 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2863 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2864 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2865 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2866 			type_str = "Fatal";
2867 			bp->fw_health->fatalities++;
2868 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2869 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2870 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2871 			type_str = "Non-fatal";
2872 			bp->fw_health->survivals++;
2873 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2874 		}
2875 		netif_warn(bp, hw, bp->dev,
2876 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2877 			   type_str, data1, data2,
2878 			   bp->fw_reset_min_dsecs * 100,
2879 			   bp->fw_reset_max_dsecs * 100);
2880 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2881 		break;
2882 	}
2883 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2884 		struct bnxt_fw_health *fw_health = bp->fw_health;
2885 		char *status_desc = "healthy";
2886 		u32 status;
2887 
2888 		if (!fw_health)
2889 			goto async_event_process_exit;
2890 
2891 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2892 			fw_health->enabled = false;
2893 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2894 			break;
2895 		}
2896 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2897 		fw_health->tmr_multiplier =
2898 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2899 				     bp->current_interval * 10);
2900 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2901 		if (!fw_health->enabled)
2902 			fw_health->last_fw_heartbeat =
2903 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2904 		fw_health->last_fw_reset_cnt =
2905 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2906 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2907 		if (status != BNXT_FW_STATUS_HEALTHY)
2908 			status_desc = "unhealthy";
2909 		netif_info(bp, drv, bp->dev,
2910 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2911 			   fw_health->primary ? "primary" : "backup", status,
2912 			   status_desc, fw_health->last_fw_reset_cnt);
2913 		if (!fw_health->enabled) {
2914 			/* Make sure tmr_counter is set and visible to
2915 			 * bnxt_health_check() before setting enabled to true.
2916 			 */
2917 			smp_wmb();
2918 			fw_health->enabled = true;
2919 		}
2920 		goto async_event_process_exit;
2921 	}
2922 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2923 		netif_notice(bp, hw, bp->dev,
2924 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2925 			     data1, data2);
2926 		goto async_event_process_exit;
2927 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2928 		struct bnxt_rx_ring_info *rxr;
2929 		u16 grp_idx;
2930 
2931 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2932 			goto async_event_process_exit;
2933 
2934 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2935 			    BNXT_EVENT_RING_TYPE(data2), data1);
2936 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2937 			goto async_event_process_exit;
2938 
2939 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2940 		if (grp_idx == INVALID_HW_RING_ID) {
2941 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2942 				    data1);
2943 			goto async_event_process_exit;
2944 		}
2945 		rxr = bp->bnapi[grp_idx]->rx_ring;
2946 		bnxt_sched_reset_rxr(bp, rxr);
2947 		goto async_event_process_exit;
2948 	}
2949 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2950 		struct bnxt_fw_health *fw_health = bp->fw_health;
2951 
2952 		netif_notice(bp, hw, bp->dev,
2953 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2954 			     data1, data2);
2955 		if (fw_health) {
2956 			fw_health->echo_req_data1 = data1;
2957 			fw_health->echo_req_data2 = data2;
2958 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2959 			break;
2960 		}
2961 		goto async_event_process_exit;
2962 	}
2963 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2964 		bnxt_ptp_pps_event(bp, data1, data2);
2965 		goto async_event_process_exit;
2966 	}
2967 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2968 		if (bnxt_event_error_report(bp, data1, data2))
2969 			break;
2970 		goto async_event_process_exit;
2971 	}
2972 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2973 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2974 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2975 			if (BNXT_PTP_USE_RTC(bp)) {
2976 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2977 				unsigned long flags;
2978 				u64 ns;
2979 
2980 				if (!ptp)
2981 					goto async_event_process_exit;
2982 
2983 				bnxt_ptp_update_current_time(bp);
2984 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2985 				       BNXT_PHC_BITS) | ptp->current_time);
2986 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2987 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2988 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2989 			}
2990 			break;
2991 		}
2992 		goto async_event_process_exit;
2993 	}
2994 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2995 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2996 
2997 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2998 		goto async_event_process_exit;
2999 	}
3000 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
3001 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
3002 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
3003 
3004 		if (type >= ARRAY_SIZE(bp->bs_trace))
3005 			goto async_event_process_exit;
3006 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
3007 		goto async_event_process_exit;
3008 	}
3009 	default:
3010 		goto async_event_process_exit;
3011 	}
3012 	__bnxt_queue_sp_work(bp);
3013 async_event_process_exit:
3014 	bnxt_ulp_async_events(bp, cmpl);
3015 	return 0;
3016 }
3017 
3018 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
3019 {
3020 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
3021 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
3022 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
3023 				(struct hwrm_fwd_req_cmpl *)txcmp;
3024 
3025 	switch (cmpl_type) {
3026 	case CMPL_BASE_TYPE_HWRM_DONE:
3027 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
3028 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
3029 		break;
3030 
3031 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
3032 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
3033 
3034 		if ((vf_id < bp->pf.first_vf_id) ||
3035 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
3036 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
3037 				   vf_id);
3038 			return -EINVAL;
3039 		}
3040 
3041 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
3042 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
3043 		break;
3044 
3045 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
3046 		bnxt_async_event_process(bp,
3047 					 (struct hwrm_async_event_cmpl *)txcmp);
3048 		break;
3049 
3050 	default:
3051 		break;
3052 	}
3053 
3054 	return 0;
3055 }
3056 
3057 static bool bnxt_vnic_is_active(struct bnxt *bp)
3058 {
3059 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
3060 
3061 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
3062 }
3063 
3064 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
3065 {
3066 	struct bnxt_napi *bnapi = dev_instance;
3067 	struct bnxt *bp = bnapi->bp;
3068 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3069 	u32 cons = RING_CMP(cpr->cp_raw_cons);
3070 
3071 	cpr->event_ctr++;
3072 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
3073 	napi_schedule(&bnapi->napi);
3074 	return IRQ_HANDLED;
3075 }
3076 
3077 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
3078 {
3079 	u32 raw_cons = cpr->cp_raw_cons;
3080 	u16 cons = RING_CMP(raw_cons);
3081 	struct tx_cmp *txcmp;
3082 
3083 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3084 
3085 	return TX_CMP_VALID(txcmp, raw_cons);
3086 }
3087 
3088 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3089 			    int budget)
3090 {
3091 	struct bnxt_napi *bnapi = cpr->bnapi;
3092 	u32 raw_cons = cpr->cp_raw_cons;
3093 	bool flush_xdp = false;
3094 	u32 cons;
3095 	int rx_pkts = 0;
3096 	u8 event = 0;
3097 	struct tx_cmp *txcmp;
3098 
3099 	cpr->has_more_work = 0;
3100 	cpr->had_work_done = 1;
3101 	while (1) {
3102 		u8 cmp_type;
3103 		int rc;
3104 
3105 		cons = RING_CMP(raw_cons);
3106 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3107 
3108 		if (!TX_CMP_VALID(txcmp, raw_cons))
3109 			break;
3110 
3111 		/* The valid test of the entry must be done first before
3112 		 * reading any further.
3113 		 */
3114 		dma_rmb();
3115 		cmp_type = TX_CMP_TYPE(txcmp);
3116 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
3117 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
3118 			u32 opaque = txcmp->tx_cmp_opaque;
3119 			struct bnxt_tx_ring_info *txr;
3120 			u16 tx_freed;
3121 
3122 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
3123 			event |= BNXT_TX_CMP_EVENT;
3124 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
3125 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
3126 			else
3127 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
3128 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
3129 				   bp->tx_ring_mask;
3130 			/* return full budget so NAPI will complete. */
3131 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
3132 				rx_pkts = budget;
3133 				raw_cons = NEXT_RAW_CMP(raw_cons);
3134 				if (budget)
3135 					cpr->has_more_work = 1;
3136 				break;
3137 			}
3138 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
3139 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
3140 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
3141 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
3142 			if (likely(budget))
3143 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3144 			else
3145 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
3146 							   &event);
3147 			if (event & BNXT_REDIRECT_EVENT)
3148 				flush_xdp = true;
3149 			if (likely(rc >= 0))
3150 				rx_pkts += rc;
3151 			/* Increment rx_pkts when rc is -ENOMEM to count towards
3152 			 * the NAPI budget.  Otherwise, we may potentially loop
3153 			 * here forever if we consistently cannot allocate
3154 			 * buffers.
3155 			 */
3156 			else if (rc == -ENOMEM && budget)
3157 				rx_pkts++;
3158 			else if (rc == -EBUSY)	/* partial completion */
3159 				break;
3160 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3161 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3162 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3163 			bnxt_hwrm_handler(bp, txcmp);
3164 		}
3165 		raw_cons = NEXT_RAW_CMP(raw_cons);
3166 
3167 		if (rx_pkts && rx_pkts == budget) {
3168 			cpr->has_more_work = 1;
3169 			break;
3170 		}
3171 	}
3172 
3173 	if (flush_xdp) {
3174 		xdp_do_flush();
3175 		event &= ~BNXT_REDIRECT_EVENT;
3176 	}
3177 
3178 	if (event & BNXT_TX_EVENT) {
3179 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3180 		u16 prod = txr->tx_prod;
3181 
3182 		/* Sync BD data before updating doorbell */
3183 		wmb();
3184 
3185 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3186 		event &= ~BNXT_TX_EVENT;
3187 	}
3188 
3189 	cpr->cp_raw_cons = raw_cons;
3190 	bnapi->events |= event;
3191 	return rx_pkts;
3192 }
3193 
3194 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3195 				  int budget)
3196 {
3197 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3198 		bnapi->tx_int(bp, bnapi, budget);
3199 
3200 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3201 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3202 
3203 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3204 		bnapi->events &= ~BNXT_RX_EVENT;
3205 	}
3206 	if (bnapi->events & BNXT_AGG_EVENT) {
3207 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3208 
3209 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3210 		bnapi->events &= ~BNXT_AGG_EVENT;
3211 	}
3212 }
3213 
3214 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3215 			  int budget)
3216 {
3217 	struct bnxt_napi *bnapi = cpr->bnapi;
3218 	int rx_pkts;
3219 
3220 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3221 
3222 	/* ACK completion ring before freeing tx ring and producing new
3223 	 * buffers in rx/agg rings to prevent overflowing the completion
3224 	 * ring.
3225 	 */
3226 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3227 
3228 	__bnxt_poll_work_done(bp, bnapi, budget);
3229 	return rx_pkts;
3230 }
3231 
3232 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3233 {
3234 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3235 	struct bnxt *bp = bnapi->bp;
3236 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3237 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3238 	struct tx_cmp *txcmp;
3239 	struct rx_cmp_ext *rxcmp1;
3240 	u32 cp_cons, tmp_raw_cons;
3241 	u32 raw_cons = cpr->cp_raw_cons;
3242 	bool flush_xdp = false;
3243 	u32 rx_pkts = 0;
3244 	u8 event = 0;
3245 
3246 	while (1) {
3247 		int rc;
3248 
3249 		cp_cons = RING_CMP(raw_cons);
3250 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3251 
3252 		if (!TX_CMP_VALID(txcmp, raw_cons))
3253 			break;
3254 
3255 		/* The valid test of the entry must be done first before
3256 		 * reading any further.
3257 		 */
3258 		dma_rmb();
3259 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3260 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3261 			cp_cons = RING_CMP(tmp_raw_cons);
3262 			rxcmp1 = (struct rx_cmp_ext *)
3263 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3264 
3265 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3266 				break;
3267 
3268 			/* force an error to recycle the buffer */
3269 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3270 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3271 
3272 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3273 			if (likely(rc == -EIO) && budget)
3274 				rx_pkts++;
3275 			else if (rc == -EBUSY)	/* partial completion */
3276 				break;
3277 			if (event & BNXT_REDIRECT_EVENT)
3278 				flush_xdp = true;
3279 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3280 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3281 			bnxt_hwrm_handler(bp, txcmp);
3282 		} else {
3283 			netdev_err(bp->dev,
3284 				   "Invalid completion received on special ring\n");
3285 		}
3286 		raw_cons = NEXT_RAW_CMP(raw_cons);
3287 
3288 		if (rx_pkts == budget)
3289 			break;
3290 	}
3291 
3292 	cpr->cp_raw_cons = raw_cons;
3293 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3294 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3295 
3296 	if (event & BNXT_AGG_EVENT)
3297 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3298 	if (flush_xdp)
3299 		xdp_do_flush();
3300 
3301 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3302 		napi_complete_done(napi, rx_pkts);
3303 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3304 	}
3305 	return rx_pkts;
3306 }
3307 
3308 static int bnxt_poll(struct napi_struct *napi, int budget)
3309 {
3310 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3311 	struct bnxt *bp = bnapi->bp;
3312 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3313 	int work_done = 0;
3314 
3315 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3316 		napi_complete(napi);
3317 		return 0;
3318 	}
3319 	while (1) {
3320 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3321 
3322 		if (work_done >= budget) {
3323 			if (!budget)
3324 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3325 			break;
3326 		}
3327 
3328 		if (!bnxt_has_work(bp, cpr)) {
3329 			if (napi_complete_done(napi, work_done))
3330 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3331 			break;
3332 		}
3333 	}
3334 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3335 		struct dim_sample dim_sample = {};
3336 
3337 		dim_update_sample(cpr->event_ctr,
3338 				  cpr->rx_packets,
3339 				  cpr->rx_bytes,
3340 				  &dim_sample);
3341 		net_dim(&cpr->dim, &dim_sample);
3342 	}
3343 	return work_done;
3344 }
3345 
3346 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3347 {
3348 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3349 	int i, work_done = 0;
3350 
3351 	for (i = 0; i < cpr->cp_ring_count; i++) {
3352 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3353 
3354 		if (cpr2->had_nqe_notify) {
3355 			work_done += __bnxt_poll_work(bp, cpr2,
3356 						      budget - work_done);
3357 			cpr->has_more_work |= cpr2->has_more_work;
3358 		}
3359 	}
3360 	return work_done;
3361 }
3362 
3363 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3364 				 u64 dbr_type, int budget)
3365 {
3366 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3367 	int i;
3368 
3369 	for (i = 0; i < cpr->cp_ring_count; i++) {
3370 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3371 		struct bnxt_db_info *db;
3372 
3373 		if (cpr2->had_work_done) {
3374 			u32 tgl = 0;
3375 
3376 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3377 				cpr2->had_nqe_notify = 0;
3378 				tgl = cpr2->toggle;
3379 			}
3380 			db = &cpr2->cp_db;
3381 			bnxt_writeq(bp,
3382 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3383 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3384 				    db->doorbell);
3385 			cpr2->had_work_done = 0;
3386 		}
3387 	}
3388 	__bnxt_poll_work_done(bp, bnapi, budget);
3389 }
3390 
3391 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3392 {
3393 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3394 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3395 	struct bnxt_cp_ring_info *cpr_rx;
3396 	u32 raw_cons = cpr->cp_raw_cons;
3397 	struct bnxt *bp = bnapi->bp;
3398 	struct nqe_cn *nqcmp;
3399 	int work_done = 0;
3400 	u32 cons;
3401 
3402 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3403 		napi_complete(napi);
3404 		return 0;
3405 	}
3406 	if (cpr->has_more_work) {
3407 		cpr->has_more_work = 0;
3408 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3409 	}
3410 	while (1) {
3411 		u16 type;
3412 
3413 		cons = RING_CMP(raw_cons);
3414 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3415 
3416 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3417 			if (cpr->has_more_work)
3418 				break;
3419 
3420 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3421 					     budget);
3422 			cpr->cp_raw_cons = raw_cons;
3423 			if (napi_complete_done(napi, work_done))
3424 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3425 						  cpr->cp_raw_cons);
3426 			goto poll_done;
3427 		}
3428 
3429 		/* The valid test of the entry must be done first before
3430 		 * reading any further.
3431 		 */
3432 		dma_rmb();
3433 
3434 		type = le16_to_cpu(nqcmp->type);
3435 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3436 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3437 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3438 			struct bnxt_cp_ring_info *cpr2;
3439 
3440 			/* No more budget for RX work */
3441 			if (budget && work_done >= budget &&
3442 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3443 				break;
3444 
3445 			idx = BNXT_NQ_HDL_IDX(idx);
3446 			cpr2 = &cpr->cp_ring_arr[idx];
3447 			cpr2->had_nqe_notify = 1;
3448 			cpr2->toggle = NQE_CN_TOGGLE(type);
3449 			work_done += __bnxt_poll_work(bp, cpr2,
3450 						      budget - work_done);
3451 			cpr->has_more_work |= cpr2->has_more_work;
3452 		} else {
3453 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3454 		}
3455 		raw_cons = NEXT_RAW_CMP(raw_cons);
3456 	}
3457 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3458 	if (raw_cons != cpr->cp_raw_cons) {
3459 		cpr->cp_raw_cons = raw_cons;
3460 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3461 	}
3462 poll_done:
3463 	cpr_rx = &cpr->cp_ring_arr[0];
3464 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3465 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3466 		struct dim_sample dim_sample = {};
3467 
3468 		dim_update_sample(cpr->event_ctr,
3469 				  cpr_rx->rx_packets,
3470 				  cpr_rx->rx_bytes,
3471 				  &dim_sample);
3472 		net_dim(&cpr->dim, &dim_sample);
3473 	}
3474 	return work_done;
3475 }
3476 
3477 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp,
3478 				       struct bnxt_tx_ring_info *txr, int idx)
3479 {
3480 	int i, max_idx;
3481 	struct pci_dev *pdev = bp->pdev;
3482 	unsigned int dma_len;
3483 	dma_addr_t dma_addr;
3484 
3485 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3486 
3487 	for (i = 0; i < max_idx;) {
3488 		struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i];
3489 		struct bnxt_sw_tx_bd *head_buf = tx_buf;
3490 		struct sk_buff *skb;
3491 		int j, last;
3492 
3493 		if (idx  < bp->tx_nr_rings_xdp &&
3494 		    tx_buf->action == XDP_REDIRECT) {
3495 			dma_addr = dma_unmap_addr(tx_buf, mapping);
3496 			dma_len = dma_unmap_len(tx_buf, len);
3497 
3498 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
3499 					 DMA_TO_DEVICE);
3500 			xdp_return_frame(tx_buf->xdpf);
3501 			tx_buf->action = 0;
3502 			tx_buf->xdpf = NULL;
3503 			i++;
3504 			continue;
3505 		}
3506 
3507 		skb = tx_buf->skb;
3508 		if (!skb) {
3509 			i++;
3510 			continue;
3511 		}
3512 
3513 		tx_buf->skb = NULL;
3514 
3515 		if (tx_buf->is_push) {
3516 			dev_kfree_skb(skb);
3517 			i += 2;
3518 			continue;
3519 		}
3520 
3521 		if (dma_unmap_len(tx_buf, len)) {
3522 			dma_addr = dma_unmap_addr(tx_buf, mapping);
3523 			dma_len = dma_unmap_len(tx_buf, len);
3524 
3525 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
3526 					 DMA_TO_DEVICE);
3527 		}
3528 
3529 		last = tx_buf->nr_frags;
3530 		i += 2;
3531 		for (j = 0; j < last; j++, i++) {
3532 			int ring_idx = i & bp->tx_ring_mask;
3533 
3534 			tx_buf = &txr->tx_buf_ring[ring_idx];
3535 			if (dma_unmap_len(tx_buf, len)) {
3536 				dma_addr = dma_unmap_addr(tx_buf, mapping);
3537 				dma_len = dma_unmap_len(tx_buf, len);
3538 
3539 				netmem_dma_unmap_page_attrs(&pdev->dev,
3540 							    dma_addr, dma_len,
3541 							    DMA_TO_DEVICE, 0);
3542 			}
3543 		}
3544 		if (head_buf->is_sw_gso) {
3545 			u16 inline_cons = txr->tx_inline_cons + 1;
3546 
3547 			WRITE_ONCE(txr->tx_inline_cons, inline_cons);
3548 			if (head_buf->is_sw_gso == BNXT_SW_GSO_LAST) {
3549 				tso_dma_map_complete(&pdev->dev,
3550 						     &head_buf->sw_gso_cstate);
3551 			} else {
3552 				skb = NULL;
3553 			}
3554 			head_buf->is_sw_gso = 0;
3555 		}
3556 		if (skb)
3557 			dev_kfree_skb(skb);
3558 	}
3559 	netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx));
3560 }
3561 
3562 static void bnxt_free_tx_skbs(struct bnxt *bp)
3563 {
3564 	int i;
3565 
3566 	if (!bp->tx_ring)
3567 		return;
3568 
3569 	for (i = 0; i < bp->tx_nr_rings; i++) {
3570 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3571 
3572 		if (!txr->tx_buf_ring)
3573 			continue;
3574 
3575 		bnxt_free_one_tx_ring_skbs(bp, txr, i);
3576 	}
3577 
3578 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
3579 		bnxt_ptp_free_txts_skbs(bp->ptp_cfg);
3580 }
3581 
3582 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3583 {
3584 	int i, max_idx;
3585 
3586 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3587 
3588 	for (i = 0; i < max_idx; i++) {
3589 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3590 		void *data = rx_buf->data;
3591 
3592 		if (!data)
3593 			continue;
3594 
3595 		rx_buf->data = NULL;
3596 		if (BNXT_RX_PAGE_MODE(bp))
3597 			page_pool_recycle_direct(rxr->page_pool, data);
3598 		else
3599 			page_pool_free_va(rxr->head_pool, data, true);
3600 	}
3601 }
3602 
3603 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3604 {
3605 	int i, max_idx;
3606 
3607 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3608 
3609 	for (i = 0; i < max_idx; i++) {
3610 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3611 		netmem_ref netmem = rx_agg_buf->netmem;
3612 
3613 		if (!netmem)
3614 			continue;
3615 
3616 		rx_agg_buf->netmem = 0;
3617 		__clear_bit(i, rxr->rx_agg_bmap);
3618 
3619 		page_pool_recycle_direct_netmem(rxr->page_pool, netmem);
3620 	}
3621 }
3622 
3623 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3624 					struct bnxt_rx_ring_info *rxr)
3625 {
3626 	int i;
3627 
3628 	for (i = 0; i < bp->max_tpa_roundup_size; i++) {
3629 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3630 		u8 *data = tpa_info->data;
3631 
3632 		if (!data)
3633 			continue;
3634 
3635 		tpa_info->data = NULL;
3636 		page_pool_free_va(rxr->head_pool, data, false);
3637 	}
3638 }
3639 
3640 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3641 				       struct bnxt_rx_ring_info *rxr)
3642 {
3643 	struct bnxt_tpa_idx_map *map;
3644 
3645 	if (!rxr->rx_tpa)
3646 		goto skip_rx_tpa_free;
3647 
3648 	bnxt_free_one_tpa_info_data(bp, rxr);
3649 
3650 skip_rx_tpa_free:
3651 	if (!rxr->rx_buf_ring)
3652 		goto skip_rx_buf_free;
3653 
3654 	bnxt_free_one_rx_ring(bp, rxr);
3655 
3656 skip_rx_buf_free:
3657 	if (!rxr->rx_agg_ring)
3658 		goto skip_rx_agg_free;
3659 
3660 	bnxt_free_one_rx_agg_ring(bp, rxr);
3661 
3662 skip_rx_agg_free:
3663 	map = rxr->rx_tpa_idx_map;
3664 	if (map)
3665 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3666 }
3667 
3668 static void bnxt_free_rx_skbs(struct bnxt *bp)
3669 {
3670 	int i;
3671 
3672 	if (!bp->rx_ring)
3673 		return;
3674 
3675 	for (i = 0; i < bp->rx_nr_rings; i++)
3676 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3677 }
3678 
3679 static void bnxt_free_skbs(struct bnxt *bp)
3680 {
3681 	bnxt_free_tx_skbs(bp);
3682 	bnxt_free_rx_skbs(bp);
3683 }
3684 
3685 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3686 {
3687 	u8 init_val = ctxm->init_value;
3688 	u16 offset = ctxm->init_offset;
3689 	u8 *p2 = p;
3690 	int i;
3691 
3692 	if (!init_val)
3693 		return;
3694 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3695 		memset(p, init_val, len);
3696 		return;
3697 	}
3698 	for (i = 0; i < len; i += ctxm->entry_size)
3699 		*(p2 + i + offset) = init_val;
3700 }
3701 
3702 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3703 			       void *buf, size_t offset, size_t head,
3704 			       size_t tail)
3705 {
3706 	int i, head_page, start_idx, source_offset;
3707 	size_t len, rem_len, total_len, max_bytes;
3708 
3709 	head_page = head / rmem->page_size;
3710 	source_offset = head % rmem->page_size;
3711 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3712 	if (!total_len)
3713 		total_len = MAX_CTX_BYTES;
3714 	start_idx = head_page % MAX_CTX_PAGES;
3715 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3716 		    source_offset;
3717 	total_len = min(total_len, max_bytes);
3718 	rem_len = total_len;
3719 
3720 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3721 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3722 		if (buf)
3723 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3724 			       len);
3725 		offset += len;
3726 		rem_len -= len;
3727 	}
3728 	return total_len;
3729 }
3730 
3731 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3732 {
3733 	struct pci_dev *pdev = bp->pdev;
3734 	int i;
3735 
3736 	if (!rmem->pg_arr)
3737 		goto skip_pages;
3738 
3739 	for (i = 0; i < rmem->nr_pages; i++) {
3740 		if (!rmem->pg_arr[i])
3741 			continue;
3742 
3743 		dma_free_coherent(&pdev->dev, rmem->page_size,
3744 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3745 
3746 		rmem->pg_arr[i] = NULL;
3747 	}
3748 skip_pages:
3749 	if (rmem->pg_tbl) {
3750 		size_t pg_tbl_size = rmem->nr_pages * 8;
3751 
3752 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3753 			pg_tbl_size = rmem->page_size;
3754 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3755 				  rmem->pg_tbl, rmem->pg_tbl_map);
3756 		rmem->pg_tbl = NULL;
3757 	}
3758 	if (rmem->vmem_size && *rmem->vmem) {
3759 		vfree(*rmem->vmem);
3760 		*rmem->vmem = NULL;
3761 	}
3762 }
3763 
3764 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3765 {
3766 	struct pci_dev *pdev = bp->pdev;
3767 	u64 valid_bit = 0;
3768 	int i;
3769 
3770 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3771 		valid_bit = PTU_PTE_VALID;
3772 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3773 		size_t pg_tbl_size = rmem->nr_pages * 8;
3774 
3775 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3776 			pg_tbl_size = rmem->page_size;
3777 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3778 						  &rmem->pg_tbl_map,
3779 						  GFP_KERNEL);
3780 		if (!rmem->pg_tbl)
3781 			return -ENOMEM;
3782 	}
3783 
3784 	for (i = 0; i < rmem->nr_pages; i++) {
3785 		u64 extra_bits = valid_bit;
3786 
3787 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3788 						     rmem->page_size,
3789 						     &rmem->dma_arr[i],
3790 						     GFP_KERNEL);
3791 		if (!rmem->pg_arr[i])
3792 			return -ENOMEM;
3793 
3794 		if (rmem->ctx_mem)
3795 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3796 					  rmem->page_size);
3797 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3798 			if (i == rmem->nr_pages - 2 &&
3799 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3800 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3801 			else if (i == rmem->nr_pages - 1 &&
3802 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3803 				extra_bits |= PTU_PTE_LAST;
3804 			rmem->pg_tbl[i] =
3805 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3806 		}
3807 	}
3808 
3809 	if (rmem->vmem_size) {
3810 		*rmem->vmem = vzalloc(rmem->vmem_size);
3811 		if (!(*rmem->vmem))
3812 			return -ENOMEM;
3813 	}
3814 	return 0;
3815 }
3816 
3817 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3818 				   struct bnxt_rx_ring_info *rxr)
3819 {
3820 	int i;
3821 
3822 	kfree(rxr->rx_tpa_idx_map);
3823 	rxr->rx_tpa_idx_map = NULL;
3824 	if (rxr->rx_tpa) {
3825 		for (i = 0; i < bp->max_tpa_roundup_size; i++) {
3826 			kfree(rxr->rx_tpa[i].agg_arr);
3827 			rxr->rx_tpa[i].agg_arr = NULL;
3828 		}
3829 	}
3830 	kfree(rxr->rx_tpa);
3831 	rxr->rx_tpa = NULL;
3832 }
3833 
3834 static void bnxt_free_tpa_info(struct bnxt *bp)
3835 {
3836 	int i;
3837 
3838 	for (i = 0; i < bp->rx_nr_rings; i++) {
3839 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3840 
3841 		bnxt_free_one_tpa_info(bp, rxr);
3842 	}
3843 }
3844 
3845 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3846 				   struct bnxt_rx_ring_info *rxr)
3847 {
3848 	struct rx_agg_cmp *agg;
3849 	int i;
3850 
3851 	rxr->rx_tpa = kzalloc_objs(struct bnxt_tpa_info,
3852 				   bp->max_tpa_roundup_size);
3853 	if (!rxr->rx_tpa)
3854 		return -ENOMEM;
3855 
3856 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3857 		return 0;
3858 	for (i = 0; i < bp->max_tpa_roundup_size; i++) {
3859 		agg = kzalloc_objs(*agg, MAX_SKB_FRAGS);
3860 		if (!agg)
3861 			return -ENOMEM;
3862 		rxr->rx_tpa[i].agg_arr = agg;
3863 	}
3864 	rxr->rx_tpa_idx_map = kzalloc_obj(*rxr->rx_tpa_idx_map);
3865 	if (!rxr->rx_tpa_idx_map)
3866 		return -ENOMEM;
3867 
3868 	return 0;
3869 }
3870 
3871 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3872 {
3873 	int i, rc;
3874 
3875 	bp->max_tpa = MAX_TPA;
3876 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3877 		/* TPA is not supported at all, so there is nothing to
3878 		 * allocate.
3879 		 */
3880 		if (!bp->max_tpa_v2)
3881 			return 0;
3882 		bp->max_tpa = min_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3883 		/* Older P5 FW sets max_tpa_v2 low by mistake except NPAR */
3884 		if (bp->max_tpa <= 32 && BNXT_CHIP_P5(bp) && !BNXT_NPAR(bp))
3885 			bp->max_tpa = MAX_TPA_P5;
3886 	}
3887 	bp->max_tpa_roundup_size = roundup_pow_of_two(bp->max_tpa);
3888 
3889 	for (i = 0; i < bp->rx_nr_rings; i++) {
3890 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3891 
3892 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3893 		if (rc)
3894 			return rc;
3895 	}
3896 	return 0;
3897 }
3898 
3899 static void bnxt_free_rx_rings(struct bnxt *bp)
3900 {
3901 	int i;
3902 
3903 	if (!bp->rx_ring)
3904 		return;
3905 
3906 	bnxt_free_tpa_info(bp);
3907 	for (i = 0; i < bp->rx_nr_rings; i++) {
3908 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3909 		struct bnxt_ring_struct *ring;
3910 
3911 		if (rxr->xdp_prog)
3912 			bpf_prog_put(rxr->xdp_prog);
3913 
3914 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3915 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3916 
3917 		page_pool_destroy(rxr->page_pool);
3918 		page_pool_destroy(rxr->head_pool);
3919 		rxr->page_pool = rxr->head_pool = NULL;
3920 
3921 		kfree(rxr->rx_agg_bmap);
3922 		rxr->rx_agg_bmap = NULL;
3923 
3924 		ring = &rxr->rx_ring_struct;
3925 		bnxt_free_ring(bp, &ring->ring_mem);
3926 
3927 		ring = &rxr->rx_agg_ring_struct;
3928 		bnxt_free_ring(bp, &ring->ring_mem);
3929 	}
3930 }
3931 
3932 static int bnxt_rx_agg_ring_fill_level(struct bnxt *bp,
3933 				       struct bnxt_rx_ring_info *rxr)
3934 {
3935 	/* User may have chosen larger than default rx_page_size,
3936 	 * we keep the ring sizes uniform and also want uniform amount
3937 	 * of bytes consumed per ring, so cap how much of the rings we fill.
3938 	 */
3939 	int fill_level = bp->rx_agg_ring_size;
3940 
3941 	if (rxr->rx_page_size > BNXT_RX_PAGE_SIZE)
3942 		fill_level /= rxr->rx_page_size / BNXT_RX_PAGE_SIZE;
3943 
3944 	return fill_level;
3945 }
3946 
3947 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3948 				   struct bnxt_rx_ring_info *rxr,
3949 				   int numa_node)
3950 {
3951 	unsigned int agg_size_fac = rxr->rx_page_size / BNXT_RX_PAGE_SIZE;
3952 	const unsigned int rx_size_fac = PAGE_SIZE / SZ_4K;
3953 	struct page_pool_params pp = { 0 };
3954 	struct page_pool *pool;
3955 
3956 	pp.pool_size = bnxt_rx_agg_ring_fill_level(bp, rxr) / agg_size_fac;
3957 	if (BNXT_RX_PAGE_MODE(bp))
3958 		pp.pool_size += bp->rx_ring_size / rx_size_fac;
3959 
3960 	pp.order = get_order(rxr->rx_page_size);
3961 	pp.nid = numa_node;
3962 	pp.netdev = bp->dev;
3963 	pp.dev = &bp->pdev->dev;
3964 	pp.dma_dir = bp->rx_dir;
3965 	pp.max_len = PAGE_SIZE << pp.order;
3966 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV |
3967 		   PP_FLAG_ALLOW_UNREADABLE_NETMEM;
3968 	pp.queue_idx = rxr->bnapi->index;
3969 
3970 	pool = page_pool_create(&pp);
3971 	if (IS_ERR(pool))
3972 		return PTR_ERR(pool);
3973 	rxr->page_pool = pool;
3974 
3975 	rxr->need_head_pool = page_pool_is_unreadable(pool);
3976 	rxr->need_head_pool |= !!pp.order;
3977 	if (bnxt_separate_head_pool(rxr)) {
3978 		pp.order = 0;
3979 		pp.max_len = PAGE_SIZE;
3980 		pp.pool_size = min(bp->rx_ring_size / rx_size_fac, 1024);
3981 		pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3982 		pool = page_pool_create(&pp);
3983 		if (IS_ERR(pool))
3984 			goto err_destroy_pp;
3985 	} else {
3986 		page_pool_get(pool);
3987 	}
3988 	rxr->head_pool = pool;
3989 
3990 	return 0;
3991 
3992 err_destroy_pp:
3993 	page_pool_destroy(rxr->page_pool);
3994 	rxr->page_pool = NULL;
3995 	return PTR_ERR(pool);
3996 }
3997 
3998 static void bnxt_enable_rx_page_pool(struct bnxt_rx_ring_info *rxr)
3999 {
4000 	page_pool_enable_direct_recycling(rxr->head_pool, &rxr->bnapi->napi);
4001 	page_pool_enable_direct_recycling(rxr->page_pool, &rxr->bnapi->napi);
4002 }
4003 
4004 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
4005 {
4006 	u16 mem_size;
4007 
4008 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
4009 	mem_size = rxr->rx_agg_bmap_size / 8;
4010 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
4011 	if (!rxr->rx_agg_bmap)
4012 		return -ENOMEM;
4013 
4014 	return 0;
4015 }
4016 
4017 static int bnxt_alloc_rx_rings(struct bnxt *bp)
4018 {
4019 	int numa_node = dev_to_node(&bp->pdev->dev);
4020 	int i, rc = 0, agg_rings = 0, cpu;
4021 
4022 	if (!bp->rx_ring)
4023 		return -ENOMEM;
4024 
4025 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
4026 		agg_rings = 1;
4027 
4028 	for (i = 0; i < bp->rx_nr_rings; i++) {
4029 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4030 		struct bnxt_ring_struct *ring;
4031 		int cpu_node;
4032 
4033 		ring = &rxr->rx_ring_struct;
4034 
4035 		cpu = cpumask_local_spread(i, numa_node);
4036 		cpu_node = cpu_to_node(cpu);
4037 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
4038 			   i, cpu_node);
4039 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
4040 		if (rc)
4041 			return rc;
4042 		bnxt_enable_rx_page_pool(rxr);
4043 
4044 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
4045 		if (rc < 0)
4046 			return rc;
4047 
4048 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
4049 						MEM_TYPE_PAGE_POOL,
4050 						rxr->page_pool);
4051 		if (rc) {
4052 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
4053 			return rc;
4054 		}
4055 
4056 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4057 		if (rc)
4058 			return rc;
4059 
4060 		ring->grp_idx = i;
4061 		if (agg_rings) {
4062 			ring = &rxr->rx_agg_ring_struct;
4063 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4064 			if (rc)
4065 				return rc;
4066 
4067 			ring->grp_idx = i;
4068 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
4069 			if (rc)
4070 				return rc;
4071 		}
4072 	}
4073 	if (bp->flags & BNXT_FLAG_TPA)
4074 		rc = bnxt_alloc_tpa_info(bp);
4075 	return rc;
4076 }
4077 
4078 static void bnxt_free_tx_inline_buf(struct bnxt_tx_ring_info *txr,
4079 				    struct pci_dev *pdev)
4080 {
4081 	if (!txr->tx_inline_buf)
4082 		return;
4083 
4084 	dma_unmap_single(&pdev->dev, txr->tx_inline_dma,
4085 			 txr->tx_inline_size, DMA_TO_DEVICE);
4086 	kfree(txr->tx_inline_buf);
4087 	txr->tx_inline_buf = NULL;
4088 	txr->tx_inline_size = 0;
4089 }
4090 
4091 static int bnxt_alloc_tx_inline_buf(struct bnxt_tx_ring_info *txr,
4092 				    struct pci_dev *pdev,
4093 				    unsigned int size)
4094 {
4095 	txr->tx_inline_buf = kmalloc(size, GFP_KERNEL);
4096 	if (!txr->tx_inline_buf)
4097 		return -ENOMEM;
4098 
4099 	txr->tx_inline_dma = dma_map_single(&pdev->dev, txr->tx_inline_buf,
4100 					    size, DMA_TO_DEVICE);
4101 	if (dma_mapping_error(&pdev->dev, txr->tx_inline_dma)) {
4102 		kfree(txr->tx_inline_buf);
4103 		txr->tx_inline_buf = NULL;
4104 		return -ENOMEM;
4105 	}
4106 	txr->tx_inline_size = size;
4107 
4108 	return 0;
4109 }
4110 
4111 static void bnxt_free_tx_rings(struct bnxt *bp)
4112 {
4113 	int i;
4114 	struct pci_dev *pdev = bp->pdev;
4115 
4116 	if (!bp->tx_ring)
4117 		return;
4118 
4119 	for (i = 0; i < bp->tx_nr_rings; i++) {
4120 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4121 		struct bnxt_ring_struct *ring;
4122 
4123 		if (txr->tx_push) {
4124 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
4125 					  txr->tx_push, txr->tx_push_mapping);
4126 			txr->tx_push = NULL;
4127 		}
4128 
4129 		bnxt_free_tx_inline_buf(txr, pdev);
4130 
4131 		ring = &txr->tx_ring_struct;
4132 
4133 		bnxt_free_ring(bp, &ring->ring_mem);
4134 	}
4135 }
4136 
4137 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
4138 	((tc) * (bp)->tx_nr_rings_per_tc)
4139 
4140 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
4141 	((tx) % (bp)->tx_nr_rings_per_tc)
4142 
4143 #define BNXT_RING_TO_TC(bp, tx)		\
4144 	((tx) / (bp)->tx_nr_rings_per_tc)
4145 
4146 static int bnxt_alloc_tx_rings(struct bnxt *bp)
4147 {
4148 	int i, j, rc;
4149 	struct pci_dev *pdev = bp->pdev;
4150 
4151 	bp->tx_push_size = 0;
4152 	if (bp->tx_push_thresh) {
4153 		int push_size;
4154 
4155 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
4156 					bp->tx_push_thresh);
4157 
4158 		if (push_size > 256) {
4159 			push_size = 0;
4160 			bp->tx_push_thresh = 0;
4161 		}
4162 
4163 		bp->tx_push_size = push_size;
4164 	}
4165 
4166 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
4167 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4168 		struct bnxt_ring_struct *ring;
4169 		u8 qidx;
4170 
4171 		ring = &txr->tx_ring_struct;
4172 
4173 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4174 		if (rc)
4175 			return rc;
4176 
4177 		ring->grp_idx = txr->bnapi->index;
4178 		if (bp->tx_push_size) {
4179 			dma_addr_t mapping;
4180 
4181 			/* One pre-allocated DMA buffer to backup
4182 			 * TX push operation
4183 			 */
4184 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
4185 						bp->tx_push_size,
4186 						&txr->tx_push_mapping,
4187 						GFP_KERNEL);
4188 
4189 			if (!txr->tx_push)
4190 				return -ENOMEM;
4191 
4192 			mapping = txr->tx_push_mapping +
4193 				sizeof(struct tx_push_bd);
4194 			txr->data_mapping = cpu_to_le64(mapping);
4195 		}
4196 		if (!(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) {
4197 			rc = bnxt_alloc_tx_inline_buf(txr, pdev,
4198 						      BNXT_SW_USO_MAX_SEGS *
4199 						      TSO_HEADER_SIZE);
4200 			if (rc)
4201 				return rc;
4202 		}
4203 		qidx = bp->tc_to_qidx[j];
4204 		ring->queue_id = bp->q_info[qidx].queue_id;
4205 		spin_lock_init(&txr->xdp_tx_lock);
4206 		if (i < bp->tx_nr_rings_xdp)
4207 			continue;
4208 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
4209 			j++;
4210 	}
4211 	return 0;
4212 }
4213 
4214 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
4215 {
4216 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4217 
4218 	kfree(cpr->cp_desc_ring);
4219 	cpr->cp_desc_ring = NULL;
4220 	ring->ring_mem.pg_arr = NULL;
4221 	kfree(cpr->cp_desc_mapping);
4222 	cpr->cp_desc_mapping = NULL;
4223 	ring->ring_mem.dma_arr = NULL;
4224 }
4225 
4226 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
4227 {
4228 	cpr->cp_desc_ring = kzalloc_objs(*cpr->cp_desc_ring, n);
4229 	if (!cpr->cp_desc_ring)
4230 		return -ENOMEM;
4231 	cpr->cp_desc_mapping = kzalloc_objs(*cpr->cp_desc_mapping, n);
4232 	if (!cpr->cp_desc_mapping)
4233 		return -ENOMEM;
4234 	return 0;
4235 }
4236 
4237 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
4238 {
4239 	int i;
4240 
4241 	if (!bp->bnapi)
4242 		return;
4243 	for (i = 0; i < bp->cp_nr_rings; i++) {
4244 		struct bnxt_napi *bnapi = bp->bnapi[i];
4245 
4246 		if (!bnapi)
4247 			continue;
4248 		bnxt_free_cp_arrays(&bnapi->cp_ring);
4249 	}
4250 }
4251 
4252 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
4253 {
4254 	int i, n = bp->cp_nr_pages;
4255 
4256 	for (i = 0; i < bp->cp_nr_rings; i++) {
4257 		struct bnxt_napi *bnapi = bp->bnapi[i];
4258 		int rc;
4259 
4260 		if (!bnapi)
4261 			continue;
4262 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
4263 		if (rc)
4264 			return rc;
4265 	}
4266 	return 0;
4267 }
4268 
4269 static void bnxt_free_cp_rings(struct bnxt *bp)
4270 {
4271 	int i;
4272 
4273 	if (!bp->bnapi)
4274 		return;
4275 
4276 	for (i = 0; i < bp->cp_nr_rings; i++) {
4277 		struct bnxt_napi *bnapi = bp->bnapi[i];
4278 		struct bnxt_cp_ring_info *cpr;
4279 		struct bnxt_ring_struct *ring;
4280 		int j;
4281 
4282 		if (!bnapi)
4283 			continue;
4284 
4285 		cpr = &bnapi->cp_ring;
4286 		ring = &cpr->cp_ring_struct;
4287 
4288 		bnxt_free_ring(bp, &ring->ring_mem);
4289 
4290 		if (!cpr->cp_ring_arr)
4291 			continue;
4292 
4293 		for (j = 0; j < cpr->cp_ring_count; j++) {
4294 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4295 
4296 			ring = &cpr2->cp_ring_struct;
4297 			bnxt_free_ring(bp, &ring->ring_mem);
4298 			bnxt_free_cp_arrays(cpr2);
4299 		}
4300 		kfree(cpr->cp_ring_arr);
4301 		cpr->cp_ring_arr = NULL;
4302 		cpr->cp_ring_count = 0;
4303 	}
4304 }
4305 
4306 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4307 				  struct bnxt_cp_ring_info *cpr)
4308 {
4309 	struct bnxt_ring_mem_info *rmem;
4310 	struct bnxt_ring_struct *ring;
4311 	int rc;
4312 
4313 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4314 	if (rc) {
4315 		bnxt_free_cp_arrays(cpr);
4316 		return -ENOMEM;
4317 	}
4318 	ring = &cpr->cp_ring_struct;
4319 	rmem = &ring->ring_mem;
4320 	rmem->nr_pages = bp->cp_nr_pages;
4321 	rmem->page_size = HW_CMPD_RING_SIZE;
4322 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4323 	rmem->dma_arr = cpr->cp_desc_mapping;
4324 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4325 	rc = bnxt_alloc_ring(bp, rmem);
4326 	if (rc) {
4327 		bnxt_free_ring(bp, rmem);
4328 		bnxt_free_cp_arrays(cpr);
4329 	}
4330 	return rc;
4331 }
4332 
4333 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4334 {
4335 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4336 	int i, j, rc, ulp_msix;
4337 	int tcs = bp->num_tc;
4338 
4339 	if (!tcs)
4340 		tcs = 1;
4341 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4342 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4343 		struct bnxt_napi *bnapi = bp->bnapi[i];
4344 		struct bnxt_cp_ring_info *cpr, *cpr2;
4345 		struct bnxt_ring_struct *ring;
4346 		int cp_count = 0, k;
4347 		int rx = 0, tx = 0;
4348 
4349 		if (!bnapi)
4350 			continue;
4351 
4352 		cpr = &bnapi->cp_ring;
4353 		cpr->bnapi = bnapi;
4354 		ring = &cpr->cp_ring_struct;
4355 
4356 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4357 		if (rc)
4358 			return rc;
4359 
4360 		ring->map_idx = ulp_msix + i;
4361 
4362 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4363 			continue;
4364 
4365 		if (i < bp->rx_nr_rings) {
4366 			cp_count++;
4367 			rx = 1;
4368 		}
4369 		if (i < bp->tx_nr_rings_xdp) {
4370 			cp_count++;
4371 			tx = 1;
4372 		} else if ((sh && i < bp->tx_nr_rings) ||
4373 			 (!sh && i >= bp->rx_nr_rings)) {
4374 			cp_count += tcs;
4375 			tx = 1;
4376 		}
4377 
4378 		cpr->cp_ring_arr = kzalloc_objs(*cpr, cp_count);
4379 		if (!cpr->cp_ring_arr)
4380 			return -ENOMEM;
4381 		cpr->cp_ring_count = cp_count;
4382 
4383 		for (k = 0; k < cp_count; k++) {
4384 			cpr2 = &cpr->cp_ring_arr[k];
4385 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4386 			if (rc)
4387 				return rc;
4388 			cpr2->bnapi = bnapi;
4389 			cpr2->sw_stats = cpr->sw_stats;
4390 			cpr2->cp_idx = k;
4391 			if (!k && rx) {
4392 				bp->rx_ring[i].rx_cpr = cpr2;
4393 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4394 			} else {
4395 				int n, tc = k - rx;
4396 
4397 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4398 				bp->tx_ring[n].tx_cpr = cpr2;
4399 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4400 			}
4401 		}
4402 		if (tx)
4403 			j++;
4404 	}
4405 	return 0;
4406 }
4407 
4408 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4409 				     struct bnxt_rx_ring_info *rxr)
4410 {
4411 	struct bnxt_ring_mem_info *rmem;
4412 	struct bnxt_ring_struct *ring;
4413 
4414 	ring = &rxr->rx_ring_struct;
4415 	rmem = &ring->ring_mem;
4416 	rmem->nr_pages = bp->rx_nr_pages;
4417 	rmem->page_size = HW_RXBD_RING_SIZE;
4418 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4419 	rmem->dma_arr = rxr->rx_desc_mapping;
4420 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4421 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4422 
4423 	ring = &rxr->rx_agg_ring_struct;
4424 	rmem = &ring->ring_mem;
4425 	rmem->nr_pages = bp->rx_agg_nr_pages;
4426 	rmem->page_size = HW_RXBD_RING_SIZE;
4427 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4428 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4429 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4430 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4431 }
4432 
4433 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4434 				      struct bnxt_rx_ring_info *rxr)
4435 {
4436 	struct bnxt_ring_mem_info *rmem;
4437 	struct bnxt_ring_struct *ring;
4438 	int i;
4439 
4440 	rxr->page_pool->p.napi = NULL;
4441 	rxr->page_pool = NULL;
4442 	rxr->head_pool->p.napi = NULL;
4443 	rxr->head_pool = NULL;
4444 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4445 
4446 	ring = &rxr->rx_ring_struct;
4447 	rmem = &ring->ring_mem;
4448 	rmem->pg_tbl = NULL;
4449 	rmem->pg_tbl_map = 0;
4450 	for (i = 0; i < rmem->nr_pages; i++) {
4451 		rmem->pg_arr[i] = NULL;
4452 		rmem->dma_arr[i] = 0;
4453 	}
4454 	*rmem->vmem = NULL;
4455 
4456 	ring = &rxr->rx_agg_ring_struct;
4457 	rmem = &ring->ring_mem;
4458 	rmem->pg_tbl = NULL;
4459 	rmem->pg_tbl_map = 0;
4460 	for (i = 0; i < rmem->nr_pages; i++) {
4461 		rmem->pg_arr[i] = NULL;
4462 		rmem->dma_arr[i] = 0;
4463 	}
4464 	*rmem->vmem = NULL;
4465 }
4466 
4467 static void bnxt_init_ring_struct(struct bnxt *bp)
4468 {
4469 	int i, j;
4470 
4471 	for (i = 0; i < bp->cp_nr_rings; i++) {
4472 		struct bnxt_napi *bnapi = bp->bnapi[i];
4473 		struct netdev_queue_config qcfg;
4474 		struct bnxt_ring_mem_info *rmem;
4475 		struct bnxt_cp_ring_info *cpr;
4476 		struct bnxt_rx_ring_info *rxr;
4477 		struct bnxt_tx_ring_info *txr;
4478 		struct bnxt_ring_struct *ring;
4479 
4480 		if (!bnapi)
4481 			continue;
4482 
4483 		cpr = &bnapi->cp_ring;
4484 		ring = &cpr->cp_ring_struct;
4485 		rmem = &ring->ring_mem;
4486 		rmem->nr_pages = bp->cp_nr_pages;
4487 		rmem->page_size = HW_CMPD_RING_SIZE;
4488 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4489 		rmem->dma_arr = cpr->cp_desc_mapping;
4490 		rmem->vmem_size = 0;
4491 
4492 		rxr = bnapi->rx_ring;
4493 		if (!rxr)
4494 			goto skip_rx;
4495 
4496 		netdev_queue_config(bp->dev, i, &qcfg);
4497 		rxr->rx_page_size = qcfg.rx_page_size;
4498 
4499 		ring = &rxr->rx_ring_struct;
4500 		rmem = &ring->ring_mem;
4501 		rmem->nr_pages = bp->rx_nr_pages;
4502 		rmem->page_size = HW_RXBD_RING_SIZE;
4503 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4504 		rmem->dma_arr = rxr->rx_desc_mapping;
4505 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4506 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4507 
4508 		ring = &rxr->rx_agg_ring_struct;
4509 		rmem = &ring->ring_mem;
4510 		rmem->nr_pages = bp->rx_agg_nr_pages;
4511 		rmem->page_size = HW_RXBD_RING_SIZE;
4512 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4513 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4514 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4515 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4516 
4517 skip_rx:
4518 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4519 			ring = &txr->tx_ring_struct;
4520 			rmem = &ring->ring_mem;
4521 			rmem->nr_pages = bp->tx_nr_pages;
4522 			rmem->page_size = HW_TXBD_RING_SIZE;
4523 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4524 			rmem->dma_arr = txr->tx_desc_mapping;
4525 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4526 			rmem->vmem = (void **)&txr->tx_buf_ring;
4527 		}
4528 	}
4529 }
4530 
4531 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4532 {
4533 	int i;
4534 	u32 prod;
4535 	struct rx_bd **rx_buf_ring;
4536 
4537 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4538 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4539 		int j;
4540 		struct rx_bd *rxbd;
4541 
4542 		rxbd = rx_buf_ring[i];
4543 		if (!rxbd)
4544 			continue;
4545 
4546 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4547 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4548 			rxbd->rx_bd_opaque = prod;
4549 		}
4550 	}
4551 }
4552 
4553 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4554 				       struct bnxt_rx_ring_info *rxr,
4555 				       int ring_nr)
4556 {
4557 	u32 prod;
4558 	int i;
4559 
4560 	prod = rxr->rx_prod;
4561 	for (i = 0; i < bp->rx_ring_size; i++) {
4562 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4563 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4564 				    ring_nr, i, bp->rx_ring_size);
4565 			break;
4566 		}
4567 		prod = NEXT_RX(prod);
4568 	}
4569 	rxr->rx_prod = prod;
4570 }
4571 
4572 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp,
4573 					  struct bnxt_rx_ring_info *rxr,
4574 					  int ring_nr)
4575 {
4576 	int fill_level, i;
4577 	u32 prod;
4578 
4579 	fill_level = bnxt_rx_agg_ring_fill_level(bp, rxr);
4580 
4581 	prod = rxr->rx_agg_prod;
4582 	for (i = 0; i < fill_level; i++) {
4583 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) {
4584 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4585 				    ring_nr, i, bp->rx_agg_ring_size);
4586 			break;
4587 		}
4588 		prod = NEXT_RX_AGG(prod);
4589 	}
4590 	rxr->rx_agg_prod = prod;
4591 }
4592 
4593 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4594 					struct bnxt_rx_ring_info *rxr)
4595 {
4596 	dma_addr_t mapping;
4597 	u8 *data;
4598 	int i;
4599 
4600 	for (i = 0; i < bp->max_tpa_roundup_size; i++) {
4601 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4602 					    GFP_KERNEL);
4603 		if (!data)
4604 			return -ENOMEM;
4605 
4606 		rxr->rx_tpa[i].data = data;
4607 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4608 		rxr->rx_tpa[i].mapping = mapping;
4609 	}
4610 
4611 	return 0;
4612 }
4613 
4614 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4615 {
4616 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4617 	int rc;
4618 
4619 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4620 
4621 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4622 		return 0;
4623 
4624 	bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr);
4625 
4626 	if (rxr->rx_tpa) {
4627 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4628 		if (rc)
4629 			return rc;
4630 	}
4631 	return 0;
4632 }
4633 
4634 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4635 				       struct bnxt_rx_ring_info *rxr)
4636 {
4637 	struct bnxt_ring_struct *ring;
4638 	u32 type;
4639 
4640 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4641 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4642 
4643 	if (NET_IP_ALIGN == 2)
4644 		type |= RX_BD_FLAGS_SOP;
4645 
4646 	ring = &rxr->rx_ring_struct;
4647 	bnxt_init_rxbd_pages(ring, type);
4648 	ring->fw_ring_id = INVALID_HW_RING_ID;
4649 }
4650 
4651 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4652 					   struct bnxt_rx_ring_info *rxr)
4653 {
4654 	struct bnxt_ring_struct *ring;
4655 	u32 type;
4656 
4657 	ring = &rxr->rx_agg_ring_struct;
4658 	ring->fw_ring_id = INVALID_HW_RING_ID;
4659 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4660 		type = ((u32)rxr->rx_page_size << RX_BD_LEN_SHIFT) |
4661 			RX_BD_TYPE_RX_AGG_BD;
4662 
4663 		/* Disable EOP if TPA is enabled to prevent overlapping zero
4664 		 * padding with the next segment's data.  On P7_PLUS, EOP will
4665 		 * automatically disable Relaxed Ordering (RO) to prevent
4666 		 * potential data corruption (and may degrade performance).  On
4667 		 * older chips, RO will not be automatically disabled and may
4668 		 * cause corruption.
4669 		 */
4670 		if (!(bp->flags & BNXT_FLAG_TPA))
4671 			type |= RX_BD_FLAGS_AGG_EOP;
4672 
4673 		bnxt_init_rxbd_pages(ring, type);
4674 	}
4675 }
4676 
4677 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4678 {
4679 	struct bnxt_rx_ring_info *rxr;
4680 
4681 	rxr = &bp->rx_ring[ring_nr];
4682 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4683 
4684 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4685 			     &rxr->bnapi->napi);
4686 
4687 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4688 		bpf_prog_add(bp->xdp_prog, 1);
4689 		rxr->xdp_prog = bp->xdp_prog;
4690 	}
4691 
4692 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4693 
4694 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4695 }
4696 
4697 static void bnxt_init_cp_rings(struct bnxt *bp)
4698 {
4699 	int i, j;
4700 
4701 	for (i = 0; i < bp->cp_nr_rings; i++) {
4702 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4703 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4704 
4705 		ring->fw_ring_id = INVALID_HW_RING_ID;
4706 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4707 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4708 		if (!cpr->cp_ring_arr)
4709 			continue;
4710 		for (j = 0; j < cpr->cp_ring_count; j++) {
4711 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4712 
4713 			ring = &cpr2->cp_ring_struct;
4714 			ring->fw_ring_id = INVALID_HW_RING_ID;
4715 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4716 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4717 		}
4718 	}
4719 }
4720 
4721 static int bnxt_init_rx_rings(struct bnxt *bp)
4722 {
4723 	int i, rc = 0;
4724 
4725 	if (BNXT_RX_PAGE_MODE(bp)) {
4726 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4727 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4728 	} else {
4729 		bp->rx_offset = BNXT_RX_OFFSET;
4730 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4731 	}
4732 
4733 	for (i = 0; i < bp->rx_nr_rings; i++) {
4734 		rc = bnxt_init_one_rx_ring(bp, i);
4735 		if (rc)
4736 			break;
4737 	}
4738 
4739 	return rc;
4740 }
4741 
4742 static int bnxt_init_tx_rings(struct bnxt *bp)
4743 {
4744 	netdev_features_t features;
4745 	u16 i;
4746 
4747 	features = bp->dev->features;
4748 
4749 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4750 				   bnxt_min_tx_desc_cnt(bp, features));
4751 
4752 	for (i = 0; i < bp->tx_nr_rings; i++) {
4753 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4754 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4755 
4756 		ring->fw_ring_id = INVALID_HW_RING_ID;
4757 
4758 		if (i >= bp->tx_nr_rings_xdp)
4759 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4760 					     NETDEV_QUEUE_TYPE_TX,
4761 					     &txr->bnapi->napi);
4762 	}
4763 
4764 	return 0;
4765 }
4766 
4767 static void bnxt_free_ring_grps(struct bnxt *bp)
4768 {
4769 	kfree(bp->grp_info);
4770 	bp->grp_info = NULL;
4771 }
4772 
4773 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4774 {
4775 	int i;
4776 
4777 	if (irq_re_init) {
4778 		bp->grp_info = kzalloc_objs(struct bnxt_ring_grp_info,
4779 					    bp->cp_nr_rings);
4780 		if (!bp->grp_info)
4781 			return -ENOMEM;
4782 	}
4783 	for (i = 0; i < bp->cp_nr_rings; i++) {
4784 		if (irq_re_init)
4785 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4786 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4787 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4788 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4789 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4790 	}
4791 	return 0;
4792 }
4793 
4794 static void bnxt_free_vnics(struct bnxt *bp)
4795 {
4796 	kfree(bp->vnic_info);
4797 	bp->vnic_info = NULL;
4798 	bp->nr_vnics = 0;
4799 }
4800 
4801 static int bnxt_alloc_vnics(struct bnxt *bp)
4802 {
4803 	int num_vnics = 1;
4804 
4805 #ifdef CONFIG_RFS_ACCEL
4806 	if (bp->flags & BNXT_FLAG_RFS) {
4807 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4808 			num_vnics++;
4809 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4810 			num_vnics += bp->rx_nr_rings;
4811 	}
4812 #endif
4813 
4814 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4815 		num_vnics++;
4816 
4817 	bp->vnic_info = kzalloc_objs(struct bnxt_vnic_info, num_vnics);
4818 	if (!bp->vnic_info)
4819 		return -ENOMEM;
4820 
4821 	bp->nr_vnics = num_vnics;
4822 	return 0;
4823 }
4824 
4825 static void bnxt_init_vnics(struct bnxt *bp)
4826 {
4827 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4828 	int i;
4829 
4830 	for (i = 0; i < bp->nr_vnics; i++) {
4831 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4832 		int j;
4833 
4834 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4835 		vnic->vnic_id = i;
4836 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4837 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4838 
4839 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4840 
4841 		if (bp->vnic_info[i].rss_hash_key) {
4842 			if (i == BNXT_VNIC_DEFAULT) {
4843 				u8 *key = (void *)vnic->rss_hash_key;
4844 				int k;
4845 
4846 				if (!bp->rss_hash_key_valid &&
4847 				    !bp->rss_hash_key_updated) {
4848 					get_random_bytes(bp->rss_hash_key,
4849 							 HW_HASH_KEY_SIZE);
4850 					bp->rss_hash_key_updated = true;
4851 				}
4852 
4853 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4854 				       HW_HASH_KEY_SIZE);
4855 
4856 				if (!bp->rss_hash_key_updated)
4857 					continue;
4858 
4859 				bp->rss_hash_key_updated = false;
4860 				bp->rss_hash_key_valid = true;
4861 
4862 				bp->toeplitz_prefix = 0;
4863 				for (k = 0; k < 8; k++) {
4864 					bp->toeplitz_prefix <<= 8;
4865 					bp->toeplitz_prefix |= key[k];
4866 				}
4867 			} else {
4868 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4869 				       HW_HASH_KEY_SIZE);
4870 			}
4871 		}
4872 	}
4873 }
4874 
4875 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4876 {
4877 	int pages;
4878 
4879 	pages = ring_size / desc_per_pg;
4880 
4881 	if (!pages)
4882 		return 1;
4883 
4884 	pages++;
4885 
4886 	while (pages & (pages - 1))
4887 		pages++;
4888 
4889 	return pages;
4890 }
4891 
4892 void bnxt_set_tpa_flags(struct bnxt *bp)
4893 {
4894 	bp->flags &= ~BNXT_FLAG_TPA;
4895 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4896 		return;
4897 	if (bp->dev->features & NETIF_F_LRO)
4898 		bp->flags |= BNXT_FLAG_LRO;
4899 	else if (bp->dev->features & NETIF_F_GRO_HW)
4900 		bp->flags |= BNXT_FLAG_GRO;
4901 }
4902 
4903 static void bnxt_init_ring_params(struct bnxt *bp)
4904 {
4905 	unsigned int rx_size;
4906 
4907 	bp->rx_copybreak = 0; /* rx-copybreak disabled by default */
4908 	/* Try to fit 4 chunks into a 4k page */
4909 	rx_size = SZ_1K -
4910 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4911 	bp->dev->cfg->hds_thresh = max(BNXT_MIN_RX_HDR_BUF, rx_size);
4912 }
4913 
4914 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4915  * be set on entry.
4916  */
4917 void bnxt_set_ring_params(struct bnxt *bp)
4918 {
4919 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4920 	u32 agg_factor = 0, agg_ring_size = 0;
4921 
4922 	/* 8 for CRC and VLAN */
4923 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4924 
4925 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4926 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4927 
4928 	ring_size = bp->rx_ring_size;
4929 	bp->rx_agg_ring_size = 0;
4930 	bp->rx_agg_nr_pages = 0;
4931 
4932 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4933 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4934 
4935 	bp->flags &= ~BNXT_FLAG_JUMBO;
4936 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4937 		u32 jumbo_factor;
4938 
4939 		bp->flags |= BNXT_FLAG_JUMBO;
4940 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4941 		if (jumbo_factor > agg_factor)
4942 			agg_factor = jumbo_factor;
4943 	}
4944 	if (agg_factor) {
4945 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4946 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4947 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4948 				    bp->rx_ring_size, ring_size);
4949 			bp->rx_ring_size = ring_size;
4950 		}
4951 		agg_ring_size = ring_size * agg_factor;
4952 
4953 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4954 							RX_DESC_CNT);
4955 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4956 			u32 tmp = agg_ring_size;
4957 
4958 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4959 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4960 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4961 				    tmp, agg_ring_size);
4962 		}
4963 		bp->rx_agg_ring_size = agg_ring_size;
4964 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4965 
4966 		if (BNXT_RX_PAGE_MODE(bp)) {
4967 			rx_space = PAGE_SIZE;
4968 			rx_size = PAGE_SIZE -
4969 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4970 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4971 		} else {
4972 			rx_size = max3(BNXT_MIN_RX_HDR_BUF,
4973 				       bp->rx_copybreak,
4974 				       bp->dev->cfg_pending->hds_thresh);
4975 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4976 			rx_space = rx_size + NET_SKB_PAD +
4977 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4978 		}
4979 	}
4980 
4981 	bp->rx_buf_use_size = rx_size;
4982 	bp->rx_buf_size = rx_space;
4983 
4984 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4985 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4986 
4987 	ring_size = bp->tx_ring_size;
4988 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4989 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4990 
4991 	max_rx_cmpl = bp->rx_ring_size;
4992 	/* MAX TPA needs to be added because TPA_START completions are
4993 	 * immediately recycled, so the TPA completions are not bound by
4994 	 * the RX ring size.
4995 	 */
4996 	if (bp->flags & BNXT_FLAG_TPA)
4997 		max_rx_cmpl += bp->max_tpa;
4998 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4999 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
5000 	bp->cp_ring_size = ring_size;
5001 
5002 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
5003 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
5004 		bp->cp_nr_pages = MAX_CP_PAGES;
5005 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
5006 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
5007 			    ring_size, bp->cp_ring_size);
5008 	}
5009 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
5010 	bp->cp_ring_mask = bp->cp_bit - 1;
5011 }
5012 
5013 /* Changing allocation mode of RX rings.
5014  * TODO: Update when extending xdp_rxq_info to support allocation modes.
5015  */
5016 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
5017 {
5018 	struct net_device *dev = bp->dev;
5019 
5020 	if (page_mode) {
5021 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
5022 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
5023 
5024 		if (bp->xdp_prog->aux->xdp_has_frags)
5025 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
5026 		else
5027 			dev->max_mtu =
5028 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
5029 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
5030 			bp->flags |= BNXT_FLAG_JUMBO;
5031 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
5032 		} else {
5033 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
5034 			bp->rx_skb_func = bnxt_rx_page_skb;
5035 		}
5036 		bp->rx_dir = DMA_BIDIRECTIONAL;
5037 	} else {
5038 		dev->max_mtu = bp->max_mtu;
5039 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
5040 		bp->rx_dir = DMA_FROM_DEVICE;
5041 		bp->rx_skb_func = bnxt_rx_skb;
5042 	}
5043 }
5044 
5045 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
5046 {
5047 	__bnxt_set_rx_skb_mode(bp, page_mode);
5048 
5049 	if (!page_mode) {
5050 		int rx, tx;
5051 
5052 		bnxt_get_max_rings(bp, &rx, &tx, true);
5053 		if (rx > 1) {
5054 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
5055 			if (BNXT_SUPPORTS_TPA(bp))
5056 				bp->dev->hw_features |= NETIF_F_LRO;
5057 		}
5058 	}
5059 
5060 	/* Update LRO and GRO_HW availability */
5061 	netdev_update_features(bp->dev);
5062 }
5063 
5064 static void bnxt_free_vnic_attributes(struct bnxt *bp)
5065 {
5066 	int i;
5067 	struct bnxt_vnic_info *vnic;
5068 	struct pci_dev *pdev = bp->pdev;
5069 
5070 	if (!bp->vnic_info)
5071 		return;
5072 
5073 	for (i = 0; i < bp->nr_vnics; i++) {
5074 		vnic = &bp->vnic_info[i];
5075 
5076 		kfree(vnic->fw_grp_ids);
5077 		vnic->fw_grp_ids = NULL;
5078 
5079 		kfree(vnic->uc_list);
5080 		vnic->uc_list = NULL;
5081 
5082 		if (vnic->mc_list) {
5083 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
5084 					  vnic->mc_list, vnic->mc_list_mapping);
5085 			vnic->mc_list = NULL;
5086 		}
5087 
5088 		if (vnic->rss_table) {
5089 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
5090 					  vnic->rss_table,
5091 					  vnic->rss_table_dma_addr);
5092 			vnic->rss_table = NULL;
5093 		}
5094 
5095 		vnic->rss_hash_key = NULL;
5096 		vnic->flags = 0;
5097 	}
5098 }
5099 
5100 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
5101 {
5102 	int i, rc = 0, size;
5103 	struct bnxt_vnic_info *vnic;
5104 	struct pci_dev *pdev = bp->pdev;
5105 	int max_rings;
5106 
5107 	for (i = 0; i < bp->nr_vnics; i++) {
5108 		vnic = &bp->vnic_info[i];
5109 
5110 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
5111 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
5112 
5113 			if (mem_size > 0) {
5114 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
5115 				if (!vnic->uc_list) {
5116 					rc = -ENOMEM;
5117 					goto out;
5118 				}
5119 			}
5120 		}
5121 
5122 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
5123 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
5124 			vnic->mc_list =
5125 				dma_alloc_coherent(&pdev->dev,
5126 						   vnic->mc_list_size,
5127 						   &vnic->mc_list_mapping,
5128 						   GFP_KERNEL);
5129 			if (!vnic->mc_list) {
5130 				rc = -ENOMEM;
5131 				goto out;
5132 			}
5133 		}
5134 
5135 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5136 			goto vnic_skip_grps;
5137 
5138 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
5139 			max_rings = bp->rx_nr_rings;
5140 		else
5141 			max_rings = 1;
5142 
5143 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
5144 		if (!vnic->fw_grp_ids) {
5145 			rc = -ENOMEM;
5146 			goto out;
5147 		}
5148 vnic_skip_grps:
5149 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
5150 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
5151 			continue;
5152 
5153 		/* Allocate rss table and hash key */
5154 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
5155 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5156 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
5157 
5158 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
5159 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
5160 						     vnic->rss_table_size,
5161 						     &vnic->rss_table_dma_addr,
5162 						     GFP_KERNEL);
5163 		if (!vnic->rss_table) {
5164 			rc = -ENOMEM;
5165 			goto out;
5166 		}
5167 
5168 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
5169 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
5170 	}
5171 	return 0;
5172 
5173 out:
5174 	return rc;
5175 }
5176 
5177 static void bnxt_free_hwrm_resources(struct bnxt *bp)
5178 {
5179 	struct bnxt_hwrm_wait_token *token;
5180 
5181 	dma_pool_destroy(bp->hwrm_dma_pool);
5182 	bp->hwrm_dma_pool = NULL;
5183 
5184 	rcu_read_lock();
5185 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
5186 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
5187 	rcu_read_unlock();
5188 }
5189 
5190 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
5191 {
5192 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
5193 					    BNXT_HWRM_DMA_SIZE,
5194 					    BNXT_HWRM_DMA_ALIGN, 0);
5195 	if (!bp->hwrm_dma_pool)
5196 		return -ENOMEM;
5197 
5198 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
5199 
5200 	return 0;
5201 }
5202 
5203 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
5204 {
5205 	kfree(stats->hw_masks);
5206 	stats->hw_masks = NULL;
5207 	kfree(stats->sw_stats);
5208 	stats->sw_stats = NULL;
5209 	if (stats->hw_stats) {
5210 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
5211 				  stats->hw_stats_map);
5212 		stats->hw_stats = NULL;
5213 	}
5214 }
5215 
5216 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
5217 				bool alloc_masks)
5218 {
5219 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
5220 					     &stats->hw_stats_map, GFP_KERNEL);
5221 	if (!stats->hw_stats)
5222 		return -ENOMEM;
5223 
5224 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
5225 	if (!stats->sw_stats)
5226 		goto stats_mem_err;
5227 
5228 	if (alloc_masks) {
5229 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
5230 		if (!stats->hw_masks)
5231 			goto stats_mem_err;
5232 	}
5233 	return 0;
5234 
5235 stats_mem_err:
5236 	bnxt_free_stats_mem(bp, stats);
5237 	return -ENOMEM;
5238 }
5239 
5240 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
5241 {
5242 	int i;
5243 
5244 	for (i = 0; i < count; i++)
5245 		mask_arr[i] = mask;
5246 }
5247 
5248 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
5249 {
5250 	int i;
5251 
5252 	for (i = 0; i < count; i++)
5253 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
5254 }
5255 
5256 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
5257 				    struct bnxt_stats_mem *stats)
5258 {
5259 	struct hwrm_func_qstats_ext_output *resp;
5260 	struct hwrm_func_qstats_ext_input *req;
5261 	__le64 *hw_masks;
5262 	int rc;
5263 
5264 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
5265 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5266 		return -EOPNOTSUPP;
5267 
5268 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
5269 	if (rc)
5270 		return rc;
5271 
5272 	req->fid = cpu_to_le16(0xffff);
5273 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5274 
5275 	resp = hwrm_req_hold(bp, req);
5276 	rc = hwrm_req_send(bp, req);
5277 	if (!rc) {
5278 		hw_masks = &resp->rx_ucast_pkts;
5279 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
5280 	}
5281 	hwrm_req_drop(bp, req);
5282 	return rc;
5283 }
5284 
5285 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5286 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5287 
5288 static void bnxt_init_stats(struct bnxt *bp)
5289 {
5290 	struct bnxt_napi *bnapi = bp->bnapi[0];
5291 	struct bnxt_cp_ring_info *cpr;
5292 	struct bnxt_stats_mem *stats;
5293 	__le64 *rx_stats, *tx_stats;
5294 	int rc, rx_count, tx_count;
5295 	u64 *rx_masks, *tx_masks;
5296 	u64 mask;
5297 	u8 flags;
5298 
5299 	cpr = &bnapi->cp_ring;
5300 	stats = &cpr->stats;
5301 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5302 	if (rc) {
5303 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5304 			mask = (1ULL << 48) - 1;
5305 		else
5306 			mask = -1ULL;
5307 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5308 	}
5309 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5310 		stats = &bp->port_stats;
5311 		rx_stats = stats->hw_stats;
5312 		rx_masks = stats->hw_masks;
5313 		rx_count = sizeof(struct rx_port_stats) / 8;
5314 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5315 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5316 		tx_count = sizeof(struct tx_port_stats) / 8;
5317 
5318 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5319 		rc = bnxt_hwrm_port_qstats(bp, flags);
5320 		if (rc) {
5321 			mask = (1ULL << 40) - 1;
5322 
5323 			bnxt_fill_masks(rx_masks, mask, rx_count);
5324 			bnxt_fill_masks(tx_masks, mask, tx_count);
5325 		} else {
5326 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5327 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5328 			bnxt_hwrm_port_qstats(bp, 0);
5329 		}
5330 	}
5331 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5332 		stats = &bp->rx_port_stats_ext;
5333 		rx_stats = stats->hw_stats;
5334 		rx_masks = stats->hw_masks;
5335 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5336 		stats = &bp->tx_port_stats_ext;
5337 		tx_stats = stats->hw_stats;
5338 		tx_masks = stats->hw_masks;
5339 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5340 
5341 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5342 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5343 		if (rc) {
5344 			mask = (1ULL << 40) - 1;
5345 
5346 			bnxt_fill_masks(rx_masks, mask, rx_count);
5347 			if (tx_stats)
5348 				bnxt_fill_masks(tx_masks, mask, tx_count);
5349 		} else {
5350 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5351 			if (tx_stats)
5352 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5353 						   tx_count);
5354 			bnxt_hwrm_port_qstats_ext(bp, 0);
5355 		}
5356 	}
5357 }
5358 
5359 static void bnxt_free_port_stats(struct bnxt *bp)
5360 {
5361 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5362 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5363 
5364 	bnxt_free_stats_mem(bp, &bp->port_stats);
5365 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5366 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5367 }
5368 
5369 static void bnxt_free_ring_stats(struct bnxt *bp)
5370 {
5371 	int i;
5372 
5373 	if (!bp->bnapi)
5374 		return;
5375 
5376 	for (i = 0; i < bp->cp_nr_rings; i++) {
5377 		struct bnxt_napi *bnapi = bp->bnapi[i];
5378 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5379 
5380 		bnxt_free_stats_mem(bp, &cpr->stats);
5381 
5382 		kfree(cpr->sw_stats);
5383 		cpr->sw_stats = NULL;
5384 	}
5385 }
5386 
5387 static int bnxt_alloc_stats(struct bnxt *bp)
5388 {
5389 	u32 size, i;
5390 	int rc;
5391 
5392 	size = bp->hw_ring_stats_size;
5393 
5394 	for (i = 0; i < bp->cp_nr_rings; i++) {
5395 		struct bnxt_napi *bnapi = bp->bnapi[i];
5396 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5397 
5398 		cpr->sw_stats = kzalloc_obj(*cpr->sw_stats);
5399 		if (!cpr->sw_stats)
5400 			return -ENOMEM;
5401 
5402 		cpr->stats.len = size;
5403 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5404 		if (rc)
5405 			return rc;
5406 
5407 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5408 	}
5409 
5410 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5411 		return 0;
5412 
5413 	if (bp->port_stats.hw_stats)
5414 		goto alloc_ext_stats;
5415 
5416 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5417 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5418 	if (rc)
5419 		return rc;
5420 
5421 	bp->flags |= BNXT_FLAG_PORT_STATS;
5422 
5423 alloc_ext_stats:
5424 	/* Display extended statistics only if FW supports it */
5425 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5426 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5427 			return 0;
5428 
5429 	if (bp->rx_port_stats_ext.hw_stats)
5430 		goto alloc_tx_ext_stats;
5431 
5432 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5433 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5434 	/* Extended stats are optional */
5435 	if (rc)
5436 		return 0;
5437 
5438 alloc_tx_ext_stats:
5439 	if (bp->tx_port_stats_ext.hw_stats)
5440 		return 0;
5441 
5442 	if (bp->hwrm_spec_code >= 0x10902 ||
5443 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5444 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5445 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5446 		/* Extended stats are optional */
5447 		if (rc)
5448 			return 0;
5449 	}
5450 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5451 	return 0;
5452 }
5453 
5454 static void bnxt_clear_ring_indices(struct bnxt *bp)
5455 {
5456 	int i, j;
5457 
5458 	if (!bp->bnapi)
5459 		return;
5460 
5461 	for (i = 0; i < bp->cp_nr_rings; i++) {
5462 		struct bnxt_napi *bnapi = bp->bnapi[i];
5463 		struct bnxt_cp_ring_info *cpr;
5464 		struct bnxt_rx_ring_info *rxr;
5465 		struct bnxt_tx_ring_info *txr;
5466 
5467 		if (!bnapi)
5468 			continue;
5469 
5470 		cpr = &bnapi->cp_ring;
5471 		cpr->cp_raw_cons = 0;
5472 
5473 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5474 			txr->tx_prod = 0;
5475 			txr->tx_cons = 0;
5476 			txr->tx_hw_cons = 0;
5477 			txr->kick_pending = 0;
5478 			txr->kick_txbd0 = NULL;
5479 		}
5480 
5481 		rxr = bnapi->rx_ring;
5482 		if (rxr) {
5483 			rxr->rx_prod = 0;
5484 			rxr->rx_agg_prod = 0;
5485 			rxr->rx_sw_agg_prod = 0;
5486 			rxr->rx_next_cons = 0;
5487 		}
5488 		bnapi->events = 0;
5489 	}
5490 }
5491 
5492 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5493 {
5494 	u8 type = fltr->type, flags = fltr->flags;
5495 
5496 	INIT_LIST_HEAD(&fltr->list);
5497 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5498 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5499 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5500 }
5501 
5502 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5503 {
5504 	if (!list_empty(&fltr->list))
5505 		list_del_init(&fltr->list);
5506 }
5507 
5508 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5509 {
5510 	struct bnxt_filter_base *usr_fltr, *tmp;
5511 
5512 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5513 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5514 			continue;
5515 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5516 	}
5517 }
5518 
5519 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5520 {
5521 	hlist_del(&fltr->hash);
5522 	bnxt_del_one_usr_fltr(bp, fltr);
5523 	if (fltr->flags) {
5524 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5525 		bp->ntp_fltr_count--;
5526 	}
5527 	kfree(fltr);
5528 }
5529 
5530 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5531 {
5532 	int i;
5533 
5534 	netdev_assert_locked_or_invisible(bp->dev);
5535 
5536 	/* Under netdev instance lock and all our NAPIs have been disabled.
5537 	 * It's safe to delete the hash table.
5538 	 */
5539 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5540 		struct hlist_head *head;
5541 		struct hlist_node *tmp;
5542 		struct bnxt_ntuple_filter *fltr;
5543 
5544 		head = &bp->ntp_fltr_hash_tbl[i];
5545 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5546 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5547 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5548 				     !list_empty(&fltr->base.list)))
5549 				continue;
5550 			bnxt_del_fltr(bp, &fltr->base);
5551 		}
5552 	}
5553 	if (!all)
5554 		return;
5555 
5556 	bitmap_free(bp->ntp_fltr_bmap);
5557 	bp->ntp_fltr_bmap = NULL;
5558 	bp->ntp_fltr_count = 0;
5559 }
5560 
5561 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5562 {
5563 	int i, rc = 0;
5564 
5565 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5566 		return 0;
5567 
5568 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5569 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5570 
5571 	bp->ntp_fltr_count = 0;
5572 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5573 
5574 	if (!bp->ntp_fltr_bmap)
5575 		rc = -ENOMEM;
5576 
5577 	return rc;
5578 }
5579 
5580 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5581 {
5582 	int i;
5583 
5584 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5585 		struct hlist_head *head;
5586 		struct hlist_node *tmp;
5587 		struct bnxt_l2_filter *fltr;
5588 
5589 		head = &bp->l2_fltr_hash_tbl[i];
5590 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5591 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5592 				     !list_empty(&fltr->base.list)))
5593 				continue;
5594 			bnxt_del_fltr(bp, &fltr->base);
5595 		}
5596 	}
5597 }
5598 
5599 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5600 {
5601 	int i;
5602 
5603 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5604 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5605 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5606 }
5607 
5608 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5609 {
5610 	bnxt_free_vnic_attributes(bp);
5611 	bnxt_free_tx_rings(bp);
5612 	bnxt_free_rx_rings(bp);
5613 	bnxt_free_cp_rings(bp);
5614 	bnxt_free_all_cp_arrays(bp);
5615 	bnxt_free_ntp_fltrs(bp, false);
5616 	bnxt_free_l2_filters(bp, false);
5617 	if (irq_re_init) {
5618 		bnxt_free_ring_stats(bp);
5619 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5620 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5621 			bnxt_free_port_stats(bp);
5622 		bnxt_free_ring_grps(bp);
5623 		bnxt_free_vnics(bp);
5624 		kfree(bp->tx_ring_map);
5625 		bp->tx_ring_map = NULL;
5626 		kfree(bp->tx_ring);
5627 		bp->tx_ring = NULL;
5628 		kfree(bp->rx_ring);
5629 		bp->rx_ring = NULL;
5630 		kfree(bp->bnapi);
5631 		bp->bnapi = NULL;
5632 	} else {
5633 		bnxt_clear_ring_indices(bp);
5634 	}
5635 }
5636 
5637 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5638 {
5639 	int i, j, rc, size, arr_size;
5640 	void *bnapi;
5641 
5642 	if (irq_re_init) {
5643 		/* Allocate bnapi mem pointer array and mem block for
5644 		 * all queues
5645 		 */
5646 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5647 				bp->cp_nr_rings);
5648 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5649 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5650 		if (!bnapi)
5651 			return -ENOMEM;
5652 
5653 		bp->bnapi = bnapi;
5654 		bnapi += arr_size;
5655 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5656 			bp->bnapi[i] = bnapi;
5657 			bp->bnapi[i]->index = i;
5658 			bp->bnapi[i]->bp = bp;
5659 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5660 				struct bnxt_cp_ring_info *cpr =
5661 					&bp->bnapi[i]->cp_ring;
5662 
5663 				cpr->cp_ring_struct.ring_mem.flags =
5664 					BNXT_RMEM_RING_PTE_FLAG;
5665 			}
5666 		}
5667 
5668 		bp->rx_ring = kzalloc_objs(struct bnxt_rx_ring_info,
5669 					   bp->rx_nr_rings);
5670 		if (!bp->rx_ring)
5671 			return -ENOMEM;
5672 
5673 		for (i = 0; i < bp->rx_nr_rings; i++) {
5674 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5675 
5676 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5677 				rxr->rx_ring_struct.ring_mem.flags =
5678 					BNXT_RMEM_RING_PTE_FLAG;
5679 				rxr->rx_agg_ring_struct.ring_mem.flags =
5680 					BNXT_RMEM_RING_PTE_FLAG;
5681 			} else {
5682 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5683 			}
5684 			rxr->bnapi = bp->bnapi[i];
5685 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5686 		}
5687 
5688 		bp->tx_ring = kzalloc_objs(struct bnxt_tx_ring_info,
5689 					   bp->tx_nr_rings);
5690 		if (!bp->tx_ring)
5691 			return -ENOMEM;
5692 
5693 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5694 					  GFP_KERNEL);
5695 
5696 		if (!bp->tx_ring_map)
5697 			return -ENOMEM;
5698 
5699 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5700 			j = 0;
5701 		else
5702 			j = bp->rx_nr_rings;
5703 
5704 		for (i = 0; i < bp->tx_nr_rings; i++) {
5705 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5706 			struct bnxt_napi *bnapi2;
5707 
5708 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5709 				txr->tx_ring_struct.ring_mem.flags =
5710 					BNXT_RMEM_RING_PTE_FLAG;
5711 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5712 			if (i >= bp->tx_nr_rings_xdp) {
5713 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5714 
5715 				bnapi2 = bp->bnapi[k];
5716 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5717 				txr->tx_napi_idx =
5718 					BNXT_RING_TO_TC(bp, txr->txq_index);
5719 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5720 				bnapi2->tx_int = bnxt_tx_int;
5721 			} else {
5722 				bnapi2 = bp->bnapi[j];
5723 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5724 				bnapi2->tx_ring[0] = txr;
5725 				bnapi2->tx_int = bnxt_tx_int_xdp;
5726 				j++;
5727 			}
5728 			txr->bnapi = bnapi2;
5729 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5730 				txr->tx_cpr = &bnapi2->cp_ring;
5731 		}
5732 
5733 		rc = bnxt_alloc_stats(bp);
5734 		if (rc)
5735 			goto alloc_mem_err;
5736 		bnxt_init_stats(bp);
5737 
5738 		rc = bnxt_alloc_ntp_fltrs(bp);
5739 		if (rc)
5740 			goto alloc_mem_err;
5741 
5742 		rc = bnxt_alloc_vnics(bp);
5743 		if (rc)
5744 			goto alloc_mem_err;
5745 	}
5746 
5747 	rc = bnxt_alloc_all_cp_arrays(bp);
5748 	if (rc)
5749 		goto alloc_mem_err;
5750 
5751 	bnxt_init_ring_struct(bp);
5752 
5753 	rc = bnxt_alloc_rx_rings(bp);
5754 	if (rc)
5755 		goto alloc_mem_err;
5756 
5757 	rc = bnxt_alloc_tx_rings(bp);
5758 	if (rc)
5759 		goto alloc_mem_err;
5760 
5761 	rc = bnxt_alloc_cp_rings(bp);
5762 	if (rc)
5763 		goto alloc_mem_err;
5764 
5765 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5766 						  BNXT_VNIC_MCAST_FLAG |
5767 						  BNXT_VNIC_UCAST_FLAG;
5768 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5769 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5770 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5771 
5772 	rc = bnxt_alloc_vnic_attributes(bp);
5773 	if (rc)
5774 		goto alloc_mem_err;
5775 	return 0;
5776 
5777 alloc_mem_err:
5778 	bnxt_free_mem(bp, true);
5779 	return rc;
5780 }
5781 
5782 static void bnxt_disable_int(struct bnxt *bp)
5783 {
5784 	int i;
5785 
5786 	if (!bp->bnapi)
5787 		return;
5788 
5789 	for (i = 0; i < bp->cp_nr_rings; i++) {
5790 		struct bnxt_napi *bnapi = bp->bnapi[i];
5791 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5792 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5793 
5794 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5795 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5796 	}
5797 }
5798 
5799 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5800 {
5801 	struct bnxt_napi *bnapi = bp->bnapi[n];
5802 	struct bnxt_cp_ring_info *cpr;
5803 
5804 	cpr = &bnapi->cp_ring;
5805 	return cpr->cp_ring_struct.map_idx;
5806 }
5807 
5808 static void bnxt_disable_int_sync(struct bnxt *bp)
5809 {
5810 	int i;
5811 
5812 	if (!bp->irq_tbl || !bp->bnapi)
5813 		return;
5814 
5815 	atomic_inc(&bp->intr_sem);
5816 
5817 	bnxt_disable_int(bp);
5818 	for (i = 0; i < bp->cp_nr_rings; i++) {
5819 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5820 
5821 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5822 	}
5823 }
5824 
5825 static void bnxt_enable_int(struct bnxt *bp)
5826 {
5827 	int i;
5828 
5829 	atomic_set(&bp->intr_sem, 0);
5830 	for (i = 0; i < bp->cp_nr_rings; i++) {
5831 		struct bnxt_napi *bnapi = bp->bnapi[i];
5832 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5833 
5834 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5835 	}
5836 }
5837 
5838 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5839 			    bool async_only)
5840 {
5841 	DECLARE_BITMAP(async_events_bmap, 256);
5842 	u32 *events = (u32 *)async_events_bmap;
5843 	struct hwrm_func_drv_rgtr_output *resp;
5844 	struct hwrm_func_drv_rgtr_input *req;
5845 	u32 flags;
5846 	int rc, i;
5847 
5848 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5849 	if (rc)
5850 		return rc;
5851 
5852 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5853 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5854 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5855 
5856 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5857 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5858 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5859 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5860 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5861 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5862 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5863 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5864 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5865 	req->flags = cpu_to_le32(flags);
5866 	req->ver_maj_8b = DRV_VER_MAJ;
5867 	req->ver_min_8b = DRV_VER_MIN;
5868 	req->ver_upd_8b = DRV_VER_UPD;
5869 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5870 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5871 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5872 
5873 	if (BNXT_PF(bp)) {
5874 		u32 data[8];
5875 		int i;
5876 
5877 		memset(data, 0, sizeof(data));
5878 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5879 			u16 cmd = bnxt_vf_req_snif[i];
5880 			unsigned int bit, idx;
5881 
5882 			if ((bp->fw_cap & BNXT_FW_CAP_LINK_ADMIN) &&
5883 			    cmd == HWRM_PORT_PHY_QCFG)
5884 				continue;
5885 
5886 			idx = cmd / 32;
5887 			bit = cmd % 32;
5888 			data[idx] |= 1 << bit;
5889 		}
5890 
5891 		for (i = 0; i < 8; i++)
5892 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5893 
5894 		req->enables |=
5895 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5896 	}
5897 
5898 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5899 		req->flags |= cpu_to_le32(
5900 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5901 
5902 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5903 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5904 		u16 event_id = bnxt_async_events_arr[i];
5905 
5906 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5907 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5908 			continue;
5909 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5910 		    !bp->ptp_cfg)
5911 			continue;
5912 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5913 	}
5914 	if (bmap && bmap_size) {
5915 		for (i = 0; i < bmap_size; i++) {
5916 			if (test_bit(i, bmap))
5917 				__set_bit(i, async_events_bmap);
5918 		}
5919 	}
5920 	for (i = 0; i < 8; i++)
5921 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5922 
5923 	if (async_only)
5924 		req->enables =
5925 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5926 
5927 	resp = hwrm_req_hold(bp, req);
5928 	rc = hwrm_req_send(bp, req);
5929 	if (!rc) {
5930 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5931 		if (resp->flags &
5932 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5933 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5934 	}
5935 	hwrm_req_drop(bp, req);
5936 	return rc;
5937 }
5938 
5939 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5940 {
5941 	struct hwrm_func_drv_unrgtr_input *req;
5942 	int rc;
5943 
5944 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5945 		return 0;
5946 
5947 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5948 	if (rc)
5949 		return rc;
5950 	return hwrm_req_send(bp, req);
5951 }
5952 
5953 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5954 
5955 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5956 {
5957 	struct hwrm_tunnel_dst_port_free_input *req;
5958 	int rc;
5959 
5960 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5961 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5962 		return 0;
5963 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5964 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5965 		return 0;
5966 
5967 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5968 	if (rc)
5969 		return rc;
5970 
5971 	req->tunnel_type = tunnel_type;
5972 
5973 	switch (tunnel_type) {
5974 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5975 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5976 		bp->vxlan_port = 0;
5977 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5978 		break;
5979 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5980 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5981 		bp->nge_port = 0;
5982 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5983 		break;
5984 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5985 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5986 		bp->vxlan_gpe_port = 0;
5987 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5988 		break;
5989 	default:
5990 		break;
5991 	}
5992 
5993 	rc = hwrm_req_send(bp, req);
5994 	if (rc)
5995 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5996 			   rc);
5997 	if (bp->flags & BNXT_FLAG_TPA)
5998 		bnxt_set_tpa(bp, true);
5999 	return rc;
6000 }
6001 
6002 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
6003 					   u8 tunnel_type)
6004 {
6005 	struct hwrm_tunnel_dst_port_alloc_output *resp;
6006 	struct hwrm_tunnel_dst_port_alloc_input *req;
6007 	int rc;
6008 
6009 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
6010 	if (rc)
6011 		return rc;
6012 
6013 	req->tunnel_type = tunnel_type;
6014 	req->tunnel_dst_port_val = port;
6015 
6016 	resp = hwrm_req_hold(bp, req);
6017 	rc = hwrm_req_send(bp, req);
6018 	if (rc) {
6019 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
6020 			   rc);
6021 		goto err_out;
6022 	}
6023 
6024 	switch (tunnel_type) {
6025 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
6026 		bp->vxlan_port = port;
6027 		bp->vxlan_fw_dst_port_id =
6028 			le16_to_cpu(resp->tunnel_dst_port_id);
6029 		break;
6030 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
6031 		bp->nge_port = port;
6032 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
6033 		break;
6034 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
6035 		bp->vxlan_gpe_port = port;
6036 		bp->vxlan_gpe_fw_dst_port_id =
6037 			le16_to_cpu(resp->tunnel_dst_port_id);
6038 		break;
6039 	default:
6040 		break;
6041 	}
6042 	if (bp->flags & BNXT_FLAG_TPA)
6043 		bnxt_set_tpa(bp, true);
6044 
6045 err_out:
6046 	hwrm_req_drop(bp, req);
6047 	return rc;
6048 }
6049 
6050 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
6051 {
6052 	struct hwrm_cfa_l2_set_rx_mask_input *req;
6053 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
6054 	int rc;
6055 
6056 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
6057 	if (rc)
6058 		return rc;
6059 
6060 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6061 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
6062 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
6063 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
6064 	}
6065 	req->mask = cpu_to_le32(vnic->rx_mask);
6066 	return hwrm_req_send_silent(bp, req);
6067 }
6068 
6069 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6070 {
6071 	if (!atomic_dec_and_test(&fltr->refcnt))
6072 		return;
6073 	spin_lock_bh(&bp->ntp_fltr_lock);
6074 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
6075 		spin_unlock_bh(&bp->ntp_fltr_lock);
6076 		return;
6077 	}
6078 	hlist_del_rcu(&fltr->base.hash);
6079 	bnxt_del_one_usr_fltr(bp, &fltr->base);
6080 	if (fltr->base.flags) {
6081 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
6082 		bp->ntp_fltr_count--;
6083 	}
6084 	spin_unlock_bh(&bp->ntp_fltr_lock);
6085 	kfree_rcu(fltr, base.rcu);
6086 }
6087 
6088 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
6089 						      struct bnxt_l2_key *key,
6090 						      u32 idx)
6091 {
6092 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
6093 	struct bnxt_l2_filter *fltr;
6094 
6095 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
6096 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
6097 
6098 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
6099 		    l2_key->vlan == key->vlan)
6100 			return fltr;
6101 	}
6102 	return NULL;
6103 }
6104 
6105 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
6106 						    struct bnxt_l2_key *key,
6107 						    u32 idx)
6108 {
6109 	struct bnxt_l2_filter *fltr = NULL;
6110 
6111 	rcu_read_lock();
6112 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6113 	if (fltr)
6114 		atomic_inc(&fltr->refcnt);
6115 	rcu_read_unlock();
6116 	return fltr;
6117 }
6118 
6119 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
6120 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
6121 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
6122 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
6123 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
6124 
6125 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
6126 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
6127 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
6128 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
6129 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
6130 
6131 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
6132 {
6133 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6134 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
6135 			return sizeof(fkeys->addrs.v4addrs) +
6136 			       sizeof(fkeys->ports);
6137 
6138 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
6139 			return sizeof(fkeys->addrs.v4addrs);
6140 	}
6141 
6142 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
6143 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
6144 			return sizeof(fkeys->addrs.v6addrs) +
6145 			       sizeof(fkeys->ports);
6146 
6147 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
6148 			return sizeof(fkeys->addrs.v6addrs);
6149 	}
6150 
6151 	return 0;
6152 }
6153 
6154 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
6155 			 const unsigned char *key)
6156 {
6157 	u64 prefix = bp->toeplitz_prefix, hash = 0;
6158 	struct bnxt_ipv4_tuple tuple4;
6159 	struct bnxt_ipv6_tuple tuple6;
6160 	int i, j, len = 0;
6161 	u8 *four_tuple;
6162 
6163 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
6164 	if (!len)
6165 		return 0;
6166 
6167 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6168 		tuple4.v4addrs = fkeys->addrs.v4addrs;
6169 		tuple4.ports = fkeys->ports;
6170 		four_tuple = (unsigned char *)&tuple4;
6171 	} else {
6172 		tuple6.v6addrs = fkeys->addrs.v6addrs;
6173 		tuple6.ports = fkeys->ports;
6174 		four_tuple = (unsigned char *)&tuple6;
6175 	}
6176 
6177 	for (i = 0, j = 8; i < len; i++, j++) {
6178 		u8 byte = four_tuple[i];
6179 		int bit;
6180 
6181 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
6182 			if (byte & 0x80)
6183 				hash ^= prefix;
6184 		}
6185 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
6186 	}
6187 
6188 	/* The valid part of the hash is in the upper 32 bits. */
6189 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
6190 }
6191 
6192 #ifdef CONFIG_RFS_ACCEL
6193 static struct bnxt_l2_filter *
6194 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
6195 {
6196 	struct bnxt_l2_filter *fltr;
6197 	u32 idx;
6198 
6199 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6200 	      BNXT_L2_FLTR_HASH_MASK;
6201 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6202 	return fltr;
6203 }
6204 #endif
6205 
6206 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
6207 			       struct bnxt_l2_key *key, u32 idx)
6208 {
6209 	struct hlist_head *head;
6210 
6211 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
6212 	fltr->l2_key.vlan = key->vlan;
6213 	fltr->base.type = BNXT_FLTR_TYPE_L2;
6214 	if (fltr->base.flags) {
6215 		int bit_id;
6216 
6217 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6218 						 bp->max_fltr, 0);
6219 		if (bit_id < 0)
6220 			return -ENOMEM;
6221 		fltr->base.sw_id = (u16)bit_id;
6222 		bp->ntp_fltr_count++;
6223 	}
6224 	head = &bp->l2_fltr_hash_tbl[idx];
6225 	hlist_add_head_rcu(&fltr->base.hash, head);
6226 	bnxt_insert_usr_fltr(bp, &fltr->base);
6227 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
6228 	atomic_set(&fltr->refcnt, 1);
6229 	return 0;
6230 }
6231 
6232 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
6233 						   struct bnxt_l2_key *key,
6234 						   gfp_t gfp)
6235 {
6236 	struct bnxt_l2_filter *fltr;
6237 	u32 idx;
6238 	int rc;
6239 
6240 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6241 	      BNXT_L2_FLTR_HASH_MASK;
6242 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6243 	if (fltr)
6244 		return fltr;
6245 
6246 	fltr = kzalloc_obj(*fltr, gfp);
6247 	if (!fltr)
6248 		return ERR_PTR(-ENOMEM);
6249 	spin_lock_bh(&bp->ntp_fltr_lock);
6250 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6251 	spin_unlock_bh(&bp->ntp_fltr_lock);
6252 	if (rc) {
6253 		bnxt_del_l2_filter(bp, fltr);
6254 		fltr = ERR_PTR(rc);
6255 	}
6256 	return fltr;
6257 }
6258 
6259 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
6260 						struct bnxt_l2_key *key,
6261 						u16 flags)
6262 {
6263 	struct bnxt_l2_filter *fltr;
6264 	u32 idx;
6265 	int rc;
6266 
6267 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6268 	      BNXT_L2_FLTR_HASH_MASK;
6269 	spin_lock_bh(&bp->ntp_fltr_lock);
6270 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6271 	if (fltr) {
6272 		fltr = ERR_PTR(-EEXIST);
6273 		goto l2_filter_exit;
6274 	}
6275 	fltr = kzalloc_obj(*fltr, GFP_ATOMIC);
6276 	if (!fltr) {
6277 		fltr = ERR_PTR(-ENOMEM);
6278 		goto l2_filter_exit;
6279 	}
6280 	fltr->base.flags = flags;
6281 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6282 	if (rc) {
6283 		spin_unlock_bh(&bp->ntp_fltr_lock);
6284 		bnxt_del_l2_filter(bp, fltr);
6285 		return ERR_PTR(rc);
6286 	}
6287 
6288 l2_filter_exit:
6289 	spin_unlock_bh(&bp->ntp_fltr_lock);
6290 	return fltr;
6291 }
6292 
6293 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6294 {
6295 #ifdef CONFIG_BNXT_SRIOV
6296 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6297 
6298 	return vf->fw_fid;
6299 #else
6300 	return INVALID_HW_RING_ID;
6301 #endif
6302 }
6303 
6304 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6305 {
6306 	struct hwrm_cfa_l2_filter_free_input *req;
6307 	u16 target_id = 0xffff;
6308 	int rc;
6309 
6310 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6311 		struct bnxt_pf_info *pf = &bp->pf;
6312 
6313 		if (fltr->base.vf_idx >= pf->active_vfs)
6314 			return -EINVAL;
6315 
6316 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6317 		if (target_id == INVALID_HW_RING_ID)
6318 			return -EINVAL;
6319 	}
6320 
6321 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6322 	if (rc)
6323 		return rc;
6324 
6325 	req->target_id = cpu_to_le16(target_id);
6326 	req->l2_filter_id = fltr->base.filter_id;
6327 	return hwrm_req_send(bp, req);
6328 }
6329 
6330 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6331 {
6332 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6333 	struct hwrm_cfa_l2_filter_alloc_input *req;
6334 	u16 target_id = 0xffff;
6335 	int rc;
6336 
6337 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6338 		struct bnxt_pf_info *pf = &bp->pf;
6339 
6340 		if (fltr->base.vf_idx >= pf->active_vfs)
6341 			return -EINVAL;
6342 
6343 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6344 	}
6345 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6346 	if (rc)
6347 		return rc;
6348 
6349 	req->target_id = cpu_to_le16(target_id);
6350 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6351 
6352 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6353 		req->flags |=
6354 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6355 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6356 	req->enables =
6357 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6358 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6359 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6360 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6361 	eth_broadcast_addr(req->l2_addr_mask);
6362 
6363 	if (fltr->l2_key.vlan) {
6364 		req->enables |=
6365 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6366 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6367 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6368 		req->num_vlans = 1;
6369 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6370 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6371 	}
6372 
6373 	resp = hwrm_req_hold(bp, req);
6374 	rc = hwrm_req_send(bp, req);
6375 	if (!rc) {
6376 		fltr->base.filter_id = resp->l2_filter_id;
6377 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6378 	}
6379 	hwrm_req_drop(bp, req);
6380 	return rc;
6381 }
6382 
6383 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6384 				     struct bnxt_ntuple_filter *fltr)
6385 {
6386 	struct hwrm_cfa_ntuple_filter_free_input *req;
6387 	int rc;
6388 
6389 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6390 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
6391 		return 0;
6392 
6393 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6394 	if (rc)
6395 		return rc;
6396 
6397 	req->ntuple_filter_id = fltr->base.filter_id;
6398 	return hwrm_req_send(bp, req);
6399 }
6400 
6401 #define BNXT_NTP_FLTR_FLAGS					\
6402 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6403 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6404 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6405 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6406 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6407 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6408 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6409 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6410 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6411 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6412 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6413 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6414 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6415 
6416 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6417 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6418 
6419 void bnxt_fill_ipv6_mask(__be32 mask[4])
6420 {
6421 	int i;
6422 
6423 	for (i = 0; i < 4; i++)
6424 		mask[i] = cpu_to_be32(~0);
6425 }
6426 
6427 static void
6428 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6429 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6430 			  struct bnxt_ntuple_filter *fltr)
6431 {
6432 	u16 rxq = fltr->base.rxq;
6433 
6434 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6435 		struct ethtool_rxfh_context *ctx;
6436 		struct bnxt_rss_ctx *rss_ctx;
6437 		struct bnxt_vnic_info *vnic;
6438 
6439 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6440 			      fltr->base.fw_vnic_id);
6441 		if (ctx) {
6442 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6443 			vnic = &rss_ctx->vnic;
6444 
6445 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6446 		}
6447 		return;
6448 	}
6449 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6450 		struct bnxt_vnic_info *vnic;
6451 		u32 enables;
6452 
6453 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6454 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6455 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6456 		req->enables |= cpu_to_le32(enables);
6457 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6458 	} else {
6459 		u32 flags;
6460 
6461 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6462 		req->flags |= cpu_to_le32(flags);
6463 		req->dst_id = cpu_to_le16(rxq);
6464 	}
6465 }
6466 
6467 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6468 				      struct bnxt_ntuple_filter *fltr)
6469 {
6470 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6471 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6472 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6473 	struct flow_keys *keys = &fltr->fkeys;
6474 	struct bnxt_l2_filter *l2_fltr;
6475 	struct bnxt_vnic_info *vnic;
6476 	int rc;
6477 
6478 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6479 	if (rc)
6480 		return rc;
6481 
6482 	l2_fltr = fltr->l2_fltr;
6483 	req->l2_filter_id = l2_fltr->base.filter_id;
6484 
6485 	if (fltr->base.flags & BNXT_ACT_DROP) {
6486 		req->flags =
6487 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6488 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6489 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6490 	} else {
6491 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6492 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6493 	}
6494 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6495 
6496 	req->ethertype = htons(ETH_P_IP);
6497 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6498 	req->ip_protocol = keys->basic.ip_proto;
6499 
6500 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6501 		req->ethertype = htons(ETH_P_IPV6);
6502 		req->ip_addr_type =
6503 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6504 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6505 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6506 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6507 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6508 	} else {
6509 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6510 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6511 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6512 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6513 	}
6514 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6515 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6516 		req->tunnel_type =
6517 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6518 	}
6519 
6520 	req->src_port = keys->ports.src;
6521 	req->src_port_mask = masks->ports.src;
6522 	req->dst_port = keys->ports.dst;
6523 	req->dst_port_mask = masks->ports.dst;
6524 
6525 	resp = hwrm_req_hold(bp, req);
6526 	rc = hwrm_req_send(bp, req);
6527 	if (!rc)
6528 		fltr->base.filter_id = resp->ntuple_filter_id;
6529 	hwrm_req_drop(bp, req);
6530 	return rc;
6531 }
6532 
6533 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6534 				     const u8 *mac_addr)
6535 {
6536 	struct bnxt_l2_filter *fltr;
6537 	struct bnxt_l2_key key;
6538 	int rc;
6539 
6540 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6541 	key.vlan = 0;
6542 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6543 	if (IS_ERR(fltr))
6544 		return PTR_ERR(fltr);
6545 
6546 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6547 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6548 	if (rc)
6549 		bnxt_del_l2_filter(bp, fltr);
6550 	else
6551 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6552 	return rc;
6553 }
6554 
6555 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6556 {
6557 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6558 
6559 	/* Any associated ntuple filters will also be cleared by firmware. */
6560 	for (i = 0; i < num_of_vnics; i++) {
6561 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6562 
6563 		for (j = 0; j < vnic->uc_filter_count; j++) {
6564 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6565 
6566 			bnxt_hwrm_l2_filter_free(bp, fltr);
6567 			bnxt_del_l2_filter(bp, fltr);
6568 		}
6569 		vnic->uc_filter_count = 0;
6570 	}
6571 }
6572 
6573 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6574 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6575 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6576 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6577 
6578 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6579 					   struct hwrm_vnic_tpa_cfg_input *req)
6580 {
6581 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6582 
6583 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6584 		return;
6585 
6586 	if (bp->vxlan_port)
6587 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6588 	if (bp->vxlan_gpe_port)
6589 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6590 	if (bp->nge_port)
6591 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6592 
6593 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6594 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6595 }
6596 
6597 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6598 			   u32 tpa_flags)
6599 {
6600 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6601 	struct hwrm_vnic_tpa_cfg_input *req;
6602 	int rc;
6603 
6604 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6605 		return 0;
6606 
6607 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6608 	if (rc)
6609 		return rc;
6610 
6611 	if (tpa_flags) {
6612 		u16 mss = bp->dev->mtu - 40;
6613 		u32 nsegs, n, segs = 0, flags;
6614 
6615 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6616 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6617 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6618 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6619 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6620 		if (tpa_flags & BNXT_FLAG_GRO)
6621 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6622 
6623 		req->flags = cpu_to_le32(flags);
6624 
6625 		req->enables =
6626 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6627 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6628 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6629 
6630 		/* Number of segs are log2 units, and first packet is not
6631 		 * included as part of this units.
6632 		 */
6633 		if (mss <= BNXT_RX_PAGE_SIZE) {
6634 			n = BNXT_RX_PAGE_SIZE / mss;
6635 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6636 		} else {
6637 			n = mss / BNXT_RX_PAGE_SIZE;
6638 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6639 				n++;
6640 			nsegs = (MAX_SKB_FRAGS - n) / n;
6641 		}
6642 
6643 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6644 			segs = MAX_TPA_SEGS_P5;
6645 			max_aggs = bp->max_tpa;
6646 		} else {
6647 			segs = ilog2(nsegs);
6648 		}
6649 		req->max_agg_segs = cpu_to_le16(segs);
6650 		req->max_aggs = cpu_to_le16(max_aggs);
6651 
6652 		req->min_agg_len = cpu_to_le32(512);
6653 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6654 	}
6655 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6656 
6657 	return hwrm_req_send(bp, req);
6658 }
6659 
6660 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6661 {
6662 	struct bnxt_ring_grp_info *grp_info;
6663 
6664 	grp_info = &bp->grp_info[ring->grp_idx];
6665 	return grp_info->cp_fw_ring_id;
6666 }
6667 
6668 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6669 {
6670 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6671 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6672 	else
6673 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6674 }
6675 
6676 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6677 {
6678 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6679 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6680 	else
6681 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6682 }
6683 
6684 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6685 {
6686 	int entries;
6687 
6688 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6689 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6690 	else
6691 		entries = HW_HASH_INDEX_SIZE;
6692 
6693 	bp->rss_indir_tbl_entries = entries;
6694 	bp->rss_indir_tbl =
6695 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6696 	if (!bp->rss_indir_tbl)
6697 		return -ENOMEM;
6698 
6699 	return 0;
6700 }
6701 
6702 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6703 				 struct ethtool_rxfh_context *rss_ctx)
6704 {
6705 	u16 max_rings, max_entries, pad, i;
6706 	u32 *rss_indir_tbl;
6707 
6708 	if (!bp->rx_nr_rings)
6709 		return;
6710 
6711 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6712 		max_rings = bp->rx_nr_rings - 1;
6713 	else
6714 		max_rings = bp->rx_nr_rings;
6715 
6716 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6717 	if (rss_ctx)
6718 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6719 	else
6720 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6721 
6722 	for (i = 0; i < max_entries; i++)
6723 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6724 
6725 	pad = bp->rss_indir_tbl_entries - max_entries;
6726 	if (pad)
6727 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6728 }
6729 
6730 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6731 {
6732 	u32 i, tbl_size, max_ring = 0;
6733 
6734 	if (!bp->rss_indir_tbl)
6735 		return 0;
6736 
6737 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6738 	for (i = 0; i < tbl_size; i++)
6739 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6740 	return max_ring;
6741 }
6742 
6743 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6744 {
6745 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6746 		if (!rx_rings)
6747 			return 0;
6748 		if (bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX)
6749 			return BNXT_RSS_TABLE_MAX_TBL_P5;
6750 
6751 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6752 					       BNXT_RSS_TABLE_ENTRIES_P5);
6753 	}
6754 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6755 		return 2;
6756 	return 1;
6757 }
6758 
6759 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6760 {
6761 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6762 	u16 i, j, min_j = bp->rx_nr_rings - 1;
6763 
6764 	if (!vnic->rss_table)
6765 		goto skip_rss_tbl;
6766 
6767 	/* Fill the RSS indirection table with ring group ids */
6768 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6769 		if (!no_rss)
6770 			j = bp->rss_indir_tbl[i];
6771 		min_j = min(j, min_j);
6772 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6773 	}
6774 
6775 skip_rss_tbl:
6776 	if (vnic->rss_table && !no_rss)
6777 		vnic->default_rx_ring = min_j;
6778 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6779 		vnic->default_rx_ring = vnic->vnic_id - 1;
6780 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6781 		vnic->default_rx_ring = bp->rx_nr_rings - 1;
6782 	else
6783 		vnic->default_rx_ring = 0;
6784 }
6785 
6786 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6787 				    struct bnxt_vnic_info *vnic)
6788 {
6789 	u16 tbl_size, i, min_j = bp->rx_nr_rings - 1;
6790 	__le16 *ring_tbl = vnic->rss_table;
6791 	struct bnxt_rx_ring_info *rxr;
6792 
6793 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6794 
6795 	for (i = 0; i < tbl_size; i++) {
6796 		u16 ring_id, j;
6797 
6798 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6799 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6800 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6801 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6802 		else
6803 			j = bp->rss_indir_tbl[i];
6804 		min_j = min(j, min_j);
6805 		rxr = &bp->rx_ring[j];
6806 
6807 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6808 		*ring_tbl++ = cpu_to_le16(ring_id);
6809 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6810 		*ring_tbl++ = cpu_to_le16(ring_id);
6811 	}
6812 	vnic->default_rx_ring = min_j;
6813 }
6814 
6815 static void
6816 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6817 			 struct bnxt_vnic_info *vnic)
6818 {
6819 	if (bp->flags & BNXT_FLAG_CHIP_P7)
6820 		req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6821 
6822 	if (bp->rss_hash_delta) {
6823 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6824 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6825 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6826 		else
6827 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6828 	} else {
6829 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6830 	}
6831 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6832 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6833 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6834 }
6835 
6836 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6837 				  bool set_rss)
6838 {
6839 	struct hwrm_vnic_rss_cfg_input *req;
6840 	int rc;
6841 
6842 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6843 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6844 		return 0;
6845 
6846 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6847 	if (rc)
6848 		return rc;
6849 
6850 	if (set_rss)
6851 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6852 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6853 	return hwrm_req_send(bp, req);
6854 }
6855 
6856 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6857 				     struct bnxt_vnic_info *vnic, bool set_rss)
6858 {
6859 	struct hwrm_vnic_rss_cfg_input *req;
6860 	dma_addr_t ring_tbl_map;
6861 	u32 i, nr_ctxs;
6862 	int rc;
6863 
6864 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6865 	if (rc)
6866 		return rc;
6867 
6868 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6869 	if (!set_rss)
6870 		return hwrm_req_send(bp, req);
6871 
6872 	bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6873 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6874 	ring_tbl_map = vnic->rss_table_dma_addr;
6875 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6876 
6877 	hwrm_req_hold(bp, req);
6878 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6879 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6880 		req->ring_table_pair_index = i;
6881 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6882 		rc = hwrm_req_send(bp, req);
6883 		if (rc)
6884 			goto exit;
6885 	}
6886 
6887 exit:
6888 	hwrm_req_drop(bp, req);
6889 	return rc;
6890 }
6891 
6892 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6893 {
6894 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6895 	struct hwrm_vnic_rss_qcfg_output *resp;
6896 	struct hwrm_vnic_rss_qcfg_input *req;
6897 
6898 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6899 		return;
6900 
6901 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6902 	/* all contexts configured to same hash_type, zero always exists */
6903 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6904 	resp = hwrm_req_hold(bp, req);
6905 	if (!hwrm_req_send(bp, req)) {
6906 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6907 		bp->rss_hash_delta = 0;
6908 	}
6909 	hwrm_req_drop(bp, req);
6910 }
6911 
6912 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6913 {
6914 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6915 	struct hwrm_vnic_plcmodes_cfg_input *req;
6916 	int rc;
6917 
6918 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6919 	if (rc)
6920 		return rc;
6921 
6922 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6923 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6924 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6925 
6926 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6927 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6928 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6929 		req->enables |=
6930 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6931 		req->hds_threshold = cpu_to_le16(hds_thresh);
6932 	}
6933 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6934 	return hwrm_req_send(bp, req);
6935 }
6936 
6937 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6938 					struct bnxt_vnic_info *vnic,
6939 					u16 ctx_idx)
6940 {
6941 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6942 
6943 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6944 		return;
6945 
6946 	req->rss_cos_lb_ctx_id =
6947 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6948 
6949 	hwrm_req_send(bp, req);
6950 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6951 }
6952 
6953 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6954 {
6955 	int i, j;
6956 
6957 	for (i = 0; i < bp->nr_vnics; i++) {
6958 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6959 
6960 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6961 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6962 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6963 		}
6964 	}
6965 	bp->rsscos_nr_ctxs = 0;
6966 }
6967 
6968 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6969 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6970 {
6971 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6972 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6973 	int rc;
6974 
6975 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6976 	if (rc)
6977 		return rc;
6978 
6979 	resp = hwrm_req_hold(bp, req);
6980 	rc = hwrm_req_send(bp, req);
6981 	if (!rc)
6982 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6983 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6984 	hwrm_req_drop(bp, req);
6985 
6986 	return rc;
6987 }
6988 
6989 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6990 {
6991 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6992 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6993 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6994 }
6995 
6996 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6997 {
6998 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6999 	struct hwrm_vnic_cfg_input *req;
7000 	unsigned int ring = 0, grp_idx;
7001 	u16 def_vlan = 0;
7002 	int rc;
7003 
7004 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
7005 	if (rc)
7006 		return rc;
7007 
7008 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7009 		struct bnxt_rx_ring_info *rxr;
7010 
7011 		rxr = &bp->rx_ring[vnic->default_rx_ring];
7012 		req->default_rx_ring_id =
7013 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
7014 		req->default_cmpl_ring_id =
7015 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
7016 		req->enables =
7017 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
7018 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
7019 		goto vnic_mru;
7020 	}
7021 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
7022 	/* Only RSS support for now TBD: COS & LB */
7023 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
7024 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
7025 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
7026 					   VNIC_CFG_REQ_ENABLES_MRU);
7027 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
7028 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
7029 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
7030 					   VNIC_CFG_REQ_ENABLES_MRU);
7031 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
7032 	} else {
7033 		req->rss_rule = cpu_to_le16(0xffff);
7034 	}
7035 
7036 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
7037 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
7038 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
7039 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
7040 	} else {
7041 		req->cos_rule = cpu_to_le16(0xffff);
7042 	}
7043 
7044 	ring = vnic->default_rx_ring;
7045 	grp_idx = bp->rx_ring[ring].bnapi->index;
7046 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
7047 	req->lb_rule = cpu_to_le16(0xffff);
7048 vnic_mru:
7049 	vnic->mru = bp->dev->mtu + VLAN_ETH_HLEN;
7050 	req->mru = cpu_to_le16(vnic->mru);
7051 
7052 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
7053 #ifdef CONFIG_BNXT_SRIOV
7054 	if (BNXT_VF(bp))
7055 		def_vlan = bp->vf.vlan;
7056 #endif
7057 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
7058 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
7059 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT &&
7060 	    bnxt_ulp_registered(bp->edev[BNXT_AUXDEV_RDMA]))
7061 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
7062 
7063 	return hwrm_req_send(bp, req);
7064 }
7065 
7066 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
7067 				    struct bnxt_vnic_info *vnic)
7068 {
7069 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
7070 		struct hwrm_vnic_free_input *req;
7071 
7072 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
7073 			return;
7074 
7075 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
7076 
7077 		hwrm_req_send(bp, req);
7078 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
7079 	}
7080 }
7081 
7082 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
7083 {
7084 	u16 i;
7085 
7086 	for (i = 0; i < bp->nr_vnics; i++)
7087 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
7088 }
7089 
7090 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
7091 			 unsigned int start_rx_ring_idx,
7092 			 unsigned int nr_rings)
7093 {
7094 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
7095 	struct hwrm_vnic_alloc_output *resp;
7096 	struct hwrm_vnic_alloc_input *req;
7097 	int rc;
7098 
7099 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
7100 	if (rc)
7101 		return rc;
7102 
7103 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7104 		goto vnic_no_ring_grps;
7105 
7106 	/* map ring groups to this vnic */
7107 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
7108 		grp_idx = bp->rx_ring[i].bnapi->index;
7109 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
7110 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
7111 				   j, nr_rings);
7112 			break;
7113 		}
7114 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
7115 	}
7116 
7117 vnic_no_ring_grps:
7118 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
7119 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
7120 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
7121 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
7122 
7123 	resp = hwrm_req_hold(bp, req);
7124 	rc = hwrm_req_send(bp, req);
7125 	if (!rc)
7126 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
7127 	hwrm_req_drop(bp, req);
7128 	return rc;
7129 }
7130 
7131 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
7132 {
7133 	struct hwrm_vnic_qcaps_output *resp;
7134 	struct hwrm_vnic_qcaps_input *req;
7135 	int rc;
7136 
7137 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
7138 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
7139 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
7140 	if (bp->hwrm_spec_code < 0x10600)
7141 		return 0;
7142 
7143 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
7144 	if (rc)
7145 		return rc;
7146 
7147 	resp = hwrm_req_hold(bp, req);
7148 	rc = hwrm_req_send(bp, req);
7149 	if (!rc) {
7150 		u32 flags = le32_to_cpu(resp->flags);
7151 
7152 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
7153 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
7154 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
7155 		if (flags &
7156 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
7157 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
7158 
7159 		/* Older P5 fw before EXT_HW_STATS support did not set
7160 		 * VLAN_STRIP_CAP properly.
7161 		 */
7162 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
7163 		    (BNXT_CHIP_P5(bp) &&
7164 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
7165 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
7166 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
7167 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
7168 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
7169 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
7170 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
7171 		if (bp->max_tpa_v2) {
7172 			if (BNXT_CHIP_P5(bp))
7173 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
7174 			else
7175 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
7176 		}
7177 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
7178 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
7179 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
7180 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
7181 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
7182 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
7183 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
7184 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
7185 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
7186 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
7187 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPV6_FLOW_LABEL_CAP)
7188 			bp->rss_cap |= BNXT_RSS_CAP_IPV6_FLOW_LABEL_RSS_CAP;
7189 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
7190 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
7191 	}
7192 	hwrm_req_drop(bp, req);
7193 	return rc;
7194 }
7195 
7196 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
7197 {
7198 	struct hwrm_ring_grp_alloc_output *resp;
7199 	struct hwrm_ring_grp_alloc_input *req;
7200 	int rc;
7201 	u16 i;
7202 
7203 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7204 		return 0;
7205 
7206 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
7207 	if (rc)
7208 		return rc;
7209 
7210 	resp = hwrm_req_hold(bp, req);
7211 	for (i = 0; i < bp->rx_nr_rings; i++) {
7212 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
7213 
7214 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
7215 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
7216 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
7217 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
7218 
7219 		rc = hwrm_req_send(bp, req);
7220 
7221 		if (rc)
7222 			break;
7223 
7224 		bp->grp_info[grp_idx].fw_grp_id =
7225 			le32_to_cpu(resp->ring_group_id);
7226 	}
7227 	hwrm_req_drop(bp, req);
7228 	return rc;
7229 }
7230 
7231 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
7232 {
7233 	struct hwrm_ring_grp_free_input *req;
7234 	u16 i;
7235 
7236 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7237 		return;
7238 
7239 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
7240 		return;
7241 
7242 	hwrm_req_hold(bp, req);
7243 	for (i = 0; i < bp->cp_nr_rings; i++) {
7244 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
7245 			continue;
7246 		req->ring_group_id =
7247 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
7248 
7249 		hwrm_req_send(bp, req);
7250 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
7251 	}
7252 	hwrm_req_drop(bp, req);
7253 }
7254 
7255 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
7256 				       struct hwrm_ring_alloc_input *req,
7257 				       struct bnxt_rx_ring_info *rxr,
7258 				       struct bnxt_ring_struct *ring)
7259 {
7260 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
7261 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
7262 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
7263 
7264 	if (ring_type == HWRM_RING_ALLOC_AGG) {
7265 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7266 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7267 		req->rx_buf_size = cpu_to_le16(rxr->rx_page_size);
7268 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
7269 	} else {
7270 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
7271 		if (NET_IP_ALIGN == 2)
7272 			req->flags =
7273 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
7274 	}
7275 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7276 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7277 	req->enables |= cpu_to_le32(enables);
7278 }
7279 
7280 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7281 				    struct bnxt_rx_ring_info *rxr,
7282 				    struct bnxt_ring_struct *ring,
7283 				    u32 ring_type, u32 map_index)
7284 {
7285 	struct hwrm_ring_alloc_output *resp;
7286 	struct hwrm_ring_alloc_input *req;
7287 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7288 	struct bnxt_ring_grp_info *grp_info;
7289 	int rc, err = 0;
7290 	u16 ring_id;
7291 
7292 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7293 	if (rc)
7294 		goto exit;
7295 
7296 	req->enables = 0;
7297 	if (rmem->nr_pages > 1) {
7298 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7299 		/* Page size is in log2 units */
7300 		req->page_size = BNXT_PAGE_SHIFT;
7301 		req->page_tbl_depth = 1;
7302 	} else {
7303 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7304 	}
7305 	req->fbo = 0;
7306 	/* Association of ring index with doorbell index and MSIX number */
7307 	req->logical_id = cpu_to_le16(map_index);
7308 
7309 	switch (ring_type) {
7310 	case HWRM_RING_ALLOC_TX: {
7311 		struct bnxt_tx_ring_info *txr;
7312 		u16 flags = 0;
7313 
7314 		txr = container_of(ring, struct bnxt_tx_ring_info,
7315 				   tx_ring_struct);
7316 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7317 		/* Association of transmit ring with completion ring */
7318 		grp_info = &bp->grp_info[ring->grp_idx];
7319 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7320 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7321 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7322 		req->queue_id = cpu_to_le16(ring->queue_id);
7323 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7324 			req->cmpl_coal_cnt =
7325 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7326 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7327 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7328 		req->flags = cpu_to_le16(flags);
7329 		break;
7330 	}
7331 	case HWRM_RING_ALLOC_RX:
7332 	case HWRM_RING_ALLOC_AGG:
7333 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7334 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7335 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7336 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7337 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7338 			bnxt_set_rx_ring_params_p5(bp, ring_type, req,
7339 						   rxr, ring);
7340 		break;
7341 	case HWRM_RING_ALLOC_CMPL:
7342 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7343 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7344 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7345 			/* Association of cp ring with nq */
7346 			grp_info = &bp->grp_info[map_index];
7347 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7348 			req->cq_handle = cpu_to_le64(ring->handle);
7349 			req->enables |= cpu_to_le32(
7350 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7351 		} else {
7352 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7353 		}
7354 		break;
7355 	case HWRM_RING_ALLOC_NQ:
7356 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7357 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7358 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7359 		break;
7360 	default:
7361 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7362 			   ring_type);
7363 		return -EINVAL;
7364 	}
7365 
7366 	resp = hwrm_req_hold(bp, req);
7367 	rc = hwrm_req_send(bp, req);
7368 	err = le16_to_cpu(resp->error_code);
7369 	ring_id = le16_to_cpu(resp->ring_id);
7370 	hwrm_req_drop(bp, req);
7371 
7372 exit:
7373 	if (rc || err) {
7374 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7375 			   ring_type, rc, err);
7376 		return -EIO;
7377 	}
7378 	ring->fw_ring_id = ring_id;
7379 	return rc;
7380 }
7381 
7382 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7383 {
7384 	int rc;
7385 
7386 	if (BNXT_PF(bp)) {
7387 		struct hwrm_func_cfg_input *req;
7388 
7389 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7390 		if (rc)
7391 			return rc;
7392 
7393 		req->fid = cpu_to_le16(0xffff);
7394 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7395 		req->async_event_cr = cpu_to_le16(idx);
7396 		return hwrm_req_send(bp, req);
7397 	} else {
7398 		struct hwrm_func_vf_cfg_input *req;
7399 
7400 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7401 		if (rc)
7402 			return rc;
7403 
7404 		req->enables =
7405 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7406 		req->async_event_cr = cpu_to_le16(idx);
7407 		return hwrm_req_send(bp, req);
7408 	}
7409 }
7410 
7411 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7412 			     u32 ring_type)
7413 {
7414 	switch (ring_type) {
7415 	case HWRM_RING_ALLOC_TX:
7416 		db->db_ring_mask = bp->tx_ring_mask;
7417 		break;
7418 	case HWRM_RING_ALLOC_RX:
7419 		db->db_ring_mask = bp->rx_ring_mask;
7420 		break;
7421 	case HWRM_RING_ALLOC_AGG:
7422 		db->db_ring_mask = bp->rx_agg_ring_mask;
7423 		break;
7424 	case HWRM_RING_ALLOC_CMPL:
7425 	case HWRM_RING_ALLOC_NQ:
7426 		db->db_ring_mask = bp->cp_ring_mask;
7427 		break;
7428 	}
7429 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7430 		db->db_epoch_mask = db->db_ring_mask + 1;
7431 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7432 	}
7433 }
7434 
7435 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7436 			u32 map_idx, u32 xid)
7437 {
7438 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7439 		switch (ring_type) {
7440 		case HWRM_RING_ALLOC_TX:
7441 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7442 			break;
7443 		case HWRM_RING_ALLOC_RX:
7444 		case HWRM_RING_ALLOC_AGG:
7445 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7446 			break;
7447 		case HWRM_RING_ALLOC_CMPL:
7448 			db->db_key64 = DBR_PATH_L2;
7449 			break;
7450 		case HWRM_RING_ALLOC_NQ:
7451 			db->db_key64 = DBR_PATH_L2;
7452 			break;
7453 		}
7454 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7455 
7456 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7457 			db->db_key64 |= DBR_VALID;
7458 
7459 		db->doorbell = bp->bar1 + bp->db_offset;
7460 	} else {
7461 		db->doorbell = bp->bar1 + map_idx * 0x80;
7462 		switch (ring_type) {
7463 		case HWRM_RING_ALLOC_TX:
7464 			db->db_key32 = DB_KEY_TX;
7465 			break;
7466 		case HWRM_RING_ALLOC_RX:
7467 		case HWRM_RING_ALLOC_AGG:
7468 			db->db_key32 = DB_KEY_RX;
7469 			break;
7470 		case HWRM_RING_ALLOC_CMPL:
7471 			db->db_key32 = DB_KEY_CP;
7472 			break;
7473 		}
7474 	}
7475 	bnxt_set_db_mask(bp, db, ring_type);
7476 }
7477 
7478 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7479 				   struct bnxt_rx_ring_info *rxr)
7480 {
7481 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7482 	struct bnxt_napi *bnapi = rxr->bnapi;
7483 	u32 type = HWRM_RING_ALLOC_RX;
7484 	u32 map_idx = bnapi->index;
7485 	int rc;
7486 
7487 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7488 	if (rc)
7489 		return rc;
7490 
7491 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7492 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7493 
7494 	return 0;
7495 }
7496 
7497 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7498 				       struct bnxt_rx_ring_info *rxr)
7499 {
7500 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7501 	u32 type = HWRM_RING_ALLOC_AGG;
7502 	u32 grp_idx = ring->grp_idx;
7503 	u32 map_idx;
7504 	int rc;
7505 
7506 	map_idx = grp_idx + bp->rx_nr_rings;
7507 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7508 	if (rc)
7509 		return rc;
7510 
7511 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7512 		    ring->fw_ring_id);
7513 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7514 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7515 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7516 
7517 	return 0;
7518 }
7519 
7520 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7521 				      struct bnxt_cp_ring_info *cpr)
7522 {
7523 	const u32 type = HWRM_RING_ALLOC_CMPL;
7524 	struct bnxt_napi *bnapi = cpr->bnapi;
7525 	struct bnxt_ring_struct *ring;
7526 	u32 map_idx = bnapi->index;
7527 	int rc;
7528 
7529 	ring = &cpr->cp_ring_struct;
7530 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7531 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7532 	if (rc)
7533 		return rc;
7534 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7535 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7536 	return 0;
7537 }
7538 
7539 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7540 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7541 {
7542 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7543 	const u32 type = HWRM_RING_ALLOC_TX;
7544 	int rc;
7545 
7546 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, tx_idx);
7547 	if (rc)
7548 		return rc;
7549 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7550 	return 0;
7551 }
7552 
7553 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7554 {
7555 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7556 	int i, rc = 0;
7557 	u32 type;
7558 
7559 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7560 		type = HWRM_RING_ALLOC_NQ;
7561 	else
7562 		type = HWRM_RING_ALLOC_CMPL;
7563 	for (i = 0; i < bp->cp_nr_rings; i++) {
7564 		struct bnxt_napi *bnapi = bp->bnapi[i];
7565 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7566 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7567 		u32 map_idx = ring->map_idx;
7568 		unsigned int vector;
7569 
7570 		vector = bp->irq_tbl[map_idx].vector;
7571 		disable_irq_nosync(vector);
7572 		rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7573 		if (rc) {
7574 			enable_irq(vector);
7575 			goto err_out;
7576 		}
7577 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7578 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7579 		enable_irq(vector);
7580 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7581 
7582 		if (!i) {
7583 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7584 			if (rc)
7585 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7586 		}
7587 	}
7588 
7589 	for (i = 0; i < bp->tx_nr_rings; i++) {
7590 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7591 
7592 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7593 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7594 			if (rc)
7595 				goto err_out;
7596 		}
7597 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7598 		if (rc)
7599 			goto err_out;
7600 	}
7601 
7602 	for (i = 0; i < bp->rx_nr_rings; i++) {
7603 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7604 
7605 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7606 		if (rc)
7607 			goto err_out;
7608 		/* If we have agg rings, post agg buffers first. */
7609 		if (!agg_rings)
7610 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7611 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7612 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7613 			if (rc)
7614 				goto err_out;
7615 		}
7616 	}
7617 
7618 	if (agg_rings) {
7619 		for (i = 0; i < bp->rx_nr_rings; i++) {
7620 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7621 			if (rc)
7622 				goto err_out;
7623 		}
7624 	}
7625 err_out:
7626 	return rc;
7627 }
7628 
7629 static void bnxt_cancel_dim(struct bnxt *bp)
7630 {
7631 	int i;
7632 
7633 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7634 	 * if NAPI is enabled.
7635 	 */
7636 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7637 		return;
7638 
7639 	/* Make sure NAPI sees that the VNIC is disabled */
7640 	synchronize_net();
7641 	for (i = 0; i < bp->rx_nr_rings; i++) {
7642 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7643 		struct bnxt_napi *bnapi = rxr->bnapi;
7644 
7645 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7646 	}
7647 }
7648 
7649 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7650 				   struct bnxt_ring_struct *ring,
7651 				   u32 ring_type, int cmpl_ring_id)
7652 {
7653 	struct hwrm_ring_free_output *resp;
7654 	struct hwrm_ring_free_input *req;
7655 	u16 error_code = 0;
7656 	int rc;
7657 
7658 	if (BNXT_NO_FW_ACCESS(bp))
7659 		return 0;
7660 
7661 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7662 	if (rc)
7663 		goto exit;
7664 
7665 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7666 	req->ring_type = ring_type;
7667 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7668 
7669 	resp = hwrm_req_hold(bp, req);
7670 	rc = hwrm_req_send(bp, req);
7671 	error_code = le16_to_cpu(resp->error_code);
7672 	hwrm_req_drop(bp, req);
7673 exit:
7674 	if (rc || error_code) {
7675 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7676 			   ring_type, rc, error_code);
7677 		return -EIO;
7678 	}
7679 	return 0;
7680 }
7681 
7682 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7683 				   struct bnxt_tx_ring_info *txr,
7684 				   bool close_path)
7685 {
7686 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7687 	u32 cmpl_ring_id;
7688 
7689 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7690 		return;
7691 
7692 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7693 		       INVALID_HW_RING_ID;
7694 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7695 				cmpl_ring_id);
7696 	ring->fw_ring_id = INVALID_HW_RING_ID;
7697 }
7698 
7699 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7700 				   struct bnxt_rx_ring_info *rxr,
7701 				   bool close_path)
7702 {
7703 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7704 	u32 grp_idx = rxr->bnapi->index;
7705 	u32 cmpl_ring_id;
7706 
7707 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7708 		return;
7709 
7710 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7711 	hwrm_ring_free_send_msg(bp, ring,
7712 				RING_FREE_REQ_RING_TYPE_RX,
7713 				close_path ? cmpl_ring_id :
7714 				INVALID_HW_RING_ID);
7715 	ring->fw_ring_id = INVALID_HW_RING_ID;
7716 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7717 }
7718 
7719 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7720 				       struct bnxt_rx_ring_info *rxr,
7721 				       bool close_path)
7722 {
7723 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7724 	u32 grp_idx = rxr->bnapi->index;
7725 	u32 type, cmpl_ring_id;
7726 
7727 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7728 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7729 	else
7730 		type = RING_FREE_REQ_RING_TYPE_RX;
7731 
7732 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7733 		return;
7734 
7735 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7736 	hwrm_ring_free_send_msg(bp, ring, type,
7737 				close_path ? cmpl_ring_id :
7738 				INVALID_HW_RING_ID);
7739 	ring->fw_ring_id = INVALID_HW_RING_ID;
7740 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7741 }
7742 
7743 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7744 				   struct bnxt_cp_ring_info *cpr)
7745 {
7746 	struct bnxt_ring_struct *ring;
7747 
7748 	ring = &cpr->cp_ring_struct;
7749 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7750 		return;
7751 
7752 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7753 				INVALID_HW_RING_ID);
7754 	ring->fw_ring_id = INVALID_HW_RING_ID;
7755 }
7756 
7757 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7758 {
7759 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7760 	int i, size = ring->ring_mem.page_size;
7761 
7762 	cpr->cp_raw_cons = 0;
7763 	cpr->toggle = 0;
7764 
7765 	for (i = 0; i < bp->cp_nr_pages; i++)
7766 		if (cpr->cp_desc_ring[i])
7767 			memset(cpr->cp_desc_ring[i], 0, size);
7768 }
7769 
7770 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7771 {
7772 	u32 type;
7773 	int i;
7774 
7775 	if (!bp->bnapi)
7776 		return;
7777 
7778 	for (i = 0; i < bp->tx_nr_rings; i++)
7779 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7780 
7781 	bnxt_cancel_dim(bp);
7782 	for (i = 0; i < bp->rx_nr_rings; i++) {
7783 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7784 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7785 	}
7786 
7787 	/* The completion rings are about to be freed.  After that the
7788 	 * IRQ doorbell will not work anymore.  So we need to disable
7789 	 * IRQ here.
7790 	 */
7791 	bnxt_disable_int_sync(bp);
7792 
7793 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7794 		type = RING_FREE_REQ_RING_TYPE_NQ;
7795 	else
7796 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7797 	for (i = 0; i < bp->cp_nr_rings; i++) {
7798 		struct bnxt_napi *bnapi = bp->bnapi[i];
7799 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7800 		struct bnxt_ring_struct *ring;
7801 		int j;
7802 
7803 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7804 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7805 
7806 		ring = &cpr->cp_ring_struct;
7807 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7808 			hwrm_ring_free_send_msg(bp, ring, type,
7809 						INVALID_HW_RING_ID);
7810 			ring->fw_ring_id = INVALID_HW_RING_ID;
7811 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7812 		}
7813 	}
7814 }
7815 
7816 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7817 			     bool shared);
7818 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7819 			   bool shared);
7820 
7821 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7822 {
7823 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7824 	struct hwrm_func_qcfg_output *resp;
7825 	struct hwrm_func_qcfg_input *req;
7826 	int rc;
7827 
7828 	if (bp->hwrm_spec_code < 0x10601)
7829 		return 0;
7830 
7831 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7832 	if (rc)
7833 		return rc;
7834 
7835 	req->fid = cpu_to_le16(0xffff);
7836 	resp = hwrm_req_hold(bp, req);
7837 	rc = hwrm_req_send(bp, req);
7838 	if (rc) {
7839 		hwrm_req_drop(bp, req);
7840 		return rc;
7841 	}
7842 
7843 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7844 	if (BNXT_NEW_RM(bp)) {
7845 		u16 cp, stats;
7846 
7847 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7848 		hw_resc->resv_hw_ring_grps =
7849 			le32_to_cpu(resp->alloc_hw_ring_grps);
7850 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7851 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7852 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7853 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7854 		hw_resc->resv_irqs = cp;
7855 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7856 			int rx = hw_resc->resv_rx_rings;
7857 			int tx = hw_resc->resv_tx_rings;
7858 
7859 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7860 				rx >>= 1;
7861 			if (cp < (rx + tx)) {
7862 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7863 				if (rc)
7864 					goto get_rings_exit;
7865 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7866 					rx <<= 1;
7867 				hw_resc->resv_rx_rings = rx;
7868 				hw_resc->resv_tx_rings = tx;
7869 			}
7870 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7871 			hw_resc->resv_hw_ring_grps = rx;
7872 		}
7873 		hw_resc->resv_cp_rings = cp;
7874 		hw_resc->resv_stat_ctxs = stats;
7875 	}
7876 get_rings_exit:
7877 	hwrm_req_drop(bp, req);
7878 	return rc;
7879 }
7880 
7881 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7882 {
7883 	struct hwrm_func_qcfg_output *resp;
7884 	struct hwrm_func_qcfg_input *req;
7885 	int rc;
7886 
7887 	if (bp->hwrm_spec_code < 0x10601)
7888 		return 0;
7889 
7890 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7891 	if (rc)
7892 		return rc;
7893 
7894 	req->fid = cpu_to_le16(fid);
7895 	resp = hwrm_req_hold(bp, req);
7896 	rc = hwrm_req_send(bp, req);
7897 	if (!rc)
7898 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7899 
7900 	hwrm_req_drop(bp, req);
7901 	return rc;
7902 }
7903 
7904 static bool bnxt_rfs_supported(struct bnxt *bp);
7905 
7906 static struct hwrm_func_cfg_input *
7907 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7908 {
7909 	struct hwrm_func_cfg_input *req;
7910 	u32 enables = 0;
7911 
7912 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7913 		return NULL;
7914 
7915 	req->fid = cpu_to_le16(0xffff);
7916 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7917 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7918 	if (BNXT_NEW_RM(bp)) {
7919 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7920 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7921 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7922 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7923 			enables |= hwr->cp_p5 ?
7924 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7925 		} else {
7926 			enables |= hwr->cp ?
7927 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7928 			enables |= hwr->grp ?
7929 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7930 		}
7931 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7932 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7933 					  0;
7934 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7935 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7936 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7937 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7938 			req->num_msix = cpu_to_le16(hwr->cp);
7939 		} else {
7940 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7941 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7942 		}
7943 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7944 		req->num_vnics = cpu_to_le16(hwr->vnic);
7945 	}
7946 	req->enables = cpu_to_le32(enables);
7947 	return req;
7948 }
7949 
7950 static struct hwrm_func_vf_cfg_input *
7951 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7952 {
7953 	struct hwrm_func_vf_cfg_input *req;
7954 	u32 enables = 0;
7955 
7956 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7957 		return NULL;
7958 
7959 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7960 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7961 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7962 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7963 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7964 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7965 		enables |= hwr->cp_p5 ?
7966 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7967 	} else {
7968 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7969 		enables |= hwr->grp ?
7970 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7971 	}
7972 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7973 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7974 
7975 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7976 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7977 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7978 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7979 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7980 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7981 	} else {
7982 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7983 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7984 	}
7985 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7986 	req->num_vnics = cpu_to_le16(hwr->vnic);
7987 
7988 	req->enables = cpu_to_le32(enables);
7989 	return req;
7990 }
7991 
7992 static int
7993 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7994 {
7995 	struct hwrm_func_cfg_input *req;
7996 	int rc;
7997 
7998 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7999 	if (!req)
8000 		return -ENOMEM;
8001 
8002 	if (!req->enables) {
8003 		hwrm_req_drop(bp, req);
8004 		return 0;
8005 	}
8006 
8007 	rc = hwrm_req_send(bp, req);
8008 	if (rc)
8009 		return rc;
8010 
8011 	if (bp->hwrm_spec_code < 0x10601)
8012 		bp->hw_resc.resv_tx_rings = hwr->tx;
8013 
8014 	return bnxt_hwrm_get_rings(bp);
8015 }
8016 
8017 static int
8018 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8019 {
8020 	struct hwrm_func_vf_cfg_input *req;
8021 	int rc;
8022 
8023 	if (!BNXT_NEW_RM(bp)) {
8024 		bp->hw_resc.resv_tx_rings = hwr->tx;
8025 		return 0;
8026 	}
8027 
8028 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8029 	if (!req)
8030 		return -ENOMEM;
8031 
8032 	rc = hwrm_req_send(bp, req);
8033 	if (rc)
8034 		return rc;
8035 
8036 	return bnxt_hwrm_get_rings(bp);
8037 }
8038 
8039 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8040 {
8041 	if (BNXT_PF(bp))
8042 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
8043 	else
8044 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
8045 }
8046 
8047 int bnxt_nq_rings_in_use(struct bnxt *bp)
8048 {
8049 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
8050 }
8051 
8052 static int bnxt_cp_rings_in_use(struct bnxt *bp)
8053 {
8054 	int cp;
8055 
8056 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8057 		return bnxt_nq_rings_in_use(bp);
8058 
8059 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
8060 	return cp;
8061 }
8062 
8063 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
8064 {
8065 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
8066 }
8067 
8068 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8069 {
8070 	if (!hwr->grp)
8071 		return 0;
8072 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8073 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
8074 
8075 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
8076 			rss_ctx *= hwr->vnic;
8077 		return rss_ctx;
8078 	}
8079 	if (BNXT_VF(bp))
8080 		return BNXT_VF_MAX_RSS_CTX;
8081 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
8082 		return hwr->grp + 1;
8083 	return 1;
8084 }
8085 
8086 /* Check if a default RSS map needs to be setup.  This function is only
8087  * used on older firmware that does not require reserving RX rings.
8088  */
8089 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
8090 {
8091 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8092 
8093 	/* The RSS map is valid for RX rings set to resv_rx_rings */
8094 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
8095 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
8096 		if (!netif_is_rxfh_configured(bp->dev))
8097 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8098 	}
8099 }
8100 
8101 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
8102 {
8103 	if (bp->flags & BNXT_FLAG_RFS) {
8104 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
8105 			return 2 + bp->num_rss_ctx;
8106 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8107 			return rx_rings + 1;
8108 	}
8109 	return 1;
8110 }
8111 
8112 static void bnxt_get_total_resources(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8113 {
8114 	hwr->cp = bnxt_nq_rings_in_use(bp);
8115 	hwr->cp_p5 = 0;
8116 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8117 		hwr->cp_p5 = bnxt_cp_rings_in_use(bp);
8118 	hwr->tx = bp->tx_nr_rings;
8119 	hwr->rx = bp->rx_nr_rings;
8120 	hwr->grp = hwr->rx;
8121 	hwr->vnic = bnxt_get_total_vnics(bp, hwr->rx);
8122 	hwr->rss_ctx = bnxt_get_total_rss_ctxs(bp, hwr);
8123 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8124 		hwr->rx <<= 1;
8125 	hwr->stat = bnxt_get_func_stat_ctxs(bp);
8126 }
8127 
8128 static bool bnxt_need_reserve_rings(struct bnxt *bp)
8129 {
8130 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8131 	struct bnxt_hw_rings hwr;
8132 
8133 	bnxt_get_total_resources(bp, &hwr);
8134 
8135 	/* Old firmware does not need RX ring reservations but we still
8136 	 * need to setup a default RSS map when needed.  With new firmware
8137 	 * we go through RX ring reservations first and then set up the
8138 	 * RSS map for the successfully reserved RX rings when needed.
8139 	 */
8140 	if (!BNXT_NEW_RM(bp))
8141 		bnxt_check_rss_tbl_no_rmgr(bp);
8142 
8143 	if (hw_resc->resv_tx_rings != hwr.tx && bp->hwrm_spec_code >= 0x10601)
8144 		return true;
8145 
8146 	if (!BNXT_NEW_RM(bp))
8147 		return false;
8148 
8149 	if (hw_resc->resv_rx_rings != hwr.rx ||
8150 	    hw_resc->resv_vnics != hwr.vnic ||
8151 	    hw_resc->resv_stat_ctxs != hwr.stat ||
8152 	    hw_resc->resv_rsscos_ctxs != hwr.rss_ctx ||
8153 	    (hw_resc->resv_hw_ring_grps != hwr.grp &&
8154 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
8155 		return true;
8156 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8157 		if (hw_resc->resv_cp_rings != hwr.cp_p5)
8158 			return true;
8159 	} else if (hw_resc->resv_cp_rings != hwr.cp) {
8160 		return true;
8161 	}
8162 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
8163 	    hw_resc->resv_irqs != hwr.cp)
8164 		return true;
8165 	return false;
8166 }
8167 
8168 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8169 {
8170 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8171 
8172 	hwr->tx = hw_resc->resv_tx_rings;
8173 	if (BNXT_NEW_RM(bp)) {
8174 		hwr->rx = hw_resc->resv_rx_rings;
8175 		hwr->cp = hw_resc->resv_irqs;
8176 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8177 			hwr->cp_p5 = hw_resc->resv_cp_rings;
8178 		hwr->grp = hw_resc->resv_hw_ring_grps;
8179 		hwr->vnic = hw_resc->resv_vnics;
8180 		hwr->stat = hw_resc->resv_stat_ctxs;
8181 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
8182 	}
8183 }
8184 
8185 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8186 {
8187 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
8188 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
8189 }
8190 
8191 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
8192 
8193 static int __bnxt_reserve_rings(struct bnxt *bp)
8194 {
8195 	struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA];
8196 	struct bnxt_hw_rings hwr = {0};
8197 	int rx_rings, old_rx_rings, rc;
8198 	int cp = bp->cp_nr_rings;
8199 	int ulp_msix = 0;
8200 	bool sh = false;
8201 	int tx_cp;
8202 
8203 	if (!bnxt_need_reserve_rings(bp))
8204 		return 0;
8205 
8206 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(edev)) {
8207 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
8208 		if (!ulp_msix)
8209 			bnxt_set_ulp_stat_ctxs(bp, 0);
8210 		else
8211 			bnxt_set_dflt_ulp_stat_ctxs(bp);
8212 
8213 		if (ulp_msix > bp->ulp_num_msix_want)
8214 			ulp_msix = bp->ulp_num_msix_want;
8215 		hwr.cp = cp + ulp_msix;
8216 	} else {
8217 		hwr.cp = bnxt_nq_rings_in_use(bp);
8218 	}
8219 
8220 	hwr.tx = bp->tx_nr_rings;
8221 	hwr.rx = bp->rx_nr_rings;
8222 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8223 		sh = true;
8224 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8225 		hwr.cp_p5 = hwr.rx + hwr.tx;
8226 
8227 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
8228 
8229 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8230 		hwr.rx <<= 1;
8231 	hwr.grp = bp->rx_nr_rings;
8232 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
8233 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
8234 	old_rx_rings = bp->hw_resc.resv_rx_rings;
8235 
8236 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
8237 	if (rc)
8238 		return rc;
8239 
8240 	bnxt_copy_reserved_rings(bp, &hwr);
8241 
8242 	rx_rings = hwr.rx;
8243 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8244 		if (hwr.rx >= 2) {
8245 			rx_rings = hwr.rx >> 1;
8246 		} else {
8247 			if (netif_running(bp->dev))
8248 				return -ENOMEM;
8249 
8250 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
8251 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
8252 			bp->dev->hw_features &= ~NETIF_F_LRO;
8253 			bp->dev->features &= ~NETIF_F_LRO;
8254 			bnxt_set_ring_params(bp);
8255 		}
8256 	}
8257 	rx_rings = min_t(int, rx_rings, hwr.grp);
8258 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
8259 	if (bnxt_ulp_registered(edev) && hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
8260 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
8261 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
8262 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
8263 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8264 		hwr.rx = rx_rings << 1;
8265 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
8266 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
8267 	if (hwr.tx != bp->tx_nr_rings) {
8268 		netdev_warn(bp->dev,
8269 			    "Able to reserve only %d out of %d requested TX rings\n",
8270 			    hwr.tx, bp->tx_nr_rings);
8271 	}
8272 	bp->tx_nr_rings = hwr.tx;
8273 
8274 	/* If we cannot reserve all the RX rings, reset the RSS map only
8275 	 * if absolutely necessary
8276 	 */
8277 	if (rx_rings != bp->rx_nr_rings) {
8278 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
8279 			    rx_rings, bp->rx_nr_rings);
8280 		if (netif_is_rxfh_configured(bp->dev) &&
8281 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
8282 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
8283 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
8284 			ethtool_rxfh_indir_lost(bp->dev);
8285 		}
8286 	}
8287 	bp->rx_nr_rings = rx_rings;
8288 	bp->cp_nr_rings = hwr.cp;
8289 
8290 	/* Fall back if we cannot reserve enough HW RSS contexts */
8291 	if ((bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX) &&
8292 	    hwr.rss_ctx < bnxt_get_total_rss_ctxs(bp, &hwr))
8293 		bp->rss_cap &= ~BNXT_RSS_CAP_LARGE_RSS_CTX;
8294 
8295 	if (!bnxt_rings_ok(bp, &hwr))
8296 		return -ENOMEM;
8297 
8298 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
8299 	    !netif_is_rxfh_configured(bp->dev))
8300 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8301 
8302 	if (!bnxt_ulp_registered(edev) && BNXT_NEW_RM(bp)) {
8303 		int resv_msix, resv_ctx, ulp_ctxs;
8304 		struct bnxt_hw_resc *hw_resc;
8305 
8306 		hw_resc = &bp->hw_resc;
8307 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
8308 		ulp_msix = min_t(int, resv_msix, ulp_msix);
8309 		bnxt_set_ulp_msix_num(bp, ulp_msix);
8310 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8311 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8312 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8313 	}
8314 
8315 	return rc;
8316 }
8317 
8318 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8319 {
8320 	struct hwrm_func_vf_cfg_input *req;
8321 	u32 flags;
8322 
8323 	if (!BNXT_NEW_RM(bp))
8324 		return 0;
8325 
8326 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8327 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8328 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8329 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8330 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8331 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8332 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8333 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8334 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8335 
8336 	req->flags = cpu_to_le32(flags);
8337 	return hwrm_req_send_silent(bp, req);
8338 }
8339 
8340 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8341 {
8342 	struct hwrm_func_cfg_input *req;
8343 	u32 flags;
8344 
8345 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8346 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8347 	if (BNXT_NEW_RM(bp)) {
8348 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8349 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8350 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8351 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8352 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8353 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8354 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8355 		else
8356 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8357 	}
8358 
8359 	req->flags = cpu_to_le32(flags);
8360 	return hwrm_req_send_silent(bp, req);
8361 }
8362 
8363 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8364 {
8365 	if (bp->hwrm_spec_code < 0x10801)
8366 		return 0;
8367 
8368 	if (BNXT_PF(bp))
8369 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8370 
8371 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8372 }
8373 
8374 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8375 {
8376 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8377 	struct hwrm_ring_aggint_qcaps_output *resp;
8378 	struct hwrm_ring_aggint_qcaps_input *req;
8379 	int rc;
8380 
8381 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8382 	coal_cap->num_cmpl_dma_aggr_max = 63;
8383 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8384 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8385 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8386 	coal_cap->int_lat_tmr_min_max = 65535;
8387 	coal_cap->int_lat_tmr_max_max = 65535;
8388 	coal_cap->num_cmpl_aggr_int_max = 65535;
8389 	coal_cap->timer_units = 80;
8390 
8391 	if (bp->hwrm_spec_code < 0x10902)
8392 		return;
8393 
8394 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8395 		return;
8396 
8397 	resp = hwrm_req_hold(bp, req);
8398 	rc = hwrm_req_send_silent(bp, req);
8399 	if (!rc) {
8400 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8401 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8402 		coal_cap->num_cmpl_dma_aggr_max =
8403 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8404 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8405 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8406 		coal_cap->cmpl_aggr_dma_tmr_max =
8407 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8408 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8409 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8410 		coal_cap->int_lat_tmr_min_max =
8411 			le16_to_cpu(resp->int_lat_tmr_min_max);
8412 		coal_cap->int_lat_tmr_max_max =
8413 			le16_to_cpu(resp->int_lat_tmr_max_max);
8414 		coal_cap->num_cmpl_aggr_int_max =
8415 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8416 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8417 	}
8418 	hwrm_req_drop(bp, req);
8419 }
8420 
8421 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8422 {
8423 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8424 
8425 	return usec * 1000 / coal_cap->timer_units;
8426 }
8427 
8428 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8429 	struct bnxt_coal *hw_coal,
8430 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8431 {
8432 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8433 	u16 val, tmr, max, flags = hw_coal->flags;
8434 	u32 cmpl_params = coal_cap->cmpl_params;
8435 
8436 	max = hw_coal->bufs_per_record * 128;
8437 	if (hw_coal->budget)
8438 		max = hw_coal->bufs_per_record * hw_coal->budget;
8439 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8440 
8441 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8442 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8443 
8444 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8445 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8446 
8447 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8448 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8449 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8450 
8451 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8452 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8453 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8454 
8455 	/* min timer set to 1/2 of interrupt timer */
8456 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8457 		val = tmr / 2;
8458 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8459 		req->int_lat_tmr_min = cpu_to_le16(val);
8460 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8461 	}
8462 
8463 	/* buf timer set to 1/4 of interrupt timer */
8464 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8465 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8466 
8467 	if (cmpl_params &
8468 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8469 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8470 		val = clamp_t(u16, tmr, 1,
8471 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8472 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8473 		req->enables |=
8474 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8475 	}
8476 
8477 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8478 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8479 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8480 	req->flags = cpu_to_le16(flags);
8481 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8482 }
8483 
8484 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8485 				   struct bnxt_coal *hw_coal)
8486 {
8487 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8488 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8489 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8490 	u32 nq_params = coal_cap->nq_params;
8491 	u16 tmr;
8492 	int rc;
8493 
8494 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8495 		return 0;
8496 
8497 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8498 	if (rc)
8499 		return rc;
8500 
8501 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8502 	req->flags =
8503 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8504 
8505 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8506 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8507 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8508 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8509 	return hwrm_req_send(bp, req);
8510 }
8511 
8512 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8513 {
8514 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8515 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8516 	struct bnxt_coal coal;
8517 	int rc;
8518 
8519 	/* Tick values in micro seconds.
8520 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8521 	 */
8522 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8523 
8524 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8525 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8526 
8527 	if (!bnapi->rx_ring)
8528 		return -ENODEV;
8529 
8530 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8531 	if (rc)
8532 		return rc;
8533 
8534 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8535 
8536 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8537 
8538 	return hwrm_req_send(bp, req_rx);
8539 }
8540 
8541 static int
8542 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8543 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8544 {
8545 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8546 
8547 	req->ring_id = cpu_to_le16(ring_id);
8548 	return hwrm_req_send(bp, req);
8549 }
8550 
8551 static int
8552 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8553 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8554 {
8555 	struct bnxt_tx_ring_info *txr;
8556 	int i, rc;
8557 
8558 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8559 		u16 ring_id;
8560 
8561 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8562 		req->ring_id = cpu_to_le16(ring_id);
8563 		rc = hwrm_req_send(bp, req);
8564 		if (rc)
8565 			return rc;
8566 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8567 			return 0;
8568 	}
8569 	return 0;
8570 }
8571 
8572 int bnxt_hwrm_set_coal(struct bnxt *bp)
8573 {
8574 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8575 	int i, rc;
8576 
8577 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8578 	if (rc)
8579 		return rc;
8580 
8581 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8582 	if (rc) {
8583 		hwrm_req_drop(bp, req_rx);
8584 		return rc;
8585 	}
8586 
8587 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8588 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8589 
8590 	hwrm_req_hold(bp, req_rx);
8591 	hwrm_req_hold(bp, req_tx);
8592 	for (i = 0; i < bp->cp_nr_rings; i++) {
8593 		struct bnxt_napi *bnapi = bp->bnapi[i];
8594 		struct bnxt_coal *hw_coal;
8595 
8596 		if (!bnapi->rx_ring)
8597 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8598 		else
8599 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8600 		if (rc)
8601 			break;
8602 
8603 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8604 			continue;
8605 
8606 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8607 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8608 			if (rc)
8609 				break;
8610 		}
8611 		if (bnapi->rx_ring)
8612 			hw_coal = &bp->rx_coal;
8613 		else
8614 			hw_coal = &bp->tx_coal;
8615 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8616 	}
8617 	hwrm_req_drop(bp, req_rx);
8618 	hwrm_req_drop(bp, req_tx);
8619 	return rc;
8620 }
8621 
8622 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8623 {
8624 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8625 	struct hwrm_stat_ctx_free_input *req;
8626 	int i;
8627 
8628 	if (!bp->bnapi)
8629 		return;
8630 
8631 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8632 		return;
8633 
8634 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8635 		return;
8636 	if (BNXT_FW_MAJ(bp) <= 20) {
8637 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8638 			hwrm_req_drop(bp, req);
8639 			return;
8640 		}
8641 		hwrm_req_hold(bp, req0);
8642 	}
8643 	hwrm_req_hold(bp, req);
8644 	for (i = 0; i < bp->cp_nr_rings; i++) {
8645 		struct bnxt_napi *bnapi = bp->bnapi[i];
8646 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8647 
8648 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8649 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8650 			if (req0) {
8651 				req0->stat_ctx_id = req->stat_ctx_id;
8652 				hwrm_req_send(bp, req0);
8653 			}
8654 			hwrm_req_send(bp, req);
8655 
8656 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8657 		}
8658 	}
8659 	hwrm_req_drop(bp, req);
8660 	if (req0)
8661 		hwrm_req_drop(bp, req0);
8662 }
8663 
8664 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8665 {
8666 	struct hwrm_stat_ctx_alloc_output *resp;
8667 	struct hwrm_stat_ctx_alloc_input *req;
8668 	int rc, i;
8669 
8670 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8671 		return 0;
8672 
8673 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8674 	if (rc)
8675 		return rc;
8676 
8677 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8678 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8679 
8680 	resp = hwrm_req_hold(bp, req);
8681 	for (i = 0; i < bp->cp_nr_rings; i++) {
8682 		struct bnxt_napi *bnapi = bp->bnapi[i];
8683 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8684 
8685 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8686 
8687 		rc = hwrm_req_send(bp, req);
8688 		if (rc)
8689 			break;
8690 
8691 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8692 
8693 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8694 	}
8695 	hwrm_req_drop(bp, req);
8696 	return rc;
8697 }
8698 
8699 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8700 {
8701 	struct hwrm_func_qcfg_output *resp;
8702 	struct hwrm_func_qcfg_input *req;
8703 	u16 flags;
8704 	int rc;
8705 
8706 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8707 	if (rc)
8708 		return rc;
8709 
8710 	req->fid = cpu_to_le16(0xffff);
8711 	resp = hwrm_req_hold(bp, req);
8712 	rc = hwrm_req_send(bp, req);
8713 	if (rc)
8714 		goto func_qcfg_exit;
8715 
8716 	flags = le16_to_cpu(resp->flags);
8717 #ifdef CONFIG_BNXT_SRIOV
8718 	if (BNXT_VF(bp)) {
8719 		struct bnxt_vf_info *vf = &bp->vf;
8720 
8721 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8722 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8723 			vf->flags |= BNXT_VF_TRUST;
8724 		else
8725 			vf->flags &= ~BNXT_VF_TRUST;
8726 	} else {
8727 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8728 	}
8729 #endif
8730 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8731 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8732 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8733 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8734 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8735 	}
8736 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8737 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8738 
8739 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8740 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8741 
8742 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8743 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8744 	if (resp->roce_bidi_opt_mode &
8745 	    FUNC_QCFG_RESP_ROCE_BIDI_OPT_MODE_DEDICATED)
8746 		bp->cos0_cos1_shared = 1;
8747 	else
8748 		bp->cos0_cos1_shared = 0;
8749 
8750 	switch (resp->port_partition_type) {
8751 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8752 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8753 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8754 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8755 		bp->port_partition_type = resp->port_partition_type;
8756 		break;
8757 	}
8758 	if (bp->hwrm_spec_code < 0x10707 ||
8759 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8760 		bp->br_mode = BRIDGE_MODE_VEB;
8761 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8762 		bp->br_mode = BRIDGE_MODE_VEPA;
8763 	else
8764 		bp->br_mode = BRIDGE_MODE_UNDEF;
8765 
8766 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8767 	if (!bp->max_mtu)
8768 		bp->max_mtu = BNXT_MAX_MTU;
8769 
8770 	if (bp->db_size)
8771 		goto func_qcfg_exit;
8772 
8773 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8774 	if (BNXT_CHIP_P5(bp)) {
8775 		if (BNXT_PF(bp))
8776 			bp->db_offset = DB_PF_OFFSET_P5;
8777 		else
8778 			bp->db_offset = DB_VF_OFFSET_P5;
8779 	}
8780 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8781 				 1024);
8782 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8783 	    bp->db_size <= bp->db_offset)
8784 		bp->db_size = pci_resource_len(bp->pdev, 2);
8785 
8786 func_qcfg_exit:
8787 	hwrm_req_drop(bp, req);
8788 	return rc;
8789 }
8790 
8791 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8792 				      u8 init_val, u8 init_offset,
8793 				      bool init_mask_set)
8794 {
8795 	ctxm->init_value = init_val;
8796 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8797 	if (init_mask_set)
8798 		ctxm->init_offset = init_offset * 4;
8799 	else
8800 		ctxm->init_value = 0;
8801 }
8802 
8803 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8804 {
8805 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8806 	u16 type;
8807 
8808 	for (type = 0; type < ctx_max; type++) {
8809 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8810 		int n = 1;
8811 
8812 		if (!ctxm->max_entries || ctxm->pg_info)
8813 			continue;
8814 
8815 		if (ctxm->instance_bmap)
8816 			n = hweight32(ctxm->instance_bmap);
8817 		ctxm->pg_info = kzalloc_objs(*ctxm->pg_info, n);
8818 		if (!ctxm->pg_info)
8819 			return -ENOMEM;
8820 	}
8821 	return 0;
8822 }
8823 
8824 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8825 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8826 
8827 #define BNXT_CTX_INIT_VALID(flags)	\
8828 	(!!((flags) &			\
8829 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8830 
8831 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8832 {
8833 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8834 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8835 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8836 	u16 type, next_type = 0;
8837 	int rc;
8838 
8839 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8840 	if (rc)
8841 		return rc;
8842 
8843 	if (!ctx) {
8844 		ctx = kzalloc_obj(*ctx);
8845 		if (!ctx)
8846 			return -ENOMEM;
8847 		bp->ctx = ctx;
8848 	}
8849 
8850 	resp = hwrm_req_hold(bp, req);
8851 
8852 	for (type = 0; type < BNXT_CTX_V2_MAX; type = next_type) {
8853 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8854 		u8 init_val, init_off, i;
8855 		u32 max_entries;
8856 		u16 entry_size;
8857 		__le32 *p;
8858 		u32 flags;
8859 
8860 		req->type = cpu_to_le16(type);
8861 		rc = hwrm_req_send(bp, req);
8862 		if (rc)
8863 			goto ctx_done;
8864 		flags = le32_to_cpu(resp->flags);
8865 		next_type = le16_to_cpu(resp->next_valid_type);
8866 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8867 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8868 			continue;
8869 		}
8870 		entry_size = le16_to_cpu(resp->entry_size);
8871 		max_entries = le32_to_cpu(resp->max_num_entries);
8872 		if (ctxm->mem_valid) {
8873 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8874 			    ctxm->entry_size != entry_size ||
8875 			    ctxm->max_entries != max_entries)
8876 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8877 			else
8878 				continue;
8879 		}
8880 		ctxm->type = type;
8881 		ctxm->entry_size = entry_size;
8882 		ctxm->flags = flags;
8883 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8884 		ctxm->entry_multiple = resp->entry_multiple;
8885 		ctxm->max_entries = max_entries;
8886 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8887 		init_val = resp->ctx_init_value;
8888 		init_off = resp->ctx_init_offset;
8889 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8890 					  BNXT_CTX_INIT_VALID(flags));
8891 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8892 					      BNXT_MAX_SPLIT_ENTRY);
8893 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8894 		     i++, p++)
8895 			ctxm->split[i] = le32_to_cpu(*p);
8896 	}
8897 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8898 
8899 ctx_done:
8900 	hwrm_req_drop(bp, req);
8901 	return rc;
8902 }
8903 
8904 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8905 {
8906 	struct hwrm_func_backing_store_qcaps_output *resp;
8907 	struct hwrm_func_backing_store_qcaps_input *req;
8908 	int rc;
8909 
8910 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8911 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8912 		return 0;
8913 
8914 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8915 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8916 
8917 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8918 	if (rc)
8919 		return rc;
8920 
8921 	resp = hwrm_req_hold(bp, req);
8922 	rc = hwrm_req_send_silent(bp, req);
8923 	if (!rc) {
8924 		struct bnxt_ctx_mem_type *ctxm;
8925 		struct bnxt_ctx_mem_info *ctx;
8926 		u8 init_val, init_idx = 0;
8927 		u16 init_mask;
8928 
8929 		ctx = bp->ctx;
8930 		if (!ctx) {
8931 			ctx = kzalloc_obj(*ctx);
8932 			if (!ctx) {
8933 				rc = -ENOMEM;
8934 				goto ctx_err;
8935 			}
8936 			bp->ctx = ctx;
8937 		}
8938 		init_val = resp->ctx_kind_initializer;
8939 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8940 
8941 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8942 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8943 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8944 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8945 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8946 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8947 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8948 					  (init_mask & (1 << init_idx++)) != 0);
8949 
8950 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8951 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8952 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8953 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8954 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8955 					  (init_mask & (1 << init_idx++)) != 0);
8956 
8957 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8958 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8959 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8960 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8961 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8962 					  (init_mask & (1 << init_idx++)) != 0);
8963 
8964 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8965 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8966 		ctxm->max_entries = ctxm->vnic_entries +
8967 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8968 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8969 		bnxt_init_ctx_initializer(ctxm, init_val,
8970 					  resp->vnic_init_offset,
8971 					  (init_mask & (1 << init_idx++)) != 0);
8972 
8973 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8974 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8975 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8976 		bnxt_init_ctx_initializer(ctxm, init_val,
8977 					  resp->stat_init_offset,
8978 					  (init_mask & (1 << init_idx++)) != 0);
8979 
8980 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8981 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8982 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8983 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8984 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8985 		if (!ctxm->entry_multiple)
8986 			ctxm->entry_multiple = 1;
8987 
8988 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8989 
8990 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8991 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8992 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8993 		ctxm->mrav_num_entries_units =
8994 			le16_to_cpu(resp->mrav_num_entries_units);
8995 		bnxt_init_ctx_initializer(ctxm, init_val,
8996 					  resp->mrav_init_offset,
8997 					  (init_mask & (1 << init_idx++)) != 0);
8998 
8999 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9000 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
9001 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
9002 
9003 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
9004 		if (!ctx->tqm_fp_rings_count)
9005 			ctx->tqm_fp_rings_count = bp->max_q;
9006 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
9007 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
9008 
9009 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9010 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
9011 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
9012 
9013 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
9014 	} else {
9015 		rc = 0;
9016 	}
9017 ctx_err:
9018 	hwrm_req_drop(bp, req);
9019 	return rc;
9020 }
9021 
9022 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
9023 				  __le64 *pg_dir)
9024 {
9025 	if (!rmem->nr_pages)
9026 		return;
9027 
9028 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
9029 	if (rmem->depth >= 1) {
9030 		if (rmem->depth == 2)
9031 			*pg_attr |= 2;
9032 		else
9033 			*pg_attr |= 1;
9034 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
9035 	} else {
9036 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
9037 	}
9038 }
9039 
9040 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
9041 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
9042 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
9043 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
9044 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
9045 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
9046 
9047 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
9048 {
9049 	struct hwrm_func_backing_store_cfg_input *req;
9050 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9051 	struct bnxt_ctx_pg_info *ctx_pg;
9052 	struct bnxt_ctx_mem_type *ctxm;
9053 	void **__req = (void **)&req;
9054 	u32 req_len = sizeof(*req);
9055 	__le32 *num_entries;
9056 	__le64 *pg_dir;
9057 	u32 flags = 0;
9058 	u8 *pg_attr;
9059 	u32 ena;
9060 	int rc;
9061 	int i;
9062 
9063 	if (!ctx)
9064 		return 0;
9065 
9066 	if (req_len > bp->hwrm_max_ext_req_len)
9067 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
9068 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
9069 	if (rc)
9070 		return rc;
9071 
9072 	req->enables = cpu_to_le32(enables);
9073 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
9074 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9075 		ctx_pg = ctxm->pg_info;
9076 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
9077 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
9078 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
9079 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
9080 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9081 				      &req->qpc_pg_size_qpc_lvl,
9082 				      &req->qpc_page_dir);
9083 
9084 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
9085 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
9086 	}
9087 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
9088 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9089 		ctx_pg = ctxm->pg_info;
9090 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
9091 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
9092 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
9093 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9094 				      &req->srq_pg_size_srq_lvl,
9095 				      &req->srq_page_dir);
9096 	}
9097 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
9098 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9099 		ctx_pg = ctxm->pg_info;
9100 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
9101 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
9102 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
9103 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9104 				      &req->cq_pg_size_cq_lvl,
9105 				      &req->cq_page_dir);
9106 	}
9107 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
9108 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9109 		ctx_pg = ctxm->pg_info;
9110 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
9111 		req->vnic_num_ring_table_entries =
9112 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
9113 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
9114 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9115 				      &req->vnic_pg_size_vnic_lvl,
9116 				      &req->vnic_page_dir);
9117 	}
9118 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
9119 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9120 		ctx_pg = ctxm->pg_info;
9121 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
9122 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
9123 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9124 				      &req->stat_pg_size_stat_lvl,
9125 				      &req->stat_page_dir);
9126 	}
9127 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
9128 		u32 units;
9129 
9130 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9131 		ctx_pg = ctxm->pg_info;
9132 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
9133 		units = ctxm->mrav_num_entries_units;
9134 		if (units) {
9135 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
9136 			u32 entries;
9137 
9138 			num_mr = ctx_pg->entries - num_ah;
9139 			entries = ((num_mr / units) << 16) | (num_ah / units);
9140 			req->mrav_num_entries = cpu_to_le32(entries);
9141 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
9142 		}
9143 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
9144 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9145 				      &req->mrav_pg_size_mrav_lvl,
9146 				      &req->mrav_page_dir);
9147 	}
9148 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
9149 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9150 		ctx_pg = ctxm->pg_info;
9151 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
9152 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
9153 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9154 				      &req->tim_pg_size_tim_lvl,
9155 				      &req->tim_page_dir);
9156 	}
9157 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9158 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
9159 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
9160 	     pg_dir = &req->tqm_sp_page_dir,
9161 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
9162 	     ctx_pg = ctxm->pg_info;
9163 	     i < BNXT_MAX_TQM_RINGS;
9164 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
9165 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
9166 		if (!(enables & ena))
9167 			continue;
9168 
9169 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
9170 		*num_entries = cpu_to_le32(ctx_pg->entries);
9171 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
9172 	}
9173 	req->flags = cpu_to_le32(flags);
9174 	return hwrm_req_send(bp, req);
9175 }
9176 
9177 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
9178 				  struct bnxt_ctx_pg_info *ctx_pg)
9179 {
9180 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9181 
9182 	rmem->page_size = BNXT_PAGE_SIZE;
9183 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
9184 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
9185 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
9186 	if (rmem->depth >= 1)
9187 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
9188 	return bnxt_alloc_ring(bp, rmem);
9189 }
9190 
9191 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
9192 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
9193 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
9194 {
9195 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9196 	int rc;
9197 
9198 	if (!mem_size)
9199 		return -EINVAL;
9200 
9201 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9202 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
9203 		ctx_pg->nr_pages = 0;
9204 		return -EINVAL;
9205 	}
9206 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
9207 		int nr_tbls, i;
9208 
9209 		rmem->depth = 2;
9210 		ctx_pg->ctx_pg_tbl = kzalloc_objs(ctx_pg, MAX_CTX_PAGES);
9211 		if (!ctx_pg->ctx_pg_tbl)
9212 			return -ENOMEM;
9213 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
9214 		rmem->nr_pages = nr_tbls;
9215 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9216 		if (rc)
9217 			return rc;
9218 		for (i = 0; i < nr_tbls; i++) {
9219 			struct bnxt_ctx_pg_info *pg_tbl;
9220 
9221 			pg_tbl = kzalloc_obj(*pg_tbl);
9222 			if (!pg_tbl)
9223 				return -ENOMEM;
9224 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
9225 			rmem = &pg_tbl->ring_mem;
9226 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
9227 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
9228 			rmem->depth = 1;
9229 			rmem->nr_pages = MAX_CTX_PAGES;
9230 			rmem->ctx_mem = ctxm;
9231 			if (i == (nr_tbls - 1)) {
9232 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
9233 
9234 				if (rem)
9235 					rmem->nr_pages = rem;
9236 			}
9237 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
9238 			if (rc)
9239 				break;
9240 		}
9241 	} else {
9242 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9243 		if (rmem->nr_pages > 1 || depth)
9244 			rmem->depth = 1;
9245 		rmem->ctx_mem = ctxm;
9246 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9247 	}
9248 	return rc;
9249 }
9250 
9251 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
9252 				    struct bnxt_ctx_pg_info *ctx_pg,
9253 				    void *buf, size_t offset, size_t head,
9254 				    size_t tail)
9255 {
9256 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9257 	size_t nr_pages = ctx_pg->nr_pages;
9258 	int page_size = rmem->page_size;
9259 	size_t len = 0, total_len = 0;
9260 	u16 depth = rmem->depth;
9261 
9262 	tail %= nr_pages * page_size;
9263 	do {
9264 		if (depth > 1) {
9265 			int i = head / (page_size * MAX_CTX_PAGES);
9266 			struct bnxt_ctx_pg_info *pg_tbl;
9267 
9268 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9269 			rmem = &pg_tbl->ring_mem;
9270 		}
9271 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
9272 		head += len;
9273 		offset += len;
9274 		total_len += len;
9275 		if (head >= nr_pages * page_size)
9276 			head = 0;
9277 	} while (head != tail);
9278 	return total_len;
9279 }
9280 
9281 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
9282 				  struct bnxt_ctx_pg_info *ctx_pg)
9283 {
9284 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9285 
9286 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
9287 	    ctx_pg->ctx_pg_tbl) {
9288 		int i, nr_tbls = rmem->nr_pages;
9289 
9290 		for (i = 0; i < nr_tbls; i++) {
9291 			struct bnxt_ctx_pg_info *pg_tbl;
9292 			struct bnxt_ring_mem_info *rmem2;
9293 
9294 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9295 			if (!pg_tbl)
9296 				continue;
9297 			rmem2 = &pg_tbl->ring_mem;
9298 			bnxt_free_ring(bp, rmem2);
9299 			ctx_pg->ctx_pg_arr[i] = NULL;
9300 			kfree(pg_tbl);
9301 			ctx_pg->ctx_pg_tbl[i] = NULL;
9302 		}
9303 		kfree(ctx_pg->ctx_pg_tbl);
9304 		ctx_pg->ctx_pg_tbl = NULL;
9305 	}
9306 	bnxt_free_ring(bp, rmem);
9307 	ctx_pg->nr_pages = 0;
9308 }
9309 
9310 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
9311 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
9312 				   u8 pg_lvl)
9313 {
9314 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9315 	int i, rc = 0, n = 1;
9316 	u32 mem_size;
9317 
9318 	if (!ctxm->entry_size || !ctx_pg)
9319 		return -EINVAL;
9320 	if (ctxm->instance_bmap)
9321 		n = hweight32(ctxm->instance_bmap);
9322 	if (ctxm->entry_multiple)
9323 		entries = roundup(entries, ctxm->entry_multiple);
9324 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9325 	mem_size = entries * ctxm->entry_size;
9326 	for (i = 0; i < n && !rc; i++) {
9327 		ctx_pg[i].entries = entries;
9328 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9329 					    ctxm->init_value ? ctxm : NULL);
9330 	}
9331 	if (!rc)
9332 		ctxm->mem_valid = 1;
9333 	return rc;
9334 }
9335 
9336 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9337 					       struct bnxt_ctx_mem_type *ctxm,
9338 					       bool last)
9339 {
9340 	struct hwrm_func_backing_store_cfg_v2_input *req;
9341 	u32 instance_bmap = ctxm->instance_bmap;
9342 	int i, j, rc = 0, n = 1;
9343 	__le32 *p;
9344 
9345 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9346 		return 0;
9347 
9348 	if (instance_bmap)
9349 		n = hweight32(ctxm->instance_bmap);
9350 	else
9351 		instance_bmap = 1;
9352 
9353 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9354 	if (rc)
9355 		return rc;
9356 	hwrm_req_hold(bp, req);
9357 	req->type = cpu_to_le16(ctxm->type);
9358 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9359 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9360 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9361 		struct bnxt_bs_trace_info *bs_trace;
9362 		u32 enables;
9363 
9364 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9365 		req->enables = cpu_to_le32(enables);
9366 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9367 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9368 	}
9369 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9370 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9371 		p[i] = cpu_to_le32(ctxm->split[i]);
9372 	for (i = 0, j = 0; j < n && !rc; i++) {
9373 		struct bnxt_ctx_pg_info *ctx_pg;
9374 
9375 		if (!(instance_bmap & (1 << i)))
9376 			continue;
9377 		req->instance = cpu_to_le16(i);
9378 		ctx_pg = &ctxm->pg_info[j++];
9379 		if (!ctx_pg->entries)
9380 			continue;
9381 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9382 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9383 				      &req->page_size_pbl_level,
9384 				      &req->page_dir);
9385 		if (last && j == n)
9386 			req->flags =
9387 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9388 		rc = hwrm_req_send(bp, req);
9389 	}
9390 	hwrm_req_drop(bp, req);
9391 	return rc;
9392 }
9393 
9394 static int bnxt_backing_store_cfg_v2(struct bnxt *bp)
9395 {
9396 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9397 	struct bnxt_ctx_mem_type *ctxm;
9398 	u16 last_type = BNXT_CTX_INV;
9399 	int rc = 0;
9400 	u16 type;
9401 
9402 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_QPC; type++) {
9403 		ctxm = &ctx->ctx_arr[type];
9404 		if (!bnxt_bs_trace_avail(bp, type))
9405 			continue;
9406 		if (!ctxm->mem_valid) {
9407 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9408 						     ctxm->max_entries, 1);
9409 			if (rc) {
9410 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9411 					    type);
9412 				continue;
9413 			}
9414 			bnxt_bs_trace_init(bp, ctxm);
9415 		}
9416 		last_type = type;
9417 	}
9418 
9419 	if (last_type == BNXT_CTX_INV) {
9420 		for (type = 0; type < BNXT_CTX_MAX; type++) {
9421 			ctxm = &ctx->ctx_arr[type];
9422 			if (ctxm->mem_valid)
9423 				last_type = type;
9424 		}
9425 		if (last_type == BNXT_CTX_INV)
9426 			return 0;
9427 	}
9428 	ctx->ctx_arr[last_type].last = 1;
9429 
9430 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9431 		ctxm = &ctx->ctx_arr[type];
9432 
9433 		if (!ctxm->mem_valid)
9434 			continue;
9435 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9436 		if (rc)
9437 			return rc;
9438 	}
9439 	return 0;
9440 }
9441 
9442 /**
9443  * __bnxt_copy_ctx_mem - copy host context memory
9444  * @bp: The driver context
9445  * @ctxm: The pointer to the context memory type
9446  * @buf: The destination buffer or NULL to just obtain the length
9447  * @offset: The buffer offset to copy the data to
9448  * @head: The head offset of context memory to copy from
9449  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9450  *
9451  * This function is called for debugging purposes to dump the host context
9452  * used by the chip.
9453  *
9454  * Return: Length of memory copied
9455  */
9456 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9457 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9458 				  size_t offset, size_t head, size_t tail)
9459 {
9460 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9461 	size_t len = 0, total_len = 0;
9462 	int i, n = 1;
9463 
9464 	if (!ctx_pg)
9465 		return 0;
9466 
9467 	if (ctxm->instance_bmap)
9468 		n = hweight32(ctxm->instance_bmap);
9469 	for (i = 0; i < n; i++) {
9470 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9471 					    tail);
9472 		offset += len;
9473 		total_len += len;
9474 	}
9475 	return total_len;
9476 }
9477 
9478 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9479 			 void *buf, size_t offset)
9480 {
9481 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9482 
9483 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9484 }
9485 
9486 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9487 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9488 {
9489 	struct bnxt_ctx_pg_info *ctx_pg;
9490 	int i, n = 1;
9491 
9492 	ctxm->last = 0;
9493 
9494 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9495 		return;
9496 
9497 	ctx_pg = ctxm->pg_info;
9498 	if (ctx_pg) {
9499 		if (ctxm->instance_bmap)
9500 			n = hweight32(ctxm->instance_bmap);
9501 		for (i = 0; i < n; i++)
9502 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9503 
9504 		kfree(ctx_pg);
9505 		ctxm->pg_info = NULL;
9506 		ctxm->mem_valid = 0;
9507 	}
9508 	memset(ctxm, 0, sizeof(*ctxm));
9509 }
9510 
9511 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9512 {
9513 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9514 	u16 type;
9515 
9516 	if (!ctx)
9517 		return;
9518 
9519 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9520 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9521 
9522 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9523 	if (force) {
9524 		kfree(ctx);
9525 		bp->ctx = NULL;
9526 	}
9527 }
9528 
9529 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9530 {
9531 	struct bnxt_ctx_mem_type *ctxm;
9532 	struct bnxt_ctx_mem_info *ctx;
9533 	u32 l2_qps, qp1_qps, max_qps;
9534 	u32 ena, entries_sp, entries;
9535 	u32 srqs, max_srqs, min;
9536 	u32 num_mr, num_ah;
9537 	u32 extra_srqs = 0;
9538 	u32 extra_qps = 0;
9539 	u32 fast_qpmd_qps;
9540 	u8 pg_lvl = 1;
9541 	int i, rc;
9542 
9543 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9544 	if (rc) {
9545 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9546 			   rc);
9547 		return rc;
9548 	}
9549 	ctx = bp->ctx;
9550 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9551 		return 0;
9552 
9553 	ena = 0;
9554 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
9555 		goto skip_legacy;
9556 
9557 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9558 	l2_qps = ctxm->qp_l2_entries;
9559 	qp1_qps = ctxm->qp_qp1_entries;
9560 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9561 	max_qps = ctxm->max_entries;
9562 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9563 	srqs = ctxm->srq_l2_entries;
9564 	max_srqs = ctxm->max_entries;
9565 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9566 		pg_lvl = 2;
9567 		if (BNXT_SW_RES_LMT(bp)) {
9568 			extra_qps = max_qps - l2_qps - qp1_qps;
9569 			extra_srqs = max_srqs - srqs;
9570 		} else {
9571 			extra_qps = min_t(u32, 65536,
9572 					  max_qps - l2_qps - qp1_qps);
9573 			/* allocate extra qps if fw supports RoCE fast qp
9574 			 * destroy feature
9575 			 */
9576 			extra_qps += fast_qpmd_qps;
9577 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9578 		}
9579 		if (fast_qpmd_qps)
9580 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9581 	}
9582 
9583 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9584 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9585 				     pg_lvl);
9586 	if (rc)
9587 		return rc;
9588 
9589 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9590 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9591 	if (rc)
9592 		return rc;
9593 
9594 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9595 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9596 				     extra_qps * 2, pg_lvl);
9597 	if (rc)
9598 		return rc;
9599 
9600 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9601 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9602 	if (rc)
9603 		return rc;
9604 
9605 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9606 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9607 	if (rc)
9608 		return rc;
9609 
9610 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9611 		goto skip_rdma;
9612 
9613 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9614 	if (BNXT_SW_RES_LMT(bp) &&
9615 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9616 		num_ah = ctxm->mrav_av_entries;
9617 		num_mr = ctxm->max_entries - num_ah;
9618 	} else {
9619 		/* 128K extra is needed to accommodate static AH context
9620 		 * allocation by f/w.
9621 		 */
9622 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9623 		num_ah = min_t(u32, num_mr, 1024 * 128);
9624 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9625 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9626 			ctxm->mrav_av_entries = num_ah;
9627 	}
9628 
9629 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9630 	if (rc)
9631 		return rc;
9632 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9633 
9634 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9635 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9636 	if (rc)
9637 		return rc;
9638 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9639 
9640 skip_rdma:
9641 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9642 	min = ctxm->min_entries;
9643 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9644 		     2 * (extra_qps + qp1_qps) + min;
9645 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9646 	if (rc)
9647 		return rc;
9648 
9649 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9650 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9651 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9652 	if (rc)
9653 		return rc;
9654 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9655 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9656 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9657 
9658 skip_legacy:
9659 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9660 		rc = bnxt_backing_store_cfg_v2(bp);
9661 	else
9662 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9663 	if (rc) {
9664 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9665 			   rc);
9666 		return rc;
9667 	}
9668 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9669 	return 0;
9670 }
9671 
9672 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9673 {
9674 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9675 	u16 page_attr;
9676 	int rc;
9677 
9678 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9679 		return 0;
9680 
9681 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9682 	if (rc)
9683 		return rc;
9684 
9685 	if (BNXT_PAGE_SIZE == 0x2000)
9686 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9687 	else if (BNXT_PAGE_SIZE == 0x10000)
9688 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9689 	else
9690 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9691 	req->pg_size_lvl = cpu_to_le16(page_attr |
9692 				       bp->fw_crash_mem->ring_mem.depth);
9693 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9694 	req->size = cpu_to_le32(bp->fw_crash_len);
9695 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9696 	return hwrm_req_send(bp, req);
9697 }
9698 
9699 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9700 {
9701 	if (bp->fw_crash_mem) {
9702 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9703 		kfree(bp->fw_crash_mem);
9704 		bp->fw_crash_mem = NULL;
9705 	}
9706 }
9707 
9708 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9709 {
9710 	u32 mem_size = 0;
9711 	int rc;
9712 
9713 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9714 		return 0;
9715 
9716 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9717 	if (rc)
9718 		return rc;
9719 
9720 	mem_size = round_up(mem_size, 4);
9721 
9722 	/* keep and use the existing pages */
9723 	if (bp->fw_crash_mem &&
9724 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9725 		goto alloc_done;
9726 
9727 	if (bp->fw_crash_mem)
9728 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9729 	else
9730 		bp->fw_crash_mem = kzalloc_obj(*bp->fw_crash_mem);
9731 	if (!bp->fw_crash_mem)
9732 		return -ENOMEM;
9733 
9734 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9735 	if (rc) {
9736 		bnxt_free_crash_dump_mem(bp);
9737 		return rc;
9738 	}
9739 
9740 alloc_done:
9741 	bp->fw_crash_len = mem_size;
9742 	return 0;
9743 }
9744 
9745 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9746 {
9747 	struct hwrm_func_resource_qcaps_output *resp;
9748 	struct hwrm_func_resource_qcaps_input *req;
9749 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9750 	int rc;
9751 
9752 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9753 	if (rc)
9754 		return rc;
9755 
9756 	req->fid = cpu_to_le16(0xffff);
9757 	resp = hwrm_req_hold(bp, req);
9758 	rc = hwrm_req_send_silent(bp, req);
9759 	if (rc)
9760 		goto hwrm_func_resc_qcaps_exit;
9761 
9762 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9763 	if (!all)
9764 		goto hwrm_func_resc_qcaps_exit;
9765 
9766 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9767 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9768 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9769 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9770 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9771 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9772 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9773 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9774 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9775 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9776 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9777 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9778 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9779 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9780 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9781 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9782 
9783 	if (hw_resc->max_rsscos_ctxs >=
9784 	    hw_resc->max_vnics * BNXT_LARGE_RSS_TO_VNIC_RATIO)
9785 		bp->rss_cap |= BNXT_RSS_CAP_LARGE_RSS_CTX;
9786 
9787 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9788 		u16 max_msix = le16_to_cpu(resp->max_msix);
9789 
9790 		hw_resc->max_nqs = max_msix;
9791 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9792 	}
9793 
9794 	if (BNXT_PF(bp)) {
9795 		struct bnxt_pf_info *pf = &bp->pf;
9796 
9797 		pf->vf_resv_strategy =
9798 			le16_to_cpu(resp->vf_reservation_strategy);
9799 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9800 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9801 	}
9802 hwrm_func_resc_qcaps_exit:
9803 	hwrm_req_drop(bp, req);
9804 	return rc;
9805 }
9806 
9807 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9808 {
9809 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9810 	struct hwrm_port_mac_ptp_qcfg_input *req;
9811 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9812 	u8 flags;
9813 	int rc;
9814 
9815 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9816 		rc = -ENODEV;
9817 		goto no_ptp;
9818 	}
9819 
9820 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9821 	if (rc)
9822 		goto no_ptp;
9823 
9824 	req->port_id = cpu_to_le16(bp->pf.port_id);
9825 	resp = hwrm_req_hold(bp, req);
9826 	rc = hwrm_req_send(bp, req);
9827 	if (rc)
9828 		goto exit;
9829 
9830 	flags = resp->flags;
9831 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9832 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9833 		rc = -ENODEV;
9834 		goto exit;
9835 	}
9836 	if (!ptp) {
9837 		ptp = kzalloc_obj(*ptp);
9838 		if (!ptp) {
9839 			rc = -ENOMEM;
9840 			goto exit;
9841 		}
9842 		ptp->bp = bp;
9843 		bp->ptp_cfg = ptp;
9844 	}
9845 
9846 	if (flags &
9847 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9848 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9849 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9850 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9851 	} else if (BNXT_CHIP_P5(bp)) {
9852 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9853 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9854 	} else {
9855 		rc = -ENODEV;
9856 		goto exit;
9857 	}
9858 	ptp->rtc_configured =
9859 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9860 	rc = bnxt_ptp_init(bp);
9861 	if (rc)
9862 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9863 exit:
9864 	hwrm_req_drop(bp, req);
9865 	if (!rc)
9866 		return 0;
9867 
9868 no_ptp:
9869 	bnxt_ptp_clear(bp);
9870 	kfree(ptp);
9871 	bp->ptp_cfg = NULL;
9872 	return rc;
9873 }
9874 
9875 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9876 {
9877 	u32 flags, flags_ext, flags_ext2, flags_ext3;
9878 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9879 	struct hwrm_func_qcaps_output *resp;
9880 	struct hwrm_func_qcaps_input *req;
9881 	int rc;
9882 
9883 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9884 	if (rc)
9885 		return rc;
9886 
9887 	req->fid = cpu_to_le16(0xffff);
9888 	resp = hwrm_req_hold(bp, req);
9889 	rc = hwrm_req_send(bp, req);
9890 	if (rc)
9891 		goto hwrm_func_qcaps_exit;
9892 
9893 	flags = le32_to_cpu(resp->flags);
9894 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9895 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9896 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9897 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9898 	if (flags & FUNC_QCAPS_RESP_FLAGS_LINK_ADMIN_STATUS_SUPPORTED)
9899 		bp->fw_cap |= BNXT_FW_CAP_LINK_ADMIN;
9900 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9901 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9902 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9903 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9904 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9905 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9906 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9907 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9908 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9909 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9910 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9911 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9912 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9913 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9914 
9915 	flags_ext = le32_to_cpu(resp->flags_ext);
9916 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9917 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9918 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9919 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9920 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PTM_SUPPORTED)
9921 		bp->fw_cap |= BNXT_FW_CAP_PTP_PTM;
9922 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9923 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9924 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9925 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9926 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9927 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9928 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9929 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9930 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9931 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9932 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9933 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9934 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9935 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9936 
9937 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9938 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9939 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9940 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9941 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9942 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9943 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9944 	if (flags_ext2 &
9945 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9946 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9947 	if (BNXT_PF(bp) &&
9948 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9949 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9950 
9951 	flags_ext3 = le32_to_cpu(resp->flags_ext3);
9952 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_ROCE_VF_DYN_ALLOC_SUPPORT)
9953 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_DYN_ALLOC_SUPPORT;
9954 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_MIRROR_ON_ROCE_SUPPORTED)
9955 		bp->fw_cap |= BNXT_FW_CAP_MIRROR_ON_ROCE;
9956 
9957 	bp->tx_push_thresh = 0;
9958 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9959 	    BNXT_FW_MAJ(bp) > 217)
9960 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9961 
9962 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9963 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9964 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9965 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9966 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9967 	if (!hw_resc->max_hw_ring_grps)
9968 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9969 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9970 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9971 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9972 
9973 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9974 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9975 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9976 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9977 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9978 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9979 
9980 	if (BNXT_PF(bp)) {
9981 		struct bnxt_pf_info *pf = &bp->pf;
9982 
9983 		pf->fw_fid = le16_to_cpu(resp->fid);
9984 		pf->port_id = le16_to_cpu(resp->port_id);
9985 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9986 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9987 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9988 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9989 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9990 			bp->flags |= BNXT_FLAG_WOL_CAP;
9991 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9992 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9993 		} else {
9994 			bnxt_ptp_clear(bp);
9995 			kfree(bp->ptp_cfg);
9996 			bp->ptp_cfg = NULL;
9997 		}
9998 	} else {
9999 #ifdef CONFIG_BNXT_SRIOV
10000 		struct bnxt_vf_info *vf = &bp->vf;
10001 
10002 		vf->fw_fid = le16_to_cpu(resp->fid);
10003 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
10004 #endif
10005 	}
10006 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
10007 
10008 hwrm_func_qcaps_exit:
10009 	hwrm_req_drop(bp, req);
10010 	return rc;
10011 }
10012 
10013 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
10014 {
10015 	struct hwrm_dbg_qcaps_output *resp;
10016 	struct hwrm_dbg_qcaps_input *req;
10017 	int rc;
10018 
10019 	bp->fw_dbg_cap = 0;
10020 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
10021 		return;
10022 
10023 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
10024 	if (rc)
10025 		return;
10026 
10027 	req->fid = cpu_to_le16(0xffff);
10028 	resp = hwrm_req_hold(bp, req);
10029 	rc = hwrm_req_send(bp, req);
10030 	if (rc)
10031 		goto hwrm_dbg_qcaps_exit;
10032 
10033 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
10034 
10035 hwrm_dbg_qcaps_exit:
10036 	hwrm_req_drop(bp, req);
10037 }
10038 
10039 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
10040 
10041 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
10042 {
10043 	int rc;
10044 
10045 	rc = __bnxt_hwrm_func_qcaps(bp);
10046 	if (rc)
10047 		return rc;
10048 
10049 	bnxt_hwrm_dbg_qcaps(bp);
10050 
10051 	rc = bnxt_hwrm_queue_qportcfg(bp);
10052 	if (rc) {
10053 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
10054 		return rc;
10055 	}
10056 	if (bp->hwrm_spec_code >= 0x10803) {
10057 		rc = bnxt_alloc_ctx_mem(bp);
10058 		if (rc)
10059 			return rc;
10060 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
10061 		if (!rc)
10062 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
10063 	}
10064 	return 0;
10065 }
10066 
10067 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
10068 {
10069 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
10070 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
10071 	u32 flags;
10072 	int rc;
10073 
10074 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
10075 		return 0;
10076 
10077 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
10078 	if (rc)
10079 		return rc;
10080 
10081 	resp = hwrm_req_hold(bp, req);
10082 	rc = hwrm_req_send(bp, req);
10083 	if (rc)
10084 		goto hwrm_cfa_adv_qcaps_exit;
10085 
10086 	flags = le32_to_cpu(resp->flags);
10087 	if (flags &
10088 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
10089 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
10090 
10091 	if (flags &
10092 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
10093 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
10094 
10095 	if (flags &
10096 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
10097 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
10098 
10099 hwrm_cfa_adv_qcaps_exit:
10100 	hwrm_req_drop(bp, req);
10101 	return rc;
10102 }
10103 
10104 static int __bnxt_alloc_fw_health(struct bnxt *bp)
10105 {
10106 	if (bp->fw_health)
10107 		return 0;
10108 
10109 	bp->fw_health = kzalloc_obj(*bp->fw_health);
10110 	if (!bp->fw_health)
10111 		return -ENOMEM;
10112 
10113 	mutex_init(&bp->fw_health->lock);
10114 	return 0;
10115 }
10116 
10117 static int bnxt_alloc_fw_health(struct bnxt *bp)
10118 {
10119 	int rc;
10120 
10121 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
10122 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10123 		return 0;
10124 
10125 	rc = __bnxt_alloc_fw_health(bp);
10126 	if (rc) {
10127 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
10128 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10129 		return rc;
10130 	}
10131 
10132 	return 0;
10133 }
10134 
10135 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
10136 {
10137 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
10138 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
10139 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
10140 }
10141 
10142 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
10143 {
10144 	struct bnxt_fw_health *fw_health = bp->fw_health;
10145 	u32 reg_type;
10146 
10147 	if (!fw_health)
10148 		return;
10149 
10150 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
10151 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
10152 		fw_health->status_reliable = false;
10153 
10154 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
10155 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
10156 		fw_health->resets_reliable = false;
10157 }
10158 
10159 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
10160 {
10161 	void __iomem *hs;
10162 	u32 status_loc;
10163 	u32 reg_type;
10164 	u32 sig;
10165 
10166 	if (bp->fw_health)
10167 		bp->fw_health->status_reliable = false;
10168 
10169 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
10170 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
10171 
10172 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
10173 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
10174 		if (!bp->chip_num) {
10175 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
10176 			bp->chip_num = readl(bp->bar0 +
10177 					     BNXT_FW_HEALTH_WIN_BASE +
10178 					     BNXT_GRC_REG_CHIP_NUM);
10179 		}
10180 		if (!BNXT_CHIP_P5_PLUS(bp))
10181 			return;
10182 
10183 		status_loc = BNXT_GRC_REG_STATUS_P5 |
10184 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
10185 	} else {
10186 		status_loc = readl(hs + offsetof(struct hcomm_status,
10187 						 fw_status_loc));
10188 	}
10189 
10190 	if (__bnxt_alloc_fw_health(bp)) {
10191 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
10192 		return;
10193 	}
10194 
10195 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
10196 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
10197 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
10198 		__bnxt_map_fw_health_reg(bp, status_loc);
10199 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
10200 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
10201 	}
10202 
10203 	bp->fw_health->status_reliable = true;
10204 }
10205 
10206 static int bnxt_map_fw_health_regs(struct bnxt *bp)
10207 {
10208 	struct bnxt_fw_health *fw_health = bp->fw_health;
10209 	u32 reg_base = 0xffffffff;
10210 	int i;
10211 
10212 	bp->fw_health->status_reliable = false;
10213 	bp->fw_health->resets_reliable = false;
10214 	/* Only pre-map the monitoring GRC registers using window 3 */
10215 	for (i = 0; i < 4; i++) {
10216 		u32 reg = fw_health->regs[i];
10217 
10218 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
10219 			continue;
10220 		if (reg_base == 0xffffffff)
10221 			reg_base = reg & BNXT_GRC_BASE_MASK;
10222 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
10223 			return -ERANGE;
10224 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
10225 	}
10226 	bp->fw_health->status_reliable = true;
10227 	bp->fw_health->resets_reliable = true;
10228 	if (reg_base == 0xffffffff)
10229 		return 0;
10230 
10231 	__bnxt_map_fw_health_reg(bp, reg_base);
10232 	return 0;
10233 }
10234 
10235 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
10236 {
10237 	if (!bp->fw_health)
10238 		return;
10239 
10240 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
10241 		bp->fw_health->status_reliable = true;
10242 		bp->fw_health->resets_reliable = true;
10243 	} else {
10244 		bnxt_try_map_fw_health_reg(bp);
10245 	}
10246 }
10247 
10248 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
10249 {
10250 	struct bnxt_fw_health *fw_health = bp->fw_health;
10251 	struct hwrm_error_recovery_qcfg_output *resp;
10252 	struct hwrm_error_recovery_qcfg_input *req;
10253 	int rc, i;
10254 
10255 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10256 		return 0;
10257 
10258 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
10259 	if (rc)
10260 		return rc;
10261 
10262 	resp = hwrm_req_hold(bp, req);
10263 	rc = hwrm_req_send(bp, req);
10264 	if (rc)
10265 		goto err_recovery_out;
10266 	fw_health->flags = le32_to_cpu(resp->flags);
10267 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
10268 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
10269 		rc = -EINVAL;
10270 		goto err_recovery_out;
10271 	}
10272 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
10273 	fw_health->master_func_wait_dsecs =
10274 		le32_to_cpu(resp->master_func_wait_period);
10275 	fw_health->normal_func_wait_dsecs =
10276 		le32_to_cpu(resp->normal_func_wait_period);
10277 	fw_health->post_reset_wait_dsecs =
10278 		le32_to_cpu(resp->master_func_wait_period_after_reset);
10279 	fw_health->post_reset_max_wait_dsecs =
10280 		le32_to_cpu(resp->max_bailout_time_after_reset);
10281 	fw_health->regs[BNXT_FW_HEALTH_REG] =
10282 		le32_to_cpu(resp->fw_health_status_reg);
10283 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
10284 		le32_to_cpu(resp->fw_heartbeat_reg);
10285 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
10286 		le32_to_cpu(resp->fw_reset_cnt_reg);
10287 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
10288 		le32_to_cpu(resp->reset_inprogress_reg);
10289 	fw_health->fw_reset_inprog_reg_mask =
10290 		le32_to_cpu(resp->reset_inprogress_reg_mask);
10291 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
10292 	if (fw_health->fw_reset_seq_cnt >= 16) {
10293 		rc = -EINVAL;
10294 		goto err_recovery_out;
10295 	}
10296 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
10297 		fw_health->fw_reset_seq_regs[i] =
10298 			le32_to_cpu(resp->reset_reg[i]);
10299 		fw_health->fw_reset_seq_vals[i] =
10300 			le32_to_cpu(resp->reset_reg_val[i]);
10301 		fw_health->fw_reset_seq_delay_msec[i] =
10302 			resp->delay_after_reset[i];
10303 	}
10304 err_recovery_out:
10305 	hwrm_req_drop(bp, req);
10306 	if (!rc)
10307 		rc = bnxt_map_fw_health_regs(bp);
10308 	if (rc)
10309 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10310 	return rc;
10311 }
10312 
10313 static int bnxt_hwrm_func_reset(struct bnxt *bp)
10314 {
10315 	struct hwrm_func_reset_input *req;
10316 	int rc;
10317 
10318 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
10319 	if (rc)
10320 		return rc;
10321 
10322 	req->enables = 0;
10323 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
10324 	return hwrm_req_send(bp, req);
10325 }
10326 
10327 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
10328 {
10329 	struct hwrm_nvm_get_dev_info_output nvm_info;
10330 
10331 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
10332 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10333 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10334 			 nvm_info.nvm_cfg_ver_upd);
10335 }
10336 
10337 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10338 {
10339 	struct hwrm_queue_qportcfg_output *resp;
10340 	struct hwrm_queue_qportcfg_input *req;
10341 	u8 i, j, *qptr;
10342 	bool no_rdma;
10343 	int rc = 0;
10344 
10345 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10346 	if (rc)
10347 		return rc;
10348 
10349 	resp = hwrm_req_hold(bp, req);
10350 	rc = hwrm_req_send(bp, req);
10351 	if (rc)
10352 		goto qportcfg_exit;
10353 
10354 	if (!resp->max_configurable_queues) {
10355 		rc = -EINVAL;
10356 		goto qportcfg_exit;
10357 	}
10358 	bp->max_tc = resp->max_configurable_queues;
10359 	bp->max_lltc = resp->max_configurable_lossless_queues;
10360 	if (bp->max_tc > BNXT_MAX_QUEUE)
10361 		bp->max_tc = BNXT_MAX_QUEUE;
10362 
10363 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10364 	qptr = &resp->queue_id0;
10365 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10366 		bp->q_info[j].queue_id = *qptr;
10367 		bp->q_ids[i] = *qptr++;
10368 		bp->q_info[j].queue_profile = *qptr++;
10369 		bp->tc_to_qidx[j] = j;
10370 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10371 		    (no_rdma && BNXT_PF(bp)))
10372 			j++;
10373 	}
10374 	bp->max_q = bp->max_tc;
10375 	bp->max_tc = max_t(u8, j, 1);
10376 
10377 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10378 		bp->max_tc = 1;
10379 
10380 	if (bp->max_lltc > bp->max_tc)
10381 		bp->max_lltc = bp->max_tc;
10382 
10383 qportcfg_exit:
10384 	hwrm_req_drop(bp, req);
10385 	return rc;
10386 }
10387 
10388 static int bnxt_hwrm_poll(struct bnxt *bp)
10389 {
10390 	struct hwrm_ver_get_input *req;
10391 	int rc;
10392 
10393 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10394 	if (rc)
10395 		return rc;
10396 
10397 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10398 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10399 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10400 
10401 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10402 	rc = hwrm_req_send(bp, req);
10403 	return rc;
10404 }
10405 
10406 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10407 {
10408 	struct hwrm_ver_get_output *resp;
10409 	struct hwrm_ver_get_input *req;
10410 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10411 	u32 dev_caps_cfg, hwrm_ver;
10412 	int rc, len, max_tmo_secs;
10413 
10414 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10415 	if (rc)
10416 		return rc;
10417 
10418 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10419 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10420 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10421 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10422 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10423 
10424 	resp = hwrm_req_hold(bp, req);
10425 	rc = hwrm_req_send(bp, req);
10426 	if (rc)
10427 		goto hwrm_ver_get_exit;
10428 
10429 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10430 
10431 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10432 			     resp->hwrm_intf_min_8b << 8 |
10433 			     resp->hwrm_intf_upd_8b;
10434 	if (resp->hwrm_intf_maj_8b < 1) {
10435 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10436 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10437 			    resp->hwrm_intf_upd_8b);
10438 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10439 	}
10440 
10441 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10442 			HWRM_VERSION_UPDATE;
10443 
10444 	if (bp->hwrm_spec_code > hwrm_ver)
10445 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10446 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10447 			 HWRM_VERSION_UPDATE);
10448 	else
10449 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10450 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10451 			 resp->hwrm_intf_upd_8b);
10452 
10453 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10454 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10455 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10456 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10457 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10458 		len = FW_VER_STR_LEN;
10459 	} else {
10460 		fw_maj = resp->hwrm_fw_maj_8b;
10461 		fw_min = resp->hwrm_fw_min_8b;
10462 		fw_bld = resp->hwrm_fw_bld_8b;
10463 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10464 		len = BC_HWRM_STR_LEN;
10465 	}
10466 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10467 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10468 		 fw_rsv);
10469 
10470 	if (strlen(resp->active_pkg_name)) {
10471 		int fw_ver_len = strlen(bp->fw_ver_str);
10472 
10473 		snprintf(bp->fw_ver_str + fw_ver_len,
10474 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10475 			 resp->active_pkg_name);
10476 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10477 	}
10478 
10479 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10480 	if (!bp->hwrm_cmd_timeout)
10481 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10482 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10483 	if (!bp->hwrm_cmd_max_timeout)
10484 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10485 	max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000;
10486 #ifdef CONFIG_DETECT_HUNG_TASK
10487 	if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT ||
10488 	    max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) {
10489 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n",
10490 			    max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT);
10491 	}
10492 #endif
10493 
10494 	if (resp->hwrm_intf_maj_8b >= 1) {
10495 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10496 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10497 	}
10498 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10499 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10500 
10501 	bp->chip_num = le16_to_cpu(resp->chip_num);
10502 	bp->chip_rev = resp->chip_rev;
10503 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10504 	    !resp->chip_metal)
10505 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10506 
10507 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10508 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10509 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10510 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10511 
10512 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10513 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10514 
10515 	if (dev_caps_cfg &
10516 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10517 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10518 
10519 	if (dev_caps_cfg &
10520 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10521 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10522 
10523 	if (dev_caps_cfg &
10524 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10525 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10526 
10527 hwrm_ver_get_exit:
10528 	hwrm_req_drop(bp, req);
10529 	return rc;
10530 }
10531 
10532 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10533 {
10534 	struct hwrm_fw_set_time_input *req;
10535 	struct tm tm;
10536 	time64_t now = ktime_get_real_seconds();
10537 	int rc;
10538 
10539 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10540 	    bp->hwrm_spec_code < 0x10400)
10541 		return -EOPNOTSUPP;
10542 
10543 	time64_to_tm(now, 0, &tm);
10544 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10545 	if (rc)
10546 		return rc;
10547 
10548 	req->year = cpu_to_le16(1900 + tm.tm_year);
10549 	req->month = 1 + tm.tm_mon;
10550 	req->day = tm.tm_mday;
10551 	req->hour = tm.tm_hour;
10552 	req->minute = tm.tm_min;
10553 	req->second = tm.tm_sec;
10554 	return hwrm_req_send(bp, req);
10555 }
10556 
10557 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10558 {
10559 	u64 sw_tmp;
10560 
10561 	hw &= mask;
10562 	sw_tmp = (*sw & ~mask) | hw;
10563 	if (hw < (*sw & mask))
10564 		sw_tmp += mask + 1;
10565 	WRITE_ONCE(*sw, sw_tmp);
10566 }
10567 
10568 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10569 				    int count, bool ignore_zero)
10570 {
10571 	int i;
10572 
10573 	for (i = 0; i < count; i++) {
10574 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10575 
10576 		if (ignore_zero && !hw)
10577 			continue;
10578 
10579 		if (masks[i] == -1ULL)
10580 			sw_stats[i] = hw;
10581 		else
10582 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10583 	}
10584 }
10585 
10586 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10587 {
10588 	if (!stats->hw_stats)
10589 		return;
10590 
10591 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10592 				stats->hw_masks, stats->len / 8, false);
10593 }
10594 
10595 static void bnxt_accumulate_ring_stats(struct bnxt *bp)
10596 {
10597 	struct bnxt_stats_mem *ring0_stats;
10598 	bool ignore_zero = false;
10599 	int i;
10600 
10601 	/* Chip bug.  Counter intermittently becomes 0. */
10602 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10603 		ignore_zero = true;
10604 
10605 	for (i = 0; i < bp->cp_nr_rings; i++) {
10606 		struct bnxt_napi *bnapi = bp->bnapi[i];
10607 		struct bnxt_cp_ring_info *cpr;
10608 		struct bnxt_stats_mem *stats;
10609 
10610 		cpr = &bnapi->cp_ring;
10611 		stats = &cpr->stats;
10612 		if (!i)
10613 			ring0_stats = stats;
10614 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10615 					ring0_stats->hw_masks,
10616 					ring0_stats->len / 8, ignore_zero);
10617 	}
10618 }
10619 
10620 static void bnxt_accumulate_port_stats(struct bnxt *bp)
10621 {
10622 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10623 		struct bnxt_stats_mem *stats = &bp->port_stats;
10624 		__le64 *hw_stats = stats->hw_stats;
10625 		u64 *sw_stats = stats->sw_stats;
10626 		u64 *masks = stats->hw_masks;
10627 		int cnt;
10628 
10629 		cnt = sizeof(struct rx_port_stats) / 8;
10630 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10631 
10632 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10633 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10634 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10635 		cnt = sizeof(struct tx_port_stats) / 8;
10636 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10637 	}
10638 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10639 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10640 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10641 	}
10642 }
10643 
10644 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10645 {
10646 	bnxt_accumulate_ring_stats(bp);
10647 	bnxt_accumulate_port_stats(bp);
10648 }
10649 
10650 /* Re-accumulate ring stats from DMA buffers if stale.
10651  * uAPIs for reading sw_stats should call this first.
10652  *
10653  * We promise user space update frequency of bp->stats_coal_ticks but
10654  * the update is a two step process - first device updates the DMA buffer,
10655  * then we have to update from that buffer to driver stats in the service work.
10656  * Worst case we would be 2x off from the desired frequency.
10657  * Sync the stats sooner, if stale. The 20% threshold was chosen arbitrarily.
10658  *
10659  * Ideally we would split the user-configured time into two portions,
10660  * i.e. also lower the DMA period by the 20%. But the DMA timer seems to have
10661  * too coarse granularity to play such tricks.
10662  */
10663 void bnxt_sync_ring_stats(struct bnxt *bp)
10664 {
10665 	unsigned long stale;
10666 
10667 	if (!netif_running(bp->dev) || !bp->stats_coal_ticks)
10668 		return;
10669 
10670 	spin_lock(&bp->stats_lock);
10671 	stale = usecs_to_jiffies(bp->stats_coal_ticks / 5);
10672 	if (time_after_eq(jiffies, bp->stats_updated_jiffies + stale)) {
10673 		bnxt_accumulate_ring_stats(bp);
10674 		bp->stats_updated_jiffies = jiffies;
10675 	}
10676 	spin_unlock(&bp->stats_lock);
10677 }
10678 
10679 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10680 {
10681 	struct hwrm_port_qstats_input *req;
10682 	struct bnxt_pf_info *pf = &bp->pf;
10683 	int rc;
10684 
10685 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10686 		return 0;
10687 
10688 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10689 		return -EOPNOTSUPP;
10690 
10691 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10692 	if (rc)
10693 		return rc;
10694 
10695 	req->flags = flags;
10696 	req->port_id = cpu_to_le16(pf->port_id);
10697 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10698 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10699 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10700 	return hwrm_req_send(bp, req);
10701 }
10702 
10703 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10704 {
10705 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10706 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10707 	struct hwrm_port_qstats_ext_output *resp_qs;
10708 	struct hwrm_port_qstats_ext_input *req_qs;
10709 	struct bnxt_pf_info *pf = &bp->pf;
10710 	u32 tx_stat_size;
10711 	int rc;
10712 
10713 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10714 		return 0;
10715 
10716 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10717 		return -EOPNOTSUPP;
10718 
10719 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10720 	if (rc)
10721 		return rc;
10722 
10723 	req_qs->flags = flags;
10724 	req_qs->port_id = cpu_to_le16(pf->port_id);
10725 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10726 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10727 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10728 		       sizeof(struct tx_port_stats_ext) : 0;
10729 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10730 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10731 	resp_qs = hwrm_req_hold(bp, req_qs);
10732 	rc = hwrm_req_send(bp, req_qs);
10733 	if (!rc) {
10734 		bp->fw_rx_stats_ext_size =
10735 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10736 		if (BNXT_FW_MAJ(bp) < 220 &&
10737 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10738 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10739 
10740 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10741 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10742 	} else {
10743 		bp->fw_rx_stats_ext_size = 0;
10744 		bp->fw_tx_stats_ext_size = 0;
10745 	}
10746 	hwrm_req_drop(bp, req_qs);
10747 
10748 	if (flags)
10749 		return rc;
10750 
10751 	if (bp->fw_tx_stats_ext_size <=
10752 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10753 		bp->pri2cos_valid = 0;
10754 		return rc;
10755 	}
10756 
10757 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10758 	if (rc)
10759 		return rc;
10760 
10761 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10762 
10763 	resp_qc = hwrm_req_hold(bp, req_qc);
10764 	rc = hwrm_req_send(bp, req_qc);
10765 	if (!rc) {
10766 		u8 *pri2cos;
10767 		int i, j;
10768 
10769 		pri2cos = &resp_qc->pri0_cos_queue_id;
10770 		for (i = 0; i < 8; i++) {
10771 			u8 queue_id = pri2cos[i];
10772 			u8 queue_idx;
10773 
10774 			/* Per port queue IDs start from 0, 10, 20, etc */
10775 			queue_idx = queue_id % 10;
10776 			if (queue_idx > BNXT_MAX_QUEUE) {
10777 				bp->pri2cos_valid = false;
10778 				hwrm_req_drop(bp, req_qc);
10779 				return rc;
10780 			}
10781 			for (j = 0; j < bp->max_q; j++) {
10782 				if (bp->q_ids[j] == queue_id)
10783 					bp->pri2cos_idx[i] = queue_idx;
10784 			}
10785 		}
10786 		bp->pri2cos_valid = true;
10787 	}
10788 	hwrm_req_drop(bp, req_qc);
10789 
10790 	return rc;
10791 }
10792 
10793 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10794 {
10795 	bnxt_hwrm_tunnel_dst_port_free(bp,
10796 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10797 	bnxt_hwrm_tunnel_dst_port_free(bp,
10798 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10799 }
10800 
10801 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10802 {
10803 	int rc, i;
10804 	u32 tpa_flags = 0;
10805 
10806 	if (set_tpa)
10807 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10808 	else if (BNXT_NO_FW_ACCESS(bp))
10809 		return 0;
10810 	for (i = 0; i < bp->nr_vnics; i++) {
10811 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10812 		if (rc) {
10813 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10814 				   i, rc);
10815 			return rc;
10816 		}
10817 	}
10818 	return 0;
10819 }
10820 
10821 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10822 {
10823 	int i;
10824 
10825 	for (i = 0; i < bp->nr_vnics; i++)
10826 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10827 }
10828 
10829 static void bnxt_clear_vnic(struct bnxt *bp)
10830 {
10831 	if (!bp->vnic_info)
10832 		return;
10833 
10834 	bnxt_hwrm_clear_vnic_filter(bp);
10835 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10836 		/* clear all RSS setting before free vnic ctx */
10837 		bnxt_hwrm_clear_vnic_rss(bp);
10838 		bnxt_hwrm_vnic_ctx_free(bp);
10839 	}
10840 	/* before free the vnic, undo the vnic tpa settings */
10841 	if (bp->flags & BNXT_FLAG_TPA)
10842 		bnxt_set_tpa(bp, false);
10843 	bnxt_hwrm_vnic_free(bp);
10844 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10845 		bnxt_hwrm_vnic_ctx_free(bp);
10846 }
10847 
10848 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10849 				    bool irq_re_init)
10850 {
10851 	bnxt_clear_vnic(bp);
10852 	bnxt_hwrm_ring_free(bp, close_path);
10853 	bnxt_hwrm_ring_grp_free(bp);
10854 	if (irq_re_init) {
10855 		bnxt_hwrm_stat_ctx_free(bp);
10856 		bnxt_hwrm_free_tunnel_ports(bp);
10857 	}
10858 }
10859 
10860 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10861 {
10862 	struct hwrm_func_cfg_input *req;
10863 	u8 evb_mode;
10864 	int rc;
10865 
10866 	if (br_mode == BRIDGE_MODE_VEB)
10867 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10868 	else if (br_mode == BRIDGE_MODE_VEPA)
10869 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10870 	else
10871 		return -EINVAL;
10872 
10873 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10874 	if (rc)
10875 		return rc;
10876 
10877 	req->fid = cpu_to_le16(0xffff);
10878 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10879 	req->evb_mode = evb_mode;
10880 	return hwrm_req_send(bp, req);
10881 }
10882 
10883 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10884 {
10885 	struct hwrm_func_cfg_input *req;
10886 	int rc;
10887 
10888 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10889 		return 0;
10890 
10891 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10892 	if (rc)
10893 		return rc;
10894 
10895 	req->fid = cpu_to_le16(0xffff);
10896 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10897 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10898 	if (size == 128)
10899 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10900 
10901 	return hwrm_req_send(bp, req);
10902 }
10903 
10904 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10905 {
10906 	int rc;
10907 
10908 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10909 		goto skip_rss_ctx;
10910 
10911 	/* allocate context for vnic */
10912 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10913 	if (rc) {
10914 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10915 			   vnic->vnic_id, rc);
10916 		goto vnic_setup_err;
10917 	}
10918 	bp->rsscos_nr_ctxs++;
10919 
10920 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10921 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10922 		if (rc) {
10923 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10924 				   vnic->vnic_id, rc);
10925 			goto vnic_setup_err;
10926 		}
10927 		bp->rsscos_nr_ctxs++;
10928 	}
10929 
10930 skip_rss_ctx:
10931 	bnxt_fill_hw_rss_tbl(bp, vnic);
10932 	/* configure default vnic, ring grp */
10933 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10934 	if (rc) {
10935 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10936 			   vnic->vnic_id, rc);
10937 		goto vnic_setup_err;
10938 	}
10939 
10940 	/* Enable RSS hashing on vnic */
10941 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10942 	if (rc) {
10943 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10944 			   vnic->vnic_id, rc);
10945 		goto vnic_setup_err;
10946 	}
10947 
10948 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10949 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10950 		if (rc) {
10951 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10952 				   vnic->vnic_id, rc);
10953 		}
10954 	}
10955 
10956 vnic_setup_err:
10957 	return rc;
10958 }
10959 
10960 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10961 			  u8 valid)
10962 {
10963 	struct hwrm_vnic_update_input *req;
10964 	int rc;
10965 
10966 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10967 	if (rc)
10968 		return rc;
10969 
10970 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10971 
10972 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10973 		req->mru = cpu_to_le16(vnic->mru);
10974 
10975 	req->enables = cpu_to_le32(valid);
10976 
10977 	return hwrm_req_send(bp, req);
10978 }
10979 
10980 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10981 {
10982 	int rc;
10983 
10984 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10985 	if (rc) {
10986 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10987 			   vnic->vnic_id, rc);
10988 		return rc;
10989 	}
10990 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10991 	if (rc)
10992 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10993 			   vnic->vnic_id, rc);
10994 	return rc;
10995 }
10996 
10997 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10998 {
10999 	int rc, i, nr_ctxs;
11000 
11001 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
11002 	for (i = 0; i < nr_ctxs; i++) {
11003 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
11004 		if (rc) {
11005 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
11006 				   vnic->vnic_id, i, rc);
11007 			break;
11008 		}
11009 		bp->rsscos_nr_ctxs++;
11010 	}
11011 	if (i < nr_ctxs)
11012 		return -ENOMEM;
11013 
11014 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
11015 	if (rc)
11016 		return rc;
11017 
11018 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
11019 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
11020 		if (rc) {
11021 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
11022 				   vnic->vnic_id, rc);
11023 		}
11024 	}
11025 	return rc;
11026 }
11027 
11028 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
11029 {
11030 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11031 		return __bnxt_setup_vnic_p5(bp, vnic);
11032 	else
11033 		return __bnxt_setup_vnic(bp, vnic);
11034 }
11035 
11036 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
11037 				     struct bnxt_vnic_info *vnic,
11038 				     u16 start_rx_ring_idx, int rx_rings)
11039 {
11040 	int rc;
11041 
11042 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
11043 	if (rc) {
11044 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
11045 			   vnic->vnic_id, rc);
11046 		return rc;
11047 	}
11048 	return bnxt_setup_vnic(bp, vnic);
11049 }
11050 
11051 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
11052 {
11053 	struct bnxt_vnic_info *vnic;
11054 	int i, rc = 0;
11055 
11056 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
11057 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
11058 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
11059 	}
11060 
11061 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11062 		return 0;
11063 
11064 	for (i = 0; i < bp->rx_nr_rings; i++) {
11065 		u16 vnic_id = i + 1;
11066 		u16 ring_id = i;
11067 
11068 		if (vnic_id >= bp->nr_vnics)
11069 			break;
11070 
11071 		vnic = &bp->vnic_info[vnic_id];
11072 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
11073 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
11074 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
11075 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
11076 			break;
11077 	}
11078 	return rc;
11079 }
11080 
11081 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
11082 			  bool all)
11083 {
11084 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11085 	struct bnxt_filter_base *usr_fltr, *tmp;
11086 	struct bnxt_ntuple_filter *ntp_fltr;
11087 	int i;
11088 
11089 	bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
11090 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
11091 		if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
11092 			bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
11093 	}
11094 	if (!all)
11095 		return;
11096 
11097 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
11098 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
11099 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
11100 			ntp_fltr = container_of(usr_fltr,
11101 						struct bnxt_ntuple_filter,
11102 						base);
11103 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
11104 			bnxt_del_ntp_filter(bp, ntp_fltr);
11105 			bnxt_del_one_usr_fltr(bp, usr_fltr);
11106 		}
11107 	}
11108 
11109 	if (vnic->rss_table)
11110 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
11111 				  vnic->rss_table,
11112 				  vnic->rss_table_dma_addr);
11113 	bp->num_rss_ctx--;
11114 }
11115 
11116 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic,
11117 				  int rxr_id)
11118 {
11119 	u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
11120 	int i, vnic_rx;
11121 
11122 	/* Ntuple VNIC always has all the rx rings. Any change of ring id
11123 	 * must be updated because a future filter may use it.
11124 	 */
11125 	if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
11126 		return true;
11127 
11128 	for (i = 0; i < tbl_size; i++) {
11129 		if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
11130 			vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
11131 		else
11132 			vnic_rx = bp->rss_indir_tbl[i];
11133 
11134 		if (rxr_id == vnic_rx)
11135 			return true;
11136 	}
11137 
11138 	return false;
11139 }
11140 
11141 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic,
11142 				u16 mru, int rxr_id)
11143 {
11144 	int rc;
11145 
11146 	if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id))
11147 		return 0;
11148 
11149 	if (mru) {
11150 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
11151 		if (rc) {
11152 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
11153 				   vnic->vnic_id, rc);
11154 			return rc;
11155 		}
11156 		if (rxr_id == vnic->default_rx_ring) {
11157 			rc = bnxt_hwrm_vnic_cfg(bp, vnic);
11158 			if (rc)
11159 				return rc;
11160 		}
11161 	}
11162 	vnic->mru = mru;
11163 	bnxt_hwrm_vnic_update(bp, vnic,
11164 			      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
11165 
11166 	return 0;
11167 }
11168 
11169 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id)
11170 {
11171 	struct ethtool_rxfh_context *ctx;
11172 	unsigned long context;
11173 	int rc;
11174 
11175 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11176 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11177 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11178 
11179 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id);
11180 		if (rc)
11181 			return rc;
11182 	}
11183 
11184 	return 0;
11185 }
11186 
11187 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
11188 {
11189 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
11190 	struct ethtool_rxfh_context *ctx;
11191 	unsigned long context;
11192 
11193 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11194 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11195 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11196 
11197 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
11198 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
11199 		    __bnxt_setup_vnic_p5(bp, vnic)) {
11200 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
11201 				   rss_ctx->index);
11202 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
11203 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
11204 		}
11205 	}
11206 }
11207 
11208 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
11209 {
11210 	struct ethtool_rxfh_context *ctx;
11211 	unsigned long context;
11212 
11213 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11214 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11215 
11216 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
11217 	}
11218 }
11219 
11220 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
11221 static bool bnxt_promisc_ok(struct bnxt *bp)
11222 {
11223 #ifdef CONFIG_BNXT_SRIOV
11224 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
11225 		return false;
11226 #endif
11227 	return true;
11228 }
11229 
11230 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
11231 {
11232 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
11233 	unsigned int rc = 0;
11234 
11235 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
11236 	if (rc) {
11237 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11238 			   rc);
11239 		return rc;
11240 	}
11241 
11242 	/* Setup the proper default RX ring */
11243 	bnxt_fill_hw_rss_tbl(bp, vnic);
11244 
11245 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
11246 	if (rc) {
11247 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11248 			   rc);
11249 		return rc;
11250 	}
11251 	return rc;
11252 }
11253 
11254 static int bnxt_cfg_rx_mode(struct bnxt *, struct netdev_hw_addr_list *, bool);
11255 static bool bnxt_mc_list_updated(struct bnxt *, u32 *,
11256 				 const struct netdev_hw_addr_list *);
11257 
11258 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
11259 {
11260 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
11261 	int rc = 0;
11262 	unsigned int rx_nr_rings = bp->rx_nr_rings;
11263 
11264 	if (irq_re_init) {
11265 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
11266 		if (rc) {
11267 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
11268 				   rc);
11269 			goto err_out;
11270 		}
11271 	}
11272 
11273 	rc = bnxt_hwrm_ring_alloc(bp);
11274 	if (rc) {
11275 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
11276 		goto err_out;
11277 	}
11278 
11279 	rc = bnxt_hwrm_ring_grp_alloc(bp);
11280 	if (rc) {
11281 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
11282 		goto err_out;
11283 	}
11284 
11285 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11286 		rx_nr_rings--;
11287 
11288 	/* default vnic 0 */
11289 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
11290 	if (rc) {
11291 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
11292 		goto err_out;
11293 	}
11294 
11295 	if (BNXT_VF(bp))
11296 		bnxt_hwrm_func_qcfg(bp);
11297 
11298 	rc = bnxt_setup_vnic(bp, vnic);
11299 	if (rc)
11300 		goto err_out;
11301 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
11302 		bnxt_hwrm_update_rss_hash_cfg(bp);
11303 
11304 	if (bp->flags & BNXT_FLAG_RFS) {
11305 		rc = bnxt_alloc_rfs_vnics(bp);
11306 		if (rc)
11307 			goto err_out;
11308 	}
11309 
11310 	if (bp->flags & BNXT_FLAG_TPA) {
11311 		rc = bnxt_set_tpa(bp, true);
11312 		if (rc)
11313 			goto err_out;
11314 	}
11315 
11316 	if (BNXT_VF(bp))
11317 		bnxt_update_vf_mac(bp);
11318 
11319 	/* Filter for default vnic 0 */
11320 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
11321 	if (rc) {
11322 		if (BNXT_VF(bp) && rc == -ENODEV)
11323 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
11324 		else
11325 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
11326 		goto err_out;
11327 	}
11328 	vnic->uc_filter_count = 1;
11329 
11330 	vnic->rx_mask = 0;
11331 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
11332 		goto skip_rx_mask;
11333 
11334 	if (bp->dev->flags & IFF_BROADCAST)
11335 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11336 
11337 	if (bp->dev->flags & IFF_PROMISC)
11338 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11339 
11340 	if (bp->dev->flags & IFF_ALLMULTI) {
11341 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11342 		vnic->mc_list_count = 0;
11343 	} else if (bp->dev->flags & IFF_MULTICAST) {
11344 		u32 mask = 0;
11345 
11346 		bnxt_mc_list_updated(bp, &mask, &bp->dev->mc);
11347 		vnic->rx_mask |= mask;
11348 	}
11349 
11350 	rc = bnxt_cfg_rx_mode(bp, &bp->dev->uc, true);
11351 	if (rc == -EAGAIN) {
11352 		netif_rx_mode_schedule_retry(bp->dev);
11353 		rc = 0;
11354 	} else if (rc) {
11355 		goto err_out;
11356 	}
11357 
11358 skip_rx_mask:
11359 	rc = bnxt_hwrm_set_coal(bp);
11360 	if (rc)
11361 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
11362 				rc);
11363 
11364 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11365 		rc = bnxt_setup_nitroa0_vnic(bp);
11366 		if (rc)
11367 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
11368 				   rc);
11369 	}
11370 
11371 	if (BNXT_VF(bp)) {
11372 		bnxt_hwrm_func_qcfg(bp);
11373 		netdev_update_features(bp->dev);
11374 	}
11375 
11376 	return 0;
11377 
11378 err_out:
11379 	bnxt_hwrm_resource_free(bp, 0, true);
11380 
11381 	return rc;
11382 }
11383 
11384 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
11385 {
11386 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
11387 	return 0;
11388 }
11389 
11390 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
11391 {
11392 	int rc;
11393 
11394 	bnxt_init_cp_rings(bp);
11395 	rc = bnxt_init_rx_rings(bp);
11396 	if (rc)
11397 		return rc;
11398 
11399 	bnxt_init_tx_rings(bp);
11400 	bnxt_init_ring_grps(bp, irq_re_init);
11401 	bnxt_init_vnics(bp);
11402 
11403 	return bnxt_init_chip(bp, irq_re_init);
11404 }
11405 
11406 static int bnxt_set_real_num_queues(struct bnxt *bp)
11407 {
11408 	int rc;
11409 	struct net_device *dev = bp->dev;
11410 
11411 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
11412 					  bp->tx_nr_rings_xdp);
11413 	if (rc)
11414 		return rc;
11415 
11416 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
11417 	if (rc)
11418 		return rc;
11419 
11420 #ifdef CONFIG_RFS_ACCEL
11421 	if (bp->flags & BNXT_FLAG_RFS)
11422 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
11423 #endif
11424 
11425 	return rc;
11426 }
11427 
11428 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11429 			     bool shared)
11430 {
11431 	int _rx = *rx, _tx = *tx;
11432 
11433 	if (shared) {
11434 		*rx = min_t(int, _rx, max);
11435 		*tx = min_t(int, _tx, max);
11436 	} else {
11437 		if (max < 2)
11438 			return -ENOMEM;
11439 
11440 		while (_rx + _tx > max) {
11441 			if (_rx > _tx && _rx > 1)
11442 				_rx--;
11443 			else if (_tx > 1)
11444 				_tx--;
11445 		}
11446 		*rx = _rx;
11447 		*tx = _tx;
11448 	}
11449 	return 0;
11450 }
11451 
11452 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
11453 {
11454 	return (tx - tx_xdp) / tx_sets + tx_xdp;
11455 }
11456 
11457 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
11458 {
11459 	int tcs = bp->num_tc;
11460 
11461 	if (!tcs)
11462 		tcs = 1;
11463 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11464 }
11465 
11466 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11467 {
11468 	int tcs = bp->num_tc;
11469 
11470 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11471 	       bp->tx_nr_rings_xdp;
11472 }
11473 
11474 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11475 			   bool sh)
11476 {
11477 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11478 
11479 	if (tx_cp != *tx) {
11480 		int tx_saved = tx_cp, rc;
11481 
11482 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11483 		if (rc)
11484 			return rc;
11485 		if (tx_cp != tx_saved)
11486 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11487 		return 0;
11488 	}
11489 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11490 }
11491 
11492 static void bnxt_setup_msix(struct bnxt *bp)
11493 {
11494 	const int len = sizeof(bp->irq_tbl[0].name);
11495 	struct net_device *dev = bp->dev;
11496 	int tcs, i;
11497 
11498 	tcs = bp->num_tc;
11499 	if (tcs) {
11500 		int i, off, count;
11501 
11502 		for (i = 0; i < tcs; i++) {
11503 			count = bp->tx_nr_rings_per_tc;
11504 			off = BNXT_TC_TO_RING_BASE(bp, i);
11505 			netdev_set_tc_queue(dev, i, count, off);
11506 		}
11507 	}
11508 
11509 	for (i = 0; i < bp->cp_nr_rings; i++) {
11510 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11511 		char *attr;
11512 
11513 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11514 			attr = "TxRx";
11515 		else if (i < bp->rx_nr_rings)
11516 			attr = "rx";
11517 		else
11518 			attr = "tx";
11519 
11520 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11521 			 attr, i);
11522 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11523 	}
11524 }
11525 
11526 static int bnxt_init_int_mode(struct bnxt *bp);
11527 
11528 static int bnxt_change_msix(struct bnxt *bp, int total)
11529 {
11530 	struct msi_map map;
11531 	int i;
11532 
11533 	/* add MSIX to the end if needed */
11534 	for (i = bp->total_irqs; i < total; i++) {
11535 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11536 		if (map.index < 0)
11537 			return bp->total_irqs;
11538 		bp->irq_tbl[i].vector = map.virq;
11539 		bp->total_irqs++;
11540 	}
11541 
11542 	/* trim MSIX from the end if needed */
11543 	for (i = bp->total_irqs; i > total; i--) {
11544 		map.index = i - 1;
11545 		map.virq = bp->irq_tbl[i - 1].vector;
11546 		pci_msix_free_irq(bp->pdev, map);
11547 		bp->total_irqs--;
11548 	}
11549 	return bp->total_irqs;
11550 }
11551 
11552 static int bnxt_setup_int_mode(struct bnxt *bp)
11553 {
11554 	int rc;
11555 
11556 	if (!bp->irq_tbl) {
11557 		rc = bnxt_init_int_mode(bp);
11558 		if (rc || !bp->irq_tbl)
11559 			return rc ?: -ENODEV;
11560 	}
11561 
11562 	bnxt_setup_msix(bp);
11563 
11564 	rc = bnxt_set_real_num_queues(bp);
11565 	return rc;
11566 }
11567 
11568 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11569 {
11570 	return bp->hw_resc.max_rsscos_ctxs;
11571 }
11572 
11573 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11574 {
11575 	return bp->hw_resc.max_vnics;
11576 }
11577 
11578 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11579 {
11580 	return bp->hw_resc.max_stat_ctxs;
11581 }
11582 
11583 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11584 {
11585 	return bp->hw_resc.max_cp_rings;
11586 }
11587 
11588 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11589 {
11590 	unsigned int cp = bp->hw_resc.max_cp_rings;
11591 
11592 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11593 		cp -= bnxt_get_ulp_msix_num(bp);
11594 
11595 	return cp;
11596 }
11597 
11598 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11599 {
11600 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11601 
11602 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11603 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11604 
11605 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11606 }
11607 
11608 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11609 {
11610 	bp->hw_resc.max_irqs = max_irqs;
11611 }
11612 
11613 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11614 {
11615 	unsigned int cp;
11616 
11617 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11618 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11619 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11620 	else
11621 		return cp - bp->cp_nr_rings;
11622 }
11623 
11624 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11625 {
11626 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11627 }
11628 
11629 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11630 {
11631 	int max_irq = bnxt_get_max_func_irqs(bp);
11632 	int total_req = bp->cp_nr_rings + num;
11633 
11634 	if (max_irq < total_req) {
11635 		num = max_irq - bp->cp_nr_rings;
11636 		if (num <= 0)
11637 			return 0;
11638 	}
11639 	return num;
11640 }
11641 
11642 static int bnxt_get_num_msix(struct bnxt *bp)
11643 {
11644 	if (!BNXT_NEW_RM(bp))
11645 		return bnxt_get_max_func_irqs(bp);
11646 
11647 	return bnxt_nq_rings_in_use(bp);
11648 }
11649 
11650 static int bnxt_init_int_mode(struct bnxt *bp)
11651 {
11652 	int i, total_vecs, max, rc, min = 1, ulp_msix, tx_cp, tbl_size;
11653 
11654 	total_vecs = bnxt_get_num_msix(bp);
11655 	max = bnxt_get_max_func_irqs(bp);
11656 	if (total_vecs > max)
11657 		total_vecs = max;
11658 
11659 	if (!total_vecs)
11660 		return 0;
11661 
11662 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11663 		min = 2;
11664 
11665 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11666 					   PCI_IRQ_MSIX);
11667 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11668 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11669 		rc = -ENODEV;
11670 		goto msix_setup_exit;
11671 	}
11672 
11673 	tbl_size = total_vecs;
11674 	if (pci_msix_can_alloc_dyn(bp->pdev))
11675 		tbl_size = max;
11676 	bp->irq_tbl = kzalloc_objs(*bp->irq_tbl, tbl_size);
11677 	if (!bp->irq_tbl) {
11678 		rc = -ENOMEM;
11679 		goto msix_setup_exit;
11680 	}
11681 
11682 	for (i = 0; i < total_vecs; i++)
11683 		bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11684 
11685 	bp->total_irqs = total_vecs;
11686 	/* Trim rings based upon num of vectors allocated */
11687 	rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11688 			     total_vecs - ulp_msix, min == 1);
11689 	if (rc)
11690 		goto msix_setup_exit;
11691 
11692 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11693 	bp->cp_nr_rings = (min == 1) ?
11694 			  max_t(int, tx_cp, bp->rx_nr_rings) :
11695 			  tx_cp + bp->rx_nr_rings;
11696 
11697 	return 0;
11698 
11699 msix_setup_exit:
11700 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11701 	kfree(bp->irq_tbl);
11702 	bp->irq_tbl = NULL;
11703 	pci_free_irq_vectors(bp->pdev);
11704 	return rc;
11705 }
11706 
11707 static void bnxt_clear_int_mode(struct bnxt *bp)
11708 {
11709 	pci_free_irq_vectors(bp->pdev);
11710 
11711 	kfree(bp->irq_tbl);
11712 	bp->irq_tbl = NULL;
11713 }
11714 
11715 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11716 {
11717 	struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA];
11718 	bool irq_cleared = false;
11719 	bool irq_change = false;
11720 	int tcs = bp->num_tc;
11721 	int irqs_required;
11722 	int rc;
11723 
11724 	if (!bnxt_need_reserve_rings(bp))
11725 		return 0;
11726 
11727 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(edev)) {
11728 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11729 
11730 		if (ulp_msix > bp->ulp_num_msix_want)
11731 			ulp_msix = bp->ulp_num_msix_want;
11732 		irqs_required = ulp_msix + bp->cp_nr_rings;
11733 	} else {
11734 		irqs_required = bnxt_get_num_msix(bp);
11735 	}
11736 
11737 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11738 		irq_change = true;
11739 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11740 			bnxt_ulp_irq_stop(bp);
11741 			bnxt_clear_int_mode(bp);
11742 			irq_cleared = true;
11743 		}
11744 	}
11745 	rc = __bnxt_reserve_rings(bp);
11746 	if (irq_cleared) {
11747 		if (!rc)
11748 			rc = bnxt_init_int_mode(bp);
11749 		bnxt_ulp_irq_restart(bp, rc);
11750 	} else if (irq_change && !rc) {
11751 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11752 			rc = -ENOSPC;
11753 	}
11754 	if (rc) {
11755 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11756 		return rc;
11757 	}
11758 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11759 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11760 		netdev_err(bp->dev, "tx ring reservation failure\n");
11761 		netdev_reset_tc(bp->dev);
11762 		bp->num_tc = 0;
11763 		if (bp->tx_nr_rings_xdp)
11764 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11765 		else
11766 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11767 		return -ENOMEM;
11768 	}
11769 	return 0;
11770 }
11771 
11772 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11773 {
11774 	struct bnxt_tx_ring_info *txr;
11775 	struct netdev_queue *txq;
11776 	struct bnxt_napi *bnapi;
11777 	int i;
11778 
11779 	bnapi = bp->bnapi[idx];
11780 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11781 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11782 		synchronize_net();
11783 
11784 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11785 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11786 			if (txq) {
11787 				__netif_tx_lock_bh(txq);
11788 				netif_tx_stop_queue(txq);
11789 				__netif_tx_unlock_bh(txq);
11790 			}
11791 		}
11792 
11793 		if (!bp->tph_mode)
11794 			continue;
11795 
11796 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11797 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11798 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11799 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11800 	}
11801 }
11802 
11803 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11804 {
11805 	struct bnxt_tx_ring_info *txr;
11806 	struct netdev_queue *txq;
11807 	struct bnxt_napi *bnapi;
11808 	int rc, i;
11809 
11810 	bnapi = bp->bnapi[idx];
11811 	/* All rings have been reserved and previously allocated.
11812 	 * Reallocating with the same parameters should never fail.
11813 	 */
11814 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11815 		if (!bp->tph_mode)
11816 			goto start_tx;
11817 
11818 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11819 		if (rc)
11820 			return rc;
11821 
11822 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11823 		if (rc)
11824 			return rc;
11825 
11826 		txr->tx_prod = 0;
11827 		txr->tx_cons = 0;
11828 		txr->tx_hw_cons = 0;
11829 		txr->kick_pending = 0;
11830 		txr->kick_txbd0 = NULL;
11831 start_tx:
11832 		WRITE_ONCE(txr->dev_state, 0);
11833 		synchronize_net();
11834 
11835 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11836 			continue;
11837 
11838 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11839 		if (txq)
11840 			netif_tx_start_queue(txq);
11841 	}
11842 
11843 	return 0;
11844 }
11845 
11846 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11847 				     const cpumask_t *mask)
11848 {
11849 	struct bnxt_irq *irq;
11850 	u16 tag;
11851 
11852 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11853 
11854 	cpumask_copy(irq->bp->ring_cpu_mask[irq->ring_nr], mask);
11855 	set_bit(irq->ring_nr, irq->bp->ring_affinity_set);
11856 
11857 #ifdef CONFIG_RFS_ACCEL
11858 	if (irq->bp->dev->rx_cpu_rmap && irq->ring_nr < irq->bp->rx_nr_rings) {
11859 		int err;
11860 
11861 		err = cpu_rmap_update(irq->bp->dev->rx_cpu_rmap, irq->ring_nr,
11862 				      mask);
11863 		if (err)
11864 			netdev_warn(irq->bp->dev,
11865 				    "aRFS rmap update failed: %d\n", err);
11866 	}
11867 #endif
11868 
11869 	if (!irq->bp->tph_mode)
11870 		return;
11871 
11872 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11873 		return;
11874 
11875 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11876 				cpumask_first(mask), &tag))
11877 		return;
11878 
11879 	WRITE_ONCE(irq->new_tag, tag);
11880 	bnxt_queue_sp_work(irq->bp, BNXT_TPH_UPDATE_SP_EVENT);
11881 }
11882 
11883 static void bnxt_irq_affinity_release(struct kref *ref)
11884 {
11885 	struct irq_affinity_notify *notify =
11886 		container_of(ref, struct irq_affinity_notify, kref);
11887 	struct bnxt_irq *irq;
11888 
11889 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11890 
11891 	if (!irq->bp->tph_mode)
11892 		return;
11893 
11894 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11895 		netdev_err(irq->bp->dev,
11896 			   "Setting ST=0 for MSIX entry %d failed\n",
11897 			   irq->msix_nr);
11898 		return;
11899 	}
11900 }
11901 
11902 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11903 {
11904 	irq_set_affinity_notifier(irq->vector, NULL);
11905 }
11906 
11907 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11908 {
11909 	struct irq_affinity_notify *notify;
11910 
11911 	irq->bp = bp;
11912 
11913 	/* Register IRQ affinity notifier */
11914 	notify = &irq->affinity_notify;
11915 	notify->irq = irq->vector;
11916 	notify->notify = bnxt_irq_affinity_notify;
11917 	notify->release = bnxt_irq_affinity_release;
11918 
11919 	irq_set_affinity_notifier(irq->vector, notify);
11920 }
11921 
11922 static int bnxt_alloc_ring_cpu_masks(struct bnxt *bp)
11923 {
11924 	int i;
11925 
11926 	bp->ring_cpu_mask = kzalloc_objs(*bp->ring_cpu_mask, bp->max_irqs);
11927 	if (!bp->ring_cpu_mask)
11928 		return -ENOMEM;
11929 
11930 	bp->ring_affinity_set = bitmap_zalloc(bp->max_irqs, GFP_KERNEL);
11931 	if (!bp->ring_affinity_set)
11932 		return -ENOMEM;
11933 
11934 	for (i = 0; i < bp->max_irqs; i++)
11935 		if (!zalloc_cpumask_var(&bp->ring_cpu_mask[i], GFP_KERNEL))
11936 			return -ENOMEM;
11937 
11938 	return 0;
11939 }
11940 
11941 static void bnxt_free_ring_cpu_masks(struct bnxt *bp)
11942 {
11943 	int i;
11944 
11945 	if (!bp->ring_cpu_mask)
11946 		return;
11947 
11948 	for (i = 0; i < bp->max_irqs; i++)
11949 		free_cpumask_var(bp->ring_cpu_mask[i]);
11950 
11951 	bitmap_free(bp->ring_affinity_set);
11952 	bp->ring_affinity_set = NULL;
11953 	kfree(bp->ring_cpu_mask);
11954 	bp->ring_cpu_mask = NULL;
11955 }
11956 
11957 static void bnxt_free_irq(struct bnxt *bp)
11958 {
11959 	struct bnxt_irq *irq;
11960 	int i;
11961 
11962 	if (!bp->irq_tbl || !bp->bnapi)
11963 		return;
11964 
11965 	for (i = 0; i < bp->cp_nr_rings; i++) {
11966 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11967 
11968 		irq = &bp->irq_tbl[map_idx];
11969 		if (irq->requested) {
11970 			bnxt_release_irq_notifier(irq);
11971 			irq_update_affinity_hint(irq->vector, NULL);
11972 			free_irq(irq->vector, bp->bnapi[i]);
11973 		}
11974 
11975 		irq->requested = 0;
11976 		irq->tag = 0;
11977 		irq->new_tag = 0;
11978 	}
11979 
11980 	/* Disable TPH support */
11981 	pcie_disable_tph(bp->pdev);
11982 	bp->tph_mode = 0;
11983 
11984 #ifdef CONFIG_RFS_ACCEL
11985 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11986 	bp->dev->rx_cpu_rmap = NULL;
11987 #endif
11988 }
11989 
11990 static int bnxt_request_irq(struct bnxt *bp)
11991 {
11992 	const int numa_node = dev_to_node(&bp->pdev->dev);
11993 	struct cpu_rmap *rmap = NULL;
11994 	int i, j, rc = 0;
11995 	unsigned long flags = 0;
11996 
11997 	rc = bnxt_setup_int_mode(bp);
11998 	if (rc) {
11999 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
12000 			   rc);
12001 		return rc;
12002 	}
12003 #ifdef CONFIG_RFS_ACCEL
12004 	rmap = bp->dev->rx_cpu_rmap;
12005 #endif
12006 
12007 	/* Enable TPH support as part of IRQ request */
12008 	if (BNXT_SUPPORTS_QUEUE_API(bp)) {
12009 		rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
12010 		if (!rc)
12011 			bp->tph_mode = PCI_TPH_ST_IV_MODE;
12012 	}
12013 
12014 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
12015 		struct cpumask *cpu_mask = bp->ring_cpu_mask[i];
12016 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
12017 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
12018 		u16 tag;
12019 
12020 		if (IS_ENABLED(CONFIG_RFS_ACCEL) &&
12021 		    rmap && bp->bnapi[i]->rx_ring) {
12022 			rc = irq_cpu_rmap_add(rmap, irq->vector);
12023 			if (rc)
12024 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
12025 					    j);
12026 			j++;
12027 		}
12028 
12029 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
12030 				 bp->bnapi[i]);
12031 		if (rc)
12032 			break;
12033 
12034 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
12035 		irq->requested = 1;
12036 		irq->msix_nr = map_idx;
12037 		irq->ring_nr = i;
12038 
12039 		/* Reuse the mask recorded before the IRQs were freed. Nothing
12040 		 * was recorded yet on the very first request, and the mask
12041 		 * may have gone stale if the CPUs went offline in between.
12042 		 */
12043 		if (!test_bit(i, bp->ring_affinity_set) ||
12044 		    !cpumask_intersects(cpu_mask, cpu_online_mask)) {
12045 			clear_bit(i, bp->ring_affinity_set);
12046 			cpumask_clear(cpu_mask);
12047 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
12048 					cpu_mask);
12049 		}
12050 
12051 		/* Init ST table entry if we can get the mapping */
12052 		if (!pcie_tph_get_cpu_st(bp->pdev, TPH_MEM_TYPE_VM,
12053 					 cpumask_first(cpu_mask), &tag)) {
12054 			pcie_tph_set_st_entry(bp->pdev, irq->msix_nr, tag);
12055 			irq->tag = tag;
12056 			irq->new_tag = tag;
12057 		}
12058 
12059 		bnxt_register_irq_notifier(bp, irq);
12060 
12061 		/* Only put the IRQ back where it was configured to be, our own
12062 		 * placement is just a hint, the core spreads within
12063 		 * irq_default_affinity which we know nothing about.
12064 		 * Set after installing the notifier, if we race with the user
12065 		 * it's better to overwrite than miss the notification.
12066 		 */
12067 		if (test_bit(i, bp->ring_affinity_set))
12068 			rc = irq_set_affinity_and_hint(irq->vector, cpu_mask);
12069 		else
12070 			rc = irq_update_affinity_hint(irq->vector, cpu_mask);
12071 		if (rc) {
12072 			netdev_warn(bp->dev,
12073 				    "Setting IRQ affinity failed, IRQ = %d\n",
12074 				    irq->vector);
12075 			break;
12076 		}
12077 	}
12078 	return rc;
12079 }
12080 
12081 static void bnxt_del_napi(struct bnxt *bp)
12082 {
12083 	int i;
12084 
12085 	if (!bp->bnapi)
12086 		return;
12087 
12088 	for (i = 0; i < bp->rx_nr_rings; i++)
12089 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
12090 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
12091 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
12092 
12093 	for (i = 0; i < bp->cp_nr_rings; i++) {
12094 		struct bnxt_napi *bnapi = bp->bnapi[i];
12095 
12096 		__netif_napi_del_locked(&bnapi->napi);
12097 	}
12098 	/* We called __netif_napi_del_locked(), we need
12099 	 * to respect an RCU grace period before freeing napi structures.
12100 	 */
12101 	synchronize_net();
12102 }
12103 
12104 static void bnxt_init_napi(struct bnxt *bp)
12105 {
12106 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
12107 	unsigned int cp_nr_rings = bp->cp_nr_rings;
12108 	struct bnxt_napi *bnapi;
12109 	int i;
12110 
12111 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
12112 		poll_fn = bnxt_poll_p5;
12113 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
12114 		cp_nr_rings--;
12115 
12116 	set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
12117 
12118 	for (i = 0; i < cp_nr_rings; i++) {
12119 		bnapi = bp->bnapi[i];
12120 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
12121 					     bnapi->index);
12122 	}
12123 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
12124 		bnapi = bp->bnapi[cp_nr_rings];
12125 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
12126 	}
12127 }
12128 
12129 static void bnxt_disable_napi(struct bnxt *bp)
12130 {
12131 	int i;
12132 
12133 	if (!bp->bnapi ||
12134 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
12135 		return;
12136 
12137 	for (i = 0; i < bp->cp_nr_rings; i++) {
12138 		struct bnxt_napi *bnapi = bp->bnapi[i];
12139 		struct bnxt_cp_ring_info *cpr;
12140 
12141 		cpr = &bnapi->cp_ring;
12142 		if (bnapi->tx_fault)
12143 			cpr->sw_stats->tx.tx_resets++;
12144 		if (bnapi->in_reset)
12145 			cpr->sw_stats->rx.rx_resets++;
12146 		napi_disable_locked(&bnapi->napi);
12147 	}
12148 }
12149 
12150 static void bnxt_enable_napi(struct bnxt *bp)
12151 {
12152 	int i;
12153 
12154 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
12155 	for (i = 0; i < bp->cp_nr_rings; i++) {
12156 		struct bnxt_napi *bnapi = bp->bnapi[i];
12157 		struct bnxt_cp_ring_info *cpr;
12158 
12159 		bnapi->tx_fault = 0;
12160 
12161 		cpr = &bnapi->cp_ring;
12162 		bnapi->in_reset = false;
12163 
12164 		if (bnapi->rx_ring) {
12165 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
12166 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
12167 		}
12168 		napi_enable_locked(&bnapi->napi);
12169 	}
12170 }
12171 
12172 void bnxt_tx_disable(struct bnxt *bp)
12173 {
12174 	int i;
12175 	struct bnxt_tx_ring_info *txr;
12176 
12177 	if (bp->tx_ring) {
12178 		for (i = 0; i < bp->tx_nr_rings; i++) {
12179 			txr = &bp->tx_ring[i];
12180 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
12181 		}
12182 	}
12183 	/* Make sure napi polls see @dev_state change */
12184 	synchronize_net();
12185 	/* Drop carrier first to prevent TX timeout */
12186 	netif_carrier_off(bp->dev);
12187 	/* Stop all TX queues */
12188 	netif_tx_disable(bp->dev);
12189 }
12190 
12191 void bnxt_tx_enable(struct bnxt *bp)
12192 {
12193 	int i;
12194 	struct bnxt_tx_ring_info *txr;
12195 
12196 	for (i = 0; i < bp->tx_nr_rings; i++) {
12197 		txr = &bp->tx_ring[i];
12198 		WRITE_ONCE(txr->dev_state, 0);
12199 	}
12200 	/* Make sure napi polls see @dev_state change */
12201 	synchronize_net();
12202 	netif_tx_wake_all_queues(bp->dev);
12203 	if (BNXT_LINK_IS_UP(bp))
12204 		netif_carrier_on(bp->dev);
12205 }
12206 
12207 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
12208 {
12209 	u8 active_fec = link_info->active_fec_sig_mode &
12210 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
12211 
12212 	switch (active_fec) {
12213 	default:
12214 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
12215 		return "None";
12216 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
12217 		return "Clause 74 BaseR";
12218 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
12219 		return "Clause 91 RS(528,514)";
12220 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
12221 		return "Clause 91 RS544_1XN";
12222 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
12223 		return "Clause 91 RS(544,514)";
12224 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
12225 		return "Clause 91 RS272_1XN";
12226 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
12227 		return "Clause 91 RS(272,257)";
12228 	}
12229 }
12230 
12231 static char *bnxt_link_down_reason(struct bnxt_link_info *link_info)
12232 {
12233 	u8 reason = link_info->link_down_reason;
12234 
12235 	/* Multiple bits can be set, we report 1 bit only in order of
12236 	 * priority.
12237 	 */
12238 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_RF)
12239 		return "(Remote fault)";
12240 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_OTP_SPEED_VIOLATION)
12241 		return "(OTP Speed limit violation)";
12242 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_CABLE_REMOVED)
12243 		return "(Cable removed)";
12244 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_MODULE_FAULT)
12245 		return "(Module fault)";
12246 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_BMC_REQUEST)
12247 		return "(BMC request down)";
12248 	return "";
12249 }
12250 
12251 void bnxt_report_link(struct bnxt *bp)
12252 {
12253 	if (BNXT_LINK_IS_UP(bp)) {
12254 		const char *signal = "";
12255 		const char *flow_ctrl;
12256 		const char *duplex;
12257 		u32 speed;
12258 		u16 fec;
12259 
12260 		netif_carrier_on(bp->dev);
12261 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
12262 		if (speed == SPEED_UNKNOWN) {
12263 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
12264 			return;
12265 		}
12266 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
12267 			duplex = "full";
12268 		else
12269 			duplex = "half";
12270 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
12271 			flow_ctrl = "ON - receive & transmit";
12272 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
12273 			flow_ctrl = "ON - transmit";
12274 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
12275 			flow_ctrl = "ON - receive";
12276 		else
12277 			flow_ctrl = "none";
12278 		if (bp->link_info.phy_qcfg_resp.option_flags &
12279 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
12280 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
12281 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
12282 			switch (sig_mode) {
12283 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
12284 				signal = "(NRZ) ";
12285 				break;
12286 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
12287 				signal = "(PAM4 56Gbps) ";
12288 				break;
12289 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
12290 				signal = "(PAM4 112Gbps) ";
12291 				break;
12292 			default:
12293 				break;
12294 			}
12295 		}
12296 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
12297 			    speed, signal, duplex, flow_ctrl);
12298 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
12299 			netdev_info(bp->dev, "EEE is %s\n",
12300 				    bp->eee.eee_active ? "active" :
12301 							 "not active");
12302 		fec = bp->link_info.fec_cfg;
12303 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
12304 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
12305 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
12306 				    bnxt_report_fec(&bp->link_info));
12307 	} else {
12308 		char *str = bnxt_link_down_reason(&bp->link_info);
12309 
12310 		netif_carrier_off(bp->dev);
12311 		netdev_err(bp->dev, "NIC Link is Down %s\n", str);
12312 	}
12313 }
12314 
12315 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
12316 {
12317 	if (!resp->supported_speeds_auto_mode &&
12318 	    !resp->supported_speeds_force_mode &&
12319 	    !resp->supported_pam4_speeds_auto_mode &&
12320 	    !resp->supported_pam4_speeds_force_mode &&
12321 	    !resp->supported_speeds2_auto_mode &&
12322 	    !resp->supported_speeds2_force_mode)
12323 		return true;
12324 	return false;
12325 }
12326 
12327 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
12328 {
12329 	struct bnxt_link_info *link_info = &bp->link_info;
12330 	struct hwrm_port_phy_qcaps_output *resp;
12331 	struct hwrm_port_phy_qcaps_input *req;
12332 	int rc = 0;
12333 
12334 	if (bp->hwrm_spec_code < 0x10201)
12335 		return 0;
12336 
12337 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
12338 	if (rc)
12339 		return rc;
12340 
12341 	resp = hwrm_req_hold(bp, req);
12342 	rc = hwrm_req_send(bp, req);
12343 	if (rc)
12344 		goto hwrm_phy_qcaps_exit;
12345 
12346 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
12347 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
12348 		struct ethtool_keee *eee = &bp->eee;
12349 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
12350 
12351 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
12352 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
12353 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
12354 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
12355 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
12356 	}
12357 
12358 	if (bp->hwrm_spec_code >= 0x10a01) {
12359 		if (bnxt_phy_qcaps_no_speed(resp)) {
12360 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
12361 			netdev_warn(bp->dev, "Ethernet link disabled\n");
12362 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
12363 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
12364 			netdev_info(bp->dev, "Ethernet link enabled\n");
12365 			/* Phy re-enabled, reprobe the speeds */
12366 			link_info->support_auto_speeds = 0;
12367 			link_info->support_pam4_auto_speeds = 0;
12368 			link_info->support_auto_speeds2 = 0;
12369 		}
12370 	}
12371 	if (resp->supported_speeds_auto_mode)
12372 		link_info->support_auto_speeds =
12373 			le16_to_cpu(resp->supported_speeds_auto_mode);
12374 	if (resp->supported_pam4_speeds_auto_mode)
12375 		link_info->support_pam4_auto_speeds =
12376 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
12377 	if (resp->supported_speeds2_auto_mode)
12378 		link_info->support_auto_speeds2 =
12379 			le16_to_cpu(resp->supported_speeds2_auto_mode);
12380 
12381 	bp->port_count = resp->port_cnt;
12382 
12383 hwrm_phy_qcaps_exit:
12384 	hwrm_req_drop(bp, req);
12385 	return rc;
12386 }
12387 
12388 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
12389 {
12390 	struct hwrm_port_mac_qcaps_output *resp;
12391 	struct hwrm_port_mac_qcaps_input *req;
12392 	int rc;
12393 
12394 	if (bp->hwrm_spec_code < 0x10a03)
12395 		return;
12396 
12397 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
12398 	if (rc)
12399 		return;
12400 
12401 	resp = hwrm_req_hold(bp, req);
12402 	rc = hwrm_req_send_silent(bp, req);
12403 	if (!rc)
12404 		bp->mac_flags = resp->flags;
12405 	hwrm_req_drop(bp, req);
12406 }
12407 
12408 static bool bnxt_support_dropped(u16 advertising, u16 supported)
12409 {
12410 	u16 diff = advertising ^ supported;
12411 
12412 	return ((supported | diff) != supported);
12413 }
12414 
12415 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
12416 {
12417 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
12418 
12419 	/* Check if any advertised speeds are no longer supported. The caller
12420 	 * holds the link_lock mutex, so we can modify link_info settings.
12421 	 */
12422 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12423 		if (bnxt_support_dropped(link_info->advertising,
12424 					 link_info->support_auto_speeds2)) {
12425 			link_info->advertising = link_info->support_auto_speeds2;
12426 			return true;
12427 		}
12428 		return false;
12429 	}
12430 	if (bnxt_support_dropped(link_info->advertising,
12431 				 link_info->support_auto_speeds)) {
12432 		link_info->advertising = link_info->support_auto_speeds;
12433 		return true;
12434 	}
12435 	if (bnxt_support_dropped(link_info->advertising_pam4,
12436 				 link_info->support_pam4_auto_speeds)) {
12437 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
12438 		return true;
12439 	}
12440 	return false;
12441 }
12442 
12443 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
12444 {
12445 	struct bnxt_link_info *link_info = &bp->link_info;
12446 	struct hwrm_port_phy_qcfg_output *resp;
12447 	struct hwrm_port_phy_qcfg_input *req;
12448 	u8 link_state = link_info->link_state;
12449 	bool support_changed;
12450 	int rc;
12451 
12452 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
12453 	if (rc)
12454 		return rc;
12455 
12456 	resp = hwrm_req_hold(bp, req);
12457 	rc = hwrm_req_send(bp, req);
12458 	if (rc) {
12459 		hwrm_req_drop(bp, req);
12460 		if (BNXT_VF(bp) && rc == -ENODEV) {
12461 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
12462 			rc = 0;
12463 		}
12464 		return rc;
12465 	}
12466 
12467 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
12468 	link_info->phy_link_status = resp->link;
12469 	link_info->duplex = resp->duplex_cfg;
12470 	if (bp->hwrm_spec_code >= 0x10800)
12471 		link_info->duplex = resp->duplex_state;
12472 	link_info->pause = resp->pause;
12473 	link_info->auto_mode = resp->auto_mode;
12474 	link_info->auto_pause_setting = resp->auto_pause;
12475 	link_info->lp_pause = resp->link_partner_adv_pause;
12476 	link_info->force_pause_setting = resp->force_pause;
12477 	link_info->duplex_setting = resp->duplex_cfg;
12478 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
12479 		link_info->link_speed = le16_to_cpu(resp->link_speed);
12480 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
12481 			link_info->active_lanes = resp->active_lanes;
12482 	} else {
12483 		link_info->link_speed = 0;
12484 		link_info->active_lanes = 0;
12485 	}
12486 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
12487 	link_info->force_pam4_link_speed =
12488 		le16_to_cpu(resp->force_pam4_link_speed);
12489 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
12490 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
12491 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
12492 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
12493 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
12494 	link_info->auto_pam4_link_speeds =
12495 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
12496 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
12497 	link_info->lp_auto_link_speeds =
12498 		le16_to_cpu(resp->link_partner_adv_speeds);
12499 	link_info->lp_auto_pam4_link_speeds =
12500 		resp->link_partner_pam4_adv_speeds;
12501 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
12502 	link_info->phy_ver[0] = resp->phy_maj;
12503 	link_info->phy_ver[1] = resp->phy_min;
12504 	link_info->phy_ver[2] = resp->phy_bld;
12505 	link_info->media_type = resp->media_type;
12506 	link_info->phy_type = resp->phy_type;
12507 	link_info->transceiver = resp->xcvr_pkg_type;
12508 	link_info->phy_addr = resp->eee_config_phy_addr &
12509 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
12510 	link_info->module_status = resp->module_status;
12511 	link_info->link_down_reason = resp->link_down_reason;
12512 
12513 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
12514 		struct ethtool_keee *eee = &bp->eee;
12515 		u16 fw_speeds;
12516 
12517 		eee->eee_active = 0;
12518 		if (resp->eee_config_phy_addr &
12519 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
12520 			eee->eee_active = 1;
12521 			fw_speeds = le16_to_cpu(
12522 				resp->link_partner_adv_eee_link_speed_mask);
12523 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
12524 		}
12525 
12526 		/* Pull initial EEE config */
12527 		if (!chng_link_state) {
12528 			if (resp->eee_config_phy_addr &
12529 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
12530 				eee->eee_enabled = 1;
12531 
12532 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
12533 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
12534 
12535 			if (resp->eee_config_phy_addr &
12536 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12537 				__le32 tmr;
12538 
12539 				eee->tx_lpi_enabled = 1;
12540 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12541 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12542 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12543 			}
12544 		}
12545 	}
12546 
12547 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12548 	if (bp->hwrm_spec_code >= 0x10504) {
12549 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12550 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12551 	}
12552 	/* TODO: need to add more logic to report VF link */
12553 	if (chng_link_state) {
12554 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12555 			link_info->link_state = BNXT_LINK_STATE_UP;
12556 		else
12557 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12558 		if (link_state != link_info->link_state)
12559 			bnxt_report_link(bp);
12560 	} else {
12561 		/* always link down if not require to update link state */
12562 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12563 	}
12564 	hwrm_req_drop(bp, req);
12565 
12566 	if (!BNXT_PHY_CFG_ABLE(bp))
12567 		return 0;
12568 
12569 	support_changed = bnxt_support_speed_dropped(link_info);
12570 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12571 		bnxt_hwrm_set_link_setting(bp, true, false);
12572 	return 0;
12573 }
12574 
12575 static void bnxt_get_port_module_status(struct bnxt *bp)
12576 {
12577 	struct bnxt_link_info *link_info = &bp->link_info;
12578 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12579 	u8 module_status;
12580 
12581 	if (bnxt_update_link(bp, true))
12582 		return;
12583 
12584 	module_status = link_info->module_status;
12585 	switch (module_status) {
12586 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12587 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12588 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12589 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12590 			    bp->pf.port_id);
12591 		if (bp->hwrm_spec_code >= 0x10201) {
12592 			netdev_warn(bp->dev, "Module part number %s\n",
12593 				    resp->phy_vendor_partnumber);
12594 		}
12595 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12596 			netdev_warn(bp->dev, "TX is disabled\n");
12597 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12598 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12599 	}
12600 }
12601 
12602 static void
12603 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12604 {
12605 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12606 		if (bp->hwrm_spec_code >= 0x10201)
12607 			req->auto_pause =
12608 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12609 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12610 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12611 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12612 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12613 		req->enables |=
12614 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12615 	} else {
12616 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12617 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12618 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12619 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12620 		req->enables |=
12621 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12622 		if (bp->hwrm_spec_code >= 0x10201) {
12623 			req->auto_pause = req->force_pause;
12624 			req->enables |= cpu_to_le32(
12625 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12626 		}
12627 	}
12628 }
12629 
12630 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12631 {
12632 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12633 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12634 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12635 			req->enables |=
12636 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12637 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12638 		} else if (bp->link_info.advertising) {
12639 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12640 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12641 		}
12642 		if (bp->link_info.advertising_pam4) {
12643 			req->enables |=
12644 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12645 			req->auto_link_pam4_speed_mask =
12646 				cpu_to_le16(bp->link_info.advertising_pam4);
12647 		}
12648 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12649 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12650 	} else {
12651 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12652 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12653 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12654 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12655 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12656 				   (u32)bp->link_info.req_link_speed);
12657 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12658 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12659 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12660 		} else {
12661 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12662 		}
12663 	}
12664 
12665 	/* tell chimp that the setting takes effect immediately */
12666 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12667 }
12668 
12669 int bnxt_hwrm_set_pause(struct bnxt *bp)
12670 {
12671 	struct hwrm_port_phy_cfg_input *req;
12672 	int rc;
12673 
12674 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12675 	if (rc)
12676 		return rc;
12677 
12678 	bnxt_hwrm_set_pause_common(bp, req);
12679 
12680 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12681 	    bp->link_info.force_link_chng)
12682 		bnxt_hwrm_set_link_common(bp, req);
12683 
12684 	rc = hwrm_req_send(bp, req);
12685 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12686 		/* since changing of pause setting doesn't trigger any link
12687 		 * change event, the driver needs to update the current pause
12688 		 * result upon successfully return of the phy_cfg command
12689 		 */
12690 		bp->link_info.pause =
12691 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12692 		bp->link_info.auto_pause_setting = 0;
12693 		if (!bp->link_info.force_link_chng)
12694 			bnxt_report_link(bp);
12695 	}
12696 	bp->link_info.force_link_chng = false;
12697 	return rc;
12698 }
12699 
12700 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12701 			      struct hwrm_port_phy_cfg_input *req)
12702 {
12703 	struct ethtool_keee *eee = &bp->eee;
12704 
12705 	if (eee->eee_enabled) {
12706 		u16 eee_speeds;
12707 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12708 
12709 		if (eee->tx_lpi_enabled)
12710 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12711 		else
12712 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12713 
12714 		req->flags |= cpu_to_le32(flags);
12715 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12716 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12717 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12718 	} else {
12719 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12720 	}
12721 }
12722 
12723 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12724 {
12725 	struct hwrm_port_phy_cfg_input *req;
12726 	int rc;
12727 
12728 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12729 	if (rc)
12730 		return rc;
12731 
12732 	if (set_pause)
12733 		bnxt_hwrm_set_pause_common(bp, req);
12734 
12735 	bnxt_hwrm_set_link_common(bp, req);
12736 
12737 	if (set_eee)
12738 		bnxt_hwrm_set_eee(bp, req);
12739 	return hwrm_req_send(bp, req);
12740 }
12741 
12742 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12743 {
12744 	struct hwrm_port_phy_cfg_input *req;
12745 	int rc;
12746 
12747 	if (!BNXT_SINGLE_PF(bp))
12748 		return 0;
12749 
12750 	if (pci_num_vf(bp->pdev) &&
12751 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12752 		return 0;
12753 
12754 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12755 	if (rc)
12756 		return rc;
12757 
12758 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12759 	rc = hwrm_req_send(bp, req);
12760 	if (!rc) {
12761 		mutex_lock(&bp->link_lock);
12762 		/* Device is not obliged link down in certain scenarios, even
12763 		 * when forced. Setting the state unknown is consistent with
12764 		 * driver startup and will force link state to be reported
12765 		 * during subsequent open based on PORT_PHY_QCFG.
12766 		 */
12767 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12768 		mutex_unlock(&bp->link_lock);
12769 	}
12770 	return rc;
12771 }
12772 
12773 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12774 {
12775 #ifdef CONFIG_TEE_BNXT_FW
12776 	int rc = tee_bnxt_fw_load();
12777 
12778 	if (rc)
12779 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12780 
12781 	return rc;
12782 #else
12783 	netdev_err(bp->dev, "OP-TEE not supported\n");
12784 	return -ENODEV;
12785 #endif
12786 }
12787 
12788 static int bnxt_try_recover_fw(struct bnxt *bp)
12789 {
12790 	if (bp->fw_health && bp->fw_health->status_reliable) {
12791 		int retry = 0, rc;
12792 		u32 sts;
12793 
12794 		do {
12795 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12796 			rc = bnxt_hwrm_poll(bp);
12797 			if (!BNXT_FW_IS_BOOTING(sts) &&
12798 			    !BNXT_FW_IS_RECOVERING(sts))
12799 				break;
12800 			retry++;
12801 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12802 
12803 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12804 			netdev_err(bp->dev,
12805 				   "Firmware not responding, status: 0x%x\n",
12806 				   sts);
12807 			rc = -ENODEV;
12808 		}
12809 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12810 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12811 			return bnxt_fw_reset_via_optee(bp);
12812 		}
12813 		return rc;
12814 	}
12815 
12816 	return -ENODEV;
12817 }
12818 
12819 void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12820 {
12821 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12822 
12823 	if (!BNXT_NEW_RM(bp))
12824 		return; /* no resource reservations required */
12825 
12826 	hw_resc->resv_cp_rings = 0;
12827 	hw_resc->resv_stat_ctxs = 0;
12828 	hw_resc->resv_irqs = 0;
12829 	hw_resc->resv_tx_rings = 0;
12830 	hw_resc->resv_rx_rings = 0;
12831 	hw_resc->resv_hw_ring_grps = 0;
12832 	hw_resc->resv_vnics = 0;
12833 	hw_resc->resv_rsscos_ctxs = 0;
12834 	if (!fw_reset) {
12835 		bp->tx_nr_rings = 0;
12836 		bp->rx_nr_rings = 0;
12837 	}
12838 }
12839 
12840 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12841 {
12842 	int rc;
12843 
12844 	if (!BNXT_NEW_RM(bp))
12845 		return 0; /* no resource reservations required */
12846 
12847 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12848 	if (rc)
12849 		netdev_err(bp->dev, "resc_qcaps failed\n");
12850 
12851 	bnxt_clear_reservations(bp, fw_reset);
12852 
12853 	return rc;
12854 }
12855 
12856 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12857 {
12858 	struct hwrm_func_drv_if_change_output *resp;
12859 	struct hwrm_func_drv_if_change_input *req;
12860 	bool resc_reinit = false;
12861 	bool caps_change = false;
12862 	int rc, retry = 0;
12863 	bool fw_reset;
12864 	u32 flags = 0;
12865 
12866 	fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT);
12867 	bp->fw_reset_state = 0;
12868 
12869 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12870 		return 0;
12871 
12872 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12873 	if (rc)
12874 		return rc;
12875 
12876 	if (up)
12877 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12878 	resp = hwrm_req_hold(bp, req);
12879 
12880 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12881 	while (retry < BNXT_FW_IF_RETRY) {
12882 		rc = hwrm_req_send(bp, req);
12883 		if (rc != -EAGAIN)
12884 			break;
12885 
12886 		msleep(50);
12887 		retry++;
12888 	}
12889 
12890 	if (rc == -EAGAIN) {
12891 		hwrm_req_drop(bp, req);
12892 		return rc;
12893 	} else if (!rc) {
12894 		flags = le32_to_cpu(resp->flags);
12895 	} else if (up) {
12896 		rc = bnxt_try_recover_fw(bp);
12897 		fw_reset = true;
12898 	}
12899 	hwrm_req_drop(bp, req);
12900 	if (rc)
12901 		return rc;
12902 
12903 	if (!up) {
12904 		bnxt_inv_fw_health_reg(bp);
12905 		return 0;
12906 	}
12907 
12908 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12909 		resc_reinit = true;
12910 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12911 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12912 		fw_reset = true;
12913 	else
12914 		bnxt_remap_fw_health_regs(bp);
12915 
12916 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12917 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12918 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12919 		return -ENODEV;
12920 	}
12921 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12922 		caps_change = true;
12923 
12924 	if (resc_reinit || fw_reset || caps_change) {
12925 		if (fw_reset || caps_change) {
12926 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12927 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12928 				bnxt_ulp_irq_stop(bp);
12929 			bnxt_free_ctx_mem(bp, false);
12930 			bnxt_dcb_free(bp);
12931 			rc = bnxt_fw_init_one(bp);
12932 			if (rc) {
12933 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12934 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12935 				return rc;
12936 			}
12937 			/* IRQ will be initialized later in bnxt_request_irq()*/
12938 			bnxt_clear_int_mode(bp);
12939 		}
12940 		rc = bnxt_cancel_reservations(bp, fw_reset);
12941 	}
12942 	return rc;
12943 }
12944 
12945 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12946 {
12947 	struct hwrm_port_led_qcaps_output *resp;
12948 	struct hwrm_port_led_qcaps_input *req;
12949 	struct bnxt_pf_info *pf = &bp->pf;
12950 	int rc;
12951 
12952 	bp->num_leds = 0;
12953 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12954 		return 0;
12955 
12956 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12957 	if (rc)
12958 		return rc;
12959 
12960 	req->port_id = cpu_to_le16(pf->port_id);
12961 	resp = hwrm_req_hold(bp, req);
12962 	rc = hwrm_req_send(bp, req);
12963 	if (rc) {
12964 		hwrm_req_drop(bp, req);
12965 		return rc;
12966 	}
12967 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12968 		int i;
12969 
12970 		bp->num_leds = resp->num_leds;
12971 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12972 						 bp->num_leds);
12973 		for (i = 0; i < bp->num_leds; i++) {
12974 			struct bnxt_led_info *led = &bp->leds[i];
12975 			__le16 caps = led->led_state_caps;
12976 
12977 			if (!led->led_group_id ||
12978 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12979 				bp->num_leds = 0;
12980 				break;
12981 			}
12982 		}
12983 	}
12984 	hwrm_req_drop(bp, req);
12985 	return 0;
12986 }
12987 
12988 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12989 {
12990 	struct hwrm_wol_filter_alloc_output *resp;
12991 	struct hwrm_wol_filter_alloc_input *req;
12992 	int rc;
12993 
12994 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12995 	if (rc)
12996 		return rc;
12997 
12998 	req->port_id = cpu_to_le16(bp->pf.port_id);
12999 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
13000 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
13001 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
13002 
13003 	resp = hwrm_req_hold(bp, req);
13004 	rc = hwrm_req_send(bp, req);
13005 	if (!rc)
13006 		bp->wol_filter_id = resp->wol_filter_id;
13007 	hwrm_req_drop(bp, req);
13008 	return rc;
13009 }
13010 
13011 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
13012 {
13013 	struct hwrm_wol_filter_free_input *req;
13014 	int rc;
13015 
13016 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
13017 	if (rc)
13018 		return rc;
13019 
13020 	req->port_id = cpu_to_le16(bp->pf.port_id);
13021 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
13022 	req->wol_filter_id = bp->wol_filter_id;
13023 
13024 	return hwrm_req_send(bp, req);
13025 }
13026 
13027 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
13028 {
13029 	struct hwrm_wol_filter_qcfg_output *resp;
13030 	struct hwrm_wol_filter_qcfg_input *req;
13031 	u16 next_handle = 0;
13032 	int rc;
13033 
13034 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
13035 	if (rc)
13036 		return rc;
13037 
13038 	req->port_id = cpu_to_le16(bp->pf.port_id);
13039 	req->handle = cpu_to_le16(handle);
13040 	resp = hwrm_req_hold(bp, req);
13041 	rc = hwrm_req_send(bp, req);
13042 	if (!rc) {
13043 		next_handle = le16_to_cpu(resp->next_handle);
13044 		if (next_handle != 0) {
13045 			if (resp->wol_type ==
13046 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
13047 				bp->wol = 1;
13048 				bp->wol_filter_id = resp->wol_filter_id;
13049 			}
13050 		}
13051 	}
13052 	hwrm_req_drop(bp, req);
13053 	return next_handle;
13054 }
13055 
13056 static void bnxt_get_wol_settings(struct bnxt *bp)
13057 {
13058 	u16 handle = 0;
13059 
13060 	bp->wol = 0;
13061 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
13062 		return;
13063 
13064 	do {
13065 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
13066 	} while (handle && handle != 0xffff);
13067 }
13068 
13069 static bool bnxt_eee_config_ok(struct bnxt *bp)
13070 {
13071 	struct ethtool_keee *eee = &bp->eee;
13072 	struct bnxt_link_info *link_info = &bp->link_info;
13073 
13074 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
13075 		return true;
13076 
13077 	if (eee->eee_enabled) {
13078 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
13079 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
13080 
13081 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
13082 
13083 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
13084 			eee->eee_enabled = 0;
13085 			return false;
13086 		}
13087 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
13088 			linkmode_and(eee->advertised, advertising,
13089 				     eee->supported);
13090 			return false;
13091 		}
13092 	}
13093 	return true;
13094 }
13095 
13096 static int bnxt_update_phy_setting(struct bnxt *bp)
13097 {
13098 	int rc;
13099 	bool update_link = false;
13100 	bool update_pause = false;
13101 	bool update_eee = false;
13102 	struct bnxt_link_info *link_info = &bp->link_info;
13103 
13104 	rc = bnxt_update_link(bp, true);
13105 	if (rc) {
13106 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
13107 			   rc);
13108 		return rc;
13109 	}
13110 	if (!BNXT_SINGLE_PF(bp))
13111 		return 0;
13112 
13113 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
13114 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
13115 	    link_info->req_flow_ctrl)
13116 		update_pause = true;
13117 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
13118 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
13119 		update_pause = true;
13120 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
13121 		if (BNXT_AUTO_MODE(link_info->auto_mode))
13122 			update_link = true;
13123 		if (bnxt_force_speed_updated(link_info))
13124 			update_link = true;
13125 		if (link_info->req_duplex != link_info->duplex_setting)
13126 			update_link = true;
13127 	} else {
13128 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
13129 			update_link = true;
13130 		if (bnxt_auto_speed_updated(link_info))
13131 			update_link = true;
13132 	}
13133 
13134 	/* The last close may have shutdown the link, so need to call
13135 	 * PHY_CFG to bring it back up.
13136 	 */
13137 	if (!BNXT_LINK_IS_UP(bp))
13138 		update_link = true;
13139 
13140 	if (!bnxt_eee_config_ok(bp))
13141 		update_eee = true;
13142 
13143 	if (update_link)
13144 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
13145 	else if (update_pause)
13146 		rc = bnxt_hwrm_set_pause(bp);
13147 	if (rc) {
13148 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
13149 			   rc);
13150 		return rc;
13151 	}
13152 
13153 	return rc;
13154 }
13155 
13156 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
13157 
13158 static int bnxt_reinit_after_abort(struct bnxt *bp)
13159 {
13160 	int rc;
13161 
13162 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13163 		return -EBUSY;
13164 
13165 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
13166 		return -ENODEV;
13167 
13168 	rc = bnxt_fw_init_one(bp);
13169 	if (!rc) {
13170 		bnxt_clear_int_mode(bp);
13171 		rc = bnxt_init_int_mode(bp);
13172 		if (!rc) {
13173 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13174 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
13175 		}
13176 	}
13177 	return rc;
13178 }
13179 
13180 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
13181 {
13182 	struct bnxt_ntuple_filter *ntp_fltr;
13183 	struct bnxt_l2_filter *l2_fltr;
13184 
13185 	if (list_empty(&fltr->list))
13186 		return;
13187 
13188 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
13189 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
13190 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
13191 		atomic_inc(&l2_fltr->refcnt);
13192 		ntp_fltr->l2_fltr = l2_fltr;
13193 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
13194 			bnxt_del_ntp_filter(bp, ntp_fltr);
13195 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
13196 				   fltr->sw_id);
13197 		}
13198 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
13199 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
13200 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
13201 			bnxt_del_l2_filter(bp, l2_fltr);
13202 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
13203 				   fltr->sw_id);
13204 		}
13205 	}
13206 }
13207 
13208 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
13209 {
13210 	struct bnxt_filter_base *usr_fltr, *tmp;
13211 
13212 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
13213 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
13214 }
13215 
13216 static int bnxt_set_xps_mapping(struct bnxt *bp)
13217 {
13218 	int numa_node = dev_to_node(&bp->pdev->dev);
13219 	unsigned int q_idx, map_idx, cpu, i;
13220 	const struct cpumask *cpu_mask_ptr;
13221 	int nr_cpus = num_online_cpus();
13222 	cpumask_t *q_map;
13223 	int rc = 0;
13224 
13225 	q_map = kzalloc_objs(*q_map, bp->tx_nr_rings_per_tc);
13226 	if (!q_map)
13227 		return -ENOMEM;
13228 
13229 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
13230 	 * Each TC has the same number of TX queues. The nth TX queue for each
13231 	 * TC will have the same CPU mask.
13232 	 */
13233 	for (i = 0; i < nr_cpus; i++) {
13234 		map_idx = i % bp->tx_nr_rings_per_tc;
13235 		cpu = cpumask_local_spread(i, numa_node);
13236 		cpu_mask_ptr = get_cpu_mask(cpu);
13237 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
13238 	}
13239 
13240 	/* Register CPU mask for each TX queue except the ones marked for XDP */
13241 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
13242 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
13243 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
13244 		if (rc) {
13245 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
13246 				    q_idx);
13247 			break;
13248 		}
13249 	}
13250 
13251 	kfree(q_map);
13252 
13253 	return rc;
13254 }
13255 
13256 static int bnxt_tx_nr_rings(struct bnxt *bp)
13257 {
13258 	return bp->num_tc ? bp->tx_nr_rings_per_tc * bp->num_tc :
13259 			    bp->tx_nr_rings_per_tc;
13260 }
13261 
13262 static int bnxt_tx_nr_rings_per_tc(struct bnxt *bp)
13263 {
13264 	return bp->num_tc ? bp->tx_nr_rings / bp->num_tc : bp->tx_nr_rings;
13265 }
13266 
13267 static void bnxt_set_xdp_tx_rings(struct bnxt *bp)
13268 {
13269 	bp->tx_nr_rings_xdp = bp->tx_nr_rings_per_tc;
13270 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
13271 }
13272 
13273 static void bnxt_adj_tx_rings(struct bnxt *bp)
13274 {
13275 	/* Make adjustments if reserved TX rings are less than requested */
13276 	bp->tx_nr_rings -= bp->tx_nr_rings_xdp;
13277 	bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
13278 	if (bp->tx_nr_rings_xdp)
13279 		bnxt_set_xdp_tx_rings(bp);
13280 }
13281 
13282 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13283 {
13284 	int rc = 0;
13285 
13286 	netif_carrier_off(bp->dev);
13287 	if (irq_re_init) {
13288 		/* Reserve rings now if none were reserved at driver probe. */
13289 		rc = bnxt_init_dflt_ring_mode(bp);
13290 		if (rc) {
13291 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
13292 			return rc;
13293 		}
13294 	}
13295 	rc = bnxt_reserve_rings(bp, irq_re_init);
13296 	if (rc)
13297 		return rc;
13298 
13299 	bnxt_adj_tx_rings(bp);
13300 	rc = bnxt_alloc_mem(bp, irq_re_init);
13301 	if (rc) {
13302 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13303 		goto open_err_free_mem;
13304 	}
13305 
13306 	if (irq_re_init) {
13307 		bnxt_init_napi(bp);
13308 		rc = bnxt_request_irq(bp);
13309 		if (rc) {
13310 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
13311 			goto open_err_irq;
13312 		}
13313 	}
13314 
13315 	rc = bnxt_init_nic(bp, irq_re_init);
13316 	if (rc) {
13317 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13318 		goto open_err_irq;
13319 	}
13320 
13321 	bnxt_enable_napi(bp);
13322 	bnxt_debug_dev_init(bp);
13323 
13324 	if (link_re_init) {
13325 		mutex_lock(&bp->link_lock);
13326 		rc = bnxt_update_phy_setting(bp);
13327 		mutex_unlock(&bp->link_lock);
13328 		if (rc) {
13329 			netdev_warn(bp->dev, "failed to update phy settings\n");
13330 			if (BNXT_SINGLE_PF(bp)) {
13331 				bp->link_info.phy_retry = true;
13332 				bp->link_info.phy_retry_expires =
13333 					jiffies + 5 * HZ;
13334 			}
13335 		}
13336 	}
13337 
13338 	if (irq_re_init) {
13339 		udp_tunnel_nic_reset_ntf(bp->dev);
13340 		rc = bnxt_set_xps_mapping(bp);
13341 		if (rc)
13342 			netdev_warn(bp->dev, "failed to set xps mapping\n");
13343 	}
13344 
13345 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
13346 		if (!static_key_enabled(&bnxt_xdp_locking_key))
13347 			static_branch_enable(&bnxt_xdp_locking_key);
13348 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
13349 		static_branch_disable(&bnxt_xdp_locking_key);
13350 	}
13351 	set_bit(BNXT_STATE_OPEN, &bp->state);
13352 	bnxt_enable_int(bp);
13353 	/* Enable TX queues */
13354 	bnxt_tx_enable(bp);
13355 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13356 	/* Poll link status and check for SFP+ module status */
13357 	mutex_lock(&bp->link_lock);
13358 	bnxt_get_port_module_status(bp);
13359 	mutex_unlock(&bp->link_lock);
13360 
13361 	/* VF-reps may need to be re-opened after the PF is re-opened */
13362 	if (BNXT_PF(bp))
13363 		bnxt_vf_reps_open(bp);
13364 	bnxt_ptp_init_rtc(bp, true);
13365 	bnxt_ptp_cfg_tstamp_filters(bp);
13366 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13367 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
13368 	bnxt_cfg_usr_fltrs(bp);
13369 	return 0;
13370 
13371 open_err_irq:
13372 	bnxt_del_napi(bp);
13373 
13374 open_err_free_mem:
13375 	bnxt_free_skbs(bp);
13376 	bnxt_free_irq(bp);
13377 	bnxt_free_mem(bp, true);
13378 	return rc;
13379 }
13380 
13381 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13382 {
13383 	int rc = 0;
13384 
13385 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
13386 		rc = -EIO;
13387 	if (!rc)
13388 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
13389 	if (rc) {
13390 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
13391 		netif_close(bp->dev);
13392 	}
13393 	return rc;
13394 }
13395 
13396 /* netdev instance lock held, open the NIC half way by allocating all
13397  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
13398  * for offline self tests.
13399  */
13400 int bnxt_half_open_nic(struct bnxt *bp)
13401 {
13402 	int rc = 0;
13403 
13404 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13405 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
13406 		rc = -ENODEV;
13407 		goto half_open_err;
13408 	}
13409 
13410 	rc = bnxt_alloc_mem(bp, true);
13411 	if (rc) {
13412 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13413 		goto half_open_err;
13414 	}
13415 	bnxt_init_napi(bp);
13416 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13417 	rc = bnxt_init_nic(bp, true);
13418 	if (rc) {
13419 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13420 		bnxt_del_napi(bp);
13421 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13422 		goto half_open_err;
13423 	}
13424 	return 0;
13425 
13426 half_open_err:
13427 	bnxt_free_skbs(bp);
13428 	bnxt_free_mem(bp, true);
13429 	netif_close(bp->dev);
13430 	return rc;
13431 }
13432 
13433 /* netdev instance lock held, this call can only be made after a previous
13434  * successful call to bnxt_half_open_nic().
13435  */
13436 void bnxt_half_close_nic(struct bnxt *bp)
13437 {
13438 	bnxt_hwrm_resource_free(bp, false, true);
13439 	bnxt_del_napi(bp);
13440 	bnxt_free_skbs(bp);
13441 	bnxt_free_mem(bp, true);
13442 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13443 }
13444 
13445 void bnxt_reenable_sriov(struct bnxt *bp)
13446 {
13447 	if (BNXT_PF(bp)) {
13448 		struct bnxt_pf_info *pf = &bp->pf;
13449 		int n = pf->active_vfs;
13450 
13451 		if (n)
13452 			bnxt_cfg_hw_sriov(bp, &n, true);
13453 	}
13454 }
13455 
13456 static int bnxt_open(struct net_device *dev)
13457 {
13458 	struct bnxt *bp = netdev_priv(dev);
13459 	int rc;
13460 
13461 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13462 		rc = bnxt_reinit_after_abort(bp);
13463 		if (rc) {
13464 			if (rc == -EBUSY)
13465 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
13466 			else
13467 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
13468 			return -ENODEV;
13469 		}
13470 	}
13471 
13472 	rc = bnxt_hwrm_if_change(bp, true);
13473 	if (rc)
13474 		return rc;
13475 
13476 	rc = __bnxt_open_nic(bp, true, true);
13477 	if (rc) {
13478 		bnxt_hwrm_if_change(bp, false);
13479 	} else {
13480 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
13481 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13482 				bnxt_queue_sp_work(bp,
13483 						   BNXT_RESTART_ULP_SP_EVENT);
13484 		}
13485 	}
13486 
13487 	return rc;
13488 }
13489 
13490 static bool bnxt_drv_busy(struct bnxt *bp)
13491 {
13492 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
13493 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
13494 }
13495 
13496 static void bnxt_get_ring_stats(struct bnxt *bp,
13497 				struct rtnl_link_stats64 *stats);
13498 
13499 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
13500 			     bool link_re_init)
13501 {
13502 	/* Close the VF-reps before closing PF */
13503 	if (BNXT_PF(bp))
13504 		bnxt_vf_reps_close(bp);
13505 
13506 	/* Change device state to avoid TX queue wake up's */
13507 	bnxt_tx_disable(bp);
13508 
13509 	clear_bit(BNXT_STATE_OPEN, &bp->state);
13510 	smp_mb__after_atomic();
13511 	while (bnxt_drv_busy(bp))
13512 		msleep(20);
13513 
13514 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13515 		bnxt_clear_rss_ctxs(bp);
13516 	/* Flush rings and disable interrupts */
13517 	bnxt_shutdown_nic(bp, irq_re_init);
13518 
13519 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
13520 
13521 	bnxt_debug_dev_exit(bp);
13522 	bnxt_disable_napi(bp);
13523 	timer_delete_sync(&bp->timer);
13524 	bnxt_free_skbs(bp);
13525 
13526 	/* Save ring stats before shutdown */
13527 	if (bp->bnapi && irq_re_init) {
13528 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
13529 		bnxt_get_ring_drv_stats(bp, &bp->ring_drv_stats_prev);
13530 	}
13531 	if (irq_re_init) {
13532 		bnxt_free_irq(bp);
13533 		bnxt_del_napi(bp);
13534 	}
13535 	bnxt_free_mem(bp, irq_re_init);
13536 }
13537 
13538 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13539 {
13540 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13541 		/* If we get here, it means firmware reset is in progress
13542 		 * while we are trying to close.  We can safely proceed with
13543 		 * the close because we are holding netdev instance lock.
13544 		 * Some firmware messages may fail as we proceed to close.
13545 		 * We set the ABORT_ERR flag here so that the FW reset thread
13546 		 * will later abort when it gets the netdev instance lock
13547 		 * and sees the flag.
13548 		 */
13549 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
13550 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13551 	}
13552 
13553 #ifdef CONFIG_BNXT_SRIOV
13554 	if (bp->sriov_cfg) {
13555 		int rc;
13556 
13557 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
13558 						      !bp->sriov_cfg,
13559 						      BNXT_SRIOV_CFG_WAIT_TMO);
13560 		if (!rc)
13561 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13562 		else if (rc < 0)
13563 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13564 	}
13565 #endif
13566 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13567 }
13568 
13569 static int bnxt_close(struct net_device *dev)
13570 {
13571 	struct bnxt *bp = netdev_priv(dev);
13572 
13573 	bnxt_close_nic(bp, true, true);
13574 	bnxt_hwrm_shutdown_link(bp);
13575 	bnxt_hwrm_if_change(bp, false);
13576 	return 0;
13577 }
13578 
13579 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13580 				   u16 *val)
13581 {
13582 	struct hwrm_port_phy_mdio_read_output *resp;
13583 	struct hwrm_port_phy_mdio_read_input *req;
13584 	int rc;
13585 
13586 	if (bp->hwrm_spec_code < 0x10a00)
13587 		return -EOPNOTSUPP;
13588 
13589 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13590 	if (rc)
13591 		return rc;
13592 
13593 	req->port_id = cpu_to_le16(bp->pf.port_id);
13594 	req->phy_addr = phy_addr;
13595 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13596 	if (mdio_phy_id_is_c45(phy_addr)) {
13597 		req->cl45_mdio = 1;
13598 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13599 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13600 		req->reg_addr = cpu_to_le16(reg);
13601 	}
13602 
13603 	resp = hwrm_req_hold(bp, req);
13604 	rc = hwrm_req_send(bp, req);
13605 	if (!rc)
13606 		*val = le16_to_cpu(resp->reg_data);
13607 	hwrm_req_drop(bp, req);
13608 	return rc;
13609 }
13610 
13611 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13612 				    u16 val)
13613 {
13614 	struct hwrm_port_phy_mdio_write_input *req;
13615 	int rc;
13616 
13617 	if (bp->hwrm_spec_code < 0x10a00)
13618 		return -EOPNOTSUPP;
13619 
13620 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13621 	if (rc)
13622 		return rc;
13623 
13624 	req->port_id = cpu_to_le16(bp->pf.port_id);
13625 	req->phy_addr = phy_addr;
13626 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13627 	if (mdio_phy_id_is_c45(phy_addr)) {
13628 		req->cl45_mdio = 1;
13629 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13630 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13631 		req->reg_addr = cpu_to_le16(reg);
13632 	}
13633 	req->reg_data = cpu_to_le16(val);
13634 
13635 	return hwrm_req_send(bp, req);
13636 }
13637 
13638 /* netdev instance lock held */
13639 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13640 {
13641 	struct mii_ioctl_data *mdio = if_mii(ifr);
13642 	struct bnxt *bp = netdev_priv(dev);
13643 	int rc;
13644 
13645 	switch (cmd) {
13646 	case SIOCGMIIPHY:
13647 		mdio->phy_id = bp->link_info.phy_addr;
13648 
13649 		fallthrough;
13650 	case SIOCGMIIREG: {
13651 		u16 mii_regval = 0;
13652 
13653 		if (!netif_running(dev))
13654 			return -EAGAIN;
13655 
13656 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13657 					     &mii_regval);
13658 		mdio->val_out = mii_regval;
13659 		return rc;
13660 	}
13661 
13662 	case SIOCSMIIREG:
13663 		if (!netif_running(dev))
13664 			return -EAGAIN;
13665 
13666 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13667 						mdio->val_in);
13668 
13669 	default:
13670 		/* do nothing */
13671 		break;
13672 	}
13673 	return -EOPNOTSUPP;
13674 }
13675 
13676 static void bnxt_get_ring_stats(struct bnxt *bp,
13677 				struct rtnl_link_stats64 *stats)
13678 {
13679 	int i;
13680 
13681 	for (i = 0; i < bp->cp_nr_rings; i++) {
13682 		struct bnxt_napi *bnapi = bp->bnapi[i];
13683 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13684 		u64 *sw = cpr->stats.sw_stats;
13685 
13686 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13687 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13688 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13689 
13690 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13691 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13692 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13693 
13694 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13695 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13696 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13697 
13698 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13699 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13700 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13701 
13702 		stats->rx_missed_errors +=
13703 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13704 
13705 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13706 
13707 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13708 
13709 		stats->rx_dropped +=
13710 			cpr->sw_stats->rx.rx_netpoll_discards +
13711 			cpr->sw_stats->rx.rx_oom_discards;
13712 	}
13713 }
13714 
13715 static void bnxt_add_prev_stats(struct bnxt *bp,
13716 				struct rtnl_link_stats64 *stats)
13717 {
13718 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13719 
13720 	stats->rx_packets += prev_stats->rx_packets;
13721 	stats->tx_packets += prev_stats->tx_packets;
13722 	stats->rx_bytes += prev_stats->rx_bytes;
13723 	stats->tx_bytes += prev_stats->tx_bytes;
13724 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13725 	stats->multicast += prev_stats->multicast;
13726 	stats->rx_dropped += prev_stats->rx_dropped;
13727 	stats->tx_dropped += prev_stats->tx_dropped;
13728 }
13729 
13730 static void
13731 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13732 {
13733 	struct bnxt *bp = netdev_priv(dev);
13734 
13735 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13736 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13737 	 * we check the BNXT_STATE_OPEN flag.
13738 	 */
13739 	smp_mb__after_atomic();
13740 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13741 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13742 		*stats = bp->net_stats_prev;
13743 		return;
13744 	}
13745 
13746 	bnxt_sync_ring_stats(bp);
13747 	bnxt_get_ring_stats(bp, stats);
13748 	bnxt_add_prev_stats(bp, stats);
13749 
13750 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13751 		u64 *rx = bp->port_stats.sw_stats;
13752 		u64 *tx = bp->port_stats.sw_stats +
13753 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13754 
13755 		stats->rx_crc_errors =
13756 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13757 		stats->rx_frame_errors =
13758 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13759 		stats->rx_length_errors =
13760 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13761 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13762 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13763 		stats->rx_errors =
13764 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13765 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13766 		stats->collisions =
13767 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13768 		stats->tx_fifo_errors =
13769 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13770 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13771 	}
13772 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13773 }
13774 
13775 static void bnxt_get_one_ring_drv_stats(struct bnxt *bp,
13776 					struct bnxt_total_ring_drv_stats *stats,
13777 					struct bnxt_cp_ring_info *cpr)
13778 {
13779 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13780 	u64 *hw_stats = cpr->stats.sw_stats;
13781 
13782 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13783 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13784 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13785 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13786 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13787 	stats->rx_total_ring_discards +=
13788 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13789 	stats->rx_total_hw_gro_packets += sw_stats->rx.rx_hw_gro_packets;
13790 	stats->rx_total_hw_gro_wire_packets += sw_stats->rx.rx_hw_gro_wire_packets;
13791 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13792 	stats->tx_total_ring_discards +=
13793 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13794 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13795 }
13796 
13797 void bnxt_get_ring_drv_stats(struct bnxt *bp,
13798 			     struct bnxt_total_ring_drv_stats *stats)
13799 {
13800 	int i;
13801 
13802 	for (i = 0; i < bp->cp_nr_rings; i++)
13803 		bnxt_get_one_ring_drv_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13804 }
13805 
13806 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask,
13807 				 const struct netdev_hw_addr_list *mc)
13808 {
13809 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13810 	struct netdev_hw_addr *ha;
13811 	u8 *haddr;
13812 	int mc_count = 0;
13813 	bool update = false;
13814 	int off = 0;
13815 
13816 	netdev_hw_addr_list_for_each(ha, mc) {
13817 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13818 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13819 			vnic->mc_list_count = 0;
13820 			return false;
13821 		}
13822 		haddr = ha->addr;
13823 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13824 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13825 			update = true;
13826 		}
13827 		off += ETH_ALEN;
13828 		mc_count++;
13829 	}
13830 	if (mc_count)
13831 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13832 
13833 	if (mc_count != vnic->mc_list_count) {
13834 		vnic->mc_list_count = mc_count;
13835 		update = true;
13836 	}
13837 	return update;
13838 }
13839 
13840 static bool bnxt_uc_list_updated(struct bnxt *bp,
13841 				 const struct netdev_hw_addr_list *uc)
13842 {
13843 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13844 	struct netdev_hw_addr *ha;
13845 	int off = 0;
13846 
13847 	if (netdev_hw_addr_list_count(uc) != (vnic->uc_filter_count - 1))
13848 		return true;
13849 
13850 	netdev_hw_addr_list_for_each(ha, uc) {
13851 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13852 			return true;
13853 
13854 		off += ETH_ALEN;
13855 	}
13856 	return false;
13857 }
13858 
13859 static int bnxt_set_rx_mode(struct net_device *dev,
13860 			    struct netdev_hw_addr_list *uc,
13861 			    struct netdev_hw_addr_list *mc)
13862 {
13863 	struct bnxt *bp = netdev_priv(dev);
13864 	struct bnxt_vnic_info *vnic;
13865 	bool mc_update = false;
13866 	bool uc_update;
13867 	u32 mask;
13868 
13869 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13870 		return 0;
13871 
13872 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13873 	mask = vnic->rx_mask;
13874 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13875 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13876 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13877 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13878 
13879 	if (dev->flags & IFF_PROMISC)
13880 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13881 
13882 	uc_update = bnxt_uc_list_updated(bp, uc);
13883 
13884 	if (dev->flags & IFF_BROADCAST)
13885 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13886 	if (dev->flags & IFF_ALLMULTI) {
13887 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13888 		vnic->mc_list_count = 0;
13889 	} else if (dev->flags & IFF_MULTICAST) {
13890 		mc_update = bnxt_mc_list_updated(bp, &mask, mc);
13891 	}
13892 
13893 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13894 		vnic->rx_mask = mask;
13895 
13896 		return bnxt_cfg_rx_mode(bp, uc, uc_update);
13897 	}
13898 
13899 	return 0;
13900 }
13901 
13902 static int bnxt_cfg_rx_mode(struct bnxt *bp, struct netdev_hw_addr_list *uc,
13903 			    bool uc_update)
13904 {
13905 	struct net_device *dev = bp->dev;
13906 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13907 	struct netdev_hw_addr *ha;
13908 	int i, off = 0, rc;
13909 
13910 	if (!uc_update)
13911 		goto skip_uc;
13912 
13913 	for (i = 1; i < vnic->uc_filter_count; i++) {
13914 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13915 
13916 		bnxt_hwrm_l2_filter_free(bp, fltr);
13917 		bnxt_del_l2_filter(bp, fltr);
13918 	}
13919 
13920 	vnic->uc_filter_count = 1;
13921 
13922 	netif_addr_lock_bh(dev);
13923 	if (netdev_hw_addr_list_count(uc) > (BNXT_MAX_UC_ADDRS - 1)) {
13924 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13925 	} else {
13926 		netdev_hw_addr_list_for_each(ha, uc) {
13927 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13928 			off += ETH_ALEN;
13929 			vnic->uc_filter_count++;
13930 		}
13931 	}
13932 	netif_addr_unlock_bh(dev);
13933 
13934 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13935 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13936 		if (rc) {
13937 			if (BNXT_VF(bp) && rc == -ENODEV) {
13938 				netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13939 				rc = -EAGAIN;
13940 			} else if (rc == -EAGAIN) {
13941 				netdev_warn(bp->dev, "FW busy while setting vnic filter, will retry\n");
13942 			} else {
13943 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13944 			}
13945 			vnic->uc_filter_count = i;
13946 			return rc;
13947 		}
13948 	}
13949 
13950 skip_uc:
13951 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13952 	    !bnxt_promisc_ok(bp))
13953 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13954 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13955 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13956 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13957 			    rc);
13958 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13959 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13960 		vnic->mc_list_count = 0;
13961 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13962 	}
13963 	if (rc)
13964 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13965 			   rc);
13966 
13967 	return rc;
13968 }
13969 
13970 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13971 {
13972 #ifdef CONFIG_BNXT_SRIOV
13973 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13974 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13975 
13976 		/* No minimum rings were provisioned by the PF.  Don't
13977 		 * reserve rings by default when device is down.
13978 		 */
13979 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13980 			return true;
13981 
13982 		if (!netif_running(bp->dev))
13983 			return false;
13984 	}
13985 #endif
13986 	return true;
13987 }
13988 
13989 /* If the chip and firmware supports RFS */
13990 static bool bnxt_rfs_supported(struct bnxt *bp)
13991 {
13992 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13993 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13994 			return true;
13995 		return false;
13996 	}
13997 	/* 212 firmware is broken for aRFS */
13998 	if (BNXT_FW_MAJ(bp) == 212)
13999 		return false;
14000 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
14001 		return true;
14002 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
14003 		return true;
14004 	return false;
14005 }
14006 
14007 /* If runtime conditions support RFS */
14008 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
14009 {
14010 	struct bnxt_hw_rings hwr = {0};
14011 	int max_vnics, max_rss_ctxs;
14012 
14013 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14014 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
14015 		return bnxt_rfs_supported(bp);
14016 
14017 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
14018 		return false;
14019 
14020 	hwr.grp = bp->rx_nr_rings;
14021 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
14022 	if (new_rss_ctx)
14023 		hwr.vnic++;
14024 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14025 	max_vnics = bnxt_get_max_func_vnics(bp);
14026 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
14027 
14028 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
14029 		if (bp->rx_nr_rings > 1)
14030 			netdev_warn(bp->dev,
14031 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
14032 				    min(max_rss_ctxs - 1, max_vnics - 1));
14033 		return false;
14034 	}
14035 
14036 	if (!BNXT_NEW_RM(bp))
14037 		return true;
14038 
14039 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
14040 	 * issue that will mess up the default VNIC if we reduce the
14041 	 * reservations.
14042 	 */
14043 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
14044 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
14045 		return true;
14046 
14047 	bnxt_hwrm_reserve_rings(bp, &hwr);
14048 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
14049 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
14050 		return true;
14051 
14052 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
14053 	hwr.vnic = 1;
14054 	hwr.rss_ctx = 0;
14055 	bnxt_hwrm_reserve_rings(bp, &hwr);
14056 	return false;
14057 }
14058 
14059 static netdev_features_t bnxt_fix_features(struct net_device *dev,
14060 					   netdev_features_t features)
14061 {
14062 	struct bnxt *bp = netdev_priv(dev);
14063 	netdev_features_t vlan_features;
14064 
14065 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
14066 		features &= ~NETIF_F_NTUPLE;
14067 
14068 	if ((features & NETIF_F_GSO_UDP_L4) &&
14069 	    !(bp->flags & BNXT_FLAG_UDP_GSO_CAP) &&
14070 	    bp->tx_ring_size < 2 * BNXT_SW_USO_MAX_DESCS)
14071 		features &= ~NETIF_F_GSO_UDP_L4;
14072 
14073 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
14074 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
14075 
14076 	if (!(features & NETIF_F_GRO))
14077 		features &= ~NETIF_F_GRO_HW;
14078 
14079 	if (features & NETIF_F_GRO_HW)
14080 		features &= ~NETIF_F_LRO;
14081 
14082 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
14083 	 * turned on or off together.
14084 	 */
14085 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
14086 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
14087 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
14088 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
14089 		else if (vlan_features)
14090 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
14091 	}
14092 #ifdef CONFIG_BNXT_SRIOV
14093 	if (BNXT_VF(bp) && bp->vf.vlan)
14094 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
14095 #endif
14096 	return features;
14097 }
14098 
14099 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
14100 				bool link_re_init, u32 flags, bool update_tpa)
14101 {
14102 	bnxt_close_nic(bp, irq_re_init, link_re_init);
14103 	bp->flags = flags;
14104 	if (update_tpa)
14105 		bnxt_set_ring_params(bp);
14106 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
14107 }
14108 
14109 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
14110 {
14111 	bool update_tpa = false, update_ntuple = false;
14112 	struct bnxt *bp = netdev_priv(dev);
14113 	u32 flags = bp->flags;
14114 	u32 changes;
14115 	int rc = 0;
14116 	bool re_init = false;
14117 
14118 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
14119 				   bnxt_min_tx_desc_cnt(bp, features));
14120 
14121 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
14122 	if (features & NETIF_F_GRO_HW)
14123 		flags |= BNXT_FLAG_GRO;
14124 	else if (features & NETIF_F_LRO)
14125 		flags |= BNXT_FLAG_LRO;
14126 
14127 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
14128 		flags &= ~BNXT_FLAG_TPA;
14129 
14130 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
14131 		flags |= BNXT_FLAG_STRIP_VLAN;
14132 
14133 	if (features & NETIF_F_NTUPLE)
14134 		flags |= BNXT_FLAG_RFS;
14135 	else
14136 		bnxt_clear_usr_fltrs(bp, true);
14137 
14138 	changes = flags ^ bp->flags;
14139 	if (changes & BNXT_FLAG_TPA) {
14140 		update_tpa = true;
14141 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
14142 		    (flags & BNXT_FLAG_TPA) == 0 ||
14143 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14144 			re_init = true;
14145 	}
14146 
14147 	if (changes & ~BNXT_FLAG_TPA)
14148 		re_init = true;
14149 
14150 	if (changes & BNXT_FLAG_RFS)
14151 		update_ntuple = true;
14152 
14153 	if (flags != bp->flags) {
14154 		u32 old_flags = bp->flags;
14155 
14156 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14157 			bp->flags = flags;
14158 			if (update_tpa)
14159 				bnxt_set_ring_params(bp);
14160 			return rc;
14161 		}
14162 
14163 		if (update_ntuple)
14164 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
14165 
14166 		if (re_init)
14167 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
14168 
14169 		if (update_tpa) {
14170 			bp->flags = flags;
14171 			rc = bnxt_set_tpa(bp,
14172 					  (flags & BNXT_FLAG_TPA) ?
14173 					  true : false);
14174 			if (rc)
14175 				bp->flags = old_flags;
14176 		}
14177 	}
14178 	return rc;
14179 }
14180 
14181 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
14182 			      u8 **nextp)
14183 {
14184 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
14185 	int hdr_count = 0;
14186 	u8 *nexthdr;
14187 	int start;
14188 
14189 	/* Check that there are at most 2 IPv6 extension headers, no
14190 	 * fragment header, and each is <= 64 bytes.
14191 	 */
14192 	start = nw_off + sizeof(*ip6h);
14193 	nexthdr = &ip6h->nexthdr;
14194 	while (ipv6_ext_hdr(*nexthdr)) {
14195 		struct ipv6_opt_hdr *hp;
14196 		int hdrlen;
14197 
14198 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
14199 		    *nexthdr == NEXTHDR_FRAGMENT)
14200 			return false;
14201 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
14202 					  skb_headlen(skb), NULL);
14203 		if (!hp)
14204 			return false;
14205 		if (*nexthdr == NEXTHDR_AUTH)
14206 			hdrlen = ipv6_authlen(hp);
14207 		else
14208 			hdrlen = ipv6_optlen(hp);
14209 
14210 		if (hdrlen > 64)
14211 			return false;
14212 
14213 		hdr_count++;
14214 		nexthdr = &hp->nexthdr;
14215 		start += hdrlen;
14216 	}
14217 	if (nextp) {
14218 		/* Caller will check inner protocol */
14219 		if (skb->encapsulation) {
14220 			*nextp = nexthdr;
14221 			return true;
14222 		}
14223 		*nextp = NULL;
14224 	}
14225 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
14226 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
14227 }
14228 
14229 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
14230 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
14231 {
14232 	struct udphdr *uh = udp_hdr(skb);
14233 	__be16 udp_port = uh->dest;
14234 
14235 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
14236 	    udp_port != bp->vxlan_gpe_port)
14237 		return false;
14238 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
14239 		struct ethhdr *eh = inner_eth_hdr(skb);
14240 
14241 		switch (eh->h_proto) {
14242 		case htons(ETH_P_IP):
14243 			return true;
14244 		case htons(ETH_P_IPV6):
14245 			return bnxt_exthdr_check(bp, skb,
14246 						 skb_inner_network_offset(skb),
14247 						 NULL);
14248 		}
14249 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
14250 		return true;
14251 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
14252 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
14253 					 NULL);
14254 	}
14255 	return false;
14256 }
14257 
14258 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
14259 {
14260 	switch (l4_proto) {
14261 	case IPPROTO_UDP:
14262 		return bnxt_udp_tunl_check(bp, skb);
14263 	case IPPROTO_IPIP:
14264 		return true;
14265 	case IPPROTO_GRE: {
14266 		switch (skb->inner_protocol) {
14267 		default:
14268 			return false;
14269 		case htons(ETH_P_IP):
14270 			return true;
14271 		case htons(ETH_P_IPV6):
14272 			fallthrough;
14273 		}
14274 	}
14275 	case IPPROTO_IPV6:
14276 		/* Check ext headers of inner ipv6 */
14277 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
14278 					 NULL);
14279 	}
14280 	return false;
14281 }
14282 
14283 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
14284 					     struct net_device *dev,
14285 					     netdev_features_t features)
14286 {
14287 	struct bnxt *bp = netdev_priv(dev);
14288 	u8 *l4_proto;
14289 
14290 	features = vlan_features_check(skb, features);
14291 
14292 	/* SW USO pads at most the last segment; let the stack segment skbs
14293 	 * where every segment would be shorter than BNXT_MIN_PKT_SIZE.
14294 	 */
14295 	if (skb_is_gso(skb) &&
14296 	    (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) &&
14297 	    !(bp->flags & BNXT_FLAG_UDP_GSO_CAP) &&
14298 	    bnxt_sw_gso_pad_len(skb_transport_offset(skb) +
14299 				sizeof(struct udphdr),
14300 				skb_shinfo(skb)->gso_size))
14301 		features &= ~NETIF_F_GSO_MASK;
14302 
14303 	switch (vlan_get_protocol(skb)) {
14304 	case htons(ETH_P_IP):
14305 		if (!skb->encapsulation)
14306 			return features;
14307 		l4_proto = &ip_hdr(skb)->protocol;
14308 		if (bnxt_tunl_check(bp, skb, *l4_proto))
14309 			return features;
14310 		break;
14311 	case htons(ETH_P_IPV6):
14312 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
14313 				       &l4_proto))
14314 			break;
14315 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
14316 			return features;
14317 		break;
14318 	}
14319 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
14320 }
14321 
14322 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
14323 			 u32 *reg_buf)
14324 {
14325 	struct hwrm_dbg_read_direct_output *resp;
14326 	struct hwrm_dbg_read_direct_input *req;
14327 	__le32 *dbg_reg_buf;
14328 	dma_addr_t mapping;
14329 	int rc, i;
14330 
14331 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
14332 	if (rc)
14333 		return rc;
14334 
14335 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
14336 					 &mapping);
14337 	if (!dbg_reg_buf) {
14338 		rc = -ENOMEM;
14339 		goto dbg_rd_reg_exit;
14340 	}
14341 
14342 	req->host_dest_addr = cpu_to_le64(mapping);
14343 
14344 	resp = hwrm_req_hold(bp, req);
14345 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
14346 	req->read_len32 = cpu_to_le32(num_words);
14347 
14348 	rc = hwrm_req_send(bp, req);
14349 	if (rc || resp->error_code) {
14350 		rc = -EIO;
14351 		goto dbg_rd_reg_exit;
14352 	}
14353 	for (i = 0; i < num_words; i++)
14354 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
14355 
14356 dbg_rd_reg_exit:
14357 	hwrm_req_drop(bp, req);
14358 	return rc;
14359 }
14360 
14361 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
14362 				       u32 ring_id, u32 *prod, u32 *cons)
14363 {
14364 	struct hwrm_dbg_ring_info_get_output *resp;
14365 	struct hwrm_dbg_ring_info_get_input *req;
14366 	int rc;
14367 
14368 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
14369 	if (rc)
14370 		return rc;
14371 
14372 	req->ring_type = ring_type;
14373 	req->fw_ring_id = cpu_to_le32(ring_id);
14374 	resp = hwrm_req_hold(bp, req);
14375 	rc = hwrm_req_send(bp, req);
14376 	if (!rc) {
14377 		*prod = le32_to_cpu(resp->producer_index);
14378 		*cons = le32_to_cpu(resp->consumer_index);
14379 	}
14380 	hwrm_req_drop(bp, req);
14381 	return rc;
14382 }
14383 
14384 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
14385 {
14386 	struct bnxt_tx_ring_info *txr;
14387 	int i = bnapi->index, j;
14388 
14389 	bnxt_for_each_napi_tx(j, bnapi, txr)
14390 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
14391 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
14392 			    txr->tx_cons);
14393 }
14394 
14395 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
14396 {
14397 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
14398 	int i = bnapi->index;
14399 
14400 	if (!rxr)
14401 		return;
14402 
14403 	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",
14404 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
14405 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
14406 		    rxr->rx_sw_agg_prod);
14407 }
14408 
14409 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
14410 {
14411 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring, *cpr2;
14412 	int i = bnapi->index, j;
14413 
14414 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
14415 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
14416 	for (j = 0; j < cpr->cp_ring_count; j++) {
14417 		cpr2 = &cpr->cp_ring_arr[j];
14418 		if (!cpr2->bnapi)
14419 			continue;
14420 		netdev_info(bnapi->bp->dev, "[%d.%d]: cp{fw_ring: %d raw_cons: %x}\n",
14421 			    i, j, cpr2->cp_ring_struct.fw_ring_id,
14422 			    cpr2->cp_raw_cons);
14423 	}
14424 }
14425 
14426 static void bnxt_dbg_dump_states(struct bnxt *bp)
14427 {
14428 	int i;
14429 	struct bnxt_napi *bnapi;
14430 
14431 	for (i = 0; i < bp->cp_nr_rings; i++) {
14432 		bnapi = bp->bnapi[i];
14433 		if (netif_msg_drv(bp)) {
14434 			bnxt_dump_tx_sw_state(bnapi);
14435 			bnxt_dump_rx_sw_state(bnapi);
14436 			bnxt_dump_cp_sw_state(bnapi);
14437 		}
14438 	}
14439 }
14440 
14441 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
14442 {
14443 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
14444 	struct hwrm_ring_reset_input *req;
14445 	struct bnxt_napi *bnapi = rxr->bnapi;
14446 	struct bnxt_cp_ring_info *cpr;
14447 	u16 cp_ring_id;
14448 	int rc;
14449 
14450 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
14451 	if (rc)
14452 		return rc;
14453 
14454 	cpr = &bnapi->cp_ring;
14455 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
14456 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
14457 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
14458 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
14459 	return hwrm_req_send_silent(bp, req);
14460 }
14461 
14462 static void bnxt_reset_task(struct bnxt *bp, bool silent)
14463 {
14464 	if (!silent)
14465 		bnxt_dbg_dump_states(bp);
14466 	if (netif_running(bp->dev)) {
14467 		bnxt_close_nic(bp, !silent, false);
14468 		bnxt_open_nic(bp, !silent, false);
14469 	}
14470 }
14471 
14472 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
14473 {
14474 	struct bnxt *bp = netdev_priv(dev);
14475 
14476 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
14477 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
14478 }
14479 
14480 static void bnxt_fw_health_check(struct bnxt *bp)
14481 {
14482 	struct bnxt_fw_health *fw_health = bp->fw_health;
14483 	struct pci_dev *pdev = bp->pdev;
14484 	u32 val;
14485 
14486 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14487 		return;
14488 
14489 	/* Make sure it is enabled before checking the tmr_counter. */
14490 	smp_rmb();
14491 	if (fw_health->tmr_counter) {
14492 		fw_health->tmr_counter--;
14493 		return;
14494 	}
14495 
14496 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14497 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
14498 		fw_health->arrests++;
14499 		goto fw_reset;
14500 	}
14501 
14502 	fw_health->last_fw_heartbeat = val;
14503 
14504 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14505 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
14506 		fw_health->discoveries++;
14507 		goto fw_reset;
14508 	}
14509 
14510 	fw_health->tmr_counter = fw_health->tmr_multiplier;
14511 	return;
14512 
14513 fw_reset:
14514 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
14515 }
14516 
14517 static void bnxt_timer(struct timer_list *t)
14518 {
14519 	struct bnxt *bp = timer_container_of(bp, t, timer);
14520 	struct net_device *dev = bp->dev;
14521 
14522 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
14523 		return;
14524 
14525 	if (atomic_read(&bp->intr_sem) != 0)
14526 		goto bnxt_restart_timer;
14527 
14528 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
14529 		bnxt_fw_health_check(bp);
14530 
14531 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
14532 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
14533 
14534 	if (bnxt_tc_flower_enabled(bp))
14535 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
14536 
14537 #ifdef CONFIG_RFS_ACCEL
14538 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
14539 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14540 #endif /*CONFIG_RFS_ACCEL*/
14541 
14542 	if (bp->link_info.phy_retry) {
14543 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
14544 			bp->link_info.phy_retry = false;
14545 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
14546 		} else {
14547 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
14548 		}
14549 	}
14550 
14551 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
14552 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
14553 
14554 bnxt_restart_timer:
14555 	mod_timer(&bp->timer, jiffies + bp->current_interval);
14556 }
14557 
14558 static void bnxt_lock_sp(struct bnxt *bp)
14559 {
14560 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
14561 	 * set.  If the device is being closed, bnxt_close() may be holding
14562 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14563 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14564 	 * instance lock.
14565 	 */
14566 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14567 	netdev_lock(bp->dev);
14568 }
14569 
14570 static void bnxt_unlock_sp(struct bnxt *bp)
14571 {
14572 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14573 	netdev_unlock(bp->dev);
14574 }
14575 
14576 /* Same as bnxt_lock_sp() with additional rtnl_lock */
14577 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
14578 {
14579 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14580 	rtnl_lock();
14581 	netdev_lock(bp->dev);
14582 }
14583 
14584 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
14585 {
14586 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14587 	netdev_unlock(bp->dev);
14588 	rtnl_unlock();
14589 }
14590 
14591 static void bnxt_tph_update(struct bnxt *bp)
14592 {
14593 	struct net_device *dev = bp->dev;
14594 	int i;
14595 
14596 	bnxt_lock_sp(bp);
14597 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
14598 		goto unlock;
14599 
14600 	for (i = 0; i < bp->rx_nr_rings; i++) {
14601 		struct bnxt_irq *irq;
14602 		int map_idx, err;
14603 		u16 tag;
14604 
14605 		map_idx = bnxt_cp_num_to_irq_num(bp, i);
14606 		irq = &bp->irq_tbl[map_idx];
14607 		tag = READ_ONCE(irq->new_tag);
14608 		if (irq->tag == tag)
14609 			continue;
14610 
14611 		if (pcie_tph_set_st_entry(bp->pdev, irq->msix_nr, tag))
14612 			continue;
14613 
14614 		err = netdev_rx_queue_restart(dev, irq->ring_nr);
14615 		if (err) {
14616 			netdev_err(dev, "RX queue restart failed: err=%d\n",
14617 				   err);
14618 			continue;
14619 		}
14620 
14621 		irq->tag = tag;
14622 	}
14623 
14624 unlock:
14625 	bnxt_unlock_sp(bp);
14626 }
14627 
14628 /* Only called from bnxt_sp_task() */
14629 static void bnxt_reset(struct bnxt *bp, bool silent)
14630 {
14631 	bnxt_rtnl_lock_sp(bp);
14632 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14633 		bnxt_reset_task(bp, silent);
14634 	bnxt_rtnl_unlock_sp(bp);
14635 }
14636 
14637 /* Only called from bnxt_sp_task() */
14638 static void bnxt_rx_ring_reset(struct bnxt *bp)
14639 {
14640 	int i;
14641 
14642 	bnxt_rtnl_lock_sp(bp);
14643 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14644 		bnxt_rtnl_unlock_sp(bp);
14645 		return;
14646 	}
14647 	/* Disable and flush TPA before resetting the RX ring */
14648 	if (bp->flags & BNXT_FLAG_TPA)
14649 		bnxt_set_tpa(bp, false);
14650 	for (i = 0; i < bp->rx_nr_rings; i++) {
14651 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14652 		struct bnxt_cp_ring_info *cpr;
14653 		int rc;
14654 
14655 		if (!rxr->bnapi->in_reset)
14656 			continue;
14657 
14658 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14659 		if (rc) {
14660 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14661 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14662 			else
14663 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14664 					    rc);
14665 			bnxt_reset_task(bp, true);
14666 			break;
14667 		}
14668 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14669 		rxr->rx_prod = 0;
14670 		rxr->rx_agg_prod = 0;
14671 		rxr->rx_sw_agg_prod = 0;
14672 		rxr->rx_next_cons = 0;
14673 		rxr->bnapi->in_reset = false;
14674 		rc = bnxt_alloc_one_rx_ring(bp, i);
14675 		if (rc) {
14676 			netdev_warn(bp->dev, "RX ring reset failed to allocate buffers, rc = %d, falling back to global reset\n",
14677 				    rc);
14678 			bnxt_reset_task(bp, true);
14679 			bnxt_rtnl_unlock_sp(bp);
14680 			return;
14681 		}
14682 		cpr = &rxr->bnapi->cp_ring;
14683 		cpr->sw_stats->rx.rx_resets++;
14684 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14685 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14686 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14687 	}
14688 	if (bp->flags & BNXT_FLAG_TPA)
14689 		bnxt_set_tpa(bp, true);
14690 	bnxt_rtnl_unlock_sp(bp);
14691 }
14692 
14693 static void bnxt_fw_fatal_close(struct bnxt *bp)
14694 {
14695 	bnxt_tx_disable(bp);
14696 	bnxt_disable_napi(bp);
14697 	bnxt_disable_int_sync(bp);
14698 	bnxt_free_irq(bp);
14699 	bnxt_clear_int_mode(bp);
14700 	pci_disable_device(bp->pdev);
14701 }
14702 
14703 static void bnxt_fw_reset_close(struct bnxt *bp)
14704 {
14705 	/* When firmware is in fatal state, quiesce device and disable
14706 	 * bus master to prevent any potential bad DMAs before freeing
14707 	 * kernel memory.
14708 	 */
14709 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14710 		u16 val = 0;
14711 
14712 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14713 		if (val == 0xffff)
14714 			bp->fw_reset_min_dsecs = 0;
14715 		bnxt_fw_fatal_close(bp);
14716 	}
14717 	__bnxt_close_nic(bp, true, false);
14718 	bnxt_vf_reps_free(bp);
14719 	bnxt_clear_int_mode(bp);
14720 	bnxt_hwrm_func_drv_unrgtr(bp);
14721 	if (pci_is_enabled(bp->pdev))
14722 		pci_disable_device(bp->pdev);
14723 	bnxt_free_ctx_mem(bp, false);
14724 }
14725 
14726 static bool is_bnxt_fw_ok(struct bnxt *bp)
14727 {
14728 	struct bnxt_fw_health *fw_health = bp->fw_health;
14729 	bool no_heartbeat = false, has_reset = false;
14730 	u32 val;
14731 
14732 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14733 	if (val == fw_health->last_fw_heartbeat)
14734 		no_heartbeat = true;
14735 
14736 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14737 	if (val != fw_health->last_fw_reset_cnt)
14738 		has_reset = true;
14739 
14740 	if (!no_heartbeat && has_reset)
14741 		return true;
14742 
14743 	return false;
14744 }
14745 
14746 /* netdev instance lock is acquired before calling this function */
14747 static void bnxt_force_fw_reset(struct bnxt *bp)
14748 {
14749 	struct bnxt_fw_health *fw_health = bp->fw_health;
14750 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14751 	u32 wait_dsecs;
14752 
14753 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14754 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14755 		return;
14756 
14757 	/* we have to serialize with bnxt_refclk_read()*/
14758 	if (ptp) {
14759 		unsigned long flags;
14760 
14761 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14762 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14763 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14764 	} else {
14765 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14766 	}
14767 	bnxt_fw_reset_close(bp);
14768 	wait_dsecs = fw_health->master_func_wait_dsecs;
14769 	if (fw_health->primary) {
14770 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14771 			wait_dsecs = 0;
14772 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14773 	} else {
14774 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14775 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14776 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14777 	}
14778 
14779 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14780 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14781 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14782 }
14783 
14784 void bnxt_fw_exception(struct bnxt *bp)
14785 {
14786 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14787 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14788 	bnxt_ulp_stop(bp);
14789 	bnxt_lock_sp(bp);
14790 	bnxt_force_fw_reset(bp);
14791 	bnxt_unlock_sp(bp);
14792 }
14793 
14794 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14795  * < 0 on error.
14796  */
14797 static int bnxt_get_registered_vfs(struct bnxt *bp)
14798 {
14799 #ifdef CONFIG_BNXT_SRIOV
14800 	int rc;
14801 
14802 	if (!BNXT_PF(bp))
14803 		return 0;
14804 
14805 	rc = bnxt_hwrm_func_qcfg(bp);
14806 	if (rc) {
14807 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14808 		return rc;
14809 	}
14810 	if (bp->pf.registered_vfs)
14811 		return bp->pf.registered_vfs;
14812 	if (bp->sriov_cfg)
14813 		return 1;
14814 #endif
14815 	return 0;
14816 }
14817 
14818 void bnxt_fw_reset(struct bnxt *bp)
14819 {
14820 	bnxt_ulp_stop(bp);
14821 	bnxt_lock_sp(bp);
14822 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14823 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14824 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14825 		int n = 0, tmo;
14826 
14827 		/* we have to serialize with bnxt_refclk_read()*/
14828 		if (ptp) {
14829 			unsigned long flags;
14830 
14831 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14832 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14833 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14834 		} else {
14835 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14836 		}
14837 		if (bp->pf.active_vfs &&
14838 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14839 			n = bnxt_get_registered_vfs(bp);
14840 		if (n < 0) {
14841 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14842 				   n);
14843 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14844 			netif_close(bp->dev);
14845 			goto fw_reset_exit;
14846 		} else if (n > 0) {
14847 			u16 vf_tmo_dsecs = n * 10;
14848 
14849 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14850 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14851 			bp->fw_reset_state =
14852 				BNXT_FW_RESET_STATE_POLL_VF;
14853 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14854 			goto fw_reset_exit;
14855 		}
14856 		bnxt_fw_reset_close(bp);
14857 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14858 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14859 			tmo = HZ / 10;
14860 		} else {
14861 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14862 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14863 		}
14864 		bnxt_queue_fw_reset_work(bp, tmo);
14865 	}
14866 fw_reset_exit:
14867 	bnxt_unlock_sp(bp);
14868 }
14869 
14870 static void bnxt_chk_missed_irq(struct bnxt *bp)
14871 {
14872 	int i;
14873 
14874 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14875 		return;
14876 
14877 	for (i = 0; i < bp->cp_nr_rings; i++) {
14878 		struct bnxt_napi *bnapi = bp->bnapi[i];
14879 		struct bnxt_cp_ring_info *cpr;
14880 		u32 fw_ring_id;
14881 		int j;
14882 
14883 		if (!bnapi)
14884 			continue;
14885 
14886 		cpr = &bnapi->cp_ring;
14887 		for (j = 0; j < cpr->cp_ring_count; j++) {
14888 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14889 			u32 val[2];
14890 
14891 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14892 				continue;
14893 
14894 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14895 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14896 				continue;
14897 			}
14898 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14899 			bnxt_dbg_hwrm_ring_info_get(bp,
14900 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14901 				fw_ring_id, &val[0], &val[1]);
14902 			cpr->sw_stats->cmn.missed_irqs++;
14903 		}
14904 	}
14905 }
14906 
14907 static void bnxt_cfg_ntp_filters(struct bnxt *);
14908 
14909 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14910 {
14911 	struct bnxt_link_info *link_info = &bp->link_info;
14912 
14913 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14914 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14915 		if (bp->hwrm_spec_code >= 0x10201) {
14916 			if (link_info->auto_pause_setting &
14917 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14918 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14919 		} else {
14920 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14921 		}
14922 		bnxt_set_auto_speed(link_info);
14923 	} else {
14924 		bnxt_set_force_speed(link_info);
14925 		link_info->req_duplex = link_info->duplex_setting;
14926 	}
14927 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14928 		link_info->req_flow_ctrl =
14929 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14930 	else
14931 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14932 }
14933 
14934 static void bnxt_fw_echo_reply(struct bnxt *bp)
14935 {
14936 	struct bnxt_fw_health *fw_health = bp->fw_health;
14937 	struct hwrm_func_echo_response_input *req;
14938 	int rc;
14939 
14940 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14941 	if (rc)
14942 		return;
14943 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14944 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14945 	hwrm_req_send(bp, req);
14946 }
14947 
14948 static void bnxt_ulp_restart(struct bnxt *bp)
14949 {
14950 	bnxt_ulp_stop(bp);
14951 	bnxt_ulp_start(bp);
14952 }
14953 
14954 static void bnxt_sp_task(struct work_struct *work)
14955 {
14956 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14957 
14958 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14959 	smp_mb__after_atomic();
14960 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14961 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14962 		return;
14963 	}
14964 
14965 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14966 		bnxt_ulp_restart(bp);
14967 		bnxt_reenable_sriov(bp);
14968 	}
14969 
14970 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14971 		bnxt_cfg_ntp_filters(bp);
14972 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14973 		bnxt_hwrm_exec_fwd_req(bp);
14974 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14975 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14976 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14977 		bnxt_hwrm_port_qstats(bp, 0);
14978 		bnxt_hwrm_port_qstats_ext(bp, 0);
14979 		spin_lock(&bp->stats_lock);
14980 		bnxt_accumulate_all_stats(bp);
14981 		bp->stats_updated_jiffies = jiffies;
14982 		spin_unlock(&bp->stats_lock);
14983 	}
14984 
14985 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14986 		int rc;
14987 
14988 		mutex_lock(&bp->link_lock);
14989 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14990 				       &bp->sp_event))
14991 			bnxt_hwrm_phy_qcaps(bp);
14992 
14993 		rc = bnxt_update_link(bp, true);
14994 		if (rc)
14995 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14996 				   rc);
14997 
14998 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14999 				       &bp->sp_event))
15000 			bnxt_init_ethtool_link_settings(bp);
15001 		mutex_unlock(&bp->link_lock);
15002 	}
15003 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
15004 		int rc;
15005 
15006 		mutex_lock(&bp->link_lock);
15007 		rc = bnxt_update_phy_setting(bp);
15008 		mutex_unlock(&bp->link_lock);
15009 		if (rc) {
15010 			netdev_warn(bp->dev, "update phy settings retry failed\n");
15011 		} else {
15012 			bp->link_info.phy_retry = false;
15013 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
15014 		}
15015 	}
15016 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
15017 		mutex_lock(&bp->link_lock);
15018 		bnxt_get_port_module_status(bp);
15019 		mutex_unlock(&bp->link_lock);
15020 	}
15021 
15022 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
15023 		bnxt_tc_flow_stats_work(bp);
15024 
15025 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
15026 		bnxt_chk_missed_irq(bp);
15027 
15028 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
15029 		bnxt_fw_echo_reply(bp);
15030 
15031 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
15032 		bnxt_hwmon_notify_event(bp);
15033 
15034 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
15035 	 * must be the last functions to be called before exiting.
15036 	 */
15037 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
15038 		bnxt_reset(bp, false);
15039 
15040 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
15041 		bnxt_reset(bp, true);
15042 
15043 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
15044 		bnxt_rx_ring_reset(bp);
15045 
15046 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
15047 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
15048 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
15049 			bnxt_devlink_health_fw_report(bp);
15050 		else
15051 			bnxt_fw_reset(bp);
15052 	}
15053 
15054 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
15055 		if (!is_bnxt_fw_ok(bp))
15056 			bnxt_devlink_health_fw_report(bp);
15057 	}
15058 
15059 	if (test_and_clear_bit(BNXT_TPH_UPDATE_SP_EVENT, &bp->sp_event))
15060 		bnxt_tph_update(bp);
15061 
15062 	smp_mb__before_atomic();
15063 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
15064 }
15065 
15066 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15067 				int *max_cp);
15068 
15069 /* Under netdev instance lock */
15070 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
15071 		     int tx_xdp)
15072 {
15073 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
15074 	struct bnxt_hw_rings hwr = {0};
15075 	int rx_rings = rx;
15076 	int rc;
15077 
15078 	if (tcs)
15079 		tx_sets = tcs;
15080 
15081 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
15082 
15083 	if (max_rx < rx_rings)
15084 		return -ENOMEM;
15085 
15086 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15087 		rx_rings <<= 1;
15088 
15089 	hwr.rx = rx_rings;
15090 	hwr.tx = tx * tx_sets + tx_xdp;
15091 	if (max_tx < hwr.tx)
15092 		return -ENOMEM;
15093 
15094 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
15095 
15096 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
15097 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
15098 	if (max_cp < hwr.cp)
15099 		return -ENOMEM;
15100 	hwr.stat = hwr.cp;
15101 	if (BNXT_NEW_RM(bp)) {
15102 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
15103 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
15104 		hwr.grp = rx;
15105 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
15106 	}
15107 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
15108 		hwr.cp_p5 = hwr.tx + rx;
15109 	rc = bnxt_hwrm_check_rings(bp, &hwr);
15110 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
15111 		if (!bnxt_ulp_registered(bp->edev[BNXT_AUXDEV_RDMA])) {
15112 			hwr.cp += bnxt_get_ulp_msix_num(bp);
15113 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
15114 		}
15115 		if (hwr.cp > bp->total_irqs) {
15116 			int total_msix = bnxt_change_msix(bp, hwr.cp);
15117 
15118 			if (total_msix < hwr.cp) {
15119 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
15120 					    hwr.cp, total_msix);
15121 				rc = -ENOSPC;
15122 			}
15123 		}
15124 	}
15125 	return rc;
15126 }
15127 
15128 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
15129 {
15130 	if (bp->bar2) {
15131 		pci_iounmap(pdev, bp->bar2);
15132 		bp->bar2 = NULL;
15133 	}
15134 
15135 	if (bp->bar1) {
15136 		pci_iounmap(pdev, bp->bar1);
15137 		bp->bar1 = NULL;
15138 	}
15139 
15140 	if (bp->bar0) {
15141 		pci_iounmap(pdev, bp->bar0);
15142 		bp->bar0 = NULL;
15143 	}
15144 }
15145 
15146 static void bnxt_cleanup_pci(struct bnxt *bp)
15147 {
15148 	pci_disable_ptm(bp->pdev);
15149 	bnxt_unmap_bars(bp, bp->pdev);
15150 	pci_release_regions(bp->pdev);
15151 	if (pci_is_enabled(bp->pdev))
15152 		pci_disable_device(bp->pdev);
15153 }
15154 
15155 static void bnxt_init_dflt_coal(struct bnxt *bp)
15156 {
15157 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
15158 	struct bnxt_coal *coal;
15159 	u16 flags = 0;
15160 
15161 	if (coal_cap->cmpl_params &
15162 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
15163 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
15164 
15165 	/* Tick values in micro seconds.
15166 	 * 1 coal_buf x bufs_per_record = 1 completion record.
15167 	 */
15168 	coal = &bp->rx_coal;
15169 	coal->coal_ticks = 10;
15170 	coal->coal_bufs = 30;
15171 	coal->coal_ticks_irq = 1;
15172 	coal->coal_bufs_irq = 2;
15173 	coal->idle_thresh = 50;
15174 	coal->bufs_per_record = 2;
15175 	coal->budget = 64;		/* NAPI budget */
15176 	coal->flags = flags;
15177 
15178 	coal = &bp->tx_coal;
15179 	coal->coal_ticks = 28;
15180 	coal->coal_bufs = 30;
15181 	coal->coal_ticks_irq = 2;
15182 	coal->coal_bufs_irq = 2;
15183 	coal->bufs_per_record = 1;
15184 	coal->flags = flags;
15185 
15186 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
15187 }
15188 
15189 /* FW that pre-reserves 1 VNIC per function */
15190 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
15191 {
15192 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
15193 
15194 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
15195 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
15196 		return true;
15197 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
15198 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
15199 		return true;
15200 	return false;
15201 }
15202 
15203 static void bnxt_hwrm_pfcwd_qcaps(struct bnxt *bp)
15204 {
15205 	struct hwrm_queue_pfcwd_timeout_qcaps_output *resp;
15206 	struct hwrm_queue_pfcwd_timeout_qcaps_input *req;
15207 	int rc;
15208 
15209 	bp->max_pfcwd_tmo_ms = 0;
15210 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_PFCWD_TIMEOUT_QCAPS);
15211 	if (rc)
15212 		return;
15213 	resp = hwrm_req_hold(bp, req);
15214 	rc = hwrm_req_send_silent(bp, req);
15215 	if (!rc)
15216 		bp->max_pfcwd_tmo_ms = le16_to_cpu(resp->max_pfcwd_timeout);
15217 	hwrm_req_drop(bp, req);
15218 }
15219 
15220 static int bnxt_fw_init_one_p1(struct bnxt *bp)
15221 {
15222 	int rc;
15223 
15224 	bp->fw_cap = 0;
15225 	rc = bnxt_hwrm_ver_get(bp);
15226 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
15227 	 * so wait before continuing with recovery.
15228 	 */
15229 	if (rc)
15230 		msleep(100);
15231 	bnxt_try_map_fw_health_reg(bp);
15232 	if (rc) {
15233 		rc = bnxt_try_recover_fw(bp);
15234 		if (rc)
15235 			return rc;
15236 		rc = bnxt_hwrm_ver_get(bp);
15237 		if (rc)
15238 			return rc;
15239 	}
15240 
15241 	bnxt_nvm_cfg_ver_get(bp);
15242 
15243 	rc = bnxt_hwrm_func_reset(bp);
15244 	if (rc)
15245 		return -ENODEV;
15246 
15247 	bnxt_hwrm_fw_set_time(bp);
15248 	return 0;
15249 }
15250 
15251 static int bnxt_fw_init_one_p2(struct bnxt *bp)
15252 {
15253 	int rc;
15254 
15255 	/* Get the MAX capabilities for this function */
15256 	rc = bnxt_hwrm_func_qcaps(bp);
15257 	if (rc) {
15258 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
15259 			   rc);
15260 		return -ENODEV;
15261 	}
15262 
15263 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
15264 	if (rc)
15265 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
15266 			    rc);
15267 
15268 	if (bnxt_alloc_fw_health(bp)) {
15269 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
15270 	} else {
15271 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
15272 		if (rc)
15273 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
15274 				    rc);
15275 	}
15276 
15277 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
15278 	if (rc)
15279 		return -ENODEV;
15280 
15281 	rc = bnxt_alloc_crash_dump_mem(bp);
15282 	if (rc)
15283 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
15284 			    rc);
15285 	if (!rc) {
15286 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
15287 		if (rc) {
15288 			bnxt_free_crash_dump_mem(bp);
15289 			netdev_warn(bp->dev,
15290 				    "hwrm crash dump mem failure rc: %d\n", rc);
15291 		}
15292 	}
15293 
15294 	if (bnxt_fw_pre_resv_vnics(bp))
15295 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
15296 
15297 	bnxt_hwrm_pfcwd_qcaps(bp);
15298 	bnxt_hwrm_func_qcfg(bp);
15299 	bnxt_hwrm_vnic_qcaps(bp);
15300 	bnxt_hwrm_port_led_qcaps(bp);
15301 	bnxt_ethtool_init(bp);
15302 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
15303 		__bnxt_hwrm_ptp_qcfg(bp);
15304 	bnxt_dcb_init(bp);
15305 	bnxt_hwmon_init(bp);
15306 	return 0;
15307 }
15308 
15309 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
15310 {
15311 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
15312 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
15313 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
15314 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
15315 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
15316 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
15317 		bp->rss_hash_delta = bp->rss_hash_cfg;
15318 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
15319 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
15320 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
15321 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
15322 	}
15323 }
15324 
15325 static void bnxt_set_dflt_rfs(struct bnxt *bp)
15326 {
15327 	struct net_device *dev = bp->dev;
15328 
15329 	dev->hw_features &= ~NETIF_F_NTUPLE;
15330 	dev->features &= ~NETIF_F_NTUPLE;
15331 	bp->flags &= ~BNXT_FLAG_RFS;
15332 	if (bnxt_rfs_supported(bp)) {
15333 		dev->hw_features |= NETIF_F_NTUPLE;
15334 		if (bnxt_rfs_capable(bp, false)) {
15335 			bp->flags |= BNXT_FLAG_RFS;
15336 			dev->features |= NETIF_F_NTUPLE;
15337 		}
15338 	}
15339 }
15340 
15341 static void bnxt_fw_init_one_p3(struct bnxt *bp)
15342 {
15343 	struct pci_dev *pdev = bp->pdev;
15344 
15345 	bnxt_set_dflt_rss_hash_type(bp);
15346 	bnxt_set_dflt_rfs(bp);
15347 
15348 	bnxt_get_wol_settings(bp);
15349 	if (bp->flags & BNXT_FLAG_WOL_CAP)
15350 		device_set_wakeup_enable(&pdev->dev, bp->wol);
15351 	else
15352 		device_set_wakeup_capable(&pdev->dev, false);
15353 
15354 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
15355 	bnxt_hwrm_coal_params_qcaps(bp);
15356 }
15357 
15358 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
15359 
15360 int bnxt_fw_init_one(struct bnxt *bp)
15361 {
15362 	int rc;
15363 
15364 	rc = bnxt_fw_init_one_p1(bp);
15365 	if (rc) {
15366 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
15367 		return rc;
15368 	}
15369 	rc = bnxt_fw_init_one_p2(bp);
15370 	if (rc) {
15371 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
15372 		return rc;
15373 	}
15374 	rc = bnxt_probe_phy(bp, false);
15375 	if (rc)
15376 		return rc;
15377 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
15378 	if (rc)
15379 		return rc;
15380 
15381 	bnxt_fw_init_one_p3(bp);
15382 	return 0;
15383 }
15384 
15385 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
15386 {
15387 	struct bnxt_fw_health *fw_health = bp->fw_health;
15388 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
15389 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
15390 	u32 reg_type, reg_off, delay_msecs;
15391 
15392 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
15393 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
15394 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
15395 	switch (reg_type) {
15396 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
15397 		pci_write_config_dword(bp->pdev, reg_off, val);
15398 		break;
15399 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
15400 		writel(reg_off & BNXT_GRC_BASE_MASK,
15401 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
15402 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
15403 		fallthrough;
15404 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
15405 		writel(val, bp->bar0 + reg_off);
15406 		break;
15407 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
15408 		writel(val, bp->bar1 + reg_off);
15409 		break;
15410 	}
15411 	if (delay_msecs) {
15412 		pci_read_config_dword(bp->pdev, 0, &val);
15413 		msleep(delay_msecs);
15414 	}
15415 }
15416 
15417 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
15418 {
15419 	struct hwrm_func_qcfg_output *resp;
15420 	struct hwrm_func_qcfg_input *req;
15421 	bool result = true; /* firmware will enforce if unknown */
15422 
15423 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
15424 		return result;
15425 
15426 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
15427 		return result;
15428 
15429 	req->fid = cpu_to_le16(0xffff);
15430 	resp = hwrm_req_hold(bp, req);
15431 	if (!hwrm_req_send(bp, req))
15432 		result = !!(le16_to_cpu(resp->flags) &
15433 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
15434 	hwrm_req_drop(bp, req);
15435 	return result;
15436 }
15437 
15438 static void bnxt_reset_all(struct bnxt *bp)
15439 {
15440 	struct bnxt_fw_health *fw_health = bp->fw_health;
15441 	int i, rc;
15442 
15443 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15444 		bnxt_fw_reset_via_optee(bp);
15445 		bp->fw_reset_timestamp = jiffies;
15446 		return;
15447 	}
15448 
15449 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
15450 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
15451 			bnxt_fw_reset_writel(bp, i);
15452 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
15453 		struct hwrm_fw_reset_input *req;
15454 
15455 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
15456 		if (!rc) {
15457 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
15458 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
15459 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
15460 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
15461 			rc = hwrm_req_send(bp, req);
15462 		}
15463 		if (rc != -ENODEV)
15464 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
15465 	}
15466 	bp->fw_reset_timestamp = jiffies;
15467 }
15468 
15469 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
15470 {
15471 	return time_after(jiffies, bp->fw_reset_timestamp +
15472 			  (bp->fw_reset_max_dsecs * HZ / 10));
15473 }
15474 
15475 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
15476 {
15477 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15478 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
15479 		bnxt_dl_health_fw_status_update(bp, false);
15480 	bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT;
15481 	netif_close(bp->dev);
15482 }
15483 
15484 static void bnxt_fw_reset_task(struct work_struct *work)
15485 {
15486 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
15487 	int rc = 0;
15488 
15489 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
15490 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
15491 		return;
15492 	}
15493 
15494 	switch (bp->fw_reset_state) {
15495 	case BNXT_FW_RESET_STATE_POLL_VF: {
15496 		int n = bnxt_get_registered_vfs(bp);
15497 		int tmo;
15498 
15499 		if (n < 0) {
15500 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
15501 				   n, jiffies_to_msecs(jiffies -
15502 				   bp->fw_reset_timestamp));
15503 			goto fw_reset_abort;
15504 		} else if (n > 0) {
15505 			if (bnxt_fw_reset_timeout(bp)) {
15506 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15507 				bp->fw_reset_state = 0;
15508 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
15509 					   n);
15510 				goto ulp_start;
15511 			}
15512 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15513 			return;
15514 		}
15515 		bp->fw_reset_timestamp = jiffies;
15516 		netdev_lock(bp->dev);
15517 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
15518 			bnxt_fw_reset_abort(bp, rc);
15519 			netdev_unlock(bp->dev);
15520 			goto ulp_start;
15521 		}
15522 		bnxt_fw_reset_close(bp);
15523 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15524 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
15525 			tmo = HZ / 10;
15526 		} else {
15527 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15528 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
15529 		}
15530 		netdev_unlock(bp->dev);
15531 		bnxt_queue_fw_reset_work(bp, tmo);
15532 		return;
15533 	}
15534 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
15535 		u32 val;
15536 
15537 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15538 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
15539 		    !bnxt_fw_reset_timeout(bp)) {
15540 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15541 			return;
15542 		}
15543 
15544 		if (!bp->fw_health->primary) {
15545 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
15546 
15547 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15548 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
15549 			return;
15550 		}
15551 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
15552 	}
15553 		fallthrough;
15554 	case BNXT_FW_RESET_STATE_RESET_FW:
15555 		bnxt_reset_all(bp);
15556 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15557 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
15558 		return;
15559 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
15560 		bnxt_inv_fw_health_reg(bp);
15561 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
15562 		    !bp->fw_reset_min_dsecs) {
15563 			u16 val;
15564 
15565 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
15566 			if (val == 0xffff) {
15567 				if (bnxt_fw_reset_timeout(bp)) {
15568 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
15569 					rc = -ETIMEDOUT;
15570 					goto fw_reset_abort;
15571 				}
15572 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
15573 				return;
15574 			}
15575 		}
15576 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
15577 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
15578 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
15579 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
15580 			bnxt_dl_remote_reload(bp);
15581 		if (!pci_is_enabled(bp->pdev) && pci_enable_device(bp->pdev)) {
15582 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
15583 			rc = -ENODEV;
15584 			goto fw_reset_abort;
15585 		}
15586 		pci_set_master(bp->pdev);
15587 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
15588 		fallthrough;
15589 	case BNXT_FW_RESET_STATE_POLL_FW:
15590 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
15591 		rc = bnxt_hwrm_poll(bp);
15592 		if (rc) {
15593 			if (bnxt_fw_reset_timeout(bp)) {
15594 				netdev_err(bp->dev, "Firmware reset aborted\n");
15595 				goto fw_reset_abort_status;
15596 			}
15597 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15598 			return;
15599 		}
15600 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
15601 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
15602 		fallthrough;
15603 	case BNXT_FW_RESET_STATE_OPENING:
15604 		while (!rtnl_trylock()) {
15605 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15606 			return;
15607 		}
15608 		netdev_lock(bp->dev);
15609 		rc = bnxt_open(bp->dev);
15610 		if (rc) {
15611 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
15612 			bnxt_fw_reset_abort(bp, rc);
15613 			netdev_unlock(bp->dev);
15614 			rtnl_unlock();
15615 			goto ulp_start;
15616 		}
15617 
15618 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
15619 		    bp->fw_health->enabled) {
15620 			bp->fw_health->last_fw_reset_cnt =
15621 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
15622 		}
15623 		bp->fw_reset_state = 0;
15624 		/* Make sure fw_reset_state is 0 before clearing the flag */
15625 		smp_mb__before_atomic();
15626 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15627 		bnxt_ptp_reapply_pps(bp);
15628 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
15629 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
15630 			bnxt_dl_health_fw_recovery_done(bp);
15631 			bnxt_dl_health_fw_status_update(bp, true);
15632 		}
15633 		netdev_unlock(bp->dev);
15634 		rtnl_unlock();
15635 		bnxt_ulp_start(bp);
15636 		bnxt_reenable_sriov(bp);
15637 		netdev_lock(bp->dev);
15638 		bnxt_vf_reps_alloc(bp);
15639 		bnxt_vf_reps_open(bp);
15640 		netdev_unlock(bp->dev);
15641 		break;
15642 	}
15643 	return;
15644 
15645 fw_reset_abort_status:
15646 	if (bp->fw_health->status_reliable ||
15647 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15648 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15649 
15650 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15651 	}
15652 fw_reset_abort:
15653 	netdev_lock(bp->dev);
15654 	bnxt_fw_reset_abort(bp, rc);
15655 	netdev_unlock(bp->dev);
15656 ulp_start:
15657 	if (!rc)
15658 		bnxt_ulp_start(bp);
15659 }
15660 
15661 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15662 {
15663 	int rc;
15664 	struct bnxt *bp = netdev_priv(dev);
15665 
15666 	SET_NETDEV_DEV(dev, &pdev->dev);
15667 
15668 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15669 	rc = pci_enable_device(pdev);
15670 	if (rc) {
15671 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15672 		goto init_err;
15673 	}
15674 
15675 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15676 		dev_err(&pdev->dev,
15677 			"Cannot find PCI device base address, aborting\n");
15678 		rc = -ENODEV;
15679 		goto init_err_disable;
15680 	}
15681 
15682 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15683 	if (rc) {
15684 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15685 		goto init_err_disable;
15686 	}
15687 
15688 	rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15689 	if (rc) {
15690 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15691 		rc = -EIO;
15692 		goto init_err_release;
15693 	}
15694 
15695 	pci_set_master(pdev);
15696 
15697 	bp->dev = dev;
15698 	bp->pdev = pdev;
15699 
15700 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15701 	 * determines the BAR size.
15702 	 */
15703 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15704 	if (!bp->bar0) {
15705 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15706 		rc = -ENOMEM;
15707 		goto init_err_release;
15708 	}
15709 
15710 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15711 	if (!bp->bar2) {
15712 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15713 		rc = -ENOMEM;
15714 		goto init_err_release;
15715 	}
15716 
15717 	pci_enable_ptm(pdev);
15718 
15719 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15720 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15721 
15722 	spin_lock_init(&bp->ntp_fltr_lock);
15723 	spin_lock_init(&bp->stats_lock);
15724 #if BITS_PER_LONG == 32
15725 	spin_lock_init(&bp->db_lock);
15726 #endif
15727 
15728 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15729 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15730 
15731 	timer_setup(&bp->timer, bnxt_timer, 0);
15732 	bp->current_interval = BNXT_TIMER_INTERVAL;
15733 
15734 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15735 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15736 
15737 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15738 	return 0;
15739 
15740 init_err_release:
15741 	bnxt_unmap_bars(bp, pdev);
15742 	pci_release_regions(pdev);
15743 
15744 init_err_disable:
15745 	pci_disable_device(pdev);
15746 
15747 init_err:
15748 	return rc;
15749 }
15750 
15751 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15752 {
15753 	struct sockaddr *addr = p;
15754 	struct bnxt *bp = netdev_priv(dev);
15755 	int rc = 0;
15756 
15757 	netdev_assert_locked(dev);
15758 
15759 	if (!is_valid_ether_addr(addr->sa_data))
15760 		return -EADDRNOTAVAIL;
15761 
15762 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15763 		return 0;
15764 
15765 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15766 	if (rc)
15767 		return rc;
15768 
15769 	eth_hw_addr_set(dev, addr->sa_data);
15770 	bnxt_clear_usr_fltrs(bp, true);
15771 	if (netif_running(dev)) {
15772 		bnxt_close_nic(bp, false, false);
15773 		rc = bnxt_open_nic(bp, false, false);
15774 	}
15775 
15776 	return rc;
15777 }
15778 
15779 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15780 {
15781 	struct bnxt *bp = netdev_priv(dev);
15782 
15783 	netdev_assert_locked(dev);
15784 
15785 	if (netif_running(dev))
15786 		bnxt_close_nic(bp, true, false);
15787 
15788 	WRITE_ONCE(dev->mtu, new_mtu);
15789 
15790 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15791 	 * program is attached.  We need to set the AGG rings settings and
15792 	 * rx_skb_func accordingly.
15793 	 */
15794 	if (READ_ONCE(bp->xdp_prog))
15795 		bnxt_set_rx_skb_mode(bp, true);
15796 
15797 	bnxt_set_ring_params(bp);
15798 
15799 	if (netif_running(dev))
15800 		return bnxt_open_nic(bp, true, false);
15801 
15802 	return 0;
15803 }
15804 
15805 void bnxt_set_cp_rings(struct bnxt *bp, bool sh)
15806 {
15807 	int tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15808 
15809 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15810 			       tx_cp + bp->rx_nr_rings;
15811 }
15812 
15813 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15814 {
15815 	struct bnxt *bp = netdev_priv(dev);
15816 	bool sh = false;
15817 	int rc;
15818 
15819 	if (tc > bp->max_tc) {
15820 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15821 			   tc, bp->max_tc);
15822 		return -EINVAL;
15823 	}
15824 
15825 	if (bp->num_tc == tc)
15826 		return 0;
15827 
15828 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15829 		sh = true;
15830 
15831 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15832 			      sh, tc, bp->tx_nr_rings_xdp);
15833 	if (rc)
15834 		return rc;
15835 
15836 	/* Needs to close the device and do hw resource re-allocations */
15837 	if (netif_running(bp->dev))
15838 		bnxt_close_nic(bp, true, false);
15839 
15840 	if (tc) {
15841 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15842 		netdev_set_num_tc(dev, tc);
15843 		bp->num_tc = tc;
15844 	} else {
15845 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15846 		netdev_reset_tc(dev);
15847 		bp->num_tc = 0;
15848 	}
15849 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15850 	bnxt_set_cp_rings(bp, sh);
15851 
15852 	if (netif_running(bp->dev))
15853 		return bnxt_open_nic(bp, true, false);
15854 
15855 	return 0;
15856 }
15857 
15858 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15859 				  void *cb_priv)
15860 {
15861 	struct bnxt *bp = cb_priv;
15862 
15863 	if (!bnxt_tc_flower_enabled(bp) ||
15864 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15865 		return -EOPNOTSUPP;
15866 
15867 	switch (type) {
15868 	case TC_SETUP_CLSFLOWER:
15869 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15870 	default:
15871 		return -EOPNOTSUPP;
15872 	}
15873 }
15874 
15875 LIST_HEAD(bnxt_block_cb_list);
15876 
15877 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15878 			 void *type_data)
15879 {
15880 	struct bnxt *bp = netdev_priv(dev);
15881 
15882 	switch (type) {
15883 	case TC_SETUP_BLOCK:
15884 		return flow_block_cb_setup_simple(type_data,
15885 						  &bnxt_block_cb_list,
15886 						  bnxt_setup_tc_block_cb,
15887 						  bp, bp, true);
15888 	case TC_SETUP_QDISC_MQPRIO: {
15889 		struct tc_mqprio_qopt *mqprio = type_data;
15890 
15891 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15892 
15893 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15894 	}
15895 	default:
15896 		return -EOPNOTSUPP;
15897 	}
15898 }
15899 
15900 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15901 			    const struct sk_buff *skb)
15902 {
15903 	struct bnxt_vnic_info *vnic;
15904 
15905 	if (skb)
15906 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15907 
15908 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15909 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15910 }
15911 
15912 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15913 			   u32 idx)
15914 {
15915 	struct hlist_head *head;
15916 	int bit_id;
15917 
15918 	spin_lock_bh(&bp->ntp_fltr_lock);
15919 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15920 	if (bit_id < 0) {
15921 		spin_unlock_bh(&bp->ntp_fltr_lock);
15922 		return -ENOMEM;
15923 	}
15924 
15925 	fltr->base.sw_id = (u16)bit_id;
15926 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15927 	fltr->base.flags |= BNXT_ACT_RING_DST;
15928 	head = &bp->ntp_fltr_hash_tbl[idx];
15929 	hlist_add_head_rcu(&fltr->base.hash, head);
15930 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15931 	bnxt_insert_usr_fltr(bp, &fltr->base);
15932 	bp->ntp_fltr_count++;
15933 	spin_unlock_bh(&bp->ntp_fltr_lock);
15934 	return 0;
15935 }
15936 
15937 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15938 			    struct bnxt_ntuple_filter *f2)
15939 {
15940 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15941 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15942 	struct flow_keys *keys1 = &f1->fkeys;
15943 	struct flow_keys *keys2 = &f2->fkeys;
15944 
15945 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15946 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15947 		return false;
15948 
15949 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15950 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15951 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15952 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15953 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15954 			return false;
15955 	} else {
15956 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15957 				     &keys2->addrs.v6addrs.src) ||
15958 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15959 				     &masks2->addrs.v6addrs.src) ||
15960 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15961 				     &keys2->addrs.v6addrs.dst) ||
15962 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15963 				     &masks2->addrs.v6addrs.dst))
15964 			return false;
15965 	}
15966 
15967 	return keys1->ports.src == keys2->ports.src &&
15968 	       masks1->ports.src == masks2->ports.src &&
15969 	       keys1->ports.dst == keys2->ports.dst &&
15970 	       masks1->ports.dst == masks2->ports.dst &&
15971 	       keys1->control.flags == keys2->control.flags &&
15972 	       f1->l2_fltr == f2->l2_fltr;
15973 }
15974 
15975 struct bnxt_ntuple_filter *
15976 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15977 				struct bnxt_ntuple_filter *fltr, u32 idx)
15978 {
15979 	struct bnxt_ntuple_filter *f;
15980 	struct hlist_head *head;
15981 
15982 	head = &bp->ntp_fltr_hash_tbl[idx];
15983 	hlist_for_each_entry_rcu(f, head, base.hash) {
15984 		if (bnxt_fltr_match(f, fltr))
15985 			return f;
15986 	}
15987 	return NULL;
15988 }
15989 
15990 #ifdef CONFIG_RFS_ACCEL
15991 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15992 			      u16 rxq_index, u32 flow_id)
15993 {
15994 	struct bnxt *bp = netdev_priv(dev);
15995 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15996 	struct flow_keys *fkeys;
15997 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15998 	struct bnxt_l2_filter *l2_fltr;
15999 	int rc = 0, idx;
16000 	u32 flags;
16001 
16002 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
16003 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
16004 		atomic_inc(&l2_fltr->refcnt);
16005 	} else {
16006 		struct bnxt_l2_key key;
16007 
16008 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
16009 		key.vlan = 0;
16010 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
16011 		if (!l2_fltr)
16012 			return -EINVAL;
16013 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
16014 			bnxt_del_l2_filter(bp, l2_fltr);
16015 			return -EINVAL;
16016 		}
16017 	}
16018 	new_fltr = kzalloc_obj(*new_fltr, GFP_ATOMIC);
16019 	if (!new_fltr) {
16020 		bnxt_del_l2_filter(bp, l2_fltr);
16021 		return -ENOMEM;
16022 	}
16023 
16024 	fkeys = &new_fltr->fkeys;
16025 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
16026 		rc = -EPROTONOSUPPORT;
16027 		goto err_free;
16028 	}
16029 
16030 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
16031 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
16032 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
16033 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
16034 		rc = -EPROTONOSUPPORT;
16035 		goto err_free;
16036 	}
16037 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
16038 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
16039 		if (bp->hwrm_spec_code < 0x10601) {
16040 			rc = -EPROTONOSUPPORT;
16041 			goto err_free;
16042 		}
16043 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
16044 	}
16045 	flags = fkeys->control.flags;
16046 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
16047 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
16048 		rc = -EPROTONOSUPPORT;
16049 		goto err_free;
16050 	}
16051 	new_fltr->l2_fltr = l2_fltr;
16052 
16053 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
16054 	rcu_read_lock();
16055 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
16056 	if (fltr) {
16057 		rc = fltr->base.sw_id;
16058 		rcu_read_unlock();
16059 		goto err_free;
16060 	}
16061 	rcu_read_unlock();
16062 
16063 	new_fltr->flow_id = flow_id;
16064 	new_fltr->base.rxq = rxq_index;
16065 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
16066 	if (!rc) {
16067 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
16068 		return new_fltr->base.sw_id;
16069 	}
16070 
16071 err_free:
16072 	bnxt_del_l2_filter(bp, l2_fltr);
16073 	kfree(new_fltr);
16074 	return rc;
16075 }
16076 #endif
16077 
16078 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
16079 {
16080 	spin_lock_bh(&bp->ntp_fltr_lock);
16081 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
16082 		spin_unlock_bh(&bp->ntp_fltr_lock);
16083 		return;
16084 	}
16085 	hlist_del_rcu(&fltr->base.hash);
16086 	bnxt_del_one_usr_fltr(bp, &fltr->base);
16087 	bp->ntp_fltr_count--;
16088 	spin_unlock_bh(&bp->ntp_fltr_lock);
16089 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
16090 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
16091 	kfree_rcu(fltr, base.rcu);
16092 }
16093 
16094 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
16095 {
16096 #ifdef CONFIG_RFS_ACCEL
16097 	int i;
16098 
16099 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
16100 		struct hlist_head *head;
16101 		struct hlist_node *tmp;
16102 		struct bnxt_ntuple_filter *fltr;
16103 		int rc;
16104 
16105 		head = &bp->ntp_fltr_hash_tbl[i];
16106 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
16107 			bool del = false;
16108 
16109 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
16110 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
16111 					continue;
16112 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
16113 							fltr->flow_id,
16114 							fltr->base.sw_id)) {
16115 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
16116 									 fltr);
16117 					del = true;
16118 				}
16119 			} else {
16120 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
16121 								       fltr);
16122 				if (rc)
16123 					del = true;
16124 				else
16125 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
16126 			}
16127 
16128 			if (del)
16129 				bnxt_del_ntp_filter(bp, fltr);
16130 		}
16131 	}
16132 #endif
16133 }
16134 
16135 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
16136 				    unsigned int entry, struct udp_tunnel_info *ti)
16137 {
16138 	struct bnxt *bp = netdev_priv(netdev);
16139 	unsigned int cmd;
16140 
16141 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
16142 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
16143 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
16144 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
16145 	else
16146 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
16147 
16148 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
16149 }
16150 
16151 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
16152 				      unsigned int entry, struct udp_tunnel_info *ti)
16153 {
16154 	struct bnxt *bp = netdev_priv(netdev);
16155 	unsigned int cmd;
16156 
16157 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
16158 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
16159 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
16160 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
16161 	else
16162 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
16163 
16164 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
16165 }
16166 
16167 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
16168 	.set_port	= bnxt_udp_tunnel_set_port,
16169 	.unset_port	= bnxt_udp_tunnel_unset_port,
16170 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
16171 	.tables		= {
16172 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
16173 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
16174 	},
16175 }, bnxt_udp_tunnels_p7 = {
16176 	.set_port	= bnxt_udp_tunnel_set_port,
16177 	.unset_port	= bnxt_udp_tunnel_unset_port,
16178 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
16179 	.tables		= {
16180 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
16181 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
16182 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
16183 	},
16184 };
16185 
16186 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
16187 			       struct net_device *dev, u32 filter_mask,
16188 			       int nlflags)
16189 {
16190 	struct bnxt *bp = netdev_priv(dev);
16191 
16192 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
16193 				       nlflags, filter_mask, NULL);
16194 }
16195 
16196 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
16197 			       u16 flags, struct netlink_ext_ack *extack)
16198 {
16199 	struct bnxt *bp = netdev_priv(dev);
16200 	struct nlattr *attr, *br_spec;
16201 	int rem, rc = 0;
16202 
16203 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
16204 		return -EOPNOTSUPP;
16205 
16206 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
16207 	if (!br_spec)
16208 		return -EINVAL;
16209 
16210 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
16211 		u16 mode;
16212 
16213 		mode = nla_get_u16(attr);
16214 		if (mode == bp->br_mode)
16215 			break;
16216 
16217 		rc = bnxt_hwrm_set_br_mode(bp, mode);
16218 		if (!rc)
16219 			bp->br_mode = mode;
16220 		break;
16221 	}
16222 	return rc;
16223 }
16224 
16225 int bnxt_get_port_parent_id(struct net_device *dev,
16226 			    struct netdev_phys_item_id *ppid)
16227 {
16228 	struct bnxt *bp = netdev_priv(dev);
16229 
16230 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
16231 		return -EOPNOTSUPP;
16232 
16233 	/* The PF and it's VF-reps only support the switchdev framework */
16234 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
16235 		return -EOPNOTSUPP;
16236 
16237 	ppid->id_len = sizeof(bp->dsn);
16238 	memcpy(ppid->id, bp->dsn, ppid->id_len);
16239 
16240 	return 0;
16241 }
16242 
16243 static const struct net_device_ops bnxt_netdev_ops = {
16244 	.ndo_open		= bnxt_open,
16245 	.ndo_start_xmit		= bnxt_start_xmit,
16246 	.ndo_stop		= bnxt_close,
16247 	.ndo_get_stats64	= bnxt_get_stats64,
16248 	.ndo_set_rx_mode_async	= bnxt_set_rx_mode,
16249 	.ndo_eth_ioctl		= bnxt_ioctl,
16250 	.ndo_validate_addr	= eth_validate_addr,
16251 	.ndo_set_mac_address	= bnxt_change_mac_addr,
16252 	.ndo_change_mtu		= bnxt_change_mtu,
16253 	.ndo_fix_features	= bnxt_fix_features,
16254 	.ndo_set_features	= bnxt_set_features,
16255 	.ndo_features_check	= bnxt_features_check,
16256 	.ndo_tx_timeout		= bnxt_tx_timeout,
16257 #ifdef CONFIG_BNXT_SRIOV
16258 	.ndo_get_vf_config	= bnxt_get_vf_config,
16259 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
16260 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
16261 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
16262 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
16263 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
16264 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
16265 #endif
16266 	.ndo_setup_tc           = bnxt_setup_tc,
16267 #ifdef CONFIG_RFS_ACCEL
16268 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
16269 #endif
16270 	.ndo_bpf		= bnxt_xdp,
16271 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
16272 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
16273 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
16274 	.ndo_hwtstamp_get	= bnxt_hwtstamp_get,
16275 	.ndo_hwtstamp_set	= bnxt_hwtstamp_set,
16276 };
16277 
16278 static const struct xdp_metadata_ops bnxt_xdp_metadata_ops = {
16279 	.xmo_rx_hash		= bnxt_xdp_rx_hash,
16280 };
16281 
16282 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
16283 				    struct netdev_queue_stats_rx *stats)
16284 {
16285 	struct bnxt *bp = netdev_priv(dev);
16286 	struct bnxt_cp_ring_info *cpr;
16287 	u64 *sw;
16288 
16289 	if (!bp->bnapi)
16290 		return;
16291 
16292 	bnxt_sync_ring_stats(bp);
16293 	cpr = &bp->bnapi[i]->cp_ring;
16294 	sw = cpr->stats.sw_stats;
16295 
16296 	stats->packets = 0;
16297 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
16298 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
16299 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
16300 
16301 	stats->bytes = 0;
16302 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
16303 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
16304 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
16305 
16306 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
16307 	stats->hw_gro_packets = cpr->sw_stats->rx.rx_hw_gro_packets;
16308 	stats->hw_gro_wire_packets = cpr->sw_stats->rx.rx_hw_gro_wire_packets;
16309 }
16310 
16311 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
16312 				    struct netdev_queue_stats_tx *stats)
16313 {
16314 	struct bnxt *bp = netdev_priv(dev);
16315 	struct bnxt_napi *bnapi;
16316 	u64 *sw;
16317 
16318 	if (!bp->tx_ring)
16319 		return;
16320 
16321 	bnxt_sync_ring_stats(bp);
16322 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
16323 	sw = bnapi->cp_ring.stats.sw_stats;
16324 
16325 	stats->packets = 0;
16326 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
16327 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
16328 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
16329 
16330 	stats->bytes = 0;
16331 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
16332 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
16333 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
16334 }
16335 
16336 static void bnxt_get_base_stats(struct net_device *dev,
16337 				struct netdev_queue_stats_rx *rx,
16338 				struct netdev_queue_stats_tx *tx)
16339 {
16340 	struct bnxt *bp = netdev_priv(dev);
16341 
16342 	rx->packets = bp->net_stats_prev.rx_packets;
16343 	rx->bytes = bp->net_stats_prev.rx_bytes;
16344 	rx->alloc_fail = bp->ring_drv_stats_prev.rx_total_oom_discards;
16345 	rx->hw_gro_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_packets;
16346 	rx->hw_gro_wire_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_wire_packets;
16347 
16348 	tx->packets = bp->net_stats_prev.tx_packets;
16349 	tx->bytes = bp->net_stats_prev.tx_bytes;
16350 }
16351 
16352 static const struct netdev_stat_ops bnxt_stat_ops = {
16353 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
16354 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
16355 	.get_base_stats		= bnxt_get_base_stats,
16356 };
16357 
16358 static void bnxt_queue_default_qcfg(struct net_device *dev,
16359 				    struct netdev_queue_config *qcfg)
16360 {
16361 	qcfg->rx_page_size = BNXT_RX_PAGE_SIZE;
16362 }
16363 
16364 static int bnxt_validate_qcfg(struct net_device *dev,
16365 			      struct netdev_queue_config *qcfg,
16366 			      struct netlink_ext_ack *extack)
16367 {
16368 	struct bnxt *bp = netdev_priv(dev);
16369 
16370 	/* Older chips need MSS calc so rx_page_size is not supported */
16371 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16372 	    qcfg->rx_page_size != BNXT_RX_PAGE_SIZE)
16373 		return -EINVAL;
16374 
16375 	if (!is_power_of_2(qcfg->rx_page_size))
16376 		return -ERANGE;
16377 
16378 	if (qcfg->rx_page_size < BNXT_RX_PAGE_SIZE ||
16379 	    qcfg->rx_page_size > BNXT_MAX_RX_PAGE_SIZE)
16380 		return -ERANGE;
16381 
16382 	return 0;
16383 }
16384 
16385 static int bnxt_queue_mem_alloc(struct net_device *dev,
16386 				struct netdev_queue_config *qcfg,
16387 				void *qmem, int idx)
16388 {
16389 	struct bnxt_rx_ring_info *rxr, *clone;
16390 	struct bnxt *bp = netdev_priv(dev);
16391 	struct bnxt_ring_struct *ring;
16392 	int rc;
16393 
16394 	if (!bp->rx_ring)
16395 		return -ENETDOWN;
16396 
16397 	rxr = &bp->rx_ring[idx];
16398 	clone = qmem;
16399 	memcpy(clone, rxr, sizeof(*rxr));
16400 	bnxt_init_rx_ring_struct(bp, clone);
16401 	bnxt_reset_rx_ring_struct(bp, clone);
16402 
16403 	clone->rx_prod = 0;
16404 	clone->rx_agg_prod = 0;
16405 	clone->rx_sw_agg_prod = 0;
16406 	clone->rx_next_cons = 0;
16407 	clone->need_head_pool = false;
16408 	clone->rx_page_size = qcfg->rx_page_size;
16409 	clone->rx_agg_bmap = NULL;
16410 	clone->rx_tpa = NULL;
16411 	clone->rx_tpa_idx_map = NULL;
16412 
16413 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
16414 	if (rc)
16415 		return rc;
16416 
16417 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
16418 	if (rc < 0)
16419 		goto err_page_pool_destroy;
16420 
16421 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
16422 					MEM_TYPE_PAGE_POOL,
16423 					clone->page_pool);
16424 	if (rc)
16425 		goto err_rxq_info_unreg;
16426 
16427 	ring = &clone->rx_ring_struct;
16428 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16429 	if (rc)
16430 		goto err_free_rx_ring;
16431 
16432 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
16433 		ring = &clone->rx_agg_ring_struct;
16434 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16435 		if (rc)
16436 			goto err_free_rx_agg_ring;
16437 
16438 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
16439 		if (rc)
16440 			goto err_free_rx_agg_ring;
16441 	}
16442 
16443 	if (bp->flags & BNXT_FLAG_TPA) {
16444 		rc = bnxt_alloc_one_tpa_info(bp, clone);
16445 		if (rc)
16446 			goto err_free_tpa_info;
16447 	}
16448 
16449 	bnxt_init_one_rx_ring_rxbd(bp, clone);
16450 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
16451 
16452 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
16453 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16454 		bnxt_alloc_one_rx_ring_netmem(bp, clone, idx);
16455 	if (bp->flags & BNXT_FLAG_TPA) {
16456 		rc = bnxt_alloc_one_tpa_info_data(bp, clone);
16457 		if (rc)
16458 			goto err_free_rx_ring_skbs;
16459 	}
16460 
16461 	return 0;
16462 
16463 err_free_rx_ring_skbs:
16464 	bnxt_free_one_rx_ring_skbs(bp, clone);
16465 err_free_tpa_info:
16466 	bnxt_free_one_tpa_info(bp, clone);
16467 err_free_rx_agg_ring:
16468 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
16469 	kfree(clone->rx_agg_bmap);
16470 	clone->rx_agg_bmap = NULL;
16471 err_free_rx_ring:
16472 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
16473 err_rxq_info_unreg:
16474 	xdp_rxq_info_unreg(&clone->xdp_rxq);
16475 err_page_pool_destroy:
16476 	page_pool_destroy(clone->page_pool);
16477 	page_pool_destroy(clone->head_pool);
16478 	clone->page_pool = NULL;
16479 	clone->head_pool = NULL;
16480 	return rc;
16481 }
16482 
16483 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
16484 {
16485 	struct bnxt_rx_ring_info *rxr = qmem;
16486 	struct bnxt *bp = netdev_priv(dev);
16487 	struct bnxt_ring_struct *ring;
16488 
16489 	bnxt_free_one_rx_ring_skbs(bp, rxr);
16490 	bnxt_free_one_tpa_info(bp, rxr);
16491 
16492 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
16493 
16494 	page_pool_destroy(rxr->page_pool);
16495 	page_pool_destroy(rxr->head_pool);
16496 	rxr->page_pool = NULL;
16497 	rxr->head_pool = NULL;
16498 
16499 	ring = &rxr->rx_ring_struct;
16500 	bnxt_free_ring(bp, &ring->ring_mem);
16501 
16502 	ring = &rxr->rx_agg_ring_struct;
16503 	bnxt_free_ring(bp, &ring->ring_mem);
16504 
16505 	kfree(rxr->rx_agg_bmap);
16506 	rxr->rx_agg_bmap = NULL;
16507 }
16508 
16509 static void bnxt_copy_rx_ring(struct bnxt *bp,
16510 			      struct bnxt_rx_ring_info *dst,
16511 			      struct bnxt_rx_ring_info *src)
16512 {
16513 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
16514 	struct bnxt_ring_struct *dst_ring, *src_ring;
16515 	int i;
16516 
16517 	dst_ring = &dst->rx_ring_struct;
16518 	dst_rmem = &dst_ring->ring_mem;
16519 	src_ring = &src->rx_ring_struct;
16520 	src_rmem = &src_ring->ring_mem;
16521 
16522 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16523 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16524 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16525 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16526 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16527 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16528 
16529 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16530 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16531 	*dst_rmem->vmem = *src_rmem->vmem;
16532 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16533 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16534 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16535 	}
16536 
16537 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
16538 		return;
16539 
16540 	dst_ring = &dst->rx_agg_ring_struct;
16541 	dst_rmem = &dst_ring->ring_mem;
16542 	src_ring = &src->rx_agg_ring_struct;
16543 	src_rmem = &src_ring->ring_mem;
16544 
16545 	dst->rx_page_size = src->rx_page_size;
16546 
16547 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16548 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16549 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16550 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16551 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16552 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16553 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
16554 
16555 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16556 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16557 	*dst_rmem->vmem = *src_rmem->vmem;
16558 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16559 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16560 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16561 	}
16562 
16563 	dst->rx_agg_bmap = src->rx_agg_bmap;
16564 }
16565 
16566 static int bnxt_queue_start(struct net_device *dev,
16567 			    struct netdev_queue_config *qcfg,
16568 			    void *qmem, int idx)
16569 {
16570 	struct bnxt *bp = netdev_priv(dev);
16571 	struct bnxt_rx_ring_info *rxr, *clone;
16572 	struct bnxt_cp_ring_info *cpr;
16573 	struct bnxt_vnic_info *vnic;
16574 	struct bnxt_napi *bnapi;
16575 	int i, rc;
16576 	u16 mru;
16577 
16578 	rxr = &bp->rx_ring[idx];
16579 	clone = qmem;
16580 
16581 	rxr->rx_prod = clone->rx_prod;
16582 	rxr->rx_agg_prod = clone->rx_agg_prod;
16583 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
16584 	rxr->rx_next_cons = clone->rx_next_cons;
16585 	rxr->rx_tpa = clone->rx_tpa;
16586 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
16587 	rxr->page_pool = clone->page_pool;
16588 	rxr->head_pool = clone->head_pool;
16589 	rxr->xdp_rxq = clone->xdp_rxq;
16590 	rxr->need_head_pool = clone->need_head_pool;
16591 
16592 	bnxt_copy_rx_ring(bp, rxr, clone);
16593 
16594 	bnapi = rxr->bnapi;
16595 	cpr = &bnapi->cp_ring;
16596 
16597 	/* All rings have been reserved and previously allocated.
16598 	 * Reallocating with the same parameters should never fail.
16599 	 */
16600 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
16601 	if (rc)
16602 		goto err_reset;
16603 
16604 	if (bp->tph_mode) {
16605 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
16606 		if (rc)
16607 			goto err_reset;
16608 	}
16609 
16610 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
16611 	if (rc)
16612 		goto err_reset;
16613 
16614 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
16615 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16616 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
16617 
16618 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
16619 		rc = bnxt_tx_queue_start(bp, idx);
16620 		if (rc)
16621 			goto err_reset;
16622 	}
16623 
16624 	bnxt_enable_rx_page_pool(rxr);
16625 	napi_enable_locked(&bnapi->napi);
16626 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16627 
16628 	mru = bp->dev->mtu + VLAN_ETH_HLEN;
16629 	for (i = 0; i < bp->nr_vnics; i++) {
16630 		vnic = &bp->vnic_info[i];
16631 
16632 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx);
16633 		if (rc)
16634 			return rc;
16635 	}
16636 	return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx);
16637 
16638 err_reset:
16639 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
16640 		   rc);
16641 	napi_enable_locked(&bnapi->napi);
16642 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16643 	netif_close(dev);
16644 	return rc;
16645 }
16646 
16647 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
16648 {
16649 	struct bnxt *bp = netdev_priv(dev);
16650 	struct bnxt_rx_ring_info *rxr;
16651 	struct bnxt_cp_ring_info *cpr;
16652 	struct bnxt_vnic_info *vnic;
16653 	struct bnxt_napi *bnapi;
16654 	int i;
16655 
16656 	for (i = 0; i < bp->nr_vnics; i++) {
16657 		vnic = &bp->vnic_info[i];
16658 
16659 		bnxt_set_vnic_mru_p5(bp, vnic, 0, idx);
16660 	}
16661 	bnxt_set_rss_ctx_vnic_mru(bp, 0, idx);
16662 	/* Make sure NAPI sees that the VNIC is disabled */
16663 	synchronize_net();
16664 	rxr = &bp->rx_ring[idx];
16665 	bnapi = rxr->bnapi;
16666 	cpr = &bnapi->cp_ring;
16667 	cancel_work_sync(&cpr->dim.work);
16668 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
16669 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
16670 	page_pool_disable_direct_recycling(rxr->page_pool);
16671 	if (bnxt_separate_head_pool(rxr))
16672 		page_pool_disable_direct_recycling(rxr->head_pool);
16673 
16674 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
16675 		bnxt_tx_queue_stop(bp, idx);
16676 
16677 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
16678 	 * completion is handled in NAPI to guarantee no more DMA on that ring
16679 	 * after seeing the completion.
16680 	 */
16681 	napi_disable_locked(&bnapi->napi);
16682 
16683 	if (bp->tph_mode) {
16684 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
16685 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
16686 	}
16687 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
16688 
16689 	memcpy(qmem, rxr, sizeof(*rxr));
16690 	bnxt_init_rx_ring_struct(bp, qmem);
16691 
16692 	return 0;
16693 }
16694 
16695 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
16696 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
16697 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
16698 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
16699 	.ndo_queue_start	= bnxt_queue_start,
16700 	.ndo_queue_stop		= bnxt_queue_stop,
16701 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16702 	.ndo_validate_qcfg	= bnxt_validate_qcfg,
16703 	.supported_params	= QCFG_RX_PAGE_SIZE,
16704 };
16705 
16706 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops_unsupp = {
16707 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16708 };
16709 
16710 static void bnxt_remove_one(struct pci_dev *pdev)
16711 {
16712 	struct net_device *dev = pci_get_drvdata(pdev);
16713 	struct bnxt *bp = netdev_priv(dev);
16714 
16715 	if (BNXT_PF(bp))
16716 		__bnxt_sriov_disable(bp);
16717 
16718 	bnxt_aux_devices_del(bp);
16719 
16720 	unregister_netdev(dev);
16721 	bnxt_ptp_clear(bp);
16722 
16723 	bnxt_aux_devices_uninit(bp);
16724 	bnxt_auxdev_id_free(bp, bp->auxdev_id);
16725 
16726 	bnxt_free_l2_filters(bp, true);
16727 	bnxt_free_ntp_fltrs(bp, true);
16728 	WARN_ON(bp->num_rss_ctx);
16729 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16730 	/* Flush any pending tasks */
16731 	cancel_work_sync(&bp->sp_task);
16732 	cancel_delayed_work_sync(&bp->fw_reset_task);
16733 	bp->sp_event = 0;
16734 
16735 	bnxt_dl_fw_reporters_destroy(bp);
16736 	bnxt_dl_unregister(bp);
16737 	bnxt_shutdown_tc(bp);
16738 
16739 	bnxt_clear_int_mode(bp);
16740 	bnxt_free_ring_cpu_masks(bp);
16741 	bnxt_hwrm_func_drv_unrgtr(bp);
16742 	bnxt_free_hwrm_resources(bp);
16743 	bnxt_hwmon_uninit(bp);
16744 	bnxt_ethtool_free(bp);
16745 	bnxt_dcb_free(bp);
16746 	kfree(bp->ptp_cfg);
16747 	bp->ptp_cfg = NULL;
16748 	kfree(bp->fw_health);
16749 	bp->fw_health = NULL;
16750 	bnxt_cleanup_pci(bp);
16751 	bnxt_free_ctx_mem(bp, true);
16752 	bnxt_free_crash_dump_mem(bp);
16753 	kfree(bp->rss_indir_tbl);
16754 	bp->rss_indir_tbl = NULL;
16755 	bnxt_free_port_stats(bp);
16756 	free_netdev(dev);
16757 }
16758 
16759 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16760 {
16761 	int rc = 0;
16762 	struct bnxt_link_info *link_info = &bp->link_info;
16763 
16764 	bp->phy_flags = 0;
16765 	rc = bnxt_hwrm_phy_qcaps(bp);
16766 	if (rc) {
16767 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16768 			   rc);
16769 		return rc;
16770 	}
16771 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16772 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16773 	else
16774 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16775 
16776 	bp->mac_flags = 0;
16777 	bnxt_hwrm_mac_qcaps(bp);
16778 
16779 	if (!fw_dflt)
16780 		return 0;
16781 
16782 	mutex_lock(&bp->link_lock);
16783 	rc = bnxt_update_link(bp, false);
16784 	if (rc) {
16785 		mutex_unlock(&bp->link_lock);
16786 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16787 			   rc);
16788 		return rc;
16789 	}
16790 
16791 	/* Older firmware does not have supported_auto_speeds, so assume
16792 	 * that all supported speeds can be autonegotiated.
16793 	 */
16794 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16795 		link_info->support_auto_speeds = link_info->support_speeds;
16796 
16797 	bnxt_init_ethtool_link_settings(bp);
16798 	mutex_unlock(&bp->link_lock);
16799 	return 0;
16800 }
16801 
16802 static int bnxt_get_max_irq(struct pci_dev *pdev)
16803 {
16804 	u16 ctrl;
16805 
16806 	if (!pdev->msix_cap)
16807 		return 1;
16808 
16809 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16810 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16811 }
16812 
16813 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16814 				int *max_cp)
16815 {
16816 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16817 	int max_ring_grps = 0, max_irq;
16818 
16819 	*max_tx = hw_resc->max_tx_rings;
16820 	*max_rx = hw_resc->max_rx_rings;
16821 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16822 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16823 			bnxt_get_ulp_msix_num_in_use(bp),
16824 			hw_resc->max_stat_ctxs -
16825 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16826 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16827 		*max_cp = min_t(int, *max_cp, max_irq);
16828 	max_ring_grps = hw_resc->max_hw_ring_grps;
16829 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16830 		*max_cp -= 1;
16831 		*max_rx -= 2;
16832 	}
16833 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16834 		*max_rx >>= 1;
16835 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16836 		int rc;
16837 
16838 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16839 		if (rc) {
16840 			*max_rx = 0;
16841 			*max_tx = 0;
16842 		}
16843 		/* On P5 chips, max_cp output param should be available NQs */
16844 		*max_cp = max_irq;
16845 	}
16846 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16847 }
16848 
16849 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16850 {
16851 	int rx, tx, cp;
16852 
16853 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16854 	*max_rx = rx;
16855 	*max_tx = tx;
16856 	if (!rx || !tx || !cp)
16857 		return -ENOMEM;
16858 
16859 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16860 }
16861 
16862 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16863 			       bool shared)
16864 {
16865 	int rc;
16866 
16867 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16868 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16869 		/* Not enough rings, try disabling agg rings. */
16870 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16871 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16872 		if (rc) {
16873 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16874 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16875 			return rc;
16876 		}
16877 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16878 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16879 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16880 		bnxt_set_ring_params(bp);
16881 	}
16882 
16883 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16884 		int max_cp, max_stat, max_irq;
16885 
16886 		/* Reserve minimum resources for RoCE */
16887 		max_cp = bnxt_get_max_func_cp_rings(bp);
16888 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16889 		max_irq = bnxt_get_max_func_irqs(bp);
16890 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16891 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16892 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16893 			return 0;
16894 
16895 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16896 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16897 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16898 		max_cp = min_t(int, max_cp, max_irq);
16899 		max_cp = min_t(int, max_cp, max_stat);
16900 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16901 		if (rc)
16902 			rc = 0;
16903 	}
16904 	return rc;
16905 }
16906 
16907 /* In initial default shared ring setting, each shared ring must have a
16908  * RX/TX ring pair.
16909  */
16910 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16911 {
16912 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16913 	bp->rx_nr_rings = bp->cp_nr_rings;
16914 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16915 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16916 }
16917 
16918 static void bnxt_adj_dflt_rings(struct bnxt *bp, bool sh)
16919 {
16920 	if (sh)
16921 		bnxt_trim_dflt_sh_rings(bp);
16922 	else
16923 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16924 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16925 	if (sh && READ_ONCE(bp->xdp_prog)) {
16926 		bnxt_set_xdp_tx_rings(bp);
16927 		bnxt_set_cp_rings(bp, true);
16928 	}
16929 }
16930 
16931 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16932 {
16933 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16934 	int avail_msix;
16935 
16936 	if (!bnxt_can_reserve_rings(bp))
16937 		return 0;
16938 
16939 	if (sh)
16940 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16941 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16942 	/* Reduce default rings on multi-port cards so that total default
16943 	 * rings do not exceed CPU count.
16944 	 */
16945 	if (bp->port_count > 1) {
16946 		int max_rings =
16947 			max_t(int, num_online_cpus() / bp->port_count, 1);
16948 
16949 		dflt_rings = min_t(int, dflt_rings, max_rings);
16950 	}
16951 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16952 	if (rc)
16953 		return rc;
16954 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16955 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16956 
16957 	bnxt_adj_dflt_rings(bp, sh);
16958 
16959 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16960 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16961 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16962 
16963 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16964 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16965 	}
16966 
16967 	rc = __bnxt_reserve_rings(bp);
16968 	if (rc && rc != -ENODEV)
16969 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16970 
16971 	bnxt_adj_tx_rings(bp);
16972 	if (sh)
16973 		bnxt_adj_dflt_rings(bp, true);
16974 
16975 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16976 	if (bnxt_need_reserve_rings(bp)) {
16977 		rc = __bnxt_reserve_rings(bp);
16978 		if (rc && rc != -ENODEV)
16979 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16980 		bnxt_adj_tx_rings(bp);
16981 	}
16982 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16983 		bp->rx_nr_rings++;
16984 		bp->cp_nr_rings++;
16985 	}
16986 	if (rc) {
16987 		bp->tx_nr_rings = 0;
16988 		bp->rx_nr_rings = 0;
16989 	}
16990 	return rc;
16991 }
16992 
16993 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16994 {
16995 	int rc;
16996 
16997 	if (bp->tx_nr_rings)
16998 		return 0;
16999 
17000 	bnxt_ulp_irq_stop(bp);
17001 	bnxt_clear_int_mode(bp);
17002 	rc = bnxt_set_dflt_rings(bp, true);
17003 	if (rc) {
17004 		if (BNXT_VF(bp) && rc == -ENODEV)
17005 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
17006 		else
17007 			netdev_err(bp->dev, "Not enough rings available.\n");
17008 		goto init_dflt_ring_err;
17009 	}
17010 	rc = bnxt_init_int_mode(bp);
17011 	if (rc)
17012 		goto init_dflt_ring_err;
17013 
17014 	bnxt_adj_tx_rings(bp);
17015 
17016 	bnxt_set_dflt_rfs(bp);
17017 
17018 init_dflt_ring_err:
17019 	bnxt_ulp_irq_restart(bp, rc);
17020 	return rc;
17021 }
17022 
17023 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
17024 {
17025 	int rc;
17026 
17027 	netdev_assert_locked_ops_compat(bp->dev);
17028 	bnxt_hwrm_func_qcaps(bp);
17029 
17030 	if (netif_running(bp->dev))
17031 		__bnxt_close_nic(bp, true, false);
17032 
17033 	bnxt_ulp_irq_stop(bp);
17034 	bnxt_clear_int_mode(bp);
17035 	rc = bnxt_init_int_mode(bp);
17036 	bnxt_ulp_irq_restart(bp, rc);
17037 
17038 	if (netif_running(bp->dev)) {
17039 		if (rc)
17040 			netif_close(bp->dev);
17041 		else
17042 			rc = bnxt_open_nic(bp, true, false);
17043 	}
17044 
17045 	return rc;
17046 }
17047 
17048 static int bnxt_init_mac_addr(struct bnxt *bp)
17049 {
17050 	int rc = 0;
17051 
17052 	if (BNXT_PF(bp)) {
17053 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
17054 	} else {
17055 #ifdef CONFIG_BNXT_SRIOV
17056 		struct bnxt_vf_info *vf = &bp->vf;
17057 		bool strict_approval = true;
17058 
17059 		if (is_valid_ether_addr(vf->mac_addr)) {
17060 			/* overwrite netdev dev_addr with admin VF MAC */
17061 			eth_hw_addr_set(bp->dev, vf->mac_addr);
17062 			/* Older PF driver or firmware may not approve this
17063 			 * correctly.
17064 			 */
17065 			strict_approval = false;
17066 		} else {
17067 			eth_hw_addr_random(bp->dev);
17068 		}
17069 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
17070 #endif
17071 	}
17072 	return rc;
17073 }
17074 
17075 static void bnxt_vpd_read_info(struct bnxt *bp)
17076 {
17077 	struct pci_dev *pdev = bp->pdev;
17078 	unsigned int vpd_size, kw_len;
17079 	int pos, size;
17080 	u8 *vpd_data;
17081 
17082 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
17083 	if (IS_ERR(vpd_data)) {
17084 		pci_warn(pdev, "Unable to read VPD\n");
17085 		return;
17086 	}
17087 
17088 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
17089 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
17090 	if (pos < 0)
17091 		goto read_sn;
17092 
17093 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
17094 	memcpy(bp->board_partno, &vpd_data[pos], size);
17095 
17096 read_sn:
17097 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
17098 					   PCI_VPD_RO_KEYWORD_SERIALNO,
17099 					   &kw_len);
17100 	if (pos < 0)
17101 		goto exit;
17102 
17103 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
17104 	memcpy(bp->board_serialno, &vpd_data[pos], size);
17105 exit:
17106 	kfree(vpd_data);
17107 }
17108 
17109 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
17110 {
17111 	struct pci_dev *pdev = bp->pdev;
17112 	u64 qword;
17113 
17114 	qword = pci_get_dsn(pdev);
17115 	if (!qword) {
17116 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
17117 		return -EOPNOTSUPP;
17118 	}
17119 
17120 	put_unaligned_le64(qword, dsn);
17121 
17122 	bp->flags |= BNXT_FLAG_DSN_VALID;
17123 	return 0;
17124 }
17125 
17126 static int bnxt_map_db_bar(struct bnxt *bp)
17127 {
17128 	if (!bp->db_size)
17129 		return -ENODEV;
17130 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
17131 	if (!bp->bar1)
17132 		return -ENOMEM;
17133 	return 0;
17134 }
17135 
17136 void bnxt_print_device_info(struct bnxt *bp)
17137 {
17138 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
17139 		    board_info[bp->board_idx].name,
17140 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
17141 
17142 	pcie_print_link_status(bp->pdev);
17143 }
17144 
17145 static void bnxt_clear_bars(struct pci_dev *pdev)
17146 {
17147 	int off;
17148 
17149 	for (off = PCI_BASE_ADDRESS_0; off <= PCI_BASE_ADDRESS_5; off += 4)
17150 		pci_write_config_dword(pdev, off, 0);
17151 }
17152 
17153 /* Clear any pending DMA transactions from crash kernel while loading driver in
17154  * capture kernel.
17155  */
17156 static int bnxt_kdump_reset(struct pci_dev *pdev)
17157 {
17158 	int rc, i;
17159 	u16 cmd;
17160 
17161 	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
17162 	cmd &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
17163 	pci_write_config_word(pdev, PCI_COMMAND, cmd);
17164 
17165 	if (pci_save_state(pdev))
17166 		dev_warn(&pdev->dev, "Failed to save PCI state, PCI restore may be incomplete\n");
17167 
17168 	rc = pcie_flr(pdev);
17169 	if (rc)
17170 		dev_warn(&pdev->dev, "pcie_flr() failed (rc: %d), trying to continue\n",
17171 			 rc);
17172 
17173 	/* In case device is not returning CRS, wait 5 seconds longer */
17174 	for (i = 0; i < 50; i++) {
17175 		pci_read_config_word(pdev, PCI_COMMAND, &cmd);
17176 		if (!PCI_POSSIBLE_ERROR(cmd))
17177 			break;
17178 		msleep(100);
17179 	}
17180 	if (PCI_POSSIBLE_ERROR(cmd)) {
17181 		dev_err(&pdev->dev, "PCI config space inaccessible after FLR, aborting\n");
17182 		return -ENODEV;
17183 	}
17184 
17185 	bnxt_clear_bars(pdev);
17186 	pci_restore_state(pdev);
17187 	return 0;
17188 }
17189 
17190 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
17191 {
17192 	struct bnxt_hw_resc *hw_resc;
17193 	struct net_device *dev;
17194 	struct bnxt *bp;
17195 	int rc, max_irqs;
17196 
17197 	if (pci_is_bridge(pdev))
17198 		return -ENODEV;
17199 
17200 	if (!pdev->msix_cap) {
17201 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
17202 		return -ENODEV;
17203 	}
17204 
17205 	if (is_kdump_kernel()) {
17206 		rc = bnxt_kdump_reset(pdev);
17207 		if (rc)
17208 			return rc;
17209 	}
17210 
17211 	max_irqs = bnxt_get_max_irq(pdev);
17212 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
17213 				 max_irqs);
17214 	if (!dev)
17215 		return -ENOMEM;
17216 
17217 	bp = netdev_priv(dev);
17218 	bp->board_idx = ent->driver_data;
17219 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
17220 	bnxt_set_max_func_irqs(bp, max_irqs);
17221 
17222 	bp->max_irqs = max_irqs;
17223 	rc = bnxt_alloc_ring_cpu_masks(bp);
17224 	if (rc)
17225 		goto init_err_free;
17226 
17227 	if (bnxt_vf_pciid(bp->board_idx))
17228 		bp->flags |= BNXT_FLAG_VF;
17229 
17230 	/* No devlink port registration in case of a VF */
17231 	if (BNXT_PF(bp))
17232 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
17233 
17234 	rc = bnxt_init_board(pdev, dev);
17235 	if (rc < 0)
17236 		goto init_err_free;
17237 
17238 	dev->netdev_ops = &bnxt_netdev_ops;
17239 	dev->xdp_metadata_ops = &bnxt_xdp_metadata_ops;
17240 	dev->stat_ops = &bnxt_stat_ops;
17241 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
17242 	dev->ethtool_ops = &bnxt_ethtool_ops;
17243 	pci_set_drvdata(pdev, dev);
17244 
17245 	rc = bnxt_alloc_hwrm_resources(bp);
17246 	if (rc)
17247 		goto init_err_pci_clean;
17248 
17249 	mutex_init(&bp->hwrm_cmd_lock);
17250 	mutex_init(&bp->link_lock);
17251 
17252 	rc = bnxt_fw_init_one_p1(bp);
17253 	if (rc)
17254 		goto init_err_pci_clean;
17255 
17256 	if (BNXT_PF(bp))
17257 		bnxt_vpd_read_info(bp);
17258 
17259 	if (BNXT_CHIP_P5_PLUS(bp)) {
17260 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
17261 		if (BNXT_CHIP_P7(bp))
17262 			bp->flags |= BNXT_FLAG_CHIP_P7;
17263 	}
17264 
17265 	rc = bnxt_alloc_rss_indir_tbl(bp);
17266 	if (rc)
17267 		goto init_err_pci_clean;
17268 
17269 	rc = bnxt_fw_init_one_p2(bp);
17270 	if (rc)
17271 		goto init_err_pci_clean;
17272 
17273 	rc = bnxt_map_db_bar(bp);
17274 	if (rc) {
17275 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
17276 			rc);
17277 		goto init_err_pci_clean;
17278 	}
17279 
17280 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
17281 			   NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN |
17282 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
17283 			   NETIF_F_GSO_IPXIP4 |
17284 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
17285 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
17286 			   NETIF_F_RXCSUM | NETIF_F_GRO;
17287 	dev->hw_features |= NETIF_F_GSO_UDP_L4;
17288 
17289 	if (BNXT_SUPPORTS_TPA(bp))
17290 		dev->hw_features |= NETIF_F_LRO;
17291 
17292 	dev->hw_enc_features =
17293 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
17294 			NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN |
17295 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
17296 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
17297 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
17298 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
17299 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
17300 	if (bp->flags & BNXT_FLAG_CHIP_P7)
17301 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
17302 	else
17303 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
17304 
17305 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
17306 				    NETIF_F_GSO_GRE_CSUM;
17307 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
17308 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
17309 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
17310 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
17311 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
17312 	if (BNXT_SUPPORTS_TPA(bp))
17313 		dev->hw_features |= NETIF_F_GRO_HW;
17314 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
17315 	if (dev->features & NETIF_F_GRO_HW)
17316 		dev->features &= ~NETIF_F_LRO;
17317 	dev->priv_flags |= IFF_UNICAST_FLT;
17318 
17319 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
17320 	if (!(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) {
17321 		u16 max_segs = BNXT_SW_USO_MAX_SEGS;
17322 
17323 		if (bp->tso_max_segs)
17324 			max_segs = min_t(u16, max_segs, bp->tso_max_segs);
17325 		netif_set_tso_max_segs(dev, max_segs);
17326 	} else if (bp->tso_max_segs) {
17327 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
17328 	}
17329 
17330 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
17331 			    NETDEV_XDP_ACT_RX_SG;
17332 
17333 #ifdef CONFIG_BNXT_SRIOV
17334 	init_waitqueue_head(&bp->sriov_cfg_wait);
17335 #endif
17336 	if (BNXT_SUPPORTS_TPA(bp)) {
17337 		bp->gro_func = bnxt_gro_func_5730x;
17338 		if (BNXT_CHIP_P4(bp))
17339 			bp->gro_func = bnxt_gro_func_5731x;
17340 		else if (BNXT_CHIP_P5_PLUS(bp))
17341 			bp->gro_func = bnxt_gro_func_5750x;
17342 	}
17343 	if (!BNXT_CHIP_P4_PLUS(bp))
17344 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
17345 
17346 	rc = bnxt_init_mac_addr(bp);
17347 	if (rc) {
17348 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
17349 		rc = -EADDRNOTAVAIL;
17350 		goto init_err_pci_clean;
17351 	}
17352 
17353 	if (BNXT_PF(bp)) {
17354 		/* Read the adapter's DSN to use as the eswitch switch_id */
17355 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
17356 	}
17357 
17358 	/* MTU range: 60 - FW defined max */
17359 	dev->min_mtu = ETH_ZLEN;
17360 	dev->max_mtu = bp->max_mtu;
17361 
17362 	rc = bnxt_probe_phy(bp, true);
17363 	if (rc)
17364 		goto init_err_pci_clean;
17365 
17366 	hw_resc = &bp->hw_resc;
17367 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
17368 		       BNXT_L2_FLTR_MAX_FLTR;
17369 	/* Older firmware may not report these filters properly */
17370 	if (bp->max_fltr < BNXT_MAX_FLTR)
17371 		bp->max_fltr = BNXT_MAX_FLTR;
17372 	bnxt_init_l2_fltr_tbl(bp);
17373 	__bnxt_set_rx_skb_mode(bp, false);
17374 	bnxt_set_tpa_flags(bp);
17375 	bnxt_init_ring_params(bp);
17376 	bnxt_set_ring_params(bp);
17377 	mutex_init(&bp->auxdev_lock);
17378 	if (!bnxt_auxdev_id_alloc(bp))
17379 		bnxt_aux_devices_init(bp);
17380 	rc = bnxt_set_dflt_rings(bp, true);
17381 	if (rc) {
17382 		if (BNXT_VF(bp) && rc == -ENODEV) {
17383 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
17384 		} else {
17385 			netdev_err(bp->dev, "Not enough rings available.\n");
17386 			rc = -ENOMEM;
17387 		}
17388 		goto init_err_pci_clean;
17389 	}
17390 
17391 	bnxt_fw_init_one_p3(bp);
17392 
17393 	bnxt_init_dflt_coal(bp);
17394 
17395 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
17396 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
17397 
17398 	rc = bnxt_init_int_mode(bp);
17399 	if (rc)
17400 		goto init_err_pci_clean;
17401 
17402 	/* No TC has been set yet and rings may have been trimmed due to
17403 	 * limited MSIX, so we re-initialize the TX rings per TC.
17404 	 */
17405 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
17406 
17407 	if (BNXT_PF(bp)) {
17408 		if (!bnxt_pf_wq) {
17409 			bnxt_pf_wq =
17410 				create_singlethread_workqueue("bnxt_pf_wq");
17411 			if (!bnxt_pf_wq) {
17412 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
17413 				rc = -ENOMEM;
17414 				goto init_err_pci_clean;
17415 			}
17416 		}
17417 		rc = bnxt_init_tc(bp);
17418 		if (rc)
17419 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
17420 				   rc);
17421 	}
17422 
17423 	bnxt_inv_fw_health_reg(bp);
17424 	rc = bnxt_dl_register(bp);
17425 	if (rc)
17426 		goto init_err_dl;
17427 
17428 	INIT_LIST_HEAD(&bp->usr_fltr_list);
17429 
17430 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
17431 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
17432 
17433 	dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops_unsupp;
17434 	if (BNXT_SUPPORTS_QUEUE_API(bp))
17435 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
17436 	dev->netmem_tx = NETMEM_TX_DMA;
17437 
17438 	rc = register_netdev(dev);
17439 	if (rc)
17440 		goto init_err_cleanup;
17441 
17442 	bnxt_dl_fw_reporters_create(bp);
17443 
17444 	bnxt_aux_devices_add(bp);
17445 
17446 	bnxt_print_device_info(bp);
17447 
17448 	pci_save_state(pdev);
17449 
17450 	return 0;
17451 init_err_cleanup:
17452 	bnxt_aux_devices_uninit(bp);
17453 	bnxt_auxdev_id_free(bp, bp->auxdev_id);
17454 	bnxt_dl_unregister(bp);
17455 init_err_dl:
17456 	bnxt_shutdown_tc(bp);
17457 	bnxt_clear_int_mode(bp);
17458 
17459 init_err_pci_clean:
17460 	bnxt_hwrm_func_drv_unrgtr(bp);
17461 	bnxt_ptp_clear(bp);
17462 	kfree(bp->ptp_cfg);
17463 	bp->ptp_cfg = NULL;
17464 	bnxt_free_hwrm_resources(bp);
17465 	bnxt_hwmon_uninit(bp);
17466 	bnxt_ethtool_free(bp);
17467 	kfree(bp->fw_health);
17468 	bp->fw_health = NULL;
17469 	bnxt_cleanup_pci(bp);
17470 	bnxt_free_ctx_mem(bp, true);
17471 	bnxt_free_crash_dump_mem(bp);
17472 	kfree(bp->rss_indir_tbl);
17473 	bp->rss_indir_tbl = NULL;
17474 
17475 init_err_free:
17476 	bnxt_free_ring_cpu_masks(bp);
17477 	free_netdev(dev);
17478 	return rc;
17479 }
17480 
17481 static void bnxt_shutdown(struct pci_dev *pdev)
17482 {
17483 	struct net_device *dev = pci_get_drvdata(pdev);
17484 	struct bnxt *bp;
17485 
17486 	if (!dev)
17487 		return;
17488 
17489 	rtnl_lock();
17490 	netdev_lock(dev);
17491 	bp = netdev_priv(dev);
17492 	if (!bp)
17493 		goto shutdown_exit;
17494 
17495 	if (netif_running(dev))
17496 		netif_close(dev);
17497 
17498 	if (bnxt_hwrm_func_drv_unrgtr(bp)) {
17499 		pcie_flr(pdev);
17500 		goto shutdown_exit;
17501 	}
17502 	bnxt_ptp_clear(bp);
17503 	bnxt_clear_int_mode(bp);
17504 	pci_disable_device(pdev);
17505 
17506 	if (system_state == SYSTEM_POWER_OFF) {
17507 		pci_wake_from_d3(pdev, bp->wol);
17508 		pci_set_power_state(pdev, PCI_D3hot);
17509 	}
17510 
17511 shutdown_exit:
17512 	netdev_unlock(dev);
17513 	rtnl_unlock();
17514 }
17515 
17516 #ifdef CONFIG_PM_SLEEP
17517 static int bnxt_suspend(struct device *device)
17518 {
17519 	struct net_device *dev = dev_get_drvdata(device);
17520 	struct bnxt *bp = netdev_priv(dev);
17521 	int rc = 0;
17522 
17523 	bnxt_ulp_stop(bp);
17524 
17525 	netdev_lock(dev);
17526 	if (netif_running(dev)) {
17527 		netif_device_detach(dev);
17528 		rc = bnxt_close(dev);
17529 	}
17530 	bnxt_hwrm_func_drv_unrgtr(bp);
17531 	bnxt_ptp_clear(bp);
17532 	pci_disable_device(bp->pdev);
17533 	bnxt_free_ctx_mem(bp, false);
17534 	netdev_unlock(dev);
17535 	return rc;
17536 }
17537 
17538 static int bnxt_resume(struct device *device)
17539 {
17540 	struct net_device *dev = dev_get_drvdata(device);
17541 	struct bnxt *bp = netdev_priv(dev);
17542 	int rc = 0;
17543 
17544 	rtnl_lock();
17545 	netdev_lock(dev);
17546 	rc = pci_enable_device(bp->pdev);
17547 	if (rc) {
17548 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
17549 			   rc);
17550 		goto resume_exit;
17551 	}
17552 	pci_set_master(bp->pdev);
17553 	if (bnxt_hwrm_ver_get(bp)) {
17554 		rc = -ENODEV;
17555 		goto resume_exit;
17556 	}
17557 	rc = bnxt_hwrm_func_reset(bp);
17558 	if (rc) {
17559 		rc = -EBUSY;
17560 		goto resume_exit;
17561 	}
17562 
17563 	rc = bnxt_hwrm_func_qcaps(bp);
17564 	if (rc)
17565 		goto resume_exit;
17566 
17567 	bnxt_clear_reservations(bp, true);
17568 
17569 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
17570 		rc = -ENODEV;
17571 		goto resume_exit;
17572 	}
17573 	if (bp->fw_crash_mem)
17574 		bnxt_hwrm_crash_dump_mem_cfg(bp);
17575 
17576 	if (bnxt_ptp_init(bp)) {
17577 		kfree(bp->ptp_cfg);
17578 		bp->ptp_cfg = NULL;
17579 	}
17580 	bnxt_get_wol_settings(bp);
17581 	if (netif_running(dev)) {
17582 		rc = bnxt_open(dev);
17583 		if (!rc)
17584 			netif_device_attach(dev);
17585 	}
17586 
17587 resume_exit:
17588 	netdev_unlock(bp->dev);
17589 	rtnl_unlock();
17590 	if (!rc) {
17591 		bnxt_ulp_start(bp);
17592 		bnxt_reenable_sriov(bp);
17593 	}
17594 	return rc;
17595 }
17596 
17597 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
17598 #define BNXT_PM_OPS (&bnxt_pm_ops)
17599 
17600 #else
17601 
17602 #define BNXT_PM_OPS NULL
17603 
17604 #endif /* CONFIG_PM_SLEEP */
17605 
17606 /**
17607  * bnxt_io_error_detected - called when PCI error is detected
17608  * @pdev: Pointer to PCI device
17609  * @state: The current pci connection state
17610  *
17611  * This function is called after a PCI bus error affecting
17612  * this device has been detected.
17613  */
17614 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
17615 					       pci_channel_state_t state)
17616 {
17617 	struct net_device *netdev = pci_get_drvdata(pdev);
17618 	struct bnxt *bp = netdev_priv(netdev);
17619 	bool abort = false;
17620 
17621 	netdev_info(netdev, "PCI I/O error detected\n");
17622 
17623 	bnxt_ulp_stop(bp);
17624 
17625 	netdev_lock(netdev);
17626 	netif_device_detach(netdev);
17627 
17628 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
17629 		netdev_err(bp->dev, "Firmware reset already in progress\n");
17630 		abort = true;
17631 	}
17632 
17633 	if (abort || state == pci_channel_io_perm_failure) {
17634 		netdev_unlock(netdev);
17635 		return PCI_ERS_RESULT_DISCONNECT;
17636 	}
17637 
17638 	/* Link is not reliable anymore if state is pci_channel_io_frozen
17639 	 * so we disable bus master to prevent any potential bad DMAs before
17640 	 * freeing kernel memory.
17641 	 */
17642 	if (state == pci_channel_io_frozen)
17643 		bnxt_fw_fatal_close(bp);
17644 
17645 	if (netif_running(netdev))
17646 		__bnxt_close_nic(bp, true, true);
17647 
17648 	if (pci_is_enabled(pdev))
17649 		pci_disable_device(pdev);
17650 	bnxt_free_ctx_mem(bp, false);
17651 	netdev_unlock(netdev);
17652 
17653 	/* Request a slot reset. */
17654 	return PCI_ERS_RESULT_NEED_RESET;
17655 }
17656 
17657 /**
17658  * bnxt_io_slot_reset - called after the pci bus has been reset.
17659  * @pdev: Pointer to PCI device
17660  *
17661  * Restart the card from scratch, as if from a cold-boot.
17662  * At this point, the card has experienced a hard reset,
17663  * followed by fixups by BIOS, and has its config space
17664  * set up identically to what it was at cold boot.
17665  */
17666 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
17667 {
17668 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
17669 	struct net_device *netdev = pci_get_drvdata(pdev);
17670 	struct bnxt *bp = netdev_priv(netdev);
17671 	int retry = 0;
17672 	int err = 0;
17673 	u16 cmd;
17674 
17675 	netdev_info(bp->dev, "PCI Slot Reset\n");
17676 
17677 	/* After a PCIe hot reset, the chip should return CRS when the
17678 	 * vendor ID config register is read until it is ready.  On all
17679 	 * chips, this is not happening reliably so add a 5-second delay
17680 	 * as a workaround.
17681 	 */
17682 	msleep(5000);
17683 
17684 	netdev_lock(netdev);
17685 
17686 	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
17687 	if (PCI_POSSIBLE_ERROR(cmd)) {
17688 		dev_err(&pdev->dev,
17689 			"PCI config space inaccessible after reset\n");
17690 		goto reset_exit;
17691 	}
17692 
17693 	/* Upon PCIe error, our device internal logic that latches to
17694 	 * BAR value is getting reset and will restore only upon
17695 	 * rewriting the BARs.
17696 	 *
17697 	 * As pci_restore_state() does not re-write the BARs if the
17698 	 * value is same as saved value earlier, driver needs to
17699 	 * write the BARs to 0 to force restore.
17700 	 */
17701 	pci_clear_master(pdev);
17702 	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
17703 	cmd &= ~PCI_COMMAND_MEMORY;
17704 	pci_write_config_word(pdev, PCI_COMMAND, cmd);
17705 
17706 	bnxt_clear_bars(pdev);
17707 	pci_restore_state(pdev);
17708 
17709 	if (!pci_is_enabled(pdev) && pci_enable_device(pdev)) {
17710 		dev_err(&pdev->dev,
17711 			"Cannot re-enable PCI device after reset.\n");
17712 		pci_read_config_word(pdev, PCI_COMMAND, &cmd);
17713 		if (!PCI_POSSIBLE_ERROR(cmd)) {
17714 			cmd &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
17715 			pci_write_config_word(pdev, PCI_COMMAND, cmd);
17716 		}
17717 		goto reset_exit;
17718 	}
17719 	pci_set_master(pdev);
17720 
17721 	bnxt_inv_fw_health_reg(bp);
17722 	bnxt_try_map_fw_health_reg(bp);
17723 
17724 	/* In some PCIe AER scenarios, firmware may take up to
17725 	 * 10 seconds to become ready in the worst case.
17726 	 */
17727 	do {
17728 		err = bnxt_try_recover_fw(bp);
17729 		if (!err)
17730 			break;
17731 		retry++;
17732 	} while (retry < BNXT_FW_SLOT_RESET_RETRY);
17733 
17734 	if (err) {
17735 		dev_err(&pdev->dev, "Firmware not ready\n");
17736 		goto reset_exit;
17737 	}
17738 
17739 	err = bnxt_hwrm_func_reset(bp);
17740 	if (!err)
17741 		result = PCI_ERS_RESULT_RECOVERED;
17742 
17743 	/* IRQ will be initialized later in bnxt_io_resume */
17744 	bnxt_ulp_irq_stop(bp);
17745 	bnxt_clear_int_mode(bp);
17746 
17747 reset_exit:
17748 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
17749 	bnxt_clear_reservations(bp, true);
17750 	netdev_unlock(netdev);
17751 
17752 	return result;
17753 }
17754 
17755 /**
17756  * bnxt_io_resume - called when traffic can start flowing again.
17757  * @pdev: Pointer to PCI device
17758  *
17759  * This callback is called when the error recovery driver tells
17760  * us that its OK to resume normal operation.
17761  */
17762 static void bnxt_io_resume(struct pci_dev *pdev)
17763 {
17764 	struct net_device *netdev = pci_get_drvdata(pdev);
17765 	struct bnxt *bp = netdev_priv(netdev);
17766 	int err;
17767 
17768 	netdev_info(bp->dev, "PCI Slot Resume\n");
17769 	rtnl_lock();
17770 	netdev_lock(netdev);
17771 
17772 	err = bnxt_hwrm_func_qcaps(bp);
17773 	if (!err) {
17774 		if (netif_running(netdev)) {
17775 			err = bnxt_open(netdev);
17776 		} else {
17777 			err = bnxt_reserve_rings(bp, true);
17778 			if (!err)
17779 				err = bnxt_init_int_mode(bp);
17780 		}
17781 	}
17782 
17783 	if (!err)
17784 		netif_device_attach(netdev);
17785 
17786 	netdev_unlock(netdev);
17787 	rtnl_unlock();
17788 	if (!err) {
17789 		bnxt_ulp_start(bp);
17790 		bnxt_reenable_sriov(bp);
17791 	}
17792 }
17793 
17794 static const struct pci_error_handlers bnxt_err_handler = {
17795 	.error_detected	= bnxt_io_error_detected,
17796 	.slot_reset	= bnxt_io_slot_reset,
17797 	.resume		= bnxt_io_resume
17798 };
17799 
17800 static struct pci_driver bnxt_pci_driver = {
17801 	.name		= DRV_MODULE_NAME,
17802 	.id_table	= bnxt_pci_tbl,
17803 	.probe		= bnxt_init_one,
17804 	.remove		= bnxt_remove_one,
17805 	.shutdown	= bnxt_shutdown,
17806 	.driver.pm	= BNXT_PM_OPS,
17807 	.err_handler	= &bnxt_err_handler,
17808 #if defined(CONFIG_BNXT_SRIOV)
17809 	.sriov_configure = bnxt_sriov_configure,
17810 #endif
17811 };
17812 
17813 static int __init bnxt_init(void)
17814 {
17815 	int err;
17816 
17817 	bnxt_debug_init();
17818 	err = pci_register_driver(&bnxt_pci_driver);
17819 	if (err) {
17820 		bnxt_debug_exit();
17821 		return err;
17822 	}
17823 
17824 	return 0;
17825 }
17826 
17827 static void __exit bnxt_exit(void)
17828 {
17829 	pci_unregister_driver(&bnxt_pci_driver);
17830 	if (bnxt_pf_wq)
17831 		destroy_workqueue(bnxt_pf_wq);
17832 	bnxt_debug_exit();
17833 }
17834 
17835 module_init(bnxt_init);
17836 module_exit(bnxt_exit);
17837