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