xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 385f186aba3d2f7122b71d6d4c7e236b9d4e8003)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_queues.h>
58 
59 #include "bnxt_hsi.h"
60 #include "bnxt.h"
61 #include "bnxt_hwrm.h"
62 #include "bnxt_ulp.h"
63 #include "bnxt_sriov.h"
64 #include "bnxt_ethtool.h"
65 #include "bnxt_dcb.h"
66 #include "bnxt_xdp.h"
67 #include "bnxt_ptp.h"
68 #include "bnxt_vfr.h"
69 #include "bnxt_tc.h"
70 #include "bnxt_devlink.h"
71 #include "bnxt_debugfs.h"
72 #include "bnxt_coredump.h"
73 #include "bnxt_hwmon.h"
74 
75 #define BNXT_TX_TIMEOUT		(5 * HZ)
76 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
77 				 NETIF_MSG_TX_ERR)
78 
79 MODULE_LICENSE("GPL");
80 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
81 
82 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
83 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
84 #define BNXT_RX_COPY_THRESH 256
85 
86 #define BNXT_TX_PUSH_THRESH 164
87 
88 /* indexed by enum board_idx */
89 static const struct {
90 	char *name;
91 } board_info[] = {
92 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
93 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
94 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
95 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
96 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
97 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
98 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
99 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
100 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
101 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
102 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
103 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
104 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
105 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
106 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
108 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
109 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
110 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
111 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
112 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
113 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
114 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
115 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
116 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
117 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
118 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
119 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
120 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
121 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
122 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
123 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
124 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
125 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
126 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
127 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
128 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
129 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
130 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
131 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
132 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
133 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
134 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
135 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
136 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
137 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
138 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
139 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
140 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
141 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
142 };
143 
144 static const struct pci_device_id bnxt_pci_tbl[] = {
145 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
146 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
147 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
148 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
149 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
150 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
151 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
152 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
153 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
166 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
168 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
169 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
170 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
171 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
172 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
173 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
178 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
179 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
180 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
181 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
182 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
183 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
188 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
189 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
190 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
191 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
193 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
194 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
195 #ifdef CONFIG_BNXT_SRIOV
196 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
197 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
198 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
199 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
200 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
201 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
202 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
206 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
207 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
208 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
209 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
210 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
211 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
212 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
214 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
215 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
216 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
217 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
218 #endif
219 	{ 0 }
220 };
221 
222 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
223 
224 static const u16 bnxt_vf_req_snif[] = {
225 	HWRM_FUNC_CFG,
226 	HWRM_FUNC_VF_CFG,
227 	HWRM_PORT_PHY_QCFG,
228 	HWRM_CFA_L2_FILTER_ALLOC,
229 };
230 
231 static const u16 bnxt_async_events_arr[] = {
232 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
233 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
234 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
235 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
236 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
237 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
238 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
239 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
240 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
241 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
242 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
243 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
244 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
245 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
246 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
247 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
248 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
249 };
250 
251 const u16 bnxt_bstore_to_trace[] = {
252 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
253 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
254 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
255 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
256 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
257 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
258 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
259 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
260 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
261 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
262 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
263 };
264 
265 static struct workqueue_struct *bnxt_pf_wq;
266 
267 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
268 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
269 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
270 
271 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
272 	.ports = {
273 		.src = 0,
274 		.dst = 0,
275 	},
276 	.addrs = {
277 		.v6addrs = {
278 			.src = BNXT_IPV6_MASK_NONE,
279 			.dst = BNXT_IPV6_MASK_NONE,
280 		},
281 	},
282 };
283 
284 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
285 	.ports = {
286 		.src = cpu_to_be16(0xffff),
287 		.dst = cpu_to_be16(0xffff),
288 	},
289 	.addrs = {
290 		.v6addrs = {
291 			.src = BNXT_IPV6_MASK_ALL,
292 			.dst = BNXT_IPV6_MASK_ALL,
293 		},
294 	},
295 };
296 
297 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
298 	.ports = {
299 		.src = cpu_to_be16(0xffff),
300 		.dst = cpu_to_be16(0xffff),
301 	},
302 	.addrs = {
303 		.v4addrs = {
304 			.src = cpu_to_be32(0xffffffff),
305 			.dst = cpu_to_be32(0xffffffff),
306 		},
307 	},
308 };
309 
310 static bool bnxt_vf_pciid(enum board_idx idx)
311 {
312 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
313 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
314 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
315 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF);
316 }
317 
318 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
319 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
320 
321 #define BNXT_DB_CQ(db, idx)						\
322 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
323 
324 #define BNXT_DB_NQ_P5(db, idx)						\
325 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
326 		    (db)->doorbell)
327 
328 #define BNXT_DB_NQ_P7(db, idx)						\
329 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
330 		    DB_RING_IDX(db, idx), (db)->doorbell)
331 
332 #define BNXT_DB_CQ_ARM(db, idx)						\
333 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
334 
335 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
336 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
337 		    DB_RING_IDX(db, idx), (db)->doorbell)
338 
339 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
340 {
341 	if (bp->flags & BNXT_FLAG_CHIP_P7)
342 		BNXT_DB_NQ_P7(db, idx);
343 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
344 		BNXT_DB_NQ_P5(db, idx);
345 	else
346 		BNXT_DB_CQ(db, idx);
347 }
348 
349 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
350 {
351 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
352 		BNXT_DB_NQ_ARM_P5(db, idx);
353 	else
354 		BNXT_DB_CQ_ARM(db, idx);
355 }
356 
357 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
358 {
359 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
360 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
361 			    DB_RING_IDX(db, idx), db->doorbell);
362 	else
363 		BNXT_DB_CQ(db, idx);
364 }
365 
366 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
367 {
368 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
369 		return;
370 
371 	if (BNXT_PF(bp))
372 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
373 	else
374 		schedule_delayed_work(&bp->fw_reset_task, delay);
375 }
376 
377 static void __bnxt_queue_sp_work(struct bnxt *bp)
378 {
379 	if (BNXT_PF(bp))
380 		queue_work(bnxt_pf_wq, &bp->sp_task);
381 	else
382 		schedule_work(&bp->sp_task);
383 }
384 
385 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
386 {
387 	set_bit(event, &bp->sp_event);
388 	__bnxt_queue_sp_work(bp);
389 }
390 
391 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
392 {
393 	if (!rxr->bnapi->in_reset) {
394 		rxr->bnapi->in_reset = true;
395 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
396 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
397 		else
398 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
399 		__bnxt_queue_sp_work(bp);
400 	}
401 	rxr->rx_next_cons = 0xffff;
402 }
403 
404 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
405 			  u16 curr)
406 {
407 	struct bnxt_napi *bnapi = txr->bnapi;
408 
409 	if (bnapi->tx_fault)
410 		return;
411 
412 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
413 		   txr->txq_index, txr->tx_hw_cons,
414 		   txr->tx_cons, txr->tx_prod, curr);
415 	WARN_ON_ONCE(1);
416 	bnapi->tx_fault = 1;
417 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
418 }
419 
420 const u16 bnxt_lhint_arr[] = {
421 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
422 	TX_BD_FLAGS_LHINT_512_TO_1023,
423 	TX_BD_FLAGS_LHINT_1024_TO_2047,
424 	TX_BD_FLAGS_LHINT_1024_TO_2047,
425 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
426 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
427 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
428 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
429 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
430 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
431 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
432 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
433 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
434 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
435 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
436 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
437 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
438 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
439 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
440 };
441 
442 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
443 {
444 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
445 
446 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
447 		return 0;
448 
449 	return md_dst->u.port_info.port_id;
450 }
451 
452 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
453 			     u16 prod)
454 {
455 	/* Sync BD data before updating doorbell */
456 	wmb();
457 	bnxt_db_write(bp, &txr->tx_db, prod);
458 	txr->kick_pending = 0;
459 }
460 
461 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
462 {
463 	struct bnxt *bp = netdev_priv(dev);
464 	struct tx_bd *txbd, *txbd0;
465 	struct tx_bd_ext *txbd1;
466 	struct netdev_queue *txq;
467 	int i;
468 	dma_addr_t mapping;
469 	unsigned int length, pad = 0;
470 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
471 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
472 	struct pci_dev *pdev = bp->pdev;
473 	u16 prod, last_frag, txts_prod;
474 	struct bnxt_tx_ring_info *txr;
475 	struct bnxt_sw_tx_bd *tx_buf;
476 	__le32 lflags = 0;
477 
478 	i = skb_get_queue_mapping(skb);
479 	if (unlikely(i >= bp->tx_nr_rings)) {
480 		dev_kfree_skb_any(skb);
481 		dev_core_stats_tx_dropped_inc(dev);
482 		return NETDEV_TX_OK;
483 	}
484 
485 	txq = netdev_get_tx_queue(dev, i);
486 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
487 	prod = txr->tx_prod;
488 
489 	free_size = bnxt_tx_avail(bp, txr);
490 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
491 		/* We must have raced with NAPI cleanup */
492 		if (net_ratelimit() && txr->kick_pending)
493 			netif_warn(bp, tx_err, dev,
494 				   "bnxt: ring busy w/ flush pending!\n");
495 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
496 					bp->tx_wake_thresh))
497 			return NETDEV_TX_BUSY;
498 	}
499 
500 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
501 		goto tx_free;
502 
503 	length = skb->len;
504 	len = skb_headlen(skb);
505 	last_frag = skb_shinfo(skb)->nr_frags;
506 
507 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
508 
509 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
510 	tx_buf->skb = skb;
511 	tx_buf->nr_frags = last_frag;
512 
513 	vlan_tag_flags = 0;
514 	cfa_action = bnxt_xmit_get_cfa_action(skb);
515 	if (skb_vlan_tag_present(skb)) {
516 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
517 				 skb_vlan_tag_get(skb);
518 		/* Currently supports 8021Q, 8021AD vlan offloads
519 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
520 		 */
521 		if (skb->vlan_proto == htons(ETH_P_8021Q))
522 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
523 	}
524 
525 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
526 	    ptp->tx_tstamp_en) {
527 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
528 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
529 			tx_buf->is_ts_pkt = 1;
530 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
531 		} else if (!skb_is_gso(skb)) {
532 			u16 seq_id, hdr_off;
533 
534 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
535 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
536 				if (vlan_tag_flags)
537 					hdr_off += VLAN_HLEN;
538 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
539 				tx_buf->is_ts_pkt = 1;
540 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
541 
542 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
543 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
544 				tx_buf->txts_prod = txts_prod;
545 			}
546 		}
547 	}
548 	if (unlikely(skb->no_fcs))
549 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
550 
551 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
552 	    !lflags) {
553 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
554 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
555 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
556 		void __iomem *db = txr->tx_db.doorbell;
557 		void *pdata = tx_push_buf->data;
558 		u64 *end;
559 		int j, push_len;
560 
561 		/* Set COAL_NOW to be ready quickly for the next push */
562 		tx_push->tx_bd_len_flags_type =
563 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
564 					TX_BD_TYPE_LONG_TX_BD |
565 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
566 					TX_BD_FLAGS_COAL_NOW |
567 					TX_BD_FLAGS_PACKET_END |
568 					(2 << TX_BD_FLAGS_BD_CNT_SHIFT));
569 
570 		if (skb->ip_summed == CHECKSUM_PARTIAL)
571 			tx_push1->tx_bd_hsize_lflags =
572 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
573 		else
574 			tx_push1->tx_bd_hsize_lflags = 0;
575 
576 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
577 		tx_push1->tx_bd_cfa_action =
578 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
579 
580 		end = pdata + length;
581 		end = PTR_ALIGN(end, 8) - 1;
582 		*end = 0;
583 
584 		skb_copy_from_linear_data(skb, pdata, len);
585 		pdata += len;
586 		for (j = 0; j < last_frag; j++) {
587 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
588 			void *fptr;
589 
590 			fptr = skb_frag_address_safe(frag);
591 			if (!fptr)
592 				goto normal_tx;
593 
594 			memcpy(pdata, fptr, skb_frag_size(frag));
595 			pdata += skb_frag_size(frag);
596 		}
597 
598 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
599 		txbd->tx_bd_haddr = txr->data_mapping;
600 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
601 		prod = NEXT_TX(prod);
602 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
603 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
604 		memcpy(txbd, tx_push1, sizeof(*txbd));
605 		prod = NEXT_TX(prod);
606 		tx_push->doorbell =
607 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
608 				    DB_RING_IDX(&txr->tx_db, prod));
609 		WRITE_ONCE(txr->tx_prod, prod);
610 
611 		tx_buf->is_push = 1;
612 		netdev_tx_sent_queue(txq, skb->len);
613 		wmb();	/* Sync is_push and byte queue before pushing data */
614 
615 		push_len = (length + sizeof(*tx_push) + 7) / 8;
616 		if (push_len > 16) {
617 			__iowrite64_copy(db, tx_push_buf, 16);
618 			__iowrite32_copy(db + 4, tx_push_buf + 1,
619 					 (push_len - 16) << 1);
620 		} else {
621 			__iowrite64_copy(db, tx_push_buf, push_len);
622 		}
623 
624 		goto tx_done;
625 	}
626 
627 normal_tx:
628 	if (length < BNXT_MIN_PKT_SIZE) {
629 		pad = BNXT_MIN_PKT_SIZE - length;
630 		if (skb_pad(skb, pad))
631 			/* SKB already freed. */
632 			goto tx_kick_pending;
633 		length = BNXT_MIN_PKT_SIZE;
634 	}
635 
636 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
637 
638 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
639 		goto tx_free;
640 
641 	dma_unmap_addr_set(tx_buf, mapping, mapping);
642 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
643 		((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
644 
645 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
646 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
647 
648 	prod = NEXT_TX(prod);
649 	txbd1 = (struct tx_bd_ext *)
650 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
651 
652 	txbd1->tx_bd_hsize_lflags = lflags;
653 	if (skb_is_gso(skb)) {
654 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
655 		u32 hdr_len;
656 
657 		if (skb->encapsulation) {
658 			if (udp_gso)
659 				hdr_len = skb_inner_transport_offset(skb) +
660 					  sizeof(struct udphdr);
661 			else
662 				hdr_len = skb_inner_tcp_all_headers(skb);
663 		} else if (udp_gso) {
664 			hdr_len = skb_transport_offset(skb) +
665 				  sizeof(struct udphdr);
666 		} else {
667 			hdr_len = skb_tcp_all_headers(skb);
668 		}
669 
670 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
671 					TX_BD_FLAGS_T_IPID |
672 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
673 		length = skb_shinfo(skb)->gso_size;
674 		txbd1->tx_bd_mss = cpu_to_le32(length);
675 		length += hdr_len;
676 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
677 		txbd1->tx_bd_hsize_lflags |=
678 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
679 		txbd1->tx_bd_mss = 0;
680 	}
681 
682 	length >>= 9;
683 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
684 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
685 				     skb->len);
686 		i = 0;
687 		goto tx_dma_error;
688 	}
689 	flags |= bnxt_lhint_arr[length];
690 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
691 
692 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
693 	txbd1->tx_bd_cfa_action =
694 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
695 	txbd0 = txbd;
696 	for (i = 0; i < last_frag; i++) {
697 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
698 
699 		prod = NEXT_TX(prod);
700 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
701 
702 		len = skb_frag_size(frag);
703 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
704 					   DMA_TO_DEVICE);
705 
706 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
707 			goto tx_dma_error;
708 
709 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
710 		dma_unmap_addr_set(tx_buf, mapping, mapping);
711 
712 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
713 
714 		flags = len << TX_BD_LEN_SHIFT;
715 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
716 	}
717 
718 	flags &= ~TX_BD_LEN;
719 	txbd->tx_bd_len_flags_type =
720 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
721 			    TX_BD_FLAGS_PACKET_END);
722 
723 	netdev_tx_sent_queue(txq, skb->len);
724 
725 	skb_tx_timestamp(skb);
726 
727 	prod = NEXT_TX(prod);
728 	WRITE_ONCE(txr->tx_prod, prod);
729 
730 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
731 		bnxt_txr_db_kick(bp, txr, prod);
732 	} else {
733 		if (free_size >= bp->tx_wake_thresh)
734 			txbd0->tx_bd_len_flags_type |=
735 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
736 		txr->kick_pending = 1;
737 	}
738 
739 tx_done:
740 
741 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
742 		if (netdev_xmit_more() && !tx_buf->is_push) {
743 			txbd0->tx_bd_len_flags_type &=
744 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
745 			bnxt_txr_db_kick(bp, txr, prod);
746 		}
747 
748 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
749 				   bp->tx_wake_thresh);
750 	}
751 	return NETDEV_TX_OK;
752 
753 tx_dma_error:
754 	last_frag = i;
755 
756 	/* start back at beginning and unmap skb */
757 	prod = txr->tx_prod;
758 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
759 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
760 			 skb_headlen(skb), DMA_TO_DEVICE);
761 	prod = NEXT_TX(prod);
762 
763 	/* unmap remaining mapped pages */
764 	for (i = 0; i < last_frag; i++) {
765 		prod = NEXT_TX(prod);
766 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
767 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
768 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
769 			       DMA_TO_DEVICE);
770 	}
771 
772 tx_free:
773 	dev_kfree_skb_any(skb);
774 tx_kick_pending:
775 	if (BNXT_TX_PTP_IS_SET(lflags)) {
776 		txr->tx_buf_ring[txr->tx_prod].is_ts_pkt = 0;
777 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
778 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
779 			/* set SKB to err so PTP worker will clean up */
780 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
781 	}
782 	if (txr->kick_pending)
783 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
784 	txr->tx_buf_ring[txr->tx_prod].skb = NULL;
785 	dev_core_stats_tx_dropped_inc(dev);
786 	return NETDEV_TX_OK;
787 }
788 
789 /* Returns true if some remaining TX packets not processed. */
790 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
791 			  int budget)
792 {
793 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
794 	struct pci_dev *pdev = bp->pdev;
795 	u16 hw_cons = txr->tx_hw_cons;
796 	unsigned int tx_bytes = 0;
797 	u16 cons = txr->tx_cons;
798 	int tx_pkts = 0;
799 	bool rc = false;
800 
801 	while (RING_TX(bp, cons) != hw_cons) {
802 		struct bnxt_sw_tx_bd *tx_buf;
803 		struct sk_buff *skb;
804 		bool is_ts_pkt;
805 		int j, last;
806 
807 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
808 		skb = tx_buf->skb;
809 
810 		if (unlikely(!skb)) {
811 			bnxt_sched_reset_txr(bp, txr, cons);
812 			return rc;
813 		}
814 
815 		is_ts_pkt = tx_buf->is_ts_pkt;
816 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
817 			rc = true;
818 			break;
819 		}
820 
821 		cons = NEXT_TX(cons);
822 		tx_pkts++;
823 		tx_bytes += skb->len;
824 		tx_buf->skb = NULL;
825 		tx_buf->is_ts_pkt = 0;
826 
827 		if (tx_buf->is_push) {
828 			tx_buf->is_push = 0;
829 			goto next_tx_int;
830 		}
831 
832 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
833 				 skb_headlen(skb), DMA_TO_DEVICE);
834 		last = tx_buf->nr_frags;
835 
836 		for (j = 0; j < last; j++) {
837 			cons = NEXT_TX(cons);
838 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
839 			dma_unmap_page(
840 				&pdev->dev,
841 				dma_unmap_addr(tx_buf, mapping),
842 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
843 				DMA_TO_DEVICE);
844 		}
845 		if (unlikely(is_ts_pkt)) {
846 			if (BNXT_CHIP_P5(bp)) {
847 				/* PTP worker takes ownership of the skb */
848 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
849 				skb = NULL;
850 			}
851 		}
852 
853 next_tx_int:
854 		cons = NEXT_TX(cons);
855 
856 		dev_consume_skb_any(skb);
857 	}
858 
859 	WRITE_ONCE(txr->tx_cons, cons);
860 
861 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
862 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
863 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
864 
865 	return rc;
866 }
867 
868 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
869 {
870 	struct bnxt_tx_ring_info *txr;
871 	bool more = false;
872 	int i;
873 
874 	bnxt_for_each_napi_tx(i, bnapi, txr) {
875 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
876 			more |= __bnxt_tx_int(bp, txr, budget);
877 	}
878 	if (!more)
879 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
880 }
881 
882 static bool bnxt_separate_head_pool(void)
883 {
884 	return PAGE_SIZE > BNXT_RX_PAGE_SIZE;
885 }
886 
887 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
888 					 struct bnxt_rx_ring_info *rxr,
889 					 unsigned int *offset,
890 					 gfp_t gfp)
891 {
892 	struct page *page;
893 
894 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
895 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
896 						BNXT_RX_PAGE_SIZE);
897 	} else {
898 		page = page_pool_dev_alloc_pages(rxr->page_pool);
899 		*offset = 0;
900 	}
901 	if (!page)
902 		return NULL;
903 
904 	*mapping = page_pool_get_dma_addr(page) + *offset;
905 	return page;
906 }
907 
908 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
909 				       struct bnxt_rx_ring_info *rxr,
910 				       gfp_t gfp)
911 {
912 	unsigned int offset;
913 	struct page *page;
914 
915 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
916 				    bp->rx_buf_size, gfp);
917 	if (!page)
918 		return NULL;
919 
920 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
921 	return page_address(page) + offset;
922 }
923 
924 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
925 		       u16 prod, gfp_t gfp)
926 {
927 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
928 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
929 	dma_addr_t mapping;
930 
931 	if (BNXT_RX_PAGE_MODE(bp)) {
932 		unsigned int offset;
933 		struct page *page =
934 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
935 
936 		if (!page)
937 			return -ENOMEM;
938 
939 		mapping += bp->rx_dma_offset;
940 		rx_buf->data = page;
941 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
942 	} else {
943 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
944 
945 		if (!data)
946 			return -ENOMEM;
947 
948 		rx_buf->data = data;
949 		rx_buf->data_ptr = data + bp->rx_offset;
950 	}
951 	rx_buf->mapping = mapping;
952 
953 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
954 	return 0;
955 }
956 
957 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
958 {
959 	u16 prod = rxr->rx_prod;
960 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
961 	struct bnxt *bp = rxr->bnapi->bp;
962 	struct rx_bd *cons_bd, *prod_bd;
963 
964 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
965 	cons_rx_buf = &rxr->rx_buf_ring[cons];
966 
967 	prod_rx_buf->data = data;
968 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
969 
970 	prod_rx_buf->mapping = cons_rx_buf->mapping;
971 
972 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
973 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
974 
975 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
976 }
977 
978 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
979 {
980 	u16 next, max = rxr->rx_agg_bmap_size;
981 
982 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
983 	if (next >= max)
984 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
985 	return next;
986 }
987 
988 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
989 				     struct bnxt_rx_ring_info *rxr,
990 				     u16 prod, gfp_t gfp)
991 {
992 	struct rx_bd *rxbd =
993 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
994 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
995 	struct page *page;
996 	dma_addr_t mapping;
997 	u16 sw_prod = rxr->rx_sw_agg_prod;
998 	unsigned int offset = 0;
999 
1000 	page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
1001 
1002 	if (!page)
1003 		return -ENOMEM;
1004 
1005 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1006 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1007 
1008 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1009 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1010 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1011 
1012 	rx_agg_buf->page = page;
1013 	rx_agg_buf->offset = offset;
1014 	rx_agg_buf->mapping = mapping;
1015 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1016 	rxbd->rx_bd_opaque = sw_prod;
1017 	return 0;
1018 }
1019 
1020 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1021 				       struct bnxt_cp_ring_info *cpr,
1022 				       u16 cp_cons, u16 curr)
1023 {
1024 	struct rx_agg_cmp *agg;
1025 
1026 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1027 	agg = (struct rx_agg_cmp *)
1028 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1029 	return agg;
1030 }
1031 
1032 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1033 					      struct bnxt_rx_ring_info *rxr,
1034 					      u16 agg_id, u16 curr)
1035 {
1036 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1037 
1038 	return &tpa_info->agg_arr[curr];
1039 }
1040 
1041 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1042 				   u16 start, u32 agg_bufs, bool tpa)
1043 {
1044 	struct bnxt_napi *bnapi = cpr->bnapi;
1045 	struct bnxt *bp = bnapi->bp;
1046 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1047 	u16 prod = rxr->rx_agg_prod;
1048 	u16 sw_prod = rxr->rx_sw_agg_prod;
1049 	bool p5_tpa = false;
1050 	u32 i;
1051 
1052 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1053 		p5_tpa = true;
1054 
1055 	for (i = 0; i < agg_bufs; i++) {
1056 		u16 cons;
1057 		struct rx_agg_cmp *agg;
1058 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1059 		struct rx_bd *prod_bd;
1060 		struct page *page;
1061 
1062 		if (p5_tpa)
1063 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1064 		else
1065 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1066 		cons = agg->rx_agg_cmp_opaque;
1067 		__clear_bit(cons, rxr->rx_agg_bmap);
1068 
1069 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1070 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1071 
1072 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1073 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1074 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1075 
1076 		/* It is possible for sw_prod to be equal to cons, so
1077 		 * set cons_rx_buf->page to NULL first.
1078 		 */
1079 		page = cons_rx_buf->page;
1080 		cons_rx_buf->page = NULL;
1081 		prod_rx_buf->page = page;
1082 		prod_rx_buf->offset = cons_rx_buf->offset;
1083 
1084 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1085 
1086 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1087 
1088 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1089 		prod_bd->rx_bd_opaque = sw_prod;
1090 
1091 		prod = NEXT_RX_AGG(prod);
1092 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1093 	}
1094 	rxr->rx_agg_prod = prod;
1095 	rxr->rx_sw_agg_prod = sw_prod;
1096 }
1097 
1098 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1099 					      struct bnxt_rx_ring_info *rxr,
1100 					      u16 cons, void *data, u8 *data_ptr,
1101 					      dma_addr_t dma_addr,
1102 					      unsigned int offset_and_len)
1103 {
1104 	unsigned int len = offset_and_len & 0xffff;
1105 	struct page *page = data;
1106 	u16 prod = rxr->rx_prod;
1107 	struct sk_buff *skb;
1108 	int err;
1109 
1110 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1111 	if (unlikely(err)) {
1112 		bnxt_reuse_rx_data(rxr, cons, data);
1113 		return NULL;
1114 	}
1115 	dma_addr -= bp->rx_dma_offset;
1116 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1117 				bp->rx_dir);
1118 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1119 	if (!skb) {
1120 		page_pool_recycle_direct(rxr->page_pool, page);
1121 		return NULL;
1122 	}
1123 	skb_mark_for_recycle(skb);
1124 	skb_reserve(skb, bp->rx_offset);
1125 	__skb_put(skb, len);
1126 
1127 	return skb;
1128 }
1129 
1130 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1131 					struct bnxt_rx_ring_info *rxr,
1132 					u16 cons, void *data, u8 *data_ptr,
1133 					dma_addr_t dma_addr,
1134 					unsigned int offset_and_len)
1135 {
1136 	unsigned int payload = offset_and_len >> 16;
1137 	unsigned int len = offset_and_len & 0xffff;
1138 	skb_frag_t *frag;
1139 	struct page *page = data;
1140 	u16 prod = rxr->rx_prod;
1141 	struct sk_buff *skb;
1142 	int off, err;
1143 
1144 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1145 	if (unlikely(err)) {
1146 		bnxt_reuse_rx_data(rxr, cons, data);
1147 		return NULL;
1148 	}
1149 	dma_addr -= bp->rx_dma_offset;
1150 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1151 				bp->rx_dir);
1152 
1153 	if (unlikely(!payload))
1154 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1155 
1156 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1157 	if (!skb) {
1158 		page_pool_recycle_direct(rxr->page_pool, page);
1159 		return NULL;
1160 	}
1161 
1162 	skb_mark_for_recycle(skb);
1163 	off = (void *)data_ptr - page_address(page);
1164 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1165 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1166 	       payload + NET_IP_ALIGN);
1167 
1168 	frag = &skb_shinfo(skb)->frags[0];
1169 	skb_frag_size_sub(frag, payload);
1170 	skb_frag_off_add(frag, payload);
1171 	skb->data_len -= payload;
1172 	skb->tail += payload;
1173 
1174 	return skb;
1175 }
1176 
1177 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1178 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1179 				   void *data, u8 *data_ptr,
1180 				   dma_addr_t dma_addr,
1181 				   unsigned int offset_and_len)
1182 {
1183 	u16 prod = rxr->rx_prod;
1184 	struct sk_buff *skb;
1185 	int err;
1186 
1187 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1188 	if (unlikely(err)) {
1189 		bnxt_reuse_rx_data(rxr, cons, data);
1190 		return NULL;
1191 	}
1192 
1193 	skb = napi_build_skb(data, bp->rx_buf_size);
1194 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1195 				bp->rx_dir);
1196 	if (!skb) {
1197 		page_pool_free_va(rxr->head_pool, data, true);
1198 		return NULL;
1199 	}
1200 
1201 	skb_mark_for_recycle(skb);
1202 	skb_reserve(skb, bp->rx_offset);
1203 	skb_put(skb, offset_and_len & 0xffff);
1204 	return skb;
1205 }
1206 
1207 static u32 __bnxt_rx_agg_pages(struct bnxt *bp,
1208 			       struct bnxt_cp_ring_info *cpr,
1209 			       struct skb_shared_info *shinfo,
1210 			       u16 idx, u32 agg_bufs, bool tpa,
1211 			       struct xdp_buff *xdp)
1212 {
1213 	struct bnxt_napi *bnapi = cpr->bnapi;
1214 	struct pci_dev *pdev = bp->pdev;
1215 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1216 	u16 prod = rxr->rx_agg_prod;
1217 	u32 i, total_frag_len = 0;
1218 	bool p5_tpa = false;
1219 
1220 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1221 		p5_tpa = true;
1222 
1223 	for (i = 0; i < agg_bufs; i++) {
1224 		skb_frag_t *frag = &shinfo->frags[i];
1225 		u16 cons, frag_len;
1226 		struct rx_agg_cmp *agg;
1227 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1228 		struct page *page;
1229 		dma_addr_t mapping;
1230 
1231 		if (p5_tpa)
1232 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1233 		else
1234 			agg = bnxt_get_agg(bp, cpr, idx, i);
1235 		cons = agg->rx_agg_cmp_opaque;
1236 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1237 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1238 
1239 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1240 		skb_frag_fill_page_desc(frag, cons_rx_buf->page,
1241 					cons_rx_buf->offset, frag_len);
1242 		shinfo->nr_frags = i + 1;
1243 		__clear_bit(cons, rxr->rx_agg_bmap);
1244 
1245 		/* It is possible for bnxt_alloc_rx_page() to allocate
1246 		 * a sw_prod index that equals the cons index, so we
1247 		 * need to clear the cons entry now.
1248 		 */
1249 		mapping = cons_rx_buf->mapping;
1250 		page = cons_rx_buf->page;
1251 		cons_rx_buf->page = NULL;
1252 
1253 		if (xdp && page_is_pfmemalloc(page))
1254 			xdp_buff_set_frag_pfmemalloc(xdp);
1255 
1256 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1257 			--shinfo->nr_frags;
1258 			cons_rx_buf->page = page;
1259 
1260 			/* Update prod since possibly some pages have been
1261 			 * allocated already.
1262 			 */
1263 			rxr->rx_agg_prod = prod;
1264 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1265 			return 0;
1266 		}
1267 
1268 		dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1269 					bp->rx_dir);
1270 
1271 		total_frag_len += frag_len;
1272 		prod = NEXT_RX_AGG(prod);
1273 	}
1274 	rxr->rx_agg_prod = prod;
1275 	return total_frag_len;
1276 }
1277 
1278 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp,
1279 					     struct bnxt_cp_ring_info *cpr,
1280 					     struct sk_buff *skb, u16 idx,
1281 					     u32 agg_bufs, bool tpa)
1282 {
1283 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1284 	u32 total_frag_len = 0;
1285 
1286 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx,
1287 					     agg_bufs, tpa, NULL);
1288 	if (!total_frag_len) {
1289 		skb_mark_for_recycle(skb);
1290 		dev_kfree_skb(skb);
1291 		return NULL;
1292 	}
1293 
1294 	skb->data_len += total_frag_len;
1295 	skb->len += total_frag_len;
1296 	skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs;
1297 	return skb;
1298 }
1299 
1300 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp,
1301 				 struct bnxt_cp_ring_info *cpr,
1302 				 struct xdp_buff *xdp, u16 idx,
1303 				 u32 agg_bufs, bool tpa)
1304 {
1305 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1306 	u32 total_frag_len = 0;
1307 
1308 	if (!xdp_buff_has_frags(xdp))
1309 		shinfo->nr_frags = 0;
1310 
1311 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo,
1312 					     idx, agg_bufs, tpa, xdp);
1313 	if (total_frag_len) {
1314 		xdp_buff_set_frags_flag(xdp);
1315 		shinfo->nr_frags = agg_bufs;
1316 		shinfo->xdp_frags_size = total_frag_len;
1317 	}
1318 	return total_frag_len;
1319 }
1320 
1321 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1322 			       u8 agg_bufs, u32 *raw_cons)
1323 {
1324 	u16 last;
1325 	struct rx_agg_cmp *agg;
1326 
1327 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1328 	last = RING_CMP(*raw_cons);
1329 	agg = (struct rx_agg_cmp *)
1330 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1331 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1332 }
1333 
1334 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1335 				      unsigned int len,
1336 				      dma_addr_t mapping)
1337 {
1338 	struct bnxt *bp = bnapi->bp;
1339 	struct pci_dev *pdev = bp->pdev;
1340 	struct sk_buff *skb;
1341 
1342 	skb = napi_alloc_skb(&bnapi->napi, len);
1343 	if (!skb)
1344 		return NULL;
1345 
1346 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1347 				bp->rx_dir);
1348 
1349 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1350 	       len + NET_IP_ALIGN);
1351 
1352 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1353 				   bp->rx_dir);
1354 
1355 	skb_put(skb, len);
1356 
1357 	return skb;
1358 }
1359 
1360 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1361 				     unsigned int len,
1362 				     dma_addr_t mapping)
1363 {
1364 	return bnxt_copy_data(bnapi, data, len, mapping);
1365 }
1366 
1367 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1368 				     struct xdp_buff *xdp,
1369 				     unsigned int len,
1370 				     dma_addr_t mapping)
1371 {
1372 	unsigned int metasize = 0;
1373 	u8 *data = xdp->data;
1374 	struct sk_buff *skb;
1375 
1376 	len = xdp->data_end - xdp->data_meta;
1377 	metasize = xdp->data - xdp->data_meta;
1378 	data = xdp->data_meta;
1379 
1380 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1381 	if (!skb)
1382 		return skb;
1383 
1384 	if (metasize) {
1385 		skb_metadata_set(skb, metasize);
1386 		__skb_pull(skb, metasize);
1387 	}
1388 
1389 	return skb;
1390 }
1391 
1392 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1393 			   u32 *raw_cons, void *cmp)
1394 {
1395 	struct rx_cmp *rxcmp = cmp;
1396 	u32 tmp_raw_cons = *raw_cons;
1397 	u8 cmp_type, agg_bufs = 0;
1398 
1399 	cmp_type = RX_CMP_TYPE(rxcmp);
1400 
1401 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1402 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1403 			    RX_CMP_AGG_BUFS) >>
1404 			   RX_CMP_AGG_BUFS_SHIFT;
1405 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1406 		struct rx_tpa_end_cmp *tpa_end = cmp;
1407 
1408 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1409 			return 0;
1410 
1411 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1412 	}
1413 
1414 	if (agg_bufs) {
1415 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1416 			return -EBUSY;
1417 	}
1418 	*raw_cons = tmp_raw_cons;
1419 	return 0;
1420 }
1421 
1422 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1423 {
1424 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1425 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1426 
1427 	if (test_bit(idx, map->agg_idx_bmap))
1428 		idx = find_first_zero_bit(map->agg_idx_bmap,
1429 					  BNXT_AGG_IDX_BMAP_SIZE);
1430 	__set_bit(idx, map->agg_idx_bmap);
1431 	map->agg_id_tbl[agg_id] = idx;
1432 	return idx;
1433 }
1434 
1435 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1436 {
1437 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1438 
1439 	__clear_bit(idx, map->agg_idx_bmap);
1440 }
1441 
1442 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1443 {
1444 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1445 
1446 	return map->agg_id_tbl[agg_id];
1447 }
1448 
1449 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1450 			      struct rx_tpa_start_cmp *tpa_start,
1451 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1452 {
1453 	tpa_info->cfa_code_valid = 1;
1454 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1455 	tpa_info->vlan_valid = 0;
1456 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1457 		tpa_info->vlan_valid = 1;
1458 		tpa_info->metadata =
1459 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1460 	}
1461 }
1462 
1463 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1464 				 struct rx_tpa_start_cmp *tpa_start,
1465 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1466 {
1467 	tpa_info->vlan_valid = 0;
1468 	if (TPA_START_VLAN_VALID(tpa_start)) {
1469 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1470 		u32 vlan_proto = ETH_P_8021Q;
1471 
1472 		tpa_info->vlan_valid = 1;
1473 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1474 			vlan_proto = ETH_P_8021AD;
1475 		tpa_info->metadata = vlan_proto << 16 |
1476 				     TPA_START_METADATA0_TCI(tpa_start1);
1477 	}
1478 }
1479 
1480 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1481 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1482 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1483 {
1484 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1485 	struct bnxt_tpa_info *tpa_info;
1486 	u16 cons, prod, agg_id;
1487 	struct rx_bd *prod_bd;
1488 	dma_addr_t mapping;
1489 
1490 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1491 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1492 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1493 	} else {
1494 		agg_id = TPA_START_AGG_ID(tpa_start);
1495 	}
1496 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1497 	prod = rxr->rx_prod;
1498 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1499 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1500 	tpa_info = &rxr->rx_tpa[agg_id];
1501 
1502 	if (unlikely(cons != rxr->rx_next_cons ||
1503 		     TPA_START_ERROR(tpa_start))) {
1504 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1505 			    cons, rxr->rx_next_cons,
1506 			    TPA_START_ERROR_CODE(tpa_start1));
1507 		bnxt_sched_reset_rxr(bp, rxr);
1508 		return;
1509 	}
1510 	prod_rx_buf->data = tpa_info->data;
1511 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1512 
1513 	mapping = tpa_info->mapping;
1514 	prod_rx_buf->mapping = mapping;
1515 
1516 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1517 
1518 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1519 
1520 	tpa_info->data = cons_rx_buf->data;
1521 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1522 	cons_rx_buf->data = NULL;
1523 	tpa_info->mapping = cons_rx_buf->mapping;
1524 
1525 	tpa_info->len =
1526 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1527 				RX_TPA_START_CMP_LEN_SHIFT;
1528 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1529 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1530 		tpa_info->gso_type = SKB_GSO_TCPV4;
1531 		if (TPA_START_IS_IPV6(tpa_start1))
1532 			tpa_info->gso_type = SKB_GSO_TCPV6;
1533 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1534 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1535 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1536 			tpa_info->gso_type = SKB_GSO_TCPV6;
1537 		tpa_info->rss_hash =
1538 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1539 	} else {
1540 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1541 		tpa_info->gso_type = 0;
1542 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1543 	}
1544 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1545 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1546 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1547 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1548 	else
1549 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1550 	tpa_info->agg_count = 0;
1551 
1552 	rxr->rx_prod = NEXT_RX(prod);
1553 	cons = RING_RX(bp, NEXT_RX(cons));
1554 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1555 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1556 
1557 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1558 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1559 	cons_rx_buf->data = NULL;
1560 }
1561 
1562 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1563 {
1564 	if (agg_bufs)
1565 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1566 }
1567 
1568 #ifdef CONFIG_INET
1569 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1570 {
1571 	struct udphdr *uh = NULL;
1572 
1573 	if (ip_proto == htons(ETH_P_IP)) {
1574 		struct iphdr *iph = (struct iphdr *)skb->data;
1575 
1576 		if (iph->protocol == IPPROTO_UDP)
1577 			uh = (struct udphdr *)(iph + 1);
1578 	} else {
1579 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1580 
1581 		if (iph->nexthdr == IPPROTO_UDP)
1582 			uh = (struct udphdr *)(iph + 1);
1583 	}
1584 	if (uh) {
1585 		if (uh->check)
1586 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1587 		else
1588 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1589 	}
1590 }
1591 #endif
1592 
1593 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1594 					   int payload_off, int tcp_ts,
1595 					   struct sk_buff *skb)
1596 {
1597 #ifdef CONFIG_INET
1598 	struct tcphdr *th;
1599 	int len, nw_off;
1600 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1601 	u32 hdr_info = tpa_info->hdr_info;
1602 	bool loopback = false;
1603 
1604 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1605 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1606 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1607 
1608 	/* If the packet is an internal loopback packet, the offsets will
1609 	 * have an extra 4 bytes.
1610 	 */
1611 	if (inner_mac_off == 4) {
1612 		loopback = true;
1613 	} else if (inner_mac_off > 4) {
1614 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1615 					    ETH_HLEN - 2));
1616 
1617 		/* We only support inner iPv4/ipv6.  If we don't see the
1618 		 * correct protocol ID, it must be a loopback packet where
1619 		 * the offsets are off by 4.
1620 		 */
1621 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1622 			loopback = true;
1623 	}
1624 	if (loopback) {
1625 		/* internal loopback packet, subtract all offsets by 4 */
1626 		inner_ip_off -= 4;
1627 		inner_mac_off -= 4;
1628 		outer_ip_off -= 4;
1629 	}
1630 
1631 	nw_off = inner_ip_off - ETH_HLEN;
1632 	skb_set_network_header(skb, nw_off);
1633 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1634 		struct ipv6hdr *iph = ipv6_hdr(skb);
1635 
1636 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1637 		len = skb->len - skb_transport_offset(skb);
1638 		th = tcp_hdr(skb);
1639 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1640 	} else {
1641 		struct iphdr *iph = ip_hdr(skb);
1642 
1643 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1644 		len = skb->len - skb_transport_offset(skb);
1645 		th = tcp_hdr(skb);
1646 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1647 	}
1648 
1649 	if (inner_mac_off) { /* tunnel */
1650 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1651 					    ETH_HLEN - 2));
1652 
1653 		bnxt_gro_tunnel(skb, proto);
1654 	}
1655 #endif
1656 	return skb;
1657 }
1658 
1659 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1660 					   int payload_off, int tcp_ts,
1661 					   struct sk_buff *skb)
1662 {
1663 #ifdef CONFIG_INET
1664 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1665 	u32 hdr_info = tpa_info->hdr_info;
1666 	int iphdr_len, nw_off;
1667 
1668 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1669 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1670 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1671 
1672 	nw_off = inner_ip_off - ETH_HLEN;
1673 	skb_set_network_header(skb, nw_off);
1674 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1675 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1676 	skb_set_transport_header(skb, nw_off + iphdr_len);
1677 
1678 	if (inner_mac_off) { /* tunnel */
1679 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1680 					    ETH_HLEN - 2));
1681 
1682 		bnxt_gro_tunnel(skb, proto);
1683 	}
1684 #endif
1685 	return skb;
1686 }
1687 
1688 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1689 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1690 
1691 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1692 					   int payload_off, int tcp_ts,
1693 					   struct sk_buff *skb)
1694 {
1695 #ifdef CONFIG_INET
1696 	struct tcphdr *th;
1697 	int len, nw_off, tcp_opt_len = 0;
1698 
1699 	if (tcp_ts)
1700 		tcp_opt_len = 12;
1701 
1702 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1703 		struct iphdr *iph;
1704 
1705 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1706 			 ETH_HLEN;
1707 		skb_set_network_header(skb, nw_off);
1708 		iph = ip_hdr(skb);
1709 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1710 		len = skb->len - skb_transport_offset(skb);
1711 		th = tcp_hdr(skb);
1712 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1713 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1714 		struct ipv6hdr *iph;
1715 
1716 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1717 			 ETH_HLEN;
1718 		skb_set_network_header(skb, nw_off);
1719 		iph = ipv6_hdr(skb);
1720 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1721 		len = skb->len - skb_transport_offset(skb);
1722 		th = tcp_hdr(skb);
1723 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1724 	} else {
1725 		dev_kfree_skb_any(skb);
1726 		return NULL;
1727 	}
1728 
1729 	if (nw_off) /* tunnel */
1730 		bnxt_gro_tunnel(skb, skb->protocol);
1731 #endif
1732 	return skb;
1733 }
1734 
1735 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1736 					   struct bnxt_tpa_info *tpa_info,
1737 					   struct rx_tpa_end_cmp *tpa_end,
1738 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1739 					   struct sk_buff *skb)
1740 {
1741 #ifdef CONFIG_INET
1742 	int payload_off;
1743 	u16 segs;
1744 
1745 	segs = TPA_END_TPA_SEGS(tpa_end);
1746 	if (segs == 1)
1747 		return skb;
1748 
1749 	NAPI_GRO_CB(skb)->count = segs;
1750 	skb_shinfo(skb)->gso_size =
1751 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1752 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1753 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1754 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1755 	else
1756 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1757 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1758 	if (likely(skb))
1759 		tcp_gro_complete(skb);
1760 #endif
1761 	return skb;
1762 }
1763 
1764 /* Given the cfa_code of a received packet determine which
1765  * netdev (vf-rep or PF) the packet is destined to.
1766  */
1767 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1768 {
1769 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1770 
1771 	/* if vf-rep dev is NULL, the must belongs to the PF */
1772 	return dev ? dev : bp->dev;
1773 }
1774 
1775 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1776 					   struct bnxt_cp_ring_info *cpr,
1777 					   u32 *raw_cons,
1778 					   struct rx_tpa_end_cmp *tpa_end,
1779 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1780 					   u8 *event)
1781 {
1782 	struct bnxt_napi *bnapi = cpr->bnapi;
1783 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1784 	struct net_device *dev = bp->dev;
1785 	u8 *data_ptr, agg_bufs;
1786 	unsigned int len;
1787 	struct bnxt_tpa_info *tpa_info;
1788 	dma_addr_t mapping;
1789 	struct sk_buff *skb;
1790 	u16 idx = 0, agg_id;
1791 	void *data;
1792 	bool gro;
1793 
1794 	if (unlikely(bnapi->in_reset)) {
1795 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1796 
1797 		if (rc < 0)
1798 			return ERR_PTR(-EBUSY);
1799 		return NULL;
1800 	}
1801 
1802 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1803 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1804 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1805 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1806 		tpa_info = &rxr->rx_tpa[agg_id];
1807 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1808 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1809 				    agg_bufs, tpa_info->agg_count);
1810 			agg_bufs = tpa_info->agg_count;
1811 		}
1812 		tpa_info->agg_count = 0;
1813 		*event |= BNXT_AGG_EVENT;
1814 		bnxt_free_agg_idx(rxr, agg_id);
1815 		idx = agg_id;
1816 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1817 	} else {
1818 		agg_id = TPA_END_AGG_ID(tpa_end);
1819 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1820 		tpa_info = &rxr->rx_tpa[agg_id];
1821 		idx = RING_CMP(*raw_cons);
1822 		if (agg_bufs) {
1823 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1824 				return ERR_PTR(-EBUSY);
1825 
1826 			*event |= BNXT_AGG_EVENT;
1827 			idx = NEXT_CMP(idx);
1828 		}
1829 		gro = !!TPA_END_GRO(tpa_end);
1830 	}
1831 	data = tpa_info->data;
1832 	data_ptr = tpa_info->data_ptr;
1833 	prefetch(data_ptr);
1834 	len = tpa_info->len;
1835 	mapping = tpa_info->mapping;
1836 
1837 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1838 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1839 		if (agg_bufs > MAX_SKB_FRAGS)
1840 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1841 				    agg_bufs, (int)MAX_SKB_FRAGS);
1842 		return NULL;
1843 	}
1844 
1845 	if (len <= bp->rx_copy_thresh) {
1846 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1847 		if (!skb) {
1848 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1849 			cpr->sw_stats->rx.rx_oom_discards += 1;
1850 			return NULL;
1851 		}
1852 	} else {
1853 		u8 *new_data;
1854 		dma_addr_t new_mapping;
1855 
1856 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1857 						GFP_ATOMIC);
1858 		if (!new_data) {
1859 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1860 			cpr->sw_stats->rx.rx_oom_discards += 1;
1861 			return NULL;
1862 		}
1863 
1864 		tpa_info->data = new_data;
1865 		tpa_info->data_ptr = new_data + bp->rx_offset;
1866 		tpa_info->mapping = new_mapping;
1867 
1868 		skb = napi_build_skb(data, bp->rx_buf_size);
1869 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1870 					bp->rx_buf_use_size, bp->rx_dir);
1871 
1872 		if (!skb) {
1873 			page_pool_free_va(rxr->head_pool, data, true);
1874 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1875 			cpr->sw_stats->rx.rx_oom_discards += 1;
1876 			return NULL;
1877 		}
1878 		skb_mark_for_recycle(skb);
1879 		skb_reserve(skb, bp->rx_offset);
1880 		skb_put(skb, len);
1881 	}
1882 
1883 	if (agg_bufs) {
1884 		skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true);
1885 		if (!skb) {
1886 			/* Page reuse already handled by bnxt_rx_pages(). */
1887 			cpr->sw_stats->rx.rx_oom_discards += 1;
1888 			return NULL;
1889 		}
1890 	}
1891 
1892 	if (tpa_info->cfa_code_valid)
1893 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1894 	skb->protocol = eth_type_trans(skb, dev);
1895 
1896 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1897 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1898 
1899 	if (tpa_info->vlan_valid &&
1900 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1901 		__be16 vlan_proto = htons(tpa_info->metadata >>
1902 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1903 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1904 
1905 		if (eth_type_vlan(vlan_proto)) {
1906 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1907 		} else {
1908 			dev_kfree_skb(skb);
1909 			return NULL;
1910 		}
1911 	}
1912 
1913 	skb_checksum_none_assert(skb);
1914 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1915 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1916 		skb->csum_level =
1917 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1918 	}
1919 
1920 	if (gro)
1921 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1922 
1923 	return skb;
1924 }
1925 
1926 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1927 			 struct rx_agg_cmp *rx_agg)
1928 {
1929 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1930 	struct bnxt_tpa_info *tpa_info;
1931 
1932 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1933 	tpa_info = &rxr->rx_tpa[agg_id];
1934 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1935 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1936 }
1937 
1938 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1939 			     struct sk_buff *skb)
1940 {
1941 	skb_mark_for_recycle(skb);
1942 
1943 	if (skb->dev != bp->dev) {
1944 		/* this packet belongs to a vf-rep */
1945 		bnxt_vf_rep_rx(bp, skb);
1946 		return;
1947 	}
1948 	skb_record_rx_queue(skb, bnapi->index);
1949 	napi_gro_receive(&bnapi->napi, skb);
1950 }
1951 
1952 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
1953 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
1954 {
1955 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1956 
1957 	if (BNXT_PTP_RX_TS_VALID(flags))
1958 		goto ts_valid;
1959 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
1960 		return false;
1961 
1962 ts_valid:
1963 	*cmpl_ts = ts;
1964 	return true;
1965 }
1966 
1967 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
1968 				    struct rx_cmp *rxcmp,
1969 				    struct rx_cmp_ext *rxcmp1)
1970 {
1971 	__be16 vlan_proto;
1972 	u16 vtag;
1973 
1974 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1975 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
1976 		u32 meta_data;
1977 
1978 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
1979 			return skb;
1980 
1981 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1982 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1983 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
1984 		if (eth_type_vlan(vlan_proto))
1985 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1986 		else
1987 			goto vlan_err;
1988 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
1989 		if (RX_CMP_VLAN_VALID(rxcmp)) {
1990 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
1991 
1992 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
1993 				vlan_proto = htons(ETH_P_8021Q);
1994 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
1995 				vlan_proto = htons(ETH_P_8021AD);
1996 			else
1997 				goto vlan_err;
1998 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
1999 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2000 		}
2001 	}
2002 	return skb;
2003 vlan_err:
2004 	dev_kfree_skb(skb);
2005 	return NULL;
2006 }
2007 
2008 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
2009 					   struct rx_cmp *rxcmp)
2010 {
2011 	u8 ext_op;
2012 
2013 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
2014 	switch (ext_op) {
2015 	case EXT_OP_INNER_4:
2016 	case EXT_OP_OUTER_4:
2017 	case EXT_OP_INNFL_3:
2018 	case EXT_OP_OUTFL_3:
2019 		return PKT_HASH_TYPE_L4;
2020 	default:
2021 		return PKT_HASH_TYPE_L3;
2022 	}
2023 }
2024 
2025 /* returns the following:
2026  * 1       - 1 packet successfully received
2027  * 0       - successful TPA_START, packet not completed yet
2028  * -EBUSY  - completion ring does not have all the agg buffers yet
2029  * -ENOMEM - packet aborted due to out of memory
2030  * -EIO    - packet aborted due to hw error indicated in BD
2031  */
2032 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2033 		       u32 *raw_cons, u8 *event)
2034 {
2035 	struct bnxt_napi *bnapi = cpr->bnapi;
2036 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2037 	struct net_device *dev = bp->dev;
2038 	struct rx_cmp *rxcmp;
2039 	struct rx_cmp_ext *rxcmp1;
2040 	u32 tmp_raw_cons = *raw_cons;
2041 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2042 	struct bnxt_sw_rx_bd *rx_buf;
2043 	unsigned int len;
2044 	u8 *data_ptr, agg_bufs, cmp_type;
2045 	bool xdp_active = false;
2046 	dma_addr_t dma_addr;
2047 	struct sk_buff *skb;
2048 	struct xdp_buff xdp;
2049 	u32 flags, misc;
2050 	u32 cmpl_ts;
2051 	void *data;
2052 	int rc = 0;
2053 
2054 	rxcmp = (struct rx_cmp *)
2055 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2056 
2057 	cmp_type = RX_CMP_TYPE(rxcmp);
2058 
2059 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2060 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2061 		goto next_rx_no_prod_no_len;
2062 	}
2063 
2064 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2065 	cp_cons = RING_CMP(tmp_raw_cons);
2066 	rxcmp1 = (struct rx_cmp_ext *)
2067 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2068 
2069 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2070 		return -EBUSY;
2071 
2072 	/* The valid test of the entry must be done first before
2073 	 * reading any further.
2074 	 */
2075 	dma_rmb();
2076 	prod = rxr->rx_prod;
2077 
2078 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2079 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2080 		bnxt_tpa_start(bp, rxr, cmp_type,
2081 			       (struct rx_tpa_start_cmp *)rxcmp,
2082 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2083 
2084 		*event |= BNXT_RX_EVENT;
2085 		goto next_rx_no_prod_no_len;
2086 
2087 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2088 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2089 				   (struct rx_tpa_end_cmp *)rxcmp,
2090 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2091 
2092 		if (IS_ERR(skb))
2093 			return -EBUSY;
2094 
2095 		rc = -ENOMEM;
2096 		if (likely(skb)) {
2097 			bnxt_deliver_skb(bp, bnapi, skb);
2098 			rc = 1;
2099 		}
2100 		*event |= BNXT_RX_EVENT;
2101 		goto next_rx_no_prod_no_len;
2102 	}
2103 
2104 	cons = rxcmp->rx_cmp_opaque;
2105 	if (unlikely(cons != rxr->rx_next_cons)) {
2106 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2107 
2108 		/* 0xffff is forced error, don't print it */
2109 		if (rxr->rx_next_cons != 0xffff)
2110 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2111 				    cons, rxr->rx_next_cons);
2112 		bnxt_sched_reset_rxr(bp, rxr);
2113 		if (rc1)
2114 			return rc1;
2115 		goto next_rx_no_prod_no_len;
2116 	}
2117 	rx_buf = &rxr->rx_buf_ring[cons];
2118 	data = rx_buf->data;
2119 	data_ptr = rx_buf->data_ptr;
2120 	prefetch(data_ptr);
2121 
2122 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2123 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2124 
2125 	if (agg_bufs) {
2126 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2127 			return -EBUSY;
2128 
2129 		cp_cons = NEXT_CMP(cp_cons);
2130 		*event |= BNXT_AGG_EVENT;
2131 	}
2132 	*event |= BNXT_RX_EVENT;
2133 
2134 	rx_buf->data = NULL;
2135 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2136 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2137 
2138 		bnxt_reuse_rx_data(rxr, cons, data);
2139 		if (agg_bufs)
2140 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2141 					       false);
2142 
2143 		rc = -EIO;
2144 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2145 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2146 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2147 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2148 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2149 						 rx_err);
2150 				bnxt_sched_reset_rxr(bp, rxr);
2151 			}
2152 		}
2153 		goto next_rx_no_len;
2154 	}
2155 
2156 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2157 	len = flags >> RX_CMP_LEN_SHIFT;
2158 	dma_addr = rx_buf->mapping;
2159 
2160 	if (bnxt_xdp_attached(bp, rxr)) {
2161 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2162 		if (agg_bufs) {
2163 			u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
2164 							     cp_cons, agg_bufs,
2165 							     false);
2166 			if (!frag_len)
2167 				goto oom_next_rx;
2168 		}
2169 		xdp_active = true;
2170 	}
2171 
2172 	if (xdp_active) {
2173 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2174 			rc = 1;
2175 			goto next_rx;
2176 		}
2177 	}
2178 
2179 	if (len <= bp->rx_copy_thresh) {
2180 		if (!xdp_active)
2181 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2182 		else
2183 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2184 		bnxt_reuse_rx_data(rxr, cons, data);
2185 		if (!skb) {
2186 			if (agg_bufs) {
2187 				if (!xdp_active)
2188 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2189 							       agg_bufs, false);
2190 				else
2191 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2192 			}
2193 			goto oom_next_rx;
2194 		}
2195 	} else {
2196 		u32 payload;
2197 
2198 		if (rx_buf->data_ptr == data_ptr)
2199 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2200 		else
2201 			payload = 0;
2202 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2203 				      payload | len);
2204 		if (!skb)
2205 			goto oom_next_rx;
2206 	}
2207 
2208 	if (agg_bufs) {
2209 		if (!xdp_active) {
2210 			skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
2211 			if (!skb)
2212 				goto oom_next_rx;
2213 		} else {
2214 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1);
2215 			if (!skb) {
2216 				/* we should be able to free the old skb here */
2217 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2218 				goto oom_next_rx;
2219 			}
2220 		}
2221 	}
2222 
2223 	if (RX_CMP_HASH_VALID(rxcmp)) {
2224 		enum pkt_hash_types type;
2225 
2226 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2227 			type = bnxt_rss_ext_op(bp, rxcmp);
2228 		} else {
2229 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2230 
2231 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2232 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2233 				type = PKT_HASH_TYPE_L4;
2234 			else
2235 				type = PKT_HASH_TYPE_L3;
2236 		}
2237 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2238 	}
2239 
2240 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2241 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2242 	skb->protocol = eth_type_trans(skb, dev);
2243 
2244 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2245 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2246 		if (!skb)
2247 			goto next_rx;
2248 	}
2249 
2250 	skb_checksum_none_assert(skb);
2251 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2252 		if (dev->features & NETIF_F_RXCSUM) {
2253 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2254 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2255 		}
2256 	} else {
2257 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2258 			if (dev->features & NETIF_F_RXCSUM)
2259 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2260 		}
2261 	}
2262 
2263 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2264 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2265 			u64 ns, ts;
2266 
2267 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2268 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2269 
2270 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2271 				memset(skb_hwtstamps(skb), 0,
2272 				       sizeof(*skb_hwtstamps(skb)));
2273 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2274 			}
2275 		}
2276 	}
2277 	bnxt_deliver_skb(bp, bnapi, skb);
2278 	rc = 1;
2279 
2280 next_rx:
2281 	cpr->rx_packets += 1;
2282 	cpr->rx_bytes += len;
2283 
2284 next_rx_no_len:
2285 	rxr->rx_prod = NEXT_RX(prod);
2286 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2287 
2288 next_rx_no_prod_no_len:
2289 	*raw_cons = tmp_raw_cons;
2290 
2291 	return rc;
2292 
2293 oom_next_rx:
2294 	cpr->sw_stats->rx.rx_oom_discards += 1;
2295 	rc = -ENOMEM;
2296 	goto next_rx;
2297 }
2298 
2299 /* In netpoll mode, if we are using a combined completion ring, we need to
2300  * discard the rx packets and recycle the buffers.
2301  */
2302 static int bnxt_force_rx_discard(struct bnxt *bp,
2303 				 struct bnxt_cp_ring_info *cpr,
2304 				 u32 *raw_cons, u8 *event)
2305 {
2306 	u32 tmp_raw_cons = *raw_cons;
2307 	struct rx_cmp_ext *rxcmp1;
2308 	struct rx_cmp *rxcmp;
2309 	u16 cp_cons;
2310 	u8 cmp_type;
2311 	int rc;
2312 
2313 	cp_cons = RING_CMP(tmp_raw_cons);
2314 	rxcmp = (struct rx_cmp *)
2315 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2316 
2317 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2318 	cp_cons = RING_CMP(tmp_raw_cons);
2319 	rxcmp1 = (struct rx_cmp_ext *)
2320 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2321 
2322 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2323 		return -EBUSY;
2324 
2325 	/* The valid test of the entry must be done first before
2326 	 * reading any further.
2327 	 */
2328 	dma_rmb();
2329 	cmp_type = RX_CMP_TYPE(rxcmp);
2330 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2331 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2332 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2333 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2334 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2335 		struct rx_tpa_end_cmp_ext *tpa_end1;
2336 
2337 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2338 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2339 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2340 	}
2341 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2342 	if (rc && rc != -EBUSY)
2343 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2344 	return rc;
2345 }
2346 
2347 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2348 {
2349 	struct bnxt_fw_health *fw_health = bp->fw_health;
2350 	u32 reg = fw_health->regs[reg_idx];
2351 	u32 reg_type, reg_off, val = 0;
2352 
2353 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2354 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2355 	switch (reg_type) {
2356 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2357 		pci_read_config_dword(bp->pdev, reg_off, &val);
2358 		break;
2359 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2360 		reg_off = fw_health->mapped_regs[reg_idx];
2361 		fallthrough;
2362 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2363 		val = readl(bp->bar0 + reg_off);
2364 		break;
2365 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2366 		val = readl(bp->bar1 + reg_off);
2367 		break;
2368 	}
2369 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2370 		val &= fw_health->fw_reset_inprog_reg_mask;
2371 	return val;
2372 }
2373 
2374 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2375 {
2376 	int i;
2377 
2378 	for (i = 0; i < bp->rx_nr_rings; i++) {
2379 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2380 		struct bnxt_ring_grp_info *grp_info;
2381 
2382 		grp_info = &bp->grp_info[grp_idx];
2383 		if (grp_info->agg_fw_ring_id == ring_id)
2384 			return grp_idx;
2385 	}
2386 	return INVALID_HW_RING_ID;
2387 }
2388 
2389 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2390 {
2391 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2392 
2393 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2394 		return link_info->force_link_speed2;
2395 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2396 		return link_info->force_pam4_link_speed;
2397 	return link_info->force_link_speed;
2398 }
2399 
2400 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2401 {
2402 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2403 
2404 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2405 		link_info->req_link_speed = link_info->force_link_speed2;
2406 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2407 		switch (link_info->req_link_speed) {
2408 		case BNXT_LINK_SPEED_50GB_PAM4:
2409 		case BNXT_LINK_SPEED_100GB_PAM4:
2410 		case BNXT_LINK_SPEED_200GB_PAM4:
2411 		case BNXT_LINK_SPEED_400GB_PAM4:
2412 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2413 			break;
2414 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2415 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2416 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2417 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2418 			break;
2419 		default:
2420 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2421 		}
2422 		return;
2423 	}
2424 	link_info->req_link_speed = link_info->force_link_speed;
2425 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2426 	if (link_info->force_pam4_link_speed) {
2427 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2428 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2429 	}
2430 }
2431 
2432 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2433 {
2434 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2435 
2436 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2437 		link_info->advertising = link_info->auto_link_speeds2;
2438 		return;
2439 	}
2440 	link_info->advertising = link_info->auto_link_speeds;
2441 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2442 }
2443 
2444 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2445 {
2446 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2447 
2448 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2449 		if (link_info->req_link_speed != link_info->force_link_speed2)
2450 			return true;
2451 		return false;
2452 	}
2453 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2454 	    link_info->req_link_speed != link_info->force_link_speed)
2455 		return true;
2456 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2457 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2458 		return true;
2459 	return false;
2460 }
2461 
2462 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2463 {
2464 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2465 
2466 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2467 		if (link_info->advertising != link_info->auto_link_speeds2)
2468 			return true;
2469 		return false;
2470 	}
2471 	if (link_info->advertising != link_info->auto_link_speeds ||
2472 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2473 		return true;
2474 	return false;
2475 }
2476 
2477 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2478 {
2479 	u32 flags = bp->ctx->ctx_arr[type].flags;
2480 
2481 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2482 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2483 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2484 }
2485 
2486 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2487 {
2488 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2489 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2490 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2491 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2492 	struct bnxt_bs_trace_info *bs_trace;
2493 	int last_pg;
2494 
2495 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2496 		return;
2497 
2498 	mem_size = ctxm->max_entries * ctxm->entry_size;
2499 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2500 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2501 
2502 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2503 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2504 
2505 	rmem = &ctx_pg[0].ring_mem;
2506 	bs_trace = &bp->bs_trace[trace_type];
2507 	bs_trace->ctx_type = ctxm->type;
2508 	bs_trace->trace_type = trace_type;
2509 	if (pages > MAX_CTX_PAGES) {
2510 		int last_pg_dir = rmem->nr_pages - 1;
2511 
2512 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2513 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2514 	} else {
2515 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2516 	}
2517 	bs_trace->magic_byte += magic_byte_offset;
2518 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2519 }
2520 
2521 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2522 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2523 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2524 
2525 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2526 	(((data2) &							\
2527 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2528 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2529 
2530 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2531 	((data2) &							\
2532 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2533 
2534 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2535 	(((data2) &							\
2536 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2537 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2538 
2539 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2540 	((data1) &							\
2541 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2542 
2543 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2544 	(((data1) &							\
2545 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2546 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2547 
2548 /* Return true if the workqueue has to be scheduled */
2549 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2550 {
2551 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2552 
2553 	switch (err_type) {
2554 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2555 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2556 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2557 		break;
2558 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2559 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2560 		break;
2561 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2562 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2563 		break;
2564 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2565 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2566 		char *threshold_type;
2567 		bool notify = false;
2568 		char *dir_str;
2569 
2570 		switch (type) {
2571 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2572 			threshold_type = "warning";
2573 			break;
2574 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2575 			threshold_type = "critical";
2576 			break;
2577 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2578 			threshold_type = "fatal";
2579 			break;
2580 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2581 			threshold_type = "shutdown";
2582 			break;
2583 		default:
2584 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2585 			return false;
2586 		}
2587 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2588 			dir_str = "above";
2589 			notify = true;
2590 		} else {
2591 			dir_str = "below";
2592 		}
2593 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2594 			    dir_str, threshold_type);
2595 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2596 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2597 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2598 		if (notify) {
2599 			bp->thermal_threshold_type = type;
2600 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2601 			return true;
2602 		}
2603 		return false;
2604 	}
2605 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2606 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2607 		break;
2608 	default:
2609 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2610 			   err_type);
2611 		break;
2612 	}
2613 	return false;
2614 }
2615 
2616 #define BNXT_GET_EVENT_PORT(data)	\
2617 	((data) &			\
2618 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2619 
2620 #define BNXT_EVENT_RING_TYPE(data2)	\
2621 	((data2) &			\
2622 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2623 
2624 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2625 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2626 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2627 
2628 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2629 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2630 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2631 
2632 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2633 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2634 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2635 
2636 #define BNXT_PHC_BITS	48
2637 
2638 static int bnxt_async_event_process(struct bnxt *bp,
2639 				    struct hwrm_async_event_cmpl *cmpl)
2640 {
2641 	u16 event_id = le16_to_cpu(cmpl->event_id);
2642 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2643 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2644 
2645 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2646 		   event_id, data1, data2);
2647 
2648 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2649 	switch (event_id) {
2650 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2651 		struct bnxt_link_info *link_info = &bp->link_info;
2652 
2653 		if (BNXT_VF(bp))
2654 			goto async_event_process_exit;
2655 
2656 		/* print unsupported speed warning in forced speed mode only */
2657 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2658 		    (data1 & 0x20000)) {
2659 			u16 fw_speed = bnxt_get_force_speed(link_info);
2660 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2661 
2662 			if (speed != SPEED_UNKNOWN)
2663 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2664 					    speed);
2665 		}
2666 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2667 	}
2668 		fallthrough;
2669 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2670 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2671 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2672 		fallthrough;
2673 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2674 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2675 		break;
2676 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2677 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2678 		break;
2679 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2680 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2681 
2682 		if (BNXT_VF(bp))
2683 			break;
2684 
2685 		if (bp->pf.port_id != port_id)
2686 			break;
2687 
2688 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2689 		break;
2690 	}
2691 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2692 		if (BNXT_PF(bp))
2693 			goto async_event_process_exit;
2694 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2695 		break;
2696 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2697 		char *type_str = "Solicited";
2698 
2699 		if (!bp->fw_health)
2700 			goto async_event_process_exit;
2701 
2702 		bp->fw_reset_timestamp = jiffies;
2703 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2704 		if (!bp->fw_reset_min_dsecs)
2705 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2706 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2707 		if (!bp->fw_reset_max_dsecs)
2708 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2709 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2710 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2711 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2712 			type_str = "Fatal";
2713 			bp->fw_health->fatalities++;
2714 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2715 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2716 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2717 			type_str = "Non-fatal";
2718 			bp->fw_health->survivals++;
2719 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2720 		}
2721 		netif_warn(bp, hw, bp->dev,
2722 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2723 			   type_str, data1, data2,
2724 			   bp->fw_reset_min_dsecs * 100,
2725 			   bp->fw_reset_max_dsecs * 100);
2726 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2727 		break;
2728 	}
2729 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2730 		struct bnxt_fw_health *fw_health = bp->fw_health;
2731 		char *status_desc = "healthy";
2732 		u32 status;
2733 
2734 		if (!fw_health)
2735 			goto async_event_process_exit;
2736 
2737 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2738 			fw_health->enabled = false;
2739 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2740 			break;
2741 		}
2742 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2743 		fw_health->tmr_multiplier =
2744 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2745 				     bp->current_interval * 10);
2746 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2747 		if (!fw_health->enabled)
2748 			fw_health->last_fw_heartbeat =
2749 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2750 		fw_health->last_fw_reset_cnt =
2751 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2752 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2753 		if (status != BNXT_FW_STATUS_HEALTHY)
2754 			status_desc = "unhealthy";
2755 		netif_info(bp, drv, bp->dev,
2756 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2757 			   fw_health->primary ? "primary" : "backup", status,
2758 			   status_desc, fw_health->last_fw_reset_cnt);
2759 		if (!fw_health->enabled) {
2760 			/* Make sure tmr_counter is set and visible to
2761 			 * bnxt_health_check() before setting enabled to true.
2762 			 */
2763 			smp_wmb();
2764 			fw_health->enabled = true;
2765 		}
2766 		goto async_event_process_exit;
2767 	}
2768 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2769 		netif_notice(bp, hw, bp->dev,
2770 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2771 			     data1, data2);
2772 		goto async_event_process_exit;
2773 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2774 		struct bnxt_rx_ring_info *rxr;
2775 		u16 grp_idx;
2776 
2777 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2778 			goto async_event_process_exit;
2779 
2780 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2781 			    BNXT_EVENT_RING_TYPE(data2), data1);
2782 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2783 			goto async_event_process_exit;
2784 
2785 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2786 		if (grp_idx == INVALID_HW_RING_ID) {
2787 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2788 				    data1);
2789 			goto async_event_process_exit;
2790 		}
2791 		rxr = bp->bnapi[grp_idx]->rx_ring;
2792 		bnxt_sched_reset_rxr(bp, rxr);
2793 		goto async_event_process_exit;
2794 	}
2795 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2796 		struct bnxt_fw_health *fw_health = bp->fw_health;
2797 
2798 		netif_notice(bp, hw, bp->dev,
2799 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2800 			     data1, data2);
2801 		if (fw_health) {
2802 			fw_health->echo_req_data1 = data1;
2803 			fw_health->echo_req_data2 = data2;
2804 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2805 			break;
2806 		}
2807 		goto async_event_process_exit;
2808 	}
2809 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2810 		bnxt_ptp_pps_event(bp, data1, data2);
2811 		goto async_event_process_exit;
2812 	}
2813 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2814 		if (bnxt_event_error_report(bp, data1, data2))
2815 			break;
2816 		goto async_event_process_exit;
2817 	}
2818 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2819 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2820 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2821 			if (BNXT_PTP_USE_RTC(bp)) {
2822 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2823 				unsigned long flags;
2824 				u64 ns;
2825 
2826 				if (!ptp)
2827 					goto async_event_process_exit;
2828 
2829 				bnxt_ptp_update_current_time(bp);
2830 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2831 				       BNXT_PHC_BITS) | ptp->current_time);
2832 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2833 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2834 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2835 			}
2836 			break;
2837 		}
2838 		goto async_event_process_exit;
2839 	}
2840 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2841 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2842 
2843 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2844 		goto async_event_process_exit;
2845 	}
2846 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2847 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2848 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2849 
2850 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2851 		goto async_event_process_exit;
2852 	}
2853 	default:
2854 		goto async_event_process_exit;
2855 	}
2856 	__bnxt_queue_sp_work(bp);
2857 async_event_process_exit:
2858 	return 0;
2859 }
2860 
2861 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2862 {
2863 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2864 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2865 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2866 				(struct hwrm_fwd_req_cmpl *)txcmp;
2867 
2868 	switch (cmpl_type) {
2869 	case CMPL_BASE_TYPE_HWRM_DONE:
2870 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2871 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2872 		break;
2873 
2874 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2875 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2876 
2877 		if ((vf_id < bp->pf.first_vf_id) ||
2878 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2879 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2880 				   vf_id);
2881 			return -EINVAL;
2882 		}
2883 
2884 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2885 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2886 		break;
2887 
2888 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2889 		bnxt_async_event_process(bp,
2890 					 (struct hwrm_async_event_cmpl *)txcmp);
2891 		break;
2892 
2893 	default:
2894 		break;
2895 	}
2896 
2897 	return 0;
2898 }
2899 
2900 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2901 {
2902 	struct bnxt_napi *bnapi = dev_instance;
2903 	struct bnxt *bp = bnapi->bp;
2904 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2905 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2906 
2907 	cpr->event_ctr++;
2908 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2909 	napi_schedule(&bnapi->napi);
2910 	return IRQ_HANDLED;
2911 }
2912 
2913 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2914 {
2915 	u32 raw_cons = cpr->cp_raw_cons;
2916 	u16 cons = RING_CMP(raw_cons);
2917 	struct tx_cmp *txcmp;
2918 
2919 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2920 
2921 	return TX_CMP_VALID(txcmp, raw_cons);
2922 }
2923 
2924 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2925 			    int budget)
2926 {
2927 	struct bnxt_napi *bnapi = cpr->bnapi;
2928 	u32 raw_cons = cpr->cp_raw_cons;
2929 	u32 cons;
2930 	int rx_pkts = 0;
2931 	u8 event = 0;
2932 	struct tx_cmp *txcmp;
2933 
2934 	cpr->has_more_work = 0;
2935 	cpr->had_work_done = 1;
2936 	while (1) {
2937 		u8 cmp_type;
2938 		int rc;
2939 
2940 		cons = RING_CMP(raw_cons);
2941 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2942 
2943 		if (!TX_CMP_VALID(txcmp, raw_cons))
2944 			break;
2945 
2946 		/* The valid test of the entry must be done first before
2947 		 * reading any further.
2948 		 */
2949 		dma_rmb();
2950 		cmp_type = TX_CMP_TYPE(txcmp);
2951 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
2952 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
2953 			u32 opaque = txcmp->tx_cmp_opaque;
2954 			struct bnxt_tx_ring_info *txr;
2955 			u16 tx_freed;
2956 
2957 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
2958 			event |= BNXT_TX_CMP_EVENT;
2959 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
2960 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
2961 			else
2962 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
2963 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
2964 				   bp->tx_ring_mask;
2965 			/* return full budget so NAPI will complete. */
2966 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
2967 				rx_pkts = budget;
2968 				raw_cons = NEXT_RAW_CMP(raw_cons);
2969 				if (budget)
2970 					cpr->has_more_work = 1;
2971 				break;
2972 			}
2973 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
2974 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
2975 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
2976 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2977 			if (likely(budget))
2978 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2979 			else
2980 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2981 							   &event);
2982 			if (likely(rc >= 0))
2983 				rx_pkts += rc;
2984 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2985 			 * the NAPI budget.  Otherwise, we may potentially loop
2986 			 * here forever if we consistently cannot allocate
2987 			 * buffers.
2988 			 */
2989 			else if (rc == -ENOMEM && budget)
2990 				rx_pkts++;
2991 			else if (rc == -EBUSY)	/* partial completion */
2992 				break;
2993 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
2994 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
2995 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
2996 			bnxt_hwrm_handler(bp, txcmp);
2997 		}
2998 		raw_cons = NEXT_RAW_CMP(raw_cons);
2999 
3000 		if (rx_pkts && rx_pkts == budget) {
3001 			cpr->has_more_work = 1;
3002 			break;
3003 		}
3004 	}
3005 
3006 	if (event & BNXT_REDIRECT_EVENT) {
3007 		xdp_do_flush();
3008 		event &= ~BNXT_REDIRECT_EVENT;
3009 	}
3010 
3011 	if (event & BNXT_TX_EVENT) {
3012 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3013 		u16 prod = txr->tx_prod;
3014 
3015 		/* Sync BD data before updating doorbell */
3016 		wmb();
3017 
3018 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3019 		event &= ~BNXT_TX_EVENT;
3020 	}
3021 
3022 	cpr->cp_raw_cons = raw_cons;
3023 	bnapi->events |= event;
3024 	return rx_pkts;
3025 }
3026 
3027 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3028 				  int budget)
3029 {
3030 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3031 		bnapi->tx_int(bp, bnapi, budget);
3032 
3033 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3034 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3035 
3036 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3037 		bnapi->events &= ~BNXT_RX_EVENT;
3038 	}
3039 	if (bnapi->events & BNXT_AGG_EVENT) {
3040 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3041 
3042 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3043 		bnapi->events &= ~BNXT_AGG_EVENT;
3044 	}
3045 }
3046 
3047 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3048 			  int budget)
3049 {
3050 	struct bnxt_napi *bnapi = cpr->bnapi;
3051 	int rx_pkts;
3052 
3053 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3054 
3055 	/* ACK completion ring before freeing tx ring and producing new
3056 	 * buffers in rx/agg rings to prevent overflowing the completion
3057 	 * ring.
3058 	 */
3059 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3060 
3061 	__bnxt_poll_work_done(bp, bnapi, budget);
3062 	return rx_pkts;
3063 }
3064 
3065 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3066 {
3067 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3068 	struct bnxt *bp = bnapi->bp;
3069 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3070 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3071 	struct tx_cmp *txcmp;
3072 	struct rx_cmp_ext *rxcmp1;
3073 	u32 cp_cons, tmp_raw_cons;
3074 	u32 raw_cons = cpr->cp_raw_cons;
3075 	bool flush_xdp = false;
3076 	u32 rx_pkts = 0;
3077 	u8 event = 0;
3078 
3079 	while (1) {
3080 		int rc;
3081 
3082 		cp_cons = RING_CMP(raw_cons);
3083 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3084 
3085 		if (!TX_CMP_VALID(txcmp, raw_cons))
3086 			break;
3087 
3088 		/* The valid test of the entry must be done first before
3089 		 * reading any further.
3090 		 */
3091 		dma_rmb();
3092 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3093 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3094 			cp_cons = RING_CMP(tmp_raw_cons);
3095 			rxcmp1 = (struct rx_cmp_ext *)
3096 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3097 
3098 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3099 				break;
3100 
3101 			/* force an error to recycle the buffer */
3102 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3103 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3104 
3105 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3106 			if (likely(rc == -EIO) && budget)
3107 				rx_pkts++;
3108 			else if (rc == -EBUSY)	/* partial completion */
3109 				break;
3110 			if (event & BNXT_REDIRECT_EVENT)
3111 				flush_xdp = true;
3112 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3113 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3114 			bnxt_hwrm_handler(bp, txcmp);
3115 		} else {
3116 			netdev_err(bp->dev,
3117 				   "Invalid completion received on special ring\n");
3118 		}
3119 		raw_cons = NEXT_RAW_CMP(raw_cons);
3120 
3121 		if (rx_pkts == budget)
3122 			break;
3123 	}
3124 
3125 	cpr->cp_raw_cons = raw_cons;
3126 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3127 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3128 
3129 	if (event & BNXT_AGG_EVENT)
3130 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3131 	if (flush_xdp)
3132 		xdp_do_flush();
3133 
3134 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3135 		napi_complete_done(napi, rx_pkts);
3136 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3137 	}
3138 	return rx_pkts;
3139 }
3140 
3141 static int bnxt_poll(struct napi_struct *napi, int budget)
3142 {
3143 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3144 	struct bnxt *bp = bnapi->bp;
3145 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3146 	int work_done = 0;
3147 
3148 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3149 		napi_complete(napi);
3150 		return 0;
3151 	}
3152 	while (1) {
3153 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3154 
3155 		if (work_done >= budget) {
3156 			if (!budget)
3157 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3158 			break;
3159 		}
3160 
3161 		if (!bnxt_has_work(bp, cpr)) {
3162 			if (napi_complete_done(napi, work_done))
3163 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3164 			break;
3165 		}
3166 	}
3167 	if (bp->flags & BNXT_FLAG_DIM) {
3168 		struct dim_sample dim_sample = {};
3169 
3170 		dim_update_sample(cpr->event_ctr,
3171 				  cpr->rx_packets,
3172 				  cpr->rx_bytes,
3173 				  &dim_sample);
3174 		net_dim(&cpr->dim, &dim_sample);
3175 	}
3176 	return work_done;
3177 }
3178 
3179 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3180 {
3181 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3182 	int i, work_done = 0;
3183 
3184 	for (i = 0; i < cpr->cp_ring_count; i++) {
3185 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3186 
3187 		if (cpr2->had_nqe_notify) {
3188 			work_done += __bnxt_poll_work(bp, cpr2,
3189 						      budget - work_done);
3190 			cpr->has_more_work |= cpr2->has_more_work;
3191 		}
3192 	}
3193 	return work_done;
3194 }
3195 
3196 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3197 				 u64 dbr_type, int budget)
3198 {
3199 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3200 	int i;
3201 
3202 	for (i = 0; i < cpr->cp_ring_count; i++) {
3203 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3204 		struct bnxt_db_info *db;
3205 
3206 		if (cpr2->had_work_done) {
3207 			u32 tgl = 0;
3208 
3209 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3210 				cpr2->had_nqe_notify = 0;
3211 				tgl = cpr2->toggle;
3212 			}
3213 			db = &cpr2->cp_db;
3214 			bnxt_writeq(bp,
3215 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3216 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3217 				    db->doorbell);
3218 			cpr2->had_work_done = 0;
3219 		}
3220 	}
3221 	__bnxt_poll_work_done(bp, bnapi, budget);
3222 }
3223 
3224 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3225 {
3226 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3227 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3228 	struct bnxt_cp_ring_info *cpr_rx;
3229 	u32 raw_cons = cpr->cp_raw_cons;
3230 	struct bnxt *bp = bnapi->bp;
3231 	struct nqe_cn *nqcmp;
3232 	int work_done = 0;
3233 	u32 cons;
3234 
3235 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3236 		napi_complete(napi);
3237 		return 0;
3238 	}
3239 	if (cpr->has_more_work) {
3240 		cpr->has_more_work = 0;
3241 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3242 	}
3243 	while (1) {
3244 		u16 type;
3245 
3246 		cons = RING_CMP(raw_cons);
3247 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3248 
3249 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3250 			if (cpr->has_more_work)
3251 				break;
3252 
3253 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3254 					     budget);
3255 			cpr->cp_raw_cons = raw_cons;
3256 			if (napi_complete_done(napi, work_done))
3257 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3258 						  cpr->cp_raw_cons);
3259 			goto poll_done;
3260 		}
3261 
3262 		/* The valid test of the entry must be done first before
3263 		 * reading any further.
3264 		 */
3265 		dma_rmb();
3266 
3267 		type = le16_to_cpu(nqcmp->type);
3268 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3269 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3270 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3271 			struct bnxt_cp_ring_info *cpr2;
3272 
3273 			/* No more budget for RX work */
3274 			if (budget && work_done >= budget &&
3275 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3276 				break;
3277 
3278 			idx = BNXT_NQ_HDL_IDX(idx);
3279 			cpr2 = &cpr->cp_ring_arr[idx];
3280 			cpr2->had_nqe_notify = 1;
3281 			cpr2->toggle = NQE_CN_TOGGLE(type);
3282 			work_done += __bnxt_poll_work(bp, cpr2,
3283 						      budget - work_done);
3284 			cpr->has_more_work |= cpr2->has_more_work;
3285 		} else {
3286 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3287 		}
3288 		raw_cons = NEXT_RAW_CMP(raw_cons);
3289 	}
3290 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3291 	if (raw_cons != cpr->cp_raw_cons) {
3292 		cpr->cp_raw_cons = raw_cons;
3293 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3294 	}
3295 poll_done:
3296 	cpr_rx = &cpr->cp_ring_arr[0];
3297 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3298 	    (bp->flags & BNXT_FLAG_DIM)) {
3299 		struct dim_sample dim_sample = {};
3300 
3301 		dim_update_sample(cpr->event_ctr,
3302 				  cpr_rx->rx_packets,
3303 				  cpr_rx->rx_bytes,
3304 				  &dim_sample);
3305 		net_dim(&cpr->dim, &dim_sample);
3306 	}
3307 	return work_done;
3308 }
3309 
3310 static void bnxt_free_tx_skbs(struct bnxt *bp)
3311 {
3312 	int i, max_idx;
3313 	struct pci_dev *pdev = bp->pdev;
3314 
3315 	if (!bp->tx_ring)
3316 		return;
3317 
3318 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3319 	for (i = 0; i < bp->tx_nr_rings; i++) {
3320 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3321 		int j;
3322 
3323 		if (!txr->tx_buf_ring)
3324 			continue;
3325 
3326 		for (j = 0; j < max_idx;) {
3327 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
3328 			struct sk_buff *skb;
3329 			int k, last;
3330 
3331 			if (i < bp->tx_nr_rings_xdp &&
3332 			    tx_buf->action == XDP_REDIRECT) {
3333 				dma_unmap_single(&pdev->dev,
3334 					dma_unmap_addr(tx_buf, mapping),
3335 					dma_unmap_len(tx_buf, len),
3336 					DMA_TO_DEVICE);
3337 				xdp_return_frame(tx_buf->xdpf);
3338 				tx_buf->action = 0;
3339 				tx_buf->xdpf = NULL;
3340 				j++;
3341 				continue;
3342 			}
3343 
3344 			skb = tx_buf->skb;
3345 			if (!skb) {
3346 				j++;
3347 				continue;
3348 			}
3349 
3350 			tx_buf->skb = NULL;
3351 
3352 			if (tx_buf->is_push) {
3353 				dev_kfree_skb(skb);
3354 				j += 2;
3355 				continue;
3356 			}
3357 
3358 			dma_unmap_single(&pdev->dev,
3359 					 dma_unmap_addr(tx_buf, mapping),
3360 					 skb_headlen(skb),
3361 					 DMA_TO_DEVICE);
3362 
3363 			last = tx_buf->nr_frags;
3364 			j += 2;
3365 			for (k = 0; k < last; k++, j++) {
3366 				int ring_idx = j & bp->tx_ring_mask;
3367 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
3368 
3369 				tx_buf = &txr->tx_buf_ring[ring_idx];
3370 				dma_unmap_page(
3371 					&pdev->dev,
3372 					dma_unmap_addr(tx_buf, mapping),
3373 					skb_frag_size(frag), DMA_TO_DEVICE);
3374 			}
3375 			dev_kfree_skb(skb);
3376 		}
3377 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
3378 	}
3379 }
3380 
3381 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3382 {
3383 	int i, max_idx;
3384 
3385 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3386 
3387 	for (i = 0; i < max_idx; i++) {
3388 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3389 		void *data = rx_buf->data;
3390 
3391 		if (!data)
3392 			continue;
3393 
3394 		rx_buf->data = NULL;
3395 		if (BNXT_RX_PAGE_MODE(bp))
3396 			page_pool_recycle_direct(rxr->page_pool, data);
3397 		else
3398 			page_pool_free_va(rxr->head_pool, data, true);
3399 	}
3400 }
3401 
3402 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3403 {
3404 	int i, max_idx;
3405 
3406 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3407 
3408 	for (i = 0; i < max_idx; i++) {
3409 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3410 		struct page *page = rx_agg_buf->page;
3411 
3412 		if (!page)
3413 			continue;
3414 
3415 		rx_agg_buf->page = NULL;
3416 		__clear_bit(i, rxr->rx_agg_bmap);
3417 
3418 		page_pool_recycle_direct(rxr->page_pool, page);
3419 	}
3420 }
3421 
3422 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3423 					struct bnxt_rx_ring_info *rxr)
3424 {
3425 	int i;
3426 
3427 	for (i = 0; i < bp->max_tpa; i++) {
3428 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3429 		u8 *data = tpa_info->data;
3430 
3431 		if (!data)
3432 			continue;
3433 
3434 		tpa_info->data = NULL;
3435 		page_pool_free_va(rxr->head_pool, data, false);
3436 	}
3437 }
3438 
3439 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3440 				       struct bnxt_rx_ring_info *rxr)
3441 {
3442 	struct bnxt_tpa_idx_map *map;
3443 
3444 	if (!rxr->rx_tpa)
3445 		goto skip_rx_tpa_free;
3446 
3447 	bnxt_free_one_tpa_info_data(bp, rxr);
3448 
3449 skip_rx_tpa_free:
3450 	if (!rxr->rx_buf_ring)
3451 		goto skip_rx_buf_free;
3452 
3453 	bnxt_free_one_rx_ring(bp, rxr);
3454 
3455 skip_rx_buf_free:
3456 	if (!rxr->rx_agg_ring)
3457 		goto skip_rx_agg_free;
3458 
3459 	bnxt_free_one_rx_agg_ring(bp, rxr);
3460 
3461 skip_rx_agg_free:
3462 	map = rxr->rx_tpa_idx_map;
3463 	if (map)
3464 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3465 }
3466 
3467 static void bnxt_free_rx_skbs(struct bnxt *bp)
3468 {
3469 	int i;
3470 
3471 	if (!bp->rx_ring)
3472 		return;
3473 
3474 	for (i = 0; i < bp->rx_nr_rings; i++)
3475 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3476 }
3477 
3478 static void bnxt_free_skbs(struct bnxt *bp)
3479 {
3480 	bnxt_free_tx_skbs(bp);
3481 	bnxt_free_rx_skbs(bp);
3482 }
3483 
3484 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3485 {
3486 	u8 init_val = ctxm->init_value;
3487 	u16 offset = ctxm->init_offset;
3488 	u8 *p2 = p;
3489 	int i;
3490 
3491 	if (!init_val)
3492 		return;
3493 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3494 		memset(p, init_val, len);
3495 		return;
3496 	}
3497 	for (i = 0; i < len; i += ctxm->entry_size)
3498 		*(p2 + i + offset) = init_val;
3499 }
3500 
3501 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3502 			       void *buf, size_t offset, size_t head,
3503 			       size_t tail)
3504 {
3505 	int i, head_page, start_idx, source_offset;
3506 	size_t len, rem_len, total_len, max_bytes;
3507 
3508 	head_page = head / rmem->page_size;
3509 	source_offset = head % rmem->page_size;
3510 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3511 	if (!total_len)
3512 		total_len = MAX_CTX_BYTES;
3513 	start_idx = head_page % MAX_CTX_PAGES;
3514 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3515 		    source_offset;
3516 	total_len = min(total_len, max_bytes);
3517 	rem_len = total_len;
3518 
3519 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3520 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3521 		if (buf)
3522 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3523 			       len);
3524 		offset += len;
3525 		rem_len -= len;
3526 	}
3527 	return total_len;
3528 }
3529 
3530 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3531 {
3532 	struct pci_dev *pdev = bp->pdev;
3533 	int i;
3534 
3535 	if (!rmem->pg_arr)
3536 		goto skip_pages;
3537 
3538 	for (i = 0; i < rmem->nr_pages; i++) {
3539 		if (!rmem->pg_arr[i])
3540 			continue;
3541 
3542 		dma_free_coherent(&pdev->dev, rmem->page_size,
3543 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3544 
3545 		rmem->pg_arr[i] = NULL;
3546 	}
3547 skip_pages:
3548 	if (rmem->pg_tbl) {
3549 		size_t pg_tbl_size = rmem->nr_pages * 8;
3550 
3551 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3552 			pg_tbl_size = rmem->page_size;
3553 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3554 				  rmem->pg_tbl, rmem->pg_tbl_map);
3555 		rmem->pg_tbl = NULL;
3556 	}
3557 	if (rmem->vmem_size && *rmem->vmem) {
3558 		vfree(*rmem->vmem);
3559 		*rmem->vmem = NULL;
3560 	}
3561 }
3562 
3563 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3564 {
3565 	struct pci_dev *pdev = bp->pdev;
3566 	u64 valid_bit = 0;
3567 	int i;
3568 
3569 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3570 		valid_bit = PTU_PTE_VALID;
3571 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3572 		size_t pg_tbl_size = rmem->nr_pages * 8;
3573 
3574 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3575 			pg_tbl_size = rmem->page_size;
3576 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3577 						  &rmem->pg_tbl_map,
3578 						  GFP_KERNEL);
3579 		if (!rmem->pg_tbl)
3580 			return -ENOMEM;
3581 	}
3582 
3583 	for (i = 0; i < rmem->nr_pages; i++) {
3584 		u64 extra_bits = valid_bit;
3585 
3586 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3587 						     rmem->page_size,
3588 						     &rmem->dma_arr[i],
3589 						     GFP_KERNEL);
3590 		if (!rmem->pg_arr[i])
3591 			return -ENOMEM;
3592 
3593 		if (rmem->ctx_mem)
3594 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3595 					  rmem->page_size);
3596 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3597 			if (i == rmem->nr_pages - 2 &&
3598 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3599 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3600 			else if (i == rmem->nr_pages - 1 &&
3601 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3602 				extra_bits |= PTU_PTE_LAST;
3603 			rmem->pg_tbl[i] =
3604 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3605 		}
3606 	}
3607 
3608 	if (rmem->vmem_size) {
3609 		*rmem->vmem = vzalloc(rmem->vmem_size);
3610 		if (!(*rmem->vmem))
3611 			return -ENOMEM;
3612 	}
3613 	return 0;
3614 }
3615 
3616 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3617 				   struct bnxt_rx_ring_info *rxr)
3618 {
3619 	int i;
3620 
3621 	kfree(rxr->rx_tpa_idx_map);
3622 	rxr->rx_tpa_idx_map = NULL;
3623 	if (rxr->rx_tpa) {
3624 		for (i = 0; i < bp->max_tpa; i++) {
3625 			kfree(rxr->rx_tpa[i].agg_arr);
3626 			rxr->rx_tpa[i].agg_arr = NULL;
3627 		}
3628 	}
3629 	kfree(rxr->rx_tpa);
3630 	rxr->rx_tpa = NULL;
3631 }
3632 
3633 static void bnxt_free_tpa_info(struct bnxt *bp)
3634 {
3635 	int i;
3636 
3637 	for (i = 0; i < bp->rx_nr_rings; i++) {
3638 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3639 
3640 		bnxt_free_one_tpa_info(bp, rxr);
3641 	}
3642 }
3643 
3644 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3645 				   struct bnxt_rx_ring_info *rxr)
3646 {
3647 	struct rx_agg_cmp *agg;
3648 	int i;
3649 
3650 	rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3651 			      GFP_KERNEL);
3652 	if (!rxr->rx_tpa)
3653 		return -ENOMEM;
3654 
3655 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3656 		return 0;
3657 	for (i = 0; i < bp->max_tpa; i++) {
3658 		agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3659 		if (!agg)
3660 			return -ENOMEM;
3661 		rxr->rx_tpa[i].agg_arr = agg;
3662 	}
3663 	rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3664 				      GFP_KERNEL);
3665 	if (!rxr->rx_tpa_idx_map)
3666 		return -ENOMEM;
3667 
3668 	return 0;
3669 }
3670 
3671 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3672 {
3673 	int i, rc;
3674 
3675 	bp->max_tpa = MAX_TPA;
3676 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3677 		if (!bp->max_tpa_v2)
3678 			return 0;
3679 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3680 	}
3681 
3682 	for (i = 0; i < bp->rx_nr_rings; i++) {
3683 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3684 
3685 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3686 		if (rc)
3687 			return rc;
3688 	}
3689 	return 0;
3690 }
3691 
3692 static void bnxt_free_rx_rings(struct bnxt *bp)
3693 {
3694 	int i;
3695 
3696 	if (!bp->rx_ring)
3697 		return;
3698 
3699 	bnxt_free_tpa_info(bp);
3700 	for (i = 0; i < bp->rx_nr_rings; i++) {
3701 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3702 		struct bnxt_ring_struct *ring;
3703 
3704 		if (rxr->xdp_prog)
3705 			bpf_prog_put(rxr->xdp_prog);
3706 
3707 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3708 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3709 
3710 		page_pool_destroy(rxr->page_pool);
3711 		if (bnxt_separate_head_pool())
3712 			page_pool_destroy(rxr->head_pool);
3713 		rxr->page_pool = rxr->head_pool = NULL;
3714 
3715 		kfree(rxr->rx_agg_bmap);
3716 		rxr->rx_agg_bmap = NULL;
3717 
3718 		ring = &rxr->rx_ring_struct;
3719 		bnxt_free_ring(bp, &ring->ring_mem);
3720 
3721 		ring = &rxr->rx_agg_ring_struct;
3722 		bnxt_free_ring(bp, &ring->ring_mem);
3723 	}
3724 }
3725 
3726 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3727 				   struct bnxt_rx_ring_info *rxr,
3728 				   int numa_node)
3729 {
3730 	struct page_pool_params pp = { 0 };
3731 	struct page_pool *pool;
3732 
3733 	pp.pool_size = bp->rx_agg_ring_size;
3734 	if (BNXT_RX_PAGE_MODE(bp))
3735 		pp.pool_size += bp->rx_ring_size;
3736 	pp.nid = numa_node;
3737 	pp.napi = &rxr->bnapi->napi;
3738 	pp.netdev = bp->dev;
3739 	pp.dev = &bp->pdev->dev;
3740 	pp.dma_dir = bp->rx_dir;
3741 	pp.max_len = PAGE_SIZE;
3742 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3743 
3744 	pool = page_pool_create(&pp);
3745 	if (IS_ERR(pool))
3746 		return PTR_ERR(pool);
3747 	rxr->page_pool = pool;
3748 
3749 	if (bnxt_separate_head_pool()) {
3750 		pp.pool_size = max(bp->rx_ring_size, 1024);
3751 		pool = page_pool_create(&pp);
3752 		if (IS_ERR(pool))
3753 			goto err_destroy_pp;
3754 	}
3755 	rxr->head_pool = pool;
3756 
3757 	return 0;
3758 
3759 err_destroy_pp:
3760 	page_pool_destroy(rxr->page_pool);
3761 	rxr->page_pool = NULL;
3762 	return PTR_ERR(pool);
3763 }
3764 
3765 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3766 {
3767 	u16 mem_size;
3768 
3769 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3770 	mem_size = rxr->rx_agg_bmap_size / 8;
3771 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3772 	if (!rxr->rx_agg_bmap)
3773 		return -ENOMEM;
3774 
3775 	return 0;
3776 }
3777 
3778 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3779 {
3780 	int numa_node = dev_to_node(&bp->pdev->dev);
3781 	int i, rc = 0, agg_rings = 0, cpu;
3782 
3783 	if (!bp->rx_ring)
3784 		return -ENOMEM;
3785 
3786 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3787 		agg_rings = 1;
3788 
3789 	for (i = 0; i < bp->rx_nr_rings; i++) {
3790 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3791 		struct bnxt_ring_struct *ring;
3792 		int cpu_node;
3793 
3794 		ring = &rxr->rx_ring_struct;
3795 
3796 		cpu = cpumask_local_spread(i, numa_node);
3797 		cpu_node = cpu_to_node(cpu);
3798 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3799 			   i, cpu_node);
3800 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3801 		if (rc)
3802 			return rc;
3803 
3804 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3805 		if (rc < 0)
3806 			return rc;
3807 
3808 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3809 						MEM_TYPE_PAGE_POOL,
3810 						rxr->page_pool);
3811 		if (rc) {
3812 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3813 			return rc;
3814 		}
3815 
3816 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3817 		if (rc)
3818 			return rc;
3819 
3820 		ring->grp_idx = i;
3821 		if (agg_rings) {
3822 			ring = &rxr->rx_agg_ring_struct;
3823 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3824 			if (rc)
3825 				return rc;
3826 
3827 			ring->grp_idx = i;
3828 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3829 			if (rc)
3830 				return rc;
3831 		}
3832 	}
3833 	if (bp->flags & BNXT_FLAG_TPA)
3834 		rc = bnxt_alloc_tpa_info(bp);
3835 	return rc;
3836 }
3837 
3838 static void bnxt_free_tx_rings(struct bnxt *bp)
3839 {
3840 	int i;
3841 	struct pci_dev *pdev = bp->pdev;
3842 
3843 	if (!bp->tx_ring)
3844 		return;
3845 
3846 	for (i = 0; i < bp->tx_nr_rings; i++) {
3847 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3848 		struct bnxt_ring_struct *ring;
3849 
3850 		if (txr->tx_push) {
3851 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3852 					  txr->tx_push, txr->tx_push_mapping);
3853 			txr->tx_push = NULL;
3854 		}
3855 
3856 		ring = &txr->tx_ring_struct;
3857 
3858 		bnxt_free_ring(bp, &ring->ring_mem);
3859 	}
3860 }
3861 
3862 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3863 	((tc) * (bp)->tx_nr_rings_per_tc)
3864 
3865 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3866 	((tx) % (bp)->tx_nr_rings_per_tc)
3867 
3868 #define BNXT_RING_TO_TC(bp, tx)		\
3869 	((tx) / (bp)->tx_nr_rings_per_tc)
3870 
3871 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3872 {
3873 	int i, j, rc;
3874 	struct pci_dev *pdev = bp->pdev;
3875 
3876 	bp->tx_push_size = 0;
3877 	if (bp->tx_push_thresh) {
3878 		int push_size;
3879 
3880 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3881 					bp->tx_push_thresh);
3882 
3883 		if (push_size > 256) {
3884 			push_size = 0;
3885 			bp->tx_push_thresh = 0;
3886 		}
3887 
3888 		bp->tx_push_size = push_size;
3889 	}
3890 
3891 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3892 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3893 		struct bnxt_ring_struct *ring;
3894 		u8 qidx;
3895 
3896 		ring = &txr->tx_ring_struct;
3897 
3898 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3899 		if (rc)
3900 			return rc;
3901 
3902 		ring->grp_idx = txr->bnapi->index;
3903 		if (bp->tx_push_size) {
3904 			dma_addr_t mapping;
3905 
3906 			/* One pre-allocated DMA buffer to backup
3907 			 * TX push operation
3908 			 */
3909 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3910 						bp->tx_push_size,
3911 						&txr->tx_push_mapping,
3912 						GFP_KERNEL);
3913 
3914 			if (!txr->tx_push)
3915 				return -ENOMEM;
3916 
3917 			mapping = txr->tx_push_mapping +
3918 				sizeof(struct tx_push_bd);
3919 			txr->data_mapping = cpu_to_le64(mapping);
3920 		}
3921 		qidx = bp->tc_to_qidx[j];
3922 		ring->queue_id = bp->q_info[qidx].queue_id;
3923 		spin_lock_init(&txr->xdp_tx_lock);
3924 		if (i < bp->tx_nr_rings_xdp)
3925 			continue;
3926 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
3927 			j++;
3928 	}
3929 	return 0;
3930 }
3931 
3932 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3933 {
3934 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3935 
3936 	kfree(cpr->cp_desc_ring);
3937 	cpr->cp_desc_ring = NULL;
3938 	ring->ring_mem.pg_arr = NULL;
3939 	kfree(cpr->cp_desc_mapping);
3940 	cpr->cp_desc_mapping = NULL;
3941 	ring->ring_mem.dma_arr = NULL;
3942 }
3943 
3944 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3945 {
3946 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3947 	if (!cpr->cp_desc_ring)
3948 		return -ENOMEM;
3949 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3950 				       GFP_KERNEL);
3951 	if (!cpr->cp_desc_mapping)
3952 		return -ENOMEM;
3953 	return 0;
3954 }
3955 
3956 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3957 {
3958 	int i;
3959 
3960 	if (!bp->bnapi)
3961 		return;
3962 	for (i = 0; i < bp->cp_nr_rings; i++) {
3963 		struct bnxt_napi *bnapi = bp->bnapi[i];
3964 
3965 		if (!bnapi)
3966 			continue;
3967 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3968 	}
3969 }
3970 
3971 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3972 {
3973 	int i, n = bp->cp_nr_pages;
3974 
3975 	for (i = 0; i < bp->cp_nr_rings; i++) {
3976 		struct bnxt_napi *bnapi = bp->bnapi[i];
3977 		int rc;
3978 
3979 		if (!bnapi)
3980 			continue;
3981 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3982 		if (rc)
3983 			return rc;
3984 	}
3985 	return 0;
3986 }
3987 
3988 static void bnxt_free_cp_rings(struct bnxt *bp)
3989 {
3990 	int i;
3991 
3992 	if (!bp->bnapi)
3993 		return;
3994 
3995 	for (i = 0; i < bp->cp_nr_rings; i++) {
3996 		struct bnxt_napi *bnapi = bp->bnapi[i];
3997 		struct bnxt_cp_ring_info *cpr;
3998 		struct bnxt_ring_struct *ring;
3999 		int j;
4000 
4001 		if (!bnapi)
4002 			continue;
4003 
4004 		cpr = &bnapi->cp_ring;
4005 		ring = &cpr->cp_ring_struct;
4006 
4007 		bnxt_free_ring(bp, &ring->ring_mem);
4008 
4009 		if (!cpr->cp_ring_arr)
4010 			continue;
4011 
4012 		for (j = 0; j < cpr->cp_ring_count; j++) {
4013 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4014 
4015 			ring = &cpr2->cp_ring_struct;
4016 			bnxt_free_ring(bp, &ring->ring_mem);
4017 			bnxt_free_cp_arrays(cpr2);
4018 		}
4019 		kfree(cpr->cp_ring_arr);
4020 		cpr->cp_ring_arr = NULL;
4021 		cpr->cp_ring_count = 0;
4022 	}
4023 }
4024 
4025 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4026 				  struct bnxt_cp_ring_info *cpr)
4027 {
4028 	struct bnxt_ring_mem_info *rmem;
4029 	struct bnxt_ring_struct *ring;
4030 	int rc;
4031 
4032 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4033 	if (rc) {
4034 		bnxt_free_cp_arrays(cpr);
4035 		return -ENOMEM;
4036 	}
4037 	ring = &cpr->cp_ring_struct;
4038 	rmem = &ring->ring_mem;
4039 	rmem->nr_pages = bp->cp_nr_pages;
4040 	rmem->page_size = HW_CMPD_RING_SIZE;
4041 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4042 	rmem->dma_arr = cpr->cp_desc_mapping;
4043 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4044 	rc = bnxt_alloc_ring(bp, rmem);
4045 	if (rc) {
4046 		bnxt_free_ring(bp, rmem);
4047 		bnxt_free_cp_arrays(cpr);
4048 	}
4049 	return rc;
4050 }
4051 
4052 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4053 {
4054 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4055 	int i, j, rc, ulp_msix;
4056 	int tcs = bp->num_tc;
4057 
4058 	if (!tcs)
4059 		tcs = 1;
4060 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4061 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4062 		struct bnxt_napi *bnapi = bp->bnapi[i];
4063 		struct bnxt_cp_ring_info *cpr, *cpr2;
4064 		struct bnxt_ring_struct *ring;
4065 		int cp_count = 0, k;
4066 		int rx = 0, tx = 0;
4067 
4068 		if (!bnapi)
4069 			continue;
4070 
4071 		cpr = &bnapi->cp_ring;
4072 		cpr->bnapi = bnapi;
4073 		ring = &cpr->cp_ring_struct;
4074 
4075 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4076 		if (rc)
4077 			return rc;
4078 
4079 		ring->map_idx = ulp_msix + i;
4080 
4081 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4082 			continue;
4083 
4084 		if (i < bp->rx_nr_rings) {
4085 			cp_count++;
4086 			rx = 1;
4087 		}
4088 		if (i < bp->tx_nr_rings_xdp) {
4089 			cp_count++;
4090 			tx = 1;
4091 		} else if ((sh && i < bp->tx_nr_rings) ||
4092 			 (!sh && i >= bp->rx_nr_rings)) {
4093 			cp_count += tcs;
4094 			tx = 1;
4095 		}
4096 
4097 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
4098 					   GFP_KERNEL);
4099 		if (!cpr->cp_ring_arr)
4100 			return -ENOMEM;
4101 		cpr->cp_ring_count = cp_count;
4102 
4103 		for (k = 0; k < cp_count; k++) {
4104 			cpr2 = &cpr->cp_ring_arr[k];
4105 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4106 			if (rc)
4107 				return rc;
4108 			cpr2->bnapi = bnapi;
4109 			cpr2->sw_stats = cpr->sw_stats;
4110 			cpr2->cp_idx = k;
4111 			if (!k && rx) {
4112 				bp->rx_ring[i].rx_cpr = cpr2;
4113 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4114 			} else {
4115 				int n, tc = k - rx;
4116 
4117 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4118 				bp->tx_ring[n].tx_cpr = cpr2;
4119 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4120 			}
4121 		}
4122 		if (tx)
4123 			j++;
4124 	}
4125 	return 0;
4126 }
4127 
4128 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4129 				     struct bnxt_rx_ring_info *rxr)
4130 {
4131 	struct bnxt_ring_mem_info *rmem;
4132 	struct bnxt_ring_struct *ring;
4133 
4134 	ring = &rxr->rx_ring_struct;
4135 	rmem = &ring->ring_mem;
4136 	rmem->nr_pages = bp->rx_nr_pages;
4137 	rmem->page_size = HW_RXBD_RING_SIZE;
4138 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4139 	rmem->dma_arr = rxr->rx_desc_mapping;
4140 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4141 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4142 
4143 	ring = &rxr->rx_agg_ring_struct;
4144 	rmem = &ring->ring_mem;
4145 	rmem->nr_pages = bp->rx_agg_nr_pages;
4146 	rmem->page_size = HW_RXBD_RING_SIZE;
4147 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4148 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4149 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4150 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4151 }
4152 
4153 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4154 				      struct bnxt_rx_ring_info *rxr)
4155 {
4156 	struct bnxt_ring_mem_info *rmem;
4157 	struct bnxt_ring_struct *ring;
4158 	int i;
4159 
4160 	rxr->page_pool->p.napi = NULL;
4161 	rxr->page_pool = NULL;
4162 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4163 
4164 	ring = &rxr->rx_ring_struct;
4165 	rmem = &ring->ring_mem;
4166 	rmem->pg_tbl = NULL;
4167 	rmem->pg_tbl_map = 0;
4168 	for (i = 0; i < rmem->nr_pages; i++) {
4169 		rmem->pg_arr[i] = NULL;
4170 		rmem->dma_arr[i] = 0;
4171 	}
4172 	*rmem->vmem = NULL;
4173 
4174 	ring = &rxr->rx_agg_ring_struct;
4175 	rmem = &ring->ring_mem;
4176 	rmem->pg_tbl = NULL;
4177 	rmem->pg_tbl_map = 0;
4178 	for (i = 0; i < rmem->nr_pages; i++) {
4179 		rmem->pg_arr[i] = NULL;
4180 		rmem->dma_arr[i] = 0;
4181 	}
4182 	*rmem->vmem = NULL;
4183 }
4184 
4185 static void bnxt_init_ring_struct(struct bnxt *bp)
4186 {
4187 	int i, j;
4188 
4189 	for (i = 0; i < bp->cp_nr_rings; i++) {
4190 		struct bnxt_napi *bnapi = bp->bnapi[i];
4191 		struct bnxt_ring_mem_info *rmem;
4192 		struct bnxt_cp_ring_info *cpr;
4193 		struct bnxt_rx_ring_info *rxr;
4194 		struct bnxt_tx_ring_info *txr;
4195 		struct bnxt_ring_struct *ring;
4196 
4197 		if (!bnapi)
4198 			continue;
4199 
4200 		cpr = &bnapi->cp_ring;
4201 		ring = &cpr->cp_ring_struct;
4202 		rmem = &ring->ring_mem;
4203 		rmem->nr_pages = bp->cp_nr_pages;
4204 		rmem->page_size = HW_CMPD_RING_SIZE;
4205 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4206 		rmem->dma_arr = cpr->cp_desc_mapping;
4207 		rmem->vmem_size = 0;
4208 
4209 		rxr = bnapi->rx_ring;
4210 		if (!rxr)
4211 			goto skip_rx;
4212 
4213 		ring = &rxr->rx_ring_struct;
4214 		rmem = &ring->ring_mem;
4215 		rmem->nr_pages = bp->rx_nr_pages;
4216 		rmem->page_size = HW_RXBD_RING_SIZE;
4217 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4218 		rmem->dma_arr = rxr->rx_desc_mapping;
4219 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4220 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4221 
4222 		ring = &rxr->rx_agg_ring_struct;
4223 		rmem = &ring->ring_mem;
4224 		rmem->nr_pages = bp->rx_agg_nr_pages;
4225 		rmem->page_size = HW_RXBD_RING_SIZE;
4226 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4227 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4228 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4229 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4230 
4231 skip_rx:
4232 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4233 			ring = &txr->tx_ring_struct;
4234 			rmem = &ring->ring_mem;
4235 			rmem->nr_pages = bp->tx_nr_pages;
4236 			rmem->page_size = HW_TXBD_RING_SIZE;
4237 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4238 			rmem->dma_arr = txr->tx_desc_mapping;
4239 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4240 			rmem->vmem = (void **)&txr->tx_buf_ring;
4241 		}
4242 	}
4243 }
4244 
4245 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4246 {
4247 	int i;
4248 	u32 prod;
4249 	struct rx_bd **rx_buf_ring;
4250 
4251 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4252 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4253 		int j;
4254 		struct rx_bd *rxbd;
4255 
4256 		rxbd = rx_buf_ring[i];
4257 		if (!rxbd)
4258 			continue;
4259 
4260 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4261 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4262 			rxbd->rx_bd_opaque = prod;
4263 		}
4264 	}
4265 }
4266 
4267 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4268 				       struct bnxt_rx_ring_info *rxr,
4269 				       int ring_nr)
4270 {
4271 	u32 prod;
4272 	int i;
4273 
4274 	prod = rxr->rx_prod;
4275 	for (i = 0; i < bp->rx_ring_size; i++) {
4276 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4277 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4278 				    ring_nr, i, bp->rx_ring_size);
4279 			break;
4280 		}
4281 		prod = NEXT_RX(prod);
4282 	}
4283 	rxr->rx_prod = prod;
4284 }
4285 
4286 static void bnxt_alloc_one_rx_ring_page(struct bnxt *bp,
4287 					struct bnxt_rx_ring_info *rxr,
4288 					int ring_nr)
4289 {
4290 	u32 prod;
4291 	int i;
4292 
4293 	prod = rxr->rx_agg_prod;
4294 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4295 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
4296 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4297 				    ring_nr, i, bp->rx_ring_size);
4298 			break;
4299 		}
4300 		prod = NEXT_RX_AGG(prod);
4301 	}
4302 	rxr->rx_agg_prod = prod;
4303 }
4304 
4305 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4306 					struct bnxt_rx_ring_info *rxr)
4307 {
4308 	dma_addr_t mapping;
4309 	u8 *data;
4310 	int i;
4311 
4312 	for (i = 0; i < bp->max_tpa; i++) {
4313 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4314 					    GFP_KERNEL);
4315 		if (!data)
4316 			return -ENOMEM;
4317 
4318 		rxr->rx_tpa[i].data = data;
4319 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4320 		rxr->rx_tpa[i].mapping = mapping;
4321 	}
4322 
4323 	return 0;
4324 }
4325 
4326 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4327 {
4328 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4329 	int rc;
4330 
4331 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4332 
4333 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4334 		return 0;
4335 
4336 	bnxt_alloc_one_rx_ring_page(bp, rxr, ring_nr);
4337 
4338 	if (rxr->rx_tpa) {
4339 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4340 		if (rc)
4341 			return rc;
4342 	}
4343 	return 0;
4344 }
4345 
4346 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4347 				       struct bnxt_rx_ring_info *rxr)
4348 {
4349 	struct bnxt_ring_struct *ring;
4350 	u32 type;
4351 
4352 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4353 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4354 
4355 	if (NET_IP_ALIGN == 2)
4356 		type |= RX_BD_FLAGS_SOP;
4357 
4358 	ring = &rxr->rx_ring_struct;
4359 	bnxt_init_rxbd_pages(ring, type);
4360 	ring->fw_ring_id = INVALID_HW_RING_ID;
4361 }
4362 
4363 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4364 					   struct bnxt_rx_ring_info *rxr)
4365 {
4366 	struct bnxt_ring_struct *ring;
4367 	u32 type;
4368 
4369 	ring = &rxr->rx_agg_ring_struct;
4370 	ring->fw_ring_id = INVALID_HW_RING_ID;
4371 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4372 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4373 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4374 
4375 		bnxt_init_rxbd_pages(ring, type);
4376 	}
4377 }
4378 
4379 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4380 {
4381 	struct bnxt_rx_ring_info *rxr;
4382 
4383 	rxr = &bp->rx_ring[ring_nr];
4384 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4385 
4386 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4387 			     &rxr->bnapi->napi);
4388 
4389 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4390 		bpf_prog_add(bp->xdp_prog, 1);
4391 		rxr->xdp_prog = bp->xdp_prog;
4392 	}
4393 
4394 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4395 
4396 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4397 }
4398 
4399 static void bnxt_init_cp_rings(struct bnxt *bp)
4400 {
4401 	int i, j;
4402 
4403 	for (i = 0; i < bp->cp_nr_rings; i++) {
4404 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4405 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4406 
4407 		ring->fw_ring_id = INVALID_HW_RING_ID;
4408 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4409 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4410 		if (!cpr->cp_ring_arr)
4411 			continue;
4412 		for (j = 0; j < cpr->cp_ring_count; j++) {
4413 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4414 
4415 			ring = &cpr2->cp_ring_struct;
4416 			ring->fw_ring_id = INVALID_HW_RING_ID;
4417 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4418 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4419 		}
4420 	}
4421 }
4422 
4423 static int bnxt_init_rx_rings(struct bnxt *bp)
4424 {
4425 	int i, rc = 0;
4426 
4427 	if (BNXT_RX_PAGE_MODE(bp)) {
4428 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4429 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4430 	} else {
4431 		bp->rx_offset = BNXT_RX_OFFSET;
4432 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4433 	}
4434 
4435 	for (i = 0; i < bp->rx_nr_rings; i++) {
4436 		rc = bnxt_init_one_rx_ring(bp, i);
4437 		if (rc)
4438 			break;
4439 	}
4440 
4441 	return rc;
4442 }
4443 
4444 static int bnxt_init_tx_rings(struct bnxt *bp)
4445 {
4446 	u16 i;
4447 
4448 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4449 				   BNXT_MIN_TX_DESC_CNT);
4450 
4451 	for (i = 0; i < bp->tx_nr_rings; i++) {
4452 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4453 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4454 
4455 		ring->fw_ring_id = INVALID_HW_RING_ID;
4456 
4457 		if (i >= bp->tx_nr_rings_xdp)
4458 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4459 					     NETDEV_QUEUE_TYPE_TX,
4460 					     &txr->bnapi->napi);
4461 	}
4462 
4463 	return 0;
4464 }
4465 
4466 static void bnxt_free_ring_grps(struct bnxt *bp)
4467 {
4468 	kfree(bp->grp_info);
4469 	bp->grp_info = NULL;
4470 }
4471 
4472 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4473 {
4474 	int i;
4475 
4476 	if (irq_re_init) {
4477 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4478 				       sizeof(struct bnxt_ring_grp_info),
4479 				       GFP_KERNEL);
4480 		if (!bp->grp_info)
4481 			return -ENOMEM;
4482 	}
4483 	for (i = 0; i < bp->cp_nr_rings; i++) {
4484 		if (irq_re_init)
4485 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4486 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4487 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4488 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4489 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4490 	}
4491 	return 0;
4492 }
4493 
4494 static void bnxt_free_vnics(struct bnxt *bp)
4495 {
4496 	kfree(bp->vnic_info);
4497 	bp->vnic_info = NULL;
4498 	bp->nr_vnics = 0;
4499 }
4500 
4501 static int bnxt_alloc_vnics(struct bnxt *bp)
4502 {
4503 	int num_vnics = 1;
4504 
4505 #ifdef CONFIG_RFS_ACCEL
4506 	if (bp->flags & BNXT_FLAG_RFS) {
4507 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4508 			num_vnics++;
4509 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4510 			num_vnics += bp->rx_nr_rings;
4511 	}
4512 #endif
4513 
4514 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4515 		num_vnics++;
4516 
4517 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4518 				GFP_KERNEL);
4519 	if (!bp->vnic_info)
4520 		return -ENOMEM;
4521 
4522 	bp->nr_vnics = num_vnics;
4523 	return 0;
4524 }
4525 
4526 static void bnxt_init_vnics(struct bnxt *bp)
4527 {
4528 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4529 	int i;
4530 
4531 	for (i = 0; i < bp->nr_vnics; i++) {
4532 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4533 		int j;
4534 
4535 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4536 		vnic->vnic_id = i;
4537 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4538 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4539 
4540 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4541 
4542 		if (bp->vnic_info[i].rss_hash_key) {
4543 			if (i == BNXT_VNIC_DEFAULT) {
4544 				u8 *key = (void *)vnic->rss_hash_key;
4545 				int k;
4546 
4547 				if (!bp->rss_hash_key_valid &&
4548 				    !bp->rss_hash_key_updated) {
4549 					get_random_bytes(bp->rss_hash_key,
4550 							 HW_HASH_KEY_SIZE);
4551 					bp->rss_hash_key_updated = true;
4552 				}
4553 
4554 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4555 				       HW_HASH_KEY_SIZE);
4556 
4557 				if (!bp->rss_hash_key_updated)
4558 					continue;
4559 
4560 				bp->rss_hash_key_updated = false;
4561 				bp->rss_hash_key_valid = true;
4562 
4563 				bp->toeplitz_prefix = 0;
4564 				for (k = 0; k < 8; k++) {
4565 					bp->toeplitz_prefix <<= 8;
4566 					bp->toeplitz_prefix |= key[k];
4567 				}
4568 			} else {
4569 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4570 				       HW_HASH_KEY_SIZE);
4571 			}
4572 		}
4573 	}
4574 }
4575 
4576 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4577 {
4578 	int pages;
4579 
4580 	pages = ring_size / desc_per_pg;
4581 
4582 	if (!pages)
4583 		return 1;
4584 
4585 	pages++;
4586 
4587 	while (pages & (pages - 1))
4588 		pages++;
4589 
4590 	return pages;
4591 }
4592 
4593 void bnxt_set_tpa_flags(struct bnxt *bp)
4594 {
4595 	bp->flags &= ~BNXT_FLAG_TPA;
4596 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4597 		return;
4598 	if (bp->dev->features & NETIF_F_LRO)
4599 		bp->flags |= BNXT_FLAG_LRO;
4600 	else if (bp->dev->features & NETIF_F_GRO_HW)
4601 		bp->flags |= BNXT_FLAG_GRO;
4602 }
4603 
4604 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4605  * be set on entry.
4606  */
4607 void bnxt_set_ring_params(struct bnxt *bp)
4608 {
4609 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4610 	u32 agg_factor = 0, agg_ring_size = 0;
4611 
4612 	/* 8 for CRC and VLAN */
4613 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4614 
4615 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4616 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4617 
4618 	bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
4619 	ring_size = bp->rx_ring_size;
4620 	bp->rx_agg_ring_size = 0;
4621 	bp->rx_agg_nr_pages = 0;
4622 
4623 	if (bp->flags & BNXT_FLAG_TPA)
4624 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4625 
4626 	bp->flags &= ~BNXT_FLAG_JUMBO;
4627 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4628 		u32 jumbo_factor;
4629 
4630 		bp->flags |= BNXT_FLAG_JUMBO;
4631 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4632 		if (jumbo_factor > agg_factor)
4633 			agg_factor = jumbo_factor;
4634 	}
4635 	if (agg_factor) {
4636 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4637 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4638 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4639 				    bp->rx_ring_size, ring_size);
4640 			bp->rx_ring_size = ring_size;
4641 		}
4642 		agg_ring_size = ring_size * agg_factor;
4643 
4644 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4645 							RX_DESC_CNT);
4646 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4647 			u32 tmp = agg_ring_size;
4648 
4649 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4650 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4651 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4652 				    tmp, agg_ring_size);
4653 		}
4654 		bp->rx_agg_ring_size = agg_ring_size;
4655 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4656 
4657 		if (BNXT_RX_PAGE_MODE(bp)) {
4658 			rx_space = PAGE_SIZE;
4659 			rx_size = PAGE_SIZE -
4660 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4661 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4662 		} else {
4663 			rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
4664 			rx_space = rx_size + NET_SKB_PAD +
4665 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4666 		}
4667 	}
4668 
4669 	bp->rx_buf_use_size = rx_size;
4670 	bp->rx_buf_size = rx_space;
4671 
4672 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4673 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4674 
4675 	ring_size = bp->tx_ring_size;
4676 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4677 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4678 
4679 	max_rx_cmpl = bp->rx_ring_size;
4680 	/* MAX TPA needs to be added because TPA_START completions are
4681 	 * immediately recycled, so the TPA completions are not bound by
4682 	 * the RX ring size.
4683 	 */
4684 	if (bp->flags & BNXT_FLAG_TPA)
4685 		max_rx_cmpl += bp->max_tpa;
4686 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4687 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4688 	bp->cp_ring_size = ring_size;
4689 
4690 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4691 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4692 		bp->cp_nr_pages = MAX_CP_PAGES;
4693 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4694 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4695 			    ring_size, bp->cp_ring_size);
4696 	}
4697 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4698 	bp->cp_ring_mask = bp->cp_bit - 1;
4699 }
4700 
4701 /* Changing allocation mode of RX rings.
4702  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4703  */
4704 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4705 {
4706 	struct net_device *dev = bp->dev;
4707 
4708 	if (page_mode) {
4709 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4710 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4711 
4712 		if (bp->xdp_prog->aux->xdp_has_frags)
4713 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4714 		else
4715 			dev->max_mtu =
4716 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4717 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4718 			bp->flags |= BNXT_FLAG_JUMBO;
4719 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4720 		} else {
4721 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4722 			bp->rx_skb_func = bnxt_rx_page_skb;
4723 		}
4724 		bp->rx_dir = DMA_BIDIRECTIONAL;
4725 		/* Disable LRO or GRO_HW */
4726 		netdev_update_features(dev);
4727 	} else {
4728 		dev->max_mtu = bp->max_mtu;
4729 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4730 		bp->rx_dir = DMA_FROM_DEVICE;
4731 		bp->rx_skb_func = bnxt_rx_skb;
4732 	}
4733 	return 0;
4734 }
4735 
4736 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4737 {
4738 	int i;
4739 	struct bnxt_vnic_info *vnic;
4740 	struct pci_dev *pdev = bp->pdev;
4741 
4742 	if (!bp->vnic_info)
4743 		return;
4744 
4745 	for (i = 0; i < bp->nr_vnics; i++) {
4746 		vnic = &bp->vnic_info[i];
4747 
4748 		kfree(vnic->fw_grp_ids);
4749 		vnic->fw_grp_ids = NULL;
4750 
4751 		kfree(vnic->uc_list);
4752 		vnic->uc_list = NULL;
4753 
4754 		if (vnic->mc_list) {
4755 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4756 					  vnic->mc_list, vnic->mc_list_mapping);
4757 			vnic->mc_list = NULL;
4758 		}
4759 
4760 		if (vnic->rss_table) {
4761 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4762 					  vnic->rss_table,
4763 					  vnic->rss_table_dma_addr);
4764 			vnic->rss_table = NULL;
4765 		}
4766 
4767 		vnic->rss_hash_key = NULL;
4768 		vnic->flags = 0;
4769 	}
4770 }
4771 
4772 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4773 {
4774 	int i, rc = 0, size;
4775 	struct bnxt_vnic_info *vnic;
4776 	struct pci_dev *pdev = bp->pdev;
4777 	int max_rings;
4778 
4779 	for (i = 0; i < bp->nr_vnics; i++) {
4780 		vnic = &bp->vnic_info[i];
4781 
4782 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4783 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4784 
4785 			if (mem_size > 0) {
4786 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4787 				if (!vnic->uc_list) {
4788 					rc = -ENOMEM;
4789 					goto out;
4790 				}
4791 			}
4792 		}
4793 
4794 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4795 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4796 			vnic->mc_list =
4797 				dma_alloc_coherent(&pdev->dev,
4798 						   vnic->mc_list_size,
4799 						   &vnic->mc_list_mapping,
4800 						   GFP_KERNEL);
4801 			if (!vnic->mc_list) {
4802 				rc = -ENOMEM;
4803 				goto out;
4804 			}
4805 		}
4806 
4807 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4808 			goto vnic_skip_grps;
4809 
4810 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4811 			max_rings = bp->rx_nr_rings;
4812 		else
4813 			max_rings = 1;
4814 
4815 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4816 		if (!vnic->fw_grp_ids) {
4817 			rc = -ENOMEM;
4818 			goto out;
4819 		}
4820 vnic_skip_grps:
4821 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4822 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4823 			continue;
4824 
4825 		/* Allocate rss table and hash key */
4826 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4827 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4828 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4829 
4830 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4831 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4832 						     vnic->rss_table_size,
4833 						     &vnic->rss_table_dma_addr,
4834 						     GFP_KERNEL);
4835 		if (!vnic->rss_table) {
4836 			rc = -ENOMEM;
4837 			goto out;
4838 		}
4839 
4840 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4841 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4842 	}
4843 	return 0;
4844 
4845 out:
4846 	return rc;
4847 }
4848 
4849 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4850 {
4851 	struct bnxt_hwrm_wait_token *token;
4852 
4853 	dma_pool_destroy(bp->hwrm_dma_pool);
4854 	bp->hwrm_dma_pool = NULL;
4855 
4856 	rcu_read_lock();
4857 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4858 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4859 	rcu_read_unlock();
4860 }
4861 
4862 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4863 {
4864 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4865 					    BNXT_HWRM_DMA_SIZE,
4866 					    BNXT_HWRM_DMA_ALIGN, 0);
4867 	if (!bp->hwrm_dma_pool)
4868 		return -ENOMEM;
4869 
4870 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4871 
4872 	return 0;
4873 }
4874 
4875 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4876 {
4877 	kfree(stats->hw_masks);
4878 	stats->hw_masks = NULL;
4879 	kfree(stats->sw_stats);
4880 	stats->sw_stats = NULL;
4881 	if (stats->hw_stats) {
4882 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4883 				  stats->hw_stats_map);
4884 		stats->hw_stats = NULL;
4885 	}
4886 }
4887 
4888 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4889 				bool alloc_masks)
4890 {
4891 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4892 					     &stats->hw_stats_map, GFP_KERNEL);
4893 	if (!stats->hw_stats)
4894 		return -ENOMEM;
4895 
4896 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4897 	if (!stats->sw_stats)
4898 		goto stats_mem_err;
4899 
4900 	if (alloc_masks) {
4901 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4902 		if (!stats->hw_masks)
4903 			goto stats_mem_err;
4904 	}
4905 	return 0;
4906 
4907 stats_mem_err:
4908 	bnxt_free_stats_mem(bp, stats);
4909 	return -ENOMEM;
4910 }
4911 
4912 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4913 {
4914 	int i;
4915 
4916 	for (i = 0; i < count; i++)
4917 		mask_arr[i] = mask;
4918 }
4919 
4920 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4921 {
4922 	int i;
4923 
4924 	for (i = 0; i < count; i++)
4925 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4926 }
4927 
4928 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4929 				    struct bnxt_stats_mem *stats)
4930 {
4931 	struct hwrm_func_qstats_ext_output *resp;
4932 	struct hwrm_func_qstats_ext_input *req;
4933 	__le64 *hw_masks;
4934 	int rc;
4935 
4936 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4937 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4938 		return -EOPNOTSUPP;
4939 
4940 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4941 	if (rc)
4942 		return rc;
4943 
4944 	req->fid = cpu_to_le16(0xffff);
4945 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4946 
4947 	resp = hwrm_req_hold(bp, req);
4948 	rc = hwrm_req_send(bp, req);
4949 	if (!rc) {
4950 		hw_masks = &resp->rx_ucast_pkts;
4951 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4952 	}
4953 	hwrm_req_drop(bp, req);
4954 	return rc;
4955 }
4956 
4957 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4958 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4959 
4960 static void bnxt_init_stats(struct bnxt *bp)
4961 {
4962 	struct bnxt_napi *bnapi = bp->bnapi[0];
4963 	struct bnxt_cp_ring_info *cpr;
4964 	struct bnxt_stats_mem *stats;
4965 	__le64 *rx_stats, *tx_stats;
4966 	int rc, rx_count, tx_count;
4967 	u64 *rx_masks, *tx_masks;
4968 	u64 mask;
4969 	u8 flags;
4970 
4971 	cpr = &bnapi->cp_ring;
4972 	stats = &cpr->stats;
4973 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4974 	if (rc) {
4975 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4976 			mask = (1ULL << 48) - 1;
4977 		else
4978 			mask = -1ULL;
4979 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
4980 	}
4981 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
4982 		stats = &bp->port_stats;
4983 		rx_stats = stats->hw_stats;
4984 		rx_masks = stats->hw_masks;
4985 		rx_count = sizeof(struct rx_port_stats) / 8;
4986 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4987 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4988 		tx_count = sizeof(struct tx_port_stats) / 8;
4989 
4990 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
4991 		rc = bnxt_hwrm_port_qstats(bp, flags);
4992 		if (rc) {
4993 			mask = (1ULL << 40) - 1;
4994 
4995 			bnxt_fill_masks(rx_masks, mask, rx_count);
4996 			bnxt_fill_masks(tx_masks, mask, tx_count);
4997 		} else {
4998 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4999 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5000 			bnxt_hwrm_port_qstats(bp, 0);
5001 		}
5002 	}
5003 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5004 		stats = &bp->rx_port_stats_ext;
5005 		rx_stats = stats->hw_stats;
5006 		rx_masks = stats->hw_masks;
5007 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5008 		stats = &bp->tx_port_stats_ext;
5009 		tx_stats = stats->hw_stats;
5010 		tx_masks = stats->hw_masks;
5011 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5012 
5013 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5014 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5015 		if (rc) {
5016 			mask = (1ULL << 40) - 1;
5017 
5018 			bnxt_fill_masks(rx_masks, mask, rx_count);
5019 			if (tx_stats)
5020 				bnxt_fill_masks(tx_masks, mask, tx_count);
5021 		} else {
5022 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5023 			if (tx_stats)
5024 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5025 						   tx_count);
5026 			bnxt_hwrm_port_qstats_ext(bp, 0);
5027 		}
5028 	}
5029 }
5030 
5031 static void bnxt_free_port_stats(struct bnxt *bp)
5032 {
5033 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5034 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5035 
5036 	bnxt_free_stats_mem(bp, &bp->port_stats);
5037 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5038 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5039 }
5040 
5041 static void bnxt_free_ring_stats(struct bnxt *bp)
5042 {
5043 	int i;
5044 
5045 	if (!bp->bnapi)
5046 		return;
5047 
5048 	for (i = 0; i < bp->cp_nr_rings; i++) {
5049 		struct bnxt_napi *bnapi = bp->bnapi[i];
5050 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5051 
5052 		bnxt_free_stats_mem(bp, &cpr->stats);
5053 
5054 		kfree(cpr->sw_stats);
5055 		cpr->sw_stats = NULL;
5056 	}
5057 }
5058 
5059 static int bnxt_alloc_stats(struct bnxt *bp)
5060 {
5061 	u32 size, i;
5062 	int rc;
5063 
5064 	size = bp->hw_ring_stats_size;
5065 
5066 	for (i = 0; i < bp->cp_nr_rings; i++) {
5067 		struct bnxt_napi *bnapi = bp->bnapi[i];
5068 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5069 
5070 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
5071 		if (!cpr->sw_stats)
5072 			return -ENOMEM;
5073 
5074 		cpr->stats.len = size;
5075 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5076 		if (rc)
5077 			return rc;
5078 
5079 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5080 	}
5081 
5082 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5083 		return 0;
5084 
5085 	if (bp->port_stats.hw_stats)
5086 		goto alloc_ext_stats;
5087 
5088 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5089 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5090 	if (rc)
5091 		return rc;
5092 
5093 	bp->flags |= BNXT_FLAG_PORT_STATS;
5094 
5095 alloc_ext_stats:
5096 	/* Display extended statistics only if FW supports it */
5097 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5098 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5099 			return 0;
5100 
5101 	if (bp->rx_port_stats_ext.hw_stats)
5102 		goto alloc_tx_ext_stats;
5103 
5104 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5105 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5106 	/* Extended stats are optional */
5107 	if (rc)
5108 		return 0;
5109 
5110 alloc_tx_ext_stats:
5111 	if (bp->tx_port_stats_ext.hw_stats)
5112 		return 0;
5113 
5114 	if (bp->hwrm_spec_code >= 0x10902 ||
5115 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5116 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5117 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5118 		/* Extended stats are optional */
5119 		if (rc)
5120 			return 0;
5121 	}
5122 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5123 	return 0;
5124 }
5125 
5126 static void bnxt_clear_ring_indices(struct bnxt *bp)
5127 {
5128 	int i, j;
5129 
5130 	if (!bp->bnapi)
5131 		return;
5132 
5133 	for (i = 0; i < bp->cp_nr_rings; i++) {
5134 		struct bnxt_napi *bnapi = bp->bnapi[i];
5135 		struct bnxt_cp_ring_info *cpr;
5136 		struct bnxt_rx_ring_info *rxr;
5137 		struct bnxt_tx_ring_info *txr;
5138 
5139 		if (!bnapi)
5140 			continue;
5141 
5142 		cpr = &bnapi->cp_ring;
5143 		cpr->cp_raw_cons = 0;
5144 
5145 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5146 			txr->tx_prod = 0;
5147 			txr->tx_cons = 0;
5148 			txr->tx_hw_cons = 0;
5149 		}
5150 
5151 		rxr = bnapi->rx_ring;
5152 		if (rxr) {
5153 			rxr->rx_prod = 0;
5154 			rxr->rx_agg_prod = 0;
5155 			rxr->rx_sw_agg_prod = 0;
5156 			rxr->rx_next_cons = 0;
5157 		}
5158 		bnapi->events = 0;
5159 	}
5160 }
5161 
5162 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5163 {
5164 	u8 type = fltr->type, flags = fltr->flags;
5165 
5166 	INIT_LIST_HEAD(&fltr->list);
5167 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5168 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5169 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5170 }
5171 
5172 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5173 {
5174 	if (!list_empty(&fltr->list))
5175 		list_del_init(&fltr->list);
5176 }
5177 
5178 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5179 {
5180 	struct bnxt_filter_base *usr_fltr, *tmp;
5181 
5182 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5183 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5184 			continue;
5185 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5186 	}
5187 }
5188 
5189 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5190 {
5191 	hlist_del(&fltr->hash);
5192 	bnxt_del_one_usr_fltr(bp, fltr);
5193 	if (fltr->flags) {
5194 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5195 		bp->ntp_fltr_count--;
5196 	}
5197 	kfree(fltr);
5198 }
5199 
5200 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5201 {
5202 	int i;
5203 
5204 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
5205 	 * safe to delete the hash table.
5206 	 */
5207 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5208 		struct hlist_head *head;
5209 		struct hlist_node *tmp;
5210 		struct bnxt_ntuple_filter *fltr;
5211 
5212 		head = &bp->ntp_fltr_hash_tbl[i];
5213 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5214 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5215 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5216 				     !list_empty(&fltr->base.list)))
5217 				continue;
5218 			bnxt_del_fltr(bp, &fltr->base);
5219 		}
5220 	}
5221 	if (!all)
5222 		return;
5223 
5224 	bitmap_free(bp->ntp_fltr_bmap);
5225 	bp->ntp_fltr_bmap = NULL;
5226 	bp->ntp_fltr_count = 0;
5227 }
5228 
5229 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5230 {
5231 	int i, rc = 0;
5232 
5233 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5234 		return 0;
5235 
5236 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5237 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5238 
5239 	bp->ntp_fltr_count = 0;
5240 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5241 
5242 	if (!bp->ntp_fltr_bmap)
5243 		rc = -ENOMEM;
5244 
5245 	return rc;
5246 }
5247 
5248 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5249 {
5250 	int i;
5251 
5252 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5253 		struct hlist_head *head;
5254 		struct hlist_node *tmp;
5255 		struct bnxt_l2_filter *fltr;
5256 
5257 		head = &bp->l2_fltr_hash_tbl[i];
5258 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5259 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5260 				     !list_empty(&fltr->base.list)))
5261 				continue;
5262 			bnxt_del_fltr(bp, &fltr->base);
5263 		}
5264 	}
5265 }
5266 
5267 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5268 {
5269 	int i;
5270 
5271 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5272 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5273 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5274 }
5275 
5276 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5277 {
5278 	bnxt_free_vnic_attributes(bp);
5279 	bnxt_free_tx_rings(bp);
5280 	bnxt_free_rx_rings(bp);
5281 	bnxt_free_cp_rings(bp);
5282 	bnxt_free_all_cp_arrays(bp);
5283 	bnxt_free_ntp_fltrs(bp, false);
5284 	bnxt_free_l2_filters(bp, false);
5285 	if (irq_re_init) {
5286 		bnxt_free_ring_stats(bp);
5287 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5288 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5289 			bnxt_free_port_stats(bp);
5290 		bnxt_free_ring_grps(bp);
5291 		bnxt_free_vnics(bp);
5292 		kfree(bp->tx_ring_map);
5293 		bp->tx_ring_map = NULL;
5294 		kfree(bp->tx_ring);
5295 		bp->tx_ring = NULL;
5296 		kfree(bp->rx_ring);
5297 		bp->rx_ring = NULL;
5298 		kfree(bp->bnapi);
5299 		bp->bnapi = NULL;
5300 	} else {
5301 		bnxt_clear_ring_indices(bp);
5302 	}
5303 }
5304 
5305 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5306 {
5307 	int i, j, rc, size, arr_size;
5308 	void *bnapi;
5309 
5310 	if (irq_re_init) {
5311 		/* Allocate bnapi mem pointer array and mem block for
5312 		 * all queues
5313 		 */
5314 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5315 				bp->cp_nr_rings);
5316 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5317 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5318 		if (!bnapi)
5319 			return -ENOMEM;
5320 
5321 		bp->bnapi = bnapi;
5322 		bnapi += arr_size;
5323 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5324 			bp->bnapi[i] = bnapi;
5325 			bp->bnapi[i]->index = i;
5326 			bp->bnapi[i]->bp = bp;
5327 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5328 				struct bnxt_cp_ring_info *cpr =
5329 					&bp->bnapi[i]->cp_ring;
5330 
5331 				cpr->cp_ring_struct.ring_mem.flags =
5332 					BNXT_RMEM_RING_PTE_FLAG;
5333 			}
5334 		}
5335 
5336 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5337 				      sizeof(struct bnxt_rx_ring_info),
5338 				      GFP_KERNEL);
5339 		if (!bp->rx_ring)
5340 			return -ENOMEM;
5341 
5342 		for (i = 0; i < bp->rx_nr_rings; i++) {
5343 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5344 
5345 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5346 				rxr->rx_ring_struct.ring_mem.flags =
5347 					BNXT_RMEM_RING_PTE_FLAG;
5348 				rxr->rx_agg_ring_struct.ring_mem.flags =
5349 					BNXT_RMEM_RING_PTE_FLAG;
5350 			} else {
5351 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5352 			}
5353 			rxr->bnapi = bp->bnapi[i];
5354 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5355 		}
5356 
5357 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5358 				      sizeof(struct bnxt_tx_ring_info),
5359 				      GFP_KERNEL);
5360 		if (!bp->tx_ring)
5361 			return -ENOMEM;
5362 
5363 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5364 					  GFP_KERNEL);
5365 
5366 		if (!bp->tx_ring_map)
5367 			return -ENOMEM;
5368 
5369 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5370 			j = 0;
5371 		else
5372 			j = bp->rx_nr_rings;
5373 
5374 		for (i = 0; i < bp->tx_nr_rings; i++) {
5375 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5376 			struct bnxt_napi *bnapi2;
5377 
5378 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5379 				txr->tx_ring_struct.ring_mem.flags =
5380 					BNXT_RMEM_RING_PTE_FLAG;
5381 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5382 			if (i >= bp->tx_nr_rings_xdp) {
5383 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5384 
5385 				bnapi2 = bp->bnapi[k];
5386 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5387 				txr->tx_napi_idx =
5388 					BNXT_RING_TO_TC(bp, txr->txq_index);
5389 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5390 				bnapi2->tx_int = bnxt_tx_int;
5391 			} else {
5392 				bnapi2 = bp->bnapi[j];
5393 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5394 				bnapi2->tx_ring[0] = txr;
5395 				bnapi2->tx_int = bnxt_tx_int_xdp;
5396 				j++;
5397 			}
5398 			txr->bnapi = bnapi2;
5399 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5400 				txr->tx_cpr = &bnapi2->cp_ring;
5401 		}
5402 
5403 		rc = bnxt_alloc_stats(bp);
5404 		if (rc)
5405 			goto alloc_mem_err;
5406 		bnxt_init_stats(bp);
5407 
5408 		rc = bnxt_alloc_ntp_fltrs(bp);
5409 		if (rc)
5410 			goto alloc_mem_err;
5411 
5412 		rc = bnxt_alloc_vnics(bp);
5413 		if (rc)
5414 			goto alloc_mem_err;
5415 	}
5416 
5417 	rc = bnxt_alloc_all_cp_arrays(bp);
5418 	if (rc)
5419 		goto alloc_mem_err;
5420 
5421 	bnxt_init_ring_struct(bp);
5422 
5423 	rc = bnxt_alloc_rx_rings(bp);
5424 	if (rc)
5425 		goto alloc_mem_err;
5426 
5427 	rc = bnxt_alloc_tx_rings(bp);
5428 	if (rc)
5429 		goto alloc_mem_err;
5430 
5431 	rc = bnxt_alloc_cp_rings(bp);
5432 	if (rc)
5433 		goto alloc_mem_err;
5434 
5435 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5436 						  BNXT_VNIC_MCAST_FLAG |
5437 						  BNXT_VNIC_UCAST_FLAG;
5438 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5439 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5440 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5441 
5442 	rc = bnxt_alloc_vnic_attributes(bp);
5443 	if (rc)
5444 		goto alloc_mem_err;
5445 	return 0;
5446 
5447 alloc_mem_err:
5448 	bnxt_free_mem(bp, true);
5449 	return rc;
5450 }
5451 
5452 static void bnxt_disable_int(struct bnxt *bp)
5453 {
5454 	int i;
5455 
5456 	if (!bp->bnapi)
5457 		return;
5458 
5459 	for (i = 0; i < bp->cp_nr_rings; i++) {
5460 		struct bnxt_napi *bnapi = bp->bnapi[i];
5461 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5462 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5463 
5464 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5465 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5466 	}
5467 }
5468 
5469 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5470 {
5471 	struct bnxt_napi *bnapi = bp->bnapi[n];
5472 	struct bnxt_cp_ring_info *cpr;
5473 
5474 	cpr = &bnapi->cp_ring;
5475 	return cpr->cp_ring_struct.map_idx;
5476 }
5477 
5478 static void bnxt_disable_int_sync(struct bnxt *bp)
5479 {
5480 	int i;
5481 
5482 	if (!bp->irq_tbl)
5483 		return;
5484 
5485 	atomic_inc(&bp->intr_sem);
5486 
5487 	bnxt_disable_int(bp);
5488 	for (i = 0; i < bp->cp_nr_rings; i++) {
5489 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5490 
5491 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5492 	}
5493 }
5494 
5495 static void bnxt_enable_int(struct bnxt *bp)
5496 {
5497 	int i;
5498 
5499 	atomic_set(&bp->intr_sem, 0);
5500 	for (i = 0; i < bp->cp_nr_rings; i++) {
5501 		struct bnxt_napi *bnapi = bp->bnapi[i];
5502 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5503 
5504 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5505 	}
5506 }
5507 
5508 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5509 			    bool async_only)
5510 {
5511 	DECLARE_BITMAP(async_events_bmap, 256);
5512 	u32 *events = (u32 *)async_events_bmap;
5513 	struct hwrm_func_drv_rgtr_output *resp;
5514 	struct hwrm_func_drv_rgtr_input *req;
5515 	u32 flags;
5516 	int rc, i;
5517 
5518 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5519 	if (rc)
5520 		return rc;
5521 
5522 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5523 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5524 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5525 
5526 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5527 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5528 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5529 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5530 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5531 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5532 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5533 	req->flags = cpu_to_le32(flags);
5534 	req->ver_maj_8b = DRV_VER_MAJ;
5535 	req->ver_min_8b = DRV_VER_MIN;
5536 	req->ver_upd_8b = DRV_VER_UPD;
5537 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5538 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5539 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5540 
5541 	if (BNXT_PF(bp)) {
5542 		u32 data[8];
5543 		int i;
5544 
5545 		memset(data, 0, sizeof(data));
5546 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5547 			u16 cmd = bnxt_vf_req_snif[i];
5548 			unsigned int bit, idx;
5549 
5550 			idx = cmd / 32;
5551 			bit = cmd % 32;
5552 			data[idx] |= 1 << bit;
5553 		}
5554 
5555 		for (i = 0; i < 8; i++)
5556 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5557 
5558 		req->enables |=
5559 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5560 	}
5561 
5562 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5563 		req->flags |= cpu_to_le32(
5564 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5565 
5566 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5567 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5568 		u16 event_id = bnxt_async_events_arr[i];
5569 
5570 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5571 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5572 			continue;
5573 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5574 		    !bp->ptp_cfg)
5575 			continue;
5576 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5577 	}
5578 	if (bmap && bmap_size) {
5579 		for (i = 0; i < bmap_size; i++) {
5580 			if (test_bit(i, bmap))
5581 				__set_bit(i, async_events_bmap);
5582 		}
5583 	}
5584 	for (i = 0; i < 8; i++)
5585 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5586 
5587 	if (async_only)
5588 		req->enables =
5589 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5590 
5591 	resp = hwrm_req_hold(bp, req);
5592 	rc = hwrm_req_send(bp, req);
5593 	if (!rc) {
5594 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5595 		if (resp->flags &
5596 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5597 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5598 	}
5599 	hwrm_req_drop(bp, req);
5600 	return rc;
5601 }
5602 
5603 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5604 {
5605 	struct hwrm_func_drv_unrgtr_input *req;
5606 	int rc;
5607 
5608 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5609 		return 0;
5610 
5611 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5612 	if (rc)
5613 		return rc;
5614 	return hwrm_req_send(bp, req);
5615 }
5616 
5617 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5618 
5619 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5620 {
5621 	struct hwrm_tunnel_dst_port_free_input *req;
5622 	int rc;
5623 
5624 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5625 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5626 		return 0;
5627 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5628 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5629 		return 0;
5630 
5631 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5632 	if (rc)
5633 		return rc;
5634 
5635 	req->tunnel_type = tunnel_type;
5636 
5637 	switch (tunnel_type) {
5638 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5639 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5640 		bp->vxlan_port = 0;
5641 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5642 		break;
5643 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5644 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5645 		bp->nge_port = 0;
5646 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5647 		break;
5648 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5649 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5650 		bp->vxlan_gpe_port = 0;
5651 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5652 		break;
5653 	default:
5654 		break;
5655 	}
5656 
5657 	rc = hwrm_req_send(bp, req);
5658 	if (rc)
5659 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5660 			   rc);
5661 	if (bp->flags & BNXT_FLAG_TPA)
5662 		bnxt_set_tpa(bp, true);
5663 	return rc;
5664 }
5665 
5666 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5667 					   u8 tunnel_type)
5668 {
5669 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5670 	struct hwrm_tunnel_dst_port_alloc_input *req;
5671 	int rc;
5672 
5673 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5674 	if (rc)
5675 		return rc;
5676 
5677 	req->tunnel_type = tunnel_type;
5678 	req->tunnel_dst_port_val = port;
5679 
5680 	resp = hwrm_req_hold(bp, req);
5681 	rc = hwrm_req_send(bp, req);
5682 	if (rc) {
5683 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5684 			   rc);
5685 		goto err_out;
5686 	}
5687 
5688 	switch (tunnel_type) {
5689 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5690 		bp->vxlan_port = port;
5691 		bp->vxlan_fw_dst_port_id =
5692 			le16_to_cpu(resp->tunnel_dst_port_id);
5693 		break;
5694 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5695 		bp->nge_port = port;
5696 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5697 		break;
5698 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5699 		bp->vxlan_gpe_port = port;
5700 		bp->vxlan_gpe_fw_dst_port_id =
5701 			le16_to_cpu(resp->tunnel_dst_port_id);
5702 		break;
5703 	default:
5704 		break;
5705 	}
5706 	if (bp->flags & BNXT_FLAG_TPA)
5707 		bnxt_set_tpa(bp, true);
5708 
5709 err_out:
5710 	hwrm_req_drop(bp, req);
5711 	return rc;
5712 }
5713 
5714 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5715 {
5716 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5717 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5718 	int rc;
5719 
5720 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5721 	if (rc)
5722 		return rc;
5723 
5724 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5725 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5726 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5727 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5728 	}
5729 	req->mask = cpu_to_le32(vnic->rx_mask);
5730 	return hwrm_req_send_silent(bp, req);
5731 }
5732 
5733 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5734 {
5735 	if (!atomic_dec_and_test(&fltr->refcnt))
5736 		return;
5737 	spin_lock_bh(&bp->ntp_fltr_lock);
5738 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5739 		spin_unlock_bh(&bp->ntp_fltr_lock);
5740 		return;
5741 	}
5742 	hlist_del_rcu(&fltr->base.hash);
5743 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5744 	if (fltr->base.flags) {
5745 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5746 		bp->ntp_fltr_count--;
5747 	}
5748 	spin_unlock_bh(&bp->ntp_fltr_lock);
5749 	kfree_rcu(fltr, base.rcu);
5750 }
5751 
5752 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5753 						      struct bnxt_l2_key *key,
5754 						      u32 idx)
5755 {
5756 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5757 	struct bnxt_l2_filter *fltr;
5758 
5759 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5760 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5761 
5762 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5763 		    l2_key->vlan == key->vlan)
5764 			return fltr;
5765 	}
5766 	return NULL;
5767 }
5768 
5769 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5770 						    struct bnxt_l2_key *key,
5771 						    u32 idx)
5772 {
5773 	struct bnxt_l2_filter *fltr = NULL;
5774 
5775 	rcu_read_lock();
5776 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5777 	if (fltr)
5778 		atomic_inc(&fltr->refcnt);
5779 	rcu_read_unlock();
5780 	return fltr;
5781 }
5782 
5783 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5784 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5785 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5786 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5787 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5788 
5789 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5790 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5791 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5792 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5793 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5794 
5795 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5796 {
5797 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5798 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5799 			return sizeof(fkeys->addrs.v4addrs) +
5800 			       sizeof(fkeys->ports);
5801 
5802 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5803 			return sizeof(fkeys->addrs.v4addrs);
5804 	}
5805 
5806 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5807 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5808 			return sizeof(fkeys->addrs.v6addrs) +
5809 			       sizeof(fkeys->ports);
5810 
5811 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5812 			return sizeof(fkeys->addrs.v6addrs);
5813 	}
5814 
5815 	return 0;
5816 }
5817 
5818 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5819 			 const unsigned char *key)
5820 {
5821 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5822 	struct bnxt_ipv4_tuple tuple4;
5823 	struct bnxt_ipv6_tuple tuple6;
5824 	int i, j, len = 0;
5825 	u8 *four_tuple;
5826 
5827 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5828 	if (!len)
5829 		return 0;
5830 
5831 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5832 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5833 		tuple4.ports = fkeys->ports;
5834 		four_tuple = (unsigned char *)&tuple4;
5835 	} else {
5836 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5837 		tuple6.ports = fkeys->ports;
5838 		four_tuple = (unsigned char *)&tuple6;
5839 	}
5840 
5841 	for (i = 0, j = 8; i < len; i++, j++) {
5842 		u8 byte = four_tuple[i];
5843 		int bit;
5844 
5845 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5846 			if (byte & 0x80)
5847 				hash ^= prefix;
5848 		}
5849 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5850 	}
5851 
5852 	/* The valid part of the hash is in the upper 32 bits. */
5853 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5854 }
5855 
5856 #ifdef CONFIG_RFS_ACCEL
5857 static struct bnxt_l2_filter *
5858 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5859 {
5860 	struct bnxt_l2_filter *fltr;
5861 	u32 idx;
5862 
5863 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5864 	      BNXT_L2_FLTR_HASH_MASK;
5865 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5866 	return fltr;
5867 }
5868 #endif
5869 
5870 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5871 			       struct bnxt_l2_key *key, u32 idx)
5872 {
5873 	struct hlist_head *head;
5874 
5875 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5876 	fltr->l2_key.vlan = key->vlan;
5877 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5878 	if (fltr->base.flags) {
5879 		int bit_id;
5880 
5881 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5882 						 bp->max_fltr, 0);
5883 		if (bit_id < 0)
5884 			return -ENOMEM;
5885 		fltr->base.sw_id = (u16)bit_id;
5886 		bp->ntp_fltr_count++;
5887 	}
5888 	head = &bp->l2_fltr_hash_tbl[idx];
5889 	hlist_add_head_rcu(&fltr->base.hash, head);
5890 	bnxt_insert_usr_fltr(bp, &fltr->base);
5891 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5892 	atomic_set(&fltr->refcnt, 1);
5893 	return 0;
5894 }
5895 
5896 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
5897 						   struct bnxt_l2_key *key,
5898 						   gfp_t gfp)
5899 {
5900 	struct bnxt_l2_filter *fltr;
5901 	u32 idx;
5902 	int rc;
5903 
5904 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5905 	      BNXT_L2_FLTR_HASH_MASK;
5906 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5907 	if (fltr)
5908 		return fltr;
5909 
5910 	fltr = kzalloc(sizeof(*fltr), gfp);
5911 	if (!fltr)
5912 		return ERR_PTR(-ENOMEM);
5913 	spin_lock_bh(&bp->ntp_fltr_lock);
5914 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5915 	spin_unlock_bh(&bp->ntp_fltr_lock);
5916 	if (rc) {
5917 		bnxt_del_l2_filter(bp, fltr);
5918 		fltr = ERR_PTR(rc);
5919 	}
5920 	return fltr;
5921 }
5922 
5923 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
5924 						struct bnxt_l2_key *key,
5925 						u16 flags)
5926 {
5927 	struct bnxt_l2_filter *fltr;
5928 	u32 idx;
5929 	int rc;
5930 
5931 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5932 	      BNXT_L2_FLTR_HASH_MASK;
5933 	spin_lock_bh(&bp->ntp_fltr_lock);
5934 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5935 	if (fltr) {
5936 		fltr = ERR_PTR(-EEXIST);
5937 		goto l2_filter_exit;
5938 	}
5939 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
5940 	if (!fltr) {
5941 		fltr = ERR_PTR(-ENOMEM);
5942 		goto l2_filter_exit;
5943 	}
5944 	fltr->base.flags = flags;
5945 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5946 	if (rc) {
5947 		spin_unlock_bh(&bp->ntp_fltr_lock);
5948 		bnxt_del_l2_filter(bp, fltr);
5949 		return ERR_PTR(rc);
5950 	}
5951 
5952 l2_filter_exit:
5953 	spin_unlock_bh(&bp->ntp_fltr_lock);
5954 	return fltr;
5955 }
5956 
5957 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
5958 {
5959 #ifdef CONFIG_BNXT_SRIOV
5960 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
5961 
5962 	return vf->fw_fid;
5963 #else
5964 	return INVALID_HW_RING_ID;
5965 #endif
5966 }
5967 
5968 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5969 {
5970 	struct hwrm_cfa_l2_filter_free_input *req;
5971 	u16 target_id = 0xffff;
5972 	int rc;
5973 
5974 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5975 		struct bnxt_pf_info *pf = &bp->pf;
5976 
5977 		if (fltr->base.vf_idx >= pf->active_vfs)
5978 			return -EINVAL;
5979 
5980 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5981 		if (target_id == INVALID_HW_RING_ID)
5982 			return -EINVAL;
5983 	}
5984 
5985 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
5986 	if (rc)
5987 		return rc;
5988 
5989 	req->target_id = cpu_to_le16(target_id);
5990 	req->l2_filter_id = fltr->base.filter_id;
5991 	return hwrm_req_send(bp, req);
5992 }
5993 
5994 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5995 {
5996 	struct hwrm_cfa_l2_filter_alloc_output *resp;
5997 	struct hwrm_cfa_l2_filter_alloc_input *req;
5998 	u16 target_id = 0xffff;
5999 	int rc;
6000 
6001 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6002 		struct bnxt_pf_info *pf = &bp->pf;
6003 
6004 		if (fltr->base.vf_idx >= pf->active_vfs)
6005 			return -EINVAL;
6006 
6007 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6008 	}
6009 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6010 	if (rc)
6011 		return rc;
6012 
6013 	req->target_id = cpu_to_le16(target_id);
6014 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6015 
6016 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6017 		req->flags |=
6018 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6019 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6020 	req->enables =
6021 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6022 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6023 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6024 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6025 	eth_broadcast_addr(req->l2_addr_mask);
6026 
6027 	if (fltr->l2_key.vlan) {
6028 		req->enables |=
6029 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6030 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6031 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6032 		req->num_vlans = 1;
6033 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6034 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6035 	}
6036 
6037 	resp = hwrm_req_hold(bp, req);
6038 	rc = hwrm_req_send(bp, req);
6039 	if (!rc) {
6040 		fltr->base.filter_id = resp->l2_filter_id;
6041 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6042 	}
6043 	hwrm_req_drop(bp, req);
6044 	return rc;
6045 }
6046 
6047 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6048 				     struct bnxt_ntuple_filter *fltr)
6049 {
6050 	struct hwrm_cfa_ntuple_filter_free_input *req;
6051 	int rc;
6052 
6053 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6054 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6055 	if (rc)
6056 		return rc;
6057 
6058 	req->ntuple_filter_id = fltr->base.filter_id;
6059 	return hwrm_req_send(bp, req);
6060 }
6061 
6062 #define BNXT_NTP_FLTR_FLAGS					\
6063 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6064 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6065 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6066 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6067 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6068 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6069 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6070 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6071 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6072 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6073 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6074 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6075 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6076 
6077 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6078 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6079 
6080 void bnxt_fill_ipv6_mask(__be32 mask[4])
6081 {
6082 	int i;
6083 
6084 	for (i = 0; i < 4; i++)
6085 		mask[i] = cpu_to_be32(~0);
6086 }
6087 
6088 static void
6089 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6090 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6091 			  struct bnxt_ntuple_filter *fltr)
6092 {
6093 	u16 rxq = fltr->base.rxq;
6094 
6095 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6096 		struct ethtool_rxfh_context *ctx;
6097 		struct bnxt_rss_ctx *rss_ctx;
6098 		struct bnxt_vnic_info *vnic;
6099 
6100 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6101 			      fltr->base.fw_vnic_id);
6102 		if (ctx) {
6103 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6104 			vnic = &rss_ctx->vnic;
6105 
6106 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6107 		}
6108 		return;
6109 	}
6110 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6111 		struct bnxt_vnic_info *vnic;
6112 		u32 enables;
6113 
6114 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6115 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6116 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6117 		req->enables |= cpu_to_le32(enables);
6118 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6119 	} else {
6120 		u32 flags;
6121 
6122 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6123 		req->flags |= cpu_to_le32(flags);
6124 		req->dst_id = cpu_to_le16(rxq);
6125 	}
6126 }
6127 
6128 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6129 				      struct bnxt_ntuple_filter *fltr)
6130 {
6131 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6132 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6133 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6134 	struct flow_keys *keys = &fltr->fkeys;
6135 	struct bnxt_l2_filter *l2_fltr;
6136 	struct bnxt_vnic_info *vnic;
6137 	int rc;
6138 
6139 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6140 	if (rc)
6141 		return rc;
6142 
6143 	l2_fltr = fltr->l2_fltr;
6144 	req->l2_filter_id = l2_fltr->base.filter_id;
6145 
6146 	if (fltr->base.flags & BNXT_ACT_DROP) {
6147 		req->flags =
6148 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6149 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6150 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6151 	} else {
6152 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6153 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6154 	}
6155 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6156 
6157 	req->ethertype = htons(ETH_P_IP);
6158 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6159 	req->ip_protocol = keys->basic.ip_proto;
6160 
6161 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6162 		req->ethertype = htons(ETH_P_IPV6);
6163 		req->ip_addr_type =
6164 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6165 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6166 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6167 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6168 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6169 	} else {
6170 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6171 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6172 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6173 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6174 	}
6175 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6176 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6177 		req->tunnel_type =
6178 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6179 	}
6180 
6181 	req->src_port = keys->ports.src;
6182 	req->src_port_mask = masks->ports.src;
6183 	req->dst_port = keys->ports.dst;
6184 	req->dst_port_mask = masks->ports.dst;
6185 
6186 	resp = hwrm_req_hold(bp, req);
6187 	rc = hwrm_req_send(bp, req);
6188 	if (!rc)
6189 		fltr->base.filter_id = resp->ntuple_filter_id;
6190 	hwrm_req_drop(bp, req);
6191 	return rc;
6192 }
6193 
6194 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6195 				     const u8 *mac_addr)
6196 {
6197 	struct bnxt_l2_filter *fltr;
6198 	struct bnxt_l2_key key;
6199 	int rc;
6200 
6201 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6202 	key.vlan = 0;
6203 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6204 	if (IS_ERR(fltr))
6205 		return PTR_ERR(fltr);
6206 
6207 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6208 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6209 	if (rc)
6210 		bnxt_del_l2_filter(bp, fltr);
6211 	else
6212 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6213 	return rc;
6214 }
6215 
6216 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6217 {
6218 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6219 
6220 	/* Any associated ntuple filters will also be cleared by firmware. */
6221 	for (i = 0; i < num_of_vnics; i++) {
6222 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6223 
6224 		for (j = 0; j < vnic->uc_filter_count; j++) {
6225 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6226 
6227 			bnxt_hwrm_l2_filter_free(bp, fltr);
6228 			bnxt_del_l2_filter(bp, fltr);
6229 		}
6230 		vnic->uc_filter_count = 0;
6231 	}
6232 }
6233 
6234 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6235 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6236 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6237 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6238 
6239 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6240 					   struct hwrm_vnic_tpa_cfg_input *req)
6241 {
6242 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6243 
6244 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6245 		return;
6246 
6247 	if (bp->vxlan_port)
6248 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6249 	if (bp->vxlan_gpe_port)
6250 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6251 	if (bp->nge_port)
6252 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6253 
6254 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6255 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6256 }
6257 
6258 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6259 			   u32 tpa_flags)
6260 {
6261 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6262 	struct hwrm_vnic_tpa_cfg_input *req;
6263 	int rc;
6264 
6265 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6266 		return 0;
6267 
6268 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6269 	if (rc)
6270 		return rc;
6271 
6272 	if (tpa_flags) {
6273 		u16 mss = bp->dev->mtu - 40;
6274 		u32 nsegs, n, segs = 0, flags;
6275 
6276 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6277 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6278 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6279 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6280 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6281 		if (tpa_flags & BNXT_FLAG_GRO)
6282 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6283 
6284 		req->flags = cpu_to_le32(flags);
6285 
6286 		req->enables =
6287 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6288 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6289 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6290 
6291 		/* Number of segs are log2 units, and first packet is not
6292 		 * included as part of this units.
6293 		 */
6294 		if (mss <= BNXT_RX_PAGE_SIZE) {
6295 			n = BNXT_RX_PAGE_SIZE / mss;
6296 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6297 		} else {
6298 			n = mss / BNXT_RX_PAGE_SIZE;
6299 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6300 				n++;
6301 			nsegs = (MAX_SKB_FRAGS - n) / n;
6302 		}
6303 
6304 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6305 			segs = MAX_TPA_SEGS_P5;
6306 			max_aggs = bp->max_tpa;
6307 		} else {
6308 			segs = ilog2(nsegs);
6309 		}
6310 		req->max_agg_segs = cpu_to_le16(segs);
6311 		req->max_aggs = cpu_to_le16(max_aggs);
6312 
6313 		req->min_agg_len = cpu_to_le32(512);
6314 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6315 	}
6316 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6317 
6318 	return hwrm_req_send(bp, req);
6319 }
6320 
6321 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6322 {
6323 	struct bnxt_ring_grp_info *grp_info;
6324 
6325 	grp_info = &bp->grp_info[ring->grp_idx];
6326 	return grp_info->cp_fw_ring_id;
6327 }
6328 
6329 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6330 {
6331 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6332 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6333 	else
6334 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6335 }
6336 
6337 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6338 {
6339 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6340 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6341 	else
6342 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6343 }
6344 
6345 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6346 {
6347 	int entries;
6348 
6349 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6350 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6351 	else
6352 		entries = HW_HASH_INDEX_SIZE;
6353 
6354 	bp->rss_indir_tbl_entries = entries;
6355 	bp->rss_indir_tbl =
6356 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6357 	if (!bp->rss_indir_tbl)
6358 		return -ENOMEM;
6359 
6360 	return 0;
6361 }
6362 
6363 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6364 				 struct ethtool_rxfh_context *rss_ctx)
6365 {
6366 	u16 max_rings, max_entries, pad, i;
6367 	u32 *rss_indir_tbl;
6368 
6369 	if (!bp->rx_nr_rings)
6370 		return;
6371 
6372 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6373 		max_rings = bp->rx_nr_rings - 1;
6374 	else
6375 		max_rings = bp->rx_nr_rings;
6376 
6377 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6378 	if (rss_ctx)
6379 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6380 	else
6381 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6382 
6383 	for (i = 0; i < max_entries; i++)
6384 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6385 
6386 	pad = bp->rss_indir_tbl_entries - max_entries;
6387 	if (pad)
6388 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6389 }
6390 
6391 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6392 {
6393 	u32 i, tbl_size, max_ring = 0;
6394 
6395 	if (!bp->rss_indir_tbl)
6396 		return 0;
6397 
6398 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6399 	for (i = 0; i < tbl_size; i++)
6400 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6401 	return max_ring;
6402 }
6403 
6404 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6405 {
6406 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6407 		if (!rx_rings)
6408 			return 0;
6409 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6410 					       BNXT_RSS_TABLE_ENTRIES_P5);
6411 	}
6412 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6413 		return 2;
6414 	return 1;
6415 }
6416 
6417 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6418 {
6419 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6420 	u16 i, j;
6421 
6422 	/* Fill the RSS indirection table with ring group ids */
6423 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6424 		if (!no_rss)
6425 			j = bp->rss_indir_tbl[i];
6426 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6427 	}
6428 }
6429 
6430 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6431 				    struct bnxt_vnic_info *vnic)
6432 {
6433 	__le16 *ring_tbl = vnic->rss_table;
6434 	struct bnxt_rx_ring_info *rxr;
6435 	u16 tbl_size, i;
6436 
6437 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6438 
6439 	for (i = 0; i < tbl_size; i++) {
6440 		u16 ring_id, j;
6441 
6442 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6443 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6444 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6445 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6446 		else
6447 			j = bp->rss_indir_tbl[i];
6448 		rxr = &bp->rx_ring[j];
6449 
6450 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6451 		*ring_tbl++ = cpu_to_le16(ring_id);
6452 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6453 		*ring_tbl++ = cpu_to_le16(ring_id);
6454 	}
6455 }
6456 
6457 static void
6458 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6459 			 struct bnxt_vnic_info *vnic)
6460 {
6461 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6462 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6463 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6464 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6465 	} else {
6466 		bnxt_fill_hw_rss_tbl(bp, vnic);
6467 	}
6468 
6469 	if (bp->rss_hash_delta) {
6470 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6471 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6472 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6473 		else
6474 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6475 	} else {
6476 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6477 	}
6478 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6479 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6480 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6481 }
6482 
6483 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6484 				  bool set_rss)
6485 {
6486 	struct hwrm_vnic_rss_cfg_input *req;
6487 	int rc;
6488 
6489 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6490 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6491 		return 0;
6492 
6493 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6494 	if (rc)
6495 		return rc;
6496 
6497 	if (set_rss)
6498 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6499 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6500 	return hwrm_req_send(bp, req);
6501 }
6502 
6503 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6504 				     struct bnxt_vnic_info *vnic, bool set_rss)
6505 {
6506 	struct hwrm_vnic_rss_cfg_input *req;
6507 	dma_addr_t ring_tbl_map;
6508 	u32 i, nr_ctxs;
6509 	int rc;
6510 
6511 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6512 	if (rc)
6513 		return rc;
6514 
6515 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6516 	if (!set_rss)
6517 		return hwrm_req_send(bp, req);
6518 
6519 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6520 	ring_tbl_map = vnic->rss_table_dma_addr;
6521 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6522 
6523 	hwrm_req_hold(bp, req);
6524 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6525 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6526 		req->ring_table_pair_index = i;
6527 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6528 		rc = hwrm_req_send(bp, req);
6529 		if (rc)
6530 			goto exit;
6531 	}
6532 
6533 exit:
6534 	hwrm_req_drop(bp, req);
6535 	return rc;
6536 }
6537 
6538 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6539 {
6540 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6541 	struct hwrm_vnic_rss_qcfg_output *resp;
6542 	struct hwrm_vnic_rss_qcfg_input *req;
6543 
6544 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6545 		return;
6546 
6547 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6548 	/* all contexts configured to same hash_type, zero always exists */
6549 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6550 	resp = hwrm_req_hold(bp, req);
6551 	if (!hwrm_req_send(bp, req)) {
6552 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6553 		bp->rss_hash_delta = 0;
6554 	}
6555 	hwrm_req_drop(bp, req);
6556 }
6557 
6558 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6559 {
6560 	struct hwrm_vnic_plcmodes_cfg_input *req;
6561 	int rc;
6562 
6563 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6564 	if (rc)
6565 		return rc;
6566 
6567 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6568 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6569 
6570 	if (BNXT_RX_PAGE_MODE(bp)) {
6571 		req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6572 	} else {
6573 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6574 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6575 		req->enables |=
6576 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6577 		req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
6578 		req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
6579 	}
6580 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6581 	return hwrm_req_send(bp, req);
6582 }
6583 
6584 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6585 					struct bnxt_vnic_info *vnic,
6586 					u16 ctx_idx)
6587 {
6588 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6589 
6590 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6591 		return;
6592 
6593 	req->rss_cos_lb_ctx_id =
6594 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6595 
6596 	hwrm_req_send(bp, req);
6597 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6598 }
6599 
6600 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6601 {
6602 	int i, j;
6603 
6604 	for (i = 0; i < bp->nr_vnics; i++) {
6605 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6606 
6607 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6608 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6609 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6610 		}
6611 	}
6612 	bp->rsscos_nr_ctxs = 0;
6613 }
6614 
6615 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6616 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6617 {
6618 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6619 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6620 	int rc;
6621 
6622 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6623 	if (rc)
6624 		return rc;
6625 
6626 	resp = hwrm_req_hold(bp, req);
6627 	rc = hwrm_req_send(bp, req);
6628 	if (!rc)
6629 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6630 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6631 	hwrm_req_drop(bp, req);
6632 
6633 	return rc;
6634 }
6635 
6636 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6637 {
6638 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6639 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6640 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6641 }
6642 
6643 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6644 {
6645 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6646 	struct hwrm_vnic_cfg_input *req;
6647 	unsigned int ring = 0, grp_idx;
6648 	u16 def_vlan = 0;
6649 	int rc;
6650 
6651 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6652 	if (rc)
6653 		return rc;
6654 
6655 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6656 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6657 
6658 		req->default_rx_ring_id =
6659 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6660 		req->default_cmpl_ring_id =
6661 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6662 		req->enables =
6663 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6664 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6665 		goto vnic_mru;
6666 	}
6667 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6668 	/* Only RSS support for now TBD: COS & LB */
6669 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6670 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6671 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6672 					   VNIC_CFG_REQ_ENABLES_MRU);
6673 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6674 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6675 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6676 					   VNIC_CFG_REQ_ENABLES_MRU);
6677 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6678 	} else {
6679 		req->rss_rule = cpu_to_le16(0xffff);
6680 	}
6681 
6682 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6683 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6684 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6685 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6686 	} else {
6687 		req->cos_rule = cpu_to_le16(0xffff);
6688 	}
6689 
6690 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6691 		ring = 0;
6692 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6693 		ring = vnic->vnic_id - 1;
6694 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6695 		ring = bp->rx_nr_rings - 1;
6696 
6697 	grp_idx = bp->rx_ring[ring].bnapi->index;
6698 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6699 	req->lb_rule = cpu_to_le16(0xffff);
6700 vnic_mru:
6701 	vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
6702 	req->mru = cpu_to_le16(vnic->mru);
6703 
6704 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6705 #ifdef CONFIG_BNXT_SRIOV
6706 	if (BNXT_VF(bp))
6707 		def_vlan = bp->vf.vlan;
6708 #endif
6709 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6710 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6711 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6712 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6713 
6714 	return hwrm_req_send(bp, req);
6715 }
6716 
6717 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6718 				    struct bnxt_vnic_info *vnic)
6719 {
6720 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6721 		struct hwrm_vnic_free_input *req;
6722 
6723 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6724 			return;
6725 
6726 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6727 
6728 		hwrm_req_send(bp, req);
6729 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6730 	}
6731 }
6732 
6733 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6734 {
6735 	u16 i;
6736 
6737 	for (i = 0; i < bp->nr_vnics; i++)
6738 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6739 }
6740 
6741 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6742 			 unsigned int start_rx_ring_idx,
6743 			 unsigned int nr_rings)
6744 {
6745 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6746 	struct hwrm_vnic_alloc_output *resp;
6747 	struct hwrm_vnic_alloc_input *req;
6748 	int rc;
6749 
6750 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6751 	if (rc)
6752 		return rc;
6753 
6754 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6755 		goto vnic_no_ring_grps;
6756 
6757 	/* map ring groups to this vnic */
6758 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6759 		grp_idx = bp->rx_ring[i].bnapi->index;
6760 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6761 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6762 				   j, nr_rings);
6763 			break;
6764 		}
6765 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6766 	}
6767 
6768 vnic_no_ring_grps:
6769 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6770 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6771 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6772 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6773 
6774 	resp = hwrm_req_hold(bp, req);
6775 	rc = hwrm_req_send(bp, req);
6776 	if (!rc)
6777 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6778 	hwrm_req_drop(bp, req);
6779 	return rc;
6780 }
6781 
6782 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6783 {
6784 	struct hwrm_vnic_qcaps_output *resp;
6785 	struct hwrm_vnic_qcaps_input *req;
6786 	int rc;
6787 
6788 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6789 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6790 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6791 	if (bp->hwrm_spec_code < 0x10600)
6792 		return 0;
6793 
6794 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6795 	if (rc)
6796 		return rc;
6797 
6798 	resp = hwrm_req_hold(bp, req);
6799 	rc = hwrm_req_send(bp, req);
6800 	if (!rc) {
6801 		u32 flags = le32_to_cpu(resp->flags);
6802 
6803 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6804 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6805 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6806 		if (flags &
6807 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6808 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6809 
6810 		/* Older P5 fw before EXT_HW_STATS support did not set
6811 		 * VLAN_STRIP_CAP properly.
6812 		 */
6813 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6814 		    (BNXT_CHIP_P5(bp) &&
6815 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6816 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6817 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6818 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6819 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6820 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6821 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6822 		if (bp->max_tpa_v2) {
6823 			if (BNXT_CHIP_P5(bp))
6824 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6825 			else
6826 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6827 		}
6828 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6829 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6830 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6831 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6832 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6833 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6834 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6835 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6836 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6837 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6838 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6839 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6840 	}
6841 	hwrm_req_drop(bp, req);
6842 	return rc;
6843 }
6844 
6845 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6846 {
6847 	struct hwrm_ring_grp_alloc_output *resp;
6848 	struct hwrm_ring_grp_alloc_input *req;
6849 	int rc;
6850 	u16 i;
6851 
6852 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6853 		return 0;
6854 
6855 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6856 	if (rc)
6857 		return rc;
6858 
6859 	resp = hwrm_req_hold(bp, req);
6860 	for (i = 0; i < bp->rx_nr_rings; i++) {
6861 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6862 
6863 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6864 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6865 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6866 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6867 
6868 		rc = hwrm_req_send(bp, req);
6869 
6870 		if (rc)
6871 			break;
6872 
6873 		bp->grp_info[grp_idx].fw_grp_id =
6874 			le32_to_cpu(resp->ring_group_id);
6875 	}
6876 	hwrm_req_drop(bp, req);
6877 	return rc;
6878 }
6879 
6880 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6881 {
6882 	struct hwrm_ring_grp_free_input *req;
6883 	u16 i;
6884 
6885 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6886 		return;
6887 
6888 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6889 		return;
6890 
6891 	hwrm_req_hold(bp, req);
6892 	for (i = 0; i < bp->cp_nr_rings; i++) {
6893 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
6894 			continue;
6895 		req->ring_group_id =
6896 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
6897 
6898 		hwrm_req_send(bp, req);
6899 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
6900 	}
6901 	hwrm_req_drop(bp, req);
6902 }
6903 
6904 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
6905 				    struct bnxt_ring_struct *ring,
6906 				    u32 ring_type, u32 map_index)
6907 {
6908 	struct hwrm_ring_alloc_output *resp;
6909 	struct hwrm_ring_alloc_input *req;
6910 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
6911 	struct bnxt_ring_grp_info *grp_info;
6912 	int rc, err = 0;
6913 	u16 ring_id;
6914 
6915 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
6916 	if (rc)
6917 		goto exit;
6918 
6919 	req->enables = 0;
6920 	if (rmem->nr_pages > 1) {
6921 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
6922 		/* Page size is in log2 units */
6923 		req->page_size = BNXT_PAGE_SHIFT;
6924 		req->page_tbl_depth = 1;
6925 	} else {
6926 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
6927 	}
6928 	req->fbo = 0;
6929 	/* Association of ring index with doorbell index and MSIX number */
6930 	req->logical_id = cpu_to_le16(map_index);
6931 
6932 	switch (ring_type) {
6933 	case HWRM_RING_ALLOC_TX: {
6934 		struct bnxt_tx_ring_info *txr;
6935 		u16 flags = 0;
6936 
6937 		txr = container_of(ring, struct bnxt_tx_ring_info,
6938 				   tx_ring_struct);
6939 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
6940 		/* Association of transmit ring with completion ring */
6941 		grp_info = &bp->grp_info[ring->grp_idx];
6942 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
6943 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
6944 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6945 		req->queue_id = cpu_to_le16(ring->queue_id);
6946 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
6947 			req->cmpl_coal_cnt =
6948 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
6949 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
6950 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
6951 		req->flags = cpu_to_le16(flags);
6952 		break;
6953 	}
6954 	case HWRM_RING_ALLOC_RX:
6955 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6956 		req->length = cpu_to_le32(bp->rx_ring_mask + 1);
6957 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6958 			u16 flags = 0;
6959 
6960 			/* Association of rx ring with stats context */
6961 			grp_info = &bp->grp_info[ring->grp_idx];
6962 			req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
6963 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6964 			req->enables |= cpu_to_le32(
6965 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6966 			if (NET_IP_ALIGN == 2)
6967 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
6968 			req->flags = cpu_to_le16(flags);
6969 		}
6970 		break;
6971 	case HWRM_RING_ALLOC_AGG:
6972 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6973 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
6974 			/* Association of agg ring with rx ring */
6975 			grp_info = &bp->grp_info[ring->grp_idx];
6976 			req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
6977 			req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
6978 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6979 			req->enables |= cpu_to_le32(
6980 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
6981 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6982 		} else {
6983 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6984 		}
6985 		req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
6986 		break;
6987 	case HWRM_RING_ALLOC_CMPL:
6988 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
6989 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6990 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6991 			/* Association of cp ring with nq */
6992 			grp_info = &bp->grp_info[map_index];
6993 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
6994 			req->cq_handle = cpu_to_le64(ring->handle);
6995 			req->enables |= cpu_to_le32(
6996 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
6997 		} else {
6998 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
6999 		}
7000 		break;
7001 	case HWRM_RING_ALLOC_NQ:
7002 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7003 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7004 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7005 		break;
7006 	default:
7007 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7008 			   ring_type);
7009 		return -1;
7010 	}
7011 
7012 	resp = hwrm_req_hold(bp, req);
7013 	rc = hwrm_req_send(bp, req);
7014 	err = le16_to_cpu(resp->error_code);
7015 	ring_id = le16_to_cpu(resp->ring_id);
7016 	hwrm_req_drop(bp, req);
7017 
7018 exit:
7019 	if (rc || err) {
7020 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7021 			   ring_type, rc, err);
7022 		return -EIO;
7023 	}
7024 	ring->fw_ring_id = ring_id;
7025 	return rc;
7026 }
7027 
7028 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7029 {
7030 	int rc;
7031 
7032 	if (BNXT_PF(bp)) {
7033 		struct hwrm_func_cfg_input *req;
7034 
7035 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7036 		if (rc)
7037 			return rc;
7038 
7039 		req->fid = cpu_to_le16(0xffff);
7040 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7041 		req->async_event_cr = cpu_to_le16(idx);
7042 		return hwrm_req_send(bp, req);
7043 	} else {
7044 		struct hwrm_func_vf_cfg_input *req;
7045 
7046 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7047 		if (rc)
7048 			return rc;
7049 
7050 		req->enables =
7051 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7052 		req->async_event_cr = cpu_to_le16(idx);
7053 		return hwrm_req_send(bp, req);
7054 	}
7055 }
7056 
7057 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7058 			     u32 ring_type)
7059 {
7060 	switch (ring_type) {
7061 	case HWRM_RING_ALLOC_TX:
7062 		db->db_ring_mask = bp->tx_ring_mask;
7063 		break;
7064 	case HWRM_RING_ALLOC_RX:
7065 		db->db_ring_mask = bp->rx_ring_mask;
7066 		break;
7067 	case HWRM_RING_ALLOC_AGG:
7068 		db->db_ring_mask = bp->rx_agg_ring_mask;
7069 		break;
7070 	case HWRM_RING_ALLOC_CMPL:
7071 	case HWRM_RING_ALLOC_NQ:
7072 		db->db_ring_mask = bp->cp_ring_mask;
7073 		break;
7074 	}
7075 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7076 		db->db_epoch_mask = db->db_ring_mask + 1;
7077 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7078 	}
7079 }
7080 
7081 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7082 			u32 map_idx, u32 xid)
7083 {
7084 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7085 		switch (ring_type) {
7086 		case HWRM_RING_ALLOC_TX:
7087 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7088 			break;
7089 		case HWRM_RING_ALLOC_RX:
7090 		case HWRM_RING_ALLOC_AGG:
7091 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7092 			break;
7093 		case HWRM_RING_ALLOC_CMPL:
7094 			db->db_key64 = DBR_PATH_L2;
7095 			break;
7096 		case HWRM_RING_ALLOC_NQ:
7097 			db->db_key64 = DBR_PATH_L2;
7098 			break;
7099 		}
7100 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7101 
7102 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7103 			db->db_key64 |= DBR_VALID;
7104 
7105 		db->doorbell = bp->bar1 + bp->db_offset;
7106 	} else {
7107 		db->doorbell = bp->bar1 + map_idx * 0x80;
7108 		switch (ring_type) {
7109 		case HWRM_RING_ALLOC_TX:
7110 			db->db_key32 = DB_KEY_TX;
7111 			break;
7112 		case HWRM_RING_ALLOC_RX:
7113 		case HWRM_RING_ALLOC_AGG:
7114 			db->db_key32 = DB_KEY_RX;
7115 			break;
7116 		case HWRM_RING_ALLOC_CMPL:
7117 			db->db_key32 = DB_KEY_CP;
7118 			break;
7119 		}
7120 	}
7121 	bnxt_set_db_mask(bp, db, ring_type);
7122 }
7123 
7124 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7125 				   struct bnxt_rx_ring_info *rxr)
7126 {
7127 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7128 	struct bnxt_napi *bnapi = rxr->bnapi;
7129 	u32 type = HWRM_RING_ALLOC_RX;
7130 	u32 map_idx = bnapi->index;
7131 	int rc;
7132 
7133 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7134 	if (rc)
7135 		return rc;
7136 
7137 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7138 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7139 
7140 	return 0;
7141 }
7142 
7143 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7144 				       struct bnxt_rx_ring_info *rxr)
7145 {
7146 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7147 	u32 type = HWRM_RING_ALLOC_AGG;
7148 	u32 grp_idx = ring->grp_idx;
7149 	u32 map_idx;
7150 	int rc;
7151 
7152 	map_idx = grp_idx + bp->rx_nr_rings;
7153 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7154 	if (rc)
7155 		return rc;
7156 
7157 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7158 		    ring->fw_ring_id);
7159 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7160 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7161 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7162 
7163 	return 0;
7164 }
7165 
7166 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7167 {
7168 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7169 	int i, rc = 0;
7170 	u32 type;
7171 
7172 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7173 		type = HWRM_RING_ALLOC_NQ;
7174 	else
7175 		type = HWRM_RING_ALLOC_CMPL;
7176 	for (i = 0; i < bp->cp_nr_rings; i++) {
7177 		struct bnxt_napi *bnapi = bp->bnapi[i];
7178 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7179 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7180 		u32 map_idx = ring->map_idx;
7181 		unsigned int vector;
7182 
7183 		vector = bp->irq_tbl[map_idx].vector;
7184 		disable_irq_nosync(vector);
7185 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7186 		if (rc) {
7187 			enable_irq(vector);
7188 			goto err_out;
7189 		}
7190 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7191 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7192 		enable_irq(vector);
7193 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7194 
7195 		if (!i) {
7196 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7197 			if (rc)
7198 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7199 		}
7200 	}
7201 
7202 	type = HWRM_RING_ALLOC_TX;
7203 	for (i = 0; i < bp->tx_nr_rings; i++) {
7204 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7205 		struct bnxt_ring_struct *ring;
7206 		u32 map_idx;
7207 
7208 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7209 			struct bnxt_cp_ring_info *cpr2 = txr->tx_cpr;
7210 			struct bnxt_napi *bnapi = txr->bnapi;
7211 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7212 
7213 			ring = &cpr2->cp_ring_struct;
7214 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7215 			map_idx = bnapi->index;
7216 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7217 			if (rc)
7218 				goto err_out;
7219 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7220 				    ring->fw_ring_id);
7221 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7222 		}
7223 		ring = &txr->tx_ring_struct;
7224 		map_idx = i;
7225 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7226 		if (rc)
7227 			goto err_out;
7228 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
7229 	}
7230 
7231 	for (i = 0; i < bp->rx_nr_rings; i++) {
7232 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7233 
7234 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7235 		if (rc)
7236 			goto err_out;
7237 		/* If we have agg rings, post agg buffers first. */
7238 		if (!agg_rings)
7239 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7240 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7241 			struct bnxt_cp_ring_info *cpr2 = rxr->rx_cpr;
7242 			struct bnxt_napi *bnapi = rxr->bnapi;
7243 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7244 			struct bnxt_ring_struct *ring;
7245 			u32 map_idx = bnapi->index;
7246 
7247 			ring = &cpr2->cp_ring_struct;
7248 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7249 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7250 			if (rc)
7251 				goto err_out;
7252 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7253 				    ring->fw_ring_id);
7254 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7255 		}
7256 	}
7257 
7258 	if (agg_rings) {
7259 		for (i = 0; i < bp->rx_nr_rings; i++) {
7260 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7261 			if (rc)
7262 				goto err_out;
7263 		}
7264 	}
7265 err_out:
7266 	return rc;
7267 }
7268 
7269 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7270 				   struct bnxt_ring_struct *ring,
7271 				   u32 ring_type, int cmpl_ring_id)
7272 {
7273 	struct hwrm_ring_free_output *resp;
7274 	struct hwrm_ring_free_input *req;
7275 	u16 error_code = 0;
7276 	int rc;
7277 
7278 	if (BNXT_NO_FW_ACCESS(bp))
7279 		return 0;
7280 
7281 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7282 	if (rc)
7283 		goto exit;
7284 
7285 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7286 	req->ring_type = ring_type;
7287 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7288 
7289 	resp = hwrm_req_hold(bp, req);
7290 	rc = hwrm_req_send(bp, req);
7291 	error_code = le16_to_cpu(resp->error_code);
7292 	hwrm_req_drop(bp, req);
7293 exit:
7294 	if (rc || error_code) {
7295 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7296 			   ring_type, rc, error_code);
7297 		return -EIO;
7298 	}
7299 	return 0;
7300 }
7301 
7302 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7303 				   struct bnxt_rx_ring_info *rxr,
7304 				   bool close_path)
7305 {
7306 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7307 	u32 grp_idx = rxr->bnapi->index;
7308 	u32 cmpl_ring_id;
7309 
7310 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7311 		return;
7312 
7313 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7314 	hwrm_ring_free_send_msg(bp, ring,
7315 				RING_FREE_REQ_RING_TYPE_RX,
7316 				close_path ? cmpl_ring_id :
7317 				INVALID_HW_RING_ID);
7318 	ring->fw_ring_id = INVALID_HW_RING_ID;
7319 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7320 }
7321 
7322 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7323 				       struct bnxt_rx_ring_info *rxr,
7324 				       bool close_path)
7325 {
7326 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7327 	u32 grp_idx = rxr->bnapi->index;
7328 	u32 type, cmpl_ring_id;
7329 
7330 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7331 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7332 	else
7333 		type = RING_FREE_REQ_RING_TYPE_RX;
7334 
7335 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7336 		return;
7337 
7338 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7339 	hwrm_ring_free_send_msg(bp, ring, type,
7340 				close_path ? cmpl_ring_id :
7341 				INVALID_HW_RING_ID);
7342 	ring->fw_ring_id = INVALID_HW_RING_ID;
7343 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7344 }
7345 
7346 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7347 {
7348 	u32 type;
7349 	int i;
7350 
7351 	if (!bp->bnapi)
7352 		return;
7353 
7354 	for (i = 0; i < bp->tx_nr_rings; i++) {
7355 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7356 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7357 
7358 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7359 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
7360 
7361 			hwrm_ring_free_send_msg(bp, ring,
7362 						RING_FREE_REQ_RING_TYPE_TX,
7363 						close_path ? cmpl_ring_id :
7364 						INVALID_HW_RING_ID);
7365 			ring->fw_ring_id = INVALID_HW_RING_ID;
7366 		}
7367 	}
7368 
7369 	for (i = 0; i < bp->rx_nr_rings; i++) {
7370 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7371 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7372 	}
7373 
7374 	/* The completion rings are about to be freed.  After that the
7375 	 * IRQ doorbell will not work anymore.  So we need to disable
7376 	 * IRQ here.
7377 	 */
7378 	bnxt_disable_int_sync(bp);
7379 
7380 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7381 		type = RING_FREE_REQ_RING_TYPE_NQ;
7382 	else
7383 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7384 	for (i = 0; i < bp->cp_nr_rings; i++) {
7385 		struct bnxt_napi *bnapi = bp->bnapi[i];
7386 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7387 		struct bnxt_ring_struct *ring;
7388 		int j;
7389 
7390 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) {
7391 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
7392 
7393 			ring = &cpr2->cp_ring_struct;
7394 			if (ring->fw_ring_id == INVALID_HW_RING_ID)
7395 				continue;
7396 			hwrm_ring_free_send_msg(bp, ring,
7397 						RING_FREE_REQ_RING_TYPE_L2_CMPL,
7398 						INVALID_HW_RING_ID);
7399 			ring->fw_ring_id = INVALID_HW_RING_ID;
7400 		}
7401 		ring = &cpr->cp_ring_struct;
7402 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7403 			hwrm_ring_free_send_msg(bp, ring, type,
7404 						INVALID_HW_RING_ID);
7405 			ring->fw_ring_id = INVALID_HW_RING_ID;
7406 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7407 		}
7408 	}
7409 }
7410 
7411 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7412 			     bool shared);
7413 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7414 			   bool shared);
7415 
7416 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7417 {
7418 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7419 	struct hwrm_func_qcfg_output *resp;
7420 	struct hwrm_func_qcfg_input *req;
7421 	int rc;
7422 
7423 	if (bp->hwrm_spec_code < 0x10601)
7424 		return 0;
7425 
7426 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7427 	if (rc)
7428 		return rc;
7429 
7430 	req->fid = cpu_to_le16(0xffff);
7431 	resp = hwrm_req_hold(bp, req);
7432 	rc = hwrm_req_send(bp, req);
7433 	if (rc) {
7434 		hwrm_req_drop(bp, req);
7435 		return rc;
7436 	}
7437 
7438 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7439 	if (BNXT_NEW_RM(bp)) {
7440 		u16 cp, stats;
7441 
7442 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7443 		hw_resc->resv_hw_ring_grps =
7444 			le32_to_cpu(resp->alloc_hw_ring_grps);
7445 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7446 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7447 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7448 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7449 		hw_resc->resv_irqs = cp;
7450 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7451 			int rx = hw_resc->resv_rx_rings;
7452 			int tx = hw_resc->resv_tx_rings;
7453 
7454 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7455 				rx >>= 1;
7456 			if (cp < (rx + tx)) {
7457 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7458 				if (rc)
7459 					goto get_rings_exit;
7460 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7461 					rx <<= 1;
7462 				hw_resc->resv_rx_rings = rx;
7463 				hw_resc->resv_tx_rings = tx;
7464 			}
7465 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7466 			hw_resc->resv_hw_ring_grps = rx;
7467 		}
7468 		hw_resc->resv_cp_rings = cp;
7469 		hw_resc->resv_stat_ctxs = stats;
7470 	}
7471 get_rings_exit:
7472 	hwrm_req_drop(bp, req);
7473 	return rc;
7474 }
7475 
7476 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7477 {
7478 	struct hwrm_func_qcfg_output *resp;
7479 	struct hwrm_func_qcfg_input *req;
7480 	int rc;
7481 
7482 	if (bp->hwrm_spec_code < 0x10601)
7483 		return 0;
7484 
7485 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7486 	if (rc)
7487 		return rc;
7488 
7489 	req->fid = cpu_to_le16(fid);
7490 	resp = hwrm_req_hold(bp, req);
7491 	rc = hwrm_req_send(bp, req);
7492 	if (!rc)
7493 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7494 
7495 	hwrm_req_drop(bp, req);
7496 	return rc;
7497 }
7498 
7499 static bool bnxt_rfs_supported(struct bnxt *bp);
7500 
7501 static struct hwrm_func_cfg_input *
7502 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7503 {
7504 	struct hwrm_func_cfg_input *req;
7505 	u32 enables = 0;
7506 
7507 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7508 		return NULL;
7509 
7510 	req->fid = cpu_to_le16(0xffff);
7511 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7512 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7513 	if (BNXT_NEW_RM(bp)) {
7514 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7515 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7516 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7517 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7518 			enables |= hwr->cp_p5 ?
7519 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7520 		} else {
7521 			enables |= hwr->cp ?
7522 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7523 			enables |= hwr->grp ?
7524 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7525 		}
7526 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7527 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7528 					  0;
7529 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7530 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7531 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7532 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7533 			req->num_msix = cpu_to_le16(hwr->cp);
7534 		} else {
7535 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7536 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7537 		}
7538 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7539 		req->num_vnics = cpu_to_le16(hwr->vnic);
7540 	}
7541 	req->enables = cpu_to_le32(enables);
7542 	return req;
7543 }
7544 
7545 static struct hwrm_func_vf_cfg_input *
7546 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7547 {
7548 	struct hwrm_func_vf_cfg_input *req;
7549 	u32 enables = 0;
7550 
7551 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7552 		return NULL;
7553 
7554 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7555 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7556 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7557 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7558 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7559 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7560 		enables |= hwr->cp_p5 ?
7561 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7562 	} else {
7563 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7564 		enables |= hwr->grp ?
7565 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7566 	}
7567 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7568 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7569 
7570 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7571 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7572 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7573 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7574 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7575 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7576 	} else {
7577 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7578 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7579 	}
7580 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7581 	req->num_vnics = cpu_to_le16(hwr->vnic);
7582 
7583 	req->enables = cpu_to_le32(enables);
7584 	return req;
7585 }
7586 
7587 static int
7588 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7589 {
7590 	struct hwrm_func_cfg_input *req;
7591 	int rc;
7592 
7593 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7594 	if (!req)
7595 		return -ENOMEM;
7596 
7597 	if (!req->enables) {
7598 		hwrm_req_drop(bp, req);
7599 		return 0;
7600 	}
7601 
7602 	rc = hwrm_req_send(bp, req);
7603 	if (rc)
7604 		return rc;
7605 
7606 	if (bp->hwrm_spec_code < 0x10601)
7607 		bp->hw_resc.resv_tx_rings = hwr->tx;
7608 
7609 	return bnxt_hwrm_get_rings(bp);
7610 }
7611 
7612 static int
7613 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7614 {
7615 	struct hwrm_func_vf_cfg_input *req;
7616 	int rc;
7617 
7618 	if (!BNXT_NEW_RM(bp)) {
7619 		bp->hw_resc.resv_tx_rings = hwr->tx;
7620 		return 0;
7621 	}
7622 
7623 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7624 	if (!req)
7625 		return -ENOMEM;
7626 
7627 	rc = hwrm_req_send(bp, req);
7628 	if (rc)
7629 		return rc;
7630 
7631 	return bnxt_hwrm_get_rings(bp);
7632 }
7633 
7634 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7635 {
7636 	if (BNXT_PF(bp))
7637 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7638 	else
7639 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7640 }
7641 
7642 int bnxt_nq_rings_in_use(struct bnxt *bp)
7643 {
7644 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7645 }
7646 
7647 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7648 {
7649 	int cp;
7650 
7651 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7652 		return bnxt_nq_rings_in_use(bp);
7653 
7654 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7655 	return cp;
7656 }
7657 
7658 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7659 {
7660 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7661 }
7662 
7663 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7664 {
7665 	if (!hwr->grp)
7666 		return 0;
7667 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7668 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7669 
7670 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7671 			rss_ctx *= hwr->vnic;
7672 		return rss_ctx;
7673 	}
7674 	if (BNXT_VF(bp))
7675 		return BNXT_VF_MAX_RSS_CTX;
7676 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7677 		return hwr->grp + 1;
7678 	return 1;
7679 }
7680 
7681 /* Check if a default RSS map needs to be setup.  This function is only
7682  * used on older firmware that does not require reserving RX rings.
7683  */
7684 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7685 {
7686 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7687 
7688 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7689 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7690 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7691 		if (!netif_is_rxfh_configured(bp->dev))
7692 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7693 	}
7694 }
7695 
7696 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7697 {
7698 	if (bp->flags & BNXT_FLAG_RFS) {
7699 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7700 			return 2 + bp->num_rss_ctx;
7701 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7702 			return rx_rings + 1;
7703 	}
7704 	return 1;
7705 }
7706 
7707 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7708 {
7709 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7710 	int cp = bnxt_cp_rings_in_use(bp);
7711 	int nq = bnxt_nq_rings_in_use(bp);
7712 	int rx = bp->rx_nr_rings, stat;
7713 	int vnic, grp = rx;
7714 
7715 	/* Old firmware does not need RX ring reservations but we still
7716 	 * need to setup a default RSS map when needed.  With new firmware
7717 	 * we go through RX ring reservations first and then set up the
7718 	 * RSS map for the successfully reserved RX rings when needed.
7719 	 */
7720 	if (!BNXT_NEW_RM(bp))
7721 		bnxt_check_rss_tbl_no_rmgr(bp);
7722 
7723 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7724 	    bp->hwrm_spec_code >= 0x10601)
7725 		return true;
7726 
7727 	if (!BNXT_NEW_RM(bp))
7728 		return false;
7729 
7730 	vnic = bnxt_get_total_vnics(bp, rx);
7731 
7732 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7733 		rx <<= 1;
7734 	stat = bnxt_get_func_stat_ctxs(bp);
7735 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7736 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7737 	    (hw_resc->resv_hw_ring_grps != grp &&
7738 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7739 		return true;
7740 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7741 	    hw_resc->resv_irqs != nq)
7742 		return true;
7743 	return false;
7744 }
7745 
7746 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7747 {
7748 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7749 
7750 	hwr->tx = hw_resc->resv_tx_rings;
7751 	if (BNXT_NEW_RM(bp)) {
7752 		hwr->rx = hw_resc->resv_rx_rings;
7753 		hwr->cp = hw_resc->resv_irqs;
7754 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7755 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7756 		hwr->grp = hw_resc->resv_hw_ring_grps;
7757 		hwr->vnic = hw_resc->resv_vnics;
7758 		hwr->stat = hw_resc->resv_stat_ctxs;
7759 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7760 	}
7761 }
7762 
7763 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7764 {
7765 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7766 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7767 }
7768 
7769 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7770 
7771 static int __bnxt_reserve_rings(struct bnxt *bp)
7772 {
7773 	struct bnxt_hw_rings hwr = {0};
7774 	int rx_rings, old_rx_rings, rc;
7775 	int cp = bp->cp_nr_rings;
7776 	int ulp_msix = 0;
7777 	bool sh = false;
7778 	int tx_cp;
7779 
7780 	if (!bnxt_need_reserve_rings(bp))
7781 		return 0;
7782 
7783 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7784 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7785 		if (!ulp_msix)
7786 			bnxt_set_ulp_stat_ctxs(bp, 0);
7787 
7788 		if (ulp_msix > bp->ulp_num_msix_want)
7789 			ulp_msix = bp->ulp_num_msix_want;
7790 		hwr.cp = cp + ulp_msix;
7791 	} else {
7792 		hwr.cp = bnxt_nq_rings_in_use(bp);
7793 	}
7794 
7795 	hwr.tx = bp->tx_nr_rings;
7796 	hwr.rx = bp->rx_nr_rings;
7797 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7798 		sh = true;
7799 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7800 		hwr.cp_p5 = hwr.rx + hwr.tx;
7801 
7802 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7803 
7804 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7805 		hwr.rx <<= 1;
7806 	hwr.grp = bp->rx_nr_rings;
7807 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7808 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7809 	old_rx_rings = bp->hw_resc.resv_rx_rings;
7810 
7811 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7812 	if (rc)
7813 		return rc;
7814 
7815 	bnxt_copy_reserved_rings(bp, &hwr);
7816 
7817 	rx_rings = hwr.rx;
7818 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7819 		if (hwr.rx >= 2) {
7820 			rx_rings = hwr.rx >> 1;
7821 		} else {
7822 			if (netif_running(bp->dev))
7823 				return -ENOMEM;
7824 
7825 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7826 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7827 			bp->dev->hw_features &= ~NETIF_F_LRO;
7828 			bp->dev->features &= ~NETIF_F_LRO;
7829 			bnxt_set_ring_params(bp);
7830 		}
7831 	}
7832 	rx_rings = min_t(int, rx_rings, hwr.grp);
7833 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7834 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7835 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7836 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7837 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7838 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7839 		hwr.rx = rx_rings << 1;
7840 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
7841 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
7842 	bp->tx_nr_rings = hwr.tx;
7843 
7844 	/* If we cannot reserve all the RX rings, reset the RSS map only
7845 	 * if absolutely necessary
7846 	 */
7847 	if (rx_rings != bp->rx_nr_rings) {
7848 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
7849 			    rx_rings, bp->rx_nr_rings);
7850 		if (netif_is_rxfh_configured(bp->dev) &&
7851 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
7852 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
7853 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
7854 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
7855 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
7856 		}
7857 	}
7858 	bp->rx_nr_rings = rx_rings;
7859 	bp->cp_nr_rings = hwr.cp;
7860 
7861 	if (!bnxt_rings_ok(bp, &hwr))
7862 		return -ENOMEM;
7863 
7864 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
7865 	    !netif_is_rxfh_configured(bp->dev))
7866 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7867 
7868 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
7869 		int resv_msix, resv_ctx, ulp_ctxs;
7870 		struct bnxt_hw_resc *hw_resc;
7871 
7872 		hw_resc = &bp->hw_resc;
7873 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
7874 		ulp_msix = min_t(int, resv_msix, ulp_msix);
7875 		bnxt_set_ulp_msix_num(bp, ulp_msix);
7876 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
7877 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
7878 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
7879 	}
7880 
7881 	return rc;
7882 }
7883 
7884 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7885 {
7886 	struct hwrm_func_vf_cfg_input *req;
7887 	u32 flags;
7888 
7889 	if (!BNXT_NEW_RM(bp))
7890 		return 0;
7891 
7892 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7893 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
7894 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7895 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7896 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7897 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
7898 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
7899 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7900 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7901 
7902 	req->flags = cpu_to_le32(flags);
7903 	return hwrm_req_send_silent(bp, req);
7904 }
7905 
7906 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7907 {
7908 	struct hwrm_func_cfg_input *req;
7909 	u32 flags;
7910 
7911 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7912 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
7913 	if (BNXT_NEW_RM(bp)) {
7914 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7915 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7916 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7917 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
7918 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7919 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
7920 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
7921 		else
7922 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7923 	}
7924 
7925 	req->flags = cpu_to_le32(flags);
7926 	return hwrm_req_send_silent(bp, req);
7927 }
7928 
7929 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7930 {
7931 	if (bp->hwrm_spec_code < 0x10801)
7932 		return 0;
7933 
7934 	if (BNXT_PF(bp))
7935 		return bnxt_hwrm_check_pf_rings(bp, hwr);
7936 
7937 	return bnxt_hwrm_check_vf_rings(bp, hwr);
7938 }
7939 
7940 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
7941 {
7942 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7943 	struct hwrm_ring_aggint_qcaps_output *resp;
7944 	struct hwrm_ring_aggint_qcaps_input *req;
7945 	int rc;
7946 
7947 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
7948 	coal_cap->num_cmpl_dma_aggr_max = 63;
7949 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
7950 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
7951 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
7952 	coal_cap->int_lat_tmr_min_max = 65535;
7953 	coal_cap->int_lat_tmr_max_max = 65535;
7954 	coal_cap->num_cmpl_aggr_int_max = 65535;
7955 	coal_cap->timer_units = 80;
7956 
7957 	if (bp->hwrm_spec_code < 0x10902)
7958 		return;
7959 
7960 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
7961 		return;
7962 
7963 	resp = hwrm_req_hold(bp, req);
7964 	rc = hwrm_req_send_silent(bp, req);
7965 	if (!rc) {
7966 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
7967 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
7968 		coal_cap->num_cmpl_dma_aggr_max =
7969 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
7970 		coal_cap->num_cmpl_dma_aggr_during_int_max =
7971 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
7972 		coal_cap->cmpl_aggr_dma_tmr_max =
7973 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
7974 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
7975 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
7976 		coal_cap->int_lat_tmr_min_max =
7977 			le16_to_cpu(resp->int_lat_tmr_min_max);
7978 		coal_cap->int_lat_tmr_max_max =
7979 			le16_to_cpu(resp->int_lat_tmr_max_max);
7980 		coal_cap->num_cmpl_aggr_int_max =
7981 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
7982 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
7983 	}
7984 	hwrm_req_drop(bp, req);
7985 }
7986 
7987 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
7988 {
7989 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7990 
7991 	return usec * 1000 / coal_cap->timer_units;
7992 }
7993 
7994 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
7995 	struct bnxt_coal *hw_coal,
7996 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7997 {
7998 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7999 	u16 val, tmr, max, flags = hw_coal->flags;
8000 	u32 cmpl_params = coal_cap->cmpl_params;
8001 
8002 	max = hw_coal->bufs_per_record * 128;
8003 	if (hw_coal->budget)
8004 		max = hw_coal->bufs_per_record * hw_coal->budget;
8005 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8006 
8007 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8008 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8009 
8010 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8011 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8012 
8013 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8014 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8015 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8016 
8017 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8018 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8019 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8020 
8021 	/* min timer set to 1/2 of interrupt timer */
8022 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8023 		val = tmr / 2;
8024 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8025 		req->int_lat_tmr_min = cpu_to_le16(val);
8026 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8027 	}
8028 
8029 	/* buf timer set to 1/4 of interrupt timer */
8030 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8031 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8032 
8033 	if (cmpl_params &
8034 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8035 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8036 		val = clamp_t(u16, tmr, 1,
8037 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8038 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8039 		req->enables |=
8040 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8041 	}
8042 
8043 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8044 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8045 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8046 	req->flags = cpu_to_le16(flags);
8047 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8048 }
8049 
8050 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8051 				   struct bnxt_coal *hw_coal)
8052 {
8053 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8054 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8055 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8056 	u32 nq_params = coal_cap->nq_params;
8057 	u16 tmr;
8058 	int rc;
8059 
8060 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8061 		return 0;
8062 
8063 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8064 	if (rc)
8065 		return rc;
8066 
8067 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8068 	req->flags =
8069 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8070 
8071 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8072 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8073 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8074 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8075 	return hwrm_req_send(bp, req);
8076 }
8077 
8078 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8079 {
8080 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8081 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8082 	struct bnxt_coal coal;
8083 	int rc;
8084 
8085 	/* Tick values in micro seconds.
8086 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8087 	 */
8088 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8089 
8090 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8091 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8092 
8093 	if (!bnapi->rx_ring)
8094 		return -ENODEV;
8095 
8096 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8097 	if (rc)
8098 		return rc;
8099 
8100 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8101 
8102 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8103 
8104 	return hwrm_req_send(bp, req_rx);
8105 }
8106 
8107 static int
8108 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8109 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8110 {
8111 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8112 
8113 	req->ring_id = cpu_to_le16(ring_id);
8114 	return hwrm_req_send(bp, req);
8115 }
8116 
8117 static int
8118 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8119 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8120 {
8121 	struct bnxt_tx_ring_info *txr;
8122 	int i, rc;
8123 
8124 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8125 		u16 ring_id;
8126 
8127 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8128 		req->ring_id = cpu_to_le16(ring_id);
8129 		rc = hwrm_req_send(bp, req);
8130 		if (rc)
8131 			return rc;
8132 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8133 			return 0;
8134 	}
8135 	return 0;
8136 }
8137 
8138 int bnxt_hwrm_set_coal(struct bnxt *bp)
8139 {
8140 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8141 	int i, rc;
8142 
8143 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8144 	if (rc)
8145 		return rc;
8146 
8147 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8148 	if (rc) {
8149 		hwrm_req_drop(bp, req_rx);
8150 		return rc;
8151 	}
8152 
8153 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8154 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8155 
8156 	hwrm_req_hold(bp, req_rx);
8157 	hwrm_req_hold(bp, req_tx);
8158 	for (i = 0; i < bp->cp_nr_rings; i++) {
8159 		struct bnxt_napi *bnapi = bp->bnapi[i];
8160 		struct bnxt_coal *hw_coal;
8161 
8162 		if (!bnapi->rx_ring)
8163 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8164 		else
8165 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8166 		if (rc)
8167 			break;
8168 
8169 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8170 			continue;
8171 
8172 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8173 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8174 			if (rc)
8175 				break;
8176 		}
8177 		if (bnapi->rx_ring)
8178 			hw_coal = &bp->rx_coal;
8179 		else
8180 			hw_coal = &bp->tx_coal;
8181 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8182 	}
8183 	hwrm_req_drop(bp, req_rx);
8184 	hwrm_req_drop(bp, req_tx);
8185 	return rc;
8186 }
8187 
8188 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8189 {
8190 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8191 	struct hwrm_stat_ctx_free_input *req;
8192 	int i;
8193 
8194 	if (!bp->bnapi)
8195 		return;
8196 
8197 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8198 		return;
8199 
8200 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8201 		return;
8202 	if (BNXT_FW_MAJ(bp) <= 20) {
8203 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8204 			hwrm_req_drop(bp, req);
8205 			return;
8206 		}
8207 		hwrm_req_hold(bp, req0);
8208 	}
8209 	hwrm_req_hold(bp, req);
8210 	for (i = 0; i < bp->cp_nr_rings; i++) {
8211 		struct bnxt_napi *bnapi = bp->bnapi[i];
8212 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8213 
8214 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8215 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8216 			if (req0) {
8217 				req0->stat_ctx_id = req->stat_ctx_id;
8218 				hwrm_req_send(bp, req0);
8219 			}
8220 			hwrm_req_send(bp, req);
8221 
8222 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8223 		}
8224 	}
8225 	hwrm_req_drop(bp, req);
8226 	if (req0)
8227 		hwrm_req_drop(bp, req0);
8228 }
8229 
8230 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8231 {
8232 	struct hwrm_stat_ctx_alloc_output *resp;
8233 	struct hwrm_stat_ctx_alloc_input *req;
8234 	int rc, i;
8235 
8236 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8237 		return 0;
8238 
8239 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8240 	if (rc)
8241 		return rc;
8242 
8243 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8244 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8245 
8246 	resp = hwrm_req_hold(bp, req);
8247 	for (i = 0; i < bp->cp_nr_rings; i++) {
8248 		struct bnxt_napi *bnapi = bp->bnapi[i];
8249 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8250 
8251 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8252 
8253 		rc = hwrm_req_send(bp, req);
8254 		if (rc)
8255 			break;
8256 
8257 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8258 
8259 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8260 	}
8261 	hwrm_req_drop(bp, req);
8262 	return rc;
8263 }
8264 
8265 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8266 {
8267 	struct hwrm_func_qcfg_output *resp;
8268 	struct hwrm_func_qcfg_input *req;
8269 	u16 flags;
8270 	int rc;
8271 
8272 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8273 	if (rc)
8274 		return rc;
8275 
8276 	req->fid = cpu_to_le16(0xffff);
8277 	resp = hwrm_req_hold(bp, req);
8278 	rc = hwrm_req_send(bp, req);
8279 	if (rc)
8280 		goto func_qcfg_exit;
8281 
8282 	flags = le16_to_cpu(resp->flags);
8283 #ifdef CONFIG_BNXT_SRIOV
8284 	if (BNXT_VF(bp)) {
8285 		struct bnxt_vf_info *vf = &bp->vf;
8286 
8287 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8288 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8289 			vf->flags |= BNXT_VF_TRUST;
8290 		else
8291 			vf->flags &= ~BNXT_VF_TRUST;
8292 	} else {
8293 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8294 	}
8295 #endif
8296 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8297 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8298 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8299 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8300 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8301 	}
8302 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8303 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8304 
8305 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8306 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8307 
8308 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8309 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8310 
8311 	switch (resp->port_partition_type) {
8312 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8313 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8314 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8315 		bp->port_partition_type = resp->port_partition_type;
8316 		break;
8317 	}
8318 	if (bp->hwrm_spec_code < 0x10707 ||
8319 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8320 		bp->br_mode = BRIDGE_MODE_VEB;
8321 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8322 		bp->br_mode = BRIDGE_MODE_VEPA;
8323 	else
8324 		bp->br_mode = BRIDGE_MODE_UNDEF;
8325 
8326 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8327 	if (!bp->max_mtu)
8328 		bp->max_mtu = BNXT_MAX_MTU;
8329 
8330 	if (bp->db_size)
8331 		goto func_qcfg_exit;
8332 
8333 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8334 	if (BNXT_CHIP_P5(bp)) {
8335 		if (BNXT_PF(bp))
8336 			bp->db_offset = DB_PF_OFFSET_P5;
8337 		else
8338 			bp->db_offset = DB_VF_OFFSET_P5;
8339 	}
8340 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8341 				 1024);
8342 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8343 	    bp->db_size <= bp->db_offset)
8344 		bp->db_size = pci_resource_len(bp->pdev, 2);
8345 
8346 func_qcfg_exit:
8347 	hwrm_req_drop(bp, req);
8348 	return rc;
8349 }
8350 
8351 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8352 				      u8 init_val, u8 init_offset,
8353 				      bool init_mask_set)
8354 {
8355 	ctxm->init_value = init_val;
8356 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8357 	if (init_mask_set)
8358 		ctxm->init_offset = init_offset * 4;
8359 	else
8360 		ctxm->init_value = 0;
8361 }
8362 
8363 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8364 {
8365 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8366 	u16 type;
8367 
8368 	for (type = 0; type < ctx_max; type++) {
8369 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8370 		int n = 1;
8371 
8372 		if (!ctxm->max_entries || ctxm->pg_info)
8373 			continue;
8374 
8375 		if (ctxm->instance_bmap)
8376 			n = hweight32(ctxm->instance_bmap);
8377 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8378 		if (!ctxm->pg_info)
8379 			return -ENOMEM;
8380 	}
8381 	return 0;
8382 }
8383 
8384 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8385 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8386 
8387 #define BNXT_CTX_INIT_VALID(flags)	\
8388 	(!!((flags) &			\
8389 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8390 
8391 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8392 {
8393 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8394 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8395 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8396 	u16 type;
8397 	int rc;
8398 
8399 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8400 	if (rc)
8401 		return rc;
8402 
8403 	if (!ctx) {
8404 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8405 		if (!ctx)
8406 			return -ENOMEM;
8407 		bp->ctx = ctx;
8408 	}
8409 
8410 	resp = hwrm_req_hold(bp, req);
8411 
8412 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8413 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8414 		u8 init_val, init_off, i;
8415 		u32 max_entries;
8416 		u16 entry_size;
8417 		__le32 *p;
8418 		u32 flags;
8419 
8420 		req->type = cpu_to_le16(type);
8421 		rc = hwrm_req_send(bp, req);
8422 		if (rc)
8423 			goto ctx_done;
8424 		flags = le32_to_cpu(resp->flags);
8425 		type = le16_to_cpu(resp->next_valid_type);
8426 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8427 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8428 			continue;
8429 		}
8430 		entry_size = le16_to_cpu(resp->entry_size);
8431 		max_entries = le32_to_cpu(resp->max_num_entries);
8432 		if (ctxm->mem_valid) {
8433 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8434 			    ctxm->entry_size != entry_size ||
8435 			    ctxm->max_entries != max_entries)
8436 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8437 			else
8438 				continue;
8439 		}
8440 		ctxm->type = le16_to_cpu(resp->type);
8441 		ctxm->entry_size = entry_size;
8442 		ctxm->flags = flags;
8443 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8444 		ctxm->entry_multiple = resp->entry_multiple;
8445 		ctxm->max_entries = max_entries;
8446 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8447 		init_val = resp->ctx_init_value;
8448 		init_off = resp->ctx_init_offset;
8449 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8450 					  BNXT_CTX_INIT_VALID(flags));
8451 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8452 					      BNXT_MAX_SPLIT_ENTRY);
8453 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8454 		     i++, p++)
8455 			ctxm->split[i] = le32_to_cpu(*p);
8456 	}
8457 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8458 
8459 ctx_done:
8460 	hwrm_req_drop(bp, req);
8461 	return rc;
8462 }
8463 
8464 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8465 {
8466 	struct hwrm_func_backing_store_qcaps_output *resp;
8467 	struct hwrm_func_backing_store_qcaps_input *req;
8468 	int rc;
8469 
8470 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8471 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8472 		return 0;
8473 
8474 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8475 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8476 
8477 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8478 	if (rc)
8479 		return rc;
8480 
8481 	resp = hwrm_req_hold(bp, req);
8482 	rc = hwrm_req_send_silent(bp, req);
8483 	if (!rc) {
8484 		struct bnxt_ctx_mem_type *ctxm;
8485 		struct bnxt_ctx_mem_info *ctx;
8486 		u8 init_val, init_idx = 0;
8487 		u16 init_mask;
8488 
8489 		ctx = bp->ctx;
8490 		if (!ctx) {
8491 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8492 			if (!ctx) {
8493 				rc = -ENOMEM;
8494 				goto ctx_err;
8495 			}
8496 			bp->ctx = ctx;
8497 		}
8498 		init_val = resp->ctx_kind_initializer;
8499 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8500 
8501 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8502 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8503 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8504 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8505 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8506 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8507 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8508 					  (init_mask & (1 << init_idx++)) != 0);
8509 
8510 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8511 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8512 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8513 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8514 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8515 					  (init_mask & (1 << init_idx++)) != 0);
8516 
8517 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8518 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8519 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8520 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8521 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8522 					  (init_mask & (1 << init_idx++)) != 0);
8523 
8524 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8525 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8526 		ctxm->max_entries = ctxm->vnic_entries +
8527 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8528 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8529 		bnxt_init_ctx_initializer(ctxm, init_val,
8530 					  resp->vnic_init_offset,
8531 					  (init_mask & (1 << init_idx++)) != 0);
8532 
8533 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8534 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8535 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8536 		bnxt_init_ctx_initializer(ctxm, init_val,
8537 					  resp->stat_init_offset,
8538 					  (init_mask & (1 << init_idx++)) != 0);
8539 
8540 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8541 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8542 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8543 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8544 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8545 		if (!ctxm->entry_multiple)
8546 			ctxm->entry_multiple = 1;
8547 
8548 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8549 
8550 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8551 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8552 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8553 		ctxm->mrav_num_entries_units =
8554 			le16_to_cpu(resp->mrav_num_entries_units);
8555 		bnxt_init_ctx_initializer(ctxm, init_val,
8556 					  resp->mrav_init_offset,
8557 					  (init_mask & (1 << init_idx++)) != 0);
8558 
8559 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8560 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8561 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8562 
8563 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8564 		if (!ctx->tqm_fp_rings_count)
8565 			ctx->tqm_fp_rings_count = bp->max_q;
8566 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8567 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8568 
8569 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8570 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8571 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8572 
8573 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8574 	} else {
8575 		rc = 0;
8576 	}
8577 ctx_err:
8578 	hwrm_req_drop(bp, req);
8579 	return rc;
8580 }
8581 
8582 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8583 				  __le64 *pg_dir)
8584 {
8585 	if (!rmem->nr_pages)
8586 		return;
8587 
8588 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8589 	if (rmem->depth >= 1) {
8590 		if (rmem->depth == 2)
8591 			*pg_attr |= 2;
8592 		else
8593 			*pg_attr |= 1;
8594 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8595 	} else {
8596 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8597 	}
8598 }
8599 
8600 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8601 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8602 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8603 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8604 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8605 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8606 
8607 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8608 {
8609 	struct hwrm_func_backing_store_cfg_input *req;
8610 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8611 	struct bnxt_ctx_pg_info *ctx_pg;
8612 	struct bnxt_ctx_mem_type *ctxm;
8613 	void **__req = (void **)&req;
8614 	u32 req_len = sizeof(*req);
8615 	__le32 *num_entries;
8616 	__le64 *pg_dir;
8617 	u32 flags = 0;
8618 	u8 *pg_attr;
8619 	u32 ena;
8620 	int rc;
8621 	int i;
8622 
8623 	if (!ctx)
8624 		return 0;
8625 
8626 	if (req_len > bp->hwrm_max_ext_req_len)
8627 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8628 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8629 	if (rc)
8630 		return rc;
8631 
8632 	req->enables = cpu_to_le32(enables);
8633 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8634 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8635 		ctx_pg = ctxm->pg_info;
8636 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8637 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8638 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8639 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8640 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8641 				      &req->qpc_pg_size_qpc_lvl,
8642 				      &req->qpc_page_dir);
8643 
8644 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8645 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8646 	}
8647 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8648 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8649 		ctx_pg = ctxm->pg_info;
8650 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8651 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8652 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8653 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8654 				      &req->srq_pg_size_srq_lvl,
8655 				      &req->srq_page_dir);
8656 	}
8657 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8658 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8659 		ctx_pg = ctxm->pg_info;
8660 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8661 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8662 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8663 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8664 				      &req->cq_pg_size_cq_lvl,
8665 				      &req->cq_page_dir);
8666 	}
8667 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8668 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8669 		ctx_pg = ctxm->pg_info;
8670 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8671 		req->vnic_num_ring_table_entries =
8672 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8673 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8674 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8675 				      &req->vnic_pg_size_vnic_lvl,
8676 				      &req->vnic_page_dir);
8677 	}
8678 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8679 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8680 		ctx_pg = ctxm->pg_info;
8681 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8682 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8683 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8684 				      &req->stat_pg_size_stat_lvl,
8685 				      &req->stat_page_dir);
8686 	}
8687 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8688 		u32 units;
8689 
8690 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8691 		ctx_pg = ctxm->pg_info;
8692 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8693 		units = ctxm->mrav_num_entries_units;
8694 		if (units) {
8695 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8696 			u32 entries;
8697 
8698 			num_mr = ctx_pg->entries - num_ah;
8699 			entries = ((num_mr / units) << 16) | (num_ah / units);
8700 			req->mrav_num_entries = cpu_to_le32(entries);
8701 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8702 		}
8703 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8704 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8705 				      &req->mrav_pg_size_mrav_lvl,
8706 				      &req->mrav_page_dir);
8707 	}
8708 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8709 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8710 		ctx_pg = ctxm->pg_info;
8711 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8712 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8713 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8714 				      &req->tim_pg_size_tim_lvl,
8715 				      &req->tim_page_dir);
8716 	}
8717 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8718 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8719 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8720 	     pg_dir = &req->tqm_sp_page_dir,
8721 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8722 	     ctx_pg = ctxm->pg_info;
8723 	     i < BNXT_MAX_TQM_RINGS;
8724 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8725 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8726 		if (!(enables & ena))
8727 			continue;
8728 
8729 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8730 		*num_entries = cpu_to_le32(ctx_pg->entries);
8731 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8732 	}
8733 	req->flags = cpu_to_le32(flags);
8734 	return hwrm_req_send(bp, req);
8735 }
8736 
8737 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8738 				  struct bnxt_ctx_pg_info *ctx_pg)
8739 {
8740 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8741 
8742 	rmem->page_size = BNXT_PAGE_SIZE;
8743 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8744 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8745 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8746 	if (rmem->depth >= 1)
8747 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8748 	return bnxt_alloc_ring(bp, rmem);
8749 }
8750 
8751 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8752 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8753 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8754 {
8755 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8756 	int rc;
8757 
8758 	if (!mem_size)
8759 		return -EINVAL;
8760 
8761 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8762 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8763 		ctx_pg->nr_pages = 0;
8764 		return -EINVAL;
8765 	}
8766 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8767 		int nr_tbls, i;
8768 
8769 		rmem->depth = 2;
8770 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8771 					     GFP_KERNEL);
8772 		if (!ctx_pg->ctx_pg_tbl)
8773 			return -ENOMEM;
8774 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8775 		rmem->nr_pages = nr_tbls;
8776 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8777 		if (rc)
8778 			return rc;
8779 		for (i = 0; i < nr_tbls; i++) {
8780 			struct bnxt_ctx_pg_info *pg_tbl;
8781 
8782 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8783 			if (!pg_tbl)
8784 				return -ENOMEM;
8785 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8786 			rmem = &pg_tbl->ring_mem;
8787 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8788 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8789 			rmem->depth = 1;
8790 			rmem->nr_pages = MAX_CTX_PAGES;
8791 			rmem->ctx_mem = ctxm;
8792 			if (i == (nr_tbls - 1)) {
8793 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8794 
8795 				if (rem)
8796 					rmem->nr_pages = rem;
8797 			}
8798 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8799 			if (rc)
8800 				break;
8801 		}
8802 	} else {
8803 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8804 		if (rmem->nr_pages > 1 || depth)
8805 			rmem->depth = 1;
8806 		rmem->ctx_mem = ctxm;
8807 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8808 	}
8809 	return rc;
8810 }
8811 
8812 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
8813 				    struct bnxt_ctx_pg_info *ctx_pg,
8814 				    void *buf, size_t offset, size_t head,
8815 				    size_t tail)
8816 {
8817 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8818 	size_t nr_pages = ctx_pg->nr_pages;
8819 	int page_size = rmem->page_size;
8820 	size_t len = 0, total_len = 0;
8821 	u16 depth = rmem->depth;
8822 
8823 	tail %= nr_pages * page_size;
8824 	do {
8825 		if (depth > 1) {
8826 			int i = head / (page_size * MAX_CTX_PAGES);
8827 			struct bnxt_ctx_pg_info *pg_tbl;
8828 
8829 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8830 			rmem = &pg_tbl->ring_mem;
8831 		}
8832 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
8833 		head += len;
8834 		offset += len;
8835 		total_len += len;
8836 		if (head >= nr_pages * page_size)
8837 			head = 0;
8838 	} while (head != tail);
8839 	return total_len;
8840 }
8841 
8842 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
8843 				  struct bnxt_ctx_pg_info *ctx_pg)
8844 {
8845 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8846 
8847 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
8848 	    ctx_pg->ctx_pg_tbl) {
8849 		int i, nr_tbls = rmem->nr_pages;
8850 
8851 		for (i = 0; i < nr_tbls; i++) {
8852 			struct bnxt_ctx_pg_info *pg_tbl;
8853 			struct bnxt_ring_mem_info *rmem2;
8854 
8855 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8856 			if (!pg_tbl)
8857 				continue;
8858 			rmem2 = &pg_tbl->ring_mem;
8859 			bnxt_free_ring(bp, rmem2);
8860 			ctx_pg->ctx_pg_arr[i] = NULL;
8861 			kfree(pg_tbl);
8862 			ctx_pg->ctx_pg_tbl[i] = NULL;
8863 		}
8864 		kfree(ctx_pg->ctx_pg_tbl);
8865 		ctx_pg->ctx_pg_tbl = NULL;
8866 	}
8867 	bnxt_free_ring(bp, rmem);
8868 	ctx_pg->nr_pages = 0;
8869 }
8870 
8871 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
8872 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
8873 				   u8 pg_lvl)
8874 {
8875 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8876 	int i, rc = 0, n = 1;
8877 	u32 mem_size;
8878 
8879 	if (!ctxm->entry_size || !ctx_pg)
8880 		return -EINVAL;
8881 	if (ctxm->instance_bmap)
8882 		n = hweight32(ctxm->instance_bmap);
8883 	if (ctxm->entry_multiple)
8884 		entries = roundup(entries, ctxm->entry_multiple);
8885 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
8886 	mem_size = entries * ctxm->entry_size;
8887 	for (i = 0; i < n && !rc; i++) {
8888 		ctx_pg[i].entries = entries;
8889 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
8890 					    ctxm->init_value ? ctxm : NULL);
8891 	}
8892 	if (!rc)
8893 		ctxm->mem_valid = 1;
8894 	return rc;
8895 }
8896 
8897 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
8898 					       struct bnxt_ctx_mem_type *ctxm,
8899 					       bool last)
8900 {
8901 	struct hwrm_func_backing_store_cfg_v2_input *req;
8902 	u32 instance_bmap = ctxm->instance_bmap;
8903 	int i, j, rc = 0, n = 1;
8904 	__le32 *p;
8905 
8906 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
8907 		return 0;
8908 
8909 	if (instance_bmap)
8910 		n = hweight32(ctxm->instance_bmap);
8911 	else
8912 		instance_bmap = 1;
8913 
8914 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
8915 	if (rc)
8916 		return rc;
8917 	hwrm_req_hold(bp, req);
8918 	req->type = cpu_to_le16(ctxm->type);
8919 	req->entry_size = cpu_to_le16(ctxm->entry_size);
8920 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
8921 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
8922 		struct bnxt_bs_trace_info *bs_trace;
8923 		u32 enables;
8924 
8925 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
8926 		req->enables = cpu_to_le32(enables);
8927 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
8928 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
8929 	}
8930 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
8931 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
8932 		p[i] = cpu_to_le32(ctxm->split[i]);
8933 	for (i = 0, j = 0; j < n && !rc; i++) {
8934 		struct bnxt_ctx_pg_info *ctx_pg;
8935 
8936 		if (!(instance_bmap & (1 << i)))
8937 			continue;
8938 		req->instance = cpu_to_le16(i);
8939 		ctx_pg = &ctxm->pg_info[j++];
8940 		if (!ctx_pg->entries)
8941 			continue;
8942 		req->num_entries = cpu_to_le32(ctx_pg->entries);
8943 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8944 				      &req->page_size_pbl_level,
8945 				      &req->page_dir);
8946 		if (last && j == n)
8947 			req->flags =
8948 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
8949 		rc = hwrm_req_send(bp, req);
8950 	}
8951 	hwrm_req_drop(bp, req);
8952 	return rc;
8953 }
8954 
8955 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
8956 {
8957 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8958 	struct bnxt_ctx_mem_type *ctxm;
8959 	u16 last_type = BNXT_CTX_INV;
8960 	int rc = 0;
8961 	u16 type;
8962 
8963 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) {
8964 		ctxm = &ctx->ctx_arr[type];
8965 		if (!bnxt_bs_trace_avail(bp, type))
8966 			continue;
8967 		if (!ctxm->mem_valid) {
8968 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
8969 						     ctxm->max_entries, 1);
8970 			if (rc) {
8971 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
8972 					    type);
8973 				continue;
8974 			}
8975 			bnxt_bs_trace_init(bp, ctxm);
8976 		}
8977 		last_type = type;
8978 	}
8979 
8980 	if (last_type == BNXT_CTX_INV) {
8981 		if (!ena)
8982 			return 0;
8983 		else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
8984 			last_type = BNXT_CTX_MAX - 1;
8985 		else
8986 			last_type = BNXT_CTX_L2_MAX - 1;
8987 	}
8988 	ctx->ctx_arr[last_type].last = 1;
8989 
8990 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
8991 		ctxm = &ctx->ctx_arr[type];
8992 
8993 		if (!ctxm->mem_valid)
8994 			continue;
8995 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
8996 		if (rc)
8997 			return rc;
8998 	}
8999 	return 0;
9000 }
9001 
9002 /**
9003  * __bnxt_copy_ctx_mem - copy host context memory
9004  * @bp: The driver context
9005  * @ctxm: The pointer to the context memory type
9006  * @buf: The destination buffer or NULL to just obtain the length
9007  * @offset: The buffer offset to copy the data to
9008  * @head: The head offset of context memory to copy from
9009  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9010  *
9011  * This function is called for debugging purposes to dump the host context
9012  * used by the chip.
9013  *
9014  * Return: Length of memory copied
9015  */
9016 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9017 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9018 				  size_t offset, size_t head, size_t tail)
9019 {
9020 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9021 	size_t len = 0, total_len = 0;
9022 	int i, n = 1;
9023 
9024 	if (!ctx_pg)
9025 		return 0;
9026 
9027 	if (ctxm->instance_bmap)
9028 		n = hweight32(ctxm->instance_bmap);
9029 	for (i = 0; i < n; i++) {
9030 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9031 					    tail);
9032 		offset += len;
9033 		total_len += len;
9034 	}
9035 	return total_len;
9036 }
9037 
9038 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9039 			 void *buf, size_t offset)
9040 {
9041 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9042 
9043 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9044 }
9045 
9046 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9047 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9048 {
9049 	struct bnxt_ctx_pg_info *ctx_pg;
9050 	int i, n = 1;
9051 
9052 	ctxm->last = 0;
9053 
9054 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9055 		return;
9056 
9057 	ctx_pg = ctxm->pg_info;
9058 	if (ctx_pg) {
9059 		if (ctxm->instance_bmap)
9060 			n = hweight32(ctxm->instance_bmap);
9061 		for (i = 0; i < n; i++)
9062 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9063 
9064 		kfree(ctx_pg);
9065 		ctxm->pg_info = NULL;
9066 		ctxm->mem_valid = 0;
9067 	}
9068 	memset(ctxm, 0, sizeof(*ctxm));
9069 }
9070 
9071 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9072 {
9073 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9074 	u16 type;
9075 
9076 	if (!ctx)
9077 		return;
9078 
9079 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9080 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9081 
9082 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9083 	if (force) {
9084 		kfree(ctx);
9085 		bp->ctx = NULL;
9086 	}
9087 }
9088 
9089 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9090 {
9091 	struct bnxt_ctx_mem_type *ctxm;
9092 	struct bnxt_ctx_mem_info *ctx;
9093 	u32 l2_qps, qp1_qps, max_qps;
9094 	u32 ena, entries_sp, entries;
9095 	u32 srqs, max_srqs, min;
9096 	u32 num_mr, num_ah;
9097 	u32 extra_srqs = 0;
9098 	u32 extra_qps = 0;
9099 	u32 fast_qpmd_qps;
9100 	u8 pg_lvl = 1;
9101 	int i, rc;
9102 
9103 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9104 	if (rc) {
9105 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9106 			   rc);
9107 		return rc;
9108 	}
9109 	ctx = bp->ctx;
9110 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9111 		return 0;
9112 
9113 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9114 	l2_qps = ctxm->qp_l2_entries;
9115 	qp1_qps = ctxm->qp_qp1_entries;
9116 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9117 	max_qps = ctxm->max_entries;
9118 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9119 	srqs = ctxm->srq_l2_entries;
9120 	max_srqs = ctxm->max_entries;
9121 	ena = 0;
9122 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9123 		pg_lvl = 2;
9124 		if (BNXT_SW_RES_LMT(bp)) {
9125 			extra_qps = max_qps - l2_qps - qp1_qps;
9126 			extra_srqs = max_srqs - srqs;
9127 		} else {
9128 			extra_qps = min_t(u32, 65536,
9129 					  max_qps - l2_qps - qp1_qps);
9130 			/* allocate extra qps if fw supports RoCE fast qp
9131 			 * destroy feature
9132 			 */
9133 			extra_qps += fast_qpmd_qps;
9134 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9135 		}
9136 		if (fast_qpmd_qps)
9137 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9138 	}
9139 
9140 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9141 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9142 				     pg_lvl);
9143 	if (rc)
9144 		return rc;
9145 
9146 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9147 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9148 	if (rc)
9149 		return rc;
9150 
9151 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9152 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9153 				     extra_qps * 2, pg_lvl);
9154 	if (rc)
9155 		return rc;
9156 
9157 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9158 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9159 	if (rc)
9160 		return rc;
9161 
9162 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9163 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9164 	if (rc)
9165 		return rc;
9166 
9167 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9168 		goto skip_rdma;
9169 
9170 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9171 	if (BNXT_SW_RES_LMT(bp) &&
9172 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9173 		num_ah = ctxm->mrav_av_entries;
9174 		num_mr = ctxm->max_entries - num_ah;
9175 	} else {
9176 		/* 128K extra is needed to accommodate static AH context
9177 		 * allocation by f/w.
9178 		 */
9179 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9180 		num_ah = min_t(u32, num_mr, 1024 * 128);
9181 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9182 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9183 			ctxm->mrav_av_entries = num_ah;
9184 	}
9185 
9186 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9187 	if (rc)
9188 		return rc;
9189 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9190 
9191 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9192 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9193 	if (rc)
9194 		return rc;
9195 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9196 
9197 skip_rdma:
9198 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9199 	min = ctxm->min_entries;
9200 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9201 		     2 * (extra_qps + qp1_qps) + min;
9202 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9203 	if (rc)
9204 		return rc;
9205 
9206 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9207 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9208 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9209 	if (rc)
9210 		return rc;
9211 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9212 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9213 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9214 
9215 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9216 		rc = bnxt_backing_store_cfg_v2(bp, ena);
9217 	else
9218 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9219 	if (rc) {
9220 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9221 			   rc);
9222 		return rc;
9223 	}
9224 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9225 	return 0;
9226 }
9227 
9228 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9229 {
9230 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9231 	u16 page_attr;
9232 	int rc;
9233 
9234 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9235 		return 0;
9236 
9237 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9238 	if (rc)
9239 		return rc;
9240 
9241 	if (BNXT_PAGE_SIZE == 0x2000)
9242 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9243 	else if (BNXT_PAGE_SIZE == 0x10000)
9244 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9245 	else
9246 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9247 	req->pg_size_lvl = cpu_to_le16(page_attr |
9248 				       bp->fw_crash_mem->ring_mem.depth);
9249 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9250 	req->size = cpu_to_le32(bp->fw_crash_len);
9251 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9252 	return hwrm_req_send(bp, req);
9253 }
9254 
9255 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9256 {
9257 	if (bp->fw_crash_mem) {
9258 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9259 		kfree(bp->fw_crash_mem);
9260 		bp->fw_crash_mem = NULL;
9261 	}
9262 }
9263 
9264 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9265 {
9266 	u32 mem_size = 0;
9267 	int rc;
9268 
9269 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9270 		return 0;
9271 
9272 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9273 	if (rc)
9274 		return rc;
9275 
9276 	mem_size = round_up(mem_size, 4);
9277 
9278 	/* keep and use the existing pages */
9279 	if (bp->fw_crash_mem &&
9280 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9281 		goto alloc_done;
9282 
9283 	if (bp->fw_crash_mem)
9284 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9285 	else
9286 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
9287 					   GFP_KERNEL);
9288 	if (!bp->fw_crash_mem)
9289 		return -ENOMEM;
9290 
9291 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9292 	if (rc) {
9293 		bnxt_free_crash_dump_mem(bp);
9294 		return rc;
9295 	}
9296 
9297 alloc_done:
9298 	bp->fw_crash_len = mem_size;
9299 	return 0;
9300 }
9301 
9302 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9303 {
9304 	struct hwrm_func_resource_qcaps_output *resp;
9305 	struct hwrm_func_resource_qcaps_input *req;
9306 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9307 	int rc;
9308 
9309 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9310 	if (rc)
9311 		return rc;
9312 
9313 	req->fid = cpu_to_le16(0xffff);
9314 	resp = hwrm_req_hold(bp, req);
9315 	rc = hwrm_req_send_silent(bp, req);
9316 	if (rc)
9317 		goto hwrm_func_resc_qcaps_exit;
9318 
9319 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9320 	if (!all)
9321 		goto hwrm_func_resc_qcaps_exit;
9322 
9323 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9324 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9325 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9326 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9327 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9328 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9329 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9330 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9331 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9332 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9333 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9334 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9335 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9336 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9337 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9338 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9339 
9340 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9341 		u16 max_msix = le16_to_cpu(resp->max_msix);
9342 
9343 		hw_resc->max_nqs = max_msix;
9344 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9345 	}
9346 
9347 	if (BNXT_PF(bp)) {
9348 		struct bnxt_pf_info *pf = &bp->pf;
9349 
9350 		pf->vf_resv_strategy =
9351 			le16_to_cpu(resp->vf_reservation_strategy);
9352 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9353 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9354 	}
9355 hwrm_func_resc_qcaps_exit:
9356 	hwrm_req_drop(bp, req);
9357 	return rc;
9358 }
9359 
9360 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9361 {
9362 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9363 	struct hwrm_port_mac_ptp_qcfg_input *req;
9364 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9365 	u8 flags;
9366 	int rc;
9367 
9368 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9369 		rc = -ENODEV;
9370 		goto no_ptp;
9371 	}
9372 
9373 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9374 	if (rc)
9375 		goto no_ptp;
9376 
9377 	req->port_id = cpu_to_le16(bp->pf.port_id);
9378 	resp = hwrm_req_hold(bp, req);
9379 	rc = hwrm_req_send(bp, req);
9380 	if (rc)
9381 		goto exit;
9382 
9383 	flags = resp->flags;
9384 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9385 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9386 		rc = -ENODEV;
9387 		goto exit;
9388 	}
9389 	if (!ptp) {
9390 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9391 		if (!ptp) {
9392 			rc = -ENOMEM;
9393 			goto exit;
9394 		}
9395 		ptp->bp = bp;
9396 		bp->ptp_cfg = ptp;
9397 	}
9398 
9399 	if (flags &
9400 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9401 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9402 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9403 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9404 	} else if (BNXT_CHIP_P5(bp)) {
9405 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9406 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9407 	} else {
9408 		rc = -ENODEV;
9409 		goto exit;
9410 	}
9411 	ptp->rtc_configured =
9412 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9413 	rc = bnxt_ptp_init(bp);
9414 	if (rc)
9415 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9416 exit:
9417 	hwrm_req_drop(bp, req);
9418 	if (!rc)
9419 		return 0;
9420 
9421 no_ptp:
9422 	bnxt_ptp_clear(bp);
9423 	kfree(ptp);
9424 	bp->ptp_cfg = NULL;
9425 	return rc;
9426 }
9427 
9428 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9429 {
9430 	struct hwrm_func_qcaps_output *resp;
9431 	struct hwrm_func_qcaps_input *req;
9432 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9433 	u32 flags, flags_ext, flags_ext2;
9434 	int rc;
9435 
9436 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9437 	if (rc)
9438 		return rc;
9439 
9440 	req->fid = cpu_to_le16(0xffff);
9441 	resp = hwrm_req_hold(bp, req);
9442 	rc = hwrm_req_send(bp, req);
9443 	if (rc)
9444 		goto hwrm_func_qcaps_exit;
9445 
9446 	flags = le32_to_cpu(resp->flags);
9447 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9448 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9449 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9450 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9451 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9452 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9453 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9454 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9455 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9456 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9457 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9458 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9459 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9460 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9461 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9462 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9463 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9464 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9465 
9466 	flags_ext = le32_to_cpu(resp->flags_ext);
9467 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9468 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9469 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9470 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9471 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9472 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9473 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9474 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9475 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9476 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9477 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9478 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9479 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9480 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9481 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9482 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9483 
9484 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9485 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9486 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9487 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9488 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9489 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9490 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9491 	if (flags_ext2 &
9492 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9493 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9494 	if (BNXT_PF(bp) &&
9495 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9496 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9497 
9498 	bp->tx_push_thresh = 0;
9499 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9500 	    BNXT_FW_MAJ(bp) > 217)
9501 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9502 
9503 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9504 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9505 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9506 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9507 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9508 	if (!hw_resc->max_hw_ring_grps)
9509 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9510 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9511 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9512 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9513 
9514 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9515 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9516 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9517 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9518 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9519 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9520 
9521 	if (BNXT_PF(bp)) {
9522 		struct bnxt_pf_info *pf = &bp->pf;
9523 
9524 		pf->fw_fid = le16_to_cpu(resp->fid);
9525 		pf->port_id = le16_to_cpu(resp->port_id);
9526 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9527 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9528 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9529 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9530 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9531 			bp->flags |= BNXT_FLAG_WOL_CAP;
9532 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9533 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9534 		} else {
9535 			bnxt_ptp_clear(bp);
9536 			kfree(bp->ptp_cfg);
9537 			bp->ptp_cfg = NULL;
9538 		}
9539 	} else {
9540 #ifdef CONFIG_BNXT_SRIOV
9541 		struct bnxt_vf_info *vf = &bp->vf;
9542 
9543 		vf->fw_fid = le16_to_cpu(resp->fid);
9544 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9545 #endif
9546 	}
9547 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9548 
9549 hwrm_func_qcaps_exit:
9550 	hwrm_req_drop(bp, req);
9551 	return rc;
9552 }
9553 
9554 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9555 {
9556 	struct hwrm_dbg_qcaps_output *resp;
9557 	struct hwrm_dbg_qcaps_input *req;
9558 	int rc;
9559 
9560 	bp->fw_dbg_cap = 0;
9561 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9562 		return;
9563 
9564 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9565 	if (rc)
9566 		return;
9567 
9568 	req->fid = cpu_to_le16(0xffff);
9569 	resp = hwrm_req_hold(bp, req);
9570 	rc = hwrm_req_send(bp, req);
9571 	if (rc)
9572 		goto hwrm_dbg_qcaps_exit;
9573 
9574 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9575 
9576 hwrm_dbg_qcaps_exit:
9577 	hwrm_req_drop(bp, req);
9578 }
9579 
9580 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9581 
9582 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9583 {
9584 	int rc;
9585 
9586 	rc = __bnxt_hwrm_func_qcaps(bp);
9587 	if (rc)
9588 		return rc;
9589 
9590 	bnxt_hwrm_dbg_qcaps(bp);
9591 
9592 	rc = bnxt_hwrm_queue_qportcfg(bp);
9593 	if (rc) {
9594 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9595 		return rc;
9596 	}
9597 	if (bp->hwrm_spec_code >= 0x10803) {
9598 		rc = bnxt_alloc_ctx_mem(bp);
9599 		if (rc)
9600 			return rc;
9601 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9602 		if (!rc)
9603 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9604 	}
9605 	return 0;
9606 }
9607 
9608 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9609 {
9610 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9611 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9612 	u32 flags;
9613 	int rc;
9614 
9615 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9616 		return 0;
9617 
9618 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9619 	if (rc)
9620 		return rc;
9621 
9622 	resp = hwrm_req_hold(bp, req);
9623 	rc = hwrm_req_send(bp, req);
9624 	if (rc)
9625 		goto hwrm_cfa_adv_qcaps_exit;
9626 
9627 	flags = le32_to_cpu(resp->flags);
9628 	if (flags &
9629 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9630 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9631 
9632 	if (flags &
9633 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9634 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9635 
9636 	if (flags &
9637 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9638 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9639 
9640 hwrm_cfa_adv_qcaps_exit:
9641 	hwrm_req_drop(bp, req);
9642 	return rc;
9643 }
9644 
9645 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9646 {
9647 	if (bp->fw_health)
9648 		return 0;
9649 
9650 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9651 	if (!bp->fw_health)
9652 		return -ENOMEM;
9653 
9654 	mutex_init(&bp->fw_health->lock);
9655 	return 0;
9656 }
9657 
9658 static int bnxt_alloc_fw_health(struct bnxt *bp)
9659 {
9660 	int rc;
9661 
9662 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9663 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9664 		return 0;
9665 
9666 	rc = __bnxt_alloc_fw_health(bp);
9667 	if (rc) {
9668 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9669 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9670 		return rc;
9671 	}
9672 
9673 	return 0;
9674 }
9675 
9676 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9677 {
9678 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9679 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9680 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9681 }
9682 
9683 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9684 {
9685 	struct bnxt_fw_health *fw_health = bp->fw_health;
9686 	u32 reg_type;
9687 
9688 	if (!fw_health)
9689 		return;
9690 
9691 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9692 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9693 		fw_health->status_reliable = false;
9694 
9695 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9696 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9697 		fw_health->resets_reliable = false;
9698 }
9699 
9700 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9701 {
9702 	void __iomem *hs;
9703 	u32 status_loc;
9704 	u32 reg_type;
9705 	u32 sig;
9706 
9707 	if (bp->fw_health)
9708 		bp->fw_health->status_reliable = false;
9709 
9710 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9711 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9712 
9713 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9714 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9715 		if (!bp->chip_num) {
9716 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9717 			bp->chip_num = readl(bp->bar0 +
9718 					     BNXT_FW_HEALTH_WIN_BASE +
9719 					     BNXT_GRC_REG_CHIP_NUM);
9720 		}
9721 		if (!BNXT_CHIP_P5_PLUS(bp))
9722 			return;
9723 
9724 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9725 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9726 	} else {
9727 		status_loc = readl(hs + offsetof(struct hcomm_status,
9728 						 fw_status_loc));
9729 	}
9730 
9731 	if (__bnxt_alloc_fw_health(bp)) {
9732 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9733 		return;
9734 	}
9735 
9736 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9737 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9738 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9739 		__bnxt_map_fw_health_reg(bp, status_loc);
9740 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9741 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9742 	}
9743 
9744 	bp->fw_health->status_reliable = true;
9745 }
9746 
9747 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9748 {
9749 	struct bnxt_fw_health *fw_health = bp->fw_health;
9750 	u32 reg_base = 0xffffffff;
9751 	int i;
9752 
9753 	bp->fw_health->status_reliable = false;
9754 	bp->fw_health->resets_reliable = false;
9755 	/* Only pre-map the monitoring GRC registers using window 3 */
9756 	for (i = 0; i < 4; i++) {
9757 		u32 reg = fw_health->regs[i];
9758 
9759 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9760 			continue;
9761 		if (reg_base == 0xffffffff)
9762 			reg_base = reg & BNXT_GRC_BASE_MASK;
9763 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9764 			return -ERANGE;
9765 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9766 	}
9767 	bp->fw_health->status_reliable = true;
9768 	bp->fw_health->resets_reliable = true;
9769 	if (reg_base == 0xffffffff)
9770 		return 0;
9771 
9772 	__bnxt_map_fw_health_reg(bp, reg_base);
9773 	return 0;
9774 }
9775 
9776 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9777 {
9778 	if (!bp->fw_health)
9779 		return;
9780 
9781 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9782 		bp->fw_health->status_reliable = true;
9783 		bp->fw_health->resets_reliable = true;
9784 	} else {
9785 		bnxt_try_map_fw_health_reg(bp);
9786 	}
9787 }
9788 
9789 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9790 {
9791 	struct bnxt_fw_health *fw_health = bp->fw_health;
9792 	struct hwrm_error_recovery_qcfg_output *resp;
9793 	struct hwrm_error_recovery_qcfg_input *req;
9794 	int rc, i;
9795 
9796 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9797 		return 0;
9798 
9799 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9800 	if (rc)
9801 		return rc;
9802 
9803 	resp = hwrm_req_hold(bp, req);
9804 	rc = hwrm_req_send(bp, req);
9805 	if (rc)
9806 		goto err_recovery_out;
9807 	fw_health->flags = le32_to_cpu(resp->flags);
9808 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9809 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9810 		rc = -EINVAL;
9811 		goto err_recovery_out;
9812 	}
9813 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9814 	fw_health->master_func_wait_dsecs =
9815 		le32_to_cpu(resp->master_func_wait_period);
9816 	fw_health->normal_func_wait_dsecs =
9817 		le32_to_cpu(resp->normal_func_wait_period);
9818 	fw_health->post_reset_wait_dsecs =
9819 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9820 	fw_health->post_reset_max_wait_dsecs =
9821 		le32_to_cpu(resp->max_bailout_time_after_reset);
9822 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9823 		le32_to_cpu(resp->fw_health_status_reg);
9824 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9825 		le32_to_cpu(resp->fw_heartbeat_reg);
9826 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9827 		le32_to_cpu(resp->fw_reset_cnt_reg);
9828 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9829 		le32_to_cpu(resp->reset_inprogress_reg);
9830 	fw_health->fw_reset_inprog_reg_mask =
9831 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9832 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9833 	if (fw_health->fw_reset_seq_cnt >= 16) {
9834 		rc = -EINVAL;
9835 		goto err_recovery_out;
9836 	}
9837 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9838 		fw_health->fw_reset_seq_regs[i] =
9839 			le32_to_cpu(resp->reset_reg[i]);
9840 		fw_health->fw_reset_seq_vals[i] =
9841 			le32_to_cpu(resp->reset_reg_val[i]);
9842 		fw_health->fw_reset_seq_delay_msec[i] =
9843 			resp->delay_after_reset[i];
9844 	}
9845 err_recovery_out:
9846 	hwrm_req_drop(bp, req);
9847 	if (!rc)
9848 		rc = bnxt_map_fw_health_regs(bp);
9849 	if (rc)
9850 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9851 	return rc;
9852 }
9853 
9854 static int bnxt_hwrm_func_reset(struct bnxt *bp)
9855 {
9856 	struct hwrm_func_reset_input *req;
9857 	int rc;
9858 
9859 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
9860 	if (rc)
9861 		return rc;
9862 
9863 	req->enables = 0;
9864 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
9865 	return hwrm_req_send(bp, req);
9866 }
9867 
9868 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
9869 {
9870 	struct hwrm_nvm_get_dev_info_output nvm_info;
9871 
9872 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
9873 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
9874 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
9875 			 nvm_info.nvm_cfg_ver_upd);
9876 }
9877 
9878 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
9879 {
9880 	struct hwrm_queue_qportcfg_output *resp;
9881 	struct hwrm_queue_qportcfg_input *req;
9882 	u8 i, j, *qptr;
9883 	bool no_rdma;
9884 	int rc = 0;
9885 
9886 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
9887 	if (rc)
9888 		return rc;
9889 
9890 	resp = hwrm_req_hold(bp, req);
9891 	rc = hwrm_req_send(bp, req);
9892 	if (rc)
9893 		goto qportcfg_exit;
9894 
9895 	if (!resp->max_configurable_queues) {
9896 		rc = -EINVAL;
9897 		goto qportcfg_exit;
9898 	}
9899 	bp->max_tc = resp->max_configurable_queues;
9900 	bp->max_lltc = resp->max_configurable_lossless_queues;
9901 	if (bp->max_tc > BNXT_MAX_QUEUE)
9902 		bp->max_tc = BNXT_MAX_QUEUE;
9903 
9904 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
9905 	qptr = &resp->queue_id0;
9906 	for (i = 0, j = 0; i < bp->max_tc; i++) {
9907 		bp->q_info[j].queue_id = *qptr;
9908 		bp->q_ids[i] = *qptr++;
9909 		bp->q_info[j].queue_profile = *qptr++;
9910 		bp->tc_to_qidx[j] = j;
9911 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
9912 		    (no_rdma && BNXT_PF(bp)))
9913 			j++;
9914 	}
9915 	bp->max_q = bp->max_tc;
9916 	bp->max_tc = max_t(u8, j, 1);
9917 
9918 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
9919 		bp->max_tc = 1;
9920 
9921 	if (bp->max_lltc > bp->max_tc)
9922 		bp->max_lltc = bp->max_tc;
9923 
9924 qportcfg_exit:
9925 	hwrm_req_drop(bp, req);
9926 	return rc;
9927 }
9928 
9929 static int bnxt_hwrm_poll(struct bnxt *bp)
9930 {
9931 	struct hwrm_ver_get_input *req;
9932 	int rc;
9933 
9934 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9935 	if (rc)
9936 		return rc;
9937 
9938 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9939 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9940 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9941 
9942 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
9943 	rc = hwrm_req_send(bp, req);
9944 	return rc;
9945 }
9946 
9947 static int bnxt_hwrm_ver_get(struct bnxt *bp)
9948 {
9949 	struct hwrm_ver_get_output *resp;
9950 	struct hwrm_ver_get_input *req;
9951 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
9952 	u32 dev_caps_cfg, hwrm_ver;
9953 	int rc, len;
9954 
9955 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9956 	if (rc)
9957 		return rc;
9958 
9959 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
9960 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
9961 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9962 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9963 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9964 
9965 	resp = hwrm_req_hold(bp, req);
9966 	rc = hwrm_req_send(bp, req);
9967 	if (rc)
9968 		goto hwrm_ver_get_exit;
9969 
9970 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
9971 
9972 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
9973 			     resp->hwrm_intf_min_8b << 8 |
9974 			     resp->hwrm_intf_upd_8b;
9975 	if (resp->hwrm_intf_maj_8b < 1) {
9976 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
9977 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9978 			    resp->hwrm_intf_upd_8b);
9979 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
9980 	}
9981 
9982 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
9983 			HWRM_VERSION_UPDATE;
9984 
9985 	if (bp->hwrm_spec_code > hwrm_ver)
9986 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9987 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
9988 			 HWRM_VERSION_UPDATE);
9989 	else
9990 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9991 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9992 			 resp->hwrm_intf_upd_8b);
9993 
9994 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
9995 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
9996 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
9997 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
9998 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
9999 		len = FW_VER_STR_LEN;
10000 	} else {
10001 		fw_maj = resp->hwrm_fw_maj_8b;
10002 		fw_min = resp->hwrm_fw_min_8b;
10003 		fw_bld = resp->hwrm_fw_bld_8b;
10004 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10005 		len = BC_HWRM_STR_LEN;
10006 	}
10007 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10008 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10009 		 fw_rsv);
10010 
10011 	if (strlen(resp->active_pkg_name)) {
10012 		int fw_ver_len = strlen(bp->fw_ver_str);
10013 
10014 		snprintf(bp->fw_ver_str + fw_ver_len,
10015 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10016 			 resp->active_pkg_name);
10017 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10018 	}
10019 
10020 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10021 	if (!bp->hwrm_cmd_timeout)
10022 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10023 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10024 	if (!bp->hwrm_cmd_max_timeout)
10025 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10026 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
10027 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
10028 			    bp->hwrm_cmd_max_timeout / 1000);
10029 
10030 	if (resp->hwrm_intf_maj_8b >= 1) {
10031 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10032 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10033 	}
10034 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10035 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10036 
10037 	bp->chip_num = le16_to_cpu(resp->chip_num);
10038 	bp->chip_rev = resp->chip_rev;
10039 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10040 	    !resp->chip_metal)
10041 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10042 
10043 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10044 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10045 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10046 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10047 
10048 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10049 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10050 
10051 	if (dev_caps_cfg &
10052 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10053 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10054 
10055 	if (dev_caps_cfg &
10056 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10057 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10058 
10059 	if (dev_caps_cfg &
10060 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10061 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10062 
10063 hwrm_ver_get_exit:
10064 	hwrm_req_drop(bp, req);
10065 	return rc;
10066 }
10067 
10068 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10069 {
10070 	struct hwrm_fw_set_time_input *req;
10071 	struct tm tm;
10072 	time64_t now = ktime_get_real_seconds();
10073 	int rc;
10074 
10075 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10076 	    bp->hwrm_spec_code < 0x10400)
10077 		return -EOPNOTSUPP;
10078 
10079 	time64_to_tm(now, 0, &tm);
10080 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10081 	if (rc)
10082 		return rc;
10083 
10084 	req->year = cpu_to_le16(1900 + tm.tm_year);
10085 	req->month = 1 + tm.tm_mon;
10086 	req->day = tm.tm_mday;
10087 	req->hour = tm.tm_hour;
10088 	req->minute = tm.tm_min;
10089 	req->second = tm.tm_sec;
10090 	return hwrm_req_send(bp, req);
10091 }
10092 
10093 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10094 {
10095 	u64 sw_tmp;
10096 
10097 	hw &= mask;
10098 	sw_tmp = (*sw & ~mask) | hw;
10099 	if (hw < (*sw & mask))
10100 		sw_tmp += mask + 1;
10101 	WRITE_ONCE(*sw, sw_tmp);
10102 }
10103 
10104 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10105 				    int count, bool ignore_zero)
10106 {
10107 	int i;
10108 
10109 	for (i = 0; i < count; i++) {
10110 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10111 
10112 		if (ignore_zero && !hw)
10113 			continue;
10114 
10115 		if (masks[i] == -1ULL)
10116 			sw_stats[i] = hw;
10117 		else
10118 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10119 	}
10120 }
10121 
10122 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10123 {
10124 	if (!stats->hw_stats)
10125 		return;
10126 
10127 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10128 				stats->hw_masks, stats->len / 8, false);
10129 }
10130 
10131 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10132 {
10133 	struct bnxt_stats_mem *ring0_stats;
10134 	bool ignore_zero = false;
10135 	int i;
10136 
10137 	/* Chip bug.  Counter intermittently becomes 0. */
10138 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10139 		ignore_zero = true;
10140 
10141 	for (i = 0; i < bp->cp_nr_rings; i++) {
10142 		struct bnxt_napi *bnapi = bp->bnapi[i];
10143 		struct bnxt_cp_ring_info *cpr;
10144 		struct bnxt_stats_mem *stats;
10145 
10146 		cpr = &bnapi->cp_ring;
10147 		stats = &cpr->stats;
10148 		if (!i)
10149 			ring0_stats = stats;
10150 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10151 					ring0_stats->hw_masks,
10152 					ring0_stats->len / 8, ignore_zero);
10153 	}
10154 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10155 		struct bnxt_stats_mem *stats = &bp->port_stats;
10156 		__le64 *hw_stats = stats->hw_stats;
10157 		u64 *sw_stats = stats->sw_stats;
10158 		u64 *masks = stats->hw_masks;
10159 		int cnt;
10160 
10161 		cnt = sizeof(struct rx_port_stats) / 8;
10162 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10163 
10164 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10165 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10166 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10167 		cnt = sizeof(struct tx_port_stats) / 8;
10168 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10169 	}
10170 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10171 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10172 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10173 	}
10174 }
10175 
10176 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10177 {
10178 	struct hwrm_port_qstats_input *req;
10179 	struct bnxt_pf_info *pf = &bp->pf;
10180 	int rc;
10181 
10182 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10183 		return 0;
10184 
10185 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10186 		return -EOPNOTSUPP;
10187 
10188 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10189 	if (rc)
10190 		return rc;
10191 
10192 	req->flags = flags;
10193 	req->port_id = cpu_to_le16(pf->port_id);
10194 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10195 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10196 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10197 	return hwrm_req_send(bp, req);
10198 }
10199 
10200 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10201 {
10202 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10203 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10204 	struct hwrm_port_qstats_ext_output *resp_qs;
10205 	struct hwrm_port_qstats_ext_input *req_qs;
10206 	struct bnxt_pf_info *pf = &bp->pf;
10207 	u32 tx_stat_size;
10208 	int rc;
10209 
10210 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10211 		return 0;
10212 
10213 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10214 		return -EOPNOTSUPP;
10215 
10216 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10217 	if (rc)
10218 		return rc;
10219 
10220 	req_qs->flags = flags;
10221 	req_qs->port_id = cpu_to_le16(pf->port_id);
10222 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10223 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10224 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10225 		       sizeof(struct tx_port_stats_ext) : 0;
10226 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10227 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10228 	resp_qs = hwrm_req_hold(bp, req_qs);
10229 	rc = hwrm_req_send(bp, req_qs);
10230 	if (!rc) {
10231 		bp->fw_rx_stats_ext_size =
10232 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10233 		if (BNXT_FW_MAJ(bp) < 220 &&
10234 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10235 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10236 
10237 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10238 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10239 	} else {
10240 		bp->fw_rx_stats_ext_size = 0;
10241 		bp->fw_tx_stats_ext_size = 0;
10242 	}
10243 	hwrm_req_drop(bp, req_qs);
10244 
10245 	if (flags)
10246 		return rc;
10247 
10248 	if (bp->fw_tx_stats_ext_size <=
10249 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10250 		bp->pri2cos_valid = 0;
10251 		return rc;
10252 	}
10253 
10254 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10255 	if (rc)
10256 		return rc;
10257 
10258 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10259 
10260 	resp_qc = hwrm_req_hold(bp, req_qc);
10261 	rc = hwrm_req_send(bp, req_qc);
10262 	if (!rc) {
10263 		u8 *pri2cos;
10264 		int i, j;
10265 
10266 		pri2cos = &resp_qc->pri0_cos_queue_id;
10267 		for (i = 0; i < 8; i++) {
10268 			u8 queue_id = pri2cos[i];
10269 			u8 queue_idx;
10270 
10271 			/* Per port queue IDs start from 0, 10, 20, etc */
10272 			queue_idx = queue_id % 10;
10273 			if (queue_idx > BNXT_MAX_QUEUE) {
10274 				bp->pri2cos_valid = false;
10275 				hwrm_req_drop(bp, req_qc);
10276 				return rc;
10277 			}
10278 			for (j = 0; j < bp->max_q; j++) {
10279 				if (bp->q_ids[j] == queue_id)
10280 					bp->pri2cos_idx[i] = queue_idx;
10281 			}
10282 		}
10283 		bp->pri2cos_valid = true;
10284 	}
10285 	hwrm_req_drop(bp, req_qc);
10286 
10287 	return rc;
10288 }
10289 
10290 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10291 {
10292 	bnxt_hwrm_tunnel_dst_port_free(bp,
10293 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10294 	bnxt_hwrm_tunnel_dst_port_free(bp,
10295 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10296 }
10297 
10298 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10299 {
10300 	int rc, i;
10301 	u32 tpa_flags = 0;
10302 
10303 	if (set_tpa)
10304 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10305 	else if (BNXT_NO_FW_ACCESS(bp))
10306 		return 0;
10307 	for (i = 0; i < bp->nr_vnics; i++) {
10308 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10309 		if (rc) {
10310 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10311 				   i, rc);
10312 			return rc;
10313 		}
10314 	}
10315 	return 0;
10316 }
10317 
10318 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10319 {
10320 	int i;
10321 
10322 	for (i = 0; i < bp->nr_vnics; i++)
10323 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10324 }
10325 
10326 static void bnxt_clear_vnic(struct bnxt *bp)
10327 {
10328 	if (!bp->vnic_info)
10329 		return;
10330 
10331 	bnxt_hwrm_clear_vnic_filter(bp);
10332 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10333 		/* clear all RSS setting before free vnic ctx */
10334 		bnxt_hwrm_clear_vnic_rss(bp);
10335 		bnxt_hwrm_vnic_ctx_free(bp);
10336 	}
10337 	/* before free the vnic, undo the vnic tpa settings */
10338 	if (bp->flags & BNXT_FLAG_TPA)
10339 		bnxt_set_tpa(bp, false);
10340 	bnxt_hwrm_vnic_free(bp);
10341 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10342 		bnxt_hwrm_vnic_ctx_free(bp);
10343 }
10344 
10345 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10346 				    bool irq_re_init)
10347 {
10348 	bnxt_clear_vnic(bp);
10349 	bnxt_hwrm_ring_free(bp, close_path);
10350 	bnxt_hwrm_ring_grp_free(bp);
10351 	if (irq_re_init) {
10352 		bnxt_hwrm_stat_ctx_free(bp);
10353 		bnxt_hwrm_free_tunnel_ports(bp);
10354 	}
10355 }
10356 
10357 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10358 {
10359 	struct hwrm_func_cfg_input *req;
10360 	u8 evb_mode;
10361 	int rc;
10362 
10363 	if (br_mode == BRIDGE_MODE_VEB)
10364 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10365 	else if (br_mode == BRIDGE_MODE_VEPA)
10366 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10367 	else
10368 		return -EINVAL;
10369 
10370 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10371 	if (rc)
10372 		return rc;
10373 
10374 	req->fid = cpu_to_le16(0xffff);
10375 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10376 	req->evb_mode = evb_mode;
10377 	return hwrm_req_send(bp, req);
10378 }
10379 
10380 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10381 {
10382 	struct hwrm_func_cfg_input *req;
10383 	int rc;
10384 
10385 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10386 		return 0;
10387 
10388 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10389 	if (rc)
10390 		return rc;
10391 
10392 	req->fid = cpu_to_le16(0xffff);
10393 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10394 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10395 	if (size == 128)
10396 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10397 
10398 	return hwrm_req_send(bp, req);
10399 }
10400 
10401 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10402 {
10403 	int rc;
10404 
10405 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10406 		goto skip_rss_ctx;
10407 
10408 	/* allocate context for vnic */
10409 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10410 	if (rc) {
10411 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10412 			   vnic->vnic_id, rc);
10413 		goto vnic_setup_err;
10414 	}
10415 	bp->rsscos_nr_ctxs++;
10416 
10417 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10418 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10419 		if (rc) {
10420 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10421 				   vnic->vnic_id, rc);
10422 			goto vnic_setup_err;
10423 		}
10424 		bp->rsscos_nr_ctxs++;
10425 	}
10426 
10427 skip_rss_ctx:
10428 	/* configure default vnic, ring grp */
10429 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10430 	if (rc) {
10431 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10432 			   vnic->vnic_id, rc);
10433 		goto vnic_setup_err;
10434 	}
10435 
10436 	/* Enable RSS hashing on vnic */
10437 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10438 	if (rc) {
10439 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10440 			   vnic->vnic_id, rc);
10441 		goto vnic_setup_err;
10442 	}
10443 
10444 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10445 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10446 		if (rc) {
10447 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10448 				   vnic->vnic_id, rc);
10449 		}
10450 	}
10451 
10452 vnic_setup_err:
10453 	return rc;
10454 }
10455 
10456 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10457 			  u8 valid)
10458 {
10459 	struct hwrm_vnic_update_input *req;
10460 	int rc;
10461 
10462 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10463 	if (rc)
10464 		return rc;
10465 
10466 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10467 
10468 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10469 		req->mru = cpu_to_le16(vnic->mru);
10470 
10471 	req->enables = cpu_to_le32(valid);
10472 
10473 	return hwrm_req_send(bp, req);
10474 }
10475 
10476 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10477 {
10478 	int rc;
10479 
10480 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10481 	if (rc) {
10482 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10483 			   vnic->vnic_id, rc);
10484 		return rc;
10485 	}
10486 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10487 	if (rc)
10488 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10489 			   vnic->vnic_id, rc);
10490 	return rc;
10491 }
10492 
10493 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10494 {
10495 	int rc, i, nr_ctxs;
10496 
10497 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10498 	for (i = 0; i < nr_ctxs; i++) {
10499 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10500 		if (rc) {
10501 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10502 				   vnic->vnic_id, i, rc);
10503 			break;
10504 		}
10505 		bp->rsscos_nr_ctxs++;
10506 	}
10507 	if (i < nr_ctxs)
10508 		return -ENOMEM;
10509 
10510 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10511 	if (rc)
10512 		return rc;
10513 
10514 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10515 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10516 		if (rc) {
10517 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10518 				   vnic->vnic_id, rc);
10519 		}
10520 	}
10521 	return rc;
10522 }
10523 
10524 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10525 {
10526 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10527 		return __bnxt_setup_vnic_p5(bp, vnic);
10528 	else
10529 		return __bnxt_setup_vnic(bp, vnic);
10530 }
10531 
10532 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10533 				     struct bnxt_vnic_info *vnic,
10534 				     u16 start_rx_ring_idx, int rx_rings)
10535 {
10536 	int rc;
10537 
10538 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10539 	if (rc) {
10540 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10541 			   vnic->vnic_id, rc);
10542 		return rc;
10543 	}
10544 	return bnxt_setup_vnic(bp, vnic);
10545 }
10546 
10547 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10548 {
10549 	struct bnxt_vnic_info *vnic;
10550 	int i, rc = 0;
10551 
10552 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10553 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10554 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10555 	}
10556 
10557 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10558 		return 0;
10559 
10560 	for (i = 0; i < bp->rx_nr_rings; i++) {
10561 		u16 vnic_id = i + 1;
10562 		u16 ring_id = i;
10563 
10564 		if (vnic_id >= bp->nr_vnics)
10565 			break;
10566 
10567 		vnic = &bp->vnic_info[vnic_id];
10568 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10569 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10570 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10571 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10572 			break;
10573 	}
10574 	return rc;
10575 }
10576 
10577 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10578 			  bool all)
10579 {
10580 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10581 	struct bnxt_filter_base *usr_fltr, *tmp;
10582 	struct bnxt_ntuple_filter *ntp_fltr;
10583 	int i;
10584 
10585 	if (netif_running(bp->dev)) {
10586 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10587 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10588 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10589 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10590 		}
10591 	}
10592 	if (!all)
10593 		return;
10594 
10595 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10596 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10597 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10598 			ntp_fltr = container_of(usr_fltr,
10599 						struct bnxt_ntuple_filter,
10600 						base);
10601 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10602 			bnxt_del_ntp_filter(bp, ntp_fltr);
10603 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10604 		}
10605 	}
10606 
10607 	if (vnic->rss_table)
10608 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10609 				  vnic->rss_table,
10610 				  vnic->rss_table_dma_addr);
10611 	bp->num_rss_ctx--;
10612 }
10613 
10614 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10615 {
10616 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10617 	struct ethtool_rxfh_context *ctx;
10618 	unsigned long context;
10619 
10620 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10621 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10622 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10623 
10624 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10625 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10626 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10627 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10628 				   rss_ctx->index);
10629 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10630 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10631 		}
10632 	}
10633 }
10634 
10635 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10636 {
10637 	struct ethtool_rxfh_context *ctx;
10638 	unsigned long context;
10639 
10640 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10641 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10642 
10643 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10644 	}
10645 }
10646 
10647 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10648 static bool bnxt_promisc_ok(struct bnxt *bp)
10649 {
10650 #ifdef CONFIG_BNXT_SRIOV
10651 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10652 		return false;
10653 #endif
10654 	return true;
10655 }
10656 
10657 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10658 {
10659 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10660 	unsigned int rc = 0;
10661 
10662 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10663 	if (rc) {
10664 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10665 			   rc);
10666 		return rc;
10667 	}
10668 
10669 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10670 	if (rc) {
10671 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10672 			   rc);
10673 		return rc;
10674 	}
10675 	return rc;
10676 }
10677 
10678 static int bnxt_cfg_rx_mode(struct bnxt *);
10679 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10680 
10681 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10682 {
10683 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10684 	int rc = 0;
10685 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10686 
10687 	if (irq_re_init) {
10688 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10689 		if (rc) {
10690 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10691 				   rc);
10692 			goto err_out;
10693 		}
10694 	}
10695 
10696 	rc = bnxt_hwrm_ring_alloc(bp);
10697 	if (rc) {
10698 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10699 		goto err_out;
10700 	}
10701 
10702 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10703 	if (rc) {
10704 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10705 		goto err_out;
10706 	}
10707 
10708 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10709 		rx_nr_rings--;
10710 
10711 	/* default vnic 0 */
10712 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10713 	if (rc) {
10714 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10715 		goto err_out;
10716 	}
10717 
10718 	if (BNXT_VF(bp))
10719 		bnxt_hwrm_func_qcfg(bp);
10720 
10721 	rc = bnxt_setup_vnic(bp, vnic);
10722 	if (rc)
10723 		goto err_out;
10724 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10725 		bnxt_hwrm_update_rss_hash_cfg(bp);
10726 
10727 	if (bp->flags & BNXT_FLAG_RFS) {
10728 		rc = bnxt_alloc_rfs_vnics(bp);
10729 		if (rc)
10730 			goto err_out;
10731 	}
10732 
10733 	if (bp->flags & BNXT_FLAG_TPA) {
10734 		rc = bnxt_set_tpa(bp, true);
10735 		if (rc)
10736 			goto err_out;
10737 	}
10738 
10739 	if (BNXT_VF(bp))
10740 		bnxt_update_vf_mac(bp);
10741 
10742 	/* Filter for default vnic 0 */
10743 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10744 	if (rc) {
10745 		if (BNXT_VF(bp) && rc == -ENODEV)
10746 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10747 		else
10748 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10749 		goto err_out;
10750 	}
10751 	vnic->uc_filter_count = 1;
10752 
10753 	vnic->rx_mask = 0;
10754 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10755 		goto skip_rx_mask;
10756 
10757 	if (bp->dev->flags & IFF_BROADCAST)
10758 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10759 
10760 	if (bp->dev->flags & IFF_PROMISC)
10761 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10762 
10763 	if (bp->dev->flags & IFF_ALLMULTI) {
10764 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10765 		vnic->mc_list_count = 0;
10766 	} else if (bp->dev->flags & IFF_MULTICAST) {
10767 		u32 mask = 0;
10768 
10769 		bnxt_mc_list_updated(bp, &mask);
10770 		vnic->rx_mask |= mask;
10771 	}
10772 
10773 	rc = bnxt_cfg_rx_mode(bp);
10774 	if (rc)
10775 		goto err_out;
10776 
10777 skip_rx_mask:
10778 	rc = bnxt_hwrm_set_coal(bp);
10779 	if (rc)
10780 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10781 				rc);
10782 
10783 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10784 		rc = bnxt_setup_nitroa0_vnic(bp);
10785 		if (rc)
10786 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10787 				   rc);
10788 	}
10789 
10790 	if (BNXT_VF(bp)) {
10791 		bnxt_hwrm_func_qcfg(bp);
10792 		netdev_update_features(bp->dev);
10793 	}
10794 
10795 	return 0;
10796 
10797 err_out:
10798 	bnxt_hwrm_resource_free(bp, 0, true);
10799 
10800 	return rc;
10801 }
10802 
10803 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10804 {
10805 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10806 	return 0;
10807 }
10808 
10809 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10810 {
10811 	bnxt_init_cp_rings(bp);
10812 	bnxt_init_rx_rings(bp);
10813 	bnxt_init_tx_rings(bp);
10814 	bnxt_init_ring_grps(bp, irq_re_init);
10815 	bnxt_init_vnics(bp);
10816 
10817 	return bnxt_init_chip(bp, irq_re_init);
10818 }
10819 
10820 static int bnxt_set_real_num_queues(struct bnxt *bp)
10821 {
10822 	int rc;
10823 	struct net_device *dev = bp->dev;
10824 
10825 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10826 					  bp->tx_nr_rings_xdp);
10827 	if (rc)
10828 		return rc;
10829 
10830 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10831 	if (rc)
10832 		return rc;
10833 
10834 #ifdef CONFIG_RFS_ACCEL
10835 	if (bp->flags & BNXT_FLAG_RFS)
10836 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
10837 #endif
10838 
10839 	return rc;
10840 }
10841 
10842 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10843 			     bool shared)
10844 {
10845 	int _rx = *rx, _tx = *tx;
10846 
10847 	if (shared) {
10848 		*rx = min_t(int, _rx, max);
10849 		*tx = min_t(int, _tx, max);
10850 	} else {
10851 		if (max < 2)
10852 			return -ENOMEM;
10853 
10854 		while (_rx + _tx > max) {
10855 			if (_rx > _tx && _rx > 1)
10856 				_rx--;
10857 			else if (_tx > 1)
10858 				_tx--;
10859 		}
10860 		*rx = _rx;
10861 		*tx = _tx;
10862 	}
10863 	return 0;
10864 }
10865 
10866 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
10867 {
10868 	return (tx - tx_xdp) / tx_sets + tx_xdp;
10869 }
10870 
10871 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
10872 {
10873 	int tcs = bp->num_tc;
10874 
10875 	if (!tcs)
10876 		tcs = 1;
10877 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
10878 }
10879 
10880 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
10881 {
10882 	int tcs = bp->num_tc;
10883 
10884 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
10885 	       bp->tx_nr_rings_xdp;
10886 }
10887 
10888 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10889 			   bool sh)
10890 {
10891 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
10892 
10893 	if (tx_cp != *tx) {
10894 		int tx_saved = tx_cp, rc;
10895 
10896 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
10897 		if (rc)
10898 			return rc;
10899 		if (tx_cp != tx_saved)
10900 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
10901 		return 0;
10902 	}
10903 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
10904 }
10905 
10906 static void bnxt_setup_msix(struct bnxt *bp)
10907 {
10908 	const int len = sizeof(bp->irq_tbl[0].name);
10909 	struct net_device *dev = bp->dev;
10910 	int tcs, i;
10911 
10912 	tcs = bp->num_tc;
10913 	if (tcs) {
10914 		int i, off, count;
10915 
10916 		for (i = 0; i < tcs; i++) {
10917 			count = bp->tx_nr_rings_per_tc;
10918 			off = BNXT_TC_TO_RING_BASE(bp, i);
10919 			netdev_set_tc_queue(dev, i, count, off);
10920 		}
10921 	}
10922 
10923 	for (i = 0; i < bp->cp_nr_rings; i++) {
10924 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10925 		char *attr;
10926 
10927 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
10928 			attr = "TxRx";
10929 		else if (i < bp->rx_nr_rings)
10930 			attr = "rx";
10931 		else
10932 			attr = "tx";
10933 
10934 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
10935 			 attr, i);
10936 		bp->irq_tbl[map_idx].handler = bnxt_msix;
10937 	}
10938 }
10939 
10940 static int bnxt_init_int_mode(struct bnxt *bp);
10941 
10942 static int bnxt_change_msix(struct bnxt *bp, int total)
10943 {
10944 	struct msi_map map;
10945 	int i;
10946 
10947 	/* add MSIX to the end if needed */
10948 	for (i = bp->total_irqs; i < total; i++) {
10949 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
10950 		if (map.index < 0)
10951 			return bp->total_irqs;
10952 		bp->irq_tbl[i].vector = map.virq;
10953 		bp->total_irqs++;
10954 	}
10955 
10956 	/* trim MSIX from the end if needed */
10957 	for (i = bp->total_irqs; i > total; i--) {
10958 		map.index = i - 1;
10959 		map.virq = bp->irq_tbl[i - 1].vector;
10960 		pci_msix_free_irq(bp->pdev, map);
10961 		bp->total_irqs--;
10962 	}
10963 	return bp->total_irqs;
10964 }
10965 
10966 static int bnxt_setup_int_mode(struct bnxt *bp)
10967 {
10968 	int rc;
10969 
10970 	if (!bp->irq_tbl) {
10971 		rc = bnxt_init_int_mode(bp);
10972 		if (rc || !bp->irq_tbl)
10973 			return rc ?: -ENODEV;
10974 	}
10975 
10976 	bnxt_setup_msix(bp);
10977 
10978 	rc = bnxt_set_real_num_queues(bp);
10979 	return rc;
10980 }
10981 
10982 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
10983 {
10984 	return bp->hw_resc.max_rsscos_ctxs;
10985 }
10986 
10987 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
10988 {
10989 	return bp->hw_resc.max_vnics;
10990 }
10991 
10992 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
10993 {
10994 	return bp->hw_resc.max_stat_ctxs;
10995 }
10996 
10997 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
10998 {
10999 	return bp->hw_resc.max_cp_rings;
11000 }
11001 
11002 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11003 {
11004 	unsigned int cp = bp->hw_resc.max_cp_rings;
11005 
11006 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11007 		cp -= bnxt_get_ulp_msix_num(bp);
11008 
11009 	return cp;
11010 }
11011 
11012 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11013 {
11014 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11015 
11016 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11017 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11018 
11019 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11020 }
11021 
11022 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11023 {
11024 	bp->hw_resc.max_irqs = max_irqs;
11025 }
11026 
11027 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11028 {
11029 	unsigned int cp;
11030 
11031 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11032 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11033 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11034 	else
11035 		return cp - bp->cp_nr_rings;
11036 }
11037 
11038 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11039 {
11040 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11041 }
11042 
11043 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11044 {
11045 	int max_irq = bnxt_get_max_func_irqs(bp);
11046 	int total_req = bp->cp_nr_rings + num;
11047 
11048 	if (max_irq < total_req) {
11049 		num = max_irq - bp->cp_nr_rings;
11050 		if (num <= 0)
11051 			return 0;
11052 	}
11053 	return num;
11054 }
11055 
11056 static int bnxt_get_num_msix(struct bnxt *bp)
11057 {
11058 	if (!BNXT_NEW_RM(bp))
11059 		return bnxt_get_max_func_irqs(bp);
11060 
11061 	return bnxt_nq_rings_in_use(bp);
11062 }
11063 
11064 static int bnxt_init_int_mode(struct bnxt *bp)
11065 {
11066 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11067 
11068 	total_vecs = bnxt_get_num_msix(bp);
11069 	max = bnxt_get_max_func_irqs(bp);
11070 	if (total_vecs > max)
11071 		total_vecs = max;
11072 
11073 	if (!total_vecs)
11074 		return 0;
11075 
11076 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11077 		min = 2;
11078 
11079 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11080 					   PCI_IRQ_MSIX);
11081 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11082 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11083 		rc = -ENODEV;
11084 		goto msix_setup_exit;
11085 	}
11086 
11087 	tbl_size = total_vecs;
11088 	if (pci_msix_can_alloc_dyn(bp->pdev))
11089 		tbl_size = max;
11090 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
11091 	if (bp->irq_tbl) {
11092 		for (i = 0; i < total_vecs; i++)
11093 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11094 
11095 		bp->total_irqs = total_vecs;
11096 		/* Trim rings based upon num of vectors allocated */
11097 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11098 				     total_vecs - ulp_msix, min == 1);
11099 		if (rc)
11100 			goto msix_setup_exit;
11101 
11102 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11103 		bp->cp_nr_rings = (min == 1) ?
11104 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11105 				  tx_cp + bp->rx_nr_rings;
11106 
11107 	} else {
11108 		rc = -ENOMEM;
11109 		goto msix_setup_exit;
11110 	}
11111 	return 0;
11112 
11113 msix_setup_exit:
11114 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11115 	kfree(bp->irq_tbl);
11116 	bp->irq_tbl = NULL;
11117 	pci_free_irq_vectors(bp->pdev);
11118 	return rc;
11119 }
11120 
11121 static void bnxt_clear_int_mode(struct bnxt *bp)
11122 {
11123 	pci_free_irq_vectors(bp->pdev);
11124 
11125 	kfree(bp->irq_tbl);
11126 	bp->irq_tbl = NULL;
11127 }
11128 
11129 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11130 {
11131 	bool irq_cleared = false;
11132 	bool irq_change = false;
11133 	int tcs = bp->num_tc;
11134 	int irqs_required;
11135 	int rc;
11136 
11137 	if (!bnxt_need_reserve_rings(bp))
11138 		return 0;
11139 
11140 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11141 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11142 
11143 		if (ulp_msix > bp->ulp_num_msix_want)
11144 			ulp_msix = bp->ulp_num_msix_want;
11145 		irqs_required = ulp_msix + bp->cp_nr_rings;
11146 	} else {
11147 		irqs_required = bnxt_get_num_msix(bp);
11148 	}
11149 
11150 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11151 		irq_change = true;
11152 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11153 			bnxt_ulp_irq_stop(bp);
11154 			bnxt_clear_int_mode(bp);
11155 			irq_cleared = true;
11156 		}
11157 	}
11158 	rc = __bnxt_reserve_rings(bp);
11159 	if (irq_cleared) {
11160 		if (!rc)
11161 			rc = bnxt_init_int_mode(bp);
11162 		bnxt_ulp_irq_restart(bp, rc);
11163 	} else if (irq_change && !rc) {
11164 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11165 			rc = -ENOSPC;
11166 	}
11167 	if (rc) {
11168 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11169 		return rc;
11170 	}
11171 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11172 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11173 		netdev_err(bp->dev, "tx ring reservation failure\n");
11174 		netdev_reset_tc(bp->dev);
11175 		bp->num_tc = 0;
11176 		if (bp->tx_nr_rings_xdp)
11177 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11178 		else
11179 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11180 		return -ENOMEM;
11181 	}
11182 	return 0;
11183 }
11184 
11185 static void bnxt_free_irq(struct bnxt *bp)
11186 {
11187 	struct bnxt_irq *irq;
11188 	int i;
11189 
11190 #ifdef CONFIG_RFS_ACCEL
11191 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11192 	bp->dev->rx_cpu_rmap = NULL;
11193 #endif
11194 	if (!bp->irq_tbl || !bp->bnapi)
11195 		return;
11196 
11197 	for (i = 0; i < bp->cp_nr_rings; i++) {
11198 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11199 
11200 		irq = &bp->irq_tbl[map_idx];
11201 		if (irq->requested) {
11202 			if (irq->have_cpumask) {
11203 				irq_update_affinity_hint(irq->vector, NULL);
11204 				free_cpumask_var(irq->cpu_mask);
11205 				irq->have_cpumask = 0;
11206 			}
11207 			free_irq(irq->vector, bp->bnapi[i]);
11208 		}
11209 
11210 		irq->requested = 0;
11211 	}
11212 }
11213 
11214 static int bnxt_request_irq(struct bnxt *bp)
11215 {
11216 	int i, j, rc = 0;
11217 	unsigned long flags = 0;
11218 #ifdef CONFIG_RFS_ACCEL
11219 	struct cpu_rmap *rmap;
11220 #endif
11221 
11222 	rc = bnxt_setup_int_mode(bp);
11223 	if (rc) {
11224 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11225 			   rc);
11226 		return rc;
11227 	}
11228 #ifdef CONFIG_RFS_ACCEL
11229 	rmap = bp->dev->rx_cpu_rmap;
11230 #endif
11231 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11232 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11233 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11234 
11235 #ifdef CONFIG_RFS_ACCEL
11236 		if (rmap && bp->bnapi[i]->rx_ring) {
11237 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11238 			if (rc)
11239 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11240 					    j);
11241 			j++;
11242 		}
11243 #endif
11244 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11245 				 bp->bnapi[i]);
11246 		if (rc)
11247 			break;
11248 
11249 		netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector);
11250 		irq->requested = 1;
11251 
11252 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11253 			int numa_node = dev_to_node(&bp->pdev->dev);
11254 
11255 			irq->have_cpumask = 1;
11256 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11257 					irq->cpu_mask);
11258 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11259 			if (rc) {
11260 				netdev_warn(bp->dev,
11261 					    "Update affinity hint failed, IRQ = %d\n",
11262 					    irq->vector);
11263 				break;
11264 			}
11265 		}
11266 	}
11267 	return rc;
11268 }
11269 
11270 static void bnxt_del_napi(struct bnxt *bp)
11271 {
11272 	int i;
11273 
11274 	if (!bp->bnapi)
11275 		return;
11276 
11277 	for (i = 0; i < bp->rx_nr_rings; i++)
11278 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11279 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11280 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11281 
11282 	for (i = 0; i < bp->cp_nr_rings; i++) {
11283 		struct bnxt_napi *bnapi = bp->bnapi[i];
11284 
11285 		__netif_napi_del(&bnapi->napi);
11286 	}
11287 	/* We called __netif_napi_del(), we need
11288 	 * to respect an RCU grace period before freeing napi structures.
11289 	 */
11290 	synchronize_net();
11291 }
11292 
11293 static void bnxt_init_napi(struct bnxt *bp)
11294 {
11295 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11296 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11297 	struct bnxt_napi *bnapi;
11298 	int i;
11299 
11300 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11301 		poll_fn = bnxt_poll_p5;
11302 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11303 		cp_nr_rings--;
11304 	for (i = 0; i < cp_nr_rings; i++) {
11305 		bnapi = bp->bnapi[i];
11306 		netif_napi_add_config(bp->dev, &bnapi->napi, poll_fn,
11307 				      bnapi->index);
11308 	}
11309 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11310 		bnapi = bp->bnapi[cp_nr_rings];
11311 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11312 	}
11313 }
11314 
11315 static void bnxt_disable_napi(struct bnxt *bp)
11316 {
11317 	int i;
11318 
11319 	if (!bp->bnapi ||
11320 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11321 		return;
11322 
11323 	for (i = 0; i < bp->cp_nr_rings; i++) {
11324 		struct bnxt_napi *bnapi = bp->bnapi[i];
11325 		struct bnxt_cp_ring_info *cpr;
11326 
11327 		cpr = &bnapi->cp_ring;
11328 		if (bnapi->tx_fault)
11329 			cpr->sw_stats->tx.tx_resets++;
11330 		if (bnapi->in_reset)
11331 			cpr->sw_stats->rx.rx_resets++;
11332 		napi_disable(&bnapi->napi);
11333 		if (bnapi->rx_ring)
11334 			cancel_work_sync(&cpr->dim.work);
11335 	}
11336 }
11337 
11338 static void bnxt_enable_napi(struct bnxt *bp)
11339 {
11340 	int i;
11341 
11342 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11343 	for (i = 0; i < bp->cp_nr_rings; i++) {
11344 		struct bnxt_napi *bnapi = bp->bnapi[i];
11345 		struct bnxt_cp_ring_info *cpr;
11346 
11347 		bnapi->tx_fault = 0;
11348 
11349 		cpr = &bnapi->cp_ring;
11350 		bnapi->in_reset = false;
11351 
11352 		if (bnapi->rx_ring) {
11353 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11354 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11355 		}
11356 		napi_enable(&bnapi->napi);
11357 	}
11358 }
11359 
11360 void bnxt_tx_disable(struct bnxt *bp)
11361 {
11362 	int i;
11363 	struct bnxt_tx_ring_info *txr;
11364 
11365 	if (bp->tx_ring) {
11366 		for (i = 0; i < bp->tx_nr_rings; i++) {
11367 			txr = &bp->tx_ring[i];
11368 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11369 		}
11370 	}
11371 	/* Make sure napi polls see @dev_state change */
11372 	synchronize_net();
11373 	/* Drop carrier first to prevent TX timeout */
11374 	netif_carrier_off(bp->dev);
11375 	/* Stop all TX queues */
11376 	netif_tx_disable(bp->dev);
11377 }
11378 
11379 void bnxt_tx_enable(struct bnxt *bp)
11380 {
11381 	int i;
11382 	struct bnxt_tx_ring_info *txr;
11383 
11384 	for (i = 0; i < bp->tx_nr_rings; i++) {
11385 		txr = &bp->tx_ring[i];
11386 		WRITE_ONCE(txr->dev_state, 0);
11387 	}
11388 	/* Make sure napi polls see @dev_state change */
11389 	synchronize_net();
11390 	netif_tx_wake_all_queues(bp->dev);
11391 	if (BNXT_LINK_IS_UP(bp))
11392 		netif_carrier_on(bp->dev);
11393 }
11394 
11395 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11396 {
11397 	u8 active_fec = link_info->active_fec_sig_mode &
11398 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11399 
11400 	switch (active_fec) {
11401 	default:
11402 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11403 		return "None";
11404 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11405 		return "Clause 74 BaseR";
11406 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11407 		return "Clause 91 RS(528,514)";
11408 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11409 		return "Clause 91 RS544_1XN";
11410 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11411 		return "Clause 91 RS(544,514)";
11412 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11413 		return "Clause 91 RS272_1XN";
11414 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11415 		return "Clause 91 RS(272,257)";
11416 	}
11417 }
11418 
11419 void bnxt_report_link(struct bnxt *bp)
11420 {
11421 	if (BNXT_LINK_IS_UP(bp)) {
11422 		const char *signal = "";
11423 		const char *flow_ctrl;
11424 		const char *duplex;
11425 		u32 speed;
11426 		u16 fec;
11427 
11428 		netif_carrier_on(bp->dev);
11429 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11430 		if (speed == SPEED_UNKNOWN) {
11431 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11432 			return;
11433 		}
11434 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11435 			duplex = "full";
11436 		else
11437 			duplex = "half";
11438 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11439 			flow_ctrl = "ON - receive & transmit";
11440 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11441 			flow_ctrl = "ON - transmit";
11442 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11443 			flow_ctrl = "ON - receive";
11444 		else
11445 			flow_ctrl = "none";
11446 		if (bp->link_info.phy_qcfg_resp.option_flags &
11447 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11448 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11449 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11450 			switch (sig_mode) {
11451 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11452 				signal = "(NRZ) ";
11453 				break;
11454 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11455 				signal = "(PAM4 56Gbps) ";
11456 				break;
11457 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11458 				signal = "(PAM4 112Gbps) ";
11459 				break;
11460 			default:
11461 				break;
11462 			}
11463 		}
11464 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11465 			    speed, signal, duplex, flow_ctrl);
11466 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11467 			netdev_info(bp->dev, "EEE is %s\n",
11468 				    bp->eee.eee_active ? "active" :
11469 							 "not active");
11470 		fec = bp->link_info.fec_cfg;
11471 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11472 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11473 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11474 				    bnxt_report_fec(&bp->link_info));
11475 	} else {
11476 		netif_carrier_off(bp->dev);
11477 		netdev_err(bp->dev, "NIC Link is Down\n");
11478 	}
11479 }
11480 
11481 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11482 {
11483 	if (!resp->supported_speeds_auto_mode &&
11484 	    !resp->supported_speeds_force_mode &&
11485 	    !resp->supported_pam4_speeds_auto_mode &&
11486 	    !resp->supported_pam4_speeds_force_mode &&
11487 	    !resp->supported_speeds2_auto_mode &&
11488 	    !resp->supported_speeds2_force_mode)
11489 		return true;
11490 	return false;
11491 }
11492 
11493 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11494 {
11495 	struct bnxt_link_info *link_info = &bp->link_info;
11496 	struct hwrm_port_phy_qcaps_output *resp;
11497 	struct hwrm_port_phy_qcaps_input *req;
11498 	int rc = 0;
11499 
11500 	if (bp->hwrm_spec_code < 0x10201)
11501 		return 0;
11502 
11503 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11504 	if (rc)
11505 		return rc;
11506 
11507 	resp = hwrm_req_hold(bp, req);
11508 	rc = hwrm_req_send(bp, req);
11509 	if (rc)
11510 		goto hwrm_phy_qcaps_exit;
11511 
11512 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11513 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11514 		struct ethtool_keee *eee = &bp->eee;
11515 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11516 
11517 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11518 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11519 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11520 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11521 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11522 	}
11523 
11524 	if (bp->hwrm_spec_code >= 0x10a01) {
11525 		if (bnxt_phy_qcaps_no_speed(resp)) {
11526 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11527 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11528 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11529 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11530 			netdev_info(bp->dev, "Ethernet link enabled\n");
11531 			/* Phy re-enabled, reprobe the speeds */
11532 			link_info->support_auto_speeds = 0;
11533 			link_info->support_pam4_auto_speeds = 0;
11534 			link_info->support_auto_speeds2 = 0;
11535 		}
11536 	}
11537 	if (resp->supported_speeds_auto_mode)
11538 		link_info->support_auto_speeds =
11539 			le16_to_cpu(resp->supported_speeds_auto_mode);
11540 	if (resp->supported_pam4_speeds_auto_mode)
11541 		link_info->support_pam4_auto_speeds =
11542 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11543 	if (resp->supported_speeds2_auto_mode)
11544 		link_info->support_auto_speeds2 =
11545 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11546 
11547 	bp->port_count = resp->port_cnt;
11548 
11549 hwrm_phy_qcaps_exit:
11550 	hwrm_req_drop(bp, req);
11551 	return rc;
11552 }
11553 
11554 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
11555 {
11556 	struct hwrm_port_mac_qcaps_output *resp;
11557 	struct hwrm_port_mac_qcaps_input *req;
11558 	int rc;
11559 
11560 	if (bp->hwrm_spec_code < 0x10a03)
11561 		return;
11562 
11563 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
11564 	if (rc)
11565 		return;
11566 
11567 	resp = hwrm_req_hold(bp, req);
11568 	rc = hwrm_req_send_silent(bp, req);
11569 	if (!rc)
11570 		bp->mac_flags = resp->flags;
11571 	hwrm_req_drop(bp, req);
11572 }
11573 
11574 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11575 {
11576 	u16 diff = advertising ^ supported;
11577 
11578 	return ((supported | diff) != supported);
11579 }
11580 
11581 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11582 {
11583 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11584 
11585 	/* Check if any advertised speeds are no longer supported. The caller
11586 	 * holds the link_lock mutex, so we can modify link_info settings.
11587 	 */
11588 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11589 		if (bnxt_support_dropped(link_info->advertising,
11590 					 link_info->support_auto_speeds2)) {
11591 			link_info->advertising = link_info->support_auto_speeds2;
11592 			return true;
11593 		}
11594 		return false;
11595 	}
11596 	if (bnxt_support_dropped(link_info->advertising,
11597 				 link_info->support_auto_speeds)) {
11598 		link_info->advertising = link_info->support_auto_speeds;
11599 		return true;
11600 	}
11601 	if (bnxt_support_dropped(link_info->advertising_pam4,
11602 				 link_info->support_pam4_auto_speeds)) {
11603 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
11604 		return true;
11605 	}
11606 	return false;
11607 }
11608 
11609 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
11610 {
11611 	struct bnxt_link_info *link_info = &bp->link_info;
11612 	struct hwrm_port_phy_qcfg_output *resp;
11613 	struct hwrm_port_phy_qcfg_input *req;
11614 	u8 link_state = link_info->link_state;
11615 	bool support_changed;
11616 	int rc;
11617 
11618 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
11619 	if (rc)
11620 		return rc;
11621 
11622 	resp = hwrm_req_hold(bp, req);
11623 	rc = hwrm_req_send(bp, req);
11624 	if (rc) {
11625 		hwrm_req_drop(bp, req);
11626 		if (BNXT_VF(bp) && rc == -ENODEV) {
11627 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
11628 			rc = 0;
11629 		}
11630 		return rc;
11631 	}
11632 
11633 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
11634 	link_info->phy_link_status = resp->link;
11635 	link_info->duplex = resp->duplex_cfg;
11636 	if (bp->hwrm_spec_code >= 0x10800)
11637 		link_info->duplex = resp->duplex_state;
11638 	link_info->pause = resp->pause;
11639 	link_info->auto_mode = resp->auto_mode;
11640 	link_info->auto_pause_setting = resp->auto_pause;
11641 	link_info->lp_pause = resp->link_partner_adv_pause;
11642 	link_info->force_pause_setting = resp->force_pause;
11643 	link_info->duplex_setting = resp->duplex_cfg;
11644 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
11645 		link_info->link_speed = le16_to_cpu(resp->link_speed);
11646 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
11647 			link_info->active_lanes = resp->active_lanes;
11648 	} else {
11649 		link_info->link_speed = 0;
11650 		link_info->active_lanes = 0;
11651 	}
11652 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
11653 	link_info->force_pam4_link_speed =
11654 		le16_to_cpu(resp->force_pam4_link_speed);
11655 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
11656 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
11657 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
11658 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
11659 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
11660 	link_info->auto_pam4_link_speeds =
11661 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
11662 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
11663 	link_info->lp_auto_link_speeds =
11664 		le16_to_cpu(resp->link_partner_adv_speeds);
11665 	link_info->lp_auto_pam4_link_speeds =
11666 		resp->link_partner_pam4_adv_speeds;
11667 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
11668 	link_info->phy_ver[0] = resp->phy_maj;
11669 	link_info->phy_ver[1] = resp->phy_min;
11670 	link_info->phy_ver[2] = resp->phy_bld;
11671 	link_info->media_type = resp->media_type;
11672 	link_info->phy_type = resp->phy_type;
11673 	link_info->transceiver = resp->xcvr_pkg_type;
11674 	link_info->phy_addr = resp->eee_config_phy_addr &
11675 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
11676 	link_info->module_status = resp->module_status;
11677 
11678 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
11679 		struct ethtool_keee *eee = &bp->eee;
11680 		u16 fw_speeds;
11681 
11682 		eee->eee_active = 0;
11683 		if (resp->eee_config_phy_addr &
11684 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
11685 			eee->eee_active = 1;
11686 			fw_speeds = le16_to_cpu(
11687 				resp->link_partner_adv_eee_link_speed_mask);
11688 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
11689 		}
11690 
11691 		/* Pull initial EEE config */
11692 		if (!chng_link_state) {
11693 			if (resp->eee_config_phy_addr &
11694 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
11695 				eee->eee_enabled = 1;
11696 
11697 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
11698 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
11699 
11700 			if (resp->eee_config_phy_addr &
11701 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
11702 				__le32 tmr;
11703 
11704 				eee->tx_lpi_enabled = 1;
11705 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
11706 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
11707 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
11708 			}
11709 		}
11710 	}
11711 
11712 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
11713 	if (bp->hwrm_spec_code >= 0x10504) {
11714 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
11715 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
11716 	}
11717 	/* TODO: need to add more logic to report VF link */
11718 	if (chng_link_state) {
11719 		if (link_info->phy_link_status == BNXT_LINK_LINK)
11720 			link_info->link_state = BNXT_LINK_STATE_UP;
11721 		else
11722 			link_info->link_state = BNXT_LINK_STATE_DOWN;
11723 		if (link_state != link_info->link_state)
11724 			bnxt_report_link(bp);
11725 	} else {
11726 		/* always link down if not require to update link state */
11727 		link_info->link_state = BNXT_LINK_STATE_DOWN;
11728 	}
11729 	hwrm_req_drop(bp, req);
11730 
11731 	if (!BNXT_PHY_CFG_ABLE(bp))
11732 		return 0;
11733 
11734 	support_changed = bnxt_support_speed_dropped(link_info);
11735 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
11736 		bnxt_hwrm_set_link_setting(bp, true, false);
11737 	return 0;
11738 }
11739 
11740 static void bnxt_get_port_module_status(struct bnxt *bp)
11741 {
11742 	struct bnxt_link_info *link_info = &bp->link_info;
11743 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
11744 	u8 module_status;
11745 
11746 	if (bnxt_update_link(bp, true))
11747 		return;
11748 
11749 	module_status = link_info->module_status;
11750 	switch (module_status) {
11751 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
11752 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
11753 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
11754 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
11755 			    bp->pf.port_id);
11756 		if (bp->hwrm_spec_code >= 0x10201) {
11757 			netdev_warn(bp->dev, "Module part number %s\n",
11758 				    resp->phy_vendor_partnumber);
11759 		}
11760 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
11761 			netdev_warn(bp->dev, "TX is disabled\n");
11762 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
11763 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
11764 	}
11765 }
11766 
11767 static void
11768 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11769 {
11770 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
11771 		if (bp->hwrm_spec_code >= 0x10201)
11772 			req->auto_pause =
11773 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
11774 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11775 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
11776 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11777 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
11778 		req->enables |=
11779 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11780 	} else {
11781 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11782 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
11783 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11784 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
11785 		req->enables |=
11786 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
11787 		if (bp->hwrm_spec_code >= 0x10201) {
11788 			req->auto_pause = req->force_pause;
11789 			req->enables |= cpu_to_le32(
11790 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11791 		}
11792 	}
11793 }
11794 
11795 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11796 {
11797 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
11798 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
11799 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11800 			req->enables |=
11801 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
11802 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
11803 		} else if (bp->link_info.advertising) {
11804 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
11805 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
11806 		}
11807 		if (bp->link_info.advertising_pam4) {
11808 			req->enables |=
11809 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
11810 			req->auto_link_pam4_speed_mask =
11811 				cpu_to_le16(bp->link_info.advertising_pam4);
11812 		}
11813 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
11814 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
11815 	} else {
11816 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
11817 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11818 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
11819 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
11820 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
11821 				   (u32)bp->link_info.req_link_speed);
11822 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
11823 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11824 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
11825 		} else {
11826 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11827 		}
11828 	}
11829 
11830 	/* tell chimp that the setting takes effect immediately */
11831 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
11832 }
11833 
11834 int bnxt_hwrm_set_pause(struct bnxt *bp)
11835 {
11836 	struct hwrm_port_phy_cfg_input *req;
11837 	int rc;
11838 
11839 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11840 	if (rc)
11841 		return rc;
11842 
11843 	bnxt_hwrm_set_pause_common(bp, req);
11844 
11845 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
11846 	    bp->link_info.force_link_chng)
11847 		bnxt_hwrm_set_link_common(bp, req);
11848 
11849 	rc = hwrm_req_send(bp, req);
11850 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
11851 		/* since changing of pause setting doesn't trigger any link
11852 		 * change event, the driver needs to update the current pause
11853 		 * result upon successfully return of the phy_cfg command
11854 		 */
11855 		bp->link_info.pause =
11856 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
11857 		bp->link_info.auto_pause_setting = 0;
11858 		if (!bp->link_info.force_link_chng)
11859 			bnxt_report_link(bp);
11860 	}
11861 	bp->link_info.force_link_chng = false;
11862 	return rc;
11863 }
11864 
11865 static void bnxt_hwrm_set_eee(struct bnxt *bp,
11866 			      struct hwrm_port_phy_cfg_input *req)
11867 {
11868 	struct ethtool_keee *eee = &bp->eee;
11869 
11870 	if (eee->eee_enabled) {
11871 		u16 eee_speeds;
11872 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
11873 
11874 		if (eee->tx_lpi_enabled)
11875 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
11876 		else
11877 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
11878 
11879 		req->flags |= cpu_to_le32(flags);
11880 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
11881 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
11882 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
11883 	} else {
11884 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
11885 	}
11886 }
11887 
11888 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
11889 {
11890 	struct hwrm_port_phy_cfg_input *req;
11891 	int rc;
11892 
11893 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11894 	if (rc)
11895 		return rc;
11896 
11897 	if (set_pause)
11898 		bnxt_hwrm_set_pause_common(bp, req);
11899 
11900 	bnxt_hwrm_set_link_common(bp, req);
11901 
11902 	if (set_eee)
11903 		bnxt_hwrm_set_eee(bp, req);
11904 	return hwrm_req_send(bp, req);
11905 }
11906 
11907 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
11908 {
11909 	struct hwrm_port_phy_cfg_input *req;
11910 	int rc;
11911 
11912 	if (!BNXT_SINGLE_PF(bp))
11913 		return 0;
11914 
11915 	if (pci_num_vf(bp->pdev) &&
11916 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
11917 		return 0;
11918 
11919 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11920 	if (rc)
11921 		return rc;
11922 
11923 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
11924 	rc = hwrm_req_send(bp, req);
11925 	if (!rc) {
11926 		mutex_lock(&bp->link_lock);
11927 		/* Device is not obliged link down in certain scenarios, even
11928 		 * when forced. Setting the state unknown is consistent with
11929 		 * driver startup and will force link state to be reported
11930 		 * during subsequent open based on PORT_PHY_QCFG.
11931 		 */
11932 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
11933 		mutex_unlock(&bp->link_lock);
11934 	}
11935 	return rc;
11936 }
11937 
11938 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
11939 {
11940 #ifdef CONFIG_TEE_BNXT_FW
11941 	int rc = tee_bnxt_fw_load();
11942 
11943 	if (rc)
11944 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
11945 
11946 	return rc;
11947 #else
11948 	netdev_err(bp->dev, "OP-TEE not supported\n");
11949 	return -ENODEV;
11950 #endif
11951 }
11952 
11953 static int bnxt_try_recover_fw(struct bnxt *bp)
11954 {
11955 	if (bp->fw_health && bp->fw_health->status_reliable) {
11956 		int retry = 0, rc;
11957 		u32 sts;
11958 
11959 		do {
11960 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11961 			rc = bnxt_hwrm_poll(bp);
11962 			if (!BNXT_FW_IS_BOOTING(sts) &&
11963 			    !BNXT_FW_IS_RECOVERING(sts))
11964 				break;
11965 			retry++;
11966 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
11967 
11968 		if (!BNXT_FW_IS_HEALTHY(sts)) {
11969 			netdev_err(bp->dev,
11970 				   "Firmware not responding, status: 0x%x\n",
11971 				   sts);
11972 			rc = -ENODEV;
11973 		}
11974 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
11975 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
11976 			return bnxt_fw_reset_via_optee(bp);
11977 		}
11978 		return rc;
11979 	}
11980 
11981 	return -ENODEV;
11982 }
11983 
11984 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
11985 {
11986 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11987 
11988 	if (!BNXT_NEW_RM(bp))
11989 		return; /* no resource reservations required */
11990 
11991 	hw_resc->resv_cp_rings = 0;
11992 	hw_resc->resv_stat_ctxs = 0;
11993 	hw_resc->resv_irqs = 0;
11994 	hw_resc->resv_tx_rings = 0;
11995 	hw_resc->resv_rx_rings = 0;
11996 	hw_resc->resv_hw_ring_grps = 0;
11997 	hw_resc->resv_vnics = 0;
11998 	hw_resc->resv_rsscos_ctxs = 0;
11999 	if (!fw_reset) {
12000 		bp->tx_nr_rings = 0;
12001 		bp->rx_nr_rings = 0;
12002 	}
12003 }
12004 
12005 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12006 {
12007 	int rc;
12008 
12009 	if (!BNXT_NEW_RM(bp))
12010 		return 0; /* no resource reservations required */
12011 
12012 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12013 	if (rc)
12014 		netdev_err(bp->dev, "resc_qcaps failed\n");
12015 
12016 	bnxt_clear_reservations(bp, fw_reset);
12017 
12018 	return rc;
12019 }
12020 
12021 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12022 {
12023 	struct hwrm_func_drv_if_change_output *resp;
12024 	struct hwrm_func_drv_if_change_input *req;
12025 	bool fw_reset = !bp->irq_tbl;
12026 	bool resc_reinit = false;
12027 	int rc, retry = 0;
12028 	u32 flags = 0;
12029 
12030 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12031 		return 0;
12032 
12033 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12034 	if (rc)
12035 		return rc;
12036 
12037 	if (up)
12038 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12039 	resp = hwrm_req_hold(bp, req);
12040 
12041 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12042 	while (retry < BNXT_FW_IF_RETRY) {
12043 		rc = hwrm_req_send(bp, req);
12044 		if (rc != -EAGAIN)
12045 			break;
12046 
12047 		msleep(50);
12048 		retry++;
12049 	}
12050 
12051 	if (rc == -EAGAIN) {
12052 		hwrm_req_drop(bp, req);
12053 		return rc;
12054 	} else if (!rc) {
12055 		flags = le32_to_cpu(resp->flags);
12056 	} else if (up) {
12057 		rc = bnxt_try_recover_fw(bp);
12058 		fw_reset = true;
12059 	}
12060 	hwrm_req_drop(bp, req);
12061 	if (rc)
12062 		return rc;
12063 
12064 	if (!up) {
12065 		bnxt_inv_fw_health_reg(bp);
12066 		return 0;
12067 	}
12068 
12069 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12070 		resc_reinit = true;
12071 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12072 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12073 		fw_reset = true;
12074 	else
12075 		bnxt_remap_fw_health_regs(bp);
12076 
12077 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12078 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12079 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12080 		return -ENODEV;
12081 	}
12082 	if (resc_reinit || fw_reset) {
12083 		if (fw_reset) {
12084 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12085 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12086 				bnxt_ulp_irq_stop(bp);
12087 			bnxt_free_ctx_mem(bp, false);
12088 			bnxt_dcb_free(bp);
12089 			rc = bnxt_fw_init_one(bp);
12090 			if (rc) {
12091 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12092 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12093 				return rc;
12094 			}
12095 			bnxt_clear_int_mode(bp);
12096 			rc = bnxt_init_int_mode(bp);
12097 			if (rc) {
12098 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12099 				netdev_err(bp->dev, "init int mode failed\n");
12100 				return rc;
12101 			}
12102 		}
12103 		rc = bnxt_cancel_reservations(bp, fw_reset);
12104 	}
12105 	return rc;
12106 }
12107 
12108 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12109 {
12110 	struct hwrm_port_led_qcaps_output *resp;
12111 	struct hwrm_port_led_qcaps_input *req;
12112 	struct bnxt_pf_info *pf = &bp->pf;
12113 	int rc;
12114 
12115 	bp->num_leds = 0;
12116 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12117 		return 0;
12118 
12119 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12120 	if (rc)
12121 		return rc;
12122 
12123 	req->port_id = cpu_to_le16(pf->port_id);
12124 	resp = hwrm_req_hold(bp, req);
12125 	rc = hwrm_req_send(bp, req);
12126 	if (rc) {
12127 		hwrm_req_drop(bp, req);
12128 		return rc;
12129 	}
12130 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12131 		int i;
12132 
12133 		bp->num_leds = resp->num_leds;
12134 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12135 						 bp->num_leds);
12136 		for (i = 0; i < bp->num_leds; i++) {
12137 			struct bnxt_led_info *led = &bp->leds[i];
12138 			__le16 caps = led->led_state_caps;
12139 
12140 			if (!led->led_group_id ||
12141 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12142 				bp->num_leds = 0;
12143 				break;
12144 			}
12145 		}
12146 	}
12147 	hwrm_req_drop(bp, req);
12148 	return 0;
12149 }
12150 
12151 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12152 {
12153 	struct hwrm_wol_filter_alloc_output *resp;
12154 	struct hwrm_wol_filter_alloc_input *req;
12155 	int rc;
12156 
12157 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12158 	if (rc)
12159 		return rc;
12160 
12161 	req->port_id = cpu_to_le16(bp->pf.port_id);
12162 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12163 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12164 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12165 
12166 	resp = hwrm_req_hold(bp, req);
12167 	rc = hwrm_req_send(bp, req);
12168 	if (!rc)
12169 		bp->wol_filter_id = resp->wol_filter_id;
12170 	hwrm_req_drop(bp, req);
12171 	return rc;
12172 }
12173 
12174 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12175 {
12176 	struct hwrm_wol_filter_free_input *req;
12177 	int rc;
12178 
12179 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12180 	if (rc)
12181 		return rc;
12182 
12183 	req->port_id = cpu_to_le16(bp->pf.port_id);
12184 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12185 	req->wol_filter_id = bp->wol_filter_id;
12186 
12187 	return hwrm_req_send(bp, req);
12188 }
12189 
12190 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12191 {
12192 	struct hwrm_wol_filter_qcfg_output *resp;
12193 	struct hwrm_wol_filter_qcfg_input *req;
12194 	u16 next_handle = 0;
12195 	int rc;
12196 
12197 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12198 	if (rc)
12199 		return rc;
12200 
12201 	req->port_id = cpu_to_le16(bp->pf.port_id);
12202 	req->handle = cpu_to_le16(handle);
12203 	resp = hwrm_req_hold(bp, req);
12204 	rc = hwrm_req_send(bp, req);
12205 	if (!rc) {
12206 		next_handle = le16_to_cpu(resp->next_handle);
12207 		if (next_handle != 0) {
12208 			if (resp->wol_type ==
12209 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12210 				bp->wol = 1;
12211 				bp->wol_filter_id = resp->wol_filter_id;
12212 			}
12213 		}
12214 	}
12215 	hwrm_req_drop(bp, req);
12216 	return next_handle;
12217 }
12218 
12219 static void bnxt_get_wol_settings(struct bnxt *bp)
12220 {
12221 	u16 handle = 0;
12222 
12223 	bp->wol = 0;
12224 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12225 		return;
12226 
12227 	do {
12228 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12229 	} while (handle && handle != 0xffff);
12230 }
12231 
12232 static bool bnxt_eee_config_ok(struct bnxt *bp)
12233 {
12234 	struct ethtool_keee *eee = &bp->eee;
12235 	struct bnxt_link_info *link_info = &bp->link_info;
12236 
12237 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12238 		return true;
12239 
12240 	if (eee->eee_enabled) {
12241 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12242 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12243 
12244 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12245 
12246 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12247 			eee->eee_enabled = 0;
12248 			return false;
12249 		}
12250 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12251 			linkmode_and(eee->advertised, advertising,
12252 				     eee->supported);
12253 			return false;
12254 		}
12255 	}
12256 	return true;
12257 }
12258 
12259 static int bnxt_update_phy_setting(struct bnxt *bp)
12260 {
12261 	int rc;
12262 	bool update_link = false;
12263 	bool update_pause = false;
12264 	bool update_eee = false;
12265 	struct bnxt_link_info *link_info = &bp->link_info;
12266 
12267 	rc = bnxt_update_link(bp, true);
12268 	if (rc) {
12269 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12270 			   rc);
12271 		return rc;
12272 	}
12273 	if (!BNXT_SINGLE_PF(bp))
12274 		return 0;
12275 
12276 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12277 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12278 	    link_info->req_flow_ctrl)
12279 		update_pause = true;
12280 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12281 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12282 		update_pause = true;
12283 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12284 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12285 			update_link = true;
12286 		if (bnxt_force_speed_updated(link_info))
12287 			update_link = true;
12288 		if (link_info->req_duplex != link_info->duplex_setting)
12289 			update_link = true;
12290 	} else {
12291 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12292 			update_link = true;
12293 		if (bnxt_auto_speed_updated(link_info))
12294 			update_link = true;
12295 	}
12296 
12297 	/* The last close may have shutdown the link, so need to call
12298 	 * PHY_CFG to bring it back up.
12299 	 */
12300 	if (!BNXT_LINK_IS_UP(bp))
12301 		update_link = true;
12302 
12303 	if (!bnxt_eee_config_ok(bp))
12304 		update_eee = true;
12305 
12306 	if (update_link)
12307 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12308 	else if (update_pause)
12309 		rc = bnxt_hwrm_set_pause(bp);
12310 	if (rc) {
12311 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12312 			   rc);
12313 		return rc;
12314 	}
12315 
12316 	return rc;
12317 }
12318 
12319 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12320 
12321 static int bnxt_reinit_after_abort(struct bnxt *bp)
12322 {
12323 	int rc;
12324 
12325 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12326 		return -EBUSY;
12327 
12328 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12329 		return -ENODEV;
12330 
12331 	rc = bnxt_fw_init_one(bp);
12332 	if (!rc) {
12333 		bnxt_clear_int_mode(bp);
12334 		rc = bnxt_init_int_mode(bp);
12335 		if (!rc) {
12336 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12337 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12338 		}
12339 	}
12340 	return rc;
12341 }
12342 
12343 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12344 {
12345 	struct bnxt_ntuple_filter *ntp_fltr;
12346 	struct bnxt_l2_filter *l2_fltr;
12347 
12348 	if (list_empty(&fltr->list))
12349 		return;
12350 
12351 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12352 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12353 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12354 		atomic_inc(&l2_fltr->refcnt);
12355 		ntp_fltr->l2_fltr = l2_fltr;
12356 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12357 			bnxt_del_ntp_filter(bp, ntp_fltr);
12358 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12359 				   fltr->sw_id);
12360 		}
12361 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12362 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12363 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12364 			bnxt_del_l2_filter(bp, l2_fltr);
12365 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12366 				   fltr->sw_id);
12367 		}
12368 	}
12369 }
12370 
12371 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12372 {
12373 	struct bnxt_filter_base *usr_fltr, *tmp;
12374 
12375 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12376 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12377 }
12378 
12379 static int bnxt_set_xps_mapping(struct bnxt *bp)
12380 {
12381 	int numa_node = dev_to_node(&bp->pdev->dev);
12382 	unsigned int q_idx, map_idx, cpu, i;
12383 	const struct cpumask *cpu_mask_ptr;
12384 	int nr_cpus = num_online_cpus();
12385 	cpumask_t *q_map;
12386 	int rc = 0;
12387 
12388 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12389 	if (!q_map)
12390 		return -ENOMEM;
12391 
12392 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12393 	 * Each TC has the same number of TX queues. The nth TX queue for each
12394 	 * TC will have the same CPU mask.
12395 	 */
12396 	for (i = 0; i < nr_cpus; i++) {
12397 		map_idx = i % bp->tx_nr_rings_per_tc;
12398 		cpu = cpumask_local_spread(i, numa_node);
12399 		cpu_mask_ptr = get_cpu_mask(cpu);
12400 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12401 	}
12402 
12403 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12404 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12405 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12406 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12407 		if (rc) {
12408 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12409 				    q_idx);
12410 			break;
12411 		}
12412 	}
12413 
12414 	kfree(q_map);
12415 
12416 	return rc;
12417 }
12418 
12419 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12420 {
12421 	int rc = 0;
12422 
12423 	netif_carrier_off(bp->dev);
12424 	if (irq_re_init) {
12425 		/* Reserve rings now if none were reserved at driver probe. */
12426 		rc = bnxt_init_dflt_ring_mode(bp);
12427 		if (rc) {
12428 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12429 			return rc;
12430 		}
12431 	}
12432 	rc = bnxt_reserve_rings(bp, irq_re_init);
12433 	if (rc)
12434 		return rc;
12435 
12436 	rc = bnxt_alloc_mem(bp, irq_re_init);
12437 	if (rc) {
12438 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12439 		goto open_err_free_mem;
12440 	}
12441 
12442 	if (irq_re_init) {
12443 		bnxt_init_napi(bp);
12444 		rc = bnxt_request_irq(bp);
12445 		if (rc) {
12446 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12447 			goto open_err_irq;
12448 		}
12449 	}
12450 
12451 	rc = bnxt_init_nic(bp, irq_re_init);
12452 	if (rc) {
12453 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12454 		goto open_err_irq;
12455 	}
12456 
12457 	bnxt_enable_napi(bp);
12458 	bnxt_debug_dev_init(bp);
12459 
12460 	if (link_re_init) {
12461 		mutex_lock(&bp->link_lock);
12462 		rc = bnxt_update_phy_setting(bp);
12463 		mutex_unlock(&bp->link_lock);
12464 		if (rc) {
12465 			netdev_warn(bp->dev, "failed to update phy settings\n");
12466 			if (BNXT_SINGLE_PF(bp)) {
12467 				bp->link_info.phy_retry = true;
12468 				bp->link_info.phy_retry_expires =
12469 					jiffies + 5 * HZ;
12470 			}
12471 		}
12472 	}
12473 
12474 	if (irq_re_init) {
12475 		udp_tunnel_nic_reset_ntf(bp->dev);
12476 		rc = bnxt_set_xps_mapping(bp);
12477 		if (rc)
12478 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12479 	}
12480 
12481 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12482 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12483 			static_branch_enable(&bnxt_xdp_locking_key);
12484 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12485 		static_branch_disable(&bnxt_xdp_locking_key);
12486 	}
12487 	set_bit(BNXT_STATE_OPEN, &bp->state);
12488 	bnxt_enable_int(bp);
12489 	/* Enable TX queues */
12490 	bnxt_tx_enable(bp);
12491 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12492 	/* Poll link status and check for SFP+ module status */
12493 	mutex_lock(&bp->link_lock);
12494 	bnxt_get_port_module_status(bp);
12495 	mutex_unlock(&bp->link_lock);
12496 
12497 	/* VF-reps may need to be re-opened after the PF is re-opened */
12498 	if (BNXT_PF(bp))
12499 		bnxt_vf_reps_open(bp);
12500 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
12501 		WRITE_ONCE(bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS);
12502 	bnxt_ptp_init_rtc(bp, true);
12503 	bnxt_ptp_cfg_tstamp_filters(bp);
12504 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12505 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12506 	bnxt_cfg_usr_fltrs(bp);
12507 	return 0;
12508 
12509 open_err_irq:
12510 	bnxt_del_napi(bp);
12511 
12512 open_err_free_mem:
12513 	bnxt_free_skbs(bp);
12514 	bnxt_free_irq(bp);
12515 	bnxt_free_mem(bp, true);
12516 	return rc;
12517 }
12518 
12519 /* rtnl_lock held */
12520 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12521 {
12522 	int rc = 0;
12523 
12524 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12525 		rc = -EIO;
12526 	if (!rc)
12527 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12528 	if (rc) {
12529 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12530 		dev_close(bp->dev);
12531 	}
12532 	return rc;
12533 }
12534 
12535 /* rtnl_lock held, open the NIC half way by allocating all resources, but
12536  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
12537  * self tests.
12538  */
12539 int bnxt_half_open_nic(struct bnxt *bp)
12540 {
12541 	int rc = 0;
12542 
12543 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12544 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12545 		rc = -ENODEV;
12546 		goto half_open_err;
12547 	}
12548 
12549 	rc = bnxt_alloc_mem(bp, true);
12550 	if (rc) {
12551 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12552 		goto half_open_err;
12553 	}
12554 	bnxt_init_napi(bp);
12555 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12556 	rc = bnxt_init_nic(bp, true);
12557 	if (rc) {
12558 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12559 		bnxt_del_napi(bp);
12560 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12561 		goto half_open_err;
12562 	}
12563 	return 0;
12564 
12565 half_open_err:
12566 	bnxt_free_skbs(bp);
12567 	bnxt_free_mem(bp, true);
12568 	dev_close(bp->dev);
12569 	return rc;
12570 }
12571 
12572 /* rtnl_lock held, this call can only be made after a previous successful
12573  * call to bnxt_half_open_nic().
12574  */
12575 void bnxt_half_close_nic(struct bnxt *bp)
12576 {
12577 	bnxt_hwrm_resource_free(bp, false, true);
12578 	bnxt_del_napi(bp);
12579 	bnxt_free_skbs(bp);
12580 	bnxt_free_mem(bp, true);
12581 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12582 }
12583 
12584 void bnxt_reenable_sriov(struct bnxt *bp)
12585 {
12586 	if (BNXT_PF(bp)) {
12587 		struct bnxt_pf_info *pf = &bp->pf;
12588 		int n = pf->active_vfs;
12589 
12590 		if (n)
12591 			bnxt_cfg_hw_sriov(bp, &n, true);
12592 	}
12593 }
12594 
12595 static int bnxt_open(struct net_device *dev)
12596 {
12597 	struct bnxt *bp = netdev_priv(dev);
12598 	int rc;
12599 
12600 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12601 		rc = bnxt_reinit_after_abort(bp);
12602 		if (rc) {
12603 			if (rc == -EBUSY)
12604 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
12605 			else
12606 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
12607 			return -ENODEV;
12608 		}
12609 	}
12610 
12611 	rc = bnxt_hwrm_if_change(bp, true);
12612 	if (rc)
12613 		return rc;
12614 
12615 	rc = __bnxt_open_nic(bp, true, true);
12616 	if (rc) {
12617 		bnxt_hwrm_if_change(bp, false);
12618 	} else {
12619 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
12620 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12621 				bnxt_queue_sp_work(bp,
12622 						   BNXT_RESTART_ULP_SP_EVENT);
12623 		}
12624 	}
12625 
12626 	return rc;
12627 }
12628 
12629 static bool bnxt_drv_busy(struct bnxt *bp)
12630 {
12631 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
12632 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
12633 }
12634 
12635 static void bnxt_get_ring_stats(struct bnxt *bp,
12636 				struct rtnl_link_stats64 *stats);
12637 
12638 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
12639 			     bool link_re_init)
12640 {
12641 	/* Close the VF-reps before closing PF */
12642 	if (BNXT_PF(bp))
12643 		bnxt_vf_reps_close(bp);
12644 
12645 	/* Change device state to avoid TX queue wake up's */
12646 	bnxt_tx_disable(bp);
12647 
12648 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12649 	smp_mb__after_atomic();
12650 	while (bnxt_drv_busy(bp))
12651 		msleep(20);
12652 
12653 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12654 		bnxt_clear_rss_ctxs(bp);
12655 	/* Flush rings and disable interrupts */
12656 	bnxt_shutdown_nic(bp, irq_re_init);
12657 
12658 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
12659 
12660 	bnxt_debug_dev_exit(bp);
12661 	bnxt_disable_napi(bp);
12662 	del_timer_sync(&bp->timer);
12663 	bnxt_free_skbs(bp);
12664 
12665 	/* Save ring stats before shutdown */
12666 	if (bp->bnapi && irq_re_init) {
12667 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
12668 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
12669 	}
12670 	if (irq_re_init) {
12671 		bnxt_free_irq(bp);
12672 		bnxt_del_napi(bp);
12673 	}
12674 	bnxt_free_mem(bp, irq_re_init);
12675 }
12676 
12677 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12678 {
12679 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12680 		/* If we get here, it means firmware reset is in progress
12681 		 * while we are trying to close.  We can safely proceed with
12682 		 * the close because we are holding rtnl_lock().  Some firmware
12683 		 * messages may fail as we proceed to close.  We set the
12684 		 * ABORT_ERR flag here so that the FW reset thread will later
12685 		 * abort when it gets the rtnl_lock() and sees the flag.
12686 		 */
12687 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
12688 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12689 	}
12690 
12691 #ifdef CONFIG_BNXT_SRIOV
12692 	if (bp->sriov_cfg) {
12693 		int rc;
12694 
12695 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
12696 						      !bp->sriov_cfg,
12697 						      BNXT_SRIOV_CFG_WAIT_TMO);
12698 		if (!rc)
12699 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
12700 		else if (rc < 0)
12701 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
12702 	}
12703 #endif
12704 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
12705 }
12706 
12707 static int bnxt_close(struct net_device *dev)
12708 {
12709 	struct bnxt *bp = netdev_priv(dev);
12710 
12711 	bnxt_close_nic(bp, true, true);
12712 	bnxt_hwrm_shutdown_link(bp);
12713 	bnxt_hwrm_if_change(bp, false);
12714 	return 0;
12715 }
12716 
12717 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
12718 				   u16 *val)
12719 {
12720 	struct hwrm_port_phy_mdio_read_output *resp;
12721 	struct hwrm_port_phy_mdio_read_input *req;
12722 	int rc;
12723 
12724 	if (bp->hwrm_spec_code < 0x10a00)
12725 		return -EOPNOTSUPP;
12726 
12727 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
12728 	if (rc)
12729 		return rc;
12730 
12731 	req->port_id = cpu_to_le16(bp->pf.port_id);
12732 	req->phy_addr = phy_addr;
12733 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12734 	if (mdio_phy_id_is_c45(phy_addr)) {
12735 		req->cl45_mdio = 1;
12736 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12737 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12738 		req->reg_addr = cpu_to_le16(reg);
12739 	}
12740 
12741 	resp = hwrm_req_hold(bp, req);
12742 	rc = hwrm_req_send(bp, req);
12743 	if (!rc)
12744 		*val = le16_to_cpu(resp->reg_data);
12745 	hwrm_req_drop(bp, req);
12746 	return rc;
12747 }
12748 
12749 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
12750 				    u16 val)
12751 {
12752 	struct hwrm_port_phy_mdio_write_input *req;
12753 	int rc;
12754 
12755 	if (bp->hwrm_spec_code < 0x10a00)
12756 		return -EOPNOTSUPP;
12757 
12758 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
12759 	if (rc)
12760 		return rc;
12761 
12762 	req->port_id = cpu_to_le16(bp->pf.port_id);
12763 	req->phy_addr = phy_addr;
12764 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12765 	if (mdio_phy_id_is_c45(phy_addr)) {
12766 		req->cl45_mdio = 1;
12767 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12768 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12769 		req->reg_addr = cpu_to_le16(reg);
12770 	}
12771 	req->reg_data = cpu_to_le16(val);
12772 
12773 	return hwrm_req_send(bp, req);
12774 }
12775 
12776 /* rtnl_lock held */
12777 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12778 {
12779 	struct mii_ioctl_data *mdio = if_mii(ifr);
12780 	struct bnxt *bp = netdev_priv(dev);
12781 	int rc;
12782 
12783 	switch (cmd) {
12784 	case SIOCGMIIPHY:
12785 		mdio->phy_id = bp->link_info.phy_addr;
12786 
12787 		fallthrough;
12788 	case SIOCGMIIREG: {
12789 		u16 mii_regval = 0;
12790 
12791 		if (!netif_running(dev))
12792 			return -EAGAIN;
12793 
12794 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
12795 					     &mii_regval);
12796 		mdio->val_out = mii_regval;
12797 		return rc;
12798 	}
12799 
12800 	case SIOCSMIIREG:
12801 		if (!netif_running(dev))
12802 			return -EAGAIN;
12803 
12804 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
12805 						mdio->val_in);
12806 
12807 	case SIOCSHWTSTAMP:
12808 		return bnxt_hwtstamp_set(dev, ifr);
12809 
12810 	case SIOCGHWTSTAMP:
12811 		return bnxt_hwtstamp_get(dev, ifr);
12812 
12813 	default:
12814 		/* do nothing */
12815 		break;
12816 	}
12817 	return -EOPNOTSUPP;
12818 }
12819 
12820 static void bnxt_get_ring_stats(struct bnxt *bp,
12821 				struct rtnl_link_stats64 *stats)
12822 {
12823 	int i;
12824 
12825 	for (i = 0; i < bp->cp_nr_rings; i++) {
12826 		struct bnxt_napi *bnapi = bp->bnapi[i];
12827 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
12828 		u64 *sw = cpr->stats.sw_stats;
12829 
12830 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
12831 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12832 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
12833 
12834 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
12835 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
12836 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
12837 
12838 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
12839 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
12840 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
12841 
12842 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
12843 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
12844 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
12845 
12846 		stats->rx_missed_errors +=
12847 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
12848 
12849 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12850 
12851 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
12852 
12853 		stats->rx_dropped +=
12854 			cpr->sw_stats->rx.rx_netpoll_discards +
12855 			cpr->sw_stats->rx.rx_oom_discards;
12856 	}
12857 }
12858 
12859 static void bnxt_add_prev_stats(struct bnxt *bp,
12860 				struct rtnl_link_stats64 *stats)
12861 {
12862 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
12863 
12864 	stats->rx_packets += prev_stats->rx_packets;
12865 	stats->tx_packets += prev_stats->tx_packets;
12866 	stats->rx_bytes += prev_stats->rx_bytes;
12867 	stats->tx_bytes += prev_stats->tx_bytes;
12868 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
12869 	stats->multicast += prev_stats->multicast;
12870 	stats->rx_dropped += prev_stats->rx_dropped;
12871 	stats->tx_dropped += prev_stats->tx_dropped;
12872 }
12873 
12874 static void
12875 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
12876 {
12877 	struct bnxt *bp = netdev_priv(dev);
12878 
12879 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
12880 	/* Make sure bnxt_close_nic() sees that we are reading stats before
12881 	 * we check the BNXT_STATE_OPEN flag.
12882 	 */
12883 	smp_mb__after_atomic();
12884 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12885 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12886 		*stats = bp->net_stats_prev;
12887 		return;
12888 	}
12889 
12890 	bnxt_get_ring_stats(bp, stats);
12891 	bnxt_add_prev_stats(bp, stats);
12892 
12893 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
12894 		u64 *rx = bp->port_stats.sw_stats;
12895 		u64 *tx = bp->port_stats.sw_stats +
12896 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
12897 
12898 		stats->rx_crc_errors =
12899 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
12900 		stats->rx_frame_errors =
12901 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
12902 		stats->rx_length_errors =
12903 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
12904 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
12905 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
12906 		stats->rx_errors =
12907 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
12908 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
12909 		stats->collisions =
12910 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
12911 		stats->tx_fifo_errors =
12912 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
12913 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
12914 	}
12915 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12916 }
12917 
12918 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
12919 					struct bnxt_total_ring_err_stats *stats,
12920 					struct bnxt_cp_ring_info *cpr)
12921 {
12922 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
12923 	u64 *hw_stats = cpr->stats.sw_stats;
12924 
12925 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
12926 	stats->rx_total_resets += sw_stats->rx.rx_resets;
12927 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
12928 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
12929 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
12930 	stats->rx_total_ring_discards +=
12931 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
12932 	stats->tx_total_resets += sw_stats->tx.tx_resets;
12933 	stats->tx_total_ring_discards +=
12934 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
12935 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
12936 }
12937 
12938 void bnxt_get_ring_err_stats(struct bnxt *bp,
12939 			     struct bnxt_total_ring_err_stats *stats)
12940 {
12941 	int i;
12942 
12943 	for (i = 0; i < bp->cp_nr_rings; i++)
12944 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
12945 }
12946 
12947 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
12948 {
12949 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12950 	struct net_device *dev = bp->dev;
12951 	struct netdev_hw_addr *ha;
12952 	u8 *haddr;
12953 	int mc_count = 0;
12954 	bool update = false;
12955 	int off = 0;
12956 
12957 	netdev_for_each_mc_addr(ha, dev) {
12958 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
12959 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12960 			vnic->mc_list_count = 0;
12961 			return false;
12962 		}
12963 		haddr = ha->addr;
12964 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
12965 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
12966 			update = true;
12967 		}
12968 		off += ETH_ALEN;
12969 		mc_count++;
12970 	}
12971 	if (mc_count)
12972 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12973 
12974 	if (mc_count != vnic->mc_list_count) {
12975 		vnic->mc_list_count = mc_count;
12976 		update = true;
12977 	}
12978 	return update;
12979 }
12980 
12981 static bool bnxt_uc_list_updated(struct bnxt *bp)
12982 {
12983 	struct net_device *dev = bp->dev;
12984 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12985 	struct netdev_hw_addr *ha;
12986 	int off = 0;
12987 
12988 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
12989 		return true;
12990 
12991 	netdev_for_each_uc_addr(ha, dev) {
12992 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
12993 			return true;
12994 
12995 		off += ETH_ALEN;
12996 	}
12997 	return false;
12998 }
12999 
13000 static void bnxt_set_rx_mode(struct net_device *dev)
13001 {
13002 	struct bnxt *bp = netdev_priv(dev);
13003 	struct bnxt_vnic_info *vnic;
13004 	bool mc_update = false;
13005 	bool uc_update;
13006 	u32 mask;
13007 
13008 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13009 		return;
13010 
13011 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13012 	mask = vnic->rx_mask;
13013 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13014 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13015 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13016 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13017 
13018 	if (dev->flags & IFF_PROMISC)
13019 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13020 
13021 	uc_update = bnxt_uc_list_updated(bp);
13022 
13023 	if (dev->flags & IFF_BROADCAST)
13024 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13025 	if (dev->flags & IFF_ALLMULTI) {
13026 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13027 		vnic->mc_list_count = 0;
13028 	} else if (dev->flags & IFF_MULTICAST) {
13029 		mc_update = bnxt_mc_list_updated(bp, &mask);
13030 	}
13031 
13032 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13033 		vnic->rx_mask = mask;
13034 
13035 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13036 	}
13037 }
13038 
13039 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13040 {
13041 	struct net_device *dev = bp->dev;
13042 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13043 	struct netdev_hw_addr *ha;
13044 	int i, off = 0, rc;
13045 	bool uc_update;
13046 
13047 	netif_addr_lock_bh(dev);
13048 	uc_update = bnxt_uc_list_updated(bp);
13049 	netif_addr_unlock_bh(dev);
13050 
13051 	if (!uc_update)
13052 		goto skip_uc;
13053 
13054 	for (i = 1; i < vnic->uc_filter_count; i++) {
13055 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13056 
13057 		bnxt_hwrm_l2_filter_free(bp, fltr);
13058 		bnxt_del_l2_filter(bp, fltr);
13059 	}
13060 
13061 	vnic->uc_filter_count = 1;
13062 
13063 	netif_addr_lock_bh(dev);
13064 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13065 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13066 	} else {
13067 		netdev_for_each_uc_addr(ha, dev) {
13068 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13069 			off += ETH_ALEN;
13070 			vnic->uc_filter_count++;
13071 		}
13072 	}
13073 	netif_addr_unlock_bh(dev);
13074 
13075 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13076 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13077 		if (rc) {
13078 			if (BNXT_VF(bp) && rc == -ENODEV) {
13079 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13080 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13081 				else
13082 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13083 				rc = 0;
13084 			} else {
13085 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13086 			}
13087 			vnic->uc_filter_count = i;
13088 			return rc;
13089 		}
13090 	}
13091 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13092 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13093 
13094 skip_uc:
13095 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13096 	    !bnxt_promisc_ok(bp))
13097 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13098 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13099 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13100 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13101 			    rc);
13102 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13103 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13104 		vnic->mc_list_count = 0;
13105 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13106 	}
13107 	if (rc)
13108 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13109 			   rc);
13110 
13111 	return rc;
13112 }
13113 
13114 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13115 {
13116 #ifdef CONFIG_BNXT_SRIOV
13117 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13118 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13119 
13120 		/* No minimum rings were provisioned by the PF.  Don't
13121 		 * reserve rings by default when device is down.
13122 		 */
13123 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13124 			return true;
13125 
13126 		if (!netif_running(bp->dev))
13127 			return false;
13128 	}
13129 #endif
13130 	return true;
13131 }
13132 
13133 /* If the chip and firmware supports RFS */
13134 static bool bnxt_rfs_supported(struct bnxt *bp)
13135 {
13136 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13137 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13138 			return true;
13139 		return false;
13140 	}
13141 	/* 212 firmware is broken for aRFS */
13142 	if (BNXT_FW_MAJ(bp) == 212)
13143 		return false;
13144 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13145 		return true;
13146 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13147 		return true;
13148 	return false;
13149 }
13150 
13151 /* If runtime conditions support RFS */
13152 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13153 {
13154 	struct bnxt_hw_rings hwr = {0};
13155 	int max_vnics, max_rss_ctxs;
13156 
13157 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13158 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13159 		return bnxt_rfs_supported(bp);
13160 
13161 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13162 		return false;
13163 
13164 	hwr.grp = bp->rx_nr_rings;
13165 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13166 	if (new_rss_ctx)
13167 		hwr.vnic++;
13168 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13169 	max_vnics = bnxt_get_max_func_vnics(bp);
13170 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13171 
13172 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13173 		if (bp->rx_nr_rings > 1)
13174 			netdev_warn(bp->dev,
13175 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13176 				    min(max_rss_ctxs - 1, max_vnics - 1));
13177 		return false;
13178 	}
13179 
13180 	if (!BNXT_NEW_RM(bp))
13181 		return true;
13182 
13183 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13184 	 * issue that will mess up the default VNIC if we reduce the
13185 	 * reservations.
13186 	 */
13187 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13188 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13189 		return true;
13190 
13191 	bnxt_hwrm_reserve_rings(bp, &hwr);
13192 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13193 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13194 		return true;
13195 
13196 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13197 	hwr.vnic = 1;
13198 	hwr.rss_ctx = 0;
13199 	bnxt_hwrm_reserve_rings(bp, &hwr);
13200 	return false;
13201 }
13202 
13203 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13204 					   netdev_features_t features)
13205 {
13206 	struct bnxt *bp = netdev_priv(dev);
13207 	netdev_features_t vlan_features;
13208 
13209 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13210 		features &= ~NETIF_F_NTUPLE;
13211 
13212 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13213 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13214 
13215 	if (!(features & NETIF_F_GRO))
13216 		features &= ~NETIF_F_GRO_HW;
13217 
13218 	if (features & NETIF_F_GRO_HW)
13219 		features &= ~NETIF_F_LRO;
13220 
13221 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13222 	 * turned on or off together.
13223 	 */
13224 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13225 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13226 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13227 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13228 		else if (vlan_features)
13229 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13230 	}
13231 #ifdef CONFIG_BNXT_SRIOV
13232 	if (BNXT_VF(bp) && bp->vf.vlan)
13233 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13234 #endif
13235 	return features;
13236 }
13237 
13238 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13239 				bool link_re_init, u32 flags, bool update_tpa)
13240 {
13241 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13242 	bp->flags = flags;
13243 	if (update_tpa)
13244 		bnxt_set_ring_params(bp);
13245 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13246 }
13247 
13248 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13249 {
13250 	bool update_tpa = false, update_ntuple = false;
13251 	struct bnxt *bp = netdev_priv(dev);
13252 	u32 flags = bp->flags;
13253 	u32 changes;
13254 	int rc = 0;
13255 	bool re_init = false;
13256 
13257 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13258 	if (features & NETIF_F_GRO_HW)
13259 		flags |= BNXT_FLAG_GRO;
13260 	else if (features & NETIF_F_LRO)
13261 		flags |= BNXT_FLAG_LRO;
13262 
13263 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13264 		flags &= ~BNXT_FLAG_TPA;
13265 
13266 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13267 		flags |= BNXT_FLAG_STRIP_VLAN;
13268 
13269 	if (features & NETIF_F_NTUPLE)
13270 		flags |= BNXT_FLAG_RFS;
13271 	else
13272 		bnxt_clear_usr_fltrs(bp, true);
13273 
13274 	changes = flags ^ bp->flags;
13275 	if (changes & BNXT_FLAG_TPA) {
13276 		update_tpa = true;
13277 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13278 		    (flags & BNXT_FLAG_TPA) == 0 ||
13279 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13280 			re_init = true;
13281 	}
13282 
13283 	if (changes & ~BNXT_FLAG_TPA)
13284 		re_init = true;
13285 
13286 	if (changes & BNXT_FLAG_RFS)
13287 		update_ntuple = true;
13288 
13289 	if (flags != bp->flags) {
13290 		u32 old_flags = bp->flags;
13291 
13292 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13293 			bp->flags = flags;
13294 			if (update_tpa)
13295 				bnxt_set_ring_params(bp);
13296 			return rc;
13297 		}
13298 
13299 		if (update_ntuple)
13300 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13301 
13302 		if (re_init)
13303 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13304 
13305 		if (update_tpa) {
13306 			bp->flags = flags;
13307 			rc = bnxt_set_tpa(bp,
13308 					  (flags & BNXT_FLAG_TPA) ?
13309 					  true : false);
13310 			if (rc)
13311 				bp->flags = old_flags;
13312 		}
13313 	}
13314 	return rc;
13315 }
13316 
13317 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13318 			      u8 **nextp)
13319 {
13320 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13321 	struct hop_jumbo_hdr *jhdr;
13322 	int hdr_count = 0;
13323 	u8 *nexthdr;
13324 	int start;
13325 
13326 	/* Check that there are at most 2 IPv6 extension headers, no
13327 	 * fragment header, and each is <= 64 bytes.
13328 	 */
13329 	start = nw_off + sizeof(*ip6h);
13330 	nexthdr = &ip6h->nexthdr;
13331 	while (ipv6_ext_hdr(*nexthdr)) {
13332 		struct ipv6_opt_hdr *hp;
13333 		int hdrlen;
13334 
13335 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13336 		    *nexthdr == NEXTHDR_FRAGMENT)
13337 			return false;
13338 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13339 					  skb_headlen(skb), NULL);
13340 		if (!hp)
13341 			return false;
13342 		if (*nexthdr == NEXTHDR_AUTH)
13343 			hdrlen = ipv6_authlen(hp);
13344 		else
13345 			hdrlen = ipv6_optlen(hp);
13346 
13347 		if (hdrlen > 64)
13348 			return false;
13349 
13350 		/* The ext header may be a hop-by-hop header inserted for
13351 		 * big TCP purposes. This will be removed before sending
13352 		 * from NIC, so do not count it.
13353 		 */
13354 		if (*nexthdr == NEXTHDR_HOP) {
13355 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13356 				goto increment_hdr;
13357 
13358 			jhdr = (struct hop_jumbo_hdr *)hp;
13359 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13360 			    jhdr->nexthdr != IPPROTO_TCP)
13361 				goto increment_hdr;
13362 
13363 			goto next_hdr;
13364 		}
13365 increment_hdr:
13366 		hdr_count++;
13367 next_hdr:
13368 		nexthdr = &hp->nexthdr;
13369 		start += hdrlen;
13370 	}
13371 	if (nextp) {
13372 		/* Caller will check inner protocol */
13373 		if (skb->encapsulation) {
13374 			*nextp = nexthdr;
13375 			return true;
13376 		}
13377 		*nextp = NULL;
13378 	}
13379 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13380 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13381 }
13382 
13383 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13384 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13385 {
13386 	struct udphdr *uh = udp_hdr(skb);
13387 	__be16 udp_port = uh->dest;
13388 
13389 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13390 	    udp_port != bp->vxlan_gpe_port)
13391 		return false;
13392 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13393 		struct ethhdr *eh = inner_eth_hdr(skb);
13394 
13395 		switch (eh->h_proto) {
13396 		case htons(ETH_P_IP):
13397 			return true;
13398 		case htons(ETH_P_IPV6):
13399 			return bnxt_exthdr_check(bp, skb,
13400 						 skb_inner_network_offset(skb),
13401 						 NULL);
13402 		}
13403 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13404 		return true;
13405 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13406 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13407 					 NULL);
13408 	}
13409 	return false;
13410 }
13411 
13412 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13413 {
13414 	switch (l4_proto) {
13415 	case IPPROTO_UDP:
13416 		return bnxt_udp_tunl_check(bp, skb);
13417 	case IPPROTO_IPIP:
13418 		return true;
13419 	case IPPROTO_GRE: {
13420 		switch (skb->inner_protocol) {
13421 		default:
13422 			return false;
13423 		case htons(ETH_P_IP):
13424 			return true;
13425 		case htons(ETH_P_IPV6):
13426 			fallthrough;
13427 		}
13428 	}
13429 	case IPPROTO_IPV6:
13430 		/* Check ext headers of inner ipv6 */
13431 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13432 					 NULL);
13433 	}
13434 	return false;
13435 }
13436 
13437 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13438 					     struct net_device *dev,
13439 					     netdev_features_t features)
13440 {
13441 	struct bnxt *bp = netdev_priv(dev);
13442 	u8 *l4_proto;
13443 
13444 	features = vlan_features_check(skb, features);
13445 	switch (vlan_get_protocol(skb)) {
13446 	case htons(ETH_P_IP):
13447 		if (!skb->encapsulation)
13448 			return features;
13449 		l4_proto = &ip_hdr(skb)->protocol;
13450 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13451 			return features;
13452 		break;
13453 	case htons(ETH_P_IPV6):
13454 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13455 				       &l4_proto))
13456 			break;
13457 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13458 			return features;
13459 		break;
13460 	}
13461 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13462 }
13463 
13464 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13465 			 u32 *reg_buf)
13466 {
13467 	struct hwrm_dbg_read_direct_output *resp;
13468 	struct hwrm_dbg_read_direct_input *req;
13469 	__le32 *dbg_reg_buf;
13470 	dma_addr_t mapping;
13471 	int rc, i;
13472 
13473 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13474 	if (rc)
13475 		return rc;
13476 
13477 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13478 					 &mapping);
13479 	if (!dbg_reg_buf) {
13480 		rc = -ENOMEM;
13481 		goto dbg_rd_reg_exit;
13482 	}
13483 
13484 	req->host_dest_addr = cpu_to_le64(mapping);
13485 
13486 	resp = hwrm_req_hold(bp, req);
13487 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13488 	req->read_len32 = cpu_to_le32(num_words);
13489 
13490 	rc = hwrm_req_send(bp, req);
13491 	if (rc || resp->error_code) {
13492 		rc = -EIO;
13493 		goto dbg_rd_reg_exit;
13494 	}
13495 	for (i = 0; i < num_words; i++)
13496 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13497 
13498 dbg_rd_reg_exit:
13499 	hwrm_req_drop(bp, req);
13500 	return rc;
13501 }
13502 
13503 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13504 				       u32 ring_id, u32 *prod, u32 *cons)
13505 {
13506 	struct hwrm_dbg_ring_info_get_output *resp;
13507 	struct hwrm_dbg_ring_info_get_input *req;
13508 	int rc;
13509 
13510 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13511 	if (rc)
13512 		return rc;
13513 
13514 	req->ring_type = ring_type;
13515 	req->fw_ring_id = cpu_to_le32(ring_id);
13516 	resp = hwrm_req_hold(bp, req);
13517 	rc = hwrm_req_send(bp, req);
13518 	if (!rc) {
13519 		*prod = le32_to_cpu(resp->producer_index);
13520 		*cons = le32_to_cpu(resp->consumer_index);
13521 	}
13522 	hwrm_req_drop(bp, req);
13523 	return rc;
13524 }
13525 
13526 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13527 {
13528 	struct bnxt_tx_ring_info *txr;
13529 	int i = bnapi->index, j;
13530 
13531 	bnxt_for_each_napi_tx(j, bnapi, txr)
13532 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13533 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13534 			    txr->tx_cons);
13535 }
13536 
13537 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13538 {
13539 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13540 	int i = bnapi->index;
13541 
13542 	if (!rxr)
13543 		return;
13544 
13545 	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",
13546 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13547 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13548 		    rxr->rx_sw_agg_prod);
13549 }
13550 
13551 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13552 {
13553 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13554 	int i = bnapi->index;
13555 
13556 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13557 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13558 }
13559 
13560 static void bnxt_dbg_dump_states(struct bnxt *bp)
13561 {
13562 	int i;
13563 	struct bnxt_napi *bnapi;
13564 
13565 	for (i = 0; i < bp->cp_nr_rings; i++) {
13566 		bnapi = bp->bnapi[i];
13567 		if (netif_msg_drv(bp)) {
13568 			bnxt_dump_tx_sw_state(bnapi);
13569 			bnxt_dump_rx_sw_state(bnapi);
13570 			bnxt_dump_cp_sw_state(bnapi);
13571 		}
13572 	}
13573 }
13574 
13575 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13576 {
13577 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13578 	struct hwrm_ring_reset_input *req;
13579 	struct bnxt_napi *bnapi = rxr->bnapi;
13580 	struct bnxt_cp_ring_info *cpr;
13581 	u16 cp_ring_id;
13582 	int rc;
13583 
13584 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13585 	if (rc)
13586 		return rc;
13587 
13588 	cpr = &bnapi->cp_ring;
13589 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
13590 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
13591 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
13592 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
13593 	return hwrm_req_send_silent(bp, req);
13594 }
13595 
13596 static void bnxt_reset_task(struct bnxt *bp, bool silent)
13597 {
13598 	if (!silent)
13599 		bnxt_dbg_dump_states(bp);
13600 	if (netif_running(bp->dev)) {
13601 		bnxt_close_nic(bp, !silent, false);
13602 		bnxt_open_nic(bp, !silent, false);
13603 	}
13604 }
13605 
13606 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
13607 {
13608 	struct bnxt *bp = netdev_priv(dev);
13609 
13610 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
13611 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
13612 }
13613 
13614 static void bnxt_fw_health_check(struct bnxt *bp)
13615 {
13616 	struct bnxt_fw_health *fw_health = bp->fw_health;
13617 	struct pci_dev *pdev = bp->pdev;
13618 	u32 val;
13619 
13620 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13621 		return;
13622 
13623 	/* Make sure it is enabled before checking the tmr_counter. */
13624 	smp_rmb();
13625 	if (fw_health->tmr_counter) {
13626 		fw_health->tmr_counter--;
13627 		return;
13628 	}
13629 
13630 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13631 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
13632 		fw_health->arrests++;
13633 		goto fw_reset;
13634 	}
13635 
13636 	fw_health->last_fw_heartbeat = val;
13637 
13638 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13639 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
13640 		fw_health->discoveries++;
13641 		goto fw_reset;
13642 	}
13643 
13644 	fw_health->tmr_counter = fw_health->tmr_multiplier;
13645 	return;
13646 
13647 fw_reset:
13648 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
13649 }
13650 
13651 static void bnxt_timer(struct timer_list *t)
13652 {
13653 	struct bnxt *bp = from_timer(bp, t, timer);
13654 	struct net_device *dev = bp->dev;
13655 
13656 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
13657 		return;
13658 
13659 	if (atomic_read(&bp->intr_sem) != 0)
13660 		goto bnxt_restart_timer;
13661 
13662 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
13663 		bnxt_fw_health_check(bp);
13664 
13665 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
13666 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
13667 
13668 	if (bnxt_tc_flower_enabled(bp))
13669 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
13670 
13671 #ifdef CONFIG_RFS_ACCEL
13672 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
13673 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
13674 #endif /*CONFIG_RFS_ACCEL*/
13675 
13676 	if (bp->link_info.phy_retry) {
13677 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
13678 			bp->link_info.phy_retry = false;
13679 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
13680 		} else {
13681 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
13682 		}
13683 	}
13684 
13685 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13686 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13687 
13688 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
13689 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
13690 
13691 bnxt_restart_timer:
13692 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13693 }
13694 
13695 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
13696 {
13697 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
13698 	 * set.  If the device is being closed, bnxt_close() may be holding
13699 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
13700 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
13701 	 */
13702 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13703 	rtnl_lock();
13704 }
13705 
13706 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
13707 {
13708 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13709 	rtnl_unlock();
13710 }
13711 
13712 /* Only called from bnxt_sp_task() */
13713 static void bnxt_reset(struct bnxt *bp, bool silent)
13714 {
13715 	bnxt_rtnl_lock_sp(bp);
13716 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
13717 		bnxt_reset_task(bp, silent);
13718 	bnxt_rtnl_unlock_sp(bp);
13719 }
13720 
13721 /* Only called from bnxt_sp_task() */
13722 static void bnxt_rx_ring_reset(struct bnxt *bp)
13723 {
13724 	int i;
13725 
13726 	bnxt_rtnl_lock_sp(bp);
13727 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13728 		bnxt_rtnl_unlock_sp(bp);
13729 		return;
13730 	}
13731 	/* Disable and flush TPA before resetting the RX ring */
13732 	if (bp->flags & BNXT_FLAG_TPA)
13733 		bnxt_set_tpa(bp, false);
13734 	for (i = 0; i < bp->rx_nr_rings; i++) {
13735 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
13736 		struct bnxt_cp_ring_info *cpr;
13737 		int rc;
13738 
13739 		if (!rxr->bnapi->in_reset)
13740 			continue;
13741 
13742 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
13743 		if (rc) {
13744 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
13745 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
13746 			else
13747 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
13748 					    rc);
13749 			bnxt_reset_task(bp, true);
13750 			break;
13751 		}
13752 		bnxt_free_one_rx_ring_skbs(bp, rxr);
13753 		rxr->rx_prod = 0;
13754 		rxr->rx_agg_prod = 0;
13755 		rxr->rx_sw_agg_prod = 0;
13756 		rxr->rx_next_cons = 0;
13757 		rxr->bnapi->in_reset = false;
13758 		bnxt_alloc_one_rx_ring(bp, i);
13759 		cpr = &rxr->bnapi->cp_ring;
13760 		cpr->sw_stats->rx.rx_resets++;
13761 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
13762 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
13763 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
13764 	}
13765 	if (bp->flags & BNXT_FLAG_TPA)
13766 		bnxt_set_tpa(bp, true);
13767 	bnxt_rtnl_unlock_sp(bp);
13768 }
13769 
13770 static void bnxt_fw_fatal_close(struct bnxt *bp)
13771 {
13772 	bnxt_tx_disable(bp);
13773 	bnxt_disable_napi(bp);
13774 	bnxt_disable_int_sync(bp);
13775 	bnxt_free_irq(bp);
13776 	bnxt_clear_int_mode(bp);
13777 	pci_disable_device(bp->pdev);
13778 }
13779 
13780 static void bnxt_fw_reset_close(struct bnxt *bp)
13781 {
13782 	/* When firmware is in fatal state, quiesce device and disable
13783 	 * bus master to prevent any potential bad DMAs before freeing
13784 	 * kernel memory.
13785 	 */
13786 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
13787 		u16 val = 0;
13788 
13789 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
13790 		if (val == 0xffff)
13791 			bp->fw_reset_min_dsecs = 0;
13792 		bnxt_fw_fatal_close(bp);
13793 	}
13794 	__bnxt_close_nic(bp, true, false);
13795 	bnxt_vf_reps_free(bp);
13796 	bnxt_clear_int_mode(bp);
13797 	bnxt_hwrm_func_drv_unrgtr(bp);
13798 	if (pci_is_enabled(bp->pdev))
13799 		pci_disable_device(bp->pdev);
13800 	bnxt_free_ctx_mem(bp, false);
13801 }
13802 
13803 static bool is_bnxt_fw_ok(struct bnxt *bp)
13804 {
13805 	struct bnxt_fw_health *fw_health = bp->fw_health;
13806 	bool no_heartbeat = false, has_reset = false;
13807 	u32 val;
13808 
13809 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13810 	if (val == fw_health->last_fw_heartbeat)
13811 		no_heartbeat = true;
13812 
13813 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13814 	if (val != fw_health->last_fw_reset_cnt)
13815 		has_reset = true;
13816 
13817 	if (!no_heartbeat && has_reset)
13818 		return true;
13819 
13820 	return false;
13821 }
13822 
13823 /* rtnl_lock is acquired before calling this function */
13824 static void bnxt_force_fw_reset(struct bnxt *bp)
13825 {
13826 	struct bnxt_fw_health *fw_health = bp->fw_health;
13827 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13828 	u32 wait_dsecs;
13829 
13830 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
13831 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13832 		return;
13833 
13834 	/* we have to serialize with bnxt_refclk_read()*/
13835 	if (ptp) {
13836 		unsigned long flags;
13837 
13838 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
13839 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13840 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
13841 	} else {
13842 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13843 	}
13844 	bnxt_fw_reset_close(bp);
13845 	wait_dsecs = fw_health->master_func_wait_dsecs;
13846 	if (fw_health->primary) {
13847 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
13848 			wait_dsecs = 0;
13849 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
13850 	} else {
13851 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
13852 		wait_dsecs = fw_health->normal_func_wait_dsecs;
13853 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13854 	}
13855 
13856 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
13857 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
13858 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
13859 }
13860 
13861 void bnxt_fw_exception(struct bnxt *bp)
13862 {
13863 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
13864 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
13865 	bnxt_ulp_stop(bp);
13866 	bnxt_rtnl_lock_sp(bp);
13867 	bnxt_force_fw_reset(bp);
13868 	bnxt_rtnl_unlock_sp(bp);
13869 }
13870 
13871 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
13872  * < 0 on error.
13873  */
13874 static int bnxt_get_registered_vfs(struct bnxt *bp)
13875 {
13876 #ifdef CONFIG_BNXT_SRIOV
13877 	int rc;
13878 
13879 	if (!BNXT_PF(bp))
13880 		return 0;
13881 
13882 	rc = bnxt_hwrm_func_qcfg(bp);
13883 	if (rc) {
13884 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
13885 		return rc;
13886 	}
13887 	if (bp->pf.registered_vfs)
13888 		return bp->pf.registered_vfs;
13889 	if (bp->sriov_cfg)
13890 		return 1;
13891 #endif
13892 	return 0;
13893 }
13894 
13895 void bnxt_fw_reset(struct bnxt *bp)
13896 {
13897 	bnxt_ulp_stop(bp);
13898 	bnxt_rtnl_lock_sp(bp);
13899 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
13900 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13901 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13902 		int n = 0, tmo;
13903 
13904 		/* we have to serialize with bnxt_refclk_read()*/
13905 		if (ptp) {
13906 			unsigned long flags;
13907 
13908 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
13909 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13910 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
13911 		} else {
13912 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13913 		}
13914 		if (bp->pf.active_vfs &&
13915 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
13916 			n = bnxt_get_registered_vfs(bp);
13917 		if (n < 0) {
13918 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
13919 				   n);
13920 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13921 			dev_close(bp->dev);
13922 			goto fw_reset_exit;
13923 		} else if (n > 0) {
13924 			u16 vf_tmo_dsecs = n * 10;
13925 
13926 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
13927 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
13928 			bp->fw_reset_state =
13929 				BNXT_FW_RESET_STATE_POLL_VF;
13930 			bnxt_queue_fw_reset_work(bp, HZ / 10);
13931 			goto fw_reset_exit;
13932 		}
13933 		bnxt_fw_reset_close(bp);
13934 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13935 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
13936 			tmo = HZ / 10;
13937 		} else {
13938 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13939 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
13940 		}
13941 		bnxt_queue_fw_reset_work(bp, tmo);
13942 	}
13943 fw_reset_exit:
13944 	bnxt_rtnl_unlock_sp(bp);
13945 }
13946 
13947 static void bnxt_chk_missed_irq(struct bnxt *bp)
13948 {
13949 	int i;
13950 
13951 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13952 		return;
13953 
13954 	for (i = 0; i < bp->cp_nr_rings; i++) {
13955 		struct bnxt_napi *bnapi = bp->bnapi[i];
13956 		struct bnxt_cp_ring_info *cpr;
13957 		u32 fw_ring_id;
13958 		int j;
13959 
13960 		if (!bnapi)
13961 			continue;
13962 
13963 		cpr = &bnapi->cp_ring;
13964 		for (j = 0; j < cpr->cp_ring_count; j++) {
13965 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
13966 			u32 val[2];
13967 
13968 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
13969 				continue;
13970 
13971 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
13972 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
13973 				continue;
13974 			}
13975 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
13976 			bnxt_dbg_hwrm_ring_info_get(bp,
13977 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
13978 				fw_ring_id, &val[0], &val[1]);
13979 			cpr->sw_stats->cmn.missed_irqs++;
13980 		}
13981 	}
13982 }
13983 
13984 static void bnxt_cfg_ntp_filters(struct bnxt *);
13985 
13986 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
13987 {
13988 	struct bnxt_link_info *link_info = &bp->link_info;
13989 
13990 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
13991 		link_info->autoneg = BNXT_AUTONEG_SPEED;
13992 		if (bp->hwrm_spec_code >= 0x10201) {
13993 			if (link_info->auto_pause_setting &
13994 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
13995 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13996 		} else {
13997 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13998 		}
13999 		bnxt_set_auto_speed(link_info);
14000 	} else {
14001 		bnxt_set_force_speed(link_info);
14002 		link_info->req_duplex = link_info->duplex_setting;
14003 	}
14004 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14005 		link_info->req_flow_ctrl =
14006 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14007 	else
14008 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14009 }
14010 
14011 static void bnxt_fw_echo_reply(struct bnxt *bp)
14012 {
14013 	struct bnxt_fw_health *fw_health = bp->fw_health;
14014 	struct hwrm_func_echo_response_input *req;
14015 	int rc;
14016 
14017 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14018 	if (rc)
14019 		return;
14020 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14021 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14022 	hwrm_req_send(bp, req);
14023 }
14024 
14025 static void bnxt_ulp_restart(struct bnxt *bp)
14026 {
14027 	bnxt_ulp_stop(bp);
14028 	bnxt_ulp_start(bp, 0);
14029 }
14030 
14031 static void bnxt_sp_task(struct work_struct *work)
14032 {
14033 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14034 
14035 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14036 	smp_mb__after_atomic();
14037 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14038 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14039 		return;
14040 	}
14041 
14042 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14043 		bnxt_ulp_restart(bp);
14044 		bnxt_reenable_sriov(bp);
14045 	}
14046 
14047 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14048 		bnxt_cfg_rx_mode(bp);
14049 
14050 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14051 		bnxt_cfg_ntp_filters(bp);
14052 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14053 		bnxt_hwrm_exec_fwd_req(bp);
14054 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14055 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14056 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14057 		bnxt_hwrm_port_qstats(bp, 0);
14058 		bnxt_hwrm_port_qstats_ext(bp, 0);
14059 		bnxt_accumulate_all_stats(bp);
14060 	}
14061 
14062 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14063 		int rc;
14064 
14065 		mutex_lock(&bp->link_lock);
14066 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14067 				       &bp->sp_event))
14068 			bnxt_hwrm_phy_qcaps(bp);
14069 
14070 		rc = bnxt_update_link(bp, true);
14071 		if (rc)
14072 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14073 				   rc);
14074 
14075 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14076 				       &bp->sp_event))
14077 			bnxt_init_ethtool_link_settings(bp);
14078 		mutex_unlock(&bp->link_lock);
14079 	}
14080 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14081 		int rc;
14082 
14083 		mutex_lock(&bp->link_lock);
14084 		rc = bnxt_update_phy_setting(bp);
14085 		mutex_unlock(&bp->link_lock);
14086 		if (rc) {
14087 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14088 		} else {
14089 			bp->link_info.phy_retry = false;
14090 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14091 		}
14092 	}
14093 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14094 		mutex_lock(&bp->link_lock);
14095 		bnxt_get_port_module_status(bp);
14096 		mutex_unlock(&bp->link_lock);
14097 	}
14098 
14099 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14100 		bnxt_tc_flow_stats_work(bp);
14101 
14102 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14103 		bnxt_chk_missed_irq(bp);
14104 
14105 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14106 		bnxt_fw_echo_reply(bp);
14107 
14108 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14109 		bnxt_hwmon_notify_event(bp);
14110 
14111 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14112 	 * must be the last functions to be called before exiting.
14113 	 */
14114 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14115 		bnxt_reset(bp, false);
14116 
14117 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14118 		bnxt_reset(bp, true);
14119 
14120 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14121 		bnxt_rx_ring_reset(bp);
14122 
14123 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14124 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14125 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14126 			bnxt_devlink_health_fw_report(bp);
14127 		else
14128 			bnxt_fw_reset(bp);
14129 	}
14130 
14131 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14132 		if (!is_bnxt_fw_ok(bp))
14133 			bnxt_devlink_health_fw_report(bp);
14134 	}
14135 
14136 	smp_mb__before_atomic();
14137 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14138 }
14139 
14140 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14141 				int *max_cp);
14142 
14143 /* Under rtnl_lock */
14144 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14145 		     int tx_xdp)
14146 {
14147 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14148 	struct bnxt_hw_rings hwr = {0};
14149 	int rx_rings = rx;
14150 	int rc;
14151 
14152 	if (tcs)
14153 		tx_sets = tcs;
14154 
14155 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14156 
14157 	if (max_rx < rx_rings)
14158 		return -ENOMEM;
14159 
14160 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14161 		rx_rings <<= 1;
14162 
14163 	hwr.rx = rx_rings;
14164 	hwr.tx = tx * tx_sets + tx_xdp;
14165 	if (max_tx < hwr.tx)
14166 		return -ENOMEM;
14167 
14168 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14169 
14170 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14171 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14172 	if (max_cp < hwr.cp)
14173 		return -ENOMEM;
14174 	hwr.stat = hwr.cp;
14175 	if (BNXT_NEW_RM(bp)) {
14176 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14177 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14178 		hwr.grp = rx;
14179 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14180 	}
14181 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14182 		hwr.cp_p5 = hwr.tx + rx;
14183 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14184 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14185 		if (!bnxt_ulp_registered(bp->edev)) {
14186 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14187 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14188 		}
14189 		if (hwr.cp > bp->total_irqs) {
14190 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14191 
14192 			if (total_msix < hwr.cp) {
14193 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14194 					    hwr.cp, total_msix);
14195 				rc = -ENOSPC;
14196 			}
14197 		}
14198 	}
14199 	return rc;
14200 }
14201 
14202 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14203 {
14204 	if (bp->bar2) {
14205 		pci_iounmap(pdev, bp->bar2);
14206 		bp->bar2 = NULL;
14207 	}
14208 
14209 	if (bp->bar1) {
14210 		pci_iounmap(pdev, bp->bar1);
14211 		bp->bar1 = NULL;
14212 	}
14213 
14214 	if (bp->bar0) {
14215 		pci_iounmap(pdev, bp->bar0);
14216 		bp->bar0 = NULL;
14217 	}
14218 }
14219 
14220 static void bnxt_cleanup_pci(struct bnxt *bp)
14221 {
14222 	bnxt_unmap_bars(bp, bp->pdev);
14223 	pci_release_regions(bp->pdev);
14224 	if (pci_is_enabled(bp->pdev))
14225 		pci_disable_device(bp->pdev);
14226 }
14227 
14228 static void bnxt_init_dflt_coal(struct bnxt *bp)
14229 {
14230 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14231 	struct bnxt_coal *coal;
14232 	u16 flags = 0;
14233 
14234 	if (coal_cap->cmpl_params &
14235 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14236 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14237 
14238 	/* Tick values in micro seconds.
14239 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14240 	 */
14241 	coal = &bp->rx_coal;
14242 	coal->coal_ticks = 10;
14243 	coal->coal_bufs = 30;
14244 	coal->coal_ticks_irq = 1;
14245 	coal->coal_bufs_irq = 2;
14246 	coal->idle_thresh = 50;
14247 	coal->bufs_per_record = 2;
14248 	coal->budget = 64;		/* NAPI budget */
14249 	coal->flags = flags;
14250 
14251 	coal = &bp->tx_coal;
14252 	coal->coal_ticks = 28;
14253 	coal->coal_bufs = 30;
14254 	coal->coal_ticks_irq = 2;
14255 	coal->coal_bufs_irq = 2;
14256 	coal->bufs_per_record = 1;
14257 	coal->flags = flags;
14258 
14259 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14260 }
14261 
14262 /* FW that pre-reserves 1 VNIC per function */
14263 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14264 {
14265 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14266 
14267 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14268 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14269 		return true;
14270 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14271 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14272 		return true;
14273 	return false;
14274 }
14275 
14276 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14277 {
14278 	int rc;
14279 
14280 	bp->fw_cap = 0;
14281 	rc = bnxt_hwrm_ver_get(bp);
14282 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14283 	 * so wait before continuing with recovery.
14284 	 */
14285 	if (rc)
14286 		msleep(100);
14287 	bnxt_try_map_fw_health_reg(bp);
14288 	if (rc) {
14289 		rc = bnxt_try_recover_fw(bp);
14290 		if (rc)
14291 			return rc;
14292 		rc = bnxt_hwrm_ver_get(bp);
14293 		if (rc)
14294 			return rc;
14295 	}
14296 
14297 	bnxt_nvm_cfg_ver_get(bp);
14298 
14299 	rc = bnxt_hwrm_func_reset(bp);
14300 	if (rc)
14301 		return -ENODEV;
14302 
14303 	bnxt_hwrm_fw_set_time(bp);
14304 	return 0;
14305 }
14306 
14307 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14308 {
14309 	int rc;
14310 
14311 	/* Get the MAX capabilities for this function */
14312 	rc = bnxt_hwrm_func_qcaps(bp);
14313 	if (rc) {
14314 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14315 			   rc);
14316 		return -ENODEV;
14317 	}
14318 
14319 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14320 	if (rc)
14321 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14322 			    rc);
14323 
14324 	if (bnxt_alloc_fw_health(bp)) {
14325 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14326 	} else {
14327 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14328 		if (rc)
14329 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14330 				    rc);
14331 	}
14332 
14333 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14334 	if (rc)
14335 		return -ENODEV;
14336 
14337 	rc = bnxt_alloc_crash_dump_mem(bp);
14338 	if (rc)
14339 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14340 			    rc);
14341 	if (!rc) {
14342 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14343 		if (rc) {
14344 			bnxt_free_crash_dump_mem(bp);
14345 			netdev_warn(bp->dev,
14346 				    "hwrm crash dump mem failure rc: %d\n", rc);
14347 		}
14348 	}
14349 
14350 	if (bnxt_fw_pre_resv_vnics(bp))
14351 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14352 
14353 	bnxt_hwrm_func_qcfg(bp);
14354 	bnxt_hwrm_vnic_qcaps(bp);
14355 	bnxt_hwrm_port_led_qcaps(bp);
14356 	bnxt_ethtool_init(bp);
14357 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14358 		__bnxt_hwrm_ptp_qcfg(bp);
14359 	bnxt_dcb_init(bp);
14360 	bnxt_hwmon_init(bp);
14361 	return 0;
14362 }
14363 
14364 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14365 {
14366 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14367 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14368 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14369 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14370 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14371 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14372 		bp->rss_hash_delta = bp->rss_hash_cfg;
14373 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14374 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14375 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14376 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14377 	}
14378 }
14379 
14380 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14381 {
14382 	struct net_device *dev = bp->dev;
14383 
14384 	dev->hw_features &= ~NETIF_F_NTUPLE;
14385 	dev->features &= ~NETIF_F_NTUPLE;
14386 	bp->flags &= ~BNXT_FLAG_RFS;
14387 	if (bnxt_rfs_supported(bp)) {
14388 		dev->hw_features |= NETIF_F_NTUPLE;
14389 		if (bnxt_rfs_capable(bp, false)) {
14390 			bp->flags |= BNXT_FLAG_RFS;
14391 			dev->features |= NETIF_F_NTUPLE;
14392 		}
14393 	}
14394 }
14395 
14396 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14397 {
14398 	struct pci_dev *pdev = bp->pdev;
14399 
14400 	bnxt_set_dflt_rss_hash_type(bp);
14401 	bnxt_set_dflt_rfs(bp);
14402 
14403 	bnxt_get_wol_settings(bp);
14404 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14405 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14406 	else
14407 		device_set_wakeup_capable(&pdev->dev, false);
14408 
14409 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14410 	bnxt_hwrm_coal_params_qcaps(bp);
14411 }
14412 
14413 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14414 
14415 int bnxt_fw_init_one(struct bnxt *bp)
14416 {
14417 	int rc;
14418 
14419 	rc = bnxt_fw_init_one_p1(bp);
14420 	if (rc) {
14421 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14422 		return rc;
14423 	}
14424 	rc = bnxt_fw_init_one_p2(bp);
14425 	if (rc) {
14426 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14427 		return rc;
14428 	}
14429 	rc = bnxt_probe_phy(bp, false);
14430 	if (rc)
14431 		return rc;
14432 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14433 	if (rc)
14434 		return rc;
14435 
14436 	bnxt_fw_init_one_p3(bp);
14437 	return 0;
14438 }
14439 
14440 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14441 {
14442 	struct bnxt_fw_health *fw_health = bp->fw_health;
14443 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14444 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14445 	u32 reg_type, reg_off, delay_msecs;
14446 
14447 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14448 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14449 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14450 	switch (reg_type) {
14451 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14452 		pci_write_config_dword(bp->pdev, reg_off, val);
14453 		break;
14454 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14455 		writel(reg_off & BNXT_GRC_BASE_MASK,
14456 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14457 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14458 		fallthrough;
14459 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14460 		writel(val, bp->bar0 + reg_off);
14461 		break;
14462 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14463 		writel(val, bp->bar1 + reg_off);
14464 		break;
14465 	}
14466 	if (delay_msecs) {
14467 		pci_read_config_dword(bp->pdev, 0, &val);
14468 		msleep(delay_msecs);
14469 	}
14470 }
14471 
14472 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14473 {
14474 	struct hwrm_func_qcfg_output *resp;
14475 	struct hwrm_func_qcfg_input *req;
14476 	bool result = true; /* firmware will enforce if unknown */
14477 
14478 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14479 		return result;
14480 
14481 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14482 		return result;
14483 
14484 	req->fid = cpu_to_le16(0xffff);
14485 	resp = hwrm_req_hold(bp, req);
14486 	if (!hwrm_req_send(bp, req))
14487 		result = !!(le16_to_cpu(resp->flags) &
14488 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14489 	hwrm_req_drop(bp, req);
14490 	return result;
14491 }
14492 
14493 static void bnxt_reset_all(struct bnxt *bp)
14494 {
14495 	struct bnxt_fw_health *fw_health = bp->fw_health;
14496 	int i, rc;
14497 
14498 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14499 		bnxt_fw_reset_via_optee(bp);
14500 		bp->fw_reset_timestamp = jiffies;
14501 		return;
14502 	}
14503 
14504 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14505 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14506 			bnxt_fw_reset_writel(bp, i);
14507 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14508 		struct hwrm_fw_reset_input *req;
14509 
14510 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14511 		if (!rc) {
14512 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14513 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14514 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14515 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14516 			rc = hwrm_req_send(bp, req);
14517 		}
14518 		if (rc != -ENODEV)
14519 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14520 	}
14521 	bp->fw_reset_timestamp = jiffies;
14522 }
14523 
14524 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14525 {
14526 	return time_after(jiffies, bp->fw_reset_timestamp +
14527 			  (bp->fw_reset_max_dsecs * HZ / 10));
14528 }
14529 
14530 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14531 {
14532 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14533 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14534 		bnxt_dl_health_fw_status_update(bp, false);
14535 	bp->fw_reset_state = 0;
14536 	dev_close(bp->dev);
14537 }
14538 
14539 static void bnxt_fw_reset_task(struct work_struct *work)
14540 {
14541 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14542 	int rc = 0;
14543 
14544 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14545 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14546 		return;
14547 	}
14548 
14549 	switch (bp->fw_reset_state) {
14550 	case BNXT_FW_RESET_STATE_POLL_VF: {
14551 		int n = bnxt_get_registered_vfs(bp);
14552 		int tmo;
14553 
14554 		if (n < 0) {
14555 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14556 				   n, jiffies_to_msecs(jiffies -
14557 				   bp->fw_reset_timestamp));
14558 			goto fw_reset_abort;
14559 		} else if (n > 0) {
14560 			if (bnxt_fw_reset_timeout(bp)) {
14561 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14562 				bp->fw_reset_state = 0;
14563 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14564 					   n);
14565 				goto ulp_start;
14566 			}
14567 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14568 			return;
14569 		}
14570 		bp->fw_reset_timestamp = jiffies;
14571 		rtnl_lock();
14572 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14573 			bnxt_fw_reset_abort(bp, rc);
14574 			rtnl_unlock();
14575 			goto ulp_start;
14576 		}
14577 		bnxt_fw_reset_close(bp);
14578 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14579 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14580 			tmo = HZ / 10;
14581 		} else {
14582 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14583 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14584 		}
14585 		rtnl_unlock();
14586 		bnxt_queue_fw_reset_work(bp, tmo);
14587 		return;
14588 	}
14589 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
14590 		u32 val;
14591 
14592 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14593 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
14594 		    !bnxt_fw_reset_timeout(bp)) {
14595 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14596 			return;
14597 		}
14598 
14599 		if (!bp->fw_health->primary) {
14600 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
14601 
14602 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14603 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14604 			return;
14605 		}
14606 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14607 	}
14608 		fallthrough;
14609 	case BNXT_FW_RESET_STATE_RESET_FW:
14610 		bnxt_reset_all(bp);
14611 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14612 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
14613 		return;
14614 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
14615 		bnxt_inv_fw_health_reg(bp);
14616 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
14617 		    !bp->fw_reset_min_dsecs) {
14618 			u16 val;
14619 
14620 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14621 			if (val == 0xffff) {
14622 				if (bnxt_fw_reset_timeout(bp)) {
14623 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
14624 					rc = -ETIMEDOUT;
14625 					goto fw_reset_abort;
14626 				}
14627 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
14628 				return;
14629 			}
14630 		}
14631 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14632 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
14633 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
14634 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
14635 			bnxt_dl_remote_reload(bp);
14636 		if (pci_enable_device(bp->pdev)) {
14637 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
14638 			rc = -ENODEV;
14639 			goto fw_reset_abort;
14640 		}
14641 		pci_set_master(bp->pdev);
14642 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
14643 		fallthrough;
14644 	case BNXT_FW_RESET_STATE_POLL_FW:
14645 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
14646 		rc = bnxt_hwrm_poll(bp);
14647 		if (rc) {
14648 			if (bnxt_fw_reset_timeout(bp)) {
14649 				netdev_err(bp->dev, "Firmware reset aborted\n");
14650 				goto fw_reset_abort_status;
14651 			}
14652 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14653 			return;
14654 		}
14655 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
14656 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
14657 		fallthrough;
14658 	case BNXT_FW_RESET_STATE_OPENING:
14659 		while (!rtnl_trylock()) {
14660 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14661 			return;
14662 		}
14663 		rc = bnxt_open(bp->dev);
14664 		if (rc) {
14665 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
14666 			bnxt_fw_reset_abort(bp, rc);
14667 			rtnl_unlock();
14668 			goto ulp_start;
14669 		}
14670 
14671 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
14672 		    bp->fw_health->enabled) {
14673 			bp->fw_health->last_fw_reset_cnt =
14674 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14675 		}
14676 		bp->fw_reset_state = 0;
14677 		/* Make sure fw_reset_state is 0 before clearing the flag */
14678 		smp_mb__before_atomic();
14679 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14680 		bnxt_ptp_reapply_pps(bp);
14681 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
14682 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
14683 			bnxt_dl_health_fw_recovery_done(bp);
14684 			bnxt_dl_health_fw_status_update(bp, true);
14685 		}
14686 		rtnl_unlock();
14687 		bnxt_ulp_start(bp, 0);
14688 		bnxt_reenable_sriov(bp);
14689 		rtnl_lock();
14690 		bnxt_vf_reps_alloc(bp);
14691 		bnxt_vf_reps_open(bp);
14692 		rtnl_unlock();
14693 		break;
14694 	}
14695 	return;
14696 
14697 fw_reset_abort_status:
14698 	if (bp->fw_health->status_reliable ||
14699 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
14700 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14701 
14702 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
14703 	}
14704 fw_reset_abort:
14705 	rtnl_lock();
14706 	bnxt_fw_reset_abort(bp, rc);
14707 	rtnl_unlock();
14708 ulp_start:
14709 	bnxt_ulp_start(bp, rc);
14710 }
14711 
14712 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
14713 {
14714 	int rc;
14715 	struct bnxt *bp = netdev_priv(dev);
14716 
14717 	SET_NETDEV_DEV(dev, &pdev->dev);
14718 
14719 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
14720 	rc = pci_enable_device(pdev);
14721 	if (rc) {
14722 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
14723 		goto init_err;
14724 	}
14725 
14726 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
14727 		dev_err(&pdev->dev,
14728 			"Cannot find PCI device base address, aborting\n");
14729 		rc = -ENODEV;
14730 		goto init_err_disable;
14731 	}
14732 
14733 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
14734 	if (rc) {
14735 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
14736 		goto init_err_disable;
14737 	}
14738 
14739 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
14740 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
14741 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
14742 		rc = -EIO;
14743 		goto init_err_release;
14744 	}
14745 
14746 	pci_set_master(pdev);
14747 
14748 	bp->dev = dev;
14749 	bp->pdev = pdev;
14750 
14751 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
14752 	 * determines the BAR size.
14753 	 */
14754 	bp->bar0 = pci_ioremap_bar(pdev, 0);
14755 	if (!bp->bar0) {
14756 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
14757 		rc = -ENOMEM;
14758 		goto init_err_release;
14759 	}
14760 
14761 	bp->bar2 = pci_ioremap_bar(pdev, 4);
14762 	if (!bp->bar2) {
14763 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
14764 		rc = -ENOMEM;
14765 		goto init_err_release;
14766 	}
14767 
14768 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
14769 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
14770 
14771 	spin_lock_init(&bp->ntp_fltr_lock);
14772 #if BITS_PER_LONG == 32
14773 	spin_lock_init(&bp->db_lock);
14774 #endif
14775 
14776 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
14777 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
14778 
14779 	timer_setup(&bp->timer, bnxt_timer, 0);
14780 	bp->current_interval = BNXT_TIMER_INTERVAL;
14781 
14782 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
14783 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
14784 
14785 	clear_bit(BNXT_STATE_OPEN, &bp->state);
14786 	return 0;
14787 
14788 init_err_release:
14789 	bnxt_unmap_bars(bp, pdev);
14790 	pci_release_regions(pdev);
14791 
14792 init_err_disable:
14793 	pci_disable_device(pdev);
14794 
14795 init_err:
14796 	return rc;
14797 }
14798 
14799 /* rtnl_lock held */
14800 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
14801 {
14802 	struct sockaddr *addr = p;
14803 	struct bnxt *bp = netdev_priv(dev);
14804 	int rc = 0;
14805 
14806 	if (!is_valid_ether_addr(addr->sa_data))
14807 		return -EADDRNOTAVAIL;
14808 
14809 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
14810 		return 0;
14811 
14812 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
14813 	if (rc)
14814 		return rc;
14815 
14816 	eth_hw_addr_set(dev, addr->sa_data);
14817 	bnxt_clear_usr_fltrs(bp, true);
14818 	if (netif_running(dev)) {
14819 		bnxt_close_nic(bp, false, false);
14820 		rc = bnxt_open_nic(bp, false, false);
14821 	}
14822 
14823 	return rc;
14824 }
14825 
14826 /* rtnl_lock held */
14827 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
14828 {
14829 	struct bnxt *bp = netdev_priv(dev);
14830 
14831 	if (netif_running(dev))
14832 		bnxt_close_nic(bp, true, false);
14833 
14834 	WRITE_ONCE(dev->mtu, new_mtu);
14835 
14836 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
14837 	 * program is attached.  We need to set the AGG rings settings and
14838 	 * rx_skb_func accordingly.
14839 	 */
14840 	if (READ_ONCE(bp->xdp_prog))
14841 		bnxt_set_rx_skb_mode(bp, true);
14842 
14843 	bnxt_set_ring_params(bp);
14844 
14845 	if (netif_running(dev))
14846 		return bnxt_open_nic(bp, true, false);
14847 
14848 	return 0;
14849 }
14850 
14851 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
14852 {
14853 	struct bnxt *bp = netdev_priv(dev);
14854 	bool sh = false;
14855 	int rc, tx_cp;
14856 
14857 	if (tc > bp->max_tc) {
14858 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
14859 			   tc, bp->max_tc);
14860 		return -EINVAL;
14861 	}
14862 
14863 	if (bp->num_tc == tc)
14864 		return 0;
14865 
14866 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
14867 		sh = true;
14868 
14869 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
14870 			      sh, tc, bp->tx_nr_rings_xdp);
14871 	if (rc)
14872 		return rc;
14873 
14874 	/* Needs to close the device and do hw resource re-allocations */
14875 	if (netif_running(bp->dev))
14876 		bnxt_close_nic(bp, true, false);
14877 
14878 	if (tc) {
14879 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
14880 		netdev_set_num_tc(dev, tc);
14881 		bp->num_tc = tc;
14882 	} else {
14883 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
14884 		netdev_reset_tc(dev);
14885 		bp->num_tc = 0;
14886 	}
14887 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
14888 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
14889 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
14890 			       tx_cp + bp->rx_nr_rings;
14891 
14892 	if (netif_running(bp->dev))
14893 		return bnxt_open_nic(bp, true, false);
14894 
14895 	return 0;
14896 }
14897 
14898 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
14899 				  void *cb_priv)
14900 {
14901 	struct bnxt *bp = cb_priv;
14902 
14903 	if (!bnxt_tc_flower_enabled(bp) ||
14904 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
14905 		return -EOPNOTSUPP;
14906 
14907 	switch (type) {
14908 	case TC_SETUP_CLSFLOWER:
14909 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
14910 	default:
14911 		return -EOPNOTSUPP;
14912 	}
14913 }
14914 
14915 LIST_HEAD(bnxt_block_cb_list);
14916 
14917 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
14918 			 void *type_data)
14919 {
14920 	struct bnxt *bp = netdev_priv(dev);
14921 
14922 	switch (type) {
14923 	case TC_SETUP_BLOCK:
14924 		return flow_block_cb_setup_simple(type_data,
14925 						  &bnxt_block_cb_list,
14926 						  bnxt_setup_tc_block_cb,
14927 						  bp, bp, true);
14928 	case TC_SETUP_QDISC_MQPRIO: {
14929 		struct tc_mqprio_qopt *mqprio = type_data;
14930 
14931 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
14932 
14933 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
14934 	}
14935 	default:
14936 		return -EOPNOTSUPP;
14937 	}
14938 }
14939 
14940 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
14941 			    const struct sk_buff *skb)
14942 {
14943 	struct bnxt_vnic_info *vnic;
14944 
14945 	if (skb)
14946 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
14947 
14948 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
14949 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
14950 }
14951 
14952 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
14953 			   u32 idx)
14954 {
14955 	struct hlist_head *head;
14956 	int bit_id;
14957 
14958 	spin_lock_bh(&bp->ntp_fltr_lock);
14959 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
14960 	if (bit_id < 0) {
14961 		spin_unlock_bh(&bp->ntp_fltr_lock);
14962 		return -ENOMEM;
14963 	}
14964 
14965 	fltr->base.sw_id = (u16)bit_id;
14966 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
14967 	fltr->base.flags |= BNXT_ACT_RING_DST;
14968 	head = &bp->ntp_fltr_hash_tbl[idx];
14969 	hlist_add_head_rcu(&fltr->base.hash, head);
14970 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
14971 	bnxt_insert_usr_fltr(bp, &fltr->base);
14972 	bp->ntp_fltr_count++;
14973 	spin_unlock_bh(&bp->ntp_fltr_lock);
14974 	return 0;
14975 }
14976 
14977 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
14978 			    struct bnxt_ntuple_filter *f2)
14979 {
14980 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
14981 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
14982 	struct flow_keys *keys1 = &f1->fkeys;
14983 	struct flow_keys *keys2 = &f2->fkeys;
14984 
14985 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
14986 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
14987 		return false;
14988 
14989 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
14990 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
14991 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
14992 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
14993 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
14994 			return false;
14995 	} else {
14996 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
14997 				     &keys2->addrs.v6addrs.src) ||
14998 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
14999 				     &masks2->addrs.v6addrs.src) ||
15000 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15001 				     &keys2->addrs.v6addrs.dst) ||
15002 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15003 				     &masks2->addrs.v6addrs.dst))
15004 			return false;
15005 	}
15006 
15007 	return keys1->ports.src == keys2->ports.src &&
15008 	       masks1->ports.src == masks2->ports.src &&
15009 	       keys1->ports.dst == keys2->ports.dst &&
15010 	       masks1->ports.dst == masks2->ports.dst &&
15011 	       keys1->control.flags == keys2->control.flags &&
15012 	       f1->l2_fltr == f2->l2_fltr;
15013 }
15014 
15015 struct bnxt_ntuple_filter *
15016 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15017 				struct bnxt_ntuple_filter *fltr, u32 idx)
15018 {
15019 	struct bnxt_ntuple_filter *f;
15020 	struct hlist_head *head;
15021 
15022 	head = &bp->ntp_fltr_hash_tbl[idx];
15023 	hlist_for_each_entry_rcu(f, head, base.hash) {
15024 		if (bnxt_fltr_match(f, fltr))
15025 			return f;
15026 	}
15027 	return NULL;
15028 }
15029 
15030 #ifdef CONFIG_RFS_ACCEL
15031 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15032 			      u16 rxq_index, u32 flow_id)
15033 {
15034 	struct bnxt *bp = netdev_priv(dev);
15035 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15036 	struct flow_keys *fkeys;
15037 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15038 	struct bnxt_l2_filter *l2_fltr;
15039 	int rc = 0, idx;
15040 	u32 flags;
15041 
15042 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15043 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15044 		atomic_inc(&l2_fltr->refcnt);
15045 	} else {
15046 		struct bnxt_l2_key key;
15047 
15048 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15049 		key.vlan = 0;
15050 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15051 		if (!l2_fltr)
15052 			return -EINVAL;
15053 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15054 			bnxt_del_l2_filter(bp, l2_fltr);
15055 			return -EINVAL;
15056 		}
15057 	}
15058 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
15059 	if (!new_fltr) {
15060 		bnxt_del_l2_filter(bp, l2_fltr);
15061 		return -ENOMEM;
15062 	}
15063 
15064 	fkeys = &new_fltr->fkeys;
15065 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15066 		rc = -EPROTONOSUPPORT;
15067 		goto err_free;
15068 	}
15069 
15070 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15071 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15072 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15073 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15074 		rc = -EPROTONOSUPPORT;
15075 		goto err_free;
15076 	}
15077 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15078 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15079 		if (bp->hwrm_spec_code < 0x10601) {
15080 			rc = -EPROTONOSUPPORT;
15081 			goto err_free;
15082 		}
15083 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15084 	}
15085 	flags = fkeys->control.flags;
15086 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15087 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15088 		rc = -EPROTONOSUPPORT;
15089 		goto err_free;
15090 	}
15091 	new_fltr->l2_fltr = l2_fltr;
15092 
15093 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15094 	rcu_read_lock();
15095 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15096 	if (fltr) {
15097 		rc = fltr->base.sw_id;
15098 		rcu_read_unlock();
15099 		goto err_free;
15100 	}
15101 	rcu_read_unlock();
15102 
15103 	new_fltr->flow_id = flow_id;
15104 	new_fltr->base.rxq = rxq_index;
15105 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15106 	if (!rc) {
15107 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15108 		return new_fltr->base.sw_id;
15109 	}
15110 
15111 err_free:
15112 	bnxt_del_l2_filter(bp, l2_fltr);
15113 	kfree(new_fltr);
15114 	return rc;
15115 }
15116 #endif
15117 
15118 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15119 {
15120 	spin_lock_bh(&bp->ntp_fltr_lock);
15121 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15122 		spin_unlock_bh(&bp->ntp_fltr_lock);
15123 		return;
15124 	}
15125 	hlist_del_rcu(&fltr->base.hash);
15126 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15127 	bp->ntp_fltr_count--;
15128 	spin_unlock_bh(&bp->ntp_fltr_lock);
15129 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15130 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15131 	kfree_rcu(fltr, base.rcu);
15132 }
15133 
15134 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15135 {
15136 #ifdef CONFIG_RFS_ACCEL
15137 	int i;
15138 
15139 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15140 		struct hlist_head *head;
15141 		struct hlist_node *tmp;
15142 		struct bnxt_ntuple_filter *fltr;
15143 		int rc;
15144 
15145 		head = &bp->ntp_fltr_hash_tbl[i];
15146 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15147 			bool del = false;
15148 
15149 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15150 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15151 					continue;
15152 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15153 							fltr->flow_id,
15154 							fltr->base.sw_id)) {
15155 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15156 									 fltr);
15157 					del = true;
15158 				}
15159 			} else {
15160 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15161 								       fltr);
15162 				if (rc)
15163 					del = true;
15164 				else
15165 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15166 			}
15167 
15168 			if (del)
15169 				bnxt_del_ntp_filter(bp, fltr);
15170 		}
15171 	}
15172 #endif
15173 }
15174 
15175 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15176 				    unsigned int entry, struct udp_tunnel_info *ti)
15177 {
15178 	struct bnxt *bp = netdev_priv(netdev);
15179 	unsigned int cmd;
15180 
15181 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15182 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15183 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15184 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15185 	else
15186 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15187 
15188 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15189 }
15190 
15191 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15192 				      unsigned int entry, struct udp_tunnel_info *ti)
15193 {
15194 	struct bnxt *bp = netdev_priv(netdev);
15195 	unsigned int cmd;
15196 
15197 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15198 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15199 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15200 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15201 	else
15202 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15203 
15204 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15205 }
15206 
15207 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15208 	.set_port	= bnxt_udp_tunnel_set_port,
15209 	.unset_port	= bnxt_udp_tunnel_unset_port,
15210 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15211 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15212 	.tables		= {
15213 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15214 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15215 	},
15216 }, bnxt_udp_tunnels_p7 = {
15217 	.set_port	= bnxt_udp_tunnel_set_port,
15218 	.unset_port	= bnxt_udp_tunnel_unset_port,
15219 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15220 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15221 	.tables		= {
15222 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15223 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15224 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15225 	},
15226 };
15227 
15228 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15229 			       struct net_device *dev, u32 filter_mask,
15230 			       int nlflags)
15231 {
15232 	struct bnxt *bp = netdev_priv(dev);
15233 
15234 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15235 				       nlflags, filter_mask, NULL);
15236 }
15237 
15238 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15239 			       u16 flags, struct netlink_ext_ack *extack)
15240 {
15241 	struct bnxt *bp = netdev_priv(dev);
15242 	struct nlattr *attr, *br_spec;
15243 	int rem, rc = 0;
15244 
15245 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15246 		return -EOPNOTSUPP;
15247 
15248 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15249 	if (!br_spec)
15250 		return -EINVAL;
15251 
15252 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15253 		u16 mode;
15254 
15255 		mode = nla_get_u16(attr);
15256 		if (mode == bp->br_mode)
15257 			break;
15258 
15259 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15260 		if (!rc)
15261 			bp->br_mode = mode;
15262 		break;
15263 	}
15264 	return rc;
15265 }
15266 
15267 int bnxt_get_port_parent_id(struct net_device *dev,
15268 			    struct netdev_phys_item_id *ppid)
15269 {
15270 	struct bnxt *bp = netdev_priv(dev);
15271 
15272 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15273 		return -EOPNOTSUPP;
15274 
15275 	/* The PF and it's VF-reps only support the switchdev framework */
15276 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15277 		return -EOPNOTSUPP;
15278 
15279 	ppid->id_len = sizeof(bp->dsn);
15280 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15281 
15282 	return 0;
15283 }
15284 
15285 static const struct net_device_ops bnxt_netdev_ops = {
15286 	.ndo_open		= bnxt_open,
15287 	.ndo_start_xmit		= bnxt_start_xmit,
15288 	.ndo_stop		= bnxt_close,
15289 	.ndo_get_stats64	= bnxt_get_stats64,
15290 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15291 	.ndo_eth_ioctl		= bnxt_ioctl,
15292 	.ndo_validate_addr	= eth_validate_addr,
15293 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15294 	.ndo_change_mtu		= bnxt_change_mtu,
15295 	.ndo_fix_features	= bnxt_fix_features,
15296 	.ndo_set_features	= bnxt_set_features,
15297 	.ndo_features_check	= bnxt_features_check,
15298 	.ndo_tx_timeout		= bnxt_tx_timeout,
15299 #ifdef CONFIG_BNXT_SRIOV
15300 	.ndo_get_vf_config	= bnxt_get_vf_config,
15301 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15302 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15303 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15304 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15305 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15306 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15307 #endif
15308 	.ndo_setup_tc           = bnxt_setup_tc,
15309 #ifdef CONFIG_RFS_ACCEL
15310 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15311 #endif
15312 	.ndo_bpf		= bnxt_xdp,
15313 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15314 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15315 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15316 };
15317 
15318 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15319 				    struct netdev_queue_stats_rx *stats)
15320 {
15321 	struct bnxt *bp = netdev_priv(dev);
15322 	struct bnxt_cp_ring_info *cpr;
15323 	u64 *sw;
15324 
15325 	cpr = &bp->bnapi[i]->cp_ring;
15326 	sw = cpr->stats.sw_stats;
15327 
15328 	stats->packets = 0;
15329 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15330 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15331 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15332 
15333 	stats->bytes = 0;
15334 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15335 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15336 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15337 
15338 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15339 }
15340 
15341 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15342 				    struct netdev_queue_stats_tx *stats)
15343 {
15344 	struct bnxt *bp = netdev_priv(dev);
15345 	struct bnxt_napi *bnapi;
15346 	u64 *sw;
15347 
15348 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15349 	sw = bnapi->cp_ring.stats.sw_stats;
15350 
15351 	stats->packets = 0;
15352 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15353 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15354 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15355 
15356 	stats->bytes = 0;
15357 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15358 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15359 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15360 }
15361 
15362 static void bnxt_get_base_stats(struct net_device *dev,
15363 				struct netdev_queue_stats_rx *rx,
15364 				struct netdev_queue_stats_tx *tx)
15365 {
15366 	struct bnxt *bp = netdev_priv(dev);
15367 
15368 	rx->packets = bp->net_stats_prev.rx_packets;
15369 	rx->bytes = bp->net_stats_prev.rx_bytes;
15370 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15371 
15372 	tx->packets = bp->net_stats_prev.tx_packets;
15373 	tx->bytes = bp->net_stats_prev.tx_bytes;
15374 }
15375 
15376 static const struct netdev_stat_ops bnxt_stat_ops = {
15377 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15378 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15379 	.get_base_stats		= bnxt_get_base_stats,
15380 };
15381 
15382 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15383 {
15384 	struct bnxt_rx_ring_info *rxr, *clone;
15385 	struct bnxt *bp = netdev_priv(dev);
15386 	struct bnxt_ring_struct *ring;
15387 	int rc;
15388 
15389 	rxr = &bp->rx_ring[idx];
15390 	clone = qmem;
15391 	memcpy(clone, rxr, sizeof(*rxr));
15392 	bnxt_init_rx_ring_struct(bp, clone);
15393 	bnxt_reset_rx_ring_struct(bp, clone);
15394 
15395 	clone->rx_prod = 0;
15396 	clone->rx_agg_prod = 0;
15397 	clone->rx_sw_agg_prod = 0;
15398 	clone->rx_next_cons = 0;
15399 
15400 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15401 	if (rc)
15402 		return rc;
15403 
15404 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15405 	if (rc < 0)
15406 		goto err_page_pool_destroy;
15407 
15408 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15409 					MEM_TYPE_PAGE_POOL,
15410 					clone->page_pool);
15411 	if (rc)
15412 		goto err_rxq_info_unreg;
15413 
15414 	ring = &clone->rx_ring_struct;
15415 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15416 	if (rc)
15417 		goto err_free_rx_ring;
15418 
15419 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15420 		ring = &clone->rx_agg_ring_struct;
15421 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15422 		if (rc)
15423 			goto err_free_rx_agg_ring;
15424 
15425 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15426 		if (rc)
15427 			goto err_free_rx_agg_ring;
15428 	}
15429 
15430 	if (bp->flags & BNXT_FLAG_TPA) {
15431 		rc = bnxt_alloc_one_tpa_info(bp, clone);
15432 		if (rc)
15433 			goto err_free_tpa_info;
15434 	}
15435 
15436 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15437 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15438 
15439 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15440 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15441 		bnxt_alloc_one_rx_ring_page(bp, clone, idx);
15442 	if (bp->flags & BNXT_FLAG_TPA)
15443 		bnxt_alloc_one_tpa_info_data(bp, clone);
15444 
15445 	return 0;
15446 
15447 err_free_tpa_info:
15448 	bnxt_free_one_tpa_info(bp, clone);
15449 err_free_rx_agg_ring:
15450 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15451 err_free_rx_ring:
15452 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15453 err_rxq_info_unreg:
15454 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15455 err_page_pool_destroy:
15456 	page_pool_destroy(clone->page_pool);
15457 	if (bnxt_separate_head_pool())
15458 		page_pool_destroy(clone->head_pool);
15459 	clone->page_pool = NULL;
15460 	clone->head_pool = NULL;
15461 	return rc;
15462 }
15463 
15464 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15465 {
15466 	struct bnxt_rx_ring_info *rxr = qmem;
15467 	struct bnxt *bp = netdev_priv(dev);
15468 	struct bnxt_ring_struct *ring;
15469 
15470 	bnxt_free_one_rx_ring_skbs(bp, rxr);
15471 
15472 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15473 
15474 	page_pool_destroy(rxr->page_pool);
15475 	if (bnxt_separate_head_pool())
15476 		page_pool_destroy(rxr->head_pool);
15477 	rxr->page_pool = NULL;
15478 	rxr->head_pool = NULL;
15479 
15480 	ring = &rxr->rx_ring_struct;
15481 	bnxt_free_ring(bp, &ring->ring_mem);
15482 
15483 	ring = &rxr->rx_agg_ring_struct;
15484 	bnxt_free_ring(bp, &ring->ring_mem);
15485 
15486 	kfree(rxr->rx_agg_bmap);
15487 	rxr->rx_agg_bmap = NULL;
15488 }
15489 
15490 static void bnxt_copy_rx_ring(struct bnxt *bp,
15491 			      struct bnxt_rx_ring_info *dst,
15492 			      struct bnxt_rx_ring_info *src)
15493 {
15494 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15495 	struct bnxt_ring_struct *dst_ring, *src_ring;
15496 	int i;
15497 
15498 	dst_ring = &dst->rx_ring_struct;
15499 	dst_rmem = &dst_ring->ring_mem;
15500 	src_ring = &src->rx_ring_struct;
15501 	src_rmem = &src_ring->ring_mem;
15502 
15503 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15504 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15505 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15506 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15507 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15508 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15509 
15510 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15511 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15512 	*dst_rmem->vmem = *src_rmem->vmem;
15513 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15514 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15515 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15516 	}
15517 
15518 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15519 		return;
15520 
15521 	dst_ring = &dst->rx_agg_ring_struct;
15522 	dst_rmem = &dst_ring->ring_mem;
15523 	src_ring = &src->rx_agg_ring_struct;
15524 	src_rmem = &src_ring->ring_mem;
15525 
15526 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15527 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15528 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15529 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15530 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15531 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15532 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15533 
15534 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15535 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15536 	*dst_rmem->vmem = *src_rmem->vmem;
15537 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15538 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15539 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15540 	}
15541 
15542 	dst->rx_agg_bmap = src->rx_agg_bmap;
15543 }
15544 
15545 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15546 {
15547 	struct bnxt *bp = netdev_priv(dev);
15548 	struct bnxt_rx_ring_info *rxr, *clone;
15549 	struct bnxt_cp_ring_info *cpr;
15550 	struct bnxt_vnic_info *vnic;
15551 	int i, rc;
15552 
15553 	rxr = &bp->rx_ring[idx];
15554 	clone = qmem;
15555 
15556 	rxr->rx_prod = clone->rx_prod;
15557 	rxr->rx_agg_prod = clone->rx_agg_prod;
15558 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
15559 	rxr->rx_next_cons = clone->rx_next_cons;
15560 	rxr->rx_tpa = clone->rx_tpa;
15561 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
15562 	rxr->page_pool = clone->page_pool;
15563 	rxr->head_pool = clone->head_pool;
15564 	rxr->xdp_rxq = clone->xdp_rxq;
15565 
15566 	bnxt_copy_rx_ring(bp, rxr, clone);
15567 
15568 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
15569 	if (rc)
15570 		return rc;
15571 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
15572 	if (rc)
15573 		goto err_free_hwrm_rx_ring;
15574 
15575 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
15576 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15577 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
15578 
15579 	cpr = &rxr->bnapi->cp_ring;
15580 	cpr->sw_stats->rx.rx_resets++;
15581 
15582 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15583 		vnic = &bp->vnic_info[i];
15584 
15585 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
15586 		if (rc) {
15587 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
15588 				   vnic->vnic_id, rc);
15589 			return rc;
15590 		}
15591 		vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
15592 		bnxt_hwrm_vnic_update(bp, vnic,
15593 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15594 	}
15595 
15596 	return 0;
15597 
15598 err_free_hwrm_rx_ring:
15599 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15600 	return rc;
15601 }
15602 
15603 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
15604 {
15605 	struct bnxt *bp = netdev_priv(dev);
15606 	struct bnxt_rx_ring_info *rxr;
15607 	struct bnxt_vnic_info *vnic;
15608 	int i;
15609 
15610 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15611 		vnic = &bp->vnic_info[i];
15612 		vnic->mru = 0;
15613 		bnxt_hwrm_vnic_update(bp, vnic,
15614 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15615 	}
15616 
15617 	rxr = &bp->rx_ring[idx];
15618 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15619 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
15620 	rxr->rx_next_cons = 0;
15621 	page_pool_disable_direct_recycling(rxr->page_pool);
15622 	if (bnxt_separate_head_pool())
15623 		page_pool_disable_direct_recycling(rxr->head_pool);
15624 
15625 	memcpy(qmem, rxr, sizeof(*rxr));
15626 	bnxt_init_rx_ring_struct(bp, qmem);
15627 
15628 	return 0;
15629 }
15630 
15631 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
15632 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
15633 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
15634 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
15635 	.ndo_queue_start	= bnxt_queue_start,
15636 	.ndo_queue_stop		= bnxt_queue_stop,
15637 };
15638 
15639 static void bnxt_remove_one(struct pci_dev *pdev)
15640 {
15641 	struct net_device *dev = pci_get_drvdata(pdev);
15642 	struct bnxt *bp = netdev_priv(dev);
15643 
15644 	if (BNXT_PF(bp))
15645 		bnxt_sriov_disable(bp);
15646 
15647 	bnxt_rdma_aux_device_del(bp);
15648 
15649 	bnxt_ptp_clear(bp);
15650 	unregister_netdev(dev);
15651 
15652 	bnxt_rdma_aux_device_uninit(bp);
15653 
15654 	bnxt_free_l2_filters(bp, true);
15655 	bnxt_free_ntp_fltrs(bp, true);
15656 	WARN_ON(bp->num_rss_ctx);
15657 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15658 	/* Flush any pending tasks */
15659 	cancel_work_sync(&bp->sp_task);
15660 	cancel_delayed_work_sync(&bp->fw_reset_task);
15661 	bp->sp_event = 0;
15662 
15663 	bnxt_dl_fw_reporters_destroy(bp);
15664 	bnxt_dl_unregister(bp);
15665 	bnxt_shutdown_tc(bp);
15666 
15667 	bnxt_clear_int_mode(bp);
15668 	bnxt_hwrm_func_drv_unrgtr(bp);
15669 	bnxt_free_hwrm_resources(bp);
15670 	bnxt_hwmon_uninit(bp);
15671 	bnxt_ethtool_free(bp);
15672 	bnxt_dcb_free(bp);
15673 	kfree(bp->ptp_cfg);
15674 	bp->ptp_cfg = NULL;
15675 	kfree(bp->fw_health);
15676 	bp->fw_health = NULL;
15677 	bnxt_cleanup_pci(bp);
15678 	bnxt_free_ctx_mem(bp, true);
15679 	bnxt_free_crash_dump_mem(bp);
15680 	kfree(bp->rss_indir_tbl);
15681 	bp->rss_indir_tbl = NULL;
15682 	bnxt_free_port_stats(bp);
15683 	free_netdev(dev);
15684 }
15685 
15686 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
15687 {
15688 	int rc = 0;
15689 	struct bnxt_link_info *link_info = &bp->link_info;
15690 
15691 	bp->phy_flags = 0;
15692 	rc = bnxt_hwrm_phy_qcaps(bp);
15693 	if (rc) {
15694 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
15695 			   rc);
15696 		return rc;
15697 	}
15698 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
15699 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
15700 	else
15701 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
15702 
15703 	bp->mac_flags = 0;
15704 	bnxt_hwrm_mac_qcaps(bp);
15705 
15706 	if (!fw_dflt)
15707 		return 0;
15708 
15709 	mutex_lock(&bp->link_lock);
15710 	rc = bnxt_update_link(bp, false);
15711 	if (rc) {
15712 		mutex_unlock(&bp->link_lock);
15713 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
15714 			   rc);
15715 		return rc;
15716 	}
15717 
15718 	/* Older firmware does not have supported_auto_speeds, so assume
15719 	 * that all supported speeds can be autonegotiated.
15720 	 */
15721 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
15722 		link_info->support_auto_speeds = link_info->support_speeds;
15723 
15724 	bnxt_init_ethtool_link_settings(bp);
15725 	mutex_unlock(&bp->link_lock);
15726 	return 0;
15727 }
15728 
15729 static int bnxt_get_max_irq(struct pci_dev *pdev)
15730 {
15731 	u16 ctrl;
15732 
15733 	if (!pdev->msix_cap)
15734 		return 1;
15735 
15736 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
15737 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
15738 }
15739 
15740 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15741 				int *max_cp)
15742 {
15743 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
15744 	int max_ring_grps = 0, max_irq;
15745 
15746 	*max_tx = hw_resc->max_tx_rings;
15747 	*max_rx = hw_resc->max_rx_rings;
15748 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
15749 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
15750 			bnxt_get_ulp_msix_num_in_use(bp),
15751 			hw_resc->max_stat_ctxs -
15752 			bnxt_get_ulp_stat_ctxs_in_use(bp));
15753 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
15754 		*max_cp = min_t(int, *max_cp, max_irq);
15755 	max_ring_grps = hw_resc->max_hw_ring_grps;
15756 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
15757 		*max_cp -= 1;
15758 		*max_rx -= 2;
15759 	}
15760 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15761 		*max_rx >>= 1;
15762 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
15763 		int rc;
15764 
15765 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
15766 		if (rc) {
15767 			*max_rx = 0;
15768 			*max_tx = 0;
15769 		}
15770 		/* On P5 chips, max_cp output param should be available NQs */
15771 		*max_cp = max_irq;
15772 	}
15773 	*max_rx = min_t(int, *max_rx, max_ring_grps);
15774 }
15775 
15776 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
15777 {
15778 	int rx, tx, cp;
15779 
15780 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
15781 	*max_rx = rx;
15782 	*max_tx = tx;
15783 	if (!rx || !tx || !cp)
15784 		return -ENOMEM;
15785 
15786 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
15787 }
15788 
15789 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15790 			       bool shared)
15791 {
15792 	int rc;
15793 
15794 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15795 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
15796 		/* Not enough rings, try disabling agg rings. */
15797 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
15798 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15799 		if (rc) {
15800 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
15801 			bp->flags |= BNXT_FLAG_AGG_RINGS;
15802 			return rc;
15803 		}
15804 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
15805 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15806 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15807 		bnxt_set_ring_params(bp);
15808 	}
15809 
15810 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
15811 		int max_cp, max_stat, max_irq;
15812 
15813 		/* Reserve minimum resources for RoCE */
15814 		max_cp = bnxt_get_max_func_cp_rings(bp);
15815 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
15816 		max_irq = bnxt_get_max_func_irqs(bp);
15817 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
15818 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
15819 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
15820 			return 0;
15821 
15822 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
15823 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
15824 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
15825 		max_cp = min_t(int, max_cp, max_irq);
15826 		max_cp = min_t(int, max_cp, max_stat);
15827 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
15828 		if (rc)
15829 			rc = 0;
15830 	}
15831 	return rc;
15832 }
15833 
15834 /* In initial default shared ring setting, each shared ring must have a
15835  * RX/TX ring pair.
15836  */
15837 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
15838 {
15839 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
15840 	bp->rx_nr_rings = bp->cp_nr_rings;
15841 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
15842 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15843 }
15844 
15845 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
15846 {
15847 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
15848 	int avail_msix;
15849 
15850 	if (!bnxt_can_reserve_rings(bp))
15851 		return 0;
15852 
15853 	if (sh)
15854 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
15855 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
15856 	/* Reduce default rings on multi-port cards so that total default
15857 	 * rings do not exceed CPU count.
15858 	 */
15859 	if (bp->port_count > 1) {
15860 		int max_rings =
15861 			max_t(int, num_online_cpus() / bp->port_count, 1);
15862 
15863 		dflt_rings = min_t(int, dflt_rings, max_rings);
15864 	}
15865 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
15866 	if (rc)
15867 		return rc;
15868 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
15869 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
15870 	if (sh)
15871 		bnxt_trim_dflt_sh_rings(bp);
15872 	else
15873 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
15874 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15875 
15876 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
15877 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
15878 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
15879 
15880 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
15881 		bnxt_set_dflt_ulp_stat_ctxs(bp);
15882 	}
15883 
15884 	rc = __bnxt_reserve_rings(bp);
15885 	if (rc && rc != -ENODEV)
15886 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
15887 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15888 	if (sh)
15889 		bnxt_trim_dflt_sh_rings(bp);
15890 
15891 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
15892 	if (bnxt_need_reserve_rings(bp)) {
15893 		rc = __bnxt_reserve_rings(bp);
15894 		if (rc && rc != -ENODEV)
15895 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
15896 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15897 	}
15898 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
15899 		bp->rx_nr_rings++;
15900 		bp->cp_nr_rings++;
15901 	}
15902 	if (rc) {
15903 		bp->tx_nr_rings = 0;
15904 		bp->rx_nr_rings = 0;
15905 	}
15906 	return rc;
15907 }
15908 
15909 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
15910 {
15911 	int rc;
15912 
15913 	if (bp->tx_nr_rings)
15914 		return 0;
15915 
15916 	bnxt_ulp_irq_stop(bp);
15917 	bnxt_clear_int_mode(bp);
15918 	rc = bnxt_set_dflt_rings(bp, true);
15919 	if (rc) {
15920 		if (BNXT_VF(bp) && rc == -ENODEV)
15921 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15922 		else
15923 			netdev_err(bp->dev, "Not enough rings available.\n");
15924 		goto init_dflt_ring_err;
15925 	}
15926 	rc = bnxt_init_int_mode(bp);
15927 	if (rc)
15928 		goto init_dflt_ring_err;
15929 
15930 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15931 
15932 	bnxt_set_dflt_rfs(bp);
15933 
15934 init_dflt_ring_err:
15935 	bnxt_ulp_irq_restart(bp, rc);
15936 	return rc;
15937 }
15938 
15939 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
15940 {
15941 	int rc;
15942 
15943 	ASSERT_RTNL();
15944 	bnxt_hwrm_func_qcaps(bp);
15945 
15946 	if (netif_running(bp->dev))
15947 		__bnxt_close_nic(bp, true, false);
15948 
15949 	bnxt_ulp_irq_stop(bp);
15950 	bnxt_clear_int_mode(bp);
15951 	rc = bnxt_init_int_mode(bp);
15952 	bnxt_ulp_irq_restart(bp, rc);
15953 
15954 	if (netif_running(bp->dev)) {
15955 		if (rc)
15956 			dev_close(bp->dev);
15957 		else
15958 			rc = bnxt_open_nic(bp, true, false);
15959 	}
15960 
15961 	return rc;
15962 }
15963 
15964 static int bnxt_init_mac_addr(struct bnxt *bp)
15965 {
15966 	int rc = 0;
15967 
15968 	if (BNXT_PF(bp)) {
15969 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
15970 	} else {
15971 #ifdef CONFIG_BNXT_SRIOV
15972 		struct bnxt_vf_info *vf = &bp->vf;
15973 		bool strict_approval = true;
15974 
15975 		if (is_valid_ether_addr(vf->mac_addr)) {
15976 			/* overwrite netdev dev_addr with admin VF MAC */
15977 			eth_hw_addr_set(bp->dev, vf->mac_addr);
15978 			/* Older PF driver or firmware may not approve this
15979 			 * correctly.
15980 			 */
15981 			strict_approval = false;
15982 		} else {
15983 			eth_hw_addr_random(bp->dev);
15984 		}
15985 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
15986 #endif
15987 	}
15988 	return rc;
15989 }
15990 
15991 static void bnxt_vpd_read_info(struct bnxt *bp)
15992 {
15993 	struct pci_dev *pdev = bp->pdev;
15994 	unsigned int vpd_size, kw_len;
15995 	int pos, size;
15996 	u8 *vpd_data;
15997 
15998 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
15999 	if (IS_ERR(vpd_data)) {
16000 		pci_warn(pdev, "Unable to read VPD\n");
16001 		return;
16002 	}
16003 
16004 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16005 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16006 	if (pos < 0)
16007 		goto read_sn;
16008 
16009 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16010 	memcpy(bp->board_partno, &vpd_data[pos], size);
16011 
16012 read_sn:
16013 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16014 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16015 					   &kw_len);
16016 	if (pos < 0)
16017 		goto exit;
16018 
16019 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16020 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16021 exit:
16022 	kfree(vpd_data);
16023 }
16024 
16025 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16026 {
16027 	struct pci_dev *pdev = bp->pdev;
16028 	u64 qword;
16029 
16030 	qword = pci_get_dsn(pdev);
16031 	if (!qword) {
16032 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16033 		return -EOPNOTSUPP;
16034 	}
16035 
16036 	put_unaligned_le64(qword, dsn);
16037 
16038 	bp->flags |= BNXT_FLAG_DSN_VALID;
16039 	return 0;
16040 }
16041 
16042 static int bnxt_map_db_bar(struct bnxt *bp)
16043 {
16044 	if (!bp->db_size)
16045 		return -ENODEV;
16046 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16047 	if (!bp->bar1)
16048 		return -ENOMEM;
16049 	return 0;
16050 }
16051 
16052 void bnxt_print_device_info(struct bnxt *bp)
16053 {
16054 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16055 		    board_info[bp->board_idx].name,
16056 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16057 
16058 	pcie_print_link_status(bp->pdev);
16059 }
16060 
16061 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16062 {
16063 	struct bnxt_hw_resc *hw_resc;
16064 	struct net_device *dev;
16065 	struct bnxt *bp;
16066 	int rc, max_irqs;
16067 
16068 	if (pci_is_bridge(pdev))
16069 		return -ENODEV;
16070 
16071 	if (!pdev->msix_cap) {
16072 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16073 		return -ENODEV;
16074 	}
16075 
16076 	/* Clear any pending DMA transactions from crash kernel
16077 	 * while loading driver in capture kernel.
16078 	 */
16079 	if (is_kdump_kernel()) {
16080 		pci_clear_master(pdev);
16081 		pcie_flr(pdev);
16082 	}
16083 
16084 	max_irqs = bnxt_get_max_irq(pdev);
16085 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16086 				 max_irqs);
16087 	if (!dev)
16088 		return -ENOMEM;
16089 
16090 	bp = netdev_priv(dev);
16091 	bp->board_idx = ent->driver_data;
16092 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16093 	bnxt_set_max_func_irqs(bp, max_irqs);
16094 
16095 	if (bnxt_vf_pciid(bp->board_idx))
16096 		bp->flags |= BNXT_FLAG_VF;
16097 
16098 	/* No devlink port registration in case of a VF */
16099 	if (BNXT_PF(bp))
16100 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16101 
16102 	rc = bnxt_init_board(pdev, dev);
16103 	if (rc < 0)
16104 		goto init_err_free;
16105 
16106 	dev->netdev_ops = &bnxt_netdev_ops;
16107 	dev->stat_ops = &bnxt_stat_ops;
16108 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16109 	dev->ethtool_ops = &bnxt_ethtool_ops;
16110 	pci_set_drvdata(pdev, dev);
16111 
16112 	rc = bnxt_alloc_hwrm_resources(bp);
16113 	if (rc)
16114 		goto init_err_pci_clean;
16115 
16116 	mutex_init(&bp->hwrm_cmd_lock);
16117 	mutex_init(&bp->link_lock);
16118 
16119 	rc = bnxt_fw_init_one_p1(bp);
16120 	if (rc)
16121 		goto init_err_pci_clean;
16122 
16123 	if (BNXT_PF(bp))
16124 		bnxt_vpd_read_info(bp);
16125 
16126 	if (BNXT_CHIP_P5_PLUS(bp)) {
16127 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16128 		if (BNXT_CHIP_P7(bp))
16129 			bp->flags |= BNXT_FLAG_CHIP_P7;
16130 	}
16131 
16132 	rc = bnxt_alloc_rss_indir_tbl(bp);
16133 	if (rc)
16134 		goto init_err_pci_clean;
16135 
16136 	rc = bnxt_fw_init_one_p2(bp);
16137 	if (rc)
16138 		goto init_err_pci_clean;
16139 
16140 	rc = bnxt_map_db_bar(bp);
16141 	if (rc) {
16142 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16143 			rc);
16144 		goto init_err_pci_clean;
16145 	}
16146 
16147 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16148 			   NETIF_F_TSO | NETIF_F_TSO6 |
16149 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16150 			   NETIF_F_GSO_IPXIP4 |
16151 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16152 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16153 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16154 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16155 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16156 
16157 	if (BNXT_SUPPORTS_TPA(bp))
16158 		dev->hw_features |= NETIF_F_LRO;
16159 
16160 	dev->hw_enc_features =
16161 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16162 			NETIF_F_TSO | NETIF_F_TSO6 |
16163 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16164 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16165 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16166 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16167 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16168 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16169 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16170 	else
16171 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16172 
16173 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16174 				    NETIF_F_GSO_GRE_CSUM;
16175 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16176 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16177 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16178 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16179 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16180 	if (BNXT_SUPPORTS_TPA(bp))
16181 		dev->hw_features |= NETIF_F_GRO_HW;
16182 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16183 	if (dev->features & NETIF_F_GRO_HW)
16184 		dev->features &= ~NETIF_F_LRO;
16185 	dev->priv_flags |= IFF_UNICAST_FLT;
16186 
16187 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16188 	if (bp->tso_max_segs)
16189 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16190 
16191 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16192 			    NETDEV_XDP_ACT_RX_SG;
16193 
16194 #ifdef CONFIG_BNXT_SRIOV
16195 	init_waitqueue_head(&bp->sriov_cfg_wait);
16196 #endif
16197 	if (BNXT_SUPPORTS_TPA(bp)) {
16198 		bp->gro_func = bnxt_gro_func_5730x;
16199 		if (BNXT_CHIP_P4(bp))
16200 			bp->gro_func = bnxt_gro_func_5731x;
16201 		else if (BNXT_CHIP_P5_PLUS(bp))
16202 			bp->gro_func = bnxt_gro_func_5750x;
16203 	}
16204 	if (!BNXT_CHIP_P4_PLUS(bp))
16205 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16206 
16207 	rc = bnxt_init_mac_addr(bp);
16208 	if (rc) {
16209 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16210 		rc = -EADDRNOTAVAIL;
16211 		goto init_err_pci_clean;
16212 	}
16213 
16214 	if (BNXT_PF(bp)) {
16215 		/* Read the adapter's DSN to use as the eswitch switch_id */
16216 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16217 	}
16218 
16219 	/* MTU range: 60 - FW defined max */
16220 	dev->min_mtu = ETH_ZLEN;
16221 	dev->max_mtu = bp->max_mtu;
16222 
16223 	rc = bnxt_probe_phy(bp, true);
16224 	if (rc)
16225 		goto init_err_pci_clean;
16226 
16227 	hw_resc = &bp->hw_resc;
16228 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16229 		       BNXT_L2_FLTR_MAX_FLTR;
16230 	/* Older firmware may not report these filters properly */
16231 	if (bp->max_fltr < BNXT_MAX_FLTR)
16232 		bp->max_fltr = BNXT_MAX_FLTR;
16233 	bnxt_init_l2_fltr_tbl(bp);
16234 	bnxt_set_rx_skb_mode(bp, false);
16235 	bnxt_set_tpa_flags(bp);
16236 	bnxt_set_ring_params(bp);
16237 	bnxt_rdma_aux_device_init(bp);
16238 	rc = bnxt_set_dflt_rings(bp, true);
16239 	if (rc) {
16240 		if (BNXT_VF(bp) && rc == -ENODEV) {
16241 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16242 		} else {
16243 			netdev_err(bp->dev, "Not enough rings available.\n");
16244 			rc = -ENOMEM;
16245 		}
16246 		goto init_err_pci_clean;
16247 	}
16248 
16249 	bnxt_fw_init_one_p3(bp);
16250 
16251 	bnxt_init_dflt_coal(bp);
16252 
16253 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16254 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16255 
16256 	rc = bnxt_init_int_mode(bp);
16257 	if (rc)
16258 		goto init_err_pci_clean;
16259 
16260 	/* No TC has been set yet and rings may have been trimmed due to
16261 	 * limited MSIX, so we re-initialize the TX rings per TC.
16262 	 */
16263 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16264 
16265 	if (BNXT_PF(bp)) {
16266 		if (!bnxt_pf_wq) {
16267 			bnxt_pf_wq =
16268 				create_singlethread_workqueue("bnxt_pf_wq");
16269 			if (!bnxt_pf_wq) {
16270 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16271 				rc = -ENOMEM;
16272 				goto init_err_pci_clean;
16273 			}
16274 		}
16275 		rc = bnxt_init_tc(bp);
16276 		if (rc)
16277 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16278 				   rc);
16279 	}
16280 
16281 	bnxt_inv_fw_health_reg(bp);
16282 	rc = bnxt_dl_register(bp);
16283 	if (rc)
16284 		goto init_err_dl;
16285 
16286 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16287 
16288 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16289 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16290 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16291 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16292 
16293 	rc = register_netdev(dev);
16294 	if (rc)
16295 		goto init_err_cleanup;
16296 
16297 	bnxt_dl_fw_reporters_create(bp);
16298 
16299 	bnxt_rdma_aux_device_add(bp);
16300 
16301 	bnxt_print_device_info(bp);
16302 
16303 	pci_save_state(pdev);
16304 
16305 	return 0;
16306 init_err_cleanup:
16307 	bnxt_rdma_aux_device_uninit(bp);
16308 	bnxt_dl_unregister(bp);
16309 init_err_dl:
16310 	bnxt_shutdown_tc(bp);
16311 	bnxt_clear_int_mode(bp);
16312 
16313 init_err_pci_clean:
16314 	bnxt_hwrm_func_drv_unrgtr(bp);
16315 	bnxt_free_hwrm_resources(bp);
16316 	bnxt_hwmon_uninit(bp);
16317 	bnxt_ethtool_free(bp);
16318 	bnxt_ptp_clear(bp);
16319 	kfree(bp->ptp_cfg);
16320 	bp->ptp_cfg = NULL;
16321 	kfree(bp->fw_health);
16322 	bp->fw_health = NULL;
16323 	bnxt_cleanup_pci(bp);
16324 	bnxt_free_ctx_mem(bp, true);
16325 	bnxt_free_crash_dump_mem(bp);
16326 	kfree(bp->rss_indir_tbl);
16327 	bp->rss_indir_tbl = NULL;
16328 
16329 init_err_free:
16330 	free_netdev(dev);
16331 	return rc;
16332 }
16333 
16334 static void bnxt_shutdown(struct pci_dev *pdev)
16335 {
16336 	struct net_device *dev = pci_get_drvdata(pdev);
16337 	struct bnxt *bp;
16338 
16339 	if (!dev)
16340 		return;
16341 
16342 	rtnl_lock();
16343 	bp = netdev_priv(dev);
16344 	if (!bp)
16345 		goto shutdown_exit;
16346 
16347 	if (netif_running(dev))
16348 		dev_close(dev);
16349 
16350 	bnxt_ptp_clear(bp);
16351 	bnxt_clear_int_mode(bp);
16352 	pci_disable_device(pdev);
16353 
16354 	if (system_state == SYSTEM_POWER_OFF) {
16355 		pci_wake_from_d3(pdev, bp->wol);
16356 		pci_set_power_state(pdev, PCI_D3hot);
16357 	}
16358 
16359 shutdown_exit:
16360 	rtnl_unlock();
16361 }
16362 
16363 #ifdef CONFIG_PM_SLEEP
16364 static int bnxt_suspend(struct device *device)
16365 {
16366 	struct net_device *dev = dev_get_drvdata(device);
16367 	struct bnxt *bp = netdev_priv(dev);
16368 	int rc = 0;
16369 
16370 	bnxt_ulp_stop(bp);
16371 
16372 	rtnl_lock();
16373 	if (netif_running(dev)) {
16374 		netif_device_detach(dev);
16375 		rc = bnxt_close(dev);
16376 	}
16377 	bnxt_hwrm_func_drv_unrgtr(bp);
16378 	bnxt_ptp_clear(bp);
16379 	pci_disable_device(bp->pdev);
16380 	bnxt_free_ctx_mem(bp, false);
16381 	rtnl_unlock();
16382 	return rc;
16383 }
16384 
16385 static int bnxt_resume(struct device *device)
16386 {
16387 	struct net_device *dev = dev_get_drvdata(device);
16388 	struct bnxt *bp = netdev_priv(dev);
16389 	int rc = 0;
16390 
16391 	rtnl_lock();
16392 	rc = pci_enable_device(bp->pdev);
16393 	if (rc) {
16394 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16395 			   rc);
16396 		goto resume_exit;
16397 	}
16398 	pci_set_master(bp->pdev);
16399 	if (bnxt_hwrm_ver_get(bp)) {
16400 		rc = -ENODEV;
16401 		goto resume_exit;
16402 	}
16403 	rc = bnxt_hwrm_func_reset(bp);
16404 	if (rc) {
16405 		rc = -EBUSY;
16406 		goto resume_exit;
16407 	}
16408 
16409 	rc = bnxt_hwrm_func_qcaps(bp);
16410 	if (rc)
16411 		goto resume_exit;
16412 
16413 	bnxt_clear_reservations(bp, true);
16414 
16415 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16416 		rc = -ENODEV;
16417 		goto resume_exit;
16418 	}
16419 	if (bp->fw_crash_mem)
16420 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16421 
16422 	if (bnxt_ptp_init(bp)) {
16423 		kfree(bp->ptp_cfg);
16424 		bp->ptp_cfg = NULL;
16425 	}
16426 	bnxt_get_wol_settings(bp);
16427 	if (netif_running(dev)) {
16428 		rc = bnxt_open(dev);
16429 		if (!rc)
16430 			netif_device_attach(dev);
16431 	}
16432 
16433 resume_exit:
16434 	rtnl_unlock();
16435 	bnxt_ulp_start(bp, rc);
16436 	if (!rc)
16437 		bnxt_reenable_sriov(bp);
16438 	return rc;
16439 }
16440 
16441 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16442 #define BNXT_PM_OPS (&bnxt_pm_ops)
16443 
16444 #else
16445 
16446 #define BNXT_PM_OPS NULL
16447 
16448 #endif /* CONFIG_PM_SLEEP */
16449 
16450 /**
16451  * bnxt_io_error_detected - called when PCI error is detected
16452  * @pdev: Pointer to PCI device
16453  * @state: The current pci connection state
16454  *
16455  * This function is called after a PCI bus error affecting
16456  * this device has been detected.
16457  */
16458 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16459 					       pci_channel_state_t state)
16460 {
16461 	struct net_device *netdev = pci_get_drvdata(pdev);
16462 	struct bnxt *bp = netdev_priv(netdev);
16463 	bool abort = false;
16464 
16465 	netdev_info(netdev, "PCI I/O error detected\n");
16466 
16467 	bnxt_ulp_stop(bp);
16468 
16469 	rtnl_lock();
16470 	netif_device_detach(netdev);
16471 
16472 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16473 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16474 		abort = true;
16475 	}
16476 
16477 	if (abort || state == pci_channel_io_perm_failure) {
16478 		rtnl_unlock();
16479 		return PCI_ERS_RESULT_DISCONNECT;
16480 	}
16481 
16482 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16483 	 * so we disable bus master to prevent any potential bad DMAs before
16484 	 * freeing kernel memory.
16485 	 */
16486 	if (state == pci_channel_io_frozen) {
16487 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16488 		bnxt_fw_fatal_close(bp);
16489 	}
16490 
16491 	if (netif_running(netdev))
16492 		__bnxt_close_nic(bp, true, true);
16493 
16494 	if (pci_is_enabled(pdev))
16495 		pci_disable_device(pdev);
16496 	bnxt_free_ctx_mem(bp, false);
16497 	rtnl_unlock();
16498 
16499 	/* Request a slot slot reset. */
16500 	return PCI_ERS_RESULT_NEED_RESET;
16501 }
16502 
16503 /**
16504  * bnxt_io_slot_reset - called after the pci bus has been reset.
16505  * @pdev: Pointer to PCI device
16506  *
16507  * Restart the card from scratch, as if from a cold-boot.
16508  * At this point, the card has experienced a hard reset,
16509  * followed by fixups by BIOS, and has its config space
16510  * set up identically to what it was at cold boot.
16511  */
16512 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
16513 {
16514 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
16515 	struct net_device *netdev = pci_get_drvdata(pdev);
16516 	struct bnxt *bp = netdev_priv(netdev);
16517 	int retry = 0;
16518 	int err = 0;
16519 	int off;
16520 
16521 	netdev_info(bp->dev, "PCI Slot Reset\n");
16522 
16523 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16524 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
16525 		msleep(900);
16526 
16527 	rtnl_lock();
16528 
16529 	if (pci_enable_device(pdev)) {
16530 		dev_err(&pdev->dev,
16531 			"Cannot re-enable PCI device after reset.\n");
16532 	} else {
16533 		pci_set_master(pdev);
16534 		/* Upon fatal error, our device internal logic that latches to
16535 		 * BAR value is getting reset and will restore only upon
16536 		 * rewriting the BARs.
16537 		 *
16538 		 * As pci_restore_state() does not re-write the BARs if the
16539 		 * value is same as saved value earlier, driver needs to
16540 		 * write the BARs to 0 to force restore, in case of fatal error.
16541 		 */
16542 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
16543 				       &bp->state)) {
16544 			for (off = PCI_BASE_ADDRESS_0;
16545 			     off <= PCI_BASE_ADDRESS_5; off += 4)
16546 				pci_write_config_dword(bp->pdev, off, 0);
16547 		}
16548 		pci_restore_state(pdev);
16549 		pci_save_state(pdev);
16550 
16551 		bnxt_inv_fw_health_reg(bp);
16552 		bnxt_try_map_fw_health_reg(bp);
16553 
16554 		/* In some PCIe AER scenarios, firmware may take up to
16555 		 * 10 seconds to become ready in the worst case.
16556 		 */
16557 		do {
16558 			err = bnxt_try_recover_fw(bp);
16559 			if (!err)
16560 				break;
16561 			retry++;
16562 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
16563 
16564 		if (err) {
16565 			dev_err(&pdev->dev, "Firmware not ready\n");
16566 			goto reset_exit;
16567 		}
16568 
16569 		err = bnxt_hwrm_func_reset(bp);
16570 		if (!err)
16571 			result = PCI_ERS_RESULT_RECOVERED;
16572 
16573 		bnxt_ulp_irq_stop(bp);
16574 		bnxt_clear_int_mode(bp);
16575 		err = bnxt_init_int_mode(bp);
16576 		bnxt_ulp_irq_restart(bp, err);
16577 	}
16578 
16579 reset_exit:
16580 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16581 	bnxt_clear_reservations(bp, true);
16582 	rtnl_unlock();
16583 
16584 	return result;
16585 }
16586 
16587 /**
16588  * bnxt_io_resume - called when traffic can start flowing again.
16589  * @pdev: Pointer to PCI device
16590  *
16591  * This callback is called when the error recovery driver tells
16592  * us that its OK to resume normal operation.
16593  */
16594 static void bnxt_io_resume(struct pci_dev *pdev)
16595 {
16596 	struct net_device *netdev = pci_get_drvdata(pdev);
16597 	struct bnxt *bp = netdev_priv(netdev);
16598 	int err;
16599 
16600 	netdev_info(bp->dev, "PCI Slot Resume\n");
16601 	rtnl_lock();
16602 
16603 	err = bnxt_hwrm_func_qcaps(bp);
16604 	if (!err) {
16605 		if (netif_running(netdev))
16606 			err = bnxt_open(netdev);
16607 		else
16608 			err = bnxt_reserve_rings(bp, true);
16609 	}
16610 
16611 	if (!err)
16612 		netif_device_attach(netdev);
16613 
16614 	rtnl_unlock();
16615 	bnxt_ulp_start(bp, err);
16616 	if (!err)
16617 		bnxt_reenable_sriov(bp);
16618 }
16619 
16620 static const struct pci_error_handlers bnxt_err_handler = {
16621 	.error_detected	= bnxt_io_error_detected,
16622 	.slot_reset	= bnxt_io_slot_reset,
16623 	.resume		= bnxt_io_resume
16624 };
16625 
16626 static struct pci_driver bnxt_pci_driver = {
16627 	.name		= DRV_MODULE_NAME,
16628 	.id_table	= bnxt_pci_tbl,
16629 	.probe		= bnxt_init_one,
16630 	.remove		= bnxt_remove_one,
16631 	.shutdown	= bnxt_shutdown,
16632 	.driver.pm	= BNXT_PM_OPS,
16633 	.err_handler	= &bnxt_err_handler,
16634 #if defined(CONFIG_BNXT_SRIOV)
16635 	.sriov_configure = bnxt_sriov_configure,
16636 #endif
16637 };
16638 
16639 static int __init bnxt_init(void)
16640 {
16641 	int err;
16642 
16643 	bnxt_debug_init();
16644 	err = pci_register_driver(&bnxt_pci_driver);
16645 	if (err) {
16646 		bnxt_debug_exit();
16647 		return err;
16648 	}
16649 
16650 	return 0;
16651 }
16652 
16653 static void __exit bnxt_exit(void)
16654 {
16655 	pci_unregister_driver(&bnxt_pci_driver);
16656 	if (bnxt_pf_wq)
16657 		destroy_workqueue(bnxt_pf_wq);
16658 	bnxt_debug_exit();
16659 }
16660 
16661 module_init(bnxt_init);
16662 module_exit(bnxt_exit);
16663