xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
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 = kzalloc_objs(u16, max_rings);
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 = kzalloc_objs(u16, bp->tx_nr_rings);
5694 
5695 		if (!bp->tx_ring_map)
5696 			return -ENOMEM;
5697 
5698 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5699 			j = 0;
5700 		else
5701 			j = bp->rx_nr_rings;
5702 
5703 		for (i = 0; i < bp->tx_nr_rings; i++) {
5704 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5705 			struct bnxt_napi *bnapi2;
5706 
5707 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5708 				txr->tx_ring_struct.ring_mem.flags =
5709 					BNXT_RMEM_RING_PTE_FLAG;
5710 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5711 			if (i >= bp->tx_nr_rings_xdp) {
5712 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5713 
5714 				bnapi2 = bp->bnapi[k];
5715 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5716 				txr->tx_napi_idx =
5717 					BNXT_RING_TO_TC(bp, txr->txq_index);
5718 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5719 				bnapi2->tx_int = bnxt_tx_int;
5720 			} else {
5721 				bnapi2 = bp->bnapi[j];
5722 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5723 				bnapi2->tx_ring[0] = txr;
5724 				bnapi2->tx_int = bnxt_tx_int_xdp;
5725 				j++;
5726 			}
5727 			txr->bnapi = bnapi2;
5728 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5729 				txr->tx_cpr = &bnapi2->cp_ring;
5730 		}
5731 
5732 		rc = bnxt_alloc_stats(bp);
5733 		if (rc)
5734 			goto alloc_mem_err;
5735 		bnxt_init_stats(bp);
5736 
5737 		rc = bnxt_alloc_ntp_fltrs(bp);
5738 		if (rc)
5739 			goto alloc_mem_err;
5740 
5741 		rc = bnxt_alloc_vnics(bp);
5742 		if (rc)
5743 			goto alloc_mem_err;
5744 	}
5745 
5746 	rc = bnxt_alloc_all_cp_arrays(bp);
5747 	if (rc)
5748 		goto alloc_mem_err;
5749 
5750 	bnxt_init_ring_struct(bp);
5751 
5752 	rc = bnxt_alloc_rx_rings(bp);
5753 	if (rc)
5754 		goto alloc_mem_err;
5755 
5756 	rc = bnxt_alloc_tx_rings(bp);
5757 	if (rc)
5758 		goto alloc_mem_err;
5759 
5760 	rc = bnxt_alloc_cp_rings(bp);
5761 	if (rc)
5762 		goto alloc_mem_err;
5763 
5764 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5765 						  BNXT_VNIC_MCAST_FLAG |
5766 						  BNXT_VNIC_UCAST_FLAG;
5767 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5768 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5769 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5770 
5771 	rc = bnxt_alloc_vnic_attributes(bp);
5772 	if (rc)
5773 		goto alloc_mem_err;
5774 	return 0;
5775 
5776 alloc_mem_err:
5777 	bnxt_free_mem(bp, true);
5778 	return rc;
5779 }
5780 
5781 static void bnxt_disable_int(struct bnxt *bp)
5782 {
5783 	int i;
5784 
5785 	if (!bp->bnapi)
5786 		return;
5787 
5788 	for (i = 0; i < bp->cp_nr_rings; i++) {
5789 		struct bnxt_napi *bnapi = bp->bnapi[i];
5790 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5791 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5792 
5793 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5794 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5795 	}
5796 }
5797 
5798 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5799 {
5800 	struct bnxt_napi *bnapi = bp->bnapi[n];
5801 	struct bnxt_cp_ring_info *cpr;
5802 
5803 	cpr = &bnapi->cp_ring;
5804 	return cpr->cp_ring_struct.map_idx;
5805 }
5806 
5807 static void bnxt_disable_int_sync(struct bnxt *bp)
5808 {
5809 	int i;
5810 
5811 	if (!bp->irq_tbl || !bp->bnapi)
5812 		return;
5813 
5814 	atomic_inc(&bp->intr_sem);
5815 
5816 	bnxt_disable_int(bp);
5817 	for (i = 0; i < bp->cp_nr_rings; i++) {
5818 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5819 
5820 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5821 	}
5822 }
5823 
5824 static void bnxt_enable_int(struct bnxt *bp)
5825 {
5826 	int i;
5827 
5828 	atomic_set(&bp->intr_sem, 0);
5829 	for (i = 0; i < bp->cp_nr_rings; i++) {
5830 		struct bnxt_napi *bnapi = bp->bnapi[i];
5831 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5832 
5833 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5834 	}
5835 }
5836 
5837 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5838 			    bool async_only)
5839 {
5840 	DECLARE_BITMAP(async_events_bmap, 256);
5841 	u32 *events = (u32 *)async_events_bmap;
5842 	struct hwrm_func_drv_rgtr_output *resp;
5843 	struct hwrm_func_drv_rgtr_input *req;
5844 	u32 flags;
5845 	int rc, i;
5846 
5847 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5848 	if (rc)
5849 		return rc;
5850 
5851 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5852 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5853 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5854 
5855 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5856 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5857 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5858 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5859 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5860 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5861 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5862 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5863 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5864 	req->flags = cpu_to_le32(flags);
5865 	req->ver_maj_8b = DRV_VER_MAJ;
5866 	req->ver_min_8b = DRV_VER_MIN;
5867 	req->ver_upd_8b = DRV_VER_UPD;
5868 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5869 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5870 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5871 
5872 	if (BNXT_PF(bp)) {
5873 		u32 data[8];
5874 		int i;
5875 
5876 		memset(data, 0, sizeof(data));
5877 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5878 			u16 cmd = bnxt_vf_req_snif[i];
5879 			unsigned int bit, idx;
5880 
5881 			if ((bp->fw_cap & BNXT_FW_CAP_LINK_ADMIN) &&
5882 			    cmd == HWRM_PORT_PHY_QCFG)
5883 				continue;
5884 
5885 			idx = cmd / 32;
5886 			bit = cmd % 32;
5887 			data[idx] |= 1 << bit;
5888 		}
5889 
5890 		for (i = 0; i < 8; i++)
5891 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5892 
5893 		req->enables |=
5894 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5895 	}
5896 
5897 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5898 		req->flags |= cpu_to_le32(
5899 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5900 
5901 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5902 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5903 		u16 event_id = bnxt_async_events_arr[i];
5904 
5905 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5906 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5907 			continue;
5908 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5909 		    !bp->ptp_cfg)
5910 			continue;
5911 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5912 	}
5913 	if (bmap && bmap_size) {
5914 		for (i = 0; i < bmap_size; i++) {
5915 			if (test_bit(i, bmap))
5916 				__set_bit(i, async_events_bmap);
5917 		}
5918 	}
5919 	for (i = 0; i < 8; i++)
5920 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5921 
5922 	if (async_only)
5923 		req->enables =
5924 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5925 
5926 	resp = hwrm_req_hold(bp, req);
5927 	rc = hwrm_req_send(bp, req);
5928 	if (!rc) {
5929 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5930 		if (resp->flags &
5931 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5932 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5933 	}
5934 	hwrm_req_drop(bp, req);
5935 	return rc;
5936 }
5937 
5938 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5939 {
5940 	struct hwrm_func_drv_unrgtr_input *req;
5941 	int rc;
5942 
5943 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5944 		return 0;
5945 
5946 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5947 	if (rc)
5948 		return rc;
5949 	return hwrm_req_send(bp, req);
5950 }
5951 
5952 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5953 
5954 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5955 {
5956 	struct hwrm_tunnel_dst_port_free_input *req;
5957 	int rc;
5958 
5959 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5960 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5961 		return 0;
5962 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5963 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5964 		return 0;
5965 
5966 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5967 	if (rc)
5968 		return rc;
5969 
5970 	req->tunnel_type = tunnel_type;
5971 
5972 	switch (tunnel_type) {
5973 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5974 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5975 		bp->vxlan_port = 0;
5976 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5977 		break;
5978 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5979 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5980 		bp->nge_port = 0;
5981 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5982 		break;
5983 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5984 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5985 		bp->vxlan_gpe_port = 0;
5986 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5987 		break;
5988 	default:
5989 		break;
5990 	}
5991 
5992 	rc = hwrm_req_send(bp, req);
5993 	if (rc)
5994 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5995 			   rc);
5996 	if (bp->flags & BNXT_FLAG_TPA)
5997 		bnxt_set_tpa(bp, true);
5998 	return rc;
5999 }
6000 
6001 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
6002 					   u8 tunnel_type)
6003 {
6004 	struct hwrm_tunnel_dst_port_alloc_output *resp;
6005 	struct hwrm_tunnel_dst_port_alloc_input *req;
6006 	int rc;
6007 
6008 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
6009 	if (rc)
6010 		return rc;
6011 
6012 	req->tunnel_type = tunnel_type;
6013 	req->tunnel_dst_port_val = port;
6014 
6015 	resp = hwrm_req_hold(bp, req);
6016 	rc = hwrm_req_send(bp, req);
6017 	if (rc) {
6018 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
6019 			   rc);
6020 		goto err_out;
6021 	}
6022 
6023 	switch (tunnel_type) {
6024 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
6025 		bp->vxlan_port = port;
6026 		bp->vxlan_fw_dst_port_id =
6027 			le16_to_cpu(resp->tunnel_dst_port_id);
6028 		break;
6029 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
6030 		bp->nge_port = port;
6031 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
6032 		break;
6033 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
6034 		bp->vxlan_gpe_port = port;
6035 		bp->vxlan_gpe_fw_dst_port_id =
6036 			le16_to_cpu(resp->tunnel_dst_port_id);
6037 		break;
6038 	default:
6039 		break;
6040 	}
6041 	if (bp->flags & BNXT_FLAG_TPA)
6042 		bnxt_set_tpa(bp, true);
6043 
6044 err_out:
6045 	hwrm_req_drop(bp, req);
6046 	return rc;
6047 }
6048 
6049 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
6050 {
6051 	struct hwrm_cfa_l2_set_rx_mask_input *req;
6052 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
6053 	int rc;
6054 
6055 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
6056 	if (rc)
6057 		return rc;
6058 
6059 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6060 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
6061 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
6062 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
6063 	}
6064 	req->mask = cpu_to_le32(vnic->rx_mask);
6065 	return hwrm_req_send_silent(bp, req);
6066 }
6067 
6068 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6069 {
6070 	if (!atomic_dec_and_test(&fltr->refcnt))
6071 		return;
6072 	spin_lock_bh(&bp->ntp_fltr_lock);
6073 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
6074 		spin_unlock_bh(&bp->ntp_fltr_lock);
6075 		return;
6076 	}
6077 	hlist_del_rcu(&fltr->base.hash);
6078 	bnxt_del_one_usr_fltr(bp, &fltr->base);
6079 	if (fltr->base.flags) {
6080 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
6081 		bp->ntp_fltr_count--;
6082 	}
6083 	spin_unlock_bh(&bp->ntp_fltr_lock);
6084 	kfree_rcu(fltr, base.rcu);
6085 }
6086 
6087 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
6088 						      struct bnxt_l2_key *key,
6089 						      u32 idx)
6090 {
6091 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
6092 	struct bnxt_l2_filter *fltr;
6093 
6094 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
6095 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
6096 
6097 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
6098 		    l2_key->vlan == key->vlan)
6099 			return fltr;
6100 	}
6101 	return NULL;
6102 }
6103 
6104 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
6105 						    struct bnxt_l2_key *key,
6106 						    u32 idx)
6107 {
6108 	struct bnxt_l2_filter *fltr = NULL;
6109 
6110 	rcu_read_lock();
6111 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6112 	if (fltr)
6113 		atomic_inc(&fltr->refcnt);
6114 	rcu_read_unlock();
6115 	return fltr;
6116 }
6117 
6118 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
6119 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
6120 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
6121 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
6122 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
6123 
6124 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
6125 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
6126 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
6127 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
6128 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
6129 
6130 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
6131 {
6132 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6133 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
6134 			return sizeof(fkeys->addrs.v4addrs) +
6135 			       sizeof(fkeys->ports);
6136 
6137 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
6138 			return sizeof(fkeys->addrs.v4addrs);
6139 	}
6140 
6141 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
6142 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
6143 			return sizeof(fkeys->addrs.v6addrs) +
6144 			       sizeof(fkeys->ports);
6145 
6146 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
6147 			return sizeof(fkeys->addrs.v6addrs);
6148 	}
6149 
6150 	return 0;
6151 }
6152 
6153 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
6154 			 const unsigned char *key)
6155 {
6156 	u64 prefix = bp->toeplitz_prefix, hash = 0;
6157 	struct bnxt_ipv4_tuple tuple4;
6158 	struct bnxt_ipv6_tuple tuple6;
6159 	int i, j, len = 0;
6160 	u8 *four_tuple;
6161 
6162 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
6163 	if (!len)
6164 		return 0;
6165 
6166 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6167 		tuple4.v4addrs = fkeys->addrs.v4addrs;
6168 		tuple4.ports = fkeys->ports;
6169 		four_tuple = (unsigned char *)&tuple4;
6170 	} else {
6171 		tuple6.v6addrs = fkeys->addrs.v6addrs;
6172 		tuple6.ports = fkeys->ports;
6173 		four_tuple = (unsigned char *)&tuple6;
6174 	}
6175 
6176 	for (i = 0, j = 8; i < len; i++, j++) {
6177 		u8 byte = four_tuple[i];
6178 		int bit;
6179 
6180 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
6181 			if (byte & 0x80)
6182 				hash ^= prefix;
6183 		}
6184 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
6185 	}
6186 
6187 	/* The valid part of the hash is in the upper 32 bits. */
6188 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
6189 }
6190 
6191 #ifdef CONFIG_RFS_ACCEL
6192 static struct bnxt_l2_filter *
6193 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
6194 {
6195 	struct bnxt_l2_filter *fltr;
6196 	u32 idx;
6197 
6198 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6199 	      BNXT_L2_FLTR_HASH_MASK;
6200 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6201 	return fltr;
6202 }
6203 #endif
6204 
6205 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
6206 			       struct bnxt_l2_key *key, u32 idx)
6207 {
6208 	struct hlist_head *head;
6209 
6210 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
6211 	fltr->l2_key.vlan = key->vlan;
6212 	fltr->base.type = BNXT_FLTR_TYPE_L2;
6213 	if (fltr->base.flags) {
6214 		int bit_id;
6215 
6216 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6217 						 bp->max_fltr, 0);
6218 		if (bit_id < 0)
6219 			return -ENOMEM;
6220 		fltr->base.sw_id = (u16)bit_id;
6221 		bp->ntp_fltr_count++;
6222 	}
6223 	head = &bp->l2_fltr_hash_tbl[idx];
6224 	hlist_add_head_rcu(&fltr->base.hash, head);
6225 	bnxt_insert_usr_fltr(bp, &fltr->base);
6226 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
6227 	atomic_set(&fltr->refcnt, 1);
6228 	return 0;
6229 }
6230 
6231 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
6232 						   struct bnxt_l2_key *key,
6233 						   gfp_t gfp)
6234 {
6235 	struct bnxt_l2_filter *fltr;
6236 	u32 idx;
6237 	int rc;
6238 
6239 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6240 	      BNXT_L2_FLTR_HASH_MASK;
6241 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6242 	if (fltr)
6243 		return fltr;
6244 
6245 	fltr = kzalloc_obj(*fltr, gfp);
6246 	if (!fltr)
6247 		return ERR_PTR(-ENOMEM);
6248 	spin_lock_bh(&bp->ntp_fltr_lock);
6249 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6250 	spin_unlock_bh(&bp->ntp_fltr_lock);
6251 	if (rc) {
6252 		bnxt_del_l2_filter(bp, fltr);
6253 		fltr = ERR_PTR(rc);
6254 	}
6255 	return fltr;
6256 }
6257 
6258 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
6259 						struct bnxt_l2_key *key,
6260 						u16 flags)
6261 {
6262 	struct bnxt_l2_filter *fltr;
6263 	u32 idx;
6264 	int rc;
6265 
6266 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6267 	      BNXT_L2_FLTR_HASH_MASK;
6268 	spin_lock_bh(&bp->ntp_fltr_lock);
6269 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6270 	if (fltr) {
6271 		fltr = ERR_PTR(-EEXIST);
6272 		goto l2_filter_exit;
6273 	}
6274 	fltr = kzalloc_obj(*fltr, GFP_ATOMIC);
6275 	if (!fltr) {
6276 		fltr = ERR_PTR(-ENOMEM);
6277 		goto l2_filter_exit;
6278 	}
6279 	fltr->base.flags = flags;
6280 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6281 	if (rc) {
6282 		spin_unlock_bh(&bp->ntp_fltr_lock);
6283 		bnxt_del_l2_filter(bp, fltr);
6284 		return ERR_PTR(rc);
6285 	}
6286 
6287 l2_filter_exit:
6288 	spin_unlock_bh(&bp->ntp_fltr_lock);
6289 	return fltr;
6290 }
6291 
6292 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6293 {
6294 #ifdef CONFIG_BNXT_SRIOV
6295 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6296 
6297 	return vf->fw_fid;
6298 #else
6299 	return INVALID_HW_RING_ID;
6300 #endif
6301 }
6302 
6303 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6304 {
6305 	struct hwrm_cfa_l2_filter_free_input *req;
6306 	u16 target_id = 0xffff;
6307 	int rc;
6308 
6309 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6310 		struct bnxt_pf_info *pf = &bp->pf;
6311 
6312 		if (fltr->base.vf_idx >= pf->active_vfs)
6313 			return -EINVAL;
6314 
6315 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6316 		if (target_id == INVALID_HW_RING_ID)
6317 			return -EINVAL;
6318 	}
6319 
6320 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6321 	if (rc)
6322 		return rc;
6323 
6324 	req->target_id = cpu_to_le16(target_id);
6325 	req->l2_filter_id = fltr->base.filter_id;
6326 	return hwrm_req_send(bp, req);
6327 }
6328 
6329 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6330 {
6331 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6332 	struct hwrm_cfa_l2_filter_alloc_input *req;
6333 	u16 target_id = 0xffff;
6334 	int rc;
6335 
6336 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6337 		struct bnxt_pf_info *pf = &bp->pf;
6338 
6339 		if (fltr->base.vf_idx >= pf->active_vfs)
6340 			return -EINVAL;
6341 
6342 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6343 	}
6344 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6345 	if (rc)
6346 		return rc;
6347 
6348 	req->target_id = cpu_to_le16(target_id);
6349 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6350 
6351 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6352 		req->flags |=
6353 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6354 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6355 	req->enables =
6356 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6357 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6358 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6359 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6360 	eth_broadcast_addr(req->l2_addr_mask);
6361 
6362 	if (fltr->l2_key.vlan) {
6363 		req->enables |=
6364 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6365 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6366 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6367 		req->num_vlans = 1;
6368 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6369 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6370 	}
6371 
6372 	resp = hwrm_req_hold(bp, req);
6373 	rc = hwrm_req_send(bp, req);
6374 	if (!rc) {
6375 		fltr->base.filter_id = resp->l2_filter_id;
6376 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6377 	}
6378 	hwrm_req_drop(bp, req);
6379 	return rc;
6380 }
6381 
6382 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6383 				     struct bnxt_ntuple_filter *fltr)
6384 {
6385 	struct hwrm_cfa_ntuple_filter_free_input *req;
6386 	int rc;
6387 
6388 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6389 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
6390 		return 0;
6391 
6392 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6393 	if (rc)
6394 		return rc;
6395 
6396 	req->ntuple_filter_id = fltr->base.filter_id;
6397 	return hwrm_req_send(bp, req);
6398 }
6399 
6400 #define BNXT_NTP_FLTR_FLAGS					\
6401 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6402 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6403 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6404 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6405 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6406 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6407 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6408 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6409 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6410 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6411 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6412 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6413 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6414 
6415 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6416 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6417 
6418 void bnxt_fill_ipv6_mask(__be32 mask[4])
6419 {
6420 	int i;
6421 
6422 	for (i = 0; i < 4; i++)
6423 		mask[i] = cpu_to_be32(~0);
6424 }
6425 
6426 static void
6427 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6428 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6429 			  struct bnxt_ntuple_filter *fltr)
6430 {
6431 	u16 rxq = fltr->base.rxq;
6432 
6433 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6434 		struct ethtool_rxfh_context *ctx;
6435 		struct bnxt_rss_ctx *rss_ctx;
6436 		struct bnxt_vnic_info *vnic;
6437 
6438 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6439 			      fltr->base.fw_vnic_id);
6440 		if (ctx) {
6441 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6442 			vnic = &rss_ctx->vnic;
6443 
6444 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6445 		}
6446 		return;
6447 	}
6448 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6449 		struct bnxt_vnic_info *vnic;
6450 		u32 enables;
6451 
6452 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6453 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6454 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6455 		req->enables |= cpu_to_le32(enables);
6456 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6457 	} else {
6458 		u32 flags;
6459 
6460 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6461 		req->flags |= cpu_to_le32(flags);
6462 		req->dst_id = cpu_to_le16(rxq);
6463 	}
6464 }
6465 
6466 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6467 				      struct bnxt_ntuple_filter *fltr)
6468 {
6469 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6470 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6471 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6472 	struct flow_keys *keys = &fltr->fkeys;
6473 	struct bnxt_l2_filter *l2_fltr;
6474 	struct bnxt_vnic_info *vnic;
6475 	int rc;
6476 
6477 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6478 	if (rc)
6479 		return rc;
6480 
6481 	l2_fltr = fltr->l2_fltr;
6482 	req->l2_filter_id = l2_fltr->base.filter_id;
6483 
6484 	if (fltr->base.flags & BNXT_ACT_DROP) {
6485 		req->flags =
6486 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6487 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6488 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6489 	} else {
6490 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6491 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6492 	}
6493 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6494 
6495 	req->ethertype = htons(ETH_P_IP);
6496 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6497 	req->ip_protocol = keys->basic.ip_proto;
6498 
6499 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6500 		req->ethertype = htons(ETH_P_IPV6);
6501 		req->ip_addr_type =
6502 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6503 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6504 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6505 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6506 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6507 	} else {
6508 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6509 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6510 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6511 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6512 	}
6513 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6514 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6515 		req->tunnel_type =
6516 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6517 	}
6518 
6519 	req->src_port = keys->ports.src;
6520 	req->src_port_mask = masks->ports.src;
6521 	req->dst_port = keys->ports.dst;
6522 	req->dst_port_mask = masks->ports.dst;
6523 
6524 	resp = hwrm_req_hold(bp, req);
6525 	rc = hwrm_req_send(bp, req);
6526 	if (!rc)
6527 		fltr->base.filter_id = resp->ntuple_filter_id;
6528 	hwrm_req_drop(bp, req);
6529 	return rc;
6530 }
6531 
6532 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6533 				     const u8 *mac_addr)
6534 {
6535 	struct bnxt_l2_filter *fltr;
6536 	struct bnxt_l2_key key;
6537 	int rc;
6538 
6539 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6540 	key.vlan = 0;
6541 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6542 	if (IS_ERR(fltr))
6543 		return PTR_ERR(fltr);
6544 
6545 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6546 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6547 	if (rc)
6548 		bnxt_del_l2_filter(bp, fltr);
6549 	else
6550 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6551 	return rc;
6552 }
6553 
6554 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6555 {
6556 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6557 
6558 	/* Any associated ntuple filters will also be cleared by firmware. */
6559 	for (i = 0; i < num_of_vnics; i++) {
6560 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6561 
6562 		for (j = 0; j < vnic->uc_filter_count; j++) {
6563 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6564 
6565 			bnxt_hwrm_l2_filter_free(bp, fltr);
6566 			bnxt_del_l2_filter(bp, fltr);
6567 		}
6568 		vnic->uc_filter_count = 0;
6569 	}
6570 }
6571 
6572 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6573 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6574 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6575 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6576 
6577 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6578 					   struct hwrm_vnic_tpa_cfg_input *req)
6579 {
6580 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6581 
6582 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6583 		return;
6584 
6585 	if (bp->vxlan_port)
6586 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6587 	if (bp->vxlan_gpe_port)
6588 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6589 	if (bp->nge_port)
6590 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6591 
6592 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6593 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6594 }
6595 
6596 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6597 			   u32 tpa_flags)
6598 {
6599 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6600 	struct hwrm_vnic_tpa_cfg_input *req;
6601 	int rc;
6602 
6603 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6604 		return 0;
6605 
6606 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6607 	if (rc)
6608 		return rc;
6609 
6610 	if (tpa_flags) {
6611 		u16 mss = bp->dev->mtu - 40;
6612 		u32 nsegs, n, segs = 0, flags;
6613 
6614 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6615 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6616 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6617 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6618 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6619 		if (tpa_flags & BNXT_FLAG_GRO)
6620 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6621 
6622 		req->flags = cpu_to_le32(flags);
6623 
6624 		req->enables =
6625 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6626 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6627 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6628 
6629 		/* Number of segs are log2 units, and first packet is not
6630 		 * included as part of this units.
6631 		 */
6632 		if (mss <= BNXT_RX_PAGE_SIZE) {
6633 			n = BNXT_RX_PAGE_SIZE / mss;
6634 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6635 		} else {
6636 			n = mss / BNXT_RX_PAGE_SIZE;
6637 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6638 				n++;
6639 			nsegs = (MAX_SKB_FRAGS - n) / n;
6640 		}
6641 
6642 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6643 			segs = MAX_TPA_SEGS_P5;
6644 			max_aggs = bp->max_tpa;
6645 		} else {
6646 			segs = ilog2(nsegs);
6647 		}
6648 		req->max_agg_segs = cpu_to_le16(segs);
6649 		req->max_aggs = cpu_to_le16(max_aggs);
6650 
6651 		req->min_agg_len = cpu_to_le32(512);
6652 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6653 	}
6654 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6655 
6656 	return hwrm_req_send(bp, req);
6657 }
6658 
6659 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6660 {
6661 	struct bnxt_ring_grp_info *grp_info;
6662 
6663 	grp_info = &bp->grp_info[ring->grp_idx];
6664 	return grp_info->cp_fw_ring_id;
6665 }
6666 
6667 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6668 {
6669 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6670 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6671 	else
6672 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6673 }
6674 
6675 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6676 {
6677 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6678 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6679 	else
6680 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6681 }
6682 
6683 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6684 {
6685 	int entries;
6686 
6687 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6688 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6689 	else
6690 		entries = HW_HASH_INDEX_SIZE;
6691 
6692 	bp->rss_indir_tbl_entries = entries;
6693 	bp->rss_indir_tbl =
6694 		kmalloc_objs(*bp->rss_indir_tbl, entries);
6695 	if (!bp->rss_indir_tbl)
6696 		return -ENOMEM;
6697 
6698 	return 0;
6699 }
6700 
6701 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6702 				 struct ethtool_rxfh_context *rss_ctx)
6703 {
6704 	u16 max_rings, max_entries, pad, i;
6705 	u32 *rss_indir_tbl;
6706 
6707 	if (!bp->rx_nr_rings)
6708 		return;
6709 
6710 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6711 		max_rings = bp->rx_nr_rings - 1;
6712 	else
6713 		max_rings = bp->rx_nr_rings;
6714 
6715 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6716 	if (rss_ctx)
6717 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6718 	else
6719 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6720 
6721 	for (i = 0; i < max_entries; i++)
6722 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6723 
6724 	pad = bp->rss_indir_tbl_entries - max_entries;
6725 	if (pad)
6726 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6727 }
6728 
6729 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6730 {
6731 	u32 i, tbl_size, max_ring = 0;
6732 
6733 	if (!bp->rss_indir_tbl)
6734 		return 0;
6735 
6736 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6737 	for (i = 0; i < tbl_size; i++)
6738 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6739 	return max_ring;
6740 }
6741 
6742 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6743 {
6744 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6745 		if (!rx_rings)
6746 			return 0;
6747 		if (bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX)
6748 			return BNXT_RSS_TABLE_MAX_TBL_P5;
6749 
6750 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6751 					       BNXT_RSS_TABLE_ENTRIES_P5);
6752 	}
6753 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6754 		return 2;
6755 	return 1;
6756 }
6757 
6758 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6759 {
6760 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6761 	u16 i, j, min_j = bp->rx_nr_rings - 1;
6762 
6763 	if (!vnic->rss_table)
6764 		goto skip_rss_tbl;
6765 
6766 	/* Fill the RSS indirection table with ring group ids */
6767 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6768 		if (!no_rss)
6769 			j = bp->rss_indir_tbl[i];
6770 		min_j = min(j, min_j);
6771 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6772 	}
6773 
6774 skip_rss_tbl:
6775 	if (vnic->rss_table && !no_rss)
6776 		vnic->default_rx_ring = min_j;
6777 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6778 		vnic->default_rx_ring = vnic->vnic_id - 1;
6779 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6780 		vnic->default_rx_ring = bp->rx_nr_rings - 1;
6781 	else
6782 		vnic->default_rx_ring = 0;
6783 }
6784 
6785 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6786 				    struct bnxt_vnic_info *vnic)
6787 {
6788 	u16 tbl_size, i, min_j = bp->rx_nr_rings - 1;
6789 	__le16 *ring_tbl = vnic->rss_table;
6790 	struct bnxt_rx_ring_info *rxr;
6791 
6792 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6793 
6794 	for (i = 0; i < tbl_size; i++) {
6795 		u16 ring_id, j;
6796 
6797 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6798 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6799 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6800 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6801 		else
6802 			j = bp->rss_indir_tbl[i];
6803 		min_j = min(j, min_j);
6804 		rxr = &bp->rx_ring[j];
6805 
6806 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6807 		*ring_tbl++ = cpu_to_le16(ring_id);
6808 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6809 		*ring_tbl++ = cpu_to_le16(ring_id);
6810 	}
6811 	vnic->default_rx_ring = min_j;
6812 }
6813 
6814 static void
6815 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6816 			 struct bnxt_vnic_info *vnic)
6817 {
6818 	if (bp->flags & BNXT_FLAG_CHIP_P7)
6819 		req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6820 
6821 	if (bp->rss_hash_delta) {
6822 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6823 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6824 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6825 		else
6826 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6827 	} else {
6828 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6829 	}
6830 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6831 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6832 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6833 }
6834 
6835 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6836 				  bool set_rss)
6837 {
6838 	struct hwrm_vnic_rss_cfg_input *req;
6839 	int rc;
6840 
6841 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6842 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6843 		return 0;
6844 
6845 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6846 	if (rc)
6847 		return rc;
6848 
6849 	if (set_rss)
6850 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6851 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6852 	return hwrm_req_send(bp, req);
6853 }
6854 
6855 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6856 				     struct bnxt_vnic_info *vnic, bool set_rss)
6857 {
6858 	struct hwrm_vnic_rss_cfg_input *req;
6859 	dma_addr_t ring_tbl_map;
6860 	u32 i, nr_ctxs;
6861 	int rc;
6862 
6863 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6864 	if (rc)
6865 		return rc;
6866 
6867 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6868 	if (!set_rss)
6869 		return hwrm_req_send(bp, req);
6870 
6871 	bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6872 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6873 	ring_tbl_map = vnic->rss_table_dma_addr;
6874 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6875 
6876 	hwrm_req_hold(bp, req);
6877 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6878 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6879 		req->ring_table_pair_index = i;
6880 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6881 		rc = hwrm_req_send(bp, req);
6882 		if (rc)
6883 			goto exit;
6884 	}
6885 
6886 exit:
6887 	hwrm_req_drop(bp, req);
6888 	return rc;
6889 }
6890 
6891 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6892 {
6893 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6894 	struct hwrm_vnic_rss_qcfg_output *resp;
6895 	struct hwrm_vnic_rss_qcfg_input *req;
6896 
6897 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6898 		return;
6899 
6900 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6901 	/* all contexts configured to same hash_type, zero always exists */
6902 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6903 	resp = hwrm_req_hold(bp, req);
6904 	if (!hwrm_req_send(bp, req)) {
6905 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6906 		bp->rss_hash_delta = 0;
6907 	}
6908 	hwrm_req_drop(bp, req);
6909 }
6910 
6911 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6912 {
6913 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6914 	struct hwrm_vnic_plcmodes_cfg_input *req;
6915 	int rc;
6916 
6917 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6918 	if (rc)
6919 		return rc;
6920 
6921 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6922 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6923 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6924 
6925 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6926 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6927 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6928 		req->enables |=
6929 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6930 		req->hds_threshold = cpu_to_le16(hds_thresh);
6931 	}
6932 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6933 	return hwrm_req_send(bp, req);
6934 }
6935 
6936 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6937 					struct bnxt_vnic_info *vnic,
6938 					u16 ctx_idx)
6939 {
6940 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6941 
6942 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6943 		return;
6944 
6945 	req->rss_cos_lb_ctx_id =
6946 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6947 
6948 	hwrm_req_send(bp, req);
6949 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6950 }
6951 
6952 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6953 {
6954 	int i, j;
6955 
6956 	for (i = 0; i < bp->nr_vnics; i++) {
6957 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6958 
6959 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6960 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6961 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6962 		}
6963 	}
6964 	bp->rsscos_nr_ctxs = 0;
6965 }
6966 
6967 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6968 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6969 {
6970 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6971 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6972 	int rc;
6973 
6974 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6975 	if (rc)
6976 		return rc;
6977 
6978 	resp = hwrm_req_hold(bp, req);
6979 	rc = hwrm_req_send(bp, req);
6980 	if (!rc)
6981 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6982 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6983 	hwrm_req_drop(bp, req);
6984 
6985 	return rc;
6986 }
6987 
6988 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6989 {
6990 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6991 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6992 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6993 }
6994 
6995 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6996 {
6997 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6998 	struct hwrm_vnic_cfg_input *req;
6999 	unsigned int ring = 0, grp_idx;
7000 	u16 def_vlan = 0;
7001 	int rc;
7002 
7003 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
7004 	if (rc)
7005 		return rc;
7006 
7007 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7008 		struct bnxt_rx_ring_info *rxr;
7009 
7010 		rxr = &bp->rx_ring[vnic->default_rx_ring];
7011 		req->default_rx_ring_id =
7012 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
7013 		req->default_cmpl_ring_id =
7014 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
7015 		req->enables =
7016 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
7017 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
7018 		goto vnic_mru;
7019 	}
7020 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
7021 	/* Only RSS support for now TBD: COS & LB */
7022 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
7023 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
7024 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
7025 					   VNIC_CFG_REQ_ENABLES_MRU);
7026 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
7027 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
7028 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
7029 					   VNIC_CFG_REQ_ENABLES_MRU);
7030 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
7031 	} else {
7032 		req->rss_rule = cpu_to_le16(0xffff);
7033 	}
7034 
7035 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
7036 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
7037 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
7038 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
7039 	} else {
7040 		req->cos_rule = cpu_to_le16(0xffff);
7041 	}
7042 
7043 	ring = vnic->default_rx_ring;
7044 	grp_idx = bp->rx_ring[ring].bnapi->index;
7045 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
7046 	req->lb_rule = cpu_to_le16(0xffff);
7047 vnic_mru:
7048 	vnic->mru = bp->dev->mtu + VLAN_ETH_HLEN;
7049 	req->mru = cpu_to_le16(vnic->mru);
7050 
7051 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
7052 #ifdef CONFIG_BNXT_SRIOV
7053 	if (BNXT_VF(bp))
7054 		def_vlan = bp->vf.vlan;
7055 #endif
7056 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
7057 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
7058 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT &&
7059 	    bnxt_ulp_registered(bp->edev[BNXT_AUXDEV_RDMA]))
7060 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
7061 
7062 	return hwrm_req_send(bp, req);
7063 }
7064 
7065 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
7066 				    struct bnxt_vnic_info *vnic)
7067 {
7068 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
7069 		struct hwrm_vnic_free_input *req;
7070 
7071 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
7072 			return;
7073 
7074 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
7075 
7076 		hwrm_req_send(bp, req);
7077 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
7078 	}
7079 }
7080 
7081 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
7082 {
7083 	u16 i;
7084 
7085 	for (i = 0; i < bp->nr_vnics; i++)
7086 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
7087 }
7088 
7089 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
7090 			 unsigned int start_rx_ring_idx,
7091 			 unsigned int nr_rings)
7092 {
7093 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
7094 	struct hwrm_vnic_alloc_output *resp;
7095 	struct hwrm_vnic_alloc_input *req;
7096 	int rc;
7097 
7098 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
7099 	if (rc)
7100 		return rc;
7101 
7102 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7103 		goto vnic_no_ring_grps;
7104 
7105 	/* map ring groups to this vnic */
7106 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
7107 		grp_idx = bp->rx_ring[i].bnapi->index;
7108 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
7109 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
7110 				   j, nr_rings);
7111 			break;
7112 		}
7113 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
7114 	}
7115 
7116 vnic_no_ring_grps:
7117 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
7118 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
7119 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
7120 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
7121 
7122 	resp = hwrm_req_hold(bp, req);
7123 	rc = hwrm_req_send(bp, req);
7124 	if (!rc)
7125 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
7126 	hwrm_req_drop(bp, req);
7127 	return rc;
7128 }
7129 
7130 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
7131 {
7132 	struct hwrm_vnic_qcaps_output *resp;
7133 	struct hwrm_vnic_qcaps_input *req;
7134 	int rc;
7135 
7136 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
7137 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
7138 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
7139 	if (bp->hwrm_spec_code < 0x10600)
7140 		return 0;
7141 
7142 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
7143 	if (rc)
7144 		return rc;
7145 
7146 	resp = hwrm_req_hold(bp, req);
7147 	rc = hwrm_req_send(bp, req);
7148 	if (!rc) {
7149 		u32 flags = le32_to_cpu(resp->flags);
7150 
7151 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
7152 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
7153 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
7154 		if (flags &
7155 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
7156 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
7157 
7158 		/* Older P5 fw before EXT_HW_STATS support did not set
7159 		 * VLAN_STRIP_CAP properly.
7160 		 */
7161 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
7162 		    (BNXT_CHIP_P5(bp) &&
7163 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
7164 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
7165 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
7166 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
7167 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
7168 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
7169 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
7170 		if (bp->max_tpa_v2) {
7171 			if (BNXT_CHIP_P5(bp))
7172 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
7173 			else
7174 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
7175 		}
7176 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
7177 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
7178 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
7179 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
7180 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
7181 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
7182 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
7183 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
7184 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
7185 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
7186 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPV6_FLOW_LABEL_CAP)
7187 			bp->rss_cap |= BNXT_RSS_CAP_IPV6_FLOW_LABEL_RSS_CAP;
7188 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
7189 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
7190 	}
7191 	hwrm_req_drop(bp, req);
7192 	return rc;
7193 }
7194 
7195 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
7196 {
7197 	struct hwrm_ring_grp_alloc_output *resp;
7198 	struct hwrm_ring_grp_alloc_input *req;
7199 	int rc;
7200 	u16 i;
7201 
7202 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7203 		return 0;
7204 
7205 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
7206 	if (rc)
7207 		return rc;
7208 
7209 	resp = hwrm_req_hold(bp, req);
7210 	for (i = 0; i < bp->rx_nr_rings; i++) {
7211 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
7212 
7213 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
7214 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
7215 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
7216 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
7217 
7218 		rc = hwrm_req_send(bp, req);
7219 
7220 		if (rc)
7221 			break;
7222 
7223 		bp->grp_info[grp_idx].fw_grp_id =
7224 			le32_to_cpu(resp->ring_group_id);
7225 	}
7226 	hwrm_req_drop(bp, req);
7227 	return rc;
7228 }
7229 
7230 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
7231 {
7232 	struct hwrm_ring_grp_free_input *req;
7233 	u16 i;
7234 
7235 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7236 		return;
7237 
7238 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
7239 		return;
7240 
7241 	hwrm_req_hold(bp, req);
7242 	for (i = 0; i < bp->cp_nr_rings; i++) {
7243 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
7244 			continue;
7245 		req->ring_group_id =
7246 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
7247 
7248 		hwrm_req_send(bp, req);
7249 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
7250 	}
7251 	hwrm_req_drop(bp, req);
7252 }
7253 
7254 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
7255 				       struct hwrm_ring_alloc_input *req,
7256 				       struct bnxt_rx_ring_info *rxr,
7257 				       struct bnxt_ring_struct *ring)
7258 {
7259 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
7260 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
7261 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
7262 
7263 	if (ring_type == HWRM_RING_ALLOC_AGG) {
7264 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7265 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7266 		req->rx_buf_size = cpu_to_le16(rxr->rx_page_size);
7267 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
7268 	} else {
7269 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
7270 		if (NET_IP_ALIGN == 2)
7271 			req->flags =
7272 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
7273 	}
7274 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7275 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7276 	req->enables |= cpu_to_le32(enables);
7277 }
7278 
7279 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7280 				    struct bnxt_rx_ring_info *rxr,
7281 				    struct bnxt_ring_struct *ring,
7282 				    u32 ring_type, u32 map_index)
7283 {
7284 	struct hwrm_ring_alloc_output *resp;
7285 	struct hwrm_ring_alloc_input *req;
7286 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7287 	struct bnxt_ring_grp_info *grp_info;
7288 	int rc, err = 0;
7289 	u16 ring_id;
7290 
7291 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7292 	if (rc)
7293 		goto exit;
7294 
7295 	req->enables = 0;
7296 	if (rmem->nr_pages > 1) {
7297 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7298 		/* Page size is in log2 units */
7299 		req->page_size = BNXT_PAGE_SHIFT;
7300 		req->page_tbl_depth = 1;
7301 	} else {
7302 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7303 	}
7304 	req->fbo = 0;
7305 	/* Association of ring index with doorbell index and MSIX number */
7306 	req->logical_id = cpu_to_le16(map_index);
7307 
7308 	switch (ring_type) {
7309 	case HWRM_RING_ALLOC_TX: {
7310 		struct bnxt_tx_ring_info *txr;
7311 		u16 flags = 0;
7312 
7313 		txr = container_of(ring, struct bnxt_tx_ring_info,
7314 				   tx_ring_struct);
7315 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7316 		/* Association of transmit ring with completion ring */
7317 		grp_info = &bp->grp_info[ring->grp_idx];
7318 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7319 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7320 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7321 		req->queue_id = cpu_to_le16(ring->queue_id);
7322 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7323 			req->cmpl_coal_cnt =
7324 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7325 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7326 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7327 		req->flags = cpu_to_le16(flags);
7328 		break;
7329 	}
7330 	case HWRM_RING_ALLOC_RX:
7331 	case HWRM_RING_ALLOC_AGG:
7332 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7333 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7334 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7335 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7336 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7337 			bnxt_set_rx_ring_params_p5(bp, ring_type, req,
7338 						   rxr, ring);
7339 		break;
7340 	case HWRM_RING_ALLOC_CMPL:
7341 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7342 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7343 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7344 			/* Association of cp ring with nq */
7345 			grp_info = &bp->grp_info[map_index];
7346 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7347 			req->cq_handle = cpu_to_le64(ring->handle);
7348 			req->enables |= cpu_to_le32(
7349 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7350 		} else {
7351 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7352 		}
7353 		break;
7354 	case HWRM_RING_ALLOC_NQ:
7355 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7356 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7357 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7358 		break;
7359 	default:
7360 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7361 			   ring_type);
7362 		return -EINVAL;
7363 	}
7364 
7365 	resp = hwrm_req_hold(bp, req);
7366 	rc = hwrm_req_send(bp, req);
7367 	err = le16_to_cpu(resp->error_code);
7368 	ring_id = le16_to_cpu(resp->ring_id);
7369 	hwrm_req_drop(bp, req);
7370 
7371 exit:
7372 	if (rc || err) {
7373 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7374 			   ring_type, rc, err);
7375 		return -EIO;
7376 	}
7377 	ring->fw_ring_id = ring_id;
7378 	return rc;
7379 }
7380 
7381 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7382 {
7383 	int rc;
7384 
7385 	if (BNXT_PF(bp)) {
7386 		struct hwrm_func_cfg_input *req;
7387 
7388 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7389 		if (rc)
7390 			return rc;
7391 
7392 		req->fid = cpu_to_le16(0xffff);
7393 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7394 		req->async_event_cr = cpu_to_le16(idx);
7395 		return hwrm_req_send(bp, req);
7396 	} else {
7397 		struct hwrm_func_vf_cfg_input *req;
7398 
7399 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7400 		if (rc)
7401 			return rc;
7402 
7403 		req->enables =
7404 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7405 		req->async_event_cr = cpu_to_le16(idx);
7406 		return hwrm_req_send(bp, req);
7407 	}
7408 }
7409 
7410 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7411 			     u32 ring_type)
7412 {
7413 	switch (ring_type) {
7414 	case HWRM_RING_ALLOC_TX:
7415 		db->db_ring_mask = bp->tx_ring_mask;
7416 		break;
7417 	case HWRM_RING_ALLOC_RX:
7418 		db->db_ring_mask = bp->rx_ring_mask;
7419 		break;
7420 	case HWRM_RING_ALLOC_AGG:
7421 		db->db_ring_mask = bp->rx_agg_ring_mask;
7422 		break;
7423 	case HWRM_RING_ALLOC_CMPL:
7424 	case HWRM_RING_ALLOC_NQ:
7425 		db->db_ring_mask = bp->cp_ring_mask;
7426 		break;
7427 	}
7428 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7429 		db->db_epoch_mask = db->db_ring_mask + 1;
7430 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7431 	}
7432 }
7433 
7434 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7435 			u32 map_idx, u32 xid)
7436 {
7437 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7438 		switch (ring_type) {
7439 		case HWRM_RING_ALLOC_TX:
7440 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7441 			break;
7442 		case HWRM_RING_ALLOC_RX:
7443 		case HWRM_RING_ALLOC_AGG:
7444 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7445 			break;
7446 		case HWRM_RING_ALLOC_CMPL:
7447 			db->db_key64 = DBR_PATH_L2;
7448 			break;
7449 		case HWRM_RING_ALLOC_NQ:
7450 			db->db_key64 = DBR_PATH_L2;
7451 			break;
7452 		}
7453 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7454 
7455 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7456 			db->db_key64 |= DBR_VALID;
7457 
7458 		db->doorbell = bp->bar1 + bp->db_offset;
7459 	} else {
7460 		db->doorbell = bp->bar1 + map_idx * 0x80;
7461 		switch (ring_type) {
7462 		case HWRM_RING_ALLOC_TX:
7463 			db->db_key32 = DB_KEY_TX;
7464 			break;
7465 		case HWRM_RING_ALLOC_RX:
7466 		case HWRM_RING_ALLOC_AGG:
7467 			db->db_key32 = DB_KEY_RX;
7468 			break;
7469 		case HWRM_RING_ALLOC_CMPL:
7470 			db->db_key32 = DB_KEY_CP;
7471 			break;
7472 		}
7473 	}
7474 	bnxt_set_db_mask(bp, db, ring_type);
7475 }
7476 
7477 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7478 				   struct bnxt_rx_ring_info *rxr)
7479 {
7480 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7481 	struct bnxt_napi *bnapi = rxr->bnapi;
7482 	u32 type = HWRM_RING_ALLOC_RX;
7483 	u32 map_idx = bnapi->index;
7484 	int rc;
7485 
7486 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7487 	if (rc)
7488 		return rc;
7489 
7490 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7491 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7492 
7493 	return 0;
7494 }
7495 
7496 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7497 				       struct bnxt_rx_ring_info *rxr)
7498 {
7499 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7500 	u32 type = HWRM_RING_ALLOC_AGG;
7501 	u32 grp_idx = ring->grp_idx;
7502 	u32 map_idx;
7503 	int rc;
7504 
7505 	map_idx = grp_idx + bp->rx_nr_rings;
7506 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7507 	if (rc)
7508 		return rc;
7509 
7510 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7511 		    ring->fw_ring_id);
7512 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7513 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7514 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7515 
7516 	return 0;
7517 }
7518 
7519 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7520 				      struct bnxt_cp_ring_info *cpr)
7521 {
7522 	const u32 type = HWRM_RING_ALLOC_CMPL;
7523 	struct bnxt_napi *bnapi = cpr->bnapi;
7524 	struct bnxt_ring_struct *ring;
7525 	u32 map_idx = bnapi->index;
7526 	int rc;
7527 
7528 	ring = &cpr->cp_ring_struct;
7529 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7530 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7531 	if (rc)
7532 		return rc;
7533 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7534 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7535 	return 0;
7536 }
7537 
7538 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7539 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7540 {
7541 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7542 	const u32 type = HWRM_RING_ALLOC_TX;
7543 	int rc;
7544 
7545 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, tx_idx);
7546 	if (rc)
7547 		return rc;
7548 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7549 	return 0;
7550 }
7551 
7552 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7553 {
7554 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7555 	int i, rc = 0;
7556 	u32 type;
7557 
7558 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7559 		type = HWRM_RING_ALLOC_NQ;
7560 	else
7561 		type = HWRM_RING_ALLOC_CMPL;
7562 	for (i = 0; i < bp->cp_nr_rings; i++) {
7563 		struct bnxt_napi *bnapi = bp->bnapi[i];
7564 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7565 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7566 		u32 map_idx = ring->map_idx;
7567 		unsigned int vector;
7568 
7569 		vector = bp->irq_tbl[map_idx].vector;
7570 		disable_irq_nosync(vector);
7571 		rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7572 		if (rc) {
7573 			enable_irq(vector);
7574 			goto err_out;
7575 		}
7576 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7577 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7578 		enable_irq(vector);
7579 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7580 
7581 		if (!i) {
7582 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7583 			if (rc)
7584 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7585 		}
7586 	}
7587 
7588 	for (i = 0; i < bp->tx_nr_rings; i++) {
7589 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7590 
7591 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7592 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7593 			if (rc)
7594 				goto err_out;
7595 		}
7596 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7597 		if (rc)
7598 			goto err_out;
7599 	}
7600 
7601 	for (i = 0; i < bp->rx_nr_rings; i++) {
7602 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7603 
7604 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7605 		if (rc)
7606 			goto err_out;
7607 		/* If we have agg rings, post agg buffers first. */
7608 		if (!agg_rings)
7609 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7610 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7611 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7612 			if (rc)
7613 				goto err_out;
7614 		}
7615 	}
7616 
7617 	if (agg_rings) {
7618 		for (i = 0; i < bp->rx_nr_rings; i++) {
7619 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7620 			if (rc)
7621 				goto err_out;
7622 		}
7623 	}
7624 err_out:
7625 	return rc;
7626 }
7627 
7628 static void bnxt_cancel_dim(struct bnxt *bp)
7629 {
7630 	int i;
7631 
7632 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7633 	 * if NAPI is enabled.
7634 	 */
7635 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7636 		return;
7637 
7638 	/* Make sure NAPI sees that the VNIC is disabled */
7639 	synchronize_net();
7640 	for (i = 0; i < bp->rx_nr_rings; i++) {
7641 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7642 		struct bnxt_napi *bnapi = rxr->bnapi;
7643 
7644 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7645 	}
7646 }
7647 
7648 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7649 				   struct bnxt_ring_struct *ring,
7650 				   u32 ring_type, int cmpl_ring_id)
7651 {
7652 	struct hwrm_ring_free_output *resp;
7653 	struct hwrm_ring_free_input *req;
7654 	u16 error_code = 0;
7655 	int rc;
7656 
7657 	if (BNXT_NO_FW_ACCESS(bp))
7658 		return 0;
7659 
7660 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7661 	if (rc)
7662 		goto exit;
7663 
7664 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7665 	req->ring_type = ring_type;
7666 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7667 
7668 	resp = hwrm_req_hold(bp, req);
7669 	rc = hwrm_req_send(bp, req);
7670 	error_code = le16_to_cpu(resp->error_code);
7671 	hwrm_req_drop(bp, req);
7672 exit:
7673 	if (rc || error_code) {
7674 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7675 			   ring_type, rc, error_code);
7676 		return -EIO;
7677 	}
7678 	return 0;
7679 }
7680 
7681 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7682 				   struct bnxt_tx_ring_info *txr,
7683 				   bool close_path)
7684 {
7685 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7686 	u32 cmpl_ring_id;
7687 
7688 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7689 		return;
7690 
7691 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7692 		       INVALID_HW_RING_ID;
7693 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7694 				cmpl_ring_id);
7695 	ring->fw_ring_id = INVALID_HW_RING_ID;
7696 }
7697 
7698 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7699 				   struct bnxt_rx_ring_info *rxr,
7700 				   bool close_path)
7701 {
7702 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7703 	u32 grp_idx = rxr->bnapi->index;
7704 	u32 cmpl_ring_id;
7705 
7706 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7707 		return;
7708 
7709 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7710 	hwrm_ring_free_send_msg(bp, ring,
7711 				RING_FREE_REQ_RING_TYPE_RX,
7712 				close_path ? cmpl_ring_id :
7713 				INVALID_HW_RING_ID);
7714 	ring->fw_ring_id = INVALID_HW_RING_ID;
7715 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7716 }
7717 
7718 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7719 				       struct bnxt_rx_ring_info *rxr,
7720 				       bool close_path)
7721 {
7722 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7723 	u32 grp_idx = rxr->bnapi->index;
7724 	u32 type, cmpl_ring_id;
7725 
7726 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7727 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7728 	else
7729 		type = RING_FREE_REQ_RING_TYPE_RX;
7730 
7731 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7732 		return;
7733 
7734 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7735 	hwrm_ring_free_send_msg(bp, ring, type,
7736 				close_path ? cmpl_ring_id :
7737 				INVALID_HW_RING_ID);
7738 	ring->fw_ring_id = INVALID_HW_RING_ID;
7739 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7740 }
7741 
7742 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7743 				   struct bnxt_cp_ring_info *cpr)
7744 {
7745 	struct bnxt_ring_struct *ring;
7746 
7747 	ring = &cpr->cp_ring_struct;
7748 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7749 		return;
7750 
7751 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7752 				INVALID_HW_RING_ID);
7753 	ring->fw_ring_id = INVALID_HW_RING_ID;
7754 }
7755 
7756 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7757 {
7758 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7759 	int i, size = ring->ring_mem.page_size;
7760 
7761 	cpr->cp_raw_cons = 0;
7762 	cpr->toggle = 0;
7763 
7764 	for (i = 0; i < bp->cp_nr_pages; i++)
7765 		if (cpr->cp_desc_ring[i])
7766 			memset(cpr->cp_desc_ring[i], 0, size);
7767 }
7768 
7769 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7770 {
7771 	u32 type;
7772 	int i;
7773 
7774 	if (!bp->bnapi)
7775 		return;
7776 
7777 	for (i = 0; i < bp->tx_nr_rings; i++)
7778 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7779 
7780 	bnxt_cancel_dim(bp);
7781 	for (i = 0; i < bp->rx_nr_rings; i++) {
7782 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7783 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7784 	}
7785 
7786 	/* The completion rings are about to be freed.  After that the
7787 	 * IRQ doorbell will not work anymore.  So we need to disable
7788 	 * IRQ here.
7789 	 */
7790 	bnxt_disable_int_sync(bp);
7791 
7792 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7793 		type = RING_FREE_REQ_RING_TYPE_NQ;
7794 	else
7795 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7796 	for (i = 0; i < bp->cp_nr_rings; i++) {
7797 		struct bnxt_napi *bnapi = bp->bnapi[i];
7798 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7799 		struct bnxt_ring_struct *ring;
7800 		int j;
7801 
7802 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7803 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7804 
7805 		ring = &cpr->cp_ring_struct;
7806 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7807 			hwrm_ring_free_send_msg(bp, ring, type,
7808 						INVALID_HW_RING_ID);
7809 			ring->fw_ring_id = INVALID_HW_RING_ID;
7810 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7811 		}
7812 	}
7813 }
7814 
7815 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7816 			     bool shared);
7817 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7818 			   bool shared);
7819 
7820 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7821 {
7822 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7823 	struct hwrm_func_qcfg_output *resp;
7824 	struct hwrm_func_qcfg_input *req;
7825 	int rc;
7826 
7827 	if (bp->hwrm_spec_code < 0x10601)
7828 		return 0;
7829 
7830 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7831 	if (rc)
7832 		return rc;
7833 
7834 	req->fid = cpu_to_le16(0xffff);
7835 	resp = hwrm_req_hold(bp, req);
7836 	rc = hwrm_req_send(bp, req);
7837 	if (rc) {
7838 		hwrm_req_drop(bp, req);
7839 		return rc;
7840 	}
7841 
7842 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7843 	if (BNXT_NEW_RM(bp)) {
7844 		u16 cp, stats;
7845 
7846 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7847 		hw_resc->resv_hw_ring_grps =
7848 			le32_to_cpu(resp->alloc_hw_ring_grps);
7849 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7850 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7851 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7852 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7853 		hw_resc->resv_irqs = cp;
7854 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7855 			int rx = hw_resc->resv_rx_rings;
7856 			int tx = hw_resc->resv_tx_rings;
7857 
7858 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7859 				rx >>= 1;
7860 			if (cp < (rx + tx)) {
7861 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7862 				if (rc)
7863 					goto get_rings_exit;
7864 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7865 					rx <<= 1;
7866 				hw_resc->resv_rx_rings = rx;
7867 				hw_resc->resv_tx_rings = tx;
7868 			}
7869 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7870 			hw_resc->resv_hw_ring_grps = rx;
7871 		}
7872 		hw_resc->resv_cp_rings = cp;
7873 		hw_resc->resv_stat_ctxs = stats;
7874 	}
7875 get_rings_exit:
7876 	hwrm_req_drop(bp, req);
7877 	return rc;
7878 }
7879 
7880 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7881 {
7882 	struct hwrm_func_qcfg_output *resp;
7883 	struct hwrm_func_qcfg_input *req;
7884 	int rc;
7885 
7886 	if (bp->hwrm_spec_code < 0x10601)
7887 		return 0;
7888 
7889 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7890 	if (rc)
7891 		return rc;
7892 
7893 	req->fid = cpu_to_le16(fid);
7894 	resp = hwrm_req_hold(bp, req);
7895 	rc = hwrm_req_send(bp, req);
7896 	if (!rc)
7897 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7898 
7899 	hwrm_req_drop(bp, req);
7900 	return rc;
7901 }
7902 
7903 static bool bnxt_rfs_supported(struct bnxt *bp);
7904 
7905 static struct hwrm_func_cfg_input *
7906 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7907 {
7908 	struct hwrm_func_cfg_input *req;
7909 	u32 enables = 0;
7910 
7911 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7912 		return NULL;
7913 
7914 	req->fid = cpu_to_le16(0xffff);
7915 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7916 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7917 	if (BNXT_NEW_RM(bp)) {
7918 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7919 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7920 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7921 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7922 			enables |= hwr->cp_p5 ?
7923 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7924 		} else {
7925 			enables |= hwr->cp ?
7926 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7927 			enables |= hwr->grp ?
7928 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7929 		}
7930 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7931 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7932 					  0;
7933 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7934 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7935 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7936 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7937 			req->num_msix = cpu_to_le16(hwr->cp);
7938 		} else {
7939 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7940 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7941 		}
7942 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7943 		req->num_vnics = cpu_to_le16(hwr->vnic);
7944 	}
7945 	req->enables = cpu_to_le32(enables);
7946 	return req;
7947 }
7948 
7949 static struct hwrm_func_vf_cfg_input *
7950 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7951 {
7952 	struct hwrm_func_vf_cfg_input *req;
7953 	u32 enables = 0;
7954 
7955 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7956 		return NULL;
7957 
7958 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7959 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7960 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7961 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7962 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7963 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7964 		enables |= hwr->cp_p5 ?
7965 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7966 	} else {
7967 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7968 		enables |= hwr->grp ?
7969 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7970 	}
7971 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7972 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7973 
7974 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7975 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7976 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7977 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7978 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7979 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7980 	} else {
7981 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7982 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7983 	}
7984 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7985 	req->num_vnics = cpu_to_le16(hwr->vnic);
7986 
7987 	req->enables = cpu_to_le32(enables);
7988 	return req;
7989 }
7990 
7991 static int
7992 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7993 {
7994 	struct hwrm_func_cfg_input *req;
7995 	int rc;
7996 
7997 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7998 	if (!req)
7999 		return -ENOMEM;
8000 
8001 	if (!req->enables) {
8002 		hwrm_req_drop(bp, req);
8003 		return 0;
8004 	}
8005 
8006 	rc = hwrm_req_send(bp, req);
8007 	if (rc)
8008 		return rc;
8009 
8010 	if (bp->hwrm_spec_code < 0x10601)
8011 		bp->hw_resc.resv_tx_rings = hwr->tx;
8012 
8013 	return bnxt_hwrm_get_rings(bp);
8014 }
8015 
8016 static int
8017 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8018 {
8019 	struct hwrm_func_vf_cfg_input *req;
8020 	int rc;
8021 
8022 	if (!BNXT_NEW_RM(bp)) {
8023 		bp->hw_resc.resv_tx_rings = hwr->tx;
8024 		return 0;
8025 	}
8026 
8027 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8028 	if (!req)
8029 		return -ENOMEM;
8030 
8031 	rc = hwrm_req_send(bp, req);
8032 	if (rc)
8033 		return rc;
8034 
8035 	return bnxt_hwrm_get_rings(bp);
8036 }
8037 
8038 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8039 {
8040 	if (BNXT_PF(bp))
8041 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
8042 	else
8043 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
8044 }
8045 
8046 int bnxt_nq_rings_in_use(struct bnxt *bp)
8047 {
8048 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
8049 }
8050 
8051 static int bnxt_cp_rings_in_use(struct bnxt *bp)
8052 {
8053 	int cp;
8054 
8055 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8056 		return bnxt_nq_rings_in_use(bp);
8057 
8058 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
8059 	return cp;
8060 }
8061 
8062 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
8063 {
8064 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
8065 }
8066 
8067 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8068 {
8069 	if (!hwr->grp)
8070 		return 0;
8071 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8072 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
8073 
8074 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
8075 			rss_ctx *= hwr->vnic;
8076 		return rss_ctx;
8077 	}
8078 	if (BNXT_VF(bp))
8079 		return BNXT_VF_MAX_RSS_CTX;
8080 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
8081 		return hwr->grp + 1;
8082 	return 1;
8083 }
8084 
8085 /* Check if a default RSS map needs to be setup.  This function is only
8086  * used on older firmware that does not require reserving RX rings.
8087  */
8088 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
8089 {
8090 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8091 
8092 	/* The RSS map is valid for RX rings set to resv_rx_rings */
8093 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
8094 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
8095 		if (!netif_is_rxfh_configured(bp->dev))
8096 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8097 	}
8098 }
8099 
8100 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
8101 {
8102 	if (bp->flags & BNXT_FLAG_RFS) {
8103 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
8104 			return 2 + bp->num_rss_ctx;
8105 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8106 			return rx_rings + 1;
8107 	}
8108 	return 1;
8109 }
8110 
8111 static void bnxt_get_total_resources(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8112 {
8113 	hwr->cp = bnxt_nq_rings_in_use(bp);
8114 	hwr->cp_p5 = 0;
8115 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8116 		hwr->cp_p5 = bnxt_cp_rings_in_use(bp);
8117 	hwr->tx = bp->tx_nr_rings;
8118 	hwr->rx = bp->rx_nr_rings;
8119 	hwr->grp = hwr->rx;
8120 	hwr->vnic = bnxt_get_total_vnics(bp, hwr->rx);
8121 	hwr->rss_ctx = bnxt_get_total_rss_ctxs(bp, hwr);
8122 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8123 		hwr->rx <<= 1;
8124 	hwr->stat = bnxt_get_func_stat_ctxs(bp);
8125 }
8126 
8127 static bool bnxt_need_reserve_rings(struct bnxt *bp)
8128 {
8129 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8130 	struct bnxt_hw_rings hwr;
8131 
8132 	bnxt_get_total_resources(bp, &hwr);
8133 
8134 	/* Old firmware does not need RX ring reservations but we still
8135 	 * need to setup a default RSS map when needed.  With new firmware
8136 	 * we go through RX ring reservations first and then set up the
8137 	 * RSS map for the successfully reserved RX rings when needed.
8138 	 */
8139 	if (!BNXT_NEW_RM(bp))
8140 		bnxt_check_rss_tbl_no_rmgr(bp);
8141 
8142 	if (hw_resc->resv_tx_rings != hwr.tx && bp->hwrm_spec_code >= 0x10601)
8143 		return true;
8144 
8145 	if (!BNXT_NEW_RM(bp))
8146 		return false;
8147 
8148 	if (hw_resc->resv_rx_rings != hwr.rx ||
8149 	    hw_resc->resv_vnics != hwr.vnic ||
8150 	    hw_resc->resv_stat_ctxs != hwr.stat ||
8151 	    hw_resc->resv_rsscos_ctxs != hwr.rss_ctx ||
8152 	    (hw_resc->resv_hw_ring_grps != hwr.grp &&
8153 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
8154 		return true;
8155 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8156 		if (hw_resc->resv_cp_rings != hwr.cp_p5)
8157 			return true;
8158 	} else if (hw_resc->resv_cp_rings != hwr.cp) {
8159 		return true;
8160 	}
8161 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
8162 	    hw_resc->resv_irqs != hwr.cp)
8163 		return true;
8164 	return false;
8165 }
8166 
8167 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8168 {
8169 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8170 
8171 	hwr->tx = hw_resc->resv_tx_rings;
8172 	if (BNXT_NEW_RM(bp)) {
8173 		hwr->rx = hw_resc->resv_rx_rings;
8174 		hwr->cp = hw_resc->resv_irqs;
8175 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8176 			hwr->cp_p5 = hw_resc->resv_cp_rings;
8177 		hwr->grp = hw_resc->resv_hw_ring_grps;
8178 		hwr->vnic = hw_resc->resv_vnics;
8179 		hwr->stat = hw_resc->resv_stat_ctxs;
8180 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
8181 	}
8182 }
8183 
8184 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8185 {
8186 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
8187 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
8188 }
8189 
8190 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
8191 
8192 static int __bnxt_reserve_rings(struct bnxt *bp)
8193 {
8194 	struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA];
8195 	struct bnxt_hw_rings hwr = {0};
8196 	int rx_rings, old_rx_rings, rc;
8197 	int cp = bp->cp_nr_rings;
8198 	int ulp_msix = 0;
8199 	bool sh = false;
8200 	int tx_cp;
8201 
8202 	if (!bnxt_need_reserve_rings(bp))
8203 		return 0;
8204 
8205 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(edev)) {
8206 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
8207 		if (!ulp_msix)
8208 			bnxt_set_ulp_stat_ctxs(bp, 0);
8209 		else
8210 			bnxt_set_dflt_ulp_stat_ctxs(bp);
8211 
8212 		if (ulp_msix > bp->ulp_num_msix_want)
8213 			ulp_msix = bp->ulp_num_msix_want;
8214 		hwr.cp = cp + ulp_msix;
8215 	} else {
8216 		hwr.cp = bnxt_nq_rings_in_use(bp);
8217 	}
8218 
8219 	hwr.tx = bp->tx_nr_rings;
8220 	hwr.rx = bp->rx_nr_rings;
8221 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8222 		sh = true;
8223 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8224 		hwr.cp_p5 = hwr.rx + hwr.tx;
8225 
8226 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
8227 
8228 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8229 		hwr.rx <<= 1;
8230 	hwr.grp = bp->rx_nr_rings;
8231 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
8232 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
8233 	old_rx_rings = bp->hw_resc.resv_rx_rings;
8234 
8235 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
8236 	if (rc)
8237 		return rc;
8238 
8239 	bnxt_copy_reserved_rings(bp, &hwr);
8240 
8241 	rx_rings = hwr.rx;
8242 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8243 		if (hwr.rx >= 2) {
8244 			rx_rings = hwr.rx >> 1;
8245 		} else {
8246 			if (netif_running(bp->dev))
8247 				return -ENOMEM;
8248 
8249 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
8250 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
8251 			bp->dev->hw_features &= ~NETIF_F_LRO;
8252 			bp->dev->features &= ~NETIF_F_LRO;
8253 			bnxt_set_ring_params(bp);
8254 		}
8255 	}
8256 	rx_rings = min_t(int, rx_rings, hwr.grp);
8257 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
8258 	if (bnxt_ulp_registered(edev) && hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
8259 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
8260 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
8261 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
8262 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8263 		hwr.rx = rx_rings << 1;
8264 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
8265 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
8266 	if (hwr.tx != bp->tx_nr_rings) {
8267 		netdev_warn(bp->dev,
8268 			    "Able to reserve only %d out of %d requested TX rings\n",
8269 			    hwr.tx, bp->tx_nr_rings);
8270 	}
8271 	bp->tx_nr_rings = hwr.tx;
8272 
8273 	/* If we cannot reserve all the RX rings, reset the RSS map only
8274 	 * if absolutely necessary
8275 	 */
8276 	if (rx_rings != bp->rx_nr_rings) {
8277 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
8278 			    rx_rings, bp->rx_nr_rings);
8279 		if (netif_is_rxfh_configured(bp->dev) &&
8280 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
8281 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
8282 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
8283 			ethtool_rxfh_indir_lost(bp->dev);
8284 		}
8285 	}
8286 	bp->rx_nr_rings = rx_rings;
8287 	bp->cp_nr_rings = hwr.cp;
8288 
8289 	/* Fall back if we cannot reserve enough HW RSS contexts */
8290 	if ((bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX) &&
8291 	    hwr.rss_ctx < bnxt_get_total_rss_ctxs(bp, &hwr))
8292 		bp->rss_cap &= ~BNXT_RSS_CAP_LARGE_RSS_CTX;
8293 
8294 	if (!bnxt_rings_ok(bp, &hwr))
8295 		return -ENOMEM;
8296 
8297 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
8298 	    !netif_is_rxfh_configured(bp->dev))
8299 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8300 
8301 	if (!bnxt_ulp_registered(edev) && BNXT_NEW_RM(bp)) {
8302 		int resv_msix, resv_ctx, ulp_ctxs;
8303 		struct bnxt_hw_resc *hw_resc;
8304 
8305 		hw_resc = &bp->hw_resc;
8306 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
8307 		ulp_msix = min_t(int, resv_msix, ulp_msix);
8308 		bnxt_set_ulp_msix_num(bp, ulp_msix);
8309 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8310 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8311 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8312 	}
8313 
8314 	return rc;
8315 }
8316 
8317 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8318 {
8319 	struct hwrm_func_vf_cfg_input *req;
8320 	u32 flags;
8321 
8322 	if (!BNXT_NEW_RM(bp))
8323 		return 0;
8324 
8325 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8326 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8327 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8328 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8329 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8330 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8331 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8332 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8333 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8334 
8335 	req->flags = cpu_to_le32(flags);
8336 	return hwrm_req_send_silent(bp, req);
8337 }
8338 
8339 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8340 {
8341 	struct hwrm_func_cfg_input *req;
8342 	u32 flags;
8343 
8344 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8345 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8346 	if (BNXT_NEW_RM(bp)) {
8347 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8348 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8349 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8350 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8351 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8352 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8353 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8354 		else
8355 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8356 	}
8357 
8358 	req->flags = cpu_to_le32(flags);
8359 	return hwrm_req_send_silent(bp, req);
8360 }
8361 
8362 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8363 {
8364 	if (bp->hwrm_spec_code < 0x10801)
8365 		return 0;
8366 
8367 	if (BNXT_PF(bp))
8368 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8369 
8370 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8371 }
8372 
8373 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8374 {
8375 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8376 	struct hwrm_ring_aggint_qcaps_output *resp;
8377 	struct hwrm_ring_aggint_qcaps_input *req;
8378 	int rc;
8379 
8380 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8381 	coal_cap->num_cmpl_dma_aggr_max = 63;
8382 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8383 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8384 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8385 	coal_cap->int_lat_tmr_min_max = 65535;
8386 	coal_cap->int_lat_tmr_max_max = 65535;
8387 	coal_cap->num_cmpl_aggr_int_max = 65535;
8388 	coal_cap->timer_units = 80;
8389 
8390 	if (bp->hwrm_spec_code < 0x10902)
8391 		return;
8392 
8393 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8394 		return;
8395 
8396 	resp = hwrm_req_hold(bp, req);
8397 	rc = hwrm_req_send_silent(bp, req);
8398 	if (!rc) {
8399 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8400 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8401 		coal_cap->num_cmpl_dma_aggr_max =
8402 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8403 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8404 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8405 		coal_cap->cmpl_aggr_dma_tmr_max =
8406 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8407 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8408 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8409 		coal_cap->int_lat_tmr_min_max =
8410 			le16_to_cpu(resp->int_lat_tmr_min_max);
8411 		coal_cap->int_lat_tmr_max_max =
8412 			le16_to_cpu(resp->int_lat_tmr_max_max);
8413 		coal_cap->num_cmpl_aggr_int_max =
8414 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8415 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8416 	}
8417 	hwrm_req_drop(bp, req);
8418 }
8419 
8420 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8421 {
8422 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8423 
8424 	return usec * 1000 / coal_cap->timer_units;
8425 }
8426 
8427 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8428 	struct bnxt_coal *hw_coal,
8429 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8430 {
8431 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8432 	u16 val, tmr, max, flags = hw_coal->flags;
8433 	u32 cmpl_params = coal_cap->cmpl_params;
8434 
8435 	max = hw_coal->bufs_per_record * 128;
8436 	if (hw_coal->budget)
8437 		max = hw_coal->bufs_per_record * hw_coal->budget;
8438 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8439 
8440 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8441 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8442 
8443 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8444 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8445 
8446 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8447 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8448 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8449 
8450 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8451 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8452 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8453 
8454 	/* min timer set to 1/2 of interrupt timer */
8455 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8456 		val = tmr / 2;
8457 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8458 		req->int_lat_tmr_min = cpu_to_le16(val);
8459 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8460 	}
8461 
8462 	/* buf timer set to 1/4 of interrupt timer */
8463 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8464 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8465 
8466 	if (cmpl_params &
8467 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8468 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8469 		val = clamp_t(u16, tmr, 1,
8470 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8471 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8472 		req->enables |=
8473 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8474 	}
8475 
8476 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8477 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8478 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8479 	req->flags = cpu_to_le16(flags);
8480 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8481 }
8482 
8483 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8484 				   struct bnxt_coal *hw_coal)
8485 {
8486 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8487 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8488 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8489 	u32 nq_params = coal_cap->nq_params;
8490 	u16 tmr;
8491 	int rc;
8492 
8493 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8494 		return 0;
8495 
8496 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8497 	if (rc)
8498 		return rc;
8499 
8500 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8501 	req->flags =
8502 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8503 
8504 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8505 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8506 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8507 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8508 	return hwrm_req_send(bp, req);
8509 }
8510 
8511 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8512 {
8513 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8514 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8515 	struct bnxt_coal coal;
8516 	int rc;
8517 
8518 	/* Tick values in micro seconds.
8519 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8520 	 */
8521 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8522 
8523 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8524 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8525 
8526 	if (!bnapi->rx_ring)
8527 		return -ENODEV;
8528 
8529 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8530 	if (rc)
8531 		return rc;
8532 
8533 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8534 
8535 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8536 
8537 	return hwrm_req_send(bp, req_rx);
8538 }
8539 
8540 static int
8541 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8542 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8543 {
8544 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8545 
8546 	req->ring_id = cpu_to_le16(ring_id);
8547 	return hwrm_req_send(bp, req);
8548 }
8549 
8550 static int
8551 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8552 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8553 {
8554 	struct bnxt_tx_ring_info *txr;
8555 	int i, rc;
8556 
8557 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8558 		u16 ring_id;
8559 
8560 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8561 		req->ring_id = cpu_to_le16(ring_id);
8562 		rc = hwrm_req_send(bp, req);
8563 		if (rc)
8564 			return rc;
8565 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8566 			return 0;
8567 	}
8568 	return 0;
8569 }
8570 
8571 int bnxt_hwrm_set_coal(struct bnxt *bp)
8572 {
8573 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8574 	int i, rc;
8575 
8576 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8577 	if (rc)
8578 		return rc;
8579 
8580 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8581 	if (rc) {
8582 		hwrm_req_drop(bp, req_rx);
8583 		return rc;
8584 	}
8585 
8586 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8587 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8588 
8589 	hwrm_req_hold(bp, req_rx);
8590 	hwrm_req_hold(bp, req_tx);
8591 	for (i = 0; i < bp->cp_nr_rings; i++) {
8592 		struct bnxt_napi *bnapi = bp->bnapi[i];
8593 		struct bnxt_coal *hw_coal;
8594 
8595 		if (!bnapi->rx_ring)
8596 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8597 		else
8598 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8599 		if (rc)
8600 			break;
8601 
8602 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8603 			continue;
8604 
8605 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8606 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8607 			if (rc)
8608 				break;
8609 		}
8610 		if (bnapi->rx_ring)
8611 			hw_coal = &bp->rx_coal;
8612 		else
8613 			hw_coal = &bp->tx_coal;
8614 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8615 	}
8616 	hwrm_req_drop(bp, req_rx);
8617 	hwrm_req_drop(bp, req_tx);
8618 	return rc;
8619 }
8620 
8621 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8622 {
8623 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8624 	struct hwrm_stat_ctx_free_input *req;
8625 	int i;
8626 
8627 	if (!bp->bnapi)
8628 		return;
8629 
8630 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8631 		return;
8632 
8633 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8634 		return;
8635 	if (BNXT_FW_MAJ(bp) <= 20) {
8636 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8637 			hwrm_req_drop(bp, req);
8638 			return;
8639 		}
8640 		hwrm_req_hold(bp, req0);
8641 	}
8642 	hwrm_req_hold(bp, req);
8643 	for (i = 0; i < bp->cp_nr_rings; i++) {
8644 		struct bnxt_napi *bnapi = bp->bnapi[i];
8645 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8646 
8647 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8648 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8649 			if (req0) {
8650 				req0->stat_ctx_id = req->stat_ctx_id;
8651 				hwrm_req_send(bp, req0);
8652 			}
8653 			hwrm_req_send(bp, req);
8654 
8655 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8656 		}
8657 	}
8658 	hwrm_req_drop(bp, req);
8659 	if (req0)
8660 		hwrm_req_drop(bp, req0);
8661 }
8662 
8663 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8664 {
8665 	struct hwrm_stat_ctx_alloc_output *resp;
8666 	struct hwrm_stat_ctx_alloc_input *req;
8667 	int rc, i;
8668 
8669 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8670 		return 0;
8671 
8672 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8673 	if (rc)
8674 		return rc;
8675 
8676 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8677 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8678 
8679 	resp = hwrm_req_hold(bp, req);
8680 	for (i = 0; i < bp->cp_nr_rings; i++) {
8681 		struct bnxt_napi *bnapi = bp->bnapi[i];
8682 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8683 
8684 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8685 
8686 		rc = hwrm_req_send(bp, req);
8687 		if (rc)
8688 			break;
8689 
8690 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8691 
8692 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8693 	}
8694 	hwrm_req_drop(bp, req);
8695 	return rc;
8696 }
8697 
8698 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8699 {
8700 	struct hwrm_func_qcfg_output *resp;
8701 	struct hwrm_func_qcfg_input *req;
8702 	u16 flags;
8703 	int rc;
8704 
8705 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8706 	if (rc)
8707 		return rc;
8708 
8709 	req->fid = cpu_to_le16(0xffff);
8710 	resp = hwrm_req_hold(bp, req);
8711 	rc = hwrm_req_send(bp, req);
8712 	if (rc)
8713 		goto func_qcfg_exit;
8714 
8715 	flags = le16_to_cpu(resp->flags);
8716 #ifdef CONFIG_BNXT_SRIOV
8717 	if (BNXT_VF(bp)) {
8718 		struct bnxt_vf_info *vf = &bp->vf;
8719 
8720 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8721 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8722 			vf->flags |= BNXT_VF_TRUST;
8723 		else
8724 			vf->flags &= ~BNXT_VF_TRUST;
8725 	} else {
8726 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8727 	}
8728 #endif
8729 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8730 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8731 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8732 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8733 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8734 	}
8735 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8736 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8737 
8738 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8739 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8740 
8741 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8742 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8743 	if (resp->roce_bidi_opt_mode &
8744 	    FUNC_QCFG_RESP_ROCE_BIDI_OPT_MODE_DEDICATED)
8745 		bp->cos0_cos1_shared = 1;
8746 	else
8747 		bp->cos0_cos1_shared = 0;
8748 
8749 	switch (resp->port_partition_type) {
8750 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8751 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8752 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8753 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8754 		bp->port_partition_type = resp->port_partition_type;
8755 		break;
8756 	}
8757 	if (bp->hwrm_spec_code < 0x10707 ||
8758 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8759 		bp->br_mode = BRIDGE_MODE_VEB;
8760 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8761 		bp->br_mode = BRIDGE_MODE_VEPA;
8762 	else
8763 		bp->br_mode = BRIDGE_MODE_UNDEF;
8764 
8765 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8766 	if (!bp->max_mtu)
8767 		bp->max_mtu = BNXT_MAX_MTU;
8768 
8769 	if (bp->db_size)
8770 		goto func_qcfg_exit;
8771 
8772 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8773 	if (BNXT_CHIP_P5(bp)) {
8774 		if (BNXT_PF(bp))
8775 			bp->db_offset = DB_PF_OFFSET_P5;
8776 		else
8777 			bp->db_offset = DB_VF_OFFSET_P5;
8778 	}
8779 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8780 				 1024);
8781 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8782 	    bp->db_size <= bp->db_offset)
8783 		bp->db_size = pci_resource_len(bp->pdev, 2);
8784 
8785 func_qcfg_exit:
8786 	hwrm_req_drop(bp, req);
8787 	return rc;
8788 }
8789 
8790 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8791 				      u8 init_val, u8 init_offset,
8792 				      bool init_mask_set)
8793 {
8794 	ctxm->init_value = init_val;
8795 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8796 	if (init_mask_set)
8797 		ctxm->init_offset = init_offset * 4;
8798 	else
8799 		ctxm->init_value = 0;
8800 }
8801 
8802 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8803 {
8804 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8805 	u16 type;
8806 
8807 	for (type = 0; type < ctx_max; type++) {
8808 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8809 		int n = 1;
8810 
8811 		if (!ctxm->max_entries || ctxm->pg_info)
8812 			continue;
8813 
8814 		if (ctxm->instance_bmap)
8815 			n = hweight32(ctxm->instance_bmap);
8816 		ctxm->pg_info = kzalloc_objs(*ctxm->pg_info, n);
8817 		if (!ctxm->pg_info)
8818 			return -ENOMEM;
8819 	}
8820 	return 0;
8821 }
8822 
8823 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8824 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8825 
8826 #define BNXT_CTX_INIT_VALID(flags)	\
8827 	(!!((flags) &			\
8828 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8829 
8830 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8831 {
8832 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8833 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8834 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8835 	u16 type, next_type = 0;
8836 	int rc;
8837 
8838 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8839 	if (rc)
8840 		return rc;
8841 
8842 	if (!ctx) {
8843 		ctx = kzalloc_obj(*ctx);
8844 		if (!ctx)
8845 			return -ENOMEM;
8846 		bp->ctx = ctx;
8847 	}
8848 
8849 	resp = hwrm_req_hold(bp, req);
8850 
8851 	for (type = 0; type < BNXT_CTX_V2_MAX; type = next_type) {
8852 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8853 		u8 init_val, init_off, i;
8854 		u32 max_entries;
8855 		u16 entry_size;
8856 		__le32 *p;
8857 		u32 flags;
8858 
8859 		req->type = cpu_to_le16(type);
8860 		rc = hwrm_req_send(bp, req);
8861 		if (rc)
8862 			goto ctx_done;
8863 		flags = le32_to_cpu(resp->flags);
8864 		next_type = le16_to_cpu(resp->next_valid_type);
8865 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8866 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8867 			continue;
8868 		}
8869 		entry_size = le16_to_cpu(resp->entry_size);
8870 		max_entries = le32_to_cpu(resp->max_num_entries);
8871 		if (ctxm->mem_valid) {
8872 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8873 			    ctxm->entry_size != entry_size ||
8874 			    ctxm->max_entries != max_entries)
8875 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8876 			else
8877 				continue;
8878 		}
8879 		ctxm->type = type;
8880 		ctxm->entry_size = entry_size;
8881 		ctxm->flags = flags;
8882 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8883 		ctxm->entry_multiple = resp->entry_multiple;
8884 		ctxm->max_entries = max_entries;
8885 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8886 		init_val = resp->ctx_init_value;
8887 		init_off = resp->ctx_init_offset;
8888 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8889 					  BNXT_CTX_INIT_VALID(flags));
8890 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8891 					      BNXT_MAX_SPLIT_ENTRY);
8892 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8893 		     i++, p++)
8894 			ctxm->split[i] = le32_to_cpu(*p);
8895 	}
8896 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8897 
8898 ctx_done:
8899 	hwrm_req_drop(bp, req);
8900 	return rc;
8901 }
8902 
8903 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8904 {
8905 	struct hwrm_func_backing_store_qcaps_output *resp;
8906 	struct hwrm_func_backing_store_qcaps_input *req;
8907 	int rc;
8908 
8909 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8910 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8911 		return 0;
8912 
8913 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8914 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8915 
8916 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8917 	if (rc)
8918 		return rc;
8919 
8920 	resp = hwrm_req_hold(bp, req);
8921 	rc = hwrm_req_send_silent(bp, req);
8922 	if (!rc) {
8923 		struct bnxt_ctx_mem_type *ctxm;
8924 		struct bnxt_ctx_mem_info *ctx;
8925 		u8 init_val, init_idx = 0;
8926 		u16 init_mask;
8927 
8928 		ctx = bp->ctx;
8929 		if (!ctx) {
8930 			ctx = kzalloc_obj(*ctx);
8931 			if (!ctx) {
8932 				rc = -ENOMEM;
8933 				goto ctx_err;
8934 			}
8935 			bp->ctx = ctx;
8936 		}
8937 		init_val = resp->ctx_kind_initializer;
8938 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8939 
8940 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8941 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8942 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8943 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8944 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8945 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8946 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8947 					  (init_mask & (1 << init_idx++)) != 0);
8948 
8949 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8950 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8951 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8952 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8953 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8954 					  (init_mask & (1 << init_idx++)) != 0);
8955 
8956 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8957 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8958 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8959 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8960 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8961 					  (init_mask & (1 << init_idx++)) != 0);
8962 
8963 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8964 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8965 		ctxm->max_entries = ctxm->vnic_entries +
8966 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8967 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8968 		bnxt_init_ctx_initializer(ctxm, init_val,
8969 					  resp->vnic_init_offset,
8970 					  (init_mask & (1 << init_idx++)) != 0);
8971 
8972 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8973 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8974 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8975 		bnxt_init_ctx_initializer(ctxm, init_val,
8976 					  resp->stat_init_offset,
8977 					  (init_mask & (1 << init_idx++)) != 0);
8978 
8979 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8980 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8981 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8982 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8983 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8984 		if (!ctxm->entry_multiple)
8985 			ctxm->entry_multiple = 1;
8986 
8987 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8988 
8989 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8990 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8991 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8992 		ctxm->mrav_num_entries_units =
8993 			le16_to_cpu(resp->mrav_num_entries_units);
8994 		bnxt_init_ctx_initializer(ctxm, init_val,
8995 					  resp->mrav_init_offset,
8996 					  (init_mask & (1 << init_idx++)) != 0);
8997 
8998 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8999 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
9000 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
9001 
9002 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
9003 		if (!ctx->tqm_fp_rings_count)
9004 			ctx->tqm_fp_rings_count = bp->max_q;
9005 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
9006 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
9007 
9008 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9009 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
9010 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
9011 
9012 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
9013 	} else {
9014 		rc = 0;
9015 	}
9016 ctx_err:
9017 	hwrm_req_drop(bp, req);
9018 	return rc;
9019 }
9020 
9021 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
9022 				  __le64 *pg_dir)
9023 {
9024 	if (!rmem->nr_pages)
9025 		return;
9026 
9027 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
9028 	if (rmem->depth >= 1) {
9029 		if (rmem->depth == 2)
9030 			*pg_attr |= 2;
9031 		else
9032 			*pg_attr |= 1;
9033 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
9034 	} else {
9035 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
9036 	}
9037 }
9038 
9039 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
9040 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
9041 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
9042 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
9043 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
9044 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
9045 
9046 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
9047 {
9048 	struct hwrm_func_backing_store_cfg_input *req;
9049 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9050 	struct bnxt_ctx_pg_info *ctx_pg;
9051 	struct bnxt_ctx_mem_type *ctxm;
9052 	void **__req = (void **)&req;
9053 	u32 req_len = sizeof(*req);
9054 	__le32 *num_entries;
9055 	__le64 *pg_dir;
9056 	u32 flags = 0;
9057 	u8 *pg_attr;
9058 	u32 ena;
9059 	int rc;
9060 	int i;
9061 
9062 	if (!ctx)
9063 		return 0;
9064 
9065 	if (req_len > bp->hwrm_max_ext_req_len)
9066 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
9067 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
9068 	if (rc)
9069 		return rc;
9070 
9071 	req->enables = cpu_to_le32(enables);
9072 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
9073 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9074 		ctx_pg = ctxm->pg_info;
9075 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
9076 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
9077 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
9078 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
9079 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9080 				      &req->qpc_pg_size_qpc_lvl,
9081 				      &req->qpc_page_dir);
9082 
9083 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
9084 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
9085 	}
9086 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
9087 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9088 		ctx_pg = ctxm->pg_info;
9089 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
9090 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
9091 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
9092 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9093 				      &req->srq_pg_size_srq_lvl,
9094 				      &req->srq_page_dir);
9095 	}
9096 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
9097 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9098 		ctx_pg = ctxm->pg_info;
9099 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
9100 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
9101 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
9102 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9103 				      &req->cq_pg_size_cq_lvl,
9104 				      &req->cq_page_dir);
9105 	}
9106 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
9107 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9108 		ctx_pg = ctxm->pg_info;
9109 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
9110 		req->vnic_num_ring_table_entries =
9111 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
9112 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
9113 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9114 				      &req->vnic_pg_size_vnic_lvl,
9115 				      &req->vnic_page_dir);
9116 	}
9117 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
9118 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9119 		ctx_pg = ctxm->pg_info;
9120 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
9121 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
9122 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9123 				      &req->stat_pg_size_stat_lvl,
9124 				      &req->stat_page_dir);
9125 	}
9126 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
9127 		u32 units;
9128 
9129 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9130 		ctx_pg = ctxm->pg_info;
9131 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
9132 		units = ctxm->mrav_num_entries_units;
9133 		if (units) {
9134 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
9135 			u32 entries;
9136 
9137 			num_mr = ctx_pg->entries - num_ah;
9138 			entries = ((num_mr / units) << 16) | (num_ah / units);
9139 			req->mrav_num_entries = cpu_to_le32(entries);
9140 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
9141 		}
9142 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
9143 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9144 				      &req->mrav_pg_size_mrav_lvl,
9145 				      &req->mrav_page_dir);
9146 	}
9147 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
9148 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9149 		ctx_pg = ctxm->pg_info;
9150 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
9151 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
9152 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9153 				      &req->tim_pg_size_tim_lvl,
9154 				      &req->tim_page_dir);
9155 	}
9156 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9157 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
9158 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
9159 	     pg_dir = &req->tqm_sp_page_dir,
9160 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
9161 	     ctx_pg = ctxm->pg_info;
9162 	     i < BNXT_MAX_TQM_RINGS;
9163 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
9164 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
9165 		if (!(enables & ena))
9166 			continue;
9167 
9168 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
9169 		*num_entries = cpu_to_le32(ctx_pg->entries);
9170 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
9171 	}
9172 	req->flags = cpu_to_le32(flags);
9173 	return hwrm_req_send(bp, req);
9174 }
9175 
9176 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
9177 				  struct bnxt_ctx_pg_info *ctx_pg)
9178 {
9179 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9180 
9181 	rmem->page_size = BNXT_PAGE_SIZE;
9182 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
9183 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
9184 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
9185 	if (rmem->depth >= 1)
9186 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
9187 	return bnxt_alloc_ring(bp, rmem);
9188 }
9189 
9190 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
9191 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
9192 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
9193 {
9194 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9195 	int rc;
9196 
9197 	if (!mem_size)
9198 		return -EINVAL;
9199 
9200 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9201 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
9202 		ctx_pg->nr_pages = 0;
9203 		return -EINVAL;
9204 	}
9205 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
9206 		int nr_tbls, i;
9207 
9208 		rmem->depth = 2;
9209 		ctx_pg->ctx_pg_tbl = kzalloc_objs(ctx_pg, MAX_CTX_PAGES);
9210 		if (!ctx_pg->ctx_pg_tbl)
9211 			return -ENOMEM;
9212 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
9213 		rmem->nr_pages = nr_tbls;
9214 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9215 		if (rc)
9216 			return rc;
9217 		for (i = 0; i < nr_tbls; i++) {
9218 			struct bnxt_ctx_pg_info *pg_tbl;
9219 
9220 			pg_tbl = kzalloc_obj(*pg_tbl);
9221 			if (!pg_tbl)
9222 				return -ENOMEM;
9223 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
9224 			rmem = &pg_tbl->ring_mem;
9225 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
9226 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
9227 			rmem->depth = 1;
9228 			rmem->nr_pages = MAX_CTX_PAGES;
9229 			rmem->ctx_mem = ctxm;
9230 			if (i == (nr_tbls - 1)) {
9231 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
9232 
9233 				if (rem)
9234 					rmem->nr_pages = rem;
9235 			}
9236 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
9237 			if (rc)
9238 				break;
9239 		}
9240 	} else {
9241 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9242 		if (rmem->nr_pages > 1 || depth)
9243 			rmem->depth = 1;
9244 		rmem->ctx_mem = ctxm;
9245 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9246 	}
9247 	return rc;
9248 }
9249 
9250 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
9251 				    struct bnxt_ctx_pg_info *ctx_pg,
9252 				    void *buf, size_t offset, size_t head,
9253 				    size_t tail)
9254 {
9255 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9256 	size_t nr_pages = ctx_pg->nr_pages;
9257 	int page_size = rmem->page_size;
9258 	size_t len = 0, total_len = 0;
9259 	u16 depth = rmem->depth;
9260 
9261 	tail %= nr_pages * page_size;
9262 	do {
9263 		if (depth > 1) {
9264 			int i = head / (page_size * MAX_CTX_PAGES);
9265 			struct bnxt_ctx_pg_info *pg_tbl;
9266 
9267 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9268 			rmem = &pg_tbl->ring_mem;
9269 		}
9270 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
9271 		head += len;
9272 		offset += len;
9273 		total_len += len;
9274 		if (head >= nr_pages * page_size)
9275 			head = 0;
9276 	} while (head != tail);
9277 	return total_len;
9278 }
9279 
9280 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
9281 				  struct bnxt_ctx_pg_info *ctx_pg)
9282 {
9283 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9284 
9285 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
9286 	    ctx_pg->ctx_pg_tbl) {
9287 		int i, nr_tbls = rmem->nr_pages;
9288 
9289 		for (i = 0; i < nr_tbls; i++) {
9290 			struct bnxt_ctx_pg_info *pg_tbl;
9291 			struct bnxt_ring_mem_info *rmem2;
9292 
9293 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9294 			if (!pg_tbl)
9295 				continue;
9296 			rmem2 = &pg_tbl->ring_mem;
9297 			bnxt_free_ring(bp, rmem2);
9298 			ctx_pg->ctx_pg_arr[i] = NULL;
9299 			kfree(pg_tbl);
9300 			ctx_pg->ctx_pg_tbl[i] = NULL;
9301 		}
9302 		kfree(ctx_pg->ctx_pg_tbl);
9303 		ctx_pg->ctx_pg_tbl = NULL;
9304 	}
9305 	bnxt_free_ring(bp, rmem);
9306 	ctx_pg->nr_pages = 0;
9307 }
9308 
9309 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
9310 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
9311 				   u8 pg_lvl)
9312 {
9313 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9314 	int i, rc = 0, n = 1;
9315 	u32 mem_size;
9316 
9317 	if (!ctxm->entry_size || !ctx_pg)
9318 		return -EINVAL;
9319 	if (ctxm->instance_bmap)
9320 		n = hweight32(ctxm->instance_bmap);
9321 	if (ctxm->entry_multiple)
9322 		entries = roundup(entries, ctxm->entry_multiple);
9323 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9324 	mem_size = entries * ctxm->entry_size;
9325 	for (i = 0; i < n && !rc; i++) {
9326 		ctx_pg[i].entries = entries;
9327 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9328 					    ctxm->init_value ? ctxm : NULL);
9329 	}
9330 	if (!rc)
9331 		ctxm->mem_valid = 1;
9332 	return rc;
9333 }
9334 
9335 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9336 					       struct bnxt_ctx_mem_type *ctxm,
9337 					       bool last)
9338 {
9339 	struct hwrm_func_backing_store_cfg_v2_input *req;
9340 	u32 instance_bmap = ctxm->instance_bmap;
9341 	int i, j, rc = 0, n = 1;
9342 	__le32 *p;
9343 
9344 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9345 		return 0;
9346 
9347 	if (instance_bmap)
9348 		n = hweight32(ctxm->instance_bmap);
9349 	else
9350 		instance_bmap = 1;
9351 
9352 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9353 	if (rc)
9354 		return rc;
9355 	hwrm_req_hold(bp, req);
9356 	req->type = cpu_to_le16(ctxm->type);
9357 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9358 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9359 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9360 		struct bnxt_bs_trace_info *bs_trace;
9361 		u32 enables;
9362 
9363 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9364 		req->enables = cpu_to_le32(enables);
9365 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9366 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9367 	}
9368 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9369 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9370 		p[i] = cpu_to_le32(ctxm->split[i]);
9371 	for (i = 0, j = 0; j < n && !rc; i++) {
9372 		struct bnxt_ctx_pg_info *ctx_pg;
9373 
9374 		if (!(instance_bmap & (1 << i)))
9375 			continue;
9376 		req->instance = cpu_to_le16(i);
9377 		ctx_pg = &ctxm->pg_info[j++];
9378 		if (!ctx_pg->entries)
9379 			continue;
9380 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9381 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9382 				      &req->page_size_pbl_level,
9383 				      &req->page_dir);
9384 		if (last && j == n)
9385 			req->flags =
9386 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9387 		rc = hwrm_req_send(bp, req);
9388 	}
9389 	hwrm_req_drop(bp, req);
9390 	return rc;
9391 }
9392 
9393 static int bnxt_backing_store_cfg_v2(struct bnxt *bp)
9394 {
9395 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9396 	struct bnxt_ctx_mem_type *ctxm;
9397 	u16 last_type = BNXT_CTX_INV;
9398 	int rc = 0;
9399 	u16 type;
9400 
9401 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_QPC; type++) {
9402 		ctxm = &ctx->ctx_arr[type];
9403 		if (!bnxt_bs_trace_avail(bp, type))
9404 			continue;
9405 		if (!ctxm->mem_valid) {
9406 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9407 						     ctxm->max_entries, 1);
9408 			if (rc) {
9409 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9410 					    type);
9411 				continue;
9412 			}
9413 			bnxt_bs_trace_init(bp, ctxm);
9414 		}
9415 		last_type = type;
9416 	}
9417 
9418 	if (last_type == BNXT_CTX_INV) {
9419 		for (type = 0; type < BNXT_CTX_MAX; type++) {
9420 			ctxm = &ctx->ctx_arr[type];
9421 			if (ctxm->mem_valid)
9422 				last_type = type;
9423 		}
9424 		if (last_type == BNXT_CTX_INV)
9425 			return 0;
9426 	}
9427 	ctx->ctx_arr[last_type].last = 1;
9428 
9429 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9430 		ctxm = &ctx->ctx_arr[type];
9431 
9432 		if (!ctxm->mem_valid)
9433 			continue;
9434 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9435 		if (rc)
9436 			return rc;
9437 	}
9438 	return 0;
9439 }
9440 
9441 /**
9442  * __bnxt_copy_ctx_mem - copy host context memory
9443  * @bp: The driver context
9444  * @ctxm: The pointer to the context memory type
9445  * @buf: The destination buffer or NULL to just obtain the length
9446  * @offset: The buffer offset to copy the data to
9447  * @head: The head offset of context memory to copy from
9448  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9449  *
9450  * This function is called for debugging purposes to dump the host context
9451  * used by the chip.
9452  *
9453  * Return: Length of memory copied
9454  */
9455 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9456 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9457 				  size_t offset, size_t head, size_t tail)
9458 {
9459 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9460 	size_t len = 0, total_len = 0;
9461 	int i, n = 1;
9462 
9463 	if (!ctx_pg)
9464 		return 0;
9465 
9466 	if (ctxm->instance_bmap)
9467 		n = hweight32(ctxm->instance_bmap);
9468 	for (i = 0; i < n; i++) {
9469 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9470 					    tail);
9471 		offset += len;
9472 		total_len += len;
9473 	}
9474 	return total_len;
9475 }
9476 
9477 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9478 			 void *buf, size_t offset)
9479 {
9480 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9481 
9482 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9483 }
9484 
9485 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9486 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9487 {
9488 	struct bnxt_ctx_pg_info *ctx_pg;
9489 	int i, n = 1;
9490 
9491 	ctxm->last = 0;
9492 
9493 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9494 		return;
9495 
9496 	ctx_pg = ctxm->pg_info;
9497 	if (ctx_pg) {
9498 		if (ctxm->instance_bmap)
9499 			n = hweight32(ctxm->instance_bmap);
9500 		for (i = 0; i < n; i++)
9501 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9502 
9503 		kfree(ctx_pg);
9504 		ctxm->pg_info = NULL;
9505 		ctxm->mem_valid = 0;
9506 	}
9507 	memset(ctxm, 0, sizeof(*ctxm));
9508 }
9509 
9510 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9511 {
9512 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9513 	u16 type;
9514 
9515 	if (!ctx)
9516 		return;
9517 
9518 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9519 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9520 
9521 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9522 	if (force) {
9523 		kfree(ctx);
9524 		bp->ctx = NULL;
9525 	}
9526 }
9527 
9528 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9529 {
9530 	struct bnxt_ctx_mem_type *ctxm;
9531 	struct bnxt_ctx_mem_info *ctx;
9532 	u32 l2_qps, qp1_qps, max_qps;
9533 	u32 ena, entries_sp, entries;
9534 	u32 srqs, max_srqs, min;
9535 	u32 num_mr, num_ah;
9536 	u32 extra_srqs = 0;
9537 	u32 extra_qps = 0;
9538 	u32 fast_qpmd_qps;
9539 	u8 pg_lvl = 1;
9540 	int i, rc;
9541 
9542 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9543 	if (rc) {
9544 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9545 			   rc);
9546 		return rc;
9547 	}
9548 	ctx = bp->ctx;
9549 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9550 		return 0;
9551 
9552 	ena = 0;
9553 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
9554 		goto skip_legacy;
9555 
9556 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9557 	l2_qps = ctxm->qp_l2_entries;
9558 	qp1_qps = ctxm->qp_qp1_entries;
9559 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9560 	max_qps = ctxm->max_entries;
9561 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9562 	srqs = ctxm->srq_l2_entries;
9563 	max_srqs = ctxm->max_entries;
9564 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9565 		pg_lvl = 2;
9566 		if (BNXT_SW_RES_LMT(bp)) {
9567 			extra_qps = max_qps - l2_qps - qp1_qps;
9568 			extra_srqs = max_srqs - srqs;
9569 		} else {
9570 			extra_qps = min_t(u32, 65536,
9571 					  max_qps - l2_qps - qp1_qps);
9572 			/* allocate extra qps if fw supports RoCE fast qp
9573 			 * destroy feature
9574 			 */
9575 			extra_qps += fast_qpmd_qps;
9576 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9577 		}
9578 		if (fast_qpmd_qps)
9579 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9580 	}
9581 
9582 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9583 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9584 				     pg_lvl);
9585 	if (rc)
9586 		return rc;
9587 
9588 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9589 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9590 	if (rc)
9591 		return rc;
9592 
9593 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9594 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9595 				     extra_qps * 2, pg_lvl);
9596 	if (rc)
9597 		return rc;
9598 
9599 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9600 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9601 	if (rc)
9602 		return rc;
9603 
9604 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9605 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9606 	if (rc)
9607 		return rc;
9608 
9609 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9610 		goto skip_rdma;
9611 
9612 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9613 	if (BNXT_SW_RES_LMT(bp) &&
9614 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9615 		num_ah = ctxm->mrav_av_entries;
9616 		num_mr = ctxm->max_entries - num_ah;
9617 	} else {
9618 		/* 128K extra is needed to accommodate static AH context
9619 		 * allocation by f/w.
9620 		 */
9621 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9622 		num_ah = min_t(u32, num_mr, 1024 * 128);
9623 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9624 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9625 			ctxm->mrav_av_entries = num_ah;
9626 	}
9627 
9628 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9629 	if (rc)
9630 		return rc;
9631 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9632 
9633 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9634 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9635 	if (rc)
9636 		return rc;
9637 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9638 
9639 skip_rdma:
9640 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9641 	min = ctxm->min_entries;
9642 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9643 		     2 * (extra_qps + qp1_qps) + min;
9644 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9645 	if (rc)
9646 		return rc;
9647 
9648 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9649 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9650 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9651 	if (rc)
9652 		return rc;
9653 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9654 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9655 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9656 
9657 skip_legacy:
9658 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9659 		rc = bnxt_backing_store_cfg_v2(bp);
9660 	else
9661 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9662 	if (rc) {
9663 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9664 			   rc);
9665 		return rc;
9666 	}
9667 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9668 	return 0;
9669 }
9670 
9671 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9672 {
9673 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9674 	u16 page_attr;
9675 	int rc;
9676 
9677 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9678 		return 0;
9679 
9680 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9681 	if (rc)
9682 		return rc;
9683 
9684 	if (BNXT_PAGE_SIZE == 0x2000)
9685 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9686 	else if (BNXT_PAGE_SIZE == 0x10000)
9687 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9688 	else
9689 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9690 	req->pg_size_lvl = cpu_to_le16(page_attr |
9691 				       bp->fw_crash_mem->ring_mem.depth);
9692 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9693 	req->size = cpu_to_le32(bp->fw_crash_len);
9694 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9695 	return hwrm_req_send(bp, req);
9696 }
9697 
9698 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9699 {
9700 	if (bp->fw_crash_mem) {
9701 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9702 		kfree(bp->fw_crash_mem);
9703 		bp->fw_crash_mem = NULL;
9704 	}
9705 }
9706 
9707 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9708 {
9709 	u32 mem_size = 0;
9710 	int rc;
9711 
9712 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9713 		return 0;
9714 
9715 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9716 	if (rc)
9717 		return rc;
9718 
9719 	mem_size = round_up(mem_size, 4);
9720 
9721 	/* keep and use the existing pages */
9722 	if (bp->fw_crash_mem &&
9723 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9724 		goto alloc_done;
9725 
9726 	if (bp->fw_crash_mem)
9727 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9728 	else
9729 		bp->fw_crash_mem = kzalloc_obj(*bp->fw_crash_mem);
9730 	if (!bp->fw_crash_mem)
9731 		return -ENOMEM;
9732 
9733 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9734 	if (rc) {
9735 		bnxt_free_crash_dump_mem(bp);
9736 		return rc;
9737 	}
9738 
9739 alloc_done:
9740 	bp->fw_crash_len = mem_size;
9741 	return 0;
9742 }
9743 
9744 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9745 {
9746 	struct hwrm_func_resource_qcaps_output *resp;
9747 	struct hwrm_func_resource_qcaps_input *req;
9748 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9749 	int rc;
9750 
9751 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9752 	if (rc)
9753 		return rc;
9754 
9755 	req->fid = cpu_to_le16(0xffff);
9756 	resp = hwrm_req_hold(bp, req);
9757 	rc = hwrm_req_send_silent(bp, req);
9758 	if (rc)
9759 		goto hwrm_func_resc_qcaps_exit;
9760 
9761 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9762 	if (!all)
9763 		goto hwrm_func_resc_qcaps_exit;
9764 
9765 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9766 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9767 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9768 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9769 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9770 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9771 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9772 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9773 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9774 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9775 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9776 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9777 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9778 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9779 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9780 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9781 
9782 	if (hw_resc->max_rsscos_ctxs >=
9783 	    hw_resc->max_vnics * BNXT_LARGE_RSS_TO_VNIC_RATIO)
9784 		bp->rss_cap |= BNXT_RSS_CAP_LARGE_RSS_CTX;
9785 
9786 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9787 		u16 max_msix = le16_to_cpu(resp->max_msix);
9788 
9789 		hw_resc->max_nqs = max_msix;
9790 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9791 	}
9792 
9793 	if (BNXT_PF(bp)) {
9794 		struct bnxt_pf_info *pf = &bp->pf;
9795 
9796 		pf->vf_resv_strategy =
9797 			le16_to_cpu(resp->vf_reservation_strategy);
9798 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9799 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9800 	}
9801 hwrm_func_resc_qcaps_exit:
9802 	hwrm_req_drop(bp, req);
9803 	return rc;
9804 }
9805 
9806 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9807 {
9808 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9809 	struct hwrm_port_mac_ptp_qcfg_input *req;
9810 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9811 	u8 flags;
9812 	int rc;
9813 
9814 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9815 		rc = -ENODEV;
9816 		goto no_ptp;
9817 	}
9818 
9819 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9820 	if (rc)
9821 		goto no_ptp;
9822 
9823 	req->port_id = cpu_to_le16(bp->pf.port_id);
9824 	resp = hwrm_req_hold(bp, req);
9825 	rc = hwrm_req_send(bp, req);
9826 	if (rc)
9827 		goto exit;
9828 
9829 	flags = resp->flags;
9830 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9831 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9832 		rc = -ENODEV;
9833 		goto exit;
9834 	}
9835 	if (!ptp) {
9836 		ptp = kzalloc_obj(*ptp);
9837 		if (!ptp) {
9838 			rc = -ENOMEM;
9839 			goto exit;
9840 		}
9841 		ptp->bp = bp;
9842 		bp->ptp_cfg = ptp;
9843 	}
9844 
9845 	if (flags &
9846 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9847 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9848 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9849 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9850 	} else if (BNXT_CHIP_P5(bp)) {
9851 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9852 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9853 	} else {
9854 		rc = -ENODEV;
9855 		goto exit;
9856 	}
9857 	ptp->rtc_configured =
9858 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9859 	rc = bnxt_ptp_init(bp);
9860 	if (rc)
9861 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9862 exit:
9863 	hwrm_req_drop(bp, req);
9864 	if (!rc)
9865 		return 0;
9866 
9867 no_ptp:
9868 	bnxt_ptp_clear(bp);
9869 	kfree(ptp);
9870 	bp->ptp_cfg = NULL;
9871 	return rc;
9872 }
9873 
9874 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9875 {
9876 	u32 flags, flags_ext, flags_ext2, flags_ext3;
9877 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9878 	struct hwrm_func_qcaps_output *resp;
9879 	struct hwrm_func_qcaps_input *req;
9880 	int rc;
9881 
9882 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9883 	if (rc)
9884 		return rc;
9885 
9886 	req->fid = cpu_to_le16(0xffff);
9887 	resp = hwrm_req_hold(bp, req);
9888 	rc = hwrm_req_send(bp, req);
9889 	if (rc)
9890 		goto hwrm_func_qcaps_exit;
9891 
9892 	flags = le32_to_cpu(resp->flags);
9893 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9894 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9895 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9896 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9897 	if (flags & FUNC_QCAPS_RESP_FLAGS_LINK_ADMIN_STATUS_SUPPORTED)
9898 		bp->fw_cap |= BNXT_FW_CAP_LINK_ADMIN;
9899 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9900 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9901 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9902 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9903 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9904 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9905 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9906 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9907 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9908 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9909 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9910 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9911 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9912 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9913 
9914 	flags_ext = le32_to_cpu(resp->flags_ext);
9915 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9916 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9917 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9918 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9919 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PTM_SUPPORTED)
9920 		bp->fw_cap |= BNXT_FW_CAP_PTP_PTM;
9921 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9922 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9923 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9924 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9925 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9926 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9927 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9928 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9929 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9930 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9931 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9932 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9933 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9934 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9935 
9936 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9937 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9938 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9939 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9940 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9941 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9942 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9943 	if (flags_ext2 &
9944 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9945 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9946 	if (BNXT_PF(bp) &&
9947 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9948 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9949 
9950 	flags_ext3 = le32_to_cpu(resp->flags_ext3);
9951 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_ROCE_VF_DYN_ALLOC_SUPPORT)
9952 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_DYN_ALLOC_SUPPORT;
9953 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_MIRROR_ON_ROCE_SUPPORTED)
9954 		bp->fw_cap |= BNXT_FW_CAP_MIRROR_ON_ROCE;
9955 
9956 	bp->tx_push_thresh = 0;
9957 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9958 	    BNXT_FW_MAJ(bp) > 217)
9959 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9960 
9961 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9962 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9963 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9964 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9965 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9966 	if (!hw_resc->max_hw_ring_grps)
9967 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9968 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9969 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9970 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9971 
9972 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9973 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9974 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9975 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9976 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9977 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9978 
9979 	if (BNXT_PF(bp)) {
9980 		struct bnxt_pf_info *pf = &bp->pf;
9981 
9982 		pf->fw_fid = le16_to_cpu(resp->fid);
9983 		pf->port_id = le16_to_cpu(resp->port_id);
9984 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9985 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9986 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9987 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9988 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9989 			bp->flags |= BNXT_FLAG_WOL_CAP;
9990 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9991 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9992 		} else {
9993 			bnxt_ptp_clear(bp);
9994 			kfree(bp->ptp_cfg);
9995 			bp->ptp_cfg = NULL;
9996 		}
9997 	} else {
9998 #ifdef CONFIG_BNXT_SRIOV
9999 		struct bnxt_vf_info *vf = &bp->vf;
10000 
10001 		vf->fw_fid = le16_to_cpu(resp->fid);
10002 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
10003 #endif
10004 	}
10005 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
10006 
10007 hwrm_func_qcaps_exit:
10008 	hwrm_req_drop(bp, req);
10009 	return rc;
10010 }
10011 
10012 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
10013 {
10014 	struct hwrm_dbg_qcaps_output *resp;
10015 	struct hwrm_dbg_qcaps_input *req;
10016 	int rc;
10017 
10018 	bp->fw_dbg_cap = 0;
10019 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
10020 		return;
10021 
10022 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
10023 	if (rc)
10024 		return;
10025 
10026 	req->fid = cpu_to_le16(0xffff);
10027 	resp = hwrm_req_hold(bp, req);
10028 	rc = hwrm_req_send(bp, req);
10029 	if (rc)
10030 		goto hwrm_dbg_qcaps_exit;
10031 
10032 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
10033 
10034 hwrm_dbg_qcaps_exit:
10035 	hwrm_req_drop(bp, req);
10036 }
10037 
10038 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
10039 
10040 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
10041 {
10042 	int rc;
10043 
10044 	rc = __bnxt_hwrm_func_qcaps(bp);
10045 	if (rc)
10046 		return rc;
10047 
10048 	bnxt_hwrm_dbg_qcaps(bp);
10049 
10050 	rc = bnxt_hwrm_queue_qportcfg(bp);
10051 	if (rc) {
10052 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
10053 		return rc;
10054 	}
10055 	if (bp->hwrm_spec_code >= 0x10803) {
10056 		rc = bnxt_alloc_ctx_mem(bp);
10057 		if (rc)
10058 			return rc;
10059 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
10060 		if (!rc)
10061 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
10062 	}
10063 	return 0;
10064 }
10065 
10066 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
10067 {
10068 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
10069 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
10070 	u32 flags;
10071 	int rc;
10072 
10073 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
10074 		return 0;
10075 
10076 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
10077 	if (rc)
10078 		return rc;
10079 
10080 	resp = hwrm_req_hold(bp, req);
10081 	rc = hwrm_req_send(bp, req);
10082 	if (rc)
10083 		goto hwrm_cfa_adv_qcaps_exit;
10084 
10085 	flags = le32_to_cpu(resp->flags);
10086 	if (flags &
10087 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
10088 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
10089 
10090 	if (flags &
10091 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
10092 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
10093 
10094 	if (flags &
10095 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
10096 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
10097 
10098 hwrm_cfa_adv_qcaps_exit:
10099 	hwrm_req_drop(bp, req);
10100 	return rc;
10101 }
10102 
10103 static int __bnxt_alloc_fw_health(struct bnxt *bp)
10104 {
10105 	if (bp->fw_health)
10106 		return 0;
10107 
10108 	bp->fw_health = kzalloc_obj(*bp->fw_health);
10109 	if (!bp->fw_health)
10110 		return -ENOMEM;
10111 
10112 	mutex_init(&bp->fw_health->lock);
10113 	return 0;
10114 }
10115 
10116 static int bnxt_alloc_fw_health(struct bnxt *bp)
10117 {
10118 	int rc;
10119 
10120 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
10121 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10122 		return 0;
10123 
10124 	rc = __bnxt_alloc_fw_health(bp);
10125 	if (rc) {
10126 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
10127 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10128 		return rc;
10129 	}
10130 
10131 	return 0;
10132 }
10133 
10134 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
10135 {
10136 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
10137 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
10138 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
10139 }
10140 
10141 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
10142 {
10143 	struct bnxt_fw_health *fw_health = bp->fw_health;
10144 	u32 reg_type;
10145 
10146 	if (!fw_health)
10147 		return;
10148 
10149 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
10150 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
10151 		fw_health->status_reliable = false;
10152 
10153 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
10154 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
10155 		fw_health->resets_reliable = false;
10156 }
10157 
10158 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
10159 {
10160 	void __iomem *hs;
10161 	u32 status_loc;
10162 	u32 reg_type;
10163 	u32 sig;
10164 
10165 	if (bp->fw_health)
10166 		bp->fw_health->status_reliable = false;
10167 
10168 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
10169 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
10170 
10171 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
10172 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
10173 		if (!bp->chip_num) {
10174 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
10175 			bp->chip_num = readl(bp->bar0 +
10176 					     BNXT_FW_HEALTH_WIN_BASE +
10177 					     BNXT_GRC_REG_CHIP_NUM);
10178 		}
10179 		if (!BNXT_CHIP_P5_PLUS(bp))
10180 			return;
10181 
10182 		status_loc = BNXT_GRC_REG_STATUS_P5 |
10183 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
10184 	} else {
10185 		status_loc = readl(hs + offsetof(struct hcomm_status,
10186 						 fw_status_loc));
10187 	}
10188 
10189 	if (__bnxt_alloc_fw_health(bp)) {
10190 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
10191 		return;
10192 	}
10193 
10194 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
10195 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
10196 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
10197 		__bnxt_map_fw_health_reg(bp, status_loc);
10198 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
10199 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
10200 	}
10201 
10202 	bp->fw_health->status_reliable = true;
10203 }
10204 
10205 static int bnxt_map_fw_health_regs(struct bnxt *bp)
10206 {
10207 	struct bnxt_fw_health *fw_health = bp->fw_health;
10208 	u32 reg_base = 0xffffffff;
10209 	int i;
10210 
10211 	bp->fw_health->status_reliable = false;
10212 	bp->fw_health->resets_reliable = false;
10213 	/* Only pre-map the monitoring GRC registers using window 3 */
10214 	for (i = 0; i < 4; i++) {
10215 		u32 reg = fw_health->regs[i];
10216 
10217 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
10218 			continue;
10219 		if (reg_base == 0xffffffff)
10220 			reg_base = reg & BNXT_GRC_BASE_MASK;
10221 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
10222 			return -ERANGE;
10223 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
10224 	}
10225 	bp->fw_health->status_reliable = true;
10226 	bp->fw_health->resets_reliable = true;
10227 	if (reg_base == 0xffffffff)
10228 		return 0;
10229 
10230 	__bnxt_map_fw_health_reg(bp, reg_base);
10231 	return 0;
10232 }
10233 
10234 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
10235 {
10236 	if (!bp->fw_health)
10237 		return;
10238 
10239 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
10240 		bp->fw_health->status_reliable = true;
10241 		bp->fw_health->resets_reliable = true;
10242 	} else {
10243 		bnxt_try_map_fw_health_reg(bp);
10244 	}
10245 }
10246 
10247 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
10248 {
10249 	struct bnxt_fw_health *fw_health = bp->fw_health;
10250 	struct hwrm_error_recovery_qcfg_output *resp;
10251 	struct hwrm_error_recovery_qcfg_input *req;
10252 	int rc, i;
10253 
10254 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10255 		return 0;
10256 
10257 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
10258 	if (rc)
10259 		return rc;
10260 
10261 	resp = hwrm_req_hold(bp, req);
10262 	rc = hwrm_req_send(bp, req);
10263 	if (rc)
10264 		goto err_recovery_out;
10265 	fw_health->flags = le32_to_cpu(resp->flags);
10266 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
10267 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
10268 		rc = -EINVAL;
10269 		goto err_recovery_out;
10270 	}
10271 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
10272 	fw_health->master_func_wait_dsecs =
10273 		le32_to_cpu(resp->master_func_wait_period);
10274 	fw_health->normal_func_wait_dsecs =
10275 		le32_to_cpu(resp->normal_func_wait_period);
10276 	fw_health->post_reset_wait_dsecs =
10277 		le32_to_cpu(resp->master_func_wait_period_after_reset);
10278 	fw_health->post_reset_max_wait_dsecs =
10279 		le32_to_cpu(resp->max_bailout_time_after_reset);
10280 	fw_health->regs[BNXT_FW_HEALTH_REG] =
10281 		le32_to_cpu(resp->fw_health_status_reg);
10282 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
10283 		le32_to_cpu(resp->fw_heartbeat_reg);
10284 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
10285 		le32_to_cpu(resp->fw_reset_cnt_reg);
10286 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
10287 		le32_to_cpu(resp->reset_inprogress_reg);
10288 	fw_health->fw_reset_inprog_reg_mask =
10289 		le32_to_cpu(resp->reset_inprogress_reg_mask);
10290 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
10291 	if (fw_health->fw_reset_seq_cnt >= 16) {
10292 		rc = -EINVAL;
10293 		goto err_recovery_out;
10294 	}
10295 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
10296 		fw_health->fw_reset_seq_regs[i] =
10297 			le32_to_cpu(resp->reset_reg[i]);
10298 		fw_health->fw_reset_seq_vals[i] =
10299 			le32_to_cpu(resp->reset_reg_val[i]);
10300 		fw_health->fw_reset_seq_delay_msec[i] =
10301 			resp->delay_after_reset[i];
10302 	}
10303 err_recovery_out:
10304 	hwrm_req_drop(bp, req);
10305 	if (!rc)
10306 		rc = bnxt_map_fw_health_regs(bp);
10307 	if (rc)
10308 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10309 	return rc;
10310 }
10311 
10312 static int bnxt_hwrm_func_reset(struct bnxt *bp)
10313 {
10314 	struct hwrm_func_reset_input *req;
10315 	int rc;
10316 
10317 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
10318 	if (rc)
10319 		return rc;
10320 
10321 	req->enables = 0;
10322 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
10323 	return hwrm_req_send(bp, req);
10324 }
10325 
10326 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
10327 {
10328 	struct hwrm_nvm_get_dev_info_output nvm_info;
10329 
10330 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
10331 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10332 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10333 			 nvm_info.nvm_cfg_ver_upd);
10334 }
10335 
10336 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10337 {
10338 	struct hwrm_queue_qportcfg_output *resp;
10339 	struct hwrm_queue_qportcfg_input *req;
10340 	u8 i, j, *qptr;
10341 	bool no_rdma;
10342 	int rc = 0;
10343 
10344 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10345 	if (rc)
10346 		return rc;
10347 
10348 	resp = hwrm_req_hold(bp, req);
10349 	rc = hwrm_req_send(bp, req);
10350 	if (rc)
10351 		goto qportcfg_exit;
10352 
10353 	if (!resp->max_configurable_queues) {
10354 		rc = -EINVAL;
10355 		goto qportcfg_exit;
10356 	}
10357 	bp->max_tc = resp->max_configurable_queues;
10358 	bp->max_lltc = resp->max_configurable_lossless_queues;
10359 	if (bp->max_tc > BNXT_MAX_QUEUE)
10360 		bp->max_tc = BNXT_MAX_QUEUE;
10361 
10362 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10363 	qptr = &resp->queue_id0;
10364 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10365 		bp->q_info[j].queue_id = *qptr;
10366 		bp->q_ids[i] = *qptr++;
10367 		bp->q_info[j].queue_profile = *qptr++;
10368 		bp->tc_to_qidx[j] = j;
10369 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10370 		    (no_rdma && BNXT_PF(bp)))
10371 			j++;
10372 	}
10373 	bp->max_q = bp->max_tc;
10374 	bp->max_tc = max_t(u8, j, 1);
10375 
10376 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10377 		bp->max_tc = 1;
10378 
10379 	if (bp->max_lltc > bp->max_tc)
10380 		bp->max_lltc = bp->max_tc;
10381 
10382 qportcfg_exit:
10383 	hwrm_req_drop(bp, req);
10384 	return rc;
10385 }
10386 
10387 static int bnxt_hwrm_poll(struct bnxt *bp)
10388 {
10389 	struct hwrm_ver_get_input *req;
10390 	int rc;
10391 
10392 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10393 	if (rc)
10394 		return rc;
10395 
10396 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10397 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10398 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10399 
10400 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10401 	rc = hwrm_req_send(bp, req);
10402 	return rc;
10403 }
10404 
10405 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10406 {
10407 	struct hwrm_ver_get_output *resp;
10408 	struct hwrm_ver_get_input *req;
10409 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10410 	u32 dev_caps_cfg, hwrm_ver;
10411 	int rc, len, max_tmo_secs;
10412 
10413 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10414 	if (rc)
10415 		return rc;
10416 
10417 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10418 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10419 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10420 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10421 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10422 
10423 	resp = hwrm_req_hold(bp, req);
10424 	rc = hwrm_req_send(bp, req);
10425 	if (rc)
10426 		goto hwrm_ver_get_exit;
10427 
10428 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10429 
10430 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10431 			     resp->hwrm_intf_min_8b << 8 |
10432 			     resp->hwrm_intf_upd_8b;
10433 	if (resp->hwrm_intf_maj_8b < 1) {
10434 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10435 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10436 			    resp->hwrm_intf_upd_8b);
10437 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10438 	}
10439 
10440 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10441 			HWRM_VERSION_UPDATE;
10442 
10443 	if (bp->hwrm_spec_code > hwrm_ver)
10444 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10445 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10446 			 HWRM_VERSION_UPDATE);
10447 	else
10448 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10449 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10450 			 resp->hwrm_intf_upd_8b);
10451 
10452 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10453 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10454 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10455 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10456 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10457 		len = FW_VER_STR_LEN;
10458 	} else {
10459 		fw_maj = resp->hwrm_fw_maj_8b;
10460 		fw_min = resp->hwrm_fw_min_8b;
10461 		fw_bld = resp->hwrm_fw_bld_8b;
10462 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10463 		len = BC_HWRM_STR_LEN;
10464 	}
10465 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10466 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10467 		 fw_rsv);
10468 
10469 	if (strlen(resp->active_pkg_name)) {
10470 		int fw_ver_len = strlen(bp->fw_ver_str);
10471 
10472 		snprintf(bp->fw_ver_str + fw_ver_len,
10473 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10474 			 resp->active_pkg_name);
10475 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10476 	}
10477 
10478 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10479 	if (!bp->hwrm_cmd_timeout)
10480 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10481 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10482 	if (!bp->hwrm_cmd_max_timeout)
10483 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10484 	max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000;
10485 #ifdef CONFIG_DETECT_HUNG_TASK
10486 	if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT ||
10487 	    max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) {
10488 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n",
10489 			    max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT);
10490 	}
10491 #endif
10492 
10493 	if (resp->hwrm_intf_maj_8b >= 1) {
10494 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10495 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10496 	}
10497 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10498 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10499 
10500 	bp->chip_num = le16_to_cpu(resp->chip_num);
10501 	bp->chip_rev = resp->chip_rev;
10502 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10503 	    !resp->chip_metal)
10504 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10505 
10506 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10507 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10508 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10509 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10510 
10511 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10512 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10513 
10514 	if (dev_caps_cfg &
10515 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10516 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10517 
10518 	if (dev_caps_cfg &
10519 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10520 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10521 
10522 	if (dev_caps_cfg &
10523 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10524 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10525 
10526 hwrm_ver_get_exit:
10527 	hwrm_req_drop(bp, req);
10528 	return rc;
10529 }
10530 
10531 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10532 {
10533 	struct hwrm_fw_set_time_input *req;
10534 	struct tm tm;
10535 	time64_t now = ktime_get_real_seconds();
10536 	int rc;
10537 
10538 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10539 	    bp->hwrm_spec_code < 0x10400)
10540 		return -EOPNOTSUPP;
10541 
10542 	time64_to_tm(now, 0, &tm);
10543 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10544 	if (rc)
10545 		return rc;
10546 
10547 	req->year = cpu_to_le16(1900 + tm.tm_year);
10548 	req->month = 1 + tm.tm_mon;
10549 	req->day = tm.tm_mday;
10550 	req->hour = tm.tm_hour;
10551 	req->minute = tm.tm_min;
10552 	req->second = tm.tm_sec;
10553 	return hwrm_req_send(bp, req);
10554 }
10555 
10556 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10557 {
10558 	u64 sw_tmp;
10559 
10560 	hw &= mask;
10561 	sw_tmp = (*sw & ~mask) | hw;
10562 	if (hw < (*sw & mask))
10563 		sw_tmp += mask + 1;
10564 	WRITE_ONCE(*sw, sw_tmp);
10565 }
10566 
10567 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10568 				    int count, bool ignore_zero)
10569 {
10570 	int i;
10571 
10572 	for (i = 0; i < count; i++) {
10573 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10574 
10575 		if (ignore_zero && !hw)
10576 			continue;
10577 
10578 		if (masks[i] == -1ULL)
10579 			sw_stats[i] = hw;
10580 		else
10581 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10582 	}
10583 }
10584 
10585 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10586 {
10587 	if (!stats->hw_stats)
10588 		return;
10589 
10590 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10591 				stats->hw_masks, stats->len / 8, false);
10592 }
10593 
10594 static void bnxt_accumulate_ring_stats(struct bnxt *bp)
10595 {
10596 	struct bnxt_stats_mem *ring0_stats;
10597 	bool ignore_zero = false;
10598 	int i;
10599 
10600 	/* Chip bug.  Counter intermittently becomes 0. */
10601 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10602 		ignore_zero = true;
10603 
10604 	for (i = 0; i < bp->cp_nr_rings; i++) {
10605 		struct bnxt_napi *bnapi = bp->bnapi[i];
10606 		struct bnxt_cp_ring_info *cpr;
10607 		struct bnxt_stats_mem *stats;
10608 
10609 		cpr = &bnapi->cp_ring;
10610 		stats = &cpr->stats;
10611 		if (!i)
10612 			ring0_stats = stats;
10613 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10614 					ring0_stats->hw_masks,
10615 					ring0_stats->len / 8, ignore_zero);
10616 	}
10617 }
10618 
10619 static void bnxt_accumulate_port_stats(struct bnxt *bp)
10620 {
10621 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10622 		struct bnxt_stats_mem *stats = &bp->port_stats;
10623 		__le64 *hw_stats = stats->hw_stats;
10624 		u64 *sw_stats = stats->sw_stats;
10625 		u64 *masks = stats->hw_masks;
10626 		int cnt;
10627 
10628 		cnt = sizeof(struct rx_port_stats) / 8;
10629 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10630 
10631 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10632 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10633 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10634 		cnt = sizeof(struct tx_port_stats) / 8;
10635 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10636 	}
10637 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10638 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10639 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10640 	}
10641 }
10642 
10643 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10644 {
10645 	bnxt_accumulate_ring_stats(bp);
10646 	bnxt_accumulate_port_stats(bp);
10647 }
10648 
10649 /* Re-accumulate ring stats from DMA buffers if stale.
10650  * uAPIs for reading sw_stats should call this first.
10651  *
10652  * We promise user space update frequency of bp->stats_coal_ticks but
10653  * the update is a two step process - first device updates the DMA buffer,
10654  * then we have to update from that buffer to driver stats in the service work.
10655  * Worst case we would be 2x off from the desired frequency.
10656  * Sync the stats sooner, if stale. The 20% threshold was chosen arbitrarily.
10657  *
10658  * Ideally we would split the user-configured time into two portions,
10659  * i.e. also lower the DMA period by the 20%. But the DMA timer seems to have
10660  * too coarse granularity to play such tricks.
10661  */
10662 void bnxt_sync_ring_stats(struct bnxt *bp)
10663 {
10664 	unsigned long stale;
10665 
10666 	if (!netif_running(bp->dev) || !bp->stats_coal_ticks)
10667 		return;
10668 
10669 	spin_lock(&bp->stats_lock);
10670 	stale = usecs_to_jiffies(bp->stats_coal_ticks / 5);
10671 	if (time_after_eq(jiffies, bp->stats_updated_jiffies + stale)) {
10672 		bnxt_accumulate_ring_stats(bp);
10673 		bp->stats_updated_jiffies = jiffies;
10674 	}
10675 	spin_unlock(&bp->stats_lock);
10676 }
10677 
10678 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10679 {
10680 	struct hwrm_port_qstats_input *req;
10681 	struct bnxt_pf_info *pf = &bp->pf;
10682 	int rc;
10683 
10684 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10685 		return 0;
10686 
10687 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10688 		return -EOPNOTSUPP;
10689 
10690 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10691 	if (rc)
10692 		return rc;
10693 
10694 	req->flags = flags;
10695 	req->port_id = cpu_to_le16(pf->port_id);
10696 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10697 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10698 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10699 	return hwrm_req_send(bp, req);
10700 }
10701 
10702 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10703 {
10704 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10705 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10706 	struct hwrm_port_qstats_ext_output *resp_qs;
10707 	struct hwrm_port_qstats_ext_input *req_qs;
10708 	struct bnxt_pf_info *pf = &bp->pf;
10709 	u32 tx_stat_size;
10710 	int rc;
10711 
10712 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10713 		return 0;
10714 
10715 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10716 		return -EOPNOTSUPP;
10717 
10718 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10719 	if (rc)
10720 		return rc;
10721 
10722 	req_qs->flags = flags;
10723 	req_qs->port_id = cpu_to_le16(pf->port_id);
10724 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10725 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10726 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10727 		       sizeof(struct tx_port_stats_ext) : 0;
10728 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10729 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10730 	resp_qs = hwrm_req_hold(bp, req_qs);
10731 	rc = hwrm_req_send(bp, req_qs);
10732 	if (!rc) {
10733 		bp->fw_rx_stats_ext_size =
10734 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10735 		if (BNXT_FW_MAJ(bp) < 220 &&
10736 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10737 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10738 
10739 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10740 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10741 	} else {
10742 		bp->fw_rx_stats_ext_size = 0;
10743 		bp->fw_tx_stats_ext_size = 0;
10744 	}
10745 	hwrm_req_drop(bp, req_qs);
10746 
10747 	if (flags)
10748 		return rc;
10749 
10750 	if (bp->fw_tx_stats_ext_size <=
10751 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10752 		bp->pri2cos_valid = 0;
10753 		return rc;
10754 	}
10755 
10756 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10757 	if (rc)
10758 		return rc;
10759 
10760 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10761 
10762 	resp_qc = hwrm_req_hold(bp, req_qc);
10763 	rc = hwrm_req_send(bp, req_qc);
10764 	if (!rc) {
10765 		u8 *pri2cos;
10766 		int i, j;
10767 
10768 		pri2cos = &resp_qc->pri0_cos_queue_id;
10769 		for (i = 0; i < 8; i++) {
10770 			u8 queue_id = pri2cos[i];
10771 			u8 queue_idx;
10772 
10773 			/* Per port queue IDs start from 0, 10, 20, etc */
10774 			queue_idx = queue_id % 10;
10775 			if (queue_idx > BNXT_MAX_QUEUE) {
10776 				bp->pri2cos_valid = false;
10777 				hwrm_req_drop(bp, req_qc);
10778 				return rc;
10779 			}
10780 			for (j = 0; j < bp->max_q; j++) {
10781 				if (bp->q_ids[j] == queue_id)
10782 					bp->pri2cos_idx[i] = queue_idx;
10783 			}
10784 		}
10785 		bp->pri2cos_valid = true;
10786 	}
10787 	hwrm_req_drop(bp, req_qc);
10788 
10789 	return rc;
10790 }
10791 
10792 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10793 {
10794 	bnxt_hwrm_tunnel_dst_port_free(bp,
10795 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10796 	bnxt_hwrm_tunnel_dst_port_free(bp,
10797 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10798 }
10799 
10800 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10801 {
10802 	int rc, i;
10803 	u32 tpa_flags = 0;
10804 
10805 	if (set_tpa)
10806 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10807 	else if (BNXT_NO_FW_ACCESS(bp))
10808 		return 0;
10809 	for (i = 0; i < bp->nr_vnics; i++) {
10810 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10811 		if (rc) {
10812 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10813 				   i, rc);
10814 			return rc;
10815 		}
10816 	}
10817 	return 0;
10818 }
10819 
10820 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10821 {
10822 	int i;
10823 
10824 	for (i = 0; i < bp->nr_vnics; i++)
10825 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10826 }
10827 
10828 static void bnxt_clear_vnic(struct bnxt *bp)
10829 {
10830 	if (!bp->vnic_info)
10831 		return;
10832 
10833 	bnxt_hwrm_clear_vnic_filter(bp);
10834 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10835 		/* clear all RSS setting before free vnic ctx */
10836 		bnxt_hwrm_clear_vnic_rss(bp);
10837 		bnxt_hwrm_vnic_ctx_free(bp);
10838 	}
10839 	/* before free the vnic, undo the vnic tpa settings */
10840 	if (bp->flags & BNXT_FLAG_TPA)
10841 		bnxt_set_tpa(bp, false);
10842 	bnxt_hwrm_vnic_free(bp);
10843 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10844 		bnxt_hwrm_vnic_ctx_free(bp);
10845 }
10846 
10847 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10848 				    bool irq_re_init)
10849 {
10850 	bnxt_clear_vnic(bp);
10851 	bnxt_hwrm_ring_free(bp, close_path);
10852 	bnxt_hwrm_ring_grp_free(bp);
10853 	if (irq_re_init) {
10854 		bnxt_hwrm_stat_ctx_free(bp);
10855 		bnxt_hwrm_free_tunnel_ports(bp);
10856 	}
10857 }
10858 
10859 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10860 {
10861 	struct hwrm_func_cfg_input *req;
10862 	u8 evb_mode;
10863 	int rc;
10864 
10865 	if (br_mode == BRIDGE_MODE_VEB)
10866 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10867 	else if (br_mode == BRIDGE_MODE_VEPA)
10868 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10869 	else
10870 		return -EINVAL;
10871 
10872 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10873 	if (rc)
10874 		return rc;
10875 
10876 	req->fid = cpu_to_le16(0xffff);
10877 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10878 	req->evb_mode = evb_mode;
10879 	return hwrm_req_send(bp, req);
10880 }
10881 
10882 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10883 {
10884 	struct hwrm_func_cfg_input *req;
10885 	int rc;
10886 
10887 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10888 		return 0;
10889 
10890 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10891 	if (rc)
10892 		return rc;
10893 
10894 	req->fid = cpu_to_le16(0xffff);
10895 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10896 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10897 	if (size == 128)
10898 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10899 
10900 	return hwrm_req_send(bp, req);
10901 }
10902 
10903 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10904 {
10905 	int rc;
10906 
10907 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10908 		goto skip_rss_ctx;
10909 
10910 	/* allocate context for vnic */
10911 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10912 	if (rc) {
10913 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10914 			   vnic->vnic_id, rc);
10915 		goto vnic_setup_err;
10916 	}
10917 	bp->rsscos_nr_ctxs++;
10918 
10919 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10920 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10921 		if (rc) {
10922 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10923 				   vnic->vnic_id, rc);
10924 			goto vnic_setup_err;
10925 		}
10926 		bp->rsscos_nr_ctxs++;
10927 	}
10928 
10929 skip_rss_ctx:
10930 	bnxt_fill_hw_rss_tbl(bp, vnic);
10931 	/* configure default vnic, ring grp */
10932 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10933 	if (rc) {
10934 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10935 			   vnic->vnic_id, rc);
10936 		goto vnic_setup_err;
10937 	}
10938 
10939 	/* Enable RSS hashing on vnic */
10940 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10941 	if (rc) {
10942 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10943 			   vnic->vnic_id, rc);
10944 		goto vnic_setup_err;
10945 	}
10946 
10947 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10948 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10949 		if (rc) {
10950 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10951 				   vnic->vnic_id, rc);
10952 		}
10953 	}
10954 
10955 vnic_setup_err:
10956 	return rc;
10957 }
10958 
10959 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10960 			  u8 valid)
10961 {
10962 	struct hwrm_vnic_update_input *req;
10963 	int rc;
10964 
10965 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10966 	if (rc)
10967 		return rc;
10968 
10969 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10970 
10971 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10972 		req->mru = cpu_to_le16(vnic->mru);
10973 
10974 	req->enables = cpu_to_le32(valid);
10975 
10976 	return hwrm_req_send(bp, req);
10977 }
10978 
10979 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10980 {
10981 	int rc;
10982 
10983 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10984 	if (rc) {
10985 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10986 			   vnic->vnic_id, rc);
10987 		return rc;
10988 	}
10989 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10990 	if (rc)
10991 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10992 			   vnic->vnic_id, rc);
10993 	return rc;
10994 }
10995 
10996 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10997 {
10998 	int rc, i, nr_ctxs;
10999 
11000 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
11001 	for (i = 0; i < nr_ctxs; i++) {
11002 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
11003 		if (rc) {
11004 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
11005 				   vnic->vnic_id, i, rc);
11006 			break;
11007 		}
11008 		bp->rsscos_nr_ctxs++;
11009 	}
11010 	if (i < nr_ctxs)
11011 		return -ENOMEM;
11012 
11013 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
11014 	if (rc)
11015 		return rc;
11016 
11017 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
11018 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
11019 		if (rc) {
11020 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
11021 				   vnic->vnic_id, rc);
11022 		}
11023 	}
11024 	return rc;
11025 }
11026 
11027 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
11028 {
11029 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11030 		return __bnxt_setup_vnic_p5(bp, vnic);
11031 	else
11032 		return __bnxt_setup_vnic(bp, vnic);
11033 }
11034 
11035 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
11036 				     struct bnxt_vnic_info *vnic,
11037 				     u16 start_rx_ring_idx, int rx_rings)
11038 {
11039 	int rc;
11040 
11041 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
11042 	if (rc) {
11043 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
11044 			   vnic->vnic_id, rc);
11045 		return rc;
11046 	}
11047 	return bnxt_setup_vnic(bp, vnic);
11048 }
11049 
11050 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
11051 {
11052 	struct bnxt_vnic_info *vnic;
11053 	int i, rc = 0;
11054 
11055 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
11056 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
11057 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
11058 	}
11059 
11060 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11061 		return 0;
11062 
11063 	for (i = 0; i < bp->rx_nr_rings; i++) {
11064 		u16 vnic_id = i + 1;
11065 		u16 ring_id = i;
11066 
11067 		if (vnic_id >= bp->nr_vnics)
11068 			break;
11069 
11070 		vnic = &bp->vnic_info[vnic_id];
11071 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
11072 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
11073 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
11074 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
11075 			break;
11076 	}
11077 	return rc;
11078 }
11079 
11080 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
11081 			  bool all)
11082 {
11083 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11084 	struct bnxt_filter_base *usr_fltr, *tmp;
11085 	struct bnxt_ntuple_filter *ntp_fltr;
11086 	int i;
11087 
11088 	bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
11089 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
11090 		if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
11091 			bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
11092 	}
11093 	if (!all)
11094 		return;
11095 
11096 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
11097 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
11098 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
11099 			ntp_fltr = container_of(usr_fltr,
11100 						struct bnxt_ntuple_filter,
11101 						base);
11102 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
11103 			bnxt_del_ntp_filter(bp, ntp_fltr);
11104 			bnxt_del_one_usr_fltr(bp, usr_fltr);
11105 		}
11106 	}
11107 
11108 	if (vnic->rss_table)
11109 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
11110 				  vnic->rss_table,
11111 				  vnic->rss_table_dma_addr);
11112 	bp->num_rss_ctx--;
11113 }
11114 
11115 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic,
11116 				  int rxr_id)
11117 {
11118 	u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
11119 	int i, vnic_rx;
11120 
11121 	/* Ntuple VNIC always has all the rx rings. Any change of ring id
11122 	 * must be updated because a future filter may use it.
11123 	 */
11124 	if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
11125 		return true;
11126 
11127 	for (i = 0; i < tbl_size; i++) {
11128 		if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
11129 			vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
11130 		else
11131 			vnic_rx = bp->rss_indir_tbl[i];
11132 
11133 		if (rxr_id == vnic_rx)
11134 			return true;
11135 	}
11136 
11137 	return false;
11138 }
11139 
11140 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic,
11141 				u16 mru, int rxr_id)
11142 {
11143 	int rc;
11144 
11145 	if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id))
11146 		return 0;
11147 
11148 	if (mru) {
11149 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
11150 		if (rc) {
11151 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
11152 				   vnic->vnic_id, rc);
11153 			return rc;
11154 		}
11155 		if (rxr_id == vnic->default_rx_ring) {
11156 			rc = bnxt_hwrm_vnic_cfg(bp, vnic);
11157 			if (rc)
11158 				return rc;
11159 		}
11160 	}
11161 	vnic->mru = mru;
11162 	bnxt_hwrm_vnic_update(bp, vnic,
11163 			      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
11164 
11165 	return 0;
11166 }
11167 
11168 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id)
11169 {
11170 	struct ethtool_rxfh_context *ctx;
11171 	unsigned long context;
11172 	int rc;
11173 
11174 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11175 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11176 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11177 
11178 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id);
11179 		if (rc)
11180 			return rc;
11181 	}
11182 
11183 	return 0;
11184 }
11185 
11186 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
11187 {
11188 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
11189 	struct ethtool_rxfh_context *ctx;
11190 	unsigned long context;
11191 
11192 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11193 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11194 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11195 
11196 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
11197 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
11198 		    __bnxt_setup_vnic_p5(bp, vnic)) {
11199 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
11200 				   rss_ctx->index);
11201 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
11202 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
11203 		}
11204 	}
11205 }
11206 
11207 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
11208 {
11209 	struct ethtool_rxfh_context *ctx;
11210 	unsigned long context;
11211 
11212 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11213 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11214 
11215 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
11216 	}
11217 }
11218 
11219 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
11220 static bool bnxt_promisc_ok(struct bnxt *bp)
11221 {
11222 #ifdef CONFIG_BNXT_SRIOV
11223 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
11224 		return false;
11225 #endif
11226 	return true;
11227 }
11228 
11229 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
11230 {
11231 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
11232 	unsigned int rc = 0;
11233 
11234 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
11235 	if (rc) {
11236 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11237 			   rc);
11238 		return rc;
11239 	}
11240 
11241 	/* Setup the proper default RX ring */
11242 	bnxt_fill_hw_rss_tbl(bp, vnic);
11243 
11244 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
11245 	if (rc) {
11246 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11247 			   rc);
11248 		return rc;
11249 	}
11250 	return rc;
11251 }
11252 
11253 static int bnxt_cfg_rx_mode(struct bnxt *, struct netdev_hw_addr_list *, bool);
11254 static bool bnxt_mc_list_updated(struct bnxt *, u32 *,
11255 				 const struct netdev_hw_addr_list *);
11256 
11257 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
11258 {
11259 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
11260 	int rc = 0;
11261 	unsigned int rx_nr_rings = bp->rx_nr_rings;
11262 
11263 	if (irq_re_init) {
11264 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
11265 		if (rc) {
11266 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
11267 				   rc);
11268 			goto err_out;
11269 		}
11270 	}
11271 
11272 	rc = bnxt_hwrm_ring_alloc(bp);
11273 	if (rc) {
11274 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
11275 		goto err_out;
11276 	}
11277 
11278 	rc = bnxt_hwrm_ring_grp_alloc(bp);
11279 	if (rc) {
11280 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
11281 		goto err_out;
11282 	}
11283 
11284 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11285 		rx_nr_rings--;
11286 
11287 	/* default vnic 0 */
11288 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
11289 	if (rc) {
11290 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
11291 		goto err_out;
11292 	}
11293 
11294 	if (BNXT_VF(bp))
11295 		bnxt_hwrm_func_qcfg(bp);
11296 
11297 	rc = bnxt_setup_vnic(bp, vnic);
11298 	if (rc)
11299 		goto err_out;
11300 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
11301 		bnxt_hwrm_update_rss_hash_cfg(bp);
11302 
11303 	if (bp->flags & BNXT_FLAG_RFS) {
11304 		rc = bnxt_alloc_rfs_vnics(bp);
11305 		if (rc)
11306 			goto err_out;
11307 	}
11308 
11309 	if (bp->flags & BNXT_FLAG_TPA) {
11310 		rc = bnxt_set_tpa(bp, true);
11311 		if (rc)
11312 			goto err_out;
11313 	}
11314 
11315 	if (BNXT_VF(bp))
11316 		bnxt_update_vf_mac(bp);
11317 
11318 	/* Filter for default vnic 0 */
11319 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
11320 	if (rc) {
11321 		if (rc == -ECOMM)
11322 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
11323 		else
11324 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
11325 		goto err_out;
11326 	}
11327 	vnic->uc_filter_count = 1;
11328 
11329 	vnic->rx_mask = 0;
11330 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
11331 		goto skip_rx_mask;
11332 
11333 	if (bp->dev->flags & IFF_BROADCAST)
11334 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11335 
11336 	if (bp->dev->flags & IFF_PROMISC)
11337 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11338 
11339 	if (bp->dev->flags & IFF_ALLMULTI) {
11340 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11341 		vnic->mc_list_count = 0;
11342 	} else if (bp->dev->flags & IFF_MULTICAST) {
11343 		u32 mask = 0;
11344 
11345 		bnxt_mc_list_updated(bp, &mask, &bp->dev->mc);
11346 		vnic->rx_mask |= mask;
11347 	}
11348 
11349 	rc = bnxt_cfg_rx_mode(bp, &bp->dev->uc, true);
11350 	if (rc == -EAGAIN) {
11351 		netif_rx_mode_schedule_retry(bp->dev);
11352 		rc = 0;
11353 	} else if (rc) {
11354 		goto err_out;
11355 	}
11356 
11357 skip_rx_mask:
11358 	rc = bnxt_hwrm_set_coal(bp);
11359 	if (rc)
11360 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
11361 				rc);
11362 
11363 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11364 		rc = bnxt_setup_nitroa0_vnic(bp);
11365 		if (rc)
11366 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
11367 				   rc);
11368 	}
11369 
11370 	if (BNXT_VF(bp)) {
11371 		bnxt_hwrm_func_qcfg(bp);
11372 		netdev_update_features(bp->dev);
11373 	}
11374 
11375 	return 0;
11376 
11377 err_out:
11378 	bnxt_hwrm_resource_free(bp, 0, true);
11379 
11380 	return rc;
11381 }
11382 
11383 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
11384 {
11385 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
11386 	return 0;
11387 }
11388 
11389 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
11390 {
11391 	int rc;
11392 
11393 	bnxt_init_cp_rings(bp);
11394 	rc = bnxt_init_rx_rings(bp);
11395 	if (rc)
11396 		return rc;
11397 
11398 	bnxt_init_tx_rings(bp);
11399 	bnxt_init_ring_grps(bp, irq_re_init);
11400 	bnxt_init_vnics(bp);
11401 
11402 	return bnxt_init_chip(bp, irq_re_init);
11403 }
11404 
11405 static int bnxt_set_real_num_queues(struct bnxt *bp)
11406 {
11407 	int rc;
11408 	struct net_device *dev = bp->dev;
11409 
11410 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
11411 					  bp->tx_nr_rings_xdp);
11412 	if (rc)
11413 		return rc;
11414 
11415 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
11416 	if (rc)
11417 		return rc;
11418 
11419 #ifdef CONFIG_RFS_ACCEL
11420 	if (bp->flags & BNXT_FLAG_RFS)
11421 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
11422 #endif
11423 
11424 	return rc;
11425 }
11426 
11427 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11428 			     bool shared)
11429 {
11430 	int _rx = *rx, _tx = *tx;
11431 
11432 	if (shared) {
11433 		*rx = min_t(int, _rx, max);
11434 		*tx = min_t(int, _tx, max);
11435 	} else {
11436 		if (max < 2)
11437 			return -ENOMEM;
11438 
11439 		while (_rx + _tx > max) {
11440 			if (_rx > _tx && _rx > 1)
11441 				_rx--;
11442 			else if (_tx > 1)
11443 				_tx--;
11444 		}
11445 		*rx = _rx;
11446 		*tx = _tx;
11447 	}
11448 	return 0;
11449 }
11450 
11451 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
11452 {
11453 	return (tx - tx_xdp) / tx_sets + tx_xdp;
11454 }
11455 
11456 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
11457 {
11458 	int tcs = bp->num_tc;
11459 
11460 	if (!tcs)
11461 		tcs = 1;
11462 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11463 }
11464 
11465 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11466 {
11467 	int tcs = bp->num_tc;
11468 
11469 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11470 	       bp->tx_nr_rings_xdp;
11471 }
11472 
11473 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11474 			   bool sh)
11475 {
11476 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11477 
11478 	if (tx_cp != *tx) {
11479 		int tx_saved = tx_cp, rc;
11480 
11481 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11482 		if (rc)
11483 			return rc;
11484 		if (tx_cp != tx_saved)
11485 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11486 		return 0;
11487 	}
11488 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11489 }
11490 
11491 static void bnxt_setup_msix(struct bnxt *bp)
11492 {
11493 	const int len = sizeof(bp->irq_tbl[0].name);
11494 	struct net_device *dev = bp->dev;
11495 	int tcs, i;
11496 
11497 	tcs = bp->num_tc;
11498 	if (tcs) {
11499 		int i, off, count;
11500 
11501 		for (i = 0; i < tcs; i++) {
11502 			count = bp->tx_nr_rings_per_tc;
11503 			off = BNXT_TC_TO_RING_BASE(bp, i);
11504 			netdev_set_tc_queue(dev, i, count, off);
11505 		}
11506 	}
11507 
11508 	for (i = 0; i < bp->cp_nr_rings; i++) {
11509 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11510 		char *attr;
11511 
11512 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11513 			attr = "TxRx";
11514 		else if (i < bp->rx_nr_rings)
11515 			attr = "rx";
11516 		else
11517 			attr = "tx";
11518 
11519 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11520 			 attr, i);
11521 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11522 	}
11523 }
11524 
11525 static int bnxt_init_int_mode(struct bnxt *bp);
11526 
11527 static int bnxt_change_msix(struct bnxt *bp, int total)
11528 {
11529 	struct msi_map map;
11530 	int i;
11531 
11532 	/* add MSIX to the end if needed */
11533 	for (i = bp->total_irqs; i < total; i++) {
11534 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11535 		if (map.index < 0)
11536 			return bp->total_irqs;
11537 		bp->irq_tbl[i].vector = map.virq;
11538 		bp->total_irqs++;
11539 	}
11540 
11541 	/* trim MSIX from the end if needed */
11542 	for (i = bp->total_irqs; i > total; i--) {
11543 		map.index = i - 1;
11544 		map.virq = bp->irq_tbl[i - 1].vector;
11545 		pci_msix_free_irq(bp->pdev, map);
11546 		bp->irq_tbl[i - 1].vector = 0;
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 		/* FW may already hold the reservation from an earlier
11726 		 * reservation that was not followed by growing the dynamic
11727 		 * MSI-X vectors, e.g. because the allocation failed.
11728 		 */
11729 		if (irq_re_init && BNXT_NEW_RM(bp) && bp->irq_tbl &&
11730 		    pci_msix_can_alloc_dyn(bp->pdev)) {
11731 			irqs_required = bnxt_get_num_msix(bp);
11732 			if (irqs_required > bp->total_irqs &&
11733 			    bnxt_change_msix(bp, irqs_required) != irqs_required) {
11734 				netdev_err(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
11735 					   irqs_required, bp->total_irqs);
11736 				return -ENOSPC;
11737 			}
11738 		}
11739 		return 0;
11740 	}
11741 
11742 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(edev)) {
11743 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11744 
11745 		if (ulp_msix > bp->ulp_num_msix_want)
11746 			ulp_msix = bp->ulp_num_msix_want;
11747 		irqs_required = ulp_msix + bp->cp_nr_rings;
11748 	} else {
11749 		irqs_required = bnxt_get_num_msix(bp);
11750 	}
11751 
11752 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11753 		irq_change = true;
11754 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11755 			bnxt_ulp_irq_stop(bp);
11756 			bnxt_clear_int_mode(bp);
11757 			irq_cleared = true;
11758 		}
11759 	}
11760 	rc = __bnxt_reserve_rings(bp);
11761 	if (irq_cleared) {
11762 		if (!rc)
11763 			rc = bnxt_init_int_mode(bp);
11764 		bnxt_ulp_irq_restart(bp, rc);
11765 	} else if (irq_change && !rc) {
11766 		/* Size the vectors from what FW actually reserved, which
11767 		 * can be less than what was requested.
11768 		 */
11769 		irqs_required = bnxt_get_num_msix(bp);
11770 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11771 			rc = -ENOSPC;
11772 	}
11773 	if (rc) {
11774 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11775 		return rc;
11776 	}
11777 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11778 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11779 		netdev_err(bp->dev, "tx ring reservation failure\n");
11780 		netdev_reset_tc(bp->dev);
11781 		bp->num_tc = 0;
11782 		if (bp->tx_nr_rings_xdp)
11783 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11784 		else
11785 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11786 		return -ENOMEM;
11787 	}
11788 	return 0;
11789 }
11790 
11791 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11792 {
11793 	struct bnxt_tx_ring_info *txr;
11794 	struct netdev_queue *txq;
11795 	struct bnxt_napi *bnapi;
11796 	int i;
11797 
11798 	bnapi = bp->bnapi[idx];
11799 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11800 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11801 		synchronize_net();
11802 
11803 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11804 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11805 			if (txq) {
11806 				__netif_tx_lock_bh(txq);
11807 				netif_tx_stop_queue(txq);
11808 				__netif_tx_unlock_bh(txq);
11809 			}
11810 		}
11811 
11812 		if (!bp->tph_mode)
11813 			continue;
11814 
11815 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11816 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11817 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11818 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11819 	}
11820 }
11821 
11822 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11823 {
11824 	struct bnxt_tx_ring_info *txr;
11825 	struct netdev_queue *txq;
11826 	struct bnxt_napi *bnapi;
11827 	int rc, i;
11828 
11829 	bnapi = bp->bnapi[idx];
11830 	/* All rings have been reserved and previously allocated.
11831 	 * Reallocating with the same parameters should never fail.
11832 	 */
11833 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11834 		if (!bp->tph_mode)
11835 			goto start_tx;
11836 
11837 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11838 		if (rc)
11839 			return rc;
11840 
11841 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11842 		if (rc)
11843 			return rc;
11844 
11845 		txr->tx_prod = 0;
11846 		txr->tx_cons = 0;
11847 		txr->tx_hw_cons = 0;
11848 		txr->kick_pending = 0;
11849 		txr->kick_txbd0 = NULL;
11850 start_tx:
11851 		WRITE_ONCE(txr->dev_state, 0);
11852 		synchronize_net();
11853 
11854 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11855 			continue;
11856 
11857 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11858 		if (txq)
11859 			netif_tx_start_queue(txq);
11860 	}
11861 
11862 	return 0;
11863 }
11864 
11865 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11866 				     const cpumask_t *mask)
11867 {
11868 	struct bnxt_irq *irq;
11869 	u16 tag;
11870 
11871 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11872 
11873 	cpumask_copy(irq->bp->ring_cpu_mask[irq->ring_nr], mask);
11874 	set_bit(irq->ring_nr, irq->bp->ring_affinity_set);
11875 
11876 #ifdef CONFIG_RFS_ACCEL
11877 	if (irq->bp->dev->rx_cpu_rmap && irq->ring_nr < irq->bp->rx_nr_rings) {
11878 		int err;
11879 
11880 		err = cpu_rmap_update(irq->bp->dev->rx_cpu_rmap, irq->ring_nr,
11881 				      mask);
11882 		if (err)
11883 			netdev_warn(irq->bp->dev,
11884 				    "aRFS rmap update failed: %d\n", err);
11885 	}
11886 #endif
11887 
11888 	if (!irq->bp->tph_mode)
11889 		return;
11890 
11891 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11892 		return;
11893 
11894 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11895 				cpumask_first(mask), &tag))
11896 		return;
11897 
11898 	WRITE_ONCE(irq->new_tag, tag);
11899 	bnxt_queue_sp_work(irq->bp, BNXT_TPH_UPDATE_SP_EVENT);
11900 }
11901 
11902 static void bnxt_irq_affinity_release(struct kref *ref)
11903 {
11904 	struct irq_affinity_notify *notify =
11905 		container_of(ref, struct irq_affinity_notify, kref);
11906 	struct bnxt_irq *irq;
11907 
11908 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11909 
11910 	if (!irq->bp->tph_mode)
11911 		return;
11912 
11913 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11914 		netdev_err(irq->bp->dev,
11915 			   "Setting ST=0 for MSIX entry %d failed\n",
11916 			   irq->msix_nr);
11917 		return;
11918 	}
11919 }
11920 
11921 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11922 {
11923 	irq_set_affinity_notifier(irq->vector, NULL);
11924 }
11925 
11926 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11927 {
11928 	struct irq_affinity_notify *notify;
11929 
11930 	irq->bp = bp;
11931 
11932 	/* Register IRQ affinity notifier */
11933 	notify = &irq->affinity_notify;
11934 	notify->irq = irq->vector;
11935 	notify->notify = bnxt_irq_affinity_notify;
11936 	notify->release = bnxt_irq_affinity_release;
11937 
11938 	irq_set_affinity_notifier(irq->vector, notify);
11939 }
11940 
11941 static int bnxt_alloc_ring_cpu_masks(struct bnxt *bp)
11942 {
11943 	int i;
11944 
11945 	bp->ring_cpu_mask = kzalloc_objs(*bp->ring_cpu_mask, bp->max_irqs);
11946 	if (!bp->ring_cpu_mask)
11947 		return -ENOMEM;
11948 
11949 	bp->ring_affinity_set = bitmap_zalloc(bp->max_irqs, GFP_KERNEL);
11950 	if (!bp->ring_affinity_set)
11951 		return -ENOMEM;
11952 
11953 	for (i = 0; i < bp->max_irqs; i++)
11954 		if (!zalloc_cpumask_var(&bp->ring_cpu_mask[i], GFP_KERNEL))
11955 			return -ENOMEM;
11956 
11957 	return 0;
11958 }
11959 
11960 static void bnxt_free_ring_cpu_masks(struct bnxt *bp)
11961 {
11962 	int i;
11963 
11964 	if (!bp->ring_cpu_mask)
11965 		return;
11966 
11967 	for (i = 0; i < bp->max_irqs; i++)
11968 		free_cpumask_var(bp->ring_cpu_mask[i]);
11969 
11970 	bitmap_free(bp->ring_affinity_set);
11971 	bp->ring_affinity_set = NULL;
11972 	kfree(bp->ring_cpu_mask);
11973 	bp->ring_cpu_mask = NULL;
11974 }
11975 
11976 static void bnxt_free_irq(struct bnxt *bp)
11977 {
11978 	struct bnxt_irq *irq;
11979 	int i;
11980 
11981 	if (!bp->irq_tbl || !bp->bnapi)
11982 		return;
11983 
11984 	for (i = 0; i < bp->cp_nr_rings; i++) {
11985 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11986 
11987 		irq = &bp->irq_tbl[map_idx];
11988 		if (irq->requested) {
11989 			bnxt_release_irq_notifier(irq);
11990 			irq_update_affinity_hint(irq->vector, NULL);
11991 			free_irq(irq->vector, bp->bnapi[i]);
11992 		}
11993 
11994 		irq->requested = 0;
11995 		irq->tag = 0;
11996 		irq->new_tag = 0;
11997 	}
11998 
11999 	/* Disable TPH support */
12000 	pcie_disable_tph(bp->pdev);
12001 	bp->tph_mode = 0;
12002 
12003 #ifdef CONFIG_RFS_ACCEL
12004 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
12005 	bp->dev->rx_cpu_rmap = NULL;
12006 #endif
12007 }
12008 
12009 static int bnxt_request_irq(struct bnxt *bp)
12010 {
12011 	const int numa_node = dev_to_node(&bp->pdev->dev);
12012 	struct cpu_rmap *rmap = NULL;
12013 	int i, j, rc = 0;
12014 	unsigned long flags = 0;
12015 
12016 	rc = bnxt_setup_int_mode(bp);
12017 	if (rc) {
12018 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
12019 			   rc);
12020 		return rc;
12021 	}
12022 #ifdef CONFIG_RFS_ACCEL
12023 	rmap = bp->dev->rx_cpu_rmap;
12024 #endif
12025 
12026 	/* Enable TPH support as part of IRQ request */
12027 	if (BNXT_SUPPORTS_QUEUE_API(bp)) {
12028 		rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
12029 		if (!rc)
12030 			bp->tph_mode = PCI_TPH_ST_IV_MODE;
12031 	}
12032 
12033 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
12034 		struct cpumask *cpu_mask = bp->ring_cpu_mask[i];
12035 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
12036 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
12037 		u16 tag;
12038 
12039 		if (IS_ENABLED(CONFIG_RFS_ACCEL) &&
12040 		    rmap && bp->bnapi[i]->rx_ring) {
12041 			rc = irq_cpu_rmap_add(rmap, irq->vector);
12042 			if (rc)
12043 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
12044 					    j);
12045 			j++;
12046 		}
12047 
12048 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
12049 				 bp->bnapi[i]);
12050 		if (rc)
12051 			break;
12052 
12053 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
12054 		irq->requested = 1;
12055 		irq->msix_nr = map_idx;
12056 		irq->ring_nr = i;
12057 
12058 		/* Reuse the mask recorded before the IRQs were freed. Nothing
12059 		 * was recorded yet on the very first request, and the mask
12060 		 * may have gone stale if the CPUs went offline in between.
12061 		 */
12062 		if (!test_bit(i, bp->ring_affinity_set) ||
12063 		    !cpumask_intersects(cpu_mask, cpu_online_mask)) {
12064 			clear_bit(i, bp->ring_affinity_set);
12065 			cpumask_clear(cpu_mask);
12066 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
12067 					cpu_mask);
12068 		}
12069 
12070 		/* Init ST table entry if we can get the mapping */
12071 		if (!pcie_tph_get_cpu_st(bp->pdev, TPH_MEM_TYPE_VM,
12072 					 cpumask_first(cpu_mask), &tag)) {
12073 			pcie_tph_set_st_entry(bp->pdev, irq->msix_nr, tag);
12074 			irq->tag = tag;
12075 			irq->new_tag = tag;
12076 		}
12077 
12078 		bnxt_register_irq_notifier(bp, irq);
12079 
12080 		/* Only put the IRQ back where it was configured to be, our own
12081 		 * placement is just a hint, the core spreads within
12082 		 * irq_default_affinity which we know nothing about.
12083 		 * Set after installing the notifier, if we race with the user
12084 		 * it's better to overwrite than miss the notification.
12085 		 */
12086 		if (test_bit(i, bp->ring_affinity_set))
12087 			rc = irq_set_affinity_and_hint(irq->vector, cpu_mask);
12088 		else
12089 			rc = irq_update_affinity_hint(irq->vector, cpu_mask);
12090 		if (rc) {
12091 			netdev_warn(bp->dev,
12092 				    "Setting IRQ affinity failed, IRQ = %d\n",
12093 				    irq->vector);
12094 			break;
12095 		}
12096 	}
12097 	return rc;
12098 }
12099 
12100 static void bnxt_del_napi(struct bnxt *bp)
12101 {
12102 	int i;
12103 
12104 	if (!bp->bnapi)
12105 		return;
12106 
12107 	for (i = 0; i < bp->rx_nr_rings; i++)
12108 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
12109 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
12110 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
12111 
12112 	for (i = 0; i < bp->cp_nr_rings; i++) {
12113 		struct bnxt_napi *bnapi = bp->bnapi[i];
12114 
12115 		__netif_napi_del_locked(&bnapi->napi);
12116 	}
12117 	/* We called __netif_napi_del_locked(), we need
12118 	 * to respect an RCU grace period before freeing napi structures.
12119 	 */
12120 	synchronize_net();
12121 }
12122 
12123 static void bnxt_init_napi(struct bnxt *bp)
12124 {
12125 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
12126 	unsigned int cp_nr_rings = bp->cp_nr_rings;
12127 	struct bnxt_napi *bnapi;
12128 	int i;
12129 
12130 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
12131 		poll_fn = bnxt_poll_p5;
12132 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
12133 		cp_nr_rings--;
12134 
12135 	set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
12136 
12137 	for (i = 0; i < cp_nr_rings; i++) {
12138 		bnapi = bp->bnapi[i];
12139 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
12140 					     bnapi->index);
12141 	}
12142 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
12143 		bnapi = bp->bnapi[cp_nr_rings];
12144 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
12145 	}
12146 }
12147 
12148 static void bnxt_disable_napi(struct bnxt *bp)
12149 {
12150 	int i;
12151 
12152 	if (!bp->bnapi ||
12153 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
12154 		return;
12155 
12156 	for (i = 0; i < bp->cp_nr_rings; i++) {
12157 		struct bnxt_napi *bnapi = bp->bnapi[i];
12158 		struct bnxt_cp_ring_info *cpr;
12159 
12160 		cpr = &bnapi->cp_ring;
12161 		if (bnapi->tx_fault)
12162 			cpr->sw_stats->tx.tx_resets++;
12163 		if (bnapi->in_reset)
12164 			cpr->sw_stats->rx.rx_resets++;
12165 		napi_disable_locked(&bnapi->napi);
12166 	}
12167 }
12168 
12169 static void bnxt_enable_napi(struct bnxt *bp)
12170 {
12171 	int i;
12172 
12173 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
12174 	for (i = 0; i < bp->cp_nr_rings; i++) {
12175 		struct bnxt_napi *bnapi = bp->bnapi[i];
12176 		struct bnxt_cp_ring_info *cpr;
12177 
12178 		bnapi->tx_fault = 0;
12179 
12180 		cpr = &bnapi->cp_ring;
12181 		bnapi->in_reset = false;
12182 
12183 		if (bnapi->rx_ring) {
12184 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
12185 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
12186 		}
12187 		napi_enable_locked(&bnapi->napi);
12188 	}
12189 }
12190 
12191 void bnxt_tx_disable(struct bnxt *bp)
12192 {
12193 	int i;
12194 	struct bnxt_tx_ring_info *txr;
12195 
12196 	if (bp->tx_ring) {
12197 		for (i = 0; i < bp->tx_nr_rings; i++) {
12198 			txr = &bp->tx_ring[i];
12199 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
12200 		}
12201 	}
12202 	/* Make sure napi polls see @dev_state change */
12203 	synchronize_net();
12204 	/* Drop carrier first to prevent TX timeout */
12205 	netif_carrier_off(bp->dev);
12206 	/* Stop all TX queues */
12207 	netif_tx_disable(bp->dev);
12208 }
12209 
12210 void bnxt_tx_enable(struct bnxt *bp)
12211 {
12212 	int i;
12213 	struct bnxt_tx_ring_info *txr;
12214 
12215 	for (i = 0; i < bp->tx_nr_rings; i++) {
12216 		txr = &bp->tx_ring[i];
12217 		WRITE_ONCE(txr->dev_state, 0);
12218 	}
12219 	/* Make sure napi polls see @dev_state change */
12220 	synchronize_net();
12221 	netif_tx_wake_all_queues(bp->dev);
12222 	if (BNXT_LINK_IS_UP(bp))
12223 		netif_carrier_on(bp->dev);
12224 }
12225 
12226 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
12227 {
12228 	u8 active_fec = link_info->active_fec_sig_mode &
12229 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
12230 
12231 	switch (active_fec) {
12232 	default:
12233 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
12234 		return "None";
12235 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
12236 		return "Clause 74 BaseR";
12237 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
12238 		return "Clause 91 RS(528,514)";
12239 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
12240 		return "Clause 91 RS544_1XN";
12241 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
12242 		return "Clause 91 RS(544,514)";
12243 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
12244 		return "Clause 91 RS272_1XN";
12245 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
12246 		return "Clause 91 RS(272,257)";
12247 	}
12248 }
12249 
12250 static char *bnxt_link_down_reason(struct bnxt_link_info *link_info)
12251 {
12252 	u8 reason = link_info->link_down_reason;
12253 
12254 	/* Multiple bits can be set, we report 1 bit only in order of
12255 	 * priority.
12256 	 */
12257 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_RF)
12258 		return "(Remote fault)";
12259 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_OTP_SPEED_VIOLATION)
12260 		return "(OTP Speed limit violation)";
12261 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_CABLE_REMOVED)
12262 		return "(Cable removed)";
12263 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_MODULE_FAULT)
12264 		return "(Module fault)";
12265 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_BMC_REQUEST)
12266 		return "(BMC request down)";
12267 	return "";
12268 }
12269 
12270 void bnxt_report_link(struct bnxt *bp)
12271 {
12272 	if (BNXT_LINK_IS_UP(bp)) {
12273 		const char *signal = "";
12274 		const char *flow_ctrl;
12275 		const char *duplex;
12276 		u32 speed;
12277 		u16 fec;
12278 
12279 		netif_carrier_on(bp->dev);
12280 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
12281 		if (speed == SPEED_UNKNOWN) {
12282 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
12283 			return;
12284 		}
12285 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
12286 			duplex = "full";
12287 		else
12288 			duplex = "half";
12289 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
12290 			flow_ctrl = "ON - receive & transmit";
12291 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
12292 			flow_ctrl = "ON - transmit";
12293 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
12294 			flow_ctrl = "ON - receive";
12295 		else
12296 			flow_ctrl = "none";
12297 		if (bp->link_info.phy_qcfg_resp.option_flags &
12298 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
12299 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
12300 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
12301 			switch (sig_mode) {
12302 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
12303 				signal = "(NRZ) ";
12304 				break;
12305 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
12306 				signal = "(PAM4 56Gbps) ";
12307 				break;
12308 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
12309 				signal = "(PAM4 112Gbps) ";
12310 				break;
12311 			default:
12312 				break;
12313 			}
12314 		}
12315 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
12316 			    speed, signal, duplex, flow_ctrl);
12317 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
12318 			netdev_info(bp->dev, "EEE is %s\n",
12319 				    bp->eee.eee_active ? "active" :
12320 							 "not active");
12321 		fec = bp->link_info.fec_cfg;
12322 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
12323 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
12324 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
12325 				    bnxt_report_fec(&bp->link_info));
12326 	} else {
12327 		char *str = bnxt_link_down_reason(&bp->link_info);
12328 
12329 		netif_carrier_off(bp->dev);
12330 		netdev_err(bp->dev, "NIC Link is Down %s\n", str);
12331 	}
12332 }
12333 
12334 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
12335 {
12336 	if (!resp->supported_speeds_auto_mode &&
12337 	    !resp->supported_speeds_force_mode &&
12338 	    !resp->supported_pam4_speeds_auto_mode &&
12339 	    !resp->supported_pam4_speeds_force_mode &&
12340 	    !resp->supported_speeds2_auto_mode &&
12341 	    !resp->supported_speeds2_force_mode)
12342 		return true;
12343 	return false;
12344 }
12345 
12346 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
12347 {
12348 	struct bnxt_link_info *link_info = &bp->link_info;
12349 	struct hwrm_port_phy_qcaps_output *resp;
12350 	struct hwrm_port_phy_qcaps_input *req;
12351 	int rc = 0;
12352 
12353 	if (bp->hwrm_spec_code < 0x10201)
12354 		return 0;
12355 
12356 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
12357 	if (rc)
12358 		return rc;
12359 
12360 	resp = hwrm_req_hold(bp, req);
12361 	rc = hwrm_req_send(bp, req);
12362 	if (rc)
12363 		goto hwrm_phy_qcaps_exit;
12364 
12365 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
12366 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
12367 		struct ethtool_keee *eee = &bp->eee;
12368 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
12369 
12370 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
12371 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
12372 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
12373 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
12374 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
12375 	}
12376 
12377 	if (bp->hwrm_spec_code >= 0x10a01) {
12378 		if (bnxt_phy_qcaps_no_speed(resp)) {
12379 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
12380 			netdev_warn(bp->dev, "Ethernet link disabled\n");
12381 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
12382 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
12383 			netdev_info(bp->dev, "Ethernet link enabled\n");
12384 			/* Phy re-enabled, reprobe the speeds */
12385 			link_info->support_auto_speeds = 0;
12386 			link_info->support_pam4_auto_speeds = 0;
12387 			link_info->support_auto_speeds2 = 0;
12388 		}
12389 	}
12390 	if (resp->supported_speeds_auto_mode)
12391 		link_info->support_auto_speeds =
12392 			le16_to_cpu(resp->supported_speeds_auto_mode);
12393 	if (resp->supported_pam4_speeds_auto_mode)
12394 		link_info->support_pam4_auto_speeds =
12395 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
12396 	if (resp->supported_speeds2_auto_mode)
12397 		link_info->support_auto_speeds2 =
12398 			le16_to_cpu(resp->supported_speeds2_auto_mode);
12399 
12400 	bp->port_count = resp->port_cnt;
12401 
12402 hwrm_phy_qcaps_exit:
12403 	hwrm_req_drop(bp, req);
12404 	return rc;
12405 }
12406 
12407 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
12408 {
12409 	struct hwrm_port_mac_qcaps_output *resp;
12410 	struct hwrm_port_mac_qcaps_input *req;
12411 	int rc;
12412 
12413 	if (bp->hwrm_spec_code < 0x10a03)
12414 		return;
12415 
12416 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
12417 	if (rc)
12418 		return;
12419 
12420 	resp = hwrm_req_hold(bp, req);
12421 	rc = hwrm_req_send_silent(bp, req);
12422 	if (!rc)
12423 		bp->mac_flags = resp->flags;
12424 	hwrm_req_drop(bp, req);
12425 }
12426 
12427 static bool bnxt_support_dropped(u16 advertising, u16 supported)
12428 {
12429 	u16 diff = advertising ^ supported;
12430 
12431 	return ((supported | diff) != supported);
12432 }
12433 
12434 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
12435 {
12436 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
12437 
12438 	/* Check if any advertised speeds are no longer supported. The caller
12439 	 * holds the link_lock mutex, so we can modify link_info settings.
12440 	 */
12441 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12442 		if (bnxt_support_dropped(link_info->advertising,
12443 					 link_info->support_auto_speeds2)) {
12444 			link_info->advertising = link_info->support_auto_speeds2;
12445 			return true;
12446 		}
12447 		return false;
12448 	}
12449 	if (bnxt_support_dropped(link_info->advertising,
12450 				 link_info->support_auto_speeds)) {
12451 		link_info->advertising = link_info->support_auto_speeds;
12452 		return true;
12453 	}
12454 	if (bnxt_support_dropped(link_info->advertising_pam4,
12455 				 link_info->support_pam4_auto_speeds)) {
12456 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
12457 		return true;
12458 	}
12459 	return false;
12460 }
12461 
12462 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
12463 {
12464 	struct bnxt_link_info *link_info = &bp->link_info;
12465 	struct hwrm_port_phy_qcfg_output *resp;
12466 	struct hwrm_port_phy_qcfg_input *req;
12467 	u8 link_state = link_info->link_state;
12468 	bool support_changed;
12469 	int rc;
12470 
12471 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
12472 	if (rc)
12473 		return rc;
12474 
12475 	resp = hwrm_req_hold(bp, req);
12476 	rc = hwrm_req_send(bp, req);
12477 	if (rc) {
12478 		hwrm_req_drop(bp, req);
12479 		if (rc == -ECOMM) {
12480 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
12481 			rc = 0;
12482 		}
12483 		return rc;
12484 	}
12485 
12486 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
12487 	link_info->phy_link_status = resp->link;
12488 	link_info->duplex = resp->duplex_cfg;
12489 	if (bp->hwrm_spec_code >= 0x10800)
12490 		link_info->duplex = resp->duplex_state;
12491 	link_info->pause = resp->pause;
12492 	link_info->auto_mode = resp->auto_mode;
12493 	link_info->auto_pause_setting = resp->auto_pause;
12494 	link_info->lp_pause = resp->link_partner_adv_pause;
12495 	link_info->force_pause_setting = resp->force_pause;
12496 	link_info->duplex_setting = resp->duplex_cfg;
12497 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
12498 		link_info->link_speed = le16_to_cpu(resp->link_speed);
12499 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
12500 			link_info->active_lanes = resp->active_lanes;
12501 	} else {
12502 		link_info->link_speed = 0;
12503 		link_info->active_lanes = 0;
12504 	}
12505 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
12506 	link_info->force_pam4_link_speed =
12507 		le16_to_cpu(resp->force_pam4_link_speed);
12508 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
12509 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
12510 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
12511 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
12512 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
12513 	link_info->auto_pam4_link_speeds =
12514 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
12515 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
12516 	link_info->lp_auto_link_speeds =
12517 		le16_to_cpu(resp->link_partner_adv_speeds);
12518 	link_info->lp_auto_pam4_link_speeds =
12519 		resp->link_partner_pam4_adv_speeds;
12520 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
12521 	link_info->phy_ver[0] = resp->phy_maj;
12522 	link_info->phy_ver[1] = resp->phy_min;
12523 	link_info->phy_ver[2] = resp->phy_bld;
12524 	link_info->media_type = resp->media_type;
12525 	link_info->phy_type = resp->phy_type;
12526 	link_info->transceiver = resp->xcvr_pkg_type;
12527 	link_info->phy_addr = resp->eee_config_phy_addr &
12528 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
12529 	link_info->module_status = resp->module_status;
12530 	link_info->link_down_reason = resp->link_down_reason;
12531 
12532 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
12533 		struct ethtool_keee *eee = &bp->eee;
12534 		u16 fw_speeds;
12535 
12536 		eee->eee_active = 0;
12537 		if (resp->eee_config_phy_addr &
12538 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
12539 			eee->eee_active = 1;
12540 			fw_speeds = le16_to_cpu(
12541 				resp->link_partner_adv_eee_link_speed_mask);
12542 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
12543 		}
12544 
12545 		/* Pull initial EEE config */
12546 		if (!chng_link_state) {
12547 			if (resp->eee_config_phy_addr &
12548 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
12549 				eee->eee_enabled = 1;
12550 
12551 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
12552 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
12553 
12554 			if (resp->eee_config_phy_addr &
12555 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12556 				__le32 tmr;
12557 
12558 				eee->tx_lpi_enabled = 1;
12559 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12560 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12561 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12562 			}
12563 		}
12564 	}
12565 
12566 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12567 	if (bp->hwrm_spec_code >= 0x10504) {
12568 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12569 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12570 	}
12571 	/* TODO: need to add more logic to report VF link */
12572 	if (chng_link_state) {
12573 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12574 			link_info->link_state = BNXT_LINK_STATE_UP;
12575 		else
12576 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12577 		if (link_state != link_info->link_state)
12578 			bnxt_report_link(bp);
12579 	} else {
12580 		/* always link down if not require to update link state */
12581 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12582 	}
12583 	hwrm_req_drop(bp, req);
12584 
12585 	if (!BNXT_PHY_CFG_ABLE(bp))
12586 		return 0;
12587 
12588 	support_changed = bnxt_support_speed_dropped(link_info);
12589 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12590 		bnxt_hwrm_set_link_setting(bp, true, false);
12591 	return 0;
12592 }
12593 
12594 static void bnxt_get_port_module_status(struct bnxt *bp)
12595 {
12596 	struct bnxt_link_info *link_info = &bp->link_info;
12597 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12598 	u8 module_status;
12599 
12600 	if (bnxt_update_link(bp, true))
12601 		return;
12602 
12603 	module_status = link_info->module_status;
12604 	switch (module_status) {
12605 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12606 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12607 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12608 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12609 			    bp->pf.port_id);
12610 		if (bp->hwrm_spec_code >= 0x10201) {
12611 			netdev_warn(bp->dev, "Module part number %s\n",
12612 				    resp->phy_vendor_partnumber);
12613 		}
12614 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12615 			netdev_warn(bp->dev, "TX is disabled\n");
12616 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12617 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12618 	}
12619 }
12620 
12621 static void
12622 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12623 {
12624 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12625 		if (bp->hwrm_spec_code >= 0x10201)
12626 			req->auto_pause =
12627 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12628 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12629 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12630 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12631 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12632 		req->enables |=
12633 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12634 	} else {
12635 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12636 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12637 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12638 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12639 		req->enables |=
12640 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12641 		if (bp->hwrm_spec_code >= 0x10201) {
12642 			req->auto_pause = req->force_pause;
12643 			req->enables |= cpu_to_le32(
12644 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12645 		}
12646 	}
12647 }
12648 
12649 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12650 {
12651 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12652 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12653 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12654 			req->enables |=
12655 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12656 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12657 		} else if (bp->link_info.advertising) {
12658 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12659 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12660 		}
12661 		if (bp->link_info.advertising_pam4) {
12662 			req->enables |=
12663 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12664 			req->auto_link_pam4_speed_mask =
12665 				cpu_to_le16(bp->link_info.advertising_pam4);
12666 		}
12667 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12668 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12669 	} else {
12670 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12671 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12672 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12673 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12674 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12675 				   (u32)bp->link_info.req_link_speed);
12676 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12677 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12678 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12679 		} else {
12680 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12681 		}
12682 	}
12683 
12684 	/* tell chimp that the setting takes effect immediately */
12685 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12686 }
12687 
12688 int bnxt_hwrm_set_pause(struct bnxt *bp)
12689 {
12690 	struct hwrm_port_phy_cfg_input *req;
12691 	int rc;
12692 
12693 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12694 	if (rc)
12695 		return rc;
12696 
12697 	bnxt_hwrm_set_pause_common(bp, req);
12698 
12699 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12700 	    bp->link_info.force_link_chng)
12701 		bnxt_hwrm_set_link_common(bp, req);
12702 
12703 	rc = hwrm_req_send(bp, req);
12704 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12705 		/* since changing of pause setting doesn't trigger any link
12706 		 * change event, the driver needs to update the current pause
12707 		 * result upon successfully return of the phy_cfg command
12708 		 */
12709 		bp->link_info.pause =
12710 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12711 		bp->link_info.auto_pause_setting = 0;
12712 		if (!bp->link_info.force_link_chng)
12713 			bnxt_report_link(bp);
12714 	}
12715 	bp->link_info.force_link_chng = false;
12716 	return rc;
12717 }
12718 
12719 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12720 			      struct hwrm_port_phy_cfg_input *req)
12721 {
12722 	struct ethtool_keee *eee = &bp->eee;
12723 
12724 	if (eee->eee_enabled) {
12725 		u16 eee_speeds;
12726 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12727 
12728 		if (eee->tx_lpi_enabled)
12729 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12730 		else
12731 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12732 
12733 		req->flags |= cpu_to_le32(flags);
12734 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12735 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12736 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12737 	} else {
12738 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12739 	}
12740 }
12741 
12742 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12743 {
12744 	struct hwrm_port_phy_cfg_input *req;
12745 	int rc;
12746 
12747 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12748 	if (rc)
12749 		return rc;
12750 
12751 	if (set_pause)
12752 		bnxt_hwrm_set_pause_common(bp, req);
12753 
12754 	bnxt_hwrm_set_link_common(bp, req);
12755 
12756 	if (set_eee)
12757 		bnxt_hwrm_set_eee(bp, req);
12758 	return hwrm_req_send(bp, req);
12759 }
12760 
12761 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12762 {
12763 	struct hwrm_port_phy_cfg_input *req;
12764 	int rc;
12765 
12766 	if (!BNXT_SINGLE_PF(bp))
12767 		return 0;
12768 
12769 	if (pci_num_vf(bp->pdev) &&
12770 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12771 		return 0;
12772 
12773 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12774 	if (rc)
12775 		return rc;
12776 
12777 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12778 	rc = hwrm_req_send(bp, req);
12779 	if (!rc) {
12780 		mutex_lock(&bp->link_lock);
12781 		/* Device is not obliged link down in certain scenarios, even
12782 		 * when forced. Setting the state unknown is consistent with
12783 		 * driver startup and will force link state to be reported
12784 		 * during subsequent open based on PORT_PHY_QCFG.
12785 		 */
12786 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12787 		mutex_unlock(&bp->link_lock);
12788 	}
12789 	return rc;
12790 }
12791 
12792 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12793 {
12794 #ifdef CONFIG_TEE_BNXT_FW
12795 	int rc = tee_bnxt_fw_load();
12796 
12797 	if (rc)
12798 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12799 
12800 	return rc;
12801 #else
12802 	netdev_err(bp->dev, "OP-TEE not supported\n");
12803 	return -ENODEV;
12804 #endif
12805 }
12806 
12807 static int bnxt_try_recover_fw(struct bnxt *bp)
12808 {
12809 	if (bp->fw_health && bp->fw_health->status_reliable) {
12810 		int retry = 0, rc;
12811 		u32 sts;
12812 
12813 		do {
12814 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12815 			rc = bnxt_hwrm_poll(bp);
12816 			if (!BNXT_FW_IS_BOOTING(sts) &&
12817 			    !BNXT_FW_IS_RECOVERING(sts))
12818 				break;
12819 			retry++;
12820 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12821 
12822 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12823 			netdev_err(bp->dev,
12824 				   "Firmware not responding, status: 0x%x\n",
12825 				   sts);
12826 			rc = -ENODEV;
12827 		}
12828 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12829 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12830 			return bnxt_fw_reset_via_optee(bp);
12831 		}
12832 		return rc;
12833 	}
12834 
12835 	return -ENODEV;
12836 }
12837 
12838 void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12839 {
12840 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12841 
12842 	if (!BNXT_NEW_RM(bp))
12843 		return; /* no resource reservations required */
12844 
12845 	hw_resc->resv_cp_rings = 0;
12846 	hw_resc->resv_stat_ctxs = 0;
12847 	hw_resc->resv_irqs = 0;
12848 	hw_resc->resv_tx_rings = 0;
12849 	hw_resc->resv_rx_rings = 0;
12850 	hw_resc->resv_hw_ring_grps = 0;
12851 	hw_resc->resv_vnics = 0;
12852 	hw_resc->resv_rsscos_ctxs = 0;
12853 	if (!fw_reset) {
12854 		bp->tx_nr_rings = 0;
12855 		bp->rx_nr_rings = 0;
12856 	}
12857 }
12858 
12859 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12860 {
12861 	int rc;
12862 
12863 	if (!BNXT_NEW_RM(bp))
12864 		return 0; /* no resource reservations required */
12865 
12866 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12867 	if (rc)
12868 		netdev_err(bp->dev, "resc_qcaps failed\n");
12869 
12870 	bnxt_clear_reservations(bp, fw_reset);
12871 
12872 	return rc;
12873 }
12874 
12875 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12876 {
12877 	struct hwrm_func_drv_if_change_output *resp;
12878 	struct hwrm_func_drv_if_change_input *req;
12879 	bool resc_reinit = false;
12880 	bool caps_change = false;
12881 	int rc, retry = 0;
12882 	bool fw_reset;
12883 	u32 flags = 0;
12884 
12885 	fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT);
12886 	bp->fw_reset_state = 0;
12887 
12888 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12889 		return 0;
12890 
12891 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12892 	if (rc)
12893 		return rc;
12894 
12895 	if (up)
12896 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12897 	resp = hwrm_req_hold(bp, req);
12898 
12899 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12900 	while (retry < BNXT_FW_IF_RETRY) {
12901 		rc = hwrm_req_send(bp, req);
12902 		if (rc != -EAGAIN)
12903 			break;
12904 
12905 		msleep(50);
12906 		retry++;
12907 	}
12908 
12909 	if (rc == -EAGAIN) {
12910 		hwrm_req_drop(bp, req);
12911 		return rc;
12912 	} else if (!rc) {
12913 		flags = le32_to_cpu(resp->flags);
12914 	} else if (up) {
12915 		rc = bnxt_try_recover_fw(bp);
12916 		fw_reset = true;
12917 	}
12918 	hwrm_req_drop(bp, req);
12919 	if (rc)
12920 		return rc;
12921 
12922 	if (!up) {
12923 		bnxt_inv_fw_health_reg(bp);
12924 		return 0;
12925 	}
12926 
12927 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12928 		resc_reinit = true;
12929 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12930 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12931 		fw_reset = true;
12932 	else
12933 		bnxt_remap_fw_health_regs(bp);
12934 
12935 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12936 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12937 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12938 		return -ENODEV;
12939 	}
12940 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12941 		caps_change = true;
12942 
12943 	if (resc_reinit || fw_reset || caps_change) {
12944 		if (fw_reset || caps_change) {
12945 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12946 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12947 				bnxt_ulp_irq_stop(bp);
12948 			bnxt_free_ctx_mem(bp, false);
12949 			bnxt_dcb_free(bp);
12950 			rc = bnxt_fw_init_one(bp);
12951 			if (rc) {
12952 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12953 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12954 				return rc;
12955 			}
12956 			/* IRQ will be initialized later in bnxt_request_irq()*/
12957 			bnxt_clear_int_mode(bp);
12958 		}
12959 		rc = bnxt_cancel_reservations(bp, fw_reset);
12960 	}
12961 	return rc;
12962 }
12963 
12964 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12965 {
12966 	struct hwrm_port_led_qcaps_output *resp;
12967 	struct hwrm_port_led_qcaps_input *req;
12968 	struct bnxt_pf_info *pf = &bp->pf;
12969 	int rc;
12970 
12971 	bp->num_leds = 0;
12972 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12973 		return 0;
12974 
12975 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12976 	if (rc)
12977 		return rc;
12978 
12979 	req->port_id = cpu_to_le16(pf->port_id);
12980 	resp = hwrm_req_hold(bp, req);
12981 	rc = hwrm_req_send(bp, req);
12982 	if (rc) {
12983 		hwrm_req_drop(bp, req);
12984 		return rc;
12985 	}
12986 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12987 		int i;
12988 
12989 		bp->num_leds = resp->num_leds;
12990 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12991 						 bp->num_leds);
12992 		for (i = 0; i < bp->num_leds; i++) {
12993 			struct bnxt_led_info *led = &bp->leds[i];
12994 			__le16 caps = led->led_state_caps;
12995 
12996 			if (!led->led_group_id ||
12997 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12998 				bp->num_leds = 0;
12999 				break;
13000 			}
13001 		}
13002 	}
13003 	hwrm_req_drop(bp, req);
13004 	return 0;
13005 }
13006 
13007 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
13008 {
13009 	struct hwrm_wol_filter_alloc_output *resp;
13010 	struct hwrm_wol_filter_alloc_input *req;
13011 	int rc;
13012 
13013 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
13014 	if (rc)
13015 		return rc;
13016 
13017 	req->port_id = cpu_to_le16(bp->pf.port_id);
13018 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
13019 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
13020 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
13021 
13022 	resp = hwrm_req_hold(bp, req);
13023 	rc = hwrm_req_send(bp, req);
13024 	if (!rc)
13025 		bp->wol_filter_id = resp->wol_filter_id;
13026 	hwrm_req_drop(bp, req);
13027 	return rc;
13028 }
13029 
13030 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
13031 {
13032 	struct hwrm_wol_filter_free_input *req;
13033 	int rc;
13034 
13035 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
13036 	if (rc)
13037 		return rc;
13038 
13039 	req->port_id = cpu_to_le16(bp->pf.port_id);
13040 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
13041 	req->wol_filter_id = bp->wol_filter_id;
13042 
13043 	return hwrm_req_send(bp, req);
13044 }
13045 
13046 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
13047 {
13048 	struct hwrm_wol_filter_qcfg_output *resp;
13049 	struct hwrm_wol_filter_qcfg_input *req;
13050 	u16 next_handle = 0;
13051 	int rc;
13052 
13053 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
13054 	if (rc)
13055 		return rc;
13056 
13057 	req->port_id = cpu_to_le16(bp->pf.port_id);
13058 	req->handle = cpu_to_le16(handle);
13059 	resp = hwrm_req_hold(bp, req);
13060 	rc = hwrm_req_send(bp, req);
13061 	if (!rc) {
13062 		next_handle = le16_to_cpu(resp->next_handle);
13063 		if (next_handle != 0) {
13064 			if (resp->wol_type ==
13065 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
13066 				bp->wol = 1;
13067 				bp->wol_filter_id = resp->wol_filter_id;
13068 			}
13069 		}
13070 	}
13071 	hwrm_req_drop(bp, req);
13072 	return next_handle;
13073 }
13074 
13075 static void bnxt_get_wol_settings(struct bnxt *bp)
13076 {
13077 	u16 handle = 0;
13078 
13079 	bp->wol = 0;
13080 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
13081 		return;
13082 
13083 	do {
13084 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
13085 	} while (handle && handle != 0xffff);
13086 }
13087 
13088 static bool bnxt_eee_config_ok(struct bnxt *bp)
13089 {
13090 	struct ethtool_keee *eee = &bp->eee;
13091 	struct bnxt_link_info *link_info = &bp->link_info;
13092 
13093 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
13094 		return true;
13095 
13096 	if (eee->eee_enabled) {
13097 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
13098 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
13099 
13100 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
13101 
13102 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
13103 			eee->eee_enabled = 0;
13104 			return false;
13105 		}
13106 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
13107 			linkmode_and(eee->advertised, advertising,
13108 				     eee->supported);
13109 			return false;
13110 		}
13111 	}
13112 	return true;
13113 }
13114 
13115 static int bnxt_update_phy_setting(struct bnxt *bp)
13116 {
13117 	int rc;
13118 	bool update_link = false;
13119 	bool update_pause = false;
13120 	bool update_eee = false;
13121 	struct bnxt_link_info *link_info = &bp->link_info;
13122 
13123 	rc = bnxt_update_link(bp, true);
13124 	if (rc) {
13125 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
13126 			   rc);
13127 		return rc;
13128 	}
13129 	if (!BNXT_SINGLE_PF(bp))
13130 		return 0;
13131 
13132 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
13133 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
13134 	    link_info->req_flow_ctrl)
13135 		update_pause = true;
13136 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
13137 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
13138 		update_pause = true;
13139 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
13140 		if (BNXT_AUTO_MODE(link_info->auto_mode))
13141 			update_link = true;
13142 		if (bnxt_force_speed_updated(link_info))
13143 			update_link = true;
13144 		if (link_info->req_duplex != link_info->duplex_setting)
13145 			update_link = true;
13146 	} else {
13147 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
13148 			update_link = true;
13149 		if (bnxt_auto_speed_updated(link_info))
13150 			update_link = true;
13151 	}
13152 
13153 	/* The last close may have shutdown the link, so need to call
13154 	 * PHY_CFG to bring it back up.
13155 	 */
13156 	if (!BNXT_LINK_IS_UP(bp))
13157 		update_link = true;
13158 
13159 	if (!bnxt_eee_config_ok(bp))
13160 		update_eee = true;
13161 
13162 	if (update_link)
13163 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
13164 	else if (update_pause)
13165 		rc = bnxt_hwrm_set_pause(bp);
13166 	if (rc) {
13167 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
13168 			   rc);
13169 		return rc;
13170 	}
13171 
13172 	return rc;
13173 }
13174 
13175 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
13176 
13177 static int bnxt_reinit_after_abort(struct bnxt *bp)
13178 {
13179 	int rc;
13180 
13181 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13182 		return -EBUSY;
13183 
13184 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
13185 		return -ENODEV;
13186 
13187 	rc = bnxt_fw_init_one(bp);
13188 	if (!rc) {
13189 		bnxt_clear_int_mode(bp);
13190 		rc = bnxt_init_int_mode(bp);
13191 		if (!rc) {
13192 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13193 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
13194 		}
13195 	}
13196 	return rc;
13197 }
13198 
13199 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
13200 {
13201 	struct bnxt_ntuple_filter *ntp_fltr;
13202 	struct bnxt_l2_filter *l2_fltr;
13203 
13204 	if (list_empty(&fltr->list))
13205 		return;
13206 
13207 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
13208 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
13209 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
13210 		atomic_inc(&l2_fltr->refcnt);
13211 		ntp_fltr->l2_fltr = l2_fltr;
13212 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
13213 			bnxt_del_ntp_filter(bp, ntp_fltr);
13214 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
13215 				   fltr->sw_id);
13216 		}
13217 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
13218 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
13219 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
13220 			bnxt_del_l2_filter(bp, l2_fltr);
13221 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
13222 				   fltr->sw_id);
13223 		}
13224 	}
13225 }
13226 
13227 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
13228 {
13229 	struct bnxt_filter_base *usr_fltr, *tmp;
13230 
13231 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
13232 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
13233 }
13234 
13235 static int bnxt_set_xps_mapping(struct bnxt *bp)
13236 {
13237 	int numa_node = dev_to_node(&bp->pdev->dev);
13238 	unsigned int q_idx, map_idx, cpu, i;
13239 	const struct cpumask *cpu_mask_ptr;
13240 	int nr_cpus = num_online_cpus();
13241 	cpumask_t *q_map;
13242 	int rc = 0;
13243 
13244 	q_map = kzalloc_objs(*q_map, bp->tx_nr_rings_per_tc);
13245 	if (!q_map)
13246 		return -ENOMEM;
13247 
13248 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
13249 	 * Each TC has the same number of TX queues. The nth TX queue for each
13250 	 * TC will have the same CPU mask.
13251 	 */
13252 	for (i = 0; i < nr_cpus; i++) {
13253 		map_idx = i % bp->tx_nr_rings_per_tc;
13254 		cpu = cpumask_local_spread(i, numa_node);
13255 		cpu_mask_ptr = get_cpu_mask(cpu);
13256 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
13257 	}
13258 
13259 	/* Register CPU mask for each TX queue except the ones marked for XDP */
13260 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
13261 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
13262 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
13263 		if (rc) {
13264 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
13265 				    q_idx);
13266 			break;
13267 		}
13268 	}
13269 
13270 	kfree(q_map);
13271 
13272 	return rc;
13273 }
13274 
13275 static int bnxt_tx_nr_rings(struct bnxt *bp)
13276 {
13277 	return bp->num_tc ? bp->tx_nr_rings_per_tc * bp->num_tc :
13278 			    bp->tx_nr_rings_per_tc;
13279 }
13280 
13281 static int bnxt_tx_nr_rings_per_tc(struct bnxt *bp)
13282 {
13283 	return bp->num_tc ? bp->tx_nr_rings / bp->num_tc : bp->tx_nr_rings;
13284 }
13285 
13286 static void bnxt_set_xdp_tx_rings(struct bnxt *bp)
13287 {
13288 	bp->tx_nr_rings_xdp = bp->tx_nr_rings_per_tc;
13289 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
13290 }
13291 
13292 static void bnxt_adj_tx_rings(struct bnxt *bp)
13293 {
13294 	/* Make adjustments if reserved TX rings are less than requested */
13295 	bp->tx_nr_rings -= bp->tx_nr_rings_xdp;
13296 	bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
13297 	if (bp->tx_nr_rings_xdp)
13298 		bnxt_set_xdp_tx_rings(bp);
13299 }
13300 
13301 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13302 {
13303 	int rc = 0;
13304 
13305 	netif_carrier_off(bp->dev);
13306 	if (irq_re_init) {
13307 		/* Reserve rings now if none were reserved at driver probe. */
13308 		rc = bnxt_init_dflt_ring_mode(bp);
13309 		if (rc) {
13310 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
13311 			return rc;
13312 		}
13313 	}
13314 	rc = bnxt_reserve_rings(bp, irq_re_init);
13315 	if (rc)
13316 		return rc;
13317 
13318 	bnxt_adj_tx_rings(bp);
13319 	rc = bnxt_alloc_mem(bp, irq_re_init);
13320 	if (rc) {
13321 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13322 		goto open_err_free_mem;
13323 	}
13324 
13325 	if (irq_re_init) {
13326 		bnxt_init_napi(bp);
13327 		rc = bnxt_request_irq(bp);
13328 		if (rc) {
13329 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
13330 			goto open_err_irq;
13331 		}
13332 	}
13333 
13334 	rc = bnxt_init_nic(bp, irq_re_init);
13335 	if (rc) {
13336 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13337 		goto open_err_irq;
13338 	}
13339 
13340 	bnxt_enable_napi(bp);
13341 	bnxt_debug_dev_init(bp);
13342 
13343 	if (link_re_init) {
13344 		mutex_lock(&bp->link_lock);
13345 		rc = bnxt_update_phy_setting(bp);
13346 		mutex_unlock(&bp->link_lock);
13347 		if (rc) {
13348 			netdev_warn(bp->dev, "failed to update phy settings\n");
13349 			if (BNXT_SINGLE_PF(bp)) {
13350 				bp->link_info.phy_retry = true;
13351 				bp->link_info.phy_retry_expires =
13352 					jiffies + 5 * HZ;
13353 			}
13354 		}
13355 	}
13356 
13357 	if (irq_re_init) {
13358 		udp_tunnel_nic_reset_ntf(bp->dev);
13359 		rc = bnxt_set_xps_mapping(bp);
13360 		if (rc)
13361 			netdev_warn(bp->dev, "failed to set xps mapping\n");
13362 	}
13363 
13364 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
13365 		if (!static_key_enabled(&bnxt_xdp_locking_key))
13366 			static_branch_enable(&bnxt_xdp_locking_key);
13367 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
13368 		static_branch_disable(&bnxt_xdp_locking_key);
13369 	}
13370 	set_bit(BNXT_STATE_OPEN, &bp->state);
13371 	bnxt_enable_int(bp);
13372 	/* Enable TX queues */
13373 	bnxt_tx_enable(bp);
13374 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13375 	/* Poll link status and check for SFP+ module status */
13376 	mutex_lock(&bp->link_lock);
13377 	bnxt_get_port_module_status(bp);
13378 	mutex_unlock(&bp->link_lock);
13379 
13380 	/* VF-reps may need to be re-opened after the PF is re-opened */
13381 	if (BNXT_PF(bp))
13382 		bnxt_vf_reps_open(bp);
13383 	bnxt_ptp_init_rtc(bp, true);
13384 	bnxt_ptp_cfg_tstamp_filters(bp);
13385 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13386 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
13387 	bnxt_cfg_usr_fltrs(bp);
13388 	return 0;
13389 
13390 open_err_irq:
13391 	bnxt_del_napi(bp);
13392 
13393 open_err_free_mem:
13394 	bnxt_free_skbs(bp);
13395 	bnxt_free_irq(bp);
13396 	bnxt_free_mem(bp, true);
13397 	return rc;
13398 }
13399 
13400 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13401 {
13402 	int rc = 0;
13403 
13404 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
13405 		rc = -EIO;
13406 	if (!rc)
13407 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
13408 	if (rc) {
13409 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
13410 		netif_close(bp->dev);
13411 	}
13412 	return rc;
13413 }
13414 
13415 /* netdev instance lock held, open the NIC half way by allocating all
13416  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
13417  * for offline self tests.
13418  */
13419 int bnxt_half_open_nic(struct bnxt *bp)
13420 {
13421 	int rc = 0;
13422 
13423 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13424 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
13425 		rc = -ENODEV;
13426 		goto half_open_err;
13427 	}
13428 
13429 	rc = bnxt_alloc_mem(bp, true);
13430 	if (rc) {
13431 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13432 		goto half_open_err;
13433 	}
13434 	bnxt_init_napi(bp);
13435 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13436 	rc = bnxt_init_nic(bp, true);
13437 	if (rc) {
13438 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13439 		bnxt_del_napi(bp);
13440 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13441 		goto half_open_err;
13442 	}
13443 	return 0;
13444 
13445 half_open_err:
13446 	bnxt_free_skbs(bp);
13447 	bnxt_free_mem(bp, true);
13448 	netif_close(bp->dev);
13449 	return rc;
13450 }
13451 
13452 /* netdev instance lock held, this call can only be made after a previous
13453  * successful call to bnxt_half_open_nic().
13454  */
13455 void bnxt_half_close_nic(struct bnxt *bp)
13456 {
13457 	bnxt_hwrm_resource_free(bp, false, true);
13458 	bnxt_del_napi(bp);
13459 	bnxt_free_skbs(bp);
13460 	bnxt_free_mem(bp, true);
13461 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13462 }
13463 
13464 void bnxt_reenable_sriov(struct bnxt *bp)
13465 {
13466 	if (BNXT_PF(bp)) {
13467 		struct bnxt_pf_info *pf = &bp->pf;
13468 		int n = pf->active_vfs;
13469 
13470 		if (n)
13471 			bnxt_cfg_hw_sriov(bp, &n, true);
13472 	}
13473 }
13474 
13475 static int bnxt_open(struct net_device *dev)
13476 {
13477 	struct bnxt *bp = netdev_priv(dev);
13478 	int rc;
13479 
13480 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13481 		rc = bnxt_reinit_after_abort(bp);
13482 		if (rc) {
13483 			if (rc == -EBUSY)
13484 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
13485 			else
13486 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
13487 			return -ENODEV;
13488 		}
13489 	}
13490 
13491 	rc = bnxt_hwrm_if_change(bp, true);
13492 	if (rc)
13493 		return rc;
13494 
13495 	rc = __bnxt_open_nic(bp, true, true);
13496 	if (rc) {
13497 		bnxt_hwrm_if_change(bp, false);
13498 	} else {
13499 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
13500 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13501 				bnxt_queue_sp_work(bp,
13502 						   BNXT_RESTART_ULP_SP_EVENT);
13503 		}
13504 	}
13505 
13506 	return rc;
13507 }
13508 
13509 static bool bnxt_drv_busy(struct bnxt *bp)
13510 {
13511 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
13512 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
13513 }
13514 
13515 static void bnxt_get_ring_stats(struct bnxt *bp,
13516 				struct rtnl_link_stats64 *stats);
13517 
13518 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
13519 			     bool link_re_init)
13520 {
13521 	/* Close the VF-reps before closing PF */
13522 	if (BNXT_PF(bp))
13523 		bnxt_vf_reps_close(bp);
13524 
13525 	/* Change device state to avoid TX queue wake up's */
13526 	bnxt_tx_disable(bp);
13527 
13528 	clear_bit(BNXT_STATE_OPEN, &bp->state);
13529 	smp_mb__after_atomic();
13530 	while (bnxt_drv_busy(bp))
13531 		msleep(20);
13532 
13533 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13534 		bnxt_clear_rss_ctxs(bp);
13535 	/* Flush rings and disable interrupts */
13536 	bnxt_shutdown_nic(bp, irq_re_init);
13537 
13538 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
13539 
13540 	bnxt_debug_dev_exit(bp);
13541 	bnxt_disable_napi(bp);
13542 	timer_delete_sync(&bp->timer);
13543 	bnxt_free_skbs(bp);
13544 
13545 	/* Save ring stats before shutdown */
13546 	if (bp->bnapi && irq_re_init) {
13547 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
13548 		bnxt_get_ring_drv_stats(bp, &bp->ring_drv_stats_prev);
13549 	}
13550 	if (irq_re_init) {
13551 		bnxt_free_irq(bp);
13552 		bnxt_del_napi(bp);
13553 	}
13554 	bnxt_free_mem(bp, irq_re_init);
13555 }
13556 
13557 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13558 {
13559 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13560 		/* If we get here, it means firmware reset is in progress
13561 		 * while we are trying to close.  We can safely proceed with
13562 		 * the close because we are holding netdev instance lock.
13563 		 * Some firmware messages may fail as we proceed to close.
13564 		 * We set the ABORT_ERR flag here so that the FW reset thread
13565 		 * will later abort when it gets the netdev instance lock
13566 		 * and sees the flag.
13567 		 */
13568 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
13569 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13570 	}
13571 
13572 #ifdef CONFIG_BNXT_SRIOV
13573 	if (bp->sriov_cfg) {
13574 		int rc;
13575 
13576 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
13577 						      !bp->sriov_cfg,
13578 						      BNXT_SRIOV_CFG_WAIT_TMO);
13579 		if (!rc)
13580 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13581 		else if (rc < 0)
13582 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13583 	}
13584 #endif
13585 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13586 }
13587 
13588 static int bnxt_close(struct net_device *dev)
13589 {
13590 	struct bnxt *bp = netdev_priv(dev);
13591 
13592 	bnxt_close_nic(bp, true, true);
13593 	bnxt_hwrm_shutdown_link(bp);
13594 	bnxt_hwrm_if_change(bp, false);
13595 	return 0;
13596 }
13597 
13598 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13599 				   u16 *val)
13600 {
13601 	struct hwrm_port_phy_mdio_read_output *resp;
13602 	struct hwrm_port_phy_mdio_read_input *req;
13603 	int rc;
13604 
13605 	if (bp->hwrm_spec_code < 0x10a00)
13606 		return -EOPNOTSUPP;
13607 
13608 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13609 	if (rc)
13610 		return rc;
13611 
13612 	req->port_id = cpu_to_le16(bp->pf.port_id);
13613 	req->phy_addr = phy_addr;
13614 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13615 	if (mdio_phy_id_is_c45(phy_addr)) {
13616 		req->cl45_mdio = 1;
13617 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13618 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13619 		req->reg_addr = cpu_to_le16(reg);
13620 	}
13621 
13622 	resp = hwrm_req_hold(bp, req);
13623 	rc = hwrm_req_send(bp, req);
13624 	if (!rc)
13625 		*val = le16_to_cpu(resp->reg_data);
13626 	hwrm_req_drop(bp, req);
13627 	return rc;
13628 }
13629 
13630 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13631 				    u16 val)
13632 {
13633 	struct hwrm_port_phy_mdio_write_input *req;
13634 	int rc;
13635 
13636 	if (bp->hwrm_spec_code < 0x10a00)
13637 		return -EOPNOTSUPP;
13638 
13639 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13640 	if (rc)
13641 		return rc;
13642 
13643 	req->port_id = cpu_to_le16(bp->pf.port_id);
13644 	req->phy_addr = phy_addr;
13645 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13646 	if (mdio_phy_id_is_c45(phy_addr)) {
13647 		req->cl45_mdio = 1;
13648 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13649 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13650 		req->reg_addr = cpu_to_le16(reg);
13651 	}
13652 	req->reg_data = cpu_to_le16(val);
13653 
13654 	return hwrm_req_send(bp, req);
13655 }
13656 
13657 /* netdev instance lock held */
13658 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13659 {
13660 	struct mii_ioctl_data *mdio = if_mii(ifr);
13661 	struct bnxt *bp = netdev_priv(dev);
13662 	int rc;
13663 
13664 	switch (cmd) {
13665 	case SIOCGMIIPHY:
13666 		mdio->phy_id = bp->link_info.phy_addr;
13667 
13668 		fallthrough;
13669 	case SIOCGMIIREG: {
13670 		u16 mii_regval = 0;
13671 
13672 		if (!netif_running(dev))
13673 			return -EAGAIN;
13674 
13675 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13676 					     &mii_regval);
13677 		mdio->val_out = mii_regval;
13678 		return rc;
13679 	}
13680 
13681 	case SIOCSMIIREG:
13682 		if (!netif_running(dev))
13683 			return -EAGAIN;
13684 
13685 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13686 						mdio->val_in);
13687 
13688 	default:
13689 		/* do nothing */
13690 		break;
13691 	}
13692 	return -EOPNOTSUPP;
13693 }
13694 
13695 static void bnxt_get_ring_stats(struct bnxt *bp,
13696 				struct rtnl_link_stats64 *stats)
13697 {
13698 	int i;
13699 
13700 	for (i = 0; i < bp->cp_nr_rings; i++) {
13701 		struct bnxt_napi *bnapi = bp->bnapi[i];
13702 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13703 		u64 *sw = cpr->stats.sw_stats;
13704 
13705 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13706 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13707 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13708 
13709 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13710 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13711 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13712 
13713 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13714 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13715 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13716 
13717 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13718 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13719 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13720 
13721 		stats->rx_missed_errors +=
13722 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13723 
13724 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13725 
13726 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13727 
13728 		stats->rx_dropped +=
13729 			cpr->sw_stats->rx.rx_netpoll_discards +
13730 			cpr->sw_stats->rx.rx_oom_discards;
13731 	}
13732 }
13733 
13734 static void bnxt_add_prev_stats(struct bnxt *bp,
13735 				struct rtnl_link_stats64 *stats)
13736 {
13737 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13738 
13739 	stats->rx_packets += prev_stats->rx_packets;
13740 	stats->tx_packets += prev_stats->tx_packets;
13741 	stats->rx_bytes += prev_stats->rx_bytes;
13742 	stats->tx_bytes += prev_stats->tx_bytes;
13743 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13744 	stats->multicast += prev_stats->multicast;
13745 	stats->rx_dropped += prev_stats->rx_dropped;
13746 	stats->tx_dropped += prev_stats->tx_dropped;
13747 }
13748 
13749 static void
13750 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13751 {
13752 	struct bnxt *bp = netdev_priv(dev);
13753 
13754 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13755 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13756 	 * we check the BNXT_STATE_OPEN flag.
13757 	 */
13758 	smp_mb__after_atomic();
13759 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13760 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13761 		*stats = bp->net_stats_prev;
13762 		return;
13763 	}
13764 
13765 	bnxt_sync_ring_stats(bp);
13766 	bnxt_get_ring_stats(bp, stats);
13767 	bnxt_add_prev_stats(bp, stats);
13768 
13769 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13770 		u64 *rx = bp->port_stats.sw_stats;
13771 		u64 *tx = bp->port_stats.sw_stats +
13772 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13773 
13774 		stats->rx_crc_errors =
13775 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13776 		stats->rx_frame_errors =
13777 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13778 		stats->rx_length_errors =
13779 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13780 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13781 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13782 		stats->rx_errors =
13783 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13784 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13785 		stats->collisions =
13786 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13787 		stats->tx_fifo_errors =
13788 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13789 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13790 	}
13791 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13792 }
13793 
13794 static void bnxt_get_one_ring_drv_stats(struct bnxt *bp,
13795 					struct bnxt_total_ring_drv_stats *stats,
13796 					struct bnxt_cp_ring_info *cpr)
13797 {
13798 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13799 	u64 *hw_stats = cpr->stats.sw_stats;
13800 
13801 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13802 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13803 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13804 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13805 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13806 	stats->rx_total_ring_discards +=
13807 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13808 	stats->rx_total_hw_gro_packets += sw_stats->rx.rx_hw_gro_packets;
13809 	stats->rx_total_hw_gro_wire_packets += sw_stats->rx.rx_hw_gro_wire_packets;
13810 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13811 	stats->tx_total_ring_discards +=
13812 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13813 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13814 }
13815 
13816 void bnxt_get_ring_drv_stats(struct bnxt *bp,
13817 			     struct bnxt_total_ring_drv_stats *stats)
13818 {
13819 	int i;
13820 
13821 	for (i = 0; i < bp->cp_nr_rings; i++)
13822 		bnxt_get_one_ring_drv_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13823 }
13824 
13825 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask,
13826 				 const struct netdev_hw_addr_list *mc)
13827 {
13828 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13829 	struct netdev_hw_addr *ha;
13830 	u8 *haddr;
13831 	int mc_count = 0;
13832 	bool update = false;
13833 	int off = 0;
13834 
13835 	netdev_hw_addr_list_for_each(ha, mc) {
13836 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13837 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13838 			vnic->mc_list_count = 0;
13839 			return false;
13840 		}
13841 		haddr = ha->addr;
13842 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13843 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13844 			update = true;
13845 		}
13846 		off += ETH_ALEN;
13847 		mc_count++;
13848 	}
13849 	if (mc_count)
13850 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13851 
13852 	if (mc_count != vnic->mc_list_count) {
13853 		vnic->mc_list_count = mc_count;
13854 		update = true;
13855 	}
13856 	return update;
13857 }
13858 
13859 static bool bnxt_uc_list_updated(struct bnxt *bp,
13860 				 const struct netdev_hw_addr_list *uc)
13861 {
13862 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13863 	struct netdev_hw_addr *ha;
13864 	int off = 0;
13865 
13866 	if (netdev_hw_addr_list_count(uc) != (vnic->uc_filter_count - 1))
13867 		return true;
13868 
13869 	netdev_hw_addr_list_for_each(ha, uc) {
13870 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13871 			return true;
13872 
13873 		off += ETH_ALEN;
13874 	}
13875 	return false;
13876 }
13877 
13878 static int bnxt_set_rx_mode(struct net_device *dev,
13879 			    struct netdev_hw_addr_list *uc,
13880 			    struct netdev_hw_addr_list *mc)
13881 {
13882 	struct bnxt *bp = netdev_priv(dev);
13883 	struct bnxt_vnic_info *vnic;
13884 	bool mc_update = false;
13885 	bool uc_update;
13886 	u32 mask;
13887 
13888 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13889 		return 0;
13890 
13891 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13892 	mask = vnic->rx_mask;
13893 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13894 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13895 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13896 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13897 
13898 	if (dev->flags & IFF_PROMISC)
13899 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13900 
13901 	uc_update = bnxt_uc_list_updated(bp, uc);
13902 
13903 	if (dev->flags & IFF_BROADCAST)
13904 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13905 	if (dev->flags & IFF_ALLMULTI) {
13906 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13907 		vnic->mc_list_count = 0;
13908 	} else if (dev->flags & IFF_MULTICAST) {
13909 		mc_update = bnxt_mc_list_updated(bp, &mask, mc);
13910 	}
13911 
13912 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13913 		vnic->rx_mask = mask;
13914 
13915 		return bnxt_cfg_rx_mode(bp, uc, uc_update);
13916 	}
13917 
13918 	return 0;
13919 }
13920 
13921 static int bnxt_cfg_rx_mode(struct bnxt *bp, struct netdev_hw_addr_list *uc,
13922 			    bool uc_update)
13923 {
13924 	struct net_device *dev = bp->dev;
13925 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13926 	struct netdev_hw_addr *ha;
13927 	int i, off = 0, rc;
13928 
13929 	if (!uc_update)
13930 		goto skip_uc;
13931 
13932 	for (i = 1; i < vnic->uc_filter_count; i++) {
13933 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13934 
13935 		bnxt_hwrm_l2_filter_free(bp, fltr);
13936 		bnxt_del_l2_filter(bp, fltr);
13937 	}
13938 
13939 	vnic->uc_filter_count = 1;
13940 
13941 	netif_addr_lock_bh(dev);
13942 	if (netdev_hw_addr_list_count(uc) > (BNXT_MAX_UC_ADDRS - 1)) {
13943 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13944 	} else {
13945 		netdev_hw_addr_list_for_each(ha, uc) {
13946 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13947 			off += ETH_ALEN;
13948 			vnic->uc_filter_count++;
13949 		}
13950 	}
13951 	netif_addr_unlock_bh(dev);
13952 
13953 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13954 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13955 		if (rc) {
13956 			if (rc == -ECOMM) {
13957 				netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13958 				rc = -EAGAIN;
13959 			} else if (rc == -EAGAIN) {
13960 				netdev_warn(bp->dev, "FW busy while setting vnic filter, will retry\n");
13961 			} else {
13962 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13963 			}
13964 			vnic->uc_filter_count = i;
13965 			return rc;
13966 		}
13967 	}
13968 
13969 skip_uc:
13970 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13971 	    !bnxt_promisc_ok(bp))
13972 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13973 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13974 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13975 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13976 			    rc);
13977 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13978 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13979 		vnic->mc_list_count = 0;
13980 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13981 	}
13982 	if (rc)
13983 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13984 			   rc);
13985 
13986 	return rc;
13987 }
13988 
13989 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13990 {
13991 #ifdef CONFIG_BNXT_SRIOV
13992 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13993 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13994 
13995 		/* No minimum rings were provisioned by the PF.  Don't
13996 		 * reserve rings by default when device is down.
13997 		 */
13998 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13999 			return true;
14000 
14001 		if (!netif_running(bp->dev))
14002 			return false;
14003 	}
14004 #endif
14005 	return true;
14006 }
14007 
14008 /* If the chip and firmware supports RFS */
14009 static bool bnxt_rfs_supported(struct bnxt *bp)
14010 {
14011 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
14012 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
14013 			return true;
14014 		return false;
14015 	}
14016 	/* 212 firmware is broken for aRFS */
14017 	if (BNXT_FW_MAJ(bp) == 212)
14018 		return false;
14019 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
14020 		return true;
14021 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
14022 		return true;
14023 	return false;
14024 }
14025 
14026 /* If runtime conditions support RFS */
14027 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
14028 {
14029 	struct bnxt_hw_rings hwr = {0};
14030 	int max_vnics, max_rss_ctxs;
14031 
14032 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14033 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
14034 		return bnxt_rfs_supported(bp);
14035 
14036 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
14037 		return false;
14038 
14039 	hwr.grp = bp->rx_nr_rings;
14040 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
14041 	if (new_rss_ctx)
14042 		hwr.vnic++;
14043 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14044 	max_vnics = bnxt_get_max_func_vnics(bp);
14045 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
14046 
14047 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
14048 		if (bp->rx_nr_rings > 1)
14049 			netdev_warn(bp->dev,
14050 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
14051 				    min(max_rss_ctxs - 1, max_vnics - 1));
14052 		return false;
14053 	}
14054 
14055 	if (!BNXT_NEW_RM(bp))
14056 		return true;
14057 
14058 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
14059 	 * issue that will mess up the default VNIC if we reduce the
14060 	 * reservations.
14061 	 */
14062 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
14063 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
14064 		return true;
14065 
14066 	bnxt_hwrm_reserve_rings(bp, &hwr);
14067 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
14068 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
14069 		return true;
14070 
14071 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
14072 	hwr.vnic = 1;
14073 	hwr.rss_ctx = 0;
14074 	bnxt_hwrm_reserve_rings(bp, &hwr);
14075 	return false;
14076 }
14077 
14078 static netdev_features_t bnxt_fix_features(struct net_device *dev,
14079 					   netdev_features_t features)
14080 {
14081 	struct bnxt *bp = netdev_priv(dev);
14082 	netdev_features_t vlan_features;
14083 
14084 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
14085 		features &= ~NETIF_F_NTUPLE;
14086 
14087 	if ((features & NETIF_F_GSO_UDP_L4) &&
14088 	    !(bp->flags & BNXT_FLAG_UDP_GSO_CAP) &&
14089 	    bp->tx_ring_size < 2 * BNXT_SW_USO_MAX_DESCS)
14090 		features &= ~NETIF_F_GSO_UDP_L4;
14091 
14092 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
14093 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
14094 
14095 	if (!(features & NETIF_F_GRO))
14096 		features &= ~NETIF_F_GRO_HW;
14097 
14098 	if (features & NETIF_F_GRO_HW)
14099 		features &= ~NETIF_F_LRO;
14100 
14101 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
14102 	 * turned on or off together.
14103 	 */
14104 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
14105 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
14106 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
14107 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
14108 		else if (vlan_features)
14109 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
14110 	}
14111 #ifdef CONFIG_BNXT_SRIOV
14112 	if (BNXT_VF(bp) && bp->vf.vlan)
14113 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
14114 #endif
14115 	return features;
14116 }
14117 
14118 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
14119 				bool link_re_init, u32 flags, bool update_tpa)
14120 {
14121 	bnxt_close_nic(bp, irq_re_init, link_re_init);
14122 	bp->flags = flags;
14123 	if (update_tpa)
14124 		bnxt_set_ring_params(bp);
14125 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
14126 }
14127 
14128 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
14129 {
14130 	bool update_tpa = false, update_ntuple = false;
14131 	struct bnxt *bp = netdev_priv(dev);
14132 	u32 flags = bp->flags;
14133 	u32 changes;
14134 	int rc = 0;
14135 	bool re_init = false;
14136 
14137 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
14138 				   bnxt_min_tx_desc_cnt(bp, features));
14139 
14140 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
14141 	if (features & NETIF_F_GRO_HW)
14142 		flags |= BNXT_FLAG_GRO;
14143 	else if (features & NETIF_F_LRO)
14144 		flags |= BNXT_FLAG_LRO;
14145 
14146 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
14147 		flags &= ~BNXT_FLAG_TPA;
14148 
14149 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
14150 		flags |= BNXT_FLAG_STRIP_VLAN;
14151 
14152 	if (features & NETIF_F_NTUPLE)
14153 		flags |= BNXT_FLAG_RFS;
14154 	else
14155 		bnxt_clear_usr_fltrs(bp, true);
14156 
14157 	changes = flags ^ bp->flags;
14158 	if (changes & BNXT_FLAG_TPA) {
14159 		update_tpa = true;
14160 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
14161 		    (flags & BNXT_FLAG_TPA) == 0 ||
14162 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14163 			re_init = true;
14164 	}
14165 
14166 	if (changes & ~BNXT_FLAG_TPA)
14167 		re_init = true;
14168 
14169 	if (changes & BNXT_FLAG_RFS)
14170 		update_ntuple = true;
14171 
14172 	if (flags != bp->flags) {
14173 		u32 old_flags = bp->flags;
14174 
14175 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14176 			bp->flags = flags;
14177 			if (update_tpa)
14178 				bnxt_set_ring_params(bp);
14179 			return rc;
14180 		}
14181 
14182 		if (update_ntuple)
14183 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
14184 
14185 		if (re_init)
14186 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
14187 
14188 		if (update_tpa) {
14189 			bp->flags = flags;
14190 			rc = bnxt_set_tpa(bp,
14191 					  (flags & BNXT_FLAG_TPA) ?
14192 					  true : false);
14193 			if (rc)
14194 				bp->flags = old_flags;
14195 		}
14196 	}
14197 	return rc;
14198 }
14199 
14200 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
14201 			      u8 **nextp)
14202 {
14203 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
14204 	int hdr_count = 0;
14205 	u8 *nexthdr;
14206 	int start;
14207 
14208 	/* Check that there are at most 2 IPv6 extension headers, no
14209 	 * fragment header, and each is <= 64 bytes.
14210 	 */
14211 	start = nw_off + sizeof(*ip6h);
14212 	nexthdr = &ip6h->nexthdr;
14213 	while (ipv6_ext_hdr(*nexthdr)) {
14214 		struct ipv6_opt_hdr *hp;
14215 		int hdrlen;
14216 
14217 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
14218 		    *nexthdr == NEXTHDR_FRAGMENT)
14219 			return false;
14220 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
14221 					  skb_headlen(skb), NULL);
14222 		if (!hp)
14223 			return false;
14224 		if (*nexthdr == NEXTHDR_AUTH)
14225 			hdrlen = ipv6_authlen(hp);
14226 		else
14227 			hdrlen = ipv6_optlen(hp);
14228 
14229 		if (hdrlen > 64)
14230 			return false;
14231 
14232 		hdr_count++;
14233 		nexthdr = &hp->nexthdr;
14234 		start += hdrlen;
14235 	}
14236 	if (nextp) {
14237 		/* Caller will check inner protocol */
14238 		if (skb->encapsulation) {
14239 			*nextp = nexthdr;
14240 			return true;
14241 		}
14242 		*nextp = NULL;
14243 	}
14244 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
14245 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
14246 }
14247 
14248 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
14249 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
14250 {
14251 	struct udphdr *uh = udp_hdr(skb);
14252 	__be16 udp_port = uh->dest;
14253 
14254 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
14255 	    udp_port != bp->vxlan_gpe_port)
14256 		return false;
14257 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
14258 		struct ethhdr *eh = inner_eth_hdr(skb);
14259 
14260 		switch (eh->h_proto) {
14261 		case htons(ETH_P_IP):
14262 			return true;
14263 		case htons(ETH_P_IPV6):
14264 			return bnxt_exthdr_check(bp, skb,
14265 						 skb_inner_network_offset(skb),
14266 						 NULL);
14267 		}
14268 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
14269 		return true;
14270 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
14271 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
14272 					 NULL);
14273 	}
14274 	return false;
14275 }
14276 
14277 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
14278 {
14279 	switch (l4_proto) {
14280 	case IPPROTO_UDP:
14281 		return bnxt_udp_tunl_check(bp, skb);
14282 	case IPPROTO_IPIP:
14283 		return true;
14284 	case IPPROTO_GRE: {
14285 		switch (skb->inner_protocol) {
14286 		default:
14287 			return false;
14288 		case htons(ETH_P_IP):
14289 			return true;
14290 		case htons(ETH_P_IPV6):
14291 			fallthrough;
14292 		}
14293 	}
14294 	case IPPROTO_IPV6:
14295 		/* Check ext headers of inner ipv6 */
14296 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
14297 					 NULL);
14298 	}
14299 	return false;
14300 }
14301 
14302 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
14303 					     struct net_device *dev,
14304 					     netdev_features_t features)
14305 {
14306 	struct bnxt *bp = netdev_priv(dev);
14307 	u8 *l4_proto;
14308 
14309 	features = vlan_features_check(skb, features);
14310 
14311 	/* SW USO pads at most the last segment; let the stack segment skbs
14312 	 * where every segment would be shorter than BNXT_MIN_PKT_SIZE.
14313 	 */
14314 	if (skb_is_gso(skb) &&
14315 	    (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) &&
14316 	    !(bp->flags & BNXT_FLAG_UDP_GSO_CAP) &&
14317 	    bnxt_sw_gso_pad_len(skb_transport_offset(skb) +
14318 				sizeof(struct udphdr),
14319 				skb_shinfo(skb)->gso_size))
14320 		features &= ~NETIF_F_GSO_MASK;
14321 
14322 	switch (vlan_get_protocol(skb)) {
14323 	case htons(ETH_P_IP):
14324 		if (!skb->encapsulation)
14325 			return features;
14326 		l4_proto = &ip_hdr(skb)->protocol;
14327 		if (bnxt_tunl_check(bp, skb, *l4_proto))
14328 			return features;
14329 		break;
14330 	case htons(ETH_P_IPV6):
14331 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
14332 				       &l4_proto))
14333 			break;
14334 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
14335 			return features;
14336 		break;
14337 	}
14338 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
14339 }
14340 
14341 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
14342 			 u32 *reg_buf)
14343 {
14344 	struct hwrm_dbg_read_direct_output *resp;
14345 	struct hwrm_dbg_read_direct_input *req;
14346 	__le32 *dbg_reg_buf;
14347 	dma_addr_t mapping;
14348 	int rc, i;
14349 
14350 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
14351 	if (rc)
14352 		return rc;
14353 
14354 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
14355 					 &mapping);
14356 	if (!dbg_reg_buf) {
14357 		rc = -ENOMEM;
14358 		goto dbg_rd_reg_exit;
14359 	}
14360 
14361 	req->host_dest_addr = cpu_to_le64(mapping);
14362 
14363 	resp = hwrm_req_hold(bp, req);
14364 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
14365 	req->read_len32 = cpu_to_le32(num_words);
14366 
14367 	rc = hwrm_req_send(bp, req);
14368 	if (rc || resp->error_code) {
14369 		rc = -EIO;
14370 		goto dbg_rd_reg_exit;
14371 	}
14372 	for (i = 0; i < num_words; i++)
14373 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
14374 
14375 dbg_rd_reg_exit:
14376 	hwrm_req_drop(bp, req);
14377 	return rc;
14378 }
14379 
14380 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
14381 				       u32 ring_id, u32 *prod, u32 *cons)
14382 {
14383 	struct hwrm_dbg_ring_info_get_output *resp;
14384 	struct hwrm_dbg_ring_info_get_input *req;
14385 	int rc;
14386 
14387 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
14388 	if (rc)
14389 		return rc;
14390 
14391 	req->ring_type = ring_type;
14392 	req->fw_ring_id = cpu_to_le32(ring_id);
14393 	resp = hwrm_req_hold(bp, req);
14394 	rc = hwrm_req_send(bp, req);
14395 	if (!rc) {
14396 		*prod = le32_to_cpu(resp->producer_index);
14397 		*cons = le32_to_cpu(resp->consumer_index);
14398 	}
14399 	hwrm_req_drop(bp, req);
14400 	return rc;
14401 }
14402 
14403 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
14404 {
14405 	struct bnxt_tx_ring_info *txr;
14406 	int i = bnapi->index, j;
14407 
14408 	bnxt_for_each_napi_tx(j, bnapi, txr)
14409 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
14410 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
14411 			    txr->tx_cons);
14412 }
14413 
14414 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
14415 {
14416 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
14417 	int i = bnapi->index;
14418 
14419 	if (!rxr)
14420 		return;
14421 
14422 	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",
14423 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
14424 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
14425 		    rxr->rx_sw_agg_prod);
14426 }
14427 
14428 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
14429 {
14430 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring, *cpr2;
14431 	int i = bnapi->index, j;
14432 
14433 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
14434 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
14435 	for (j = 0; j < cpr->cp_ring_count; j++) {
14436 		cpr2 = &cpr->cp_ring_arr[j];
14437 		if (!cpr2->bnapi)
14438 			continue;
14439 		netdev_info(bnapi->bp->dev, "[%d.%d]: cp{fw_ring: %d raw_cons: %x}\n",
14440 			    i, j, cpr2->cp_ring_struct.fw_ring_id,
14441 			    cpr2->cp_raw_cons);
14442 	}
14443 }
14444 
14445 static void bnxt_dbg_dump_states(struct bnxt *bp)
14446 {
14447 	int i;
14448 	struct bnxt_napi *bnapi;
14449 
14450 	for (i = 0; i < bp->cp_nr_rings; i++) {
14451 		bnapi = bp->bnapi[i];
14452 		if (netif_msg_drv(bp)) {
14453 			bnxt_dump_tx_sw_state(bnapi);
14454 			bnxt_dump_rx_sw_state(bnapi);
14455 			bnxt_dump_cp_sw_state(bnapi);
14456 		}
14457 	}
14458 }
14459 
14460 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
14461 {
14462 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
14463 	struct hwrm_ring_reset_input *req;
14464 	struct bnxt_napi *bnapi = rxr->bnapi;
14465 	struct bnxt_cp_ring_info *cpr;
14466 	u16 cp_ring_id;
14467 	int rc;
14468 
14469 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
14470 	if (rc)
14471 		return rc;
14472 
14473 	cpr = &bnapi->cp_ring;
14474 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
14475 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
14476 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
14477 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
14478 	return hwrm_req_send_silent(bp, req);
14479 }
14480 
14481 static void bnxt_reset_task(struct bnxt *bp, bool silent)
14482 {
14483 	if (!silent)
14484 		bnxt_dbg_dump_states(bp);
14485 	if (netif_running(bp->dev)) {
14486 		bnxt_close_nic(bp, !silent, false);
14487 		bnxt_open_nic(bp, !silent, false);
14488 	}
14489 }
14490 
14491 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
14492 {
14493 	struct bnxt *bp = netdev_priv(dev);
14494 
14495 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
14496 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
14497 }
14498 
14499 static void bnxt_fw_health_check(struct bnxt *bp)
14500 {
14501 	struct bnxt_fw_health *fw_health = bp->fw_health;
14502 	struct pci_dev *pdev = bp->pdev;
14503 	u32 val;
14504 
14505 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14506 		return;
14507 
14508 	/* Make sure it is enabled before checking the tmr_counter. */
14509 	smp_rmb();
14510 	if (fw_health->tmr_counter) {
14511 		fw_health->tmr_counter--;
14512 		return;
14513 	}
14514 
14515 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14516 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
14517 		fw_health->arrests++;
14518 		goto fw_reset;
14519 	}
14520 
14521 	fw_health->last_fw_heartbeat = val;
14522 
14523 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14524 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
14525 		fw_health->discoveries++;
14526 		goto fw_reset;
14527 	}
14528 
14529 	fw_health->tmr_counter = fw_health->tmr_multiplier;
14530 	return;
14531 
14532 fw_reset:
14533 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
14534 }
14535 
14536 static void bnxt_timer(struct timer_list *t)
14537 {
14538 	struct bnxt *bp = timer_container_of(bp, t, timer);
14539 	struct net_device *dev = bp->dev;
14540 
14541 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
14542 		return;
14543 
14544 	if (atomic_read(&bp->intr_sem) != 0)
14545 		goto bnxt_restart_timer;
14546 
14547 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
14548 		bnxt_fw_health_check(bp);
14549 
14550 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
14551 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
14552 
14553 	if (bnxt_tc_flower_enabled(bp))
14554 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
14555 
14556 #ifdef CONFIG_RFS_ACCEL
14557 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
14558 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14559 #endif /*CONFIG_RFS_ACCEL*/
14560 
14561 	if (bp->link_info.phy_retry) {
14562 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
14563 			bp->link_info.phy_retry = false;
14564 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
14565 		} else {
14566 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
14567 		}
14568 	}
14569 
14570 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
14571 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
14572 
14573 bnxt_restart_timer:
14574 	mod_timer(&bp->timer, jiffies + bp->current_interval);
14575 }
14576 
14577 static void bnxt_lock_sp(struct bnxt *bp)
14578 {
14579 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
14580 	 * set.  If the device is being closed, bnxt_close() may be holding
14581 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14582 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14583 	 * instance lock.
14584 	 */
14585 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14586 	netdev_lock(bp->dev);
14587 }
14588 
14589 static void bnxt_unlock_sp(struct bnxt *bp)
14590 {
14591 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14592 	netdev_unlock(bp->dev);
14593 }
14594 
14595 /* Same as bnxt_lock_sp() with additional rtnl_lock */
14596 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
14597 {
14598 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14599 	rtnl_lock();
14600 	netdev_lock(bp->dev);
14601 }
14602 
14603 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
14604 {
14605 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14606 	netdev_unlock(bp->dev);
14607 	rtnl_unlock();
14608 }
14609 
14610 static void bnxt_tph_update(struct bnxt *bp)
14611 {
14612 	struct net_device *dev = bp->dev;
14613 	int i;
14614 
14615 	bnxt_lock_sp(bp);
14616 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
14617 		goto unlock;
14618 
14619 	for (i = 0; i < bp->rx_nr_rings; i++) {
14620 		struct bnxt_irq *irq;
14621 		int map_idx, err;
14622 		u16 tag;
14623 
14624 		map_idx = bnxt_cp_num_to_irq_num(bp, i);
14625 		irq = &bp->irq_tbl[map_idx];
14626 		tag = READ_ONCE(irq->new_tag);
14627 		if (irq->tag == tag)
14628 			continue;
14629 
14630 		if (pcie_tph_set_st_entry(bp->pdev, irq->msix_nr, tag))
14631 			continue;
14632 
14633 		err = netdev_rx_queue_restart(dev, irq->ring_nr);
14634 		if (err) {
14635 			netdev_err(dev, "RX queue restart failed: err=%d\n",
14636 				   err);
14637 			continue;
14638 		}
14639 
14640 		irq->tag = tag;
14641 	}
14642 
14643 unlock:
14644 	bnxt_unlock_sp(bp);
14645 }
14646 
14647 /* Only called from bnxt_sp_task() */
14648 static void bnxt_reset(struct bnxt *bp, bool silent)
14649 {
14650 	bnxt_rtnl_lock_sp(bp);
14651 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14652 		bnxt_reset_task(bp, silent);
14653 	bnxt_rtnl_unlock_sp(bp);
14654 }
14655 
14656 /* Only called from bnxt_sp_task() */
14657 static void bnxt_rx_ring_reset(struct bnxt *bp)
14658 {
14659 	int i;
14660 
14661 	bnxt_rtnl_lock_sp(bp);
14662 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14663 		bnxt_rtnl_unlock_sp(bp);
14664 		return;
14665 	}
14666 	/* Disable and flush TPA before resetting the RX ring */
14667 	if (bp->flags & BNXT_FLAG_TPA)
14668 		bnxt_set_tpa(bp, false);
14669 	for (i = 0; i < bp->rx_nr_rings; i++) {
14670 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14671 		struct bnxt_cp_ring_info *cpr;
14672 		int rc;
14673 
14674 		if (!rxr->bnapi->in_reset)
14675 			continue;
14676 
14677 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14678 		if (rc) {
14679 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14680 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14681 			else
14682 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14683 					    rc);
14684 			bnxt_reset_task(bp, true);
14685 			break;
14686 		}
14687 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14688 		rxr->rx_prod = 0;
14689 		rxr->rx_agg_prod = 0;
14690 		rxr->rx_sw_agg_prod = 0;
14691 		rxr->rx_next_cons = 0;
14692 		rxr->bnapi->in_reset = false;
14693 		rc = bnxt_alloc_one_rx_ring(bp, i);
14694 		if (rc) {
14695 			netdev_warn(bp->dev, "RX ring reset failed to allocate buffers, rc = %d, falling back to global reset\n",
14696 				    rc);
14697 			bnxt_reset_task(bp, true);
14698 			bnxt_rtnl_unlock_sp(bp);
14699 			return;
14700 		}
14701 		cpr = &rxr->bnapi->cp_ring;
14702 		cpr->sw_stats->rx.rx_resets++;
14703 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14704 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14705 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14706 	}
14707 	if (bp->flags & BNXT_FLAG_TPA)
14708 		bnxt_set_tpa(bp, true);
14709 	bnxt_rtnl_unlock_sp(bp);
14710 }
14711 
14712 static void bnxt_fw_fatal_close(struct bnxt *bp)
14713 {
14714 	bnxt_tx_disable(bp);
14715 	bnxt_disable_napi(bp);
14716 	bnxt_disable_int_sync(bp);
14717 	bnxt_free_irq(bp);
14718 	bnxt_clear_int_mode(bp);
14719 	pci_disable_device(bp->pdev);
14720 }
14721 
14722 static void bnxt_fw_reset_close(struct bnxt *bp)
14723 {
14724 	/* When firmware is in fatal state, quiesce device and disable
14725 	 * bus master to prevent any potential bad DMAs before freeing
14726 	 * kernel memory.
14727 	 */
14728 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14729 		u16 val = 0;
14730 
14731 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14732 		if (val == 0xffff)
14733 			bp->fw_reset_min_dsecs = 0;
14734 		bnxt_fw_fatal_close(bp);
14735 	}
14736 	__bnxt_close_nic(bp, true, false);
14737 	bnxt_vf_reps_free(bp);
14738 	bnxt_clear_int_mode(bp);
14739 	bnxt_hwrm_func_drv_unrgtr(bp);
14740 	if (pci_is_enabled(bp->pdev))
14741 		pci_disable_device(bp->pdev);
14742 	bnxt_free_ctx_mem(bp, false);
14743 }
14744 
14745 static bool is_bnxt_fw_ok(struct bnxt *bp)
14746 {
14747 	struct bnxt_fw_health *fw_health = bp->fw_health;
14748 	bool no_heartbeat = false, has_reset = false;
14749 	u32 val;
14750 
14751 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14752 	if (val == fw_health->last_fw_heartbeat)
14753 		no_heartbeat = true;
14754 
14755 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14756 	if (val != fw_health->last_fw_reset_cnt)
14757 		has_reset = true;
14758 
14759 	if (!no_heartbeat && has_reset)
14760 		return true;
14761 
14762 	return false;
14763 }
14764 
14765 /* netdev instance lock is acquired before calling this function */
14766 static void bnxt_force_fw_reset(struct bnxt *bp)
14767 {
14768 	struct bnxt_fw_health *fw_health = bp->fw_health;
14769 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14770 	u32 wait_dsecs;
14771 
14772 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14773 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14774 		return;
14775 
14776 	/* we have to serialize with bnxt_refclk_read()*/
14777 	if (ptp) {
14778 		unsigned long flags;
14779 
14780 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14781 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14782 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14783 	} else {
14784 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14785 	}
14786 	bnxt_fw_reset_close(bp);
14787 	wait_dsecs = fw_health->master_func_wait_dsecs;
14788 	if (fw_health->primary) {
14789 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14790 			wait_dsecs = 0;
14791 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14792 	} else {
14793 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14794 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14795 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14796 	}
14797 
14798 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14799 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14800 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14801 }
14802 
14803 void bnxt_fw_exception(struct bnxt *bp)
14804 {
14805 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14806 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14807 	bnxt_ulp_stop(bp);
14808 	bnxt_lock_sp(bp);
14809 	bnxt_force_fw_reset(bp);
14810 	bnxt_unlock_sp(bp);
14811 }
14812 
14813 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14814  * < 0 on error.
14815  */
14816 static int bnxt_get_registered_vfs(struct bnxt *bp)
14817 {
14818 #ifdef CONFIG_BNXT_SRIOV
14819 	int rc;
14820 
14821 	if (!BNXT_PF(bp))
14822 		return 0;
14823 
14824 	rc = bnxt_hwrm_func_qcfg(bp);
14825 	if (rc) {
14826 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14827 		return rc;
14828 	}
14829 	if (bp->pf.registered_vfs)
14830 		return bp->pf.registered_vfs;
14831 	if (bp->sriov_cfg)
14832 		return 1;
14833 #endif
14834 	return 0;
14835 }
14836 
14837 void bnxt_fw_reset(struct bnxt *bp)
14838 {
14839 	bnxt_ulp_stop(bp);
14840 	if (BNXT_PF(bp))
14841 		bnxt_rtnl_lock_sp(bp);
14842 	else
14843 		bnxt_lock_sp(bp);
14844 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14845 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14846 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14847 		int n = 0, tmo;
14848 
14849 		/* we have to serialize with bnxt_refclk_read()*/
14850 		if (ptp) {
14851 			unsigned long flags;
14852 
14853 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14854 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14855 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14856 		} else {
14857 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14858 		}
14859 		if (bp->pf.active_vfs &&
14860 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14861 			n = bnxt_get_registered_vfs(bp);
14862 		if (n < 0) {
14863 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14864 				   n);
14865 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14866 			netif_close(bp->dev);
14867 			goto fw_reset_exit;
14868 		} else if (n > 0) {
14869 			u16 vf_tmo_dsecs = n * 10;
14870 
14871 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14872 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14873 			bp->fw_reset_state =
14874 				BNXT_FW_RESET_STATE_POLL_VF;
14875 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14876 			goto fw_reset_exit;
14877 		}
14878 		bnxt_fw_reset_close(bp);
14879 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14880 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14881 			tmo = HZ / 10;
14882 		} else {
14883 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14884 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14885 		}
14886 		bnxt_queue_fw_reset_work(bp, tmo);
14887 	}
14888 fw_reset_exit:
14889 	if (BNXT_PF(bp))
14890 		bnxt_rtnl_unlock_sp(bp);
14891 	else
14892 		bnxt_unlock_sp(bp);
14893 }
14894 
14895 static void bnxt_chk_missed_irq(struct bnxt *bp)
14896 {
14897 	int i;
14898 
14899 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14900 		return;
14901 
14902 	for (i = 0; i < bp->cp_nr_rings; i++) {
14903 		struct bnxt_napi *bnapi = bp->bnapi[i];
14904 		struct bnxt_cp_ring_info *cpr;
14905 		u32 fw_ring_id;
14906 		int j;
14907 
14908 		if (!bnapi)
14909 			continue;
14910 
14911 		cpr = &bnapi->cp_ring;
14912 		for (j = 0; j < cpr->cp_ring_count; j++) {
14913 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14914 			u32 val[2];
14915 
14916 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14917 				continue;
14918 
14919 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14920 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14921 				continue;
14922 			}
14923 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14924 			bnxt_dbg_hwrm_ring_info_get(bp,
14925 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14926 				fw_ring_id, &val[0], &val[1]);
14927 			cpr->sw_stats->cmn.missed_irqs++;
14928 		}
14929 	}
14930 }
14931 
14932 static void bnxt_cfg_ntp_filters(struct bnxt *);
14933 
14934 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14935 {
14936 	struct bnxt_link_info *link_info = &bp->link_info;
14937 
14938 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14939 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14940 		if (bp->hwrm_spec_code >= 0x10201) {
14941 			if (link_info->auto_pause_setting &
14942 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14943 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14944 		} else {
14945 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14946 		}
14947 		bnxt_set_auto_speed(link_info);
14948 	} else {
14949 		bnxt_set_force_speed(link_info);
14950 		link_info->req_duplex = link_info->duplex_setting;
14951 	}
14952 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14953 		link_info->req_flow_ctrl =
14954 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14955 	else
14956 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14957 }
14958 
14959 static void bnxt_fw_echo_reply(struct bnxt *bp)
14960 {
14961 	struct bnxt_fw_health *fw_health = bp->fw_health;
14962 	struct hwrm_func_echo_response_input *req;
14963 	int rc;
14964 
14965 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14966 	if (rc)
14967 		return;
14968 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14969 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14970 	hwrm_req_send(bp, req);
14971 }
14972 
14973 static void bnxt_ulp_restart(struct bnxt *bp)
14974 {
14975 	bnxt_ulp_stop(bp);
14976 	bnxt_ulp_start(bp);
14977 }
14978 
14979 static void bnxt_sp_task(struct work_struct *work)
14980 {
14981 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14982 
14983 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14984 	smp_mb__after_atomic();
14985 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14986 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14987 		return;
14988 	}
14989 
14990 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14991 		bnxt_ulp_restart(bp);
14992 		bnxt_reenable_sriov(bp);
14993 	}
14994 
14995 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14996 		bnxt_cfg_ntp_filters(bp);
14997 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14998 		bnxt_hwrm_exec_fwd_req(bp);
14999 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
15000 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
15001 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
15002 		bnxt_hwrm_port_qstats(bp, 0);
15003 		bnxt_hwrm_port_qstats_ext(bp, 0);
15004 		spin_lock(&bp->stats_lock);
15005 		bnxt_accumulate_all_stats(bp);
15006 		bp->stats_updated_jiffies = jiffies;
15007 		spin_unlock(&bp->stats_lock);
15008 	}
15009 
15010 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
15011 		int rc;
15012 
15013 		mutex_lock(&bp->link_lock);
15014 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
15015 				       &bp->sp_event))
15016 			bnxt_hwrm_phy_qcaps(bp);
15017 
15018 		rc = bnxt_update_link(bp, true);
15019 		if (rc)
15020 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
15021 				   rc);
15022 
15023 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
15024 				       &bp->sp_event))
15025 			bnxt_init_ethtool_link_settings(bp);
15026 		mutex_unlock(&bp->link_lock);
15027 	}
15028 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
15029 		int rc;
15030 
15031 		mutex_lock(&bp->link_lock);
15032 		rc = bnxt_update_phy_setting(bp);
15033 		mutex_unlock(&bp->link_lock);
15034 		if (rc) {
15035 			netdev_warn(bp->dev, "update phy settings retry failed\n");
15036 		} else {
15037 			bp->link_info.phy_retry = false;
15038 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
15039 		}
15040 	}
15041 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
15042 		mutex_lock(&bp->link_lock);
15043 		bnxt_get_port_module_status(bp);
15044 		mutex_unlock(&bp->link_lock);
15045 	}
15046 
15047 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
15048 		bnxt_tc_flow_stats_work(bp);
15049 
15050 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
15051 		bnxt_chk_missed_irq(bp);
15052 
15053 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
15054 		bnxt_fw_echo_reply(bp);
15055 
15056 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
15057 		bnxt_hwmon_notify_event(bp);
15058 
15059 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
15060 	 * must be the last functions to be called before exiting.
15061 	 */
15062 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
15063 		bnxt_reset(bp, false);
15064 
15065 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
15066 		bnxt_reset(bp, true);
15067 
15068 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
15069 		bnxt_rx_ring_reset(bp);
15070 
15071 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
15072 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
15073 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
15074 			bnxt_devlink_health_fw_report(bp);
15075 		else
15076 			bnxt_fw_reset(bp);
15077 	}
15078 
15079 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
15080 		if (!is_bnxt_fw_ok(bp))
15081 			bnxt_devlink_health_fw_report(bp);
15082 	}
15083 
15084 	if (test_and_clear_bit(BNXT_TPH_UPDATE_SP_EVENT, &bp->sp_event))
15085 		bnxt_tph_update(bp);
15086 
15087 	smp_mb__before_atomic();
15088 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
15089 }
15090 
15091 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15092 				int *max_cp);
15093 
15094 /* Under netdev instance lock */
15095 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
15096 		     int tx_xdp)
15097 {
15098 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
15099 	struct bnxt_hw_rings hwr = {0};
15100 	int rx_rings = rx;
15101 	int rc;
15102 
15103 	if (tcs)
15104 		tx_sets = tcs;
15105 
15106 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
15107 
15108 	if (max_rx < rx_rings)
15109 		return -ENOMEM;
15110 
15111 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15112 		rx_rings <<= 1;
15113 
15114 	hwr.rx = rx_rings;
15115 	hwr.tx = tx * tx_sets + tx_xdp;
15116 	if (max_tx < hwr.tx)
15117 		return -ENOMEM;
15118 
15119 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
15120 
15121 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
15122 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
15123 	if (max_cp < hwr.cp)
15124 		return -ENOMEM;
15125 	hwr.stat = hwr.cp;
15126 	if (BNXT_NEW_RM(bp)) {
15127 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
15128 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
15129 		hwr.grp = rx;
15130 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
15131 	}
15132 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
15133 		hwr.cp_p5 = hwr.tx + rx;
15134 	rc = bnxt_hwrm_check_rings(bp, &hwr);
15135 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
15136 		if (!bnxt_ulp_registered(bp->edev[BNXT_AUXDEV_RDMA])) {
15137 			hwr.cp += bnxt_get_ulp_msix_num(bp);
15138 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
15139 		}
15140 		if (hwr.cp > bp->total_irqs) {
15141 			int old_total = bp->total_irqs;
15142 			int total_msix = bnxt_change_msix(bp, hwr.cp);
15143 
15144 			if (total_msix < hwr.cp) {
15145 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
15146 					    hwr.cp, total_msix);
15147 				rc = -ENOSPC;
15148 			}
15149 			/* FW has not reserved the new vectors yet, and writes to
15150 			 * MSI-X table entries beyond the reserved IRQs don't
15151 			 * stick (seen on BCM57508).  Free them again;
15152 			 * bnxt_reserve_rings() allocates them after
15153 			 * __bnxt_reserve_rings().  Without the new resource
15154 			 * manager there is no IRQ reservation and
15155 			 * bnxt_reserve_rings() does not grow the vectors, so
15156 			 * keep them.
15157 			 */
15158 			if (BNXT_NEW_RM(bp))
15159 				bnxt_change_msix(bp, old_total);
15160 		}
15161 	}
15162 	return rc;
15163 }
15164 
15165 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
15166 {
15167 	if (bp->bar2) {
15168 		pci_iounmap(pdev, bp->bar2);
15169 		bp->bar2 = NULL;
15170 	}
15171 
15172 	if (bp->bar1) {
15173 		pci_iounmap(pdev, bp->bar1);
15174 		bp->bar1 = NULL;
15175 	}
15176 
15177 	if (bp->bar0) {
15178 		pci_iounmap(pdev, bp->bar0);
15179 		bp->bar0 = NULL;
15180 	}
15181 }
15182 
15183 static void bnxt_cleanup_pci(struct bnxt *bp)
15184 {
15185 	pci_disable_ptm(bp->pdev);
15186 	bnxt_unmap_bars(bp, bp->pdev);
15187 	pci_release_regions(bp->pdev);
15188 	if (pci_is_enabled(bp->pdev))
15189 		pci_disable_device(bp->pdev);
15190 }
15191 
15192 static void bnxt_init_dflt_coal(struct bnxt *bp)
15193 {
15194 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
15195 	struct bnxt_coal *coal;
15196 	u16 flags = 0;
15197 
15198 	if (coal_cap->cmpl_params &
15199 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
15200 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
15201 
15202 	/* Tick values in micro seconds.
15203 	 * 1 coal_buf x bufs_per_record = 1 completion record.
15204 	 */
15205 	coal = &bp->rx_coal;
15206 	coal->coal_ticks = 10;
15207 	coal->coal_bufs = 30;
15208 	coal->coal_ticks_irq = 1;
15209 	coal->coal_bufs_irq = 2;
15210 	coal->idle_thresh = 50;
15211 	coal->bufs_per_record = 2;
15212 	coal->budget = 64;		/* NAPI budget */
15213 	coal->flags = flags;
15214 
15215 	coal = &bp->tx_coal;
15216 	coal->coal_ticks = 28;
15217 	coal->coal_bufs = 30;
15218 	coal->coal_ticks_irq = 2;
15219 	coal->coal_bufs_irq = 2;
15220 	coal->bufs_per_record = 1;
15221 	coal->flags = flags;
15222 
15223 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
15224 }
15225 
15226 /* FW that pre-reserves 1 VNIC per function */
15227 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
15228 {
15229 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
15230 
15231 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
15232 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
15233 		return true;
15234 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
15235 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
15236 		return true;
15237 	return false;
15238 }
15239 
15240 static void bnxt_hwrm_pfcwd_qcaps(struct bnxt *bp)
15241 {
15242 	struct hwrm_queue_pfcwd_timeout_qcaps_output *resp;
15243 	struct hwrm_queue_pfcwd_timeout_qcaps_input *req;
15244 	int rc;
15245 
15246 	bp->max_pfcwd_tmo_ms = 0;
15247 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_PFCWD_TIMEOUT_QCAPS);
15248 	if (rc)
15249 		return;
15250 	resp = hwrm_req_hold(bp, req);
15251 	rc = hwrm_req_send_silent(bp, req);
15252 	if (!rc)
15253 		bp->max_pfcwd_tmo_ms = le16_to_cpu(resp->max_pfcwd_timeout);
15254 	hwrm_req_drop(bp, req);
15255 }
15256 
15257 static int bnxt_fw_init_one_p1(struct bnxt *bp)
15258 {
15259 	int rc;
15260 
15261 	bp->fw_cap = 0;
15262 	rc = bnxt_hwrm_ver_get(bp);
15263 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
15264 	 * so wait before continuing with recovery.
15265 	 */
15266 	if (rc)
15267 		msleep(100);
15268 	bnxt_try_map_fw_health_reg(bp);
15269 	if (rc) {
15270 		rc = bnxt_try_recover_fw(bp);
15271 		if (rc)
15272 			return rc;
15273 		rc = bnxt_hwrm_ver_get(bp);
15274 		if (rc)
15275 			return rc;
15276 	}
15277 
15278 	bnxt_nvm_cfg_ver_get(bp);
15279 
15280 	rc = bnxt_hwrm_func_reset(bp);
15281 	if (rc)
15282 		return -ENODEV;
15283 
15284 	bnxt_hwrm_fw_set_time(bp);
15285 	return 0;
15286 }
15287 
15288 static int bnxt_fw_init_one_p2(struct bnxt *bp)
15289 {
15290 	int rc;
15291 
15292 	/* Get the MAX capabilities for this function */
15293 	rc = bnxt_hwrm_func_qcaps(bp);
15294 	if (rc) {
15295 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
15296 			   rc);
15297 		return -ENODEV;
15298 	}
15299 
15300 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
15301 	if (rc)
15302 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
15303 			    rc);
15304 
15305 	if (bnxt_alloc_fw_health(bp)) {
15306 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
15307 	} else {
15308 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
15309 		if (rc)
15310 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
15311 				    rc);
15312 	}
15313 
15314 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
15315 	if (rc)
15316 		return -ENODEV;
15317 
15318 	rc = bnxt_alloc_crash_dump_mem(bp);
15319 	if (rc)
15320 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
15321 			    rc);
15322 	if (!rc) {
15323 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
15324 		if (rc) {
15325 			bnxt_free_crash_dump_mem(bp);
15326 			netdev_warn(bp->dev,
15327 				    "hwrm crash dump mem failure rc: %d\n", rc);
15328 		}
15329 	}
15330 
15331 	if (bnxt_fw_pre_resv_vnics(bp))
15332 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
15333 
15334 	bnxt_hwrm_pfcwd_qcaps(bp);
15335 	bnxt_hwrm_func_qcfg(bp);
15336 	bnxt_hwrm_vnic_qcaps(bp);
15337 	bnxt_hwrm_port_led_qcaps(bp);
15338 	bnxt_ethtool_init(bp);
15339 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
15340 		__bnxt_hwrm_ptp_qcfg(bp);
15341 	bnxt_dcb_init(bp);
15342 	bnxt_hwmon_init(bp);
15343 	return 0;
15344 }
15345 
15346 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
15347 {
15348 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
15349 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
15350 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
15351 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
15352 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
15353 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
15354 		bp->rss_hash_delta = bp->rss_hash_cfg;
15355 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
15356 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
15357 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
15358 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
15359 	}
15360 }
15361 
15362 static void bnxt_set_dflt_rfs(struct bnxt *bp)
15363 {
15364 	struct net_device *dev = bp->dev;
15365 
15366 	dev->hw_features &= ~NETIF_F_NTUPLE;
15367 	dev->features &= ~NETIF_F_NTUPLE;
15368 	bp->flags &= ~BNXT_FLAG_RFS;
15369 	if (bnxt_rfs_supported(bp)) {
15370 		dev->hw_features |= NETIF_F_NTUPLE;
15371 		if (bnxt_rfs_capable(bp, false)) {
15372 			bp->flags |= BNXT_FLAG_RFS;
15373 			dev->features |= NETIF_F_NTUPLE;
15374 		}
15375 	}
15376 }
15377 
15378 static void bnxt_fw_init_one_p3(struct bnxt *bp)
15379 {
15380 	struct pci_dev *pdev = bp->pdev;
15381 
15382 	bnxt_set_dflt_rss_hash_type(bp);
15383 	bnxt_set_dflt_rfs(bp);
15384 
15385 	bnxt_get_wol_settings(bp);
15386 	if (bp->flags & BNXT_FLAG_WOL_CAP)
15387 		device_set_wakeup_enable(&pdev->dev, bp->wol);
15388 	else
15389 		device_set_wakeup_capable(&pdev->dev, false);
15390 
15391 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
15392 	bnxt_hwrm_coal_params_qcaps(bp);
15393 }
15394 
15395 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
15396 
15397 int bnxt_fw_init_one(struct bnxt *bp)
15398 {
15399 	int rc;
15400 
15401 	rc = bnxt_fw_init_one_p1(bp);
15402 	if (rc) {
15403 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
15404 		return rc;
15405 	}
15406 	rc = bnxt_fw_init_one_p2(bp);
15407 	if (rc) {
15408 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
15409 		return rc;
15410 	}
15411 	rc = bnxt_probe_phy(bp, false);
15412 	if (rc)
15413 		return rc;
15414 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
15415 	if (rc)
15416 		return rc;
15417 
15418 	bnxt_fw_init_one_p3(bp);
15419 	return 0;
15420 }
15421 
15422 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
15423 {
15424 	struct bnxt_fw_health *fw_health = bp->fw_health;
15425 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
15426 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
15427 	u32 reg_type, reg_off, delay_msecs;
15428 
15429 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
15430 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
15431 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
15432 	switch (reg_type) {
15433 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
15434 		pci_write_config_dword(bp->pdev, reg_off, val);
15435 		break;
15436 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
15437 		writel(reg_off & BNXT_GRC_BASE_MASK,
15438 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
15439 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
15440 		fallthrough;
15441 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
15442 		writel(val, bp->bar0 + reg_off);
15443 		break;
15444 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
15445 		writel(val, bp->bar1 + reg_off);
15446 		break;
15447 	}
15448 	if (delay_msecs) {
15449 		pci_read_config_dword(bp->pdev, 0, &val);
15450 		msleep(delay_msecs);
15451 	}
15452 }
15453 
15454 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
15455 {
15456 	struct hwrm_func_qcfg_output *resp;
15457 	struct hwrm_func_qcfg_input *req;
15458 	bool result = true; /* firmware will enforce if unknown */
15459 
15460 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
15461 		return result;
15462 
15463 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
15464 		return result;
15465 
15466 	req->fid = cpu_to_le16(0xffff);
15467 	resp = hwrm_req_hold(bp, req);
15468 	if (!hwrm_req_send(bp, req))
15469 		result = !!(le16_to_cpu(resp->flags) &
15470 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
15471 	hwrm_req_drop(bp, req);
15472 	return result;
15473 }
15474 
15475 static void bnxt_reset_all(struct bnxt *bp)
15476 {
15477 	struct bnxt_fw_health *fw_health = bp->fw_health;
15478 	int i, rc;
15479 
15480 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15481 		bnxt_fw_reset_via_optee(bp);
15482 		bp->fw_reset_timestamp = jiffies;
15483 		return;
15484 	}
15485 
15486 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
15487 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
15488 			bnxt_fw_reset_writel(bp, i);
15489 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
15490 		struct hwrm_fw_reset_input *req;
15491 
15492 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
15493 		if (!rc) {
15494 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
15495 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
15496 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
15497 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
15498 			rc = hwrm_req_send(bp, req);
15499 		}
15500 		if (rc != -ENODEV)
15501 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
15502 	}
15503 	bp->fw_reset_timestamp = jiffies;
15504 }
15505 
15506 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
15507 {
15508 	return time_after(jiffies, bp->fw_reset_timestamp +
15509 			  (bp->fw_reset_max_dsecs * HZ / 10));
15510 }
15511 
15512 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
15513 {
15514 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15515 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
15516 		bnxt_dl_health_fw_status_update(bp, false);
15517 	bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT;
15518 	netif_close(bp->dev);
15519 }
15520 
15521 static void bnxt_fw_reset_task(struct work_struct *work)
15522 {
15523 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
15524 	int rc = 0;
15525 
15526 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
15527 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
15528 		return;
15529 	}
15530 
15531 	switch (bp->fw_reset_state) {
15532 	case BNXT_FW_RESET_STATE_POLL_VF: {
15533 		int n = bnxt_get_registered_vfs(bp);
15534 		int tmo;
15535 
15536 		if (n < 0) {
15537 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
15538 				   n, jiffies_to_msecs(jiffies -
15539 				   bp->fw_reset_timestamp));
15540 			goto fw_reset_abort;
15541 		} else if (n > 0) {
15542 			if (bnxt_fw_reset_timeout(bp)) {
15543 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15544 				bp->fw_reset_state = 0;
15545 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
15546 					   n);
15547 				goto ulp_start;
15548 			}
15549 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15550 			return;
15551 		}
15552 		bp->fw_reset_timestamp = jiffies;
15553 		netdev_lock(bp->dev);
15554 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
15555 			netdev_unlock(bp->dev);
15556 			goto fw_reset_abort;
15557 		}
15558 		bnxt_fw_reset_close(bp);
15559 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15560 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
15561 			tmo = HZ / 10;
15562 		} else {
15563 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15564 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
15565 		}
15566 		netdev_unlock(bp->dev);
15567 		bnxt_queue_fw_reset_work(bp, tmo);
15568 		return;
15569 	}
15570 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
15571 		u32 val;
15572 
15573 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15574 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
15575 		    !bnxt_fw_reset_timeout(bp)) {
15576 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15577 			return;
15578 		}
15579 
15580 		if (!bp->fw_health->primary) {
15581 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
15582 
15583 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15584 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
15585 			return;
15586 		}
15587 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
15588 	}
15589 		fallthrough;
15590 	case BNXT_FW_RESET_STATE_RESET_FW:
15591 		bnxt_reset_all(bp);
15592 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15593 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
15594 		return;
15595 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
15596 		bnxt_inv_fw_health_reg(bp);
15597 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
15598 		    !bp->fw_reset_min_dsecs) {
15599 			u16 val;
15600 
15601 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
15602 			if (val == 0xffff) {
15603 				if (bnxt_fw_reset_timeout(bp)) {
15604 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
15605 					rc = -ETIMEDOUT;
15606 					goto fw_reset_abort;
15607 				}
15608 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
15609 				return;
15610 			}
15611 		}
15612 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
15613 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
15614 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
15615 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
15616 			bnxt_dl_remote_reload(bp);
15617 		if (!pci_is_enabled(bp->pdev) && pci_enable_device(bp->pdev)) {
15618 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
15619 			rc = -ENODEV;
15620 			goto fw_reset_abort;
15621 		}
15622 		pci_set_master(bp->pdev);
15623 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
15624 		fallthrough;
15625 	case BNXT_FW_RESET_STATE_POLL_FW:
15626 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
15627 		rc = bnxt_hwrm_poll(bp);
15628 		if (rc) {
15629 			if (bnxt_fw_reset_timeout(bp)) {
15630 				netdev_err(bp->dev, "Firmware reset aborted\n");
15631 				goto fw_reset_abort_status;
15632 			}
15633 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15634 			return;
15635 		}
15636 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
15637 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
15638 		fallthrough;
15639 	case BNXT_FW_RESET_STATE_OPENING:
15640 		while (!rtnl_trylock()) {
15641 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15642 			return;
15643 		}
15644 		netdev_lock(bp->dev);
15645 		rc = bnxt_open(bp->dev);
15646 		if (rc) {
15647 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
15648 			bnxt_fw_reset_abort(bp, rc);
15649 			netdev_unlock(bp->dev);
15650 			rtnl_unlock();
15651 			goto ulp_start;
15652 		}
15653 
15654 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
15655 		    bp->fw_health->enabled) {
15656 			bp->fw_health->last_fw_reset_cnt =
15657 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
15658 		}
15659 		bp->fw_reset_state = 0;
15660 		/* Make sure fw_reset_state is 0 before clearing the flag */
15661 		smp_mb__before_atomic();
15662 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15663 		bnxt_ptp_reapply_pps(bp);
15664 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
15665 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
15666 			bnxt_dl_health_fw_recovery_done(bp);
15667 			bnxt_dl_health_fw_status_update(bp, true);
15668 		}
15669 		netdev_unlock(bp->dev);
15670 		rtnl_unlock();
15671 		bnxt_ulp_start(bp);
15672 		bnxt_reenable_sriov(bp);
15673 		netdev_lock(bp->dev);
15674 		bnxt_vf_reps_alloc(bp);
15675 		bnxt_vf_reps_open(bp);
15676 		netdev_unlock(bp->dev);
15677 		break;
15678 	}
15679 	return;
15680 
15681 fw_reset_abort_status:
15682 	if (bp->fw_health->status_reliable ||
15683 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15684 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15685 
15686 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15687 	}
15688 fw_reset_abort:
15689 	rtnl_lock();
15690 	netdev_lock(bp->dev);
15691 	bnxt_fw_reset_abort(bp, rc);
15692 	netdev_unlock(bp->dev);
15693 	rtnl_unlock();
15694 ulp_start:
15695 	if (!rc)
15696 		bnxt_ulp_start(bp);
15697 }
15698 
15699 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15700 {
15701 	int rc;
15702 	struct bnxt *bp = netdev_priv(dev);
15703 
15704 	SET_NETDEV_DEV(dev, &pdev->dev);
15705 
15706 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15707 	rc = pci_enable_device(pdev);
15708 	if (rc) {
15709 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15710 		goto init_err;
15711 	}
15712 
15713 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15714 		dev_err(&pdev->dev,
15715 			"Cannot find PCI device base address, aborting\n");
15716 		rc = -ENODEV;
15717 		goto init_err_disable;
15718 	}
15719 
15720 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15721 	if (rc) {
15722 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15723 		goto init_err_disable;
15724 	}
15725 
15726 	rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15727 	if (rc) {
15728 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15729 		rc = -EIO;
15730 		goto init_err_release;
15731 	}
15732 
15733 	pci_set_master(pdev);
15734 
15735 	bp->dev = dev;
15736 	bp->pdev = pdev;
15737 
15738 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15739 	 * determines the BAR size.
15740 	 */
15741 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15742 	if (!bp->bar0) {
15743 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15744 		rc = -ENOMEM;
15745 		goto init_err_release;
15746 	}
15747 
15748 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15749 	if (!bp->bar2) {
15750 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15751 		rc = -ENOMEM;
15752 		goto init_err_release;
15753 	}
15754 
15755 	pci_enable_ptm(pdev);
15756 
15757 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15758 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15759 
15760 	spin_lock_init(&bp->ntp_fltr_lock);
15761 	spin_lock_init(&bp->stats_lock);
15762 #if BITS_PER_LONG == 32
15763 	spin_lock_init(&bp->db_lock);
15764 #endif
15765 
15766 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15767 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15768 
15769 	timer_setup(&bp->timer, bnxt_timer, 0);
15770 	bp->current_interval = BNXT_TIMER_INTERVAL;
15771 
15772 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15773 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15774 
15775 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15776 	return 0;
15777 
15778 init_err_release:
15779 	bnxt_unmap_bars(bp, pdev);
15780 	pci_release_regions(pdev);
15781 
15782 init_err_disable:
15783 	pci_disable_device(pdev);
15784 
15785 init_err:
15786 	return rc;
15787 }
15788 
15789 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15790 {
15791 	struct sockaddr *addr = p;
15792 	struct bnxt *bp = netdev_priv(dev);
15793 	int rc = 0;
15794 
15795 	netdev_assert_locked(dev);
15796 
15797 	if (!is_valid_ether_addr(addr->sa_data))
15798 		return -EADDRNOTAVAIL;
15799 
15800 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15801 		return 0;
15802 
15803 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15804 	if (rc)
15805 		return rc;
15806 
15807 	eth_hw_addr_set(dev, addr->sa_data);
15808 	bnxt_clear_usr_fltrs(bp, true);
15809 	if (netif_running(dev)) {
15810 		bnxt_close_nic(bp, false, false);
15811 		rc = bnxt_open_nic(bp, false, false);
15812 	}
15813 
15814 	return rc;
15815 }
15816 
15817 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15818 {
15819 	struct bnxt *bp = netdev_priv(dev);
15820 
15821 	netdev_assert_locked(dev);
15822 
15823 	if (netif_running(dev))
15824 		bnxt_close_nic(bp, true, false);
15825 
15826 	WRITE_ONCE(dev->mtu, new_mtu);
15827 
15828 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15829 	 * program is attached.  We need to set the AGG rings settings and
15830 	 * rx_skb_func accordingly.
15831 	 */
15832 	if (READ_ONCE(bp->xdp_prog))
15833 		bnxt_set_rx_skb_mode(bp, true);
15834 
15835 	bnxt_set_ring_params(bp);
15836 
15837 	if (netif_running(dev))
15838 		return bnxt_open_nic(bp, true, false);
15839 
15840 	return 0;
15841 }
15842 
15843 void bnxt_set_cp_rings(struct bnxt *bp, bool sh)
15844 {
15845 	int tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15846 
15847 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15848 			       tx_cp + bp->rx_nr_rings;
15849 }
15850 
15851 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15852 {
15853 	struct bnxt *bp = netdev_priv(dev);
15854 	bool sh = false;
15855 	int rc;
15856 
15857 	if (tc > bp->max_tc) {
15858 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15859 			   tc, bp->max_tc);
15860 		return -EINVAL;
15861 	}
15862 
15863 	if (bp->num_tc == tc)
15864 		return 0;
15865 
15866 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15867 		sh = true;
15868 
15869 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15870 			      sh, tc, bp->tx_nr_rings_xdp);
15871 	if (rc)
15872 		return rc;
15873 
15874 	/* Needs to close the device and do hw resource re-allocations */
15875 	if (netif_running(bp->dev))
15876 		bnxt_close_nic(bp, true, false);
15877 
15878 	if (tc) {
15879 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15880 		netdev_set_num_tc(dev, tc);
15881 		bp->num_tc = tc;
15882 	} else {
15883 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15884 		netdev_reset_tc(dev);
15885 		bp->num_tc = 0;
15886 	}
15887 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15888 	bnxt_set_cp_rings(bp, sh);
15889 
15890 	if (netif_running(bp->dev))
15891 		return bnxt_open_nic(bp, true, false);
15892 
15893 	return 0;
15894 }
15895 
15896 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15897 				  void *cb_priv)
15898 {
15899 	struct bnxt *bp = cb_priv;
15900 
15901 	if (!bnxt_tc_flower_enabled(bp) ||
15902 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15903 		return -EOPNOTSUPP;
15904 
15905 	switch (type) {
15906 	case TC_SETUP_CLSFLOWER:
15907 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15908 	default:
15909 		return -EOPNOTSUPP;
15910 	}
15911 }
15912 
15913 LIST_HEAD(bnxt_block_cb_list);
15914 
15915 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15916 			 void *type_data)
15917 {
15918 	struct bnxt *bp = netdev_priv(dev);
15919 
15920 	switch (type) {
15921 	case TC_SETUP_BLOCK:
15922 		return flow_block_cb_setup_simple(type_data,
15923 						  &bnxt_block_cb_list,
15924 						  bnxt_setup_tc_block_cb,
15925 						  bp, bp, true);
15926 	case TC_SETUP_QDISC_MQPRIO: {
15927 		struct tc_mqprio_qopt *mqprio = type_data;
15928 
15929 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15930 
15931 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15932 	}
15933 	default:
15934 		return -EOPNOTSUPP;
15935 	}
15936 }
15937 
15938 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15939 			    const struct sk_buff *skb)
15940 {
15941 	struct bnxt_vnic_info *vnic;
15942 
15943 	if (skb)
15944 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15945 
15946 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15947 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15948 }
15949 
15950 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15951 			   u32 idx)
15952 {
15953 	struct hlist_head *head;
15954 	int bit_id;
15955 
15956 	spin_lock_bh(&bp->ntp_fltr_lock);
15957 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15958 	if (bit_id < 0) {
15959 		spin_unlock_bh(&bp->ntp_fltr_lock);
15960 		return -ENOMEM;
15961 	}
15962 
15963 	fltr->base.sw_id = (u16)bit_id;
15964 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15965 	fltr->base.flags |= BNXT_ACT_RING_DST;
15966 	head = &bp->ntp_fltr_hash_tbl[idx];
15967 	hlist_add_head_rcu(&fltr->base.hash, head);
15968 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15969 	bnxt_insert_usr_fltr(bp, &fltr->base);
15970 	bp->ntp_fltr_count++;
15971 	spin_unlock_bh(&bp->ntp_fltr_lock);
15972 	return 0;
15973 }
15974 
15975 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15976 			    struct bnxt_ntuple_filter *f2)
15977 {
15978 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15979 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15980 	struct flow_keys *keys1 = &f1->fkeys;
15981 	struct flow_keys *keys2 = &f2->fkeys;
15982 
15983 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15984 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15985 		return false;
15986 
15987 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15988 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15989 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15990 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15991 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15992 			return false;
15993 	} else {
15994 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15995 				     &keys2->addrs.v6addrs.src) ||
15996 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15997 				     &masks2->addrs.v6addrs.src) ||
15998 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15999 				     &keys2->addrs.v6addrs.dst) ||
16000 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
16001 				     &masks2->addrs.v6addrs.dst))
16002 			return false;
16003 	}
16004 
16005 	return keys1->ports.src == keys2->ports.src &&
16006 	       masks1->ports.src == masks2->ports.src &&
16007 	       keys1->ports.dst == keys2->ports.dst &&
16008 	       masks1->ports.dst == masks2->ports.dst &&
16009 	       keys1->control.flags == keys2->control.flags &&
16010 	       f1->l2_fltr == f2->l2_fltr;
16011 }
16012 
16013 struct bnxt_ntuple_filter *
16014 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
16015 				struct bnxt_ntuple_filter *fltr, u32 idx)
16016 {
16017 	struct bnxt_ntuple_filter *f;
16018 	struct hlist_head *head;
16019 
16020 	head = &bp->ntp_fltr_hash_tbl[idx];
16021 	hlist_for_each_entry_rcu(f, head, base.hash) {
16022 		if (bnxt_fltr_match(f, fltr))
16023 			return f;
16024 	}
16025 	return NULL;
16026 }
16027 
16028 #ifdef CONFIG_RFS_ACCEL
16029 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
16030 			      u16 rxq_index, u32 flow_id)
16031 {
16032 	struct bnxt *bp = netdev_priv(dev);
16033 	struct bnxt_ntuple_filter *fltr, *new_fltr;
16034 	struct flow_keys *fkeys;
16035 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
16036 	struct bnxt_l2_filter *l2_fltr;
16037 	int rc = 0, idx;
16038 	u32 flags;
16039 
16040 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
16041 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
16042 		atomic_inc(&l2_fltr->refcnt);
16043 	} else {
16044 		struct bnxt_l2_key key;
16045 
16046 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
16047 		key.vlan = 0;
16048 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
16049 		if (!l2_fltr)
16050 			return -EINVAL;
16051 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
16052 			bnxt_del_l2_filter(bp, l2_fltr);
16053 			return -EINVAL;
16054 		}
16055 	}
16056 	new_fltr = kzalloc_obj(*new_fltr, GFP_ATOMIC);
16057 	if (!new_fltr) {
16058 		bnxt_del_l2_filter(bp, l2_fltr);
16059 		return -ENOMEM;
16060 	}
16061 
16062 	fkeys = &new_fltr->fkeys;
16063 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
16064 		rc = -EPROTONOSUPPORT;
16065 		goto err_free;
16066 	}
16067 
16068 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
16069 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
16070 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
16071 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
16072 		rc = -EPROTONOSUPPORT;
16073 		goto err_free;
16074 	}
16075 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
16076 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
16077 		if (bp->hwrm_spec_code < 0x10601) {
16078 			rc = -EPROTONOSUPPORT;
16079 			goto err_free;
16080 		}
16081 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
16082 	}
16083 	flags = fkeys->control.flags;
16084 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
16085 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
16086 		rc = -EPROTONOSUPPORT;
16087 		goto err_free;
16088 	}
16089 	new_fltr->l2_fltr = l2_fltr;
16090 
16091 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
16092 	rcu_read_lock();
16093 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
16094 	if (fltr) {
16095 		rc = fltr->base.sw_id;
16096 		rcu_read_unlock();
16097 		goto err_free;
16098 	}
16099 	rcu_read_unlock();
16100 
16101 	new_fltr->flow_id = flow_id;
16102 	new_fltr->base.rxq = rxq_index;
16103 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
16104 	if (!rc) {
16105 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
16106 		return new_fltr->base.sw_id;
16107 	}
16108 
16109 err_free:
16110 	bnxt_del_l2_filter(bp, l2_fltr);
16111 	kfree(new_fltr);
16112 	return rc;
16113 }
16114 #endif
16115 
16116 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
16117 {
16118 	spin_lock_bh(&bp->ntp_fltr_lock);
16119 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
16120 		spin_unlock_bh(&bp->ntp_fltr_lock);
16121 		return;
16122 	}
16123 	hlist_del_rcu(&fltr->base.hash);
16124 	bnxt_del_one_usr_fltr(bp, &fltr->base);
16125 	bp->ntp_fltr_count--;
16126 	spin_unlock_bh(&bp->ntp_fltr_lock);
16127 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
16128 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
16129 	kfree_rcu(fltr, base.rcu);
16130 }
16131 
16132 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
16133 {
16134 #ifdef CONFIG_RFS_ACCEL
16135 	int i;
16136 
16137 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
16138 		struct hlist_head *head;
16139 		struct hlist_node *tmp;
16140 		struct bnxt_ntuple_filter *fltr;
16141 		int rc;
16142 
16143 		head = &bp->ntp_fltr_hash_tbl[i];
16144 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
16145 			bool del = false;
16146 
16147 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
16148 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
16149 					continue;
16150 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
16151 							fltr->flow_id,
16152 							fltr->base.sw_id)) {
16153 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
16154 									 fltr);
16155 					del = true;
16156 				}
16157 			} else {
16158 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
16159 								       fltr);
16160 				if (rc)
16161 					del = true;
16162 				else
16163 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
16164 			}
16165 
16166 			if (del)
16167 				bnxt_del_ntp_filter(bp, fltr);
16168 		}
16169 	}
16170 #endif
16171 }
16172 
16173 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
16174 				    unsigned int entry, struct udp_tunnel_info *ti)
16175 {
16176 	struct bnxt *bp = netdev_priv(netdev);
16177 	unsigned int cmd;
16178 
16179 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
16180 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
16181 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
16182 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
16183 	else
16184 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
16185 
16186 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
16187 }
16188 
16189 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
16190 				      unsigned int entry, struct udp_tunnel_info *ti)
16191 {
16192 	struct bnxt *bp = netdev_priv(netdev);
16193 	unsigned int cmd;
16194 
16195 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
16196 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
16197 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
16198 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
16199 	else
16200 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
16201 
16202 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
16203 }
16204 
16205 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
16206 	.set_port	= bnxt_udp_tunnel_set_port,
16207 	.unset_port	= bnxt_udp_tunnel_unset_port,
16208 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
16209 	.tables		= {
16210 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
16211 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
16212 	},
16213 }, bnxt_udp_tunnels_p7 = {
16214 	.set_port	= bnxt_udp_tunnel_set_port,
16215 	.unset_port	= bnxt_udp_tunnel_unset_port,
16216 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
16217 	.tables		= {
16218 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
16219 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
16220 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
16221 	},
16222 };
16223 
16224 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
16225 			       struct net_device *dev, u32 filter_mask,
16226 			       int nlflags)
16227 {
16228 	struct bnxt *bp = netdev_priv(dev);
16229 
16230 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
16231 				       nlflags, filter_mask, NULL);
16232 }
16233 
16234 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
16235 			       u16 flags, struct netlink_ext_ack *extack)
16236 {
16237 	struct bnxt *bp = netdev_priv(dev);
16238 	struct nlattr *attr, *br_spec;
16239 	int rem, rc = 0;
16240 
16241 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
16242 		return -EOPNOTSUPP;
16243 
16244 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
16245 	if (!br_spec)
16246 		return -EINVAL;
16247 
16248 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
16249 		u16 mode;
16250 
16251 		mode = nla_get_u16(attr);
16252 		if (mode == bp->br_mode)
16253 			break;
16254 
16255 		rc = bnxt_hwrm_set_br_mode(bp, mode);
16256 		if (!rc)
16257 			bp->br_mode = mode;
16258 		break;
16259 	}
16260 	return rc;
16261 }
16262 
16263 int bnxt_get_port_parent_id(struct net_device *dev,
16264 			    struct netdev_phys_item_id *ppid)
16265 {
16266 	struct bnxt *bp = netdev_priv(dev);
16267 
16268 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
16269 		return -EOPNOTSUPP;
16270 
16271 	/* The PF and it's VF-reps only support the switchdev framework */
16272 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
16273 		return -EOPNOTSUPP;
16274 
16275 	ppid->id_len = sizeof(bp->dsn);
16276 	memcpy(ppid->id, bp->dsn, ppid->id_len);
16277 
16278 	return 0;
16279 }
16280 
16281 static const struct net_device_ops bnxt_netdev_ops = {
16282 	.ndo_open		= bnxt_open,
16283 	.ndo_start_xmit		= bnxt_start_xmit,
16284 	.ndo_stop		= bnxt_close,
16285 	.ndo_get_stats64	= bnxt_get_stats64,
16286 	.ndo_set_rx_mode_async	= bnxt_set_rx_mode,
16287 	.ndo_eth_ioctl		= bnxt_ioctl,
16288 	.ndo_validate_addr	= eth_validate_addr,
16289 	.ndo_set_mac_address	= bnxt_change_mac_addr,
16290 	.ndo_change_mtu		= bnxt_change_mtu,
16291 	.ndo_fix_features	= bnxt_fix_features,
16292 	.ndo_set_features	= bnxt_set_features,
16293 	.ndo_features_check	= bnxt_features_check,
16294 	.ndo_tx_timeout		= bnxt_tx_timeout,
16295 #ifdef CONFIG_BNXT_SRIOV
16296 	.ndo_get_vf_config	= bnxt_get_vf_config,
16297 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
16298 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
16299 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
16300 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
16301 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
16302 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
16303 #endif
16304 	.ndo_setup_tc           = bnxt_setup_tc,
16305 #ifdef CONFIG_RFS_ACCEL
16306 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
16307 #endif
16308 	.ndo_bpf		= bnxt_xdp,
16309 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
16310 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
16311 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
16312 	.ndo_hwtstamp_get	= bnxt_hwtstamp_get,
16313 	.ndo_hwtstamp_set	= bnxt_hwtstamp_set,
16314 };
16315 
16316 static const struct xdp_metadata_ops bnxt_xdp_metadata_ops = {
16317 	.xmo_rx_hash		= bnxt_xdp_rx_hash,
16318 };
16319 
16320 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
16321 				    struct netdev_queue_stats_rx *stats)
16322 {
16323 	struct bnxt *bp = netdev_priv(dev);
16324 	struct bnxt_cp_ring_info *cpr;
16325 	u64 *sw;
16326 
16327 	if (!bp->bnapi)
16328 		return;
16329 
16330 	bnxt_sync_ring_stats(bp);
16331 	cpr = &bp->bnapi[i]->cp_ring;
16332 	sw = cpr->stats.sw_stats;
16333 
16334 	stats->packets = 0;
16335 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
16336 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
16337 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
16338 
16339 	stats->bytes = 0;
16340 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
16341 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
16342 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
16343 
16344 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
16345 	stats->hw_gro_packets = cpr->sw_stats->rx.rx_hw_gro_packets;
16346 	stats->hw_gro_wire_packets = cpr->sw_stats->rx.rx_hw_gro_wire_packets;
16347 }
16348 
16349 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
16350 				    struct netdev_queue_stats_tx *stats)
16351 {
16352 	struct bnxt *bp = netdev_priv(dev);
16353 	struct bnxt_napi *bnapi;
16354 	u64 *sw;
16355 
16356 	if (!bp->tx_ring)
16357 		return;
16358 
16359 	bnxt_sync_ring_stats(bp);
16360 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
16361 	sw = bnapi->cp_ring.stats.sw_stats;
16362 
16363 	stats->packets = 0;
16364 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
16365 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
16366 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
16367 
16368 	stats->bytes = 0;
16369 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
16370 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
16371 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
16372 }
16373 
16374 static void bnxt_get_base_stats(struct net_device *dev,
16375 				struct netdev_queue_stats_rx *rx,
16376 				struct netdev_queue_stats_tx *tx)
16377 {
16378 	struct bnxt *bp = netdev_priv(dev);
16379 
16380 	rx->packets = bp->net_stats_prev.rx_packets;
16381 	rx->bytes = bp->net_stats_prev.rx_bytes;
16382 	rx->alloc_fail = bp->ring_drv_stats_prev.rx_total_oom_discards;
16383 	rx->hw_gro_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_packets;
16384 	rx->hw_gro_wire_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_wire_packets;
16385 
16386 	tx->packets = bp->net_stats_prev.tx_packets;
16387 	tx->bytes = bp->net_stats_prev.tx_bytes;
16388 }
16389 
16390 static const struct netdev_stat_ops bnxt_stat_ops = {
16391 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
16392 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
16393 	.get_base_stats		= bnxt_get_base_stats,
16394 };
16395 
16396 static void bnxt_queue_default_qcfg(struct net_device *dev,
16397 				    struct netdev_queue_config *qcfg)
16398 {
16399 	qcfg->rx_page_size = BNXT_RX_PAGE_SIZE;
16400 }
16401 
16402 static int bnxt_validate_qcfg(struct net_device *dev,
16403 			      struct netdev_queue_config *qcfg,
16404 			      struct netlink_ext_ack *extack)
16405 {
16406 	struct bnxt *bp = netdev_priv(dev);
16407 
16408 	/* Older chips need MSS calc so rx_page_size is not supported */
16409 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16410 	    qcfg->rx_page_size != BNXT_RX_PAGE_SIZE)
16411 		return -EINVAL;
16412 
16413 	if (!is_power_of_2(qcfg->rx_page_size))
16414 		return -ERANGE;
16415 
16416 	if (qcfg->rx_page_size < BNXT_RX_PAGE_SIZE ||
16417 	    qcfg->rx_page_size > BNXT_MAX_RX_PAGE_SIZE)
16418 		return -ERANGE;
16419 
16420 	return 0;
16421 }
16422 
16423 static int bnxt_queue_mem_alloc(struct net_device *dev,
16424 				struct netdev_queue_config *qcfg,
16425 				void *qmem, int idx)
16426 {
16427 	struct bnxt_rx_ring_info *rxr, *clone;
16428 	struct bnxt *bp = netdev_priv(dev);
16429 	struct bnxt_ring_struct *ring;
16430 	int rc;
16431 
16432 	if (!bp->rx_ring)
16433 		return -ENETDOWN;
16434 
16435 	rxr = &bp->rx_ring[idx];
16436 	clone = qmem;
16437 	memcpy(clone, rxr, sizeof(*rxr));
16438 	bnxt_init_rx_ring_struct(bp, clone);
16439 	bnxt_reset_rx_ring_struct(bp, clone);
16440 
16441 	clone->rx_prod = 0;
16442 	clone->rx_agg_prod = 0;
16443 	clone->rx_sw_agg_prod = 0;
16444 	clone->rx_next_cons = 0;
16445 	clone->need_head_pool = false;
16446 	clone->rx_page_size = qcfg->rx_page_size;
16447 	clone->rx_agg_bmap = NULL;
16448 	clone->rx_tpa = NULL;
16449 	clone->rx_tpa_idx_map = NULL;
16450 
16451 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
16452 	if (rc)
16453 		return rc;
16454 
16455 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
16456 	if (rc < 0)
16457 		goto err_page_pool_destroy;
16458 
16459 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
16460 					MEM_TYPE_PAGE_POOL,
16461 					clone->page_pool);
16462 	if (rc)
16463 		goto err_rxq_info_unreg;
16464 
16465 	ring = &clone->rx_ring_struct;
16466 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16467 	if (rc)
16468 		goto err_free_rx_ring;
16469 
16470 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
16471 		ring = &clone->rx_agg_ring_struct;
16472 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16473 		if (rc)
16474 			goto err_free_rx_agg_ring;
16475 
16476 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
16477 		if (rc)
16478 			goto err_free_rx_agg_ring;
16479 	}
16480 
16481 	if (bp->flags & BNXT_FLAG_TPA) {
16482 		rc = bnxt_alloc_one_tpa_info(bp, clone);
16483 		if (rc)
16484 			goto err_free_tpa_info;
16485 	}
16486 
16487 	bnxt_init_one_rx_ring_rxbd(bp, clone);
16488 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
16489 
16490 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
16491 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16492 		bnxt_alloc_one_rx_ring_netmem(bp, clone, idx);
16493 	if (bp->flags & BNXT_FLAG_TPA) {
16494 		rc = bnxt_alloc_one_tpa_info_data(bp, clone);
16495 		if (rc)
16496 			goto err_free_rx_ring_skbs;
16497 	}
16498 
16499 	return 0;
16500 
16501 err_free_rx_ring_skbs:
16502 	bnxt_free_one_rx_ring_skbs(bp, clone);
16503 err_free_tpa_info:
16504 	bnxt_free_one_tpa_info(bp, clone);
16505 err_free_rx_agg_ring:
16506 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
16507 	kfree(clone->rx_agg_bmap);
16508 	clone->rx_agg_bmap = NULL;
16509 err_free_rx_ring:
16510 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
16511 err_rxq_info_unreg:
16512 	xdp_rxq_info_unreg(&clone->xdp_rxq);
16513 err_page_pool_destroy:
16514 	page_pool_destroy(clone->page_pool);
16515 	page_pool_destroy(clone->head_pool);
16516 	clone->page_pool = NULL;
16517 	clone->head_pool = NULL;
16518 	return rc;
16519 }
16520 
16521 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
16522 {
16523 	struct bnxt_rx_ring_info *rxr = qmem;
16524 	struct bnxt *bp = netdev_priv(dev);
16525 	struct bnxt_ring_struct *ring;
16526 
16527 	bnxt_free_one_rx_ring_skbs(bp, rxr);
16528 	bnxt_free_one_tpa_info(bp, rxr);
16529 
16530 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
16531 
16532 	page_pool_destroy(rxr->page_pool);
16533 	page_pool_destroy(rxr->head_pool);
16534 	rxr->page_pool = NULL;
16535 	rxr->head_pool = NULL;
16536 
16537 	ring = &rxr->rx_ring_struct;
16538 	bnxt_free_ring(bp, &ring->ring_mem);
16539 
16540 	ring = &rxr->rx_agg_ring_struct;
16541 	bnxt_free_ring(bp, &ring->ring_mem);
16542 
16543 	kfree(rxr->rx_agg_bmap);
16544 	rxr->rx_agg_bmap = NULL;
16545 }
16546 
16547 static void bnxt_copy_rx_ring(struct bnxt *bp,
16548 			      struct bnxt_rx_ring_info *dst,
16549 			      struct bnxt_rx_ring_info *src)
16550 {
16551 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
16552 	struct bnxt_ring_struct *dst_ring, *src_ring;
16553 	int i;
16554 
16555 	dst_ring = &dst->rx_ring_struct;
16556 	dst_rmem = &dst_ring->ring_mem;
16557 	src_ring = &src->rx_ring_struct;
16558 	src_rmem = &src_ring->ring_mem;
16559 
16560 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16561 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16562 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16563 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16564 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16565 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16566 
16567 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16568 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16569 	*dst_rmem->vmem = *src_rmem->vmem;
16570 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16571 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16572 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16573 	}
16574 
16575 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
16576 		return;
16577 
16578 	dst_ring = &dst->rx_agg_ring_struct;
16579 	dst_rmem = &dst_ring->ring_mem;
16580 	src_ring = &src->rx_agg_ring_struct;
16581 	src_rmem = &src_ring->ring_mem;
16582 
16583 	dst->rx_page_size = src->rx_page_size;
16584 
16585 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16586 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16587 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16588 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16589 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16590 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16591 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
16592 
16593 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16594 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16595 	*dst_rmem->vmem = *src_rmem->vmem;
16596 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16597 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16598 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16599 	}
16600 
16601 	dst->rx_agg_bmap = src->rx_agg_bmap;
16602 }
16603 
16604 static int bnxt_queue_start(struct net_device *dev,
16605 			    struct netdev_queue_config *qcfg,
16606 			    void *qmem, int idx)
16607 {
16608 	struct bnxt *bp = netdev_priv(dev);
16609 	struct bnxt_rx_ring_info *rxr, *clone;
16610 	struct bnxt_cp_ring_info *cpr;
16611 	struct bnxt_vnic_info *vnic;
16612 	struct bnxt_napi *bnapi;
16613 	int i, rc;
16614 	u16 mru;
16615 
16616 	rxr = &bp->rx_ring[idx];
16617 	clone = qmem;
16618 
16619 	rxr->rx_prod = clone->rx_prod;
16620 	rxr->rx_agg_prod = clone->rx_agg_prod;
16621 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
16622 	rxr->rx_next_cons = clone->rx_next_cons;
16623 	rxr->rx_tpa = clone->rx_tpa;
16624 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
16625 	rxr->page_pool = clone->page_pool;
16626 	rxr->head_pool = clone->head_pool;
16627 	rxr->xdp_rxq = clone->xdp_rxq;
16628 	rxr->need_head_pool = clone->need_head_pool;
16629 
16630 	bnxt_copy_rx_ring(bp, rxr, clone);
16631 
16632 	bnapi = rxr->bnapi;
16633 	cpr = &bnapi->cp_ring;
16634 
16635 	/* All rings have been reserved and previously allocated.
16636 	 * Reallocating with the same parameters should never fail.
16637 	 */
16638 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
16639 	if (rc)
16640 		goto err_reset;
16641 
16642 	if (bp->tph_mode) {
16643 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
16644 		if (rc)
16645 			goto err_reset;
16646 	}
16647 
16648 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
16649 	if (rc)
16650 		goto err_reset;
16651 
16652 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
16653 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16654 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
16655 
16656 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
16657 		rc = bnxt_tx_queue_start(bp, idx);
16658 		if (rc)
16659 			goto err_reset;
16660 	}
16661 
16662 	bnxt_enable_rx_page_pool(rxr);
16663 	napi_enable_locked(&bnapi->napi);
16664 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16665 
16666 	mru = bp->dev->mtu + VLAN_ETH_HLEN;
16667 	for (i = 0; i < bp->nr_vnics; i++) {
16668 		vnic = &bp->vnic_info[i];
16669 
16670 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx);
16671 		if (rc)
16672 			return rc;
16673 	}
16674 	return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx);
16675 
16676 err_reset:
16677 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
16678 		   rc);
16679 	napi_enable_locked(&bnapi->napi);
16680 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16681 	netif_close(dev);
16682 	return rc;
16683 }
16684 
16685 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
16686 {
16687 	struct bnxt *bp = netdev_priv(dev);
16688 	struct bnxt_rx_ring_info *rxr;
16689 	struct bnxt_cp_ring_info *cpr;
16690 	struct bnxt_vnic_info *vnic;
16691 	struct bnxt_napi *bnapi;
16692 	int i;
16693 
16694 	for (i = 0; i < bp->nr_vnics; i++) {
16695 		vnic = &bp->vnic_info[i];
16696 
16697 		bnxt_set_vnic_mru_p5(bp, vnic, 0, idx);
16698 	}
16699 	bnxt_set_rss_ctx_vnic_mru(bp, 0, idx);
16700 	/* Make sure NAPI sees that the VNIC is disabled */
16701 	synchronize_net();
16702 	rxr = &bp->rx_ring[idx];
16703 	bnapi = rxr->bnapi;
16704 	cpr = &bnapi->cp_ring;
16705 	cancel_work_sync(&cpr->dim.work);
16706 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
16707 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
16708 	page_pool_disable_direct_recycling(rxr->page_pool);
16709 	if (bnxt_separate_head_pool(rxr))
16710 		page_pool_disable_direct_recycling(rxr->head_pool);
16711 
16712 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
16713 		bnxt_tx_queue_stop(bp, idx);
16714 
16715 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
16716 	 * completion is handled in NAPI to guarantee no more DMA on that ring
16717 	 * after seeing the completion.
16718 	 */
16719 	napi_disable_locked(&bnapi->napi);
16720 
16721 	if (bp->tph_mode) {
16722 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
16723 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
16724 	}
16725 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
16726 
16727 	memcpy(qmem, rxr, sizeof(*rxr));
16728 	bnxt_init_rx_ring_struct(bp, qmem);
16729 
16730 	return 0;
16731 }
16732 
16733 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
16734 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
16735 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
16736 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
16737 	.ndo_queue_start	= bnxt_queue_start,
16738 	.ndo_queue_stop		= bnxt_queue_stop,
16739 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16740 	.ndo_validate_qcfg	= bnxt_validate_qcfg,
16741 	.supported_params	= QCFG_RX_PAGE_SIZE,
16742 };
16743 
16744 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops_unsupp = {
16745 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16746 };
16747 
16748 static void bnxt_remove_one(struct pci_dev *pdev)
16749 {
16750 	struct net_device *dev = pci_get_drvdata(pdev);
16751 	struct bnxt *bp = netdev_priv(dev);
16752 
16753 	if (BNXT_PF(bp))
16754 		__bnxt_sriov_disable(bp);
16755 
16756 	bnxt_aux_devices_del(bp);
16757 
16758 	unregister_netdev(dev);
16759 	bnxt_ptp_clear(bp);
16760 
16761 	bnxt_aux_devices_uninit(bp);
16762 	bnxt_auxdev_id_free(bp, bp->auxdev_id);
16763 
16764 	bnxt_free_l2_filters(bp, true);
16765 	bnxt_free_ntp_fltrs(bp, true);
16766 	WARN_ON(bp->num_rss_ctx);
16767 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16768 	/* Flush any pending tasks */
16769 	cancel_work_sync(&bp->sp_task);
16770 	cancel_delayed_work_sync(&bp->fw_reset_task);
16771 	bp->sp_event = 0;
16772 
16773 	bnxt_dl_fw_reporters_destroy(bp);
16774 	bnxt_dl_unregister(bp);
16775 	bnxt_shutdown_tc(bp);
16776 
16777 	bnxt_clear_int_mode(bp);
16778 	bnxt_free_ring_cpu_masks(bp);
16779 	bnxt_hwrm_func_drv_unrgtr(bp);
16780 	bnxt_free_hwrm_resources(bp);
16781 	bnxt_hwmon_uninit(bp);
16782 	bnxt_ethtool_free(bp);
16783 	bnxt_dcb_free(bp);
16784 	kfree(bp->ptp_cfg);
16785 	bp->ptp_cfg = NULL;
16786 	kfree(bp->fw_health);
16787 	bp->fw_health = NULL;
16788 	bnxt_cleanup_pci(bp);
16789 	bnxt_free_ctx_mem(bp, true);
16790 	bnxt_free_crash_dump_mem(bp);
16791 	kfree(bp->rss_indir_tbl);
16792 	bp->rss_indir_tbl = NULL;
16793 	bnxt_free_port_stats(bp);
16794 	free_netdev(dev);
16795 }
16796 
16797 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16798 {
16799 	int rc = 0;
16800 	struct bnxt_link_info *link_info = &bp->link_info;
16801 
16802 	bp->phy_flags = 0;
16803 	rc = bnxt_hwrm_phy_qcaps(bp);
16804 	if (rc) {
16805 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16806 			   rc);
16807 		return rc;
16808 	}
16809 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16810 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16811 	else
16812 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16813 
16814 	bp->mac_flags = 0;
16815 	bnxt_hwrm_mac_qcaps(bp);
16816 
16817 	if (!fw_dflt)
16818 		return 0;
16819 
16820 	mutex_lock(&bp->link_lock);
16821 	rc = bnxt_update_link(bp, false);
16822 	if (rc) {
16823 		mutex_unlock(&bp->link_lock);
16824 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16825 			   rc);
16826 		return rc;
16827 	}
16828 
16829 	/* Older firmware does not have supported_auto_speeds, so assume
16830 	 * that all supported speeds can be autonegotiated.
16831 	 */
16832 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16833 		link_info->support_auto_speeds = link_info->support_speeds;
16834 
16835 	bnxt_init_ethtool_link_settings(bp);
16836 	mutex_unlock(&bp->link_lock);
16837 	return 0;
16838 }
16839 
16840 static int bnxt_get_max_irq(struct pci_dev *pdev)
16841 {
16842 	u16 ctrl;
16843 
16844 	if (!pdev->msix_cap)
16845 		return 1;
16846 
16847 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16848 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16849 }
16850 
16851 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16852 				int *max_cp)
16853 {
16854 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16855 	int max_ring_grps = 0, max_irq;
16856 
16857 	*max_tx = hw_resc->max_tx_rings;
16858 	*max_rx = hw_resc->max_rx_rings;
16859 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16860 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16861 			bnxt_get_ulp_msix_num_in_use(bp),
16862 			hw_resc->max_stat_ctxs -
16863 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16864 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16865 		*max_cp = min_t(int, *max_cp, max_irq);
16866 	max_ring_grps = hw_resc->max_hw_ring_grps;
16867 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16868 		*max_cp -= 1;
16869 		*max_rx -= 2;
16870 	}
16871 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16872 		*max_rx >>= 1;
16873 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16874 		int rc;
16875 
16876 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16877 		if (rc) {
16878 			*max_rx = 0;
16879 			*max_tx = 0;
16880 		}
16881 		/* On P5 chips, max_cp output param should be available NQs */
16882 		*max_cp = max_irq;
16883 	}
16884 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16885 }
16886 
16887 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16888 {
16889 	int rx, tx, cp;
16890 
16891 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16892 	*max_rx = rx;
16893 	*max_tx = tx;
16894 	if (!rx || !tx || !cp)
16895 		return -ENOMEM;
16896 
16897 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16898 }
16899 
16900 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16901 			       bool shared)
16902 {
16903 	int rc;
16904 
16905 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16906 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16907 		/* Not enough rings, try disabling agg rings. */
16908 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16909 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16910 		if (rc) {
16911 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16912 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16913 			return rc;
16914 		}
16915 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16916 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16917 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16918 		bnxt_set_ring_params(bp);
16919 	}
16920 
16921 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16922 		int max_cp, max_stat, max_irq;
16923 
16924 		/* Reserve minimum resources for RoCE */
16925 		max_cp = bnxt_get_max_func_cp_rings(bp);
16926 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16927 		max_irq = bnxt_get_max_func_irqs(bp);
16928 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16929 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16930 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16931 			return 0;
16932 
16933 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16934 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16935 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16936 		max_cp = min_t(int, max_cp, max_irq);
16937 		max_cp = min_t(int, max_cp, max_stat);
16938 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16939 		if (rc)
16940 			rc = 0;
16941 	}
16942 	return rc;
16943 }
16944 
16945 /* In initial default shared ring setting, each shared ring must have a
16946  * RX/TX ring pair.
16947  */
16948 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16949 {
16950 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16951 	bp->rx_nr_rings = bp->cp_nr_rings;
16952 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16953 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16954 }
16955 
16956 static void bnxt_adj_dflt_rings(struct bnxt *bp, bool sh)
16957 {
16958 	if (sh)
16959 		bnxt_trim_dflt_sh_rings(bp);
16960 	else
16961 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16962 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16963 	if (sh && READ_ONCE(bp->xdp_prog)) {
16964 		bnxt_set_xdp_tx_rings(bp);
16965 		bnxt_set_cp_rings(bp, true);
16966 	}
16967 }
16968 
16969 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16970 {
16971 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16972 	int avail_msix;
16973 
16974 	if (!bnxt_can_reserve_rings(bp))
16975 		return 0;
16976 
16977 	if (sh)
16978 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16979 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16980 	/* Reduce default rings on multi-port cards so that total default
16981 	 * rings do not exceed CPU count.
16982 	 */
16983 	if (bp->port_count > 1) {
16984 		int max_rings =
16985 			max_t(int, num_online_cpus() / bp->port_count, 1);
16986 
16987 		dflt_rings = min_t(int, dflt_rings, max_rings);
16988 	}
16989 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16990 	if (rc)
16991 		return rc;
16992 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16993 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16994 
16995 	bnxt_adj_dflt_rings(bp, sh);
16996 
16997 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16998 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16999 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
17000 
17001 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
17002 		bnxt_set_dflt_ulp_stat_ctxs(bp);
17003 	}
17004 
17005 	rc = __bnxt_reserve_rings(bp);
17006 	if (rc && rc != -ECOMM)
17007 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
17008 
17009 	bnxt_adj_tx_rings(bp);
17010 	if (sh)
17011 		bnxt_adj_dflt_rings(bp, true);
17012 
17013 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
17014 	if (bnxt_need_reserve_rings(bp)) {
17015 		rc = __bnxt_reserve_rings(bp);
17016 		if (rc && rc != -ECOMM)
17017 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
17018 		bnxt_adj_tx_rings(bp);
17019 	}
17020 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
17021 		bp->rx_nr_rings++;
17022 		bp->cp_nr_rings++;
17023 	}
17024 	if (rc) {
17025 		bp->tx_nr_rings = 0;
17026 		bp->rx_nr_rings = 0;
17027 	}
17028 	return rc;
17029 }
17030 
17031 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
17032 {
17033 	int rc;
17034 
17035 	if (bp->tx_nr_rings)
17036 		return 0;
17037 
17038 	bnxt_ulp_irq_stop(bp);
17039 	bnxt_clear_int_mode(bp);
17040 	rc = bnxt_set_dflt_rings(bp, true);
17041 	if (rc) {
17042 		if (rc == -ECOMM)
17043 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
17044 		else
17045 			netdev_err(bp->dev, "Not enough rings available.\n");
17046 		goto init_dflt_ring_err;
17047 	}
17048 	rc = bnxt_init_int_mode(bp);
17049 	if (rc)
17050 		goto init_dflt_ring_err;
17051 
17052 	bnxt_adj_tx_rings(bp);
17053 
17054 	bnxt_set_dflt_rfs(bp);
17055 
17056 init_dflt_ring_err:
17057 	bnxt_ulp_irq_restart(bp, rc);
17058 	return rc;
17059 }
17060 
17061 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
17062 {
17063 	int rc;
17064 
17065 	netdev_assert_locked_ops_compat(bp->dev);
17066 	bnxt_hwrm_func_qcaps(bp);
17067 
17068 	if (netif_running(bp->dev))
17069 		__bnxt_close_nic(bp, true, false);
17070 
17071 	bnxt_ulp_irq_stop(bp);
17072 	bnxt_clear_int_mode(bp);
17073 	rc = bnxt_init_int_mode(bp);
17074 	bnxt_ulp_irq_restart(bp, rc);
17075 
17076 	if (netif_running(bp->dev)) {
17077 		if (rc)
17078 			netif_close(bp->dev);
17079 		else
17080 			rc = bnxt_open_nic(bp, true, false);
17081 	}
17082 
17083 	return rc;
17084 }
17085 
17086 static int bnxt_init_mac_addr(struct bnxt *bp)
17087 {
17088 	int rc = 0;
17089 
17090 	if (BNXT_PF(bp)) {
17091 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
17092 	} else {
17093 #ifdef CONFIG_BNXT_SRIOV
17094 		struct bnxt_vf_info *vf = &bp->vf;
17095 		bool strict_approval = true;
17096 
17097 		if (is_valid_ether_addr(vf->mac_addr)) {
17098 			/* overwrite netdev dev_addr with admin VF MAC */
17099 			eth_hw_addr_set(bp->dev, vf->mac_addr);
17100 			/* Older PF driver or firmware may not approve this
17101 			 * correctly.
17102 			 */
17103 			strict_approval = false;
17104 		} else {
17105 			eth_hw_addr_random(bp->dev);
17106 		}
17107 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
17108 #endif
17109 	}
17110 	return rc;
17111 }
17112 
17113 static void bnxt_vpd_read_info(struct bnxt *bp)
17114 {
17115 	struct pci_dev *pdev = bp->pdev;
17116 	unsigned int vpd_size, kw_len;
17117 	int pos, size;
17118 	u8 *vpd_data;
17119 
17120 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
17121 	if (IS_ERR(vpd_data)) {
17122 		pci_warn(pdev, "Unable to read VPD\n");
17123 		return;
17124 	}
17125 
17126 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
17127 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
17128 	if (pos < 0)
17129 		goto read_sn;
17130 
17131 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
17132 	memcpy(bp->board_partno, &vpd_data[pos], size);
17133 
17134 read_sn:
17135 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
17136 					   PCI_VPD_RO_KEYWORD_SERIALNO,
17137 					   &kw_len);
17138 	if (pos < 0)
17139 		goto exit;
17140 
17141 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
17142 	memcpy(bp->board_serialno, &vpd_data[pos], size);
17143 exit:
17144 	kfree(vpd_data);
17145 }
17146 
17147 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
17148 {
17149 	struct pci_dev *pdev = bp->pdev;
17150 	u64 qword;
17151 
17152 	qword = pci_get_dsn(pdev);
17153 	if (!qword) {
17154 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
17155 		return -EOPNOTSUPP;
17156 	}
17157 
17158 	put_unaligned_le64(qword, dsn);
17159 
17160 	bp->flags |= BNXT_FLAG_DSN_VALID;
17161 	return 0;
17162 }
17163 
17164 static int bnxt_map_db_bar(struct bnxt *bp)
17165 {
17166 	if (!bp->db_size)
17167 		return -ENODEV;
17168 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
17169 	if (!bp->bar1)
17170 		return -ENOMEM;
17171 	return 0;
17172 }
17173 
17174 void bnxt_print_device_info(struct bnxt *bp)
17175 {
17176 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
17177 		    board_info[bp->board_idx].name,
17178 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
17179 
17180 	pcie_print_link_status(bp->pdev);
17181 }
17182 
17183 static void bnxt_clear_bars(struct pci_dev *pdev)
17184 {
17185 	int off;
17186 
17187 	for (off = PCI_BASE_ADDRESS_0; off <= PCI_BASE_ADDRESS_5; off += 4)
17188 		pci_write_config_dword(pdev, off, 0);
17189 }
17190 
17191 /* Clear any pending DMA transactions from crash kernel while loading driver in
17192  * capture kernel.
17193  */
17194 static int bnxt_kdump_reset(struct pci_dev *pdev)
17195 {
17196 	int rc, i;
17197 	u16 cmd;
17198 
17199 	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
17200 	cmd &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
17201 	pci_write_config_word(pdev, PCI_COMMAND, cmd);
17202 
17203 	if (pci_save_state(pdev))
17204 		dev_warn(&pdev->dev, "Failed to save PCI state, PCI restore may be incomplete\n");
17205 
17206 	rc = pcie_flr(pdev);
17207 	if (rc)
17208 		dev_warn(&pdev->dev, "pcie_flr() failed (rc: %d), trying to continue\n",
17209 			 rc);
17210 
17211 	/* In case device is not returning CRS, wait 5 seconds longer */
17212 	for (i = 0; i < 50; i++) {
17213 		pci_read_config_word(pdev, PCI_COMMAND, &cmd);
17214 		if (!PCI_POSSIBLE_ERROR(cmd))
17215 			break;
17216 		msleep(100);
17217 	}
17218 	if (PCI_POSSIBLE_ERROR(cmd)) {
17219 		dev_err(&pdev->dev, "PCI config space inaccessible after FLR, aborting\n");
17220 		return -ENODEV;
17221 	}
17222 
17223 	bnxt_clear_bars(pdev);
17224 	pci_restore_state(pdev);
17225 	return 0;
17226 }
17227 
17228 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
17229 {
17230 	struct bnxt_hw_resc *hw_resc;
17231 	struct net_device *dev;
17232 	struct bnxt *bp;
17233 	int rc, max_irqs;
17234 
17235 	if (pci_is_bridge(pdev))
17236 		return -ENODEV;
17237 
17238 	if (!pdev->msix_cap) {
17239 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
17240 		return -ENODEV;
17241 	}
17242 
17243 	if (is_kdump_kernel()) {
17244 		rc = bnxt_kdump_reset(pdev);
17245 		if (rc)
17246 			return rc;
17247 	}
17248 
17249 	max_irqs = bnxt_get_max_irq(pdev);
17250 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
17251 				 max_irqs);
17252 	if (!dev)
17253 		return -ENOMEM;
17254 
17255 	bp = netdev_priv(dev);
17256 	bp->board_idx = ent->driver_data;
17257 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
17258 	bnxt_set_max_func_irqs(bp, max_irqs);
17259 
17260 	bp->max_irqs = max_irqs;
17261 	rc = bnxt_alloc_ring_cpu_masks(bp);
17262 	if (rc)
17263 		goto init_err_free;
17264 
17265 	if (bnxt_vf_pciid(bp->board_idx))
17266 		bp->flags |= BNXT_FLAG_VF;
17267 
17268 	/* No devlink port registration in case of a VF */
17269 	if (BNXT_PF(bp))
17270 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
17271 
17272 	rc = bnxt_init_board(pdev, dev);
17273 	if (rc < 0)
17274 		goto init_err_free;
17275 
17276 	dev->netdev_ops = &bnxt_netdev_ops;
17277 	dev->xdp_metadata_ops = &bnxt_xdp_metadata_ops;
17278 	dev->stat_ops = &bnxt_stat_ops;
17279 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
17280 	dev->ethtool_ops = &bnxt_ethtool_ops;
17281 	pci_set_drvdata(pdev, dev);
17282 
17283 	rc = bnxt_alloc_hwrm_resources(bp);
17284 	if (rc)
17285 		goto init_err_pci_clean;
17286 
17287 	mutex_init(&bp->hwrm_cmd_lock);
17288 	mutex_init(&bp->link_lock);
17289 
17290 	rc = bnxt_fw_init_one_p1(bp);
17291 	if (rc)
17292 		goto init_err_pci_clean;
17293 
17294 	if (BNXT_PF(bp))
17295 		bnxt_vpd_read_info(bp);
17296 
17297 	if (BNXT_CHIP_P5_PLUS(bp)) {
17298 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
17299 		if (BNXT_CHIP_P7(bp))
17300 			bp->flags |= BNXT_FLAG_CHIP_P7;
17301 	}
17302 
17303 	rc = bnxt_alloc_rss_indir_tbl(bp);
17304 	if (rc)
17305 		goto init_err_pci_clean;
17306 
17307 	rc = bnxt_fw_init_one_p2(bp);
17308 	if (rc)
17309 		goto init_err_pci_clean;
17310 
17311 	rc = bnxt_map_db_bar(bp);
17312 	if (rc) {
17313 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
17314 			rc);
17315 		goto init_err_pci_clean;
17316 	}
17317 
17318 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
17319 			   NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN |
17320 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
17321 			   NETIF_F_GSO_IPXIP4 |
17322 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
17323 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
17324 			   NETIF_F_RXCSUM | NETIF_F_GRO;
17325 	dev->hw_features |= NETIF_F_GSO_UDP_L4;
17326 
17327 	if (BNXT_SUPPORTS_TPA(bp))
17328 		dev->hw_features |= NETIF_F_LRO;
17329 
17330 	dev->hw_enc_features =
17331 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
17332 			NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN |
17333 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
17334 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
17335 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
17336 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
17337 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
17338 	if (bp->flags & BNXT_FLAG_CHIP_P7)
17339 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
17340 	else
17341 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
17342 
17343 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
17344 				    NETIF_F_GSO_GRE_CSUM;
17345 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
17346 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
17347 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
17348 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
17349 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
17350 	if (BNXT_SUPPORTS_TPA(bp))
17351 		dev->hw_features |= NETIF_F_GRO_HW;
17352 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
17353 	if (dev->features & NETIF_F_GRO_HW)
17354 		dev->features &= ~NETIF_F_LRO;
17355 	dev->priv_flags |= IFF_UNICAST_FLT;
17356 
17357 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
17358 	if (!(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) {
17359 		u16 max_segs = BNXT_SW_USO_MAX_SEGS;
17360 
17361 		if (bp->tso_max_segs)
17362 			max_segs = min_t(u16, max_segs, bp->tso_max_segs);
17363 		netif_set_tso_max_segs(dev, max_segs);
17364 	} else if (bp->tso_max_segs) {
17365 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
17366 	}
17367 
17368 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
17369 			    NETDEV_XDP_ACT_RX_SG;
17370 
17371 #ifdef CONFIG_BNXT_SRIOV
17372 	init_waitqueue_head(&bp->sriov_cfg_wait);
17373 #endif
17374 	if (BNXT_SUPPORTS_TPA(bp)) {
17375 		bp->gro_func = bnxt_gro_func_5730x;
17376 		if (BNXT_CHIP_P4(bp))
17377 			bp->gro_func = bnxt_gro_func_5731x;
17378 		else if (BNXT_CHIP_P5_PLUS(bp))
17379 			bp->gro_func = bnxt_gro_func_5750x;
17380 	}
17381 	if (!BNXT_CHIP_P4_PLUS(bp))
17382 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
17383 
17384 	rc = bnxt_init_mac_addr(bp);
17385 	if (rc) {
17386 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
17387 		rc = -EADDRNOTAVAIL;
17388 		goto init_err_pci_clean;
17389 	}
17390 
17391 	if (BNXT_PF(bp)) {
17392 		/* Read the adapter's DSN to use as the eswitch switch_id */
17393 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
17394 	}
17395 
17396 	/* MTU range: 60 - FW defined max */
17397 	dev->min_mtu = ETH_ZLEN;
17398 	dev->max_mtu = bp->max_mtu;
17399 
17400 	rc = bnxt_probe_phy(bp, true);
17401 	if (rc)
17402 		goto init_err_pci_clean;
17403 
17404 	hw_resc = &bp->hw_resc;
17405 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
17406 		       BNXT_L2_FLTR_MAX_FLTR;
17407 	/* Older firmware may not report these filters properly */
17408 	if (bp->max_fltr < BNXT_MAX_FLTR)
17409 		bp->max_fltr = BNXT_MAX_FLTR;
17410 	bnxt_init_l2_fltr_tbl(bp);
17411 	__bnxt_set_rx_skb_mode(bp, false);
17412 	bnxt_set_tpa_flags(bp);
17413 	bnxt_init_ring_params(bp);
17414 	bnxt_set_ring_params(bp);
17415 	mutex_init(&bp->auxdev_lock);
17416 	if (!bnxt_auxdev_id_alloc(bp))
17417 		bnxt_aux_devices_init(bp);
17418 	rc = bnxt_set_dflt_rings(bp, true);
17419 	if (rc) {
17420 		if (rc == -ECOMM) {
17421 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
17422 		} else {
17423 			netdev_err(bp->dev, "Not enough rings available.\n");
17424 			rc = -ENOMEM;
17425 		}
17426 		goto init_err_pci_clean;
17427 	}
17428 
17429 	bnxt_fw_init_one_p3(bp);
17430 
17431 	bnxt_init_dflt_coal(bp);
17432 
17433 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
17434 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
17435 
17436 	rc = bnxt_init_int_mode(bp);
17437 	if (rc)
17438 		goto init_err_pci_clean;
17439 
17440 	/* No TC has been set yet and rings may have been trimmed due to
17441 	 * limited MSIX, so we re-initialize the TX rings per TC.
17442 	 */
17443 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
17444 
17445 	if (BNXT_PF(bp)) {
17446 		if (!bnxt_pf_wq) {
17447 			bnxt_pf_wq =
17448 				create_singlethread_workqueue("bnxt_pf_wq");
17449 			if (!bnxt_pf_wq) {
17450 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
17451 				rc = -ENOMEM;
17452 				goto init_err_pci_clean;
17453 			}
17454 		}
17455 		rc = bnxt_init_tc(bp);
17456 		if (rc)
17457 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
17458 				   rc);
17459 	}
17460 
17461 	bnxt_inv_fw_health_reg(bp);
17462 	rc = bnxt_dl_register(bp);
17463 	if (rc)
17464 		goto init_err_dl;
17465 
17466 	INIT_LIST_HEAD(&bp->usr_fltr_list);
17467 
17468 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
17469 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
17470 
17471 	dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops_unsupp;
17472 	if (BNXT_SUPPORTS_QUEUE_API(bp))
17473 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
17474 	dev->netmem_tx = NETMEM_TX_DMA;
17475 
17476 	rc = register_netdev(dev);
17477 	if (rc)
17478 		goto init_err_cleanup;
17479 
17480 	bnxt_dl_fw_reporters_create(bp);
17481 
17482 	bnxt_aux_devices_add(bp);
17483 
17484 	bnxt_print_device_info(bp);
17485 
17486 	pci_save_state(pdev);
17487 
17488 	return 0;
17489 init_err_cleanup:
17490 	bnxt_aux_devices_uninit(bp);
17491 	bnxt_auxdev_id_free(bp, bp->auxdev_id);
17492 	bnxt_dl_unregister(bp);
17493 init_err_dl:
17494 	bnxt_shutdown_tc(bp);
17495 	bnxt_clear_int_mode(bp);
17496 
17497 init_err_pci_clean:
17498 	bnxt_hwrm_func_drv_unrgtr(bp);
17499 	bnxt_ptp_clear(bp);
17500 	kfree(bp->ptp_cfg);
17501 	bp->ptp_cfg = NULL;
17502 	bnxt_free_hwrm_resources(bp);
17503 	bnxt_hwmon_uninit(bp);
17504 	bnxt_ethtool_free(bp);
17505 	kfree(bp->fw_health);
17506 	bp->fw_health = NULL;
17507 	bnxt_cleanup_pci(bp);
17508 	bnxt_free_ctx_mem(bp, true);
17509 	bnxt_free_crash_dump_mem(bp);
17510 	kfree(bp->rss_indir_tbl);
17511 	bp->rss_indir_tbl = NULL;
17512 
17513 init_err_free:
17514 	bnxt_free_ring_cpu_masks(bp);
17515 	free_netdev(dev);
17516 	return rc;
17517 }
17518 
17519 static void bnxt_shutdown(struct pci_dev *pdev)
17520 {
17521 	struct net_device *dev = pci_get_drvdata(pdev);
17522 	struct bnxt *bp;
17523 
17524 	if (!dev)
17525 		return;
17526 
17527 	rtnl_lock();
17528 	netdev_lock(dev);
17529 	bp = netdev_priv(dev);
17530 	if (!bp)
17531 		goto shutdown_exit;
17532 
17533 	if (netif_running(dev))
17534 		netif_close(dev);
17535 
17536 	if (bnxt_hwrm_func_drv_unrgtr(bp)) {
17537 		pcie_flr(pdev);
17538 		goto shutdown_exit;
17539 	}
17540 	bnxt_ptp_clear(bp);
17541 	bnxt_clear_int_mode(bp);
17542 	pci_disable_device(pdev);
17543 
17544 	if (system_state == SYSTEM_POWER_OFF) {
17545 		pci_wake_from_d3(pdev, bp->wol);
17546 		pci_set_power_state(pdev, PCI_D3hot);
17547 	}
17548 
17549 shutdown_exit:
17550 	netdev_unlock(dev);
17551 	rtnl_unlock();
17552 }
17553 
17554 #ifdef CONFIG_PM_SLEEP
17555 static int bnxt_suspend(struct device *device)
17556 {
17557 	struct net_device *dev = dev_get_drvdata(device);
17558 	struct bnxt *bp = netdev_priv(dev);
17559 	int rc = 0;
17560 
17561 	bnxt_ulp_stop(bp);
17562 
17563 	netdev_lock(dev);
17564 	if (netif_running(dev)) {
17565 		netif_device_detach(dev);
17566 		rc = bnxt_close(dev);
17567 	}
17568 	bnxt_hwrm_func_drv_unrgtr(bp);
17569 	bnxt_ptp_clear(bp);
17570 	pci_disable_device(bp->pdev);
17571 	bnxt_free_ctx_mem(bp, false);
17572 	netdev_unlock(dev);
17573 	return rc;
17574 }
17575 
17576 #define BNXT_RESUME_MAX_RETRY	10
17577 
17578 static int bnxt_resume(struct device *device)
17579 {
17580 	struct net_device *dev = dev_get_drvdata(device);
17581 	struct bnxt *bp = netdev_priv(dev);
17582 	int i, rc;
17583 
17584 	rc = pci_enable_device(bp->pdev);
17585 	if (rc) {
17586 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
17587 			   rc);
17588 		return rc;
17589 	}
17590 	pci_set_master(bp->pdev);
17591 	if (BNXT_VF(bp)) {
17592 		for (i = 0; i < BNXT_RESUME_MAX_RETRY; i++) {
17593 			rc = bnxt_hwrm_poll(bp);
17594 			if (rc != -EAGAIN && rc != -EBUSY && rc != -ECOMM)
17595 				break;
17596 
17597 			/* Wait briefly for -EAGAIN (FW busy), a bit longer for
17598 			 * -ECOMM (PF down) to allow the PF to fully initialize.
17599 			 * For -EBUSY, the message typically has timed out, so
17600 			 * try again immediately.
17601 			 */
17602 			if (i < BNXT_RESUME_MAX_RETRY - 1) {
17603 				if (rc == -EAGAIN)
17604 					msleep(250);
17605 				else if (rc == -ECOMM)
17606 					msleep(750);
17607 			}
17608 		}
17609 		if (rc) {
17610 			netdev_err(dev,
17611 				   "Firmware not responding during resume, rc = %d\n",
17612 				   rc);
17613 			return -ENODEV;
17614 		}
17615 	}
17616 
17617 	rtnl_lock();
17618 	netdev_lock(dev);
17619 
17620 	if (bnxt_hwrm_ver_get(bp)) {
17621 		rc = -ENODEV;
17622 		goto resume_exit;
17623 	}
17624 	rc = bnxt_hwrm_func_reset(bp);
17625 	if (rc) {
17626 		rc = -EBUSY;
17627 		goto resume_exit;
17628 	}
17629 
17630 	rc = bnxt_hwrm_func_qcaps(bp);
17631 	if (rc)
17632 		goto resume_exit;
17633 
17634 	bnxt_clear_reservations(bp, true);
17635 
17636 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
17637 		rc = -ENODEV;
17638 		goto resume_exit;
17639 	}
17640 	if (bp->fw_crash_mem)
17641 		bnxt_hwrm_crash_dump_mem_cfg(bp);
17642 
17643 	if (bnxt_ptp_init(bp)) {
17644 		kfree(bp->ptp_cfg);
17645 		bp->ptp_cfg = NULL;
17646 	}
17647 	bnxt_get_wol_settings(bp);
17648 	if (netif_running(dev)) {
17649 		rc = bnxt_open(dev);
17650 		if (!rc)
17651 			netif_device_attach(dev);
17652 	}
17653 
17654 resume_exit:
17655 	netdev_unlock(bp->dev);
17656 	rtnl_unlock();
17657 	if (!rc) {
17658 		bnxt_ulp_start(bp);
17659 		bnxt_reenable_sriov(bp);
17660 	}
17661 	return rc;
17662 }
17663 
17664 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
17665 #define BNXT_PM_OPS (&bnxt_pm_ops)
17666 
17667 #else
17668 
17669 #define BNXT_PM_OPS NULL
17670 
17671 #endif /* CONFIG_PM_SLEEP */
17672 
17673 /**
17674  * bnxt_io_error_detected - called when PCI error is detected
17675  * @pdev: Pointer to PCI device
17676  * @state: The current pci connection state
17677  *
17678  * This function is called after a PCI bus error affecting
17679  * this device has been detected.
17680  */
17681 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
17682 					       pci_channel_state_t state)
17683 {
17684 	struct net_device *netdev = pci_get_drvdata(pdev);
17685 	struct bnxt *bp = netdev_priv(netdev);
17686 	bool abort = false;
17687 
17688 	netdev_info(netdev, "PCI I/O error detected\n");
17689 
17690 	bnxt_ulp_stop(bp);
17691 
17692 	netdev_lock(netdev);
17693 	netif_device_detach(netdev);
17694 
17695 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
17696 		netdev_err(bp->dev, "Firmware reset already in progress\n");
17697 		abort = true;
17698 	}
17699 
17700 	if (abort || state == pci_channel_io_perm_failure) {
17701 		netdev_unlock(netdev);
17702 		return PCI_ERS_RESULT_DISCONNECT;
17703 	}
17704 
17705 	/* Link is not reliable anymore if state is pci_channel_io_frozen
17706 	 * so we disable bus master to prevent any potential bad DMAs before
17707 	 * freeing kernel memory.
17708 	 */
17709 	if (state == pci_channel_io_frozen)
17710 		bnxt_fw_fatal_close(bp);
17711 
17712 	if (netif_running(netdev))
17713 		__bnxt_close_nic(bp, true, true);
17714 
17715 	if (pci_is_enabled(pdev))
17716 		pci_disable_device(pdev);
17717 	bnxt_free_ctx_mem(bp, false);
17718 	netdev_unlock(netdev);
17719 
17720 	/* Request a slot reset. */
17721 	return PCI_ERS_RESULT_NEED_RESET;
17722 }
17723 
17724 /**
17725  * bnxt_io_slot_reset - called after the pci bus has been reset.
17726  * @pdev: Pointer to PCI device
17727  *
17728  * Restart the card from scratch, as if from a cold-boot.
17729  * At this point, the card has experienced a hard reset,
17730  * followed by fixups by BIOS, and has its config space
17731  * set up identically to what it was at cold boot.
17732  */
17733 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
17734 {
17735 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
17736 	struct net_device *netdev = pci_get_drvdata(pdev);
17737 	struct bnxt *bp = netdev_priv(netdev);
17738 	int retry = 0;
17739 	int err = 0;
17740 	u16 cmd;
17741 
17742 	netdev_info(bp->dev, "PCI Slot Reset\n");
17743 
17744 	/* After a PCIe hot reset, the chip should return CRS when the
17745 	 * vendor ID config register is read until it is ready.  On all
17746 	 * chips, this is not happening reliably so add a 5-second delay
17747 	 * as a workaround.
17748 	 */
17749 	msleep(5000);
17750 
17751 	netdev_lock(netdev);
17752 
17753 	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
17754 	if (PCI_POSSIBLE_ERROR(cmd)) {
17755 		dev_err(&pdev->dev,
17756 			"PCI config space inaccessible after reset\n");
17757 		goto reset_exit;
17758 	}
17759 
17760 	/* Upon PCIe error, our device internal logic that latches to
17761 	 * BAR value is getting reset and will restore only upon
17762 	 * rewriting the BARs.
17763 	 *
17764 	 * As pci_restore_state() does not re-write the BARs if the
17765 	 * value is same as saved value earlier, driver needs to
17766 	 * write the BARs to 0 to force restore.
17767 	 */
17768 	pci_clear_master(pdev);
17769 	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
17770 	cmd &= ~PCI_COMMAND_MEMORY;
17771 	pci_write_config_word(pdev, PCI_COMMAND, cmd);
17772 
17773 	bnxt_clear_bars(pdev);
17774 	pci_restore_state(pdev);
17775 
17776 	if (!pci_is_enabled(pdev) && pci_enable_device(pdev)) {
17777 		dev_err(&pdev->dev,
17778 			"Cannot re-enable PCI device after reset.\n");
17779 		pci_read_config_word(pdev, PCI_COMMAND, &cmd);
17780 		if (!PCI_POSSIBLE_ERROR(cmd)) {
17781 			cmd &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
17782 			pci_write_config_word(pdev, PCI_COMMAND, cmd);
17783 		}
17784 		goto reset_exit;
17785 	}
17786 	pci_set_master(pdev);
17787 
17788 	bnxt_inv_fw_health_reg(bp);
17789 	bnxt_try_map_fw_health_reg(bp);
17790 
17791 	/* In some PCIe AER scenarios, firmware may take up to
17792 	 * 10 seconds to become ready in the worst case.
17793 	 */
17794 	do {
17795 		err = bnxt_try_recover_fw(bp);
17796 		if (!err)
17797 			break;
17798 		retry++;
17799 	} while (retry < BNXT_FW_SLOT_RESET_RETRY);
17800 
17801 	if (err) {
17802 		dev_err(&pdev->dev, "Firmware not ready\n");
17803 		goto reset_exit;
17804 	}
17805 
17806 	err = bnxt_hwrm_func_reset(bp);
17807 	if (!err)
17808 		result = PCI_ERS_RESULT_RECOVERED;
17809 
17810 	/* IRQ will be initialized later in bnxt_io_resume */
17811 	bnxt_ulp_irq_stop(bp);
17812 	bnxt_clear_int_mode(bp);
17813 
17814 reset_exit:
17815 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
17816 	bnxt_clear_reservations(bp, true);
17817 	netdev_unlock(netdev);
17818 
17819 	return result;
17820 }
17821 
17822 /**
17823  * bnxt_io_resume - called when traffic can start flowing again.
17824  * @pdev: Pointer to PCI device
17825  *
17826  * This callback is called when the error recovery driver tells
17827  * us that its OK to resume normal operation.
17828  */
17829 static void bnxt_io_resume(struct pci_dev *pdev)
17830 {
17831 	struct net_device *netdev = pci_get_drvdata(pdev);
17832 	struct bnxt *bp = netdev_priv(netdev);
17833 	int err;
17834 
17835 	netdev_info(bp->dev, "PCI Slot Resume\n");
17836 	rtnl_lock();
17837 	netdev_lock(netdev);
17838 
17839 	err = bnxt_hwrm_func_qcaps(bp);
17840 	if (!err) {
17841 		if (netif_running(netdev)) {
17842 			err = bnxt_open(netdev);
17843 		} else {
17844 			err = bnxt_reserve_rings(bp, true);
17845 			if (!err)
17846 				err = bnxt_init_int_mode(bp);
17847 		}
17848 	}
17849 
17850 	if (!err)
17851 		netif_device_attach(netdev);
17852 
17853 	netdev_unlock(netdev);
17854 	rtnl_unlock();
17855 	if (!err) {
17856 		bnxt_ulp_start(bp);
17857 		bnxt_reenable_sriov(bp);
17858 	}
17859 }
17860 
17861 static const struct pci_error_handlers bnxt_err_handler = {
17862 	.error_detected	= bnxt_io_error_detected,
17863 	.slot_reset	= bnxt_io_slot_reset,
17864 	.resume		= bnxt_io_resume
17865 };
17866 
17867 static struct pci_driver bnxt_pci_driver = {
17868 	.name		= DRV_MODULE_NAME,
17869 	.id_table	= bnxt_pci_tbl,
17870 	.probe		= bnxt_init_one,
17871 	.remove		= bnxt_remove_one,
17872 	.shutdown	= bnxt_shutdown,
17873 	.driver.pm	= BNXT_PM_OPS,
17874 	.err_handler	= &bnxt_err_handler,
17875 #if defined(CONFIG_BNXT_SRIOV)
17876 	.sriov_configure = bnxt_sriov_configure,
17877 #endif
17878 };
17879 
17880 static int __init bnxt_init(void)
17881 {
17882 	int err;
17883 
17884 	bnxt_debug_init();
17885 	err = pci_register_driver(&bnxt_pci_driver);
17886 	if (err) {
17887 		bnxt_debug_exit();
17888 		return err;
17889 	}
17890 
17891 	return 0;
17892 }
17893 
17894 static void __exit bnxt_exit(void)
17895 {
17896 	pci_unregister_driver(&bnxt_pci_driver);
17897 	if (bnxt_pf_wq)
17898 		destroy_workqueue(bnxt_pf_wq);
17899 	bnxt_debug_exit();
17900 }
17901 
17902 module_init(bnxt_init);
17903 module_exit(bnxt_exit);
17904