xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 2ee738e90e80850582cbe10f34c6447965c1d87b)
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 
85 #define BNXT_TX_PUSH_THRESH 164
86 
87 /* indexed by enum board_idx */
88 static const struct {
89 	char *name;
90 } board_info[] = {
91 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
92 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
93 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
94 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
95 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
96 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
97 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
98 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
99 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
100 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
101 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
102 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
103 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
104 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
105 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
106 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
108 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
109 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
110 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
111 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
112 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
113 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
114 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
115 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
116 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
117 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
118 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
119 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
120 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
121 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
122 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
123 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
124 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
125 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
126 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
127 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
128 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
129 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
130 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
131 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
132 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
133 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
134 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
135 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
136 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
137 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
138 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
139 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
140 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
141 };
142 
143 static const struct pci_device_id bnxt_pci_tbl[] = {
144 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
145 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
146 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
147 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
148 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
149 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
150 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
151 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
152 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
153 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
159 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
165 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
166 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
167 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
168 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
169 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
170 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
171 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
172 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
173 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
178 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
179 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
180 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
181 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
182 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
183 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
187 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
188 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
189 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
190 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
191 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
193 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
194 #ifdef CONFIG_BNXT_SRIOV
195 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
196 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
197 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
198 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
199 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
200 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
201 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
202 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
206 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
207 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
208 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
209 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
210 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
211 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
212 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
214 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
215 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
216 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
217 #endif
218 	{ 0 }
219 };
220 
221 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
222 
223 static const u16 bnxt_vf_req_snif[] = {
224 	HWRM_FUNC_CFG,
225 	HWRM_FUNC_VF_CFG,
226 	HWRM_PORT_PHY_QCFG,
227 	HWRM_CFA_L2_FILTER_ALLOC,
228 };
229 
230 static const u16 bnxt_async_events_arr[] = {
231 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
232 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
233 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
234 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
235 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
236 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
237 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
238 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
239 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
240 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
241 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
242 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
243 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
244 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
245 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
246 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
247 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
248 };
249 
250 const u16 bnxt_bstore_to_trace[] = {
251 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
252 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
253 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
254 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
255 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
256 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
257 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
258 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
259 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
260 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
261 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
262 };
263 
264 static struct workqueue_struct *bnxt_pf_wq;
265 
266 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
267 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
268 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
269 
270 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
271 	.ports = {
272 		.src = 0,
273 		.dst = 0,
274 	},
275 	.addrs = {
276 		.v6addrs = {
277 			.src = BNXT_IPV6_MASK_NONE,
278 			.dst = BNXT_IPV6_MASK_NONE,
279 		},
280 	},
281 };
282 
283 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
284 	.ports = {
285 		.src = cpu_to_be16(0xffff),
286 		.dst = cpu_to_be16(0xffff),
287 	},
288 	.addrs = {
289 		.v6addrs = {
290 			.src = BNXT_IPV6_MASK_ALL,
291 			.dst = BNXT_IPV6_MASK_ALL,
292 		},
293 	},
294 };
295 
296 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
297 	.ports = {
298 		.src = cpu_to_be16(0xffff),
299 		.dst = cpu_to_be16(0xffff),
300 	},
301 	.addrs = {
302 		.v4addrs = {
303 			.src = cpu_to_be32(0xffffffff),
304 			.dst = cpu_to_be32(0xffffffff),
305 		},
306 	},
307 };
308 
309 static bool bnxt_vf_pciid(enum board_idx idx)
310 {
311 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
312 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
313 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
314 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF);
315 }
316 
317 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
318 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
319 
320 #define BNXT_DB_CQ(db, idx)						\
321 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
322 
323 #define BNXT_DB_NQ_P5(db, idx)						\
324 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
325 		    (db)->doorbell)
326 
327 #define BNXT_DB_NQ_P7(db, idx)						\
328 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
329 		    DB_RING_IDX(db, idx), (db)->doorbell)
330 
331 #define BNXT_DB_CQ_ARM(db, idx)						\
332 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
333 
334 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
335 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
336 		    DB_RING_IDX(db, idx), (db)->doorbell)
337 
338 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
339 {
340 	if (bp->flags & BNXT_FLAG_CHIP_P7)
341 		BNXT_DB_NQ_P7(db, idx);
342 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
343 		BNXT_DB_NQ_P5(db, idx);
344 	else
345 		BNXT_DB_CQ(db, idx);
346 }
347 
348 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
349 {
350 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
351 		BNXT_DB_NQ_ARM_P5(db, idx);
352 	else
353 		BNXT_DB_CQ_ARM(db, idx);
354 }
355 
356 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
357 {
358 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
359 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
360 			    DB_RING_IDX(db, idx), db->doorbell);
361 	else
362 		BNXT_DB_CQ(db, idx);
363 }
364 
365 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
366 {
367 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
368 		return;
369 
370 	if (BNXT_PF(bp))
371 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
372 	else
373 		schedule_delayed_work(&bp->fw_reset_task, delay);
374 }
375 
376 static void __bnxt_queue_sp_work(struct bnxt *bp)
377 {
378 	if (BNXT_PF(bp))
379 		queue_work(bnxt_pf_wq, &bp->sp_task);
380 	else
381 		schedule_work(&bp->sp_task);
382 }
383 
384 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
385 {
386 	set_bit(event, &bp->sp_event);
387 	__bnxt_queue_sp_work(bp);
388 }
389 
390 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
391 {
392 	if (!rxr->bnapi->in_reset) {
393 		rxr->bnapi->in_reset = true;
394 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
395 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
396 		else
397 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
398 		__bnxt_queue_sp_work(bp);
399 	}
400 	rxr->rx_next_cons = 0xffff;
401 }
402 
403 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
404 			  u16 curr)
405 {
406 	struct bnxt_napi *bnapi = txr->bnapi;
407 
408 	if (bnapi->tx_fault)
409 		return;
410 
411 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
412 		   txr->txq_index, txr->tx_hw_cons,
413 		   txr->tx_cons, txr->tx_prod, curr);
414 	WARN_ON_ONCE(1);
415 	bnapi->tx_fault = 1;
416 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
417 }
418 
419 const u16 bnxt_lhint_arr[] = {
420 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
421 	TX_BD_FLAGS_LHINT_512_TO_1023,
422 	TX_BD_FLAGS_LHINT_1024_TO_2047,
423 	TX_BD_FLAGS_LHINT_1024_TO_2047,
424 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
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 };
440 
441 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
442 {
443 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
444 
445 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
446 		return 0;
447 
448 	return md_dst->u.port_info.port_id;
449 }
450 
451 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
452 			     u16 prod)
453 {
454 	/* Sync BD data before updating doorbell */
455 	wmb();
456 	bnxt_db_write(bp, &txr->tx_db, prod);
457 	txr->kick_pending = 0;
458 }
459 
460 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
461 {
462 	struct bnxt *bp = netdev_priv(dev);
463 	struct tx_bd *txbd, *txbd0;
464 	struct tx_bd_ext *txbd1;
465 	struct netdev_queue *txq;
466 	int i;
467 	dma_addr_t mapping;
468 	unsigned int length, pad = 0;
469 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
470 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
471 	struct pci_dev *pdev = bp->pdev;
472 	u16 prod, last_frag, txts_prod;
473 	struct bnxt_tx_ring_info *txr;
474 	struct bnxt_sw_tx_bd *tx_buf;
475 	__le32 lflags = 0;
476 
477 	i = skb_get_queue_mapping(skb);
478 	if (unlikely(i >= bp->tx_nr_rings)) {
479 		dev_kfree_skb_any(skb);
480 		dev_core_stats_tx_dropped_inc(dev);
481 		return NETDEV_TX_OK;
482 	}
483 
484 	txq = netdev_get_tx_queue(dev, i);
485 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
486 	prod = txr->tx_prod;
487 
488 	free_size = bnxt_tx_avail(bp, txr);
489 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
490 		/* We must have raced with NAPI cleanup */
491 		if (net_ratelimit() && txr->kick_pending)
492 			netif_warn(bp, tx_err, dev,
493 				   "bnxt: ring busy w/ flush pending!\n");
494 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
495 					bp->tx_wake_thresh))
496 			return NETDEV_TX_BUSY;
497 	}
498 
499 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
500 		goto tx_free;
501 
502 	length = skb->len;
503 	len = skb_headlen(skb);
504 	last_frag = skb_shinfo(skb)->nr_frags;
505 
506 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
507 
508 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
509 	tx_buf->skb = skb;
510 	tx_buf->nr_frags = last_frag;
511 
512 	vlan_tag_flags = 0;
513 	cfa_action = bnxt_xmit_get_cfa_action(skb);
514 	if (skb_vlan_tag_present(skb)) {
515 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
516 				 skb_vlan_tag_get(skb);
517 		/* Currently supports 8021Q, 8021AD vlan offloads
518 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
519 		 */
520 		if (skb->vlan_proto == htons(ETH_P_8021Q))
521 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
522 	}
523 
524 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
525 	    ptp->tx_tstamp_en) {
526 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
527 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
528 			tx_buf->is_ts_pkt = 1;
529 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
530 		} else if (!skb_is_gso(skb)) {
531 			u16 seq_id, hdr_off;
532 
533 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
534 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
535 				if (vlan_tag_flags)
536 					hdr_off += VLAN_HLEN;
537 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
538 				tx_buf->is_ts_pkt = 1;
539 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
540 
541 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
542 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
543 				tx_buf->txts_prod = txts_prod;
544 			}
545 		}
546 	}
547 	if (unlikely(skb->no_fcs))
548 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
549 
550 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
551 	    !lflags) {
552 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
553 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
554 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
555 		void __iomem *db = txr->tx_db.doorbell;
556 		void *pdata = tx_push_buf->data;
557 		u64 *end;
558 		int j, push_len;
559 
560 		/* Set COAL_NOW to be ready quickly for the next push */
561 		tx_push->tx_bd_len_flags_type =
562 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
563 					TX_BD_TYPE_LONG_TX_BD |
564 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
565 					TX_BD_FLAGS_COAL_NOW |
566 					TX_BD_FLAGS_PACKET_END |
567 					(2 << TX_BD_FLAGS_BD_CNT_SHIFT));
568 
569 		if (skb->ip_summed == CHECKSUM_PARTIAL)
570 			tx_push1->tx_bd_hsize_lflags =
571 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
572 		else
573 			tx_push1->tx_bd_hsize_lflags = 0;
574 
575 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
576 		tx_push1->tx_bd_cfa_action =
577 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
578 
579 		end = pdata + length;
580 		end = PTR_ALIGN(end, 8) - 1;
581 		*end = 0;
582 
583 		skb_copy_from_linear_data(skb, pdata, len);
584 		pdata += len;
585 		for (j = 0; j < last_frag; j++) {
586 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
587 			void *fptr;
588 
589 			fptr = skb_frag_address_safe(frag);
590 			if (!fptr)
591 				goto normal_tx;
592 
593 			memcpy(pdata, fptr, skb_frag_size(frag));
594 			pdata += skb_frag_size(frag);
595 		}
596 
597 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
598 		txbd->tx_bd_haddr = txr->data_mapping;
599 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
600 		prod = NEXT_TX(prod);
601 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
602 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
603 		memcpy(txbd, tx_push1, sizeof(*txbd));
604 		prod = NEXT_TX(prod);
605 		tx_push->doorbell =
606 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
607 				    DB_RING_IDX(&txr->tx_db, prod));
608 		WRITE_ONCE(txr->tx_prod, prod);
609 
610 		tx_buf->is_push = 1;
611 		netdev_tx_sent_queue(txq, skb->len);
612 		wmb();	/* Sync is_push and byte queue before pushing data */
613 
614 		push_len = (length + sizeof(*tx_push) + 7) / 8;
615 		if (push_len > 16) {
616 			__iowrite64_copy(db, tx_push_buf, 16);
617 			__iowrite32_copy(db + 4, tx_push_buf + 1,
618 					 (push_len - 16) << 1);
619 		} else {
620 			__iowrite64_copy(db, tx_push_buf, push_len);
621 		}
622 
623 		goto tx_done;
624 	}
625 
626 normal_tx:
627 	if (length < BNXT_MIN_PKT_SIZE) {
628 		pad = BNXT_MIN_PKT_SIZE - length;
629 		if (skb_pad(skb, pad))
630 			/* SKB already freed. */
631 			goto tx_kick_pending;
632 		length = BNXT_MIN_PKT_SIZE;
633 	}
634 
635 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
636 
637 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
638 		goto tx_free;
639 
640 	dma_unmap_addr_set(tx_buf, mapping, mapping);
641 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
642 		((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
643 
644 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
645 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
646 
647 	prod = NEXT_TX(prod);
648 	txbd1 = (struct tx_bd_ext *)
649 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
650 
651 	txbd1->tx_bd_hsize_lflags = lflags;
652 	if (skb_is_gso(skb)) {
653 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
654 		u32 hdr_len;
655 
656 		if (skb->encapsulation) {
657 			if (udp_gso)
658 				hdr_len = skb_inner_transport_offset(skb) +
659 					  sizeof(struct udphdr);
660 			else
661 				hdr_len = skb_inner_tcp_all_headers(skb);
662 		} else if (udp_gso) {
663 			hdr_len = skb_transport_offset(skb) +
664 				  sizeof(struct udphdr);
665 		} else {
666 			hdr_len = skb_tcp_all_headers(skb);
667 		}
668 
669 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
670 					TX_BD_FLAGS_T_IPID |
671 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
672 		length = skb_shinfo(skb)->gso_size;
673 		txbd1->tx_bd_mss = cpu_to_le32(length);
674 		length += hdr_len;
675 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
676 		txbd1->tx_bd_hsize_lflags |=
677 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
678 		txbd1->tx_bd_mss = 0;
679 	}
680 
681 	length >>= 9;
682 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
683 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
684 				     skb->len);
685 		i = 0;
686 		goto tx_dma_error;
687 	}
688 	flags |= bnxt_lhint_arr[length];
689 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
690 
691 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
692 	txbd1->tx_bd_cfa_action =
693 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
694 	txbd0 = txbd;
695 	for (i = 0; i < last_frag; i++) {
696 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
697 
698 		prod = NEXT_TX(prod);
699 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
700 
701 		len = skb_frag_size(frag);
702 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
703 					   DMA_TO_DEVICE);
704 
705 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
706 			goto tx_dma_error;
707 
708 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
709 		dma_unmap_addr_set(tx_buf, mapping, mapping);
710 
711 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
712 
713 		flags = len << TX_BD_LEN_SHIFT;
714 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
715 	}
716 
717 	flags &= ~TX_BD_LEN;
718 	txbd->tx_bd_len_flags_type =
719 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
720 			    TX_BD_FLAGS_PACKET_END);
721 
722 	netdev_tx_sent_queue(txq, skb->len);
723 
724 	skb_tx_timestamp(skb);
725 
726 	prod = NEXT_TX(prod);
727 	WRITE_ONCE(txr->tx_prod, prod);
728 
729 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
730 		bnxt_txr_db_kick(bp, txr, prod);
731 	} else {
732 		if (free_size >= bp->tx_wake_thresh)
733 			txbd0->tx_bd_len_flags_type |=
734 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
735 		txr->kick_pending = 1;
736 	}
737 
738 tx_done:
739 
740 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
741 		if (netdev_xmit_more() && !tx_buf->is_push) {
742 			txbd0->tx_bd_len_flags_type &=
743 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
744 			bnxt_txr_db_kick(bp, txr, prod);
745 		}
746 
747 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
748 				   bp->tx_wake_thresh);
749 	}
750 	return NETDEV_TX_OK;
751 
752 tx_dma_error:
753 	last_frag = i;
754 
755 	/* start back at beginning and unmap skb */
756 	prod = txr->tx_prod;
757 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
758 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
759 			 skb_headlen(skb), DMA_TO_DEVICE);
760 	prod = NEXT_TX(prod);
761 
762 	/* unmap remaining mapped pages */
763 	for (i = 0; i < last_frag; i++) {
764 		prod = NEXT_TX(prod);
765 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
766 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
767 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
768 			       DMA_TO_DEVICE);
769 	}
770 
771 tx_free:
772 	dev_kfree_skb_any(skb);
773 tx_kick_pending:
774 	if (BNXT_TX_PTP_IS_SET(lflags)) {
775 		txr->tx_buf_ring[txr->tx_prod].is_ts_pkt = 0;
776 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
777 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
778 			/* set SKB to err so PTP worker will clean up */
779 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
780 	}
781 	if (txr->kick_pending)
782 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
783 	txr->tx_buf_ring[txr->tx_prod].skb = NULL;
784 	dev_core_stats_tx_dropped_inc(dev);
785 	return NETDEV_TX_OK;
786 }
787 
788 /* Returns true if some remaining TX packets not processed. */
789 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
790 			  int budget)
791 {
792 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
793 	struct pci_dev *pdev = bp->pdev;
794 	u16 hw_cons = txr->tx_hw_cons;
795 	unsigned int tx_bytes = 0;
796 	u16 cons = txr->tx_cons;
797 	int tx_pkts = 0;
798 	bool rc = false;
799 
800 	while (RING_TX(bp, cons) != hw_cons) {
801 		struct bnxt_sw_tx_bd *tx_buf;
802 		struct sk_buff *skb;
803 		bool is_ts_pkt;
804 		int j, last;
805 
806 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
807 		skb = tx_buf->skb;
808 
809 		if (unlikely(!skb)) {
810 			bnxt_sched_reset_txr(bp, txr, cons);
811 			return rc;
812 		}
813 
814 		is_ts_pkt = tx_buf->is_ts_pkt;
815 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
816 			rc = true;
817 			break;
818 		}
819 
820 		cons = NEXT_TX(cons);
821 		tx_pkts++;
822 		tx_bytes += skb->len;
823 		tx_buf->skb = NULL;
824 		tx_buf->is_ts_pkt = 0;
825 
826 		if (tx_buf->is_push) {
827 			tx_buf->is_push = 0;
828 			goto next_tx_int;
829 		}
830 
831 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
832 				 skb_headlen(skb), DMA_TO_DEVICE);
833 		last = tx_buf->nr_frags;
834 
835 		for (j = 0; j < last; j++) {
836 			cons = NEXT_TX(cons);
837 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
838 			dma_unmap_page(
839 				&pdev->dev,
840 				dma_unmap_addr(tx_buf, mapping),
841 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
842 				DMA_TO_DEVICE);
843 		}
844 		if (unlikely(is_ts_pkt)) {
845 			if (BNXT_CHIP_P5(bp)) {
846 				/* PTP worker takes ownership of the skb */
847 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
848 				skb = NULL;
849 			}
850 		}
851 
852 next_tx_int:
853 		cons = NEXT_TX(cons);
854 
855 		dev_consume_skb_any(skb);
856 	}
857 
858 	WRITE_ONCE(txr->tx_cons, cons);
859 
860 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
861 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
862 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
863 
864 	return rc;
865 }
866 
867 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
868 {
869 	struct bnxt_tx_ring_info *txr;
870 	bool more = false;
871 	int i;
872 
873 	bnxt_for_each_napi_tx(i, bnapi, txr) {
874 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
875 			more |= __bnxt_tx_int(bp, txr, budget);
876 	}
877 	if (!more)
878 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
879 }
880 
881 static bool bnxt_separate_head_pool(void)
882 {
883 	return PAGE_SIZE > BNXT_RX_PAGE_SIZE;
884 }
885 
886 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
887 					 struct bnxt_rx_ring_info *rxr,
888 					 unsigned int *offset,
889 					 gfp_t gfp)
890 {
891 	struct page *page;
892 
893 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
894 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
895 						BNXT_RX_PAGE_SIZE);
896 	} else {
897 		page = page_pool_dev_alloc_pages(rxr->page_pool);
898 		*offset = 0;
899 	}
900 	if (!page)
901 		return NULL;
902 
903 	*mapping = page_pool_get_dma_addr(page) + *offset;
904 	return page;
905 }
906 
907 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
908 				       struct bnxt_rx_ring_info *rxr,
909 				       gfp_t gfp)
910 {
911 	unsigned int offset;
912 	struct page *page;
913 
914 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
915 				    bp->rx_buf_size, gfp);
916 	if (!page)
917 		return NULL;
918 
919 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
920 	return page_address(page) + offset;
921 }
922 
923 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
924 		       u16 prod, gfp_t gfp)
925 {
926 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
927 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
928 	dma_addr_t mapping;
929 
930 	if (BNXT_RX_PAGE_MODE(bp)) {
931 		unsigned int offset;
932 		struct page *page =
933 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
934 
935 		if (!page)
936 			return -ENOMEM;
937 
938 		mapping += bp->rx_dma_offset;
939 		rx_buf->data = page;
940 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
941 	} else {
942 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
943 
944 		if (!data)
945 			return -ENOMEM;
946 
947 		rx_buf->data = data;
948 		rx_buf->data_ptr = data + bp->rx_offset;
949 	}
950 	rx_buf->mapping = mapping;
951 
952 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
953 	return 0;
954 }
955 
956 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
957 {
958 	u16 prod = rxr->rx_prod;
959 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
960 	struct bnxt *bp = rxr->bnapi->bp;
961 	struct rx_bd *cons_bd, *prod_bd;
962 
963 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
964 	cons_rx_buf = &rxr->rx_buf_ring[cons];
965 
966 	prod_rx_buf->data = data;
967 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
968 
969 	prod_rx_buf->mapping = cons_rx_buf->mapping;
970 
971 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
972 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
973 
974 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
975 }
976 
977 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
978 {
979 	u16 next, max = rxr->rx_agg_bmap_size;
980 
981 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
982 	if (next >= max)
983 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
984 	return next;
985 }
986 
987 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
988 				     struct bnxt_rx_ring_info *rxr,
989 				     u16 prod, gfp_t gfp)
990 {
991 	struct rx_bd *rxbd =
992 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
993 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
994 	struct page *page;
995 	dma_addr_t mapping;
996 	u16 sw_prod = rxr->rx_sw_agg_prod;
997 	unsigned int offset = 0;
998 
999 	page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
1000 
1001 	if (!page)
1002 		return -ENOMEM;
1003 
1004 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1005 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1006 
1007 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1008 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1009 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1010 
1011 	rx_agg_buf->page = page;
1012 	rx_agg_buf->offset = offset;
1013 	rx_agg_buf->mapping = mapping;
1014 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1015 	rxbd->rx_bd_opaque = sw_prod;
1016 	return 0;
1017 }
1018 
1019 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1020 				       struct bnxt_cp_ring_info *cpr,
1021 				       u16 cp_cons, u16 curr)
1022 {
1023 	struct rx_agg_cmp *agg;
1024 
1025 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1026 	agg = (struct rx_agg_cmp *)
1027 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1028 	return agg;
1029 }
1030 
1031 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1032 					      struct bnxt_rx_ring_info *rxr,
1033 					      u16 agg_id, u16 curr)
1034 {
1035 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1036 
1037 	return &tpa_info->agg_arr[curr];
1038 }
1039 
1040 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1041 				   u16 start, u32 agg_bufs, bool tpa)
1042 {
1043 	struct bnxt_napi *bnapi = cpr->bnapi;
1044 	struct bnxt *bp = bnapi->bp;
1045 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1046 	u16 prod = rxr->rx_agg_prod;
1047 	u16 sw_prod = rxr->rx_sw_agg_prod;
1048 	bool p5_tpa = false;
1049 	u32 i;
1050 
1051 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1052 		p5_tpa = true;
1053 
1054 	for (i = 0; i < agg_bufs; i++) {
1055 		u16 cons;
1056 		struct rx_agg_cmp *agg;
1057 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1058 		struct rx_bd *prod_bd;
1059 		struct page *page;
1060 
1061 		if (p5_tpa)
1062 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1063 		else
1064 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1065 		cons = agg->rx_agg_cmp_opaque;
1066 		__clear_bit(cons, rxr->rx_agg_bmap);
1067 
1068 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1069 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1070 
1071 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1072 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1073 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1074 
1075 		/* It is possible for sw_prod to be equal to cons, so
1076 		 * set cons_rx_buf->page to NULL first.
1077 		 */
1078 		page = cons_rx_buf->page;
1079 		cons_rx_buf->page = NULL;
1080 		prod_rx_buf->page = page;
1081 		prod_rx_buf->offset = cons_rx_buf->offset;
1082 
1083 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1084 
1085 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1086 
1087 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1088 		prod_bd->rx_bd_opaque = sw_prod;
1089 
1090 		prod = NEXT_RX_AGG(prod);
1091 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1092 	}
1093 	rxr->rx_agg_prod = prod;
1094 	rxr->rx_sw_agg_prod = sw_prod;
1095 }
1096 
1097 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1098 					      struct bnxt_rx_ring_info *rxr,
1099 					      u16 cons, void *data, u8 *data_ptr,
1100 					      dma_addr_t dma_addr,
1101 					      unsigned int offset_and_len)
1102 {
1103 	unsigned int len = offset_and_len & 0xffff;
1104 	struct page *page = data;
1105 	u16 prod = rxr->rx_prod;
1106 	struct sk_buff *skb;
1107 	int err;
1108 
1109 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1110 	if (unlikely(err)) {
1111 		bnxt_reuse_rx_data(rxr, cons, data);
1112 		return NULL;
1113 	}
1114 	dma_addr -= bp->rx_dma_offset;
1115 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1116 				bp->rx_dir);
1117 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1118 	if (!skb) {
1119 		page_pool_recycle_direct(rxr->page_pool, page);
1120 		return NULL;
1121 	}
1122 	skb_mark_for_recycle(skb);
1123 	skb_reserve(skb, bp->rx_offset);
1124 	__skb_put(skb, len);
1125 
1126 	return skb;
1127 }
1128 
1129 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1130 					struct bnxt_rx_ring_info *rxr,
1131 					u16 cons, void *data, u8 *data_ptr,
1132 					dma_addr_t dma_addr,
1133 					unsigned int offset_and_len)
1134 {
1135 	unsigned int payload = offset_and_len >> 16;
1136 	unsigned int len = offset_and_len & 0xffff;
1137 	skb_frag_t *frag;
1138 	struct page *page = data;
1139 	u16 prod = rxr->rx_prod;
1140 	struct sk_buff *skb;
1141 	int off, err;
1142 
1143 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1144 	if (unlikely(err)) {
1145 		bnxt_reuse_rx_data(rxr, cons, data);
1146 		return NULL;
1147 	}
1148 	dma_addr -= bp->rx_dma_offset;
1149 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1150 				bp->rx_dir);
1151 
1152 	if (unlikely(!payload))
1153 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1154 
1155 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1156 	if (!skb) {
1157 		page_pool_recycle_direct(rxr->page_pool, page);
1158 		return NULL;
1159 	}
1160 
1161 	skb_mark_for_recycle(skb);
1162 	off = (void *)data_ptr - page_address(page);
1163 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1164 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1165 	       payload + NET_IP_ALIGN);
1166 
1167 	frag = &skb_shinfo(skb)->frags[0];
1168 	skb_frag_size_sub(frag, payload);
1169 	skb_frag_off_add(frag, payload);
1170 	skb->data_len -= payload;
1171 	skb->tail += payload;
1172 
1173 	return skb;
1174 }
1175 
1176 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1177 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1178 				   void *data, u8 *data_ptr,
1179 				   dma_addr_t dma_addr,
1180 				   unsigned int offset_and_len)
1181 {
1182 	u16 prod = rxr->rx_prod;
1183 	struct sk_buff *skb;
1184 	int err;
1185 
1186 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1187 	if (unlikely(err)) {
1188 		bnxt_reuse_rx_data(rxr, cons, data);
1189 		return NULL;
1190 	}
1191 
1192 	skb = napi_build_skb(data, bp->rx_buf_size);
1193 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1194 				bp->rx_dir);
1195 	if (!skb) {
1196 		page_pool_free_va(rxr->head_pool, data, true);
1197 		return NULL;
1198 	}
1199 
1200 	skb_mark_for_recycle(skb);
1201 	skb_reserve(skb, bp->rx_offset);
1202 	skb_put(skb, offset_and_len & 0xffff);
1203 	return skb;
1204 }
1205 
1206 static u32 __bnxt_rx_agg_pages(struct bnxt *bp,
1207 			       struct bnxt_cp_ring_info *cpr,
1208 			       struct skb_shared_info *shinfo,
1209 			       u16 idx, u32 agg_bufs, bool tpa,
1210 			       struct xdp_buff *xdp)
1211 {
1212 	struct bnxt_napi *bnapi = cpr->bnapi;
1213 	struct pci_dev *pdev = bp->pdev;
1214 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1215 	u16 prod = rxr->rx_agg_prod;
1216 	u32 i, total_frag_len = 0;
1217 	bool p5_tpa = false;
1218 
1219 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1220 		p5_tpa = true;
1221 
1222 	for (i = 0; i < agg_bufs; i++) {
1223 		skb_frag_t *frag = &shinfo->frags[i];
1224 		u16 cons, frag_len;
1225 		struct rx_agg_cmp *agg;
1226 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1227 		struct page *page;
1228 		dma_addr_t mapping;
1229 
1230 		if (p5_tpa)
1231 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1232 		else
1233 			agg = bnxt_get_agg(bp, cpr, idx, i);
1234 		cons = agg->rx_agg_cmp_opaque;
1235 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1236 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1237 
1238 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1239 		skb_frag_fill_page_desc(frag, cons_rx_buf->page,
1240 					cons_rx_buf->offset, frag_len);
1241 		shinfo->nr_frags = i + 1;
1242 		__clear_bit(cons, rxr->rx_agg_bmap);
1243 
1244 		/* It is possible for bnxt_alloc_rx_page() to allocate
1245 		 * a sw_prod index that equals the cons index, so we
1246 		 * need to clear the cons entry now.
1247 		 */
1248 		mapping = cons_rx_buf->mapping;
1249 		page = cons_rx_buf->page;
1250 		cons_rx_buf->page = NULL;
1251 
1252 		if (xdp && page_is_pfmemalloc(page))
1253 			xdp_buff_set_frag_pfmemalloc(xdp);
1254 
1255 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1256 			--shinfo->nr_frags;
1257 			cons_rx_buf->page = page;
1258 
1259 			/* Update prod since possibly some pages have been
1260 			 * allocated already.
1261 			 */
1262 			rxr->rx_agg_prod = prod;
1263 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1264 			return 0;
1265 		}
1266 
1267 		dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1268 					bp->rx_dir);
1269 
1270 		total_frag_len += frag_len;
1271 		prod = NEXT_RX_AGG(prod);
1272 	}
1273 	rxr->rx_agg_prod = prod;
1274 	return total_frag_len;
1275 }
1276 
1277 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp,
1278 					     struct bnxt_cp_ring_info *cpr,
1279 					     struct sk_buff *skb, u16 idx,
1280 					     u32 agg_bufs, bool tpa)
1281 {
1282 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1283 	u32 total_frag_len = 0;
1284 
1285 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx,
1286 					     agg_bufs, tpa, NULL);
1287 	if (!total_frag_len) {
1288 		skb_mark_for_recycle(skb);
1289 		dev_kfree_skb(skb);
1290 		return NULL;
1291 	}
1292 
1293 	skb->data_len += total_frag_len;
1294 	skb->len += total_frag_len;
1295 	skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs;
1296 	return skb;
1297 }
1298 
1299 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp,
1300 				 struct bnxt_cp_ring_info *cpr,
1301 				 struct xdp_buff *xdp, u16 idx,
1302 				 u32 agg_bufs, bool tpa)
1303 {
1304 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1305 	u32 total_frag_len = 0;
1306 
1307 	if (!xdp_buff_has_frags(xdp))
1308 		shinfo->nr_frags = 0;
1309 
1310 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo,
1311 					     idx, agg_bufs, tpa, xdp);
1312 	if (total_frag_len) {
1313 		xdp_buff_set_frags_flag(xdp);
1314 		shinfo->nr_frags = agg_bufs;
1315 		shinfo->xdp_frags_size = total_frag_len;
1316 	}
1317 	return total_frag_len;
1318 }
1319 
1320 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1321 			       u8 agg_bufs, u32 *raw_cons)
1322 {
1323 	u16 last;
1324 	struct rx_agg_cmp *agg;
1325 
1326 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1327 	last = RING_CMP(*raw_cons);
1328 	agg = (struct rx_agg_cmp *)
1329 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1330 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1331 }
1332 
1333 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1334 				      unsigned int len,
1335 				      dma_addr_t mapping)
1336 {
1337 	struct bnxt *bp = bnapi->bp;
1338 	struct pci_dev *pdev = bp->pdev;
1339 	struct sk_buff *skb;
1340 
1341 	skb = napi_alloc_skb(&bnapi->napi, len);
1342 	if (!skb)
1343 		return NULL;
1344 
1345 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1346 				bp->rx_dir);
1347 
1348 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1349 	       len + NET_IP_ALIGN);
1350 
1351 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1352 				   bp->rx_dir);
1353 
1354 	skb_put(skb, len);
1355 
1356 	return skb;
1357 }
1358 
1359 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1360 				     unsigned int len,
1361 				     dma_addr_t mapping)
1362 {
1363 	return bnxt_copy_data(bnapi, data, len, mapping);
1364 }
1365 
1366 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1367 				     struct xdp_buff *xdp,
1368 				     unsigned int len,
1369 				     dma_addr_t mapping)
1370 {
1371 	unsigned int metasize = 0;
1372 	u8 *data = xdp->data;
1373 	struct sk_buff *skb;
1374 
1375 	len = xdp->data_end - xdp->data_meta;
1376 	metasize = xdp->data - xdp->data_meta;
1377 	data = xdp->data_meta;
1378 
1379 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1380 	if (!skb)
1381 		return skb;
1382 
1383 	if (metasize) {
1384 		skb_metadata_set(skb, metasize);
1385 		__skb_pull(skb, metasize);
1386 	}
1387 
1388 	return skb;
1389 }
1390 
1391 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1392 			   u32 *raw_cons, void *cmp)
1393 {
1394 	struct rx_cmp *rxcmp = cmp;
1395 	u32 tmp_raw_cons = *raw_cons;
1396 	u8 cmp_type, agg_bufs = 0;
1397 
1398 	cmp_type = RX_CMP_TYPE(rxcmp);
1399 
1400 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1401 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1402 			    RX_CMP_AGG_BUFS) >>
1403 			   RX_CMP_AGG_BUFS_SHIFT;
1404 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1405 		struct rx_tpa_end_cmp *tpa_end = cmp;
1406 
1407 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1408 			return 0;
1409 
1410 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1411 	}
1412 
1413 	if (agg_bufs) {
1414 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1415 			return -EBUSY;
1416 	}
1417 	*raw_cons = tmp_raw_cons;
1418 	return 0;
1419 }
1420 
1421 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1422 {
1423 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1424 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1425 
1426 	if (test_bit(idx, map->agg_idx_bmap))
1427 		idx = find_first_zero_bit(map->agg_idx_bmap,
1428 					  BNXT_AGG_IDX_BMAP_SIZE);
1429 	__set_bit(idx, map->agg_idx_bmap);
1430 	map->agg_id_tbl[agg_id] = idx;
1431 	return idx;
1432 }
1433 
1434 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1435 {
1436 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1437 
1438 	__clear_bit(idx, map->agg_idx_bmap);
1439 }
1440 
1441 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1442 {
1443 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1444 
1445 	return map->agg_id_tbl[agg_id];
1446 }
1447 
1448 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1449 			      struct rx_tpa_start_cmp *tpa_start,
1450 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1451 {
1452 	tpa_info->cfa_code_valid = 1;
1453 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1454 	tpa_info->vlan_valid = 0;
1455 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1456 		tpa_info->vlan_valid = 1;
1457 		tpa_info->metadata =
1458 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1459 	}
1460 }
1461 
1462 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1463 				 struct rx_tpa_start_cmp *tpa_start,
1464 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1465 {
1466 	tpa_info->vlan_valid = 0;
1467 	if (TPA_START_VLAN_VALID(tpa_start)) {
1468 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1469 		u32 vlan_proto = ETH_P_8021Q;
1470 
1471 		tpa_info->vlan_valid = 1;
1472 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1473 			vlan_proto = ETH_P_8021AD;
1474 		tpa_info->metadata = vlan_proto << 16 |
1475 				     TPA_START_METADATA0_TCI(tpa_start1);
1476 	}
1477 }
1478 
1479 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1480 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1481 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1482 {
1483 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1484 	struct bnxt_tpa_info *tpa_info;
1485 	u16 cons, prod, agg_id;
1486 	struct rx_bd *prod_bd;
1487 	dma_addr_t mapping;
1488 
1489 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1490 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1491 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1492 	} else {
1493 		agg_id = TPA_START_AGG_ID(tpa_start);
1494 	}
1495 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1496 	prod = rxr->rx_prod;
1497 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1498 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1499 	tpa_info = &rxr->rx_tpa[agg_id];
1500 
1501 	if (unlikely(cons != rxr->rx_next_cons ||
1502 		     TPA_START_ERROR(tpa_start))) {
1503 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1504 			    cons, rxr->rx_next_cons,
1505 			    TPA_START_ERROR_CODE(tpa_start1));
1506 		bnxt_sched_reset_rxr(bp, rxr);
1507 		return;
1508 	}
1509 	prod_rx_buf->data = tpa_info->data;
1510 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1511 
1512 	mapping = tpa_info->mapping;
1513 	prod_rx_buf->mapping = mapping;
1514 
1515 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1516 
1517 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1518 
1519 	tpa_info->data = cons_rx_buf->data;
1520 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1521 	cons_rx_buf->data = NULL;
1522 	tpa_info->mapping = cons_rx_buf->mapping;
1523 
1524 	tpa_info->len =
1525 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1526 				RX_TPA_START_CMP_LEN_SHIFT;
1527 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1528 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1529 		tpa_info->gso_type = SKB_GSO_TCPV4;
1530 		if (TPA_START_IS_IPV6(tpa_start1))
1531 			tpa_info->gso_type = SKB_GSO_TCPV6;
1532 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1533 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1534 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1535 			tpa_info->gso_type = SKB_GSO_TCPV6;
1536 		tpa_info->rss_hash =
1537 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1538 	} else {
1539 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1540 		tpa_info->gso_type = 0;
1541 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1542 	}
1543 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1544 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1545 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1546 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1547 	else
1548 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1549 	tpa_info->agg_count = 0;
1550 
1551 	rxr->rx_prod = NEXT_RX(prod);
1552 	cons = RING_RX(bp, NEXT_RX(cons));
1553 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1554 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1555 
1556 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1557 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1558 	cons_rx_buf->data = NULL;
1559 }
1560 
1561 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1562 {
1563 	if (agg_bufs)
1564 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1565 }
1566 
1567 #ifdef CONFIG_INET
1568 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1569 {
1570 	struct udphdr *uh = NULL;
1571 
1572 	if (ip_proto == htons(ETH_P_IP)) {
1573 		struct iphdr *iph = (struct iphdr *)skb->data;
1574 
1575 		if (iph->protocol == IPPROTO_UDP)
1576 			uh = (struct udphdr *)(iph + 1);
1577 	} else {
1578 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1579 
1580 		if (iph->nexthdr == IPPROTO_UDP)
1581 			uh = (struct udphdr *)(iph + 1);
1582 	}
1583 	if (uh) {
1584 		if (uh->check)
1585 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1586 		else
1587 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1588 	}
1589 }
1590 #endif
1591 
1592 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1593 					   int payload_off, int tcp_ts,
1594 					   struct sk_buff *skb)
1595 {
1596 #ifdef CONFIG_INET
1597 	struct tcphdr *th;
1598 	int len, nw_off;
1599 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1600 	u32 hdr_info = tpa_info->hdr_info;
1601 	bool loopback = false;
1602 
1603 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1604 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1605 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1606 
1607 	/* If the packet is an internal loopback packet, the offsets will
1608 	 * have an extra 4 bytes.
1609 	 */
1610 	if (inner_mac_off == 4) {
1611 		loopback = true;
1612 	} else if (inner_mac_off > 4) {
1613 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1614 					    ETH_HLEN - 2));
1615 
1616 		/* We only support inner iPv4/ipv6.  If we don't see the
1617 		 * correct protocol ID, it must be a loopback packet where
1618 		 * the offsets are off by 4.
1619 		 */
1620 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1621 			loopback = true;
1622 	}
1623 	if (loopback) {
1624 		/* internal loopback packet, subtract all offsets by 4 */
1625 		inner_ip_off -= 4;
1626 		inner_mac_off -= 4;
1627 		outer_ip_off -= 4;
1628 	}
1629 
1630 	nw_off = inner_ip_off - ETH_HLEN;
1631 	skb_set_network_header(skb, nw_off);
1632 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1633 		struct ipv6hdr *iph = ipv6_hdr(skb);
1634 
1635 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1636 		len = skb->len - skb_transport_offset(skb);
1637 		th = tcp_hdr(skb);
1638 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1639 	} else {
1640 		struct iphdr *iph = ip_hdr(skb);
1641 
1642 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1643 		len = skb->len - skb_transport_offset(skb);
1644 		th = tcp_hdr(skb);
1645 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1646 	}
1647 
1648 	if (inner_mac_off) { /* tunnel */
1649 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1650 					    ETH_HLEN - 2));
1651 
1652 		bnxt_gro_tunnel(skb, proto);
1653 	}
1654 #endif
1655 	return skb;
1656 }
1657 
1658 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1659 					   int payload_off, int tcp_ts,
1660 					   struct sk_buff *skb)
1661 {
1662 #ifdef CONFIG_INET
1663 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1664 	u32 hdr_info = tpa_info->hdr_info;
1665 	int iphdr_len, nw_off;
1666 
1667 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1668 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1669 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1670 
1671 	nw_off = inner_ip_off - ETH_HLEN;
1672 	skb_set_network_header(skb, nw_off);
1673 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1674 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1675 	skb_set_transport_header(skb, nw_off + iphdr_len);
1676 
1677 	if (inner_mac_off) { /* tunnel */
1678 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1679 					    ETH_HLEN - 2));
1680 
1681 		bnxt_gro_tunnel(skb, proto);
1682 	}
1683 #endif
1684 	return skb;
1685 }
1686 
1687 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1688 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1689 
1690 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1691 					   int payload_off, int tcp_ts,
1692 					   struct sk_buff *skb)
1693 {
1694 #ifdef CONFIG_INET
1695 	struct tcphdr *th;
1696 	int len, nw_off, tcp_opt_len = 0;
1697 
1698 	if (tcp_ts)
1699 		tcp_opt_len = 12;
1700 
1701 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1702 		struct iphdr *iph;
1703 
1704 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1705 			 ETH_HLEN;
1706 		skb_set_network_header(skb, nw_off);
1707 		iph = ip_hdr(skb);
1708 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1709 		len = skb->len - skb_transport_offset(skb);
1710 		th = tcp_hdr(skb);
1711 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1712 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1713 		struct ipv6hdr *iph;
1714 
1715 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1716 			 ETH_HLEN;
1717 		skb_set_network_header(skb, nw_off);
1718 		iph = ipv6_hdr(skb);
1719 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1720 		len = skb->len - skb_transport_offset(skb);
1721 		th = tcp_hdr(skb);
1722 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1723 	} else {
1724 		dev_kfree_skb_any(skb);
1725 		return NULL;
1726 	}
1727 
1728 	if (nw_off) /* tunnel */
1729 		bnxt_gro_tunnel(skb, skb->protocol);
1730 #endif
1731 	return skb;
1732 }
1733 
1734 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1735 					   struct bnxt_tpa_info *tpa_info,
1736 					   struct rx_tpa_end_cmp *tpa_end,
1737 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1738 					   struct sk_buff *skb)
1739 {
1740 #ifdef CONFIG_INET
1741 	int payload_off;
1742 	u16 segs;
1743 
1744 	segs = TPA_END_TPA_SEGS(tpa_end);
1745 	if (segs == 1)
1746 		return skb;
1747 
1748 	NAPI_GRO_CB(skb)->count = segs;
1749 	skb_shinfo(skb)->gso_size =
1750 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1751 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1752 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1753 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1754 	else
1755 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1756 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1757 	if (likely(skb))
1758 		tcp_gro_complete(skb);
1759 #endif
1760 	return skb;
1761 }
1762 
1763 /* Given the cfa_code of a received packet determine which
1764  * netdev (vf-rep or PF) the packet is destined to.
1765  */
1766 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1767 {
1768 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1769 
1770 	/* if vf-rep dev is NULL, the must belongs to the PF */
1771 	return dev ? dev : bp->dev;
1772 }
1773 
1774 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1775 					   struct bnxt_cp_ring_info *cpr,
1776 					   u32 *raw_cons,
1777 					   struct rx_tpa_end_cmp *tpa_end,
1778 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1779 					   u8 *event)
1780 {
1781 	struct bnxt_napi *bnapi = cpr->bnapi;
1782 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1783 	struct net_device *dev = bp->dev;
1784 	u8 *data_ptr, agg_bufs;
1785 	unsigned int len;
1786 	struct bnxt_tpa_info *tpa_info;
1787 	dma_addr_t mapping;
1788 	struct sk_buff *skb;
1789 	u16 idx = 0, agg_id;
1790 	void *data;
1791 	bool gro;
1792 
1793 	if (unlikely(bnapi->in_reset)) {
1794 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1795 
1796 		if (rc < 0)
1797 			return ERR_PTR(-EBUSY);
1798 		return NULL;
1799 	}
1800 
1801 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1802 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1803 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1804 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1805 		tpa_info = &rxr->rx_tpa[agg_id];
1806 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1807 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1808 				    agg_bufs, tpa_info->agg_count);
1809 			agg_bufs = tpa_info->agg_count;
1810 		}
1811 		tpa_info->agg_count = 0;
1812 		*event |= BNXT_AGG_EVENT;
1813 		bnxt_free_agg_idx(rxr, agg_id);
1814 		idx = agg_id;
1815 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1816 	} else {
1817 		agg_id = TPA_END_AGG_ID(tpa_end);
1818 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1819 		tpa_info = &rxr->rx_tpa[agg_id];
1820 		idx = RING_CMP(*raw_cons);
1821 		if (agg_bufs) {
1822 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1823 				return ERR_PTR(-EBUSY);
1824 
1825 			*event |= BNXT_AGG_EVENT;
1826 			idx = NEXT_CMP(idx);
1827 		}
1828 		gro = !!TPA_END_GRO(tpa_end);
1829 	}
1830 	data = tpa_info->data;
1831 	data_ptr = tpa_info->data_ptr;
1832 	prefetch(data_ptr);
1833 	len = tpa_info->len;
1834 	mapping = tpa_info->mapping;
1835 
1836 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1837 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1838 		if (agg_bufs > MAX_SKB_FRAGS)
1839 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1840 				    agg_bufs, (int)MAX_SKB_FRAGS);
1841 		return NULL;
1842 	}
1843 
1844 	if (len <= bp->rx_copybreak) {
1845 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1846 		if (!skb) {
1847 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1848 			cpr->sw_stats->rx.rx_oom_discards += 1;
1849 			return NULL;
1850 		}
1851 	} else {
1852 		u8 *new_data;
1853 		dma_addr_t new_mapping;
1854 
1855 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1856 						GFP_ATOMIC);
1857 		if (!new_data) {
1858 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1859 			cpr->sw_stats->rx.rx_oom_discards += 1;
1860 			return NULL;
1861 		}
1862 
1863 		tpa_info->data = new_data;
1864 		tpa_info->data_ptr = new_data + bp->rx_offset;
1865 		tpa_info->mapping = new_mapping;
1866 
1867 		skb = napi_build_skb(data, bp->rx_buf_size);
1868 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1869 					bp->rx_buf_use_size, bp->rx_dir);
1870 
1871 		if (!skb) {
1872 			page_pool_free_va(rxr->head_pool, data, true);
1873 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1874 			cpr->sw_stats->rx.rx_oom_discards += 1;
1875 			return NULL;
1876 		}
1877 		skb_mark_for_recycle(skb);
1878 		skb_reserve(skb, bp->rx_offset);
1879 		skb_put(skb, len);
1880 	}
1881 
1882 	if (agg_bufs) {
1883 		skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true);
1884 		if (!skb) {
1885 			/* Page reuse already handled by bnxt_rx_pages(). */
1886 			cpr->sw_stats->rx.rx_oom_discards += 1;
1887 			return NULL;
1888 		}
1889 	}
1890 
1891 	if (tpa_info->cfa_code_valid)
1892 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1893 	skb->protocol = eth_type_trans(skb, dev);
1894 
1895 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1896 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1897 
1898 	if (tpa_info->vlan_valid &&
1899 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1900 		__be16 vlan_proto = htons(tpa_info->metadata >>
1901 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1902 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1903 
1904 		if (eth_type_vlan(vlan_proto)) {
1905 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1906 		} else {
1907 			dev_kfree_skb(skb);
1908 			return NULL;
1909 		}
1910 	}
1911 
1912 	skb_checksum_none_assert(skb);
1913 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1914 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1915 		skb->csum_level =
1916 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1917 	}
1918 
1919 	if (gro)
1920 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1921 
1922 	return skb;
1923 }
1924 
1925 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1926 			 struct rx_agg_cmp *rx_agg)
1927 {
1928 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1929 	struct bnxt_tpa_info *tpa_info;
1930 
1931 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1932 	tpa_info = &rxr->rx_tpa[agg_id];
1933 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1934 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1935 }
1936 
1937 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1938 			     struct sk_buff *skb)
1939 {
1940 	skb_mark_for_recycle(skb);
1941 
1942 	if (skb->dev != bp->dev) {
1943 		/* this packet belongs to a vf-rep */
1944 		bnxt_vf_rep_rx(bp, skb);
1945 		return;
1946 	}
1947 	skb_record_rx_queue(skb, bnapi->index);
1948 	napi_gro_receive(&bnapi->napi, skb);
1949 }
1950 
1951 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
1952 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
1953 {
1954 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1955 
1956 	if (BNXT_PTP_RX_TS_VALID(flags))
1957 		goto ts_valid;
1958 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
1959 		return false;
1960 
1961 ts_valid:
1962 	*cmpl_ts = ts;
1963 	return true;
1964 }
1965 
1966 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
1967 				    struct rx_cmp *rxcmp,
1968 				    struct rx_cmp_ext *rxcmp1)
1969 {
1970 	__be16 vlan_proto;
1971 	u16 vtag;
1972 
1973 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1974 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
1975 		u32 meta_data;
1976 
1977 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
1978 			return skb;
1979 
1980 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1981 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1982 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
1983 		if (eth_type_vlan(vlan_proto))
1984 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1985 		else
1986 			goto vlan_err;
1987 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
1988 		if (RX_CMP_VLAN_VALID(rxcmp)) {
1989 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
1990 
1991 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
1992 				vlan_proto = htons(ETH_P_8021Q);
1993 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
1994 				vlan_proto = htons(ETH_P_8021AD);
1995 			else
1996 				goto vlan_err;
1997 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
1998 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1999 		}
2000 	}
2001 	return skb;
2002 vlan_err:
2003 	dev_kfree_skb(skb);
2004 	return NULL;
2005 }
2006 
2007 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
2008 					   struct rx_cmp *rxcmp)
2009 {
2010 	u8 ext_op;
2011 
2012 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
2013 	switch (ext_op) {
2014 	case EXT_OP_INNER_4:
2015 	case EXT_OP_OUTER_4:
2016 	case EXT_OP_INNFL_3:
2017 	case EXT_OP_OUTFL_3:
2018 		return PKT_HASH_TYPE_L4;
2019 	default:
2020 		return PKT_HASH_TYPE_L3;
2021 	}
2022 }
2023 
2024 /* returns the following:
2025  * 1       - 1 packet successfully received
2026  * 0       - successful TPA_START, packet not completed yet
2027  * -EBUSY  - completion ring does not have all the agg buffers yet
2028  * -ENOMEM - packet aborted due to out of memory
2029  * -EIO    - packet aborted due to hw error indicated in BD
2030  */
2031 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2032 		       u32 *raw_cons, u8 *event)
2033 {
2034 	struct bnxt_napi *bnapi = cpr->bnapi;
2035 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2036 	struct net_device *dev = bp->dev;
2037 	struct rx_cmp *rxcmp;
2038 	struct rx_cmp_ext *rxcmp1;
2039 	u32 tmp_raw_cons = *raw_cons;
2040 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2041 	struct bnxt_sw_rx_bd *rx_buf;
2042 	unsigned int len;
2043 	u8 *data_ptr, agg_bufs, cmp_type;
2044 	bool xdp_active = false;
2045 	dma_addr_t dma_addr;
2046 	struct sk_buff *skb;
2047 	struct xdp_buff xdp;
2048 	u32 flags, misc;
2049 	u32 cmpl_ts;
2050 	void *data;
2051 	int rc = 0;
2052 
2053 	rxcmp = (struct rx_cmp *)
2054 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2055 
2056 	cmp_type = RX_CMP_TYPE(rxcmp);
2057 
2058 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2059 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2060 		goto next_rx_no_prod_no_len;
2061 	}
2062 
2063 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2064 	cp_cons = RING_CMP(tmp_raw_cons);
2065 	rxcmp1 = (struct rx_cmp_ext *)
2066 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2067 
2068 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2069 		return -EBUSY;
2070 
2071 	/* The valid test of the entry must be done first before
2072 	 * reading any further.
2073 	 */
2074 	dma_rmb();
2075 	prod = rxr->rx_prod;
2076 
2077 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2078 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2079 		bnxt_tpa_start(bp, rxr, cmp_type,
2080 			       (struct rx_tpa_start_cmp *)rxcmp,
2081 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2082 
2083 		*event |= BNXT_RX_EVENT;
2084 		goto next_rx_no_prod_no_len;
2085 
2086 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2087 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2088 				   (struct rx_tpa_end_cmp *)rxcmp,
2089 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2090 
2091 		if (IS_ERR(skb))
2092 			return -EBUSY;
2093 
2094 		rc = -ENOMEM;
2095 		if (likely(skb)) {
2096 			bnxt_deliver_skb(bp, bnapi, skb);
2097 			rc = 1;
2098 		}
2099 		*event |= BNXT_RX_EVENT;
2100 		goto next_rx_no_prod_no_len;
2101 	}
2102 
2103 	cons = rxcmp->rx_cmp_opaque;
2104 	if (unlikely(cons != rxr->rx_next_cons)) {
2105 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2106 
2107 		/* 0xffff is forced error, don't print it */
2108 		if (rxr->rx_next_cons != 0xffff)
2109 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2110 				    cons, rxr->rx_next_cons);
2111 		bnxt_sched_reset_rxr(bp, rxr);
2112 		if (rc1)
2113 			return rc1;
2114 		goto next_rx_no_prod_no_len;
2115 	}
2116 	rx_buf = &rxr->rx_buf_ring[cons];
2117 	data = rx_buf->data;
2118 	data_ptr = rx_buf->data_ptr;
2119 	prefetch(data_ptr);
2120 
2121 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2122 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2123 
2124 	if (agg_bufs) {
2125 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2126 			return -EBUSY;
2127 
2128 		cp_cons = NEXT_CMP(cp_cons);
2129 		*event |= BNXT_AGG_EVENT;
2130 	}
2131 	*event |= BNXT_RX_EVENT;
2132 
2133 	rx_buf->data = NULL;
2134 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2135 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2136 
2137 		bnxt_reuse_rx_data(rxr, cons, data);
2138 		if (agg_bufs)
2139 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2140 					       false);
2141 
2142 		rc = -EIO;
2143 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2144 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2145 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2146 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2147 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2148 						 rx_err);
2149 				bnxt_sched_reset_rxr(bp, rxr);
2150 			}
2151 		}
2152 		goto next_rx_no_len;
2153 	}
2154 
2155 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2156 	len = flags >> RX_CMP_LEN_SHIFT;
2157 	dma_addr = rx_buf->mapping;
2158 
2159 	if (bnxt_xdp_attached(bp, rxr)) {
2160 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2161 		if (agg_bufs) {
2162 			u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
2163 							     cp_cons, agg_bufs,
2164 							     false);
2165 			if (!frag_len)
2166 				goto oom_next_rx;
2167 		}
2168 		xdp_active = true;
2169 	}
2170 
2171 	if (xdp_active) {
2172 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2173 			rc = 1;
2174 			goto next_rx;
2175 		}
2176 	}
2177 
2178 	if (len <= bp->rx_copybreak) {
2179 		if (!xdp_active)
2180 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2181 		else
2182 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2183 		bnxt_reuse_rx_data(rxr, cons, data);
2184 		if (!skb) {
2185 			if (agg_bufs) {
2186 				if (!xdp_active)
2187 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2188 							       agg_bufs, false);
2189 				else
2190 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2191 			}
2192 			goto oom_next_rx;
2193 		}
2194 	} else {
2195 		u32 payload;
2196 
2197 		if (rx_buf->data_ptr == data_ptr)
2198 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2199 		else
2200 			payload = 0;
2201 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2202 				      payload | len);
2203 		if (!skb)
2204 			goto oom_next_rx;
2205 	}
2206 
2207 	if (agg_bufs) {
2208 		if (!xdp_active) {
2209 			skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
2210 			if (!skb)
2211 				goto oom_next_rx;
2212 		} else {
2213 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1);
2214 			if (!skb) {
2215 				/* we should be able to free the old skb here */
2216 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2217 				goto oom_next_rx;
2218 			}
2219 		}
2220 	}
2221 
2222 	if (RX_CMP_HASH_VALID(rxcmp)) {
2223 		enum pkt_hash_types type;
2224 
2225 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2226 			type = bnxt_rss_ext_op(bp, rxcmp);
2227 		} else {
2228 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2229 
2230 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2231 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2232 				type = PKT_HASH_TYPE_L4;
2233 			else
2234 				type = PKT_HASH_TYPE_L3;
2235 		}
2236 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2237 	}
2238 
2239 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2240 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2241 	skb->protocol = eth_type_trans(skb, dev);
2242 
2243 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2244 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2245 		if (!skb)
2246 			goto next_rx;
2247 	}
2248 
2249 	skb_checksum_none_assert(skb);
2250 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2251 		if (dev->features & NETIF_F_RXCSUM) {
2252 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2253 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2254 		}
2255 	} else {
2256 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2257 			if (dev->features & NETIF_F_RXCSUM)
2258 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2259 		}
2260 	}
2261 
2262 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2263 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2264 			u64 ns, ts;
2265 
2266 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2267 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2268 
2269 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2270 				memset(skb_hwtstamps(skb), 0,
2271 				       sizeof(*skb_hwtstamps(skb)));
2272 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2273 			}
2274 		}
2275 	}
2276 	bnxt_deliver_skb(bp, bnapi, skb);
2277 	rc = 1;
2278 
2279 next_rx:
2280 	cpr->rx_packets += 1;
2281 	cpr->rx_bytes += len;
2282 
2283 next_rx_no_len:
2284 	rxr->rx_prod = NEXT_RX(prod);
2285 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2286 
2287 next_rx_no_prod_no_len:
2288 	*raw_cons = tmp_raw_cons;
2289 
2290 	return rc;
2291 
2292 oom_next_rx:
2293 	cpr->sw_stats->rx.rx_oom_discards += 1;
2294 	rc = -ENOMEM;
2295 	goto next_rx;
2296 }
2297 
2298 /* In netpoll mode, if we are using a combined completion ring, we need to
2299  * discard the rx packets and recycle the buffers.
2300  */
2301 static int bnxt_force_rx_discard(struct bnxt *bp,
2302 				 struct bnxt_cp_ring_info *cpr,
2303 				 u32 *raw_cons, u8 *event)
2304 {
2305 	u32 tmp_raw_cons = *raw_cons;
2306 	struct rx_cmp_ext *rxcmp1;
2307 	struct rx_cmp *rxcmp;
2308 	u16 cp_cons;
2309 	u8 cmp_type;
2310 	int rc;
2311 
2312 	cp_cons = RING_CMP(tmp_raw_cons);
2313 	rxcmp = (struct rx_cmp *)
2314 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2315 
2316 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2317 	cp_cons = RING_CMP(tmp_raw_cons);
2318 	rxcmp1 = (struct rx_cmp_ext *)
2319 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2320 
2321 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2322 		return -EBUSY;
2323 
2324 	/* The valid test of the entry must be done first before
2325 	 * reading any further.
2326 	 */
2327 	dma_rmb();
2328 	cmp_type = RX_CMP_TYPE(rxcmp);
2329 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2330 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2331 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2332 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2333 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2334 		struct rx_tpa_end_cmp_ext *tpa_end1;
2335 
2336 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2337 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2338 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2339 	}
2340 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2341 	if (rc && rc != -EBUSY)
2342 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2343 	return rc;
2344 }
2345 
2346 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2347 {
2348 	struct bnxt_fw_health *fw_health = bp->fw_health;
2349 	u32 reg = fw_health->regs[reg_idx];
2350 	u32 reg_type, reg_off, val = 0;
2351 
2352 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2353 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2354 	switch (reg_type) {
2355 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2356 		pci_read_config_dword(bp->pdev, reg_off, &val);
2357 		break;
2358 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2359 		reg_off = fw_health->mapped_regs[reg_idx];
2360 		fallthrough;
2361 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2362 		val = readl(bp->bar0 + reg_off);
2363 		break;
2364 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2365 		val = readl(bp->bar1 + reg_off);
2366 		break;
2367 	}
2368 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2369 		val &= fw_health->fw_reset_inprog_reg_mask;
2370 	return val;
2371 }
2372 
2373 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2374 {
2375 	int i;
2376 
2377 	for (i = 0; i < bp->rx_nr_rings; i++) {
2378 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2379 		struct bnxt_ring_grp_info *grp_info;
2380 
2381 		grp_info = &bp->grp_info[grp_idx];
2382 		if (grp_info->agg_fw_ring_id == ring_id)
2383 			return grp_idx;
2384 	}
2385 	return INVALID_HW_RING_ID;
2386 }
2387 
2388 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2389 {
2390 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2391 
2392 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2393 		return link_info->force_link_speed2;
2394 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2395 		return link_info->force_pam4_link_speed;
2396 	return link_info->force_link_speed;
2397 }
2398 
2399 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2400 {
2401 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2402 
2403 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2404 		link_info->req_link_speed = link_info->force_link_speed2;
2405 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2406 		switch (link_info->req_link_speed) {
2407 		case BNXT_LINK_SPEED_50GB_PAM4:
2408 		case BNXT_LINK_SPEED_100GB_PAM4:
2409 		case BNXT_LINK_SPEED_200GB_PAM4:
2410 		case BNXT_LINK_SPEED_400GB_PAM4:
2411 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2412 			break;
2413 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2414 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2415 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2416 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2417 			break;
2418 		default:
2419 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2420 		}
2421 		return;
2422 	}
2423 	link_info->req_link_speed = link_info->force_link_speed;
2424 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2425 	if (link_info->force_pam4_link_speed) {
2426 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2427 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2428 	}
2429 }
2430 
2431 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2432 {
2433 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2434 
2435 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2436 		link_info->advertising = link_info->auto_link_speeds2;
2437 		return;
2438 	}
2439 	link_info->advertising = link_info->auto_link_speeds;
2440 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2441 }
2442 
2443 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2444 {
2445 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2446 
2447 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2448 		if (link_info->req_link_speed != link_info->force_link_speed2)
2449 			return true;
2450 		return false;
2451 	}
2452 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2453 	    link_info->req_link_speed != link_info->force_link_speed)
2454 		return true;
2455 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2456 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2457 		return true;
2458 	return false;
2459 }
2460 
2461 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2462 {
2463 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2464 
2465 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2466 		if (link_info->advertising != link_info->auto_link_speeds2)
2467 			return true;
2468 		return false;
2469 	}
2470 	if (link_info->advertising != link_info->auto_link_speeds ||
2471 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2472 		return true;
2473 	return false;
2474 }
2475 
2476 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2477 {
2478 	u32 flags = bp->ctx->ctx_arr[type].flags;
2479 
2480 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2481 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2482 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2483 }
2484 
2485 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2486 {
2487 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2488 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2489 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2490 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2491 	struct bnxt_bs_trace_info *bs_trace;
2492 	int last_pg;
2493 
2494 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2495 		return;
2496 
2497 	mem_size = ctxm->max_entries * ctxm->entry_size;
2498 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2499 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2500 
2501 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2502 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2503 
2504 	rmem = &ctx_pg[0].ring_mem;
2505 	bs_trace = &bp->bs_trace[trace_type];
2506 	bs_trace->ctx_type = ctxm->type;
2507 	bs_trace->trace_type = trace_type;
2508 	if (pages > MAX_CTX_PAGES) {
2509 		int last_pg_dir = rmem->nr_pages - 1;
2510 
2511 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2512 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2513 	} else {
2514 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2515 	}
2516 	bs_trace->magic_byte += magic_byte_offset;
2517 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2518 }
2519 
2520 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2521 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2522 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2523 
2524 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2525 	(((data2) &							\
2526 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2527 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2528 
2529 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2530 	((data2) &							\
2531 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2532 
2533 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2534 	(((data2) &							\
2535 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2536 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2537 
2538 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2539 	((data1) &							\
2540 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2541 
2542 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2543 	(((data1) &							\
2544 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2545 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2546 
2547 /* Return true if the workqueue has to be scheduled */
2548 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2549 {
2550 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2551 
2552 	switch (err_type) {
2553 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2554 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2555 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2556 		break;
2557 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2558 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2559 		break;
2560 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2561 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2562 		break;
2563 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2564 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2565 		char *threshold_type;
2566 		bool notify = false;
2567 		char *dir_str;
2568 
2569 		switch (type) {
2570 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2571 			threshold_type = "warning";
2572 			break;
2573 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2574 			threshold_type = "critical";
2575 			break;
2576 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2577 			threshold_type = "fatal";
2578 			break;
2579 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2580 			threshold_type = "shutdown";
2581 			break;
2582 		default:
2583 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2584 			return false;
2585 		}
2586 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2587 			dir_str = "above";
2588 			notify = true;
2589 		} else {
2590 			dir_str = "below";
2591 		}
2592 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2593 			    dir_str, threshold_type);
2594 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2595 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2596 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2597 		if (notify) {
2598 			bp->thermal_threshold_type = type;
2599 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2600 			return true;
2601 		}
2602 		return false;
2603 	}
2604 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2605 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2606 		break;
2607 	default:
2608 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2609 			   err_type);
2610 		break;
2611 	}
2612 	return false;
2613 }
2614 
2615 #define BNXT_GET_EVENT_PORT(data)	\
2616 	((data) &			\
2617 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2618 
2619 #define BNXT_EVENT_RING_TYPE(data2)	\
2620 	((data2) &			\
2621 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2622 
2623 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2624 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2625 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2626 
2627 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2628 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2629 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2630 
2631 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2632 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2633 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2634 
2635 #define BNXT_PHC_BITS	48
2636 
2637 static int bnxt_async_event_process(struct bnxt *bp,
2638 				    struct hwrm_async_event_cmpl *cmpl)
2639 {
2640 	u16 event_id = le16_to_cpu(cmpl->event_id);
2641 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2642 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2643 
2644 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2645 		   event_id, data1, data2);
2646 
2647 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2648 	switch (event_id) {
2649 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2650 		struct bnxt_link_info *link_info = &bp->link_info;
2651 
2652 		if (BNXT_VF(bp))
2653 			goto async_event_process_exit;
2654 
2655 		/* print unsupported speed warning in forced speed mode only */
2656 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2657 		    (data1 & 0x20000)) {
2658 			u16 fw_speed = bnxt_get_force_speed(link_info);
2659 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2660 
2661 			if (speed != SPEED_UNKNOWN)
2662 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2663 					    speed);
2664 		}
2665 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2666 	}
2667 		fallthrough;
2668 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2669 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2670 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2671 		fallthrough;
2672 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2673 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2674 		break;
2675 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2676 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2677 		break;
2678 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2679 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2680 
2681 		if (BNXT_VF(bp))
2682 			break;
2683 
2684 		if (bp->pf.port_id != port_id)
2685 			break;
2686 
2687 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2688 		break;
2689 	}
2690 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2691 		if (BNXT_PF(bp))
2692 			goto async_event_process_exit;
2693 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2694 		break;
2695 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2696 		char *type_str = "Solicited";
2697 
2698 		if (!bp->fw_health)
2699 			goto async_event_process_exit;
2700 
2701 		bp->fw_reset_timestamp = jiffies;
2702 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2703 		if (!bp->fw_reset_min_dsecs)
2704 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2705 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2706 		if (!bp->fw_reset_max_dsecs)
2707 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2708 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2709 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2710 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2711 			type_str = "Fatal";
2712 			bp->fw_health->fatalities++;
2713 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2714 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2715 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2716 			type_str = "Non-fatal";
2717 			bp->fw_health->survivals++;
2718 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2719 		}
2720 		netif_warn(bp, hw, bp->dev,
2721 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2722 			   type_str, data1, data2,
2723 			   bp->fw_reset_min_dsecs * 100,
2724 			   bp->fw_reset_max_dsecs * 100);
2725 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2726 		break;
2727 	}
2728 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2729 		struct bnxt_fw_health *fw_health = bp->fw_health;
2730 		char *status_desc = "healthy";
2731 		u32 status;
2732 
2733 		if (!fw_health)
2734 			goto async_event_process_exit;
2735 
2736 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2737 			fw_health->enabled = false;
2738 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2739 			break;
2740 		}
2741 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2742 		fw_health->tmr_multiplier =
2743 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2744 				     bp->current_interval * 10);
2745 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2746 		if (!fw_health->enabled)
2747 			fw_health->last_fw_heartbeat =
2748 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2749 		fw_health->last_fw_reset_cnt =
2750 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2751 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2752 		if (status != BNXT_FW_STATUS_HEALTHY)
2753 			status_desc = "unhealthy";
2754 		netif_info(bp, drv, bp->dev,
2755 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2756 			   fw_health->primary ? "primary" : "backup", status,
2757 			   status_desc, fw_health->last_fw_reset_cnt);
2758 		if (!fw_health->enabled) {
2759 			/* Make sure tmr_counter is set and visible to
2760 			 * bnxt_health_check() before setting enabled to true.
2761 			 */
2762 			smp_wmb();
2763 			fw_health->enabled = true;
2764 		}
2765 		goto async_event_process_exit;
2766 	}
2767 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2768 		netif_notice(bp, hw, bp->dev,
2769 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2770 			     data1, data2);
2771 		goto async_event_process_exit;
2772 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2773 		struct bnxt_rx_ring_info *rxr;
2774 		u16 grp_idx;
2775 
2776 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2777 			goto async_event_process_exit;
2778 
2779 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2780 			    BNXT_EVENT_RING_TYPE(data2), data1);
2781 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2782 			goto async_event_process_exit;
2783 
2784 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2785 		if (grp_idx == INVALID_HW_RING_ID) {
2786 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2787 				    data1);
2788 			goto async_event_process_exit;
2789 		}
2790 		rxr = bp->bnapi[grp_idx]->rx_ring;
2791 		bnxt_sched_reset_rxr(bp, rxr);
2792 		goto async_event_process_exit;
2793 	}
2794 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2795 		struct bnxt_fw_health *fw_health = bp->fw_health;
2796 
2797 		netif_notice(bp, hw, bp->dev,
2798 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2799 			     data1, data2);
2800 		if (fw_health) {
2801 			fw_health->echo_req_data1 = data1;
2802 			fw_health->echo_req_data2 = data2;
2803 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2804 			break;
2805 		}
2806 		goto async_event_process_exit;
2807 	}
2808 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2809 		bnxt_ptp_pps_event(bp, data1, data2);
2810 		goto async_event_process_exit;
2811 	}
2812 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2813 		if (bnxt_event_error_report(bp, data1, data2))
2814 			break;
2815 		goto async_event_process_exit;
2816 	}
2817 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2818 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2819 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2820 			if (BNXT_PTP_USE_RTC(bp)) {
2821 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2822 				unsigned long flags;
2823 				u64 ns;
2824 
2825 				if (!ptp)
2826 					goto async_event_process_exit;
2827 
2828 				bnxt_ptp_update_current_time(bp);
2829 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2830 				       BNXT_PHC_BITS) | ptp->current_time);
2831 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2832 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2833 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2834 			}
2835 			break;
2836 		}
2837 		goto async_event_process_exit;
2838 	}
2839 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2840 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2841 
2842 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2843 		goto async_event_process_exit;
2844 	}
2845 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2846 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2847 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2848 
2849 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2850 		goto async_event_process_exit;
2851 	}
2852 	default:
2853 		goto async_event_process_exit;
2854 	}
2855 	__bnxt_queue_sp_work(bp);
2856 async_event_process_exit:
2857 	return 0;
2858 }
2859 
2860 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2861 {
2862 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2863 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2864 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2865 				(struct hwrm_fwd_req_cmpl *)txcmp;
2866 
2867 	switch (cmpl_type) {
2868 	case CMPL_BASE_TYPE_HWRM_DONE:
2869 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2870 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2871 		break;
2872 
2873 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2874 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2875 
2876 		if ((vf_id < bp->pf.first_vf_id) ||
2877 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2878 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2879 				   vf_id);
2880 			return -EINVAL;
2881 		}
2882 
2883 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2884 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2885 		break;
2886 
2887 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2888 		bnxt_async_event_process(bp,
2889 					 (struct hwrm_async_event_cmpl *)txcmp);
2890 		break;
2891 
2892 	default:
2893 		break;
2894 	}
2895 
2896 	return 0;
2897 }
2898 
2899 static bool bnxt_vnic_is_active(struct bnxt *bp)
2900 {
2901 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
2902 
2903 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
2904 }
2905 
2906 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2907 {
2908 	struct bnxt_napi *bnapi = dev_instance;
2909 	struct bnxt *bp = bnapi->bp;
2910 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2911 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2912 
2913 	cpr->event_ctr++;
2914 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2915 	napi_schedule(&bnapi->napi);
2916 	return IRQ_HANDLED;
2917 }
2918 
2919 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2920 {
2921 	u32 raw_cons = cpr->cp_raw_cons;
2922 	u16 cons = RING_CMP(raw_cons);
2923 	struct tx_cmp *txcmp;
2924 
2925 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2926 
2927 	return TX_CMP_VALID(txcmp, raw_cons);
2928 }
2929 
2930 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2931 			    int budget)
2932 {
2933 	struct bnxt_napi *bnapi = cpr->bnapi;
2934 	u32 raw_cons = cpr->cp_raw_cons;
2935 	u32 cons;
2936 	int rx_pkts = 0;
2937 	u8 event = 0;
2938 	struct tx_cmp *txcmp;
2939 
2940 	cpr->has_more_work = 0;
2941 	cpr->had_work_done = 1;
2942 	while (1) {
2943 		u8 cmp_type;
2944 		int rc;
2945 
2946 		cons = RING_CMP(raw_cons);
2947 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2948 
2949 		if (!TX_CMP_VALID(txcmp, raw_cons))
2950 			break;
2951 
2952 		/* The valid test of the entry must be done first before
2953 		 * reading any further.
2954 		 */
2955 		dma_rmb();
2956 		cmp_type = TX_CMP_TYPE(txcmp);
2957 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
2958 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
2959 			u32 opaque = txcmp->tx_cmp_opaque;
2960 			struct bnxt_tx_ring_info *txr;
2961 			u16 tx_freed;
2962 
2963 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
2964 			event |= BNXT_TX_CMP_EVENT;
2965 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
2966 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
2967 			else
2968 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
2969 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
2970 				   bp->tx_ring_mask;
2971 			/* return full budget so NAPI will complete. */
2972 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
2973 				rx_pkts = budget;
2974 				raw_cons = NEXT_RAW_CMP(raw_cons);
2975 				if (budget)
2976 					cpr->has_more_work = 1;
2977 				break;
2978 			}
2979 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
2980 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
2981 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
2982 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2983 			if (likely(budget))
2984 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2985 			else
2986 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2987 							   &event);
2988 			if (likely(rc >= 0))
2989 				rx_pkts += rc;
2990 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2991 			 * the NAPI budget.  Otherwise, we may potentially loop
2992 			 * here forever if we consistently cannot allocate
2993 			 * buffers.
2994 			 */
2995 			else if (rc == -ENOMEM && budget)
2996 				rx_pkts++;
2997 			else if (rc == -EBUSY)	/* partial completion */
2998 				break;
2999 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3000 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3001 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3002 			bnxt_hwrm_handler(bp, txcmp);
3003 		}
3004 		raw_cons = NEXT_RAW_CMP(raw_cons);
3005 
3006 		if (rx_pkts && rx_pkts == budget) {
3007 			cpr->has_more_work = 1;
3008 			break;
3009 		}
3010 	}
3011 
3012 	if (event & BNXT_REDIRECT_EVENT) {
3013 		xdp_do_flush();
3014 		event &= ~BNXT_REDIRECT_EVENT;
3015 	}
3016 
3017 	if (event & BNXT_TX_EVENT) {
3018 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3019 		u16 prod = txr->tx_prod;
3020 
3021 		/* Sync BD data before updating doorbell */
3022 		wmb();
3023 
3024 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3025 		event &= ~BNXT_TX_EVENT;
3026 	}
3027 
3028 	cpr->cp_raw_cons = raw_cons;
3029 	bnapi->events |= event;
3030 	return rx_pkts;
3031 }
3032 
3033 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3034 				  int budget)
3035 {
3036 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3037 		bnapi->tx_int(bp, bnapi, budget);
3038 
3039 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3040 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3041 
3042 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3043 		bnapi->events &= ~BNXT_RX_EVENT;
3044 	}
3045 	if (bnapi->events & BNXT_AGG_EVENT) {
3046 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3047 
3048 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3049 		bnapi->events &= ~BNXT_AGG_EVENT;
3050 	}
3051 }
3052 
3053 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3054 			  int budget)
3055 {
3056 	struct bnxt_napi *bnapi = cpr->bnapi;
3057 	int rx_pkts;
3058 
3059 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3060 
3061 	/* ACK completion ring before freeing tx ring and producing new
3062 	 * buffers in rx/agg rings to prevent overflowing the completion
3063 	 * ring.
3064 	 */
3065 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3066 
3067 	__bnxt_poll_work_done(bp, bnapi, budget);
3068 	return rx_pkts;
3069 }
3070 
3071 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3072 {
3073 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3074 	struct bnxt *bp = bnapi->bp;
3075 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3076 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3077 	struct tx_cmp *txcmp;
3078 	struct rx_cmp_ext *rxcmp1;
3079 	u32 cp_cons, tmp_raw_cons;
3080 	u32 raw_cons = cpr->cp_raw_cons;
3081 	bool flush_xdp = false;
3082 	u32 rx_pkts = 0;
3083 	u8 event = 0;
3084 
3085 	while (1) {
3086 		int rc;
3087 
3088 		cp_cons = RING_CMP(raw_cons);
3089 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3090 
3091 		if (!TX_CMP_VALID(txcmp, raw_cons))
3092 			break;
3093 
3094 		/* The valid test of the entry must be done first before
3095 		 * reading any further.
3096 		 */
3097 		dma_rmb();
3098 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3099 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3100 			cp_cons = RING_CMP(tmp_raw_cons);
3101 			rxcmp1 = (struct rx_cmp_ext *)
3102 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3103 
3104 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3105 				break;
3106 
3107 			/* force an error to recycle the buffer */
3108 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3109 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3110 
3111 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3112 			if (likely(rc == -EIO) && budget)
3113 				rx_pkts++;
3114 			else if (rc == -EBUSY)	/* partial completion */
3115 				break;
3116 			if (event & BNXT_REDIRECT_EVENT)
3117 				flush_xdp = true;
3118 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3119 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3120 			bnxt_hwrm_handler(bp, txcmp);
3121 		} else {
3122 			netdev_err(bp->dev,
3123 				   "Invalid completion received on special ring\n");
3124 		}
3125 		raw_cons = NEXT_RAW_CMP(raw_cons);
3126 
3127 		if (rx_pkts == budget)
3128 			break;
3129 	}
3130 
3131 	cpr->cp_raw_cons = raw_cons;
3132 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3133 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3134 
3135 	if (event & BNXT_AGG_EVENT)
3136 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3137 	if (flush_xdp)
3138 		xdp_do_flush();
3139 
3140 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3141 		napi_complete_done(napi, rx_pkts);
3142 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3143 	}
3144 	return rx_pkts;
3145 }
3146 
3147 static int bnxt_poll(struct napi_struct *napi, int budget)
3148 {
3149 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3150 	struct bnxt *bp = bnapi->bp;
3151 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3152 	int work_done = 0;
3153 
3154 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3155 		napi_complete(napi);
3156 		return 0;
3157 	}
3158 	while (1) {
3159 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3160 
3161 		if (work_done >= budget) {
3162 			if (!budget)
3163 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3164 			break;
3165 		}
3166 
3167 		if (!bnxt_has_work(bp, cpr)) {
3168 			if (napi_complete_done(napi, work_done))
3169 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3170 			break;
3171 		}
3172 	}
3173 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3174 		struct dim_sample dim_sample = {};
3175 
3176 		dim_update_sample(cpr->event_ctr,
3177 				  cpr->rx_packets,
3178 				  cpr->rx_bytes,
3179 				  &dim_sample);
3180 		net_dim(&cpr->dim, &dim_sample);
3181 	}
3182 	return work_done;
3183 }
3184 
3185 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3186 {
3187 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3188 	int i, work_done = 0;
3189 
3190 	for (i = 0; i < cpr->cp_ring_count; i++) {
3191 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3192 
3193 		if (cpr2->had_nqe_notify) {
3194 			work_done += __bnxt_poll_work(bp, cpr2,
3195 						      budget - work_done);
3196 			cpr->has_more_work |= cpr2->has_more_work;
3197 		}
3198 	}
3199 	return work_done;
3200 }
3201 
3202 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3203 				 u64 dbr_type, int budget)
3204 {
3205 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3206 	int i;
3207 
3208 	for (i = 0; i < cpr->cp_ring_count; i++) {
3209 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3210 		struct bnxt_db_info *db;
3211 
3212 		if (cpr2->had_work_done) {
3213 			u32 tgl = 0;
3214 
3215 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3216 				cpr2->had_nqe_notify = 0;
3217 				tgl = cpr2->toggle;
3218 			}
3219 			db = &cpr2->cp_db;
3220 			bnxt_writeq(bp,
3221 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3222 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3223 				    db->doorbell);
3224 			cpr2->had_work_done = 0;
3225 		}
3226 	}
3227 	__bnxt_poll_work_done(bp, bnapi, budget);
3228 }
3229 
3230 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3231 {
3232 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3233 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3234 	struct bnxt_cp_ring_info *cpr_rx;
3235 	u32 raw_cons = cpr->cp_raw_cons;
3236 	struct bnxt *bp = bnapi->bp;
3237 	struct nqe_cn *nqcmp;
3238 	int work_done = 0;
3239 	u32 cons;
3240 
3241 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3242 		napi_complete(napi);
3243 		return 0;
3244 	}
3245 	if (cpr->has_more_work) {
3246 		cpr->has_more_work = 0;
3247 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3248 	}
3249 	while (1) {
3250 		u16 type;
3251 
3252 		cons = RING_CMP(raw_cons);
3253 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3254 
3255 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3256 			if (cpr->has_more_work)
3257 				break;
3258 
3259 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3260 					     budget);
3261 			cpr->cp_raw_cons = raw_cons;
3262 			if (napi_complete_done(napi, work_done))
3263 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3264 						  cpr->cp_raw_cons);
3265 			goto poll_done;
3266 		}
3267 
3268 		/* The valid test of the entry must be done first before
3269 		 * reading any further.
3270 		 */
3271 		dma_rmb();
3272 
3273 		type = le16_to_cpu(nqcmp->type);
3274 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3275 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3276 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3277 			struct bnxt_cp_ring_info *cpr2;
3278 
3279 			/* No more budget for RX work */
3280 			if (budget && work_done >= budget &&
3281 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3282 				break;
3283 
3284 			idx = BNXT_NQ_HDL_IDX(idx);
3285 			cpr2 = &cpr->cp_ring_arr[idx];
3286 			cpr2->had_nqe_notify = 1;
3287 			cpr2->toggle = NQE_CN_TOGGLE(type);
3288 			work_done += __bnxt_poll_work(bp, cpr2,
3289 						      budget - work_done);
3290 			cpr->has_more_work |= cpr2->has_more_work;
3291 		} else {
3292 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3293 		}
3294 		raw_cons = NEXT_RAW_CMP(raw_cons);
3295 	}
3296 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3297 	if (raw_cons != cpr->cp_raw_cons) {
3298 		cpr->cp_raw_cons = raw_cons;
3299 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3300 	}
3301 poll_done:
3302 	cpr_rx = &cpr->cp_ring_arr[0];
3303 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3304 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3305 		struct dim_sample dim_sample = {};
3306 
3307 		dim_update_sample(cpr->event_ctr,
3308 				  cpr_rx->rx_packets,
3309 				  cpr_rx->rx_bytes,
3310 				  &dim_sample);
3311 		net_dim(&cpr->dim, &dim_sample);
3312 	}
3313 	return work_done;
3314 }
3315 
3316 static void bnxt_free_tx_skbs(struct bnxt *bp)
3317 {
3318 	int i, max_idx;
3319 	struct pci_dev *pdev = bp->pdev;
3320 
3321 	if (!bp->tx_ring)
3322 		return;
3323 
3324 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3325 	for (i = 0; i < bp->tx_nr_rings; i++) {
3326 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3327 		int j;
3328 
3329 		if (!txr->tx_buf_ring)
3330 			continue;
3331 
3332 		for (j = 0; j < max_idx;) {
3333 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
3334 			struct sk_buff *skb;
3335 			int k, last;
3336 
3337 			if (i < bp->tx_nr_rings_xdp &&
3338 			    tx_buf->action == XDP_REDIRECT) {
3339 				dma_unmap_single(&pdev->dev,
3340 					dma_unmap_addr(tx_buf, mapping),
3341 					dma_unmap_len(tx_buf, len),
3342 					DMA_TO_DEVICE);
3343 				xdp_return_frame(tx_buf->xdpf);
3344 				tx_buf->action = 0;
3345 				tx_buf->xdpf = NULL;
3346 				j++;
3347 				continue;
3348 			}
3349 
3350 			skb = tx_buf->skb;
3351 			if (!skb) {
3352 				j++;
3353 				continue;
3354 			}
3355 
3356 			tx_buf->skb = NULL;
3357 
3358 			if (tx_buf->is_push) {
3359 				dev_kfree_skb(skb);
3360 				j += 2;
3361 				continue;
3362 			}
3363 
3364 			dma_unmap_single(&pdev->dev,
3365 					 dma_unmap_addr(tx_buf, mapping),
3366 					 skb_headlen(skb),
3367 					 DMA_TO_DEVICE);
3368 
3369 			last = tx_buf->nr_frags;
3370 			j += 2;
3371 			for (k = 0; k < last; k++, j++) {
3372 				int ring_idx = j & bp->tx_ring_mask;
3373 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
3374 
3375 				tx_buf = &txr->tx_buf_ring[ring_idx];
3376 				dma_unmap_page(
3377 					&pdev->dev,
3378 					dma_unmap_addr(tx_buf, mapping),
3379 					skb_frag_size(frag), DMA_TO_DEVICE);
3380 			}
3381 			dev_kfree_skb(skb);
3382 		}
3383 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
3384 	}
3385 }
3386 
3387 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3388 {
3389 	int i, max_idx;
3390 
3391 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3392 
3393 	for (i = 0; i < max_idx; i++) {
3394 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3395 		void *data = rx_buf->data;
3396 
3397 		if (!data)
3398 			continue;
3399 
3400 		rx_buf->data = NULL;
3401 		if (BNXT_RX_PAGE_MODE(bp))
3402 			page_pool_recycle_direct(rxr->page_pool, data);
3403 		else
3404 			page_pool_free_va(rxr->head_pool, data, true);
3405 	}
3406 }
3407 
3408 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3409 {
3410 	int i, max_idx;
3411 
3412 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3413 
3414 	for (i = 0; i < max_idx; i++) {
3415 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3416 		struct page *page = rx_agg_buf->page;
3417 
3418 		if (!page)
3419 			continue;
3420 
3421 		rx_agg_buf->page = NULL;
3422 		__clear_bit(i, rxr->rx_agg_bmap);
3423 
3424 		page_pool_recycle_direct(rxr->page_pool, page);
3425 	}
3426 }
3427 
3428 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3429 					struct bnxt_rx_ring_info *rxr)
3430 {
3431 	int i;
3432 
3433 	for (i = 0; i < bp->max_tpa; i++) {
3434 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3435 		u8 *data = tpa_info->data;
3436 
3437 		if (!data)
3438 			continue;
3439 
3440 		tpa_info->data = NULL;
3441 		page_pool_free_va(rxr->head_pool, data, false);
3442 	}
3443 }
3444 
3445 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3446 				       struct bnxt_rx_ring_info *rxr)
3447 {
3448 	struct bnxt_tpa_idx_map *map;
3449 
3450 	if (!rxr->rx_tpa)
3451 		goto skip_rx_tpa_free;
3452 
3453 	bnxt_free_one_tpa_info_data(bp, rxr);
3454 
3455 skip_rx_tpa_free:
3456 	if (!rxr->rx_buf_ring)
3457 		goto skip_rx_buf_free;
3458 
3459 	bnxt_free_one_rx_ring(bp, rxr);
3460 
3461 skip_rx_buf_free:
3462 	if (!rxr->rx_agg_ring)
3463 		goto skip_rx_agg_free;
3464 
3465 	bnxt_free_one_rx_agg_ring(bp, rxr);
3466 
3467 skip_rx_agg_free:
3468 	map = rxr->rx_tpa_idx_map;
3469 	if (map)
3470 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3471 }
3472 
3473 static void bnxt_free_rx_skbs(struct bnxt *bp)
3474 {
3475 	int i;
3476 
3477 	if (!bp->rx_ring)
3478 		return;
3479 
3480 	for (i = 0; i < bp->rx_nr_rings; i++)
3481 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3482 }
3483 
3484 static void bnxt_free_skbs(struct bnxt *bp)
3485 {
3486 	bnxt_free_tx_skbs(bp);
3487 	bnxt_free_rx_skbs(bp);
3488 }
3489 
3490 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3491 {
3492 	u8 init_val = ctxm->init_value;
3493 	u16 offset = ctxm->init_offset;
3494 	u8 *p2 = p;
3495 	int i;
3496 
3497 	if (!init_val)
3498 		return;
3499 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3500 		memset(p, init_val, len);
3501 		return;
3502 	}
3503 	for (i = 0; i < len; i += ctxm->entry_size)
3504 		*(p2 + i + offset) = init_val;
3505 }
3506 
3507 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3508 			       void *buf, size_t offset, size_t head,
3509 			       size_t tail)
3510 {
3511 	int i, head_page, start_idx, source_offset;
3512 	size_t len, rem_len, total_len, max_bytes;
3513 
3514 	head_page = head / rmem->page_size;
3515 	source_offset = head % rmem->page_size;
3516 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3517 	if (!total_len)
3518 		total_len = MAX_CTX_BYTES;
3519 	start_idx = head_page % MAX_CTX_PAGES;
3520 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3521 		    source_offset;
3522 	total_len = min(total_len, max_bytes);
3523 	rem_len = total_len;
3524 
3525 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3526 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3527 		if (buf)
3528 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3529 			       len);
3530 		offset += len;
3531 		rem_len -= len;
3532 	}
3533 	return total_len;
3534 }
3535 
3536 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3537 {
3538 	struct pci_dev *pdev = bp->pdev;
3539 	int i;
3540 
3541 	if (!rmem->pg_arr)
3542 		goto skip_pages;
3543 
3544 	for (i = 0; i < rmem->nr_pages; i++) {
3545 		if (!rmem->pg_arr[i])
3546 			continue;
3547 
3548 		dma_free_coherent(&pdev->dev, rmem->page_size,
3549 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3550 
3551 		rmem->pg_arr[i] = NULL;
3552 	}
3553 skip_pages:
3554 	if (rmem->pg_tbl) {
3555 		size_t pg_tbl_size = rmem->nr_pages * 8;
3556 
3557 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3558 			pg_tbl_size = rmem->page_size;
3559 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3560 				  rmem->pg_tbl, rmem->pg_tbl_map);
3561 		rmem->pg_tbl = NULL;
3562 	}
3563 	if (rmem->vmem_size && *rmem->vmem) {
3564 		vfree(*rmem->vmem);
3565 		*rmem->vmem = NULL;
3566 	}
3567 }
3568 
3569 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3570 {
3571 	struct pci_dev *pdev = bp->pdev;
3572 	u64 valid_bit = 0;
3573 	int i;
3574 
3575 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3576 		valid_bit = PTU_PTE_VALID;
3577 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3578 		size_t pg_tbl_size = rmem->nr_pages * 8;
3579 
3580 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3581 			pg_tbl_size = rmem->page_size;
3582 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3583 						  &rmem->pg_tbl_map,
3584 						  GFP_KERNEL);
3585 		if (!rmem->pg_tbl)
3586 			return -ENOMEM;
3587 	}
3588 
3589 	for (i = 0; i < rmem->nr_pages; i++) {
3590 		u64 extra_bits = valid_bit;
3591 
3592 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3593 						     rmem->page_size,
3594 						     &rmem->dma_arr[i],
3595 						     GFP_KERNEL);
3596 		if (!rmem->pg_arr[i])
3597 			return -ENOMEM;
3598 
3599 		if (rmem->ctx_mem)
3600 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3601 					  rmem->page_size);
3602 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3603 			if (i == rmem->nr_pages - 2 &&
3604 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3605 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3606 			else if (i == rmem->nr_pages - 1 &&
3607 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3608 				extra_bits |= PTU_PTE_LAST;
3609 			rmem->pg_tbl[i] =
3610 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3611 		}
3612 	}
3613 
3614 	if (rmem->vmem_size) {
3615 		*rmem->vmem = vzalloc(rmem->vmem_size);
3616 		if (!(*rmem->vmem))
3617 			return -ENOMEM;
3618 	}
3619 	return 0;
3620 }
3621 
3622 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3623 				   struct bnxt_rx_ring_info *rxr)
3624 {
3625 	int i;
3626 
3627 	kfree(rxr->rx_tpa_idx_map);
3628 	rxr->rx_tpa_idx_map = NULL;
3629 	if (rxr->rx_tpa) {
3630 		for (i = 0; i < bp->max_tpa; i++) {
3631 			kfree(rxr->rx_tpa[i].agg_arr);
3632 			rxr->rx_tpa[i].agg_arr = NULL;
3633 		}
3634 	}
3635 	kfree(rxr->rx_tpa);
3636 	rxr->rx_tpa = NULL;
3637 }
3638 
3639 static void bnxt_free_tpa_info(struct bnxt *bp)
3640 {
3641 	int i;
3642 
3643 	for (i = 0; i < bp->rx_nr_rings; i++) {
3644 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3645 
3646 		bnxt_free_one_tpa_info(bp, rxr);
3647 	}
3648 }
3649 
3650 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3651 				   struct bnxt_rx_ring_info *rxr)
3652 {
3653 	struct rx_agg_cmp *agg;
3654 	int i;
3655 
3656 	rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3657 			      GFP_KERNEL);
3658 	if (!rxr->rx_tpa)
3659 		return -ENOMEM;
3660 
3661 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3662 		return 0;
3663 	for (i = 0; i < bp->max_tpa; i++) {
3664 		agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3665 		if (!agg)
3666 			return -ENOMEM;
3667 		rxr->rx_tpa[i].agg_arr = agg;
3668 	}
3669 	rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3670 				      GFP_KERNEL);
3671 	if (!rxr->rx_tpa_idx_map)
3672 		return -ENOMEM;
3673 
3674 	return 0;
3675 }
3676 
3677 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3678 {
3679 	int i, rc;
3680 
3681 	bp->max_tpa = MAX_TPA;
3682 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3683 		if (!bp->max_tpa_v2)
3684 			return 0;
3685 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3686 	}
3687 
3688 	for (i = 0; i < bp->rx_nr_rings; i++) {
3689 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3690 
3691 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3692 		if (rc)
3693 			return rc;
3694 	}
3695 	return 0;
3696 }
3697 
3698 static void bnxt_free_rx_rings(struct bnxt *bp)
3699 {
3700 	int i;
3701 
3702 	if (!bp->rx_ring)
3703 		return;
3704 
3705 	bnxt_free_tpa_info(bp);
3706 	for (i = 0; i < bp->rx_nr_rings; i++) {
3707 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3708 		struct bnxt_ring_struct *ring;
3709 
3710 		if (rxr->xdp_prog)
3711 			bpf_prog_put(rxr->xdp_prog);
3712 
3713 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3714 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3715 
3716 		page_pool_destroy(rxr->page_pool);
3717 		if (bnxt_separate_head_pool())
3718 			page_pool_destroy(rxr->head_pool);
3719 		rxr->page_pool = rxr->head_pool = NULL;
3720 
3721 		kfree(rxr->rx_agg_bmap);
3722 		rxr->rx_agg_bmap = NULL;
3723 
3724 		ring = &rxr->rx_ring_struct;
3725 		bnxt_free_ring(bp, &ring->ring_mem);
3726 
3727 		ring = &rxr->rx_agg_ring_struct;
3728 		bnxt_free_ring(bp, &ring->ring_mem);
3729 	}
3730 }
3731 
3732 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3733 				   struct bnxt_rx_ring_info *rxr,
3734 				   int numa_node)
3735 {
3736 	struct page_pool_params pp = { 0 };
3737 	struct page_pool *pool;
3738 
3739 	pp.pool_size = bp->rx_agg_ring_size;
3740 	if (BNXT_RX_PAGE_MODE(bp))
3741 		pp.pool_size += bp->rx_ring_size;
3742 	pp.nid = numa_node;
3743 	pp.napi = &rxr->bnapi->napi;
3744 	pp.netdev = bp->dev;
3745 	pp.dev = &bp->pdev->dev;
3746 	pp.dma_dir = bp->rx_dir;
3747 	pp.max_len = PAGE_SIZE;
3748 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3749 
3750 	pool = page_pool_create(&pp);
3751 	if (IS_ERR(pool))
3752 		return PTR_ERR(pool);
3753 	rxr->page_pool = pool;
3754 
3755 	if (bnxt_separate_head_pool()) {
3756 		pp.pool_size = max(bp->rx_ring_size, 1024);
3757 		pool = page_pool_create(&pp);
3758 		if (IS_ERR(pool))
3759 			goto err_destroy_pp;
3760 	}
3761 	rxr->head_pool = pool;
3762 
3763 	return 0;
3764 
3765 err_destroy_pp:
3766 	page_pool_destroy(rxr->page_pool);
3767 	rxr->page_pool = NULL;
3768 	return PTR_ERR(pool);
3769 }
3770 
3771 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3772 {
3773 	u16 mem_size;
3774 
3775 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3776 	mem_size = rxr->rx_agg_bmap_size / 8;
3777 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3778 	if (!rxr->rx_agg_bmap)
3779 		return -ENOMEM;
3780 
3781 	return 0;
3782 }
3783 
3784 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3785 {
3786 	int numa_node = dev_to_node(&bp->pdev->dev);
3787 	int i, rc = 0, agg_rings = 0, cpu;
3788 
3789 	if (!bp->rx_ring)
3790 		return -ENOMEM;
3791 
3792 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3793 		agg_rings = 1;
3794 
3795 	for (i = 0; i < bp->rx_nr_rings; i++) {
3796 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3797 		struct bnxt_ring_struct *ring;
3798 		int cpu_node;
3799 
3800 		ring = &rxr->rx_ring_struct;
3801 
3802 		cpu = cpumask_local_spread(i, numa_node);
3803 		cpu_node = cpu_to_node(cpu);
3804 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3805 			   i, cpu_node);
3806 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3807 		if (rc)
3808 			return rc;
3809 
3810 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3811 		if (rc < 0)
3812 			return rc;
3813 
3814 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3815 						MEM_TYPE_PAGE_POOL,
3816 						rxr->page_pool);
3817 		if (rc) {
3818 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3819 			return rc;
3820 		}
3821 
3822 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3823 		if (rc)
3824 			return rc;
3825 
3826 		ring->grp_idx = i;
3827 		if (agg_rings) {
3828 			ring = &rxr->rx_agg_ring_struct;
3829 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3830 			if (rc)
3831 				return rc;
3832 
3833 			ring->grp_idx = i;
3834 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3835 			if (rc)
3836 				return rc;
3837 		}
3838 	}
3839 	if (bp->flags & BNXT_FLAG_TPA)
3840 		rc = bnxt_alloc_tpa_info(bp);
3841 	return rc;
3842 }
3843 
3844 static void bnxt_free_tx_rings(struct bnxt *bp)
3845 {
3846 	int i;
3847 	struct pci_dev *pdev = bp->pdev;
3848 
3849 	if (!bp->tx_ring)
3850 		return;
3851 
3852 	for (i = 0; i < bp->tx_nr_rings; i++) {
3853 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3854 		struct bnxt_ring_struct *ring;
3855 
3856 		if (txr->tx_push) {
3857 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3858 					  txr->tx_push, txr->tx_push_mapping);
3859 			txr->tx_push = NULL;
3860 		}
3861 
3862 		ring = &txr->tx_ring_struct;
3863 
3864 		bnxt_free_ring(bp, &ring->ring_mem);
3865 	}
3866 }
3867 
3868 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3869 	((tc) * (bp)->tx_nr_rings_per_tc)
3870 
3871 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3872 	((tx) % (bp)->tx_nr_rings_per_tc)
3873 
3874 #define BNXT_RING_TO_TC(bp, tx)		\
3875 	((tx) / (bp)->tx_nr_rings_per_tc)
3876 
3877 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3878 {
3879 	int i, j, rc;
3880 	struct pci_dev *pdev = bp->pdev;
3881 
3882 	bp->tx_push_size = 0;
3883 	if (bp->tx_push_thresh) {
3884 		int push_size;
3885 
3886 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3887 					bp->tx_push_thresh);
3888 
3889 		if (push_size > 256) {
3890 			push_size = 0;
3891 			bp->tx_push_thresh = 0;
3892 		}
3893 
3894 		bp->tx_push_size = push_size;
3895 	}
3896 
3897 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3898 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3899 		struct bnxt_ring_struct *ring;
3900 		u8 qidx;
3901 
3902 		ring = &txr->tx_ring_struct;
3903 
3904 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3905 		if (rc)
3906 			return rc;
3907 
3908 		ring->grp_idx = txr->bnapi->index;
3909 		if (bp->tx_push_size) {
3910 			dma_addr_t mapping;
3911 
3912 			/* One pre-allocated DMA buffer to backup
3913 			 * TX push operation
3914 			 */
3915 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3916 						bp->tx_push_size,
3917 						&txr->tx_push_mapping,
3918 						GFP_KERNEL);
3919 
3920 			if (!txr->tx_push)
3921 				return -ENOMEM;
3922 
3923 			mapping = txr->tx_push_mapping +
3924 				sizeof(struct tx_push_bd);
3925 			txr->data_mapping = cpu_to_le64(mapping);
3926 		}
3927 		qidx = bp->tc_to_qidx[j];
3928 		ring->queue_id = bp->q_info[qidx].queue_id;
3929 		spin_lock_init(&txr->xdp_tx_lock);
3930 		if (i < bp->tx_nr_rings_xdp)
3931 			continue;
3932 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
3933 			j++;
3934 	}
3935 	return 0;
3936 }
3937 
3938 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3939 {
3940 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3941 
3942 	kfree(cpr->cp_desc_ring);
3943 	cpr->cp_desc_ring = NULL;
3944 	ring->ring_mem.pg_arr = NULL;
3945 	kfree(cpr->cp_desc_mapping);
3946 	cpr->cp_desc_mapping = NULL;
3947 	ring->ring_mem.dma_arr = NULL;
3948 }
3949 
3950 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3951 {
3952 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3953 	if (!cpr->cp_desc_ring)
3954 		return -ENOMEM;
3955 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3956 				       GFP_KERNEL);
3957 	if (!cpr->cp_desc_mapping)
3958 		return -ENOMEM;
3959 	return 0;
3960 }
3961 
3962 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3963 {
3964 	int i;
3965 
3966 	if (!bp->bnapi)
3967 		return;
3968 	for (i = 0; i < bp->cp_nr_rings; i++) {
3969 		struct bnxt_napi *bnapi = bp->bnapi[i];
3970 
3971 		if (!bnapi)
3972 			continue;
3973 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3974 	}
3975 }
3976 
3977 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3978 {
3979 	int i, n = bp->cp_nr_pages;
3980 
3981 	for (i = 0; i < bp->cp_nr_rings; i++) {
3982 		struct bnxt_napi *bnapi = bp->bnapi[i];
3983 		int rc;
3984 
3985 		if (!bnapi)
3986 			continue;
3987 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3988 		if (rc)
3989 			return rc;
3990 	}
3991 	return 0;
3992 }
3993 
3994 static void bnxt_free_cp_rings(struct bnxt *bp)
3995 {
3996 	int i;
3997 
3998 	if (!bp->bnapi)
3999 		return;
4000 
4001 	for (i = 0; i < bp->cp_nr_rings; i++) {
4002 		struct bnxt_napi *bnapi = bp->bnapi[i];
4003 		struct bnxt_cp_ring_info *cpr;
4004 		struct bnxt_ring_struct *ring;
4005 		int j;
4006 
4007 		if (!bnapi)
4008 			continue;
4009 
4010 		cpr = &bnapi->cp_ring;
4011 		ring = &cpr->cp_ring_struct;
4012 
4013 		bnxt_free_ring(bp, &ring->ring_mem);
4014 
4015 		if (!cpr->cp_ring_arr)
4016 			continue;
4017 
4018 		for (j = 0; j < cpr->cp_ring_count; j++) {
4019 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4020 
4021 			ring = &cpr2->cp_ring_struct;
4022 			bnxt_free_ring(bp, &ring->ring_mem);
4023 			bnxt_free_cp_arrays(cpr2);
4024 		}
4025 		kfree(cpr->cp_ring_arr);
4026 		cpr->cp_ring_arr = NULL;
4027 		cpr->cp_ring_count = 0;
4028 	}
4029 }
4030 
4031 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4032 				  struct bnxt_cp_ring_info *cpr)
4033 {
4034 	struct bnxt_ring_mem_info *rmem;
4035 	struct bnxt_ring_struct *ring;
4036 	int rc;
4037 
4038 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4039 	if (rc) {
4040 		bnxt_free_cp_arrays(cpr);
4041 		return -ENOMEM;
4042 	}
4043 	ring = &cpr->cp_ring_struct;
4044 	rmem = &ring->ring_mem;
4045 	rmem->nr_pages = bp->cp_nr_pages;
4046 	rmem->page_size = HW_CMPD_RING_SIZE;
4047 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4048 	rmem->dma_arr = cpr->cp_desc_mapping;
4049 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4050 	rc = bnxt_alloc_ring(bp, rmem);
4051 	if (rc) {
4052 		bnxt_free_ring(bp, rmem);
4053 		bnxt_free_cp_arrays(cpr);
4054 	}
4055 	return rc;
4056 }
4057 
4058 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4059 {
4060 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4061 	int i, j, rc, ulp_msix;
4062 	int tcs = bp->num_tc;
4063 
4064 	if (!tcs)
4065 		tcs = 1;
4066 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4067 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4068 		struct bnxt_napi *bnapi = bp->bnapi[i];
4069 		struct bnxt_cp_ring_info *cpr, *cpr2;
4070 		struct bnxt_ring_struct *ring;
4071 		int cp_count = 0, k;
4072 		int rx = 0, tx = 0;
4073 
4074 		if (!bnapi)
4075 			continue;
4076 
4077 		cpr = &bnapi->cp_ring;
4078 		cpr->bnapi = bnapi;
4079 		ring = &cpr->cp_ring_struct;
4080 
4081 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4082 		if (rc)
4083 			return rc;
4084 
4085 		ring->map_idx = ulp_msix + i;
4086 
4087 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4088 			continue;
4089 
4090 		if (i < bp->rx_nr_rings) {
4091 			cp_count++;
4092 			rx = 1;
4093 		}
4094 		if (i < bp->tx_nr_rings_xdp) {
4095 			cp_count++;
4096 			tx = 1;
4097 		} else if ((sh && i < bp->tx_nr_rings) ||
4098 			 (!sh && i >= bp->rx_nr_rings)) {
4099 			cp_count += tcs;
4100 			tx = 1;
4101 		}
4102 
4103 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
4104 					   GFP_KERNEL);
4105 		if (!cpr->cp_ring_arr)
4106 			return -ENOMEM;
4107 		cpr->cp_ring_count = cp_count;
4108 
4109 		for (k = 0; k < cp_count; k++) {
4110 			cpr2 = &cpr->cp_ring_arr[k];
4111 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4112 			if (rc)
4113 				return rc;
4114 			cpr2->bnapi = bnapi;
4115 			cpr2->sw_stats = cpr->sw_stats;
4116 			cpr2->cp_idx = k;
4117 			if (!k && rx) {
4118 				bp->rx_ring[i].rx_cpr = cpr2;
4119 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4120 			} else {
4121 				int n, tc = k - rx;
4122 
4123 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4124 				bp->tx_ring[n].tx_cpr = cpr2;
4125 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4126 			}
4127 		}
4128 		if (tx)
4129 			j++;
4130 	}
4131 	return 0;
4132 }
4133 
4134 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4135 				     struct bnxt_rx_ring_info *rxr)
4136 {
4137 	struct bnxt_ring_mem_info *rmem;
4138 	struct bnxt_ring_struct *ring;
4139 
4140 	ring = &rxr->rx_ring_struct;
4141 	rmem = &ring->ring_mem;
4142 	rmem->nr_pages = bp->rx_nr_pages;
4143 	rmem->page_size = HW_RXBD_RING_SIZE;
4144 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4145 	rmem->dma_arr = rxr->rx_desc_mapping;
4146 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4147 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4148 
4149 	ring = &rxr->rx_agg_ring_struct;
4150 	rmem = &ring->ring_mem;
4151 	rmem->nr_pages = bp->rx_agg_nr_pages;
4152 	rmem->page_size = HW_RXBD_RING_SIZE;
4153 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4154 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4155 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4156 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4157 }
4158 
4159 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4160 				      struct bnxt_rx_ring_info *rxr)
4161 {
4162 	struct bnxt_ring_mem_info *rmem;
4163 	struct bnxt_ring_struct *ring;
4164 	int i;
4165 
4166 	rxr->page_pool->p.napi = NULL;
4167 	rxr->page_pool = NULL;
4168 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4169 
4170 	ring = &rxr->rx_ring_struct;
4171 	rmem = &ring->ring_mem;
4172 	rmem->pg_tbl = NULL;
4173 	rmem->pg_tbl_map = 0;
4174 	for (i = 0; i < rmem->nr_pages; i++) {
4175 		rmem->pg_arr[i] = NULL;
4176 		rmem->dma_arr[i] = 0;
4177 	}
4178 	*rmem->vmem = NULL;
4179 
4180 	ring = &rxr->rx_agg_ring_struct;
4181 	rmem = &ring->ring_mem;
4182 	rmem->pg_tbl = NULL;
4183 	rmem->pg_tbl_map = 0;
4184 	for (i = 0; i < rmem->nr_pages; i++) {
4185 		rmem->pg_arr[i] = NULL;
4186 		rmem->dma_arr[i] = 0;
4187 	}
4188 	*rmem->vmem = NULL;
4189 }
4190 
4191 static void bnxt_init_ring_struct(struct bnxt *bp)
4192 {
4193 	int i, j;
4194 
4195 	for (i = 0; i < bp->cp_nr_rings; i++) {
4196 		struct bnxt_napi *bnapi = bp->bnapi[i];
4197 		struct bnxt_ring_mem_info *rmem;
4198 		struct bnxt_cp_ring_info *cpr;
4199 		struct bnxt_rx_ring_info *rxr;
4200 		struct bnxt_tx_ring_info *txr;
4201 		struct bnxt_ring_struct *ring;
4202 
4203 		if (!bnapi)
4204 			continue;
4205 
4206 		cpr = &bnapi->cp_ring;
4207 		ring = &cpr->cp_ring_struct;
4208 		rmem = &ring->ring_mem;
4209 		rmem->nr_pages = bp->cp_nr_pages;
4210 		rmem->page_size = HW_CMPD_RING_SIZE;
4211 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4212 		rmem->dma_arr = cpr->cp_desc_mapping;
4213 		rmem->vmem_size = 0;
4214 
4215 		rxr = bnapi->rx_ring;
4216 		if (!rxr)
4217 			goto skip_rx;
4218 
4219 		ring = &rxr->rx_ring_struct;
4220 		rmem = &ring->ring_mem;
4221 		rmem->nr_pages = bp->rx_nr_pages;
4222 		rmem->page_size = HW_RXBD_RING_SIZE;
4223 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4224 		rmem->dma_arr = rxr->rx_desc_mapping;
4225 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4226 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4227 
4228 		ring = &rxr->rx_agg_ring_struct;
4229 		rmem = &ring->ring_mem;
4230 		rmem->nr_pages = bp->rx_agg_nr_pages;
4231 		rmem->page_size = HW_RXBD_RING_SIZE;
4232 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4233 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4234 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4235 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4236 
4237 skip_rx:
4238 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4239 			ring = &txr->tx_ring_struct;
4240 			rmem = &ring->ring_mem;
4241 			rmem->nr_pages = bp->tx_nr_pages;
4242 			rmem->page_size = HW_TXBD_RING_SIZE;
4243 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4244 			rmem->dma_arr = txr->tx_desc_mapping;
4245 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4246 			rmem->vmem = (void **)&txr->tx_buf_ring;
4247 		}
4248 	}
4249 }
4250 
4251 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4252 {
4253 	int i;
4254 	u32 prod;
4255 	struct rx_bd **rx_buf_ring;
4256 
4257 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4258 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4259 		int j;
4260 		struct rx_bd *rxbd;
4261 
4262 		rxbd = rx_buf_ring[i];
4263 		if (!rxbd)
4264 			continue;
4265 
4266 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4267 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4268 			rxbd->rx_bd_opaque = prod;
4269 		}
4270 	}
4271 }
4272 
4273 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4274 				       struct bnxt_rx_ring_info *rxr,
4275 				       int ring_nr)
4276 {
4277 	u32 prod;
4278 	int i;
4279 
4280 	prod = rxr->rx_prod;
4281 	for (i = 0; i < bp->rx_ring_size; i++) {
4282 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4283 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4284 				    ring_nr, i, bp->rx_ring_size);
4285 			break;
4286 		}
4287 		prod = NEXT_RX(prod);
4288 	}
4289 	rxr->rx_prod = prod;
4290 }
4291 
4292 static void bnxt_alloc_one_rx_ring_page(struct bnxt *bp,
4293 					struct bnxt_rx_ring_info *rxr,
4294 					int ring_nr)
4295 {
4296 	u32 prod;
4297 	int i;
4298 
4299 	prod = rxr->rx_agg_prod;
4300 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4301 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
4302 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4303 				    ring_nr, i, bp->rx_ring_size);
4304 			break;
4305 		}
4306 		prod = NEXT_RX_AGG(prod);
4307 	}
4308 	rxr->rx_agg_prod = prod;
4309 }
4310 
4311 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4312 					struct bnxt_rx_ring_info *rxr)
4313 {
4314 	dma_addr_t mapping;
4315 	u8 *data;
4316 	int i;
4317 
4318 	for (i = 0; i < bp->max_tpa; i++) {
4319 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4320 					    GFP_KERNEL);
4321 		if (!data)
4322 			return -ENOMEM;
4323 
4324 		rxr->rx_tpa[i].data = data;
4325 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4326 		rxr->rx_tpa[i].mapping = mapping;
4327 	}
4328 
4329 	return 0;
4330 }
4331 
4332 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4333 {
4334 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4335 	int rc;
4336 
4337 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4338 
4339 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4340 		return 0;
4341 
4342 	bnxt_alloc_one_rx_ring_page(bp, rxr, ring_nr);
4343 
4344 	if (rxr->rx_tpa) {
4345 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4346 		if (rc)
4347 			return rc;
4348 	}
4349 	return 0;
4350 }
4351 
4352 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4353 				       struct bnxt_rx_ring_info *rxr)
4354 {
4355 	struct bnxt_ring_struct *ring;
4356 	u32 type;
4357 
4358 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4359 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4360 
4361 	if (NET_IP_ALIGN == 2)
4362 		type |= RX_BD_FLAGS_SOP;
4363 
4364 	ring = &rxr->rx_ring_struct;
4365 	bnxt_init_rxbd_pages(ring, type);
4366 	ring->fw_ring_id = INVALID_HW_RING_ID;
4367 }
4368 
4369 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4370 					   struct bnxt_rx_ring_info *rxr)
4371 {
4372 	struct bnxt_ring_struct *ring;
4373 	u32 type;
4374 
4375 	ring = &rxr->rx_agg_ring_struct;
4376 	ring->fw_ring_id = INVALID_HW_RING_ID;
4377 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4378 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4379 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4380 
4381 		bnxt_init_rxbd_pages(ring, type);
4382 	}
4383 }
4384 
4385 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4386 {
4387 	struct bnxt_rx_ring_info *rxr;
4388 
4389 	rxr = &bp->rx_ring[ring_nr];
4390 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4391 
4392 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4393 			     &rxr->bnapi->napi);
4394 
4395 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4396 		bpf_prog_add(bp->xdp_prog, 1);
4397 		rxr->xdp_prog = bp->xdp_prog;
4398 	}
4399 
4400 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4401 
4402 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4403 }
4404 
4405 static void bnxt_init_cp_rings(struct bnxt *bp)
4406 {
4407 	int i, j;
4408 
4409 	for (i = 0; i < bp->cp_nr_rings; i++) {
4410 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4411 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4412 
4413 		ring->fw_ring_id = INVALID_HW_RING_ID;
4414 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4415 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4416 		if (!cpr->cp_ring_arr)
4417 			continue;
4418 		for (j = 0; j < cpr->cp_ring_count; j++) {
4419 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4420 
4421 			ring = &cpr2->cp_ring_struct;
4422 			ring->fw_ring_id = INVALID_HW_RING_ID;
4423 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4424 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4425 		}
4426 	}
4427 }
4428 
4429 static int bnxt_init_rx_rings(struct bnxt *bp)
4430 {
4431 	int i, rc = 0;
4432 
4433 	if (BNXT_RX_PAGE_MODE(bp)) {
4434 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4435 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4436 	} else {
4437 		bp->rx_offset = BNXT_RX_OFFSET;
4438 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4439 	}
4440 
4441 	for (i = 0; i < bp->rx_nr_rings; i++) {
4442 		rc = bnxt_init_one_rx_ring(bp, i);
4443 		if (rc)
4444 			break;
4445 	}
4446 
4447 	return rc;
4448 }
4449 
4450 static int bnxt_init_tx_rings(struct bnxt *bp)
4451 {
4452 	u16 i;
4453 
4454 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4455 				   BNXT_MIN_TX_DESC_CNT);
4456 
4457 	for (i = 0; i < bp->tx_nr_rings; i++) {
4458 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4459 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4460 
4461 		ring->fw_ring_id = INVALID_HW_RING_ID;
4462 
4463 		if (i >= bp->tx_nr_rings_xdp)
4464 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4465 					     NETDEV_QUEUE_TYPE_TX,
4466 					     &txr->bnapi->napi);
4467 	}
4468 
4469 	return 0;
4470 }
4471 
4472 static void bnxt_free_ring_grps(struct bnxt *bp)
4473 {
4474 	kfree(bp->grp_info);
4475 	bp->grp_info = NULL;
4476 }
4477 
4478 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4479 {
4480 	int i;
4481 
4482 	if (irq_re_init) {
4483 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4484 				       sizeof(struct bnxt_ring_grp_info),
4485 				       GFP_KERNEL);
4486 		if (!bp->grp_info)
4487 			return -ENOMEM;
4488 	}
4489 	for (i = 0; i < bp->cp_nr_rings; i++) {
4490 		if (irq_re_init)
4491 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4492 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4493 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4494 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4495 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4496 	}
4497 	return 0;
4498 }
4499 
4500 static void bnxt_free_vnics(struct bnxt *bp)
4501 {
4502 	kfree(bp->vnic_info);
4503 	bp->vnic_info = NULL;
4504 	bp->nr_vnics = 0;
4505 }
4506 
4507 static int bnxt_alloc_vnics(struct bnxt *bp)
4508 {
4509 	int num_vnics = 1;
4510 
4511 #ifdef CONFIG_RFS_ACCEL
4512 	if (bp->flags & BNXT_FLAG_RFS) {
4513 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4514 			num_vnics++;
4515 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4516 			num_vnics += bp->rx_nr_rings;
4517 	}
4518 #endif
4519 
4520 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4521 		num_vnics++;
4522 
4523 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4524 				GFP_KERNEL);
4525 	if (!bp->vnic_info)
4526 		return -ENOMEM;
4527 
4528 	bp->nr_vnics = num_vnics;
4529 	return 0;
4530 }
4531 
4532 static void bnxt_init_vnics(struct bnxt *bp)
4533 {
4534 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4535 	int i;
4536 
4537 	for (i = 0; i < bp->nr_vnics; i++) {
4538 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4539 		int j;
4540 
4541 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4542 		vnic->vnic_id = i;
4543 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4544 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4545 
4546 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4547 
4548 		if (bp->vnic_info[i].rss_hash_key) {
4549 			if (i == BNXT_VNIC_DEFAULT) {
4550 				u8 *key = (void *)vnic->rss_hash_key;
4551 				int k;
4552 
4553 				if (!bp->rss_hash_key_valid &&
4554 				    !bp->rss_hash_key_updated) {
4555 					get_random_bytes(bp->rss_hash_key,
4556 							 HW_HASH_KEY_SIZE);
4557 					bp->rss_hash_key_updated = true;
4558 				}
4559 
4560 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4561 				       HW_HASH_KEY_SIZE);
4562 
4563 				if (!bp->rss_hash_key_updated)
4564 					continue;
4565 
4566 				bp->rss_hash_key_updated = false;
4567 				bp->rss_hash_key_valid = true;
4568 
4569 				bp->toeplitz_prefix = 0;
4570 				for (k = 0; k < 8; k++) {
4571 					bp->toeplitz_prefix <<= 8;
4572 					bp->toeplitz_prefix |= key[k];
4573 				}
4574 			} else {
4575 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4576 				       HW_HASH_KEY_SIZE);
4577 			}
4578 		}
4579 	}
4580 }
4581 
4582 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4583 {
4584 	int pages;
4585 
4586 	pages = ring_size / desc_per_pg;
4587 
4588 	if (!pages)
4589 		return 1;
4590 
4591 	pages++;
4592 
4593 	while (pages & (pages - 1))
4594 		pages++;
4595 
4596 	return pages;
4597 }
4598 
4599 void bnxt_set_tpa_flags(struct bnxt *bp)
4600 {
4601 	bp->flags &= ~BNXT_FLAG_TPA;
4602 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4603 		return;
4604 	if (bp->dev->features & NETIF_F_LRO)
4605 		bp->flags |= BNXT_FLAG_LRO;
4606 	else if (bp->dev->features & NETIF_F_GRO_HW)
4607 		bp->flags |= BNXT_FLAG_GRO;
4608 }
4609 
4610 static void bnxt_init_ring_params(struct bnxt *bp)
4611 {
4612 	bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK;
4613 	bp->dev->ethtool->hds_thresh = BNXT_DEFAULT_RX_COPYBREAK;
4614 }
4615 
4616 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4617  * be set on entry.
4618  */
4619 void bnxt_set_ring_params(struct bnxt *bp)
4620 {
4621 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4622 	u32 agg_factor = 0, agg_ring_size = 0;
4623 
4624 	/* 8 for CRC and VLAN */
4625 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4626 
4627 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4628 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4629 
4630 	ring_size = bp->rx_ring_size;
4631 	bp->rx_agg_ring_size = 0;
4632 	bp->rx_agg_nr_pages = 0;
4633 
4634 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4635 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4636 
4637 	bp->flags &= ~BNXT_FLAG_JUMBO;
4638 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4639 		u32 jumbo_factor;
4640 
4641 		bp->flags |= BNXT_FLAG_JUMBO;
4642 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4643 		if (jumbo_factor > agg_factor)
4644 			agg_factor = jumbo_factor;
4645 	}
4646 	if (agg_factor) {
4647 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4648 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4649 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4650 				    bp->rx_ring_size, ring_size);
4651 			bp->rx_ring_size = ring_size;
4652 		}
4653 		agg_ring_size = ring_size * agg_factor;
4654 
4655 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4656 							RX_DESC_CNT);
4657 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4658 			u32 tmp = agg_ring_size;
4659 
4660 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4661 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4662 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4663 				    tmp, agg_ring_size);
4664 		}
4665 		bp->rx_agg_ring_size = agg_ring_size;
4666 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4667 
4668 		if (BNXT_RX_PAGE_MODE(bp)) {
4669 			rx_space = PAGE_SIZE;
4670 			rx_size = PAGE_SIZE -
4671 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4672 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4673 		} else {
4674 			rx_size = SKB_DATA_ALIGN(max(BNXT_DEFAULT_RX_COPYBREAK,
4675 						     bp->rx_copybreak) +
4676 						 NET_IP_ALIGN);
4677 			rx_space = rx_size + NET_SKB_PAD +
4678 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4679 		}
4680 	}
4681 
4682 	bp->rx_buf_use_size = rx_size;
4683 	bp->rx_buf_size = rx_space;
4684 
4685 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4686 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4687 
4688 	ring_size = bp->tx_ring_size;
4689 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4690 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4691 
4692 	max_rx_cmpl = bp->rx_ring_size;
4693 	/* MAX TPA needs to be added because TPA_START completions are
4694 	 * immediately recycled, so the TPA completions are not bound by
4695 	 * the RX ring size.
4696 	 */
4697 	if (bp->flags & BNXT_FLAG_TPA)
4698 		max_rx_cmpl += bp->max_tpa;
4699 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4700 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4701 	bp->cp_ring_size = ring_size;
4702 
4703 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4704 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4705 		bp->cp_nr_pages = MAX_CP_PAGES;
4706 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4707 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4708 			    ring_size, bp->cp_ring_size);
4709 	}
4710 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4711 	bp->cp_ring_mask = bp->cp_bit - 1;
4712 }
4713 
4714 /* Changing allocation mode of RX rings.
4715  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4716  */
4717 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4718 {
4719 	struct net_device *dev = bp->dev;
4720 
4721 	if (page_mode) {
4722 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4723 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4724 
4725 		if (bp->xdp_prog->aux->xdp_has_frags)
4726 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4727 		else
4728 			dev->max_mtu =
4729 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4730 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4731 			bp->flags |= BNXT_FLAG_JUMBO;
4732 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4733 		} else {
4734 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4735 			bp->rx_skb_func = bnxt_rx_page_skb;
4736 		}
4737 		bp->rx_dir = DMA_BIDIRECTIONAL;
4738 	} else {
4739 		dev->max_mtu = bp->max_mtu;
4740 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4741 		bp->rx_dir = DMA_FROM_DEVICE;
4742 		bp->rx_skb_func = bnxt_rx_skb;
4743 	}
4744 }
4745 
4746 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4747 {
4748 	__bnxt_set_rx_skb_mode(bp, page_mode);
4749 
4750 	if (!page_mode) {
4751 		int rx, tx;
4752 
4753 		bnxt_get_max_rings(bp, &rx, &tx, true);
4754 		if (rx > 1) {
4755 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
4756 			bp->dev->hw_features |= NETIF_F_LRO;
4757 		}
4758 	}
4759 
4760 	/* Update LRO and GRO_HW availability */
4761 	netdev_update_features(bp->dev);
4762 }
4763 
4764 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4765 {
4766 	int i;
4767 	struct bnxt_vnic_info *vnic;
4768 	struct pci_dev *pdev = bp->pdev;
4769 
4770 	if (!bp->vnic_info)
4771 		return;
4772 
4773 	for (i = 0; i < bp->nr_vnics; i++) {
4774 		vnic = &bp->vnic_info[i];
4775 
4776 		kfree(vnic->fw_grp_ids);
4777 		vnic->fw_grp_ids = NULL;
4778 
4779 		kfree(vnic->uc_list);
4780 		vnic->uc_list = NULL;
4781 
4782 		if (vnic->mc_list) {
4783 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4784 					  vnic->mc_list, vnic->mc_list_mapping);
4785 			vnic->mc_list = NULL;
4786 		}
4787 
4788 		if (vnic->rss_table) {
4789 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4790 					  vnic->rss_table,
4791 					  vnic->rss_table_dma_addr);
4792 			vnic->rss_table = NULL;
4793 		}
4794 
4795 		vnic->rss_hash_key = NULL;
4796 		vnic->flags = 0;
4797 	}
4798 }
4799 
4800 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4801 {
4802 	int i, rc = 0, size;
4803 	struct bnxt_vnic_info *vnic;
4804 	struct pci_dev *pdev = bp->pdev;
4805 	int max_rings;
4806 
4807 	for (i = 0; i < bp->nr_vnics; i++) {
4808 		vnic = &bp->vnic_info[i];
4809 
4810 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4811 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4812 
4813 			if (mem_size > 0) {
4814 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4815 				if (!vnic->uc_list) {
4816 					rc = -ENOMEM;
4817 					goto out;
4818 				}
4819 			}
4820 		}
4821 
4822 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4823 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4824 			vnic->mc_list =
4825 				dma_alloc_coherent(&pdev->dev,
4826 						   vnic->mc_list_size,
4827 						   &vnic->mc_list_mapping,
4828 						   GFP_KERNEL);
4829 			if (!vnic->mc_list) {
4830 				rc = -ENOMEM;
4831 				goto out;
4832 			}
4833 		}
4834 
4835 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4836 			goto vnic_skip_grps;
4837 
4838 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4839 			max_rings = bp->rx_nr_rings;
4840 		else
4841 			max_rings = 1;
4842 
4843 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4844 		if (!vnic->fw_grp_ids) {
4845 			rc = -ENOMEM;
4846 			goto out;
4847 		}
4848 vnic_skip_grps:
4849 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4850 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4851 			continue;
4852 
4853 		/* Allocate rss table and hash key */
4854 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4855 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4856 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4857 
4858 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4859 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4860 						     vnic->rss_table_size,
4861 						     &vnic->rss_table_dma_addr,
4862 						     GFP_KERNEL);
4863 		if (!vnic->rss_table) {
4864 			rc = -ENOMEM;
4865 			goto out;
4866 		}
4867 
4868 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4869 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4870 	}
4871 	return 0;
4872 
4873 out:
4874 	return rc;
4875 }
4876 
4877 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4878 {
4879 	struct bnxt_hwrm_wait_token *token;
4880 
4881 	dma_pool_destroy(bp->hwrm_dma_pool);
4882 	bp->hwrm_dma_pool = NULL;
4883 
4884 	rcu_read_lock();
4885 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4886 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4887 	rcu_read_unlock();
4888 }
4889 
4890 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4891 {
4892 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4893 					    BNXT_HWRM_DMA_SIZE,
4894 					    BNXT_HWRM_DMA_ALIGN, 0);
4895 	if (!bp->hwrm_dma_pool)
4896 		return -ENOMEM;
4897 
4898 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4899 
4900 	return 0;
4901 }
4902 
4903 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4904 {
4905 	kfree(stats->hw_masks);
4906 	stats->hw_masks = NULL;
4907 	kfree(stats->sw_stats);
4908 	stats->sw_stats = NULL;
4909 	if (stats->hw_stats) {
4910 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4911 				  stats->hw_stats_map);
4912 		stats->hw_stats = NULL;
4913 	}
4914 }
4915 
4916 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4917 				bool alloc_masks)
4918 {
4919 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4920 					     &stats->hw_stats_map, GFP_KERNEL);
4921 	if (!stats->hw_stats)
4922 		return -ENOMEM;
4923 
4924 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4925 	if (!stats->sw_stats)
4926 		goto stats_mem_err;
4927 
4928 	if (alloc_masks) {
4929 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4930 		if (!stats->hw_masks)
4931 			goto stats_mem_err;
4932 	}
4933 	return 0;
4934 
4935 stats_mem_err:
4936 	bnxt_free_stats_mem(bp, stats);
4937 	return -ENOMEM;
4938 }
4939 
4940 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4941 {
4942 	int i;
4943 
4944 	for (i = 0; i < count; i++)
4945 		mask_arr[i] = mask;
4946 }
4947 
4948 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4949 {
4950 	int i;
4951 
4952 	for (i = 0; i < count; i++)
4953 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4954 }
4955 
4956 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4957 				    struct bnxt_stats_mem *stats)
4958 {
4959 	struct hwrm_func_qstats_ext_output *resp;
4960 	struct hwrm_func_qstats_ext_input *req;
4961 	__le64 *hw_masks;
4962 	int rc;
4963 
4964 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4965 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4966 		return -EOPNOTSUPP;
4967 
4968 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4969 	if (rc)
4970 		return rc;
4971 
4972 	req->fid = cpu_to_le16(0xffff);
4973 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4974 
4975 	resp = hwrm_req_hold(bp, req);
4976 	rc = hwrm_req_send(bp, req);
4977 	if (!rc) {
4978 		hw_masks = &resp->rx_ucast_pkts;
4979 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4980 	}
4981 	hwrm_req_drop(bp, req);
4982 	return rc;
4983 }
4984 
4985 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4986 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4987 
4988 static void bnxt_init_stats(struct bnxt *bp)
4989 {
4990 	struct bnxt_napi *bnapi = bp->bnapi[0];
4991 	struct bnxt_cp_ring_info *cpr;
4992 	struct bnxt_stats_mem *stats;
4993 	__le64 *rx_stats, *tx_stats;
4994 	int rc, rx_count, tx_count;
4995 	u64 *rx_masks, *tx_masks;
4996 	u64 mask;
4997 	u8 flags;
4998 
4999 	cpr = &bnapi->cp_ring;
5000 	stats = &cpr->stats;
5001 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5002 	if (rc) {
5003 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5004 			mask = (1ULL << 48) - 1;
5005 		else
5006 			mask = -1ULL;
5007 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5008 	}
5009 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5010 		stats = &bp->port_stats;
5011 		rx_stats = stats->hw_stats;
5012 		rx_masks = stats->hw_masks;
5013 		rx_count = sizeof(struct rx_port_stats) / 8;
5014 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5015 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5016 		tx_count = sizeof(struct tx_port_stats) / 8;
5017 
5018 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5019 		rc = bnxt_hwrm_port_qstats(bp, flags);
5020 		if (rc) {
5021 			mask = (1ULL << 40) - 1;
5022 
5023 			bnxt_fill_masks(rx_masks, mask, rx_count);
5024 			bnxt_fill_masks(tx_masks, mask, tx_count);
5025 		} else {
5026 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5027 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5028 			bnxt_hwrm_port_qstats(bp, 0);
5029 		}
5030 	}
5031 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5032 		stats = &bp->rx_port_stats_ext;
5033 		rx_stats = stats->hw_stats;
5034 		rx_masks = stats->hw_masks;
5035 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5036 		stats = &bp->tx_port_stats_ext;
5037 		tx_stats = stats->hw_stats;
5038 		tx_masks = stats->hw_masks;
5039 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5040 
5041 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5042 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5043 		if (rc) {
5044 			mask = (1ULL << 40) - 1;
5045 
5046 			bnxt_fill_masks(rx_masks, mask, rx_count);
5047 			if (tx_stats)
5048 				bnxt_fill_masks(tx_masks, mask, tx_count);
5049 		} else {
5050 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5051 			if (tx_stats)
5052 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5053 						   tx_count);
5054 			bnxt_hwrm_port_qstats_ext(bp, 0);
5055 		}
5056 	}
5057 }
5058 
5059 static void bnxt_free_port_stats(struct bnxt *bp)
5060 {
5061 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5062 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5063 
5064 	bnxt_free_stats_mem(bp, &bp->port_stats);
5065 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5066 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5067 }
5068 
5069 static void bnxt_free_ring_stats(struct bnxt *bp)
5070 {
5071 	int i;
5072 
5073 	if (!bp->bnapi)
5074 		return;
5075 
5076 	for (i = 0; i < bp->cp_nr_rings; i++) {
5077 		struct bnxt_napi *bnapi = bp->bnapi[i];
5078 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5079 
5080 		bnxt_free_stats_mem(bp, &cpr->stats);
5081 
5082 		kfree(cpr->sw_stats);
5083 		cpr->sw_stats = NULL;
5084 	}
5085 }
5086 
5087 static int bnxt_alloc_stats(struct bnxt *bp)
5088 {
5089 	u32 size, i;
5090 	int rc;
5091 
5092 	size = bp->hw_ring_stats_size;
5093 
5094 	for (i = 0; i < bp->cp_nr_rings; i++) {
5095 		struct bnxt_napi *bnapi = bp->bnapi[i];
5096 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5097 
5098 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
5099 		if (!cpr->sw_stats)
5100 			return -ENOMEM;
5101 
5102 		cpr->stats.len = size;
5103 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5104 		if (rc)
5105 			return rc;
5106 
5107 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5108 	}
5109 
5110 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5111 		return 0;
5112 
5113 	if (bp->port_stats.hw_stats)
5114 		goto alloc_ext_stats;
5115 
5116 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5117 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5118 	if (rc)
5119 		return rc;
5120 
5121 	bp->flags |= BNXT_FLAG_PORT_STATS;
5122 
5123 alloc_ext_stats:
5124 	/* Display extended statistics only if FW supports it */
5125 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5126 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5127 			return 0;
5128 
5129 	if (bp->rx_port_stats_ext.hw_stats)
5130 		goto alloc_tx_ext_stats;
5131 
5132 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5133 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5134 	/* Extended stats are optional */
5135 	if (rc)
5136 		return 0;
5137 
5138 alloc_tx_ext_stats:
5139 	if (bp->tx_port_stats_ext.hw_stats)
5140 		return 0;
5141 
5142 	if (bp->hwrm_spec_code >= 0x10902 ||
5143 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5144 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5145 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5146 		/* Extended stats are optional */
5147 		if (rc)
5148 			return 0;
5149 	}
5150 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5151 	return 0;
5152 }
5153 
5154 static void bnxt_clear_ring_indices(struct bnxt *bp)
5155 {
5156 	int i, j;
5157 
5158 	if (!bp->bnapi)
5159 		return;
5160 
5161 	for (i = 0; i < bp->cp_nr_rings; i++) {
5162 		struct bnxt_napi *bnapi = bp->bnapi[i];
5163 		struct bnxt_cp_ring_info *cpr;
5164 		struct bnxt_rx_ring_info *rxr;
5165 		struct bnxt_tx_ring_info *txr;
5166 
5167 		if (!bnapi)
5168 			continue;
5169 
5170 		cpr = &bnapi->cp_ring;
5171 		cpr->cp_raw_cons = 0;
5172 
5173 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5174 			txr->tx_prod = 0;
5175 			txr->tx_cons = 0;
5176 			txr->tx_hw_cons = 0;
5177 		}
5178 
5179 		rxr = bnapi->rx_ring;
5180 		if (rxr) {
5181 			rxr->rx_prod = 0;
5182 			rxr->rx_agg_prod = 0;
5183 			rxr->rx_sw_agg_prod = 0;
5184 			rxr->rx_next_cons = 0;
5185 		}
5186 		bnapi->events = 0;
5187 	}
5188 }
5189 
5190 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5191 {
5192 	u8 type = fltr->type, flags = fltr->flags;
5193 
5194 	INIT_LIST_HEAD(&fltr->list);
5195 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5196 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5197 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5198 }
5199 
5200 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5201 {
5202 	if (!list_empty(&fltr->list))
5203 		list_del_init(&fltr->list);
5204 }
5205 
5206 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5207 {
5208 	struct bnxt_filter_base *usr_fltr, *tmp;
5209 
5210 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5211 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5212 			continue;
5213 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5214 	}
5215 }
5216 
5217 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5218 {
5219 	hlist_del(&fltr->hash);
5220 	bnxt_del_one_usr_fltr(bp, fltr);
5221 	if (fltr->flags) {
5222 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5223 		bp->ntp_fltr_count--;
5224 	}
5225 	kfree(fltr);
5226 }
5227 
5228 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5229 {
5230 	int i;
5231 
5232 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
5233 	 * safe to delete the hash table.
5234 	 */
5235 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5236 		struct hlist_head *head;
5237 		struct hlist_node *tmp;
5238 		struct bnxt_ntuple_filter *fltr;
5239 
5240 		head = &bp->ntp_fltr_hash_tbl[i];
5241 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5242 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5243 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5244 				     !list_empty(&fltr->base.list)))
5245 				continue;
5246 			bnxt_del_fltr(bp, &fltr->base);
5247 		}
5248 	}
5249 	if (!all)
5250 		return;
5251 
5252 	bitmap_free(bp->ntp_fltr_bmap);
5253 	bp->ntp_fltr_bmap = NULL;
5254 	bp->ntp_fltr_count = 0;
5255 }
5256 
5257 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5258 {
5259 	int i, rc = 0;
5260 
5261 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5262 		return 0;
5263 
5264 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5265 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5266 
5267 	bp->ntp_fltr_count = 0;
5268 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5269 
5270 	if (!bp->ntp_fltr_bmap)
5271 		rc = -ENOMEM;
5272 
5273 	return rc;
5274 }
5275 
5276 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5277 {
5278 	int i;
5279 
5280 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5281 		struct hlist_head *head;
5282 		struct hlist_node *tmp;
5283 		struct bnxt_l2_filter *fltr;
5284 
5285 		head = &bp->l2_fltr_hash_tbl[i];
5286 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5287 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5288 				     !list_empty(&fltr->base.list)))
5289 				continue;
5290 			bnxt_del_fltr(bp, &fltr->base);
5291 		}
5292 	}
5293 }
5294 
5295 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5296 {
5297 	int i;
5298 
5299 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5300 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5301 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5302 }
5303 
5304 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5305 {
5306 	bnxt_free_vnic_attributes(bp);
5307 	bnxt_free_tx_rings(bp);
5308 	bnxt_free_rx_rings(bp);
5309 	bnxt_free_cp_rings(bp);
5310 	bnxt_free_all_cp_arrays(bp);
5311 	bnxt_free_ntp_fltrs(bp, false);
5312 	bnxt_free_l2_filters(bp, false);
5313 	if (irq_re_init) {
5314 		bnxt_free_ring_stats(bp);
5315 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5316 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5317 			bnxt_free_port_stats(bp);
5318 		bnxt_free_ring_grps(bp);
5319 		bnxt_free_vnics(bp);
5320 		kfree(bp->tx_ring_map);
5321 		bp->tx_ring_map = NULL;
5322 		kfree(bp->tx_ring);
5323 		bp->tx_ring = NULL;
5324 		kfree(bp->rx_ring);
5325 		bp->rx_ring = NULL;
5326 		kfree(bp->bnapi);
5327 		bp->bnapi = NULL;
5328 	} else {
5329 		bnxt_clear_ring_indices(bp);
5330 	}
5331 }
5332 
5333 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5334 {
5335 	int i, j, rc, size, arr_size;
5336 	void *bnapi;
5337 
5338 	if (irq_re_init) {
5339 		/* Allocate bnapi mem pointer array and mem block for
5340 		 * all queues
5341 		 */
5342 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5343 				bp->cp_nr_rings);
5344 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5345 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5346 		if (!bnapi)
5347 			return -ENOMEM;
5348 
5349 		bp->bnapi = bnapi;
5350 		bnapi += arr_size;
5351 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5352 			bp->bnapi[i] = bnapi;
5353 			bp->bnapi[i]->index = i;
5354 			bp->bnapi[i]->bp = bp;
5355 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5356 				struct bnxt_cp_ring_info *cpr =
5357 					&bp->bnapi[i]->cp_ring;
5358 
5359 				cpr->cp_ring_struct.ring_mem.flags =
5360 					BNXT_RMEM_RING_PTE_FLAG;
5361 			}
5362 		}
5363 
5364 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5365 				      sizeof(struct bnxt_rx_ring_info),
5366 				      GFP_KERNEL);
5367 		if (!bp->rx_ring)
5368 			return -ENOMEM;
5369 
5370 		for (i = 0; i < bp->rx_nr_rings; i++) {
5371 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5372 
5373 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5374 				rxr->rx_ring_struct.ring_mem.flags =
5375 					BNXT_RMEM_RING_PTE_FLAG;
5376 				rxr->rx_agg_ring_struct.ring_mem.flags =
5377 					BNXT_RMEM_RING_PTE_FLAG;
5378 			} else {
5379 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5380 			}
5381 			rxr->bnapi = bp->bnapi[i];
5382 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5383 		}
5384 
5385 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5386 				      sizeof(struct bnxt_tx_ring_info),
5387 				      GFP_KERNEL);
5388 		if (!bp->tx_ring)
5389 			return -ENOMEM;
5390 
5391 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5392 					  GFP_KERNEL);
5393 
5394 		if (!bp->tx_ring_map)
5395 			return -ENOMEM;
5396 
5397 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5398 			j = 0;
5399 		else
5400 			j = bp->rx_nr_rings;
5401 
5402 		for (i = 0; i < bp->tx_nr_rings; i++) {
5403 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5404 			struct bnxt_napi *bnapi2;
5405 
5406 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5407 				txr->tx_ring_struct.ring_mem.flags =
5408 					BNXT_RMEM_RING_PTE_FLAG;
5409 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5410 			if (i >= bp->tx_nr_rings_xdp) {
5411 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5412 
5413 				bnapi2 = bp->bnapi[k];
5414 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5415 				txr->tx_napi_idx =
5416 					BNXT_RING_TO_TC(bp, txr->txq_index);
5417 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5418 				bnapi2->tx_int = bnxt_tx_int;
5419 			} else {
5420 				bnapi2 = bp->bnapi[j];
5421 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5422 				bnapi2->tx_ring[0] = txr;
5423 				bnapi2->tx_int = bnxt_tx_int_xdp;
5424 				j++;
5425 			}
5426 			txr->bnapi = bnapi2;
5427 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5428 				txr->tx_cpr = &bnapi2->cp_ring;
5429 		}
5430 
5431 		rc = bnxt_alloc_stats(bp);
5432 		if (rc)
5433 			goto alloc_mem_err;
5434 		bnxt_init_stats(bp);
5435 
5436 		rc = bnxt_alloc_ntp_fltrs(bp);
5437 		if (rc)
5438 			goto alloc_mem_err;
5439 
5440 		rc = bnxt_alloc_vnics(bp);
5441 		if (rc)
5442 			goto alloc_mem_err;
5443 	}
5444 
5445 	rc = bnxt_alloc_all_cp_arrays(bp);
5446 	if (rc)
5447 		goto alloc_mem_err;
5448 
5449 	bnxt_init_ring_struct(bp);
5450 
5451 	rc = bnxt_alloc_rx_rings(bp);
5452 	if (rc)
5453 		goto alloc_mem_err;
5454 
5455 	rc = bnxt_alloc_tx_rings(bp);
5456 	if (rc)
5457 		goto alloc_mem_err;
5458 
5459 	rc = bnxt_alloc_cp_rings(bp);
5460 	if (rc)
5461 		goto alloc_mem_err;
5462 
5463 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5464 						  BNXT_VNIC_MCAST_FLAG |
5465 						  BNXT_VNIC_UCAST_FLAG;
5466 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5467 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5468 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5469 
5470 	rc = bnxt_alloc_vnic_attributes(bp);
5471 	if (rc)
5472 		goto alloc_mem_err;
5473 	return 0;
5474 
5475 alloc_mem_err:
5476 	bnxt_free_mem(bp, true);
5477 	return rc;
5478 }
5479 
5480 static void bnxt_disable_int(struct bnxt *bp)
5481 {
5482 	int i;
5483 
5484 	if (!bp->bnapi)
5485 		return;
5486 
5487 	for (i = 0; i < bp->cp_nr_rings; i++) {
5488 		struct bnxt_napi *bnapi = bp->bnapi[i];
5489 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5490 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5491 
5492 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5493 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5494 	}
5495 }
5496 
5497 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5498 {
5499 	struct bnxt_napi *bnapi = bp->bnapi[n];
5500 	struct bnxt_cp_ring_info *cpr;
5501 
5502 	cpr = &bnapi->cp_ring;
5503 	return cpr->cp_ring_struct.map_idx;
5504 }
5505 
5506 static void bnxt_disable_int_sync(struct bnxt *bp)
5507 {
5508 	int i;
5509 
5510 	if (!bp->irq_tbl)
5511 		return;
5512 
5513 	atomic_inc(&bp->intr_sem);
5514 
5515 	bnxt_disable_int(bp);
5516 	for (i = 0; i < bp->cp_nr_rings; i++) {
5517 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5518 
5519 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5520 	}
5521 }
5522 
5523 static void bnxt_enable_int(struct bnxt *bp)
5524 {
5525 	int i;
5526 
5527 	atomic_set(&bp->intr_sem, 0);
5528 	for (i = 0; i < bp->cp_nr_rings; i++) {
5529 		struct bnxt_napi *bnapi = bp->bnapi[i];
5530 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5531 
5532 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5533 	}
5534 }
5535 
5536 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5537 			    bool async_only)
5538 {
5539 	DECLARE_BITMAP(async_events_bmap, 256);
5540 	u32 *events = (u32 *)async_events_bmap;
5541 	struct hwrm_func_drv_rgtr_output *resp;
5542 	struct hwrm_func_drv_rgtr_input *req;
5543 	u32 flags;
5544 	int rc, i;
5545 
5546 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5547 	if (rc)
5548 		return rc;
5549 
5550 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5551 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5552 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5553 
5554 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5555 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5556 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5557 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5558 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5559 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5560 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5561 	req->flags = cpu_to_le32(flags);
5562 	req->ver_maj_8b = DRV_VER_MAJ;
5563 	req->ver_min_8b = DRV_VER_MIN;
5564 	req->ver_upd_8b = DRV_VER_UPD;
5565 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5566 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5567 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5568 
5569 	if (BNXT_PF(bp)) {
5570 		u32 data[8];
5571 		int i;
5572 
5573 		memset(data, 0, sizeof(data));
5574 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5575 			u16 cmd = bnxt_vf_req_snif[i];
5576 			unsigned int bit, idx;
5577 
5578 			idx = cmd / 32;
5579 			bit = cmd % 32;
5580 			data[idx] |= 1 << bit;
5581 		}
5582 
5583 		for (i = 0; i < 8; i++)
5584 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5585 
5586 		req->enables |=
5587 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5588 	}
5589 
5590 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5591 		req->flags |= cpu_to_le32(
5592 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5593 
5594 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5595 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5596 		u16 event_id = bnxt_async_events_arr[i];
5597 
5598 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5599 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5600 			continue;
5601 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5602 		    !bp->ptp_cfg)
5603 			continue;
5604 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5605 	}
5606 	if (bmap && bmap_size) {
5607 		for (i = 0; i < bmap_size; i++) {
5608 			if (test_bit(i, bmap))
5609 				__set_bit(i, async_events_bmap);
5610 		}
5611 	}
5612 	for (i = 0; i < 8; i++)
5613 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5614 
5615 	if (async_only)
5616 		req->enables =
5617 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5618 
5619 	resp = hwrm_req_hold(bp, req);
5620 	rc = hwrm_req_send(bp, req);
5621 	if (!rc) {
5622 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5623 		if (resp->flags &
5624 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5625 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5626 	}
5627 	hwrm_req_drop(bp, req);
5628 	return rc;
5629 }
5630 
5631 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5632 {
5633 	struct hwrm_func_drv_unrgtr_input *req;
5634 	int rc;
5635 
5636 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5637 		return 0;
5638 
5639 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5640 	if (rc)
5641 		return rc;
5642 	return hwrm_req_send(bp, req);
5643 }
5644 
5645 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5646 
5647 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5648 {
5649 	struct hwrm_tunnel_dst_port_free_input *req;
5650 	int rc;
5651 
5652 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5653 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5654 		return 0;
5655 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5656 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5657 		return 0;
5658 
5659 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5660 	if (rc)
5661 		return rc;
5662 
5663 	req->tunnel_type = tunnel_type;
5664 
5665 	switch (tunnel_type) {
5666 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5667 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5668 		bp->vxlan_port = 0;
5669 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5670 		break;
5671 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5672 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5673 		bp->nge_port = 0;
5674 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5675 		break;
5676 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5677 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5678 		bp->vxlan_gpe_port = 0;
5679 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5680 		break;
5681 	default:
5682 		break;
5683 	}
5684 
5685 	rc = hwrm_req_send(bp, req);
5686 	if (rc)
5687 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5688 			   rc);
5689 	if (bp->flags & BNXT_FLAG_TPA)
5690 		bnxt_set_tpa(bp, true);
5691 	return rc;
5692 }
5693 
5694 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5695 					   u8 tunnel_type)
5696 {
5697 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5698 	struct hwrm_tunnel_dst_port_alloc_input *req;
5699 	int rc;
5700 
5701 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5702 	if (rc)
5703 		return rc;
5704 
5705 	req->tunnel_type = tunnel_type;
5706 	req->tunnel_dst_port_val = port;
5707 
5708 	resp = hwrm_req_hold(bp, req);
5709 	rc = hwrm_req_send(bp, req);
5710 	if (rc) {
5711 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5712 			   rc);
5713 		goto err_out;
5714 	}
5715 
5716 	switch (tunnel_type) {
5717 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5718 		bp->vxlan_port = port;
5719 		bp->vxlan_fw_dst_port_id =
5720 			le16_to_cpu(resp->tunnel_dst_port_id);
5721 		break;
5722 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5723 		bp->nge_port = port;
5724 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5725 		break;
5726 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5727 		bp->vxlan_gpe_port = port;
5728 		bp->vxlan_gpe_fw_dst_port_id =
5729 			le16_to_cpu(resp->tunnel_dst_port_id);
5730 		break;
5731 	default:
5732 		break;
5733 	}
5734 	if (bp->flags & BNXT_FLAG_TPA)
5735 		bnxt_set_tpa(bp, true);
5736 
5737 err_out:
5738 	hwrm_req_drop(bp, req);
5739 	return rc;
5740 }
5741 
5742 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5743 {
5744 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5745 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5746 	int rc;
5747 
5748 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5749 	if (rc)
5750 		return rc;
5751 
5752 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5753 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5754 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5755 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5756 	}
5757 	req->mask = cpu_to_le32(vnic->rx_mask);
5758 	return hwrm_req_send_silent(bp, req);
5759 }
5760 
5761 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5762 {
5763 	if (!atomic_dec_and_test(&fltr->refcnt))
5764 		return;
5765 	spin_lock_bh(&bp->ntp_fltr_lock);
5766 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5767 		spin_unlock_bh(&bp->ntp_fltr_lock);
5768 		return;
5769 	}
5770 	hlist_del_rcu(&fltr->base.hash);
5771 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5772 	if (fltr->base.flags) {
5773 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5774 		bp->ntp_fltr_count--;
5775 	}
5776 	spin_unlock_bh(&bp->ntp_fltr_lock);
5777 	kfree_rcu(fltr, base.rcu);
5778 }
5779 
5780 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5781 						      struct bnxt_l2_key *key,
5782 						      u32 idx)
5783 {
5784 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5785 	struct bnxt_l2_filter *fltr;
5786 
5787 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5788 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5789 
5790 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5791 		    l2_key->vlan == key->vlan)
5792 			return fltr;
5793 	}
5794 	return NULL;
5795 }
5796 
5797 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5798 						    struct bnxt_l2_key *key,
5799 						    u32 idx)
5800 {
5801 	struct bnxt_l2_filter *fltr = NULL;
5802 
5803 	rcu_read_lock();
5804 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5805 	if (fltr)
5806 		atomic_inc(&fltr->refcnt);
5807 	rcu_read_unlock();
5808 	return fltr;
5809 }
5810 
5811 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5812 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5813 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5814 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5815 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5816 
5817 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5818 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5819 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5820 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5821 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5822 
5823 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5824 {
5825 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5826 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5827 			return sizeof(fkeys->addrs.v4addrs) +
5828 			       sizeof(fkeys->ports);
5829 
5830 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5831 			return sizeof(fkeys->addrs.v4addrs);
5832 	}
5833 
5834 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5835 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5836 			return sizeof(fkeys->addrs.v6addrs) +
5837 			       sizeof(fkeys->ports);
5838 
5839 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5840 			return sizeof(fkeys->addrs.v6addrs);
5841 	}
5842 
5843 	return 0;
5844 }
5845 
5846 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5847 			 const unsigned char *key)
5848 {
5849 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5850 	struct bnxt_ipv4_tuple tuple4;
5851 	struct bnxt_ipv6_tuple tuple6;
5852 	int i, j, len = 0;
5853 	u8 *four_tuple;
5854 
5855 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5856 	if (!len)
5857 		return 0;
5858 
5859 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5860 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5861 		tuple4.ports = fkeys->ports;
5862 		four_tuple = (unsigned char *)&tuple4;
5863 	} else {
5864 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5865 		tuple6.ports = fkeys->ports;
5866 		four_tuple = (unsigned char *)&tuple6;
5867 	}
5868 
5869 	for (i = 0, j = 8; i < len; i++, j++) {
5870 		u8 byte = four_tuple[i];
5871 		int bit;
5872 
5873 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5874 			if (byte & 0x80)
5875 				hash ^= prefix;
5876 		}
5877 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5878 	}
5879 
5880 	/* The valid part of the hash is in the upper 32 bits. */
5881 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5882 }
5883 
5884 #ifdef CONFIG_RFS_ACCEL
5885 static struct bnxt_l2_filter *
5886 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5887 {
5888 	struct bnxt_l2_filter *fltr;
5889 	u32 idx;
5890 
5891 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5892 	      BNXT_L2_FLTR_HASH_MASK;
5893 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5894 	return fltr;
5895 }
5896 #endif
5897 
5898 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5899 			       struct bnxt_l2_key *key, u32 idx)
5900 {
5901 	struct hlist_head *head;
5902 
5903 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5904 	fltr->l2_key.vlan = key->vlan;
5905 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5906 	if (fltr->base.flags) {
5907 		int bit_id;
5908 
5909 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5910 						 bp->max_fltr, 0);
5911 		if (bit_id < 0)
5912 			return -ENOMEM;
5913 		fltr->base.sw_id = (u16)bit_id;
5914 		bp->ntp_fltr_count++;
5915 	}
5916 	head = &bp->l2_fltr_hash_tbl[idx];
5917 	hlist_add_head_rcu(&fltr->base.hash, head);
5918 	bnxt_insert_usr_fltr(bp, &fltr->base);
5919 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5920 	atomic_set(&fltr->refcnt, 1);
5921 	return 0;
5922 }
5923 
5924 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
5925 						   struct bnxt_l2_key *key,
5926 						   gfp_t gfp)
5927 {
5928 	struct bnxt_l2_filter *fltr;
5929 	u32 idx;
5930 	int rc;
5931 
5932 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5933 	      BNXT_L2_FLTR_HASH_MASK;
5934 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5935 	if (fltr)
5936 		return fltr;
5937 
5938 	fltr = kzalloc(sizeof(*fltr), gfp);
5939 	if (!fltr)
5940 		return ERR_PTR(-ENOMEM);
5941 	spin_lock_bh(&bp->ntp_fltr_lock);
5942 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5943 	spin_unlock_bh(&bp->ntp_fltr_lock);
5944 	if (rc) {
5945 		bnxt_del_l2_filter(bp, fltr);
5946 		fltr = ERR_PTR(rc);
5947 	}
5948 	return fltr;
5949 }
5950 
5951 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
5952 						struct bnxt_l2_key *key,
5953 						u16 flags)
5954 {
5955 	struct bnxt_l2_filter *fltr;
5956 	u32 idx;
5957 	int rc;
5958 
5959 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5960 	      BNXT_L2_FLTR_HASH_MASK;
5961 	spin_lock_bh(&bp->ntp_fltr_lock);
5962 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5963 	if (fltr) {
5964 		fltr = ERR_PTR(-EEXIST);
5965 		goto l2_filter_exit;
5966 	}
5967 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
5968 	if (!fltr) {
5969 		fltr = ERR_PTR(-ENOMEM);
5970 		goto l2_filter_exit;
5971 	}
5972 	fltr->base.flags = flags;
5973 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5974 	if (rc) {
5975 		spin_unlock_bh(&bp->ntp_fltr_lock);
5976 		bnxt_del_l2_filter(bp, fltr);
5977 		return ERR_PTR(rc);
5978 	}
5979 
5980 l2_filter_exit:
5981 	spin_unlock_bh(&bp->ntp_fltr_lock);
5982 	return fltr;
5983 }
5984 
5985 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
5986 {
5987 #ifdef CONFIG_BNXT_SRIOV
5988 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
5989 
5990 	return vf->fw_fid;
5991 #else
5992 	return INVALID_HW_RING_ID;
5993 #endif
5994 }
5995 
5996 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5997 {
5998 	struct hwrm_cfa_l2_filter_free_input *req;
5999 	u16 target_id = 0xffff;
6000 	int rc;
6001 
6002 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6003 		struct bnxt_pf_info *pf = &bp->pf;
6004 
6005 		if (fltr->base.vf_idx >= pf->active_vfs)
6006 			return -EINVAL;
6007 
6008 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6009 		if (target_id == INVALID_HW_RING_ID)
6010 			return -EINVAL;
6011 	}
6012 
6013 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6014 	if (rc)
6015 		return rc;
6016 
6017 	req->target_id = cpu_to_le16(target_id);
6018 	req->l2_filter_id = fltr->base.filter_id;
6019 	return hwrm_req_send(bp, req);
6020 }
6021 
6022 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6023 {
6024 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6025 	struct hwrm_cfa_l2_filter_alloc_input *req;
6026 	u16 target_id = 0xffff;
6027 	int rc;
6028 
6029 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6030 		struct bnxt_pf_info *pf = &bp->pf;
6031 
6032 		if (fltr->base.vf_idx >= pf->active_vfs)
6033 			return -EINVAL;
6034 
6035 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6036 	}
6037 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6038 	if (rc)
6039 		return rc;
6040 
6041 	req->target_id = cpu_to_le16(target_id);
6042 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6043 
6044 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6045 		req->flags |=
6046 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6047 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6048 	req->enables =
6049 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6050 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6051 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6052 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6053 	eth_broadcast_addr(req->l2_addr_mask);
6054 
6055 	if (fltr->l2_key.vlan) {
6056 		req->enables |=
6057 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6058 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6059 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6060 		req->num_vlans = 1;
6061 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6062 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6063 	}
6064 
6065 	resp = hwrm_req_hold(bp, req);
6066 	rc = hwrm_req_send(bp, req);
6067 	if (!rc) {
6068 		fltr->base.filter_id = resp->l2_filter_id;
6069 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6070 	}
6071 	hwrm_req_drop(bp, req);
6072 	return rc;
6073 }
6074 
6075 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6076 				     struct bnxt_ntuple_filter *fltr)
6077 {
6078 	struct hwrm_cfa_ntuple_filter_free_input *req;
6079 	int rc;
6080 
6081 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6082 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6083 	if (rc)
6084 		return rc;
6085 
6086 	req->ntuple_filter_id = fltr->base.filter_id;
6087 	return hwrm_req_send(bp, req);
6088 }
6089 
6090 #define BNXT_NTP_FLTR_FLAGS					\
6091 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6092 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6093 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6094 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6095 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6096 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6097 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6098 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6099 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6100 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6101 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6102 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6103 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6104 
6105 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6106 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6107 
6108 void bnxt_fill_ipv6_mask(__be32 mask[4])
6109 {
6110 	int i;
6111 
6112 	for (i = 0; i < 4; i++)
6113 		mask[i] = cpu_to_be32(~0);
6114 }
6115 
6116 static void
6117 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6118 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6119 			  struct bnxt_ntuple_filter *fltr)
6120 {
6121 	u16 rxq = fltr->base.rxq;
6122 
6123 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6124 		struct ethtool_rxfh_context *ctx;
6125 		struct bnxt_rss_ctx *rss_ctx;
6126 		struct bnxt_vnic_info *vnic;
6127 
6128 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6129 			      fltr->base.fw_vnic_id);
6130 		if (ctx) {
6131 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6132 			vnic = &rss_ctx->vnic;
6133 
6134 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6135 		}
6136 		return;
6137 	}
6138 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6139 		struct bnxt_vnic_info *vnic;
6140 		u32 enables;
6141 
6142 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6143 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6144 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6145 		req->enables |= cpu_to_le32(enables);
6146 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6147 	} else {
6148 		u32 flags;
6149 
6150 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6151 		req->flags |= cpu_to_le32(flags);
6152 		req->dst_id = cpu_to_le16(rxq);
6153 	}
6154 }
6155 
6156 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6157 				      struct bnxt_ntuple_filter *fltr)
6158 {
6159 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6160 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6161 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6162 	struct flow_keys *keys = &fltr->fkeys;
6163 	struct bnxt_l2_filter *l2_fltr;
6164 	struct bnxt_vnic_info *vnic;
6165 	int rc;
6166 
6167 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6168 	if (rc)
6169 		return rc;
6170 
6171 	l2_fltr = fltr->l2_fltr;
6172 	req->l2_filter_id = l2_fltr->base.filter_id;
6173 
6174 	if (fltr->base.flags & BNXT_ACT_DROP) {
6175 		req->flags =
6176 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6177 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6178 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6179 	} else {
6180 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6181 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6182 	}
6183 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6184 
6185 	req->ethertype = htons(ETH_P_IP);
6186 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6187 	req->ip_protocol = keys->basic.ip_proto;
6188 
6189 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6190 		req->ethertype = htons(ETH_P_IPV6);
6191 		req->ip_addr_type =
6192 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6193 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6194 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6195 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6196 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6197 	} else {
6198 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6199 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6200 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6201 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6202 	}
6203 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6204 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6205 		req->tunnel_type =
6206 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6207 	}
6208 
6209 	req->src_port = keys->ports.src;
6210 	req->src_port_mask = masks->ports.src;
6211 	req->dst_port = keys->ports.dst;
6212 	req->dst_port_mask = masks->ports.dst;
6213 
6214 	resp = hwrm_req_hold(bp, req);
6215 	rc = hwrm_req_send(bp, req);
6216 	if (!rc)
6217 		fltr->base.filter_id = resp->ntuple_filter_id;
6218 	hwrm_req_drop(bp, req);
6219 	return rc;
6220 }
6221 
6222 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6223 				     const u8 *mac_addr)
6224 {
6225 	struct bnxt_l2_filter *fltr;
6226 	struct bnxt_l2_key key;
6227 	int rc;
6228 
6229 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6230 	key.vlan = 0;
6231 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6232 	if (IS_ERR(fltr))
6233 		return PTR_ERR(fltr);
6234 
6235 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6236 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6237 	if (rc)
6238 		bnxt_del_l2_filter(bp, fltr);
6239 	else
6240 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6241 	return rc;
6242 }
6243 
6244 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6245 {
6246 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6247 
6248 	/* Any associated ntuple filters will also be cleared by firmware. */
6249 	for (i = 0; i < num_of_vnics; i++) {
6250 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6251 
6252 		for (j = 0; j < vnic->uc_filter_count; j++) {
6253 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6254 
6255 			bnxt_hwrm_l2_filter_free(bp, fltr);
6256 			bnxt_del_l2_filter(bp, fltr);
6257 		}
6258 		vnic->uc_filter_count = 0;
6259 	}
6260 }
6261 
6262 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6263 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6264 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6265 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6266 
6267 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6268 					   struct hwrm_vnic_tpa_cfg_input *req)
6269 {
6270 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6271 
6272 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6273 		return;
6274 
6275 	if (bp->vxlan_port)
6276 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6277 	if (bp->vxlan_gpe_port)
6278 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6279 	if (bp->nge_port)
6280 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6281 
6282 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6283 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6284 }
6285 
6286 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6287 			   u32 tpa_flags)
6288 {
6289 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6290 	struct hwrm_vnic_tpa_cfg_input *req;
6291 	int rc;
6292 
6293 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6294 		return 0;
6295 
6296 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6297 	if (rc)
6298 		return rc;
6299 
6300 	if (tpa_flags) {
6301 		u16 mss = bp->dev->mtu - 40;
6302 		u32 nsegs, n, segs = 0, flags;
6303 
6304 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6305 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6306 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6307 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6308 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6309 		if (tpa_flags & BNXT_FLAG_GRO)
6310 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6311 
6312 		req->flags = cpu_to_le32(flags);
6313 
6314 		req->enables =
6315 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6316 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6317 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6318 
6319 		/* Number of segs are log2 units, and first packet is not
6320 		 * included as part of this units.
6321 		 */
6322 		if (mss <= BNXT_RX_PAGE_SIZE) {
6323 			n = BNXT_RX_PAGE_SIZE / mss;
6324 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6325 		} else {
6326 			n = mss / BNXT_RX_PAGE_SIZE;
6327 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6328 				n++;
6329 			nsegs = (MAX_SKB_FRAGS - n) / n;
6330 		}
6331 
6332 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6333 			segs = MAX_TPA_SEGS_P5;
6334 			max_aggs = bp->max_tpa;
6335 		} else {
6336 			segs = ilog2(nsegs);
6337 		}
6338 		req->max_agg_segs = cpu_to_le16(segs);
6339 		req->max_aggs = cpu_to_le16(max_aggs);
6340 
6341 		req->min_agg_len = cpu_to_le32(512);
6342 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6343 	}
6344 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6345 
6346 	return hwrm_req_send(bp, req);
6347 }
6348 
6349 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6350 {
6351 	struct bnxt_ring_grp_info *grp_info;
6352 
6353 	grp_info = &bp->grp_info[ring->grp_idx];
6354 	return grp_info->cp_fw_ring_id;
6355 }
6356 
6357 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6358 {
6359 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6360 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6361 	else
6362 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6363 }
6364 
6365 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6366 {
6367 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6368 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6369 	else
6370 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6371 }
6372 
6373 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6374 {
6375 	int entries;
6376 
6377 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6378 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6379 	else
6380 		entries = HW_HASH_INDEX_SIZE;
6381 
6382 	bp->rss_indir_tbl_entries = entries;
6383 	bp->rss_indir_tbl =
6384 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6385 	if (!bp->rss_indir_tbl)
6386 		return -ENOMEM;
6387 
6388 	return 0;
6389 }
6390 
6391 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6392 				 struct ethtool_rxfh_context *rss_ctx)
6393 {
6394 	u16 max_rings, max_entries, pad, i;
6395 	u32 *rss_indir_tbl;
6396 
6397 	if (!bp->rx_nr_rings)
6398 		return;
6399 
6400 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6401 		max_rings = bp->rx_nr_rings - 1;
6402 	else
6403 		max_rings = bp->rx_nr_rings;
6404 
6405 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6406 	if (rss_ctx)
6407 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6408 	else
6409 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6410 
6411 	for (i = 0; i < max_entries; i++)
6412 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6413 
6414 	pad = bp->rss_indir_tbl_entries - max_entries;
6415 	if (pad)
6416 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6417 }
6418 
6419 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6420 {
6421 	u32 i, tbl_size, max_ring = 0;
6422 
6423 	if (!bp->rss_indir_tbl)
6424 		return 0;
6425 
6426 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6427 	for (i = 0; i < tbl_size; i++)
6428 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6429 	return max_ring;
6430 }
6431 
6432 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6433 {
6434 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6435 		if (!rx_rings)
6436 			return 0;
6437 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6438 					       BNXT_RSS_TABLE_ENTRIES_P5);
6439 	}
6440 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6441 		return 2;
6442 	return 1;
6443 }
6444 
6445 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6446 {
6447 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6448 	u16 i, j;
6449 
6450 	/* Fill the RSS indirection table with ring group ids */
6451 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6452 		if (!no_rss)
6453 			j = bp->rss_indir_tbl[i];
6454 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6455 	}
6456 }
6457 
6458 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6459 				    struct bnxt_vnic_info *vnic)
6460 {
6461 	__le16 *ring_tbl = vnic->rss_table;
6462 	struct bnxt_rx_ring_info *rxr;
6463 	u16 tbl_size, i;
6464 
6465 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6466 
6467 	for (i = 0; i < tbl_size; i++) {
6468 		u16 ring_id, j;
6469 
6470 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6471 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6472 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6473 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6474 		else
6475 			j = bp->rss_indir_tbl[i];
6476 		rxr = &bp->rx_ring[j];
6477 
6478 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6479 		*ring_tbl++ = cpu_to_le16(ring_id);
6480 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6481 		*ring_tbl++ = cpu_to_le16(ring_id);
6482 	}
6483 }
6484 
6485 static void
6486 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6487 			 struct bnxt_vnic_info *vnic)
6488 {
6489 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6490 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6491 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6492 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6493 	} else {
6494 		bnxt_fill_hw_rss_tbl(bp, vnic);
6495 	}
6496 
6497 	if (bp->rss_hash_delta) {
6498 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6499 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6500 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6501 		else
6502 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6503 	} else {
6504 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6505 	}
6506 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6507 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6508 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6509 }
6510 
6511 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6512 				  bool set_rss)
6513 {
6514 	struct hwrm_vnic_rss_cfg_input *req;
6515 	int rc;
6516 
6517 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6518 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6519 		return 0;
6520 
6521 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6522 	if (rc)
6523 		return rc;
6524 
6525 	if (set_rss)
6526 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6527 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6528 	return hwrm_req_send(bp, req);
6529 }
6530 
6531 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6532 				     struct bnxt_vnic_info *vnic, bool set_rss)
6533 {
6534 	struct hwrm_vnic_rss_cfg_input *req;
6535 	dma_addr_t ring_tbl_map;
6536 	u32 i, nr_ctxs;
6537 	int rc;
6538 
6539 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6540 	if (rc)
6541 		return rc;
6542 
6543 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6544 	if (!set_rss)
6545 		return hwrm_req_send(bp, req);
6546 
6547 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6548 	ring_tbl_map = vnic->rss_table_dma_addr;
6549 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6550 
6551 	hwrm_req_hold(bp, req);
6552 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6553 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6554 		req->ring_table_pair_index = i;
6555 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6556 		rc = hwrm_req_send(bp, req);
6557 		if (rc)
6558 			goto exit;
6559 	}
6560 
6561 exit:
6562 	hwrm_req_drop(bp, req);
6563 	return rc;
6564 }
6565 
6566 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6567 {
6568 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6569 	struct hwrm_vnic_rss_qcfg_output *resp;
6570 	struct hwrm_vnic_rss_qcfg_input *req;
6571 
6572 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6573 		return;
6574 
6575 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6576 	/* all contexts configured to same hash_type, zero always exists */
6577 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6578 	resp = hwrm_req_hold(bp, req);
6579 	if (!hwrm_req_send(bp, req)) {
6580 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6581 		bp->rss_hash_delta = 0;
6582 	}
6583 	hwrm_req_drop(bp, req);
6584 }
6585 
6586 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6587 {
6588 	u16 hds_thresh = (u16)bp->dev->ethtool->hds_thresh;
6589 	struct hwrm_vnic_plcmodes_cfg_input *req;
6590 	int rc;
6591 
6592 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6593 	if (rc)
6594 		return rc;
6595 
6596 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6597 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6598 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6599 
6600 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6601 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6602 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6603 		req->enables |=
6604 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6605 		req->hds_threshold = cpu_to_le16(hds_thresh);
6606 	}
6607 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6608 	return hwrm_req_send(bp, req);
6609 }
6610 
6611 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6612 					struct bnxt_vnic_info *vnic,
6613 					u16 ctx_idx)
6614 {
6615 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6616 
6617 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6618 		return;
6619 
6620 	req->rss_cos_lb_ctx_id =
6621 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6622 
6623 	hwrm_req_send(bp, req);
6624 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6625 }
6626 
6627 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6628 {
6629 	int i, j;
6630 
6631 	for (i = 0; i < bp->nr_vnics; i++) {
6632 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6633 
6634 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6635 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6636 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6637 		}
6638 	}
6639 	bp->rsscos_nr_ctxs = 0;
6640 }
6641 
6642 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6643 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6644 {
6645 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6646 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6647 	int rc;
6648 
6649 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6650 	if (rc)
6651 		return rc;
6652 
6653 	resp = hwrm_req_hold(bp, req);
6654 	rc = hwrm_req_send(bp, req);
6655 	if (!rc)
6656 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6657 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6658 	hwrm_req_drop(bp, req);
6659 
6660 	return rc;
6661 }
6662 
6663 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6664 {
6665 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6666 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6667 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6668 }
6669 
6670 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6671 {
6672 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6673 	struct hwrm_vnic_cfg_input *req;
6674 	unsigned int ring = 0, grp_idx;
6675 	u16 def_vlan = 0;
6676 	int rc;
6677 
6678 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6679 	if (rc)
6680 		return rc;
6681 
6682 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6683 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6684 
6685 		req->default_rx_ring_id =
6686 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6687 		req->default_cmpl_ring_id =
6688 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6689 		req->enables =
6690 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6691 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6692 		goto vnic_mru;
6693 	}
6694 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6695 	/* Only RSS support for now TBD: COS & LB */
6696 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6697 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6698 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6699 					   VNIC_CFG_REQ_ENABLES_MRU);
6700 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6701 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6702 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6703 					   VNIC_CFG_REQ_ENABLES_MRU);
6704 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6705 	} else {
6706 		req->rss_rule = cpu_to_le16(0xffff);
6707 	}
6708 
6709 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6710 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6711 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6712 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6713 	} else {
6714 		req->cos_rule = cpu_to_le16(0xffff);
6715 	}
6716 
6717 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6718 		ring = 0;
6719 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6720 		ring = vnic->vnic_id - 1;
6721 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6722 		ring = bp->rx_nr_rings - 1;
6723 
6724 	grp_idx = bp->rx_ring[ring].bnapi->index;
6725 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6726 	req->lb_rule = cpu_to_le16(0xffff);
6727 vnic_mru:
6728 	vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
6729 	req->mru = cpu_to_le16(vnic->mru);
6730 
6731 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6732 #ifdef CONFIG_BNXT_SRIOV
6733 	if (BNXT_VF(bp))
6734 		def_vlan = bp->vf.vlan;
6735 #endif
6736 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6737 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6738 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6739 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6740 
6741 	return hwrm_req_send(bp, req);
6742 }
6743 
6744 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6745 				    struct bnxt_vnic_info *vnic)
6746 {
6747 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6748 		struct hwrm_vnic_free_input *req;
6749 
6750 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6751 			return;
6752 
6753 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6754 
6755 		hwrm_req_send(bp, req);
6756 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6757 	}
6758 }
6759 
6760 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6761 {
6762 	u16 i;
6763 
6764 	for (i = 0; i < bp->nr_vnics; i++)
6765 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6766 }
6767 
6768 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6769 			 unsigned int start_rx_ring_idx,
6770 			 unsigned int nr_rings)
6771 {
6772 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6773 	struct hwrm_vnic_alloc_output *resp;
6774 	struct hwrm_vnic_alloc_input *req;
6775 	int rc;
6776 
6777 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6778 	if (rc)
6779 		return rc;
6780 
6781 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6782 		goto vnic_no_ring_grps;
6783 
6784 	/* map ring groups to this vnic */
6785 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6786 		grp_idx = bp->rx_ring[i].bnapi->index;
6787 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6788 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6789 				   j, nr_rings);
6790 			break;
6791 		}
6792 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6793 	}
6794 
6795 vnic_no_ring_grps:
6796 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6797 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6798 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6799 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6800 
6801 	resp = hwrm_req_hold(bp, req);
6802 	rc = hwrm_req_send(bp, req);
6803 	if (!rc)
6804 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6805 	hwrm_req_drop(bp, req);
6806 	return rc;
6807 }
6808 
6809 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6810 {
6811 	struct hwrm_vnic_qcaps_output *resp;
6812 	struct hwrm_vnic_qcaps_input *req;
6813 	int rc;
6814 
6815 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6816 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6817 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6818 	if (bp->hwrm_spec_code < 0x10600)
6819 		return 0;
6820 
6821 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6822 	if (rc)
6823 		return rc;
6824 
6825 	resp = hwrm_req_hold(bp, req);
6826 	rc = hwrm_req_send(bp, req);
6827 	if (!rc) {
6828 		u32 flags = le32_to_cpu(resp->flags);
6829 
6830 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6831 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6832 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6833 		if (flags &
6834 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6835 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6836 
6837 		/* Older P5 fw before EXT_HW_STATS support did not set
6838 		 * VLAN_STRIP_CAP properly.
6839 		 */
6840 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6841 		    (BNXT_CHIP_P5(bp) &&
6842 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6843 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6844 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6845 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6846 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6847 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6848 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6849 		if (bp->max_tpa_v2) {
6850 			if (BNXT_CHIP_P5(bp))
6851 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6852 			else
6853 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6854 		}
6855 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6856 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6857 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6858 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6859 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6860 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6861 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6862 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6863 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6864 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6865 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6866 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6867 	}
6868 	hwrm_req_drop(bp, req);
6869 	return rc;
6870 }
6871 
6872 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6873 {
6874 	struct hwrm_ring_grp_alloc_output *resp;
6875 	struct hwrm_ring_grp_alloc_input *req;
6876 	int rc;
6877 	u16 i;
6878 
6879 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6880 		return 0;
6881 
6882 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6883 	if (rc)
6884 		return rc;
6885 
6886 	resp = hwrm_req_hold(bp, req);
6887 	for (i = 0; i < bp->rx_nr_rings; i++) {
6888 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6889 
6890 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6891 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6892 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6893 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6894 
6895 		rc = hwrm_req_send(bp, req);
6896 
6897 		if (rc)
6898 			break;
6899 
6900 		bp->grp_info[grp_idx].fw_grp_id =
6901 			le32_to_cpu(resp->ring_group_id);
6902 	}
6903 	hwrm_req_drop(bp, req);
6904 	return rc;
6905 }
6906 
6907 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6908 {
6909 	struct hwrm_ring_grp_free_input *req;
6910 	u16 i;
6911 
6912 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6913 		return;
6914 
6915 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6916 		return;
6917 
6918 	hwrm_req_hold(bp, req);
6919 	for (i = 0; i < bp->cp_nr_rings; i++) {
6920 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
6921 			continue;
6922 		req->ring_group_id =
6923 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
6924 
6925 		hwrm_req_send(bp, req);
6926 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
6927 	}
6928 	hwrm_req_drop(bp, req);
6929 }
6930 
6931 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
6932 				    struct bnxt_ring_struct *ring,
6933 				    u32 ring_type, u32 map_index)
6934 {
6935 	struct hwrm_ring_alloc_output *resp;
6936 	struct hwrm_ring_alloc_input *req;
6937 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
6938 	struct bnxt_ring_grp_info *grp_info;
6939 	int rc, err = 0;
6940 	u16 ring_id;
6941 
6942 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
6943 	if (rc)
6944 		goto exit;
6945 
6946 	req->enables = 0;
6947 	if (rmem->nr_pages > 1) {
6948 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
6949 		/* Page size is in log2 units */
6950 		req->page_size = BNXT_PAGE_SHIFT;
6951 		req->page_tbl_depth = 1;
6952 	} else {
6953 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
6954 	}
6955 	req->fbo = 0;
6956 	/* Association of ring index with doorbell index and MSIX number */
6957 	req->logical_id = cpu_to_le16(map_index);
6958 
6959 	switch (ring_type) {
6960 	case HWRM_RING_ALLOC_TX: {
6961 		struct bnxt_tx_ring_info *txr;
6962 		u16 flags = 0;
6963 
6964 		txr = container_of(ring, struct bnxt_tx_ring_info,
6965 				   tx_ring_struct);
6966 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
6967 		/* Association of transmit ring with completion ring */
6968 		grp_info = &bp->grp_info[ring->grp_idx];
6969 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
6970 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
6971 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6972 		req->queue_id = cpu_to_le16(ring->queue_id);
6973 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
6974 			req->cmpl_coal_cnt =
6975 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
6976 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
6977 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
6978 		req->flags = cpu_to_le16(flags);
6979 		break;
6980 	}
6981 	case HWRM_RING_ALLOC_RX:
6982 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6983 		req->length = cpu_to_le32(bp->rx_ring_mask + 1);
6984 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6985 			u16 flags = 0;
6986 
6987 			/* Association of rx ring with stats context */
6988 			grp_info = &bp->grp_info[ring->grp_idx];
6989 			req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
6990 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6991 			req->enables |= cpu_to_le32(
6992 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6993 			if (NET_IP_ALIGN == 2)
6994 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
6995 			req->flags = cpu_to_le16(flags);
6996 		}
6997 		break;
6998 	case HWRM_RING_ALLOC_AGG:
6999 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7000 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7001 			/* Association of agg ring with rx ring */
7002 			grp_info = &bp->grp_info[ring->grp_idx];
7003 			req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7004 			req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
7005 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7006 			req->enables |= cpu_to_le32(
7007 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
7008 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
7009 		} else {
7010 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7011 		}
7012 		req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
7013 		break;
7014 	case HWRM_RING_ALLOC_CMPL:
7015 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7016 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7017 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7018 			/* Association of cp ring with nq */
7019 			grp_info = &bp->grp_info[map_index];
7020 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7021 			req->cq_handle = cpu_to_le64(ring->handle);
7022 			req->enables |= cpu_to_le32(
7023 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7024 		} else {
7025 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7026 		}
7027 		break;
7028 	case HWRM_RING_ALLOC_NQ:
7029 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7030 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7031 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7032 		break;
7033 	default:
7034 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7035 			   ring_type);
7036 		return -1;
7037 	}
7038 
7039 	resp = hwrm_req_hold(bp, req);
7040 	rc = hwrm_req_send(bp, req);
7041 	err = le16_to_cpu(resp->error_code);
7042 	ring_id = le16_to_cpu(resp->ring_id);
7043 	hwrm_req_drop(bp, req);
7044 
7045 exit:
7046 	if (rc || err) {
7047 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7048 			   ring_type, rc, err);
7049 		return -EIO;
7050 	}
7051 	ring->fw_ring_id = ring_id;
7052 	return rc;
7053 }
7054 
7055 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7056 {
7057 	int rc;
7058 
7059 	if (BNXT_PF(bp)) {
7060 		struct hwrm_func_cfg_input *req;
7061 
7062 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7063 		if (rc)
7064 			return rc;
7065 
7066 		req->fid = cpu_to_le16(0xffff);
7067 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7068 		req->async_event_cr = cpu_to_le16(idx);
7069 		return hwrm_req_send(bp, req);
7070 	} else {
7071 		struct hwrm_func_vf_cfg_input *req;
7072 
7073 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7074 		if (rc)
7075 			return rc;
7076 
7077 		req->enables =
7078 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7079 		req->async_event_cr = cpu_to_le16(idx);
7080 		return hwrm_req_send(bp, req);
7081 	}
7082 }
7083 
7084 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7085 			     u32 ring_type)
7086 {
7087 	switch (ring_type) {
7088 	case HWRM_RING_ALLOC_TX:
7089 		db->db_ring_mask = bp->tx_ring_mask;
7090 		break;
7091 	case HWRM_RING_ALLOC_RX:
7092 		db->db_ring_mask = bp->rx_ring_mask;
7093 		break;
7094 	case HWRM_RING_ALLOC_AGG:
7095 		db->db_ring_mask = bp->rx_agg_ring_mask;
7096 		break;
7097 	case HWRM_RING_ALLOC_CMPL:
7098 	case HWRM_RING_ALLOC_NQ:
7099 		db->db_ring_mask = bp->cp_ring_mask;
7100 		break;
7101 	}
7102 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7103 		db->db_epoch_mask = db->db_ring_mask + 1;
7104 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7105 	}
7106 }
7107 
7108 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7109 			u32 map_idx, u32 xid)
7110 {
7111 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7112 		switch (ring_type) {
7113 		case HWRM_RING_ALLOC_TX:
7114 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7115 			break;
7116 		case HWRM_RING_ALLOC_RX:
7117 		case HWRM_RING_ALLOC_AGG:
7118 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7119 			break;
7120 		case HWRM_RING_ALLOC_CMPL:
7121 			db->db_key64 = DBR_PATH_L2;
7122 			break;
7123 		case HWRM_RING_ALLOC_NQ:
7124 			db->db_key64 = DBR_PATH_L2;
7125 			break;
7126 		}
7127 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7128 
7129 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7130 			db->db_key64 |= DBR_VALID;
7131 
7132 		db->doorbell = bp->bar1 + bp->db_offset;
7133 	} else {
7134 		db->doorbell = bp->bar1 + map_idx * 0x80;
7135 		switch (ring_type) {
7136 		case HWRM_RING_ALLOC_TX:
7137 			db->db_key32 = DB_KEY_TX;
7138 			break;
7139 		case HWRM_RING_ALLOC_RX:
7140 		case HWRM_RING_ALLOC_AGG:
7141 			db->db_key32 = DB_KEY_RX;
7142 			break;
7143 		case HWRM_RING_ALLOC_CMPL:
7144 			db->db_key32 = DB_KEY_CP;
7145 			break;
7146 		}
7147 	}
7148 	bnxt_set_db_mask(bp, db, ring_type);
7149 }
7150 
7151 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7152 				   struct bnxt_rx_ring_info *rxr)
7153 {
7154 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7155 	struct bnxt_napi *bnapi = rxr->bnapi;
7156 	u32 type = HWRM_RING_ALLOC_RX;
7157 	u32 map_idx = bnapi->index;
7158 	int rc;
7159 
7160 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7161 	if (rc)
7162 		return rc;
7163 
7164 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7165 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7166 
7167 	return 0;
7168 }
7169 
7170 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7171 				       struct bnxt_rx_ring_info *rxr)
7172 {
7173 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7174 	u32 type = HWRM_RING_ALLOC_AGG;
7175 	u32 grp_idx = ring->grp_idx;
7176 	u32 map_idx;
7177 	int rc;
7178 
7179 	map_idx = grp_idx + bp->rx_nr_rings;
7180 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7181 	if (rc)
7182 		return rc;
7183 
7184 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7185 		    ring->fw_ring_id);
7186 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7187 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7188 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7189 
7190 	return 0;
7191 }
7192 
7193 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7194 {
7195 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7196 	int i, rc = 0;
7197 	u32 type;
7198 
7199 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7200 		type = HWRM_RING_ALLOC_NQ;
7201 	else
7202 		type = HWRM_RING_ALLOC_CMPL;
7203 	for (i = 0; i < bp->cp_nr_rings; i++) {
7204 		struct bnxt_napi *bnapi = bp->bnapi[i];
7205 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7206 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7207 		u32 map_idx = ring->map_idx;
7208 		unsigned int vector;
7209 
7210 		vector = bp->irq_tbl[map_idx].vector;
7211 		disable_irq_nosync(vector);
7212 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7213 		if (rc) {
7214 			enable_irq(vector);
7215 			goto err_out;
7216 		}
7217 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7218 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7219 		enable_irq(vector);
7220 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7221 
7222 		if (!i) {
7223 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7224 			if (rc)
7225 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7226 		}
7227 	}
7228 
7229 	type = HWRM_RING_ALLOC_TX;
7230 	for (i = 0; i < bp->tx_nr_rings; i++) {
7231 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7232 		struct bnxt_ring_struct *ring;
7233 		u32 map_idx;
7234 
7235 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7236 			struct bnxt_cp_ring_info *cpr2 = txr->tx_cpr;
7237 			struct bnxt_napi *bnapi = txr->bnapi;
7238 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7239 
7240 			ring = &cpr2->cp_ring_struct;
7241 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7242 			map_idx = bnapi->index;
7243 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7244 			if (rc)
7245 				goto err_out;
7246 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7247 				    ring->fw_ring_id);
7248 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7249 		}
7250 		ring = &txr->tx_ring_struct;
7251 		map_idx = i;
7252 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7253 		if (rc)
7254 			goto err_out;
7255 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
7256 	}
7257 
7258 	for (i = 0; i < bp->rx_nr_rings; i++) {
7259 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7260 
7261 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7262 		if (rc)
7263 			goto err_out;
7264 		/* If we have agg rings, post agg buffers first. */
7265 		if (!agg_rings)
7266 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7267 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7268 			struct bnxt_cp_ring_info *cpr2 = rxr->rx_cpr;
7269 			struct bnxt_napi *bnapi = rxr->bnapi;
7270 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7271 			struct bnxt_ring_struct *ring;
7272 			u32 map_idx = bnapi->index;
7273 
7274 			ring = &cpr2->cp_ring_struct;
7275 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7276 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7277 			if (rc)
7278 				goto err_out;
7279 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7280 				    ring->fw_ring_id);
7281 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7282 		}
7283 	}
7284 
7285 	if (agg_rings) {
7286 		for (i = 0; i < bp->rx_nr_rings; i++) {
7287 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7288 			if (rc)
7289 				goto err_out;
7290 		}
7291 	}
7292 err_out:
7293 	return rc;
7294 }
7295 
7296 static void bnxt_cancel_dim(struct bnxt *bp)
7297 {
7298 	int i;
7299 
7300 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7301 	 * if NAPI is enabled.
7302 	 */
7303 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7304 		return;
7305 
7306 	/* Make sure NAPI sees that the VNIC is disabled */
7307 	synchronize_net();
7308 	for (i = 0; i < bp->rx_nr_rings; i++) {
7309 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7310 		struct bnxt_napi *bnapi = rxr->bnapi;
7311 
7312 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7313 	}
7314 }
7315 
7316 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7317 				   struct bnxt_ring_struct *ring,
7318 				   u32 ring_type, int cmpl_ring_id)
7319 {
7320 	struct hwrm_ring_free_output *resp;
7321 	struct hwrm_ring_free_input *req;
7322 	u16 error_code = 0;
7323 	int rc;
7324 
7325 	if (BNXT_NO_FW_ACCESS(bp))
7326 		return 0;
7327 
7328 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7329 	if (rc)
7330 		goto exit;
7331 
7332 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7333 	req->ring_type = ring_type;
7334 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7335 
7336 	resp = hwrm_req_hold(bp, req);
7337 	rc = hwrm_req_send(bp, req);
7338 	error_code = le16_to_cpu(resp->error_code);
7339 	hwrm_req_drop(bp, req);
7340 exit:
7341 	if (rc || error_code) {
7342 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7343 			   ring_type, rc, error_code);
7344 		return -EIO;
7345 	}
7346 	return 0;
7347 }
7348 
7349 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7350 				   struct bnxt_rx_ring_info *rxr,
7351 				   bool close_path)
7352 {
7353 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7354 	u32 grp_idx = rxr->bnapi->index;
7355 	u32 cmpl_ring_id;
7356 
7357 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7358 		return;
7359 
7360 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7361 	hwrm_ring_free_send_msg(bp, ring,
7362 				RING_FREE_REQ_RING_TYPE_RX,
7363 				close_path ? cmpl_ring_id :
7364 				INVALID_HW_RING_ID);
7365 	ring->fw_ring_id = INVALID_HW_RING_ID;
7366 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7367 }
7368 
7369 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7370 				       struct bnxt_rx_ring_info *rxr,
7371 				       bool close_path)
7372 {
7373 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7374 	u32 grp_idx = rxr->bnapi->index;
7375 	u32 type, cmpl_ring_id;
7376 
7377 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7378 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7379 	else
7380 		type = RING_FREE_REQ_RING_TYPE_RX;
7381 
7382 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7383 		return;
7384 
7385 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7386 	hwrm_ring_free_send_msg(bp, ring, type,
7387 				close_path ? cmpl_ring_id :
7388 				INVALID_HW_RING_ID);
7389 	ring->fw_ring_id = INVALID_HW_RING_ID;
7390 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7391 }
7392 
7393 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7394 {
7395 	u32 type;
7396 	int i;
7397 
7398 	if (!bp->bnapi)
7399 		return;
7400 
7401 	for (i = 0; i < bp->tx_nr_rings; i++) {
7402 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7403 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7404 
7405 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7406 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
7407 
7408 			hwrm_ring_free_send_msg(bp, ring,
7409 						RING_FREE_REQ_RING_TYPE_TX,
7410 						close_path ? cmpl_ring_id :
7411 						INVALID_HW_RING_ID);
7412 			ring->fw_ring_id = INVALID_HW_RING_ID;
7413 		}
7414 	}
7415 
7416 	bnxt_cancel_dim(bp);
7417 	for (i = 0; i < bp->rx_nr_rings; i++) {
7418 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7419 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7420 	}
7421 
7422 	/* The completion rings are about to be freed.  After that the
7423 	 * IRQ doorbell will not work anymore.  So we need to disable
7424 	 * IRQ here.
7425 	 */
7426 	bnxt_disable_int_sync(bp);
7427 
7428 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7429 		type = RING_FREE_REQ_RING_TYPE_NQ;
7430 	else
7431 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7432 	for (i = 0; i < bp->cp_nr_rings; i++) {
7433 		struct bnxt_napi *bnapi = bp->bnapi[i];
7434 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7435 		struct bnxt_ring_struct *ring;
7436 		int j;
7437 
7438 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) {
7439 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
7440 
7441 			ring = &cpr2->cp_ring_struct;
7442 			if (ring->fw_ring_id == INVALID_HW_RING_ID)
7443 				continue;
7444 			hwrm_ring_free_send_msg(bp, ring,
7445 						RING_FREE_REQ_RING_TYPE_L2_CMPL,
7446 						INVALID_HW_RING_ID);
7447 			ring->fw_ring_id = INVALID_HW_RING_ID;
7448 		}
7449 		ring = &cpr->cp_ring_struct;
7450 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7451 			hwrm_ring_free_send_msg(bp, ring, type,
7452 						INVALID_HW_RING_ID);
7453 			ring->fw_ring_id = INVALID_HW_RING_ID;
7454 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7455 		}
7456 	}
7457 }
7458 
7459 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7460 			     bool shared);
7461 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7462 			   bool shared);
7463 
7464 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7465 {
7466 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7467 	struct hwrm_func_qcfg_output *resp;
7468 	struct hwrm_func_qcfg_input *req;
7469 	int rc;
7470 
7471 	if (bp->hwrm_spec_code < 0x10601)
7472 		return 0;
7473 
7474 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7475 	if (rc)
7476 		return rc;
7477 
7478 	req->fid = cpu_to_le16(0xffff);
7479 	resp = hwrm_req_hold(bp, req);
7480 	rc = hwrm_req_send(bp, req);
7481 	if (rc) {
7482 		hwrm_req_drop(bp, req);
7483 		return rc;
7484 	}
7485 
7486 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7487 	if (BNXT_NEW_RM(bp)) {
7488 		u16 cp, stats;
7489 
7490 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7491 		hw_resc->resv_hw_ring_grps =
7492 			le32_to_cpu(resp->alloc_hw_ring_grps);
7493 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7494 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7495 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7496 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7497 		hw_resc->resv_irqs = cp;
7498 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7499 			int rx = hw_resc->resv_rx_rings;
7500 			int tx = hw_resc->resv_tx_rings;
7501 
7502 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7503 				rx >>= 1;
7504 			if (cp < (rx + tx)) {
7505 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7506 				if (rc)
7507 					goto get_rings_exit;
7508 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7509 					rx <<= 1;
7510 				hw_resc->resv_rx_rings = rx;
7511 				hw_resc->resv_tx_rings = tx;
7512 			}
7513 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7514 			hw_resc->resv_hw_ring_grps = rx;
7515 		}
7516 		hw_resc->resv_cp_rings = cp;
7517 		hw_resc->resv_stat_ctxs = stats;
7518 	}
7519 get_rings_exit:
7520 	hwrm_req_drop(bp, req);
7521 	return rc;
7522 }
7523 
7524 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7525 {
7526 	struct hwrm_func_qcfg_output *resp;
7527 	struct hwrm_func_qcfg_input *req;
7528 	int rc;
7529 
7530 	if (bp->hwrm_spec_code < 0x10601)
7531 		return 0;
7532 
7533 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7534 	if (rc)
7535 		return rc;
7536 
7537 	req->fid = cpu_to_le16(fid);
7538 	resp = hwrm_req_hold(bp, req);
7539 	rc = hwrm_req_send(bp, req);
7540 	if (!rc)
7541 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7542 
7543 	hwrm_req_drop(bp, req);
7544 	return rc;
7545 }
7546 
7547 static bool bnxt_rfs_supported(struct bnxt *bp);
7548 
7549 static struct hwrm_func_cfg_input *
7550 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7551 {
7552 	struct hwrm_func_cfg_input *req;
7553 	u32 enables = 0;
7554 
7555 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7556 		return NULL;
7557 
7558 	req->fid = cpu_to_le16(0xffff);
7559 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7560 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7561 	if (BNXT_NEW_RM(bp)) {
7562 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7563 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7564 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7565 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7566 			enables |= hwr->cp_p5 ?
7567 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7568 		} else {
7569 			enables |= hwr->cp ?
7570 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7571 			enables |= hwr->grp ?
7572 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7573 		}
7574 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7575 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7576 					  0;
7577 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7578 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7579 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7580 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7581 			req->num_msix = cpu_to_le16(hwr->cp);
7582 		} else {
7583 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7584 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7585 		}
7586 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7587 		req->num_vnics = cpu_to_le16(hwr->vnic);
7588 	}
7589 	req->enables = cpu_to_le32(enables);
7590 	return req;
7591 }
7592 
7593 static struct hwrm_func_vf_cfg_input *
7594 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7595 {
7596 	struct hwrm_func_vf_cfg_input *req;
7597 	u32 enables = 0;
7598 
7599 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7600 		return NULL;
7601 
7602 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7603 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7604 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7605 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7606 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7607 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7608 		enables |= hwr->cp_p5 ?
7609 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7610 	} else {
7611 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7612 		enables |= hwr->grp ?
7613 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7614 	}
7615 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7616 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7617 
7618 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7619 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7620 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7621 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7622 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7623 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7624 	} else {
7625 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7626 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7627 	}
7628 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7629 	req->num_vnics = cpu_to_le16(hwr->vnic);
7630 
7631 	req->enables = cpu_to_le32(enables);
7632 	return req;
7633 }
7634 
7635 static int
7636 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7637 {
7638 	struct hwrm_func_cfg_input *req;
7639 	int rc;
7640 
7641 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7642 	if (!req)
7643 		return -ENOMEM;
7644 
7645 	if (!req->enables) {
7646 		hwrm_req_drop(bp, req);
7647 		return 0;
7648 	}
7649 
7650 	rc = hwrm_req_send(bp, req);
7651 	if (rc)
7652 		return rc;
7653 
7654 	if (bp->hwrm_spec_code < 0x10601)
7655 		bp->hw_resc.resv_tx_rings = hwr->tx;
7656 
7657 	return bnxt_hwrm_get_rings(bp);
7658 }
7659 
7660 static int
7661 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7662 {
7663 	struct hwrm_func_vf_cfg_input *req;
7664 	int rc;
7665 
7666 	if (!BNXT_NEW_RM(bp)) {
7667 		bp->hw_resc.resv_tx_rings = hwr->tx;
7668 		return 0;
7669 	}
7670 
7671 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7672 	if (!req)
7673 		return -ENOMEM;
7674 
7675 	rc = hwrm_req_send(bp, req);
7676 	if (rc)
7677 		return rc;
7678 
7679 	return bnxt_hwrm_get_rings(bp);
7680 }
7681 
7682 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7683 {
7684 	if (BNXT_PF(bp))
7685 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7686 	else
7687 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7688 }
7689 
7690 int bnxt_nq_rings_in_use(struct bnxt *bp)
7691 {
7692 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7693 }
7694 
7695 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7696 {
7697 	int cp;
7698 
7699 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7700 		return bnxt_nq_rings_in_use(bp);
7701 
7702 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7703 	return cp;
7704 }
7705 
7706 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7707 {
7708 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7709 }
7710 
7711 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7712 {
7713 	if (!hwr->grp)
7714 		return 0;
7715 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7716 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7717 
7718 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7719 			rss_ctx *= hwr->vnic;
7720 		return rss_ctx;
7721 	}
7722 	if (BNXT_VF(bp))
7723 		return BNXT_VF_MAX_RSS_CTX;
7724 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7725 		return hwr->grp + 1;
7726 	return 1;
7727 }
7728 
7729 /* Check if a default RSS map needs to be setup.  This function is only
7730  * used on older firmware that does not require reserving RX rings.
7731  */
7732 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7733 {
7734 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7735 
7736 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7737 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7738 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7739 		if (!netif_is_rxfh_configured(bp->dev))
7740 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7741 	}
7742 }
7743 
7744 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7745 {
7746 	if (bp->flags & BNXT_FLAG_RFS) {
7747 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7748 			return 2 + bp->num_rss_ctx;
7749 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7750 			return rx_rings + 1;
7751 	}
7752 	return 1;
7753 }
7754 
7755 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7756 {
7757 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7758 	int cp = bnxt_cp_rings_in_use(bp);
7759 	int nq = bnxt_nq_rings_in_use(bp);
7760 	int rx = bp->rx_nr_rings, stat;
7761 	int vnic, grp = rx;
7762 
7763 	/* Old firmware does not need RX ring reservations but we still
7764 	 * need to setup a default RSS map when needed.  With new firmware
7765 	 * we go through RX ring reservations first and then set up the
7766 	 * RSS map for the successfully reserved RX rings when needed.
7767 	 */
7768 	if (!BNXT_NEW_RM(bp))
7769 		bnxt_check_rss_tbl_no_rmgr(bp);
7770 
7771 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7772 	    bp->hwrm_spec_code >= 0x10601)
7773 		return true;
7774 
7775 	if (!BNXT_NEW_RM(bp))
7776 		return false;
7777 
7778 	vnic = bnxt_get_total_vnics(bp, rx);
7779 
7780 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7781 		rx <<= 1;
7782 	stat = bnxt_get_func_stat_ctxs(bp);
7783 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7784 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7785 	    (hw_resc->resv_hw_ring_grps != grp &&
7786 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7787 		return true;
7788 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7789 	    hw_resc->resv_irqs != nq)
7790 		return true;
7791 	return false;
7792 }
7793 
7794 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7795 {
7796 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7797 
7798 	hwr->tx = hw_resc->resv_tx_rings;
7799 	if (BNXT_NEW_RM(bp)) {
7800 		hwr->rx = hw_resc->resv_rx_rings;
7801 		hwr->cp = hw_resc->resv_irqs;
7802 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7803 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7804 		hwr->grp = hw_resc->resv_hw_ring_grps;
7805 		hwr->vnic = hw_resc->resv_vnics;
7806 		hwr->stat = hw_resc->resv_stat_ctxs;
7807 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7808 	}
7809 }
7810 
7811 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7812 {
7813 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7814 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7815 }
7816 
7817 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7818 
7819 static int __bnxt_reserve_rings(struct bnxt *bp)
7820 {
7821 	struct bnxt_hw_rings hwr = {0};
7822 	int rx_rings, old_rx_rings, rc;
7823 	int cp = bp->cp_nr_rings;
7824 	int ulp_msix = 0;
7825 	bool sh = false;
7826 	int tx_cp;
7827 
7828 	if (!bnxt_need_reserve_rings(bp))
7829 		return 0;
7830 
7831 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7832 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7833 		if (!ulp_msix)
7834 			bnxt_set_ulp_stat_ctxs(bp, 0);
7835 
7836 		if (ulp_msix > bp->ulp_num_msix_want)
7837 			ulp_msix = bp->ulp_num_msix_want;
7838 		hwr.cp = cp + ulp_msix;
7839 	} else {
7840 		hwr.cp = bnxt_nq_rings_in_use(bp);
7841 	}
7842 
7843 	hwr.tx = bp->tx_nr_rings;
7844 	hwr.rx = bp->rx_nr_rings;
7845 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7846 		sh = true;
7847 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7848 		hwr.cp_p5 = hwr.rx + hwr.tx;
7849 
7850 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7851 
7852 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7853 		hwr.rx <<= 1;
7854 	hwr.grp = bp->rx_nr_rings;
7855 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7856 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7857 	old_rx_rings = bp->hw_resc.resv_rx_rings;
7858 
7859 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7860 	if (rc)
7861 		return rc;
7862 
7863 	bnxt_copy_reserved_rings(bp, &hwr);
7864 
7865 	rx_rings = hwr.rx;
7866 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7867 		if (hwr.rx >= 2) {
7868 			rx_rings = hwr.rx >> 1;
7869 		} else {
7870 			if (netif_running(bp->dev))
7871 				return -ENOMEM;
7872 
7873 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7874 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7875 			bp->dev->hw_features &= ~NETIF_F_LRO;
7876 			bp->dev->features &= ~NETIF_F_LRO;
7877 			bnxt_set_ring_params(bp);
7878 		}
7879 	}
7880 	rx_rings = min_t(int, rx_rings, hwr.grp);
7881 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7882 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7883 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7884 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7885 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7886 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7887 		hwr.rx = rx_rings << 1;
7888 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
7889 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
7890 	bp->tx_nr_rings = hwr.tx;
7891 
7892 	/* If we cannot reserve all the RX rings, reset the RSS map only
7893 	 * if absolutely necessary
7894 	 */
7895 	if (rx_rings != bp->rx_nr_rings) {
7896 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
7897 			    rx_rings, bp->rx_nr_rings);
7898 		if (netif_is_rxfh_configured(bp->dev) &&
7899 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
7900 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
7901 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
7902 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
7903 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
7904 		}
7905 	}
7906 	bp->rx_nr_rings = rx_rings;
7907 	bp->cp_nr_rings = hwr.cp;
7908 
7909 	if (!bnxt_rings_ok(bp, &hwr))
7910 		return -ENOMEM;
7911 
7912 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
7913 	    !netif_is_rxfh_configured(bp->dev))
7914 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7915 
7916 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
7917 		int resv_msix, resv_ctx, ulp_ctxs;
7918 		struct bnxt_hw_resc *hw_resc;
7919 
7920 		hw_resc = &bp->hw_resc;
7921 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
7922 		ulp_msix = min_t(int, resv_msix, ulp_msix);
7923 		bnxt_set_ulp_msix_num(bp, ulp_msix);
7924 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
7925 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
7926 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
7927 	}
7928 
7929 	return rc;
7930 }
7931 
7932 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7933 {
7934 	struct hwrm_func_vf_cfg_input *req;
7935 	u32 flags;
7936 
7937 	if (!BNXT_NEW_RM(bp))
7938 		return 0;
7939 
7940 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7941 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
7942 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7943 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7944 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7945 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
7946 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
7947 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7948 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7949 
7950 	req->flags = cpu_to_le32(flags);
7951 	return hwrm_req_send_silent(bp, req);
7952 }
7953 
7954 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7955 {
7956 	struct hwrm_func_cfg_input *req;
7957 	u32 flags;
7958 
7959 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7960 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
7961 	if (BNXT_NEW_RM(bp)) {
7962 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7963 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7964 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7965 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
7966 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7967 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
7968 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
7969 		else
7970 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7971 	}
7972 
7973 	req->flags = cpu_to_le32(flags);
7974 	return hwrm_req_send_silent(bp, req);
7975 }
7976 
7977 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7978 {
7979 	if (bp->hwrm_spec_code < 0x10801)
7980 		return 0;
7981 
7982 	if (BNXT_PF(bp))
7983 		return bnxt_hwrm_check_pf_rings(bp, hwr);
7984 
7985 	return bnxt_hwrm_check_vf_rings(bp, hwr);
7986 }
7987 
7988 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
7989 {
7990 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7991 	struct hwrm_ring_aggint_qcaps_output *resp;
7992 	struct hwrm_ring_aggint_qcaps_input *req;
7993 	int rc;
7994 
7995 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
7996 	coal_cap->num_cmpl_dma_aggr_max = 63;
7997 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
7998 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
7999 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8000 	coal_cap->int_lat_tmr_min_max = 65535;
8001 	coal_cap->int_lat_tmr_max_max = 65535;
8002 	coal_cap->num_cmpl_aggr_int_max = 65535;
8003 	coal_cap->timer_units = 80;
8004 
8005 	if (bp->hwrm_spec_code < 0x10902)
8006 		return;
8007 
8008 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8009 		return;
8010 
8011 	resp = hwrm_req_hold(bp, req);
8012 	rc = hwrm_req_send_silent(bp, req);
8013 	if (!rc) {
8014 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8015 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8016 		coal_cap->num_cmpl_dma_aggr_max =
8017 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8018 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8019 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8020 		coal_cap->cmpl_aggr_dma_tmr_max =
8021 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8022 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8023 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8024 		coal_cap->int_lat_tmr_min_max =
8025 			le16_to_cpu(resp->int_lat_tmr_min_max);
8026 		coal_cap->int_lat_tmr_max_max =
8027 			le16_to_cpu(resp->int_lat_tmr_max_max);
8028 		coal_cap->num_cmpl_aggr_int_max =
8029 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8030 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8031 	}
8032 	hwrm_req_drop(bp, req);
8033 }
8034 
8035 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8036 {
8037 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8038 
8039 	return usec * 1000 / coal_cap->timer_units;
8040 }
8041 
8042 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8043 	struct bnxt_coal *hw_coal,
8044 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8045 {
8046 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8047 	u16 val, tmr, max, flags = hw_coal->flags;
8048 	u32 cmpl_params = coal_cap->cmpl_params;
8049 
8050 	max = hw_coal->bufs_per_record * 128;
8051 	if (hw_coal->budget)
8052 		max = hw_coal->bufs_per_record * hw_coal->budget;
8053 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8054 
8055 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8056 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8057 
8058 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8059 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8060 
8061 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8062 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8063 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8064 
8065 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8066 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8067 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8068 
8069 	/* min timer set to 1/2 of interrupt timer */
8070 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8071 		val = tmr / 2;
8072 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8073 		req->int_lat_tmr_min = cpu_to_le16(val);
8074 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8075 	}
8076 
8077 	/* buf timer set to 1/4 of interrupt timer */
8078 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8079 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8080 
8081 	if (cmpl_params &
8082 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8083 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8084 		val = clamp_t(u16, tmr, 1,
8085 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8086 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8087 		req->enables |=
8088 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8089 	}
8090 
8091 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8092 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8093 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8094 	req->flags = cpu_to_le16(flags);
8095 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8096 }
8097 
8098 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8099 				   struct bnxt_coal *hw_coal)
8100 {
8101 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8102 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8103 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8104 	u32 nq_params = coal_cap->nq_params;
8105 	u16 tmr;
8106 	int rc;
8107 
8108 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8109 		return 0;
8110 
8111 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8112 	if (rc)
8113 		return rc;
8114 
8115 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8116 	req->flags =
8117 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8118 
8119 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8120 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8121 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8122 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8123 	return hwrm_req_send(bp, req);
8124 }
8125 
8126 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8127 {
8128 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8129 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8130 	struct bnxt_coal coal;
8131 	int rc;
8132 
8133 	/* Tick values in micro seconds.
8134 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8135 	 */
8136 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8137 
8138 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8139 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8140 
8141 	if (!bnapi->rx_ring)
8142 		return -ENODEV;
8143 
8144 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8145 	if (rc)
8146 		return rc;
8147 
8148 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8149 
8150 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8151 
8152 	return hwrm_req_send(bp, req_rx);
8153 }
8154 
8155 static int
8156 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8157 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8158 {
8159 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8160 
8161 	req->ring_id = cpu_to_le16(ring_id);
8162 	return hwrm_req_send(bp, req);
8163 }
8164 
8165 static int
8166 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8167 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8168 {
8169 	struct bnxt_tx_ring_info *txr;
8170 	int i, rc;
8171 
8172 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8173 		u16 ring_id;
8174 
8175 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8176 		req->ring_id = cpu_to_le16(ring_id);
8177 		rc = hwrm_req_send(bp, req);
8178 		if (rc)
8179 			return rc;
8180 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8181 			return 0;
8182 	}
8183 	return 0;
8184 }
8185 
8186 int bnxt_hwrm_set_coal(struct bnxt *bp)
8187 {
8188 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8189 	int i, rc;
8190 
8191 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8192 	if (rc)
8193 		return rc;
8194 
8195 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8196 	if (rc) {
8197 		hwrm_req_drop(bp, req_rx);
8198 		return rc;
8199 	}
8200 
8201 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8202 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8203 
8204 	hwrm_req_hold(bp, req_rx);
8205 	hwrm_req_hold(bp, req_tx);
8206 	for (i = 0; i < bp->cp_nr_rings; i++) {
8207 		struct bnxt_napi *bnapi = bp->bnapi[i];
8208 		struct bnxt_coal *hw_coal;
8209 
8210 		if (!bnapi->rx_ring)
8211 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8212 		else
8213 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8214 		if (rc)
8215 			break;
8216 
8217 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8218 			continue;
8219 
8220 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8221 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8222 			if (rc)
8223 				break;
8224 		}
8225 		if (bnapi->rx_ring)
8226 			hw_coal = &bp->rx_coal;
8227 		else
8228 			hw_coal = &bp->tx_coal;
8229 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8230 	}
8231 	hwrm_req_drop(bp, req_rx);
8232 	hwrm_req_drop(bp, req_tx);
8233 	return rc;
8234 }
8235 
8236 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8237 {
8238 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8239 	struct hwrm_stat_ctx_free_input *req;
8240 	int i;
8241 
8242 	if (!bp->bnapi)
8243 		return;
8244 
8245 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8246 		return;
8247 
8248 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8249 		return;
8250 	if (BNXT_FW_MAJ(bp) <= 20) {
8251 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8252 			hwrm_req_drop(bp, req);
8253 			return;
8254 		}
8255 		hwrm_req_hold(bp, req0);
8256 	}
8257 	hwrm_req_hold(bp, req);
8258 	for (i = 0; i < bp->cp_nr_rings; i++) {
8259 		struct bnxt_napi *bnapi = bp->bnapi[i];
8260 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8261 
8262 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8263 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8264 			if (req0) {
8265 				req0->stat_ctx_id = req->stat_ctx_id;
8266 				hwrm_req_send(bp, req0);
8267 			}
8268 			hwrm_req_send(bp, req);
8269 
8270 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8271 		}
8272 	}
8273 	hwrm_req_drop(bp, req);
8274 	if (req0)
8275 		hwrm_req_drop(bp, req0);
8276 }
8277 
8278 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8279 {
8280 	struct hwrm_stat_ctx_alloc_output *resp;
8281 	struct hwrm_stat_ctx_alloc_input *req;
8282 	int rc, i;
8283 
8284 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8285 		return 0;
8286 
8287 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8288 	if (rc)
8289 		return rc;
8290 
8291 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8292 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8293 
8294 	resp = hwrm_req_hold(bp, req);
8295 	for (i = 0; i < bp->cp_nr_rings; i++) {
8296 		struct bnxt_napi *bnapi = bp->bnapi[i];
8297 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8298 
8299 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8300 
8301 		rc = hwrm_req_send(bp, req);
8302 		if (rc)
8303 			break;
8304 
8305 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8306 
8307 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8308 	}
8309 	hwrm_req_drop(bp, req);
8310 	return rc;
8311 }
8312 
8313 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8314 {
8315 	struct hwrm_func_qcfg_output *resp;
8316 	struct hwrm_func_qcfg_input *req;
8317 	u16 flags;
8318 	int rc;
8319 
8320 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8321 	if (rc)
8322 		return rc;
8323 
8324 	req->fid = cpu_to_le16(0xffff);
8325 	resp = hwrm_req_hold(bp, req);
8326 	rc = hwrm_req_send(bp, req);
8327 	if (rc)
8328 		goto func_qcfg_exit;
8329 
8330 	flags = le16_to_cpu(resp->flags);
8331 #ifdef CONFIG_BNXT_SRIOV
8332 	if (BNXT_VF(bp)) {
8333 		struct bnxt_vf_info *vf = &bp->vf;
8334 
8335 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8336 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8337 			vf->flags |= BNXT_VF_TRUST;
8338 		else
8339 			vf->flags &= ~BNXT_VF_TRUST;
8340 	} else {
8341 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8342 	}
8343 #endif
8344 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8345 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8346 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8347 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8348 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8349 	}
8350 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8351 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8352 
8353 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8354 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8355 
8356 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8357 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8358 
8359 	switch (resp->port_partition_type) {
8360 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8361 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8362 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8363 		bp->port_partition_type = resp->port_partition_type;
8364 		break;
8365 	}
8366 	if (bp->hwrm_spec_code < 0x10707 ||
8367 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8368 		bp->br_mode = BRIDGE_MODE_VEB;
8369 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8370 		bp->br_mode = BRIDGE_MODE_VEPA;
8371 	else
8372 		bp->br_mode = BRIDGE_MODE_UNDEF;
8373 
8374 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8375 	if (!bp->max_mtu)
8376 		bp->max_mtu = BNXT_MAX_MTU;
8377 
8378 	if (bp->db_size)
8379 		goto func_qcfg_exit;
8380 
8381 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8382 	if (BNXT_CHIP_P5(bp)) {
8383 		if (BNXT_PF(bp))
8384 			bp->db_offset = DB_PF_OFFSET_P5;
8385 		else
8386 			bp->db_offset = DB_VF_OFFSET_P5;
8387 	}
8388 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8389 				 1024);
8390 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8391 	    bp->db_size <= bp->db_offset)
8392 		bp->db_size = pci_resource_len(bp->pdev, 2);
8393 
8394 func_qcfg_exit:
8395 	hwrm_req_drop(bp, req);
8396 	return rc;
8397 }
8398 
8399 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8400 				      u8 init_val, u8 init_offset,
8401 				      bool init_mask_set)
8402 {
8403 	ctxm->init_value = init_val;
8404 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8405 	if (init_mask_set)
8406 		ctxm->init_offset = init_offset * 4;
8407 	else
8408 		ctxm->init_value = 0;
8409 }
8410 
8411 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8412 {
8413 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8414 	u16 type;
8415 
8416 	for (type = 0; type < ctx_max; type++) {
8417 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8418 		int n = 1;
8419 
8420 		if (!ctxm->max_entries || ctxm->pg_info)
8421 			continue;
8422 
8423 		if (ctxm->instance_bmap)
8424 			n = hweight32(ctxm->instance_bmap);
8425 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8426 		if (!ctxm->pg_info)
8427 			return -ENOMEM;
8428 	}
8429 	return 0;
8430 }
8431 
8432 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8433 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8434 
8435 #define BNXT_CTX_INIT_VALID(flags)	\
8436 	(!!((flags) &			\
8437 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8438 
8439 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8440 {
8441 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8442 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8443 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8444 	u16 type;
8445 	int rc;
8446 
8447 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8448 	if (rc)
8449 		return rc;
8450 
8451 	if (!ctx) {
8452 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8453 		if (!ctx)
8454 			return -ENOMEM;
8455 		bp->ctx = ctx;
8456 	}
8457 
8458 	resp = hwrm_req_hold(bp, req);
8459 
8460 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8461 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8462 		u8 init_val, init_off, i;
8463 		u32 max_entries;
8464 		u16 entry_size;
8465 		__le32 *p;
8466 		u32 flags;
8467 
8468 		req->type = cpu_to_le16(type);
8469 		rc = hwrm_req_send(bp, req);
8470 		if (rc)
8471 			goto ctx_done;
8472 		flags = le32_to_cpu(resp->flags);
8473 		type = le16_to_cpu(resp->next_valid_type);
8474 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8475 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8476 			continue;
8477 		}
8478 		entry_size = le16_to_cpu(resp->entry_size);
8479 		max_entries = le32_to_cpu(resp->max_num_entries);
8480 		if (ctxm->mem_valid) {
8481 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8482 			    ctxm->entry_size != entry_size ||
8483 			    ctxm->max_entries != max_entries)
8484 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8485 			else
8486 				continue;
8487 		}
8488 		ctxm->type = le16_to_cpu(resp->type);
8489 		ctxm->entry_size = entry_size;
8490 		ctxm->flags = flags;
8491 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8492 		ctxm->entry_multiple = resp->entry_multiple;
8493 		ctxm->max_entries = max_entries;
8494 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8495 		init_val = resp->ctx_init_value;
8496 		init_off = resp->ctx_init_offset;
8497 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8498 					  BNXT_CTX_INIT_VALID(flags));
8499 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8500 					      BNXT_MAX_SPLIT_ENTRY);
8501 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8502 		     i++, p++)
8503 			ctxm->split[i] = le32_to_cpu(*p);
8504 	}
8505 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8506 
8507 ctx_done:
8508 	hwrm_req_drop(bp, req);
8509 	return rc;
8510 }
8511 
8512 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8513 {
8514 	struct hwrm_func_backing_store_qcaps_output *resp;
8515 	struct hwrm_func_backing_store_qcaps_input *req;
8516 	int rc;
8517 
8518 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8519 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8520 		return 0;
8521 
8522 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8523 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8524 
8525 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8526 	if (rc)
8527 		return rc;
8528 
8529 	resp = hwrm_req_hold(bp, req);
8530 	rc = hwrm_req_send_silent(bp, req);
8531 	if (!rc) {
8532 		struct bnxt_ctx_mem_type *ctxm;
8533 		struct bnxt_ctx_mem_info *ctx;
8534 		u8 init_val, init_idx = 0;
8535 		u16 init_mask;
8536 
8537 		ctx = bp->ctx;
8538 		if (!ctx) {
8539 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8540 			if (!ctx) {
8541 				rc = -ENOMEM;
8542 				goto ctx_err;
8543 			}
8544 			bp->ctx = ctx;
8545 		}
8546 		init_val = resp->ctx_kind_initializer;
8547 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8548 
8549 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8550 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8551 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8552 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8553 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8554 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8555 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8556 					  (init_mask & (1 << init_idx++)) != 0);
8557 
8558 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8559 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8560 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8561 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8562 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8563 					  (init_mask & (1 << init_idx++)) != 0);
8564 
8565 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8566 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8567 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8568 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8569 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8570 					  (init_mask & (1 << init_idx++)) != 0);
8571 
8572 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8573 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8574 		ctxm->max_entries = ctxm->vnic_entries +
8575 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8576 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8577 		bnxt_init_ctx_initializer(ctxm, init_val,
8578 					  resp->vnic_init_offset,
8579 					  (init_mask & (1 << init_idx++)) != 0);
8580 
8581 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8582 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8583 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8584 		bnxt_init_ctx_initializer(ctxm, init_val,
8585 					  resp->stat_init_offset,
8586 					  (init_mask & (1 << init_idx++)) != 0);
8587 
8588 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8589 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8590 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8591 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8592 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8593 		if (!ctxm->entry_multiple)
8594 			ctxm->entry_multiple = 1;
8595 
8596 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8597 
8598 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8599 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8600 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8601 		ctxm->mrav_num_entries_units =
8602 			le16_to_cpu(resp->mrav_num_entries_units);
8603 		bnxt_init_ctx_initializer(ctxm, init_val,
8604 					  resp->mrav_init_offset,
8605 					  (init_mask & (1 << init_idx++)) != 0);
8606 
8607 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8608 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8609 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8610 
8611 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8612 		if (!ctx->tqm_fp_rings_count)
8613 			ctx->tqm_fp_rings_count = bp->max_q;
8614 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8615 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8616 
8617 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8618 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8619 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8620 
8621 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8622 	} else {
8623 		rc = 0;
8624 	}
8625 ctx_err:
8626 	hwrm_req_drop(bp, req);
8627 	return rc;
8628 }
8629 
8630 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8631 				  __le64 *pg_dir)
8632 {
8633 	if (!rmem->nr_pages)
8634 		return;
8635 
8636 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8637 	if (rmem->depth >= 1) {
8638 		if (rmem->depth == 2)
8639 			*pg_attr |= 2;
8640 		else
8641 			*pg_attr |= 1;
8642 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8643 	} else {
8644 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8645 	}
8646 }
8647 
8648 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8649 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8650 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8651 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8652 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8653 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8654 
8655 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8656 {
8657 	struct hwrm_func_backing_store_cfg_input *req;
8658 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8659 	struct bnxt_ctx_pg_info *ctx_pg;
8660 	struct bnxt_ctx_mem_type *ctxm;
8661 	void **__req = (void **)&req;
8662 	u32 req_len = sizeof(*req);
8663 	__le32 *num_entries;
8664 	__le64 *pg_dir;
8665 	u32 flags = 0;
8666 	u8 *pg_attr;
8667 	u32 ena;
8668 	int rc;
8669 	int i;
8670 
8671 	if (!ctx)
8672 		return 0;
8673 
8674 	if (req_len > bp->hwrm_max_ext_req_len)
8675 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8676 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8677 	if (rc)
8678 		return rc;
8679 
8680 	req->enables = cpu_to_le32(enables);
8681 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8682 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8683 		ctx_pg = ctxm->pg_info;
8684 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8685 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8686 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8687 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8688 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8689 				      &req->qpc_pg_size_qpc_lvl,
8690 				      &req->qpc_page_dir);
8691 
8692 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8693 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8694 	}
8695 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8696 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8697 		ctx_pg = ctxm->pg_info;
8698 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8699 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8700 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8701 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8702 				      &req->srq_pg_size_srq_lvl,
8703 				      &req->srq_page_dir);
8704 	}
8705 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8706 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8707 		ctx_pg = ctxm->pg_info;
8708 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8709 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8710 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8711 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8712 				      &req->cq_pg_size_cq_lvl,
8713 				      &req->cq_page_dir);
8714 	}
8715 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8716 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8717 		ctx_pg = ctxm->pg_info;
8718 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8719 		req->vnic_num_ring_table_entries =
8720 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8721 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8722 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8723 				      &req->vnic_pg_size_vnic_lvl,
8724 				      &req->vnic_page_dir);
8725 	}
8726 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8727 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8728 		ctx_pg = ctxm->pg_info;
8729 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8730 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8731 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8732 				      &req->stat_pg_size_stat_lvl,
8733 				      &req->stat_page_dir);
8734 	}
8735 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8736 		u32 units;
8737 
8738 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8739 		ctx_pg = ctxm->pg_info;
8740 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8741 		units = ctxm->mrav_num_entries_units;
8742 		if (units) {
8743 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8744 			u32 entries;
8745 
8746 			num_mr = ctx_pg->entries - num_ah;
8747 			entries = ((num_mr / units) << 16) | (num_ah / units);
8748 			req->mrav_num_entries = cpu_to_le32(entries);
8749 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8750 		}
8751 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8752 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8753 				      &req->mrav_pg_size_mrav_lvl,
8754 				      &req->mrav_page_dir);
8755 	}
8756 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8757 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8758 		ctx_pg = ctxm->pg_info;
8759 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8760 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8761 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8762 				      &req->tim_pg_size_tim_lvl,
8763 				      &req->tim_page_dir);
8764 	}
8765 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8766 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8767 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8768 	     pg_dir = &req->tqm_sp_page_dir,
8769 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8770 	     ctx_pg = ctxm->pg_info;
8771 	     i < BNXT_MAX_TQM_RINGS;
8772 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8773 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8774 		if (!(enables & ena))
8775 			continue;
8776 
8777 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8778 		*num_entries = cpu_to_le32(ctx_pg->entries);
8779 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8780 	}
8781 	req->flags = cpu_to_le32(flags);
8782 	return hwrm_req_send(bp, req);
8783 }
8784 
8785 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8786 				  struct bnxt_ctx_pg_info *ctx_pg)
8787 {
8788 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8789 
8790 	rmem->page_size = BNXT_PAGE_SIZE;
8791 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8792 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8793 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8794 	if (rmem->depth >= 1)
8795 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8796 	return bnxt_alloc_ring(bp, rmem);
8797 }
8798 
8799 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8800 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8801 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8802 {
8803 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8804 	int rc;
8805 
8806 	if (!mem_size)
8807 		return -EINVAL;
8808 
8809 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8810 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8811 		ctx_pg->nr_pages = 0;
8812 		return -EINVAL;
8813 	}
8814 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8815 		int nr_tbls, i;
8816 
8817 		rmem->depth = 2;
8818 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8819 					     GFP_KERNEL);
8820 		if (!ctx_pg->ctx_pg_tbl)
8821 			return -ENOMEM;
8822 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8823 		rmem->nr_pages = nr_tbls;
8824 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8825 		if (rc)
8826 			return rc;
8827 		for (i = 0; i < nr_tbls; i++) {
8828 			struct bnxt_ctx_pg_info *pg_tbl;
8829 
8830 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8831 			if (!pg_tbl)
8832 				return -ENOMEM;
8833 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8834 			rmem = &pg_tbl->ring_mem;
8835 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8836 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8837 			rmem->depth = 1;
8838 			rmem->nr_pages = MAX_CTX_PAGES;
8839 			rmem->ctx_mem = ctxm;
8840 			if (i == (nr_tbls - 1)) {
8841 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8842 
8843 				if (rem)
8844 					rmem->nr_pages = rem;
8845 			}
8846 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8847 			if (rc)
8848 				break;
8849 		}
8850 	} else {
8851 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8852 		if (rmem->nr_pages > 1 || depth)
8853 			rmem->depth = 1;
8854 		rmem->ctx_mem = ctxm;
8855 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8856 	}
8857 	return rc;
8858 }
8859 
8860 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
8861 				    struct bnxt_ctx_pg_info *ctx_pg,
8862 				    void *buf, size_t offset, size_t head,
8863 				    size_t tail)
8864 {
8865 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8866 	size_t nr_pages = ctx_pg->nr_pages;
8867 	int page_size = rmem->page_size;
8868 	size_t len = 0, total_len = 0;
8869 	u16 depth = rmem->depth;
8870 
8871 	tail %= nr_pages * page_size;
8872 	do {
8873 		if (depth > 1) {
8874 			int i = head / (page_size * MAX_CTX_PAGES);
8875 			struct bnxt_ctx_pg_info *pg_tbl;
8876 
8877 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8878 			rmem = &pg_tbl->ring_mem;
8879 		}
8880 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
8881 		head += len;
8882 		offset += len;
8883 		total_len += len;
8884 		if (head >= nr_pages * page_size)
8885 			head = 0;
8886 	} while (head != tail);
8887 	return total_len;
8888 }
8889 
8890 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
8891 				  struct bnxt_ctx_pg_info *ctx_pg)
8892 {
8893 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8894 
8895 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
8896 	    ctx_pg->ctx_pg_tbl) {
8897 		int i, nr_tbls = rmem->nr_pages;
8898 
8899 		for (i = 0; i < nr_tbls; i++) {
8900 			struct bnxt_ctx_pg_info *pg_tbl;
8901 			struct bnxt_ring_mem_info *rmem2;
8902 
8903 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8904 			if (!pg_tbl)
8905 				continue;
8906 			rmem2 = &pg_tbl->ring_mem;
8907 			bnxt_free_ring(bp, rmem2);
8908 			ctx_pg->ctx_pg_arr[i] = NULL;
8909 			kfree(pg_tbl);
8910 			ctx_pg->ctx_pg_tbl[i] = NULL;
8911 		}
8912 		kfree(ctx_pg->ctx_pg_tbl);
8913 		ctx_pg->ctx_pg_tbl = NULL;
8914 	}
8915 	bnxt_free_ring(bp, rmem);
8916 	ctx_pg->nr_pages = 0;
8917 }
8918 
8919 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
8920 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
8921 				   u8 pg_lvl)
8922 {
8923 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8924 	int i, rc = 0, n = 1;
8925 	u32 mem_size;
8926 
8927 	if (!ctxm->entry_size || !ctx_pg)
8928 		return -EINVAL;
8929 	if (ctxm->instance_bmap)
8930 		n = hweight32(ctxm->instance_bmap);
8931 	if (ctxm->entry_multiple)
8932 		entries = roundup(entries, ctxm->entry_multiple);
8933 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
8934 	mem_size = entries * ctxm->entry_size;
8935 	for (i = 0; i < n && !rc; i++) {
8936 		ctx_pg[i].entries = entries;
8937 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
8938 					    ctxm->init_value ? ctxm : NULL);
8939 	}
8940 	if (!rc)
8941 		ctxm->mem_valid = 1;
8942 	return rc;
8943 }
8944 
8945 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
8946 					       struct bnxt_ctx_mem_type *ctxm,
8947 					       bool last)
8948 {
8949 	struct hwrm_func_backing_store_cfg_v2_input *req;
8950 	u32 instance_bmap = ctxm->instance_bmap;
8951 	int i, j, rc = 0, n = 1;
8952 	__le32 *p;
8953 
8954 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
8955 		return 0;
8956 
8957 	if (instance_bmap)
8958 		n = hweight32(ctxm->instance_bmap);
8959 	else
8960 		instance_bmap = 1;
8961 
8962 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
8963 	if (rc)
8964 		return rc;
8965 	hwrm_req_hold(bp, req);
8966 	req->type = cpu_to_le16(ctxm->type);
8967 	req->entry_size = cpu_to_le16(ctxm->entry_size);
8968 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
8969 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
8970 		struct bnxt_bs_trace_info *bs_trace;
8971 		u32 enables;
8972 
8973 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
8974 		req->enables = cpu_to_le32(enables);
8975 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
8976 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
8977 	}
8978 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
8979 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
8980 		p[i] = cpu_to_le32(ctxm->split[i]);
8981 	for (i = 0, j = 0; j < n && !rc; i++) {
8982 		struct bnxt_ctx_pg_info *ctx_pg;
8983 
8984 		if (!(instance_bmap & (1 << i)))
8985 			continue;
8986 		req->instance = cpu_to_le16(i);
8987 		ctx_pg = &ctxm->pg_info[j++];
8988 		if (!ctx_pg->entries)
8989 			continue;
8990 		req->num_entries = cpu_to_le32(ctx_pg->entries);
8991 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8992 				      &req->page_size_pbl_level,
8993 				      &req->page_dir);
8994 		if (last && j == n)
8995 			req->flags =
8996 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
8997 		rc = hwrm_req_send(bp, req);
8998 	}
8999 	hwrm_req_drop(bp, req);
9000 	return rc;
9001 }
9002 
9003 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
9004 {
9005 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9006 	struct bnxt_ctx_mem_type *ctxm;
9007 	u16 last_type = BNXT_CTX_INV;
9008 	int rc = 0;
9009 	u16 type;
9010 
9011 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) {
9012 		ctxm = &ctx->ctx_arr[type];
9013 		if (!bnxt_bs_trace_avail(bp, type))
9014 			continue;
9015 		if (!ctxm->mem_valid) {
9016 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9017 						     ctxm->max_entries, 1);
9018 			if (rc) {
9019 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9020 					    type);
9021 				continue;
9022 			}
9023 			bnxt_bs_trace_init(bp, ctxm);
9024 		}
9025 		last_type = type;
9026 	}
9027 
9028 	if (last_type == BNXT_CTX_INV) {
9029 		if (!ena)
9030 			return 0;
9031 		else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
9032 			last_type = BNXT_CTX_MAX - 1;
9033 		else
9034 			last_type = BNXT_CTX_L2_MAX - 1;
9035 	}
9036 	ctx->ctx_arr[last_type].last = 1;
9037 
9038 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9039 		ctxm = &ctx->ctx_arr[type];
9040 
9041 		if (!ctxm->mem_valid)
9042 			continue;
9043 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9044 		if (rc)
9045 			return rc;
9046 	}
9047 	return 0;
9048 }
9049 
9050 /**
9051  * __bnxt_copy_ctx_mem - copy host context memory
9052  * @bp: The driver context
9053  * @ctxm: The pointer to the context memory type
9054  * @buf: The destination buffer or NULL to just obtain the length
9055  * @offset: The buffer offset to copy the data to
9056  * @head: The head offset of context memory to copy from
9057  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9058  *
9059  * This function is called for debugging purposes to dump the host context
9060  * used by the chip.
9061  *
9062  * Return: Length of memory copied
9063  */
9064 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9065 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9066 				  size_t offset, size_t head, size_t tail)
9067 {
9068 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9069 	size_t len = 0, total_len = 0;
9070 	int i, n = 1;
9071 
9072 	if (!ctx_pg)
9073 		return 0;
9074 
9075 	if (ctxm->instance_bmap)
9076 		n = hweight32(ctxm->instance_bmap);
9077 	for (i = 0; i < n; i++) {
9078 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9079 					    tail);
9080 		offset += len;
9081 		total_len += len;
9082 	}
9083 	return total_len;
9084 }
9085 
9086 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9087 			 void *buf, size_t offset)
9088 {
9089 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9090 
9091 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9092 }
9093 
9094 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9095 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9096 {
9097 	struct bnxt_ctx_pg_info *ctx_pg;
9098 	int i, n = 1;
9099 
9100 	ctxm->last = 0;
9101 
9102 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9103 		return;
9104 
9105 	ctx_pg = ctxm->pg_info;
9106 	if (ctx_pg) {
9107 		if (ctxm->instance_bmap)
9108 			n = hweight32(ctxm->instance_bmap);
9109 		for (i = 0; i < n; i++)
9110 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9111 
9112 		kfree(ctx_pg);
9113 		ctxm->pg_info = NULL;
9114 		ctxm->mem_valid = 0;
9115 	}
9116 	memset(ctxm, 0, sizeof(*ctxm));
9117 }
9118 
9119 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9120 {
9121 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9122 	u16 type;
9123 
9124 	if (!ctx)
9125 		return;
9126 
9127 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9128 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9129 
9130 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9131 	if (force) {
9132 		kfree(ctx);
9133 		bp->ctx = NULL;
9134 	}
9135 }
9136 
9137 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9138 {
9139 	struct bnxt_ctx_mem_type *ctxm;
9140 	struct bnxt_ctx_mem_info *ctx;
9141 	u32 l2_qps, qp1_qps, max_qps;
9142 	u32 ena, entries_sp, entries;
9143 	u32 srqs, max_srqs, min;
9144 	u32 num_mr, num_ah;
9145 	u32 extra_srqs = 0;
9146 	u32 extra_qps = 0;
9147 	u32 fast_qpmd_qps;
9148 	u8 pg_lvl = 1;
9149 	int i, rc;
9150 
9151 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9152 	if (rc) {
9153 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9154 			   rc);
9155 		return rc;
9156 	}
9157 	ctx = bp->ctx;
9158 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9159 		return 0;
9160 
9161 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9162 	l2_qps = ctxm->qp_l2_entries;
9163 	qp1_qps = ctxm->qp_qp1_entries;
9164 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9165 	max_qps = ctxm->max_entries;
9166 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9167 	srqs = ctxm->srq_l2_entries;
9168 	max_srqs = ctxm->max_entries;
9169 	ena = 0;
9170 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9171 		pg_lvl = 2;
9172 		if (BNXT_SW_RES_LMT(bp)) {
9173 			extra_qps = max_qps - l2_qps - qp1_qps;
9174 			extra_srqs = max_srqs - srqs;
9175 		} else {
9176 			extra_qps = min_t(u32, 65536,
9177 					  max_qps - l2_qps - qp1_qps);
9178 			/* allocate extra qps if fw supports RoCE fast qp
9179 			 * destroy feature
9180 			 */
9181 			extra_qps += fast_qpmd_qps;
9182 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9183 		}
9184 		if (fast_qpmd_qps)
9185 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9186 	}
9187 
9188 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9189 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9190 				     pg_lvl);
9191 	if (rc)
9192 		return rc;
9193 
9194 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9195 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9196 	if (rc)
9197 		return rc;
9198 
9199 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9200 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9201 				     extra_qps * 2, pg_lvl);
9202 	if (rc)
9203 		return rc;
9204 
9205 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9206 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9207 	if (rc)
9208 		return rc;
9209 
9210 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9211 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9212 	if (rc)
9213 		return rc;
9214 
9215 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9216 		goto skip_rdma;
9217 
9218 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9219 	if (BNXT_SW_RES_LMT(bp) &&
9220 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9221 		num_ah = ctxm->mrav_av_entries;
9222 		num_mr = ctxm->max_entries - num_ah;
9223 	} else {
9224 		/* 128K extra is needed to accommodate static AH context
9225 		 * allocation by f/w.
9226 		 */
9227 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9228 		num_ah = min_t(u32, num_mr, 1024 * 128);
9229 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9230 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9231 			ctxm->mrav_av_entries = num_ah;
9232 	}
9233 
9234 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9235 	if (rc)
9236 		return rc;
9237 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9238 
9239 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9240 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9241 	if (rc)
9242 		return rc;
9243 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9244 
9245 skip_rdma:
9246 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9247 	min = ctxm->min_entries;
9248 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9249 		     2 * (extra_qps + qp1_qps) + min;
9250 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9251 	if (rc)
9252 		return rc;
9253 
9254 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9255 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9256 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9257 	if (rc)
9258 		return rc;
9259 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9260 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9261 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9262 
9263 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9264 		rc = bnxt_backing_store_cfg_v2(bp, ena);
9265 	else
9266 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9267 	if (rc) {
9268 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9269 			   rc);
9270 		return rc;
9271 	}
9272 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9273 	return 0;
9274 }
9275 
9276 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9277 {
9278 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9279 	u16 page_attr;
9280 	int rc;
9281 
9282 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9283 		return 0;
9284 
9285 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9286 	if (rc)
9287 		return rc;
9288 
9289 	if (BNXT_PAGE_SIZE == 0x2000)
9290 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9291 	else if (BNXT_PAGE_SIZE == 0x10000)
9292 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9293 	else
9294 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9295 	req->pg_size_lvl = cpu_to_le16(page_attr |
9296 				       bp->fw_crash_mem->ring_mem.depth);
9297 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9298 	req->size = cpu_to_le32(bp->fw_crash_len);
9299 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9300 	return hwrm_req_send(bp, req);
9301 }
9302 
9303 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9304 {
9305 	if (bp->fw_crash_mem) {
9306 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9307 		kfree(bp->fw_crash_mem);
9308 		bp->fw_crash_mem = NULL;
9309 	}
9310 }
9311 
9312 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9313 {
9314 	u32 mem_size = 0;
9315 	int rc;
9316 
9317 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9318 		return 0;
9319 
9320 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9321 	if (rc)
9322 		return rc;
9323 
9324 	mem_size = round_up(mem_size, 4);
9325 
9326 	/* keep and use the existing pages */
9327 	if (bp->fw_crash_mem &&
9328 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9329 		goto alloc_done;
9330 
9331 	if (bp->fw_crash_mem)
9332 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9333 	else
9334 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
9335 					   GFP_KERNEL);
9336 	if (!bp->fw_crash_mem)
9337 		return -ENOMEM;
9338 
9339 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9340 	if (rc) {
9341 		bnxt_free_crash_dump_mem(bp);
9342 		return rc;
9343 	}
9344 
9345 alloc_done:
9346 	bp->fw_crash_len = mem_size;
9347 	return 0;
9348 }
9349 
9350 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9351 {
9352 	struct hwrm_func_resource_qcaps_output *resp;
9353 	struct hwrm_func_resource_qcaps_input *req;
9354 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9355 	int rc;
9356 
9357 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9358 	if (rc)
9359 		return rc;
9360 
9361 	req->fid = cpu_to_le16(0xffff);
9362 	resp = hwrm_req_hold(bp, req);
9363 	rc = hwrm_req_send_silent(bp, req);
9364 	if (rc)
9365 		goto hwrm_func_resc_qcaps_exit;
9366 
9367 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9368 	if (!all)
9369 		goto hwrm_func_resc_qcaps_exit;
9370 
9371 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9372 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9373 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9374 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9375 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9376 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9377 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9378 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9379 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9380 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9381 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9382 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9383 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9384 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9385 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9386 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9387 
9388 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9389 		u16 max_msix = le16_to_cpu(resp->max_msix);
9390 
9391 		hw_resc->max_nqs = max_msix;
9392 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9393 	}
9394 
9395 	if (BNXT_PF(bp)) {
9396 		struct bnxt_pf_info *pf = &bp->pf;
9397 
9398 		pf->vf_resv_strategy =
9399 			le16_to_cpu(resp->vf_reservation_strategy);
9400 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9401 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9402 	}
9403 hwrm_func_resc_qcaps_exit:
9404 	hwrm_req_drop(bp, req);
9405 	return rc;
9406 }
9407 
9408 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9409 {
9410 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9411 	struct hwrm_port_mac_ptp_qcfg_input *req;
9412 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9413 	u8 flags;
9414 	int rc;
9415 
9416 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9417 		rc = -ENODEV;
9418 		goto no_ptp;
9419 	}
9420 
9421 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9422 	if (rc)
9423 		goto no_ptp;
9424 
9425 	req->port_id = cpu_to_le16(bp->pf.port_id);
9426 	resp = hwrm_req_hold(bp, req);
9427 	rc = hwrm_req_send(bp, req);
9428 	if (rc)
9429 		goto exit;
9430 
9431 	flags = resp->flags;
9432 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9433 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9434 		rc = -ENODEV;
9435 		goto exit;
9436 	}
9437 	if (!ptp) {
9438 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9439 		if (!ptp) {
9440 			rc = -ENOMEM;
9441 			goto exit;
9442 		}
9443 		ptp->bp = bp;
9444 		bp->ptp_cfg = ptp;
9445 	}
9446 
9447 	if (flags &
9448 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9449 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9450 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9451 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9452 	} else if (BNXT_CHIP_P5(bp)) {
9453 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9454 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9455 	} else {
9456 		rc = -ENODEV;
9457 		goto exit;
9458 	}
9459 	ptp->rtc_configured =
9460 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9461 	rc = bnxt_ptp_init(bp);
9462 	if (rc)
9463 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9464 exit:
9465 	hwrm_req_drop(bp, req);
9466 	if (!rc)
9467 		return 0;
9468 
9469 no_ptp:
9470 	bnxt_ptp_clear(bp);
9471 	kfree(ptp);
9472 	bp->ptp_cfg = NULL;
9473 	return rc;
9474 }
9475 
9476 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9477 {
9478 	struct hwrm_func_qcaps_output *resp;
9479 	struct hwrm_func_qcaps_input *req;
9480 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9481 	u32 flags, flags_ext, flags_ext2;
9482 	int rc;
9483 
9484 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9485 	if (rc)
9486 		return rc;
9487 
9488 	req->fid = cpu_to_le16(0xffff);
9489 	resp = hwrm_req_hold(bp, req);
9490 	rc = hwrm_req_send(bp, req);
9491 	if (rc)
9492 		goto hwrm_func_qcaps_exit;
9493 
9494 	flags = le32_to_cpu(resp->flags);
9495 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9496 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9497 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9498 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9499 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9500 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9501 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9502 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9503 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9504 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9505 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9506 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9507 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9508 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9509 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9510 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9511 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9512 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9513 
9514 	flags_ext = le32_to_cpu(resp->flags_ext);
9515 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9516 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9517 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9518 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9519 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9520 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9521 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9522 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9523 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9524 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9525 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9526 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9527 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9528 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9529 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9530 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9531 
9532 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9533 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9534 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9535 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9536 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9537 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9538 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9539 	if (flags_ext2 &
9540 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9541 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9542 	if (BNXT_PF(bp) &&
9543 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9544 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9545 
9546 	bp->tx_push_thresh = 0;
9547 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9548 	    BNXT_FW_MAJ(bp) > 217)
9549 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9550 
9551 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9552 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9553 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9554 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9555 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9556 	if (!hw_resc->max_hw_ring_grps)
9557 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9558 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9559 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9560 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9561 
9562 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9563 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9564 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9565 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9566 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9567 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9568 
9569 	if (BNXT_PF(bp)) {
9570 		struct bnxt_pf_info *pf = &bp->pf;
9571 
9572 		pf->fw_fid = le16_to_cpu(resp->fid);
9573 		pf->port_id = le16_to_cpu(resp->port_id);
9574 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9575 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9576 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9577 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9578 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9579 			bp->flags |= BNXT_FLAG_WOL_CAP;
9580 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9581 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9582 		} else {
9583 			bnxt_ptp_clear(bp);
9584 			kfree(bp->ptp_cfg);
9585 			bp->ptp_cfg = NULL;
9586 		}
9587 	} else {
9588 #ifdef CONFIG_BNXT_SRIOV
9589 		struct bnxt_vf_info *vf = &bp->vf;
9590 
9591 		vf->fw_fid = le16_to_cpu(resp->fid);
9592 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9593 #endif
9594 	}
9595 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9596 
9597 hwrm_func_qcaps_exit:
9598 	hwrm_req_drop(bp, req);
9599 	return rc;
9600 }
9601 
9602 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9603 {
9604 	struct hwrm_dbg_qcaps_output *resp;
9605 	struct hwrm_dbg_qcaps_input *req;
9606 	int rc;
9607 
9608 	bp->fw_dbg_cap = 0;
9609 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9610 		return;
9611 
9612 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9613 	if (rc)
9614 		return;
9615 
9616 	req->fid = cpu_to_le16(0xffff);
9617 	resp = hwrm_req_hold(bp, req);
9618 	rc = hwrm_req_send(bp, req);
9619 	if (rc)
9620 		goto hwrm_dbg_qcaps_exit;
9621 
9622 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9623 
9624 hwrm_dbg_qcaps_exit:
9625 	hwrm_req_drop(bp, req);
9626 }
9627 
9628 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9629 
9630 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9631 {
9632 	int rc;
9633 
9634 	rc = __bnxt_hwrm_func_qcaps(bp);
9635 	if (rc)
9636 		return rc;
9637 
9638 	bnxt_hwrm_dbg_qcaps(bp);
9639 
9640 	rc = bnxt_hwrm_queue_qportcfg(bp);
9641 	if (rc) {
9642 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9643 		return rc;
9644 	}
9645 	if (bp->hwrm_spec_code >= 0x10803) {
9646 		rc = bnxt_alloc_ctx_mem(bp);
9647 		if (rc)
9648 			return rc;
9649 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9650 		if (!rc)
9651 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9652 	}
9653 	return 0;
9654 }
9655 
9656 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9657 {
9658 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9659 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9660 	u32 flags;
9661 	int rc;
9662 
9663 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9664 		return 0;
9665 
9666 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9667 	if (rc)
9668 		return rc;
9669 
9670 	resp = hwrm_req_hold(bp, req);
9671 	rc = hwrm_req_send(bp, req);
9672 	if (rc)
9673 		goto hwrm_cfa_adv_qcaps_exit;
9674 
9675 	flags = le32_to_cpu(resp->flags);
9676 	if (flags &
9677 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9678 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9679 
9680 	if (flags &
9681 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9682 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9683 
9684 	if (flags &
9685 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9686 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9687 
9688 hwrm_cfa_adv_qcaps_exit:
9689 	hwrm_req_drop(bp, req);
9690 	return rc;
9691 }
9692 
9693 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9694 {
9695 	if (bp->fw_health)
9696 		return 0;
9697 
9698 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9699 	if (!bp->fw_health)
9700 		return -ENOMEM;
9701 
9702 	mutex_init(&bp->fw_health->lock);
9703 	return 0;
9704 }
9705 
9706 static int bnxt_alloc_fw_health(struct bnxt *bp)
9707 {
9708 	int rc;
9709 
9710 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9711 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9712 		return 0;
9713 
9714 	rc = __bnxt_alloc_fw_health(bp);
9715 	if (rc) {
9716 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9717 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9718 		return rc;
9719 	}
9720 
9721 	return 0;
9722 }
9723 
9724 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9725 {
9726 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9727 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9728 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9729 }
9730 
9731 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9732 {
9733 	struct bnxt_fw_health *fw_health = bp->fw_health;
9734 	u32 reg_type;
9735 
9736 	if (!fw_health)
9737 		return;
9738 
9739 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9740 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9741 		fw_health->status_reliable = false;
9742 
9743 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9744 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9745 		fw_health->resets_reliable = false;
9746 }
9747 
9748 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9749 {
9750 	void __iomem *hs;
9751 	u32 status_loc;
9752 	u32 reg_type;
9753 	u32 sig;
9754 
9755 	if (bp->fw_health)
9756 		bp->fw_health->status_reliable = false;
9757 
9758 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9759 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9760 
9761 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9762 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9763 		if (!bp->chip_num) {
9764 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9765 			bp->chip_num = readl(bp->bar0 +
9766 					     BNXT_FW_HEALTH_WIN_BASE +
9767 					     BNXT_GRC_REG_CHIP_NUM);
9768 		}
9769 		if (!BNXT_CHIP_P5_PLUS(bp))
9770 			return;
9771 
9772 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9773 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9774 	} else {
9775 		status_loc = readl(hs + offsetof(struct hcomm_status,
9776 						 fw_status_loc));
9777 	}
9778 
9779 	if (__bnxt_alloc_fw_health(bp)) {
9780 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9781 		return;
9782 	}
9783 
9784 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9785 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9786 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9787 		__bnxt_map_fw_health_reg(bp, status_loc);
9788 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9789 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9790 	}
9791 
9792 	bp->fw_health->status_reliable = true;
9793 }
9794 
9795 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9796 {
9797 	struct bnxt_fw_health *fw_health = bp->fw_health;
9798 	u32 reg_base = 0xffffffff;
9799 	int i;
9800 
9801 	bp->fw_health->status_reliable = false;
9802 	bp->fw_health->resets_reliable = false;
9803 	/* Only pre-map the monitoring GRC registers using window 3 */
9804 	for (i = 0; i < 4; i++) {
9805 		u32 reg = fw_health->regs[i];
9806 
9807 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9808 			continue;
9809 		if (reg_base == 0xffffffff)
9810 			reg_base = reg & BNXT_GRC_BASE_MASK;
9811 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9812 			return -ERANGE;
9813 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9814 	}
9815 	bp->fw_health->status_reliable = true;
9816 	bp->fw_health->resets_reliable = true;
9817 	if (reg_base == 0xffffffff)
9818 		return 0;
9819 
9820 	__bnxt_map_fw_health_reg(bp, reg_base);
9821 	return 0;
9822 }
9823 
9824 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9825 {
9826 	if (!bp->fw_health)
9827 		return;
9828 
9829 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9830 		bp->fw_health->status_reliable = true;
9831 		bp->fw_health->resets_reliable = true;
9832 	} else {
9833 		bnxt_try_map_fw_health_reg(bp);
9834 	}
9835 }
9836 
9837 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9838 {
9839 	struct bnxt_fw_health *fw_health = bp->fw_health;
9840 	struct hwrm_error_recovery_qcfg_output *resp;
9841 	struct hwrm_error_recovery_qcfg_input *req;
9842 	int rc, i;
9843 
9844 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9845 		return 0;
9846 
9847 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9848 	if (rc)
9849 		return rc;
9850 
9851 	resp = hwrm_req_hold(bp, req);
9852 	rc = hwrm_req_send(bp, req);
9853 	if (rc)
9854 		goto err_recovery_out;
9855 	fw_health->flags = le32_to_cpu(resp->flags);
9856 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9857 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9858 		rc = -EINVAL;
9859 		goto err_recovery_out;
9860 	}
9861 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9862 	fw_health->master_func_wait_dsecs =
9863 		le32_to_cpu(resp->master_func_wait_period);
9864 	fw_health->normal_func_wait_dsecs =
9865 		le32_to_cpu(resp->normal_func_wait_period);
9866 	fw_health->post_reset_wait_dsecs =
9867 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9868 	fw_health->post_reset_max_wait_dsecs =
9869 		le32_to_cpu(resp->max_bailout_time_after_reset);
9870 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9871 		le32_to_cpu(resp->fw_health_status_reg);
9872 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9873 		le32_to_cpu(resp->fw_heartbeat_reg);
9874 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9875 		le32_to_cpu(resp->fw_reset_cnt_reg);
9876 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9877 		le32_to_cpu(resp->reset_inprogress_reg);
9878 	fw_health->fw_reset_inprog_reg_mask =
9879 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9880 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9881 	if (fw_health->fw_reset_seq_cnt >= 16) {
9882 		rc = -EINVAL;
9883 		goto err_recovery_out;
9884 	}
9885 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9886 		fw_health->fw_reset_seq_regs[i] =
9887 			le32_to_cpu(resp->reset_reg[i]);
9888 		fw_health->fw_reset_seq_vals[i] =
9889 			le32_to_cpu(resp->reset_reg_val[i]);
9890 		fw_health->fw_reset_seq_delay_msec[i] =
9891 			resp->delay_after_reset[i];
9892 	}
9893 err_recovery_out:
9894 	hwrm_req_drop(bp, req);
9895 	if (!rc)
9896 		rc = bnxt_map_fw_health_regs(bp);
9897 	if (rc)
9898 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9899 	return rc;
9900 }
9901 
9902 static int bnxt_hwrm_func_reset(struct bnxt *bp)
9903 {
9904 	struct hwrm_func_reset_input *req;
9905 	int rc;
9906 
9907 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
9908 	if (rc)
9909 		return rc;
9910 
9911 	req->enables = 0;
9912 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
9913 	return hwrm_req_send(bp, req);
9914 }
9915 
9916 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
9917 {
9918 	struct hwrm_nvm_get_dev_info_output nvm_info;
9919 
9920 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
9921 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
9922 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
9923 			 nvm_info.nvm_cfg_ver_upd);
9924 }
9925 
9926 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
9927 {
9928 	struct hwrm_queue_qportcfg_output *resp;
9929 	struct hwrm_queue_qportcfg_input *req;
9930 	u8 i, j, *qptr;
9931 	bool no_rdma;
9932 	int rc = 0;
9933 
9934 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
9935 	if (rc)
9936 		return rc;
9937 
9938 	resp = hwrm_req_hold(bp, req);
9939 	rc = hwrm_req_send(bp, req);
9940 	if (rc)
9941 		goto qportcfg_exit;
9942 
9943 	if (!resp->max_configurable_queues) {
9944 		rc = -EINVAL;
9945 		goto qportcfg_exit;
9946 	}
9947 	bp->max_tc = resp->max_configurable_queues;
9948 	bp->max_lltc = resp->max_configurable_lossless_queues;
9949 	if (bp->max_tc > BNXT_MAX_QUEUE)
9950 		bp->max_tc = BNXT_MAX_QUEUE;
9951 
9952 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
9953 	qptr = &resp->queue_id0;
9954 	for (i = 0, j = 0; i < bp->max_tc; i++) {
9955 		bp->q_info[j].queue_id = *qptr;
9956 		bp->q_ids[i] = *qptr++;
9957 		bp->q_info[j].queue_profile = *qptr++;
9958 		bp->tc_to_qidx[j] = j;
9959 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
9960 		    (no_rdma && BNXT_PF(bp)))
9961 			j++;
9962 	}
9963 	bp->max_q = bp->max_tc;
9964 	bp->max_tc = max_t(u8, j, 1);
9965 
9966 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
9967 		bp->max_tc = 1;
9968 
9969 	if (bp->max_lltc > bp->max_tc)
9970 		bp->max_lltc = bp->max_tc;
9971 
9972 qportcfg_exit:
9973 	hwrm_req_drop(bp, req);
9974 	return rc;
9975 }
9976 
9977 static int bnxt_hwrm_poll(struct bnxt *bp)
9978 {
9979 	struct hwrm_ver_get_input *req;
9980 	int rc;
9981 
9982 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9983 	if (rc)
9984 		return rc;
9985 
9986 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9987 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9988 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9989 
9990 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
9991 	rc = hwrm_req_send(bp, req);
9992 	return rc;
9993 }
9994 
9995 static int bnxt_hwrm_ver_get(struct bnxt *bp)
9996 {
9997 	struct hwrm_ver_get_output *resp;
9998 	struct hwrm_ver_get_input *req;
9999 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10000 	u32 dev_caps_cfg, hwrm_ver;
10001 	int rc, len;
10002 
10003 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10004 	if (rc)
10005 		return rc;
10006 
10007 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10008 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10009 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10010 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10011 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10012 
10013 	resp = hwrm_req_hold(bp, req);
10014 	rc = hwrm_req_send(bp, req);
10015 	if (rc)
10016 		goto hwrm_ver_get_exit;
10017 
10018 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10019 
10020 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10021 			     resp->hwrm_intf_min_8b << 8 |
10022 			     resp->hwrm_intf_upd_8b;
10023 	if (resp->hwrm_intf_maj_8b < 1) {
10024 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10025 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10026 			    resp->hwrm_intf_upd_8b);
10027 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10028 	}
10029 
10030 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10031 			HWRM_VERSION_UPDATE;
10032 
10033 	if (bp->hwrm_spec_code > hwrm_ver)
10034 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10035 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10036 			 HWRM_VERSION_UPDATE);
10037 	else
10038 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10039 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10040 			 resp->hwrm_intf_upd_8b);
10041 
10042 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10043 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10044 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10045 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10046 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10047 		len = FW_VER_STR_LEN;
10048 	} else {
10049 		fw_maj = resp->hwrm_fw_maj_8b;
10050 		fw_min = resp->hwrm_fw_min_8b;
10051 		fw_bld = resp->hwrm_fw_bld_8b;
10052 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10053 		len = BC_HWRM_STR_LEN;
10054 	}
10055 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10056 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10057 		 fw_rsv);
10058 
10059 	if (strlen(resp->active_pkg_name)) {
10060 		int fw_ver_len = strlen(bp->fw_ver_str);
10061 
10062 		snprintf(bp->fw_ver_str + fw_ver_len,
10063 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10064 			 resp->active_pkg_name);
10065 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10066 	}
10067 
10068 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10069 	if (!bp->hwrm_cmd_timeout)
10070 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10071 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10072 	if (!bp->hwrm_cmd_max_timeout)
10073 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10074 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
10075 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
10076 			    bp->hwrm_cmd_max_timeout / 1000);
10077 
10078 	if (resp->hwrm_intf_maj_8b >= 1) {
10079 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10080 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10081 	}
10082 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10083 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10084 
10085 	bp->chip_num = le16_to_cpu(resp->chip_num);
10086 	bp->chip_rev = resp->chip_rev;
10087 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10088 	    !resp->chip_metal)
10089 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10090 
10091 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10092 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10093 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10094 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10095 
10096 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10097 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10098 
10099 	if (dev_caps_cfg &
10100 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10101 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10102 
10103 	if (dev_caps_cfg &
10104 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10105 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10106 
10107 	if (dev_caps_cfg &
10108 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10109 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10110 
10111 hwrm_ver_get_exit:
10112 	hwrm_req_drop(bp, req);
10113 	return rc;
10114 }
10115 
10116 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10117 {
10118 	struct hwrm_fw_set_time_input *req;
10119 	struct tm tm;
10120 	time64_t now = ktime_get_real_seconds();
10121 	int rc;
10122 
10123 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10124 	    bp->hwrm_spec_code < 0x10400)
10125 		return -EOPNOTSUPP;
10126 
10127 	time64_to_tm(now, 0, &tm);
10128 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10129 	if (rc)
10130 		return rc;
10131 
10132 	req->year = cpu_to_le16(1900 + tm.tm_year);
10133 	req->month = 1 + tm.tm_mon;
10134 	req->day = tm.tm_mday;
10135 	req->hour = tm.tm_hour;
10136 	req->minute = tm.tm_min;
10137 	req->second = tm.tm_sec;
10138 	return hwrm_req_send(bp, req);
10139 }
10140 
10141 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10142 {
10143 	u64 sw_tmp;
10144 
10145 	hw &= mask;
10146 	sw_tmp = (*sw & ~mask) | hw;
10147 	if (hw < (*sw & mask))
10148 		sw_tmp += mask + 1;
10149 	WRITE_ONCE(*sw, sw_tmp);
10150 }
10151 
10152 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10153 				    int count, bool ignore_zero)
10154 {
10155 	int i;
10156 
10157 	for (i = 0; i < count; i++) {
10158 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10159 
10160 		if (ignore_zero && !hw)
10161 			continue;
10162 
10163 		if (masks[i] == -1ULL)
10164 			sw_stats[i] = hw;
10165 		else
10166 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10167 	}
10168 }
10169 
10170 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10171 {
10172 	if (!stats->hw_stats)
10173 		return;
10174 
10175 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10176 				stats->hw_masks, stats->len / 8, false);
10177 }
10178 
10179 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10180 {
10181 	struct bnxt_stats_mem *ring0_stats;
10182 	bool ignore_zero = false;
10183 	int i;
10184 
10185 	/* Chip bug.  Counter intermittently becomes 0. */
10186 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10187 		ignore_zero = true;
10188 
10189 	for (i = 0; i < bp->cp_nr_rings; i++) {
10190 		struct bnxt_napi *bnapi = bp->bnapi[i];
10191 		struct bnxt_cp_ring_info *cpr;
10192 		struct bnxt_stats_mem *stats;
10193 
10194 		cpr = &bnapi->cp_ring;
10195 		stats = &cpr->stats;
10196 		if (!i)
10197 			ring0_stats = stats;
10198 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10199 					ring0_stats->hw_masks,
10200 					ring0_stats->len / 8, ignore_zero);
10201 	}
10202 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10203 		struct bnxt_stats_mem *stats = &bp->port_stats;
10204 		__le64 *hw_stats = stats->hw_stats;
10205 		u64 *sw_stats = stats->sw_stats;
10206 		u64 *masks = stats->hw_masks;
10207 		int cnt;
10208 
10209 		cnt = sizeof(struct rx_port_stats) / 8;
10210 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10211 
10212 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10213 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10214 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10215 		cnt = sizeof(struct tx_port_stats) / 8;
10216 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10217 	}
10218 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10219 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10220 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10221 	}
10222 }
10223 
10224 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10225 {
10226 	struct hwrm_port_qstats_input *req;
10227 	struct bnxt_pf_info *pf = &bp->pf;
10228 	int rc;
10229 
10230 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10231 		return 0;
10232 
10233 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10234 		return -EOPNOTSUPP;
10235 
10236 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10237 	if (rc)
10238 		return rc;
10239 
10240 	req->flags = flags;
10241 	req->port_id = cpu_to_le16(pf->port_id);
10242 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10243 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10244 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10245 	return hwrm_req_send(bp, req);
10246 }
10247 
10248 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10249 {
10250 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10251 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10252 	struct hwrm_port_qstats_ext_output *resp_qs;
10253 	struct hwrm_port_qstats_ext_input *req_qs;
10254 	struct bnxt_pf_info *pf = &bp->pf;
10255 	u32 tx_stat_size;
10256 	int rc;
10257 
10258 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10259 		return 0;
10260 
10261 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10262 		return -EOPNOTSUPP;
10263 
10264 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10265 	if (rc)
10266 		return rc;
10267 
10268 	req_qs->flags = flags;
10269 	req_qs->port_id = cpu_to_le16(pf->port_id);
10270 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10271 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10272 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10273 		       sizeof(struct tx_port_stats_ext) : 0;
10274 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10275 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10276 	resp_qs = hwrm_req_hold(bp, req_qs);
10277 	rc = hwrm_req_send(bp, req_qs);
10278 	if (!rc) {
10279 		bp->fw_rx_stats_ext_size =
10280 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10281 		if (BNXT_FW_MAJ(bp) < 220 &&
10282 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10283 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10284 
10285 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10286 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10287 	} else {
10288 		bp->fw_rx_stats_ext_size = 0;
10289 		bp->fw_tx_stats_ext_size = 0;
10290 	}
10291 	hwrm_req_drop(bp, req_qs);
10292 
10293 	if (flags)
10294 		return rc;
10295 
10296 	if (bp->fw_tx_stats_ext_size <=
10297 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10298 		bp->pri2cos_valid = 0;
10299 		return rc;
10300 	}
10301 
10302 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10303 	if (rc)
10304 		return rc;
10305 
10306 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10307 
10308 	resp_qc = hwrm_req_hold(bp, req_qc);
10309 	rc = hwrm_req_send(bp, req_qc);
10310 	if (!rc) {
10311 		u8 *pri2cos;
10312 		int i, j;
10313 
10314 		pri2cos = &resp_qc->pri0_cos_queue_id;
10315 		for (i = 0; i < 8; i++) {
10316 			u8 queue_id = pri2cos[i];
10317 			u8 queue_idx;
10318 
10319 			/* Per port queue IDs start from 0, 10, 20, etc */
10320 			queue_idx = queue_id % 10;
10321 			if (queue_idx > BNXT_MAX_QUEUE) {
10322 				bp->pri2cos_valid = false;
10323 				hwrm_req_drop(bp, req_qc);
10324 				return rc;
10325 			}
10326 			for (j = 0; j < bp->max_q; j++) {
10327 				if (bp->q_ids[j] == queue_id)
10328 					bp->pri2cos_idx[i] = queue_idx;
10329 			}
10330 		}
10331 		bp->pri2cos_valid = true;
10332 	}
10333 	hwrm_req_drop(bp, req_qc);
10334 
10335 	return rc;
10336 }
10337 
10338 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10339 {
10340 	bnxt_hwrm_tunnel_dst_port_free(bp,
10341 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10342 	bnxt_hwrm_tunnel_dst_port_free(bp,
10343 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10344 }
10345 
10346 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10347 {
10348 	int rc, i;
10349 	u32 tpa_flags = 0;
10350 
10351 	if (set_tpa)
10352 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10353 	else if (BNXT_NO_FW_ACCESS(bp))
10354 		return 0;
10355 	for (i = 0; i < bp->nr_vnics; i++) {
10356 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10357 		if (rc) {
10358 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10359 				   i, rc);
10360 			return rc;
10361 		}
10362 	}
10363 	return 0;
10364 }
10365 
10366 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10367 {
10368 	int i;
10369 
10370 	for (i = 0; i < bp->nr_vnics; i++)
10371 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10372 }
10373 
10374 static void bnxt_clear_vnic(struct bnxt *bp)
10375 {
10376 	if (!bp->vnic_info)
10377 		return;
10378 
10379 	bnxt_hwrm_clear_vnic_filter(bp);
10380 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10381 		/* clear all RSS setting before free vnic ctx */
10382 		bnxt_hwrm_clear_vnic_rss(bp);
10383 		bnxt_hwrm_vnic_ctx_free(bp);
10384 	}
10385 	/* before free the vnic, undo the vnic tpa settings */
10386 	if (bp->flags & BNXT_FLAG_TPA)
10387 		bnxt_set_tpa(bp, false);
10388 	bnxt_hwrm_vnic_free(bp);
10389 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10390 		bnxt_hwrm_vnic_ctx_free(bp);
10391 }
10392 
10393 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10394 				    bool irq_re_init)
10395 {
10396 	bnxt_clear_vnic(bp);
10397 	bnxt_hwrm_ring_free(bp, close_path);
10398 	bnxt_hwrm_ring_grp_free(bp);
10399 	if (irq_re_init) {
10400 		bnxt_hwrm_stat_ctx_free(bp);
10401 		bnxt_hwrm_free_tunnel_ports(bp);
10402 	}
10403 }
10404 
10405 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10406 {
10407 	struct hwrm_func_cfg_input *req;
10408 	u8 evb_mode;
10409 	int rc;
10410 
10411 	if (br_mode == BRIDGE_MODE_VEB)
10412 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10413 	else if (br_mode == BRIDGE_MODE_VEPA)
10414 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10415 	else
10416 		return -EINVAL;
10417 
10418 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10419 	if (rc)
10420 		return rc;
10421 
10422 	req->fid = cpu_to_le16(0xffff);
10423 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10424 	req->evb_mode = evb_mode;
10425 	return hwrm_req_send(bp, req);
10426 }
10427 
10428 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10429 {
10430 	struct hwrm_func_cfg_input *req;
10431 	int rc;
10432 
10433 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10434 		return 0;
10435 
10436 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10437 	if (rc)
10438 		return rc;
10439 
10440 	req->fid = cpu_to_le16(0xffff);
10441 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10442 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10443 	if (size == 128)
10444 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10445 
10446 	return hwrm_req_send(bp, req);
10447 }
10448 
10449 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10450 {
10451 	int rc;
10452 
10453 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10454 		goto skip_rss_ctx;
10455 
10456 	/* allocate context for vnic */
10457 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10458 	if (rc) {
10459 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10460 			   vnic->vnic_id, rc);
10461 		goto vnic_setup_err;
10462 	}
10463 	bp->rsscos_nr_ctxs++;
10464 
10465 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10466 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10467 		if (rc) {
10468 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10469 				   vnic->vnic_id, rc);
10470 			goto vnic_setup_err;
10471 		}
10472 		bp->rsscos_nr_ctxs++;
10473 	}
10474 
10475 skip_rss_ctx:
10476 	/* configure default vnic, ring grp */
10477 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10478 	if (rc) {
10479 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10480 			   vnic->vnic_id, rc);
10481 		goto vnic_setup_err;
10482 	}
10483 
10484 	/* Enable RSS hashing on vnic */
10485 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10486 	if (rc) {
10487 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10488 			   vnic->vnic_id, rc);
10489 		goto vnic_setup_err;
10490 	}
10491 
10492 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10493 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10494 		if (rc) {
10495 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10496 				   vnic->vnic_id, rc);
10497 		}
10498 	}
10499 
10500 vnic_setup_err:
10501 	return rc;
10502 }
10503 
10504 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10505 			  u8 valid)
10506 {
10507 	struct hwrm_vnic_update_input *req;
10508 	int rc;
10509 
10510 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10511 	if (rc)
10512 		return rc;
10513 
10514 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10515 
10516 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10517 		req->mru = cpu_to_le16(vnic->mru);
10518 
10519 	req->enables = cpu_to_le32(valid);
10520 
10521 	return hwrm_req_send(bp, req);
10522 }
10523 
10524 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10525 {
10526 	int rc;
10527 
10528 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10529 	if (rc) {
10530 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10531 			   vnic->vnic_id, rc);
10532 		return rc;
10533 	}
10534 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10535 	if (rc)
10536 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10537 			   vnic->vnic_id, rc);
10538 	return rc;
10539 }
10540 
10541 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10542 {
10543 	int rc, i, nr_ctxs;
10544 
10545 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10546 	for (i = 0; i < nr_ctxs; i++) {
10547 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10548 		if (rc) {
10549 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10550 				   vnic->vnic_id, i, rc);
10551 			break;
10552 		}
10553 		bp->rsscos_nr_ctxs++;
10554 	}
10555 	if (i < nr_ctxs)
10556 		return -ENOMEM;
10557 
10558 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10559 	if (rc)
10560 		return rc;
10561 
10562 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10563 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10564 		if (rc) {
10565 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10566 				   vnic->vnic_id, rc);
10567 		}
10568 	}
10569 	return rc;
10570 }
10571 
10572 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10573 {
10574 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10575 		return __bnxt_setup_vnic_p5(bp, vnic);
10576 	else
10577 		return __bnxt_setup_vnic(bp, vnic);
10578 }
10579 
10580 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10581 				     struct bnxt_vnic_info *vnic,
10582 				     u16 start_rx_ring_idx, int rx_rings)
10583 {
10584 	int rc;
10585 
10586 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10587 	if (rc) {
10588 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10589 			   vnic->vnic_id, rc);
10590 		return rc;
10591 	}
10592 	return bnxt_setup_vnic(bp, vnic);
10593 }
10594 
10595 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10596 {
10597 	struct bnxt_vnic_info *vnic;
10598 	int i, rc = 0;
10599 
10600 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10601 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10602 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10603 	}
10604 
10605 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10606 		return 0;
10607 
10608 	for (i = 0; i < bp->rx_nr_rings; i++) {
10609 		u16 vnic_id = i + 1;
10610 		u16 ring_id = i;
10611 
10612 		if (vnic_id >= bp->nr_vnics)
10613 			break;
10614 
10615 		vnic = &bp->vnic_info[vnic_id];
10616 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10617 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10618 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10619 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10620 			break;
10621 	}
10622 	return rc;
10623 }
10624 
10625 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10626 			  bool all)
10627 {
10628 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10629 	struct bnxt_filter_base *usr_fltr, *tmp;
10630 	struct bnxt_ntuple_filter *ntp_fltr;
10631 	int i;
10632 
10633 	if (netif_running(bp->dev)) {
10634 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10635 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10636 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10637 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10638 		}
10639 	}
10640 	if (!all)
10641 		return;
10642 
10643 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10644 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10645 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10646 			ntp_fltr = container_of(usr_fltr,
10647 						struct bnxt_ntuple_filter,
10648 						base);
10649 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10650 			bnxt_del_ntp_filter(bp, ntp_fltr);
10651 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10652 		}
10653 	}
10654 
10655 	if (vnic->rss_table)
10656 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10657 				  vnic->rss_table,
10658 				  vnic->rss_table_dma_addr);
10659 	bp->num_rss_ctx--;
10660 }
10661 
10662 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10663 {
10664 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10665 	struct ethtool_rxfh_context *ctx;
10666 	unsigned long context;
10667 
10668 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10669 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10670 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10671 
10672 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10673 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10674 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10675 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10676 				   rss_ctx->index);
10677 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10678 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10679 		}
10680 	}
10681 }
10682 
10683 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10684 {
10685 	struct ethtool_rxfh_context *ctx;
10686 	unsigned long context;
10687 
10688 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10689 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10690 
10691 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10692 	}
10693 }
10694 
10695 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10696 static bool bnxt_promisc_ok(struct bnxt *bp)
10697 {
10698 #ifdef CONFIG_BNXT_SRIOV
10699 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10700 		return false;
10701 #endif
10702 	return true;
10703 }
10704 
10705 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10706 {
10707 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10708 	unsigned int rc = 0;
10709 
10710 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10711 	if (rc) {
10712 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10713 			   rc);
10714 		return rc;
10715 	}
10716 
10717 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10718 	if (rc) {
10719 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10720 			   rc);
10721 		return rc;
10722 	}
10723 	return rc;
10724 }
10725 
10726 static int bnxt_cfg_rx_mode(struct bnxt *);
10727 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10728 
10729 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10730 {
10731 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10732 	int rc = 0;
10733 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10734 
10735 	if (irq_re_init) {
10736 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10737 		if (rc) {
10738 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10739 				   rc);
10740 			goto err_out;
10741 		}
10742 	}
10743 
10744 	rc = bnxt_hwrm_ring_alloc(bp);
10745 	if (rc) {
10746 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10747 		goto err_out;
10748 	}
10749 
10750 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10751 	if (rc) {
10752 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10753 		goto err_out;
10754 	}
10755 
10756 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10757 		rx_nr_rings--;
10758 
10759 	/* default vnic 0 */
10760 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10761 	if (rc) {
10762 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10763 		goto err_out;
10764 	}
10765 
10766 	if (BNXT_VF(bp))
10767 		bnxt_hwrm_func_qcfg(bp);
10768 
10769 	rc = bnxt_setup_vnic(bp, vnic);
10770 	if (rc)
10771 		goto err_out;
10772 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10773 		bnxt_hwrm_update_rss_hash_cfg(bp);
10774 
10775 	if (bp->flags & BNXT_FLAG_RFS) {
10776 		rc = bnxt_alloc_rfs_vnics(bp);
10777 		if (rc)
10778 			goto err_out;
10779 	}
10780 
10781 	if (bp->flags & BNXT_FLAG_TPA) {
10782 		rc = bnxt_set_tpa(bp, true);
10783 		if (rc)
10784 			goto err_out;
10785 	}
10786 
10787 	if (BNXT_VF(bp))
10788 		bnxt_update_vf_mac(bp);
10789 
10790 	/* Filter for default vnic 0 */
10791 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10792 	if (rc) {
10793 		if (BNXT_VF(bp) && rc == -ENODEV)
10794 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10795 		else
10796 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10797 		goto err_out;
10798 	}
10799 	vnic->uc_filter_count = 1;
10800 
10801 	vnic->rx_mask = 0;
10802 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10803 		goto skip_rx_mask;
10804 
10805 	if (bp->dev->flags & IFF_BROADCAST)
10806 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10807 
10808 	if (bp->dev->flags & IFF_PROMISC)
10809 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10810 
10811 	if (bp->dev->flags & IFF_ALLMULTI) {
10812 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10813 		vnic->mc_list_count = 0;
10814 	} else if (bp->dev->flags & IFF_MULTICAST) {
10815 		u32 mask = 0;
10816 
10817 		bnxt_mc_list_updated(bp, &mask);
10818 		vnic->rx_mask |= mask;
10819 	}
10820 
10821 	rc = bnxt_cfg_rx_mode(bp);
10822 	if (rc)
10823 		goto err_out;
10824 
10825 skip_rx_mask:
10826 	rc = bnxt_hwrm_set_coal(bp);
10827 	if (rc)
10828 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10829 				rc);
10830 
10831 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10832 		rc = bnxt_setup_nitroa0_vnic(bp);
10833 		if (rc)
10834 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10835 				   rc);
10836 	}
10837 
10838 	if (BNXT_VF(bp)) {
10839 		bnxt_hwrm_func_qcfg(bp);
10840 		netdev_update_features(bp->dev);
10841 	}
10842 
10843 	return 0;
10844 
10845 err_out:
10846 	bnxt_hwrm_resource_free(bp, 0, true);
10847 
10848 	return rc;
10849 }
10850 
10851 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10852 {
10853 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10854 	return 0;
10855 }
10856 
10857 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10858 {
10859 	bnxt_init_cp_rings(bp);
10860 	bnxt_init_rx_rings(bp);
10861 	bnxt_init_tx_rings(bp);
10862 	bnxt_init_ring_grps(bp, irq_re_init);
10863 	bnxt_init_vnics(bp);
10864 
10865 	return bnxt_init_chip(bp, irq_re_init);
10866 }
10867 
10868 static int bnxt_set_real_num_queues(struct bnxt *bp)
10869 {
10870 	int rc;
10871 	struct net_device *dev = bp->dev;
10872 
10873 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10874 					  bp->tx_nr_rings_xdp);
10875 	if (rc)
10876 		return rc;
10877 
10878 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10879 	if (rc)
10880 		return rc;
10881 
10882 #ifdef CONFIG_RFS_ACCEL
10883 	if (bp->flags & BNXT_FLAG_RFS)
10884 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
10885 #endif
10886 
10887 	return rc;
10888 }
10889 
10890 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10891 			     bool shared)
10892 {
10893 	int _rx = *rx, _tx = *tx;
10894 
10895 	if (shared) {
10896 		*rx = min_t(int, _rx, max);
10897 		*tx = min_t(int, _tx, max);
10898 	} else {
10899 		if (max < 2)
10900 			return -ENOMEM;
10901 
10902 		while (_rx + _tx > max) {
10903 			if (_rx > _tx && _rx > 1)
10904 				_rx--;
10905 			else if (_tx > 1)
10906 				_tx--;
10907 		}
10908 		*rx = _rx;
10909 		*tx = _tx;
10910 	}
10911 	return 0;
10912 }
10913 
10914 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
10915 {
10916 	return (tx - tx_xdp) / tx_sets + tx_xdp;
10917 }
10918 
10919 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
10920 {
10921 	int tcs = bp->num_tc;
10922 
10923 	if (!tcs)
10924 		tcs = 1;
10925 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
10926 }
10927 
10928 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
10929 {
10930 	int tcs = bp->num_tc;
10931 
10932 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
10933 	       bp->tx_nr_rings_xdp;
10934 }
10935 
10936 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10937 			   bool sh)
10938 {
10939 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
10940 
10941 	if (tx_cp != *tx) {
10942 		int tx_saved = tx_cp, rc;
10943 
10944 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
10945 		if (rc)
10946 			return rc;
10947 		if (tx_cp != tx_saved)
10948 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
10949 		return 0;
10950 	}
10951 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
10952 }
10953 
10954 static void bnxt_setup_msix(struct bnxt *bp)
10955 {
10956 	const int len = sizeof(bp->irq_tbl[0].name);
10957 	struct net_device *dev = bp->dev;
10958 	int tcs, i;
10959 
10960 	tcs = bp->num_tc;
10961 	if (tcs) {
10962 		int i, off, count;
10963 
10964 		for (i = 0; i < tcs; i++) {
10965 			count = bp->tx_nr_rings_per_tc;
10966 			off = BNXT_TC_TO_RING_BASE(bp, i);
10967 			netdev_set_tc_queue(dev, i, count, off);
10968 		}
10969 	}
10970 
10971 	for (i = 0; i < bp->cp_nr_rings; i++) {
10972 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10973 		char *attr;
10974 
10975 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
10976 			attr = "TxRx";
10977 		else if (i < bp->rx_nr_rings)
10978 			attr = "rx";
10979 		else
10980 			attr = "tx";
10981 
10982 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
10983 			 attr, i);
10984 		bp->irq_tbl[map_idx].handler = bnxt_msix;
10985 	}
10986 }
10987 
10988 static int bnxt_init_int_mode(struct bnxt *bp);
10989 
10990 static int bnxt_change_msix(struct bnxt *bp, int total)
10991 {
10992 	struct msi_map map;
10993 	int i;
10994 
10995 	/* add MSIX to the end if needed */
10996 	for (i = bp->total_irqs; i < total; i++) {
10997 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
10998 		if (map.index < 0)
10999 			return bp->total_irqs;
11000 		bp->irq_tbl[i].vector = map.virq;
11001 		bp->total_irqs++;
11002 	}
11003 
11004 	/* trim MSIX from the end if needed */
11005 	for (i = bp->total_irqs; i > total; i--) {
11006 		map.index = i - 1;
11007 		map.virq = bp->irq_tbl[i - 1].vector;
11008 		pci_msix_free_irq(bp->pdev, map);
11009 		bp->total_irqs--;
11010 	}
11011 	return bp->total_irqs;
11012 }
11013 
11014 static int bnxt_setup_int_mode(struct bnxt *bp)
11015 {
11016 	int rc;
11017 
11018 	if (!bp->irq_tbl) {
11019 		rc = bnxt_init_int_mode(bp);
11020 		if (rc || !bp->irq_tbl)
11021 			return rc ?: -ENODEV;
11022 	}
11023 
11024 	bnxt_setup_msix(bp);
11025 
11026 	rc = bnxt_set_real_num_queues(bp);
11027 	return rc;
11028 }
11029 
11030 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11031 {
11032 	return bp->hw_resc.max_rsscos_ctxs;
11033 }
11034 
11035 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11036 {
11037 	return bp->hw_resc.max_vnics;
11038 }
11039 
11040 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11041 {
11042 	return bp->hw_resc.max_stat_ctxs;
11043 }
11044 
11045 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11046 {
11047 	return bp->hw_resc.max_cp_rings;
11048 }
11049 
11050 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11051 {
11052 	unsigned int cp = bp->hw_resc.max_cp_rings;
11053 
11054 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11055 		cp -= bnxt_get_ulp_msix_num(bp);
11056 
11057 	return cp;
11058 }
11059 
11060 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11061 {
11062 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11063 
11064 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11065 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11066 
11067 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11068 }
11069 
11070 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11071 {
11072 	bp->hw_resc.max_irqs = max_irqs;
11073 }
11074 
11075 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11076 {
11077 	unsigned int cp;
11078 
11079 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11080 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11081 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11082 	else
11083 		return cp - bp->cp_nr_rings;
11084 }
11085 
11086 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11087 {
11088 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11089 }
11090 
11091 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11092 {
11093 	int max_irq = bnxt_get_max_func_irqs(bp);
11094 	int total_req = bp->cp_nr_rings + num;
11095 
11096 	if (max_irq < total_req) {
11097 		num = max_irq - bp->cp_nr_rings;
11098 		if (num <= 0)
11099 			return 0;
11100 	}
11101 	return num;
11102 }
11103 
11104 static int bnxt_get_num_msix(struct bnxt *bp)
11105 {
11106 	if (!BNXT_NEW_RM(bp))
11107 		return bnxt_get_max_func_irqs(bp);
11108 
11109 	return bnxt_nq_rings_in_use(bp);
11110 }
11111 
11112 static int bnxt_init_int_mode(struct bnxt *bp)
11113 {
11114 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11115 
11116 	total_vecs = bnxt_get_num_msix(bp);
11117 	max = bnxt_get_max_func_irqs(bp);
11118 	if (total_vecs > max)
11119 		total_vecs = max;
11120 
11121 	if (!total_vecs)
11122 		return 0;
11123 
11124 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11125 		min = 2;
11126 
11127 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11128 					   PCI_IRQ_MSIX);
11129 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11130 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11131 		rc = -ENODEV;
11132 		goto msix_setup_exit;
11133 	}
11134 
11135 	tbl_size = total_vecs;
11136 	if (pci_msix_can_alloc_dyn(bp->pdev))
11137 		tbl_size = max;
11138 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
11139 	if (bp->irq_tbl) {
11140 		for (i = 0; i < total_vecs; i++)
11141 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11142 
11143 		bp->total_irqs = total_vecs;
11144 		/* Trim rings based upon num of vectors allocated */
11145 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11146 				     total_vecs - ulp_msix, min == 1);
11147 		if (rc)
11148 			goto msix_setup_exit;
11149 
11150 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11151 		bp->cp_nr_rings = (min == 1) ?
11152 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11153 				  tx_cp + bp->rx_nr_rings;
11154 
11155 	} else {
11156 		rc = -ENOMEM;
11157 		goto msix_setup_exit;
11158 	}
11159 	return 0;
11160 
11161 msix_setup_exit:
11162 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11163 	kfree(bp->irq_tbl);
11164 	bp->irq_tbl = NULL;
11165 	pci_free_irq_vectors(bp->pdev);
11166 	return rc;
11167 }
11168 
11169 static void bnxt_clear_int_mode(struct bnxt *bp)
11170 {
11171 	pci_free_irq_vectors(bp->pdev);
11172 
11173 	kfree(bp->irq_tbl);
11174 	bp->irq_tbl = NULL;
11175 }
11176 
11177 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11178 {
11179 	bool irq_cleared = false;
11180 	bool irq_change = false;
11181 	int tcs = bp->num_tc;
11182 	int irqs_required;
11183 	int rc;
11184 
11185 	if (!bnxt_need_reserve_rings(bp))
11186 		return 0;
11187 
11188 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11189 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11190 
11191 		if (ulp_msix > bp->ulp_num_msix_want)
11192 			ulp_msix = bp->ulp_num_msix_want;
11193 		irqs_required = ulp_msix + bp->cp_nr_rings;
11194 	} else {
11195 		irqs_required = bnxt_get_num_msix(bp);
11196 	}
11197 
11198 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11199 		irq_change = true;
11200 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11201 			bnxt_ulp_irq_stop(bp);
11202 			bnxt_clear_int_mode(bp);
11203 			irq_cleared = true;
11204 		}
11205 	}
11206 	rc = __bnxt_reserve_rings(bp);
11207 	if (irq_cleared) {
11208 		if (!rc)
11209 			rc = bnxt_init_int_mode(bp);
11210 		bnxt_ulp_irq_restart(bp, rc);
11211 	} else if (irq_change && !rc) {
11212 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11213 			rc = -ENOSPC;
11214 	}
11215 	if (rc) {
11216 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11217 		return rc;
11218 	}
11219 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11220 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11221 		netdev_err(bp->dev, "tx ring reservation failure\n");
11222 		netdev_reset_tc(bp->dev);
11223 		bp->num_tc = 0;
11224 		if (bp->tx_nr_rings_xdp)
11225 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11226 		else
11227 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11228 		return -ENOMEM;
11229 	}
11230 	return 0;
11231 }
11232 
11233 static void bnxt_free_irq(struct bnxt *bp)
11234 {
11235 	struct bnxt_irq *irq;
11236 	int i;
11237 
11238 #ifdef CONFIG_RFS_ACCEL
11239 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11240 	bp->dev->rx_cpu_rmap = NULL;
11241 #endif
11242 	if (!bp->irq_tbl || !bp->bnapi)
11243 		return;
11244 
11245 	for (i = 0; i < bp->cp_nr_rings; i++) {
11246 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11247 
11248 		irq = &bp->irq_tbl[map_idx];
11249 		if (irq->requested) {
11250 			if (irq->have_cpumask) {
11251 				irq_update_affinity_hint(irq->vector, NULL);
11252 				free_cpumask_var(irq->cpu_mask);
11253 				irq->have_cpumask = 0;
11254 			}
11255 			free_irq(irq->vector, bp->bnapi[i]);
11256 		}
11257 
11258 		irq->requested = 0;
11259 	}
11260 }
11261 
11262 static int bnxt_request_irq(struct bnxt *bp)
11263 {
11264 	int i, j, rc = 0;
11265 	unsigned long flags = 0;
11266 #ifdef CONFIG_RFS_ACCEL
11267 	struct cpu_rmap *rmap;
11268 #endif
11269 
11270 	rc = bnxt_setup_int_mode(bp);
11271 	if (rc) {
11272 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11273 			   rc);
11274 		return rc;
11275 	}
11276 #ifdef CONFIG_RFS_ACCEL
11277 	rmap = bp->dev->rx_cpu_rmap;
11278 #endif
11279 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11280 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11281 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11282 
11283 #ifdef CONFIG_RFS_ACCEL
11284 		if (rmap && bp->bnapi[i]->rx_ring) {
11285 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11286 			if (rc)
11287 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11288 					    j);
11289 			j++;
11290 		}
11291 #endif
11292 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11293 				 bp->bnapi[i]);
11294 		if (rc)
11295 			break;
11296 
11297 		netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector);
11298 		irq->requested = 1;
11299 
11300 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11301 			int numa_node = dev_to_node(&bp->pdev->dev);
11302 
11303 			irq->have_cpumask = 1;
11304 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11305 					irq->cpu_mask);
11306 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11307 			if (rc) {
11308 				netdev_warn(bp->dev,
11309 					    "Update affinity hint failed, IRQ = %d\n",
11310 					    irq->vector);
11311 				break;
11312 			}
11313 		}
11314 	}
11315 	return rc;
11316 }
11317 
11318 static void bnxt_del_napi(struct bnxt *bp)
11319 {
11320 	int i;
11321 
11322 	if (!bp->bnapi)
11323 		return;
11324 
11325 	for (i = 0; i < bp->rx_nr_rings; i++)
11326 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11327 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11328 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11329 
11330 	for (i = 0; i < bp->cp_nr_rings; i++) {
11331 		struct bnxt_napi *bnapi = bp->bnapi[i];
11332 
11333 		__netif_napi_del(&bnapi->napi);
11334 	}
11335 	/* We called __netif_napi_del(), we need
11336 	 * to respect an RCU grace period before freeing napi structures.
11337 	 */
11338 	synchronize_net();
11339 }
11340 
11341 static void bnxt_init_napi(struct bnxt *bp)
11342 {
11343 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11344 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11345 	struct bnxt_napi *bnapi;
11346 	int i;
11347 
11348 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11349 		poll_fn = bnxt_poll_p5;
11350 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11351 		cp_nr_rings--;
11352 	for (i = 0; i < cp_nr_rings; i++) {
11353 		bnapi = bp->bnapi[i];
11354 		netif_napi_add_config(bp->dev, &bnapi->napi, poll_fn,
11355 				      bnapi->index);
11356 	}
11357 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11358 		bnapi = bp->bnapi[cp_nr_rings];
11359 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11360 	}
11361 }
11362 
11363 static void bnxt_disable_napi(struct bnxt *bp)
11364 {
11365 	int i;
11366 
11367 	if (!bp->bnapi ||
11368 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11369 		return;
11370 
11371 	for (i = 0; i < bp->cp_nr_rings; i++) {
11372 		struct bnxt_napi *bnapi = bp->bnapi[i];
11373 		struct bnxt_cp_ring_info *cpr;
11374 
11375 		cpr = &bnapi->cp_ring;
11376 		if (bnapi->tx_fault)
11377 			cpr->sw_stats->tx.tx_resets++;
11378 		if (bnapi->in_reset)
11379 			cpr->sw_stats->rx.rx_resets++;
11380 		napi_disable(&bnapi->napi);
11381 	}
11382 }
11383 
11384 static void bnxt_enable_napi(struct bnxt *bp)
11385 {
11386 	int i;
11387 
11388 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11389 	for (i = 0; i < bp->cp_nr_rings; i++) {
11390 		struct bnxt_napi *bnapi = bp->bnapi[i];
11391 		struct bnxt_cp_ring_info *cpr;
11392 
11393 		bnapi->tx_fault = 0;
11394 
11395 		cpr = &bnapi->cp_ring;
11396 		bnapi->in_reset = false;
11397 
11398 		if (bnapi->rx_ring) {
11399 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11400 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11401 		}
11402 		napi_enable(&bnapi->napi);
11403 	}
11404 }
11405 
11406 void bnxt_tx_disable(struct bnxt *bp)
11407 {
11408 	int i;
11409 	struct bnxt_tx_ring_info *txr;
11410 
11411 	if (bp->tx_ring) {
11412 		for (i = 0; i < bp->tx_nr_rings; i++) {
11413 			txr = &bp->tx_ring[i];
11414 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11415 		}
11416 	}
11417 	/* Make sure napi polls see @dev_state change */
11418 	synchronize_net();
11419 	/* Drop carrier first to prevent TX timeout */
11420 	netif_carrier_off(bp->dev);
11421 	/* Stop all TX queues */
11422 	netif_tx_disable(bp->dev);
11423 }
11424 
11425 void bnxt_tx_enable(struct bnxt *bp)
11426 {
11427 	int i;
11428 	struct bnxt_tx_ring_info *txr;
11429 
11430 	for (i = 0; i < bp->tx_nr_rings; i++) {
11431 		txr = &bp->tx_ring[i];
11432 		WRITE_ONCE(txr->dev_state, 0);
11433 	}
11434 	/* Make sure napi polls see @dev_state change */
11435 	synchronize_net();
11436 	netif_tx_wake_all_queues(bp->dev);
11437 	if (BNXT_LINK_IS_UP(bp))
11438 		netif_carrier_on(bp->dev);
11439 }
11440 
11441 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11442 {
11443 	u8 active_fec = link_info->active_fec_sig_mode &
11444 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11445 
11446 	switch (active_fec) {
11447 	default:
11448 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11449 		return "None";
11450 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11451 		return "Clause 74 BaseR";
11452 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11453 		return "Clause 91 RS(528,514)";
11454 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11455 		return "Clause 91 RS544_1XN";
11456 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11457 		return "Clause 91 RS(544,514)";
11458 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11459 		return "Clause 91 RS272_1XN";
11460 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11461 		return "Clause 91 RS(272,257)";
11462 	}
11463 }
11464 
11465 void bnxt_report_link(struct bnxt *bp)
11466 {
11467 	if (BNXT_LINK_IS_UP(bp)) {
11468 		const char *signal = "";
11469 		const char *flow_ctrl;
11470 		const char *duplex;
11471 		u32 speed;
11472 		u16 fec;
11473 
11474 		netif_carrier_on(bp->dev);
11475 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11476 		if (speed == SPEED_UNKNOWN) {
11477 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11478 			return;
11479 		}
11480 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11481 			duplex = "full";
11482 		else
11483 			duplex = "half";
11484 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11485 			flow_ctrl = "ON - receive & transmit";
11486 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11487 			flow_ctrl = "ON - transmit";
11488 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11489 			flow_ctrl = "ON - receive";
11490 		else
11491 			flow_ctrl = "none";
11492 		if (bp->link_info.phy_qcfg_resp.option_flags &
11493 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11494 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11495 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11496 			switch (sig_mode) {
11497 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11498 				signal = "(NRZ) ";
11499 				break;
11500 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11501 				signal = "(PAM4 56Gbps) ";
11502 				break;
11503 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11504 				signal = "(PAM4 112Gbps) ";
11505 				break;
11506 			default:
11507 				break;
11508 			}
11509 		}
11510 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11511 			    speed, signal, duplex, flow_ctrl);
11512 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11513 			netdev_info(bp->dev, "EEE is %s\n",
11514 				    bp->eee.eee_active ? "active" :
11515 							 "not active");
11516 		fec = bp->link_info.fec_cfg;
11517 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11518 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11519 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11520 				    bnxt_report_fec(&bp->link_info));
11521 	} else {
11522 		netif_carrier_off(bp->dev);
11523 		netdev_err(bp->dev, "NIC Link is Down\n");
11524 	}
11525 }
11526 
11527 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11528 {
11529 	if (!resp->supported_speeds_auto_mode &&
11530 	    !resp->supported_speeds_force_mode &&
11531 	    !resp->supported_pam4_speeds_auto_mode &&
11532 	    !resp->supported_pam4_speeds_force_mode &&
11533 	    !resp->supported_speeds2_auto_mode &&
11534 	    !resp->supported_speeds2_force_mode)
11535 		return true;
11536 	return false;
11537 }
11538 
11539 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11540 {
11541 	struct bnxt_link_info *link_info = &bp->link_info;
11542 	struct hwrm_port_phy_qcaps_output *resp;
11543 	struct hwrm_port_phy_qcaps_input *req;
11544 	int rc = 0;
11545 
11546 	if (bp->hwrm_spec_code < 0x10201)
11547 		return 0;
11548 
11549 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11550 	if (rc)
11551 		return rc;
11552 
11553 	resp = hwrm_req_hold(bp, req);
11554 	rc = hwrm_req_send(bp, req);
11555 	if (rc)
11556 		goto hwrm_phy_qcaps_exit;
11557 
11558 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11559 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11560 		struct ethtool_keee *eee = &bp->eee;
11561 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11562 
11563 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11564 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11565 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11566 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11567 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11568 	}
11569 
11570 	if (bp->hwrm_spec_code >= 0x10a01) {
11571 		if (bnxt_phy_qcaps_no_speed(resp)) {
11572 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11573 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11574 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11575 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11576 			netdev_info(bp->dev, "Ethernet link enabled\n");
11577 			/* Phy re-enabled, reprobe the speeds */
11578 			link_info->support_auto_speeds = 0;
11579 			link_info->support_pam4_auto_speeds = 0;
11580 			link_info->support_auto_speeds2 = 0;
11581 		}
11582 	}
11583 	if (resp->supported_speeds_auto_mode)
11584 		link_info->support_auto_speeds =
11585 			le16_to_cpu(resp->supported_speeds_auto_mode);
11586 	if (resp->supported_pam4_speeds_auto_mode)
11587 		link_info->support_pam4_auto_speeds =
11588 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11589 	if (resp->supported_speeds2_auto_mode)
11590 		link_info->support_auto_speeds2 =
11591 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11592 
11593 	bp->port_count = resp->port_cnt;
11594 
11595 hwrm_phy_qcaps_exit:
11596 	hwrm_req_drop(bp, req);
11597 	return rc;
11598 }
11599 
11600 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
11601 {
11602 	struct hwrm_port_mac_qcaps_output *resp;
11603 	struct hwrm_port_mac_qcaps_input *req;
11604 	int rc;
11605 
11606 	if (bp->hwrm_spec_code < 0x10a03)
11607 		return;
11608 
11609 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
11610 	if (rc)
11611 		return;
11612 
11613 	resp = hwrm_req_hold(bp, req);
11614 	rc = hwrm_req_send_silent(bp, req);
11615 	if (!rc)
11616 		bp->mac_flags = resp->flags;
11617 	hwrm_req_drop(bp, req);
11618 }
11619 
11620 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11621 {
11622 	u16 diff = advertising ^ supported;
11623 
11624 	return ((supported | diff) != supported);
11625 }
11626 
11627 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11628 {
11629 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11630 
11631 	/* Check if any advertised speeds are no longer supported. The caller
11632 	 * holds the link_lock mutex, so we can modify link_info settings.
11633 	 */
11634 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11635 		if (bnxt_support_dropped(link_info->advertising,
11636 					 link_info->support_auto_speeds2)) {
11637 			link_info->advertising = link_info->support_auto_speeds2;
11638 			return true;
11639 		}
11640 		return false;
11641 	}
11642 	if (bnxt_support_dropped(link_info->advertising,
11643 				 link_info->support_auto_speeds)) {
11644 		link_info->advertising = link_info->support_auto_speeds;
11645 		return true;
11646 	}
11647 	if (bnxt_support_dropped(link_info->advertising_pam4,
11648 				 link_info->support_pam4_auto_speeds)) {
11649 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
11650 		return true;
11651 	}
11652 	return false;
11653 }
11654 
11655 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
11656 {
11657 	struct bnxt_link_info *link_info = &bp->link_info;
11658 	struct hwrm_port_phy_qcfg_output *resp;
11659 	struct hwrm_port_phy_qcfg_input *req;
11660 	u8 link_state = link_info->link_state;
11661 	bool support_changed;
11662 	int rc;
11663 
11664 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
11665 	if (rc)
11666 		return rc;
11667 
11668 	resp = hwrm_req_hold(bp, req);
11669 	rc = hwrm_req_send(bp, req);
11670 	if (rc) {
11671 		hwrm_req_drop(bp, req);
11672 		if (BNXT_VF(bp) && rc == -ENODEV) {
11673 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
11674 			rc = 0;
11675 		}
11676 		return rc;
11677 	}
11678 
11679 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
11680 	link_info->phy_link_status = resp->link;
11681 	link_info->duplex = resp->duplex_cfg;
11682 	if (bp->hwrm_spec_code >= 0x10800)
11683 		link_info->duplex = resp->duplex_state;
11684 	link_info->pause = resp->pause;
11685 	link_info->auto_mode = resp->auto_mode;
11686 	link_info->auto_pause_setting = resp->auto_pause;
11687 	link_info->lp_pause = resp->link_partner_adv_pause;
11688 	link_info->force_pause_setting = resp->force_pause;
11689 	link_info->duplex_setting = resp->duplex_cfg;
11690 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
11691 		link_info->link_speed = le16_to_cpu(resp->link_speed);
11692 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
11693 			link_info->active_lanes = resp->active_lanes;
11694 	} else {
11695 		link_info->link_speed = 0;
11696 		link_info->active_lanes = 0;
11697 	}
11698 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
11699 	link_info->force_pam4_link_speed =
11700 		le16_to_cpu(resp->force_pam4_link_speed);
11701 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
11702 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
11703 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
11704 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
11705 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
11706 	link_info->auto_pam4_link_speeds =
11707 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
11708 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
11709 	link_info->lp_auto_link_speeds =
11710 		le16_to_cpu(resp->link_partner_adv_speeds);
11711 	link_info->lp_auto_pam4_link_speeds =
11712 		resp->link_partner_pam4_adv_speeds;
11713 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
11714 	link_info->phy_ver[0] = resp->phy_maj;
11715 	link_info->phy_ver[1] = resp->phy_min;
11716 	link_info->phy_ver[2] = resp->phy_bld;
11717 	link_info->media_type = resp->media_type;
11718 	link_info->phy_type = resp->phy_type;
11719 	link_info->transceiver = resp->xcvr_pkg_type;
11720 	link_info->phy_addr = resp->eee_config_phy_addr &
11721 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
11722 	link_info->module_status = resp->module_status;
11723 
11724 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
11725 		struct ethtool_keee *eee = &bp->eee;
11726 		u16 fw_speeds;
11727 
11728 		eee->eee_active = 0;
11729 		if (resp->eee_config_phy_addr &
11730 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
11731 			eee->eee_active = 1;
11732 			fw_speeds = le16_to_cpu(
11733 				resp->link_partner_adv_eee_link_speed_mask);
11734 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
11735 		}
11736 
11737 		/* Pull initial EEE config */
11738 		if (!chng_link_state) {
11739 			if (resp->eee_config_phy_addr &
11740 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
11741 				eee->eee_enabled = 1;
11742 
11743 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
11744 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
11745 
11746 			if (resp->eee_config_phy_addr &
11747 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
11748 				__le32 tmr;
11749 
11750 				eee->tx_lpi_enabled = 1;
11751 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
11752 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
11753 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
11754 			}
11755 		}
11756 	}
11757 
11758 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
11759 	if (bp->hwrm_spec_code >= 0x10504) {
11760 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
11761 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
11762 	}
11763 	/* TODO: need to add more logic to report VF link */
11764 	if (chng_link_state) {
11765 		if (link_info->phy_link_status == BNXT_LINK_LINK)
11766 			link_info->link_state = BNXT_LINK_STATE_UP;
11767 		else
11768 			link_info->link_state = BNXT_LINK_STATE_DOWN;
11769 		if (link_state != link_info->link_state)
11770 			bnxt_report_link(bp);
11771 	} else {
11772 		/* always link down if not require to update link state */
11773 		link_info->link_state = BNXT_LINK_STATE_DOWN;
11774 	}
11775 	hwrm_req_drop(bp, req);
11776 
11777 	if (!BNXT_PHY_CFG_ABLE(bp))
11778 		return 0;
11779 
11780 	support_changed = bnxt_support_speed_dropped(link_info);
11781 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
11782 		bnxt_hwrm_set_link_setting(bp, true, false);
11783 	return 0;
11784 }
11785 
11786 static void bnxt_get_port_module_status(struct bnxt *bp)
11787 {
11788 	struct bnxt_link_info *link_info = &bp->link_info;
11789 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
11790 	u8 module_status;
11791 
11792 	if (bnxt_update_link(bp, true))
11793 		return;
11794 
11795 	module_status = link_info->module_status;
11796 	switch (module_status) {
11797 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
11798 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
11799 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
11800 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
11801 			    bp->pf.port_id);
11802 		if (bp->hwrm_spec_code >= 0x10201) {
11803 			netdev_warn(bp->dev, "Module part number %s\n",
11804 				    resp->phy_vendor_partnumber);
11805 		}
11806 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
11807 			netdev_warn(bp->dev, "TX is disabled\n");
11808 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
11809 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
11810 	}
11811 }
11812 
11813 static void
11814 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11815 {
11816 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
11817 		if (bp->hwrm_spec_code >= 0x10201)
11818 			req->auto_pause =
11819 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
11820 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11821 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
11822 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11823 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
11824 		req->enables |=
11825 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11826 	} else {
11827 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11828 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
11829 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11830 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
11831 		req->enables |=
11832 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
11833 		if (bp->hwrm_spec_code >= 0x10201) {
11834 			req->auto_pause = req->force_pause;
11835 			req->enables |= cpu_to_le32(
11836 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11837 		}
11838 	}
11839 }
11840 
11841 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11842 {
11843 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
11844 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
11845 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11846 			req->enables |=
11847 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
11848 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
11849 		} else if (bp->link_info.advertising) {
11850 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
11851 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
11852 		}
11853 		if (bp->link_info.advertising_pam4) {
11854 			req->enables |=
11855 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
11856 			req->auto_link_pam4_speed_mask =
11857 				cpu_to_le16(bp->link_info.advertising_pam4);
11858 		}
11859 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
11860 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
11861 	} else {
11862 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
11863 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11864 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
11865 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
11866 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
11867 				   (u32)bp->link_info.req_link_speed);
11868 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
11869 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11870 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
11871 		} else {
11872 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11873 		}
11874 	}
11875 
11876 	/* tell chimp that the setting takes effect immediately */
11877 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
11878 }
11879 
11880 int bnxt_hwrm_set_pause(struct bnxt *bp)
11881 {
11882 	struct hwrm_port_phy_cfg_input *req;
11883 	int rc;
11884 
11885 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11886 	if (rc)
11887 		return rc;
11888 
11889 	bnxt_hwrm_set_pause_common(bp, req);
11890 
11891 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
11892 	    bp->link_info.force_link_chng)
11893 		bnxt_hwrm_set_link_common(bp, req);
11894 
11895 	rc = hwrm_req_send(bp, req);
11896 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
11897 		/* since changing of pause setting doesn't trigger any link
11898 		 * change event, the driver needs to update the current pause
11899 		 * result upon successfully return of the phy_cfg command
11900 		 */
11901 		bp->link_info.pause =
11902 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
11903 		bp->link_info.auto_pause_setting = 0;
11904 		if (!bp->link_info.force_link_chng)
11905 			bnxt_report_link(bp);
11906 	}
11907 	bp->link_info.force_link_chng = false;
11908 	return rc;
11909 }
11910 
11911 static void bnxt_hwrm_set_eee(struct bnxt *bp,
11912 			      struct hwrm_port_phy_cfg_input *req)
11913 {
11914 	struct ethtool_keee *eee = &bp->eee;
11915 
11916 	if (eee->eee_enabled) {
11917 		u16 eee_speeds;
11918 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
11919 
11920 		if (eee->tx_lpi_enabled)
11921 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
11922 		else
11923 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
11924 
11925 		req->flags |= cpu_to_le32(flags);
11926 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
11927 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
11928 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
11929 	} else {
11930 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
11931 	}
11932 }
11933 
11934 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
11935 {
11936 	struct hwrm_port_phy_cfg_input *req;
11937 	int rc;
11938 
11939 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11940 	if (rc)
11941 		return rc;
11942 
11943 	if (set_pause)
11944 		bnxt_hwrm_set_pause_common(bp, req);
11945 
11946 	bnxt_hwrm_set_link_common(bp, req);
11947 
11948 	if (set_eee)
11949 		bnxt_hwrm_set_eee(bp, req);
11950 	return hwrm_req_send(bp, req);
11951 }
11952 
11953 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
11954 {
11955 	struct hwrm_port_phy_cfg_input *req;
11956 	int rc;
11957 
11958 	if (!BNXT_SINGLE_PF(bp))
11959 		return 0;
11960 
11961 	if (pci_num_vf(bp->pdev) &&
11962 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
11963 		return 0;
11964 
11965 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11966 	if (rc)
11967 		return rc;
11968 
11969 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
11970 	rc = hwrm_req_send(bp, req);
11971 	if (!rc) {
11972 		mutex_lock(&bp->link_lock);
11973 		/* Device is not obliged link down in certain scenarios, even
11974 		 * when forced. Setting the state unknown is consistent with
11975 		 * driver startup and will force link state to be reported
11976 		 * during subsequent open based on PORT_PHY_QCFG.
11977 		 */
11978 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
11979 		mutex_unlock(&bp->link_lock);
11980 	}
11981 	return rc;
11982 }
11983 
11984 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
11985 {
11986 #ifdef CONFIG_TEE_BNXT_FW
11987 	int rc = tee_bnxt_fw_load();
11988 
11989 	if (rc)
11990 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
11991 
11992 	return rc;
11993 #else
11994 	netdev_err(bp->dev, "OP-TEE not supported\n");
11995 	return -ENODEV;
11996 #endif
11997 }
11998 
11999 static int bnxt_try_recover_fw(struct bnxt *bp)
12000 {
12001 	if (bp->fw_health && bp->fw_health->status_reliable) {
12002 		int retry = 0, rc;
12003 		u32 sts;
12004 
12005 		do {
12006 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12007 			rc = bnxt_hwrm_poll(bp);
12008 			if (!BNXT_FW_IS_BOOTING(sts) &&
12009 			    !BNXT_FW_IS_RECOVERING(sts))
12010 				break;
12011 			retry++;
12012 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12013 
12014 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12015 			netdev_err(bp->dev,
12016 				   "Firmware not responding, status: 0x%x\n",
12017 				   sts);
12018 			rc = -ENODEV;
12019 		}
12020 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12021 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12022 			return bnxt_fw_reset_via_optee(bp);
12023 		}
12024 		return rc;
12025 	}
12026 
12027 	return -ENODEV;
12028 }
12029 
12030 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12031 {
12032 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12033 
12034 	if (!BNXT_NEW_RM(bp))
12035 		return; /* no resource reservations required */
12036 
12037 	hw_resc->resv_cp_rings = 0;
12038 	hw_resc->resv_stat_ctxs = 0;
12039 	hw_resc->resv_irqs = 0;
12040 	hw_resc->resv_tx_rings = 0;
12041 	hw_resc->resv_rx_rings = 0;
12042 	hw_resc->resv_hw_ring_grps = 0;
12043 	hw_resc->resv_vnics = 0;
12044 	hw_resc->resv_rsscos_ctxs = 0;
12045 	if (!fw_reset) {
12046 		bp->tx_nr_rings = 0;
12047 		bp->rx_nr_rings = 0;
12048 	}
12049 }
12050 
12051 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12052 {
12053 	int rc;
12054 
12055 	if (!BNXT_NEW_RM(bp))
12056 		return 0; /* no resource reservations required */
12057 
12058 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12059 	if (rc)
12060 		netdev_err(bp->dev, "resc_qcaps failed\n");
12061 
12062 	bnxt_clear_reservations(bp, fw_reset);
12063 
12064 	return rc;
12065 }
12066 
12067 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12068 {
12069 	struct hwrm_func_drv_if_change_output *resp;
12070 	struct hwrm_func_drv_if_change_input *req;
12071 	bool fw_reset = !bp->irq_tbl;
12072 	bool resc_reinit = false;
12073 	int rc, retry = 0;
12074 	u32 flags = 0;
12075 
12076 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12077 		return 0;
12078 
12079 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12080 	if (rc)
12081 		return rc;
12082 
12083 	if (up)
12084 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12085 	resp = hwrm_req_hold(bp, req);
12086 
12087 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12088 	while (retry < BNXT_FW_IF_RETRY) {
12089 		rc = hwrm_req_send(bp, req);
12090 		if (rc != -EAGAIN)
12091 			break;
12092 
12093 		msleep(50);
12094 		retry++;
12095 	}
12096 
12097 	if (rc == -EAGAIN) {
12098 		hwrm_req_drop(bp, req);
12099 		return rc;
12100 	} else if (!rc) {
12101 		flags = le32_to_cpu(resp->flags);
12102 	} else if (up) {
12103 		rc = bnxt_try_recover_fw(bp);
12104 		fw_reset = true;
12105 	}
12106 	hwrm_req_drop(bp, req);
12107 	if (rc)
12108 		return rc;
12109 
12110 	if (!up) {
12111 		bnxt_inv_fw_health_reg(bp);
12112 		return 0;
12113 	}
12114 
12115 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12116 		resc_reinit = true;
12117 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12118 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12119 		fw_reset = true;
12120 	else
12121 		bnxt_remap_fw_health_regs(bp);
12122 
12123 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12124 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12125 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12126 		return -ENODEV;
12127 	}
12128 	if (resc_reinit || fw_reset) {
12129 		if (fw_reset) {
12130 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12131 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12132 				bnxt_ulp_irq_stop(bp);
12133 			bnxt_free_ctx_mem(bp, false);
12134 			bnxt_dcb_free(bp);
12135 			rc = bnxt_fw_init_one(bp);
12136 			if (rc) {
12137 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12138 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12139 				return rc;
12140 			}
12141 			bnxt_clear_int_mode(bp);
12142 			rc = bnxt_init_int_mode(bp);
12143 			if (rc) {
12144 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12145 				netdev_err(bp->dev, "init int mode failed\n");
12146 				return rc;
12147 			}
12148 		}
12149 		rc = bnxt_cancel_reservations(bp, fw_reset);
12150 	}
12151 	return rc;
12152 }
12153 
12154 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12155 {
12156 	struct hwrm_port_led_qcaps_output *resp;
12157 	struct hwrm_port_led_qcaps_input *req;
12158 	struct bnxt_pf_info *pf = &bp->pf;
12159 	int rc;
12160 
12161 	bp->num_leds = 0;
12162 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12163 		return 0;
12164 
12165 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12166 	if (rc)
12167 		return rc;
12168 
12169 	req->port_id = cpu_to_le16(pf->port_id);
12170 	resp = hwrm_req_hold(bp, req);
12171 	rc = hwrm_req_send(bp, req);
12172 	if (rc) {
12173 		hwrm_req_drop(bp, req);
12174 		return rc;
12175 	}
12176 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12177 		int i;
12178 
12179 		bp->num_leds = resp->num_leds;
12180 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12181 						 bp->num_leds);
12182 		for (i = 0; i < bp->num_leds; i++) {
12183 			struct bnxt_led_info *led = &bp->leds[i];
12184 			__le16 caps = led->led_state_caps;
12185 
12186 			if (!led->led_group_id ||
12187 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12188 				bp->num_leds = 0;
12189 				break;
12190 			}
12191 		}
12192 	}
12193 	hwrm_req_drop(bp, req);
12194 	return 0;
12195 }
12196 
12197 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12198 {
12199 	struct hwrm_wol_filter_alloc_output *resp;
12200 	struct hwrm_wol_filter_alloc_input *req;
12201 	int rc;
12202 
12203 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12204 	if (rc)
12205 		return rc;
12206 
12207 	req->port_id = cpu_to_le16(bp->pf.port_id);
12208 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12209 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12210 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12211 
12212 	resp = hwrm_req_hold(bp, req);
12213 	rc = hwrm_req_send(bp, req);
12214 	if (!rc)
12215 		bp->wol_filter_id = resp->wol_filter_id;
12216 	hwrm_req_drop(bp, req);
12217 	return rc;
12218 }
12219 
12220 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12221 {
12222 	struct hwrm_wol_filter_free_input *req;
12223 	int rc;
12224 
12225 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12226 	if (rc)
12227 		return rc;
12228 
12229 	req->port_id = cpu_to_le16(bp->pf.port_id);
12230 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12231 	req->wol_filter_id = bp->wol_filter_id;
12232 
12233 	return hwrm_req_send(bp, req);
12234 }
12235 
12236 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12237 {
12238 	struct hwrm_wol_filter_qcfg_output *resp;
12239 	struct hwrm_wol_filter_qcfg_input *req;
12240 	u16 next_handle = 0;
12241 	int rc;
12242 
12243 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12244 	if (rc)
12245 		return rc;
12246 
12247 	req->port_id = cpu_to_le16(bp->pf.port_id);
12248 	req->handle = cpu_to_le16(handle);
12249 	resp = hwrm_req_hold(bp, req);
12250 	rc = hwrm_req_send(bp, req);
12251 	if (!rc) {
12252 		next_handle = le16_to_cpu(resp->next_handle);
12253 		if (next_handle != 0) {
12254 			if (resp->wol_type ==
12255 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12256 				bp->wol = 1;
12257 				bp->wol_filter_id = resp->wol_filter_id;
12258 			}
12259 		}
12260 	}
12261 	hwrm_req_drop(bp, req);
12262 	return next_handle;
12263 }
12264 
12265 static void bnxt_get_wol_settings(struct bnxt *bp)
12266 {
12267 	u16 handle = 0;
12268 
12269 	bp->wol = 0;
12270 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12271 		return;
12272 
12273 	do {
12274 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12275 	} while (handle && handle != 0xffff);
12276 }
12277 
12278 static bool bnxt_eee_config_ok(struct bnxt *bp)
12279 {
12280 	struct ethtool_keee *eee = &bp->eee;
12281 	struct bnxt_link_info *link_info = &bp->link_info;
12282 
12283 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12284 		return true;
12285 
12286 	if (eee->eee_enabled) {
12287 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12288 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12289 
12290 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12291 
12292 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12293 			eee->eee_enabled = 0;
12294 			return false;
12295 		}
12296 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12297 			linkmode_and(eee->advertised, advertising,
12298 				     eee->supported);
12299 			return false;
12300 		}
12301 	}
12302 	return true;
12303 }
12304 
12305 static int bnxt_update_phy_setting(struct bnxt *bp)
12306 {
12307 	int rc;
12308 	bool update_link = false;
12309 	bool update_pause = false;
12310 	bool update_eee = false;
12311 	struct bnxt_link_info *link_info = &bp->link_info;
12312 
12313 	rc = bnxt_update_link(bp, true);
12314 	if (rc) {
12315 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12316 			   rc);
12317 		return rc;
12318 	}
12319 	if (!BNXT_SINGLE_PF(bp))
12320 		return 0;
12321 
12322 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12323 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12324 	    link_info->req_flow_ctrl)
12325 		update_pause = true;
12326 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12327 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12328 		update_pause = true;
12329 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12330 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12331 			update_link = true;
12332 		if (bnxt_force_speed_updated(link_info))
12333 			update_link = true;
12334 		if (link_info->req_duplex != link_info->duplex_setting)
12335 			update_link = true;
12336 	} else {
12337 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12338 			update_link = true;
12339 		if (bnxt_auto_speed_updated(link_info))
12340 			update_link = true;
12341 	}
12342 
12343 	/* The last close may have shutdown the link, so need to call
12344 	 * PHY_CFG to bring it back up.
12345 	 */
12346 	if (!BNXT_LINK_IS_UP(bp))
12347 		update_link = true;
12348 
12349 	if (!bnxt_eee_config_ok(bp))
12350 		update_eee = true;
12351 
12352 	if (update_link)
12353 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12354 	else if (update_pause)
12355 		rc = bnxt_hwrm_set_pause(bp);
12356 	if (rc) {
12357 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12358 			   rc);
12359 		return rc;
12360 	}
12361 
12362 	return rc;
12363 }
12364 
12365 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12366 
12367 static int bnxt_reinit_after_abort(struct bnxt *bp)
12368 {
12369 	int rc;
12370 
12371 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12372 		return -EBUSY;
12373 
12374 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12375 		return -ENODEV;
12376 
12377 	rc = bnxt_fw_init_one(bp);
12378 	if (!rc) {
12379 		bnxt_clear_int_mode(bp);
12380 		rc = bnxt_init_int_mode(bp);
12381 		if (!rc) {
12382 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12383 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12384 		}
12385 	}
12386 	return rc;
12387 }
12388 
12389 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12390 {
12391 	struct bnxt_ntuple_filter *ntp_fltr;
12392 	struct bnxt_l2_filter *l2_fltr;
12393 
12394 	if (list_empty(&fltr->list))
12395 		return;
12396 
12397 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12398 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12399 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12400 		atomic_inc(&l2_fltr->refcnt);
12401 		ntp_fltr->l2_fltr = l2_fltr;
12402 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12403 			bnxt_del_ntp_filter(bp, ntp_fltr);
12404 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12405 				   fltr->sw_id);
12406 		}
12407 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12408 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12409 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12410 			bnxt_del_l2_filter(bp, l2_fltr);
12411 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12412 				   fltr->sw_id);
12413 		}
12414 	}
12415 }
12416 
12417 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12418 {
12419 	struct bnxt_filter_base *usr_fltr, *tmp;
12420 
12421 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12422 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12423 }
12424 
12425 static int bnxt_set_xps_mapping(struct bnxt *bp)
12426 {
12427 	int numa_node = dev_to_node(&bp->pdev->dev);
12428 	unsigned int q_idx, map_idx, cpu, i;
12429 	const struct cpumask *cpu_mask_ptr;
12430 	int nr_cpus = num_online_cpus();
12431 	cpumask_t *q_map;
12432 	int rc = 0;
12433 
12434 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12435 	if (!q_map)
12436 		return -ENOMEM;
12437 
12438 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12439 	 * Each TC has the same number of TX queues. The nth TX queue for each
12440 	 * TC will have the same CPU mask.
12441 	 */
12442 	for (i = 0; i < nr_cpus; i++) {
12443 		map_idx = i % bp->tx_nr_rings_per_tc;
12444 		cpu = cpumask_local_spread(i, numa_node);
12445 		cpu_mask_ptr = get_cpu_mask(cpu);
12446 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12447 	}
12448 
12449 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12450 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12451 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12452 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12453 		if (rc) {
12454 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12455 				    q_idx);
12456 			break;
12457 		}
12458 	}
12459 
12460 	kfree(q_map);
12461 
12462 	return rc;
12463 }
12464 
12465 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12466 {
12467 	int rc = 0;
12468 
12469 	netif_carrier_off(bp->dev);
12470 	if (irq_re_init) {
12471 		/* Reserve rings now if none were reserved at driver probe. */
12472 		rc = bnxt_init_dflt_ring_mode(bp);
12473 		if (rc) {
12474 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12475 			return rc;
12476 		}
12477 	}
12478 	rc = bnxt_reserve_rings(bp, irq_re_init);
12479 	if (rc)
12480 		return rc;
12481 
12482 	rc = bnxt_alloc_mem(bp, irq_re_init);
12483 	if (rc) {
12484 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12485 		goto open_err_free_mem;
12486 	}
12487 
12488 	if (irq_re_init) {
12489 		bnxt_init_napi(bp);
12490 		rc = bnxt_request_irq(bp);
12491 		if (rc) {
12492 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12493 			goto open_err_irq;
12494 		}
12495 	}
12496 
12497 	rc = bnxt_init_nic(bp, irq_re_init);
12498 	if (rc) {
12499 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12500 		goto open_err_irq;
12501 	}
12502 
12503 	bnxt_enable_napi(bp);
12504 	bnxt_debug_dev_init(bp);
12505 
12506 	if (link_re_init) {
12507 		mutex_lock(&bp->link_lock);
12508 		rc = bnxt_update_phy_setting(bp);
12509 		mutex_unlock(&bp->link_lock);
12510 		if (rc) {
12511 			netdev_warn(bp->dev, "failed to update phy settings\n");
12512 			if (BNXT_SINGLE_PF(bp)) {
12513 				bp->link_info.phy_retry = true;
12514 				bp->link_info.phy_retry_expires =
12515 					jiffies + 5 * HZ;
12516 			}
12517 		}
12518 	}
12519 
12520 	if (irq_re_init) {
12521 		udp_tunnel_nic_reset_ntf(bp->dev);
12522 		rc = bnxt_set_xps_mapping(bp);
12523 		if (rc)
12524 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12525 	}
12526 
12527 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12528 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12529 			static_branch_enable(&bnxt_xdp_locking_key);
12530 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12531 		static_branch_disable(&bnxt_xdp_locking_key);
12532 	}
12533 	set_bit(BNXT_STATE_OPEN, &bp->state);
12534 	bnxt_enable_int(bp);
12535 	/* Enable TX queues */
12536 	bnxt_tx_enable(bp);
12537 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12538 	/* Poll link status and check for SFP+ module status */
12539 	mutex_lock(&bp->link_lock);
12540 	bnxt_get_port_module_status(bp);
12541 	mutex_unlock(&bp->link_lock);
12542 
12543 	/* VF-reps may need to be re-opened after the PF is re-opened */
12544 	if (BNXT_PF(bp))
12545 		bnxt_vf_reps_open(bp);
12546 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
12547 		WRITE_ONCE(bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS);
12548 	bnxt_ptp_init_rtc(bp, true);
12549 	bnxt_ptp_cfg_tstamp_filters(bp);
12550 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12551 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12552 	bnxt_cfg_usr_fltrs(bp);
12553 	return 0;
12554 
12555 open_err_irq:
12556 	bnxt_del_napi(bp);
12557 
12558 open_err_free_mem:
12559 	bnxt_free_skbs(bp);
12560 	bnxt_free_irq(bp);
12561 	bnxt_free_mem(bp, true);
12562 	return rc;
12563 }
12564 
12565 /* rtnl_lock held */
12566 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12567 {
12568 	int rc = 0;
12569 
12570 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12571 		rc = -EIO;
12572 	if (!rc)
12573 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12574 	if (rc) {
12575 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12576 		dev_close(bp->dev);
12577 	}
12578 	return rc;
12579 }
12580 
12581 /* rtnl_lock held, open the NIC half way by allocating all resources, but
12582  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
12583  * self tests.
12584  */
12585 int bnxt_half_open_nic(struct bnxt *bp)
12586 {
12587 	int rc = 0;
12588 
12589 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12590 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12591 		rc = -ENODEV;
12592 		goto half_open_err;
12593 	}
12594 
12595 	rc = bnxt_alloc_mem(bp, true);
12596 	if (rc) {
12597 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12598 		goto half_open_err;
12599 	}
12600 	bnxt_init_napi(bp);
12601 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12602 	rc = bnxt_init_nic(bp, true);
12603 	if (rc) {
12604 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12605 		bnxt_del_napi(bp);
12606 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12607 		goto half_open_err;
12608 	}
12609 	return 0;
12610 
12611 half_open_err:
12612 	bnxt_free_skbs(bp);
12613 	bnxt_free_mem(bp, true);
12614 	dev_close(bp->dev);
12615 	return rc;
12616 }
12617 
12618 /* rtnl_lock held, this call can only be made after a previous successful
12619  * call to bnxt_half_open_nic().
12620  */
12621 void bnxt_half_close_nic(struct bnxt *bp)
12622 {
12623 	bnxt_hwrm_resource_free(bp, false, true);
12624 	bnxt_del_napi(bp);
12625 	bnxt_free_skbs(bp);
12626 	bnxt_free_mem(bp, true);
12627 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12628 }
12629 
12630 void bnxt_reenable_sriov(struct bnxt *bp)
12631 {
12632 	if (BNXT_PF(bp)) {
12633 		struct bnxt_pf_info *pf = &bp->pf;
12634 		int n = pf->active_vfs;
12635 
12636 		if (n)
12637 			bnxt_cfg_hw_sriov(bp, &n, true);
12638 	}
12639 }
12640 
12641 static int bnxt_open(struct net_device *dev)
12642 {
12643 	struct bnxt *bp = netdev_priv(dev);
12644 	int rc;
12645 
12646 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12647 		rc = bnxt_reinit_after_abort(bp);
12648 		if (rc) {
12649 			if (rc == -EBUSY)
12650 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
12651 			else
12652 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
12653 			return -ENODEV;
12654 		}
12655 	}
12656 
12657 	rc = bnxt_hwrm_if_change(bp, true);
12658 	if (rc)
12659 		return rc;
12660 
12661 	rc = __bnxt_open_nic(bp, true, true);
12662 	if (rc) {
12663 		bnxt_hwrm_if_change(bp, false);
12664 	} else {
12665 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
12666 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12667 				bnxt_queue_sp_work(bp,
12668 						   BNXT_RESTART_ULP_SP_EVENT);
12669 		}
12670 	}
12671 
12672 	return rc;
12673 }
12674 
12675 static bool bnxt_drv_busy(struct bnxt *bp)
12676 {
12677 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
12678 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
12679 }
12680 
12681 static void bnxt_get_ring_stats(struct bnxt *bp,
12682 				struct rtnl_link_stats64 *stats);
12683 
12684 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
12685 			     bool link_re_init)
12686 {
12687 	/* Close the VF-reps before closing PF */
12688 	if (BNXT_PF(bp))
12689 		bnxt_vf_reps_close(bp);
12690 
12691 	/* Change device state to avoid TX queue wake up's */
12692 	bnxt_tx_disable(bp);
12693 
12694 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12695 	smp_mb__after_atomic();
12696 	while (bnxt_drv_busy(bp))
12697 		msleep(20);
12698 
12699 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12700 		bnxt_clear_rss_ctxs(bp);
12701 	/* Flush rings and disable interrupts */
12702 	bnxt_shutdown_nic(bp, irq_re_init);
12703 
12704 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
12705 
12706 	bnxt_debug_dev_exit(bp);
12707 	bnxt_disable_napi(bp);
12708 	del_timer_sync(&bp->timer);
12709 	bnxt_free_skbs(bp);
12710 
12711 	/* Save ring stats before shutdown */
12712 	if (bp->bnapi && irq_re_init) {
12713 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
12714 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
12715 	}
12716 	if (irq_re_init) {
12717 		bnxt_free_irq(bp);
12718 		bnxt_del_napi(bp);
12719 	}
12720 	bnxt_free_mem(bp, irq_re_init);
12721 }
12722 
12723 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12724 {
12725 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12726 		/* If we get here, it means firmware reset is in progress
12727 		 * while we are trying to close.  We can safely proceed with
12728 		 * the close because we are holding rtnl_lock().  Some firmware
12729 		 * messages may fail as we proceed to close.  We set the
12730 		 * ABORT_ERR flag here so that the FW reset thread will later
12731 		 * abort when it gets the rtnl_lock() and sees the flag.
12732 		 */
12733 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
12734 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12735 	}
12736 
12737 #ifdef CONFIG_BNXT_SRIOV
12738 	if (bp->sriov_cfg) {
12739 		int rc;
12740 
12741 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
12742 						      !bp->sriov_cfg,
12743 						      BNXT_SRIOV_CFG_WAIT_TMO);
12744 		if (!rc)
12745 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
12746 		else if (rc < 0)
12747 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
12748 	}
12749 #endif
12750 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
12751 }
12752 
12753 static int bnxt_close(struct net_device *dev)
12754 {
12755 	struct bnxt *bp = netdev_priv(dev);
12756 
12757 	bnxt_close_nic(bp, true, true);
12758 	bnxt_hwrm_shutdown_link(bp);
12759 	bnxt_hwrm_if_change(bp, false);
12760 	return 0;
12761 }
12762 
12763 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
12764 				   u16 *val)
12765 {
12766 	struct hwrm_port_phy_mdio_read_output *resp;
12767 	struct hwrm_port_phy_mdio_read_input *req;
12768 	int rc;
12769 
12770 	if (bp->hwrm_spec_code < 0x10a00)
12771 		return -EOPNOTSUPP;
12772 
12773 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
12774 	if (rc)
12775 		return rc;
12776 
12777 	req->port_id = cpu_to_le16(bp->pf.port_id);
12778 	req->phy_addr = phy_addr;
12779 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12780 	if (mdio_phy_id_is_c45(phy_addr)) {
12781 		req->cl45_mdio = 1;
12782 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12783 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12784 		req->reg_addr = cpu_to_le16(reg);
12785 	}
12786 
12787 	resp = hwrm_req_hold(bp, req);
12788 	rc = hwrm_req_send(bp, req);
12789 	if (!rc)
12790 		*val = le16_to_cpu(resp->reg_data);
12791 	hwrm_req_drop(bp, req);
12792 	return rc;
12793 }
12794 
12795 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
12796 				    u16 val)
12797 {
12798 	struct hwrm_port_phy_mdio_write_input *req;
12799 	int rc;
12800 
12801 	if (bp->hwrm_spec_code < 0x10a00)
12802 		return -EOPNOTSUPP;
12803 
12804 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
12805 	if (rc)
12806 		return rc;
12807 
12808 	req->port_id = cpu_to_le16(bp->pf.port_id);
12809 	req->phy_addr = phy_addr;
12810 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12811 	if (mdio_phy_id_is_c45(phy_addr)) {
12812 		req->cl45_mdio = 1;
12813 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12814 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12815 		req->reg_addr = cpu_to_le16(reg);
12816 	}
12817 	req->reg_data = cpu_to_le16(val);
12818 
12819 	return hwrm_req_send(bp, req);
12820 }
12821 
12822 /* rtnl_lock held */
12823 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12824 {
12825 	struct mii_ioctl_data *mdio = if_mii(ifr);
12826 	struct bnxt *bp = netdev_priv(dev);
12827 	int rc;
12828 
12829 	switch (cmd) {
12830 	case SIOCGMIIPHY:
12831 		mdio->phy_id = bp->link_info.phy_addr;
12832 
12833 		fallthrough;
12834 	case SIOCGMIIREG: {
12835 		u16 mii_regval = 0;
12836 
12837 		if (!netif_running(dev))
12838 			return -EAGAIN;
12839 
12840 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
12841 					     &mii_regval);
12842 		mdio->val_out = mii_regval;
12843 		return rc;
12844 	}
12845 
12846 	case SIOCSMIIREG:
12847 		if (!netif_running(dev))
12848 			return -EAGAIN;
12849 
12850 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
12851 						mdio->val_in);
12852 
12853 	case SIOCSHWTSTAMP:
12854 		return bnxt_hwtstamp_set(dev, ifr);
12855 
12856 	case SIOCGHWTSTAMP:
12857 		return bnxt_hwtstamp_get(dev, ifr);
12858 
12859 	default:
12860 		/* do nothing */
12861 		break;
12862 	}
12863 	return -EOPNOTSUPP;
12864 }
12865 
12866 static void bnxt_get_ring_stats(struct bnxt *bp,
12867 				struct rtnl_link_stats64 *stats)
12868 {
12869 	int i;
12870 
12871 	for (i = 0; i < bp->cp_nr_rings; i++) {
12872 		struct bnxt_napi *bnapi = bp->bnapi[i];
12873 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
12874 		u64 *sw = cpr->stats.sw_stats;
12875 
12876 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
12877 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12878 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
12879 
12880 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
12881 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
12882 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
12883 
12884 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
12885 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
12886 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
12887 
12888 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
12889 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
12890 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
12891 
12892 		stats->rx_missed_errors +=
12893 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
12894 
12895 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12896 
12897 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
12898 
12899 		stats->rx_dropped +=
12900 			cpr->sw_stats->rx.rx_netpoll_discards +
12901 			cpr->sw_stats->rx.rx_oom_discards;
12902 	}
12903 }
12904 
12905 static void bnxt_add_prev_stats(struct bnxt *bp,
12906 				struct rtnl_link_stats64 *stats)
12907 {
12908 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
12909 
12910 	stats->rx_packets += prev_stats->rx_packets;
12911 	stats->tx_packets += prev_stats->tx_packets;
12912 	stats->rx_bytes += prev_stats->rx_bytes;
12913 	stats->tx_bytes += prev_stats->tx_bytes;
12914 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
12915 	stats->multicast += prev_stats->multicast;
12916 	stats->rx_dropped += prev_stats->rx_dropped;
12917 	stats->tx_dropped += prev_stats->tx_dropped;
12918 }
12919 
12920 static void
12921 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
12922 {
12923 	struct bnxt *bp = netdev_priv(dev);
12924 
12925 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
12926 	/* Make sure bnxt_close_nic() sees that we are reading stats before
12927 	 * we check the BNXT_STATE_OPEN flag.
12928 	 */
12929 	smp_mb__after_atomic();
12930 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12931 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12932 		*stats = bp->net_stats_prev;
12933 		return;
12934 	}
12935 
12936 	bnxt_get_ring_stats(bp, stats);
12937 	bnxt_add_prev_stats(bp, stats);
12938 
12939 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
12940 		u64 *rx = bp->port_stats.sw_stats;
12941 		u64 *tx = bp->port_stats.sw_stats +
12942 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
12943 
12944 		stats->rx_crc_errors =
12945 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
12946 		stats->rx_frame_errors =
12947 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
12948 		stats->rx_length_errors =
12949 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
12950 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
12951 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
12952 		stats->rx_errors =
12953 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
12954 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
12955 		stats->collisions =
12956 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
12957 		stats->tx_fifo_errors =
12958 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
12959 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
12960 	}
12961 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12962 }
12963 
12964 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
12965 					struct bnxt_total_ring_err_stats *stats,
12966 					struct bnxt_cp_ring_info *cpr)
12967 {
12968 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
12969 	u64 *hw_stats = cpr->stats.sw_stats;
12970 
12971 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
12972 	stats->rx_total_resets += sw_stats->rx.rx_resets;
12973 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
12974 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
12975 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
12976 	stats->rx_total_ring_discards +=
12977 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
12978 	stats->tx_total_resets += sw_stats->tx.tx_resets;
12979 	stats->tx_total_ring_discards +=
12980 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
12981 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
12982 }
12983 
12984 void bnxt_get_ring_err_stats(struct bnxt *bp,
12985 			     struct bnxt_total_ring_err_stats *stats)
12986 {
12987 	int i;
12988 
12989 	for (i = 0; i < bp->cp_nr_rings; i++)
12990 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
12991 }
12992 
12993 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
12994 {
12995 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12996 	struct net_device *dev = bp->dev;
12997 	struct netdev_hw_addr *ha;
12998 	u8 *haddr;
12999 	int mc_count = 0;
13000 	bool update = false;
13001 	int off = 0;
13002 
13003 	netdev_for_each_mc_addr(ha, dev) {
13004 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13005 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13006 			vnic->mc_list_count = 0;
13007 			return false;
13008 		}
13009 		haddr = ha->addr;
13010 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13011 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13012 			update = true;
13013 		}
13014 		off += ETH_ALEN;
13015 		mc_count++;
13016 	}
13017 	if (mc_count)
13018 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13019 
13020 	if (mc_count != vnic->mc_list_count) {
13021 		vnic->mc_list_count = mc_count;
13022 		update = true;
13023 	}
13024 	return update;
13025 }
13026 
13027 static bool bnxt_uc_list_updated(struct bnxt *bp)
13028 {
13029 	struct net_device *dev = bp->dev;
13030 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13031 	struct netdev_hw_addr *ha;
13032 	int off = 0;
13033 
13034 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
13035 		return true;
13036 
13037 	netdev_for_each_uc_addr(ha, dev) {
13038 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13039 			return true;
13040 
13041 		off += ETH_ALEN;
13042 	}
13043 	return false;
13044 }
13045 
13046 static void bnxt_set_rx_mode(struct net_device *dev)
13047 {
13048 	struct bnxt *bp = netdev_priv(dev);
13049 	struct bnxt_vnic_info *vnic;
13050 	bool mc_update = false;
13051 	bool uc_update;
13052 	u32 mask;
13053 
13054 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13055 		return;
13056 
13057 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13058 	mask = vnic->rx_mask;
13059 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13060 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13061 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13062 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13063 
13064 	if (dev->flags & IFF_PROMISC)
13065 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13066 
13067 	uc_update = bnxt_uc_list_updated(bp);
13068 
13069 	if (dev->flags & IFF_BROADCAST)
13070 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13071 	if (dev->flags & IFF_ALLMULTI) {
13072 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13073 		vnic->mc_list_count = 0;
13074 	} else if (dev->flags & IFF_MULTICAST) {
13075 		mc_update = bnxt_mc_list_updated(bp, &mask);
13076 	}
13077 
13078 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13079 		vnic->rx_mask = mask;
13080 
13081 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13082 	}
13083 }
13084 
13085 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13086 {
13087 	struct net_device *dev = bp->dev;
13088 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13089 	struct netdev_hw_addr *ha;
13090 	int i, off = 0, rc;
13091 	bool uc_update;
13092 
13093 	netif_addr_lock_bh(dev);
13094 	uc_update = bnxt_uc_list_updated(bp);
13095 	netif_addr_unlock_bh(dev);
13096 
13097 	if (!uc_update)
13098 		goto skip_uc;
13099 
13100 	for (i = 1; i < vnic->uc_filter_count; i++) {
13101 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13102 
13103 		bnxt_hwrm_l2_filter_free(bp, fltr);
13104 		bnxt_del_l2_filter(bp, fltr);
13105 	}
13106 
13107 	vnic->uc_filter_count = 1;
13108 
13109 	netif_addr_lock_bh(dev);
13110 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13111 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13112 	} else {
13113 		netdev_for_each_uc_addr(ha, dev) {
13114 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13115 			off += ETH_ALEN;
13116 			vnic->uc_filter_count++;
13117 		}
13118 	}
13119 	netif_addr_unlock_bh(dev);
13120 
13121 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13122 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13123 		if (rc) {
13124 			if (BNXT_VF(bp) && rc == -ENODEV) {
13125 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13126 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13127 				else
13128 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13129 				rc = 0;
13130 			} else {
13131 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13132 			}
13133 			vnic->uc_filter_count = i;
13134 			return rc;
13135 		}
13136 	}
13137 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13138 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13139 
13140 skip_uc:
13141 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13142 	    !bnxt_promisc_ok(bp))
13143 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13144 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13145 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13146 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13147 			    rc);
13148 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13149 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13150 		vnic->mc_list_count = 0;
13151 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13152 	}
13153 	if (rc)
13154 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13155 			   rc);
13156 
13157 	return rc;
13158 }
13159 
13160 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13161 {
13162 #ifdef CONFIG_BNXT_SRIOV
13163 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13164 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13165 
13166 		/* No minimum rings were provisioned by the PF.  Don't
13167 		 * reserve rings by default when device is down.
13168 		 */
13169 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13170 			return true;
13171 
13172 		if (!netif_running(bp->dev))
13173 			return false;
13174 	}
13175 #endif
13176 	return true;
13177 }
13178 
13179 /* If the chip and firmware supports RFS */
13180 static bool bnxt_rfs_supported(struct bnxt *bp)
13181 {
13182 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13183 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13184 			return true;
13185 		return false;
13186 	}
13187 	/* 212 firmware is broken for aRFS */
13188 	if (BNXT_FW_MAJ(bp) == 212)
13189 		return false;
13190 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13191 		return true;
13192 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13193 		return true;
13194 	return false;
13195 }
13196 
13197 /* If runtime conditions support RFS */
13198 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13199 {
13200 	struct bnxt_hw_rings hwr = {0};
13201 	int max_vnics, max_rss_ctxs;
13202 
13203 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13204 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13205 		return bnxt_rfs_supported(bp);
13206 
13207 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13208 		return false;
13209 
13210 	hwr.grp = bp->rx_nr_rings;
13211 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13212 	if (new_rss_ctx)
13213 		hwr.vnic++;
13214 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13215 	max_vnics = bnxt_get_max_func_vnics(bp);
13216 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13217 
13218 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13219 		if (bp->rx_nr_rings > 1)
13220 			netdev_warn(bp->dev,
13221 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13222 				    min(max_rss_ctxs - 1, max_vnics - 1));
13223 		return false;
13224 	}
13225 
13226 	if (!BNXT_NEW_RM(bp))
13227 		return true;
13228 
13229 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13230 	 * issue that will mess up the default VNIC if we reduce the
13231 	 * reservations.
13232 	 */
13233 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13234 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13235 		return true;
13236 
13237 	bnxt_hwrm_reserve_rings(bp, &hwr);
13238 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13239 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13240 		return true;
13241 
13242 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13243 	hwr.vnic = 1;
13244 	hwr.rss_ctx = 0;
13245 	bnxt_hwrm_reserve_rings(bp, &hwr);
13246 	return false;
13247 }
13248 
13249 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13250 					   netdev_features_t features)
13251 {
13252 	struct bnxt *bp = netdev_priv(dev);
13253 	netdev_features_t vlan_features;
13254 
13255 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13256 		features &= ~NETIF_F_NTUPLE;
13257 
13258 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13259 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13260 
13261 	if (!(features & NETIF_F_GRO))
13262 		features &= ~NETIF_F_GRO_HW;
13263 
13264 	if (features & NETIF_F_GRO_HW)
13265 		features &= ~NETIF_F_LRO;
13266 
13267 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13268 	 * turned on or off together.
13269 	 */
13270 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13271 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13272 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13273 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13274 		else if (vlan_features)
13275 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13276 	}
13277 #ifdef CONFIG_BNXT_SRIOV
13278 	if (BNXT_VF(bp) && bp->vf.vlan)
13279 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13280 #endif
13281 	return features;
13282 }
13283 
13284 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13285 				bool link_re_init, u32 flags, bool update_tpa)
13286 {
13287 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13288 	bp->flags = flags;
13289 	if (update_tpa)
13290 		bnxt_set_ring_params(bp);
13291 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13292 }
13293 
13294 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13295 {
13296 	bool update_tpa = false, update_ntuple = false;
13297 	struct bnxt *bp = netdev_priv(dev);
13298 	u32 flags = bp->flags;
13299 	u32 changes;
13300 	int rc = 0;
13301 	bool re_init = false;
13302 
13303 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13304 	if (features & NETIF_F_GRO_HW)
13305 		flags |= BNXT_FLAG_GRO;
13306 	else if (features & NETIF_F_LRO)
13307 		flags |= BNXT_FLAG_LRO;
13308 
13309 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13310 		flags &= ~BNXT_FLAG_TPA;
13311 
13312 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13313 		flags |= BNXT_FLAG_STRIP_VLAN;
13314 
13315 	if (features & NETIF_F_NTUPLE)
13316 		flags |= BNXT_FLAG_RFS;
13317 	else
13318 		bnxt_clear_usr_fltrs(bp, true);
13319 
13320 	changes = flags ^ bp->flags;
13321 	if (changes & BNXT_FLAG_TPA) {
13322 		update_tpa = true;
13323 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13324 		    (flags & BNXT_FLAG_TPA) == 0 ||
13325 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13326 			re_init = true;
13327 	}
13328 
13329 	if (changes & ~BNXT_FLAG_TPA)
13330 		re_init = true;
13331 
13332 	if (changes & BNXT_FLAG_RFS)
13333 		update_ntuple = true;
13334 
13335 	if (flags != bp->flags) {
13336 		u32 old_flags = bp->flags;
13337 
13338 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13339 			bp->flags = flags;
13340 			if (update_tpa)
13341 				bnxt_set_ring_params(bp);
13342 			return rc;
13343 		}
13344 
13345 		if (update_ntuple)
13346 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13347 
13348 		if (re_init)
13349 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13350 
13351 		if (update_tpa) {
13352 			bp->flags = flags;
13353 			rc = bnxt_set_tpa(bp,
13354 					  (flags & BNXT_FLAG_TPA) ?
13355 					  true : false);
13356 			if (rc)
13357 				bp->flags = old_flags;
13358 		}
13359 	}
13360 	return rc;
13361 }
13362 
13363 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13364 			      u8 **nextp)
13365 {
13366 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13367 	struct hop_jumbo_hdr *jhdr;
13368 	int hdr_count = 0;
13369 	u8 *nexthdr;
13370 	int start;
13371 
13372 	/* Check that there are at most 2 IPv6 extension headers, no
13373 	 * fragment header, and each is <= 64 bytes.
13374 	 */
13375 	start = nw_off + sizeof(*ip6h);
13376 	nexthdr = &ip6h->nexthdr;
13377 	while (ipv6_ext_hdr(*nexthdr)) {
13378 		struct ipv6_opt_hdr *hp;
13379 		int hdrlen;
13380 
13381 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13382 		    *nexthdr == NEXTHDR_FRAGMENT)
13383 			return false;
13384 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13385 					  skb_headlen(skb), NULL);
13386 		if (!hp)
13387 			return false;
13388 		if (*nexthdr == NEXTHDR_AUTH)
13389 			hdrlen = ipv6_authlen(hp);
13390 		else
13391 			hdrlen = ipv6_optlen(hp);
13392 
13393 		if (hdrlen > 64)
13394 			return false;
13395 
13396 		/* The ext header may be a hop-by-hop header inserted for
13397 		 * big TCP purposes. This will be removed before sending
13398 		 * from NIC, so do not count it.
13399 		 */
13400 		if (*nexthdr == NEXTHDR_HOP) {
13401 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13402 				goto increment_hdr;
13403 
13404 			jhdr = (struct hop_jumbo_hdr *)hp;
13405 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13406 			    jhdr->nexthdr != IPPROTO_TCP)
13407 				goto increment_hdr;
13408 
13409 			goto next_hdr;
13410 		}
13411 increment_hdr:
13412 		hdr_count++;
13413 next_hdr:
13414 		nexthdr = &hp->nexthdr;
13415 		start += hdrlen;
13416 	}
13417 	if (nextp) {
13418 		/* Caller will check inner protocol */
13419 		if (skb->encapsulation) {
13420 			*nextp = nexthdr;
13421 			return true;
13422 		}
13423 		*nextp = NULL;
13424 	}
13425 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13426 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13427 }
13428 
13429 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13430 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13431 {
13432 	struct udphdr *uh = udp_hdr(skb);
13433 	__be16 udp_port = uh->dest;
13434 
13435 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13436 	    udp_port != bp->vxlan_gpe_port)
13437 		return false;
13438 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13439 		struct ethhdr *eh = inner_eth_hdr(skb);
13440 
13441 		switch (eh->h_proto) {
13442 		case htons(ETH_P_IP):
13443 			return true;
13444 		case htons(ETH_P_IPV6):
13445 			return bnxt_exthdr_check(bp, skb,
13446 						 skb_inner_network_offset(skb),
13447 						 NULL);
13448 		}
13449 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13450 		return true;
13451 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13452 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13453 					 NULL);
13454 	}
13455 	return false;
13456 }
13457 
13458 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13459 {
13460 	switch (l4_proto) {
13461 	case IPPROTO_UDP:
13462 		return bnxt_udp_tunl_check(bp, skb);
13463 	case IPPROTO_IPIP:
13464 		return true;
13465 	case IPPROTO_GRE: {
13466 		switch (skb->inner_protocol) {
13467 		default:
13468 			return false;
13469 		case htons(ETH_P_IP):
13470 			return true;
13471 		case htons(ETH_P_IPV6):
13472 			fallthrough;
13473 		}
13474 	}
13475 	case IPPROTO_IPV6:
13476 		/* Check ext headers of inner ipv6 */
13477 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13478 					 NULL);
13479 	}
13480 	return false;
13481 }
13482 
13483 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13484 					     struct net_device *dev,
13485 					     netdev_features_t features)
13486 {
13487 	struct bnxt *bp = netdev_priv(dev);
13488 	u8 *l4_proto;
13489 
13490 	features = vlan_features_check(skb, features);
13491 	switch (vlan_get_protocol(skb)) {
13492 	case htons(ETH_P_IP):
13493 		if (!skb->encapsulation)
13494 			return features;
13495 		l4_proto = &ip_hdr(skb)->protocol;
13496 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13497 			return features;
13498 		break;
13499 	case htons(ETH_P_IPV6):
13500 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13501 				       &l4_proto))
13502 			break;
13503 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13504 			return features;
13505 		break;
13506 	}
13507 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13508 }
13509 
13510 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13511 			 u32 *reg_buf)
13512 {
13513 	struct hwrm_dbg_read_direct_output *resp;
13514 	struct hwrm_dbg_read_direct_input *req;
13515 	__le32 *dbg_reg_buf;
13516 	dma_addr_t mapping;
13517 	int rc, i;
13518 
13519 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13520 	if (rc)
13521 		return rc;
13522 
13523 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13524 					 &mapping);
13525 	if (!dbg_reg_buf) {
13526 		rc = -ENOMEM;
13527 		goto dbg_rd_reg_exit;
13528 	}
13529 
13530 	req->host_dest_addr = cpu_to_le64(mapping);
13531 
13532 	resp = hwrm_req_hold(bp, req);
13533 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13534 	req->read_len32 = cpu_to_le32(num_words);
13535 
13536 	rc = hwrm_req_send(bp, req);
13537 	if (rc || resp->error_code) {
13538 		rc = -EIO;
13539 		goto dbg_rd_reg_exit;
13540 	}
13541 	for (i = 0; i < num_words; i++)
13542 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13543 
13544 dbg_rd_reg_exit:
13545 	hwrm_req_drop(bp, req);
13546 	return rc;
13547 }
13548 
13549 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13550 				       u32 ring_id, u32 *prod, u32 *cons)
13551 {
13552 	struct hwrm_dbg_ring_info_get_output *resp;
13553 	struct hwrm_dbg_ring_info_get_input *req;
13554 	int rc;
13555 
13556 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13557 	if (rc)
13558 		return rc;
13559 
13560 	req->ring_type = ring_type;
13561 	req->fw_ring_id = cpu_to_le32(ring_id);
13562 	resp = hwrm_req_hold(bp, req);
13563 	rc = hwrm_req_send(bp, req);
13564 	if (!rc) {
13565 		*prod = le32_to_cpu(resp->producer_index);
13566 		*cons = le32_to_cpu(resp->consumer_index);
13567 	}
13568 	hwrm_req_drop(bp, req);
13569 	return rc;
13570 }
13571 
13572 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13573 {
13574 	struct bnxt_tx_ring_info *txr;
13575 	int i = bnapi->index, j;
13576 
13577 	bnxt_for_each_napi_tx(j, bnapi, txr)
13578 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13579 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13580 			    txr->tx_cons);
13581 }
13582 
13583 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13584 {
13585 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13586 	int i = bnapi->index;
13587 
13588 	if (!rxr)
13589 		return;
13590 
13591 	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",
13592 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13593 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13594 		    rxr->rx_sw_agg_prod);
13595 }
13596 
13597 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13598 {
13599 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13600 	int i = bnapi->index;
13601 
13602 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13603 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13604 }
13605 
13606 static void bnxt_dbg_dump_states(struct bnxt *bp)
13607 {
13608 	int i;
13609 	struct bnxt_napi *bnapi;
13610 
13611 	for (i = 0; i < bp->cp_nr_rings; i++) {
13612 		bnapi = bp->bnapi[i];
13613 		if (netif_msg_drv(bp)) {
13614 			bnxt_dump_tx_sw_state(bnapi);
13615 			bnxt_dump_rx_sw_state(bnapi);
13616 			bnxt_dump_cp_sw_state(bnapi);
13617 		}
13618 	}
13619 }
13620 
13621 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13622 {
13623 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13624 	struct hwrm_ring_reset_input *req;
13625 	struct bnxt_napi *bnapi = rxr->bnapi;
13626 	struct bnxt_cp_ring_info *cpr;
13627 	u16 cp_ring_id;
13628 	int rc;
13629 
13630 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13631 	if (rc)
13632 		return rc;
13633 
13634 	cpr = &bnapi->cp_ring;
13635 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
13636 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
13637 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
13638 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
13639 	return hwrm_req_send_silent(bp, req);
13640 }
13641 
13642 static void bnxt_reset_task(struct bnxt *bp, bool silent)
13643 {
13644 	if (!silent)
13645 		bnxt_dbg_dump_states(bp);
13646 	if (netif_running(bp->dev)) {
13647 		bnxt_close_nic(bp, !silent, false);
13648 		bnxt_open_nic(bp, !silent, false);
13649 	}
13650 }
13651 
13652 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
13653 {
13654 	struct bnxt *bp = netdev_priv(dev);
13655 
13656 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
13657 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
13658 }
13659 
13660 static void bnxt_fw_health_check(struct bnxt *bp)
13661 {
13662 	struct bnxt_fw_health *fw_health = bp->fw_health;
13663 	struct pci_dev *pdev = bp->pdev;
13664 	u32 val;
13665 
13666 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13667 		return;
13668 
13669 	/* Make sure it is enabled before checking the tmr_counter. */
13670 	smp_rmb();
13671 	if (fw_health->tmr_counter) {
13672 		fw_health->tmr_counter--;
13673 		return;
13674 	}
13675 
13676 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13677 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
13678 		fw_health->arrests++;
13679 		goto fw_reset;
13680 	}
13681 
13682 	fw_health->last_fw_heartbeat = val;
13683 
13684 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13685 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
13686 		fw_health->discoveries++;
13687 		goto fw_reset;
13688 	}
13689 
13690 	fw_health->tmr_counter = fw_health->tmr_multiplier;
13691 	return;
13692 
13693 fw_reset:
13694 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
13695 }
13696 
13697 static void bnxt_timer(struct timer_list *t)
13698 {
13699 	struct bnxt *bp = from_timer(bp, t, timer);
13700 	struct net_device *dev = bp->dev;
13701 
13702 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
13703 		return;
13704 
13705 	if (atomic_read(&bp->intr_sem) != 0)
13706 		goto bnxt_restart_timer;
13707 
13708 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
13709 		bnxt_fw_health_check(bp);
13710 
13711 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
13712 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
13713 
13714 	if (bnxt_tc_flower_enabled(bp))
13715 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
13716 
13717 #ifdef CONFIG_RFS_ACCEL
13718 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
13719 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
13720 #endif /*CONFIG_RFS_ACCEL*/
13721 
13722 	if (bp->link_info.phy_retry) {
13723 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
13724 			bp->link_info.phy_retry = false;
13725 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
13726 		} else {
13727 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
13728 		}
13729 	}
13730 
13731 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13732 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13733 
13734 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
13735 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
13736 
13737 bnxt_restart_timer:
13738 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13739 }
13740 
13741 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
13742 {
13743 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
13744 	 * set.  If the device is being closed, bnxt_close() may be holding
13745 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
13746 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
13747 	 */
13748 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13749 	rtnl_lock();
13750 }
13751 
13752 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
13753 {
13754 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13755 	rtnl_unlock();
13756 }
13757 
13758 /* Only called from bnxt_sp_task() */
13759 static void bnxt_reset(struct bnxt *bp, bool silent)
13760 {
13761 	bnxt_rtnl_lock_sp(bp);
13762 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
13763 		bnxt_reset_task(bp, silent);
13764 	bnxt_rtnl_unlock_sp(bp);
13765 }
13766 
13767 /* Only called from bnxt_sp_task() */
13768 static void bnxt_rx_ring_reset(struct bnxt *bp)
13769 {
13770 	int i;
13771 
13772 	bnxt_rtnl_lock_sp(bp);
13773 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13774 		bnxt_rtnl_unlock_sp(bp);
13775 		return;
13776 	}
13777 	/* Disable and flush TPA before resetting the RX ring */
13778 	if (bp->flags & BNXT_FLAG_TPA)
13779 		bnxt_set_tpa(bp, false);
13780 	for (i = 0; i < bp->rx_nr_rings; i++) {
13781 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
13782 		struct bnxt_cp_ring_info *cpr;
13783 		int rc;
13784 
13785 		if (!rxr->bnapi->in_reset)
13786 			continue;
13787 
13788 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
13789 		if (rc) {
13790 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
13791 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
13792 			else
13793 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
13794 					    rc);
13795 			bnxt_reset_task(bp, true);
13796 			break;
13797 		}
13798 		bnxt_free_one_rx_ring_skbs(bp, rxr);
13799 		rxr->rx_prod = 0;
13800 		rxr->rx_agg_prod = 0;
13801 		rxr->rx_sw_agg_prod = 0;
13802 		rxr->rx_next_cons = 0;
13803 		rxr->bnapi->in_reset = false;
13804 		bnxt_alloc_one_rx_ring(bp, i);
13805 		cpr = &rxr->bnapi->cp_ring;
13806 		cpr->sw_stats->rx.rx_resets++;
13807 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
13808 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
13809 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
13810 	}
13811 	if (bp->flags & BNXT_FLAG_TPA)
13812 		bnxt_set_tpa(bp, true);
13813 	bnxt_rtnl_unlock_sp(bp);
13814 }
13815 
13816 static void bnxt_fw_fatal_close(struct bnxt *bp)
13817 {
13818 	bnxt_tx_disable(bp);
13819 	bnxt_disable_napi(bp);
13820 	bnxt_disable_int_sync(bp);
13821 	bnxt_free_irq(bp);
13822 	bnxt_clear_int_mode(bp);
13823 	pci_disable_device(bp->pdev);
13824 }
13825 
13826 static void bnxt_fw_reset_close(struct bnxt *bp)
13827 {
13828 	/* When firmware is in fatal state, quiesce device and disable
13829 	 * bus master to prevent any potential bad DMAs before freeing
13830 	 * kernel memory.
13831 	 */
13832 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
13833 		u16 val = 0;
13834 
13835 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
13836 		if (val == 0xffff)
13837 			bp->fw_reset_min_dsecs = 0;
13838 		bnxt_fw_fatal_close(bp);
13839 	}
13840 	__bnxt_close_nic(bp, true, false);
13841 	bnxt_vf_reps_free(bp);
13842 	bnxt_clear_int_mode(bp);
13843 	bnxt_hwrm_func_drv_unrgtr(bp);
13844 	if (pci_is_enabled(bp->pdev))
13845 		pci_disable_device(bp->pdev);
13846 	bnxt_free_ctx_mem(bp, false);
13847 }
13848 
13849 static bool is_bnxt_fw_ok(struct bnxt *bp)
13850 {
13851 	struct bnxt_fw_health *fw_health = bp->fw_health;
13852 	bool no_heartbeat = false, has_reset = false;
13853 	u32 val;
13854 
13855 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13856 	if (val == fw_health->last_fw_heartbeat)
13857 		no_heartbeat = true;
13858 
13859 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13860 	if (val != fw_health->last_fw_reset_cnt)
13861 		has_reset = true;
13862 
13863 	if (!no_heartbeat && has_reset)
13864 		return true;
13865 
13866 	return false;
13867 }
13868 
13869 /* rtnl_lock is acquired before calling this function */
13870 static void bnxt_force_fw_reset(struct bnxt *bp)
13871 {
13872 	struct bnxt_fw_health *fw_health = bp->fw_health;
13873 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13874 	u32 wait_dsecs;
13875 
13876 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
13877 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13878 		return;
13879 
13880 	/* we have to serialize with bnxt_refclk_read()*/
13881 	if (ptp) {
13882 		unsigned long flags;
13883 
13884 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
13885 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13886 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
13887 	} else {
13888 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13889 	}
13890 	bnxt_fw_reset_close(bp);
13891 	wait_dsecs = fw_health->master_func_wait_dsecs;
13892 	if (fw_health->primary) {
13893 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
13894 			wait_dsecs = 0;
13895 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
13896 	} else {
13897 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
13898 		wait_dsecs = fw_health->normal_func_wait_dsecs;
13899 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13900 	}
13901 
13902 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
13903 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
13904 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
13905 }
13906 
13907 void bnxt_fw_exception(struct bnxt *bp)
13908 {
13909 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
13910 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
13911 	bnxt_ulp_stop(bp);
13912 	bnxt_rtnl_lock_sp(bp);
13913 	bnxt_force_fw_reset(bp);
13914 	bnxt_rtnl_unlock_sp(bp);
13915 }
13916 
13917 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
13918  * < 0 on error.
13919  */
13920 static int bnxt_get_registered_vfs(struct bnxt *bp)
13921 {
13922 #ifdef CONFIG_BNXT_SRIOV
13923 	int rc;
13924 
13925 	if (!BNXT_PF(bp))
13926 		return 0;
13927 
13928 	rc = bnxt_hwrm_func_qcfg(bp);
13929 	if (rc) {
13930 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
13931 		return rc;
13932 	}
13933 	if (bp->pf.registered_vfs)
13934 		return bp->pf.registered_vfs;
13935 	if (bp->sriov_cfg)
13936 		return 1;
13937 #endif
13938 	return 0;
13939 }
13940 
13941 void bnxt_fw_reset(struct bnxt *bp)
13942 {
13943 	bnxt_ulp_stop(bp);
13944 	bnxt_rtnl_lock_sp(bp);
13945 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
13946 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13947 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13948 		int n = 0, tmo;
13949 
13950 		/* we have to serialize with bnxt_refclk_read()*/
13951 		if (ptp) {
13952 			unsigned long flags;
13953 
13954 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
13955 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13956 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
13957 		} else {
13958 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13959 		}
13960 		if (bp->pf.active_vfs &&
13961 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
13962 			n = bnxt_get_registered_vfs(bp);
13963 		if (n < 0) {
13964 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
13965 				   n);
13966 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13967 			dev_close(bp->dev);
13968 			goto fw_reset_exit;
13969 		} else if (n > 0) {
13970 			u16 vf_tmo_dsecs = n * 10;
13971 
13972 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
13973 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
13974 			bp->fw_reset_state =
13975 				BNXT_FW_RESET_STATE_POLL_VF;
13976 			bnxt_queue_fw_reset_work(bp, HZ / 10);
13977 			goto fw_reset_exit;
13978 		}
13979 		bnxt_fw_reset_close(bp);
13980 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13981 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
13982 			tmo = HZ / 10;
13983 		} else {
13984 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13985 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
13986 		}
13987 		bnxt_queue_fw_reset_work(bp, tmo);
13988 	}
13989 fw_reset_exit:
13990 	bnxt_rtnl_unlock_sp(bp);
13991 }
13992 
13993 static void bnxt_chk_missed_irq(struct bnxt *bp)
13994 {
13995 	int i;
13996 
13997 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13998 		return;
13999 
14000 	for (i = 0; i < bp->cp_nr_rings; i++) {
14001 		struct bnxt_napi *bnapi = bp->bnapi[i];
14002 		struct bnxt_cp_ring_info *cpr;
14003 		u32 fw_ring_id;
14004 		int j;
14005 
14006 		if (!bnapi)
14007 			continue;
14008 
14009 		cpr = &bnapi->cp_ring;
14010 		for (j = 0; j < cpr->cp_ring_count; j++) {
14011 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14012 			u32 val[2];
14013 
14014 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14015 				continue;
14016 
14017 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14018 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14019 				continue;
14020 			}
14021 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14022 			bnxt_dbg_hwrm_ring_info_get(bp,
14023 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14024 				fw_ring_id, &val[0], &val[1]);
14025 			cpr->sw_stats->cmn.missed_irqs++;
14026 		}
14027 	}
14028 }
14029 
14030 static void bnxt_cfg_ntp_filters(struct bnxt *);
14031 
14032 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14033 {
14034 	struct bnxt_link_info *link_info = &bp->link_info;
14035 
14036 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14037 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14038 		if (bp->hwrm_spec_code >= 0x10201) {
14039 			if (link_info->auto_pause_setting &
14040 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14041 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14042 		} else {
14043 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14044 		}
14045 		bnxt_set_auto_speed(link_info);
14046 	} else {
14047 		bnxt_set_force_speed(link_info);
14048 		link_info->req_duplex = link_info->duplex_setting;
14049 	}
14050 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14051 		link_info->req_flow_ctrl =
14052 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14053 	else
14054 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14055 }
14056 
14057 static void bnxt_fw_echo_reply(struct bnxt *bp)
14058 {
14059 	struct bnxt_fw_health *fw_health = bp->fw_health;
14060 	struct hwrm_func_echo_response_input *req;
14061 	int rc;
14062 
14063 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14064 	if (rc)
14065 		return;
14066 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14067 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14068 	hwrm_req_send(bp, req);
14069 }
14070 
14071 static void bnxt_ulp_restart(struct bnxt *bp)
14072 {
14073 	bnxt_ulp_stop(bp);
14074 	bnxt_ulp_start(bp, 0);
14075 }
14076 
14077 static void bnxt_sp_task(struct work_struct *work)
14078 {
14079 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14080 
14081 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14082 	smp_mb__after_atomic();
14083 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14084 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14085 		return;
14086 	}
14087 
14088 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14089 		bnxt_ulp_restart(bp);
14090 		bnxt_reenable_sriov(bp);
14091 	}
14092 
14093 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14094 		bnxt_cfg_rx_mode(bp);
14095 
14096 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14097 		bnxt_cfg_ntp_filters(bp);
14098 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14099 		bnxt_hwrm_exec_fwd_req(bp);
14100 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14101 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14102 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14103 		bnxt_hwrm_port_qstats(bp, 0);
14104 		bnxt_hwrm_port_qstats_ext(bp, 0);
14105 		bnxt_accumulate_all_stats(bp);
14106 	}
14107 
14108 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14109 		int rc;
14110 
14111 		mutex_lock(&bp->link_lock);
14112 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14113 				       &bp->sp_event))
14114 			bnxt_hwrm_phy_qcaps(bp);
14115 
14116 		rc = bnxt_update_link(bp, true);
14117 		if (rc)
14118 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14119 				   rc);
14120 
14121 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14122 				       &bp->sp_event))
14123 			bnxt_init_ethtool_link_settings(bp);
14124 		mutex_unlock(&bp->link_lock);
14125 	}
14126 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14127 		int rc;
14128 
14129 		mutex_lock(&bp->link_lock);
14130 		rc = bnxt_update_phy_setting(bp);
14131 		mutex_unlock(&bp->link_lock);
14132 		if (rc) {
14133 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14134 		} else {
14135 			bp->link_info.phy_retry = false;
14136 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14137 		}
14138 	}
14139 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14140 		mutex_lock(&bp->link_lock);
14141 		bnxt_get_port_module_status(bp);
14142 		mutex_unlock(&bp->link_lock);
14143 	}
14144 
14145 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14146 		bnxt_tc_flow_stats_work(bp);
14147 
14148 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14149 		bnxt_chk_missed_irq(bp);
14150 
14151 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14152 		bnxt_fw_echo_reply(bp);
14153 
14154 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14155 		bnxt_hwmon_notify_event(bp);
14156 
14157 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14158 	 * must be the last functions to be called before exiting.
14159 	 */
14160 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14161 		bnxt_reset(bp, false);
14162 
14163 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14164 		bnxt_reset(bp, true);
14165 
14166 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14167 		bnxt_rx_ring_reset(bp);
14168 
14169 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14170 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14171 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14172 			bnxt_devlink_health_fw_report(bp);
14173 		else
14174 			bnxt_fw_reset(bp);
14175 	}
14176 
14177 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14178 		if (!is_bnxt_fw_ok(bp))
14179 			bnxt_devlink_health_fw_report(bp);
14180 	}
14181 
14182 	smp_mb__before_atomic();
14183 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14184 }
14185 
14186 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14187 				int *max_cp);
14188 
14189 /* Under rtnl_lock */
14190 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14191 		     int tx_xdp)
14192 {
14193 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14194 	struct bnxt_hw_rings hwr = {0};
14195 	int rx_rings = rx;
14196 	int rc;
14197 
14198 	if (tcs)
14199 		tx_sets = tcs;
14200 
14201 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14202 
14203 	if (max_rx < rx_rings)
14204 		return -ENOMEM;
14205 
14206 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14207 		rx_rings <<= 1;
14208 
14209 	hwr.rx = rx_rings;
14210 	hwr.tx = tx * tx_sets + tx_xdp;
14211 	if (max_tx < hwr.tx)
14212 		return -ENOMEM;
14213 
14214 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14215 
14216 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14217 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14218 	if (max_cp < hwr.cp)
14219 		return -ENOMEM;
14220 	hwr.stat = hwr.cp;
14221 	if (BNXT_NEW_RM(bp)) {
14222 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14223 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14224 		hwr.grp = rx;
14225 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14226 	}
14227 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14228 		hwr.cp_p5 = hwr.tx + rx;
14229 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14230 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14231 		if (!bnxt_ulp_registered(bp->edev)) {
14232 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14233 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14234 		}
14235 		if (hwr.cp > bp->total_irqs) {
14236 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14237 
14238 			if (total_msix < hwr.cp) {
14239 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14240 					    hwr.cp, total_msix);
14241 				rc = -ENOSPC;
14242 			}
14243 		}
14244 	}
14245 	return rc;
14246 }
14247 
14248 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14249 {
14250 	if (bp->bar2) {
14251 		pci_iounmap(pdev, bp->bar2);
14252 		bp->bar2 = NULL;
14253 	}
14254 
14255 	if (bp->bar1) {
14256 		pci_iounmap(pdev, bp->bar1);
14257 		bp->bar1 = NULL;
14258 	}
14259 
14260 	if (bp->bar0) {
14261 		pci_iounmap(pdev, bp->bar0);
14262 		bp->bar0 = NULL;
14263 	}
14264 }
14265 
14266 static void bnxt_cleanup_pci(struct bnxt *bp)
14267 {
14268 	bnxt_unmap_bars(bp, bp->pdev);
14269 	pci_release_regions(bp->pdev);
14270 	if (pci_is_enabled(bp->pdev))
14271 		pci_disable_device(bp->pdev);
14272 }
14273 
14274 static void bnxt_init_dflt_coal(struct bnxt *bp)
14275 {
14276 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14277 	struct bnxt_coal *coal;
14278 	u16 flags = 0;
14279 
14280 	if (coal_cap->cmpl_params &
14281 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14282 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14283 
14284 	/* Tick values in micro seconds.
14285 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14286 	 */
14287 	coal = &bp->rx_coal;
14288 	coal->coal_ticks = 10;
14289 	coal->coal_bufs = 30;
14290 	coal->coal_ticks_irq = 1;
14291 	coal->coal_bufs_irq = 2;
14292 	coal->idle_thresh = 50;
14293 	coal->bufs_per_record = 2;
14294 	coal->budget = 64;		/* NAPI budget */
14295 	coal->flags = flags;
14296 
14297 	coal = &bp->tx_coal;
14298 	coal->coal_ticks = 28;
14299 	coal->coal_bufs = 30;
14300 	coal->coal_ticks_irq = 2;
14301 	coal->coal_bufs_irq = 2;
14302 	coal->bufs_per_record = 1;
14303 	coal->flags = flags;
14304 
14305 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14306 }
14307 
14308 /* FW that pre-reserves 1 VNIC per function */
14309 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14310 {
14311 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14312 
14313 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14314 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14315 		return true;
14316 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14317 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14318 		return true;
14319 	return false;
14320 }
14321 
14322 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14323 {
14324 	int rc;
14325 
14326 	bp->fw_cap = 0;
14327 	rc = bnxt_hwrm_ver_get(bp);
14328 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14329 	 * so wait before continuing with recovery.
14330 	 */
14331 	if (rc)
14332 		msleep(100);
14333 	bnxt_try_map_fw_health_reg(bp);
14334 	if (rc) {
14335 		rc = bnxt_try_recover_fw(bp);
14336 		if (rc)
14337 			return rc;
14338 		rc = bnxt_hwrm_ver_get(bp);
14339 		if (rc)
14340 			return rc;
14341 	}
14342 
14343 	bnxt_nvm_cfg_ver_get(bp);
14344 
14345 	rc = bnxt_hwrm_func_reset(bp);
14346 	if (rc)
14347 		return -ENODEV;
14348 
14349 	bnxt_hwrm_fw_set_time(bp);
14350 	return 0;
14351 }
14352 
14353 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14354 {
14355 	int rc;
14356 
14357 	/* Get the MAX capabilities for this function */
14358 	rc = bnxt_hwrm_func_qcaps(bp);
14359 	if (rc) {
14360 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14361 			   rc);
14362 		return -ENODEV;
14363 	}
14364 
14365 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14366 	if (rc)
14367 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14368 			    rc);
14369 
14370 	if (bnxt_alloc_fw_health(bp)) {
14371 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14372 	} else {
14373 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14374 		if (rc)
14375 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14376 				    rc);
14377 	}
14378 
14379 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14380 	if (rc)
14381 		return -ENODEV;
14382 
14383 	rc = bnxt_alloc_crash_dump_mem(bp);
14384 	if (rc)
14385 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14386 			    rc);
14387 	if (!rc) {
14388 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14389 		if (rc) {
14390 			bnxt_free_crash_dump_mem(bp);
14391 			netdev_warn(bp->dev,
14392 				    "hwrm crash dump mem failure rc: %d\n", rc);
14393 		}
14394 	}
14395 
14396 	if (bnxt_fw_pre_resv_vnics(bp))
14397 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14398 
14399 	bnxt_hwrm_func_qcfg(bp);
14400 	bnxt_hwrm_vnic_qcaps(bp);
14401 	bnxt_hwrm_port_led_qcaps(bp);
14402 	bnxt_ethtool_init(bp);
14403 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14404 		__bnxt_hwrm_ptp_qcfg(bp);
14405 	bnxt_dcb_init(bp);
14406 	bnxt_hwmon_init(bp);
14407 	return 0;
14408 }
14409 
14410 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14411 {
14412 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14413 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14414 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14415 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14416 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14417 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14418 		bp->rss_hash_delta = bp->rss_hash_cfg;
14419 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14420 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14421 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14422 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14423 	}
14424 }
14425 
14426 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14427 {
14428 	struct net_device *dev = bp->dev;
14429 
14430 	dev->hw_features &= ~NETIF_F_NTUPLE;
14431 	dev->features &= ~NETIF_F_NTUPLE;
14432 	bp->flags &= ~BNXT_FLAG_RFS;
14433 	if (bnxt_rfs_supported(bp)) {
14434 		dev->hw_features |= NETIF_F_NTUPLE;
14435 		if (bnxt_rfs_capable(bp, false)) {
14436 			bp->flags |= BNXT_FLAG_RFS;
14437 			dev->features |= NETIF_F_NTUPLE;
14438 		}
14439 	}
14440 }
14441 
14442 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14443 {
14444 	struct pci_dev *pdev = bp->pdev;
14445 
14446 	bnxt_set_dflt_rss_hash_type(bp);
14447 	bnxt_set_dflt_rfs(bp);
14448 
14449 	bnxt_get_wol_settings(bp);
14450 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14451 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14452 	else
14453 		device_set_wakeup_capable(&pdev->dev, false);
14454 
14455 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14456 	bnxt_hwrm_coal_params_qcaps(bp);
14457 }
14458 
14459 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14460 
14461 int bnxt_fw_init_one(struct bnxt *bp)
14462 {
14463 	int rc;
14464 
14465 	rc = bnxt_fw_init_one_p1(bp);
14466 	if (rc) {
14467 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14468 		return rc;
14469 	}
14470 	rc = bnxt_fw_init_one_p2(bp);
14471 	if (rc) {
14472 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14473 		return rc;
14474 	}
14475 	rc = bnxt_probe_phy(bp, false);
14476 	if (rc)
14477 		return rc;
14478 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14479 	if (rc)
14480 		return rc;
14481 
14482 	bnxt_fw_init_one_p3(bp);
14483 	return 0;
14484 }
14485 
14486 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14487 {
14488 	struct bnxt_fw_health *fw_health = bp->fw_health;
14489 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14490 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14491 	u32 reg_type, reg_off, delay_msecs;
14492 
14493 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14494 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14495 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14496 	switch (reg_type) {
14497 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14498 		pci_write_config_dword(bp->pdev, reg_off, val);
14499 		break;
14500 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14501 		writel(reg_off & BNXT_GRC_BASE_MASK,
14502 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14503 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14504 		fallthrough;
14505 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14506 		writel(val, bp->bar0 + reg_off);
14507 		break;
14508 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14509 		writel(val, bp->bar1 + reg_off);
14510 		break;
14511 	}
14512 	if (delay_msecs) {
14513 		pci_read_config_dword(bp->pdev, 0, &val);
14514 		msleep(delay_msecs);
14515 	}
14516 }
14517 
14518 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14519 {
14520 	struct hwrm_func_qcfg_output *resp;
14521 	struct hwrm_func_qcfg_input *req;
14522 	bool result = true; /* firmware will enforce if unknown */
14523 
14524 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14525 		return result;
14526 
14527 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14528 		return result;
14529 
14530 	req->fid = cpu_to_le16(0xffff);
14531 	resp = hwrm_req_hold(bp, req);
14532 	if (!hwrm_req_send(bp, req))
14533 		result = !!(le16_to_cpu(resp->flags) &
14534 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14535 	hwrm_req_drop(bp, req);
14536 	return result;
14537 }
14538 
14539 static void bnxt_reset_all(struct bnxt *bp)
14540 {
14541 	struct bnxt_fw_health *fw_health = bp->fw_health;
14542 	int i, rc;
14543 
14544 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14545 		bnxt_fw_reset_via_optee(bp);
14546 		bp->fw_reset_timestamp = jiffies;
14547 		return;
14548 	}
14549 
14550 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14551 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14552 			bnxt_fw_reset_writel(bp, i);
14553 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14554 		struct hwrm_fw_reset_input *req;
14555 
14556 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14557 		if (!rc) {
14558 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14559 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14560 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14561 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14562 			rc = hwrm_req_send(bp, req);
14563 		}
14564 		if (rc != -ENODEV)
14565 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14566 	}
14567 	bp->fw_reset_timestamp = jiffies;
14568 }
14569 
14570 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14571 {
14572 	return time_after(jiffies, bp->fw_reset_timestamp +
14573 			  (bp->fw_reset_max_dsecs * HZ / 10));
14574 }
14575 
14576 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14577 {
14578 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14579 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14580 		bnxt_dl_health_fw_status_update(bp, false);
14581 	bp->fw_reset_state = 0;
14582 	dev_close(bp->dev);
14583 }
14584 
14585 static void bnxt_fw_reset_task(struct work_struct *work)
14586 {
14587 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14588 	int rc = 0;
14589 
14590 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14591 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14592 		return;
14593 	}
14594 
14595 	switch (bp->fw_reset_state) {
14596 	case BNXT_FW_RESET_STATE_POLL_VF: {
14597 		int n = bnxt_get_registered_vfs(bp);
14598 		int tmo;
14599 
14600 		if (n < 0) {
14601 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14602 				   n, jiffies_to_msecs(jiffies -
14603 				   bp->fw_reset_timestamp));
14604 			goto fw_reset_abort;
14605 		} else if (n > 0) {
14606 			if (bnxt_fw_reset_timeout(bp)) {
14607 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14608 				bp->fw_reset_state = 0;
14609 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14610 					   n);
14611 				goto ulp_start;
14612 			}
14613 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14614 			return;
14615 		}
14616 		bp->fw_reset_timestamp = jiffies;
14617 		rtnl_lock();
14618 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14619 			bnxt_fw_reset_abort(bp, rc);
14620 			rtnl_unlock();
14621 			goto ulp_start;
14622 		}
14623 		bnxt_fw_reset_close(bp);
14624 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14625 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14626 			tmo = HZ / 10;
14627 		} else {
14628 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14629 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14630 		}
14631 		rtnl_unlock();
14632 		bnxt_queue_fw_reset_work(bp, tmo);
14633 		return;
14634 	}
14635 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
14636 		u32 val;
14637 
14638 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14639 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
14640 		    !bnxt_fw_reset_timeout(bp)) {
14641 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14642 			return;
14643 		}
14644 
14645 		if (!bp->fw_health->primary) {
14646 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
14647 
14648 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14649 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14650 			return;
14651 		}
14652 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14653 	}
14654 		fallthrough;
14655 	case BNXT_FW_RESET_STATE_RESET_FW:
14656 		bnxt_reset_all(bp);
14657 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14658 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
14659 		return;
14660 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
14661 		bnxt_inv_fw_health_reg(bp);
14662 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
14663 		    !bp->fw_reset_min_dsecs) {
14664 			u16 val;
14665 
14666 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14667 			if (val == 0xffff) {
14668 				if (bnxt_fw_reset_timeout(bp)) {
14669 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
14670 					rc = -ETIMEDOUT;
14671 					goto fw_reset_abort;
14672 				}
14673 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
14674 				return;
14675 			}
14676 		}
14677 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14678 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
14679 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
14680 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
14681 			bnxt_dl_remote_reload(bp);
14682 		if (pci_enable_device(bp->pdev)) {
14683 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
14684 			rc = -ENODEV;
14685 			goto fw_reset_abort;
14686 		}
14687 		pci_set_master(bp->pdev);
14688 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
14689 		fallthrough;
14690 	case BNXT_FW_RESET_STATE_POLL_FW:
14691 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
14692 		rc = bnxt_hwrm_poll(bp);
14693 		if (rc) {
14694 			if (bnxt_fw_reset_timeout(bp)) {
14695 				netdev_err(bp->dev, "Firmware reset aborted\n");
14696 				goto fw_reset_abort_status;
14697 			}
14698 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14699 			return;
14700 		}
14701 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
14702 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
14703 		fallthrough;
14704 	case BNXT_FW_RESET_STATE_OPENING:
14705 		while (!rtnl_trylock()) {
14706 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14707 			return;
14708 		}
14709 		rc = bnxt_open(bp->dev);
14710 		if (rc) {
14711 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
14712 			bnxt_fw_reset_abort(bp, rc);
14713 			rtnl_unlock();
14714 			goto ulp_start;
14715 		}
14716 
14717 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
14718 		    bp->fw_health->enabled) {
14719 			bp->fw_health->last_fw_reset_cnt =
14720 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14721 		}
14722 		bp->fw_reset_state = 0;
14723 		/* Make sure fw_reset_state is 0 before clearing the flag */
14724 		smp_mb__before_atomic();
14725 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14726 		bnxt_ptp_reapply_pps(bp);
14727 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
14728 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
14729 			bnxt_dl_health_fw_recovery_done(bp);
14730 			bnxt_dl_health_fw_status_update(bp, true);
14731 		}
14732 		rtnl_unlock();
14733 		bnxt_ulp_start(bp, 0);
14734 		bnxt_reenable_sriov(bp);
14735 		rtnl_lock();
14736 		bnxt_vf_reps_alloc(bp);
14737 		bnxt_vf_reps_open(bp);
14738 		rtnl_unlock();
14739 		break;
14740 	}
14741 	return;
14742 
14743 fw_reset_abort_status:
14744 	if (bp->fw_health->status_reliable ||
14745 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
14746 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14747 
14748 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
14749 	}
14750 fw_reset_abort:
14751 	rtnl_lock();
14752 	bnxt_fw_reset_abort(bp, rc);
14753 	rtnl_unlock();
14754 ulp_start:
14755 	bnxt_ulp_start(bp, rc);
14756 }
14757 
14758 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
14759 {
14760 	int rc;
14761 	struct bnxt *bp = netdev_priv(dev);
14762 
14763 	SET_NETDEV_DEV(dev, &pdev->dev);
14764 
14765 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
14766 	rc = pci_enable_device(pdev);
14767 	if (rc) {
14768 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
14769 		goto init_err;
14770 	}
14771 
14772 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
14773 		dev_err(&pdev->dev,
14774 			"Cannot find PCI device base address, aborting\n");
14775 		rc = -ENODEV;
14776 		goto init_err_disable;
14777 	}
14778 
14779 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
14780 	if (rc) {
14781 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
14782 		goto init_err_disable;
14783 	}
14784 
14785 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
14786 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
14787 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
14788 		rc = -EIO;
14789 		goto init_err_release;
14790 	}
14791 
14792 	pci_set_master(pdev);
14793 
14794 	bp->dev = dev;
14795 	bp->pdev = pdev;
14796 
14797 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
14798 	 * determines the BAR size.
14799 	 */
14800 	bp->bar0 = pci_ioremap_bar(pdev, 0);
14801 	if (!bp->bar0) {
14802 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
14803 		rc = -ENOMEM;
14804 		goto init_err_release;
14805 	}
14806 
14807 	bp->bar2 = pci_ioremap_bar(pdev, 4);
14808 	if (!bp->bar2) {
14809 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
14810 		rc = -ENOMEM;
14811 		goto init_err_release;
14812 	}
14813 
14814 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
14815 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
14816 
14817 	spin_lock_init(&bp->ntp_fltr_lock);
14818 #if BITS_PER_LONG == 32
14819 	spin_lock_init(&bp->db_lock);
14820 #endif
14821 
14822 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
14823 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
14824 
14825 	timer_setup(&bp->timer, bnxt_timer, 0);
14826 	bp->current_interval = BNXT_TIMER_INTERVAL;
14827 
14828 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
14829 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
14830 
14831 	clear_bit(BNXT_STATE_OPEN, &bp->state);
14832 	return 0;
14833 
14834 init_err_release:
14835 	bnxt_unmap_bars(bp, pdev);
14836 	pci_release_regions(pdev);
14837 
14838 init_err_disable:
14839 	pci_disable_device(pdev);
14840 
14841 init_err:
14842 	return rc;
14843 }
14844 
14845 /* rtnl_lock held */
14846 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
14847 {
14848 	struct sockaddr *addr = p;
14849 	struct bnxt *bp = netdev_priv(dev);
14850 	int rc = 0;
14851 
14852 	if (!is_valid_ether_addr(addr->sa_data))
14853 		return -EADDRNOTAVAIL;
14854 
14855 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
14856 		return 0;
14857 
14858 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
14859 	if (rc)
14860 		return rc;
14861 
14862 	eth_hw_addr_set(dev, addr->sa_data);
14863 	bnxt_clear_usr_fltrs(bp, true);
14864 	if (netif_running(dev)) {
14865 		bnxt_close_nic(bp, false, false);
14866 		rc = bnxt_open_nic(bp, false, false);
14867 	}
14868 
14869 	return rc;
14870 }
14871 
14872 /* rtnl_lock held */
14873 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
14874 {
14875 	struct bnxt *bp = netdev_priv(dev);
14876 
14877 	if (netif_running(dev))
14878 		bnxt_close_nic(bp, true, false);
14879 
14880 	WRITE_ONCE(dev->mtu, new_mtu);
14881 
14882 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
14883 	 * program is attached.  We need to set the AGG rings settings and
14884 	 * rx_skb_func accordingly.
14885 	 */
14886 	if (READ_ONCE(bp->xdp_prog))
14887 		bnxt_set_rx_skb_mode(bp, true);
14888 
14889 	bnxt_set_ring_params(bp);
14890 
14891 	if (netif_running(dev))
14892 		return bnxt_open_nic(bp, true, false);
14893 
14894 	return 0;
14895 }
14896 
14897 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
14898 {
14899 	struct bnxt *bp = netdev_priv(dev);
14900 	bool sh = false;
14901 	int rc, tx_cp;
14902 
14903 	if (tc > bp->max_tc) {
14904 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
14905 			   tc, bp->max_tc);
14906 		return -EINVAL;
14907 	}
14908 
14909 	if (bp->num_tc == tc)
14910 		return 0;
14911 
14912 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
14913 		sh = true;
14914 
14915 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
14916 			      sh, tc, bp->tx_nr_rings_xdp);
14917 	if (rc)
14918 		return rc;
14919 
14920 	/* Needs to close the device and do hw resource re-allocations */
14921 	if (netif_running(bp->dev))
14922 		bnxt_close_nic(bp, true, false);
14923 
14924 	if (tc) {
14925 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
14926 		netdev_set_num_tc(dev, tc);
14927 		bp->num_tc = tc;
14928 	} else {
14929 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
14930 		netdev_reset_tc(dev);
14931 		bp->num_tc = 0;
14932 	}
14933 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
14934 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
14935 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
14936 			       tx_cp + bp->rx_nr_rings;
14937 
14938 	if (netif_running(bp->dev))
14939 		return bnxt_open_nic(bp, true, false);
14940 
14941 	return 0;
14942 }
14943 
14944 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
14945 				  void *cb_priv)
14946 {
14947 	struct bnxt *bp = cb_priv;
14948 
14949 	if (!bnxt_tc_flower_enabled(bp) ||
14950 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
14951 		return -EOPNOTSUPP;
14952 
14953 	switch (type) {
14954 	case TC_SETUP_CLSFLOWER:
14955 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
14956 	default:
14957 		return -EOPNOTSUPP;
14958 	}
14959 }
14960 
14961 LIST_HEAD(bnxt_block_cb_list);
14962 
14963 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
14964 			 void *type_data)
14965 {
14966 	struct bnxt *bp = netdev_priv(dev);
14967 
14968 	switch (type) {
14969 	case TC_SETUP_BLOCK:
14970 		return flow_block_cb_setup_simple(type_data,
14971 						  &bnxt_block_cb_list,
14972 						  bnxt_setup_tc_block_cb,
14973 						  bp, bp, true);
14974 	case TC_SETUP_QDISC_MQPRIO: {
14975 		struct tc_mqprio_qopt *mqprio = type_data;
14976 
14977 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
14978 
14979 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
14980 	}
14981 	default:
14982 		return -EOPNOTSUPP;
14983 	}
14984 }
14985 
14986 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
14987 			    const struct sk_buff *skb)
14988 {
14989 	struct bnxt_vnic_info *vnic;
14990 
14991 	if (skb)
14992 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
14993 
14994 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
14995 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
14996 }
14997 
14998 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
14999 			   u32 idx)
15000 {
15001 	struct hlist_head *head;
15002 	int bit_id;
15003 
15004 	spin_lock_bh(&bp->ntp_fltr_lock);
15005 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15006 	if (bit_id < 0) {
15007 		spin_unlock_bh(&bp->ntp_fltr_lock);
15008 		return -ENOMEM;
15009 	}
15010 
15011 	fltr->base.sw_id = (u16)bit_id;
15012 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15013 	fltr->base.flags |= BNXT_ACT_RING_DST;
15014 	head = &bp->ntp_fltr_hash_tbl[idx];
15015 	hlist_add_head_rcu(&fltr->base.hash, head);
15016 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15017 	bnxt_insert_usr_fltr(bp, &fltr->base);
15018 	bp->ntp_fltr_count++;
15019 	spin_unlock_bh(&bp->ntp_fltr_lock);
15020 	return 0;
15021 }
15022 
15023 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15024 			    struct bnxt_ntuple_filter *f2)
15025 {
15026 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15027 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15028 	struct flow_keys *keys1 = &f1->fkeys;
15029 	struct flow_keys *keys2 = &f2->fkeys;
15030 
15031 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15032 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15033 		return false;
15034 
15035 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15036 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15037 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15038 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15039 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15040 			return false;
15041 	} else {
15042 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15043 				     &keys2->addrs.v6addrs.src) ||
15044 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15045 				     &masks2->addrs.v6addrs.src) ||
15046 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15047 				     &keys2->addrs.v6addrs.dst) ||
15048 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15049 				     &masks2->addrs.v6addrs.dst))
15050 			return false;
15051 	}
15052 
15053 	return keys1->ports.src == keys2->ports.src &&
15054 	       masks1->ports.src == masks2->ports.src &&
15055 	       keys1->ports.dst == keys2->ports.dst &&
15056 	       masks1->ports.dst == masks2->ports.dst &&
15057 	       keys1->control.flags == keys2->control.flags &&
15058 	       f1->l2_fltr == f2->l2_fltr;
15059 }
15060 
15061 struct bnxt_ntuple_filter *
15062 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15063 				struct bnxt_ntuple_filter *fltr, u32 idx)
15064 {
15065 	struct bnxt_ntuple_filter *f;
15066 	struct hlist_head *head;
15067 
15068 	head = &bp->ntp_fltr_hash_tbl[idx];
15069 	hlist_for_each_entry_rcu(f, head, base.hash) {
15070 		if (bnxt_fltr_match(f, fltr))
15071 			return f;
15072 	}
15073 	return NULL;
15074 }
15075 
15076 #ifdef CONFIG_RFS_ACCEL
15077 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15078 			      u16 rxq_index, u32 flow_id)
15079 {
15080 	struct bnxt *bp = netdev_priv(dev);
15081 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15082 	struct flow_keys *fkeys;
15083 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15084 	struct bnxt_l2_filter *l2_fltr;
15085 	int rc = 0, idx;
15086 	u32 flags;
15087 
15088 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15089 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15090 		atomic_inc(&l2_fltr->refcnt);
15091 	} else {
15092 		struct bnxt_l2_key key;
15093 
15094 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15095 		key.vlan = 0;
15096 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15097 		if (!l2_fltr)
15098 			return -EINVAL;
15099 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15100 			bnxt_del_l2_filter(bp, l2_fltr);
15101 			return -EINVAL;
15102 		}
15103 	}
15104 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
15105 	if (!new_fltr) {
15106 		bnxt_del_l2_filter(bp, l2_fltr);
15107 		return -ENOMEM;
15108 	}
15109 
15110 	fkeys = &new_fltr->fkeys;
15111 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15112 		rc = -EPROTONOSUPPORT;
15113 		goto err_free;
15114 	}
15115 
15116 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15117 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15118 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15119 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15120 		rc = -EPROTONOSUPPORT;
15121 		goto err_free;
15122 	}
15123 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15124 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15125 		if (bp->hwrm_spec_code < 0x10601) {
15126 			rc = -EPROTONOSUPPORT;
15127 			goto err_free;
15128 		}
15129 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15130 	}
15131 	flags = fkeys->control.flags;
15132 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15133 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15134 		rc = -EPROTONOSUPPORT;
15135 		goto err_free;
15136 	}
15137 	new_fltr->l2_fltr = l2_fltr;
15138 
15139 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15140 	rcu_read_lock();
15141 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15142 	if (fltr) {
15143 		rc = fltr->base.sw_id;
15144 		rcu_read_unlock();
15145 		goto err_free;
15146 	}
15147 	rcu_read_unlock();
15148 
15149 	new_fltr->flow_id = flow_id;
15150 	new_fltr->base.rxq = rxq_index;
15151 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15152 	if (!rc) {
15153 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15154 		return new_fltr->base.sw_id;
15155 	}
15156 
15157 err_free:
15158 	bnxt_del_l2_filter(bp, l2_fltr);
15159 	kfree(new_fltr);
15160 	return rc;
15161 }
15162 #endif
15163 
15164 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15165 {
15166 	spin_lock_bh(&bp->ntp_fltr_lock);
15167 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15168 		spin_unlock_bh(&bp->ntp_fltr_lock);
15169 		return;
15170 	}
15171 	hlist_del_rcu(&fltr->base.hash);
15172 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15173 	bp->ntp_fltr_count--;
15174 	spin_unlock_bh(&bp->ntp_fltr_lock);
15175 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15176 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15177 	kfree_rcu(fltr, base.rcu);
15178 }
15179 
15180 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15181 {
15182 #ifdef CONFIG_RFS_ACCEL
15183 	int i;
15184 
15185 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15186 		struct hlist_head *head;
15187 		struct hlist_node *tmp;
15188 		struct bnxt_ntuple_filter *fltr;
15189 		int rc;
15190 
15191 		head = &bp->ntp_fltr_hash_tbl[i];
15192 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15193 			bool del = false;
15194 
15195 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15196 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15197 					continue;
15198 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15199 							fltr->flow_id,
15200 							fltr->base.sw_id)) {
15201 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15202 									 fltr);
15203 					del = true;
15204 				}
15205 			} else {
15206 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15207 								       fltr);
15208 				if (rc)
15209 					del = true;
15210 				else
15211 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15212 			}
15213 
15214 			if (del)
15215 				bnxt_del_ntp_filter(bp, fltr);
15216 		}
15217 	}
15218 #endif
15219 }
15220 
15221 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15222 				    unsigned int entry, struct udp_tunnel_info *ti)
15223 {
15224 	struct bnxt *bp = netdev_priv(netdev);
15225 	unsigned int cmd;
15226 
15227 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15228 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15229 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15230 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15231 	else
15232 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15233 
15234 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15235 }
15236 
15237 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15238 				      unsigned int entry, struct udp_tunnel_info *ti)
15239 {
15240 	struct bnxt *bp = netdev_priv(netdev);
15241 	unsigned int cmd;
15242 
15243 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15244 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15245 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15246 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15247 	else
15248 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15249 
15250 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15251 }
15252 
15253 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15254 	.set_port	= bnxt_udp_tunnel_set_port,
15255 	.unset_port	= bnxt_udp_tunnel_unset_port,
15256 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15257 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15258 	.tables		= {
15259 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15260 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15261 	},
15262 }, bnxt_udp_tunnels_p7 = {
15263 	.set_port	= bnxt_udp_tunnel_set_port,
15264 	.unset_port	= bnxt_udp_tunnel_unset_port,
15265 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15266 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15267 	.tables		= {
15268 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15269 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15270 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15271 	},
15272 };
15273 
15274 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15275 			       struct net_device *dev, u32 filter_mask,
15276 			       int nlflags)
15277 {
15278 	struct bnxt *bp = netdev_priv(dev);
15279 
15280 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15281 				       nlflags, filter_mask, NULL);
15282 }
15283 
15284 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15285 			       u16 flags, struct netlink_ext_ack *extack)
15286 {
15287 	struct bnxt *bp = netdev_priv(dev);
15288 	struct nlattr *attr, *br_spec;
15289 	int rem, rc = 0;
15290 
15291 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15292 		return -EOPNOTSUPP;
15293 
15294 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15295 	if (!br_spec)
15296 		return -EINVAL;
15297 
15298 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15299 		u16 mode;
15300 
15301 		mode = nla_get_u16(attr);
15302 		if (mode == bp->br_mode)
15303 			break;
15304 
15305 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15306 		if (!rc)
15307 			bp->br_mode = mode;
15308 		break;
15309 	}
15310 	return rc;
15311 }
15312 
15313 int bnxt_get_port_parent_id(struct net_device *dev,
15314 			    struct netdev_phys_item_id *ppid)
15315 {
15316 	struct bnxt *bp = netdev_priv(dev);
15317 
15318 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15319 		return -EOPNOTSUPP;
15320 
15321 	/* The PF and it's VF-reps only support the switchdev framework */
15322 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15323 		return -EOPNOTSUPP;
15324 
15325 	ppid->id_len = sizeof(bp->dsn);
15326 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15327 
15328 	return 0;
15329 }
15330 
15331 static const struct net_device_ops bnxt_netdev_ops = {
15332 	.ndo_open		= bnxt_open,
15333 	.ndo_start_xmit		= bnxt_start_xmit,
15334 	.ndo_stop		= bnxt_close,
15335 	.ndo_get_stats64	= bnxt_get_stats64,
15336 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15337 	.ndo_eth_ioctl		= bnxt_ioctl,
15338 	.ndo_validate_addr	= eth_validate_addr,
15339 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15340 	.ndo_change_mtu		= bnxt_change_mtu,
15341 	.ndo_fix_features	= bnxt_fix_features,
15342 	.ndo_set_features	= bnxt_set_features,
15343 	.ndo_features_check	= bnxt_features_check,
15344 	.ndo_tx_timeout		= bnxt_tx_timeout,
15345 #ifdef CONFIG_BNXT_SRIOV
15346 	.ndo_get_vf_config	= bnxt_get_vf_config,
15347 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15348 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15349 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15350 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15351 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15352 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15353 #endif
15354 	.ndo_setup_tc           = bnxt_setup_tc,
15355 #ifdef CONFIG_RFS_ACCEL
15356 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15357 #endif
15358 	.ndo_bpf		= bnxt_xdp,
15359 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15360 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15361 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15362 };
15363 
15364 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15365 				    struct netdev_queue_stats_rx *stats)
15366 {
15367 	struct bnxt *bp = netdev_priv(dev);
15368 	struct bnxt_cp_ring_info *cpr;
15369 	u64 *sw;
15370 
15371 	cpr = &bp->bnapi[i]->cp_ring;
15372 	sw = cpr->stats.sw_stats;
15373 
15374 	stats->packets = 0;
15375 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15376 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15377 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15378 
15379 	stats->bytes = 0;
15380 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15381 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15382 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15383 
15384 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15385 }
15386 
15387 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15388 				    struct netdev_queue_stats_tx *stats)
15389 {
15390 	struct bnxt *bp = netdev_priv(dev);
15391 	struct bnxt_napi *bnapi;
15392 	u64 *sw;
15393 
15394 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15395 	sw = bnapi->cp_ring.stats.sw_stats;
15396 
15397 	stats->packets = 0;
15398 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15399 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15400 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15401 
15402 	stats->bytes = 0;
15403 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15404 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15405 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15406 }
15407 
15408 static void bnxt_get_base_stats(struct net_device *dev,
15409 				struct netdev_queue_stats_rx *rx,
15410 				struct netdev_queue_stats_tx *tx)
15411 {
15412 	struct bnxt *bp = netdev_priv(dev);
15413 
15414 	rx->packets = bp->net_stats_prev.rx_packets;
15415 	rx->bytes = bp->net_stats_prev.rx_bytes;
15416 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15417 
15418 	tx->packets = bp->net_stats_prev.tx_packets;
15419 	tx->bytes = bp->net_stats_prev.tx_bytes;
15420 }
15421 
15422 static const struct netdev_stat_ops bnxt_stat_ops = {
15423 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15424 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15425 	.get_base_stats		= bnxt_get_base_stats,
15426 };
15427 
15428 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15429 {
15430 	struct bnxt_rx_ring_info *rxr, *clone;
15431 	struct bnxt *bp = netdev_priv(dev);
15432 	struct bnxt_ring_struct *ring;
15433 	int rc;
15434 
15435 	rxr = &bp->rx_ring[idx];
15436 	clone = qmem;
15437 	memcpy(clone, rxr, sizeof(*rxr));
15438 	bnxt_init_rx_ring_struct(bp, clone);
15439 	bnxt_reset_rx_ring_struct(bp, clone);
15440 
15441 	clone->rx_prod = 0;
15442 	clone->rx_agg_prod = 0;
15443 	clone->rx_sw_agg_prod = 0;
15444 	clone->rx_next_cons = 0;
15445 
15446 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15447 	if (rc)
15448 		return rc;
15449 
15450 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15451 	if (rc < 0)
15452 		goto err_page_pool_destroy;
15453 
15454 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15455 					MEM_TYPE_PAGE_POOL,
15456 					clone->page_pool);
15457 	if (rc)
15458 		goto err_rxq_info_unreg;
15459 
15460 	ring = &clone->rx_ring_struct;
15461 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15462 	if (rc)
15463 		goto err_free_rx_ring;
15464 
15465 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15466 		ring = &clone->rx_agg_ring_struct;
15467 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15468 		if (rc)
15469 			goto err_free_rx_agg_ring;
15470 
15471 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15472 		if (rc)
15473 			goto err_free_rx_agg_ring;
15474 	}
15475 
15476 	if (bp->flags & BNXT_FLAG_TPA) {
15477 		rc = bnxt_alloc_one_tpa_info(bp, clone);
15478 		if (rc)
15479 			goto err_free_tpa_info;
15480 	}
15481 
15482 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15483 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15484 
15485 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15486 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15487 		bnxt_alloc_one_rx_ring_page(bp, clone, idx);
15488 	if (bp->flags & BNXT_FLAG_TPA)
15489 		bnxt_alloc_one_tpa_info_data(bp, clone);
15490 
15491 	return 0;
15492 
15493 err_free_tpa_info:
15494 	bnxt_free_one_tpa_info(bp, clone);
15495 err_free_rx_agg_ring:
15496 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15497 err_free_rx_ring:
15498 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15499 err_rxq_info_unreg:
15500 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15501 err_page_pool_destroy:
15502 	page_pool_destroy(clone->page_pool);
15503 	if (bnxt_separate_head_pool())
15504 		page_pool_destroy(clone->head_pool);
15505 	clone->page_pool = NULL;
15506 	clone->head_pool = NULL;
15507 	return rc;
15508 }
15509 
15510 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15511 {
15512 	struct bnxt_rx_ring_info *rxr = qmem;
15513 	struct bnxt *bp = netdev_priv(dev);
15514 	struct bnxt_ring_struct *ring;
15515 
15516 	bnxt_free_one_rx_ring_skbs(bp, rxr);
15517 
15518 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15519 
15520 	page_pool_destroy(rxr->page_pool);
15521 	if (bnxt_separate_head_pool())
15522 		page_pool_destroy(rxr->head_pool);
15523 	rxr->page_pool = NULL;
15524 	rxr->head_pool = NULL;
15525 
15526 	ring = &rxr->rx_ring_struct;
15527 	bnxt_free_ring(bp, &ring->ring_mem);
15528 
15529 	ring = &rxr->rx_agg_ring_struct;
15530 	bnxt_free_ring(bp, &ring->ring_mem);
15531 
15532 	kfree(rxr->rx_agg_bmap);
15533 	rxr->rx_agg_bmap = NULL;
15534 }
15535 
15536 static void bnxt_copy_rx_ring(struct bnxt *bp,
15537 			      struct bnxt_rx_ring_info *dst,
15538 			      struct bnxt_rx_ring_info *src)
15539 {
15540 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15541 	struct bnxt_ring_struct *dst_ring, *src_ring;
15542 	int i;
15543 
15544 	dst_ring = &dst->rx_ring_struct;
15545 	dst_rmem = &dst_ring->ring_mem;
15546 	src_ring = &src->rx_ring_struct;
15547 	src_rmem = &src_ring->ring_mem;
15548 
15549 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15550 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15551 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15552 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15553 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15554 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15555 
15556 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15557 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15558 	*dst_rmem->vmem = *src_rmem->vmem;
15559 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15560 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15561 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15562 	}
15563 
15564 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15565 		return;
15566 
15567 	dst_ring = &dst->rx_agg_ring_struct;
15568 	dst_rmem = &dst_ring->ring_mem;
15569 	src_ring = &src->rx_agg_ring_struct;
15570 	src_rmem = &src_ring->ring_mem;
15571 
15572 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15573 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15574 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15575 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15576 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15577 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15578 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15579 
15580 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15581 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15582 	*dst_rmem->vmem = *src_rmem->vmem;
15583 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15584 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15585 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15586 	}
15587 
15588 	dst->rx_agg_bmap = src->rx_agg_bmap;
15589 }
15590 
15591 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15592 {
15593 	struct bnxt *bp = netdev_priv(dev);
15594 	struct bnxt_rx_ring_info *rxr, *clone;
15595 	struct bnxt_cp_ring_info *cpr;
15596 	struct bnxt_vnic_info *vnic;
15597 	int i, rc;
15598 
15599 	rxr = &bp->rx_ring[idx];
15600 	clone = qmem;
15601 
15602 	rxr->rx_prod = clone->rx_prod;
15603 	rxr->rx_agg_prod = clone->rx_agg_prod;
15604 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
15605 	rxr->rx_next_cons = clone->rx_next_cons;
15606 	rxr->rx_tpa = clone->rx_tpa;
15607 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
15608 	rxr->page_pool = clone->page_pool;
15609 	rxr->head_pool = clone->head_pool;
15610 	rxr->xdp_rxq = clone->xdp_rxq;
15611 
15612 	bnxt_copy_rx_ring(bp, rxr, clone);
15613 
15614 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
15615 	if (rc)
15616 		return rc;
15617 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
15618 	if (rc)
15619 		goto err_free_hwrm_rx_ring;
15620 
15621 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
15622 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15623 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
15624 
15625 	cpr = &rxr->bnapi->cp_ring;
15626 	cpr->sw_stats->rx.rx_resets++;
15627 
15628 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15629 		vnic = &bp->vnic_info[i];
15630 
15631 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
15632 		if (rc) {
15633 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
15634 				   vnic->vnic_id, rc);
15635 			return rc;
15636 		}
15637 		vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
15638 		bnxt_hwrm_vnic_update(bp, vnic,
15639 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15640 	}
15641 
15642 	return 0;
15643 
15644 err_free_hwrm_rx_ring:
15645 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15646 	return rc;
15647 }
15648 
15649 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
15650 {
15651 	struct bnxt *bp = netdev_priv(dev);
15652 	struct bnxt_rx_ring_info *rxr;
15653 	struct bnxt_vnic_info *vnic;
15654 	int i;
15655 
15656 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15657 		vnic = &bp->vnic_info[i];
15658 		vnic->mru = 0;
15659 		bnxt_hwrm_vnic_update(bp, vnic,
15660 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15661 	}
15662 	/* Make sure NAPI sees that the VNIC is disabled */
15663 	synchronize_net();
15664 	rxr = &bp->rx_ring[idx];
15665 	cancel_work_sync(&rxr->bnapi->cp_ring.dim.work);
15666 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15667 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
15668 	rxr->rx_next_cons = 0;
15669 	page_pool_disable_direct_recycling(rxr->page_pool);
15670 	if (bnxt_separate_head_pool())
15671 		page_pool_disable_direct_recycling(rxr->head_pool);
15672 
15673 	memcpy(qmem, rxr, sizeof(*rxr));
15674 	bnxt_init_rx_ring_struct(bp, qmem);
15675 
15676 	return 0;
15677 }
15678 
15679 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
15680 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
15681 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
15682 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
15683 	.ndo_queue_start	= bnxt_queue_start,
15684 	.ndo_queue_stop		= bnxt_queue_stop,
15685 };
15686 
15687 static void bnxt_remove_one(struct pci_dev *pdev)
15688 {
15689 	struct net_device *dev = pci_get_drvdata(pdev);
15690 	struct bnxt *bp = netdev_priv(dev);
15691 
15692 	if (BNXT_PF(bp))
15693 		bnxt_sriov_disable(bp);
15694 
15695 	bnxt_rdma_aux_device_del(bp);
15696 
15697 	bnxt_ptp_clear(bp);
15698 	unregister_netdev(dev);
15699 
15700 	bnxt_rdma_aux_device_uninit(bp);
15701 
15702 	bnxt_free_l2_filters(bp, true);
15703 	bnxt_free_ntp_fltrs(bp, true);
15704 	WARN_ON(bp->num_rss_ctx);
15705 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15706 	/* Flush any pending tasks */
15707 	cancel_work_sync(&bp->sp_task);
15708 	cancel_delayed_work_sync(&bp->fw_reset_task);
15709 	bp->sp_event = 0;
15710 
15711 	bnxt_dl_fw_reporters_destroy(bp);
15712 	bnxt_dl_unregister(bp);
15713 	bnxt_shutdown_tc(bp);
15714 
15715 	bnxt_clear_int_mode(bp);
15716 	bnxt_hwrm_func_drv_unrgtr(bp);
15717 	bnxt_free_hwrm_resources(bp);
15718 	bnxt_hwmon_uninit(bp);
15719 	bnxt_ethtool_free(bp);
15720 	bnxt_dcb_free(bp);
15721 	kfree(bp->ptp_cfg);
15722 	bp->ptp_cfg = NULL;
15723 	kfree(bp->fw_health);
15724 	bp->fw_health = NULL;
15725 	bnxt_cleanup_pci(bp);
15726 	bnxt_free_ctx_mem(bp, true);
15727 	bnxt_free_crash_dump_mem(bp);
15728 	kfree(bp->rss_indir_tbl);
15729 	bp->rss_indir_tbl = NULL;
15730 	bnxt_free_port_stats(bp);
15731 	free_netdev(dev);
15732 }
15733 
15734 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
15735 {
15736 	int rc = 0;
15737 	struct bnxt_link_info *link_info = &bp->link_info;
15738 
15739 	bp->phy_flags = 0;
15740 	rc = bnxt_hwrm_phy_qcaps(bp);
15741 	if (rc) {
15742 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
15743 			   rc);
15744 		return rc;
15745 	}
15746 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
15747 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
15748 	else
15749 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
15750 
15751 	bp->mac_flags = 0;
15752 	bnxt_hwrm_mac_qcaps(bp);
15753 
15754 	if (!fw_dflt)
15755 		return 0;
15756 
15757 	mutex_lock(&bp->link_lock);
15758 	rc = bnxt_update_link(bp, false);
15759 	if (rc) {
15760 		mutex_unlock(&bp->link_lock);
15761 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
15762 			   rc);
15763 		return rc;
15764 	}
15765 
15766 	/* Older firmware does not have supported_auto_speeds, so assume
15767 	 * that all supported speeds can be autonegotiated.
15768 	 */
15769 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
15770 		link_info->support_auto_speeds = link_info->support_speeds;
15771 
15772 	bnxt_init_ethtool_link_settings(bp);
15773 	mutex_unlock(&bp->link_lock);
15774 	return 0;
15775 }
15776 
15777 static int bnxt_get_max_irq(struct pci_dev *pdev)
15778 {
15779 	u16 ctrl;
15780 
15781 	if (!pdev->msix_cap)
15782 		return 1;
15783 
15784 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
15785 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
15786 }
15787 
15788 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15789 				int *max_cp)
15790 {
15791 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
15792 	int max_ring_grps = 0, max_irq;
15793 
15794 	*max_tx = hw_resc->max_tx_rings;
15795 	*max_rx = hw_resc->max_rx_rings;
15796 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
15797 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
15798 			bnxt_get_ulp_msix_num_in_use(bp),
15799 			hw_resc->max_stat_ctxs -
15800 			bnxt_get_ulp_stat_ctxs_in_use(bp));
15801 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
15802 		*max_cp = min_t(int, *max_cp, max_irq);
15803 	max_ring_grps = hw_resc->max_hw_ring_grps;
15804 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
15805 		*max_cp -= 1;
15806 		*max_rx -= 2;
15807 	}
15808 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15809 		*max_rx >>= 1;
15810 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
15811 		int rc;
15812 
15813 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
15814 		if (rc) {
15815 			*max_rx = 0;
15816 			*max_tx = 0;
15817 		}
15818 		/* On P5 chips, max_cp output param should be available NQs */
15819 		*max_cp = max_irq;
15820 	}
15821 	*max_rx = min_t(int, *max_rx, max_ring_grps);
15822 }
15823 
15824 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
15825 {
15826 	int rx, tx, cp;
15827 
15828 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
15829 	*max_rx = rx;
15830 	*max_tx = tx;
15831 	if (!rx || !tx || !cp)
15832 		return -ENOMEM;
15833 
15834 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
15835 }
15836 
15837 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15838 			       bool shared)
15839 {
15840 	int rc;
15841 
15842 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15843 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
15844 		/* Not enough rings, try disabling agg rings. */
15845 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
15846 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15847 		if (rc) {
15848 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
15849 			bp->flags |= BNXT_FLAG_AGG_RINGS;
15850 			return rc;
15851 		}
15852 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
15853 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15854 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15855 		bnxt_set_ring_params(bp);
15856 	}
15857 
15858 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
15859 		int max_cp, max_stat, max_irq;
15860 
15861 		/* Reserve minimum resources for RoCE */
15862 		max_cp = bnxt_get_max_func_cp_rings(bp);
15863 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
15864 		max_irq = bnxt_get_max_func_irqs(bp);
15865 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
15866 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
15867 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
15868 			return 0;
15869 
15870 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
15871 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
15872 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
15873 		max_cp = min_t(int, max_cp, max_irq);
15874 		max_cp = min_t(int, max_cp, max_stat);
15875 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
15876 		if (rc)
15877 			rc = 0;
15878 	}
15879 	return rc;
15880 }
15881 
15882 /* In initial default shared ring setting, each shared ring must have a
15883  * RX/TX ring pair.
15884  */
15885 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
15886 {
15887 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
15888 	bp->rx_nr_rings = bp->cp_nr_rings;
15889 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
15890 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15891 }
15892 
15893 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
15894 {
15895 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
15896 	int avail_msix;
15897 
15898 	if (!bnxt_can_reserve_rings(bp))
15899 		return 0;
15900 
15901 	if (sh)
15902 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
15903 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
15904 	/* Reduce default rings on multi-port cards so that total default
15905 	 * rings do not exceed CPU count.
15906 	 */
15907 	if (bp->port_count > 1) {
15908 		int max_rings =
15909 			max_t(int, num_online_cpus() / bp->port_count, 1);
15910 
15911 		dflt_rings = min_t(int, dflt_rings, max_rings);
15912 	}
15913 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
15914 	if (rc)
15915 		return rc;
15916 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
15917 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
15918 	if (sh)
15919 		bnxt_trim_dflt_sh_rings(bp);
15920 	else
15921 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
15922 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15923 
15924 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
15925 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
15926 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
15927 
15928 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
15929 		bnxt_set_dflt_ulp_stat_ctxs(bp);
15930 	}
15931 
15932 	rc = __bnxt_reserve_rings(bp);
15933 	if (rc && rc != -ENODEV)
15934 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
15935 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15936 	if (sh)
15937 		bnxt_trim_dflt_sh_rings(bp);
15938 
15939 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
15940 	if (bnxt_need_reserve_rings(bp)) {
15941 		rc = __bnxt_reserve_rings(bp);
15942 		if (rc && rc != -ENODEV)
15943 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
15944 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15945 	}
15946 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
15947 		bp->rx_nr_rings++;
15948 		bp->cp_nr_rings++;
15949 	}
15950 	if (rc) {
15951 		bp->tx_nr_rings = 0;
15952 		bp->rx_nr_rings = 0;
15953 	}
15954 	return rc;
15955 }
15956 
15957 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
15958 {
15959 	int rc;
15960 
15961 	if (bp->tx_nr_rings)
15962 		return 0;
15963 
15964 	bnxt_ulp_irq_stop(bp);
15965 	bnxt_clear_int_mode(bp);
15966 	rc = bnxt_set_dflt_rings(bp, true);
15967 	if (rc) {
15968 		if (BNXT_VF(bp) && rc == -ENODEV)
15969 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15970 		else
15971 			netdev_err(bp->dev, "Not enough rings available.\n");
15972 		goto init_dflt_ring_err;
15973 	}
15974 	rc = bnxt_init_int_mode(bp);
15975 	if (rc)
15976 		goto init_dflt_ring_err;
15977 
15978 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15979 
15980 	bnxt_set_dflt_rfs(bp);
15981 
15982 init_dflt_ring_err:
15983 	bnxt_ulp_irq_restart(bp, rc);
15984 	return rc;
15985 }
15986 
15987 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
15988 {
15989 	int rc;
15990 
15991 	ASSERT_RTNL();
15992 	bnxt_hwrm_func_qcaps(bp);
15993 
15994 	if (netif_running(bp->dev))
15995 		__bnxt_close_nic(bp, true, false);
15996 
15997 	bnxt_ulp_irq_stop(bp);
15998 	bnxt_clear_int_mode(bp);
15999 	rc = bnxt_init_int_mode(bp);
16000 	bnxt_ulp_irq_restart(bp, rc);
16001 
16002 	if (netif_running(bp->dev)) {
16003 		if (rc)
16004 			dev_close(bp->dev);
16005 		else
16006 			rc = bnxt_open_nic(bp, true, false);
16007 	}
16008 
16009 	return rc;
16010 }
16011 
16012 static int bnxt_init_mac_addr(struct bnxt *bp)
16013 {
16014 	int rc = 0;
16015 
16016 	if (BNXT_PF(bp)) {
16017 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16018 	} else {
16019 #ifdef CONFIG_BNXT_SRIOV
16020 		struct bnxt_vf_info *vf = &bp->vf;
16021 		bool strict_approval = true;
16022 
16023 		if (is_valid_ether_addr(vf->mac_addr)) {
16024 			/* overwrite netdev dev_addr with admin VF MAC */
16025 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16026 			/* Older PF driver or firmware may not approve this
16027 			 * correctly.
16028 			 */
16029 			strict_approval = false;
16030 		} else {
16031 			eth_hw_addr_random(bp->dev);
16032 		}
16033 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16034 #endif
16035 	}
16036 	return rc;
16037 }
16038 
16039 static void bnxt_vpd_read_info(struct bnxt *bp)
16040 {
16041 	struct pci_dev *pdev = bp->pdev;
16042 	unsigned int vpd_size, kw_len;
16043 	int pos, size;
16044 	u8 *vpd_data;
16045 
16046 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16047 	if (IS_ERR(vpd_data)) {
16048 		pci_warn(pdev, "Unable to read VPD\n");
16049 		return;
16050 	}
16051 
16052 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16053 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16054 	if (pos < 0)
16055 		goto read_sn;
16056 
16057 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16058 	memcpy(bp->board_partno, &vpd_data[pos], size);
16059 
16060 read_sn:
16061 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16062 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16063 					   &kw_len);
16064 	if (pos < 0)
16065 		goto exit;
16066 
16067 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16068 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16069 exit:
16070 	kfree(vpd_data);
16071 }
16072 
16073 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16074 {
16075 	struct pci_dev *pdev = bp->pdev;
16076 	u64 qword;
16077 
16078 	qword = pci_get_dsn(pdev);
16079 	if (!qword) {
16080 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16081 		return -EOPNOTSUPP;
16082 	}
16083 
16084 	put_unaligned_le64(qword, dsn);
16085 
16086 	bp->flags |= BNXT_FLAG_DSN_VALID;
16087 	return 0;
16088 }
16089 
16090 static int bnxt_map_db_bar(struct bnxt *bp)
16091 {
16092 	if (!bp->db_size)
16093 		return -ENODEV;
16094 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16095 	if (!bp->bar1)
16096 		return -ENOMEM;
16097 	return 0;
16098 }
16099 
16100 void bnxt_print_device_info(struct bnxt *bp)
16101 {
16102 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16103 		    board_info[bp->board_idx].name,
16104 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16105 
16106 	pcie_print_link_status(bp->pdev);
16107 }
16108 
16109 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16110 {
16111 	struct bnxt_hw_resc *hw_resc;
16112 	struct net_device *dev;
16113 	struct bnxt *bp;
16114 	int rc, max_irqs;
16115 
16116 	if (pci_is_bridge(pdev))
16117 		return -ENODEV;
16118 
16119 	if (!pdev->msix_cap) {
16120 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16121 		return -ENODEV;
16122 	}
16123 
16124 	/* Clear any pending DMA transactions from crash kernel
16125 	 * while loading driver in capture kernel.
16126 	 */
16127 	if (is_kdump_kernel()) {
16128 		pci_clear_master(pdev);
16129 		pcie_flr(pdev);
16130 	}
16131 
16132 	max_irqs = bnxt_get_max_irq(pdev);
16133 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16134 				 max_irqs);
16135 	if (!dev)
16136 		return -ENOMEM;
16137 
16138 	bp = netdev_priv(dev);
16139 	bp->board_idx = ent->driver_data;
16140 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16141 	bnxt_set_max_func_irqs(bp, max_irqs);
16142 
16143 	if (bnxt_vf_pciid(bp->board_idx))
16144 		bp->flags |= BNXT_FLAG_VF;
16145 
16146 	/* No devlink port registration in case of a VF */
16147 	if (BNXT_PF(bp))
16148 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16149 
16150 	rc = bnxt_init_board(pdev, dev);
16151 	if (rc < 0)
16152 		goto init_err_free;
16153 
16154 	dev->netdev_ops = &bnxt_netdev_ops;
16155 	dev->stat_ops = &bnxt_stat_ops;
16156 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16157 	dev->ethtool_ops = &bnxt_ethtool_ops;
16158 	pci_set_drvdata(pdev, dev);
16159 
16160 	rc = bnxt_alloc_hwrm_resources(bp);
16161 	if (rc)
16162 		goto init_err_pci_clean;
16163 
16164 	mutex_init(&bp->hwrm_cmd_lock);
16165 	mutex_init(&bp->link_lock);
16166 
16167 	rc = bnxt_fw_init_one_p1(bp);
16168 	if (rc)
16169 		goto init_err_pci_clean;
16170 
16171 	if (BNXT_PF(bp))
16172 		bnxt_vpd_read_info(bp);
16173 
16174 	if (BNXT_CHIP_P5_PLUS(bp)) {
16175 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16176 		if (BNXT_CHIP_P7(bp))
16177 			bp->flags |= BNXT_FLAG_CHIP_P7;
16178 	}
16179 
16180 	rc = bnxt_alloc_rss_indir_tbl(bp);
16181 	if (rc)
16182 		goto init_err_pci_clean;
16183 
16184 	rc = bnxt_fw_init_one_p2(bp);
16185 	if (rc)
16186 		goto init_err_pci_clean;
16187 
16188 	rc = bnxt_map_db_bar(bp);
16189 	if (rc) {
16190 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16191 			rc);
16192 		goto init_err_pci_clean;
16193 	}
16194 
16195 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16196 			   NETIF_F_TSO | NETIF_F_TSO6 |
16197 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16198 			   NETIF_F_GSO_IPXIP4 |
16199 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16200 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16201 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16202 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16203 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16204 
16205 	if (BNXT_SUPPORTS_TPA(bp))
16206 		dev->hw_features |= NETIF_F_LRO;
16207 
16208 	dev->hw_enc_features =
16209 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16210 			NETIF_F_TSO | NETIF_F_TSO6 |
16211 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16212 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16213 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16214 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16215 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16216 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16217 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16218 	else
16219 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16220 
16221 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16222 				    NETIF_F_GSO_GRE_CSUM;
16223 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16224 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16225 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16226 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16227 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16228 	if (BNXT_SUPPORTS_TPA(bp))
16229 		dev->hw_features |= NETIF_F_GRO_HW;
16230 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16231 	if (dev->features & NETIF_F_GRO_HW)
16232 		dev->features &= ~NETIF_F_LRO;
16233 	dev->priv_flags |= IFF_UNICAST_FLT;
16234 
16235 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16236 	if (bp->tso_max_segs)
16237 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16238 
16239 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16240 			    NETDEV_XDP_ACT_RX_SG;
16241 
16242 #ifdef CONFIG_BNXT_SRIOV
16243 	init_waitqueue_head(&bp->sriov_cfg_wait);
16244 #endif
16245 	if (BNXT_SUPPORTS_TPA(bp)) {
16246 		bp->gro_func = bnxt_gro_func_5730x;
16247 		if (BNXT_CHIP_P4(bp))
16248 			bp->gro_func = bnxt_gro_func_5731x;
16249 		else if (BNXT_CHIP_P5_PLUS(bp))
16250 			bp->gro_func = bnxt_gro_func_5750x;
16251 	}
16252 	if (!BNXT_CHIP_P4_PLUS(bp))
16253 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16254 
16255 	rc = bnxt_init_mac_addr(bp);
16256 	if (rc) {
16257 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16258 		rc = -EADDRNOTAVAIL;
16259 		goto init_err_pci_clean;
16260 	}
16261 
16262 	if (BNXT_PF(bp)) {
16263 		/* Read the adapter's DSN to use as the eswitch switch_id */
16264 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16265 	}
16266 
16267 	/* MTU range: 60 - FW defined max */
16268 	dev->min_mtu = ETH_ZLEN;
16269 	dev->max_mtu = bp->max_mtu;
16270 
16271 	rc = bnxt_probe_phy(bp, true);
16272 	if (rc)
16273 		goto init_err_pci_clean;
16274 
16275 	hw_resc = &bp->hw_resc;
16276 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16277 		       BNXT_L2_FLTR_MAX_FLTR;
16278 	/* Older firmware may not report these filters properly */
16279 	if (bp->max_fltr < BNXT_MAX_FLTR)
16280 		bp->max_fltr = BNXT_MAX_FLTR;
16281 	bnxt_init_l2_fltr_tbl(bp);
16282 	__bnxt_set_rx_skb_mode(bp, false);
16283 	bnxt_set_tpa_flags(bp);
16284 	bnxt_init_ring_params(bp);
16285 	bnxt_set_ring_params(bp);
16286 	bnxt_rdma_aux_device_init(bp);
16287 	rc = bnxt_set_dflt_rings(bp, true);
16288 	if (rc) {
16289 		if (BNXT_VF(bp) && rc == -ENODEV) {
16290 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16291 		} else {
16292 			netdev_err(bp->dev, "Not enough rings available.\n");
16293 			rc = -ENOMEM;
16294 		}
16295 		goto init_err_pci_clean;
16296 	}
16297 
16298 	bnxt_fw_init_one_p3(bp);
16299 
16300 	bnxt_init_dflt_coal(bp);
16301 
16302 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16303 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16304 
16305 	rc = bnxt_init_int_mode(bp);
16306 	if (rc)
16307 		goto init_err_pci_clean;
16308 
16309 	/* No TC has been set yet and rings may have been trimmed due to
16310 	 * limited MSIX, so we re-initialize the TX rings per TC.
16311 	 */
16312 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16313 
16314 	if (BNXT_PF(bp)) {
16315 		if (!bnxt_pf_wq) {
16316 			bnxt_pf_wq =
16317 				create_singlethread_workqueue("bnxt_pf_wq");
16318 			if (!bnxt_pf_wq) {
16319 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16320 				rc = -ENOMEM;
16321 				goto init_err_pci_clean;
16322 			}
16323 		}
16324 		rc = bnxt_init_tc(bp);
16325 		if (rc)
16326 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16327 				   rc);
16328 	}
16329 
16330 	bnxt_inv_fw_health_reg(bp);
16331 	rc = bnxt_dl_register(bp);
16332 	if (rc)
16333 		goto init_err_dl;
16334 
16335 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16336 
16337 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16338 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16339 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16340 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16341 
16342 	rc = register_netdev(dev);
16343 	if (rc)
16344 		goto init_err_cleanup;
16345 
16346 	bnxt_dl_fw_reporters_create(bp);
16347 
16348 	bnxt_rdma_aux_device_add(bp);
16349 
16350 	bnxt_print_device_info(bp);
16351 
16352 	pci_save_state(pdev);
16353 
16354 	return 0;
16355 init_err_cleanup:
16356 	bnxt_rdma_aux_device_uninit(bp);
16357 	bnxt_dl_unregister(bp);
16358 init_err_dl:
16359 	bnxt_shutdown_tc(bp);
16360 	bnxt_clear_int_mode(bp);
16361 
16362 init_err_pci_clean:
16363 	bnxt_hwrm_func_drv_unrgtr(bp);
16364 	bnxt_free_hwrm_resources(bp);
16365 	bnxt_hwmon_uninit(bp);
16366 	bnxt_ethtool_free(bp);
16367 	bnxt_ptp_clear(bp);
16368 	kfree(bp->ptp_cfg);
16369 	bp->ptp_cfg = NULL;
16370 	kfree(bp->fw_health);
16371 	bp->fw_health = NULL;
16372 	bnxt_cleanup_pci(bp);
16373 	bnxt_free_ctx_mem(bp, true);
16374 	bnxt_free_crash_dump_mem(bp);
16375 	kfree(bp->rss_indir_tbl);
16376 	bp->rss_indir_tbl = NULL;
16377 
16378 init_err_free:
16379 	free_netdev(dev);
16380 	return rc;
16381 }
16382 
16383 static void bnxt_shutdown(struct pci_dev *pdev)
16384 {
16385 	struct net_device *dev = pci_get_drvdata(pdev);
16386 	struct bnxt *bp;
16387 
16388 	if (!dev)
16389 		return;
16390 
16391 	rtnl_lock();
16392 	bp = netdev_priv(dev);
16393 	if (!bp)
16394 		goto shutdown_exit;
16395 
16396 	if (netif_running(dev))
16397 		dev_close(dev);
16398 
16399 	bnxt_ptp_clear(bp);
16400 	bnxt_clear_int_mode(bp);
16401 	pci_disable_device(pdev);
16402 
16403 	if (system_state == SYSTEM_POWER_OFF) {
16404 		pci_wake_from_d3(pdev, bp->wol);
16405 		pci_set_power_state(pdev, PCI_D3hot);
16406 	}
16407 
16408 shutdown_exit:
16409 	rtnl_unlock();
16410 }
16411 
16412 #ifdef CONFIG_PM_SLEEP
16413 static int bnxt_suspend(struct device *device)
16414 {
16415 	struct net_device *dev = dev_get_drvdata(device);
16416 	struct bnxt *bp = netdev_priv(dev);
16417 	int rc = 0;
16418 
16419 	bnxt_ulp_stop(bp);
16420 
16421 	rtnl_lock();
16422 	if (netif_running(dev)) {
16423 		netif_device_detach(dev);
16424 		rc = bnxt_close(dev);
16425 	}
16426 	bnxt_hwrm_func_drv_unrgtr(bp);
16427 	bnxt_ptp_clear(bp);
16428 	pci_disable_device(bp->pdev);
16429 	bnxt_free_ctx_mem(bp, false);
16430 	rtnl_unlock();
16431 	return rc;
16432 }
16433 
16434 static int bnxt_resume(struct device *device)
16435 {
16436 	struct net_device *dev = dev_get_drvdata(device);
16437 	struct bnxt *bp = netdev_priv(dev);
16438 	int rc = 0;
16439 
16440 	rtnl_lock();
16441 	rc = pci_enable_device(bp->pdev);
16442 	if (rc) {
16443 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16444 			   rc);
16445 		goto resume_exit;
16446 	}
16447 	pci_set_master(bp->pdev);
16448 	if (bnxt_hwrm_ver_get(bp)) {
16449 		rc = -ENODEV;
16450 		goto resume_exit;
16451 	}
16452 	rc = bnxt_hwrm_func_reset(bp);
16453 	if (rc) {
16454 		rc = -EBUSY;
16455 		goto resume_exit;
16456 	}
16457 
16458 	rc = bnxt_hwrm_func_qcaps(bp);
16459 	if (rc)
16460 		goto resume_exit;
16461 
16462 	bnxt_clear_reservations(bp, true);
16463 
16464 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16465 		rc = -ENODEV;
16466 		goto resume_exit;
16467 	}
16468 	if (bp->fw_crash_mem)
16469 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16470 
16471 	if (bnxt_ptp_init(bp)) {
16472 		kfree(bp->ptp_cfg);
16473 		bp->ptp_cfg = NULL;
16474 	}
16475 	bnxt_get_wol_settings(bp);
16476 	if (netif_running(dev)) {
16477 		rc = bnxt_open(dev);
16478 		if (!rc)
16479 			netif_device_attach(dev);
16480 	}
16481 
16482 resume_exit:
16483 	rtnl_unlock();
16484 	bnxt_ulp_start(bp, rc);
16485 	if (!rc)
16486 		bnxt_reenable_sriov(bp);
16487 	return rc;
16488 }
16489 
16490 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16491 #define BNXT_PM_OPS (&bnxt_pm_ops)
16492 
16493 #else
16494 
16495 #define BNXT_PM_OPS NULL
16496 
16497 #endif /* CONFIG_PM_SLEEP */
16498 
16499 /**
16500  * bnxt_io_error_detected - called when PCI error is detected
16501  * @pdev: Pointer to PCI device
16502  * @state: The current pci connection state
16503  *
16504  * This function is called after a PCI bus error affecting
16505  * this device has been detected.
16506  */
16507 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16508 					       pci_channel_state_t state)
16509 {
16510 	struct net_device *netdev = pci_get_drvdata(pdev);
16511 	struct bnxt *bp = netdev_priv(netdev);
16512 	bool abort = false;
16513 
16514 	netdev_info(netdev, "PCI I/O error detected\n");
16515 
16516 	bnxt_ulp_stop(bp);
16517 
16518 	rtnl_lock();
16519 	netif_device_detach(netdev);
16520 
16521 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16522 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16523 		abort = true;
16524 	}
16525 
16526 	if (abort || state == pci_channel_io_perm_failure) {
16527 		rtnl_unlock();
16528 		return PCI_ERS_RESULT_DISCONNECT;
16529 	}
16530 
16531 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16532 	 * so we disable bus master to prevent any potential bad DMAs before
16533 	 * freeing kernel memory.
16534 	 */
16535 	if (state == pci_channel_io_frozen) {
16536 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16537 		bnxt_fw_fatal_close(bp);
16538 	}
16539 
16540 	if (netif_running(netdev))
16541 		__bnxt_close_nic(bp, true, true);
16542 
16543 	if (pci_is_enabled(pdev))
16544 		pci_disable_device(pdev);
16545 	bnxt_free_ctx_mem(bp, false);
16546 	rtnl_unlock();
16547 
16548 	/* Request a slot slot reset. */
16549 	return PCI_ERS_RESULT_NEED_RESET;
16550 }
16551 
16552 /**
16553  * bnxt_io_slot_reset - called after the pci bus has been reset.
16554  * @pdev: Pointer to PCI device
16555  *
16556  * Restart the card from scratch, as if from a cold-boot.
16557  * At this point, the card has experienced a hard reset,
16558  * followed by fixups by BIOS, and has its config space
16559  * set up identically to what it was at cold boot.
16560  */
16561 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
16562 {
16563 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
16564 	struct net_device *netdev = pci_get_drvdata(pdev);
16565 	struct bnxt *bp = netdev_priv(netdev);
16566 	int retry = 0;
16567 	int err = 0;
16568 	int off;
16569 
16570 	netdev_info(bp->dev, "PCI Slot Reset\n");
16571 
16572 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16573 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
16574 		msleep(900);
16575 
16576 	rtnl_lock();
16577 
16578 	if (pci_enable_device(pdev)) {
16579 		dev_err(&pdev->dev,
16580 			"Cannot re-enable PCI device after reset.\n");
16581 	} else {
16582 		pci_set_master(pdev);
16583 		/* Upon fatal error, our device internal logic that latches to
16584 		 * BAR value is getting reset and will restore only upon
16585 		 * rewriting the BARs.
16586 		 *
16587 		 * As pci_restore_state() does not re-write the BARs if the
16588 		 * value is same as saved value earlier, driver needs to
16589 		 * write the BARs to 0 to force restore, in case of fatal error.
16590 		 */
16591 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
16592 				       &bp->state)) {
16593 			for (off = PCI_BASE_ADDRESS_0;
16594 			     off <= PCI_BASE_ADDRESS_5; off += 4)
16595 				pci_write_config_dword(bp->pdev, off, 0);
16596 		}
16597 		pci_restore_state(pdev);
16598 		pci_save_state(pdev);
16599 
16600 		bnxt_inv_fw_health_reg(bp);
16601 		bnxt_try_map_fw_health_reg(bp);
16602 
16603 		/* In some PCIe AER scenarios, firmware may take up to
16604 		 * 10 seconds to become ready in the worst case.
16605 		 */
16606 		do {
16607 			err = bnxt_try_recover_fw(bp);
16608 			if (!err)
16609 				break;
16610 			retry++;
16611 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
16612 
16613 		if (err) {
16614 			dev_err(&pdev->dev, "Firmware not ready\n");
16615 			goto reset_exit;
16616 		}
16617 
16618 		err = bnxt_hwrm_func_reset(bp);
16619 		if (!err)
16620 			result = PCI_ERS_RESULT_RECOVERED;
16621 
16622 		bnxt_ulp_irq_stop(bp);
16623 		bnxt_clear_int_mode(bp);
16624 		err = bnxt_init_int_mode(bp);
16625 		bnxt_ulp_irq_restart(bp, err);
16626 	}
16627 
16628 reset_exit:
16629 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16630 	bnxt_clear_reservations(bp, true);
16631 	rtnl_unlock();
16632 
16633 	return result;
16634 }
16635 
16636 /**
16637  * bnxt_io_resume - called when traffic can start flowing again.
16638  * @pdev: Pointer to PCI device
16639  *
16640  * This callback is called when the error recovery driver tells
16641  * us that its OK to resume normal operation.
16642  */
16643 static void bnxt_io_resume(struct pci_dev *pdev)
16644 {
16645 	struct net_device *netdev = pci_get_drvdata(pdev);
16646 	struct bnxt *bp = netdev_priv(netdev);
16647 	int err;
16648 
16649 	netdev_info(bp->dev, "PCI Slot Resume\n");
16650 	rtnl_lock();
16651 
16652 	err = bnxt_hwrm_func_qcaps(bp);
16653 	if (!err) {
16654 		if (netif_running(netdev))
16655 			err = bnxt_open(netdev);
16656 		else
16657 			err = bnxt_reserve_rings(bp, true);
16658 	}
16659 
16660 	if (!err)
16661 		netif_device_attach(netdev);
16662 
16663 	rtnl_unlock();
16664 	bnxt_ulp_start(bp, err);
16665 	if (!err)
16666 		bnxt_reenable_sriov(bp);
16667 }
16668 
16669 static const struct pci_error_handlers bnxt_err_handler = {
16670 	.error_detected	= bnxt_io_error_detected,
16671 	.slot_reset	= bnxt_io_slot_reset,
16672 	.resume		= bnxt_io_resume
16673 };
16674 
16675 static struct pci_driver bnxt_pci_driver = {
16676 	.name		= DRV_MODULE_NAME,
16677 	.id_table	= bnxt_pci_tbl,
16678 	.probe		= bnxt_init_one,
16679 	.remove		= bnxt_remove_one,
16680 	.shutdown	= bnxt_shutdown,
16681 	.driver.pm	= BNXT_PM_OPS,
16682 	.err_handler	= &bnxt_err_handler,
16683 #if defined(CONFIG_BNXT_SRIOV)
16684 	.sriov_configure = bnxt_sriov_configure,
16685 #endif
16686 };
16687 
16688 static int __init bnxt_init(void)
16689 {
16690 	int err;
16691 
16692 	bnxt_debug_init();
16693 	err = pci_register_driver(&bnxt_pci_driver);
16694 	if (err) {
16695 		bnxt_debug_exit();
16696 		return err;
16697 	}
16698 
16699 	return 0;
16700 }
16701 
16702 static void __exit bnxt_exit(void)
16703 {
16704 	pci_unregister_driver(&bnxt_pci_driver);
16705 	if (bnxt_pf_wq)
16706 		destroy_workqueue(bnxt_pf_wq);
16707 	bnxt_debug_exit();
16708 }
16709 
16710 module_init(bnxt_init);
16711 module_exit(bnxt_exit);
16712