xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 7a5f93ea5862da91488975acaa0c7abd508f192b)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_queues.h>
58 
59 #include "bnxt_hsi.h"
60 #include "bnxt.h"
61 #include "bnxt_hwrm.h"
62 #include "bnxt_ulp.h"
63 #include "bnxt_sriov.h"
64 #include "bnxt_ethtool.h"
65 #include "bnxt_dcb.h"
66 #include "bnxt_xdp.h"
67 #include "bnxt_ptp.h"
68 #include "bnxt_vfr.h"
69 #include "bnxt_tc.h"
70 #include "bnxt_devlink.h"
71 #include "bnxt_debugfs.h"
72 #include "bnxt_coredump.h"
73 #include "bnxt_hwmon.h"
74 
75 #define BNXT_TX_TIMEOUT		(5 * HZ)
76 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
77 				 NETIF_MSG_TX_ERR)
78 
79 MODULE_LICENSE("GPL");
80 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
81 
82 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
83 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
84 #define BNXT_RX_COPY_THRESH 256
85 
86 #define BNXT_TX_PUSH_THRESH 164
87 
88 /* indexed by enum board_idx */
89 static const struct {
90 	char *name;
91 } board_info[] = {
92 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
93 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
94 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
95 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
96 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
97 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
98 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
99 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
100 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
101 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
102 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
103 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
104 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
105 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
106 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
108 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
109 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
110 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
111 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
112 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
113 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
114 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
115 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
116 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
117 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
118 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
119 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
120 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
121 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
122 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
123 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
124 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
125 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
126 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
127 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
128 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
129 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
130 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
131 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
132 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
133 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
134 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
135 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
136 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
137 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
138 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
139 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
140 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
141 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
142 };
143 
144 static const struct pci_device_id bnxt_pci_tbl[] = {
145 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
146 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
147 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
148 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
149 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
150 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
151 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
152 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
153 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
166 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
168 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
169 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
170 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
171 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
172 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
173 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
178 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
179 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
180 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
181 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
182 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
183 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
188 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
189 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
190 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
191 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
193 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
194 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
195 #ifdef CONFIG_BNXT_SRIOV
196 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
197 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
198 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
199 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
200 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
201 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
202 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
206 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
207 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
208 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
209 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
210 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
211 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
212 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
214 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
215 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
216 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
217 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
218 #endif
219 	{ 0 }
220 };
221 
222 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
223 
224 static const u16 bnxt_vf_req_snif[] = {
225 	HWRM_FUNC_CFG,
226 	HWRM_FUNC_VF_CFG,
227 	HWRM_PORT_PHY_QCFG,
228 	HWRM_CFA_L2_FILTER_ALLOC,
229 };
230 
231 static const u16 bnxt_async_events_arr[] = {
232 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
233 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
234 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
235 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
236 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
237 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
238 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
239 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
240 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
241 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
242 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
243 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
244 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
245 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
246 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
247 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
248 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
249 };
250 
251 const u16 bnxt_bstore_to_trace[] = {
252 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
253 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
254 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
255 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
256 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
257 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
258 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
259 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
260 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
261 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
262 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
263 };
264 
265 static struct workqueue_struct *bnxt_pf_wq;
266 
267 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
268 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
269 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
270 
271 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
272 	.ports = {
273 		.src = 0,
274 		.dst = 0,
275 	},
276 	.addrs = {
277 		.v6addrs = {
278 			.src = BNXT_IPV6_MASK_NONE,
279 			.dst = BNXT_IPV6_MASK_NONE,
280 		},
281 	},
282 };
283 
284 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
285 	.ports = {
286 		.src = cpu_to_be16(0xffff),
287 		.dst = cpu_to_be16(0xffff),
288 	},
289 	.addrs = {
290 		.v6addrs = {
291 			.src = BNXT_IPV6_MASK_ALL,
292 			.dst = BNXT_IPV6_MASK_ALL,
293 		},
294 	},
295 };
296 
297 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
298 	.ports = {
299 		.src = cpu_to_be16(0xffff),
300 		.dst = cpu_to_be16(0xffff),
301 	},
302 	.addrs = {
303 		.v4addrs = {
304 			.src = cpu_to_be32(0xffffffff),
305 			.dst = cpu_to_be32(0xffffffff),
306 		},
307 	},
308 };
309 
310 static bool bnxt_vf_pciid(enum board_idx idx)
311 {
312 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
313 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
314 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
315 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF);
316 }
317 
318 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
319 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
320 
321 #define BNXT_DB_CQ(db, idx)						\
322 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
323 
324 #define BNXT_DB_NQ_P5(db, idx)						\
325 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
326 		    (db)->doorbell)
327 
328 #define BNXT_DB_NQ_P7(db, idx)						\
329 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
330 		    DB_RING_IDX(db, idx), (db)->doorbell)
331 
332 #define BNXT_DB_CQ_ARM(db, idx)						\
333 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
334 
335 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
336 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
337 		    DB_RING_IDX(db, idx), (db)->doorbell)
338 
339 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
340 {
341 	if (bp->flags & BNXT_FLAG_CHIP_P7)
342 		BNXT_DB_NQ_P7(db, idx);
343 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
344 		BNXT_DB_NQ_P5(db, idx);
345 	else
346 		BNXT_DB_CQ(db, idx);
347 }
348 
349 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
350 {
351 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
352 		BNXT_DB_NQ_ARM_P5(db, idx);
353 	else
354 		BNXT_DB_CQ_ARM(db, idx);
355 }
356 
357 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
358 {
359 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
360 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
361 			    DB_RING_IDX(db, idx), db->doorbell);
362 	else
363 		BNXT_DB_CQ(db, idx);
364 }
365 
366 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
367 {
368 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
369 		return;
370 
371 	if (BNXT_PF(bp))
372 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
373 	else
374 		schedule_delayed_work(&bp->fw_reset_task, delay);
375 }
376 
377 static void __bnxt_queue_sp_work(struct bnxt *bp)
378 {
379 	if (BNXT_PF(bp))
380 		queue_work(bnxt_pf_wq, &bp->sp_task);
381 	else
382 		schedule_work(&bp->sp_task);
383 }
384 
385 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
386 {
387 	set_bit(event, &bp->sp_event);
388 	__bnxt_queue_sp_work(bp);
389 }
390 
391 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
392 {
393 	if (!rxr->bnapi->in_reset) {
394 		rxr->bnapi->in_reset = true;
395 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
396 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
397 		else
398 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
399 		__bnxt_queue_sp_work(bp);
400 	}
401 	rxr->rx_next_cons = 0xffff;
402 }
403 
404 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
405 			  u16 curr)
406 {
407 	struct bnxt_napi *bnapi = txr->bnapi;
408 
409 	if (bnapi->tx_fault)
410 		return;
411 
412 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
413 		   txr->txq_index, txr->tx_hw_cons,
414 		   txr->tx_cons, txr->tx_prod, curr);
415 	WARN_ON_ONCE(1);
416 	bnapi->tx_fault = 1;
417 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
418 }
419 
420 const u16 bnxt_lhint_arr[] = {
421 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
422 	TX_BD_FLAGS_LHINT_512_TO_1023,
423 	TX_BD_FLAGS_LHINT_1024_TO_2047,
424 	TX_BD_FLAGS_LHINT_1024_TO_2047,
425 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
426 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
427 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
428 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
429 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
430 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
431 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
432 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
433 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
434 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
435 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
436 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
437 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
438 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
439 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
440 };
441 
442 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
443 {
444 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
445 
446 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
447 		return 0;
448 
449 	return md_dst->u.port_info.port_id;
450 }
451 
452 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
453 			     u16 prod)
454 {
455 	/* Sync BD data before updating doorbell */
456 	wmb();
457 	bnxt_db_write(bp, &txr->tx_db, prod);
458 	txr->kick_pending = 0;
459 }
460 
461 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
462 {
463 	struct bnxt *bp = netdev_priv(dev);
464 	struct tx_bd *txbd, *txbd0;
465 	struct tx_bd_ext *txbd1;
466 	struct netdev_queue *txq;
467 	int i;
468 	dma_addr_t mapping;
469 	unsigned int length, pad = 0;
470 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
471 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
472 	struct pci_dev *pdev = bp->pdev;
473 	u16 prod, last_frag, txts_prod;
474 	struct bnxt_tx_ring_info *txr;
475 	struct bnxt_sw_tx_bd *tx_buf;
476 	__le32 lflags = 0;
477 
478 	i = skb_get_queue_mapping(skb);
479 	if (unlikely(i >= bp->tx_nr_rings)) {
480 		dev_kfree_skb_any(skb);
481 		dev_core_stats_tx_dropped_inc(dev);
482 		return NETDEV_TX_OK;
483 	}
484 
485 	txq = netdev_get_tx_queue(dev, i);
486 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
487 	prod = txr->tx_prod;
488 
489 	free_size = bnxt_tx_avail(bp, txr);
490 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
491 		/* We must have raced with NAPI cleanup */
492 		if (net_ratelimit() && txr->kick_pending)
493 			netif_warn(bp, tx_err, dev,
494 				   "bnxt: ring busy w/ flush pending!\n");
495 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
496 					bp->tx_wake_thresh))
497 			return NETDEV_TX_BUSY;
498 	}
499 
500 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
501 		goto tx_free;
502 
503 	length = skb->len;
504 	len = skb_headlen(skb);
505 	last_frag = skb_shinfo(skb)->nr_frags;
506 
507 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
508 
509 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
510 	tx_buf->skb = skb;
511 	tx_buf->nr_frags = last_frag;
512 
513 	vlan_tag_flags = 0;
514 	cfa_action = bnxt_xmit_get_cfa_action(skb);
515 	if (skb_vlan_tag_present(skb)) {
516 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
517 				 skb_vlan_tag_get(skb);
518 		/* Currently supports 8021Q, 8021AD vlan offloads
519 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
520 		 */
521 		if (skb->vlan_proto == htons(ETH_P_8021Q))
522 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
523 	}
524 
525 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
526 	    ptp->tx_tstamp_en) {
527 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
528 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
529 			tx_buf->is_ts_pkt = 1;
530 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
531 		} else if (!skb_is_gso(skb)) {
532 			u16 seq_id, hdr_off;
533 
534 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
535 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
536 				if (vlan_tag_flags)
537 					hdr_off += VLAN_HLEN;
538 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
539 				tx_buf->is_ts_pkt = 1;
540 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
541 
542 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
543 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
544 				tx_buf->txts_prod = txts_prod;
545 			}
546 		}
547 	}
548 	if (unlikely(skb->no_fcs))
549 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
550 
551 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
552 	    !lflags) {
553 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
554 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
555 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
556 		void __iomem *db = txr->tx_db.doorbell;
557 		void *pdata = tx_push_buf->data;
558 		u64 *end;
559 		int j, push_len;
560 
561 		/* Set COAL_NOW to be ready quickly for the next push */
562 		tx_push->tx_bd_len_flags_type =
563 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
564 					TX_BD_TYPE_LONG_TX_BD |
565 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
566 					TX_BD_FLAGS_COAL_NOW |
567 					TX_BD_FLAGS_PACKET_END |
568 					(2 << TX_BD_FLAGS_BD_CNT_SHIFT));
569 
570 		if (skb->ip_summed == CHECKSUM_PARTIAL)
571 			tx_push1->tx_bd_hsize_lflags =
572 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
573 		else
574 			tx_push1->tx_bd_hsize_lflags = 0;
575 
576 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
577 		tx_push1->tx_bd_cfa_action =
578 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
579 
580 		end = pdata + length;
581 		end = PTR_ALIGN(end, 8) - 1;
582 		*end = 0;
583 
584 		skb_copy_from_linear_data(skb, pdata, len);
585 		pdata += len;
586 		for (j = 0; j < last_frag; j++) {
587 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
588 			void *fptr;
589 
590 			fptr = skb_frag_address_safe(frag);
591 			if (!fptr)
592 				goto normal_tx;
593 
594 			memcpy(pdata, fptr, skb_frag_size(frag));
595 			pdata += skb_frag_size(frag);
596 		}
597 
598 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
599 		txbd->tx_bd_haddr = txr->data_mapping;
600 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
601 		prod = NEXT_TX(prod);
602 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
603 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
604 		memcpy(txbd, tx_push1, sizeof(*txbd));
605 		prod = NEXT_TX(prod);
606 		tx_push->doorbell =
607 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
608 				    DB_RING_IDX(&txr->tx_db, prod));
609 		WRITE_ONCE(txr->tx_prod, prod);
610 
611 		tx_buf->is_push = 1;
612 		netdev_tx_sent_queue(txq, skb->len);
613 		wmb();	/* Sync is_push and byte queue before pushing data */
614 
615 		push_len = (length + sizeof(*tx_push) + 7) / 8;
616 		if (push_len > 16) {
617 			__iowrite64_copy(db, tx_push_buf, 16);
618 			__iowrite32_copy(db + 4, tx_push_buf + 1,
619 					 (push_len - 16) << 1);
620 		} else {
621 			__iowrite64_copy(db, tx_push_buf, push_len);
622 		}
623 
624 		goto tx_done;
625 	}
626 
627 normal_tx:
628 	if (length < BNXT_MIN_PKT_SIZE) {
629 		pad = BNXT_MIN_PKT_SIZE - length;
630 		if (skb_pad(skb, pad))
631 			/* SKB already freed. */
632 			goto tx_kick_pending;
633 		length = BNXT_MIN_PKT_SIZE;
634 	}
635 
636 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
637 
638 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
639 		goto tx_free;
640 
641 	dma_unmap_addr_set(tx_buf, mapping, mapping);
642 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
643 		((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
644 
645 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
646 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
647 
648 	prod = NEXT_TX(prod);
649 	txbd1 = (struct tx_bd_ext *)
650 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
651 
652 	txbd1->tx_bd_hsize_lflags = lflags;
653 	if (skb_is_gso(skb)) {
654 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
655 		u32 hdr_len;
656 
657 		if (skb->encapsulation) {
658 			if (udp_gso)
659 				hdr_len = skb_inner_transport_offset(skb) +
660 					  sizeof(struct udphdr);
661 			else
662 				hdr_len = skb_inner_tcp_all_headers(skb);
663 		} else if (udp_gso) {
664 			hdr_len = skb_transport_offset(skb) +
665 				  sizeof(struct udphdr);
666 		} else {
667 			hdr_len = skb_tcp_all_headers(skb);
668 		}
669 
670 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
671 					TX_BD_FLAGS_T_IPID |
672 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
673 		length = skb_shinfo(skb)->gso_size;
674 		txbd1->tx_bd_mss = cpu_to_le32(length);
675 		length += hdr_len;
676 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
677 		txbd1->tx_bd_hsize_lflags |=
678 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
679 		txbd1->tx_bd_mss = 0;
680 	}
681 
682 	length >>= 9;
683 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
684 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
685 				     skb->len);
686 		i = 0;
687 		goto tx_dma_error;
688 	}
689 	flags |= bnxt_lhint_arr[length];
690 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
691 
692 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
693 	txbd1->tx_bd_cfa_action =
694 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
695 	txbd0 = txbd;
696 	for (i = 0; i < last_frag; i++) {
697 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
698 
699 		prod = NEXT_TX(prod);
700 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
701 
702 		len = skb_frag_size(frag);
703 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
704 					   DMA_TO_DEVICE);
705 
706 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
707 			goto tx_dma_error;
708 
709 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
710 		dma_unmap_addr_set(tx_buf, mapping, mapping);
711 
712 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
713 
714 		flags = len << TX_BD_LEN_SHIFT;
715 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
716 	}
717 
718 	flags &= ~TX_BD_LEN;
719 	txbd->tx_bd_len_flags_type =
720 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
721 			    TX_BD_FLAGS_PACKET_END);
722 
723 	netdev_tx_sent_queue(txq, skb->len);
724 
725 	skb_tx_timestamp(skb);
726 
727 	prod = NEXT_TX(prod);
728 	WRITE_ONCE(txr->tx_prod, prod);
729 
730 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
731 		bnxt_txr_db_kick(bp, txr, prod);
732 	} else {
733 		if (free_size >= bp->tx_wake_thresh)
734 			txbd0->tx_bd_len_flags_type |=
735 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
736 		txr->kick_pending = 1;
737 	}
738 
739 tx_done:
740 
741 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
742 		if (netdev_xmit_more() && !tx_buf->is_push) {
743 			txbd0->tx_bd_len_flags_type &=
744 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
745 			bnxt_txr_db_kick(bp, txr, prod);
746 		}
747 
748 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
749 				   bp->tx_wake_thresh);
750 	}
751 	return NETDEV_TX_OK;
752 
753 tx_dma_error:
754 	last_frag = i;
755 
756 	/* start back at beginning and unmap skb */
757 	prod = txr->tx_prod;
758 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
759 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
760 			 skb_headlen(skb), DMA_TO_DEVICE);
761 	prod = NEXT_TX(prod);
762 
763 	/* unmap remaining mapped pages */
764 	for (i = 0; i < last_frag; i++) {
765 		prod = NEXT_TX(prod);
766 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
767 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
768 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
769 			       DMA_TO_DEVICE);
770 	}
771 
772 tx_free:
773 	dev_kfree_skb_any(skb);
774 tx_kick_pending:
775 	if (BNXT_TX_PTP_IS_SET(lflags)) {
776 		txr->tx_buf_ring[txr->tx_prod].is_ts_pkt = 0;
777 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
778 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
779 			/* set SKB to err so PTP worker will clean up */
780 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
781 	}
782 	if (txr->kick_pending)
783 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
784 	txr->tx_buf_ring[txr->tx_prod].skb = NULL;
785 	dev_core_stats_tx_dropped_inc(dev);
786 	return NETDEV_TX_OK;
787 }
788 
789 /* Returns true if some remaining TX packets not processed. */
790 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
791 			  int budget)
792 {
793 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
794 	struct pci_dev *pdev = bp->pdev;
795 	u16 hw_cons = txr->tx_hw_cons;
796 	unsigned int tx_bytes = 0;
797 	u16 cons = txr->tx_cons;
798 	int tx_pkts = 0;
799 	bool rc = false;
800 
801 	while (RING_TX(bp, cons) != hw_cons) {
802 		struct bnxt_sw_tx_bd *tx_buf;
803 		struct sk_buff *skb;
804 		bool is_ts_pkt;
805 		int j, last;
806 
807 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
808 		skb = tx_buf->skb;
809 
810 		if (unlikely(!skb)) {
811 			bnxt_sched_reset_txr(bp, txr, cons);
812 			return rc;
813 		}
814 
815 		is_ts_pkt = tx_buf->is_ts_pkt;
816 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
817 			rc = true;
818 			break;
819 		}
820 
821 		cons = NEXT_TX(cons);
822 		tx_pkts++;
823 		tx_bytes += skb->len;
824 		tx_buf->skb = NULL;
825 		tx_buf->is_ts_pkt = 0;
826 
827 		if (tx_buf->is_push) {
828 			tx_buf->is_push = 0;
829 			goto next_tx_int;
830 		}
831 
832 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
833 				 skb_headlen(skb), DMA_TO_DEVICE);
834 		last = tx_buf->nr_frags;
835 
836 		for (j = 0; j < last; j++) {
837 			cons = NEXT_TX(cons);
838 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
839 			dma_unmap_page(
840 				&pdev->dev,
841 				dma_unmap_addr(tx_buf, mapping),
842 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
843 				DMA_TO_DEVICE);
844 		}
845 		if (unlikely(is_ts_pkt)) {
846 			if (BNXT_CHIP_P5(bp)) {
847 				/* PTP worker takes ownership of the skb */
848 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
849 				skb = NULL;
850 			}
851 		}
852 
853 next_tx_int:
854 		cons = NEXT_TX(cons);
855 
856 		dev_consume_skb_any(skb);
857 	}
858 
859 	WRITE_ONCE(txr->tx_cons, cons);
860 
861 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
862 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
863 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
864 
865 	return rc;
866 }
867 
868 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
869 {
870 	struct bnxt_tx_ring_info *txr;
871 	bool more = false;
872 	int i;
873 
874 	bnxt_for_each_napi_tx(i, bnapi, txr) {
875 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
876 			more |= __bnxt_tx_int(bp, txr, budget);
877 	}
878 	if (!more)
879 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
880 }
881 
882 static bool bnxt_separate_head_pool(void)
883 {
884 	return PAGE_SIZE > BNXT_RX_PAGE_SIZE;
885 }
886 
887 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
888 					 struct bnxt_rx_ring_info *rxr,
889 					 unsigned int *offset,
890 					 gfp_t gfp)
891 {
892 	struct page *page;
893 
894 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
895 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
896 						BNXT_RX_PAGE_SIZE);
897 	} else {
898 		page = page_pool_dev_alloc_pages(rxr->page_pool);
899 		*offset = 0;
900 	}
901 	if (!page)
902 		return NULL;
903 
904 	*mapping = page_pool_get_dma_addr(page) + *offset;
905 	return page;
906 }
907 
908 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
909 				       struct bnxt_rx_ring_info *rxr,
910 				       gfp_t gfp)
911 {
912 	unsigned int offset;
913 	struct page *page;
914 
915 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
916 				    bp->rx_buf_size, gfp);
917 	if (!page)
918 		return NULL;
919 
920 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
921 	return page_address(page) + offset;
922 }
923 
924 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
925 		       u16 prod, gfp_t gfp)
926 {
927 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
928 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
929 	dma_addr_t mapping;
930 
931 	if (BNXT_RX_PAGE_MODE(bp)) {
932 		unsigned int offset;
933 		struct page *page =
934 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
935 
936 		if (!page)
937 			return -ENOMEM;
938 
939 		mapping += bp->rx_dma_offset;
940 		rx_buf->data = page;
941 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
942 	} else {
943 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
944 
945 		if (!data)
946 			return -ENOMEM;
947 
948 		rx_buf->data = data;
949 		rx_buf->data_ptr = data + bp->rx_offset;
950 	}
951 	rx_buf->mapping = mapping;
952 
953 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
954 	return 0;
955 }
956 
957 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
958 {
959 	u16 prod = rxr->rx_prod;
960 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
961 	struct bnxt *bp = rxr->bnapi->bp;
962 	struct rx_bd *cons_bd, *prod_bd;
963 
964 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
965 	cons_rx_buf = &rxr->rx_buf_ring[cons];
966 
967 	prod_rx_buf->data = data;
968 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
969 
970 	prod_rx_buf->mapping = cons_rx_buf->mapping;
971 
972 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
973 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
974 
975 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
976 }
977 
978 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
979 {
980 	u16 next, max = rxr->rx_agg_bmap_size;
981 
982 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
983 	if (next >= max)
984 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
985 	return next;
986 }
987 
988 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
989 				     struct bnxt_rx_ring_info *rxr,
990 				     u16 prod, gfp_t gfp)
991 {
992 	struct rx_bd *rxbd =
993 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
994 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
995 	struct page *page;
996 	dma_addr_t mapping;
997 	u16 sw_prod = rxr->rx_sw_agg_prod;
998 	unsigned int offset = 0;
999 
1000 	page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
1001 
1002 	if (!page)
1003 		return -ENOMEM;
1004 
1005 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1006 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1007 
1008 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1009 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1010 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1011 
1012 	rx_agg_buf->page = page;
1013 	rx_agg_buf->offset = offset;
1014 	rx_agg_buf->mapping = mapping;
1015 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1016 	rxbd->rx_bd_opaque = sw_prod;
1017 	return 0;
1018 }
1019 
1020 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1021 				       struct bnxt_cp_ring_info *cpr,
1022 				       u16 cp_cons, u16 curr)
1023 {
1024 	struct rx_agg_cmp *agg;
1025 
1026 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1027 	agg = (struct rx_agg_cmp *)
1028 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1029 	return agg;
1030 }
1031 
1032 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1033 					      struct bnxt_rx_ring_info *rxr,
1034 					      u16 agg_id, u16 curr)
1035 {
1036 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1037 
1038 	return &tpa_info->agg_arr[curr];
1039 }
1040 
1041 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1042 				   u16 start, u32 agg_bufs, bool tpa)
1043 {
1044 	struct bnxt_napi *bnapi = cpr->bnapi;
1045 	struct bnxt *bp = bnapi->bp;
1046 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1047 	u16 prod = rxr->rx_agg_prod;
1048 	u16 sw_prod = rxr->rx_sw_agg_prod;
1049 	bool p5_tpa = false;
1050 	u32 i;
1051 
1052 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1053 		p5_tpa = true;
1054 
1055 	for (i = 0; i < agg_bufs; i++) {
1056 		u16 cons;
1057 		struct rx_agg_cmp *agg;
1058 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1059 		struct rx_bd *prod_bd;
1060 		struct page *page;
1061 
1062 		if (p5_tpa)
1063 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1064 		else
1065 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1066 		cons = agg->rx_agg_cmp_opaque;
1067 		__clear_bit(cons, rxr->rx_agg_bmap);
1068 
1069 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1070 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1071 
1072 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1073 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1074 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1075 
1076 		/* It is possible for sw_prod to be equal to cons, so
1077 		 * set cons_rx_buf->page to NULL first.
1078 		 */
1079 		page = cons_rx_buf->page;
1080 		cons_rx_buf->page = NULL;
1081 		prod_rx_buf->page = page;
1082 		prod_rx_buf->offset = cons_rx_buf->offset;
1083 
1084 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1085 
1086 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1087 
1088 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1089 		prod_bd->rx_bd_opaque = sw_prod;
1090 
1091 		prod = NEXT_RX_AGG(prod);
1092 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1093 	}
1094 	rxr->rx_agg_prod = prod;
1095 	rxr->rx_sw_agg_prod = sw_prod;
1096 }
1097 
1098 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1099 					      struct bnxt_rx_ring_info *rxr,
1100 					      u16 cons, void *data, u8 *data_ptr,
1101 					      dma_addr_t dma_addr,
1102 					      unsigned int offset_and_len)
1103 {
1104 	unsigned int len = offset_and_len & 0xffff;
1105 	struct page *page = data;
1106 	u16 prod = rxr->rx_prod;
1107 	struct sk_buff *skb;
1108 	int err;
1109 
1110 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1111 	if (unlikely(err)) {
1112 		bnxt_reuse_rx_data(rxr, cons, data);
1113 		return NULL;
1114 	}
1115 	dma_addr -= bp->rx_dma_offset;
1116 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1117 				bp->rx_dir);
1118 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1119 	if (!skb) {
1120 		page_pool_recycle_direct(rxr->page_pool, page);
1121 		return NULL;
1122 	}
1123 	skb_mark_for_recycle(skb);
1124 	skb_reserve(skb, bp->rx_offset);
1125 	__skb_put(skb, len);
1126 
1127 	return skb;
1128 }
1129 
1130 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1131 					struct bnxt_rx_ring_info *rxr,
1132 					u16 cons, void *data, u8 *data_ptr,
1133 					dma_addr_t dma_addr,
1134 					unsigned int offset_and_len)
1135 {
1136 	unsigned int payload = offset_and_len >> 16;
1137 	unsigned int len = offset_and_len & 0xffff;
1138 	skb_frag_t *frag;
1139 	struct page *page = data;
1140 	u16 prod = rxr->rx_prod;
1141 	struct sk_buff *skb;
1142 	int off, err;
1143 
1144 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1145 	if (unlikely(err)) {
1146 		bnxt_reuse_rx_data(rxr, cons, data);
1147 		return NULL;
1148 	}
1149 	dma_addr -= bp->rx_dma_offset;
1150 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1151 				bp->rx_dir);
1152 
1153 	if (unlikely(!payload))
1154 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1155 
1156 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1157 	if (!skb) {
1158 		page_pool_recycle_direct(rxr->page_pool, page);
1159 		return NULL;
1160 	}
1161 
1162 	skb_mark_for_recycle(skb);
1163 	off = (void *)data_ptr - page_address(page);
1164 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1165 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1166 	       payload + NET_IP_ALIGN);
1167 
1168 	frag = &skb_shinfo(skb)->frags[0];
1169 	skb_frag_size_sub(frag, payload);
1170 	skb_frag_off_add(frag, payload);
1171 	skb->data_len -= payload;
1172 	skb->tail += payload;
1173 
1174 	return skb;
1175 }
1176 
1177 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1178 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1179 				   void *data, u8 *data_ptr,
1180 				   dma_addr_t dma_addr,
1181 				   unsigned int offset_and_len)
1182 {
1183 	u16 prod = rxr->rx_prod;
1184 	struct sk_buff *skb;
1185 	int err;
1186 
1187 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1188 	if (unlikely(err)) {
1189 		bnxt_reuse_rx_data(rxr, cons, data);
1190 		return NULL;
1191 	}
1192 
1193 	skb = napi_build_skb(data, bp->rx_buf_size);
1194 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1195 				bp->rx_dir);
1196 	if (!skb) {
1197 		page_pool_free_va(rxr->head_pool, data, true);
1198 		return NULL;
1199 	}
1200 
1201 	skb_mark_for_recycle(skb);
1202 	skb_reserve(skb, bp->rx_offset);
1203 	skb_put(skb, offset_and_len & 0xffff);
1204 	return skb;
1205 }
1206 
1207 static u32 __bnxt_rx_agg_pages(struct bnxt *bp,
1208 			       struct bnxt_cp_ring_info *cpr,
1209 			       struct skb_shared_info *shinfo,
1210 			       u16 idx, u32 agg_bufs, bool tpa,
1211 			       struct xdp_buff *xdp)
1212 {
1213 	struct bnxt_napi *bnapi = cpr->bnapi;
1214 	struct pci_dev *pdev = bp->pdev;
1215 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1216 	u16 prod = rxr->rx_agg_prod;
1217 	u32 i, total_frag_len = 0;
1218 	bool p5_tpa = false;
1219 
1220 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1221 		p5_tpa = true;
1222 
1223 	for (i = 0; i < agg_bufs; i++) {
1224 		skb_frag_t *frag = &shinfo->frags[i];
1225 		u16 cons, frag_len;
1226 		struct rx_agg_cmp *agg;
1227 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1228 		struct page *page;
1229 		dma_addr_t mapping;
1230 
1231 		if (p5_tpa)
1232 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1233 		else
1234 			agg = bnxt_get_agg(bp, cpr, idx, i);
1235 		cons = agg->rx_agg_cmp_opaque;
1236 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1237 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1238 
1239 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1240 		skb_frag_fill_page_desc(frag, cons_rx_buf->page,
1241 					cons_rx_buf->offset, frag_len);
1242 		shinfo->nr_frags = i + 1;
1243 		__clear_bit(cons, rxr->rx_agg_bmap);
1244 
1245 		/* It is possible for bnxt_alloc_rx_page() to allocate
1246 		 * a sw_prod index that equals the cons index, so we
1247 		 * need to clear the cons entry now.
1248 		 */
1249 		mapping = cons_rx_buf->mapping;
1250 		page = cons_rx_buf->page;
1251 		cons_rx_buf->page = NULL;
1252 
1253 		if (xdp && page_is_pfmemalloc(page))
1254 			xdp_buff_set_frag_pfmemalloc(xdp);
1255 
1256 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1257 			--shinfo->nr_frags;
1258 			cons_rx_buf->page = page;
1259 
1260 			/* Update prod since possibly some pages have been
1261 			 * allocated already.
1262 			 */
1263 			rxr->rx_agg_prod = prod;
1264 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1265 			return 0;
1266 		}
1267 
1268 		dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1269 					bp->rx_dir);
1270 
1271 		total_frag_len += frag_len;
1272 		prod = NEXT_RX_AGG(prod);
1273 	}
1274 	rxr->rx_agg_prod = prod;
1275 	return total_frag_len;
1276 }
1277 
1278 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp,
1279 					     struct bnxt_cp_ring_info *cpr,
1280 					     struct sk_buff *skb, u16 idx,
1281 					     u32 agg_bufs, bool tpa)
1282 {
1283 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1284 	u32 total_frag_len = 0;
1285 
1286 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx,
1287 					     agg_bufs, tpa, NULL);
1288 	if (!total_frag_len) {
1289 		skb_mark_for_recycle(skb);
1290 		dev_kfree_skb(skb);
1291 		return NULL;
1292 	}
1293 
1294 	skb->data_len += total_frag_len;
1295 	skb->len += total_frag_len;
1296 	skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs;
1297 	return skb;
1298 }
1299 
1300 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp,
1301 				 struct bnxt_cp_ring_info *cpr,
1302 				 struct xdp_buff *xdp, u16 idx,
1303 				 u32 agg_bufs, bool tpa)
1304 {
1305 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1306 	u32 total_frag_len = 0;
1307 
1308 	if (!xdp_buff_has_frags(xdp))
1309 		shinfo->nr_frags = 0;
1310 
1311 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo,
1312 					     idx, agg_bufs, tpa, xdp);
1313 	if (total_frag_len) {
1314 		xdp_buff_set_frags_flag(xdp);
1315 		shinfo->nr_frags = agg_bufs;
1316 		shinfo->xdp_frags_size = total_frag_len;
1317 	}
1318 	return total_frag_len;
1319 }
1320 
1321 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1322 			       u8 agg_bufs, u32 *raw_cons)
1323 {
1324 	u16 last;
1325 	struct rx_agg_cmp *agg;
1326 
1327 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1328 	last = RING_CMP(*raw_cons);
1329 	agg = (struct rx_agg_cmp *)
1330 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1331 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1332 }
1333 
1334 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1335 				      unsigned int len,
1336 				      dma_addr_t mapping)
1337 {
1338 	struct bnxt *bp = bnapi->bp;
1339 	struct pci_dev *pdev = bp->pdev;
1340 	struct sk_buff *skb;
1341 
1342 	skb = napi_alloc_skb(&bnapi->napi, len);
1343 	if (!skb)
1344 		return NULL;
1345 
1346 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1347 				bp->rx_dir);
1348 
1349 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1350 	       len + NET_IP_ALIGN);
1351 
1352 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1353 				   bp->rx_dir);
1354 
1355 	skb_put(skb, len);
1356 
1357 	return skb;
1358 }
1359 
1360 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1361 				     unsigned int len,
1362 				     dma_addr_t mapping)
1363 {
1364 	return bnxt_copy_data(bnapi, data, len, mapping);
1365 }
1366 
1367 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1368 				     struct xdp_buff *xdp,
1369 				     unsigned int len,
1370 				     dma_addr_t mapping)
1371 {
1372 	unsigned int metasize = 0;
1373 	u8 *data = xdp->data;
1374 	struct sk_buff *skb;
1375 
1376 	len = xdp->data_end - xdp->data_meta;
1377 	metasize = xdp->data - xdp->data_meta;
1378 	data = xdp->data_meta;
1379 
1380 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1381 	if (!skb)
1382 		return skb;
1383 
1384 	if (metasize) {
1385 		skb_metadata_set(skb, metasize);
1386 		__skb_pull(skb, metasize);
1387 	}
1388 
1389 	return skb;
1390 }
1391 
1392 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1393 			   u32 *raw_cons, void *cmp)
1394 {
1395 	struct rx_cmp *rxcmp = cmp;
1396 	u32 tmp_raw_cons = *raw_cons;
1397 	u8 cmp_type, agg_bufs = 0;
1398 
1399 	cmp_type = RX_CMP_TYPE(rxcmp);
1400 
1401 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1402 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1403 			    RX_CMP_AGG_BUFS) >>
1404 			   RX_CMP_AGG_BUFS_SHIFT;
1405 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1406 		struct rx_tpa_end_cmp *tpa_end = cmp;
1407 
1408 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1409 			return 0;
1410 
1411 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1412 	}
1413 
1414 	if (agg_bufs) {
1415 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1416 			return -EBUSY;
1417 	}
1418 	*raw_cons = tmp_raw_cons;
1419 	return 0;
1420 }
1421 
1422 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1423 {
1424 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1425 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1426 
1427 	if (test_bit(idx, map->agg_idx_bmap))
1428 		idx = find_first_zero_bit(map->agg_idx_bmap,
1429 					  BNXT_AGG_IDX_BMAP_SIZE);
1430 	__set_bit(idx, map->agg_idx_bmap);
1431 	map->agg_id_tbl[agg_id] = idx;
1432 	return idx;
1433 }
1434 
1435 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1436 {
1437 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1438 
1439 	__clear_bit(idx, map->agg_idx_bmap);
1440 }
1441 
1442 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1443 {
1444 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1445 
1446 	return map->agg_id_tbl[agg_id];
1447 }
1448 
1449 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1450 			      struct rx_tpa_start_cmp *tpa_start,
1451 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1452 {
1453 	tpa_info->cfa_code_valid = 1;
1454 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1455 	tpa_info->vlan_valid = 0;
1456 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1457 		tpa_info->vlan_valid = 1;
1458 		tpa_info->metadata =
1459 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1460 	}
1461 }
1462 
1463 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1464 				 struct rx_tpa_start_cmp *tpa_start,
1465 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1466 {
1467 	tpa_info->vlan_valid = 0;
1468 	if (TPA_START_VLAN_VALID(tpa_start)) {
1469 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1470 		u32 vlan_proto = ETH_P_8021Q;
1471 
1472 		tpa_info->vlan_valid = 1;
1473 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1474 			vlan_proto = ETH_P_8021AD;
1475 		tpa_info->metadata = vlan_proto << 16 |
1476 				     TPA_START_METADATA0_TCI(tpa_start1);
1477 	}
1478 }
1479 
1480 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1481 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1482 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1483 {
1484 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1485 	struct bnxt_tpa_info *tpa_info;
1486 	u16 cons, prod, agg_id;
1487 	struct rx_bd *prod_bd;
1488 	dma_addr_t mapping;
1489 
1490 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1491 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1492 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1493 	} else {
1494 		agg_id = TPA_START_AGG_ID(tpa_start);
1495 	}
1496 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1497 	prod = rxr->rx_prod;
1498 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1499 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1500 	tpa_info = &rxr->rx_tpa[agg_id];
1501 
1502 	if (unlikely(cons != rxr->rx_next_cons ||
1503 		     TPA_START_ERROR(tpa_start))) {
1504 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1505 			    cons, rxr->rx_next_cons,
1506 			    TPA_START_ERROR_CODE(tpa_start1));
1507 		bnxt_sched_reset_rxr(bp, rxr);
1508 		return;
1509 	}
1510 	prod_rx_buf->data = tpa_info->data;
1511 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1512 
1513 	mapping = tpa_info->mapping;
1514 	prod_rx_buf->mapping = mapping;
1515 
1516 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1517 
1518 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1519 
1520 	tpa_info->data = cons_rx_buf->data;
1521 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1522 	cons_rx_buf->data = NULL;
1523 	tpa_info->mapping = cons_rx_buf->mapping;
1524 
1525 	tpa_info->len =
1526 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1527 				RX_TPA_START_CMP_LEN_SHIFT;
1528 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1529 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1530 		tpa_info->gso_type = SKB_GSO_TCPV4;
1531 		if (TPA_START_IS_IPV6(tpa_start1))
1532 			tpa_info->gso_type = SKB_GSO_TCPV6;
1533 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1534 		else if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP &&
1535 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1536 			tpa_info->gso_type = SKB_GSO_TCPV6;
1537 		tpa_info->rss_hash =
1538 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1539 	} else {
1540 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1541 		tpa_info->gso_type = 0;
1542 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1543 	}
1544 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1545 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1546 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1547 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1548 	else
1549 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1550 	tpa_info->agg_count = 0;
1551 
1552 	rxr->rx_prod = NEXT_RX(prod);
1553 	cons = RING_RX(bp, NEXT_RX(cons));
1554 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1555 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1556 
1557 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1558 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1559 	cons_rx_buf->data = NULL;
1560 }
1561 
1562 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1563 {
1564 	if (agg_bufs)
1565 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1566 }
1567 
1568 #ifdef CONFIG_INET
1569 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1570 {
1571 	struct udphdr *uh = NULL;
1572 
1573 	if (ip_proto == htons(ETH_P_IP)) {
1574 		struct iphdr *iph = (struct iphdr *)skb->data;
1575 
1576 		if (iph->protocol == IPPROTO_UDP)
1577 			uh = (struct udphdr *)(iph + 1);
1578 	} else {
1579 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1580 
1581 		if (iph->nexthdr == IPPROTO_UDP)
1582 			uh = (struct udphdr *)(iph + 1);
1583 	}
1584 	if (uh) {
1585 		if (uh->check)
1586 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1587 		else
1588 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1589 	}
1590 }
1591 #endif
1592 
1593 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1594 					   int payload_off, int tcp_ts,
1595 					   struct sk_buff *skb)
1596 {
1597 #ifdef CONFIG_INET
1598 	struct tcphdr *th;
1599 	int len, nw_off;
1600 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1601 	u32 hdr_info = tpa_info->hdr_info;
1602 	bool loopback = false;
1603 
1604 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1605 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1606 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1607 
1608 	/* If the packet is an internal loopback packet, the offsets will
1609 	 * have an extra 4 bytes.
1610 	 */
1611 	if (inner_mac_off == 4) {
1612 		loopback = true;
1613 	} else if (inner_mac_off > 4) {
1614 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1615 					    ETH_HLEN - 2));
1616 
1617 		/* We only support inner iPv4/ipv6.  If we don't see the
1618 		 * correct protocol ID, it must be a loopback packet where
1619 		 * the offsets are off by 4.
1620 		 */
1621 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1622 			loopback = true;
1623 	}
1624 	if (loopback) {
1625 		/* internal loopback packet, subtract all offsets by 4 */
1626 		inner_ip_off -= 4;
1627 		inner_mac_off -= 4;
1628 		outer_ip_off -= 4;
1629 	}
1630 
1631 	nw_off = inner_ip_off - ETH_HLEN;
1632 	skb_set_network_header(skb, nw_off);
1633 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1634 		struct ipv6hdr *iph = ipv6_hdr(skb);
1635 
1636 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1637 		len = skb->len - skb_transport_offset(skb);
1638 		th = tcp_hdr(skb);
1639 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1640 	} else {
1641 		struct iphdr *iph = ip_hdr(skb);
1642 
1643 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1644 		len = skb->len - skb_transport_offset(skb);
1645 		th = tcp_hdr(skb);
1646 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1647 	}
1648 
1649 	if (inner_mac_off) { /* tunnel */
1650 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1651 					    ETH_HLEN - 2));
1652 
1653 		bnxt_gro_tunnel(skb, proto);
1654 	}
1655 #endif
1656 	return skb;
1657 }
1658 
1659 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1660 					   int payload_off, int tcp_ts,
1661 					   struct sk_buff *skb)
1662 {
1663 #ifdef CONFIG_INET
1664 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1665 	u32 hdr_info = tpa_info->hdr_info;
1666 	int iphdr_len, nw_off;
1667 
1668 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1669 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1670 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1671 
1672 	nw_off = inner_ip_off - ETH_HLEN;
1673 	skb_set_network_header(skb, nw_off);
1674 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1675 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1676 	skb_set_transport_header(skb, nw_off + iphdr_len);
1677 
1678 	if (inner_mac_off) { /* tunnel */
1679 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1680 					    ETH_HLEN - 2));
1681 
1682 		bnxt_gro_tunnel(skb, proto);
1683 	}
1684 #endif
1685 	return skb;
1686 }
1687 
1688 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1689 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1690 
1691 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1692 					   int payload_off, int tcp_ts,
1693 					   struct sk_buff *skb)
1694 {
1695 #ifdef CONFIG_INET
1696 	struct tcphdr *th;
1697 	int len, nw_off, tcp_opt_len = 0;
1698 
1699 	if (tcp_ts)
1700 		tcp_opt_len = 12;
1701 
1702 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1703 		struct iphdr *iph;
1704 
1705 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1706 			 ETH_HLEN;
1707 		skb_set_network_header(skb, nw_off);
1708 		iph = ip_hdr(skb);
1709 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1710 		len = skb->len - skb_transport_offset(skb);
1711 		th = tcp_hdr(skb);
1712 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1713 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1714 		struct ipv6hdr *iph;
1715 
1716 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1717 			 ETH_HLEN;
1718 		skb_set_network_header(skb, nw_off);
1719 		iph = ipv6_hdr(skb);
1720 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1721 		len = skb->len - skb_transport_offset(skb);
1722 		th = tcp_hdr(skb);
1723 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1724 	} else {
1725 		dev_kfree_skb_any(skb);
1726 		return NULL;
1727 	}
1728 
1729 	if (nw_off) /* tunnel */
1730 		bnxt_gro_tunnel(skb, skb->protocol);
1731 #endif
1732 	return skb;
1733 }
1734 
1735 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1736 					   struct bnxt_tpa_info *tpa_info,
1737 					   struct rx_tpa_end_cmp *tpa_end,
1738 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1739 					   struct sk_buff *skb)
1740 {
1741 #ifdef CONFIG_INET
1742 	int payload_off;
1743 	u16 segs;
1744 
1745 	segs = TPA_END_TPA_SEGS(tpa_end);
1746 	if (segs == 1)
1747 		return skb;
1748 
1749 	NAPI_GRO_CB(skb)->count = segs;
1750 	skb_shinfo(skb)->gso_size =
1751 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1752 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1753 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1754 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1755 	else
1756 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1757 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1758 	if (likely(skb))
1759 		tcp_gro_complete(skb);
1760 #endif
1761 	return skb;
1762 }
1763 
1764 /* Given the cfa_code of a received packet determine which
1765  * netdev (vf-rep or PF) the packet is destined to.
1766  */
1767 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1768 {
1769 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1770 
1771 	/* if vf-rep dev is NULL, the must belongs to the PF */
1772 	return dev ? dev : bp->dev;
1773 }
1774 
1775 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1776 					   struct bnxt_cp_ring_info *cpr,
1777 					   u32 *raw_cons,
1778 					   struct rx_tpa_end_cmp *tpa_end,
1779 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1780 					   u8 *event)
1781 {
1782 	struct bnxt_napi *bnapi = cpr->bnapi;
1783 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1784 	struct net_device *dev = bp->dev;
1785 	u8 *data_ptr, agg_bufs;
1786 	unsigned int len;
1787 	struct bnxt_tpa_info *tpa_info;
1788 	dma_addr_t mapping;
1789 	struct sk_buff *skb;
1790 	u16 idx = 0, agg_id;
1791 	void *data;
1792 	bool gro;
1793 
1794 	if (unlikely(bnapi->in_reset)) {
1795 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1796 
1797 		if (rc < 0)
1798 			return ERR_PTR(-EBUSY);
1799 		return NULL;
1800 	}
1801 
1802 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1803 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1804 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1805 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1806 		tpa_info = &rxr->rx_tpa[agg_id];
1807 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1808 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1809 				    agg_bufs, tpa_info->agg_count);
1810 			agg_bufs = tpa_info->agg_count;
1811 		}
1812 		tpa_info->agg_count = 0;
1813 		*event |= BNXT_AGG_EVENT;
1814 		bnxt_free_agg_idx(rxr, agg_id);
1815 		idx = agg_id;
1816 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1817 	} else {
1818 		agg_id = TPA_END_AGG_ID(tpa_end);
1819 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1820 		tpa_info = &rxr->rx_tpa[agg_id];
1821 		idx = RING_CMP(*raw_cons);
1822 		if (agg_bufs) {
1823 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1824 				return ERR_PTR(-EBUSY);
1825 
1826 			*event |= BNXT_AGG_EVENT;
1827 			idx = NEXT_CMP(idx);
1828 		}
1829 		gro = !!TPA_END_GRO(tpa_end);
1830 	}
1831 	data = tpa_info->data;
1832 	data_ptr = tpa_info->data_ptr;
1833 	prefetch(data_ptr);
1834 	len = tpa_info->len;
1835 	mapping = tpa_info->mapping;
1836 
1837 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1838 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1839 		if (agg_bufs > MAX_SKB_FRAGS)
1840 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1841 				    agg_bufs, (int)MAX_SKB_FRAGS);
1842 		return NULL;
1843 	}
1844 
1845 	if (len <= bp->rx_copy_thresh) {
1846 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1847 		if (!skb) {
1848 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1849 			cpr->sw_stats->rx.rx_oom_discards += 1;
1850 			return NULL;
1851 		}
1852 	} else {
1853 		u8 *new_data;
1854 		dma_addr_t new_mapping;
1855 
1856 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1857 						GFP_ATOMIC);
1858 		if (!new_data) {
1859 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1860 			cpr->sw_stats->rx.rx_oom_discards += 1;
1861 			return NULL;
1862 		}
1863 
1864 		tpa_info->data = new_data;
1865 		tpa_info->data_ptr = new_data + bp->rx_offset;
1866 		tpa_info->mapping = new_mapping;
1867 
1868 		skb = napi_build_skb(data, bp->rx_buf_size);
1869 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1870 					bp->rx_buf_use_size, bp->rx_dir);
1871 
1872 		if (!skb) {
1873 			page_pool_free_va(rxr->head_pool, data, true);
1874 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1875 			cpr->sw_stats->rx.rx_oom_discards += 1;
1876 			return NULL;
1877 		}
1878 		skb_mark_for_recycle(skb);
1879 		skb_reserve(skb, bp->rx_offset);
1880 		skb_put(skb, len);
1881 	}
1882 
1883 	if (agg_bufs) {
1884 		skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true);
1885 		if (!skb) {
1886 			/* Page reuse already handled by bnxt_rx_pages(). */
1887 			cpr->sw_stats->rx.rx_oom_discards += 1;
1888 			return NULL;
1889 		}
1890 	}
1891 
1892 	if (tpa_info->cfa_code_valid)
1893 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1894 	skb->protocol = eth_type_trans(skb, dev);
1895 
1896 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1897 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1898 
1899 	if (tpa_info->vlan_valid &&
1900 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1901 		__be16 vlan_proto = htons(tpa_info->metadata >>
1902 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1903 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1904 
1905 		if (eth_type_vlan(vlan_proto)) {
1906 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1907 		} else {
1908 			dev_kfree_skb(skb);
1909 			return NULL;
1910 		}
1911 	}
1912 
1913 	skb_checksum_none_assert(skb);
1914 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1915 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1916 		skb->csum_level =
1917 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1918 	}
1919 
1920 	if (gro)
1921 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1922 
1923 	return skb;
1924 }
1925 
1926 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1927 			 struct rx_agg_cmp *rx_agg)
1928 {
1929 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1930 	struct bnxt_tpa_info *tpa_info;
1931 
1932 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1933 	tpa_info = &rxr->rx_tpa[agg_id];
1934 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1935 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1936 }
1937 
1938 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1939 			     struct sk_buff *skb)
1940 {
1941 	skb_mark_for_recycle(skb);
1942 
1943 	if (skb->dev != bp->dev) {
1944 		/* this packet belongs to a vf-rep */
1945 		bnxt_vf_rep_rx(bp, skb);
1946 		return;
1947 	}
1948 	skb_record_rx_queue(skb, bnapi->index);
1949 	napi_gro_receive(&bnapi->napi, skb);
1950 }
1951 
1952 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
1953 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
1954 {
1955 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1956 
1957 	if (BNXT_PTP_RX_TS_VALID(flags))
1958 		goto ts_valid;
1959 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
1960 		return false;
1961 
1962 ts_valid:
1963 	*cmpl_ts = ts;
1964 	return true;
1965 }
1966 
1967 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
1968 				    struct rx_cmp *rxcmp,
1969 				    struct rx_cmp_ext *rxcmp1)
1970 {
1971 	__be16 vlan_proto;
1972 	u16 vtag;
1973 
1974 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1975 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
1976 		u32 meta_data;
1977 
1978 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
1979 			return skb;
1980 
1981 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1982 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1983 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
1984 		if (eth_type_vlan(vlan_proto))
1985 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1986 		else
1987 			goto vlan_err;
1988 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
1989 		if (RX_CMP_VLAN_VALID(rxcmp)) {
1990 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
1991 
1992 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
1993 				vlan_proto = htons(ETH_P_8021Q);
1994 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
1995 				vlan_proto = htons(ETH_P_8021AD);
1996 			else
1997 				goto vlan_err;
1998 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
1999 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2000 		}
2001 	}
2002 	return skb;
2003 vlan_err:
2004 	dev_kfree_skb(skb);
2005 	return NULL;
2006 }
2007 
2008 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
2009 					   struct rx_cmp *rxcmp)
2010 {
2011 	u8 ext_op;
2012 
2013 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
2014 	switch (ext_op) {
2015 	case EXT_OP_INNER_4:
2016 	case EXT_OP_OUTER_4:
2017 	case EXT_OP_INNFL_3:
2018 	case EXT_OP_OUTFL_3:
2019 		return PKT_HASH_TYPE_L4;
2020 	default:
2021 		return PKT_HASH_TYPE_L3;
2022 	}
2023 }
2024 
2025 /* returns the following:
2026  * 1       - 1 packet successfully received
2027  * 0       - successful TPA_START, packet not completed yet
2028  * -EBUSY  - completion ring does not have all the agg buffers yet
2029  * -ENOMEM - packet aborted due to out of memory
2030  * -EIO    - packet aborted due to hw error indicated in BD
2031  */
2032 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2033 		       u32 *raw_cons, u8 *event)
2034 {
2035 	struct bnxt_napi *bnapi = cpr->bnapi;
2036 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2037 	struct net_device *dev = bp->dev;
2038 	struct rx_cmp *rxcmp;
2039 	struct rx_cmp_ext *rxcmp1;
2040 	u32 tmp_raw_cons = *raw_cons;
2041 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2042 	struct bnxt_sw_rx_bd *rx_buf;
2043 	unsigned int len;
2044 	u8 *data_ptr, agg_bufs, cmp_type;
2045 	bool xdp_active = false;
2046 	dma_addr_t dma_addr;
2047 	struct sk_buff *skb;
2048 	struct xdp_buff xdp;
2049 	u32 flags, misc;
2050 	u32 cmpl_ts;
2051 	void *data;
2052 	int rc = 0;
2053 
2054 	rxcmp = (struct rx_cmp *)
2055 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2056 
2057 	cmp_type = RX_CMP_TYPE(rxcmp);
2058 
2059 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2060 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2061 		goto next_rx_no_prod_no_len;
2062 	}
2063 
2064 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2065 	cp_cons = RING_CMP(tmp_raw_cons);
2066 	rxcmp1 = (struct rx_cmp_ext *)
2067 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2068 
2069 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2070 		return -EBUSY;
2071 
2072 	/* The valid test of the entry must be done first before
2073 	 * reading any further.
2074 	 */
2075 	dma_rmb();
2076 	prod = rxr->rx_prod;
2077 
2078 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2079 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2080 		bnxt_tpa_start(bp, rxr, cmp_type,
2081 			       (struct rx_tpa_start_cmp *)rxcmp,
2082 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2083 
2084 		*event |= BNXT_RX_EVENT;
2085 		goto next_rx_no_prod_no_len;
2086 
2087 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2088 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2089 				   (struct rx_tpa_end_cmp *)rxcmp,
2090 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2091 
2092 		if (IS_ERR(skb))
2093 			return -EBUSY;
2094 
2095 		rc = -ENOMEM;
2096 		if (likely(skb)) {
2097 			bnxt_deliver_skb(bp, bnapi, skb);
2098 			rc = 1;
2099 		}
2100 		*event |= BNXT_RX_EVENT;
2101 		goto next_rx_no_prod_no_len;
2102 	}
2103 
2104 	cons = rxcmp->rx_cmp_opaque;
2105 	if (unlikely(cons != rxr->rx_next_cons)) {
2106 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2107 
2108 		/* 0xffff is forced error, don't print it */
2109 		if (rxr->rx_next_cons != 0xffff)
2110 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2111 				    cons, rxr->rx_next_cons);
2112 		bnxt_sched_reset_rxr(bp, rxr);
2113 		if (rc1)
2114 			return rc1;
2115 		goto next_rx_no_prod_no_len;
2116 	}
2117 	rx_buf = &rxr->rx_buf_ring[cons];
2118 	data = rx_buf->data;
2119 	data_ptr = rx_buf->data_ptr;
2120 	prefetch(data_ptr);
2121 
2122 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2123 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2124 
2125 	if (agg_bufs) {
2126 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2127 			return -EBUSY;
2128 
2129 		cp_cons = NEXT_CMP(cp_cons);
2130 		*event |= BNXT_AGG_EVENT;
2131 	}
2132 	*event |= BNXT_RX_EVENT;
2133 
2134 	rx_buf->data = NULL;
2135 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2136 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2137 
2138 		bnxt_reuse_rx_data(rxr, cons, data);
2139 		if (agg_bufs)
2140 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2141 					       false);
2142 
2143 		rc = -EIO;
2144 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2145 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2146 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2147 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2148 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2149 						 rx_err);
2150 				bnxt_sched_reset_rxr(bp, rxr);
2151 			}
2152 		}
2153 		goto next_rx_no_len;
2154 	}
2155 
2156 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2157 	len = flags >> RX_CMP_LEN_SHIFT;
2158 	dma_addr = rx_buf->mapping;
2159 
2160 	if (bnxt_xdp_attached(bp, rxr)) {
2161 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2162 		if (agg_bufs) {
2163 			u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
2164 							     cp_cons, agg_bufs,
2165 							     false);
2166 			if (!frag_len)
2167 				goto oom_next_rx;
2168 		}
2169 		xdp_active = true;
2170 	}
2171 
2172 	if (xdp_active) {
2173 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2174 			rc = 1;
2175 			goto next_rx;
2176 		}
2177 	}
2178 
2179 	if (len <= bp->rx_copy_thresh) {
2180 		if (!xdp_active)
2181 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2182 		else
2183 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2184 		bnxt_reuse_rx_data(rxr, cons, data);
2185 		if (!skb) {
2186 			if (agg_bufs) {
2187 				if (!xdp_active)
2188 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2189 							       agg_bufs, false);
2190 				else
2191 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2192 			}
2193 			goto oom_next_rx;
2194 		}
2195 	} else {
2196 		u32 payload;
2197 
2198 		if (rx_buf->data_ptr == data_ptr)
2199 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2200 		else
2201 			payload = 0;
2202 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2203 				      payload | len);
2204 		if (!skb)
2205 			goto oom_next_rx;
2206 	}
2207 
2208 	if (agg_bufs) {
2209 		if (!xdp_active) {
2210 			skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
2211 			if (!skb)
2212 				goto oom_next_rx;
2213 		} else {
2214 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1);
2215 			if (!skb) {
2216 				/* we should be able to free the old skb here */
2217 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2218 				goto oom_next_rx;
2219 			}
2220 		}
2221 	}
2222 
2223 	if (RX_CMP_HASH_VALID(rxcmp)) {
2224 		enum pkt_hash_types type;
2225 
2226 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2227 			type = bnxt_rss_ext_op(bp, rxcmp);
2228 		} else {
2229 			u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
2230 
2231 			/* RSS profiles 1 and 3 with extract code 0 for inner
2232 			 * 4-tuple
2233 			 */
2234 			if (hash_type != 1 && hash_type != 3)
2235 				type = PKT_HASH_TYPE_L3;
2236 			else
2237 				type = PKT_HASH_TYPE_L4;
2238 		}
2239 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2240 	}
2241 
2242 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2243 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2244 	skb->protocol = eth_type_trans(skb, dev);
2245 
2246 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2247 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2248 		if (!skb)
2249 			goto next_rx;
2250 	}
2251 
2252 	skb_checksum_none_assert(skb);
2253 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2254 		if (dev->features & NETIF_F_RXCSUM) {
2255 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2256 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2257 		}
2258 	} else {
2259 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2260 			if (dev->features & NETIF_F_RXCSUM)
2261 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2262 		}
2263 	}
2264 
2265 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2266 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2267 			u64 ns, ts;
2268 
2269 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2270 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2271 
2272 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2273 				memset(skb_hwtstamps(skb), 0,
2274 				       sizeof(*skb_hwtstamps(skb)));
2275 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2276 			}
2277 		}
2278 	}
2279 	bnxt_deliver_skb(bp, bnapi, skb);
2280 	rc = 1;
2281 
2282 next_rx:
2283 	cpr->rx_packets += 1;
2284 	cpr->rx_bytes += len;
2285 
2286 next_rx_no_len:
2287 	rxr->rx_prod = NEXT_RX(prod);
2288 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2289 
2290 next_rx_no_prod_no_len:
2291 	*raw_cons = tmp_raw_cons;
2292 
2293 	return rc;
2294 
2295 oom_next_rx:
2296 	cpr->sw_stats->rx.rx_oom_discards += 1;
2297 	rc = -ENOMEM;
2298 	goto next_rx;
2299 }
2300 
2301 /* In netpoll mode, if we are using a combined completion ring, we need to
2302  * discard the rx packets and recycle the buffers.
2303  */
2304 static int bnxt_force_rx_discard(struct bnxt *bp,
2305 				 struct bnxt_cp_ring_info *cpr,
2306 				 u32 *raw_cons, u8 *event)
2307 {
2308 	u32 tmp_raw_cons = *raw_cons;
2309 	struct rx_cmp_ext *rxcmp1;
2310 	struct rx_cmp *rxcmp;
2311 	u16 cp_cons;
2312 	u8 cmp_type;
2313 	int rc;
2314 
2315 	cp_cons = RING_CMP(tmp_raw_cons);
2316 	rxcmp = (struct rx_cmp *)
2317 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2318 
2319 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2320 	cp_cons = RING_CMP(tmp_raw_cons);
2321 	rxcmp1 = (struct rx_cmp_ext *)
2322 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2323 
2324 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2325 		return -EBUSY;
2326 
2327 	/* The valid test of the entry must be done first before
2328 	 * reading any further.
2329 	 */
2330 	dma_rmb();
2331 	cmp_type = RX_CMP_TYPE(rxcmp);
2332 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2333 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2334 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2335 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2336 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2337 		struct rx_tpa_end_cmp_ext *tpa_end1;
2338 
2339 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2340 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2341 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2342 	}
2343 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2344 	if (rc && rc != -EBUSY)
2345 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2346 	return rc;
2347 }
2348 
2349 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2350 {
2351 	struct bnxt_fw_health *fw_health = bp->fw_health;
2352 	u32 reg = fw_health->regs[reg_idx];
2353 	u32 reg_type, reg_off, val = 0;
2354 
2355 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2356 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2357 	switch (reg_type) {
2358 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2359 		pci_read_config_dword(bp->pdev, reg_off, &val);
2360 		break;
2361 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2362 		reg_off = fw_health->mapped_regs[reg_idx];
2363 		fallthrough;
2364 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2365 		val = readl(bp->bar0 + reg_off);
2366 		break;
2367 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2368 		val = readl(bp->bar1 + reg_off);
2369 		break;
2370 	}
2371 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2372 		val &= fw_health->fw_reset_inprog_reg_mask;
2373 	return val;
2374 }
2375 
2376 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2377 {
2378 	int i;
2379 
2380 	for (i = 0; i < bp->rx_nr_rings; i++) {
2381 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2382 		struct bnxt_ring_grp_info *grp_info;
2383 
2384 		grp_info = &bp->grp_info[grp_idx];
2385 		if (grp_info->agg_fw_ring_id == ring_id)
2386 			return grp_idx;
2387 	}
2388 	return INVALID_HW_RING_ID;
2389 }
2390 
2391 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2392 {
2393 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2394 
2395 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2396 		return link_info->force_link_speed2;
2397 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2398 		return link_info->force_pam4_link_speed;
2399 	return link_info->force_link_speed;
2400 }
2401 
2402 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2403 {
2404 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2405 
2406 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2407 		link_info->req_link_speed = link_info->force_link_speed2;
2408 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2409 		switch (link_info->req_link_speed) {
2410 		case BNXT_LINK_SPEED_50GB_PAM4:
2411 		case BNXT_LINK_SPEED_100GB_PAM4:
2412 		case BNXT_LINK_SPEED_200GB_PAM4:
2413 		case BNXT_LINK_SPEED_400GB_PAM4:
2414 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2415 			break;
2416 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2417 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2418 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2419 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2420 			break;
2421 		default:
2422 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2423 		}
2424 		return;
2425 	}
2426 	link_info->req_link_speed = link_info->force_link_speed;
2427 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2428 	if (link_info->force_pam4_link_speed) {
2429 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2430 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2431 	}
2432 }
2433 
2434 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2435 {
2436 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2437 
2438 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2439 		link_info->advertising = link_info->auto_link_speeds2;
2440 		return;
2441 	}
2442 	link_info->advertising = link_info->auto_link_speeds;
2443 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2444 }
2445 
2446 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2447 {
2448 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2449 
2450 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2451 		if (link_info->req_link_speed != link_info->force_link_speed2)
2452 			return true;
2453 		return false;
2454 	}
2455 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2456 	    link_info->req_link_speed != link_info->force_link_speed)
2457 		return true;
2458 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2459 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2460 		return true;
2461 	return false;
2462 }
2463 
2464 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2465 {
2466 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2467 
2468 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2469 		if (link_info->advertising != link_info->auto_link_speeds2)
2470 			return true;
2471 		return false;
2472 	}
2473 	if (link_info->advertising != link_info->auto_link_speeds ||
2474 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2475 		return true;
2476 	return false;
2477 }
2478 
2479 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2480 {
2481 	u32 flags = bp->ctx->ctx_arr[type].flags;
2482 
2483 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2484 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2485 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2486 }
2487 
2488 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2489 {
2490 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2491 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2492 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2493 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2494 	struct bnxt_bs_trace_info *bs_trace;
2495 	int last_pg;
2496 
2497 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2498 		return;
2499 
2500 	mem_size = ctxm->max_entries * ctxm->entry_size;
2501 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2502 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2503 
2504 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2505 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2506 
2507 	rmem = &ctx_pg[0].ring_mem;
2508 	bs_trace = &bp->bs_trace[trace_type];
2509 	bs_trace->ctx_type = ctxm->type;
2510 	bs_trace->trace_type = trace_type;
2511 	if (pages > MAX_CTX_PAGES) {
2512 		int last_pg_dir = rmem->nr_pages - 1;
2513 
2514 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2515 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2516 	} else {
2517 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2518 	}
2519 	bs_trace->magic_byte += magic_byte_offset;
2520 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2521 }
2522 
2523 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2524 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2525 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2526 
2527 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2528 	(((data2) &							\
2529 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2530 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2531 
2532 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2533 	((data2) &							\
2534 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2535 
2536 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2537 	(((data2) &							\
2538 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2539 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2540 
2541 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2542 	((data1) &							\
2543 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2544 
2545 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2546 	(((data1) &							\
2547 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2548 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2549 
2550 /* Return true if the workqueue has to be scheduled */
2551 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2552 {
2553 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2554 
2555 	switch (err_type) {
2556 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2557 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2558 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2559 		break;
2560 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2561 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2562 		break;
2563 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2564 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2565 		break;
2566 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2567 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2568 		char *threshold_type;
2569 		bool notify = false;
2570 		char *dir_str;
2571 
2572 		switch (type) {
2573 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2574 			threshold_type = "warning";
2575 			break;
2576 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2577 			threshold_type = "critical";
2578 			break;
2579 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2580 			threshold_type = "fatal";
2581 			break;
2582 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2583 			threshold_type = "shutdown";
2584 			break;
2585 		default:
2586 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2587 			return false;
2588 		}
2589 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2590 			dir_str = "above";
2591 			notify = true;
2592 		} else {
2593 			dir_str = "below";
2594 		}
2595 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2596 			    dir_str, threshold_type);
2597 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2598 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2599 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2600 		if (notify) {
2601 			bp->thermal_threshold_type = type;
2602 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2603 			return true;
2604 		}
2605 		return false;
2606 	}
2607 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2608 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2609 		break;
2610 	default:
2611 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2612 			   err_type);
2613 		break;
2614 	}
2615 	return false;
2616 }
2617 
2618 #define BNXT_GET_EVENT_PORT(data)	\
2619 	((data) &			\
2620 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2621 
2622 #define BNXT_EVENT_RING_TYPE(data2)	\
2623 	((data2) &			\
2624 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2625 
2626 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2627 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2628 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2629 
2630 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2631 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2632 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2633 
2634 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2635 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2636 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2637 
2638 #define BNXT_PHC_BITS	48
2639 
2640 static int bnxt_async_event_process(struct bnxt *bp,
2641 				    struct hwrm_async_event_cmpl *cmpl)
2642 {
2643 	u16 event_id = le16_to_cpu(cmpl->event_id);
2644 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2645 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2646 
2647 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2648 		   event_id, data1, data2);
2649 
2650 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2651 	switch (event_id) {
2652 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2653 		struct bnxt_link_info *link_info = &bp->link_info;
2654 
2655 		if (BNXT_VF(bp))
2656 			goto async_event_process_exit;
2657 
2658 		/* print unsupported speed warning in forced speed mode only */
2659 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2660 		    (data1 & 0x20000)) {
2661 			u16 fw_speed = bnxt_get_force_speed(link_info);
2662 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2663 
2664 			if (speed != SPEED_UNKNOWN)
2665 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2666 					    speed);
2667 		}
2668 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2669 	}
2670 		fallthrough;
2671 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2672 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2673 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2674 		fallthrough;
2675 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2676 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2677 		break;
2678 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2679 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2680 		break;
2681 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2682 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2683 
2684 		if (BNXT_VF(bp))
2685 			break;
2686 
2687 		if (bp->pf.port_id != port_id)
2688 			break;
2689 
2690 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2691 		break;
2692 	}
2693 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2694 		if (BNXT_PF(bp))
2695 			goto async_event_process_exit;
2696 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2697 		break;
2698 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2699 		char *type_str = "Solicited";
2700 
2701 		if (!bp->fw_health)
2702 			goto async_event_process_exit;
2703 
2704 		bp->fw_reset_timestamp = jiffies;
2705 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2706 		if (!bp->fw_reset_min_dsecs)
2707 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2708 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2709 		if (!bp->fw_reset_max_dsecs)
2710 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2711 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2712 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2713 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2714 			type_str = "Fatal";
2715 			bp->fw_health->fatalities++;
2716 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2717 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2718 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2719 			type_str = "Non-fatal";
2720 			bp->fw_health->survivals++;
2721 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2722 		}
2723 		netif_warn(bp, hw, bp->dev,
2724 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2725 			   type_str, data1, data2,
2726 			   bp->fw_reset_min_dsecs * 100,
2727 			   bp->fw_reset_max_dsecs * 100);
2728 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2729 		break;
2730 	}
2731 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2732 		struct bnxt_fw_health *fw_health = bp->fw_health;
2733 		char *status_desc = "healthy";
2734 		u32 status;
2735 
2736 		if (!fw_health)
2737 			goto async_event_process_exit;
2738 
2739 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2740 			fw_health->enabled = false;
2741 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2742 			break;
2743 		}
2744 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2745 		fw_health->tmr_multiplier =
2746 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2747 				     bp->current_interval * 10);
2748 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2749 		if (!fw_health->enabled)
2750 			fw_health->last_fw_heartbeat =
2751 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2752 		fw_health->last_fw_reset_cnt =
2753 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2754 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2755 		if (status != BNXT_FW_STATUS_HEALTHY)
2756 			status_desc = "unhealthy";
2757 		netif_info(bp, drv, bp->dev,
2758 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2759 			   fw_health->primary ? "primary" : "backup", status,
2760 			   status_desc, fw_health->last_fw_reset_cnt);
2761 		if (!fw_health->enabled) {
2762 			/* Make sure tmr_counter is set and visible to
2763 			 * bnxt_health_check() before setting enabled to true.
2764 			 */
2765 			smp_wmb();
2766 			fw_health->enabled = true;
2767 		}
2768 		goto async_event_process_exit;
2769 	}
2770 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2771 		netif_notice(bp, hw, bp->dev,
2772 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2773 			     data1, data2);
2774 		goto async_event_process_exit;
2775 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2776 		struct bnxt_rx_ring_info *rxr;
2777 		u16 grp_idx;
2778 
2779 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2780 			goto async_event_process_exit;
2781 
2782 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2783 			    BNXT_EVENT_RING_TYPE(data2), data1);
2784 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2785 			goto async_event_process_exit;
2786 
2787 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2788 		if (grp_idx == INVALID_HW_RING_ID) {
2789 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2790 				    data1);
2791 			goto async_event_process_exit;
2792 		}
2793 		rxr = bp->bnapi[grp_idx]->rx_ring;
2794 		bnxt_sched_reset_rxr(bp, rxr);
2795 		goto async_event_process_exit;
2796 	}
2797 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2798 		struct bnxt_fw_health *fw_health = bp->fw_health;
2799 
2800 		netif_notice(bp, hw, bp->dev,
2801 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2802 			     data1, data2);
2803 		if (fw_health) {
2804 			fw_health->echo_req_data1 = data1;
2805 			fw_health->echo_req_data2 = data2;
2806 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2807 			break;
2808 		}
2809 		goto async_event_process_exit;
2810 	}
2811 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2812 		bnxt_ptp_pps_event(bp, data1, data2);
2813 		goto async_event_process_exit;
2814 	}
2815 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2816 		if (bnxt_event_error_report(bp, data1, data2))
2817 			break;
2818 		goto async_event_process_exit;
2819 	}
2820 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2821 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2822 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2823 			if (BNXT_PTP_USE_RTC(bp)) {
2824 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2825 				unsigned long flags;
2826 				u64 ns;
2827 
2828 				if (!ptp)
2829 					goto async_event_process_exit;
2830 
2831 				bnxt_ptp_update_current_time(bp);
2832 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2833 				       BNXT_PHC_BITS) | ptp->current_time);
2834 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2835 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2836 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2837 			}
2838 			break;
2839 		}
2840 		goto async_event_process_exit;
2841 	}
2842 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2843 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2844 
2845 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2846 		goto async_event_process_exit;
2847 	}
2848 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2849 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2850 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2851 
2852 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2853 		goto async_event_process_exit;
2854 	}
2855 	default:
2856 		goto async_event_process_exit;
2857 	}
2858 	__bnxt_queue_sp_work(bp);
2859 async_event_process_exit:
2860 	return 0;
2861 }
2862 
2863 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2864 {
2865 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2866 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2867 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2868 				(struct hwrm_fwd_req_cmpl *)txcmp;
2869 
2870 	switch (cmpl_type) {
2871 	case CMPL_BASE_TYPE_HWRM_DONE:
2872 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2873 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2874 		break;
2875 
2876 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2877 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2878 
2879 		if ((vf_id < bp->pf.first_vf_id) ||
2880 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2881 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2882 				   vf_id);
2883 			return -EINVAL;
2884 		}
2885 
2886 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2887 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2888 		break;
2889 
2890 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2891 		bnxt_async_event_process(bp,
2892 					 (struct hwrm_async_event_cmpl *)txcmp);
2893 		break;
2894 
2895 	default:
2896 		break;
2897 	}
2898 
2899 	return 0;
2900 }
2901 
2902 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2903 {
2904 	struct bnxt_napi *bnapi = dev_instance;
2905 	struct bnxt *bp = bnapi->bp;
2906 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2907 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2908 
2909 	cpr->event_ctr++;
2910 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2911 	napi_schedule(&bnapi->napi);
2912 	return IRQ_HANDLED;
2913 }
2914 
2915 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2916 {
2917 	u32 raw_cons = cpr->cp_raw_cons;
2918 	u16 cons = RING_CMP(raw_cons);
2919 	struct tx_cmp *txcmp;
2920 
2921 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2922 
2923 	return TX_CMP_VALID(txcmp, raw_cons);
2924 }
2925 
2926 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2927 			    int budget)
2928 {
2929 	struct bnxt_napi *bnapi = cpr->bnapi;
2930 	u32 raw_cons = cpr->cp_raw_cons;
2931 	u32 cons;
2932 	int rx_pkts = 0;
2933 	u8 event = 0;
2934 	struct tx_cmp *txcmp;
2935 
2936 	cpr->has_more_work = 0;
2937 	cpr->had_work_done = 1;
2938 	while (1) {
2939 		u8 cmp_type;
2940 		int rc;
2941 
2942 		cons = RING_CMP(raw_cons);
2943 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2944 
2945 		if (!TX_CMP_VALID(txcmp, raw_cons))
2946 			break;
2947 
2948 		/* The valid test of the entry must be done first before
2949 		 * reading any further.
2950 		 */
2951 		dma_rmb();
2952 		cmp_type = TX_CMP_TYPE(txcmp);
2953 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
2954 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
2955 			u32 opaque = txcmp->tx_cmp_opaque;
2956 			struct bnxt_tx_ring_info *txr;
2957 			u16 tx_freed;
2958 
2959 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
2960 			event |= BNXT_TX_CMP_EVENT;
2961 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
2962 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
2963 			else
2964 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
2965 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
2966 				   bp->tx_ring_mask;
2967 			/* return full budget so NAPI will complete. */
2968 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
2969 				rx_pkts = budget;
2970 				raw_cons = NEXT_RAW_CMP(raw_cons);
2971 				if (budget)
2972 					cpr->has_more_work = 1;
2973 				break;
2974 			}
2975 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
2976 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
2977 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
2978 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2979 			if (likely(budget))
2980 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2981 			else
2982 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2983 							   &event);
2984 			if (likely(rc >= 0))
2985 				rx_pkts += rc;
2986 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2987 			 * the NAPI budget.  Otherwise, we may potentially loop
2988 			 * here forever if we consistently cannot allocate
2989 			 * buffers.
2990 			 */
2991 			else if (rc == -ENOMEM && budget)
2992 				rx_pkts++;
2993 			else if (rc == -EBUSY)	/* partial completion */
2994 				break;
2995 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
2996 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
2997 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
2998 			bnxt_hwrm_handler(bp, txcmp);
2999 		}
3000 		raw_cons = NEXT_RAW_CMP(raw_cons);
3001 
3002 		if (rx_pkts && rx_pkts == budget) {
3003 			cpr->has_more_work = 1;
3004 			break;
3005 		}
3006 	}
3007 
3008 	if (event & BNXT_REDIRECT_EVENT) {
3009 		xdp_do_flush();
3010 		event &= ~BNXT_REDIRECT_EVENT;
3011 	}
3012 
3013 	if (event & BNXT_TX_EVENT) {
3014 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3015 		u16 prod = txr->tx_prod;
3016 
3017 		/* Sync BD data before updating doorbell */
3018 		wmb();
3019 
3020 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3021 		event &= ~BNXT_TX_EVENT;
3022 	}
3023 
3024 	cpr->cp_raw_cons = raw_cons;
3025 	bnapi->events |= event;
3026 	return rx_pkts;
3027 }
3028 
3029 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3030 				  int budget)
3031 {
3032 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3033 		bnapi->tx_int(bp, bnapi, budget);
3034 
3035 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3036 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3037 
3038 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3039 		bnapi->events &= ~BNXT_RX_EVENT;
3040 	}
3041 	if (bnapi->events & BNXT_AGG_EVENT) {
3042 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3043 
3044 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3045 		bnapi->events &= ~BNXT_AGG_EVENT;
3046 	}
3047 }
3048 
3049 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3050 			  int budget)
3051 {
3052 	struct bnxt_napi *bnapi = cpr->bnapi;
3053 	int rx_pkts;
3054 
3055 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3056 
3057 	/* ACK completion ring before freeing tx ring and producing new
3058 	 * buffers in rx/agg rings to prevent overflowing the completion
3059 	 * ring.
3060 	 */
3061 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3062 
3063 	__bnxt_poll_work_done(bp, bnapi, budget);
3064 	return rx_pkts;
3065 }
3066 
3067 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3068 {
3069 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3070 	struct bnxt *bp = bnapi->bp;
3071 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3072 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3073 	struct tx_cmp *txcmp;
3074 	struct rx_cmp_ext *rxcmp1;
3075 	u32 cp_cons, tmp_raw_cons;
3076 	u32 raw_cons = cpr->cp_raw_cons;
3077 	bool flush_xdp = false;
3078 	u32 rx_pkts = 0;
3079 	u8 event = 0;
3080 
3081 	while (1) {
3082 		int rc;
3083 
3084 		cp_cons = RING_CMP(raw_cons);
3085 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3086 
3087 		if (!TX_CMP_VALID(txcmp, raw_cons))
3088 			break;
3089 
3090 		/* The valid test of the entry must be done first before
3091 		 * reading any further.
3092 		 */
3093 		dma_rmb();
3094 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3095 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3096 			cp_cons = RING_CMP(tmp_raw_cons);
3097 			rxcmp1 = (struct rx_cmp_ext *)
3098 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3099 
3100 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3101 				break;
3102 
3103 			/* force an error to recycle the buffer */
3104 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3105 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3106 
3107 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3108 			if (likely(rc == -EIO) && budget)
3109 				rx_pkts++;
3110 			else if (rc == -EBUSY)	/* partial completion */
3111 				break;
3112 			if (event & BNXT_REDIRECT_EVENT)
3113 				flush_xdp = true;
3114 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3115 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3116 			bnxt_hwrm_handler(bp, txcmp);
3117 		} else {
3118 			netdev_err(bp->dev,
3119 				   "Invalid completion received on special ring\n");
3120 		}
3121 		raw_cons = NEXT_RAW_CMP(raw_cons);
3122 
3123 		if (rx_pkts == budget)
3124 			break;
3125 	}
3126 
3127 	cpr->cp_raw_cons = raw_cons;
3128 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3129 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3130 
3131 	if (event & BNXT_AGG_EVENT)
3132 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3133 	if (flush_xdp)
3134 		xdp_do_flush();
3135 
3136 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3137 		napi_complete_done(napi, rx_pkts);
3138 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3139 	}
3140 	return rx_pkts;
3141 }
3142 
3143 static int bnxt_poll(struct napi_struct *napi, int budget)
3144 {
3145 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3146 	struct bnxt *bp = bnapi->bp;
3147 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3148 	int work_done = 0;
3149 
3150 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3151 		napi_complete(napi);
3152 		return 0;
3153 	}
3154 	while (1) {
3155 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3156 
3157 		if (work_done >= budget) {
3158 			if (!budget)
3159 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3160 			break;
3161 		}
3162 
3163 		if (!bnxt_has_work(bp, cpr)) {
3164 			if (napi_complete_done(napi, work_done))
3165 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3166 			break;
3167 		}
3168 	}
3169 	if (bp->flags & BNXT_FLAG_DIM) {
3170 		struct dim_sample dim_sample = {};
3171 
3172 		dim_update_sample(cpr->event_ctr,
3173 				  cpr->rx_packets,
3174 				  cpr->rx_bytes,
3175 				  &dim_sample);
3176 		net_dim(&cpr->dim, &dim_sample);
3177 	}
3178 	return work_done;
3179 }
3180 
3181 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3182 {
3183 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3184 	int i, work_done = 0;
3185 
3186 	for (i = 0; i < cpr->cp_ring_count; i++) {
3187 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3188 
3189 		if (cpr2->had_nqe_notify) {
3190 			work_done += __bnxt_poll_work(bp, cpr2,
3191 						      budget - work_done);
3192 			cpr->has_more_work |= cpr2->has_more_work;
3193 		}
3194 	}
3195 	return work_done;
3196 }
3197 
3198 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3199 				 u64 dbr_type, int budget)
3200 {
3201 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3202 	int i;
3203 
3204 	for (i = 0; i < cpr->cp_ring_count; i++) {
3205 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3206 		struct bnxt_db_info *db;
3207 
3208 		if (cpr2->had_work_done) {
3209 			u32 tgl = 0;
3210 
3211 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3212 				cpr2->had_nqe_notify = 0;
3213 				tgl = cpr2->toggle;
3214 			}
3215 			db = &cpr2->cp_db;
3216 			bnxt_writeq(bp,
3217 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3218 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3219 				    db->doorbell);
3220 			cpr2->had_work_done = 0;
3221 		}
3222 	}
3223 	__bnxt_poll_work_done(bp, bnapi, budget);
3224 }
3225 
3226 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3227 {
3228 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3229 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3230 	struct bnxt_cp_ring_info *cpr_rx;
3231 	u32 raw_cons = cpr->cp_raw_cons;
3232 	struct bnxt *bp = bnapi->bp;
3233 	struct nqe_cn *nqcmp;
3234 	int work_done = 0;
3235 	u32 cons;
3236 
3237 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3238 		napi_complete(napi);
3239 		return 0;
3240 	}
3241 	if (cpr->has_more_work) {
3242 		cpr->has_more_work = 0;
3243 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3244 	}
3245 	while (1) {
3246 		u16 type;
3247 
3248 		cons = RING_CMP(raw_cons);
3249 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3250 
3251 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3252 			if (cpr->has_more_work)
3253 				break;
3254 
3255 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3256 					     budget);
3257 			cpr->cp_raw_cons = raw_cons;
3258 			if (napi_complete_done(napi, work_done))
3259 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3260 						  cpr->cp_raw_cons);
3261 			goto poll_done;
3262 		}
3263 
3264 		/* The valid test of the entry must be done first before
3265 		 * reading any further.
3266 		 */
3267 		dma_rmb();
3268 
3269 		type = le16_to_cpu(nqcmp->type);
3270 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3271 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3272 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3273 			struct bnxt_cp_ring_info *cpr2;
3274 
3275 			/* No more budget for RX work */
3276 			if (budget && work_done >= budget &&
3277 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3278 				break;
3279 
3280 			idx = BNXT_NQ_HDL_IDX(idx);
3281 			cpr2 = &cpr->cp_ring_arr[idx];
3282 			cpr2->had_nqe_notify = 1;
3283 			cpr2->toggle = NQE_CN_TOGGLE(type);
3284 			work_done += __bnxt_poll_work(bp, cpr2,
3285 						      budget - work_done);
3286 			cpr->has_more_work |= cpr2->has_more_work;
3287 		} else {
3288 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3289 		}
3290 		raw_cons = NEXT_RAW_CMP(raw_cons);
3291 	}
3292 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3293 	if (raw_cons != cpr->cp_raw_cons) {
3294 		cpr->cp_raw_cons = raw_cons;
3295 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3296 	}
3297 poll_done:
3298 	cpr_rx = &cpr->cp_ring_arr[0];
3299 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3300 	    (bp->flags & BNXT_FLAG_DIM)) {
3301 		struct dim_sample dim_sample = {};
3302 
3303 		dim_update_sample(cpr->event_ctr,
3304 				  cpr_rx->rx_packets,
3305 				  cpr_rx->rx_bytes,
3306 				  &dim_sample);
3307 		net_dim(&cpr->dim, &dim_sample);
3308 	}
3309 	return work_done;
3310 }
3311 
3312 static void bnxt_free_tx_skbs(struct bnxt *bp)
3313 {
3314 	int i, max_idx;
3315 	struct pci_dev *pdev = bp->pdev;
3316 
3317 	if (!bp->tx_ring)
3318 		return;
3319 
3320 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3321 	for (i = 0; i < bp->tx_nr_rings; i++) {
3322 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3323 		int j;
3324 
3325 		if (!txr->tx_buf_ring)
3326 			continue;
3327 
3328 		for (j = 0; j < max_idx;) {
3329 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
3330 			struct sk_buff *skb;
3331 			int k, last;
3332 
3333 			if (i < bp->tx_nr_rings_xdp &&
3334 			    tx_buf->action == XDP_REDIRECT) {
3335 				dma_unmap_single(&pdev->dev,
3336 					dma_unmap_addr(tx_buf, mapping),
3337 					dma_unmap_len(tx_buf, len),
3338 					DMA_TO_DEVICE);
3339 				xdp_return_frame(tx_buf->xdpf);
3340 				tx_buf->action = 0;
3341 				tx_buf->xdpf = NULL;
3342 				j++;
3343 				continue;
3344 			}
3345 
3346 			skb = tx_buf->skb;
3347 			if (!skb) {
3348 				j++;
3349 				continue;
3350 			}
3351 
3352 			tx_buf->skb = NULL;
3353 
3354 			if (tx_buf->is_push) {
3355 				dev_kfree_skb(skb);
3356 				j += 2;
3357 				continue;
3358 			}
3359 
3360 			dma_unmap_single(&pdev->dev,
3361 					 dma_unmap_addr(tx_buf, mapping),
3362 					 skb_headlen(skb),
3363 					 DMA_TO_DEVICE);
3364 
3365 			last = tx_buf->nr_frags;
3366 			j += 2;
3367 			for (k = 0; k < last; k++, j++) {
3368 				int ring_idx = j & bp->tx_ring_mask;
3369 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
3370 
3371 				tx_buf = &txr->tx_buf_ring[ring_idx];
3372 				dma_unmap_page(
3373 					&pdev->dev,
3374 					dma_unmap_addr(tx_buf, mapping),
3375 					skb_frag_size(frag), DMA_TO_DEVICE);
3376 			}
3377 			dev_kfree_skb(skb);
3378 		}
3379 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
3380 	}
3381 }
3382 
3383 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3384 {
3385 	int i, max_idx;
3386 
3387 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3388 
3389 	for (i = 0; i < max_idx; i++) {
3390 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3391 		void *data = rx_buf->data;
3392 
3393 		if (!data)
3394 			continue;
3395 
3396 		rx_buf->data = NULL;
3397 		if (BNXT_RX_PAGE_MODE(bp))
3398 			page_pool_recycle_direct(rxr->page_pool, data);
3399 		else
3400 			page_pool_free_va(rxr->head_pool, data, true);
3401 	}
3402 }
3403 
3404 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3405 {
3406 	int i, max_idx;
3407 
3408 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3409 
3410 	for (i = 0; i < max_idx; i++) {
3411 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3412 		struct page *page = rx_agg_buf->page;
3413 
3414 		if (!page)
3415 			continue;
3416 
3417 		rx_agg_buf->page = NULL;
3418 		__clear_bit(i, rxr->rx_agg_bmap);
3419 
3420 		page_pool_recycle_direct(rxr->page_pool, page);
3421 	}
3422 }
3423 
3424 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3425 					struct bnxt_rx_ring_info *rxr)
3426 {
3427 	int i;
3428 
3429 	for (i = 0; i < bp->max_tpa; i++) {
3430 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3431 		u8 *data = tpa_info->data;
3432 
3433 		if (!data)
3434 			continue;
3435 
3436 		tpa_info->data = NULL;
3437 		page_pool_free_va(rxr->head_pool, data, false);
3438 	}
3439 }
3440 
3441 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3442 				       struct bnxt_rx_ring_info *rxr)
3443 {
3444 	struct bnxt_tpa_idx_map *map;
3445 
3446 	if (!rxr->rx_tpa)
3447 		goto skip_rx_tpa_free;
3448 
3449 	bnxt_free_one_tpa_info_data(bp, rxr);
3450 
3451 skip_rx_tpa_free:
3452 	if (!rxr->rx_buf_ring)
3453 		goto skip_rx_buf_free;
3454 
3455 	bnxt_free_one_rx_ring(bp, rxr);
3456 
3457 skip_rx_buf_free:
3458 	if (!rxr->rx_agg_ring)
3459 		goto skip_rx_agg_free;
3460 
3461 	bnxt_free_one_rx_agg_ring(bp, rxr);
3462 
3463 skip_rx_agg_free:
3464 	map = rxr->rx_tpa_idx_map;
3465 	if (map)
3466 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3467 }
3468 
3469 static void bnxt_free_rx_skbs(struct bnxt *bp)
3470 {
3471 	int i;
3472 
3473 	if (!bp->rx_ring)
3474 		return;
3475 
3476 	for (i = 0; i < bp->rx_nr_rings; i++)
3477 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3478 }
3479 
3480 static void bnxt_free_skbs(struct bnxt *bp)
3481 {
3482 	bnxt_free_tx_skbs(bp);
3483 	bnxt_free_rx_skbs(bp);
3484 }
3485 
3486 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3487 {
3488 	u8 init_val = ctxm->init_value;
3489 	u16 offset = ctxm->init_offset;
3490 	u8 *p2 = p;
3491 	int i;
3492 
3493 	if (!init_val)
3494 		return;
3495 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3496 		memset(p, init_val, len);
3497 		return;
3498 	}
3499 	for (i = 0; i < len; i += ctxm->entry_size)
3500 		*(p2 + i + offset) = init_val;
3501 }
3502 
3503 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3504 			       void *buf, size_t offset, size_t head,
3505 			       size_t tail)
3506 {
3507 	int i, head_page, start_idx, source_offset;
3508 	size_t len, rem_len, total_len, max_bytes;
3509 
3510 	head_page = head / rmem->page_size;
3511 	source_offset = head % rmem->page_size;
3512 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3513 	if (!total_len)
3514 		total_len = MAX_CTX_BYTES;
3515 	start_idx = head_page % MAX_CTX_PAGES;
3516 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3517 		    source_offset;
3518 	total_len = min(total_len, max_bytes);
3519 	rem_len = total_len;
3520 
3521 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3522 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3523 		if (buf)
3524 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3525 			       len);
3526 		offset += len;
3527 		rem_len -= len;
3528 	}
3529 	return total_len;
3530 }
3531 
3532 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3533 {
3534 	struct pci_dev *pdev = bp->pdev;
3535 	int i;
3536 
3537 	if (!rmem->pg_arr)
3538 		goto skip_pages;
3539 
3540 	for (i = 0; i < rmem->nr_pages; i++) {
3541 		if (!rmem->pg_arr[i])
3542 			continue;
3543 
3544 		dma_free_coherent(&pdev->dev, rmem->page_size,
3545 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3546 
3547 		rmem->pg_arr[i] = NULL;
3548 	}
3549 skip_pages:
3550 	if (rmem->pg_tbl) {
3551 		size_t pg_tbl_size = rmem->nr_pages * 8;
3552 
3553 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3554 			pg_tbl_size = rmem->page_size;
3555 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3556 				  rmem->pg_tbl, rmem->pg_tbl_map);
3557 		rmem->pg_tbl = NULL;
3558 	}
3559 	if (rmem->vmem_size && *rmem->vmem) {
3560 		vfree(*rmem->vmem);
3561 		*rmem->vmem = NULL;
3562 	}
3563 }
3564 
3565 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3566 {
3567 	struct pci_dev *pdev = bp->pdev;
3568 	u64 valid_bit = 0;
3569 	int i;
3570 
3571 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3572 		valid_bit = PTU_PTE_VALID;
3573 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3574 		size_t pg_tbl_size = rmem->nr_pages * 8;
3575 
3576 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3577 			pg_tbl_size = rmem->page_size;
3578 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3579 						  &rmem->pg_tbl_map,
3580 						  GFP_KERNEL);
3581 		if (!rmem->pg_tbl)
3582 			return -ENOMEM;
3583 	}
3584 
3585 	for (i = 0; i < rmem->nr_pages; i++) {
3586 		u64 extra_bits = valid_bit;
3587 
3588 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3589 						     rmem->page_size,
3590 						     &rmem->dma_arr[i],
3591 						     GFP_KERNEL);
3592 		if (!rmem->pg_arr[i])
3593 			return -ENOMEM;
3594 
3595 		if (rmem->ctx_mem)
3596 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3597 					  rmem->page_size);
3598 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3599 			if (i == rmem->nr_pages - 2 &&
3600 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3601 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3602 			else if (i == rmem->nr_pages - 1 &&
3603 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3604 				extra_bits |= PTU_PTE_LAST;
3605 			rmem->pg_tbl[i] =
3606 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3607 		}
3608 	}
3609 
3610 	if (rmem->vmem_size) {
3611 		*rmem->vmem = vzalloc(rmem->vmem_size);
3612 		if (!(*rmem->vmem))
3613 			return -ENOMEM;
3614 	}
3615 	return 0;
3616 }
3617 
3618 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3619 				   struct bnxt_rx_ring_info *rxr)
3620 {
3621 	int i;
3622 
3623 	kfree(rxr->rx_tpa_idx_map);
3624 	rxr->rx_tpa_idx_map = NULL;
3625 	if (rxr->rx_tpa) {
3626 		for (i = 0; i < bp->max_tpa; i++) {
3627 			kfree(rxr->rx_tpa[i].agg_arr);
3628 			rxr->rx_tpa[i].agg_arr = NULL;
3629 		}
3630 	}
3631 	kfree(rxr->rx_tpa);
3632 	rxr->rx_tpa = NULL;
3633 }
3634 
3635 static void bnxt_free_tpa_info(struct bnxt *bp)
3636 {
3637 	int i;
3638 
3639 	for (i = 0; i < bp->rx_nr_rings; i++) {
3640 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3641 
3642 		bnxt_free_one_tpa_info(bp, rxr);
3643 	}
3644 }
3645 
3646 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3647 				   struct bnxt_rx_ring_info *rxr)
3648 {
3649 	struct rx_agg_cmp *agg;
3650 	int i;
3651 
3652 	rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3653 			      GFP_KERNEL);
3654 	if (!rxr->rx_tpa)
3655 		return -ENOMEM;
3656 
3657 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3658 		return 0;
3659 	for (i = 0; i < bp->max_tpa; i++) {
3660 		agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3661 		if (!agg)
3662 			return -ENOMEM;
3663 		rxr->rx_tpa[i].agg_arr = agg;
3664 	}
3665 	rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3666 				      GFP_KERNEL);
3667 	if (!rxr->rx_tpa_idx_map)
3668 		return -ENOMEM;
3669 
3670 	return 0;
3671 }
3672 
3673 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3674 {
3675 	int i, rc;
3676 
3677 	bp->max_tpa = MAX_TPA;
3678 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3679 		if (!bp->max_tpa_v2)
3680 			return 0;
3681 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3682 	}
3683 
3684 	for (i = 0; i < bp->rx_nr_rings; i++) {
3685 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3686 
3687 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3688 		if (rc)
3689 			return rc;
3690 	}
3691 	return 0;
3692 }
3693 
3694 static void bnxt_free_rx_rings(struct bnxt *bp)
3695 {
3696 	int i;
3697 
3698 	if (!bp->rx_ring)
3699 		return;
3700 
3701 	bnxt_free_tpa_info(bp);
3702 	for (i = 0; i < bp->rx_nr_rings; i++) {
3703 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3704 		struct bnxt_ring_struct *ring;
3705 
3706 		if (rxr->xdp_prog)
3707 			bpf_prog_put(rxr->xdp_prog);
3708 
3709 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3710 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3711 
3712 		page_pool_destroy(rxr->page_pool);
3713 		if (bnxt_separate_head_pool())
3714 			page_pool_destroy(rxr->head_pool);
3715 		rxr->page_pool = rxr->head_pool = NULL;
3716 
3717 		kfree(rxr->rx_agg_bmap);
3718 		rxr->rx_agg_bmap = NULL;
3719 
3720 		ring = &rxr->rx_ring_struct;
3721 		bnxt_free_ring(bp, &ring->ring_mem);
3722 
3723 		ring = &rxr->rx_agg_ring_struct;
3724 		bnxt_free_ring(bp, &ring->ring_mem);
3725 	}
3726 }
3727 
3728 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3729 				   struct bnxt_rx_ring_info *rxr,
3730 				   int numa_node)
3731 {
3732 	struct page_pool_params pp = { 0 };
3733 	struct page_pool *pool;
3734 
3735 	pp.pool_size = bp->rx_agg_ring_size;
3736 	if (BNXT_RX_PAGE_MODE(bp))
3737 		pp.pool_size += bp->rx_ring_size;
3738 	pp.nid = numa_node;
3739 	pp.napi = &rxr->bnapi->napi;
3740 	pp.netdev = bp->dev;
3741 	pp.dev = &bp->pdev->dev;
3742 	pp.dma_dir = bp->rx_dir;
3743 	pp.max_len = PAGE_SIZE;
3744 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3745 
3746 	pool = page_pool_create(&pp);
3747 	if (IS_ERR(pool))
3748 		return PTR_ERR(pool);
3749 	rxr->page_pool = pool;
3750 
3751 	if (bnxt_separate_head_pool()) {
3752 		pp.pool_size = max(bp->rx_ring_size, 1024);
3753 		pool = page_pool_create(&pp);
3754 		if (IS_ERR(pool))
3755 			goto err_destroy_pp;
3756 	}
3757 	rxr->head_pool = pool;
3758 
3759 	return 0;
3760 
3761 err_destroy_pp:
3762 	page_pool_destroy(rxr->page_pool);
3763 	rxr->page_pool = NULL;
3764 	return PTR_ERR(pool);
3765 }
3766 
3767 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3768 {
3769 	u16 mem_size;
3770 
3771 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3772 	mem_size = rxr->rx_agg_bmap_size / 8;
3773 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3774 	if (!rxr->rx_agg_bmap)
3775 		return -ENOMEM;
3776 
3777 	return 0;
3778 }
3779 
3780 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3781 {
3782 	int numa_node = dev_to_node(&bp->pdev->dev);
3783 	int i, rc = 0, agg_rings = 0, cpu;
3784 
3785 	if (!bp->rx_ring)
3786 		return -ENOMEM;
3787 
3788 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3789 		agg_rings = 1;
3790 
3791 	for (i = 0; i < bp->rx_nr_rings; i++) {
3792 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3793 		struct bnxt_ring_struct *ring;
3794 		int cpu_node;
3795 
3796 		ring = &rxr->rx_ring_struct;
3797 
3798 		cpu = cpumask_local_spread(i, numa_node);
3799 		cpu_node = cpu_to_node(cpu);
3800 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3801 			   i, cpu_node);
3802 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3803 		if (rc)
3804 			return rc;
3805 
3806 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3807 		if (rc < 0)
3808 			return rc;
3809 
3810 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3811 						MEM_TYPE_PAGE_POOL,
3812 						rxr->page_pool);
3813 		if (rc) {
3814 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3815 			return rc;
3816 		}
3817 
3818 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3819 		if (rc)
3820 			return rc;
3821 
3822 		ring->grp_idx = i;
3823 		if (agg_rings) {
3824 			ring = &rxr->rx_agg_ring_struct;
3825 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3826 			if (rc)
3827 				return rc;
3828 
3829 			ring->grp_idx = i;
3830 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3831 			if (rc)
3832 				return rc;
3833 		}
3834 	}
3835 	if (bp->flags & BNXT_FLAG_TPA)
3836 		rc = bnxt_alloc_tpa_info(bp);
3837 	return rc;
3838 }
3839 
3840 static void bnxt_free_tx_rings(struct bnxt *bp)
3841 {
3842 	int i;
3843 	struct pci_dev *pdev = bp->pdev;
3844 
3845 	if (!bp->tx_ring)
3846 		return;
3847 
3848 	for (i = 0; i < bp->tx_nr_rings; i++) {
3849 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3850 		struct bnxt_ring_struct *ring;
3851 
3852 		if (txr->tx_push) {
3853 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3854 					  txr->tx_push, txr->tx_push_mapping);
3855 			txr->tx_push = NULL;
3856 		}
3857 
3858 		ring = &txr->tx_ring_struct;
3859 
3860 		bnxt_free_ring(bp, &ring->ring_mem);
3861 	}
3862 }
3863 
3864 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3865 	((tc) * (bp)->tx_nr_rings_per_tc)
3866 
3867 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3868 	((tx) % (bp)->tx_nr_rings_per_tc)
3869 
3870 #define BNXT_RING_TO_TC(bp, tx)		\
3871 	((tx) / (bp)->tx_nr_rings_per_tc)
3872 
3873 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3874 {
3875 	int i, j, rc;
3876 	struct pci_dev *pdev = bp->pdev;
3877 
3878 	bp->tx_push_size = 0;
3879 	if (bp->tx_push_thresh) {
3880 		int push_size;
3881 
3882 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3883 					bp->tx_push_thresh);
3884 
3885 		if (push_size > 256) {
3886 			push_size = 0;
3887 			bp->tx_push_thresh = 0;
3888 		}
3889 
3890 		bp->tx_push_size = push_size;
3891 	}
3892 
3893 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3894 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3895 		struct bnxt_ring_struct *ring;
3896 		u8 qidx;
3897 
3898 		ring = &txr->tx_ring_struct;
3899 
3900 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3901 		if (rc)
3902 			return rc;
3903 
3904 		ring->grp_idx = txr->bnapi->index;
3905 		if (bp->tx_push_size) {
3906 			dma_addr_t mapping;
3907 
3908 			/* One pre-allocated DMA buffer to backup
3909 			 * TX push operation
3910 			 */
3911 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3912 						bp->tx_push_size,
3913 						&txr->tx_push_mapping,
3914 						GFP_KERNEL);
3915 
3916 			if (!txr->tx_push)
3917 				return -ENOMEM;
3918 
3919 			mapping = txr->tx_push_mapping +
3920 				sizeof(struct tx_push_bd);
3921 			txr->data_mapping = cpu_to_le64(mapping);
3922 		}
3923 		qidx = bp->tc_to_qidx[j];
3924 		ring->queue_id = bp->q_info[qidx].queue_id;
3925 		spin_lock_init(&txr->xdp_tx_lock);
3926 		if (i < bp->tx_nr_rings_xdp)
3927 			continue;
3928 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
3929 			j++;
3930 	}
3931 	return 0;
3932 }
3933 
3934 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3935 {
3936 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3937 
3938 	kfree(cpr->cp_desc_ring);
3939 	cpr->cp_desc_ring = NULL;
3940 	ring->ring_mem.pg_arr = NULL;
3941 	kfree(cpr->cp_desc_mapping);
3942 	cpr->cp_desc_mapping = NULL;
3943 	ring->ring_mem.dma_arr = NULL;
3944 }
3945 
3946 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3947 {
3948 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3949 	if (!cpr->cp_desc_ring)
3950 		return -ENOMEM;
3951 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3952 				       GFP_KERNEL);
3953 	if (!cpr->cp_desc_mapping)
3954 		return -ENOMEM;
3955 	return 0;
3956 }
3957 
3958 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3959 {
3960 	int i;
3961 
3962 	if (!bp->bnapi)
3963 		return;
3964 	for (i = 0; i < bp->cp_nr_rings; i++) {
3965 		struct bnxt_napi *bnapi = bp->bnapi[i];
3966 
3967 		if (!bnapi)
3968 			continue;
3969 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3970 	}
3971 }
3972 
3973 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3974 {
3975 	int i, n = bp->cp_nr_pages;
3976 
3977 	for (i = 0; i < bp->cp_nr_rings; i++) {
3978 		struct bnxt_napi *bnapi = bp->bnapi[i];
3979 		int rc;
3980 
3981 		if (!bnapi)
3982 			continue;
3983 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3984 		if (rc)
3985 			return rc;
3986 	}
3987 	return 0;
3988 }
3989 
3990 static void bnxt_free_cp_rings(struct bnxt *bp)
3991 {
3992 	int i;
3993 
3994 	if (!bp->bnapi)
3995 		return;
3996 
3997 	for (i = 0; i < bp->cp_nr_rings; i++) {
3998 		struct bnxt_napi *bnapi = bp->bnapi[i];
3999 		struct bnxt_cp_ring_info *cpr;
4000 		struct bnxt_ring_struct *ring;
4001 		int j;
4002 
4003 		if (!bnapi)
4004 			continue;
4005 
4006 		cpr = &bnapi->cp_ring;
4007 		ring = &cpr->cp_ring_struct;
4008 
4009 		bnxt_free_ring(bp, &ring->ring_mem);
4010 
4011 		if (!cpr->cp_ring_arr)
4012 			continue;
4013 
4014 		for (j = 0; j < cpr->cp_ring_count; j++) {
4015 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4016 
4017 			ring = &cpr2->cp_ring_struct;
4018 			bnxt_free_ring(bp, &ring->ring_mem);
4019 			bnxt_free_cp_arrays(cpr2);
4020 		}
4021 		kfree(cpr->cp_ring_arr);
4022 		cpr->cp_ring_arr = NULL;
4023 		cpr->cp_ring_count = 0;
4024 	}
4025 }
4026 
4027 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4028 				  struct bnxt_cp_ring_info *cpr)
4029 {
4030 	struct bnxt_ring_mem_info *rmem;
4031 	struct bnxt_ring_struct *ring;
4032 	int rc;
4033 
4034 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4035 	if (rc) {
4036 		bnxt_free_cp_arrays(cpr);
4037 		return -ENOMEM;
4038 	}
4039 	ring = &cpr->cp_ring_struct;
4040 	rmem = &ring->ring_mem;
4041 	rmem->nr_pages = bp->cp_nr_pages;
4042 	rmem->page_size = HW_CMPD_RING_SIZE;
4043 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4044 	rmem->dma_arr = cpr->cp_desc_mapping;
4045 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4046 	rc = bnxt_alloc_ring(bp, rmem);
4047 	if (rc) {
4048 		bnxt_free_ring(bp, rmem);
4049 		bnxt_free_cp_arrays(cpr);
4050 	}
4051 	return rc;
4052 }
4053 
4054 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4055 {
4056 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4057 	int i, j, rc, ulp_msix;
4058 	int tcs = bp->num_tc;
4059 
4060 	if (!tcs)
4061 		tcs = 1;
4062 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4063 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4064 		struct bnxt_napi *bnapi = bp->bnapi[i];
4065 		struct bnxt_cp_ring_info *cpr, *cpr2;
4066 		struct bnxt_ring_struct *ring;
4067 		int cp_count = 0, k;
4068 		int rx = 0, tx = 0;
4069 
4070 		if (!bnapi)
4071 			continue;
4072 
4073 		cpr = &bnapi->cp_ring;
4074 		cpr->bnapi = bnapi;
4075 		ring = &cpr->cp_ring_struct;
4076 
4077 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4078 		if (rc)
4079 			return rc;
4080 
4081 		ring->map_idx = ulp_msix + i;
4082 
4083 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4084 			continue;
4085 
4086 		if (i < bp->rx_nr_rings) {
4087 			cp_count++;
4088 			rx = 1;
4089 		}
4090 		if (i < bp->tx_nr_rings_xdp) {
4091 			cp_count++;
4092 			tx = 1;
4093 		} else if ((sh && i < bp->tx_nr_rings) ||
4094 			 (!sh && i >= bp->rx_nr_rings)) {
4095 			cp_count += tcs;
4096 			tx = 1;
4097 		}
4098 
4099 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
4100 					   GFP_KERNEL);
4101 		if (!cpr->cp_ring_arr)
4102 			return -ENOMEM;
4103 		cpr->cp_ring_count = cp_count;
4104 
4105 		for (k = 0; k < cp_count; k++) {
4106 			cpr2 = &cpr->cp_ring_arr[k];
4107 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4108 			if (rc)
4109 				return rc;
4110 			cpr2->bnapi = bnapi;
4111 			cpr2->sw_stats = cpr->sw_stats;
4112 			cpr2->cp_idx = k;
4113 			if (!k && rx) {
4114 				bp->rx_ring[i].rx_cpr = cpr2;
4115 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4116 			} else {
4117 				int n, tc = k - rx;
4118 
4119 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4120 				bp->tx_ring[n].tx_cpr = cpr2;
4121 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4122 			}
4123 		}
4124 		if (tx)
4125 			j++;
4126 	}
4127 	return 0;
4128 }
4129 
4130 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4131 				     struct bnxt_rx_ring_info *rxr)
4132 {
4133 	struct bnxt_ring_mem_info *rmem;
4134 	struct bnxt_ring_struct *ring;
4135 
4136 	ring = &rxr->rx_ring_struct;
4137 	rmem = &ring->ring_mem;
4138 	rmem->nr_pages = bp->rx_nr_pages;
4139 	rmem->page_size = HW_RXBD_RING_SIZE;
4140 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4141 	rmem->dma_arr = rxr->rx_desc_mapping;
4142 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4143 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4144 
4145 	ring = &rxr->rx_agg_ring_struct;
4146 	rmem = &ring->ring_mem;
4147 	rmem->nr_pages = bp->rx_agg_nr_pages;
4148 	rmem->page_size = HW_RXBD_RING_SIZE;
4149 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4150 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4151 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4152 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4153 }
4154 
4155 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4156 				      struct bnxt_rx_ring_info *rxr)
4157 {
4158 	struct bnxt_ring_mem_info *rmem;
4159 	struct bnxt_ring_struct *ring;
4160 	int i;
4161 
4162 	rxr->page_pool->p.napi = NULL;
4163 	rxr->page_pool = NULL;
4164 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4165 
4166 	ring = &rxr->rx_ring_struct;
4167 	rmem = &ring->ring_mem;
4168 	rmem->pg_tbl = NULL;
4169 	rmem->pg_tbl_map = 0;
4170 	for (i = 0; i < rmem->nr_pages; i++) {
4171 		rmem->pg_arr[i] = NULL;
4172 		rmem->dma_arr[i] = 0;
4173 	}
4174 	*rmem->vmem = NULL;
4175 
4176 	ring = &rxr->rx_agg_ring_struct;
4177 	rmem = &ring->ring_mem;
4178 	rmem->pg_tbl = NULL;
4179 	rmem->pg_tbl_map = 0;
4180 	for (i = 0; i < rmem->nr_pages; i++) {
4181 		rmem->pg_arr[i] = NULL;
4182 		rmem->dma_arr[i] = 0;
4183 	}
4184 	*rmem->vmem = NULL;
4185 }
4186 
4187 static void bnxt_init_ring_struct(struct bnxt *bp)
4188 {
4189 	int i, j;
4190 
4191 	for (i = 0; i < bp->cp_nr_rings; i++) {
4192 		struct bnxt_napi *bnapi = bp->bnapi[i];
4193 		struct bnxt_ring_mem_info *rmem;
4194 		struct bnxt_cp_ring_info *cpr;
4195 		struct bnxt_rx_ring_info *rxr;
4196 		struct bnxt_tx_ring_info *txr;
4197 		struct bnxt_ring_struct *ring;
4198 
4199 		if (!bnapi)
4200 			continue;
4201 
4202 		cpr = &bnapi->cp_ring;
4203 		ring = &cpr->cp_ring_struct;
4204 		rmem = &ring->ring_mem;
4205 		rmem->nr_pages = bp->cp_nr_pages;
4206 		rmem->page_size = HW_CMPD_RING_SIZE;
4207 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4208 		rmem->dma_arr = cpr->cp_desc_mapping;
4209 		rmem->vmem_size = 0;
4210 
4211 		rxr = bnapi->rx_ring;
4212 		if (!rxr)
4213 			goto skip_rx;
4214 
4215 		ring = &rxr->rx_ring_struct;
4216 		rmem = &ring->ring_mem;
4217 		rmem->nr_pages = bp->rx_nr_pages;
4218 		rmem->page_size = HW_RXBD_RING_SIZE;
4219 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4220 		rmem->dma_arr = rxr->rx_desc_mapping;
4221 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4222 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4223 
4224 		ring = &rxr->rx_agg_ring_struct;
4225 		rmem = &ring->ring_mem;
4226 		rmem->nr_pages = bp->rx_agg_nr_pages;
4227 		rmem->page_size = HW_RXBD_RING_SIZE;
4228 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4229 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4230 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4231 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4232 
4233 skip_rx:
4234 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4235 			ring = &txr->tx_ring_struct;
4236 			rmem = &ring->ring_mem;
4237 			rmem->nr_pages = bp->tx_nr_pages;
4238 			rmem->page_size = HW_TXBD_RING_SIZE;
4239 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4240 			rmem->dma_arr = txr->tx_desc_mapping;
4241 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4242 			rmem->vmem = (void **)&txr->tx_buf_ring;
4243 		}
4244 	}
4245 }
4246 
4247 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4248 {
4249 	int i;
4250 	u32 prod;
4251 	struct rx_bd **rx_buf_ring;
4252 
4253 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4254 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4255 		int j;
4256 		struct rx_bd *rxbd;
4257 
4258 		rxbd = rx_buf_ring[i];
4259 		if (!rxbd)
4260 			continue;
4261 
4262 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4263 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4264 			rxbd->rx_bd_opaque = prod;
4265 		}
4266 	}
4267 }
4268 
4269 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4270 				       struct bnxt_rx_ring_info *rxr,
4271 				       int ring_nr)
4272 {
4273 	u32 prod;
4274 	int i;
4275 
4276 	prod = rxr->rx_prod;
4277 	for (i = 0; i < bp->rx_ring_size; i++) {
4278 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4279 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4280 				    ring_nr, i, bp->rx_ring_size);
4281 			break;
4282 		}
4283 		prod = NEXT_RX(prod);
4284 	}
4285 	rxr->rx_prod = prod;
4286 }
4287 
4288 static void bnxt_alloc_one_rx_ring_page(struct bnxt *bp,
4289 					struct bnxt_rx_ring_info *rxr,
4290 					int ring_nr)
4291 {
4292 	u32 prod;
4293 	int i;
4294 
4295 	prod = rxr->rx_agg_prod;
4296 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4297 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
4298 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4299 				    ring_nr, i, bp->rx_ring_size);
4300 			break;
4301 		}
4302 		prod = NEXT_RX_AGG(prod);
4303 	}
4304 	rxr->rx_agg_prod = prod;
4305 }
4306 
4307 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4308 					struct bnxt_rx_ring_info *rxr)
4309 {
4310 	dma_addr_t mapping;
4311 	u8 *data;
4312 	int i;
4313 
4314 	for (i = 0; i < bp->max_tpa; i++) {
4315 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4316 					    GFP_KERNEL);
4317 		if (!data)
4318 			return -ENOMEM;
4319 
4320 		rxr->rx_tpa[i].data = data;
4321 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4322 		rxr->rx_tpa[i].mapping = mapping;
4323 	}
4324 
4325 	return 0;
4326 }
4327 
4328 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4329 {
4330 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4331 	int rc;
4332 
4333 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4334 
4335 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4336 		return 0;
4337 
4338 	bnxt_alloc_one_rx_ring_page(bp, rxr, ring_nr);
4339 
4340 	if (rxr->rx_tpa) {
4341 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4342 		if (rc)
4343 			return rc;
4344 	}
4345 	return 0;
4346 }
4347 
4348 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4349 				       struct bnxt_rx_ring_info *rxr)
4350 {
4351 	struct bnxt_ring_struct *ring;
4352 	u32 type;
4353 
4354 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4355 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4356 
4357 	if (NET_IP_ALIGN == 2)
4358 		type |= RX_BD_FLAGS_SOP;
4359 
4360 	ring = &rxr->rx_ring_struct;
4361 	bnxt_init_rxbd_pages(ring, type);
4362 	ring->fw_ring_id = INVALID_HW_RING_ID;
4363 }
4364 
4365 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4366 					   struct bnxt_rx_ring_info *rxr)
4367 {
4368 	struct bnxt_ring_struct *ring;
4369 	u32 type;
4370 
4371 	ring = &rxr->rx_agg_ring_struct;
4372 	ring->fw_ring_id = INVALID_HW_RING_ID;
4373 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4374 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4375 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4376 
4377 		bnxt_init_rxbd_pages(ring, type);
4378 	}
4379 }
4380 
4381 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4382 {
4383 	struct bnxt_rx_ring_info *rxr;
4384 
4385 	rxr = &bp->rx_ring[ring_nr];
4386 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4387 
4388 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4389 			     &rxr->bnapi->napi);
4390 
4391 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4392 		bpf_prog_add(bp->xdp_prog, 1);
4393 		rxr->xdp_prog = bp->xdp_prog;
4394 	}
4395 
4396 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4397 
4398 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4399 }
4400 
4401 static void bnxt_init_cp_rings(struct bnxt *bp)
4402 {
4403 	int i, j;
4404 
4405 	for (i = 0; i < bp->cp_nr_rings; i++) {
4406 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4407 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4408 
4409 		ring->fw_ring_id = INVALID_HW_RING_ID;
4410 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4411 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4412 		if (!cpr->cp_ring_arr)
4413 			continue;
4414 		for (j = 0; j < cpr->cp_ring_count; j++) {
4415 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4416 
4417 			ring = &cpr2->cp_ring_struct;
4418 			ring->fw_ring_id = INVALID_HW_RING_ID;
4419 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4420 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4421 		}
4422 	}
4423 }
4424 
4425 static int bnxt_init_rx_rings(struct bnxt *bp)
4426 {
4427 	int i, rc = 0;
4428 
4429 	if (BNXT_RX_PAGE_MODE(bp)) {
4430 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4431 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4432 	} else {
4433 		bp->rx_offset = BNXT_RX_OFFSET;
4434 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4435 	}
4436 
4437 	for (i = 0; i < bp->rx_nr_rings; i++) {
4438 		rc = bnxt_init_one_rx_ring(bp, i);
4439 		if (rc)
4440 			break;
4441 	}
4442 
4443 	return rc;
4444 }
4445 
4446 static int bnxt_init_tx_rings(struct bnxt *bp)
4447 {
4448 	u16 i;
4449 
4450 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4451 				   BNXT_MIN_TX_DESC_CNT);
4452 
4453 	for (i = 0; i < bp->tx_nr_rings; i++) {
4454 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4455 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4456 
4457 		ring->fw_ring_id = INVALID_HW_RING_ID;
4458 
4459 		if (i >= bp->tx_nr_rings_xdp)
4460 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4461 					     NETDEV_QUEUE_TYPE_TX,
4462 					     &txr->bnapi->napi);
4463 	}
4464 
4465 	return 0;
4466 }
4467 
4468 static void bnxt_free_ring_grps(struct bnxt *bp)
4469 {
4470 	kfree(bp->grp_info);
4471 	bp->grp_info = NULL;
4472 }
4473 
4474 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4475 {
4476 	int i;
4477 
4478 	if (irq_re_init) {
4479 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4480 				       sizeof(struct bnxt_ring_grp_info),
4481 				       GFP_KERNEL);
4482 		if (!bp->grp_info)
4483 			return -ENOMEM;
4484 	}
4485 	for (i = 0; i < bp->cp_nr_rings; i++) {
4486 		if (irq_re_init)
4487 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4488 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4489 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4490 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4491 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4492 	}
4493 	return 0;
4494 }
4495 
4496 static void bnxt_free_vnics(struct bnxt *bp)
4497 {
4498 	kfree(bp->vnic_info);
4499 	bp->vnic_info = NULL;
4500 	bp->nr_vnics = 0;
4501 }
4502 
4503 static int bnxt_alloc_vnics(struct bnxt *bp)
4504 {
4505 	int num_vnics = 1;
4506 
4507 #ifdef CONFIG_RFS_ACCEL
4508 	if (bp->flags & BNXT_FLAG_RFS) {
4509 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4510 			num_vnics++;
4511 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4512 			num_vnics += bp->rx_nr_rings;
4513 	}
4514 #endif
4515 
4516 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4517 		num_vnics++;
4518 
4519 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4520 				GFP_KERNEL);
4521 	if (!bp->vnic_info)
4522 		return -ENOMEM;
4523 
4524 	bp->nr_vnics = num_vnics;
4525 	return 0;
4526 }
4527 
4528 static void bnxt_init_vnics(struct bnxt *bp)
4529 {
4530 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4531 	int i;
4532 
4533 	for (i = 0; i < bp->nr_vnics; i++) {
4534 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4535 		int j;
4536 
4537 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4538 		vnic->vnic_id = i;
4539 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4540 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4541 
4542 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4543 
4544 		if (bp->vnic_info[i].rss_hash_key) {
4545 			if (i == BNXT_VNIC_DEFAULT) {
4546 				u8 *key = (void *)vnic->rss_hash_key;
4547 				int k;
4548 
4549 				if (!bp->rss_hash_key_valid &&
4550 				    !bp->rss_hash_key_updated) {
4551 					get_random_bytes(bp->rss_hash_key,
4552 							 HW_HASH_KEY_SIZE);
4553 					bp->rss_hash_key_updated = true;
4554 				}
4555 
4556 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4557 				       HW_HASH_KEY_SIZE);
4558 
4559 				if (!bp->rss_hash_key_updated)
4560 					continue;
4561 
4562 				bp->rss_hash_key_updated = false;
4563 				bp->rss_hash_key_valid = true;
4564 
4565 				bp->toeplitz_prefix = 0;
4566 				for (k = 0; k < 8; k++) {
4567 					bp->toeplitz_prefix <<= 8;
4568 					bp->toeplitz_prefix |= key[k];
4569 				}
4570 			} else {
4571 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4572 				       HW_HASH_KEY_SIZE);
4573 			}
4574 		}
4575 	}
4576 }
4577 
4578 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4579 {
4580 	int pages;
4581 
4582 	pages = ring_size / desc_per_pg;
4583 
4584 	if (!pages)
4585 		return 1;
4586 
4587 	pages++;
4588 
4589 	while (pages & (pages - 1))
4590 		pages++;
4591 
4592 	return pages;
4593 }
4594 
4595 void bnxt_set_tpa_flags(struct bnxt *bp)
4596 {
4597 	bp->flags &= ~BNXT_FLAG_TPA;
4598 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4599 		return;
4600 	if (bp->dev->features & NETIF_F_LRO)
4601 		bp->flags |= BNXT_FLAG_LRO;
4602 	else if (bp->dev->features & NETIF_F_GRO_HW)
4603 		bp->flags |= BNXT_FLAG_GRO;
4604 }
4605 
4606 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4607  * be set on entry.
4608  */
4609 void bnxt_set_ring_params(struct bnxt *bp)
4610 {
4611 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4612 	u32 agg_factor = 0, agg_ring_size = 0;
4613 
4614 	/* 8 for CRC and VLAN */
4615 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4616 
4617 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4618 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4619 
4620 	bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
4621 	ring_size = bp->rx_ring_size;
4622 	bp->rx_agg_ring_size = 0;
4623 	bp->rx_agg_nr_pages = 0;
4624 
4625 	if (bp->flags & BNXT_FLAG_TPA)
4626 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4627 
4628 	bp->flags &= ~BNXT_FLAG_JUMBO;
4629 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4630 		u32 jumbo_factor;
4631 
4632 		bp->flags |= BNXT_FLAG_JUMBO;
4633 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4634 		if (jumbo_factor > agg_factor)
4635 			agg_factor = jumbo_factor;
4636 	}
4637 	if (agg_factor) {
4638 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4639 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4640 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4641 				    bp->rx_ring_size, ring_size);
4642 			bp->rx_ring_size = ring_size;
4643 		}
4644 		agg_ring_size = ring_size * agg_factor;
4645 
4646 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4647 							RX_DESC_CNT);
4648 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4649 			u32 tmp = agg_ring_size;
4650 
4651 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4652 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4653 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4654 				    tmp, agg_ring_size);
4655 		}
4656 		bp->rx_agg_ring_size = agg_ring_size;
4657 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4658 
4659 		if (BNXT_RX_PAGE_MODE(bp)) {
4660 			rx_space = PAGE_SIZE;
4661 			rx_size = PAGE_SIZE -
4662 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4663 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4664 		} else {
4665 			rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
4666 			rx_space = rx_size + NET_SKB_PAD +
4667 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4668 		}
4669 	}
4670 
4671 	bp->rx_buf_use_size = rx_size;
4672 	bp->rx_buf_size = rx_space;
4673 
4674 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4675 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4676 
4677 	ring_size = bp->tx_ring_size;
4678 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4679 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4680 
4681 	max_rx_cmpl = bp->rx_ring_size;
4682 	/* MAX TPA needs to be added because TPA_START completions are
4683 	 * immediately recycled, so the TPA completions are not bound by
4684 	 * the RX ring size.
4685 	 */
4686 	if (bp->flags & BNXT_FLAG_TPA)
4687 		max_rx_cmpl += bp->max_tpa;
4688 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4689 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4690 	bp->cp_ring_size = ring_size;
4691 
4692 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4693 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4694 		bp->cp_nr_pages = MAX_CP_PAGES;
4695 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4696 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4697 			    ring_size, bp->cp_ring_size);
4698 	}
4699 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4700 	bp->cp_ring_mask = bp->cp_bit - 1;
4701 }
4702 
4703 /* Changing allocation mode of RX rings.
4704  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4705  */
4706 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4707 {
4708 	struct net_device *dev = bp->dev;
4709 
4710 	if (page_mode) {
4711 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4712 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4713 
4714 		if (bp->xdp_prog->aux->xdp_has_frags)
4715 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4716 		else
4717 			dev->max_mtu =
4718 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4719 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4720 			bp->flags |= BNXT_FLAG_JUMBO;
4721 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4722 		} else {
4723 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4724 			bp->rx_skb_func = bnxt_rx_page_skb;
4725 		}
4726 		bp->rx_dir = DMA_BIDIRECTIONAL;
4727 		/* Disable LRO or GRO_HW */
4728 		netdev_update_features(dev);
4729 	} else {
4730 		dev->max_mtu = bp->max_mtu;
4731 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4732 		bp->rx_dir = DMA_FROM_DEVICE;
4733 		bp->rx_skb_func = bnxt_rx_skb;
4734 	}
4735 	return 0;
4736 }
4737 
4738 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4739 {
4740 	int i;
4741 	struct bnxt_vnic_info *vnic;
4742 	struct pci_dev *pdev = bp->pdev;
4743 
4744 	if (!bp->vnic_info)
4745 		return;
4746 
4747 	for (i = 0; i < bp->nr_vnics; i++) {
4748 		vnic = &bp->vnic_info[i];
4749 
4750 		kfree(vnic->fw_grp_ids);
4751 		vnic->fw_grp_ids = NULL;
4752 
4753 		kfree(vnic->uc_list);
4754 		vnic->uc_list = NULL;
4755 
4756 		if (vnic->mc_list) {
4757 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4758 					  vnic->mc_list, vnic->mc_list_mapping);
4759 			vnic->mc_list = NULL;
4760 		}
4761 
4762 		if (vnic->rss_table) {
4763 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4764 					  vnic->rss_table,
4765 					  vnic->rss_table_dma_addr);
4766 			vnic->rss_table = NULL;
4767 		}
4768 
4769 		vnic->rss_hash_key = NULL;
4770 		vnic->flags = 0;
4771 	}
4772 }
4773 
4774 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4775 {
4776 	int i, rc = 0, size;
4777 	struct bnxt_vnic_info *vnic;
4778 	struct pci_dev *pdev = bp->pdev;
4779 	int max_rings;
4780 
4781 	for (i = 0; i < bp->nr_vnics; i++) {
4782 		vnic = &bp->vnic_info[i];
4783 
4784 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4785 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4786 
4787 			if (mem_size > 0) {
4788 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4789 				if (!vnic->uc_list) {
4790 					rc = -ENOMEM;
4791 					goto out;
4792 				}
4793 			}
4794 		}
4795 
4796 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4797 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4798 			vnic->mc_list =
4799 				dma_alloc_coherent(&pdev->dev,
4800 						   vnic->mc_list_size,
4801 						   &vnic->mc_list_mapping,
4802 						   GFP_KERNEL);
4803 			if (!vnic->mc_list) {
4804 				rc = -ENOMEM;
4805 				goto out;
4806 			}
4807 		}
4808 
4809 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4810 			goto vnic_skip_grps;
4811 
4812 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4813 			max_rings = bp->rx_nr_rings;
4814 		else
4815 			max_rings = 1;
4816 
4817 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4818 		if (!vnic->fw_grp_ids) {
4819 			rc = -ENOMEM;
4820 			goto out;
4821 		}
4822 vnic_skip_grps:
4823 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4824 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4825 			continue;
4826 
4827 		/* Allocate rss table and hash key */
4828 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4829 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4830 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4831 
4832 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4833 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4834 						     vnic->rss_table_size,
4835 						     &vnic->rss_table_dma_addr,
4836 						     GFP_KERNEL);
4837 		if (!vnic->rss_table) {
4838 			rc = -ENOMEM;
4839 			goto out;
4840 		}
4841 
4842 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4843 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4844 	}
4845 	return 0;
4846 
4847 out:
4848 	return rc;
4849 }
4850 
4851 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4852 {
4853 	struct bnxt_hwrm_wait_token *token;
4854 
4855 	dma_pool_destroy(bp->hwrm_dma_pool);
4856 	bp->hwrm_dma_pool = NULL;
4857 
4858 	rcu_read_lock();
4859 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4860 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4861 	rcu_read_unlock();
4862 }
4863 
4864 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4865 {
4866 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4867 					    BNXT_HWRM_DMA_SIZE,
4868 					    BNXT_HWRM_DMA_ALIGN, 0);
4869 	if (!bp->hwrm_dma_pool)
4870 		return -ENOMEM;
4871 
4872 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4873 
4874 	return 0;
4875 }
4876 
4877 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4878 {
4879 	kfree(stats->hw_masks);
4880 	stats->hw_masks = NULL;
4881 	kfree(stats->sw_stats);
4882 	stats->sw_stats = NULL;
4883 	if (stats->hw_stats) {
4884 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4885 				  stats->hw_stats_map);
4886 		stats->hw_stats = NULL;
4887 	}
4888 }
4889 
4890 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4891 				bool alloc_masks)
4892 {
4893 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4894 					     &stats->hw_stats_map, GFP_KERNEL);
4895 	if (!stats->hw_stats)
4896 		return -ENOMEM;
4897 
4898 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4899 	if (!stats->sw_stats)
4900 		goto stats_mem_err;
4901 
4902 	if (alloc_masks) {
4903 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4904 		if (!stats->hw_masks)
4905 			goto stats_mem_err;
4906 	}
4907 	return 0;
4908 
4909 stats_mem_err:
4910 	bnxt_free_stats_mem(bp, stats);
4911 	return -ENOMEM;
4912 }
4913 
4914 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4915 {
4916 	int i;
4917 
4918 	for (i = 0; i < count; i++)
4919 		mask_arr[i] = mask;
4920 }
4921 
4922 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4923 {
4924 	int i;
4925 
4926 	for (i = 0; i < count; i++)
4927 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4928 }
4929 
4930 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4931 				    struct bnxt_stats_mem *stats)
4932 {
4933 	struct hwrm_func_qstats_ext_output *resp;
4934 	struct hwrm_func_qstats_ext_input *req;
4935 	__le64 *hw_masks;
4936 	int rc;
4937 
4938 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4939 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4940 		return -EOPNOTSUPP;
4941 
4942 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4943 	if (rc)
4944 		return rc;
4945 
4946 	req->fid = cpu_to_le16(0xffff);
4947 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4948 
4949 	resp = hwrm_req_hold(bp, req);
4950 	rc = hwrm_req_send(bp, req);
4951 	if (!rc) {
4952 		hw_masks = &resp->rx_ucast_pkts;
4953 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4954 	}
4955 	hwrm_req_drop(bp, req);
4956 	return rc;
4957 }
4958 
4959 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4960 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4961 
4962 static void bnxt_init_stats(struct bnxt *bp)
4963 {
4964 	struct bnxt_napi *bnapi = bp->bnapi[0];
4965 	struct bnxt_cp_ring_info *cpr;
4966 	struct bnxt_stats_mem *stats;
4967 	__le64 *rx_stats, *tx_stats;
4968 	int rc, rx_count, tx_count;
4969 	u64 *rx_masks, *tx_masks;
4970 	u64 mask;
4971 	u8 flags;
4972 
4973 	cpr = &bnapi->cp_ring;
4974 	stats = &cpr->stats;
4975 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4976 	if (rc) {
4977 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4978 			mask = (1ULL << 48) - 1;
4979 		else
4980 			mask = -1ULL;
4981 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
4982 	}
4983 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
4984 		stats = &bp->port_stats;
4985 		rx_stats = stats->hw_stats;
4986 		rx_masks = stats->hw_masks;
4987 		rx_count = sizeof(struct rx_port_stats) / 8;
4988 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4989 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4990 		tx_count = sizeof(struct tx_port_stats) / 8;
4991 
4992 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
4993 		rc = bnxt_hwrm_port_qstats(bp, flags);
4994 		if (rc) {
4995 			mask = (1ULL << 40) - 1;
4996 
4997 			bnxt_fill_masks(rx_masks, mask, rx_count);
4998 			bnxt_fill_masks(tx_masks, mask, tx_count);
4999 		} else {
5000 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5001 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5002 			bnxt_hwrm_port_qstats(bp, 0);
5003 		}
5004 	}
5005 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5006 		stats = &bp->rx_port_stats_ext;
5007 		rx_stats = stats->hw_stats;
5008 		rx_masks = stats->hw_masks;
5009 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5010 		stats = &bp->tx_port_stats_ext;
5011 		tx_stats = stats->hw_stats;
5012 		tx_masks = stats->hw_masks;
5013 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5014 
5015 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5016 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5017 		if (rc) {
5018 			mask = (1ULL << 40) - 1;
5019 
5020 			bnxt_fill_masks(rx_masks, mask, rx_count);
5021 			if (tx_stats)
5022 				bnxt_fill_masks(tx_masks, mask, tx_count);
5023 		} else {
5024 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5025 			if (tx_stats)
5026 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5027 						   tx_count);
5028 			bnxt_hwrm_port_qstats_ext(bp, 0);
5029 		}
5030 	}
5031 }
5032 
5033 static void bnxt_free_port_stats(struct bnxt *bp)
5034 {
5035 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5036 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5037 
5038 	bnxt_free_stats_mem(bp, &bp->port_stats);
5039 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5040 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5041 }
5042 
5043 static void bnxt_free_ring_stats(struct bnxt *bp)
5044 {
5045 	int i;
5046 
5047 	if (!bp->bnapi)
5048 		return;
5049 
5050 	for (i = 0; i < bp->cp_nr_rings; i++) {
5051 		struct bnxt_napi *bnapi = bp->bnapi[i];
5052 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5053 
5054 		bnxt_free_stats_mem(bp, &cpr->stats);
5055 
5056 		kfree(cpr->sw_stats);
5057 		cpr->sw_stats = NULL;
5058 	}
5059 }
5060 
5061 static int bnxt_alloc_stats(struct bnxt *bp)
5062 {
5063 	u32 size, i;
5064 	int rc;
5065 
5066 	size = bp->hw_ring_stats_size;
5067 
5068 	for (i = 0; i < bp->cp_nr_rings; i++) {
5069 		struct bnxt_napi *bnapi = bp->bnapi[i];
5070 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5071 
5072 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
5073 		if (!cpr->sw_stats)
5074 			return -ENOMEM;
5075 
5076 		cpr->stats.len = size;
5077 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5078 		if (rc)
5079 			return rc;
5080 
5081 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5082 	}
5083 
5084 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5085 		return 0;
5086 
5087 	if (bp->port_stats.hw_stats)
5088 		goto alloc_ext_stats;
5089 
5090 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5091 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5092 	if (rc)
5093 		return rc;
5094 
5095 	bp->flags |= BNXT_FLAG_PORT_STATS;
5096 
5097 alloc_ext_stats:
5098 	/* Display extended statistics only if FW supports it */
5099 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5100 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5101 			return 0;
5102 
5103 	if (bp->rx_port_stats_ext.hw_stats)
5104 		goto alloc_tx_ext_stats;
5105 
5106 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5107 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5108 	/* Extended stats are optional */
5109 	if (rc)
5110 		return 0;
5111 
5112 alloc_tx_ext_stats:
5113 	if (bp->tx_port_stats_ext.hw_stats)
5114 		return 0;
5115 
5116 	if (bp->hwrm_spec_code >= 0x10902 ||
5117 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5118 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5119 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5120 		/* Extended stats are optional */
5121 		if (rc)
5122 			return 0;
5123 	}
5124 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5125 	return 0;
5126 }
5127 
5128 static void bnxt_clear_ring_indices(struct bnxt *bp)
5129 {
5130 	int i, j;
5131 
5132 	if (!bp->bnapi)
5133 		return;
5134 
5135 	for (i = 0; i < bp->cp_nr_rings; i++) {
5136 		struct bnxt_napi *bnapi = bp->bnapi[i];
5137 		struct bnxt_cp_ring_info *cpr;
5138 		struct bnxt_rx_ring_info *rxr;
5139 		struct bnxt_tx_ring_info *txr;
5140 
5141 		if (!bnapi)
5142 			continue;
5143 
5144 		cpr = &bnapi->cp_ring;
5145 		cpr->cp_raw_cons = 0;
5146 
5147 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5148 			txr->tx_prod = 0;
5149 			txr->tx_cons = 0;
5150 			txr->tx_hw_cons = 0;
5151 		}
5152 
5153 		rxr = bnapi->rx_ring;
5154 		if (rxr) {
5155 			rxr->rx_prod = 0;
5156 			rxr->rx_agg_prod = 0;
5157 			rxr->rx_sw_agg_prod = 0;
5158 			rxr->rx_next_cons = 0;
5159 		}
5160 		bnapi->events = 0;
5161 	}
5162 }
5163 
5164 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5165 {
5166 	u8 type = fltr->type, flags = fltr->flags;
5167 
5168 	INIT_LIST_HEAD(&fltr->list);
5169 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5170 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5171 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5172 }
5173 
5174 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5175 {
5176 	if (!list_empty(&fltr->list))
5177 		list_del_init(&fltr->list);
5178 }
5179 
5180 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5181 {
5182 	struct bnxt_filter_base *usr_fltr, *tmp;
5183 
5184 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5185 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5186 			continue;
5187 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5188 	}
5189 }
5190 
5191 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5192 {
5193 	hlist_del(&fltr->hash);
5194 	bnxt_del_one_usr_fltr(bp, fltr);
5195 	if (fltr->flags) {
5196 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5197 		bp->ntp_fltr_count--;
5198 	}
5199 	kfree(fltr);
5200 }
5201 
5202 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5203 {
5204 	int i;
5205 
5206 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
5207 	 * safe to delete the hash table.
5208 	 */
5209 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5210 		struct hlist_head *head;
5211 		struct hlist_node *tmp;
5212 		struct bnxt_ntuple_filter *fltr;
5213 
5214 		head = &bp->ntp_fltr_hash_tbl[i];
5215 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5216 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5217 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5218 				     !list_empty(&fltr->base.list)))
5219 				continue;
5220 			bnxt_del_fltr(bp, &fltr->base);
5221 		}
5222 	}
5223 	if (!all)
5224 		return;
5225 
5226 	bitmap_free(bp->ntp_fltr_bmap);
5227 	bp->ntp_fltr_bmap = NULL;
5228 	bp->ntp_fltr_count = 0;
5229 }
5230 
5231 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5232 {
5233 	int i, rc = 0;
5234 
5235 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5236 		return 0;
5237 
5238 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5239 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5240 
5241 	bp->ntp_fltr_count = 0;
5242 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5243 
5244 	if (!bp->ntp_fltr_bmap)
5245 		rc = -ENOMEM;
5246 
5247 	return rc;
5248 }
5249 
5250 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5251 {
5252 	int i;
5253 
5254 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5255 		struct hlist_head *head;
5256 		struct hlist_node *tmp;
5257 		struct bnxt_l2_filter *fltr;
5258 
5259 		head = &bp->l2_fltr_hash_tbl[i];
5260 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5261 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5262 				     !list_empty(&fltr->base.list)))
5263 				continue;
5264 			bnxt_del_fltr(bp, &fltr->base);
5265 		}
5266 	}
5267 }
5268 
5269 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5270 {
5271 	int i;
5272 
5273 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5274 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5275 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5276 }
5277 
5278 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5279 {
5280 	bnxt_free_vnic_attributes(bp);
5281 	bnxt_free_tx_rings(bp);
5282 	bnxt_free_rx_rings(bp);
5283 	bnxt_free_cp_rings(bp);
5284 	bnxt_free_all_cp_arrays(bp);
5285 	bnxt_free_ntp_fltrs(bp, false);
5286 	bnxt_free_l2_filters(bp, false);
5287 	if (irq_re_init) {
5288 		bnxt_free_ring_stats(bp);
5289 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5290 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5291 			bnxt_free_port_stats(bp);
5292 		bnxt_free_ring_grps(bp);
5293 		bnxt_free_vnics(bp);
5294 		kfree(bp->tx_ring_map);
5295 		bp->tx_ring_map = NULL;
5296 		kfree(bp->tx_ring);
5297 		bp->tx_ring = NULL;
5298 		kfree(bp->rx_ring);
5299 		bp->rx_ring = NULL;
5300 		kfree(bp->bnapi);
5301 		bp->bnapi = NULL;
5302 	} else {
5303 		bnxt_clear_ring_indices(bp);
5304 	}
5305 }
5306 
5307 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5308 {
5309 	int i, j, rc, size, arr_size;
5310 	void *bnapi;
5311 
5312 	if (irq_re_init) {
5313 		/* Allocate bnapi mem pointer array and mem block for
5314 		 * all queues
5315 		 */
5316 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5317 				bp->cp_nr_rings);
5318 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5319 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5320 		if (!bnapi)
5321 			return -ENOMEM;
5322 
5323 		bp->bnapi = bnapi;
5324 		bnapi += arr_size;
5325 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5326 			bp->bnapi[i] = bnapi;
5327 			bp->bnapi[i]->index = i;
5328 			bp->bnapi[i]->bp = bp;
5329 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5330 				struct bnxt_cp_ring_info *cpr =
5331 					&bp->bnapi[i]->cp_ring;
5332 
5333 				cpr->cp_ring_struct.ring_mem.flags =
5334 					BNXT_RMEM_RING_PTE_FLAG;
5335 			}
5336 		}
5337 
5338 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5339 				      sizeof(struct bnxt_rx_ring_info),
5340 				      GFP_KERNEL);
5341 		if (!bp->rx_ring)
5342 			return -ENOMEM;
5343 
5344 		for (i = 0; i < bp->rx_nr_rings; i++) {
5345 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5346 
5347 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5348 				rxr->rx_ring_struct.ring_mem.flags =
5349 					BNXT_RMEM_RING_PTE_FLAG;
5350 				rxr->rx_agg_ring_struct.ring_mem.flags =
5351 					BNXT_RMEM_RING_PTE_FLAG;
5352 			} else {
5353 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5354 			}
5355 			rxr->bnapi = bp->bnapi[i];
5356 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5357 		}
5358 
5359 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5360 				      sizeof(struct bnxt_tx_ring_info),
5361 				      GFP_KERNEL);
5362 		if (!bp->tx_ring)
5363 			return -ENOMEM;
5364 
5365 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5366 					  GFP_KERNEL);
5367 
5368 		if (!bp->tx_ring_map)
5369 			return -ENOMEM;
5370 
5371 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5372 			j = 0;
5373 		else
5374 			j = bp->rx_nr_rings;
5375 
5376 		for (i = 0; i < bp->tx_nr_rings; i++) {
5377 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5378 			struct bnxt_napi *bnapi2;
5379 
5380 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5381 				txr->tx_ring_struct.ring_mem.flags =
5382 					BNXT_RMEM_RING_PTE_FLAG;
5383 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5384 			if (i >= bp->tx_nr_rings_xdp) {
5385 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5386 
5387 				bnapi2 = bp->bnapi[k];
5388 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5389 				txr->tx_napi_idx =
5390 					BNXT_RING_TO_TC(bp, txr->txq_index);
5391 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5392 				bnapi2->tx_int = bnxt_tx_int;
5393 			} else {
5394 				bnapi2 = bp->bnapi[j];
5395 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5396 				bnapi2->tx_ring[0] = txr;
5397 				bnapi2->tx_int = bnxt_tx_int_xdp;
5398 				j++;
5399 			}
5400 			txr->bnapi = bnapi2;
5401 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5402 				txr->tx_cpr = &bnapi2->cp_ring;
5403 		}
5404 
5405 		rc = bnxt_alloc_stats(bp);
5406 		if (rc)
5407 			goto alloc_mem_err;
5408 		bnxt_init_stats(bp);
5409 
5410 		rc = bnxt_alloc_ntp_fltrs(bp);
5411 		if (rc)
5412 			goto alloc_mem_err;
5413 
5414 		rc = bnxt_alloc_vnics(bp);
5415 		if (rc)
5416 			goto alloc_mem_err;
5417 	}
5418 
5419 	rc = bnxt_alloc_all_cp_arrays(bp);
5420 	if (rc)
5421 		goto alloc_mem_err;
5422 
5423 	bnxt_init_ring_struct(bp);
5424 
5425 	rc = bnxt_alloc_rx_rings(bp);
5426 	if (rc)
5427 		goto alloc_mem_err;
5428 
5429 	rc = bnxt_alloc_tx_rings(bp);
5430 	if (rc)
5431 		goto alloc_mem_err;
5432 
5433 	rc = bnxt_alloc_cp_rings(bp);
5434 	if (rc)
5435 		goto alloc_mem_err;
5436 
5437 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5438 						  BNXT_VNIC_MCAST_FLAG |
5439 						  BNXT_VNIC_UCAST_FLAG;
5440 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5441 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5442 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5443 
5444 	rc = bnxt_alloc_vnic_attributes(bp);
5445 	if (rc)
5446 		goto alloc_mem_err;
5447 	return 0;
5448 
5449 alloc_mem_err:
5450 	bnxt_free_mem(bp, true);
5451 	return rc;
5452 }
5453 
5454 static void bnxt_disable_int(struct bnxt *bp)
5455 {
5456 	int i;
5457 
5458 	if (!bp->bnapi)
5459 		return;
5460 
5461 	for (i = 0; i < bp->cp_nr_rings; i++) {
5462 		struct bnxt_napi *bnapi = bp->bnapi[i];
5463 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5464 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5465 
5466 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5467 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5468 	}
5469 }
5470 
5471 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5472 {
5473 	struct bnxt_napi *bnapi = bp->bnapi[n];
5474 	struct bnxt_cp_ring_info *cpr;
5475 
5476 	cpr = &bnapi->cp_ring;
5477 	return cpr->cp_ring_struct.map_idx;
5478 }
5479 
5480 static void bnxt_disable_int_sync(struct bnxt *bp)
5481 {
5482 	int i;
5483 
5484 	if (!bp->irq_tbl)
5485 		return;
5486 
5487 	atomic_inc(&bp->intr_sem);
5488 
5489 	bnxt_disable_int(bp);
5490 	for (i = 0; i < bp->cp_nr_rings; i++) {
5491 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5492 
5493 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5494 	}
5495 }
5496 
5497 static void bnxt_enable_int(struct bnxt *bp)
5498 {
5499 	int i;
5500 
5501 	atomic_set(&bp->intr_sem, 0);
5502 	for (i = 0; i < bp->cp_nr_rings; i++) {
5503 		struct bnxt_napi *bnapi = bp->bnapi[i];
5504 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5505 
5506 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5507 	}
5508 }
5509 
5510 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5511 			    bool async_only)
5512 {
5513 	DECLARE_BITMAP(async_events_bmap, 256);
5514 	u32 *events = (u32 *)async_events_bmap;
5515 	struct hwrm_func_drv_rgtr_output *resp;
5516 	struct hwrm_func_drv_rgtr_input *req;
5517 	u32 flags;
5518 	int rc, i;
5519 
5520 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5521 	if (rc)
5522 		return rc;
5523 
5524 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5525 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5526 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5527 
5528 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5529 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5530 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5531 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5532 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5533 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5534 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5535 	req->flags = cpu_to_le32(flags);
5536 	req->ver_maj_8b = DRV_VER_MAJ;
5537 	req->ver_min_8b = DRV_VER_MIN;
5538 	req->ver_upd_8b = DRV_VER_UPD;
5539 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5540 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5541 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5542 
5543 	if (BNXT_PF(bp)) {
5544 		u32 data[8];
5545 		int i;
5546 
5547 		memset(data, 0, sizeof(data));
5548 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5549 			u16 cmd = bnxt_vf_req_snif[i];
5550 			unsigned int bit, idx;
5551 
5552 			idx = cmd / 32;
5553 			bit = cmd % 32;
5554 			data[idx] |= 1 << bit;
5555 		}
5556 
5557 		for (i = 0; i < 8; i++)
5558 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5559 
5560 		req->enables |=
5561 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5562 	}
5563 
5564 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5565 		req->flags |= cpu_to_le32(
5566 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5567 
5568 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5569 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5570 		u16 event_id = bnxt_async_events_arr[i];
5571 
5572 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5573 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5574 			continue;
5575 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5576 		    !bp->ptp_cfg)
5577 			continue;
5578 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5579 	}
5580 	if (bmap && bmap_size) {
5581 		for (i = 0; i < bmap_size; i++) {
5582 			if (test_bit(i, bmap))
5583 				__set_bit(i, async_events_bmap);
5584 		}
5585 	}
5586 	for (i = 0; i < 8; i++)
5587 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5588 
5589 	if (async_only)
5590 		req->enables =
5591 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5592 
5593 	resp = hwrm_req_hold(bp, req);
5594 	rc = hwrm_req_send(bp, req);
5595 	if (!rc) {
5596 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5597 		if (resp->flags &
5598 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5599 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5600 	}
5601 	hwrm_req_drop(bp, req);
5602 	return rc;
5603 }
5604 
5605 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5606 {
5607 	struct hwrm_func_drv_unrgtr_input *req;
5608 	int rc;
5609 
5610 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5611 		return 0;
5612 
5613 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5614 	if (rc)
5615 		return rc;
5616 	return hwrm_req_send(bp, req);
5617 }
5618 
5619 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5620 
5621 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5622 {
5623 	struct hwrm_tunnel_dst_port_free_input *req;
5624 	int rc;
5625 
5626 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5627 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5628 		return 0;
5629 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5630 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5631 		return 0;
5632 
5633 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5634 	if (rc)
5635 		return rc;
5636 
5637 	req->tunnel_type = tunnel_type;
5638 
5639 	switch (tunnel_type) {
5640 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5641 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5642 		bp->vxlan_port = 0;
5643 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5644 		break;
5645 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5646 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5647 		bp->nge_port = 0;
5648 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5649 		break;
5650 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5651 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5652 		bp->vxlan_gpe_port = 0;
5653 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5654 		break;
5655 	default:
5656 		break;
5657 	}
5658 
5659 	rc = hwrm_req_send(bp, req);
5660 	if (rc)
5661 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5662 			   rc);
5663 	if (bp->flags & BNXT_FLAG_TPA)
5664 		bnxt_set_tpa(bp, true);
5665 	return rc;
5666 }
5667 
5668 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5669 					   u8 tunnel_type)
5670 {
5671 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5672 	struct hwrm_tunnel_dst_port_alloc_input *req;
5673 	int rc;
5674 
5675 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5676 	if (rc)
5677 		return rc;
5678 
5679 	req->tunnel_type = tunnel_type;
5680 	req->tunnel_dst_port_val = port;
5681 
5682 	resp = hwrm_req_hold(bp, req);
5683 	rc = hwrm_req_send(bp, req);
5684 	if (rc) {
5685 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5686 			   rc);
5687 		goto err_out;
5688 	}
5689 
5690 	switch (tunnel_type) {
5691 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5692 		bp->vxlan_port = port;
5693 		bp->vxlan_fw_dst_port_id =
5694 			le16_to_cpu(resp->tunnel_dst_port_id);
5695 		break;
5696 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5697 		bp->nge_port = port;
5698 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5699 		break;
5700 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5701 		bp->vxlan_gpe_port = port;
5702 		bp->vxlan_gpe_fw_dst_port_id =
5703 			le16_to_cpu(resp->tunnel_dst_port_id);
5704 		break;
5705 	default:
5706 		break;
5707 	}
5708 	if (bp->flags & BNXT_FLAG_TPA)
5709 		bnxt_set_tpa(bp, true);
5710 
5711 err_out:
5712 	hwrm_req_drop(bp, req);
5713 	return rc;
5714 }
5715 
5716 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5717 {
5718 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5719 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5720 	int rc;
5721 
5722 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5723 	if (rc)
5724 		return rc;
5725 
5726 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5727 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5728 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5729 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5730 	}
5731 	req->mask = cpu_to_le32(vnic->rx_mask);
5732 	return hwrm_req_send_silent(bp, req);
5733 }
5734 
5735 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5736 {
5737 	if (!atomic_dec_and_test(&fltr->refcnt))
5738 		return;
5739 	spin_lock_bh(&bp->ntp_fltr_lock);
5740 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5741 		spin_unlock_bh(&bp->ntp_fltr_lock);
5742 		return;
5743 	}
5744 	hlist_del_rcu(&fltr->base.hash);
5745 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5746 	if (fltr->base.flags) {
5747 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5748 		bp->ntp_fltr_count--;
5749 	}
5750 	spin_unlock_bh(&bp->ntp_fltr_lock);
5751 	kfree_rcu(fltr, base.rcu);
5752 }
5753 
5754 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5755 						      struct bnxt_l2_key *key,
5756 						      u32 idx)
5757 {
5758 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5759 	struct bnxt_l2_filter *fltr;
5760 
5761 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5762 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5763 
5764 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5765 		    l2_key->vlan == key->vlan)
5766 			return fltr;
5767 	}
5768 	return NULL;
5769 }
5770 
5771 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5772 						    struct bnxt_l2_key *key,
5773 						    u32 idx)
5774 {
5775 	struct bnxt_l2_filter *fltr = NULL;
5776 
5777 	rcu_read_lock();
5778 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5779 	if (fltr)
5780 		atomic_inc(&fltr->refcnt);
5781 	rcu_read_unlock();
5782 	return fltr;
5783 }
5784 
5785 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5786 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5787 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5788 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5789 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5790 
5791 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5792 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5793 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5794 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5795 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5796 
5797 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5798 {
5799 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5800 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5801 			return sizeof(fkeys->addrs.v4addrs) +
5802 			       sizeof(fkeys->ports);
5803 
5804 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5805 			return sizeof(fkeys->addrs.v4addrs);
5806 	}
5807 
5808 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5809 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5810 			return sizeof(fkeys->addrs.v6addrs) +
5811 			       sizeof(fkeys->ports);
5812 
5813 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5814 			return sizeof(fkeys->addrs.v6addrs);
5815 	}
5816 
5817 	return 0;
5818 }
5819 
5820 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5821 			 const unsigned char *key)
5822 {
5823 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5824 	struct bnxt_ipv4_tuple tuple4;
5825 	struct bnxt_ipv6_tuple tuple6;
5826 	int i, j, len = 0;
5827 	u8 *four_tuple;
5828 
5829 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5830 	if (!len)
5831 		return 0;
5832 
5833 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5834 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5835 		tuple4.ports = fkeys->ports;
5836 		four_tuple = (unsigned char *)&tuple4;
5837 	} else {
5838 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5839 		tuple6.ports = fkeys->ports;
5840 		four_tuple = (unsigned char *)&tuple6;
5841 	}
5842 
5843 	for (i = 0, j = 8; i < len; i++, j++) {
5844 		u8 byte = four_tuple[i];
5845 		int bit;
5846 
5847 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5848 			if (byte & 0x80)
5849 				hash ^= prefix;
5850 		}
5851 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5852 	}
5853 
5854 	/* The valid part of the hash is in the upper 32 bits. */
5855 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5856 }
5857 
5858 #ifdef CONFIG_RFS_ACCEL
5859 static struct bnxt_l2_filter *
5860 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5861 {
5862 	struct bnxt_l2_filter *fltr;
5863 	u32 idx;
5864 
5865 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5866 	      BNXT_L2_FLTR_HASH_MASK;
5867 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5868 	return fltr;
5869 }
5870 #endif
5871 
5872 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5873 			       struct bnxt_l2_key *key, u32 idx)
5874 {
5875 	struct hlist_head *head;
5876 
5877 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5878 	fltr->l2_key.vlan = key->vlan;
5879 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5880 	if (fltr->base.flags) {
5881 		int bit_id;
5882 
5883 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5884 						 bp->max_fltr, 0);
5885 		if (bit_id < 0)
5886 			return -ENOMEM;
5887 		fltr->base.sw_id = (u16)bit_id;
5888 		bp->ntp_fltr_count++;
5889 	}
5890 	head = &bp->l2_fltr_hash_tbl[idx];
5891 	hlist_add_head_rcu(&fltr->base.hash, head);
5892 	bnxt_insert_usr_fltr(bp, &fltr->base);
5893 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5894 	atomic_set(&fltr->refcnt, 1);
5895 	return 0;
5896 }
5897 
5898 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
5899 						   struct bnxt_l2_key *key,
5900 						   gfp_t gfp)
5901 {
5902 	struct bnxt_l2_filter *fltr;
5903 	u32 idx;
5904 	int rc;
5905 
5906 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5907 	      BNXT_L2_FLTR_HASH_MASK;
5908 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5909 	if (fltr)
5910 		return fltr;
5911 
5912 	fltr = kzalloc(sizeof(*fltr), gfp);
5913 	if (!fltr)
5914 		return ERR_PTR(-ENOMEM);
5915 	spin_lock_bh(&bp->ntp_fltr_lock);
5916 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5917 	spin_unlock_bh(&bp->ntp_fltr_lock);
5918 	if (rc) {
5919 		bnxt_del_l2_filter(bp, fltr);
5920 		fltr = ERR_PTR(rc);
5921 	}
5922 	return fltr;
5923 }
5924 
5925 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
5926 						struct bnxt_l2_key *key,
5927 						u16 flags)
5928 {
5929 	struct bnxt_l2_filter *fltr;
5930 	u32 idx;
5931 	int rc;
5932 
5933 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5934 	      BNXT_L2_FLTR_HASH_MASK;
5935 	spin_lock_bh(&bp->ntp_fltr_lock);
5936 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5937 	if (fltr) {
5938 		fltr = ERR_PTR(-EEXIST);
5939 		goto l2_filter_exit;
5940 	}
5941 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
5942 	if (!fltr) {
5943 		fltr = ERR_PTR(-ENOMEM);
5944 		goto l2_filter_exit;
5945 	}
5946 	fltr->base.flags = flags;
5947 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5948 	if (rc) {
5949 		spin_unlock_bh(&bp->ntp_fltr_lock);
5950 		bnxt_del_l2_filter(bp, fltr);
5951 		return ERR_PTR(rc);
5952 	}
5953 
5954 l2_filter_exit:
5955 	spin_unlock_bh(&bp->ntp_fltr_lock);
5956 	return fltr;
5957 }
5958 
5959 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
5960 {
5961 #ifdef CONFIG_BNXT_SRIOV
5962 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
5963 
5964 	return vf->fw_fid;
5965 #else
5966 	return INVALID_HW_RING_ID;
5967 #endif
5968 }
5969 
5970 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5971 {
5972 	struct hwrm_cfa_l2_filter_free_input *req;
5973 	u16 target_id = 0xffff;
5974 	int rc;
5975 
5976 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5977 		struct bnxt_pf_info *pf = &bp->pf;
5978 
5979 		if (fltr->base.vf_idx >= pf->active_vfs)
5980 			return -EINVAL;
5981 
5982 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5983 		if (target_id == INVALID_HW_RING_ID)
5984 			return -EINVAL;
5985 	}
5986 
5987 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
5988 	if (rc)
5989 		return rc;
5990 
5991 	req->target_id = cpu_to_le16(target_id);
5992 	req->l2_filter_id = fltr->base.filter_id;
5993 	return hwrm_req_send(bp, req);
5994 }
5995 
5996 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5997 {
5998 	struct hwrm_cfa_l2_filter_alloc_output *resp;
5999 	struct hwrm_cfa_l2_filter_alloc_input *req;
6000 	u16 target_id = 0xffff;
6001 	int rc;
6002 
6003 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6004 		struct bnxt_pf_info *pf = &bp->pf;
6005 
6006 		if (fltr->base.vf_idx >= pf->active_vfs)
6007 			return -EINVAL;
6008 
6009 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6010 	}
6011 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6012 	if (rc)
6013 		return rc;
6014 
6015 	req->target_id = cpu_to_le16(target_id);
6016 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6017 
6018 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6019 		req->flags |=
6020 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6021 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6022 	req->enables =
6023 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6024 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6025 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6026 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6027 	eth_broadcast_addr(req->l2_addr_mask);
6028 
6029 	if (fltr->l2_key.vlan) {
6030 		req->enables |=
6031 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6032 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6033 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6034 		req->num_vlans = 1;
6035 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6036 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6037 	}
6038 
6039 	resp = hwrm_req_hold(bp, req);
6040 	rc = hwrm_req_send(bp, req);
6041 	if (!rc) {
6042 		fltr->base.filter_id = resp->l2_filter_id;
6043 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6044 	}
6045 	hwrm_req_drop(bp, req);
6046 	return rc;
6047 }
6048 
6049 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6050 				     struct bnxt_ntuple_filter *fltr)
6051 {
6052 	struct hwrm_cfa_ntuple_filter_free_input *req;
6053 	int rc;
6054 
6055 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6056 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6057 	if (rc)
6058 		return rc;
6059 
6060 	req->ntuple_filter_id = fltr->base.filter_id;
6061 	return hwrm_req_send(bp, req);
6062 }
6063 
6064 #define BNXT_NTP_FLTR_FLAGS					\
6065 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6066 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6067 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6068 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6069 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6070 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6071 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6072 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6073 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6074 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6075 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6076 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6077 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6078 
6079 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6080 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6081 
6082 void bnxt_fill_ipv6_mask(__be32 mask[4])
6083 {
6084 	int i;
6085 
6086 	for (i = 0; i < 4; i++)
6087 		mask[i] = cpu_to_be32(~0);
6088 }
6089 
6090 static void
6091 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6092 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6093 			  struct bnxt_ntuple_filter *fltr)
6094 {
6095 	u16 rxq = fltr->base.rxq;
6096 
6097 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6098 		struct ethtool_rxfh_context *ctx;
6099 		struct bnxt_rss_ctx *rss_ctx;
6100 		struct bnxt_vnic_info *vnic;
6101 
6102 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6103 			      fltr->base.fw_vnic_id);
6104 		if (ctx) {
6105 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6106 			vnic = &rss_ctx->vnic;
6107 
6108 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6109 		}
6110 		return;
6111 	}
6112 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6113 		struct bnxt_vnic_info *vnic;
6114 		u32 enables;
6115 
6116 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6117 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6118 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6119 		req->enables |= cpu_to_le32(enables);
6120 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6121 	} else {
6122 		u32 flags;
6123 
6124 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6125 		req->flags |= cpu_to_le32(flags);
6126 		req->dst_id = cpu_to_le16(rxq);
6127 	}
6128 }
6129 
6130 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6131 				      struct bnxt_ntuple_filter *fltr)
6132 {
6133 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6134 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6135 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6136 	struct flow_keys *keys = &fltr->fkeys;
6137 	struct bnxt_l2_filter *l2_fltr;
6138 	struct bnxt_vnic_info *vnic;
6139 	int rc;
6140 
6141 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6142 	if (rc)
6143 		return rc;
6144 
6145 	l2_fltr = fltr->l2_fltr;
6146 	req->l2_filter_id = l2_fltr->base.filter_id;
6147 
6148 	if (fltr->base.flags & BNXT_ACT_DROP) {
6149 		req->flags =
6150 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6151 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6152 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6153 	} else {
6154 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6155 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6156 	}
6157 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6158 
6159 	req->ethertype = htons(ETH_P_IP);
6160 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6161 	req->ip_protocol = keys->basic.ip_proto;
6162 
6163 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6164 		req->ethertype = htons(ETH_P_IPV6);
6165 		req->ip_addr_type =
6166 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6167 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6168 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6169 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6170 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6171 	} else {
6172 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6173 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6174 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6175 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6176 	}
6177 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6178 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6179 		req->tunnel_type =
6180 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6181 	}
6182 
6183 	req->src_port = keys->ports.src;
6184 	req->src_port_mask = masks->ports.src;
6185 	req->dst_port = keys->ports.dst;
6186 	req->dst_port_mask = masks->ports.dst;
6187 
6188 	resp = hwrm_req_hold(bp, req);
6189 	rc = hwrm_req_send(bp, req);
6190 	if (!rc)
6191 		fltr->base.filter_id = resp->ntuple_filter_id;
6192 	hwrm_req_drop(bp, req);
6193 	return rc;
6194 }
6195 
6196 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6197 				     const u8 *mac_addr)
6198 {
6199 	struct bnxt_l2_filter *fltr;
6200 	struct bnxt_l2_key key;
6201 	int rc;
6202 
6203 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6204 	key.vlan = 0;
6205 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6206 	if (IS_ERR(fltr))
6207 		return PTR_ERR(fltr);
6208 
6209 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6210 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6211 	if (rc)
6212 		bnxt_del_l2_filter(bp, fltr);
6213 	else
6214 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6215 	return rc;
6216 }
6217 
6218 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6219 {
6220 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6221 
6222 	/* Any associated ntuple filters will also be cleared by firmware. */
6223 	for (i = 0; i < num_of_vnics; i++) {
6224 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6225 
6226 		for (j = 0; j < vnic->uc_filter_count; j++) {
6227 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6228 
6229 			bnxt_hwrm_l2_filter_free(bp, fltr);
6230 			bnxt_del_l2_filter(bp, fltr);
6231 		}
6232 		vnic->uc_filter_count = 0;
6233 	}
6234 }
6235 
6236 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6237 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6238 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6239 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6240 
6241 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6242 					   struct hwrm_vnic_tpa_cfg_input *req)
6243 {
6244 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6245 
6246 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6247 		return;
6248 
6249 	if (bp->vxlan_port)
6250 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6251 	if (bp->vxlan_gpe_port)
6252 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6253 	if (bp->nge_port)
6254 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6255 
6256 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6257 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6258 }
6259 
6260 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6261 			   u32 tpa_flags)
6262 {
6263 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6264 	struct hwrm_vnic_tpa_cfg_input *req;
6265 	int rc;
6266 
6267 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6268 		return 0;
6269 
6270 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6271 	if (rc)
6272 		return rc;
6273 
6274 	if (tpa_flags) {
6275 		u16 mss = bp->dev->mtu - 40;
6276 		u32 nsegs, n, segs = 0, flags;
6277 
6278 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6279 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6280 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6281 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6282 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6283 		if (tpa_flags & BNXT_FLAG_GRO)
6284 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6285 
6286 		req->flags = cpu_to_le32(flags);
6287 
6288 		req->enables =
6289 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6290 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6291 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6292 
6293 		/* Number of segs are log2 units, and first packet is not
6294 		 * included as part of this units.
6295 		 */
6296 		if (mss <= BNXT_RX_PAGE_SIZE) {
6297 			n = BNXT_RX_PAGE_SIZE / mss;
6298 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6299 		} else {
6300 			n = mss / BNXT_RX_PAGE_SIZE;
6301 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6302 				n++;
6303 			nsegs = (MAX_SKB_FRAGS - n) / n;
6304 		}
6305 
6306 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6307 			segs = MAX_TPA_SEGS_P5;
6308 			max_aggs = bp->max_tpa;
6309 		} else {
6310 			segs = ilog2(nsegs);
6311 		}
6312 		req->max_agg_segs = cpu_to_le16(segs);
6313 		req->max_aggs = cpu_to_le16(max_aggs);
6314 
6315 		req->min_agg_len = cpu_to_le32(512);
6316 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6317 	}
6318 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6319 
6320 	return hwrm_req_send(bp, req);
6321 }
6322 
6323 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6324 {
6325 	struct bnxt_ring_grp_info *grp_info;
6326 
6327 	grp_info = &bp->grp_info[ring->grp_idx];
6328 	return grp_info->cp_fw_ring_id;
6329 }
6330 
6331 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6332 {
6333 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6334 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6335 	else
6336 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6337 }
6338 
6339 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6340 {
6341 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6342 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6343 	else
6344 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6345 }
6346 
6347 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6348 {
6349 	int entries;
6350 
6351 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6352 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6353 	else
6354 		entries = HW_HASH_INDEX_SIZE;
6355 
6356 	bp->rss_indir_tbl_entries = entries;
6357 	bp->rss_indir_tbl =
6358 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6359 	if (!bp->rss_indir_tbl)
6360 		return -ENOMEM;
6361 
6362 	return 0;
6363 }
6364 
6365 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6366 				 struct ethtool_rxfh_context *rss_ctx)
6367 {
6368 	u16 max_rings, max_entries, pad, i;
6369 	u32 *rss_indir_tbl;
6370 
6371 	if (!bp->rx_nr_rings)
6372 		return;
6373 
6374 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6375 		max_rings = bp->rx_nr_rings - 1;
6376 	else
6377 		max_rings = bp->rx_nr_rings;
6378 
6379 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6380 	if (rss_ctx)
6381 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6382 	else
6383 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6384 
6385 	for (i = 0; i < max_entries; i++)
6386 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6387 
6388 	pad = bp->rss_indir_tbl_entries - max_entries;
6389 	if (pad)
6390 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6391 }
6392 
6393 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6394 {
6395 	u32 i, tbl_size, max_ring = 0;
6396 
6397 	if (!bp->rss_indir_tbl)
6398 		return 0;
6399 
6400 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6401 	for (i = 0; i < tbl_size; i++)
6402 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6403 	return max_ring;
6404 }
6405 
6406 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6407 {
6408 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6409 		if (!rx_rings)
6410 			return 0;
6411 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6412 					       BNXT_RSS_TABLE_ENTRIES_P5);
6413 	}
6414 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6415 		return 2;
6416 	return 1;
6417 }
6418 
6419 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6420 {
6421 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6422 	u16 i, j;
6423 
6424 	/* Fill the RSS indirection table with ring group ids */
6425 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6426 		if (!no_rss)
6427 			j = bp->rss_indir_tbl[i];
6428 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6429 	}
6430 }
6431 
6432 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6433 				    struct bnxt_vnic_info *vnic)
6434 {
6435 	__le16 *ring_tbl = vnic->rss_table;
6436 	struct bnxt_rx_ring_info *rxr;
6437 	u16 tbl_size, i;
6438 
6439 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6440 
6441 	for (i = 0; i < tbl_size; i++) {
6442 		u16 ring_id, j;
6443 
6444 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6445 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6446 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6447 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6448 		else
6449 			j = bp->rss_indir_tbl[i];
6450 		rxr = &bp->rx_ring[j];
6451 
6452 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6453 		*ring_tbl++ = cpu_to_le16(ring_id);
6454 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6455 		*ring_tbl++ = cpu_to_le16(ring_id);
6456 	}
6457 }
6458 
6459 static void
6460 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6461 			 struct bnxt_vnic_info *vnic)
6462 {
6463 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6464 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6465 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6466 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6467 	} else {
6468 		bnxt_fill_hw_rss_tbl(bp, vnic);
6469 	}
6470 
6471 	if (bp->rss_hash_delta) {
6472 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6473 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6474 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6475 		else
6476 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6477 	} else {
6478 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6479 	}
6480 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6481 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6482 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6483 }
6484 
6485 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6486 				  bool set_rss)
6487 {
6488 	struct hwrm_vnic_rss_cfg_input *req;
6489 	int rc;
6490 
6491 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6492 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6493 		return 0;
6494 
6495 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6496 	if (rc)
6497 		return rc;
6498 
6499 	if (set_rss)
6500 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6501 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6502 	return hwrm_req_send(bp, req);
6503 }
6504 
6505 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6506 				     struct bnxt_vnic_info *vnic, bool set_rss)
6507 {
6508 	struct hwrm_vnic_rss_cfg_input *req;
6509 	dma_addr_t ring_tbl_map;
6510 	u32 i, nr_ctxs;
6511 	int rc;
6512 
6513 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6514 	if (rc)
6515 		return rc;
6516 
6517 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6518 	if (!set_rss)
6519 		return hwrm_req_send(bp, req);
6520 
6521 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6522 	ring_tbl_map = vnic->rss_table_dma_addr;
6523 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6524 
6525 	hwrm_req_hold(bp, req);
6526 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6527 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6528 		req->ring_table_pair_index = i;
6529 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6530 		rc = hwrm_req_send(bp, req);
6531 		if (rc)
6532 			goto exit;
6533 	}
6534 
6535 exit:
6536 	hwrm_req_drop(bp, req);
6537 	return rc;
6538 }
6539 
6540 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6541 {
6542 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6543 	struct hwrm_vnic_rss_qcfg_output *resp;
6544 	struct hwrm_vnic_rss_qcfg_input *req;
6545 
6546 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6547 		return;
6548 
6549 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6550 	/* all contexts configured to same hash_type, zero always exists */
6551 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6552 	resp = hwrm_req_hold(bp, req);
6553 	if (!hwrm_req_send(bp, req)) {
6554 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6555 		bp->rss_hash_delta = 0;
6556 	}
6557 	hwrm_req_drop(bp, req);
6558 }
6559 
6560 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6561 {
6562 	struct hwrm_vnic_plcmodes_cfg_input *req;
6563 	int rc;
6564 
6565 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6566 	if (rc)
6567 		return rc;
6568 
6569 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6570 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6571 
6572 	if (BNXT_RX_PAGE_MODE(bp)) {
6573 		req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6574 	} else {
6575 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6576 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6577 		req->enables |=
6578 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6579 		req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
6580 		req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
6581 	}
6582 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6583 	return hwrm_req_send(bp, req);
6584 }
6585 
6586 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6587 					struct bnxt_vnic_info *vnic,
6588 					u16 ctx_idx)
6589 {
6590 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6591 
6592 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6593 		return;
6594 
6595 	req->rss_cos_lb_ctx_id =
6596 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6597 
6598 	hwrm_req_send(bp, req);
6599 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6600 }
6601 
6602 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6603 {
6604 	int i, j;
6605 
6606 	for (i = 0; i < bp->nr_vnics; i++) {
6607 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6608 
6609 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6610 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6611 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6612 		}
6613 	}
6614 	bp->rsscos_nr_ctxs = 0;
6615 }
6616 
6617 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6618 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6619 {
6620 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6621 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6622 	int rc;
6623 
6624 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6625 	if (rc)
6626 		return rc;
6627 
6628 	resp = hwrm_req_hold(bp, req);
6629 	rc = hwrm_req_send(bp, req);
6630 	if (!rc)
6631 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6632 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6633 	hwrm_req_drop(bp, req);
6634 
6635 	return rc;
6636 }
6637 
6638 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6639 {
6640 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6641 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6642 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6643 }
6644 
6645 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6646 {
6647 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6648 	struct hwrm_vnic_cfg_input *req;
6649 	unsigned int ring = 0, grp_idx;
6650 	u16 def_vlan = 0;
6651 	int rc;
6652 
6653 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6654 	if (rc)
6655 		return rc;
6656 
6657 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6658 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6659 
6660 		req->default_rx_ring_id =
6661 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6662 		req->default_cmpl_ring_id =
6663 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6664 		req->enables =
6665 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6666 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6667 		goto vnic_mru;
6668 	}
6669 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6670 	/* Only RSS support for now TBD: COS & LB */
6671 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6672 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6673 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6674 					   VNIC_CFG_REQ_ENABLES_MRU);
6675 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6676 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6677 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6678 					   VNIC_CFG_REQ_ENABLES_MRU);
6679 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6680 	} else {
6681 		req->rss_rule = cpu_to_le16(0xffff);
6682 	}
6683 
6684 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6685 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6686 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6687 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6688 	} else {
6689 		req->cos_rule = cpu_to_le16(0xffff);
6690 	}
6691 
6692 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6693 		ring = 0;
6694 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6695 		ring = vnic->vnic_id - 1;
6696 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6697 		ring = bp->rx_nr_rings - 1;
6698 
6699 	grp_idx = bp->rx_ring[ring].bnapi->index;
6700 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6701 	req->lb_rule = cpu_to_le16(0xffff);
6702 vnic_mru:
6703 	vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
6704 	req->mru = cpu_to_le16(vnic->mru);
6705 
6706 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6707 #ifdef CONFIG_BNXT_SRIOV
6708 	if (BNXT_VF(bp))
6709 		def_vlan = bp->vf.vlan;
6710 #endif
6711 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6712 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6713 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6714 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6715 
6716 	return hwrm_req_send(bp, req);
6717 }
6718 
6719 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6720 				    struct bnxt_vnic_info *vnic)
6721 {
6722 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6723 		struct hwrm_vnic_free_input *req;
6724 
6725 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6726 			return;
6727 
6728 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6729 
6730 		hwrm_req_send(bp, req);
6731 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6732 	}
6733 }
6734 
6735 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6736 {
6737 	u16 i;
6738 
6739 	for (i = 0; i < bp->nr_vnics; i++)
6740 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6741 }
6742 
6743 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6744 			 unsigned int start_rx_ring_idx,
6745 			 unsigned int nr_rings)
6746 {
6747 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6748 	struct hwrm_vnic_alloc_output *resp;
6749 	struct hwrm_vnic_alloc_input *req;
6750 	int rc;
6751 
6752 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6753 	if (rc)
6754 		return rc;
6755 
6756 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6757 		goto vnic_no_ring_grps;
6758 
6759 	/* map ring groups to this vnic */
6760 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6761 		grp_idx = bp->rx_ring[i].bnapi->index;
6762 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6763 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6764 				   j, nr_rings);
6765 			break;
6766 		}
6767 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6768 	}
6769 
6770 vnic_no_ring_grps:
6771 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6772 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6773 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6774 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6775 
6776 	resp = hwrm_req_hold(bp, req);
6777 	rc = hwrm_req_send(bp, req);
6778 	if (!rc)
6779 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6780 	hwrm_req_drop(bp, req);
6781 	return rc;
6782 }
6783 
6784 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6785 {
6786 	struct hwrm_vnic_qcaps_output *resp;
6787 	struct hwrm_vnic_qcaps_input *req;
6788 	int rc;
6789 
6790 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6791 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6792 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6793 	if (bp->hwrm_spec_code < 0x10600)
6794 		return 0;
6795 
6796 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6797 	if (rc)
6798 		return rc;
6799 
6800 	resp = hwrm_req_hold(bp, req);
6801 	rc = hwrm_req_send(bp, req);
6802 	if (!rc) {
6803 		u32 flags = le32_to_cpu(resp->flags);
6804 
6805 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6806 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6807 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6808 		if (flags &
6809 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6810 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6811 
6812 		/* Older P5 fw before EXT_HW_STATS support did not set
6813 		 * VLAN_STRIP_CAP properly.
6814 		 */
6815 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6816 		    (BNXT_CHIP_P5(bp) &&
6817 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6818 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6819 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6820 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6821 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6822 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6823 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6824 		if (bp->max_tpa_v2) {
6825 			if (BNXT_CHIP_P5(bp))
6826 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6827 			else
6828 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6829 		}
6830 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6831 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6832 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6833 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6834 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6835 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6836 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6837 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6838 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6839 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6840 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6841 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6842 	}
6843 	hwrm_req_drop(bp, req);
6844 	return rc;
6845 }
6846 
6847 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6848 {
6849 	struct hwrm_ring_grp_alloc_output *resp;
6850 	struct hwrm_ring_grp_alloc_input *req;
6851 	int rc;
6852 	u16 i;
6853 
6854 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6855 		return 0;
6856 
6857 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6858 	if (rc)
6859 		return rc;
6860 
6861 	resp = hwrm_req_hold(bp, req);
6862 	for (i = 0; i < bp->rx_nr_rings; i++) {
6863 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6864 
6865 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6866 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6867 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6868 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6869 
6870 		rc = hwrm_req_send(bp, req);
6871 
6872 		if (rc)
6873 			break;
6874 
6875 		bp->grp_info[grp_idx].fw_grp_id =
6876 			le32_to_cpu(resp->ring_group_id);
6877 	}
6878 	hwrm_req_drop(bp, req);
6879 	return rc;
6880 }
6881 
6882 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6883 {
6884 	struct hwrm_ring_grp_free_input *req;
6885 	u16 i;
6886 
6887 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6888 		return;
6889 
6890 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6891 		return;
6892 
6893 	hwrm_req_hold(bp, req);
6894 	for (i = 0; i < bp->cp_nr_rings; i++) {
6895 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
6896 			continue;
6897 		req->ring_group_id =
6898 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
6899 
6900 		hwrm_req_send(bp, req);
6901 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
6902 	}
6903 	hwrm_req_drop(bp, req);
6904 }
6905 
6906 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
6907 				    struct bnxt_ring_struct *ring,
6908 				    u32 ring_type, u32 map_index)
6909 {
6910 	struct hwrm_ring_alloc_output *resp;
6911 	struct hwrm_ring_alloc_input *req;
6912 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
6913 	struct bnxt_ring_grp_info *grp_info;
6914 	int rc, err = 0;
6915 	u16 ring_id;
6916 
6917 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
6918 	if (rc)
6919 		goto exit;
6920 
6921 	req->enables = 0;
6922 	if (rmem->nr_pages > 1) {
6923 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
6924 		/* Page size is in log2 units */
6925 		req->page_size = BNXT_PAGE_SHIFT;
6926 		req->page_tbl_depth = 1;
6927 	} else {
6928 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
6929 	}
6930 	req->fbo = 0;
6931 	/* Association of ring index with doorbell index and MSIX number */
6932 	req->logical_id = cpu_to_le16(map_index);
6933 
6934 	switch (ring_type) {
6935 	case HWRM_RING_ALLOC_TX: {
6936 		struct bnxt_tx_ring_info *txr;
6937 		u16 flags = 0;
6938 
6939 		txr = container_of(ring, struct bnxt_tx_ring_info,
6940 				   tx_ring_struct);
6941 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
6942 		/* Association of transmit ring with completion ring */
6943 		grp_info = &bp->grp_info[ring->grp_idx];
6944 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
6945 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
6946 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6947 		req->queue_id = cpu_to_le16(ring->queue_id);
6948 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
6949 			req->cmpl_coal_cnt =
6950 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
6951 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
6952 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
6953 		req->flags = cpu_to_le16(flags);
6954 		break;
6955 	}
6956 	case HWRM_RING_ALLOC_RX:
6957 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6958 		req->length = cpu_to_le32(bp->rx_ring_mask + 1);
6959 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6960 			u16 flags = 0;
6961 
6962 			/* Association of rx ring with stats context */
6963 			grp_info = &bp->grp_info[ring->grp_idx];
6964 			req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
6965 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6966 			req->enables |= cpu_to_le32(
6967 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6968 			if (NET_IP_ALIGN == 2)
6969 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
6970 			req->flags = cpu_to_le16(flags);
6971 		}
6972 		break;
6973 	case HWRM_RING_ALLOC_AGG:
6974 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6975 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
6976 			/* Association of agg ring with rx ring */
6977 			grp_info = &bp->grp_info[ring->grp_idx];
6978 			req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
6979 			req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
6980 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6981 			req->enables |= cpu_to_le32(
6982 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
6983 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6984 		} else {
6985 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6986 		}
6987 		req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
6988 		break;
6989 	case HWRM_RING_ALLOC_CMPL:
6990 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
6991 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6992 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6993 			/* Association of cp ring with nq */
6994 			grp_info = &bp->grp_info[map_index];
6995 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
6996 			req->cq_handle = cpu_to_le64(ring->handle);
6997 			req->enables |= cpu_to_le32(
6998 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
6999 		} else {
7000 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7001 		}
7002 		break;
7003 	case HWRM_RING_ALLOC_NQ:
7004 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7005 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7006 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7007 		break;
7008 	default:
7009 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7010 			   ring_type);
7011 		return -1;
7012 	}
7013 
7014 	resp = hwrm_req_hold(bp, req);
7015 	rc = hwrm_req_send(bp, req);
7016 	err = le16_to_cpu(resp->error_code);
7017 	ring_id = le16_to_cpu(resp->ring_id);
7018 	hwrm_req_drop(bp, req);
7019 
7020 exit:
7021 	if (rc || err) {
7022 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7023 			   ring_type, rc, err);
7024 		return -EIO;
7025 	}
7026 	ring->fw_ring_id = ring_id;
7027 	return rc;
7028 }
7029 
7030 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7031 {
7032 	int rc;
7033 
7034 	if (BNXT_PF(bp)) {
7035 		struct hwrm_func_cfg_input *req;
7036 
7037 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7038 		if (rc)
7039 			return rc;
7040 
7041 		req->fid = cpu_to_le16(0xffff);
7042 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7043 		req->async_event_cr = cpu_to_le16(idx);
7044 		return hwrm_req_send(bp, req);
7045 	} else {
7046 		struct hwrm_func_vf_cfg_input *req;
7047 
7048 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7049 		if (rc)
7050 			return rc;
7051 
7052 		req->enables =
7053 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7054 		req->async_event_cr = cpu_to_le16(idx);
7055 		return hwrm_req_send(bp, req);
7056 	}
7057 }
7058 
7059 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7060 			     u32 ring_type)
7061 {
7062 	switch (ring_type) {
7063 	case HWRM_RING_ALLOC_TX:
7064 		db->db_ring_mask = bp->tx_ring_mask;
7065 		break;
7066 	case HWRM_RING_ALLOC_RX:
7067 		db->db_ring_mask = bp->rx_ring_mask;
7068 		break;
7069 	case HWRM_RING_ALLOC_AGG:
7070 		db->db_ring_mask = bp->rx_agg_ring_mask;
7071 		break;
7072 	case HWRM_RING_ALLOC_CMPL:
7073 	case HWRM_RING_ALLOC_NQ:
7074 		db->db_ring_mask = bp->cp_ring_mask;
7075 		break;
7076 	}
7077 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7078 		db->db_epoch_mask = db->db_ring_mask + 1;
7079 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7080 	}
7081 }
7082 
7083 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7084 			u32 map_idx, u32 xid)
7085 {
7086 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7087 		switch (ring_type) {
7088 		case HWRM_RING_ALLOC_TX:
7089 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7090 			break;
7091 		case HWRM_RING_ALLOC_RX:
7092 		case HWRM_RING_ALLOC_AGG:
7093 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7094 			break;
7095 		case HWRM_RING_ALLOC_CMPL:
7096 			db->db_key64 = DBR_PATH_L2;
7097 			break;
7098 		case HWRM_RING_ALLOC_NQ:
7099 			db->db_key64 = DBR_PATH_L2;
7100 			break;
7101 		}
7102 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7103 
7104 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7105 			db->db_key64 |= DBR_VALID;
7106 
7107 		db->doorbell = bp->bar1 + bp->db_offset;
7108 	} else {
7109 		db->doorbell = bp->bar1 + map_idx * 0x80;
7110 		switch (ring_type) {
7111 		case HWRM_RING_ALLOC_TX:
7112 			db->db_key32 = DB_KEY_TX;
7113 			break;
7114 		case HWRM_RING_ALLOC_RX:
7115 		case HWRM_RING_ALLOC_AGG:
7116 			db->db_key32 = DB_KEY_RX;
7117 			break;
7118 		case HWRM_RING_ALLOC_CMPL:
7119 			db->db_key32 = DB_KEY_CP;
7120 			break;
7121 		}
7122 	}
7123 	bnxt_set_db_mask(bp, db, ring_type);
7124 }
7125 
7126 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7127 				   struct bnxt_rx_ring_info *rxr)
7128 {
7129 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7130 	struct bnxt_napi *bnapi = rxr->bnapi;
7131 	u32 type = HWRM_RING_ALLOC_RX;
7132 	u32 map_idx = bnapi->index;
7133 	int rc;
7134 
7135 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7136 	if (rc)
7137 		return rc;
7138 
7139 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7140 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7141 
7142 	return 0;
7143 }
7144 
7145 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7146 				       struct bnxt_rx_ring_info *rxr)
7147 {
7148 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7149 	u32 type = HWRM_RING_ALLOC_AGG;
7150 	u32 grp_idx = ring->grp_idx;
7151 	u32 map_idx;
7152 	int rc;
7153 
7154 	map_idx = grp_idx + bp->rx_nr_rings;
7155 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7156 	if (rc)
7157 		return rc;
7158 
7159 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7160 		    ring->fw_ring_id);
7161 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7162 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7163 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7164 
7165 	return 0;
7166 }
7167 
7168 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7169 {
7170 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7171 	int i, rc = 0;
7172 	u32 type;
7173 
7174 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7175 		type = HWRM_RING_ALLOC_NQ;
7176 	else
7177 		type = HWRM_RING_ALLOC_CMPL;
7178 	for (i = 0; i < bp->cp_nr_rings; i++) {
7179 		struct bnxt_napi *bnapi = bp->bnapi[i];
7180 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7181 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7182 		u32 map_idx = ring->map_idx;
7183 		unsigned int vector;
7184 
7185 		vector = bp->irq_tbl[map_idx].vector;
7186 		disable_irq_nosync(vector);
7187 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7188 		if (rc) {
7189 			enable_irq(vector);
7190 			goto err_out;
7191 		}
7192 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7193 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7194 		enable_irq(vector);
7195 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7196 
7197 		if (!i) {
7198 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7199 			if (rc)
7200 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7201 		}
7202 	}
7203 
7204 	type = HWRM_RING_ALLOC_TX;
7205 	for (i = 0; i < bp->tx_nr_rings; i++) {
7206 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7207 		struct bnxt_ring_struct *ring;
7208 		u32 map_idx;
7209 
7210 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7211 			struct bnxt_cp_ring_info *cpr2 = txr->tx_cpr;
7212 			struct bnxt_napi *bnapi = txr->bnapi;
7213 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7214 
7215 			ring = &cpr2->cp_ring_struct;
7216 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7217 			map_idx = bnapi->index;
7218 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7219 			if (rc)
7220 				goto err_out;
7221 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7222 				    ring->fw_ring_id);
7223 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7224 		}
7225 		ring = &txr->tx_ring_struct;
7226 		map_idx = i;
7227 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7228 		if (rc)
7229 			goto err_out;
7230 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
7231 	}
7232 
7233 	for (i = 0; i < bp->rx_nr_rings; i++) {
7234 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7235 
7236 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7237 		if (rc)
7238 			goto err_out;
7239 		/* If we have agg rings, post agg buffers first. */
7240 		if (!agg_rings)
7241 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7242 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7243 			struct bnxt_cp_ring_info *cpr2 = rxr->rx_cpr;
7244 			struct bnxt_napi *bnapi = rxr->bnapi;
7245 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7246 			struct bnxt_ring_struct *ring;
7247 			u32 map_idx = bnapi->index;
7248 
7249 			ring = &cpr2->cp_ring_struct;
7250 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7251 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7252 			if (rc)
7253 				goto err_out;
7254 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7255 				    ring->fw_ring_id);
7256 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7257 		}
7258 	}
7259 
7260 	if (agg_rings) {
7261 		for (i = 0; i < bp->rx_nr_rings; i++) {
7262 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7263 			if (rc)
7264 				goto err_out;
7265 		}
7266 	}
7267 err_out:
7268 	return rc;
7269 }
7270 
7271 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7272 				   struct bnxt_ring_struct *ring,
7273 				   u32 ring_type, int cmpl_ring_id)
7274 {
7275 	struct hwrm_ring_free_output *resp;
7276 	struct hwrm_ring_free_input *req;
7277 	u16 error_code = 0;
7278 	int rc;
7279 
7280 	if (BNXT_NO_FW_ACCESS(bp))
7281 		return 0;
7282 
7283 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7284 	if (rc)
7285 		goto exit;
7286 
7287 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7288 	req->ring_type = ring_type;
7289 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7290 
7291 	resp = hwrm_req_hold(bp, req);
7292 	rc = hwrm_req_send(bp, req);
7293 	error_code = le16_to_cpu(resp->error_code);
7294 	hwrm_req_drop(bp, req);
7295 exit:
7296 	if (rc || error_code) {
7297 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7298 			   ring_type, rc, error_code);
7299 		return -EIO;
7300 	}
7301 	return 0;
7302 }
7303 
7304 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7305 				   struct bnxt_rx_ring_info *rxr,
7306 				   bool close_path)
7307 {
7308 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7309 	u32 grp_idx = rxr->bnapi->index;
7310 	u32 cmpl_ring_id;
7311 
7312 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7313 		return;
7314 
7315 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7316 	hwrm_ring_free_send_msg(bp, ring,
7317 				RING_FREE_REQ_RING_TYPE_RX,
7318 				close_path ? cmpl_ring_id :
7319 				INVALID_HW_RING_ID);
7320 	ring->fw_ring_id = INVALID_HW_RING_ID;
7321 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7322 }
7323 
7324 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7325 				       struct bnxt_rx_ring_info *rxr,
7326 				       bool close_path)
7327 {
7328 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7329 	u32 grp_idx = rxr->bnapi->index;
7330 	u32 type, cmpl_ring_id;
7331 
7332 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7333 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7334 	else
7335 		type = RING_FREE_REQ_RING_TYPE_RX;
7336 
7337 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7338 		return;
7339 
7340 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7341 	hwrm_ring_free_send_msg(bp, ring, type,
7342 				close_path ? cmpl_ring_id :
7343 				INVALID_HW_RING_ID);
7344 	ring->fw_ring_id = INVALID_HW_RING_ID;
7345 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7346 }
7347 
7348 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7349 {
7350 	u32 type;
7351 	int i;
7352 
7353 	if (!bp->bnapi)
7354 		return;
7355 
7356 	for (i = 0; i < bp->tx_nr_rings; i++) {
7357 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7358 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7359 
7360 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7361 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
7362 
7363 			hwrm_ring_free_send_msg(bp, ring,
7364 						RING_FREE_REQ_RING_TYPE_TX,
7365 						close_path ? cmpl_ring_id :
7366 						INVALID_HW_RING_ID);
7367 			ring->fw_ring_id = INVALID_HW_RING_ID;
7368 		}
7369 	}
7370 
7371 	for (i = 0; i < bp->rx_nr_rings; i++) {
7372 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7373 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7374 	}
7375 
7376 	/* The completion rings are about to be freed.  After that the
7377 	 * IRQ doorbell will not work anymore.  So we need to disable
7378 	 * IRQ here.
7379 	 */
7380 	bnxt_disable_int_sync(bp);
7381 
7382 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7383 		type = RING_FREE_REQ_RING_TYPE_NQ;
7384 	else
7385 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7386 	for (i = 0; i < bp->cp_nr_rings; i++) {
7387 		struct bnxt_napi *bnapi = bp->bnapi[i];
7388 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7389 		struct bnxt_ring_struct *ring;
7390 		int j;
7391 
7392 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) {
7393 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
7394 
7395 			ring = &cpr2->cp_ring_struct;
7396 			if (ring->fw_ring_id == INVALID_HW_RING_ID)
7397 				continue;
7398 			hwrm_ring_free_send_msg(bp, ring,
7399 						RING_FREE_REQ_RING_TYPE_L2_CMPL,
7400 						INVALID_HW_RING_ID);
7401 			ring->fw_ring_id = INVALID_HW_RING_ID;
7402 		}
7403 		ring = &cpr->cp_ring_struct;
7404 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7405 			hwrm_ring_free_send_msg(bp, ring, type,
7406 						INVALID_HW_RING_ID);
7407 			ring->fw_ring_id = INVALID_HW_RING_ID;
7408 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7409 		}
7410 	}
7411 }
7412 
7413 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7414 			     bool shared);
7415 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7416 			   bool shared);
7417 
7418 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7419 {
7420 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7421 	struct hwrm_func_qcfg_output *resp;
7422 	struct hwrm_func_qcfg_input *req;
7423 	int rc;
7424 
7425 	if (bp->hwrm_spec_code < 0x10601)
7426 		return 0;
7427 
7428 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7429 	if (rc)
7430 		return rc;
7431 
7432 	req->fid = cpu_to_le16(0xffff);
7433 	resp = hwrm_req_hold(bp, req);
7434 	rc = hwrm_req_send(bp, req);
7435 	if (rc) {
7436 		hwrm_req_drop(bp, req);
7437 		return rc;
7438 	}
7439 
7440 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7441 	if (BNXT_NEW_RM(bp)) {
7442 		u16 cp, stats;
7443 
7444 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7445 		hw_resc->resv_hw_ring_grps =
7446 			le32_to_cpu(resp->alloc_hw_ring_grps);
7447 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7448 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7449 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7450 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7451 		hw_resc->resv_irqs = cp;
7452 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7453 			int rx = hw_resc->resv_rx_rings;
7454 			int tx = hw_resc->resv_tx_rings;
7455 
7456 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7457 				rx >>= 1;
7458 			if (cp < (rx + tx)) {
7459 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7460 				if (rc)
7461 					goto get_rings_exit;
7462 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7463 					rx <<= 1;
7464 				hw_resc->resv_rx_rings = rx;
7465 				hw_resc->resv_tx_rings = tx;
7466 			}
7467 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7468 			hw_resc->resv_hw_ring_grps = rx;
7469 		}
7470 		hw_resc->resv_cp_rings = cp;
7471 		hw_resc->resv_stat_ctxs = stats;
7472 	}
7473 get_rings_exit:
7474 	hwrm_req_drop(bp, req);
7475 	return rc;
7476 }
7477 
7478 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7479 {
7480 	struct hwrm_func_qcfg_output *resp;
7481 	struct hwrm_func_qcfg_input *req;
7482 	int rc;
7483 
7484 	if (bp->hwrm_spec_code < 0x10601)
7485 		return 0;
7486 
7487 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7488 	if (rc)
7489 		return rc;
7490 
7491 	req->fid = cpu_to_le16(fid);
7492 	resp = hwrm_req_hold(bp, req);
7493 	rc = hwrm_req_send(bp, req);
7494 	if (!rc)
7495 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7496 
7497 	hwrm_req_drop(bp, req);
7498 	return rc;
7499 }
7500 
7501 static bool bnxt_rfs_supported(struct bnxt *bp);
7502 
7503 static struct hwrm_func_cfg_input *
7504 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7505 {
7506 	struct hwrm_func_cfg_input *req;
7507 	u32 enables = 0;
7508 
7509 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7510 		return NULL;
7511 
7512 	req->fid = cpu_to_le16(0xffff);
7513 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7514 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7515 	if (BNXT_NEW_RM(bp)) {
7516 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7517 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7518 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7519 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7520 			enables |= hwr->cp_p5 ?
7521 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7522 		} else {
7523 			enables |= hwr->cp ?
7524 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7525 			enables |= hwr->grp ?
7526 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7527 		}
7528 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7529 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7530 					  0;
7531 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7532 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7533 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7534 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7535 			req->num_msix = cpu_to_le16(hwr->cp);
7536 		} else {
7537 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7538 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7539 		}
7540 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7541 		req->num_vnics = cpu_to_le16(hwr->vnic);
7542 	}
7543 	req->enables = cpu_to_le32(enables);
7544 	return req;
7545 }
7546 
7547 static struct hwrm_func_vf_cfg_input *
7548 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7549 {
7550 	struct hwrm_func_vf_cfg_input *req;
7551 	u32 enables = 0;
7552 
7553 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7554 		return NULL;
7555 
7556 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7557 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7558 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7559 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7560 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7561 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7562 		enables |= hwr->cp_p5 ?
7563 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7564 	} else {
7565 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7566 		enables |= hwr->grp ?
7567 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7568 	}
7569 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7570 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7571 
7572 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7573 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7574 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7575 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7576 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7577 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7578 	} else {
7579 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7580 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7581 	}
7582 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7583 	req->num_vnics = cpu_to_le16(hwr->vnic);
7584 
7585 	req->enables = cpu_to_le32(enables);
7586 	return req;
7587 }
7588 
7589 static int
7590 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7591 {
7592 	struct hwrm_func_cfg_input *req;
7593 	int rc;
7594 
7595 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7596 	if (!req)
7597 		return -ENOMEM;
7598 
7599 	if (!req->enables) {
7600 		hwrm_req_drop(bp, req);
7601 		return 0;
7602 	}
7603 
7604 	rc = hwrm_req_send(bp, req);
7605 	if (rc)
7606 		return rc;
7607 
7608 	if (bp->hwrm_spec_code < 0x10601)
7609 		bp->hw_resc.resv_tx_rings = hwr->tx;
7610 
7611 	return bnxt_hwrm_get_rings(bp);
7612 }
7613 
7614 static int
7615 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7616 {
7617 	struct hwrm_func_vf_cfg_input *req;
7618 	int rc;
7619 
7620 	if (!BNXT_NEW_RM(bp)) {
7621 		bp->hw_resc.resv_tx_rings = hwr->tx;
7622 		return 0;
7623 	}
7624 
7625 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7626 	if (!req)
7627 		return -ENOMEM;
7628 
7629 	rc = hwrm_req_send(bp, req);
7630 	if (rc)
7631 		return rc;
7632 
7633 	return bnxt_hwrm_get_rings(bp);
7634 }
7635 
7636 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7637 {
7638 	if (BNXT_PF(bp))
7639 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7640 	else
7641 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7642 }
7643 
7644 int bnxt_nq_rings_in_use(struct bnxt *bp)
7645 {
7646 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7647 }
7648 
7649 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7650 {
7651 	int cp;
7652 
7653 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7654 		return bnxt_nq_rings_in_use(bp);
7655 
7656 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7657 	return cp;
7658 }
7659 
7660 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7661 {
7662 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7663 }
7664 
7665 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7666 {
7667 	if (!hwr->grp)
7668 		return 0;
7669 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7670 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7671 
7672 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7673 			rss_ctx *= hwr->vnic;
7674 		return rss_ctx;
7675 	}
7676 	if (BNXT_VF(bp))
7677 		return BNXT_VF_MAX_RSS_CTX;
7678 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7679 		return hwr->grp + 1;
7680 	return 1;
7681 }
7682 
7683 /* Check if a default RSS map needs to be setup.  This function is only
7684  * used on older firmware that does not require reserving RX rings.
7685  */
7686 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7687 {
7688 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7689 
7690 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7691 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7692 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7693 		if (!netif_is_rxfh_configured(bp->dev))
7694 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7695 	}
7696 }
7697 
7698 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7699 {
7700 	if (bp->flags & BNXT_FLAG_RFS) {
7701 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7702 			return 2 + bp->num_rss_ctx;
7703 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7704 			return rx_rings + 1;
7705 	}
7706 	return 1;
7707 }
7708 
7709 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7710 {
7711 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7712 	int cp = bnxt_cp_rings_in_use(bp);
7713 	int nq = bnxt_nq_rings_in_use(bp);
7714 	int rx = bp->rx_nr_rings, stat;
7715 	int vnic, grp = rx;
7716 
7717 	/* Old firmware does not need RX ring reservations but we still
7718 	 * need to setup a default RSS map when needed.  With new firmware
7719 	 * we go through RX ring reservations first and then set up the
7720 	 * RSS map for the successfully reserved RX rings when needed.
7721 	 */
7722 	if (!BNXT_NEW_RM(bp))
7723 		bnxt_check_rss_tbl_no_rmgr(bp);
7724 
7725 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7726 	    bp->hwrm_spec_code >= 0x10601)
7727 		return true;
7728 
7729 	if (!BNXT_NEW_RM(bp))
7730 		return false;
7731 
7732 	vnic = bnxt_get_total_vnics(bp, rx);
7733 
7734 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7735 		rx <<= 1;
7736 	stat = bnxt_get_func_stat_ctxs(bp);
7737 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7738 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7739 	    (hw_resc->resv_hw_ring_grps != grp &&
7740 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7741 		return true;
7742 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7743 	    hw_resc->resv_irqs != nq)
7744 		return true;
7745 	return false;
7746 }
7747 
7748 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7749 {
7750 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7751 
7752 	hwr->tx = hw_resc->resv_tx_rings;
7753 	if (BNXT_NEW_RM(bp)) {
7754 		hwr->rx = hw_resc->resv_rx_rings;
7755 		hwr->cp = hw_resc->resv_irqs;
7756 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7757 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7758 		hwr->grp = hw_resc->resv_hw_ring_grps;
7759 		hwr->vnic = hw_resc->resv_vnics;
7760 		hwr->stat = hw_resc->resv_stat_ctxs;
7761 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7762 	}
7763 }
7764 
7765 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7766 {
7767 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7768 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7769 }
7770 
7771 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7772 
7773 static int __bnxt_reserve_rings(struct bnxt *bp)
7774 {
7775 	struct bnxt_hw_rings hwr = {0};
7776 	int rx_rings, old_rx_rings, rc;
7777 	int cp = bp->cp_nr_rings;
7778 	int ulp_msix = 0;
7779 	bool sh = false;
7780 	int tx_cp;
7781 
7782 	if (!bnxt_need_reserve_rings(bp))
7783 		return 0;
7784 
7785 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7786 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7787 		if (!ulp_msix)
7788 			bnxt_set_ulp_stat_ctxs(bp, 0);
7789 
7790 		if (ulp_msix > bp->ulp_num_msix_want)
7791 			ulp_msix = bp->ulp_num_msix_want;
7792 		hwr.cp = cp + ulp_msix;
7793 	} else {
7794 		hwr.cp = bnxt_nq_rings_in_use(bp);
7795 	}
7796 
7797 	hwr.tx = bp->tx_nr_rings;
7798 	hwr.rx = bp->rx_nr_rings;
7799 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7800 		sh = true;
7801 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7802 		hwr.cp_p5 = hwr.rx + hwr.tx;
7803 
7804 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7805 
7806 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7807 		hwr.rx <<= 1;
7808 	hwr.grp = bp->rx_nr_rings;
7809 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7810 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7811 	old_rx_rings = bp->hw_resc.resv_rx_rings;
7812 
7813 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7814 	if (rc)
7815 		return rc;
7816 
7817 	bnxt_copy_reserved_rings(bp, &hwr);
7818 
7819 	rx_rings = hwr.rx;
7820 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7821 		if (hwr.rx >= 2) {
7822 			rx_rings = hwr.rx >> 1;
7823 		} else {
7824 			if (netif_running(bp->dev))
7825 				return -ENOMEM;
7826 
7827 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7828 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7829 			bp->dev->hw_features &= ~NETIF_F_LRO;
7830 			bp->dev->features &= ~NETIF_F_LRO;
7831 			bnxt_set_ring_params(bp);
7832 		}
7833 	}
7834 	rx_rings = min_t(int, rx_rings, hwr.grp);
7835 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7836 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7837 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7838 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7839 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7840 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7841 		hwr.rx = rx_rings << 1;
7842 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
7843 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
7844 	bp->tx_nr_rings = hwr.tx;
7845 
7846 	/* If we cannot reserve all the RX rings, reset the RSS map only
7847 	 * if absolutely necessary
7848 	 */
7849 	if (rx_rings != bp->rx_nr_rings) {
7850 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
7851 			    rx_rings, bp->rx_nr_rings);
7852 		if (netif_is_rxfh_configured(bp->dev) &&
7853 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
7854 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
7855 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
7856 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
7857 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
7858 		}
7859 	}
7860 	bp->rx_nr_rings = rx_rings;
7861 	bp->cp_nr_rings = hwr.cp;
7862 
7863 	if (!bnxt_rings_ok(bp, &hwr))
7864 		return -ENOMEM;
7865 
7866 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
7867 	    !netif_is_rxfh_configured(bp->dev))
7868 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7869 
7870 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
7871 		int resv_msix, resv_ctx, ulp_ctxs;
7872 		struct bnxt_hw_resc *hw_resc;
7873 
7874 		hw_resc = &bp->hw_resc;
7875 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
7876 		ulp_msix = min_t(int, resv_msix, ulp_msix);
7877 		bnxt_set_ulp_msix_num(bp, ulp_msix);
7878 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
7879 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
7880 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
7881 	}
7882 
7883 	return rc;
7884 }
7885 
7886 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7887 {
7888 	struct hwrm_func_vf_cfg_input *req;
7889 	u32 flags;
7890 
7891 	if (!BNXT_NEW_RM(bp))
7892 		return 0;
7893 
7894 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7895 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
7896 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7897 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7898 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7899 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
7900 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
7901 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7902 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7903 
7904 	req->flags = cpu_to_le32(flags);
7905 	return hwrm_req_send_silent(bp, req);
7906 }
7907 
7908 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7909 {
7910 	struct hwrm_func_cfg_input *req;
7911 	u32 flags;
7912 
7913 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7914 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
7915 	if (BNXT_NEW_RM(bp)) {
7916 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7917 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7918 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7919 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
7920 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7921 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
7922 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
7923 		else
7924 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7925 	}
7926 
7927 	req->flags = cpu_to_le32(flags);
7928 	return hwrm_req_send_silent(bp, req);
7929 }
7930 
7931 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7932 {
7933 	if (bp->hwrm_spec_code < 0x10801)
7934 		return 0;
7935 
7936 	if (BNXT_PF(bp))
7937 		return bnxt_hwrm_check_pf_rings(bp, hwr);
7938 
7939 	return bnxt_hwrm_check_vf_rings(bp, hwr);
7940 }
7941 
7942 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
7943 {
7944 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7945 	struct hwrm_ring_aggint_qcaps_output *resp;
7946 	struct hwrm_ring_aggint_qcaps_input *req;
7947 	int rc;
7948 
7949 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
7950 	coal_cap->num_cmpl_dma_aggr_max = 63;
7951 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
7952 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
7953 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
7954 	coal_cap->int_lat_tmr_min_max = 65535;
7955 	coal_cap->int_lat_tmr_max_max = 65535;
7956 	coal_cap->num_cmpl_aggr_int_max = 65535;
7957 	coal_cap->timer_units = 80;
7958 
7959 	if (bp->hwrm_spec_code < 0x10902)
7960 		return;
7961 
7962 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
7963 		return;
7964 
7965 	resp = hwrm_req_hold(bp, req);
7966 	rc = hwrm_req_send_silent(bp, req);
7967 	if (!rc) {
7968 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
7969 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
7970 		coal_cap->num_cmpl_dma_aggr_max =
7971 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
7972 		coal_cap->num_cmpl_dma_aggr_during_int_max =
7973 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
7974 		coal_cap->cmpl_aggr_dma_tmr_max =
7975 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
7976 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
7977 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
7978 		coal_cap->int_lat_tmr_min_max =
7979 			le16_to_cpu(resp->int_lat_tmr_min_max);
7980 		coal_cap->int_lat_tmr_max_max =
7981 			le16_to_cpu(resp->int_lat_tmr_max_max);
7982 		coal_cap->num_cmpl_aggr_int_max =
7983 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
7984 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
7985 	}
7986 	hwrm_req_drop(bp, req);
7987 }
7988 
7989 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
7990 {
7991 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7992 
7993 	return usec * 1000 / coal_cap->timer_units;
7994 }
7995 
7996 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
7997 	struct bnxt_coal *hw_coal,
7998 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7999 {
8000 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8001 	u16 val, tmr, max, flags = hw_coal->flags;
8002 	u32 cmpl_params = coal_cap->cmpl_params;
8003 
8004 	max = hw_coal->bufs_per_record * 128;
8005 	if (hw_coal->budget)
8006 		max = hw_coal->bufs_per_record * hw_coal->budget;
8007 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8008 
8009 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8010 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8011 
8012 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8013 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8014 
8015 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8016 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8017 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8018 
8019 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8020 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8021 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8022 
8023 	/* min timer set to 1/2 of interrupt timer */
8024 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8025 		val = tmr / 2;
8026 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8027 		req->int_lat_tmr_min = cpu_to_le16(val);
8028 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8029 	}
8030 
8031 	/* buf timer set to 1/4 of interrupt timer */
8032 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8033 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8034 
8035 	if (cmpl_params &
8036 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8037 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8038 		val = clamp_t(u16, tmr, 1,
8039 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8040 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8041 		req->enables |=
8042 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8043 	}
8044 
8045 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8046 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8047 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8048 	req->flags = cpu_to_le16(flags);
8049 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8050 }
8051 
8052 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8053 				   struct bnxt_coal *hw_coal)
8054 {
8055 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8056 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8057 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8058 	u32 nq_params = coal_cap->nq_params;
8059 	u16 tmr;
8060 	int rc;
8061 
8062 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8063 		return 0;
8064 
8065 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8066 	if (rc)
8067 		return rc;
8068 
8069 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8070 	req->flags =
8071 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8072 
8073 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8074 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8075 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8076 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8077 	return hwrm_req_send(bp, req);
8078 }
8079 
8080 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8081 {
8082 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8083 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8084 	struct bnxt_coal coal;
8085 	int rc;
8086 
8087 	/* Tick values in micro seconds.
8088 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8089 	 */
8090 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8091 
8092 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8093 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8094 
8095 	if (!bnapi->rx_ring)
8096 		return -ENODEV;
8097 
8098 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8099 	if (rc)
8100 		return rc;
8101 
8102 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8103 
8104 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8105 
8106 	return hwrm_req_send(bp, req_rx);
8107 }
8108 
8109 static int
8110 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8111 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8112 {
8113 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8114 
8115 	req->ring_id = cpu_to_le16(ring_id);
8116 	return hwrm_req_send(bp, req);
8117 }
8118 
8119 static int
8120 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8121 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8122 {
8123 	struct bnxt_tx_ring_info *txr;
8124 	int i, rc;
8125 
8126 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8127 		u16 ring_id;
8128 
8129 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8130 		req->ring_id = cpu_to_le16(ring_id);
8131 		rc = hwrm_req_send(bp, req);
8132 		if (rc)
8133 			return rc;
8134 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8135 			return 0;
8136 	}
8137 	return 0;
8138 }
8139 
8140 int bnxt_hwrm_set_coal(struct bnxt *bp)
8141 {
8142 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8143 	int i, rc;
8144 
8145 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8146 	if (rc)
8147 		return rc;
8148 
8149 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8150 	if (rc) {
8151 		hwrm_req_drop(bp, req_rx);
8152 		return rc;
8153 	}
8154 
8155 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8156 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8157 
8158 	hwrm_req_hold(bp, req_rx);
8159 	hwrm_req_hold(bp, req_tx);
8160 	for (i = 0; i < bp->cp_nr_rings; i++) {
8161 		struct bnxt_napi *bnapi = bp->bnapi[i];
8162 		struct bnxt_coal *hw_coal;
8163 
8164 		if (!bnapi->rx_ring)
8165 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8166 		else
8167 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8168 		if (rc)
8169 			break;
8170 
8171 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8172 			continue;
8173 
8174 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8175 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8176 			if (rc)
8177 				break;
8178 		}
8179 		if (bnapi->rx_ring)
8180 			hw_coal = &bp->rx_coal;
8181 		else
8182 			hw_coal = &bp->tx_coal;
8183 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8184 	}
8185 	hwrm_req_drop(bp, req_rx);
8186 	hwrm_req_drop(bp, req_tx);
8187 	return rc;
8188 }
8189 
8190 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8191 {
8192 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8193 	struct hwrm_stat_ctx_free_input *req;
8194 	int i;
8195 
8196 	if (!bp->bnapi)
8197 		return;
8198 
8199 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8200 		return;
8201 
8202 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8203 		return;
8204 	if (BNXT_FW_MAJ(bp) <= 20) {
8205 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8206 			hwrm_req_drop(bp, req);
8207 			return;
8208 		}
8209 		hwrm_req_hold(bp, req0);
8210 	}
8211 	hwrm_req_hold(bp, req);
8212 	for (i = 0; i < bp->cp_nr_rings; i++) {
8213 		struct bnxt_napi *bnapi = bp->bnapi[i];
8214 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8215 
8216 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8217 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8218 			if (req0) {
8219 				req0->stat_ctx_id = req->stat_ctx_id;
8220 				hwrm_req_send(bp, req0);
8221 			}
8222 			hwrm_req_send(bp, req);
8223 
8224 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8225 		}
8226 	}
8227 	hwrm_req_drop(bp, req);
8228 	if (req0)
8229 		hwrm_req_drop(bp, req0);
8230 }
8231 
8232 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8233 {
8234 	struct hwrm_stat_ctx_alloc_output *resp;
8235 	struct hwrm_stat_ctx_alloc_input *req;
8236 	int rc, i;
8237 
8238 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8239 		return 0;
8240 
8241 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8242 	if (rc)
8243 		return rc;
8244 
8245 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8246 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8247 
8248 	resp = hwrm_req_hold(bp, req);
8249 	for (i = 0; i < bp->cp_nr_rings; i++) {
8250 		struct bnxt_napi *bnapi = bp->bnapi[i];
8251 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8252 
8253 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8254 
8255 		rc = hwrm_req_send(bp, req);
8256 		if (rc)
8257 			break;
8258 
8259 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8260 
8261 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8262 	}
8263 	hwrm_req_drop(bp, req);
8264 	return rc;
8265 }
8266 
8267 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8268 {
8269 	struct hwrm_func_qcfg_output *resp;
8270 	struct hwrm_func_qcfg_input *req;
8271 	u16 flags;
8272 	int rc;
8273 
8274 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8275 	if (rc)
8276 		return rc;
8277 
8278 	req->fid = cpu_to_le16(0xffff);
8279 	resp = hwrm_req_hold(bp, req);
8280 	rc = hwrm_req_send(bp, req);
8281 	if (rc)
8282 		goto func_qcfg_exit;
8283 
8284 #ifdef CONFIG_BNXT_SRIOV
8285 	if (BNXT_VF(bp)) {
8286 		struct bnxt_vf_info *vf = &bp->vf;
8287 
8288 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8289 	} else {
8290 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8291 	}
8292 #endif
8293 	flags = le16_to_cpu(resp->flags);
8294 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8295 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8296 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8297 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8298 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8299 	}
8300 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8301 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8302 
8303 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8304 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8305 
8306 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8307 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8308 
8309 	switch (resp->port_partition_type) {
8310 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8311 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8312 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8313 		bp->port_partition_type = resp->port_partition_type;
8314 		break;
8315 	}
8316 	if (bp->hwrm_spec_code < 0x10707 ||
8317 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8318 		bp->br_mode = BRIDGE_MODE_VEB;
8319 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8320 		bp->br_mode = BRIDGE_MODE_VEPA;
8321 	else
8322 		bp->br_mode = BRIDGE_MODE_UNDEF;
8323 
8324 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8325 	if (!bp->max_mtu)
8326 		bp->max_mtu = BNXT_MAX_MTU;
8327 
8328 	if (bp->db_size)
8329 		goto func_qcfg_exit;
8330 
8331 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8332 	if (BNXT_CHIP_P5(bp)) {
8333 		if (BNXT_PF(bp))
8334 			bp->db_offset = DB_PF_OFFSET_P5;
8335 		else
8336 			bp->db_offset = DB_VF_OFFSET_P5;
8337 	}
8338 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8339 				 1024);
8340 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8341 	    bp->db_size <= bp->db_offset)
8342 		bp->db_size = pci_resource_len(bp->pdev, 2);
8343 
8344 func_qcfg_exit:
8345 	hwrm_req_drop(bp, req);
8346 	return rc;
8347 }
8348 
8349 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8350 				      u8 init_val, u8 init_offset,
8351 				      bool init_mask_set)
8352 {
8353 	ctxm->init_value = init_val;
8354 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8355 	if (init_mask_set)
8356 		ctxm->init_offset = init_offset * 4;
8357 	else
8358 		ctxm->init_value = 0;
8359 }
8360 
8361 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8362 {
8363 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8364 	u16 type;
8365 
8366 	for (type = 0; type < ctx_max; type++) {
8367 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8368 		int n = 1;
8369 
8370 		if (!ctxm->max_entries)
8371 			continue;
8372 
8373 		if (ctxm->instance_bmap)
8374 			n = hweight32(ctxm->instance_bmap);
8375 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8376 		if (!ctxm->pg_info)
8377 			return -ENOMEM;
8378 	}
8379 	return 0;
8380 }
8381 
8382 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8383 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8384 
8385 #define BNXT_CTX_INIT_VALID(flags)	\
8386 	(!!((flags) &			\
8387 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8388 
8389 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8390 {
8391 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8392 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8393 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8394 	u16 type;
8395 	int rc;
8396 
8397 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8398 	if (rc)
8399 		return rc;
8400 
8401 	if (!ctx) {
8402 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8403 		if (!ctx)
8404 			return -ENOMEM;
8405 		bp->ctx = ctx;
8406 	}
8407 
8408 	resp = hwrm_req_hold(bp, req);
8409 
8410 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8411 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8412 		u8 init_val, init_off, i;
8413 		u32 max_entries;
8414 		u16 entry_size;
8415 		__le32 *p;
8416 		u32 flags;
8417 
8418 		req->type = cpu_to_le16(type);
8419 		rc = hwrm_req_send(bp, req);
8420 		if (rc)
8421 			goto ctx_done;
8422 		flags = le32_to_cpu(resp->flags);
8423 		type = le16_to_cpu(resp->next_valid_type);
8424 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8425 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8426 			continue;
8427 		}
8428 		entry_size = le16_to_cpu(resp->entry_size);
8429 		max_entries = le32_to_cpu(resp->max_num_entries);
8430 		if (ctxm->mem_valid) {
8431 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8432 			    ctxm->entry_size != entry_size ||
8433 			    ctxm->max_entries != max_entries)
8434 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8435 			else
8436 				continue;
8437 		}
8438 		ctxm->type = le16_to_cpu(resp->type);
8439 		ctxm->entry_size = entry_size;
8440 		ctxm->flags = flags;
8441 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8442 		ctxm->entry_multiple = resp->entry_multiple;
8443 		ctxm->max_entries = max_entries;
8444 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8445 		init_val = resp->ctx_init_value;
8446 		init_off = resp->ctx_init_offset;
8447 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8448 					  BNXT_CTX_INIT_VALID(flags));
8449 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8450 					      BNXT_MAX_SPLIT_ENTRY);
8451 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8452 		     i++, p++)
8453 			ctxm->split[i] = le32_to_cpu(*p);
8454 	}
8455 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8456 
8457 ctx_done:
8458 	hwrm_req_drop(bp, req);
8459 	return rc;
8460 }
8461 
8462 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8463 {
8464 	struct hwrm_func_backing_store_qcaps_output *resp;
8465 	struct hwrm_func_backing_store_qcaps_input *req;
8466 	int rc;
8467 
8468 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8469 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8470 		return 0;
8471 
8472 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8473 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8474 
8475 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8476 	if (rc)
8477 		return rc;
8478 
8479 	resp = hwrm_req_hold(bp, req);
8480 	rc = hwrm_req_send_silent(bp, req);
8481 	if (!rc) {
8482 		struct bnxt_ctx_mem_type *ctxm;
8483 		struct bnxt_ctx_mem_info *ctx;
8484 		u8 init_val, init_idx = 0;
8485 		u16 init_mask;
8486 
8487 		ctx = bp->ctx;
8488 		if (!ctx) {
8489 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8490 			if (!ctx) {
8491 				rc = -ENOMEM;
8492 				goto ctx_err;
8493 			}
8494 			bp->ctx = ctx;
8495 		}
8496 		init_val = resp->ctx_kind_initializer;
8497 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8498 
8499 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8500 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8501 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8502 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8503 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8504 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8505 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8506 					  (init_mask & (1 << init_idx++)) != 0);
8507 
8508 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8509 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8510 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8511 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8512 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8513 					  (init_mask & (1 << init_idx++)) != 0);
8514 
8515 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8516 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8517 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8518 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8519 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8520 					  (init_mask & (1 << init_idx++)) != 0);
8521 
8522 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8523 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8524 		ctxm->max_entries = ctxm->vnic_entries +
8525 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8526 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8527 		bnxt_init_ctx_initializer(ctxm, init_val,
8528 					  resp->vnic_init_offset,
8529 					  (init_mask & (1 << init_idx++)) != 0);
8530 
8531 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8532 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8533 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8534 		bnxt_init_ctx_initializer(ctxm, init_val,
8535 					  resp->stat_init_offset,
8536 					  (init_mask & (1 << init_idx++)) != 0);
8537 
8538 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8539 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8540 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8541 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8542 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8543 		if (!ctxm->entry_multiple)
8544 			ctxm->entry_multiple = 1;
8545 
8546 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8547 
8548 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8549 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8550 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8551 		ctxm->mrav_num_entries_units =
8552 			le16_to_cpu(resp->mrav_num_entries_units);
8553 		bnxt_init_ctx_initializer(ctxm, init_val,
8554 					  resp->mrav_init_offset,
8555 					  (init_mask & (1 << init_idx++)) != 0);
8556 
8557 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8558 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8559 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8560 
8561 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8562 		if (!ctx->tqm_fp_rings_count)
8563 			ctx->tqm_fp_rings_count = bp->max_q;
8564 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8565 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8566 
8567 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8568 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8569 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8570 
8571 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8572 	} else {
8573 		rc = 0;
8574 	}
8575 ctx_err:
8576 	hwrm_req_drop(bp, req);
8577 	return rc;
8578 }
8579 
8580 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8581 				  __le64 *pg_dir)
8582 {
8583 	if (!rmem->nr_pages)
8584 		return;
8585 
8586 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8587 	if (rmem->depth >= 1) {
8588 		if (rmem->depth == 2)
8589 			*pg_attr |= 2;
8590 		else
8591 			*pg_attr |= 1;
8592 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8593 	} else {
8594 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8595 	}
8596 }
8597 
8598 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8599 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8600 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8601 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8602 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8603 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8604 
8605 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8606 {
8607 	struct hwrm_func_backing_store_cfg_input *req;
8608 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8609 	struct bnxt_ctx_pg_info *ctx_pg;
8610 	struct bnxt_ctx_mem_type *ctxm;
8611 	void **__req = (void **)&req;
8612 	u32 req_len = sizeof(*req);
8613 	__le32 *num_entries;
8614 	__le64 *pg_dir;
8615 	u32 flags = 0;
8616 	u8 *pg_attr;
8617 	u32 ena;
8618 	int rc;
8619 	int i;
8620 
8621 	if (!ctx)
8622 		return 0;
8623 
8624 	if (req_len > bp->hwrm_max_ext_req_len)
8625 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8626 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8627 	if (rc)
8628 		return rc;
8629 
8630 	req->enables = cpu_to_le32(enables);
8631 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8632 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8633 		ctx_pg = ctxm->pg_info;
8634 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8635 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8636 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8637 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8638 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8639 				      &req->qpc_pg_size_qpc_lvl,
8640 				      &req->qpc_page_dir);
8641 
8642 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8643 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8644 	}
8645 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8646 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8647 		ctx_pg = ctxm->pg_info;
8648 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8649 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8650 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8651 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8652 				      &req->srq_pg_size_srq_lvl,
8653 				      &req->srq_page_dir);
8654 	}
8655 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8656 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8657 		ctx_pg = ctxm->pg_info;
8658 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8659 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8660 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8661 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8662 				      &req->cq_pg_size_cq_lvl,
8663 				      &req->cq_page_dir);
8664 	}
8665 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8666 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8667 		ctx_pg = ctxm->pg_info;
8668 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8669 		req->vnic_num_ring_table_entries =
8670 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8671 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8672 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8673 				      &req->vnic_pg_size_vnic_lvl,
8674 				      &req->vnic_page_dir);
8675 	}
8676 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8677 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8678 		ctx_pg = ctxm->pg_info;
8679 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8680 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8681 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8682 				      &req->stat_pg_size_stat_lvl,
8683 				      &req->stat_page_dir);
8684 	}
8685 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8686 		u32 units;
8687 
8688 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8689 		ctx_pg = ctxm->pg_info;
8690 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8691 		units = ctxm->mrav_num_entries_units;
8692 		if (units) {
8693 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8694 			u32 entries;
8695 
8696 			num_mr = ctx_pg->entries - num_ah;
8697 			entries = ((num_mr / units) << 16) | (num_ah / units);
8698 			req->mrav_num_entries = cpu_to_le32(entries);
8699 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8700 		}
8701 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8702 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8703 				      &req->mrav_pg_size_mrav_lvl,
8704 				      &req->mrav_page_dir);
8705 	}
8706 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8707 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8708 		ctx_pg = ctxm->pg_info;
8709 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8710 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8711 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8712 				      &req->tim_pg_size_tim_lvl,
8713 				      &req->tim_page_dir);
8714 	}
8715 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8716 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8717 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8718 	     pg_dir = &req->tqm_sp_page_dir,
8719 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8720 	     ctx_pg = ctxm->pg_info;
8721 	     i < BNXT_MAX_TQM_RINGS;
8722 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8723 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8724 		if (!(enables & ena))
8725 			continue;
8726 
8727 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8728 		*num_entries = cpu_to_le32(ctx_pg->entries);
8729 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8730 	}
8731 	req->flags = cpu_to_le32(flags);
8732 	return hwrm_req_send(bp, req);
8733 }
8734 
8735 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8736 				  struct bnxt_ctx_pg_info *ctx_pg)
8737 {
8738 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8739 
8740 	rmem->page_size = BNXT_PAGE_SIZE;
8741 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8742 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8743 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8744 	if (rmem->depth >= 1)
8745 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8746 	return bnxt_alloc_ring(bp, rmem);
8747 }
8748 
8749 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8750 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8751 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8752 {
8753 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8754 	int rc;
8755 
8756 	if (!mem_size)
8757 		return -EINVAL;
8758 
8759 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8760 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8761 		ctx_pg->nr_pages = 0;
8762 		return -EINVAL;
8763 	}
8764 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8765 		int nr_tbls, i;
8766 
8767 		rmem->depth = 2;
8768 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8769 					     GFP_KERNEL);
8770 		if (!ctx_pg->ctx_pg_tbl)
8771 			return -ENOMEM;
8772 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8773 		rmem->nr_pages = nr_tbls;
8774 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8775 		if (rc)
8776 			return rc;
8777 		for (i = 0; i < nr_tbls; i++) {
8778 			struct bnxt_ctx_pg_info *pg_tbl;
8779 
8780 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8781 			if (!pg_tbl)
8782 				return -ENOMEM;
8783 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8784 			rmem = &pg_tbl->ring_mem;
8785 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8786 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8787 			rmem->depth = 1;
8788 			rmem->nr_pages = MAX_CTX_PAGES;
8789 			rmem->ctx_mem = ctxm;
8790 			if (i == (nr_tbls - 1)) {
8791 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8792 
8793 				if (rem)
8794 					rmem->nr_pages = rem;
8795 			}
8796 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8797 			if (rc)
8798 				break;
8799 		}
8800 	} else {
8801 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8802 		if (rmem->nr_pages > 1 || depth)
8803 			rmem->depth = 1;
8804 		rmem->ctx_mem = ctxm;
8805 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8806 	}
8807 	return rc;
8808 }
8809 
8810 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
8811 				    struct bnxt_ctx_pg_info *ctx_pg,
8812 				    void *buf, size_t offset, size_t head,
8813 				    size_t tail)
8814 {
8815 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8816 	size_t nr_pages = ctx_pg->nr_pages;
8817 	int page_size = rmem->page_size;
8818 	size_t len = 0, total_len = 0;
8819 	u16 depth = rmem->depth;
8820 
8821 	tail %= nr_pages * page_size;
8822 	do {
8823 		if (depth > 1) {
8824 			int i = head / (page_size * MAX_CTX_PAGES);
8825 			struct bnxt_ctx_pg_info *pg_tbl;
8826 
8827 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8828 			rmem = &pg_tbl->ring_mem;
8829 		}
8830 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
8831 		head += len;
8832 		offset += len;
8833 		total_len += len;
8834 		if (head >= nr_pages * page_size)
8835 			head = 0;
8836 	} while (head != tail);
8837 	return total_len;
8838 }
8839 
8840 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
8841 				  struct bnxt_ctx_pg_info *ctx_pg)
8842 {
8843 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8844 
8845 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
8846 	    ctx_pg->ctx_pg_tbl) {
8847 		int i, nr_tbls = rmem->nr_pages;
8848 
8849 		for (i = 0; i < nr_tbls; i++) {
8850 			struct bnxt_ctx_pg_info *pg_tbl;
8851 			struct bnxt_ring_mem_info *rmem2;
8852 
8853 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8854 			if (!pg_tbl)
8855 				continue;
8856 			rmem2 = &pg_tbl->ring_mem;
8857 			bnxt_free_ring(bp, rmem2);
8858 			ctx_pg->ctx_pg_arr[i] = NULL;
8859 			kfree(pg_tbl);
8860 			ctx_pg->ctx_pg_tbl[i] = NULL;
8861 		}
8862 		kfree(ctx_pg->ctx_pg_tbl);
8863 		ctx_pg->ctx_pg_tbl = NULL;
8864 	}
8865 	bnxt_free_ring(bp, rmem);
8866 	ctx_pg->nr_pages = 0;
8867 }
8868 
8869 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
8870 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
8871 				   u8 pg_lvl)
8872 {
8873 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8874 	int i, rc = 0, n = 1;
8875 	u32 mem_size;
8876 
8877 	if (!ctxm->entry_size || !ctx_pg)
8878 		return -EINVAL;
8879 	if (ctxm->instance_bmap)
8880 		n = hweight32(ctxm->instance_bmap);
8881 	if (ctxm->entry_multiple)
8882 		entries = roundup(entries, ctxm->entry_multiple);
8883 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
8884 	mem_size = entries * ctxm->entry_size;
8885 	for (i = 0; i < n && !rc; i++) {
8886 		ctx_pg[i].entries = entries;
8887 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
8888 					    ctxm->init_value ? ctxm : NULL);
8889 	}
8890 	if (!rc)
8891 		ctxm->mem_valid = 1;
8892 	return rc;
8893 }
8894 
8895 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
8896 					       struct bnxt_ctx_mem_type *ctxm,
8897 					       bool last)
8898 {
8899 	struct hwrm_func_backing_store_cfg_v2_input *req;
8900 	u32 instance_bmap = ctxm->instance_bmap;
8901 	int i, j, rc = 0, n = 1;
8902 	__le32 *p;
8903 
8904 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
8905 		return 0;
8906 
8907 	if (instance_bmap)
8908 		n = hweight32(ctxm->instance_bmap);
8909 	else
8910 		instance_bmap = 1;
8911 
8912 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
8913 	if (rc)
8914 		return rc;
8915 	hwrm_req_hold(bp, req);
8916 	req->type = cpu_to_le16(ctxm->type);
8917 	req->entry_size = cpu_to_le16(ctxm->entry_size);
8918 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
8919 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
8920 		struct bnxt_bs_trace_info *bs_trace;
8921 		u32 enables;
8922 
8923 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
8924 		req->enables = cpu_to_le32(enables);
8925 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
8926 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
8927 	}
8928 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
8929 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
8930 		p[i] = cpu_to_le32(ctxm->split[i]);
8931 	for (i = 0, j = 0; j < n && !rc; i++) {
8932 		struct bnxt_ctx_pg_info *ctx_pg;
8933 
8934 		if (!(instance_bmap & (1 << i)))
8935 			continue;
8936 		req->instance = cpu_to_le16(i);
8937 		ctx_pg = &ctxm->pg_info[j++];
8938 		if (!ctx_pg->entries)
8939 			continue;
8940 		req->num_entries = cpu_to_le32(ctx_pg->entries);
8941 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8942 				      &req->page_size_pbl_level,
8943 				      &req->page_dir);
8944 		if (last && j == n)
8945 			req->flags =
8946 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
8947 		rc = hwrm_req_send(bp, req);
8948 	}
8949 	hwrm_req_drop(bp, req);
8950 	return rc;
8951 }
8952 
8953 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
8954 {
8955 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8956 	struct bnxt_ctx_mem_type *ctxm;
8957 	u16 last_type = BNXT_CTX_INV;
8958 	int rc = 0;
8959 	u16 type;
8960 
8961 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) {
8962 		ctxm = &ctx->ctx_arr[type];
8963 		if (!bnxt_bs_trace_avail(bp, type))
8964 			continue;
8965 		if (!ctxm->mem_valid) {
8966 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
8967 						     ctxm->max_entries, 1);
8968 			if (rc) {
8969 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
8970 					    type);
8971 				continue;
8972 			}
8973 			bnxt_bs_trace_init(bp, ctxm);
8974 			last_type = type;
8975 		}
8976 	}
8977 
8978 	if (last_type == BNXT_CTX_INV) {
8979 		if (!ena)
8980 			return 0;
8981 		else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
8982 			last_type = BNXT_CTX_MAX - 1;
8983 		else
8984 			last_type = BNXT_CTX_L2_MAX - 1;
8985 	}
8986 	ctx->ctx_arr[last_type].last = 1;
8987 
8988 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
8989 		ctxm = &ctx->ctx_arr[type];
8990 
8991 		if (!ctxm->mem_valid)
8992 			continue;
8993 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
8994 		if (rc)
8995 			return rc;
8996 	}
8997 	return 0;
8998 }
8999 
9000 /**
9001  * __bnxt_copy_ctx_mem - copy host context memory
9002  * @bp: The driver context
9003  * @ctxm: The pointer to the context memory type
9004  * @buf: The destination buffer or NULL to just obtain the length
9005  * @offset: The buffer offset to copy the data to
9006  * @head: The head offset of context memory to copy from
9007  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9008  *
9009  * This function is called for debugging purposes to dump the host context
9010  * used by the chip.
9011  *
9012  * Return: Length of memory copied
9013  */
9014 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9015 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9016 				  size_t offset, size_t head, size_t tail)
9017 {
9018 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9019 	size_t len = 0, total_len = 0;
9020 	int i, n = 1;
9021 
9022 	if (!ctx_pg)
9023 		return 0;
9024 
9025 	if (ctxm->instance_bmap)
9026 		n = hweight32(ctxm->instance_bmap);
9027 	for (i = 0; i < n; i++) {
9028 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9029 					    tail);
9030 		offset += len;
9031 		total_len += len;
9032 	}
9033 	return total_len;
9034 }
9035 
9036 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9037 			 void *buf, size_t offset)
9038 {
9039 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9040 
9041 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9042 }
9043 
9044 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9045 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9046 {
9047 	struct bnxt_ctx_pg_info *ctx_pg;
9048 	int i, n = 1;
9049 
9050 	ctxm->last = 0;
9051 
9052 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9053 		return;
9054 
9055 	ctx_pg = ctxm->pg_info;
9056 	if (ctx_pg) {
9057 		if (ctxm->instance_bmap)
9058 			n = hweight32(ctxm->instance_bmap);
9059 		for (i = 0; i < n; i++)
9060 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9061 
9062 		kfree(ctx_pg);
9063 		ctxm->pg_info = NULL;
9064 		ctxm->mem_valid = 0;
9065 	}
9066 	memset(ctxm, 0, sizeof(*ctxm));
9067 }
9068 
9069 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9070 {
9071 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9072 	u16 type;
9073 
9074 	if (!ctx)
9075 		return;
9076 
9077 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9078 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9079 
9080 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9081 	if (force) {
9082 		kfree(ctx);
9083 		bp->ctx = NULL;
9084 	}
9085 }
9086 
9087 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9088 {
9089 	struct bnxt_ctx_mem_type *ctxm;
9090 	struct bnxt_ctx_mem_info *ctx;
9091 	u32 l2_qps, qp1_qps, max_qps;
9092 	u32 ena, entries_sp, entries;
9093 	u32 srqs, max_srqs, min;
9094 	u32 num_mr, num_ah;
9095 	u32 extra_srqs = 0;
9096 	u32 extra_qps = 0;
9097 	u32 fast_qpmd_qps;
9098 	u8 pg_lvl = 1;
9099 	int i, rc;
9100 
9101 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9102 	if (rc) {
9103 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9104 			   rc);
9105 		return rc;
9106 	}
9107 	ctx = bp->ctx;
9108 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9109 		return 0;
9110 
9111 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9112 	l2_qps = ctxm->qp_l2_entries;
9113 	qp1_qps = ctxm->qp_qp1_entries;
9114 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9115 	max_qps = ctxm->max_entries;
9116 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9117 	srqs = ctxm->srq_l2_entries;
9118 	max_srqs = ctxm->max_entries;
9119 	ena = 0;
9120 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9121 		pg_lvl = 2;
9122 		extra_qps = min_t(u32, 65536, max_qps - l2_qps - qp1_qps);
9123 		/* allocate extra qps if fw supports RoCE fast qp destroy feature */
9124 		extra_qps += fast_qpmd_qps;
9125 		extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9126 		if (fast_qpmd_qps)
9127 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9128 	}
9129 
9130 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9131 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9132 				     pg_lvl);
9133 	if (rc)
9134 		return rc;
9135 
9136 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9137 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9138 	if (rc)
9139 		return rc;
9140 
9141 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9142 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9143 				     extra_qps * 2, pg_lvl);
9144 	if (rc)
9145 		return rc;
9146 
9147 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9148 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9149 	if (rc)
9150 		return rc;
9151 
9152 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9153 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9154 	if (rc)
9155 		return rc;
9156 
9157 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9158 		goto skip_rdma;
9159 
9160 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9161 	/* 128K extra is needed to accommodate static AH context
9162 	 * allocation by f/w.
9163 	 */
9164 	num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9165 	num_ah = min_t(u32, num_mr, 1024 * 128);
9166 	ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9167 	if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9168 		ctxm->mrav_av_entries = num_ah;
9169 
9170 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9171 	if (rc)
9172 		return rc;
9173 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9174 
9175 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9176 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9177 	if (rc)
9178 		return rc;
9179 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9180 
9181 skip_rdma:
9182 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9183 	min = ctxm->min_entries;
9184 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9185 		     2 * (extra_qps + qp1_qps) + min;
9186 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9187 	if (rc)
9188 		return rc;
9189 
9190 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9191 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9192 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9193 	if (rc)
9194 		return rc;
9195 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9196 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9197 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9198 
9199 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9200 		rc = bnxt_backing_store_cfg_v2(bp, ena);
9201 	else
9202 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9203 	if (rc) {
9204 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9205 			   rc);
9206 		return rc;
9207 	}
9208 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9209 	return 0;
9210 }
9211 
9212 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9213 {
9214 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9215 	u16 page_attr;
9216 	int rc;
9217 
9218 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9219 		return 0;
9220 
9221 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9222 	if (rc)
9223 		return rc;
9224 
9225 	if (BNXT_PAGE_SIZE == 0x2000)
9226 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9227 	else if (BNXT_PAGE_SIZE == 0x10000)
9228 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9229 	else
9230 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9231 	req->pg_size_lvl = cpu_to_le16(page_attr |
9232 				       bp->fw_crash_mem->ring_mem.depth);
9233 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9234 	req->size = cpu_to_le32(bp->fw_crash_len);
9235 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9236 	return hwrm_req_send(bp, req);
9237 }
9238 
9239 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9240 {
9241 	if (bp->fw_crash_mem) {
9242 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9243 		kfree(bp->fw_crash_mem);
9244 		bp->fw_crash_mem = NULL;
9245 	}
9246 }
9247 
9248 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9249 {
9250 	u32 mem_size = 0;
9251 	int rc;
9252 
9253 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9254 		return 0;
9255 
9256 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9257 	if (rc)
9258 		return rc;
9259 
9260 	mem_size = round_up(mem_size, 4);
9261 
9262 	/* keep and use the existing pages */
9263 	if (bp->fw_crash_mem &&
9264 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9265 		goto alloc_done;
9266 
9267 	if (bp->fw_crash_mem)
9268 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9269 	else
9270 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
9271 					   GFP_KERNEL);
9272 	if (!bp->fw_crash_mem)
9273 		return -ENOMEM;
9274 
9275 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9276 	if (rc) {
9277 		bnxt_free_crash_dump_mem(bp);
9278 		return rc;
9279 	}
9280 
9281 alloc_done:
9282 	bp->fw_crash_len = mem_size;
9283 	return 0;
9284 }
9285 
9286 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9287 {
9288 	struct hwrm_func_resource_qcaps_output *resp;
9289 	struct hwrm_func_resource_qcaps_input *req;
9290 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9291 	int rc;
9292 
9293 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9294 	if (rc)
9295 		return rc;
9296 
9297 	req->fid = cpu_to_le16(0xffff);
9298 	resp = hwrm_req_hold(bp, req);
9299 	rc = hwrm_req_send_silent(bp, req);
9300 	if (rc)
9301 		goto hwrm_func_resc_qcaps_exit;
9302 
9303 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9304 	if (!all)
9305 		goto hwrm_func_resc_qcaps_exit;
9306 
9307 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9308 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9309 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9310 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9311 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9312 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9313 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9314 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9315 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9316 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9317 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9318 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9319 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9320 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9321 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9322 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9323 
9324 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9325 		u16 max_msix = le16_to_cpu(resp->max_msix);
9326 
9327 		hw_resc->max_nqs = max_msix;
9328 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9329 	}
9330 
9331 	if (BNXT_PF(bp)) {
9332 		struct bnxt_pf_info *pf = &bp->pf;
9333 
9334 		pf->vf_resv_strategy =
9335 			le16_to_cpu(resp->vf_reservation_strategy);
9336 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9337 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9338 	}
9339 hwrm_func_resc_qcaps_exit:
9340 	hwrm_req_drop(bp, req);
9341 	return rc;
9342 }
9343 
9344 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9345 {
9346 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9347 	struct hwrm_port_mac_ptp_qcfg_input *req;
9348 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9349 	u8 flags;
9350 	int rc;
9351 
9352 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9353 		rc = -ENODEV;
9354 		goto no_ptp;
9355 	}
9356 
9357 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9358 	if (rc)
9359 		goto no_ptp;
9360 
9361 	req->port_id = cpu_to_le16(bp->pf.port_id);
9362 	resp = hwrm_req_hold(bp, req);
9363 	rc = hwrm_req_send(bp, req);
9364 	if (rc)
9365 		goto exit;
9366 
9367 	flags = resp->flags;
9368 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9369 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9370 		rc = -ENODEV;
9371 		goto exit;
9372 	}
9373 	if (!ptp) {
9374 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9375 		if (!ptp) {
9376 			rc = -ENOMEM;
9377 			goto exit;
9378 		}
9379 		ptp->bp = bp;
9380 		bp->ptp_cfg = ptp;
9381 	}
9382 
9383 	if (flags &
9384 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9385 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9386 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9387 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9388 	} else if (BNXT_CHIP_P5(bp)) {
9389 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9390 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9391 	} else {
9392 		rc = -ENODEV;
9393 		goto exit;
9394 	}
9395 	ptp->rtc_configured =
9396 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9397 	rc = bnxt_ptp_init(bp);
9398 	if (rc)
9399 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9400 exit:
9401 	hwrm_req_drop(bp, req);
9402 	if (!rc)
9403 		return 0;
9404 
9405 no_ptp:
9406 	bnxt_ptp_clear(bp);
9407 	kfree(ptp);
9408 	bp->ptp_cfg = NULL;
9409 	return rc;
9410 }
9411 
9412 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9413 {
9414 	struct hwrm_func_qcaps_output *resp;
9415 	struct hwrm_func_qcaps_input *req;
9416 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9417 	u32 flags, flags_ext, flags_ext2;
9418 	int rc;
9419 
9420 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9421 	if (rc)
9422 		return rc;
9423 
9424 	req->fid = cpu_to_le16(0xffff);
9425 	resp = hwrm_req_hold(bp, req);
9426 	rc = hwrm_req_send(bp, req);
9427 	if (rc)
9428 		goto hwrm_func_qcaps_exit;
9429 
9430 	flags = le32_to_cpu(resp->flags);
9431 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9432 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9433 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9434 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9435 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9436 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9437 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9438 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9439 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9440 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9441 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9442 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9443 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9444 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9445 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9446 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9447 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9448 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9449 
9450 	flags_ext = le32_to_cpu(resp->flags_ext);
9451 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9452 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9453 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9454 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9455 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9456 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9457 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9458 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9459 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9460 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9461 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9462 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9463 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9464 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9465 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9466 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9467 
9468 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9469 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9470 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9471 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9472 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9473 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9474 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9475 	if (BNXT_PF(bp) &&
9476 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9477 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9478 
9479 	bp->tx_push_thresh = 0;
9480 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9481 	    BNXT_FW_MAJ(bp) > 217)
9482 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9483 
9484 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9485 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9486 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9487 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9488 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9489 	if (!hw_resc->max_hw_ring_grps)
9490 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9491 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9492 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9493 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9494 
9495 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9496 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9497 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9498 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9499 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9500 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9501 
9502 	if (BNXT_PF(bp)) {
9503 		struct bnxt_pf_info *pf = &bp->pf;
9504 
9505 		pf->fw_fid = le16_to_cpu(resp->fid);
9506 		pf->port_id = le16_to_cpu(resp->port_id);
9507 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9508 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9509 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9510 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9511 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9512 			bp->flags |= BNXT_FLAG_WOL_CAP;
9513 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9514 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9515 		} else {
9516 			bnxt_ptp_clear(bp);
9517 			kfree(bp->ptp_cfg);
9518 			bp->ptp_cfg = NULL;
9519 		}
9520 	} else {
9521 #ifdef CONFIG_BNXT_SRIOV
9522 		struct bnxt_vf_info *vf = &bp->vf;
9523 
9524 		vf->fw_fid = le16_to_cpu(resp->fid);
9525 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9526 #endif
9527 	}
9528 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9529 
9530 hwrm_func_qcaps_exit:
9531 	hwrm_req_drop(bp, req);
9532 	return rc;
9533 }
9534 
9535 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9536 {
9537 	struct hwrm_dbg_qcaps_output *resp;
9538 	struct hwrm_dbg_qcaps_input *req;
9539 	int rc;
9540 
9541 	bp->fw_dbg_cap = 0;
9542 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9543 		return;
9544 
9545 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9546 	if (rc)
9547 		return;
9548 
9549 	req->fid = cpu_to_le16(0xffff);
9550 	resp = hwrm_req_hold(bp, req);
9551 	rc = hwrm_req_send(bp, req);
9552 	if (rc)
9553 		goto hwrm_dbg_qcaps_exit;
9554 
9555 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9556 
9557 hwrm_dbg_qcaps_exit:
9558 	hwrm_req_drop(bp, req);
9559 }
9560 
9561 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9562 
9563 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9564 {
9565 	int rc;
9566 
9567 	rc = __bnxt_hwrm_func_qcaps(bp);
9568 	if (rc)
9569 		return rc;
9570 
9571 	bnxt_hwrm_dbg_qcaps(bp);
9572 
9573 	rc = bnxt_hwrm_queue_qportcfg(bp);
9574 	if (rc) {
9575 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9576 		return rc;
9577 	}
9578 	if (bp->hwrm_spec_code >= 0x10803) {
9579 		rc = bnxt_alloc_ctx_mem(bp);
9580 		if (rc)
9581 			return rc;
9582 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9583 		if (!rc)
9584 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9585 	}
9586 	return 0;
9587 }
9588 
9589 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9590 {
9591 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9592 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9593 	u32 flags;
9594 	int rc;
9595 
9596 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9597 		return 0;
9598 
9599 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9600 	if (rc)
9601 		return rc;
9602 
9603 	resp = hwrm_req_hold(bp, req);
9604 	rc = hwrm_req_send(bp, req);
9605 	if (rc)
9606 		goto hwrm_cfa_adv_qcaps_exit;
9607 
9608 	flags = le32_to_cpu(resp->flags);
9609 	if (flags &
9610 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9611 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9612 
9613 	if (flags &
9614 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9615 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9616 
9617 	if (flags &
9618 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9619 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9620 
9621 hwrm_cfa_adv_qcaps_exit:
9622 	hwrm_req_drop(bp, req);
9623 	return rc;
9624 }
9625 
9626 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9627 {
9628 	if (bp->fw_health)
9629 		return 0;
9630 
9631 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9632 	if (!bp->fw_health)
9633 		return -ENOMEM;
9634 
9635 	mutex_init(&bp->fw_health->lock);
9636 	return 0;
9637 }
9638 
9639 static int bnxt_alloc_fw_health(struct bnxt *bp)
9640 {
9641 	int rc;
9642 
9643 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9644 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9645 		return 0;
9646 
9647 	rc = __bnxt_alloc_fw_health(bp);
9648 	if (rc) {
9649 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9650 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9651 		return rc;
9652 	}
9653 
9654 	return 0;
9655 }
9656 
9657 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9658 {
9659 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9660 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9661 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9662 }
9663 
9664 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9665 {
9666 	struct bnxt_fw_health *fw_health = bp->fw_health;
9667 	u32 reg_type;
9668 
9669 	if (!fw_health)
9670 		return;
9671 
9672 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9673 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9674 		fw_health->status_reliable = false;
9675 
9676 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9677 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9678 		fw_health->resets_reliable = false;
9679 }
9680 
9681 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9682 {
9683 	void __iomem *hs;
9684 	u32 status_loc;
9685 	u32 reg_type;
9686 	u32 sig;
9687 
9688 	if (bp->fw_health)
9689 		bp->fw_health->status_reliable = false;
9690 
9691 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9692 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9693 
9694 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9695 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9696 		if (!bp->chip_num) {
9697 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9698 			bp->chip_num = readl(bp->bar0 +
9699 					     BNXT_FW_HEALTH_WIN_BASE +
9700 					     BNXT_GRC_REG_CHIP_NUM);
9701 		}
9702 		if (!BNXT_CHIP_P5_PLUS(bp))
9703 			return;
9704 
9705 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9706 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9707 	} else {
9708 		status_loc = readl(hs + offsetof(struct hcomm_status,
9709 						 fw_status_loc));
9710 	}
9711 
9712 	if (__bnxt_alloc_fw_health(bp)) {
9713 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9714 		return;
9715 	}
9716 
9717 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9718 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9719 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9720 		__bnxt_map_fw_health_reg(bp, status_loc);
9721 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9722 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9723 	}
9724 
9725 	bp->fw_health->status_reliable = true;
9726 }
9727 
9728 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9729 {
9730 	struct bnxt_fw_health *fw_health = bp->fw_health;
9731 	u32 reg_base = 0xffffffff;
9732 	int i;
9733 
9734 	bp->fw_health->status_reliable = false;
9735 	bp->fw_health->resets_reliable = false;
9736 	/* Only pre-map the monitoring GRC registers using window 3 */
9737 	for (i = 0; i < 4; i++) {
9738 		u32 reg = fw_health->regs[i];
9739 
9740 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9741 			continue;
9742 		if (reg_base == 0xffffffff)
9743 			reg_base = reg & BNXT_GRC_BASE_MASK;
9744 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9745 			return -ERANGE;
9746 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9747 	}
9748 	bp->fw_health->status_reliable = true;
9749 	bp->fw_health->resets_reliable = true;
9750 	if (reg_base == 0xffffffff)
9751 		return 0;
9752 
9753 	__bnxt_map_fw_health_reg(bp, reg_base);
9754 	return 0;
9755 }
9756 
9757 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9758 {
9759 	if (!bp->fw_health)
9760 		return;
9761 
9762 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9763 		bp->fw_health->status_reliable = true;
9764 		bp->fw_health->resets_reliable = true;
9765 	} else {
9766 		bnxt_try_map_fw_health_reg(bp);
9767 	}
9768 }
9769 
9770 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9771 {
9772 	struct bnxt_fw_health *fw_health = bp->fw_health;
9773 	struct hwrm_error_recovery_qcfg_output *resp;
9774 	struct hwrm_error_recovery_qcfg_input *req;
9775 	int rc, i;
9776 
9777 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9778 		return 0;
9779 
9780 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9781 	if (rc)
9782 		return rc;
9783 
9784 	resp = hwrm_req_hold(bp, req);
9785 	rc = hwrm_req_send(bp, req);
9786 	if (rc)
9787 		goto err_recovery_out;
9788 	fw_health->flags = le32_to_cpu(resp->flags);
9789 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9790 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9791 		rc = -EINVAL;
9792 		goto err_recovery_out;
9793 	}
9794 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9795 	fw_health->master_func_wait_dsecs =
9796 		le32_to_cpu(resp->master_func_wait_period);
9797 	fw_health->normal_func_wait_dsecs =
9798 		le32_to_cpu(resp->normal_func_wait_period);
9799 	fw_health->post_reset_wait_dsecs =
9800 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9801 	fw_health->post_reset_max_wait_dsecs =
9802 		le32_to_cpu(resp->max_bailout_time_after_reset);
9803 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9804 		le32_to_cpu(resp->fw_health_status_reg);
9805 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9806 		le32_to_cpu(resp->fw_heartbeat_reg);
9807 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9808 		le32_to_cpu(resp->fw_reset_cnt_reg);
9809 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9810 		le32_to_cpu(resp->reset_inprogress_reg);
9811 	fw_health->fw_reset_inprog_reg_mask =
9812 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9813 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9814 	if (fw_health->fw_reset_seq_cnt >= 16) {
9815 		rc = -EINVAL;
9816 		goto err_recovery_out;
9817 	}
9818 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9819 		fw_health->fw_reset_seq_regs[i] =
9820 			le32_to_cpu(resp->reset_reg[i]);
9821 		fw_health->fw_reset_seq_vals[i] =
9822 			le32_to_cpu(resp->reset_reg_val[i]);
9823 		fw_health->fw_reset_seq_delay_msec[i] =
9824 			resp->delay_after_reset[i];
9825 	}
9826 err_recovery_out:
9827 	hwrm_req_drop(bp, req);
9828 	if (!rc)
9829 		rc = bnxt_map_fw_health_regs(bp);
9830 	if (rc)
9831 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9832 	return rc;
9833 }
9834 
9835 static int bnxt_hwrm_func_reset(struct bnxt *bp)
9836 {
9837 	struct hwrm_func_reset_input *req;
9838 	int rc;
9839 
9840 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
9841 	if (rc)
9842 		return rc;
9843 
9844 	req->enables = 0;
9845 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
9846 	return hwrm_req_send(bp, req);
9847 }
9848 
9849 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
9850 {
9851 	struct hwrm_nvm_get_dev_info_output nvm_info;
9852 
9853 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
9854 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
9855 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
9856 			 nvm_info.nvm_cfg_ver_upd);
9857 }
9858 
9859 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
9860 {
9861 	struct hwrm_queue_qportcfg_output *resp;
9862 	struct hwrm_queue_qportcfg_input *req;
9863 	u8 i, j, *qptr;
9864 	bool no_rdma;
9865 	int rc = 0;
9866 
9867 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
9868 	if (rc)
9869 		return rc;
9870 
9871 	resp = hwrm_req_hold(bp, req);
9872 	rc = hwrm_req_send(bp, req);
9873 	if (rc)
9874 		goto qportcfg_exit;
9875 
9876 	if (!resp->max_configurable_queues) {
9877 		rc = -EINVAL;
9878 		goto qportcfg_exit;
9879 	}
9880 	bp->max_tc = resp->max_configurable_queues;
9881 	bp->max_lltc = resp->max_configurable_lossless_queues;
9882 	if (bp->max_tc > BNXT_MAX_QUEUE)
9883 		bp->max_tc = BNXT_MAX_QUEUE;
9884 
9885 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
9886 	qptr = &resp->queue_id0;
9887 	for (i = 0, j = 0; i < bp->max_tc; i++) {
9888 		bp->q_info[j].queue_id = *qptr;
9889 		bp->q_ids[i] = *qptr++;
9890 		bp->q_info[j].queue_profile = *qptr++;
9891 		bp->tc_to_qidx[j] = j;
9892 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
9893 		    (no_rdma && BNXT_PF(bp)))
9894 			j++;
9895 	}
9896 	bp->max_q = bp->max_tc;
9897 	bp->max_tc = max_t(u8, j, 1);
9898 
9899 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
9900 		bp->max_tc = 1;
9901 
9902 	if (bp->max_lltc > bp->max_tc)
9903 		bp->max_lltc = bp->max_tc;
9904 
9905 qportcfg_exit:
9906 	hwrm_req_drop(bp, req);
9907 	return rc;
9908 }
9909 
9910 static int bnxt_hwrm_poll(struct bnxt *bp)
9911 {
9912 	struct hwrm_ver_get_input *req;
9913 	int rc;
9914 
9915 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9916 	if (rc)
9917 		return rc;
9918 
9919 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9920 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9921 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9922 
9923 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
9924 	rc = hwrm_req_send(bp, req);
9925 	return rc;
9926 }
9927 
9928 static int bnxt_hwrm_ver_get(struct bnxt *bp)
9929 {
9930 	struct hwrm_ver_get_output *resp;
9931 	struct hwrm_ver_get_input *req;
9932 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
9933 	u32 dev_caps_cfg, hwrm_ver;
9934 	int rc, len;
9935 
9936 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9937 	if (rc)
9938 		return rc;
9939 
9940 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
9941 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
9942 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9943 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9944 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9945 
9946 	resp = hwrm_req_hold(bp, req);
9947 	rc = hwrm_req_send(bp, req);
9948 	if (rc)
9949 		goto hwrm_ver_get_exit;
9950 
9951 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
9952 
9953 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
9954 			     resp->hwrm_intf_min_8b << 8 |
9955 			     resp->hwrm_intf_upd_8b;
9956 	if (resp->hwrm_intf_maj_8b < 1) {
9957 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
9958 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9959 			    resp->hwrm_intf_upd_8b);
9960 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
9961 	}
9962 
9963 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
9964 			HWRM_VERSION_UPDATE;
9965 
9966 	if (bp->hwrm_spec_code > hwrm_ver)
9967 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9968 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
9969 			 HWRM_VERSION_UPDATE);
9970 	else
9971 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9972 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9973 			 resp->hwrm_intf_upd_8b);
9974 
9975 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
9976 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
9977 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
9978 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
9979 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
9980 		len = FW_VER_STR_LEN;
9981 	} else {
9982 		fw_maj = resp->hwrm_fw_maj_8b;
9983 		fw_min = resp->hwrm_fw_min_8b;
9984 		fw_bld = resp->hwrm_fw_bld_8b;
9985 		fw_rsv = resp->hwrm_fw_rsvd_8b;
9986 		len = BC_HWRM_STR_LEN;
9987 	}
9988 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
9989 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
9990 		 fw_rsv);
9991 
9992 	if (strlen(resp->active_pkg_name)) {
9993 		int fw_ver_len = strlen(bp->fw_ver_str);
9994 
9995 		snprintf(bp->fw_ver_str + fw_ver_len,
9996 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
9997 			 resp->active_pkg_name);
9998 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
9999 	}
10000 
10001 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10002 	if (!bp->hwrm_cmd_timeout)
10003 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10004 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10005 	if (!bp->hwrm_cmd_max_timeout)
10006 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10007 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
10008 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
10009 			    bp->hwrm_cmd_max_timeout / 1000);
10010 
10011 	if (resp->hwrm_intf_maj_8b >= 1) {
10012 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10013 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10014 	}
10015 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10016 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10017 
10018 	bp->chip_num = le16_to_cpu(resp->chip_num);
10019 	bp->chip_rev = resp->chip_rev;
10020 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10021 	    !resp->chip_metal)
10022 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10023 
10024 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10025 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10026 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10027 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10028 
10029 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10030 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10031 
10032 	if (dev_caps_cfg &
10033 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10034 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10035 
10036 	if (dev_caps_cfg &
10037 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10038 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10039 
10040 	if (dev_caps_cfg &
10041 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10042 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10043 
10044 hwrm_ver_get_exit:
10045 	hwrm_req_drop(bp, req);
10046 	return rc;
10047 }
10048 
10049 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10050 {
10051 	struct hwrm_fw_set_time_input *req;
10052 	struct tm tm;
10053 	time64_t now = ktime_get_real_seconds();
10054 	int rc;
10055 
10056 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10057 	    bp->hwrm_spec_code < 0x10400)
10058 		return -EOPNOTSUPP;
10059 
10060 	time64_to_tm(now, 0, &tm);
10061 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10062 	if (rc)
10063 		return rc;
10064 
10065 	req->year = cpu_to_le16(1900 + tm.tm_year);
10066 	req->month = 1 + tm.tm_mon;
10067 	req->day = tm.tm_mday;
10068 	req->hour = tm.tm_hour;
10069 	req->minute = tm.tm_min;
10070 	req->second = tm.tm_sec;
10071 	return hwrm_req_send(bp, req);
10072 }
10073 
10074 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10075 {
10076 	u64 sw_tmp;
10077 
10078 	hw &= mask;
10079 	sw_tmp = (*sw & ~mask) | hw;
10080 	if (hw < (*sw & mask))
10081 		sw_tmp += mask + 1;
10082 	WRITE_ONCE(*sw, sw_tmp);
10083 }
10084 
10085 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10086 				    int count, bool ignore_zero)
10087 {
10088 	int i;
10089 
10090 	for (i = 0; i < count; i++) {
10091 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10092 
10093 		if (ignore_zero && !hw)
10094 			continue;
10095 
10096 		if (masks[i] == -1ULL)
10097 			sw_stats[i] = hw;
10098 		else
10099 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10100 	}
10101 }
10102 
10103 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10104 {
10105 	if (!stats->hw_stats)
10106 		return;
10107 
10108 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10109 				stats->hw_masks, stats->len / 8, false);
10110 }
10111 
10112 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10113 {
10114 	struct bnxt_stats_mem *ring0_stats;
10115 	bool ignore_zero = false;
10116 	int i;
10117 
10118 	/* Chip bug.  Counter intermittently becomes 0. */
10119 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10120 		ignore_zero = true;
10121 
10122 	for (i = 0; i < bp->cp_nr_rings; i++) {
10123 		struct bnxt_napi *bnapi = bp->bnapi[i];
10124 		struct bnxt_cp_ring_info *cpr;
10125 		struct bnxt_stats_mem *stats;
10126 
10127 		cpr = &bnapi->cp_ring;
10128 		stats = &cpr->stats;
10129 		if (!i)
10130 			ring0_stats = stats;
10131 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10132 					ring0_stats->hw_masks,
10133 					ring0_stats->len / 8, ignore_zero);
10134 	}
10135 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10136 		struct bnxt_stats_mem *stats = &bp->port_stats;
10137 		__le64 *hw_stats = stats->hw_stats;
10138 		u64 *sw_stats = stats->sw_stats;
10139 		u64 *masks = stats->hw_masks;
10140 		int cnt;
10141 
10142 		cnt = sizeof(struct rx_port_stats) / 8;
10143 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10144 
10145 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10146 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10147 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10148 		cnt = sizeof(struct tx_port_stats) / 8;
10149 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10150 	}
10151 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10152 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10153 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10154 	}
10155 }
10156 
10157 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10158 {
10159 	struct hwrm_port_qstats_input *req;
10160 	struct bnxt_pf_info *pf = &bp->pf;
10161 	int rc;
10162 
10163 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10164 		return 0;
10165 
10166 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10167 		return -EOPNOTSUPP;
10168 
10169 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10170 	if (rc)
10171 		return rc;
10172 
10173 	req->flags = flags;
10174 	req->port_id = cpu_to_le16(pf->port_id);
10175 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10176 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10177 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10178 	return hwrm_req_send(bp, req);
10179 }
10180 
10181 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10182 {
10183 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10184 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10185 	struct hwrm_port_qstats_ext_output *resp_qs;
10186 	struct hwrm_port_qstats_ext_input *req_qs;
10187 	struct bnxt_pf_info *pf = &bp->pf;
10188 	u32 tx_stat_size;
10189 	int rc;
10190 
10191 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10192 		return 0;
10193 
10194 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10195 		return -EOPNOTSUPP;
10196 
10197 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10198 	if (rc)
10199 		return rc;
10200 
10201 	req_qs->flags = flags;
10202 	req_qs->port_id = cpu_to_le16(pf->port_id);
10203 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10204 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10205 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10206 		       sizeof(struct tx_port_stats_ext) : 0;
10207 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10208 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10209 	resp_qs = hwrm_req_hold(bp, req_qs);
10210 	rc = hwrm_req_send(bp, req_qs);
10211 	if (!rc) {
10212 		bp->fw_rx_stats_ext_size =
10213 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10214 		if (BNXT_FW_MAJ(bp) < 220 &&
10215 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10216 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10217 
10218 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10219 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10220 	} else {
10221 		bp->fw_rx_stats_ext_size = 0;
10222 		bp->fw_tx_stats_ext_size = 0;
10223 	}
10224 	hwrm_req_drop(bp, req_qs);
10225 
10226 	if (flags)
10227 		return rc;
10228 
10229 	if (bp->fw_tx_stats_ext_size <=
10230 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10231 		bp->pri2cos_valid = 0;
10232 		return rc;
10233 	}
10234 
10235 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10236 	if (rc)
10237 		return rc;
10238 
10239 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10240 
10241 	resp_qc = hwrm_req_hold(bp, req_qc);
10242 	rc = hwrm_req_send(bp, req_qc);
10243 	if (!rc) {
10244 		u8 *pri2cos;
10245 		int i, j;
10246 
10247 		pri2cos = &resp_qc->pri0_cos_queue_id;
10248 		for (i = 0; i < 8; i++) {
10249 			u8 queue_id = pri2cos[i];
10250 			u8 queue_idx;
10251 
10252 			/* Per port queue IDs start from 0, 10, 20, etc */
10253 			queue_idx = queue_id % 10;
10254 			if (queue_idx > BNXT_MAX_QUEUE) {
10255 				bp->pri2cos_valid = false;
10256 				hwrm_req_drop(bp, req_qc);
10257 				return rc;
10258 			}
10259 			for (j = 0; j < bp->max_q; j++) {
10260 				if (bp->q_ids[j] == queue_id)
10261 					bp->pri2cos_idx[i] = queue_idx;
10262 			}
10263 		}
10264 		bp->pri2cos_valid = true;
10265 	}
10266 	hwrm_req_drop(bp, req_qc);
10267 
10268 	return rc;
10269 }
10270 
10271 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10272 {
10273 	bnxt_hwrm_tunnel_dst_port_free(bp,
10274 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10275 	bnxt_hwrm_tunnel_dst_port_free(bp,
10276 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10277 }
10278 
10279 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10280 {
10281 	int rc, i;
10282 	u32 tpa_flags = 0;
10283 
10284 	if (set_tpa)
10285 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10286 	else if (BNXT_NO_FW_ACCESS(bp))
10287 		return 0;
10288 	for (i = 0; i < bp->nr_vnics; i++) {
10289 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10290 		if (rc) {
10291 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10292 				   i, rc);
10293 			return rc;
10294 		}
10295 	}
10296 	return 0;
10297 }
10298 
10299 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10300 {
10301 	int i;
10302 
10303 	for (i = 0; i < bp->nr_vnics; i++)
10304 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10305 }
10306 
10307 static void bnxt_clear_vnic(struct bnxt *bp)
10308 {
10309 	if (!bp->vnic_info)
10310 		return;
10311 
10312 	bnxt_hwrm_clear_vnic_filter(bp);
10313 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10314 		/* clear all RSS setting before free vnic ctx */
10315 		bnxt_hwrm_clear_vnic_rss(bp);
10316 		bnxt_hwrm_vnic_ctx_free(bp);
10317 	}
10318 	/* before free the vnic, undo the vnic tpa settings */
10319 	if (bp->flags & BNXT_FLAG_TPA)
10320 		bnxt_set_tpa(bp, false);
10321 	bnxt_hwrm_vnic_free(bp);
10322 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10323 		bnxt_hwrm_vnic_ctx_free(bp);
10324 }
10325 
10326 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10327 				    bool irq_re_init)
10328 {
10329 	bnxt_clear_vnic(bp);
10330 	bnxt_hwrm_ring_free(bp, close_path);
10331 	bnxt_hwrm_ring_grp_free(bp);
10332 	if (irq_re_init) {
10333 		bnxt_hwrm_stat_ctx_free(bp);
10334 		bnxt_hwrm_free_tunnel_ports(bp);
10335 	}
10336 }
10337 
10338 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10339 {
10340 	struct hwrm_func_cfg_input *req;
10341 	u8 evb_mode;
10342 	int rc;
10343 
10344 	if (br_mode == BRIDGE_MODE_VEB)
10345 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10346 	else if (br_mode == BRIDGE_MODE_VEPA)
10347 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10348 	else
10349 		return -EINVAL;
10350 
10351 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10352 	if (rc)
10353 		return rc;
10354 
10355 	req->fid = cpu_to_le16(0xffff);
10356 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10357 	req->evb_mode = evb_mode;
10358 	return hwrm_req_send(bp, req);
10359 }
10360 
10361 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10362 {
10363 	struct hwrm_func_cfg_input *req;
10364 	int rc;
10365 
10366 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10367 		return 0;
10368 
10369 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10370 	if (rc)
10371 		return rc;
10372 
10373 	req->fid = cpu_to_le16(0xffff);
10374 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10375 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10376 	if (size == 128)
10377 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10378 
10379 	return hwrm_req_send(bp, req);
10380 }
10381 
10382 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10383 {
10384 	int rc;
10385 
10386 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10387 		goto skip_rss_ctx;
10388 
10389 	/* allocate context for vnic */
10390 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10391 	if (rc) {
10392 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10393 			   vnic->vnic_id, rc);
10394 		goto vnic_setup_err;
10395 	}
10396 	bp->rsscos_nr_ctxs++;
10397 
10398 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10399 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10400 		if (rc) {
10401 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10402 				   vnic->vnic_id, rc);
10403 			goto vnic_setup_err;
10404 		}
10405 		bp->rsscos_nr_ctxs++;
10406 	}
10407 
10408 skip_rss_ctx:
10409 	/* configure default vnic, ring grp */
10410 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10411 	if (rc) {
10412 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10413 			   vnic->vnic_id, rc);
10414 		goto vnic_setup_err;
10415 	}
10416 
10417 	/* Enable RSS hashing on vnic */
10418 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10419 	if (rc) {
10420 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10421 			   vnic->vnic_id, rc);
10422 		goto vnic_setup_err;
10423 	}
10424 
10425 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10426 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10427 		if (rc) {
10428 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10429 				   vnic->vnic_id, rc);
10430 		}
10431 	}
10432 
10433 vnic_setup_err:
10434 	return rc;
10435 }
10436 
10437 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10438 			  u8 valid)
10439 {
10440 	struct hwrm_vnic_update_input *req;
10441 	int rc;
10442 
10443 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10444 	if (rc)
10445 		return rc;
10446 
10447 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10448 
10449 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10450 		req->mru = cpu_to_le16(vnic->mru);
10451 
10452 	req->enables = cpu_to_le32(valid);
10453 
10454 	return hwrm_req_send(bp, req);
10455 }
10456 
10457 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10458 {
10459 	int rc;
10460 
10461 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10462 	if (rc) {
10463 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10464 			   vnic->vnic_id, rc);
10465 		return rc;
10466 	}
10467 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10468 	if (rc)
10469 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10470 			   vnic->vnic_id, rc);
10471 	return rc;
10472 }
10473 
10474 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10475 {
10476 	int rc, i, nr_ctxs;
10477 
10478 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10479 	for (i = 0; i < nr_ctxs; i++) {
10480 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10481 		if (rc) {
10482 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10483 				   vnic->vnic_id, i, rc);
10484 			break;
10485 		}
10486 		bp->rsscos_nr_ctxs++;
10487 	}
10488 	if (i < nr_ctxs)
10489 		return -ENOMEM;
10490 
10491 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10492 	if (rc)
10493 		return rc;
10494 
10495 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10496 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10497 		if (rc) {
10498 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10499 				   vnic->vnic_id, rc);
10500 		}
10501 	}
10502 	return rc;
10503 }
10504 
10505 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10506 {
10507 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10508 		return __bnxt_setup_vnic_p5(bp, vnic);
10509 	else
10510 		return __bnxt_setup_vnic(bp, vnic);
10511 }
10512 
10513 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10514 				     struct bnxt_vnic_info *vnic,
10515 				     u16 start_rx_ring_idx, int rx_rings)
10516 {
10517 	int rc;
10518 
10519 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10520 	if (rc) {
10521 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10522 			   vnic->vnic_id, rc);
10523 		return rc;
10524 	}
10525 	return bnxt_setup_vnic(bp, vnic);
10526 }
10527 
10528 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10529 {
10530 	struct bnxt_vnic_info *vnic;
10531 	int i, rc = 0;
10532 
10533 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10534 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10535 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10536 	}
10537 
10538 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10539 		return 0;
10540 
10541 	for (i = 0; i < bp->rx_nr_rings; i++) {
10542 		u16 vnic_id = i + 1;
10543 		u16 ring_id = i;
10544 
10545 		if (vnic_id >= bp->nr_vnics)
10546 			break;
10547 
10548 		vnic = &bp->vnic_info[vnic_id];
10549 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10550 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10551 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10552 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10553 			break;
10554 	}
10555 	return rc;
10556 }
10557 
10558 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10559 			  bool all)
10560 {
10561 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10562 	struct bnxt_filter_base *usr_fltr, *tmp;
10563 	struct bnxt_ntuple_filter *ntp_fltr;
10564 	int i;
10565 
10566 	if (netif_running(bp->dev)) {
10567 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10568 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10569 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10570 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10571 		}
10572 	}
10573 	if (!all)
10574 		return;
10575 
10576 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10577 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10578 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10579 			ntp_fltr = container_of(usr_fltr,
10580 						struct bnxt_ntuple_filter,
10581 						base);
10582 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10583 			bnxt_del_ntp_filter(bp, ntp_fltr);
10584 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10585 		}
10586 	}
10587 
10588 	if (vnic->rss_table)
10589 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10590 				  vnic->rss_table,
10591 				  vnic->rss_table_dma_addr);
10592 	bp->num_rss_ctx--;
10593 }
10594 
10595 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10596 {
10597 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10598 	struct ethtool_rxfh_context *ctx;
10599 	unsigned long context;
10600 
10601 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10602 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10603 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10604 
10605 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10606 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10607 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10608 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10609 				   rss_ctx->index);
10610 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10611 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10612 		}
10613 	}
10614 }
10615 
10616 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10617 {
10618 	struct ethtool_rxfh_context *ctx;
10619 	unsigned long context;
10620 
10621 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10622 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10623 
10624 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10625 	}
10626 }
10627 
10628 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10629 static bool bnxt_promisc_ok(struct bnxt *bp)
10630 {
10631 #ifdef CONFIG_BNXT_SRIOV
10632 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10633 		return false;
10634 #endif
10635 	return true;
10636 }
10637 
10638 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10639 {
10640 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10641 	unsigned int rc = 0;
10642 
10643 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10644 	if (rc) {
10645 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10646 			   rc);
10647 		return rc;
10648 	}
10649 
10650 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10651 	if (rc) {
10652 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10653 			   rc);
10654 		return rc;
10655 	}
10656 	return rc;
10657 }
10658 
10659 static int bnxt_cfg_rx_mode(struct bnxt *);
10660 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10661 
10662 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10663 {
10664 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10665 	int rc = 0;
10666 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10667 
10668 	if (irq_re_init) {
10669 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10670 		if (rc) {
10671 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10672 				   rc);
10673 			goto err_out;
10674 		}
10675 	}
10676 
10677 	rc = bnxt_hwrm_ring_alloc(bp);
10678 	if (rc) {
10679 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10680 		goto err_out;
10681 	}
10682 
10683 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10684 	if (rc) {
10685 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10686 		goto err_out;
10687 	}
10688 
10689 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10690 		rx_nr_rings--;
10691 
10692 	/* default vnic 0 */
10693 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10694 	if (rc) {
10695 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10696 		goto err_out;
10697 	}
10698 
10699 	if (BNXT_VF(bp))
10700 		bnxt_hwrm_func_qcfg(bp);
10701 
10702 	rc = bnxt_setup_vnic(bp, vnic);
10703 	if (rc)
10704 		goto err_out;
10705 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10706 		bnxt_hwrm_update_rss_hash_cfg(bp);
10707 
10708 	if (bp->flags & BNXT_FLAG_RFS) {
10709 		rc = bnxt_alloc_rfs_vnics(bp);
10710 		if (rc)
10711 			goto err_out;
10712 	}
10713 
10714 	if (bp->flags & BNXT_FLAG_TPA) {
10715 		rc = bnxt_set_tpa(bp, true);
10716 		if (rc)
10717 			goto err_out;
10718 	}
10719 
10720 	if (BNXT_VF(bp))
10721 		bnxt_update_vf_mac(bp);
10722 
10723 	/* Filter for default vnic 0 */
10724 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10725 	if (rc) {
10726 		if (BNXT_VF(bp) && rc == -ENODEV)
10727 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10728 		else
10729 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10730 		goto err_out;
10731 	}
10732 	vnic->uc_filter_count = 1;
10733 
10734 	vnic->rx_mask = 0;
10735 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10736 		goto skip_rx_mask;
10737 
10738 	if (bp->dev->flags & IFF_BROADCAST)
10739 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10740 
10741 	if (bp->dev->flags & IFF_PROMISC)
10742 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10743 
10744 	if (bp->dev->flags & IFF_ALLMULTI) {
10745 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10746 		vnic->mc_list_count = 0;
10747 	} else if (bp->dev->flags & IFF_MULTICAST) {
10748 		u32 mask = 0;
10749 
10750 		bnxt_mc_list_updated(bp, &mask);
10751 		vnic->rx_mask |= mask;
10752 	}
10753 
10754 	rc = bnxt_cfg_rx_mode(bp);
10755 	if (rc)
10756 		goto err_out;
10757 
10758 skip_rx_mask:
10759 	rc = bnxt_hwrm_set_coal(bp);
10760 	if (rc)
10761 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10762 				rc);
10763 
10764 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10765 		rc = bnxt_setup_nitroa0_vnic(bp);
10766 		if (rc)
10767 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10768 				   rc);
10769 	}
10770 
10771 	if (BNXT_VF(bp)) {
10772 		bnxt_hwrm_func_qcfg(bp);
10773 		netdev_update_features(bp->dev);
10774 	}
10775 
10776 	return 0;
10777 
10778 err_out:
10779 	bnxt_hwrm_resource_free(bp, 0, true);
10780 
10781 	return rc;
10782 }
10783 
10784 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10785 {
10786 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10787 	return 0;
10788 }
10789 
10790 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10791 {
10792 	bnxt_init_cp_rings(bp);
10793 	bnxt_init_rx_rings(bp);
10794 	bnxt_init_tx_rings(bp);
10795 	bnxt_init_ring_grps(bp, irq_re_init);
10796 	bnxt_init_vnics(bp);
10797 
10798 	return bnxt_init_chip(bp, irq_re_init);
10799 }
10800 
10801 static int bnxt_set_real_num_queues(struct bnxt *bp)
10802 {
10803 	int rc;
10804 	struct net_device *dev = bp->dev;
10805 
10806 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10807 					  bp->tx_nr_rings_xdp);
10808 	if (rc)
10809 		return rc;
10810 
10811 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10812 	if (rc)
10813 		return rc;
10814 
10815 #ifdef CONFIG_RFS_ACCEL
10816 	if (bp->flags & BNXT_FLAG_RFS)
10817 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
10818 #endif
10819 
10820 	return rc;
10821 }
10822 
10823 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10824 			     bool shared)
10825 {
10826 	int _rx = *rx, _tx = *tx;
10827 
10828 	if (shared) {
10829 		*rx = min_t(int, _rx, max);
10830 		*tx = min_t(int, _tx, max);
10831 	} else {
10832 		if (max < 2)
10833 			return -ENOMEM;
10834 
10835 		while (_rx + _tx > max) {
10836 			if (_rx > _tx && _rx > 1)
10837 				_rx--;
10838 			else if (_tx > 1)
10839 				_tx--;
10840 		}
10841 		*rx = _rx;
10842 		*tx = _tx;
10843 	}
10844 	return 0;
10845 }
10846 
10847 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
10848 {
10849 	return (tx - tx_xdp) / tx_sets + tx_xdp;
10850 }
10851 
10852 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
10853 {
10854 	int tcs = bp->num_tc;
10855 
10856 	if (!tcs)
10857 		tcs = 1;
10858 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
10859 }
10860 
10861 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
10862 {
10863 	int tcs = bp->num_tc;
10864 
10865 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
10866 	       bp->tx_nr_rings_xdp;
10867 }
10868 
10869 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10870 			   bool sh)
10871 {
10872 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
10873 
10874 	if (tx_cp != *tx) {
10875 		int tx_saved = tx_cp, rc;
10876 
10877 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
10878 		if (rc)
10879 			return rc;
10880 		if (tx_cp != tx_saved)
10881 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
10882 		return 0;
10883 	}
10884 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
10885 }
10886 
10887 static void bnxt_setup_msix(struct bnxt *bp)
10888 {
10889 	const int len = sizeof(bp->irq_tbl[0].name);
10890 	struct net_device *dev = bp->dev;
10891 	int tcs, i;
10892 
10893 	tcs = bp->num_tc;
10894 	if (tcs) {
10895 		int i, off, count;
10896 
10897 		for (i = 0; i < tcs; i++) {
10898 			count = bp->tx_nr_rings_per_tc;
10899 			off = BNXT_TC_TO_RING_BASE(bp, i);
10900 			netdev_set_tc_queue(dev, i, count, off);
10901 		}
10902 	}
10903 
10904 	for (i = 0; i < bp->cp_nr_rings; i++) {
10905 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10906 		char *attr;
10907 
10908 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
10909 			attr = "TxRx";
10910 		else if (i < bp->rx_nr_rings)
10911 			attr = "rx";
10912 		else
10913 			attr = "tx";
10914 
10915 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
10916 			 attr, i);
10917 		bp->irq_tbl[map_idx].handler = bnxt_msix;
10918 	}
10919 }
10920 
10921 static int bnxt_init_int_mode(struct bnxt *bp);
10922 
10923 static int bnxt_change_msix(struct bnxt *bp, int total)
10924 {
10925 	struct msi_map map;
10926 	int i;
10927 
10928 	/* add MSIX to the end if needed */
10929 	for (i = bp->total_irqs; i < total; i++) {
10930 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
10931 		if (map.index < 0)
10932 			return bp->total_irqs;
10933 		bp->irq_tbl[i].vector = map.virq;
10934 		bp->total_irqs++;
10935 	}
10936 
10937 	/* trim MSIX from the end if needed */
10938 	for (i = bp->total_irqs; i > total; i--) {
10939 		map.index = i - 1;
10940 		map.virq = bp->irq_tbl[i - 1].vector;
10941 		pci_msix_free_irq(bp->pdev, map);
10942 		bp->total_irqs--;
10943 	}
10944 	return bp->total_irqs;
10945 }
10946 
10947 static int bnxt_setup_int_mode(struct bnxt *bp)
10948 {
10949 	int rc;
10950 
10951 	if (!bp->irq_tbl) {
10952 		rc = bnxt_init_int_mode(bp);
10953 		if (rc || !bp->irq_tbl)
10954 			return rc ?: -ENODEV;
10955 	}
10956 
10957 	bnxt_setup_msix(bp);
10958 
10959 	rc = bnxt_set_real_num_queues(bp);
10960 	return rc;
10961 }
10962 
10963 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
10964 {
10965 	return bp->hw_resc.max_rsscos_ctxs;
10966 }
10967 
10968 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
10969 {
10970 	return bp->hw_resc.max_vnics;
10971 }
10972 
10973 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
10974 {
10975 	return bp->hw_resc.max_stat_ctxs;
10976 }
10977 
10978 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
10979 {
10980 	return bp->hw_resc.max_cp_rings;
10981 }
10982 
10983 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
10984 {
10985 	unsigned int cp = bp->hw_resc.max_cp_rings;
10986 
10987 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
10988 		cp -= bnxt_get_ulp_msix_num(bp);
10989 
10990 	return cp;
10991 }
10992 
10993 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
10994 {
10995 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10996 
10997 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10998 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
10999 
11000 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11001 }
11002 
11003 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11004 {
11005 	bp->hw_resc.max_irqs = max_irqs;
11006 }
11007 
11008 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11009 {
11010 	unsigned int cp;
11011 
11012 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11013 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11014 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11015 	else
11016 		return cp - bp->cp_nr_rings;
11017 }
11018 
11019 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11020 {
11021 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11022 }
11023 
11024 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11025 {
11026 	int max_irq = bnxt_get_max_func_irqs(bp);
11027 	int total_req = bp->cp_nr_rings + num;
11028 
11029 	if (max_irq < total_req) {
11030 		num = max_irq - bp->cp_nr_rings;
11031 		if (num <= 0)
11032 			return 0;
11033 	}
11034 	return num;
11035 }
11036 
11037 static int bnxt_get_num_msix(struct bnxt *bp)
11038 {
11039 	if (!BNXT_NEW_RM(bp))
11040 		return bnxt_get_max_func_irqs(bp);
11041 
11042 	return bnxt_nq_rings_in_use(bp);
11043 }
11044 
11045 static int bnxt_init_int_mode(struct bnxt *bp)
11046 {
11047 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11048 
11049 	total_vecs = bnxt_get_num_msix(bp);
11050 	max = bnxt_get_max_func_irqs(bp);
11051 	if (total_vecs > max)
11052 		total_vecs = max;
11053 
11054 	if (!total_vecs)
11055 		return 0;
11056 
11057 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11058 		min = 2;
11059 
11060 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11061 					   PCI_IRQ_MSIX);
11062 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11063 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11064 		rc = -ENODEV;
11065 		goto msix_setup_exit;
11066 	}
11067 
11068 	tbl_size = total_vecs;
11069 	if (pci_msix_can_alloc_dyn(bp->pdev))
11070 		tbl_size = max;
11071 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
11072 	if (bp->irq_tbl) {
11073 		for (i = 0; i < total_vecs; i++)
11074 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11075 
11076 		bp->total_irqs = total_vecs;
11077 		/* Trim rings based upon num of vectors allocated */
11078 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11079 				     total_vecs - ulp_msix, min == 1);
11080 		if (rc)
11081 			goto msix_setup_exit;
11082 
11083 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11084 		bp->cp_nr_rings = (min == 1) ?
11085 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11086 				  tx_cp + bp->rx_nr_rings;
11087 
11088 	} else {
11089 		rc = -ENOMEM;
11090 		goto msix_setup_exit;
11091 	}
11092 	return 0;
11093 
11094 msix_setup_exit:
11095 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11096 	kfree(bp->irq_tbl);
11097 	bp->irq_tbl = NULL;
11098 	pci_free_irq_vectors(bp->pdev);
11099 	return rc;
11100 }
11101 
11102 static void bnxt_clear_int_mode(struct bnxt *bp)
11103 {
11104 	pci_free_irq_vectors(bp->pdev);
11105 
11106 	kfree(bp->irq_tbl);
11107 	bp->irq_tbl = NULL;
11108 }
11109 
11110 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11111 {
11112 	bool irq_cleared = false;
11113 	bool irq_change = false;
11114 	int tcs = bp->num_tc;
11115 	int irqs_required;
11116 	int rc;
11117 
11118 	if (!bnxt_need_reserve_rings(bp))
11119 		return 0;
11120 
11121 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11122 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11123 
11124 		if (ulp_msix > bp->ulp_num_msix_want)
11125 			ulp_msix = bp->ulp_num_msix_want;
11126 		irqs_required = ulp_msix + bp->cp_nr_rings;
11127 	} else {
11128 		irqs_required = bnxt_get_num_msix(bp);
11129 	}
11130 
11131 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11132 		irq_change = true;
11133 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11134 			bnxt_ulp_irq_stop(bp);
11135 			bnxt_clear_int_mode(bp);
11136 			irq_cleared = true;
11137 		}
11138 	}
11139 	rc = __bnxt_reserve_rings(bp);
11140 	if (irq_cleared) {
11141 		if (!rc)
11142 			rc = bnxt_init_int_mode(bp);
11143 		bnxt_ulp_irq_restart(bp, rc);
11144 	} else if (irq_change && !rc) {
11145 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11146 			rc = -ENOSPC;
11147 	}
11148 	if (rc) {
11149 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11150 		return rc;
11151 	}
11152 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11153 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11154 		netdev_err(bp->dev, "tx ring reservation failure\n");
11155 		netdev_reset_tc(bp->dev);
11156 		bp->num_tc = 0;
11157 		if (bp->tx_nr_rings_xdp)
11158 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11159 		else
11160 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11161 		return -ENOMEM;
11162 	}
11163 	return 0;
11164 }
11165 
11166 static void bnxt_free_irq(struct bnxt *bp)
11167 {
11168 	struct bnxt_irq *irq;
11169 	int i;
11170 
11171 #ifdef CONFIG_RFS_ACCEL
11172 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11173 	bp->dev->rx_cpu_rmap = NULL;
11174 #endif
11175 	if (!bp->irq_tbl || !bp->bnapi)
11176 		return;
11177 
11178 	for (i = 0; i < bp->cp_nr_rings; i++) {
11179 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11180 
11181 		irq = &bp->irq_tbl[map_idx];
11182 		if (irq->requested) {
11183 			if (irq->have_cpumask) {
11184 				irq_update_affinity_hint(irq->vector, NULL);
11185 				free_cpumask_var(irq->cpu_mask);
11186 				irq->have_cpumask = 0;
11187 			}
11188 			free_irq(irq->vector, bp->bnapi[i]);
11189 		}
11190 
11191 		irq->requested = 0;
11192 	}
11193 }
11194 
11195 static int bnxt_request_irq(struct bnxt *bp)
11196 {
11197 	int i, j, rc = 0;
11198 	unsigned long flags = 0;
11199 #ifdef CONFIG_RFS_ACCEL
11200 	struct cpu_rmap *rmap;
11201 #endif
11202 
11203 	rc = bnxt_setup_int_mode(bp);
11204 	if (rc) {
11205 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11206 			   rc);
11207 		return rc;
11208 	}
11209 #ifdef CONFIG_RFS_ACCEL
11210 	rmap = bp->dev->rx_cpu_rmap;
11211 #endif
11212 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11213 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11214 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11215 
11216 #ifdef CONFIG_RFS_ACCEL
11217 		if (rmap && bp->bnapi[i]->rx_ring) {
11218 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11219 			if (rc)
11220 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11221 					    j);
11222 			j++;
11223 		}
11224 #endif
11225 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11226 				 bp->bnapi[i]);
11227 		if (rc)
11228 			break;
11229 
11230 		netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector);
11231 		irq->requested = 1;
11232 
11233 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11234 			int numa_node = dev_to_node(&bp->pdev->dev);
11235 
11236 			irq->have_cpumask = 1;
11237 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11238 					irq->cpu_mask);
11239 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11240 			if (rc) {
11241 				netdev_warn(bp->dev,
11242 					    "Update affinity hint failed, IRQ = %d\n",
11243 					    irq->vector);
11244 				break;
11245 			}
11246 		}
11247 	}
11248 	return rc;
11249 }
11250 
11251 static void bnxt_del_napi(struct bnxt *bp)
11252 {
11253 	int i;
11254 
11255 	if (!bp->bnapi)
11256 		return;
11257 
11258 	for (i = 0; i < bp->rx_nr_rings; i++)
11259 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11260 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11261 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11262 
11263 	for (i = 0; i < bp->cp_nr_rings; i++) {
11264 		struct bnxt_napi *bnapi = bp->bnapi[i];
11265 
11266 		__netif_napi_del(&bnapi->napi);
11267 	}
11268 	/* We called __netif_napi_del(), we need
11269 	 * to respect an RCU grace period before freeing napi structures.
11270 	 */
11271 	synchronize_net();
11272 }
11273 
11274 static void bnxt_init_napi(struct bnxt *bp)
11275 {
11276 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11277 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11278 	struct bnxt_napi *bnapi;
11279 	int i;
11280 
11281 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11282 		poll_fn = bnxt_poll_p5;
11283 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11284 		cp_nr_rings--;
11285 	for (i = 0; i < cp_nr_rings; i++) {
11286 		bnapi = bp->bnapi[i];
11287 		netif_napi_add_config(bp->dev, &bnapi->napi, poll_fn,
11288 				      bnapi->index);
11289 	}
11290 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11291 		bnapi = bp->bnapi[cp_nr_rings];
11292 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11293 	}
11294 }
11295 
11296 static void bnxt_disable_napi(struct bnxt *bp)
11297 {
11298 	int i;
11299 
11300 	if (!bp->bnapi ||
11301 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11302 		return;
11303 
11304 	for (i = 0; i < bp->cp_nr_rings; i++) {
11305 		struct bnxt_napi *bnapi = bp->bnapi[i];
11306 		struct bnxt_cp_ring_info *cpr;
11307 
11308 		cpr = &bnapi->cp_ring;
11309 		if (bnapi->tx_fault)
11310 			cpr->sw_stats->tx.tx_resets++;
11311 		if (bnapi->in_reset)
11312 			cpr->sw_stats->rx.rx_resets++;
11313 		napi_disable(&bnapi->napi);
11314 		if (bnapi->rx_ring)
11315 			cancel_work_sync(&cpr->dim.work);
11316 	}
11317 }
11318 
11319 static void bnxt_enable_napi(struct bnxt *bp)
11320 {
11321 	int i;
11322 
11323 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11324 	for (i = 0; i < bp->cp_nr_rings; i++) {
11325 		struct bnxt_napi *bnapi = bp->bnapi[i];
11326 		struct bnxt_cp_ring_info *cpr;
11327 
11328 		bnapi->tx_fault = 0;
11329 
11330 		cpr = &bnapi->cp_ring;
11331 		bnapi->in_reset = false;
11332 
11333 		if (bnapi->rx_ring) {
11334 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11335 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11336 		}
11337 		napi_enable(&bnapi->napi);
11338 	}
11339 }
11340 
11341 void bnxt_tx_disable(struct bnxt *bp)
11342 {
11343 	int i;
11344 	struct bnxt_tx_ring_info *txr;
11345 
11346 	if (bp->tx_ring) {
11347 		for (i = 0; i < bp->tx_nr_rings; i++) {
11348 			txr = &bp->tx_ring[i];
11349 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11350 		}
11351 	}
11352 	/* Make sure napi polls see @dev_state change */
11353 	synchronize_net();
11354 	/* Drop carrier first to prevent TX timeout */
11355 	netif_carrier_off(bp->dev);
11356 	/* Stop all TX queues */
11357 	netif_tx_disable(bp->dev);
11358 }
11359 
11360 void bnxt_tx_enable(struct bnxt *bp)
11361 {
11362 	int i;
11363 	struct bnxt_tx_ring_info *txr;
11364 
11365 	for (i = 0; i < bp->tx_nr_rings; i++) {
11366 		txr = &bp->tx_ring[i];
11367 		WRITE_ONCE(txr->dev_state, 0);
11368 	}
11369 	/* Make sure napi polls see @dev_state change */
11370 	synchronize_net();
11371 	netif_tx_wake_all_queues(bp->dev);
11372 	if (BNXT_LINK_IS_UP(bp))
11373 		netif_carrier_on(bp->dev);
11374 }
11375 
11376 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11377 {
11378 	u8 active_fec = link_info->active_fec_sig_mode &
11379 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11380 
11381 	switch (active_fec) {
11382 	default:
11383 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11384 		return "None";
11385 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11386 		return "Clause 74 BaseR";
11387 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11388 		return "Clause 91 RS(528,514)";
11389 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11390 		return "Clause 91 RS544_1XN";
11391 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11392 		return "Clause 91 RS(544,514)";
11393 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11394 		return "Clause 91 RS272_1XN";
11395 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11396 		return "Clause 91 RS(272,257)";
11397 	}
11398 }
11399 
11400 void bnxt_report_link(struct bnxt *bp)
11401 {
11402 	if (BNXT_LINK_IS_UP(bp)) {
11403 		const char *signal = "";
11404 		const char *flow_ctrl;
11405 		const char *duplex;
11406 		u32 speed;
11407 		u16 fec;
11408 
11409 		netif_carrier_on(bp->dev);
11410 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11411 		if (speed == SPEED_UNKNOWN) {
11412 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11413 			return;
11414 		}
11415 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11416 			duplex = "full";
11417 		else
11418 			duplex = "half";
11419 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11420 			flow_ctrl = "ON - receive & transmit";
11421 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11422 			flow_ctrl = "ON - transmit";
11423 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11424 			flow_ctrl = "ON - receive";
11425 		else
11426 			flow_ctrl = "none";
11427 		if (bp->link_info.phy_qcfg_resp.option_flags &
11428 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11429 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11430 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11431 			switch (sig_mode) {
11432 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11433 				signal = "(NRZ) ";
11434 				break;
11435 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11436 				signal = "(PAM4 56Gbps) ";
11437 				break;
11438 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11439 				signal = "(PAM4 112Gbps) ";
11440 				break;
11441 			default:
11442 				break;
11443 			}
11444 		}
11445 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11446 			    speed, signal, duplex, flow_ctrl);
11447 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11448 			netdev_info(bp->dev, "EEE is %s\n",
11449 				    bp->eee.eee_active ? "active" :
11450 							 "not active");
11451 		fec = bp->link_info.fec_cfg;
11452 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11453 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11454 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11455 				    bnxt_report_fec(&bp->link_info));
11456 	} else {
11457 		netif_carrier_off(bp->dev);
11458 		netdev_err(bp->dev, "NIC Link is Down\n");
11459 	}
11460 }
11461 
11462 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11463 {
11464 	if (!resp->supported_speeds_auto_mode &&
11465 	    !resp->supported_speeds_force_mode &&
11466 	    !resp->supported_pam4_speeds_auto_mode &&
11467 	    !resp->supported_pam4_speeds_force_mode &&
11468 	    !resp->supported_speeds2_auto_mode &&
11469 	    !resp->supported_speeds2_force_mode)
11470 		return true;
11471 	return false;
11472 }
11473 
11474 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11475 {
11476 	struct bnxt_link_info *link_info = &bp->link_info;
11477 	struct hwrm_port_phy_qcaps_output *resp;
11478 	struct hwrm_port_phy_qcaps_input *req;
11479 	int rc = 0;
11480 
11481 	if (bp->hwrm_spec_code < 0x10201)
11482 		return 0;
11483 
11484 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11485 	if (rc)
11486 		return rc;
11487 
11488 	resp = hwrm_req_hold(bp, req);
11489 	rc = hwrm_req_send(bp, req);
11490 	if (rc)
11491 		goto hwrm_phy_qcaps_exit;
11492 
11493 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11494 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11495 		struct ethtool_keee *eee = &bp->eee;
11496 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11497 
11498 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11499 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11500 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11501 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11502 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11503 	}
11504 
11505 	if (bp->hwrm_spec_code >= 0x10a01) {
11506 		if (bnxt_phy_qcaps_no_speed(resp)) {
11507 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11508 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11509 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11510 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11511 			netdev_info(bp->dev, "Ethernet link enabled\n");
11512 			/* Phy re-enabled, reprobe the speeds */
11513 			link_info->support_auto_speeds = 0;
11514 			link_info->support_pam4_auto_speeds = 0;
11515 			link_info->support_auto_speeds2 = 0;
11516 		}
11517 	}
11518 	if (resp->supported_speeds_auto_mode)
11519 		link_info->support_auto_speeds =
11520 			le16_to_cpu(resp->supported_speeds_auto_mode);
11521 	if (resp->supported_pam4_speeds_auto_mode)
11522 		link_info->support_pam4_auto_speeds =
11523 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11524 	if (resp->supported_speeds2_auto_mode)
11525 		link_info->support_auto_speeds2 =
11526 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11527 
11528 	bp->port_count = resp->port_cnt;
11529 
11530 hwrm_phy_qcaps_exit:
11531 	hwrm_req_drop(bp, req);
11532 	return rc;
11533 }
11534 
11535 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11536 {
11537 	u16 diff = advertising ^ supported;
11538 
11539 	return ((supported | diff) != supported);
11540 }
11541 
11542 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11543 {
11544 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11545 
11546 	/* Check if any advertised speeds are no longer supported. The caller
11547 	 * holds the link_lock mutex, so we can modify link_info settings.
11548 	 */
11549 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11550 		if (bnxt_support_dropped(link_info->advertising,
11551 					 link_info->support_auto_speeds2)) {
11552 			link_info->advertising = link_info->support_auto_speeds2;
11553 			return true;
11554 		}
11555 		return false;
11556 	}
11557 	if (bnxt_support_dropped(link_info->advertising,
11558 				 link_info->support_auto_speeds)) {
11559 		link_info->advertising = link_info->support_auto_speeds;
11560 		return true;
11561 	}
11562 	if (bnxt_support_dropped(link_info->advertising_pam4,
11563 				 link_info->support_pam4_auto_speeds)) {
11564 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
11565 		return true;
11566 	}
11567 	return false;
11568 }
11569 
11570 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
11571 {
11572 	struct bnxt_link_info *link_info = &bp->link_info;
11573 	struct hwrm_port_phy_qcfg_output *resp;
11574 	struct hwrm_port_phy_qcfg_input *req;
11575 	u8 link_state = link_info->link_state;
11576 	bool support_changed;
11577 	int rc;
11578 
11579 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
11580 	if (rc)
11581 		return rc;
11582 
11583 	resp = hwrm_req_hold(bp, req);
11584 	rc = hwrm_req_send(bp, req);
11585 	if (rc) {
11586 		hwrm_req_drop(bp, req);
11587 		if (BNXT_VF(bp) && rc == -ENODEV) {
11588 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
11589 			rc = 0;
11590 		}
11591 		return rc;
11592 	}
11593 
11594 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
11595 	link_info->phy_link_status = resp->link;
11596 	link_info->duplex = resp->duplex_cfg;
11597 	if (bp->hwrm_spec_code >= 0x10800)
11598 		link_info->duplex = resp->duplex_state;
11599 	link_info->pause = resp->pause;
11600 	link_info->auto_mode = resp->auto_mode;
11601 	link_info->auto_pause_setting = resp->auto_pause;
11602 	link_info->lp_pause = resp->link_partner_adv_pause;
11603 	link_info->force_pause_setting = resp->force_pause;
11604 	link_info->duplex_setting = resp->duplex_cfg;
11605 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
11606 		link_info->link_speed = le16_to_cpu(resp->link_speed);
11607 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
11608 			link_info->active_lanes = resp->active_lanes;
11609 	} else {
11610 		link_info->link_speed = 0;
11611 		link_info->active_lanes = 0;
11612 	}
11613 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
11614 	link_info->force_pam4_link_speed =
11615 		le16_to_cpu(resp->force_pam4_link_speed);
11616 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
11617 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
11618 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
11619 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
11620 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
11621 	link_info->auto_pam4_link_speeds =
11622 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
11623 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
11624 	link_info->lp_auto_link_speeds =
11625 		le16_to_cpu(resp->link_partner_adv_speeds);
11626 	link_info->lp_auto_pam4_link_speeds =
11627 		resp->link_partner_pam4_adv_speeds;
11628 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
11629 	link_info->phy_ver[0] = resp->phy_maj;
11630 	link_info->phy_ver[1] = resp->phy_min;
11631 	link_info->phy_ver[2] = resp->phy_bld;
11632 	link_info->media_type = resp->media_type;
11633 	link_info->phy_type = resp->phy_type;
11634 	link_info->transceiver = resp->xcvr_pkg_type;
11635 	link_info->phy_addr = resp->eee_config_phy_addr &
11636 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
11637 	link_info->module_status = resp->module_status;
11638 
11639 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
11640 		struct ethtool_keee *eee = &bp->eee;
11641 		u16 fw_speeds;
11642 
11643 		eee->eee_active = 0;
11644 		if (resp->eee_config_phy_addr &
11645 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
11646 			eee->eee_active = 1;
11647 			fw_speeds = le16_to_cpu(
11648 				resp->link_partner_adv_eee_link_speed_mask);
11649 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
11650 		}
11651 
11652 		/* Pull initial EEE config */
11653 		if (!chng_link_state) {
11654 			if (resp->eee_config_phy_addr &
11655 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
11656 				eee->eee_enabled = 1;
11657 
11658 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
11659 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
11660 
11661 			if (resp->eee_config_phy_addr &
11662 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
11663 				__le32 tmr;
11664 
11665 				eee->tx_lpi_enabled = 1;
11666 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
11667 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
11668 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
11669 			}
11670 		}
11671 	}
11672 
11673 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
11674 	if (bp->hwrm_spec_code >= 0x10504) {
11675 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
11676 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
11677 	}
11678 	/* TODO: need to add more logic to report VF link */
11679 	if (chng_link_state) {
11680 		if (link_info->phy_link_status == BNXT_LINK_LINK)
11681 			link_info->link_state = BNXT_LINK_STATE_UP;
11682 		else
11683 			link_info->link_state = BNXT_LINK_STATE_DOWN;
11684 		if (link_state != link_info->link_state)
11685 			bnxt_report_link(bp);
11686 	} else {
11687 		/* always link down if not require to update link state */
11688 		link_info->link_state = BNXT_LINK_STATE_DOWN;
11689 	}
11690 	hwrm_req_drop(bp, req);
11691 
11692 	if (!BNXT_PHY_CFG_ABLE(bp))
11693 		return 0;
11694 
11695 	support_changed = bnxt_support_speed_dropped(link_info);
11696 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
11697 		bnxt_hwrm_set_link_setting(bp, true, false);
11698 	return 0;
11699 }
11700 
11701 static void bnxt_get_port_module_status(struct bnxt *bp)
11702 {
11703 	struct bnxt_link_info *link_info = &bp->link_info;
11704 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
11705 	u8 module_status;
11706 
11707 	if (bnxt_update_link(bp, true))
11708 		return;
11709 
11710 	module_status = link_info->module_status;
11711 	switch (module_status) {
11712 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
11713 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
11714 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
11715 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
11716 			    bp->pf.port_id);
11717 		if (bp->hwrm_spec_code >= 0x10201) {
11718 			netdev_warn(bp->dev, "Module part number %s\n",
11719 				    resp->phy_vendor_partnumber);
11720 		}
11721 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
11722 			netdev_warn(bp->dev, "TX is disabled\n");
11723 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
11724 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
11725 	}
11726 }
11727 
11728 static void
11729 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11730 {
11731 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
11732 		if (bp->hwrm_spec_code >= 0x10201)
11733 			req->auto_pause =
11734 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
11735 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11736 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
11737 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11738 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
11739 		req->enables |=
11740 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11741 	} else {
11742 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11743 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
11744 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11745 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
11746 		req->enables |=
11747 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
11748 		if (bp->hwrm_spec_code >= 0x10201) {
11749 			req->auto_pause = req->force_pause;
11750 			req->enables |= cpu_to_le32(
11751 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11752 		}
11753 	}
11754 }
11755 
11756 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11757 {
11758 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
11759 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
11760 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11761 			req->enables |=
11762 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
11763 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
11764 		} else if (bp->link_info.advertising) {
11765 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
11766 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
11767 		}
11768 		if (bp->link_info.advertising_pam4) {
11769 			req->enables |=
11770 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
11771 			req->auto_link_pam4_speed_mask =
11772 				cpu_to_le16(bp->link_info.advertising_pam4);
11773 		}
11774 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
11775 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
11776 	} else {
11777 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
11778 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11779 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
11780 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
11781 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
11782 				   (u32)bp->link_info.req_link_speed);
11783 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
11784 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11785 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
11786 		} else {
11787 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11788 		}
11789 	}
11790 
11791 	/* tell chimp that the setting takes effect immediately */
11792 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
11793 }
11794 
11795 int bnxt_hwrm_set_pause(struct bnxt *bp)
11796 {
11797 	struct hwrm_port_phy_cfg_input *req;
11798 	int rc;
11799 
11800 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11801 	if (rc)
11802 		return rc;
11803 
11804 	bnxt_hwrm_set_pause_common(bp, req);
11805 
11806 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
11807 	    bp->link_info.force_link_chng)
11808 		bnxt_hwrm_set_link_common(bp, req);
11809 
11810 	rc = hwrm_req_send(bp, req);
11811 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
11812 		/* since changing of pause setting doesn't trigger any link
11813 		 * change event, the driver needs to update the current pause
11814 		 * result upon successfully return of the phy_cfg command
11815 		 */
11816 		bp->link_info.pause =
11817 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
11818 		bp->link_info.auto_pause_setting = 0;
11819 		if (!bp->link_info.force_link_chng)
11820 			bnxt_report_link(bp);
11821 	}
11822 	bp->link_info.force_link_chng = false;
11823 	return rc;
11824 }
11825 
11826 static void bnxt_hwrm_set_eee(struct bnxt *bp,
11827 			      struct hwrm_port_phy_cfg_input *req)
11828 {
11829 	struct ethtool_keee *eee = &bp->eee;
11830 
11831 	if (eee->eee_enabled) {
11832 		u16 eee_speeds;
11833 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
11834 
11835 		if (eee->tx_lpi_enabled)
11836 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
11837 		else
11838 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
11839 
11840 		req->flags |= cpu_to_le32(flags);
11841 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
11842 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
11843 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
11844 	} else {
11845 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
11846 	}
11847 }
11848 
11849 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
11850 {
11851 	struct hwrm_port_phy_cfg_input *req;
11852 	int rc;
11853 
11854 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11855 	if (rc)
11856 		return rc;
11857 
11858 	if (set_pause)
11859 		bnxt_hwrm_set_pause_common(bp, req);
11860 
11861 	bnxt_hwrm_set_link_common(bp, req);
11862 
11863 	if (set_eee)
11864 		bnxt_hwrm_set_eee(bp, req);
11865 	return hwrm_req_send(bp, req);
11866 }
11867 
11868 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
11869 {
11870 	struct hwrm_port_phy_cfg_input *req;
11871 	int rc;
11872 
11873 	if (!BNXT_SINGLE_PF(bp))
11874 		return 0;
11875 
11876 	if (pci_num_vf(bp->pdev) &&
11877 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
11878 		return 0;
11879 
11880 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11881 	if (rc)
11882 		return rc;
11883 
11884 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
11885 	rc = hwrm_req_send(bp, req);
11886 	if (!rc) {
11887 		mutex_lock(&bp->link_lock);
11888 		/* Device is not obliged link down in certain scenarios, even
11889 		 * when forced. Setting the state unknown is consistent with
11890 		 * driver startup and will force link state to be reported
11891 		 * during subsequent open based on PORT_PHY_QCFG.
11892 		 */
11893 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
11894 		mutex_unlock(&bp->link_lock);
11895 	}
11896 	return rc;
11897 }
11898 
11899 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
11900 {
11901 #ifdef CONFIG_TEE_BNXT_FW
11902 	int rc = tee_bnxt_fw_load();
11903 
11904 	if (rc)
11905 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
11906 
11907 	return rc;
11908 #else
11909 	netdev_err(bp->dev, "OP-TEE not supported\n");
11910 	return -ENODEV;
11911 #endif
11912 }
11913 
11914 static int bnxt_try_recover_fw(struct bnxt *bp)
11915 {
11916 	if (bp->fw_health && bp->fw_health->status_reliable) {
11917 		int retry = 0, rc;
11918 		u32 sts;
11919 
11920 		do {
11921 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11922 			rc = bnxt_hwrm_poll(bp);
11923 			if (!BNXT_FW_IS_BOOTING(sts) &&
11924 			    !BNXT_FW_IS_RECOVERING(sts))
11925 				break;
11926 			retry++;
11927 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
11928 
11929 		if (!BNXT_FW_IS_HEALTHY(sts)) {
11930 			netdev_err(bp->dev,
11931 				   "Firmware not responding, status: 0x%x\n",
11932 				   sts);
11933 			rc = -ENODEV;
11934 		}
11935 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
11936 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
11937 			return bnxt_fw_reset_via_optee(bp);
11938 		}
11939 		return rc;
11940 	}
11941 
11942 	return -ENODEV;
11943 }
11944 
11945 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
11946 {
11947 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11948 
11949 	if (!BNXT_NEW_RM(bp))
11950 		return; /* no resource reservations required */
11951 
11952 	hw_resc->resv_cp_rings = 0;
11953 	hw_resc->resv_stat_ctxs = 0;
11954 	hw_resc->resv_irqs = 0;
11955 	hw_resc->resv_tx_rings = 0;
11956 	hw_resc->resv_rx_rings = 0;
11957 	hw_resc->resv_hw_ring_grps = 0;
11958 	hw_resc->resv_vnics = 0;
11959 	hw_resc->resv_rsscos_ctxs = 0;
11960 	if (!fw_reset) {
11961 		bp->tx_nr_rings = 0;
11962 		bp->rx_nr_rings = 0;
11963 	}
11964 }
11965 
11966 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
11967 {
11968 	int rc;
11969 
11970 	if (!BNXT_NEW_RM(bp))
11971 		return 0; /* no resource reservations required */
11972 
11973 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
11974 	if (rc)
11975 		netdev_err(bp->dev, "resc_qcaps failed\n");
11976 
11977 	bnxt_clear_reservations(bp, fw_reset);
11978 
11979 	return rc;
11980 }
11981 
11982 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
11983 {
11984 	struct hwrm_func_drv_if_change_output *resp;
11985 	struct hwrm_func_drv_if_change_input *req;
11986 	bool fw_reset = !bp->irq_tbl;
11987 	bool resc_reinit = false;
11988 	int rc, retry = 0;
11989 	u32 flags = 0;
11990 
11991 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
11992 		return 0;
11993 
11994 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
11995 	if (rc)
11996 		return rc;
11997 
11998 	if (up)
11999 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12000 	resp = hwrm_req_hold(bp, req);
12001 
12002 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12003 	while (retry < BNXT_FW_IF_RETRY) {
12004 		rc = hwrm_req_send(bp, req);
12005 		if (rc != -EAGAIN)
12006 			break;
12007 
12008 		msleep(50);
12009 		retry++;
12010 	}
12011 
12012 	if (rc == -EAGAIN) {
12013 		hwrm_req_drop(bp, req);
12014 		return rc;
12015 	} else if (!rc) {
12016 		flags = le32_to_cpu(resp->flags);
12017 	} else if (up) {
12018 		rc = bnxt_try_recover_fw(bp);
12019 		fw_reset = true;
12020 	}
12021 	hwrm_req_drop(bp, req);
12022 	if (rc)
12023 		return rc;
12024 
12025 	if (!up) {
12026 		bnxt_inv_fw_health_reg(bp);
12027 		return 0;
12028 	}
12029 
12030 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12031 		resc_reinit = true;
12032 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12033 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12034 		fw_reset = true;
12035 	else
12036 		bnxt_remap_fw_health_regs(bp);
12037 
12038 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12039 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12040 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12041 		return -ENODEV;
12042 	}
12043 	if (resc_reinit || fw_reset) {
12044 		if (fw_reset) {
12045 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12046 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12047 				bnxt_ulp_irq_stop(bp);
12048 			bnxt_free_ctx_mem(bp, false);
12049 			bnxt_dcb_free(bp);
12050 			rc = bnxt_fw_init_one(bp);
12051 			if (rc) {
12052 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12053 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12054 				return rc;
12055 			}
12056 			bnxt_clear_int_mode(bp);
12057 			rc = bnxt_init_int_mode(bp);
12058 			if (rc) {
12059 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12060 				netdev_err(bp->dev, "init int mode failed\n");
12061 				return rc;
12062 			}
12063 		}
12064 		rc = bnxt_cancel_reservations(bp, fw_reset);
12065 	}
12066 	return rc;
12067 }
12068 
12069 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12070 {
12071 	struct hwrm_port_led_qcaps_output *resp;
12072 	struct hwrm_port_led_qcaps_input *req;
12073 	struct bnxt_pf_info *pf = &bp->pf;
12074 	int rc;
12075 
12076 	bp->num_leds = 0;
12077 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12078 		return 0;
12079 
12080 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12081 	if (rc)
12082 		return rc;
12083 
12084 	req->port_id = cpu_to_le16(pf->port_id);
12085 	resp = hwrm_req_hold(bp, req);
12086 	rc = hwrm_req_send(bp, req);
12087 	if (rc) {
12088 		hwrm_req_drop(bp, req);
12089 		return rc;
12090 	}
12091 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12092 		int i;
12093 
12094 		bp->num_leds = resp->num_leds;
12095 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12096 						 bp->num_leds);
12097 		for (i = 0; i < bp->num_leds; i++) {
12098 			struct bnxt_led_info *led = &bp->leds[i];
12099 			__le16 caps = led->led_state_caps;
12100 
12101 			if (!led->led_group_id ||
12102 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12103 				bp->num_leds = 0;
12104 				break;
12105 			}
12106 		}
12107 	}
12108 	hwrm_req_drop(bp, req);
12109 	return 0;
12110 }
12111 
12112 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12113 {
12114 	struct hwrm_wol_filter_alloc_output *resp;
12115 	struct hwrm_wol_filter_alloc_input *req;
12116 	int rc;
12117 
12118 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12119 	if (rc)
12120 		return rc;
12121 
12122 	req->port_id = cpu_to_le16(bp->pf.port_id);
12123 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12124 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12125 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12126 
12127 	resp = hwrm_req_hold(bp, req);
12128 	rc = hwrm_req_send(bp, req);
12129 	if (!rc)
12130 		bp->wol_filter_id = resp->wol_filter_id;
12131 	hwrm_req_drop(bp, req);
12132 	return rc;
12133 }
12134 
12135 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12136 {
12137 	struct hwrm_wol_filter_free_input *req;
12138 	int rc;
12139 
12140 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12141 	if (rc)
12142 		return rc;
12143 
12144 	req->port_id = cpu_to_le16(bp->pf.port_id);
12145 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12146 	req->wol_filter_id = bp->wol_filter_id;
12147 
12148 	return hwrm_req_send(bp, req);
12149 }
12150 
12151 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12152 {
12153 	struct hwrm_wol_filter_qcfg_output *resp;
12154 	struct hwrm_wol_filter_qcfg_input *req;
12155 	u16 next_handle = 0;
12156 	int rc;
12157 
12158 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12159 	if (rc)
12160 		return rc;
12161 
12162 	req->port_id = cpu_to_le16(bp->pf.port_id);
12163 	req->handle = cpu_to_le16(handle);
12164 	resp = hwrm_req_hold(bp, req);
12165 	rc = hwrm_req_send(bp, req);
12166 	if (!rc) {
12167 		next_handle = le16_to_cpu(resp->next_handle);
12168 		if (next_handle != 0) {
12169 			if (resp->wol_type ==
12170 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12171 				bp->wol = 1;
12172 				bp->wol_filter_id = resp->wol_filter_id;
12173 			}
12174 		}
12175 	}
12176 	hwrm_req_drop(bp, req);
12177 	return next_handle;
12178 }
12179 
12180 static void bnxt_get_wol_settings(struct bnxt *bp)
12181 {
12182 	u16 handle = 0;
12183 
12184 	bp->wol = 0;
12185 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12186 		return;
12187 
12188 	do {
12189 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12190 	} while (handle && handle != 0xffff);
12191 }
12192 
12193 static bool bnxt_eee_config_ok(struct bnxt *bp)
12194 {
12195 	struct ethtool_keee *eee = &bp->eee;
12196 	struct bnxt_link_info *link_info = &bp->link_info;
12197 
12198 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12199 		return true;
12200 
12201 	if (eee->eee_enabled) {
12202 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12203 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12204 
12205 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12206 
12207 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12208 			eee->eee_enabled = 0;
12209 			return false;
12210 		}
12211 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12212 			linkmode_and(eee->advertised, advertising,
12213 				     eee->supported);
12214 			return false;
12215 		}
12216 	}
12217 	return true;
12218 }
12219 
12220 static int bnxt_update_phy_setting(struct bnxt *bp)
12221 {
12222 	int rc;
12223 	bool update_link = false;
12224 	bool update_pause = false;
12225 	bool update_eee = false;
12226 	struct bnxt_link_info *link_info = &bp->link_info;
12227 
12228 	rc = bnxt_update_link(bp, true);
12229 	if (rc) {
12230 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12231 			   rc);
12232 		return rc;
12233 	}
12234 	if (!BNXT_SINGLE_PF(bp))
12235 		return 0;
12236 
12237 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12238 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12239 	    link_info->req_flow_ctrl)
12240 		update_pause = true;
12241 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12242 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12243 		update_pause = true;
12244 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12245 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12246 			update_link = true;
12247 		if (bnxt_force_speed_updated(link_info))
12248 			update_link = true;
12249 		if (link_info->req_duplex != link_info->duplex_setting)
12250 			update_link = true;
12251 	} else {
12252 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12253 			update_link = true;
12254 		if (bnxt_auto_speed_updated(link_info))
12255 			update_link = true;
12256 	}
12257 
12258 	/* The last close may have shutdown the link, so need to call
12259 	 * PHY_CFG to bring it back up.
12260 	 */
12261 	if (!BNXT_LINK_IS_UP(bp))
12262 		update_link = true;
12263 
12264 	if (!bnxt_eee_config_ok(bp))
12265 		update_eee = true;
12266 
12267 	if (update_link)
12268 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12269 	else if (update_pause)
12270 		rc = bnxt_hwrm_set_pause(bp);
12271 	if (rc) {
12272 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12273 			   rc);
12274 		return rc;
12275 	}
12276 
12277 	return rc;
12278 }
12279 
12280 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12281 
12282 static int bnxt_reinit_after_abort(struct bnxt *bp)
12283 {
12284 	int rc;
12285 
12286 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12287 		return -EBUSY;
12288 
12289 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12290 		return -ENODEV;
12291 
12292 	rc = bnxt_fw_init_one(bp);
12293 	if (!rc) {
12294 		bnxt_clear_int_mode(bp);
12295 		rc = bnxt_init_int_mode(bp);
12296 		if (!rc) {
12297 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12298 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12299 		}
12300 	}
12301 	return rc;
12302 }
12303 
12304 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12305 {
12306 	struct bnxt_ntuple_filter *ntp_fltr;
12307 	struct bnxt_l2_filter *l2_fltr;
12308 
12309 	if (list_empty(&fltr->list))
12310 		return;
12311 
12312 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12313 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12314 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12315 		atomic_inc(&l2_fltr->refcnt);
12316 		ntp_fltr->l2_fltr = l2_fltr;
12317 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12318 			bnxt_del_ntp_filter(bp, ntp_fltr);
12319 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12320 				   fltr->sw_id);
12321 		}
12322 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12323 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12324 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12325 			bnxt_del_l2_filter(bp, l2_fltr);
12326 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12327 				   fltr->sw_id);
12328 		}
12329 	}
12330 }
12331 
12332 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12333 {
12334 	struct bnxt_filter_base *usr_fltr, *tmp;
12335 
12336 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12337 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12338 }
12339 
12340 static int bnxt_set_xps_mapping(struct bnxt *bp)
12341 {
12342 	int numa_node = dev_to_node(&bp->pdev->dev);
12343 	unsigned int q_idx, map_idx, cpu, i;
12344 	const struct cpumask *cpu_mask_ptr;
12345 	int nr_cpus = num_online_cpus();
12346 	cpumask_t *q_map;
12347 	int rc = 0;
12348 
12349 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12350 	if (!q_map)
12351 		return -ENOMEM;
12352 
12353 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12354 	 * Each TC has the same number of TX queues. The nth TX queue for each
12355 	 * TC will have the same CPU mask.
12356 	 */
12357 	for (i = 0; i < nr_cpus; i++) {
12358 		map_idx = i % bp->tx_nr_rings_per_tc;
12359 		cpu = cpumask_local_spread(i, numa_node);
12360 		cpu_mask_ptr = get_cpu_mask(cpu);
12361 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12362 	}
12363 
12364 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12365 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12366 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12367 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12368 		if (rc) {
12369 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12370 				    q_idx);
12371 			break;
12372 		}
12373 	}
12374 
12375 	kfree(q_map);
12376 
12377 	return rc;
12378 }
12379 
12380 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12381 {
12382 	int rc = 0;
12383 
12384 	netif_carrier_off(bp->dev);
12385 	if (irq_re_init) {
12386 		/* Reserve rings now if none were reserved at driver probe. */
12387 		rc = bnxt_init_dflt_ring_mode(bp);
12388 		if (rc) {
12389 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12390 			return rc;
12391 		}
12392 	}
12393 	rc = bnxt_reserve_rings(bp, irq_re_init);
12394 	if (rc)
12395 		return rc;
12396 
12397 	rc = bnxt_alloc_mem(bp, irq_re_init);
12398 	if (rc) {
12399 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12400 		goto open_err_free_mem;
12401 	}
12402 
12403 	if (irq_re_init) {
12404 		bnxt_init_napi(bp);
12405 		rc = bnxt_request_irq(bp);
12406 		if (rc) {
12407 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12408 			goto open_err_irq;
12409 		}
12410 	}
12411 
12412 	rc = bnxt_init_nic(bp, irq_re_init);
12413 	if (rc) {
12414 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12415 		goto open_err_irq;
12416 	}
12417 
12418 	bnxt_enable_napi(bp);
12419 	bnxt_debug_dev_init(bp);
12420 
12421 	if (link_re_init) {
12422 		mutex_lock(&bp->link_lock);
12423 		rc = bnxt_update_phy_setting(bp);
12424 		mutex_unlock(&bp->link_lock);
12425 		if (rc) {
12426 			netdev_warn(bp->dev, "failed to update phy settings\n");
12427 			if (BNXT_SINGLE_PF(bp)) {
12428 				bp->link_info.phy_retry = true;
12429 				bp->link_info.phy_retry_expires =
12430 					jiffies + 5 * HZ;
12431 			}
12432 		}
12433 	}
12434 
12435 	if (irq_re_init) {
12436 		udp_tunnel_nic_reset_ntf(bp->dev);
12437 		rc = bnxt_set_xps_mapping(bp);
12438 		if (rc)
12439 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12440 	}
12441 
12442 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12443 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12444 			static_branch_enable(&bnxt_xdp_locking_key);
12445 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12446 		static_branch_disable(&bnxt_xdp_locking_key);
12447 	}
12448 	set_bit(BNXT_STATE_OPEN, &bp->state);
12449 	bnxt_enable_int(bp);
12450 	/* Enable TX queues */
12451 	bnxt_tx_enable(bp);
12452 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12453 	/* Poll link status and check for SFP+ module status */
12454 	mutex_lock(&bp->link_lock);
12455 	bnxt_get_port_module_status(bp);
12456 	mutex_unlock(&bp->link_lock);
12457 
12458 	/* VF-reps may need to be re-opened after the PF is re-opened */
12459 	if (BNXT_PF(bp))
12460 		bnxt_vf_reps_open(bp);
12461 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
12462 		WRITE_ONCE(bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS);
12463 	bnxt_ptp_init_rtc(bp, true);
12464 	bnxt_ptp_cfg_tstamp_filters(bp);
12465 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12466 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12467 	bnxt_cfg_usr_fltrs(bp);
12468 	return 0;
12469 
12470 open_err_irq:
12471 	bnxt_del_napi(bp);
12472 
12473 open_err_free_mem:
12474 	bnxt_free_skbs(bp);
12475 	bnxt_free_irq(bp);
12476 	bnxt_free_mem(bp, true);
12477 	return rc;
12478 }
12479 
12480 /* rtnl_lock held */
12481 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12482 {
12483 	int rc = 0;
12484 
12485 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12486 		rc = -EIO;
12487 	if (!rc)
12488 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12489 	if (rc) {
12490 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12491 		dev_close(bp->dev);
12492 	}
12493 	return rc;
12494 }
12495 
12496 /* rtnl_lock held, open the NIC half way by allocating all resources, but
12497  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
12498  * self tests.
12499  */
12500 int bnxt_half_open_nic(struct bnxt *bp)
12501 {
12502 	int rc = 0;
12503 
12504 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12505 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12506 		rc = -ENODEV;
12507 		goto half_open_err;
12508 	}
12509 
12510 	rc = bnxt_alloc_mem(bp, true);
12511 	if (rc) {
12512 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12513 		goto half_open_err;
12514 	}
12515 	bnxt_init_napi(bp);
12516 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12517 	rc = bnxt_init_nic(bp, true);
12518 	if (rc) {
12519 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12520 		bnxt_del_napi(bp);
12521 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12522 		goto half_open_err;
12523 	}
12524 	return 0;
12525 
12526 half_open_err:
12527 	bnxt_free_skbs(bp);
12528 	bnxt_free_mem(bp, true);
12529 	dev_close(bp->dev);
12530 	return rc;
12531 }
12532 
12533 /* rtnl_lock held, this call can only be made after a previous successful
12534  * call to bnxt_half_open_nic().
12535  */
12536 void bnxt_half_close_nic(struct bnxt *bp)
12537 {
12538 	bnxt_hwrm_resource_free(bp, false, true);
12539 	bnxt_del_napi(bp);
12540 	bnxt_free_skbs(bp);
12541 	bnxt_free_mem(bp, true);
12542 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12543 }
12544 
12545 void bnxt_reenable_sriov(struct bnxt *bp)
12546 {
12547 	if (BNXT_PF(bp)) {
12548 		struct bnxt_pf_info *pf = &bp->pf;
12549 		int n = pf->active_vfs;
12550 
12551 		if (n)
12552 			bnxt_cfg_hw_sriov(bp, &n, true);
12553 	}
12554 }
12555 
12556 static int bnxt_open(struct net_device *dev)
12557 {
12558 	struct bnxt *bp = netdev_priv(dev);
12559 	int rc;
12560 
12561 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12562 		rc = bnxt_reinit_after_abort(bp);
12563 		if (rc) {
12564 			if (rc == -EBUSY)
12565 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
12566 			else
12567 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
12568 			return -ENODEV;
12569 		}
12570 	}
12571 
12572 	rc = bnxt_hwrm_if_change(bp, true);
12573 	if (rc)
12574 		return rc;
12575 
12576 	rc = __bnxt_open_nic(bp, true, true);
12577 	if (rc) {
12578 		bnxt_hwrm_if_change(bp, false);
12579 	} else {
12580 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
12581 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12582 				bnxt_queue_sp_work(bp,
12583 						   BNXT_RESTART_ULP_SP_EVENT);
12584 		}
12585 	}
12586 
12587 	return rc;
12588 }
12589 
12590 static bool bnxt_drv_busy(struct bnxt *bp)
12591 {
12592 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
12593 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
12594 }
12595 
12596 static void bnxt_get_ring_stats(struct bnxt *bp,
12597 				struct rtnl_link_stats64 *stats);
12598 
12599 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
12600 			     bool link_re_init)
12601 {
12602 	/* Close the VF-reps before closing PF */
12603 	if (BNXT_PF(bp))
12604 		bnxt_vf_reps_close(bp);
12605 
12606 	/* Change device state to avoid TX queue wake up's */
12607 	bnxt_tx_disable(bp);
12608 
12609 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12610 	smp_mb__after_atomic();
12611 	while (bnxt_drv_busy(bp))
12612 		msleep(20);
12613 
12614 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12615 		bnxt_clear_rss_ctxs(bp);
12616 	/* Flush rings and disable interrupts */
12617 	bnxt_shutdown_nic(bp, irq_re_init);
12618 
12619 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
12620 
12621 	bnxt_debug_dev_exit(bp);
12622 	bnxt_disable_napi(bp);
12623 	del_timer_sync(&bp->timer);
12624 	bnxt_free_skbs(bp);
12625 
12626 	/* Save ring stats before shutdown */
12627 	if (bp->bnapi && irq_re_init) {
12628 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
12629 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
12630 	}
12631 	if (irq_re_init) {
12632 		bnxt_free_irq(bp);
12633 		bnxt_del_napi(bp);
12634 	}
12635 	bnxt_free_mem(bp, irq_re_init);
12636 }
12637 
12638 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12639 {
12640 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12641 		/* If we get here, it means firmware reset is in progress
12642 		 * while we are trying to close.  We can safely proceed with
12643 		 * the close because we are holding rtnl_lock().  Some firmware
12644 		 * messages may fail as we proceed to close.  We set the
12645 		 * ABORT_ERR flag here so that the FW reset thread will later
12646 		 * abort when it gets the rtnl_lock() and sees the flag.
12647 		 */
12648 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
12649 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12650 	}
12651 
12652 #ifdef CONFIG_BNXT_SRIOV
12653 	if (bp->sriov_cfg) {
12654 		int rc;
12655 
12656 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
12657 						      !bp->sriov_cfg,
12658 						      BNXT_SRIOV_CFG_WAIT_TMO);
12659 		if (!rc)
12660 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
12661 		else if (rc < 0)
12662 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
12663 	}
12664 #endif
12665 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
12666 }
12667 
12668 static int bnxt_close(struct net_device *dev)
12669 {
12670 	struct bnxt *bp = netdev_priv(dev);
12671 
12672 	bnxt_close_nic(bp, true, true);
12673 	bnxt_hwrm_shutdown_link(bp);
12674 	bnxt_hwrm_if_change(bp, false);
12675 	return 0;
12676 }
12677 
12678 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
12679 				   u16 *val)
12680 {
12681 	struct hwrm_port_phy_mdio_read_output *resp;
12682 	struct hwrm_port_phy_mdio_read_input *req;
12683 	int rc;
12684 
12685 	if (bp->hwrm_spec_code < 0x10a00)
12686 		return -EOPNOTSUPP;
12687 
12688 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
12689 	if (rc)
12690 		return rc;
12691 
12692 	req->port_id = cpu_to_le16(bp->pf.port_id);
12693 	req->phy_addr = phy_addr;
12694 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12695 	if (mdio_phy_id_is_c45(phy_addr)) {
12696 		req->cl45_mdio = 1;
12697 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12698 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12699 		req->reg_addr = cpu_to_le16(reg);
12700 	}
12701 
12702 	resp = hwrm_req_hold(bp, req);
12703 	rc = hwrm_req_send(bp, req);
12704 	if (!rc)
12705 		*val = le16_to_cpu(resp->reg_data);
12706 	hwrm_req_drop(bp, req);
12707 	return rc;
12708 }
12709 
12710 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
12711 				    u16 val)
12712 {
12713 	struct hwrm_port_phy_mdio_write_input *req;
12714 	int rc;
12715 
12716 	if (bp->hwrm_spec_code < 0x10a00)
12717 		return -EOPNOTSUPP;
12718 
12719 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
12720 	if (rc)
12721 		return rc;
12722 
12723 	req->port_id = cpu_to_le16(bp->pf.port_id);
12724 	req->phy_addr = phy_addr;
12725 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12726 	if (mdio_phy_id_is_c45(phy_addr)) {
12727 		req->cl45_mdio = 1;
12728 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12729 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12730 		req->reg_addr = cpu_to_le16(reg);
12731 	}
12732 	req->reg_data = cpu_to_le16(val);
12733 
12734 	return hwrm_req_send(bp, req);
12735 }
12736 
12737 /* rtnl_lock held */
12738 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12739 {
12740 	struct mii_ioctl_data *mdio = if_mii(ifr);
12741 	struct bnxt *bp = netdev_priv(dev);
12742 	int rc;
12743 
12744 	switch (cmd) {
12745 	case SIOCGMIIPHY:
12746 		mdio->phy_id = bp->link_info.phy_addr;
12747 
12748 		fallthrough;
12749 	case SIOCGMIIREG: {
12750 		u16 mii_regval = 0;
12751 
12752 		if (!netif_running(dev))
12753 			return -EAGAIN;
12754 
12755 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
12756 					     &mii_regval);
12757 		mdio->val_out = mii_regval;
12758 		return rc;
12759 	}
12760 
12761 	case SIOCSMIIREG:
12762 		if (!netif_running(dev))
12763 			return -EAGAIN;
12764 
12765 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
12766 						mdio->val_in);
12767 
12768 	case SIOCSHWTSTAMP:
12769 		return bnxt_hwtstamp_set(dev, ifr);
12770 
12771 	case SIOCGHWTSTAMP:
12772 		return bnxt_hwtstamp_get(dev, ifr);
12773 
12774 	default:
12775 		/* do nothing */
12776 		break;
12777 	}
12778 	return -EOPNOTSUPP;
12779 }
12780 
12781 static void bnxt_get_ring_stats(struct bnxt *bp,
12782 				struct rtnl_link_stats64 *stats)
12783 {
12784 	int i;
12785 
12786 	for (i = 0; i < bp->cp_nr_rings; i++) {
12787 		struct bnxt_napi *bnapi = bp->bnapi[i];
12788 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
12789 		u64 *sw = cpr->stats.sw_stats;
12790 
12791 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
12792 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12793 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
12794 
12795 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
12796 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
12797 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
12798 
12799 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
12800 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
12801 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
12802 
12803 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
12804 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
12805 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
12806 
12807 		stats->rx_missed_errors +=
12808 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
12809 
12810 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12811 
12812 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
12813 
12814 		stats->rx_dropped +=
12815 			cpr->sw_stats->rx.rx_netpoll_discards +
12816 			cpr->sw_stats->rx.rx_oom_discards;
12817 	}
12818 }
12819 
12820 static void bnxt_add_prev_stats(struct bnxt *bp,
12821 				struct rtnl_link_stats64 *stats)
12822 {
12823 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
12824 
12825 	stats->rx_packets += prev_stats->rx_packets;
12826 	stats->tx_packets += prev_stats->tx_packets;
12827 	stats->rx_bytes += prev_stats->rx_bytes;
12828 	stats->tx_bytes += prev_stats->tx_bytes;
12829 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
12830 	stats->multicast += prev_stats->multicast;
12831 	stats->rx_dropped += prev_stats->rx_dropped;
12832 	stats->tx_dropped += prev_stats->tx_dropped;
12833 }
12834 
12835 static void
12836 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
12837 {
12838 	struct bnxt *bp = netdev_priv(dev);
12839 
12840 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
12841 	/* Make sure bnxt_close_nic() sees that we are reading stats before
12842 	 * we check the BNXT_STATE_OPEN flag.
12843 	 */
12844 	smp_mb__after_atomic();
12845 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12846 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12847 		*stats = bp->net_stats_prev;
12848 		return;
12849 	}
12850 
12851 	bnxt_get_ring_stats(bp, stats);
12852 	bnxt_add_prev_stats(bp, stats);
12853 
12854 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
12855 		u64 *rx = bp->port_stats.sw_stats;
12856 		u64 *tx = bp->port_stats.sw_stats +
12857 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
12858 
12859 		stats->rx_crc_errors =
12860 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
12861 		stats->rx_frame_errors =
12862 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
12863 		stats->rx_length_errors =
12864 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
12865 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
12866 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
12867 		stats->rx_errors =
12868 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
12869 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
12870 		stats->collisions =
12871 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
12872 		stats->tx_fifo_errors =
12873 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
12874 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
12875 	}
12876 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12877 }
12878 
12879 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
12880 					struct bnxt_total_ring_err_stats *stats,
12881 					struct bnxt_cp_ring_info *cpr)
12882 {
12883 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
12884 	u64 *hw_stats = cpr->stats.sw_stats;
12885 
12886 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
12887 	stats->rx_total_resets += sw_stats->rx.rx_resets;
12888 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
12889 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
12890 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
12891 	stats->rx_total_ring_discards +=
12892 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
12893 	stats->tx_total_resets += sw_stats->tx.tx_resets;
12894 	stats->tx_total_ring_discards +=
12895 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
12896 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
12897 }
12898 
12899 void bnxt_get_ring_err_stats(struct bnxt *bp,
12900 			     struct bnxt_total_ring_err_stats *stats)
12901 {
12902 	int i;
12903 
12904 	for (i = 0; i < bp->cp_nr_rings; i++)
12905 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
12906 }
12907 
12908 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
12909 {
12910 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12911 	struct net_device *dev = bp->dev;
12912 	struct netdev_hw_addr *ha;
12913 	u8 *haddr;
12914 	int mc_count = 0;
12915 	bool update = false;
12916 	int off = 0;
12917 
12918 	netdev_for_each_mc_addr(ha, dev) {
12919 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
12920 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12921 			vnic->mc_list_count = 0;
12922 			return false;
12923 		}
12924 		haddr = ha->addr;
12925 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
12926 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
12927 			update = true;
12928 		}
12929 		off += ETH_ALEN;
12930 		mc_count++;
12931 	}
12932 	if (mc_count)
12933 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12934 
12935 	if (mc_count != vnic->mc_list_count) {
12936 		vnic->mc_list_count = mc_count;
12937 		update = true;
12938 	}
12939 	return update;
12940 }
12941 
12942 static bool bnxt_uc_list_updated(struct bnxt *bp)
12943 {
12944 	struct net_device *dev = bp->dev;
12945 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12946 	struct netdev_hw_addr *ha;
12947 	int off = 0;
12948 
12949 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
12950 		return true;
12951 
12952 	netdev_for_each_uc_addr(ha, dev) {
12953 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
12954 			return true;
12955 
12956 		off += ETH_ALEN;
12957 	}
12958 	return false;
12959 }
12960 
12961 static void bnxt_set_rx_mode(struct net_device *dev)
12962 {
12963 	struct bnxt *bp = netdev_priv(dev);
12964 	struct bnxt_vnic_info *vnic;
12965 	bool mc_update = false;
12966 	bool uc_update;
12967 	u32 mask;
12968 
12969 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
12970 		return;
12971 
12972 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12973 	mask = vnic->rx_mask;
12974 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
12975 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
12976 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
12977 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
12978 
12979 	if (dev->flags & IFF_PROMISC)
12980 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12981 
12982 	uc_update = bnxt_uc_list_updated(bp);
12983 
12984 	if (dev->flags & IFF_BROADCAST)
12985 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
12986 	if (dev->flags & IFF_ALLMULTI) {
12987 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12988 		vnic->mc_list_count = 0;
12989 	} else if (dev->flags & IFF_MULTICAST) {
12990 		mc_update = bnxt_mc_list_updated(bp, &mask);
12991 	}
12992 
12993 	if (mask != vnic->rx_mask || uc_update || mc_update) {
12994 		vnic->rx_mask = mask;
12995 
12996 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
12997 	}
12998 }
12999 
13000 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13001 {
13002 	struct net_device *dev = bp->dev;
13003 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13004 	struct netdev_hw_addr *ha;
13005 	int i, off = 0, rc;
13006 	bool uc_update;
13007 
13008 	netif_addr_lock_bh(dev);
13009 	uc_update = bnxt_uc_list_updated(bp);
13010 	netif_addr_unlock_bh(dev);
13011 
13012 	if (!uc_update)
13013 		goto skip_uc;
13014 
13015 	for (i = 1; i < vnic->uc_filter_count; i++) {
13016 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13017 
13018 		bnxt_hwrm_l2_filter_free(bp, fltr);
13019 		bnxt_del_l2_filter(bp, fltr);
13020 	}
13021 
13022 	vnic->uc_filter_count = 1;
13023 
13024 	netif_addr_lock_bh(dev);
13025 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13026 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13027 	} else {
13028 		netdev_for_each_uc_addr(ha, dev) {
13029 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13030 			off += ETH_ALEN;
13031 			vnic->uc_filter_count++;
13032 		}
13033 	}
13034 	netif_addr_unlock_bh(dev);
13035 
13036 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13037 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13038 		if (rc) {
13039 			if (BNXT_VF(bp) && rc == -ENODEV) {
13040 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13041 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13042 				else
13043 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13044 				rc = 0;
13045 			} else {
13046 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13047 			}
13048 			vnic->uc_filter_count = i;
13049 			return rc;
13050 		}
13051 	}
13052 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13053 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13054 
13055 skip_uc:
13056 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13057 	    !bnxt_promisc_ok(bp))
13058 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13059 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13060 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13061 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13062 			    rc);
13063 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13064 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13065 		vnic->mc_list_count = 0;
13066 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13067 	}
13068 	if (rc)
13069 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13070 			   rc);
13071 
13072 	return rc;
13073 }
13074 
13075 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13076 {
13077 #ifdef CONFIG_BNXT_SRIOV
13078 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13079 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13080 
13081 		/* No minimum rings were provisioned by the PF.  Don't
13082 		 * reserve rings by default when device is down.
13083 		 */
13084 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13085 			return true;
13086 
13087 		if (!netif_running(bp->dev))
13088 			return false;
13089 	}
13090 #endif
13091 	return true;
13092 }
13093 
13094 /* If the chip and firmware supports RFS */
13095 static bool bnxt_rfs_supported(struct bnxt *bp)
13096 {
13097 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13098 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13099 			return true;
13100 		return false;
13101 	}
13102 	/* 212 firmware is broken for aRFS */
13103 	if (BNXT_FW_MAJ(bp) == 212)
13104 		return false;
13105 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13106 		return true;
13107 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13108 		return true;
13109 	return false;
13110 }
13111 
13112 /* If runtime conditions support RFS */
13113 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13114 {
13115 	struct bnxt_hw_rings hwr = {0};
13116 	int max_vnics, max_rss_ctxs;
13117 
13118 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13119 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13120 		return bnxt_rfs_supported(bp);
13121 
13122 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13123 		return false;
13124 
13125 	hwr.grp = bp->rx_nr_rings;
13126 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13127 	if (new_rss_ctx)
13128 		hwr.vnic++;
13129 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13130 	max_vnics = bnxt_get_max_func_vnics(bp);
13131 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13132 
13133 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13134 		if (bp->rx_nr_rings > 1)
13135 			netdev_warn(bp->dev,
13136 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13137 				    min(max_rss_ctxs - 1, max_vnics - 1));
13138 		return false;
13139 	}
13140 
13141 	if (!BNXT_NEW_RM(bp))
13142 		return true;
13143 
13144 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13145 	 * issue that will mess up the default VNIC if we reduce the
13146 	 * reservations.
13147 	 */
13148 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13149 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13150 		return true;
13151 
13152 	bnxt_hwrm_reserve_rings(bp, &hwr);
13153 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13154 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13155 		return true;
13156 
13157 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13158 	hwr.vnic = 1;
13159 	hwr.rss_ctx = 0;
13160 	bnxt_hwrm_reserve_rings(bp, &hwr);
13161 	return false;
13162 }
13163 
13164 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13165 					   netdev_features_t features)
13166 {
13167 	struct bnxt *bp = netdev_priv(dev);
13168 	netdev_features_t vlan_features;
13169 
13170 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13171 		features &= ~NETIF_F_NTUPLE;
13172 
13173 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13174 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13175 
13176 	if (!(features & NETIF_F_GRO))
13177 		features &= ~NETIF_F_GRO_HW;
13178 
13179 	if (features & NETIF_F_GRO_HW)
13180 		features &= ~NETIF_F_LRO;
13181 
13182 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13183 	 * turned on or off together.
13184 	 */
13185 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13186 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13187 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13188 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13189 		else if (vlan_features)
13190 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13191 	}
13192 #ifdef CONFIG_BNXT_SRIOV
13193 	if (BNXT_VF(bp) && bp->vf.vlan)
13194 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13195 #endif
13196 	return features;
13197 }
13198 
13199 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13200 				bool link_re_init, u32 flags, bool update_tpa)
13201 {
13202 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13203 	bp->flags = flags;
13204 	if (update_tpa)
13205 		bnxt_set_ring_params(bp);
13206 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13207 }
13208 
13209 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13210 {
13211 	bool update_tpa = false, update_ntuple = false;
13212 	struct bnxt *bp = netdev_priv(dev);
13213 	u32 flags = bp->flags;
13214 	u32 changes;
13215 	int rc = 0;
13216 	bool re_init = false;
13217 
13218 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13219 	if (features & NETIF_F_GRO_HW)
13220 		flags |= BNXT_FLAG_GRO;
13221 	else if (features & NETIF_F_LRO)
13222 		flags |= BNXT_FLAG_LRO;
13223 
13224 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13225 		flags &= ~BNXT_FLAG_TPA;
13226 
13227 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13228 		flags |= BNXT_FLAG_STRIP_VLAN;
13229 
13230 	if (features & NETIF_F_NTUPLE)
13231 		flags |= BNXT_FLAG_RFS;
13232 	else
13233 		bnxt_clear_usr_fltrs(bp, true);
13234 
13235 	changes = flags ^ bp->flags;
13236 	if (changes & BNXT_FLAG_TPA) {
13237 		update_tpa = true;
13238 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13239 		    (flags & BNXT_FLAG_TPA) == 0 ||
13240 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13241 			re_init = true;
13242 	}
13243 
13244 	if (changes & ~BNXT_FLAG_TPA)
13245 		re_init = true;
13246 
13247 	if (changes & BNXT_FLAG_RFS)
13248 		update_ntuple = true;
13249 
13250 	if (flags != bp->flags) {
13251 		u32 old_flags = bp->flags;
13252 
13253 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13254 			bp->flags = flags;
13255 			if (update_tpa)
13256 				bnxt_set_ring_params(bp);
13257 			return rc;
13258 		}
13259 
13260 		if (update_ntuple)
13261 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13262 
13263 		if (re_init)
13264 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13265 
13266 		if (update_tpa) {
13267 			bp->flags = flags;
13268 			rc = bnxt_set_tpa(bp,
13269 					  (flags & BNXT_FLAG_TPA) ?
13270 					  true : false);
13271 			if (rc)
13272 				bp->flags = old_flags;
13273 		}
13274 	}
13275 	return rc;
13276 }
13277 
13278 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13279 			      u8 **nextp)
13280 {
13281 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13282 	struct hop_jumbo_hdr *jhdr;
13283 	int hdr_count = 0;
13284 	u8 *nexthdr;
13285 	int start;
13286 
13287 	/* Check that there are at most 2 IPv6 extension headers, no
13288 	 * fragment header, and each is <= 64 bytes.
13289 	 */
13290 	start = nw_off + sizeof(*ip6h);
13291 	nexthdr = &ip6h->nexthdr;
13292 	while (ipv6_ext_hdr(*nexthdr)) {
13293 		struct ipv6_opt_hdr *hp;
13294 		int hdrlen;
13295 
13296 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13297 		    *nexthdr == NEXTHDR_FRAGMENT)
13298 			return false;
13299 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13300 					  skb_headlen(skb), NULL);
13301 		if (!hp)
13302 			return false;
13303 		if (*nexthdr == NEXTHDR_AUTH)
13304 			hdrlen = ipv6_authlen(hp);
13305 		else
13306 			hdrlen = ipv6_optlen(hp);
13307 
13308 		if (hdrlen > 64)
13309 			return false;
13310 
13311 		/* The ext header may be a hop-by-hop header inserted for
13312 		 * big TCP purposes. This will be removed before sending
13313 		 * from NIC, so do not count it.
13314 		 */
13315 		if (*nexthdr == NEXTHDR_HOP) {
13316 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13317 				goto increment_hdr;
13318 
13319 			jhdr = (struct hop_jumbo_hdr *)hp;
13320 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13321 			    jhdr->nexthdr != IPPROTO_TCP)
13322 				goto increment_hdr;
13323 
13324 			goto next_hdr;
13325 		}
13326 increment_hdr:
13327 		hdr_count++;
13328 next_hdr:
13329 		nexthdr = &hp->nexthdr;
13330 		start += hdrlen;
13331 	}
13332 	if (nextp) {
13333 		/* Caller will check inner protocol */
13334 		if (skb->encapsulation) {
13335 			*nextp = nexthdr;
13336 			return true;
13337 		}
13338 		*nextp = NULL;
13339 	}
13340 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13341 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13342 }
13343 
13344 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13345 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13346 {
13347 	struct udphdr *uh = udp_hdr(skb);
13348 	__be16 udp_port = uh->dest;
13349 
13350 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13351 	    udp_port != bp->vxlan_gpe_port)
13352 		return false;
13353 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13354 		struct ethhdr *eh = inner_eth_hdr(skb);
13355 
13356 		switch (eh->h_proto) {
13357 		case htons(ETH_P_IP):
13358 			return true;
13359 		case htons(ETH_P_IPV6):
13360 			return bnxt_exthdr_check(bp, skb,
13361 						 skb_inner_network_offset(skb),
13362 						 NULL);
13363 		}
13364 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13365 		return true;
13366 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13367 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13368 					 NULL);
13369 	}
13370 	return false;
13371 }
13372 
13373 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13374 {
13375 	switch (l4_proto) {
13376 	case IPPROTO_UDP:
13377 		return bnxt_udp_tunl_check(bp, skb);
13378 	case IPPROTO_IPIP:
13379 		return true;
13380 	case IPPROTO_GRE: {
13381 		switch (skb->inner_protocol) {
13382 		default:
13383 			return false;
13384 		case htons(ETH_P_IP):
13385 			return true;
13386 		case htons(ETH_P_IPV6):
13387 			fallthrough;
13388 		}
13389 	}
13390 	case IPPROTO_IPV6:
13391 		/* Check ext headers of inner ipv6 */
13392 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13393 					 NULL);
13394 	}
13395 	return false;
13396 }
13397 
13398 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13399 					     struct net_device *dev,
13400 					     netdev_features_t features)
13401 {
13402 	struct bnxt *bp = netdev_priv(dev);
13403 	u8 *l4_proto;
13404 
13405 	features = vlan_features_check(skb, features);
13406 	switch (vlan_get_protocol(skb)) {
13407 	case htons(ETH_P_IP):
13408 		if (!skb->encapsulation)
13409 			return features;
13410 		l4_proto = &ip_hdr(skb)->protocol;
13411 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13412 			return features;
13413 		break;
13414 	case htons(ETH_P_IPV6):
13415 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13416 				       &l4_proto))
13417 			break;
13418 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13419 			return features;
13420 		break;
13421 	}
13422 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13423 }
13424 
13425 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13426 			 u32 *reg_buf)
13427 {
13428 	struct hwrm_dbg_read_direct_output *resp;
13429 	struct hwrm_dbg_read_direct_input *req;
13430 	__le32 *dbg_reg_buf;
13431 	dma_addr_t mapping;
13432 	int rc, i;
13433 
13434 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13435 	if (rc)
13436 		return rc;
13437 
13438 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13439 					 &mapping);
13440 	if (!dbg_reg_buf) {
13441 		rc = -ENOMEM;
13442 		goto dbg_rd_reg_exit;
13443 	}
13444 
13445 	req->host_dest_addr = cpu_to_le64(mapping);
13446 
13447 	resp = hwrm_req_hold(bp, req);
13448 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13449 	req->read_len32 = cpu_to_le32(num_words);
13450 
13451 	rc = hwrm_req_send(bp, req);
13452 	if (rc || resp->error_code) {
13453 		rc = -EIO;
13454 		goto dbg_rd_reg_exit;
13455 	}
13456 	for (i = 0; i < num_words; i++)
13457 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13458 
13459 dbg_rd_reg_exit:
13460 	hwrm_req_drop(bp, req);
13461 	return rc;
13462 }
13463 
13464 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13465 				       u32 ring_id, u32 *prod, u32 *cons)
13466 {
13467 	struct hwrm_dbg_ring_info_get_output *resp;
13468 	struct hwrm_dbg_ring_info_get_input *req;
13469 	int rc;
13470 
13471 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13472 	if (rc)
13473 		return rc;
13474 
13475 	req->ring_type = ring_type;
13476 	req->fw_ring_id = cpu_to_le32(ring_id);
13477 	resp = hwrm_req_hold(bp, req);
13478 	rc = hwrm_req_send(bp, req);
13479 	if (!rc) {
13480 		*prod = le32_to_cpu(resp->producer_index);
13481 		*cons = le32_to_cpu(resp->consumer_index);
13482 	}
13483 	hwrm_req_drop(bp, req);
13484 	return rc;
13485 }
13486 
13487 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13488 {
13489 	struct bnxt_tx_ring_info *txr;
13490 	int i = bnapi->index, j;
13491 
13492 	bnxt_for_each_napi_tx(j, bnapi, txr)
13493 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13494 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13495 			    txr->tx_cons);
13496 }
13497 
13498 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13499 {
13500 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13501 	int i = bnapi->index;
13502 
13503 	if (!rxr)
13504 		return;
13505 
13506 	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",
13507 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13508 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13509 		    rxr->rx_sw_agg_prod);
13510 }
13511 
13512 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13513 {
13514 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13515 	int i = bnapi->index;
13516 
13517 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13518 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13519 }
13520 
13521 static void bnxt_dbg_dump_states(struct bnxt *bp)
13522 {
13523 	int i;
13524 	struct bnxt_napi *bnapi;
13525 
13526 	for (i = 0; i < bp->cp_nr_rings; i++) {
13527 		bnapi = bp->bnapi[i];
13528 		if (netif_msg_drv(bp)) {
13529 			bnxt_dump_tx_sw_state(bnapi);
13530 			bnxt_dump_rx_sw_state(bnapi);
13531 			bnxt_dump_cp_sw_state(bnapi);
13532 		}
13533 	}
13534 }
13535 
13536 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13537 {
13538 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13539 	struct hwrm_ring_reset_input *req;
13540 	struct bnxt_napi *bnapi = rxr->bnapi;
13541 	struct bnxt_cp_ring_info *cpr;
13542 	u16 cp_ring_id;
13543 	int rc;
13544 
13545 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13546 	if (rc)
13547 		return rc;
13548 
13549 	cpr = &bnapi->cp_ring;
13550 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
13551 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
13552 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
13553 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
13554 	return hwrm_req_send_silent(bp, req);
13555 }
13556 
13557 static void bnxt_reset_task(struct bnxt *bp, bool silent)
13558 {
13559 	if (!silent)
13560 		bnxt_dbg_dump_states(bp);
13561 	if (netif_running(bp->dev)) {
13562 		bnxt_close_nic(bp, !silent, false);
13563 		bnxt_open_nic(bp, !silent, false);
13564 	}
13565 }
13566 
13567 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
13568 {
13569 	struct bnxt *bp = netdev_priv(dev);
13570 
13571 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
13572 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
13573 }
13574 
13575 static void bnxt_fw_health_check(struct bnxt *bp)
13576 {
13577 	struct bnxt_fw_health *fw_health = bp->fw_health;
13578 	struct pci_dev *pdev = bp->pdev;
13579 	u32 val;
13580 
13581 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13582 		return;
13583 
13584 	/* Make sure it is enabled before checking the tmr_counter. */
13585 	smp_rmb();
13586 	if (fw_health->tmr_counter) {
13587 		fw_health->tmr_counter--;
13588 		return;
13589 	}
13590 
13591 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13592 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
13593 		fw_health->arrests++;
13594 		goto fw_reset;
13595 	}
13596 
13597 	fw_health->last_fw_heartbeat = val;
13598 
13599 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13600 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
13601 		fw_health->discoveries++;
13602 		goto fw_reset;
13603 	}
13604 
13605 	fw_health->tmr_counter = fw_health->tmr_multiplier;
13606 	return;
13607 
13608 fw_reset:
13609 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
13610 }
13611 
13612 static void bnxt_timer(struct timer_list *t)
13613 {
13614 	struct bnxt *bp = from_timer(bp, t, timer);
13615 	struct net_device *dev = bp->dev;
13616 
13617 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
13618 		return;
13619 
13620 	if (atomic_read(&bp->intr_sem) != 0)
13621 		goto bnxt_restart_timer;
13622 
13623 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
13624 		bnxt_fw_health_check(bp);
13625 
13626 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
13627 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
13628 
13629 	if (bnxt_tc_flower_enabled(bp))
13630 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
13631 
13632 #ifdef CONFIG_RFS_ACCEL
13633 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
13634 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
13635 #endif /*CONFIG_RFS_ACCEL*/
13636 
13637 	if (bp->link_info.phy_retry) {
13638 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
13639 			bp->link_info.phy_retry = false;
13640 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
13641 		} else {
13642 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
13643 		}
13644 	}
13645 
13646 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13647 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13648 
13649 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
13650 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
13651 
13652 bnxt_restart_timer:
13653 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13654 }
13655 
13656 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
13657 {
13658 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
13659 	 * set.  If the device is being closed, bnxt_close() may be holding
13660 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
13661 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
13662 	 */
13663 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13664 	rtnl_lock();
13665 }
13666 
13667 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
13668 {
13669 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13670 	rtnl_unlock();
13671 }
13672 
13673 /* Only called from bnxt_sp_task() */
13674 static void bnxt_reset(struct bnxt *bp, bool silent)
13675 {
13676 	bnxt_rtnl_lock_sp(bp);
13677 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
13678 		bnxt_reset_task(bp, silent);
13679 	bnxt_rtnl_unlock_sp(bp);
13680 }
13681 
13682 /* Only called from bnxt_sp_task() */
13683 static void bnxt_rx_ring_reset(struct bnxt *bp)
13684 {
13685 	int i;
13686 
13687 	bnxt_rtnl_lock_sp(bp);
13688 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13689 		bnxt_rtnl_unlock_sp(bp);
13690 		return;
13691 	}
13692 	/* Disable and flush TPA before resetting the RX ring */
13693 	if (bp->flags & BNXT_FLAG_TPA)
13694 		bnxt_set_tpa(bp, false);
13695 	for (i = 0; i < bp->rx_nr_rings; i++) {
13696 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
13697 		struct bnxt_cp_ring_info *cpr;
13698 		int rc;
13699 
13700 		if (!rxr->bnapi->in_reset)
13701 			continue;
13702 
13703 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
13704 		if (rc) {
13705 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
13706 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
13707 			else
13708 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
13709 					    rc);
13710 			bnxt_reset_task(bp, true);
13711 			break;
13712 		}
13713 		bnxt_free_one_rx_ring_skbs(bp, rxr);
13714 		rxr->rx_prod = 0;
13715 		rxr->rx_agg_prod = 0;
13716 		rxr->rx_sw_agg_prod = 0;
13717 		rxr->rx_next_cons = 0;
13718 		rxr->bnapi->in_reset = false;
13719 		bnxt_alloc_one_rx_ring(bp, i);
13720 		cpr = &rxr->bnapi->cp_ring;
13721 		cpr->sw_stats->rx.rx_resets++;
13722 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
13723 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
13724 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
13725 	}
13726 	if (bp->flags & BNXT_FLAG_TPA)
13727 		bnxt_set_tpa(bp, true);
13728 	bnxt_rtnl_unlock_sp(bp);
13729 }
13730 
13731 static void bnxt_fw_fatal_close(struct bnxt *bp)
13732 {
13733 	bnxt_tx_disable(bp);
13734 	bnxt_disable_napi(bp);
13735 	bnxt_disable_int_sync(bp);
13736 	bnxt_free_irq(bp);
13737 	bnxt_clear_int_mode(bp);
13738 	pci_disable_device(bp->pdev);
13739 }
13740 
13741 static void bnxt_fw_reset_close(struct bnxt *bp)
13742 {
13743 	/* When firmware is in fatal state, quiesce device and disable
13744 	 * bus master to prevent any potential bad DMAs before freeing
13745 	 * kernel memory.
13746 	 */
13747 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
13748 		u16 val = 0;
13749 
13750 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
13751 		if (val == 0xffff)
13752 			bp->fw_reset_min_dsecs = 0;
13753 		bnxt_fw_fatal_close(bp);
13754 	}
13755 	__bnxt_close_nic(bp, true, false);
13756 	bnxt_vf_reps_free(bp);
13757 	bnxt_clear_int_mode(bp);
13758 	bnxt_hwrm_func_drv_unrgtr(bp);
13759 	if (pci_is_enabled(bp->pdev))
13760 		pci_disable_device(bp->pdev);
13761 	bnxt_free_ctx_mem(bp, false);
13762 }
13763 
13764 static bool is_bnxt_fw_ok(struct bnxt *bp)
13765 {
13766 	struct bnxt_fw_health *fw_health = bp->fw_health;
13767 	bool no_heartbeat = false, has_reset = false;
13768 	u32 val;
13769 
13770 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13771 	if (val == fw_health->last_fw_heartbeat)
13772 		no_heartbeat = true;
13773 
13774 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13775 	if (val != fw_health->last_fw_reset_cnt)
13776 		has_reset = true;
13777 
13778 	if (!no_heartbeat && has_reset)
13779 		return true;
13780 
13781 	return false;
13782 }
13783 
13784 /* rtnl_lock is acquired before calling this function */
13785 static void bnxt_force_fw_reset(struct bnxt *bp)
13786 {
13787 	struct bnxt_fw_health *fw_health = bp->fw_health;
13788 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13789 	u32 wait_dsecs;
13790 
13791 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
13792 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13793 		return;
13794 
13795 	/* we have to serialize with bnxt_refclk_read()*/
13796 	if (ptp) {
13797 		unsigned long flags;
13798 
13799 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
13800 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13801 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
13802 	} else {
13803 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13804 	}
13805 	bnxt_fw_reset_close(bp);
13806 	wait_dsecs = fw_health->master_func_wait_dsecs;
13807 	if (fw_health->primary) {
13808 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
13809 			wait_dsecs = 0;
13810 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
13811 	} else {
13812 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
13813 		wait_dsecs = fw_health->normal_func_wait_dsecs;
13814 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13815 	}
13816 
13817 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
13818 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
13819 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
13820 }
13821 
13822 void bnxt_fw_exception(struct bnxt *bp)
13823 {
13824 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
13825 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
13826 	bnxt_ulp_stop(bp);
13827 	bnxt_rtnl_lock_sp(bp);
13828 	bnxt_force_fw_reset(bp);
13829 	bnxt_rtnl_unlock_sp(bp);
13830 }
13831 
13832 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
13833  * < 0 on error.
13834  */
13835 static int bnxt_get_registered_vfs(struct bnxt *bp)
13836 {
13837 #ifdef CONFIG_BNXT_SRIOV
13838 	int rc;
13839 
13840 	if (!BNXT_PF(bp))
13841 		return 0;
13842 
13843 	rc = bnxt_hwrm_func_qcfg(bp);
13844 	if (rc) {
13845 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
13846 		return rc;
13847 	}
13848 	if (bp->pf.registered_vfs)
13849 		return bp->pf.registered_vfs;
13850 	if (bp->sriov_cfg)
13851 		return 1;
13852 #endif
13853 	return 0;
13854 }
13855 
13856 void bnxt_fw_reset(struct bnxt *bp)
13857 {
13858 	bnxt_ulp_stop(bp);
13859 	bnxt_rtnl_lock_sp(bp);
13860 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
13861 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13862 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13863 		int n = 0, tmo;
13864 
13865 		/* we have to serialize with bnxt_refclk_read()*/
13866 		if (ptp) {
13867 			unsigned long flags;
13868 
13869 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
13870 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13871 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
13872 		} else {
13873 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13874 		}
13875 		if (bp->pf.active_vfs &&
13876 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
13877 			n = bnxt_get_registered_vfs(bp);
13878 		if (n < 0) {
13879 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
13880 				   n);
13881 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13882 			dev_close(bp->dev);
13883 			goto fw_reset_exit;
13884 		} else if (n > 0) {
13885 			u16 vf_tmo_dsecs = n * 10;
13886 
13887 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
13888 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
13889 			bp->fw_reset_state =
13890 				BNXT_FW_RESET_STATE_POLL_VF;
13891 			bnxt_queue_fw_reset_work(bp, HZ / 10);
13892 			goto fw_reset_exit;
13893 		}
13894 		bnxt_fw_reset_close(bp);
13895 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13896 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
13897 			tmo = HZ / 10;
13898 		} else {
13899 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13900 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
13901 		}
13902 		bnxt_queue_fw_reset_work(bp, tmo);
13903 	}
13904 fw_reset_exit:
13905 	bnxt_rtnl_unlock_sp(bp);
13906 }
13907 
13908 static void bnxt_chk_missed_irq(struct bnxt *bp)
13909 {
13910 	int i;
13911 
13912 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13913 		return;
13914 
13915 	for (i = 0; i < bp->cp_nr_rings; i++) {
13916 		struct bnxt_napi *bnapi = bp->bnapi[i];
13917 		struct bnxt_cp_ring_info *cpr;
13918 		u32 fw_ring_id;
13919 		int j;
13920 
13921 		if (!bnapi)
13922 			continue;
13923 
13924 		cpr = &bnapi->cp_ring;
13925 		for (j = 0; j < cpr->cp_ring_count; j++) {
13926 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
13927 			u32 val[2];
13928 
13929 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
13930 				continue;
13931 
13932 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
13933 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
13934 				continue;
13935 			}
13936 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
13937 			bnxt_dbg_hwrm_ring_info_get(bp,
13938 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
13939 				fw_ring_id, &val[0], &val[1]);
13940 			cpr->sw_stats->cmn.missed_irqs++;
13941 		}
13942 	}
13943 }
13944 
13945 static void bnxt_cfg_ntp_filters(struct bnxt *);
13946 
13947 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
13948 {
13949 	struct bnxt_link_info *link_info = &bp->link_info;
13950 
13951 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
13952 		link_info->autoneg = BNXT_AUTONEG_SPEED;
13953 		if (bp->hwrm_spec_code >= 0x10201) {
13954 			if (link_info->auto_pause_setting &
13955 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
13956 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13957 		} else {
13958 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13959 		}
13960 		bnxt_set_auto_speed(link_info);
13961 	} else {
13962 		bnxt_set_force_speed(link_info);
13963 		link_info->req_duplex = link_info->duplex_setting;
13964 	}
13965 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
13966 		link_info->req_flow_ctrl =
13967 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
13968 	else
13969 		link_info->req_flow_ctrl = link_info->force_pause_setting;
13970 }
13971 
13972 static void bnxt_fw_echo_reply(struct bnxt *bp)
13973 {
13974 	struct bnxt_fw_health *fw_health = bp->fw_health;
13975 	struct hwrm_func_echo_response_input *req;
13976 	int rc;
13977 
13978 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
13979 	if (rc)
13980 		return;
13981 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
13982 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
13983 	hwrm_req_send(bp, req);
13984 }
13985 
13986 static void bnxt_ulp_restart(struct bnxt *bp)
13987 {
13988 	bnxt_ulp_stop(bp);
13989 	bnxt_ulp_start(bp, 0);
13990 }
13991 
13992 static void bnxt_sp_task(struct work_struct *work)
13993 {
13994 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
13995 
13996 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13997 	smp_mb__after_atomic();
13998 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13999 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14000 		return;
14001 	}
14002 
14003 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14004 		bnxt_ulp_restart(bp);
14005 		bnxt_reenable_sriov(bp);
14006 	}
14007 
14008 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14009 		bnxt_cfg_rx_mode(bp);
14010 
14011 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14012 		bnxt_cfg_ntp_filters(bp);
14013 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14014 		bnxt_hwrm_exec_fwd_req(bp);
14015 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14016 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14017 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14018 		bnxt_hwrm_port_qstats(bp, 0);
14019 		bnxt_hwrm_port_qstats_ext(bp, 0);
14020 		bnxt_accumulate_all_stats(bp);
14021 	}
14022 
14023 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14024 		int rc;
14025 
14026 		mutex_lock(&bp->link_lock);
14027 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14028 				       &bp->sp_event))
14029 			bnxt_hwrm_phy_qcaps(bp);
14030 
14031 		rc = bnxt_update_link(bp, true);
14032 		if (rc)
14033 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14034 				   rc);
14035 
14036 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14037 				       &bp->sp_event))
14038 			bnxt_init_ethtool_link_settings(bp);
14039 		mutex_unlock(&bp->link_lock);
14040 	}
14041 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14042 		int rc;
14043 
14044 		mutex_lock(&bp->link_lock);
14045 		rc = bnxt_update_phy_setting(bp);
14046 		mutex_unlock(&bp->link_lock);
14047 		if (rc) {
14048 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14049 		} else {
14050 			bp->link_info.phy_retry = false;
14051 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14052 		}
14053 	}
14054 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14055 		mutex_lock(&bp->link_lock);
14056 		bnxt_get_port_module_status(bp);
14057 		mutex_unlock(&bp->link_lock);
14058 	}
14059 
14060 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14061 		bnxt_tc_flow_stats_work(bp);
14062 
14063 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14064 		bnxt_chk_missed_irq(bp);
14065 
14066 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14067 		bnxt_fw_echo_reply(bp);
14068 
14069 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14070 		bnxt_hwmon_notify_event(bp);
14071 
14072 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14073 	 * must be the last functions to be called before exiting.
14074 	 */
14075 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14076 		bnxt_reset(bp, false);
14077 
14078 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14079 		bnxt_reset(bp, true);
14080 
14081 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14082 		bnxt_rx_ring_reset(bp);
14083 
14084 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14085 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14086 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14087 			bnxt_devlink_health_fw_report(bp);
14088 		else
14089 			bnxt_fw_reset(bp);
14090 	}
14091 
14092 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14093 		if (!is_bnxt_fw_ok(bp))
14094 			bnxt_devlink_health_fw_report(bp);
14095 	}
14096 
14097 	smp_mb__before_atomic();
14098 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14099 }
14100 
14101 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14102 				int *max_cp);
14103 
14104 /* Under rtnl_lock */
14105 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14106 		     int tx_xdp)
14107 {
14108 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14109 	struct bnxt_hw_rings hwr = {0};
14110 	int rx_rings = rx;
14111 	int rc;
14112 
14113 	if (tcs)
14114 		tx_sets = tcs;
14115 
14116 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14117 
14118 	if (max_rx < rx_rings)
14119 		return -ENOMEM;
14120 
14121 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14122 		rx_rings <<= 1;
14123 
14124 	hwr.rx = rx_rings;
14125 	hwr.tx = tx * tx_sets + tx_xdp;
14126 	if (max_tx < hwr.tx)
14127 		return -ENOMEM;
14128 
14129 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14130 
14131 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14132 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14133 	if (max_cp < hwr.cp)
14134 		return -ENOMEM;
14135 	hwr.stat = hwr.cp;
14136 	if (BNXT_NEW_RM(bp)) {
14137 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14138 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14139 		hwr.grp = rx;
14140 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14141 	}
14142 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14143 		hwr.cp_p5 = hwr.tx + rx;
14144 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14145 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14146 		if (!bnxt_ulp_registered(bp->edev)) {
14147 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14148 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14149 		}
14150 		if (hwr.cp > bp->total_irqs) {
14151 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14152 
14153 			if (total_msix < hwr.cp) {
14154 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14155 					    hwr.cp, total_msix);
14156 				rc = -ENOSPC;
14157 			}
14158 		}
14159 	}
14160 	return rc;
14161 }
14162 
14163 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14164 {
14165 	if (bp->bar2) {
14166 		pci_iounmap(pdev, bp->bar2);
14167 		bp->bar2 = NULL;
14168 	}
14169 
14170 	if (bp->bar1) {
14171 		pci_iounmap(pdev, bp->bar1);
14172 		bp->bar1 = NULL;
14173 	}
14174 
14175 	if (bp->bar0) {
14176 		pci_iounmap(pdev, bp->bar0);
14177 		bp->bar0 = NULL;
14178 	}
14179 }
14180 
14181 static void bnxt_cleanup_pci(struct bnxt *bp)
14182 {
14183 	bnxt_unmap_bars(bp, bp->pdev);
14184 	pci_release_regions(bp->pdev);
14185 	if (pci_is_enabled(bp->pdev))
14186 		pci_disable_device(bp->pdev);
14187 }
14188 
14189 static void bnxt_init_dflt_coal(struct bnxt *bp)
14190 {
14191 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14192 	struct bnxt_coal *coal;
14193 	u16 flags = 0;
14194 
14195 	if (coal_cap->cmpl_params &
14196 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14197 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14198 
14199 	/* Tick values in micro seconds.
14200 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14201 	 */
14202 	coal = &bp->rx_coal;
14203 	coal->coal_ticks = 10;
14204 	coal->coal_bufs = 30;
14205 	coal->coal_ticks_irq = 1;
14206 	coal->coal_bufs_irq = 2;
14207 	coal->idle_thresh = 50;
14208 	coal->bufs_per_record = 2;
14209 	coal->budget = 64;		/* NAPI budget */
14210 	coal->flags = flags;
14211 
14212 	coal = &bp->tx_coal;
14213 	coal->coal_ticks = 28;
14214 	coal->coal_bufs = 30;
14215 	coal->coal_ticks_irq = 2;
14216 	coal->coal_bufs_irq = 2;
14217 	coal->bufs_per_record = 1;
14218 	coal->flags = flags;
14219 
14220 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14221 }
14222 
14223 /* FW that pre-reserves 1 VNIC per function */
14224 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14225 {
14226 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14227 
14228 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14229 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14230 		return true;
14231 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14232 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14233 		return true;
14234 	return false;
14235 }
14236 
14237 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14238 {
14239 	int rc;
14240 
14241 	bp->fw_cap = 0;
14242 	rc = bnxt_hwrm_ver_get(bp);
14243 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14244 	 * so wait before continuing with recovery.
14245 	 */
14246 	if (rc)
14247 		msleep(100);
14248 	bnxt_try_map_fw_health_reg(bp);
14249 	if (rc) {
14250 		rc = bnxt_try_recover_fw(bp);
14251 		if (rc)
14252 			return rc;
14253 		rc = bnxt_hwrm_ver_get(bp);
14254 		if (rc)
14255 			return rc;
14256 	}
14257 
14258 	bnxt_nvm_cfg_ver_get(bp);
14259 
14260 	rc = bnxt_hwrm_func_reset(bp);
14261 	if (rc)
14262 		return -ENODEV;
14263 
14264 	bnxt_hwrm_fw_set_time(bp);
14265 	return 0;
14266 }
14267 
14268 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14269 {
14270 	int rc;
14271 
14272 	/* Get the MAX capabilities for this function */
14273 	rc = bnxt_hwrm_func_qcaps(bp);
14274 	if (rc) {
14275 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14276 			   rc);
14277 		return -ENODEV;
14278 	}
14279 
14280 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14281 	if (rc)
14282 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14283 			    rc);
14284 
14285 	if (bnxt_alloc_fw_health(bp)) {
14286 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14287 	} else {
14288 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14289 		if (rc)
14290 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14291 				    rc);
14292 	}
14293 
14294 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14295 	if (rc)
14296 		return -ENODEV;
14297 
14298 	rc = bnxt_alloc_crash_dump_mem(bp);
14299 	if (rc)
14300 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14301 			    rc);
14302 	if (!rc) {
14303 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14304 		if (rc) {
14305 			bnxt_free_crash_dump_mem(bp);
14306 			netdev_warn(bp->dev,
14307 				    "hwrm crash dump mem failure rc: %d\n", rc);
14308 		}
14309 	}
14310 
14311 	if (bnxt_fw_pre_resv_vnics(bp))
14312 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14313 
14314 	bnxt_hwrm_func_qcfg(bp);
14315 	bnxt_hwrm_vnic_qcaps(bp);
14316 	bnxt_hwrm_port_led_qcaps(bp);
14317 	bnxt_ethtool_init(bp);
14318 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14319 		__bnxt_hwrm_ptp_qcfg(bp);
14320 	bnxt_dcb_init(bp);
14321 	bnxt_hwmon_init(bp);
14322 	return 0;
14323 }
14324 
14325 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14326 {
14327 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14328 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14329 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14330 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14331 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14332 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14333 		bp->rss_hash_delta = bp->rss_hash_cfg;
14334 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14335 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14336 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14337 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14338 	}
14339 }
14340 
14341 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14342 {
14343 	struct net_device *dev = bp->dev;
14344 
14345 	dev->hw_features &= ~NETIF_F_NTUPLE;
14346 	dev->features &= ~NETIF_F_NTUPLE;
14347 	bp->flags &= ~BNXT_FLAG_RFS;
14348 	if (bnxt_rfs_supported(bp)) {
14349 		dev->hw_features |= NETIF_F_NTUPLE;
14350 		if (bnxt_rfs_capable(bp, false)) {
14351 			bp->flags |= BNXT_FLAG_RFS;
14352 			dev->features |= NETIF_F_NTUPLE;
14353 		}
14354 	}
14355 }
14356 
14357 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14358 {
14359 	struct pci_dev *pdev = bp->pdev;
14360 
14361 	bnxt_set_dflt_rss_hash_type(bp);
14362 	bnxt_set_dflt_rfs(bp);
14363 
14364 	bnxt_get_wol_settings(bp);
14365 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14366 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14367 	else
14368 		device_set_wakeup_capable(&pdev->dev, false);
14369 
14370 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14371 	bnxt_hwrm_coal_params_qcaps(bp);
14372 }
14373 
14374 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14375 
14376 int bnxt_fw_init_one(struct bnxt *bp)
14377 {
14378 	int rc;
14379 
14380 	rc = bnxt_fw_init_one_p1(bp);
14381 	if (rc) {
14382 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14383 		return rc;
14384 	}
14385 	rc = bnxt_fw_init_one_p2(bp);
14386 	if (rc) {
14387 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14388 		return rc;
14389 	}
14390 	rc = bnxt_probe_phy(bp, false);
14391 	if (rc)
14392 		return rc;
14393 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14394 	if (rc)
14395 		return rc;
14396 
14397 	bnxt_fw_init_one_p3(bp);
14398 	return 0;
14399 }
14400 
14401 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14402 {
14403 	struct bnxt_fw_health *fw_health = bp->fw_health;
14404 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14405 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14406 	u32 reg_type, reg_off, delay_msecs;
14407 
14408 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14409 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14410 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14411 	switch (reg_type) {
14412 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14413 		pci_write_config_dword(bp->pdev, reg_off, val);
14414 		break;
14415 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14416 		writel(reg_off & BNXT_GRC_BASE_MASK,
14417 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14418 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14419 		fallthrough;
14420 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14421 		writel(val, bp->bar0 + reg_off);
14422 		break;
14423 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14424 		writel(val, bp->bar1 + reg_off);
14425 		break;
14426 	}
14427 	if (delay_msecs) {
14428 		pci_read_config_dword(bp->pdev, 0, &val);
14429 		msleep(delay_msecs);
14430 	}
14431 }
14432 
14433 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14434 {
14435 	struct hwrm_func_qcfg_output *resp;
14436 	struct hwrm_func_qcfg_input *req;
14437 	bool result = true; /* firmware will enforce if unknown */
14438 
14439 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14440 		return result;
14441 
14442 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14443 		return result;
14444 
14445 	req->fid = cpu_to_le16(0xffff);
14446 	resp = hwrm_req_hold(bp, req);
14447 	if (!hwrm_req_send(bp, req))
14448 		result = !!(le16_to_cpu(resp->flags) &
14449 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14450 	hwrm_req_drop(bp, req);
14451 	return result;
14452 }
14453 
14454 static void bnxt_reset_all(struct bnxt *bp)
14455 {
14456 	struct bnxt_fw_health *fw_health = bp->fw_health;
14457 	int i, rc;
14458 
14459 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14460 		bnxt_fw_reset_via_optee(bp);
14461 		bp->fw_reset_timestamp = jiffies;
14462 		return;
14463 	}
14464 
14465 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14466 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14467 			bnxt_fw_reset_writel(bp, i);
14468 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14469 		struct hwrm_fw_reset_input *req;
14470 
14471 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14472 		if (!rc) {
14473 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14474 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14475 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14476 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14477 			rc = hwrm_req_send(bp, req);
14478 		}
14479 		if (rc != -ENODEV)
14480 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14481 	}
14482 	bp->fw_reset_timestamp = jiffies;
14483 }
14484 
14485 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14486 {
14487 	return time_after(jiffies, bp->fw_reset_timestamp +
14488 			  (bp->fw_reset_max_dsecs * HZ / 10));
14489 }
14490 
14491 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14492 {
14493 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14494 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14495 		bnxt_dl_health_fw_status_update(bp, false);
14496 	bp->fw_reset_state = 0;
14497 	dev_close(bp->dev);
14498 }
14499 
14500 static void bnxt_fw_reset_task(struct work_struct *work)
14501 {
14502 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14503 	int rc = 0;
14504 
14505 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14506 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14507 		return;
14508 	}
14509 
14510 	switch (bp->fw_reset_state) {
14511 	case BNXT_FW_RESET_STATE_POLL_VF: {
14512 		int n = bnxt_get_registered_vfs(bp);
14513 		int tmo;
14514 
14515 		if (n < 0) {
14516 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14517 				   n, jiffies_to_msecs(jiffies -
14518 				   bp->fw_reset_timestamp));
14519 			goto fw_reset_abort;
14520 		} else if (n > 0) {
14521 			if (bnxt_fw_reset_timeout(bp)) {
14522 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14523 				bp->fw_reset_state = 0;
14524 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14525 					   n);
14526 				goto ulp_start;
14527 			}
14528 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14529 			return;
14530 		}
14531 		bp->fw_reset_timestamp = jiffies;
14532 		rtnl_lock();
14533 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14534 			bnxt_fw_reset_abort(bp, rc);
14535 			rtnl_unlock();
14536 			goto ulp_start;
14537 		}
14538 		bnxt_fw_reset_close(bp);
14539 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14540 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14541 			tmo = HZ / 10;
14542 		} else {
14543 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14544 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14545 		}
14546 		rtnl_unlock();
14547 		bnxt_queue_fw_reset_work(bp, tmo);
14548 		return;
14549 	}
14550 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
14551 		u32 val;
14552 
14553 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14554 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
14555 		    !bnxt_fw_reset_timeout(bp)) {
14556 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14557 			return;
14558 		}
14559 
14560 		if (!bp->fw_health->primary) {
14561 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
14562 
14563 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14564 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14565 			return;
14566 		}
14567 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14568 	}
14569 		fallthrough;
14570 	case BNXT_FW_RESET_STATE_RESET_FW:
14571 		bnxt_reset_all(bp);
14572 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14573 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
14574 		return;
14575 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
14576 		bnxt_inv_fw_health_reg(bp);
14577 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
14578 		    !bp->fw_reset_min_dsecs) {
14579 			u16 val;
14580 
14581 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14582 			if (val == 0xffff) {
14583 				if (bnxt_fw_reset_timeout(bp)) {
14584 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
14585 					rc = -ETIMEDOUT;
14586 					goto fw_reset_abort;
14587 				}
14588 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
14589 				return;
14590 			}
14591 		}
14592 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14593 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
14594 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
14595 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
14596 			bnxt_dl_remote_reload(bp);
14597 		if (pci_enable_device(bp->pdev)) {
14598 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
14599 			rc = -ENODEV;
14600 			goto fw_reset_abort;
14601 		}
14602 		pci_set_master(bp->pdev);
14603 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
14604 		fallthrough;
14605 	case BNXT_FW_RESET_STATE_POLL_FW:
14606 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
14607 		rc = bnxt_hwrm_poll(bp);
14608 		if (rc) {
14609 			if (bnxt_fw_reset_timeout(bp)) {
14610 				netdev_err(bp->dev, "Firmware reset aborted\n");
14611 				goto fw_reset_abort_status;
14612 			}
14613 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14614 			return;
14615 		}
14616 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
14617 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
14618 		fallthrough;
14619 	case BNXT_FW_RESET_STATE_OPENING:
14620 		while (!rtnl_trylock()) {
14621 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14622 			return;
14623 		}
14624 		rc = bnxt_open(bp->dev);
14625 		if (rc) {
14626 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
14627 			bnxt_fw_reset_abort(bp, rc);
14628 			rtnl_unlock();
14629 			goto ulp_start;
14630 		}
14631 
14632 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
14633 		    bp->fw_health->enabled) {
14634 			bp->fw_health->last_fw_reset_cnt =
14635 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14636 		}
14637 		bp->fw_reset_state = 0;
14638 		/* Make sure fw_reset_state is 0 before clearing the flag */
14639 		smp_mb__before_atomic();
14640 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14641 		bnxt_ptp_reapply_pps(bp);
14642 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
14643 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
14644 			bnxt_dl_health_fw_recovery_done(bp);
14645 			bnxt_dl_health_fw_status_update(bp, true);
14646 		}
14647 		rtnl_unlock();
14648 		bnxt_ulp_start(bp, 0);
14649 		bnxt_reenable_sriov(bp);
14650 		rtnl_lock();
14651 		bnxt_vf_reps_alloc(bp);
14652 		bnxt_vf_reps_open(bp);
14653 		rtnl_unlock();
14654 		break;
14655 	}
14656 	return;
14657 
14658 fw_reset_abort_status:
14659 	if (bp->fw_health->status_reliable ||
14660 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
14661 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14662 
14663 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
14664 	}
14665 fw_reset_abort:
14666 	rtnl_lock();
14667 	bnxt_fw_reset_abort(bp, rc);
14668 	rtnl_unlock();
14669 ulp_start:
14670 	bnxt_ulp_start(bp, rc);
14671 }
14672 
14673 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
14674 {
14675 	int rc;
14676 	struct bnxt *bp = netdev_priv(dev);
14677 
14678 	SET_NETDEV_DEV(dev, &pdev->dev);
14679 
14680 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
14681 	rc = pci_enable_device(pdev);
14682 	if (rc) {
14683 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
14684 		goto init_err;
14685 	}
14686 
14687 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
14688 		dev_err(&pdev->dev,
14689 			"Cannot find PCI device base address, aborting\n");
14690 		rc = -ENODEV;
14691 		goto init_err_disable;
14692 	}
14693 
14694 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
14695 	if (rc) {
14696 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
14697 		goto init_err_disable;
14698 	}
14699 
14700 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
14701 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
14702 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
14703 		rc = -EIO;
14704 		goto init_err_release;
14705 	}
14706 
14707 	pci_set_master(pdev);
14708 
14709 	bp->dev = dev;
14710 	bp->pdev = pdev;
14711 
14712 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
14713 	 * determines the BAR size.
14714 	 */
14715 	bp->bar0 = pci_ioremap_bar(pdev, 0);
14716 	if (!bp->bar0) {
14717 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
14718 		rc = -ENOMEM;
14719 		goto init_err_release;
14720 	}
14721 
14722 	bp->bar2 = pci_ioremap_bar(pdev, 4);
14723 	if (!bp->bar2) {
14724 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
14725 		rc = -ENOMEM;
14726 		goto init_err_release;
14727 	}
14728 
14729 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
14730 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
14731 
14732 	spin_lock_init(&bp->ntp_fltr_lock);
14733 #if BITS_PER_LONG == 32
14734 	spin_lock_init(&bp->db_lock);
14735 #endif
14736 
14737 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
14738 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
14739 
14740 	timer_setup(&bp->timer, bnxt_timer, 0);
14741 	bp->current_interval = BNXT_TIMER_INTERVAL;
14742 
14743 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
14744 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
14745 
14746 	clear_bit(BNXT_STATE_OPEN, &bp->state);
14747 	return 0;
14748 
14749 init_err_release:
14750 	bnxt_unmap_bars(bp, pdev);
14751 	pci_release_regions(pdev);
14752 
14753 init_err_disable:
14754 	pci_disable_device(pdev);
14755 
14756 init_err:
14757 	return rc;
14758 }
14759 
14760 /* rtnl_lock held */
14761 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
14762 {
14763 	struct sockaddr *addr = p;
14764 	struct bnxt *bp = netdev_priv(dev);
14765 	int rc = 0;
14766 
14767 	if (!is_valid_ether_addr(addr->sa_data))
14768 		return -EADDRNOTAVAIL;
14769 
14770 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
14771 		return 0;
14772 
14773 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
14774 	if (rc)
14775 		return rc;
14776 
14777 	eth_hw_addr_set(dev, addr->sa_data);
14778 	bnxt_clear_usr_fltrs(bp, true);
14779 	if (netif_running(dev)) {
14780 		bnxt_close_nic(bp, false, false);
14781 		rc = bnxt_open_nic(bp, false, false);
14782 	}
14783 
14784 	return rc;
14785 }
14786 
14787 /* rtnl_lock held */
14788 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
14789 {
14790 	struct bnxt *bp = netdev_priv(dev);
14791 
14792 	if (netif_running(dev))
14793 		bnxt_close_nic(bp, true, false);
14794 
14795 	WRITE_ONCE(dev->mtu, new_mtu);
14796 
14797 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
14798 	 * program is attached.  We need to set the AGG rings settings and
14799 	 * rx_skb_func accordingly.
14800 	 */
14801 	if (READ_ONCE(bp->xdp_prog))
14802 		bnxt_set_rx_skb_mode(bp, true);
14803 
14804 	bnxt_set_ring_params(bp);
14805 
14806 	if (netif_running(dev))
14807 		return bnxt_open_nic(bp, true, false);
14808 
14809 	return 0;
14810 }
14811 
14812 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
14813 {
14814 	struct bnxt *bp = netdev_priv(dev);
14815 	bool sh = false;
14816 	int rc, tx_cp;
14817 
14818 	if (tc > bp->max_tc) {
14819 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
14820 			   tc, bp->max_tc);
14821 		return -EINVAL;
14822 	}
14823 
14824 	if (bp->num_tc == tc)
14825 		return 0;
14826 
14827 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
14828 		sh = true;
14829 
14830 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
14831 			      sh, tc, bp->tx_nr_rings_xdp);
14832 	if (rc)
14833 		return rc;
14834 
14835 	/* Needs to close the device and do hw resource re-allocations */
14836 	if (netif_running(bp->dev))
14837 		bnxt_close_nic(bp, true, false);
14838 
14839 	if (tc) {
14840 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
14841 		netdev_set_num_tc(dev, tc);
14842 		bp->num_tc = tc;
14843 	} else {
14844 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
14845 		netdev_reset_tc(dev);
14846 		bp->num_tc = 0;
14847 	}
14848 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
14849 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
14850 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
14851 			       tx_cp + bp->rx_nr_rings;
14852 
14853 	if (netif_running(bp->dev))
14854 		return bnxt_open_nic(bp, true, false);
14855 
14856 	return 0;
14857 }
14858 
14859 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
14860 				  void *cb_priv)
14861 {
14862 	struct bnxt *bp = cb_priv;
14863 
14864 	if (!bnxt_tc_flower_enabled(bp) ||
14865 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
14866 		return -EOPNOTSUPP;
14867 
14868 	switch (type) {
14869 	case TC_SETUP_CLSFLOWER:
14870 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
14871 	default:
14872 		return -EOPNOTSUPP;
14873 	}
14874 }
14875 
14876 LIST_HEAD(bnxt_block_cb_list);
14877 
14878 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
14879 			 void *type_data)
14880 {
14881 	struct bnxt *bp = netdev_priv(dev);
14882 
14883 	switch (type) {
14884 	case TC_SETUP_BLOCK:
14885 		return flow_block_cb_setup_simple(type_data,
14886 						  &bnxt_block_cb_list,
14887 						  bnxt_setup_tc_block_cb,
14888 						  bp, bp, true);
14889 	case TC_SETUP_QDISC_MQPRIO: {
14890 		struct tc_mqprio_qopt *mqprio = type_data;
14891 
14892 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
14893 
14894 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
14895 	}
14896 	default:
14897 		return -EOPNOTSUPP;
14898 	}
14899 }
14900 
14901 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
14902 			    const struct sk_buff *skb)
14903 {
14904 	struct bnxt_vnic_info *vnic;
14905 
14906 	if (skb)
14907 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
14908 
14909 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
14910 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
14911 }
14912 
14913 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
14914 			   u32 idx)
14915 {
14916 	struct hlist_head *head;
14917 	int bit_id;
14918 
14919 	spin_lock_bh(&bp->ntp_fltr_lock);
14920 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
14921 	if (bit_id < 0) {
14922 		spin_unlock_bh(&bp->ntp_fltr_lock);
14923 		return -ENOMEM;
14924 	}
14925 
14926 	fltr->base.sw_id = (u16)bit_id;
14927 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
14928 	fltr->base.flags |= BNXT_ACT_RING_DST;
14929 	head = &bp->ntp_fltr_hash_tbl[idx];
14930 	hlist_add_head_rcu(&fltr->base.hash, head);
14931 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
14932 	bnxt_insert_usr_fltr(bp, &fltr->base);
14933 	bp->ntp_fltr_count++;
14934 	spin_unlock_bh(&bp->ntp_fltr_lock);
14935 	return 0;
14936 }
14937 
14938 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
14939 			    struct bnxt_ntuple_filter *f2)
14940 {
14941 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
14942 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
14943 	struct flow_keys *keys1 = &f1->fkeys;
14944 	struct flow_keys *keys2 = &f2->fkeys;
14945 
14946 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
14947 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
14948 		return false;
14949 
14950 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
14951 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
14952 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
14953 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
14954 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
14955 			return false;
14956 	} else {
14957 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
14958 				     &keys2->addrs.v6addrs.src) ||
14959 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
14960 				     &masks2->addrs.v6addrs.src) ||
14961 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
14962 				     &keys2->addrs.v6addrs.dst) ||
14963 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
14964 				     &masks2->addrs.v6addrs.dst))
14965 			return false;
14966 	}
14967 
14968 	return keys1->ports.src == keys2->ports.src &&
14969 	       masks1->ports.src == masks2->ports.src &&
14970 	       keys1->ports.dst == keys2->ports.dst &&
14971 	       masks1->ports.dst == masks2->ports.dst &&
14972 	       keys1->control.flags == keys2->control.flags &&
14973 	       f1->l2_fltr == f2->l2_fltr;
14974 }
14975 
14976 struct bnxt_ntuple_filter *
14977 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
14978 				struct bnxt_ntuple_filter *fltr, u32 idx)
14979 {
14980 	struct bnxt_ntuple_filter *f;
14981 	struct hlist_head *head;
14982 
14983 	head = &bp->ntp_fltr_hash_tbl[idx];
14984 	hlist_for_each_entry_rcu(f, head, base.hash) {
14985 		if (bnxt_fltr_match(f, fltr))
14986 			return f;
14987 	}
14988 	return NULL;
14989 }
14990 
14991 #ifdef CONFIG_RFS_ACCEL
14992 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
14993 			      u16 rxq_index, u32 flow_id)
14994 {
14995 	struct bnxt *bp = netdev_priv(dev);
14996 	struct bnxt_ntuple_filter *fltr, *new_fltr;
14997 	struct flow_keys *fkeys;
14998 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
14999 	struct bnxt_l2_filter *l2_fltr;
15000 	int rc = 0, idx;
15001 	u32 flags;
15002 
15003 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15004 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15005 		atomic_inc(&l2_fltr->refcnt);
15006 	} else {
15007 		struct bnxt_l2_key key;
15008 
15009 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15010 		key.vlan = 0;
15011 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15012 		if (!l2_fltr)
15013 			return -EINVAL;
15014 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15015 			bnxt_del_l2_filter(bp, l2_fltr);
15016 			return -EINVAL;
15017 		}
15018 	}
15019 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
15020 	if (!new_fltr) {
15021 		bnxt_del_l2_filter(bp, l2_fltr);
15022 		return -ENOMEM;
15023 	}
15024 
15025 	fkeys = &new_fltr->fkeys;
15026 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15027 		rc = -EPROTONOSUPPORT;
15028 		goto err_free;
15029 	}
15030 
15031 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15032 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15033 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15034 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15035 		rc = -EPROTONOSUPPORT;
15036 		goto err_free;
15037 	}
15038 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15039 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15040 		if (bp->hwrm_spec_code < 0x10601) {
15041 			rc = -EPROTONOSUPPORT;
15042 			goto err_free;
15043 		}
15044 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15045 	}
15046 	flags = fkeys->control.flags;
15047 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15048 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15049 		rc = -EPROTONOSUPPORT;
15050 		goto err_free;
15051 	}
15052 	new_fltr->l2_fltr = l2_fltr;
15053 
15054 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15055 	rcu_read_lock();
15056 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15057 	if (fltr) {
15058 		rc = fltr->base.sw_id;
15059 		rcu_read_unlock();
15060 		goto err_free;
15061 	}
15062 	rcu_read_unlock();
15063 
15064 	new_fltr->flow_id = flow_id;
15065 	new_fltr->base.rxq = rxq_index;
15066 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15067 	if (!rc) {
15068 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15069 		return new_fltr->base.sw_id;
15070 	}
15071 
15072 err_free:
15073 	bnxt_del_l2_filter(bp, l2_fltr);
15074 	kfree(new_fltr);
15075 	return rc;
15076 }
15077 #endif
15078 
15079 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15080 {
15081 	spin_lock_bh(&bp->ntp_fltr_lock);
15082 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15083 		spin_unlock_bh(&bp->ntp_fltr_lock);
15084 		return;
15085 	}
15086 	hlist_del_rcu(&fltr->base.hash);
15087 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15088 	bp->ntp_fltr_count--;
15089 	spin_unlock_bh(&bp->ntp_fltr_lock);
15090 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15091 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15092 	kfree_rcu(fltr, base.rcu);
15093 }
15094 
15095 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15096 {
15097 #ifdef CONFIG_RFS_ACCEL
15098 	int i;
15099 
15100 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15101 		struct hlist_head *head;
15102 		struct hlist_node *tmp;
15103 		struct bnxt_ntuple_filter *fltr;
15104 		int rc;
15105 
15106 		head = &bp->ntp_fltr_hash_tbl[i];
15107 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15108 			bool del = false;
15109 
15110 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15111 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15112 					continue;
15113 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15114 							fltr->flow_id,
15115 							fltr->base.sw_id)) {
15116 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15117 									 fltr);
15118 					del = true;
15119 				}
15120 			} else {
15121 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15122 								       fltr);
15123 				if (rc)
15124 					del = true;
15125 				else
15126 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15127 			}
15128 
15129 			if (del)
15130 				bnxt_del_ntp_filter(bp, fltr);
15131 		}
15132 	}
15133 #endif
15134 }
15135 
15136 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15137 				    unsigned int entry, struct udp_tunnel_info *ti)
15138 {
15139 	struct bnxt *bp = netdev_priv(netdev);
15140 	unsigned int cmd;
15141 
15142 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15143 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15144 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15145 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15146 	else
15147 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15148 
15149 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15150 }
15151 
15152 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15153 				      unsigned int entry, struct udp_tunnel_info *ti)
15154 {
15155 	struct bnxt *bp = netdev_priv(netdev);
15156 	unsigned int cmd;
15157 
15158 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15159 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15160 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15161 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15162 	else
15163 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15164 
15165 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15166 }
15167 
15168 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15169 	.set_port	= bnxt_udp_tunnel_set_port,
15170 	.unset_port	= bnxt_udp_tunnel_unset_port,
15171 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15172 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15173 	.tables		= {
15174 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15175 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15176 	},
15177 }, bnxt_udp_tunnels_p7 = {
15178 	.set_port	= bnxt_udp_tunnel_set_port,
15179 	.unset_port	= bnxt_udp_tunnel_unset_port,
15180 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15181 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15182 	.tables		= {
15183 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15184 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15185 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15186 	},
15187 };
15188 
15189 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15190 			       struct net_device *dev, u32 filter_mask,
15191 			       int nlflags)
15192 {
15193 	struct bnxt *bp = netdev_priv(dev);
15194 
15195 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15196 				       nlflags, filter_mask, NULL);
15197 }
15198 
15199 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15200 			       u16 flags, struct netlink_ext_ack *extack)
15201 {
15202 	struct bnxt *bp = netdev_priv(dev);
15203 	struct nlattr *attr, *br_spec;
15204 	int rem, rc = 0;
15205 
15206 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15207 		return -EOPNOTSUPP;
15208 
15209 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15210 	if (!br_spec)
15211 		return -EINVAL;
15212 
15213 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15214 		u16 mode;
15215 
15216 		mode = nla_get_u16(attr);
15217 		if (mode == bp->br_mode)
15218 			break;
15219 
15220 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15221 		if (!rc)
15222 			bp->br_mode = mode;
15223 		break;
15224 	}
15225 	return rc;
15226 }
15227 
15228 int bnxt_get_port_parent_id(struct net_device *dev,
15229 			    struct netdev_phys_item_id *ppid)
15230 {
15231 	struct bnxt *bp = netdev_priv(dev);
15232 
15233 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15234 		return -EOPNOTSUPP;
15235 
15236 	/* The PF and it's VF-reps only support the switchdev framework */
15237 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15238 		return -EOPNOTSUPP;
15239 
15240 	ppid->id_len = sizeof(bp->dsn);
15241 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15242 
15243 	return 0;
15244 }
15245 
15246 static const struct net_device_ops bnxt_netdev_ops = {
15247 	.ndo_open		= bnxt_open,
15248 	.ndo_start_xmit		= bnxt_start_xmit,
15249 	.ndo_stop		= bnxt_close,
15250 	.ndo_get_stats64	= bnxt_get_stats64,
15251 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15252 	.ndo_eth_ioctl		= bnxt_ioctl,
15253 	.ndo_validate_addr	= eth_validate_addr,
15254 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15255 	.ndo_change_mtu		= bnxt_change_mtu,
15256 	.ndo_fix_features	= bnxt_fix_features,
15257 	.ndo_set_features	= bnxt_set_features,
15258 	.ndo_features_check	= bnxt_features_check,
15259 	.ndo_tx_timeout		= bnxt_tx_timeout,
15260 #ifdef CONFIG_BNXT_SRIOV
15261 	.ndo_get_vf_config	= bnxt_get_vf_config,
15262 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15263 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15264 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15265 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15266 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15267 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15268 #endif
15269 	.ndo_setup_tc           = bnxt_setup_tc,
15270 #ifdef CONFIG_RFS_ACCEL
15271 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15272 #endif
15273 	.ndo_bpf		= bnxt_xdp,
15274 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15275 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15276 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15277 };
15278 
15279 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15280 				    struct netdev_queue_stats_rx *stats)
15281 {
15282 	struct bnxt *bp = netdev_priv(dev);
15283 	struct bnxt_cp_ring_info *cpr;
15284 	u64 *sw;
15285 
15286 	cpr = &bp->bnapi[i]->cp_ring;
15287 	sw = cpr->stats.sw_stats;
15288 
15289 	stats->packets = 0;
15290 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15291 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15292 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15293 
15294 	stats->bytes = 0;
15295 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15296 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15297 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15298 
15299 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15300 }
15301 
15302 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15303 				    struct netdev_queue_stats_tx *stats)
15304 {
15305 	struct bnxt *bp = netdev_priv(dev);
15306 	struct bnxt_napi *bnapi;
15307 	u64 *sw;
15308 
15309 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15310 	sw = bnapi->cp_ring.stats.sw_stats;
15311 
15312 	stats->packets = 0;
15313 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15314 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15315 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15316 
15317 	stats->bytes = 0;
15318 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15319 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15320 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15321 }
15322 
15323 static void bnxt_get_base_stats(struct net_device *dev,
15324 				struct netdev_queue_stats_rx *rx,
15325 				struct netdev_queue_stats_tx *tx)
15326 {
15327 	struct bnxt *bp = netdev_priv(dev);
15328 
15329 	rx->packets = bp->net_stats_prev.rx_packets;
15330 	rx->bytes = bp->net_stats_prev.rx_bytes;
15331 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15332 
15333 	tx->packets = bp->net_stats_prev.tx_packets;
15334 	tx->bytes = bp->net_stats_prev.tx_bytes;
15335 }
15336 
15337 static const struct netdev_stat_ops bnxt_stat_ops = {
15338 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15339 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15340 	.get_base_stats		= bnxt_get_base_stats,
15341 };
15342 
15343 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15344 {
15345 	struct bnxt_rx_ring_info *rxr, *clone;
15346 	struct bnxt *bp = netdev_priv(dev);
15347 	struct bnxt_ring_struct *ring;
15348 	int rc;
15349 
15350 	rxr = &bp->rx_ring[idx];
15351 	clone = qmem;
15352 	memcpy(clone, rxr, sizeof(*rxr));
15353 	bnxt_init_rx_ring_struct(bp, clone);
15354 	bnxt_reset_rx_ring_struct(bp, clone);
15355 
15356 	clone->rx_prod = 0;
15357 	clone->rx_agg_prod = 0;
15358 	clone->rx_sw_agg_prod = 0;
15359 	clone->rx_next_cons = 0;
15360 
15361 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15362 	if (rc)
15363 		return rc;
15364 
15365 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15366 	if (rc < 0)
15367 		goto err_page_pool_destroy;
15368 
15369 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15370 					MEM_TYPE_PAGE_POOL,
15371 					clone->page_pool);
15372 	if (rc)
15373 		goto err_rxq_info_unreg;
15374 
15375 	ring = &clone->rx_ring_struct;
15376 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15377 	if (rc)
15378 		goto err_free_rx_ring;
15379 
15380 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15381 		ring = &clone->rx_agg_ring_struct;
15382 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15383 		if (rc)
15384 			goto err_free_rx_agg_ring;
15385 
15386 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15387 		if (rc)
15388 			goto err_free_rx_agg_ring;
15389 	}
15390 
15391 	if (bp->flags & BNXT_FLAG_TPA) {
15392 		rc = bnxt_alloc_one_tpa_info(bp, clone);
15393 		if (rc)
15394 			goto err_free_tpa_info;
15395 	}
15396 
15397 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15398 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15399 
15400 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15401 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15402 		bnxt_alloc_one_rx_ring_page(bp, clone, idx);
15403 	if (bp->flags & BNXT_FLAG_TPA)
15404 		bnxt_alloc_one_tpa_info_data(bp, clone);
15405 
15406 	return 0;
15407 
15408 err_free_tpa_info:
15409 	bnxt_free_one_tpa_info(bp, clone);
15410 err_free_rx_agg_ring:
15411 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15412 err_free_rx_ring:
15413 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15414 err_rxq_info_unreg:
15415 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15416 err_page_pool_destroy:
15417 	page_pool_destroy(clone->page_pool);
15418 	if (bnxt_separate_head_pool())
15419 		page_pool_destroy(clone->head_pool);
15420 	clone->page_pool = NULL;
15421 	clone->head_pool = NULL;
15422 	return rc;
15423 }
15424 
15425 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15426 {
15427 	struct bnxt_rx_ring_info *rxr = qmem;
15428 	struct bnxt *bp = netdev_priv(dev);
15429 	struct bnxt_ring_struct *ring;
15430 
15431 	bnxt_free_one_rx_ring_skbs(bp, rxr);
15432 
15433 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15434 
15435 	page_pool_destroy(rxr->page_pool);
15436 	if (bnxt_separate_head_pool())
15437 		page_pool_destroy(rxr->head_pool);
15438 	rxr->page_pool = NULL;
15439 	rxr->head_pool = NULL;
15440 
15441 	ring = &rxr->rx_ring_struct;
15442 	bnxt_free_ring(bp, &ring->ring_mem);
15443 
15444 	ring = &rxr->rx_agg_ring_struct;
15445 	bnxt_free_ring(bp, &ring->ring_mem);
15446 
15447 	kfree(rxr->rx_agg_bmap);
15448 	rxr->rx_agg_bmap = NULL;
15449 }
15450 
15451 static void bnxt_copy_rx_ring(struct bnxt *bp,
15452 			      struct bnxt_rx_ring_info *dst,
15453 			      struct bnxt_rx_ring_info *src)
15454 {
15455 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15456 	struct bnxt_ring_struct *dst_ring, *src_ring;
15457 	int i;
15458 
15459 	dst_ring = &dst->rx_ring_struct;
15460 	dst_rmem = &dst_ring->ring_mem;
15461 	src_ring = &src->rx_ring_struct;
15462 	src_rmem = &src_ring->ring_mem;
15463 
15464 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15465 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15466 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15467 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15468 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15469 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15470 
15471 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15472 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15473 	*dst_rmem->vmem = *src_rmem->vmem;
15474 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15475 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15476 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15477 	}
15478 
15479 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15480 		return;
15481 
15482 	dst_ring = &dst->rx_agg_ring_struct;
15483 	dst_rmem = &dst_ring->ring_mem;
15484 	src_ring = &src->rx_agg_ring_struct;
15485 	src_rmem = &src_ring->ring_mem;
15486 
15487 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15488 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15489 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15490 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15491 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15492 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15493 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15494 
15495 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15496 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15497 	*dst_rmem->vmem = *src_rmem->vmem;
15498 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15499 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15500 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15501 	}
15502 
15503 	dst->rx_agg_bmap = src->rx_agg_bmap;
15504 }
15505 
15506 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15507 {
15508 	struct bnxt *bp = netdev_priv(dev);
15509 	struct bnxt_rx_ring_info *rxr, *clone;
15510 	struct bnxt_cp_ring_info *cpr;
15511 	struct bnxt_vnic_info *vnic;
15512 	int i, rc;
15513 
15514 	rxr = &bp->rx_ring[idx];
15515 	clone = qmem;
15516 
15517 	rxr->rx_prod = clone->rx_prod;
15518 	rxr->rx_agg_prod = clone->rx_agg_prod;
15519 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
15520 	rxr->rx_next_cons = clone->rx_next_cons;
15521 	rxr->rx_tpa = clone->rx_tpa;
15522 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
15523 	rxr->page_pool = clone->page_pool;
15524 	rxr->head_pool = clone->head_pool;
15525 	rxr->xdp_rxq = clone->xdp_rxq;
15526 
15527 	bnxt_copy_rx_ring(bp, rxr, clone);
15528 
15529 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
15530 	if (rc)
15531 		return rc;
15532 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
15533 	if (rc)
15534 		goto err_free_hwrm_rx_ring;
15535 
15536 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
15537 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15538 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
15539 
15540 	cpr = &rxr->bnapi->cp_ring;
15541 	cpr->sw_stats->rx.rx_resets++;
15542 
15543 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15544 		vnic = &bp->vnic_info[i];
15545 
15546 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
15547 		if (rc) {
15548 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
15549 				   vnic->vnic_id, rc);
15550 			return rc;
15551 		}
15552 		vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
15553 		bnxt_hwrm_vnic_update(bp, vnic,
15554 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15555 	}
15556 
15557 	return 0;
15558 
15559 err_free_hwrm_rx_ring:
15560 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15561 	return rc;
15562 }
15563 
15564 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
15565 {
15566 	struct bnxt *bp = netdev_priv(dev);
15567 	struct bnxt_rx_ring_info *rxr;
15568 	struct bnxt_vnic_info *vnic;
15569 	int i;
15570 
15571 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15572 		vnic = &bp->vnic_info[i];
15573 		vnic->mru = 0;
15574 		bnxt_hwrm_vnic_update(bp, vnic,
15575 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15576 	}
15577 
15578 	rxr = &bp->rx_ring[idx];
15579 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15580 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
15581 	rxr->rx_next_cons = 0;
15582 	page_pool_disable_direct_recycling(rxr->page_pool);
15583 	if (bnxt_separate_head_pool())
15584 		page_pool_disable_direct_recycling(rxr->head_pool);
15585 
15586 	memcpy(qmem, rxr, sizeof(*rxr));
15587 	bnxt_init_rx_ring_struct(bp, qmem);
15588 
15589 	return 0;
15590 }
15591 
15592 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
15593 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
15594 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
15595 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
15596 	.ndo_queue_start	= bnxt_queue_start,
15597 	.ndo_queue_stop		= bnxt_queue_stop,
15598 };
15599 
15600 static void bnxt_remove_one(struct pci_dev *pdev)
15601 {
15602 	struct net_device *dev = pci_get_drvdata(pdev);
15603 	struct bnxt *bp = netdev_priv(dev);
15604 
15605 	if (BNXT_PF(bp))
15606 		bnxt_sriov_disable(bp);
15607 
15608 	bnxt_rdma_aux_device_del(bp);
15609 
15610 	bnxt_ptp_clear(bp);
15611 	unregister_netdev(dev);
15612 
15613 	bnxt_rdma_aux_device_uninit(bp);
15614 
15615 	bnxt_free_l2_filters(bp, true);
15616 	bnxt_free_ntp_fltrs(bp, true);
15617 	WARN_ON(bp->num_rss_ctx);
15618 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15619 	/* Flush any pending tasks */
15620 	cancel_work_sync(&bp->sp_task);
15621 	cancel_delayed_work_sync(&bp->fw_reset_task);
15622 	bp->sp_event = 0;
15623 
15624 	bnxt_dl_fw_reporters_destroy(bp);
15625 	bnxt_dl_unregister(bp);
15626 	bnxt_shutdown_tc(bp);
15627 
15628 	bnxt_clear_int_mode(bp);
15629 	bnxt_hwrm_func_drv_unrgtr(bp);
15630 	bnxt_free_hwrm_resources(bp);
15631 	bnxt_hwmon_uninit(bp);
15632 	bnxt_ethtool_free(bp);
15633 	bnxt_dcb_free(bp);
15634 	kfree(bp->ptp_cfg);
15635 	bp->ptp_cfg = NULL;
15636 	kfree(bp->fw_health);
15637 	bp->fw_health = NULL;
15638 	bnxt_cleanup_pci(bp);
15639 	bnxt_free_ctx_mem(bp, true);
15640 	bnxt_free_crash_dump_mem(bp);
15641 	kfree(bp->rss_indir_tbl);
15642 	bp->rss_indir_tbl = NULL;
15643 	bnxt_free_port_stats(bp);
15644 	free_netdev(dev);
15645 }
15646 
15647 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
15648 {
15649 	int rc = 0;
15650 	struct bnxt_link_info *link_info = &bp->link_info;
15651 
15652 	bp->phy_flags = 0;
15653 	rc = bnxt_hwrm_phy_qcaps(bp);
15654 	if (rc) {
15655 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
15656 			   rc);
15657 		return rc;
15658 	}
15659 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
15660 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
15661 	else
15662 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
15663 	if (!fw_dflt)
15664 		return 0;
15665 
15666 	mutex_lock(&bp->link_lock);
15667 	rc = bnxt_update_link(bp, false);
15668 	if (rc) {
15669 		mutex_unlock(&bp->link_lock);
15670 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
15671 			   rc);
15672 		return rc;
15673 	}
15674 
15675 	/* Older firmware does not have supported_auto_speeds, so assume
15676 	 * that all supported speeds can be autonegotiated.
15677 	 */
15678 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
15679 		link_info->support_auto_speeds = link_info->support_speeds;
15680 
15681 	bnxt_init_ethtool_link_settings(bp);
15682 	mutex_unlock(&bp->link_lock);
15683 	return 0;
15684 }
15685 
15686 static int bnxt_get_max_irq(struct pci_dev *pdev)
15687 {
15688 	u16 ctrl;
15689 
15690 	if (!pdev->msix_cap)
15691 		return 1;
15692 
15693 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
15694 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
15695 }
15696 
15697 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15698 				int *max_cp)
15699 {
15700 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
15701 	int max_ring_grps = 0, max_irq;
15702 
15703 	*max_tx = hw_resc->max_tx_rings;
15704 	*max_rx = hw_resc->max_rx_rings;
15705 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
15706 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
15707 			bnxt_get_ulp_msix_num_in_use(bp),
15708 			hw_resc->max_stat_ctxs -
15709 			bnxt_get_ulp_stat_ctxs_in_use(bp));
15710 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
15711 		*max_cp = min_t(int, *max_cp, max_irq);
15712 	max_ring_grps = hw_resc->max_hw_ring_grps;
15713 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
15714 		*max_cp -= 1;
15715 		*max_rx -= 2;
15716 	}
15717 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15718 		*max_rx >>= 1;
15719 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
15720 		int rc;
15721 
15722 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
15723 		if (rc) {
15724 			*max_rx = 0;
15725 			*max_tx = 0;
15726 		}
15727 		/* On P5 chips, max_cp output param should be available NQs */
15728 		*max_cp = max_irq;
15729 	}
15730 	*max_rx = min_t(int, *max_rx, max_ring_grps);
15731 }
15732 
15733 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
15734 {
15735 	int rx, tx, cp;
15736 
15737 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
15738 	*max_rx = rx;
15739 	*max_tx = tx;
15740 	if (!rx || !tx || !cp)
15741 		return -ENOMEM;
15742 
15743 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
15744 }
15745 
15746 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15747 			       bool shared)
15748 {
15749 	int rc;
15750 
15751 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15752 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
15753 		/* Not enough rings, try disabling agg rings. */
15754 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
15755 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15756 		if (rc) {
15757 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
15758 			bp->flags |= BNXT_FLAG_AGG_RINGS;
15759 			return rc;
15760 		}
15761 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
15762 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15763 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15764 		bnxt_set_ring_params(bp);
15765 	}
15766 
15767 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
15768 		int max_cp, max_stat, max_irq;
15769 
15770 		/* Reserve minimum resources for RoCE */
15771 		max_cp = bnxt_get_max_func_cp_rings(bp);
15772 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
15773 		max_irq = bnxt_get_max_func_irqs(bp);
15774 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
15775 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
15776 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
15777 			return 0;
15778 
15779 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
15780 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
15781 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
15782 		max_cp = min_t(int, max_cp, max_irq);
15783 		max_cp = min_t(int, max_cp, max_stat);
15784 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
15785 		if (rc)
15786 			rc = 0;
15787 	}
15788 	return rc;
15789 }
15790 
15791 /* In initial default shared ring setting, each shared ring must have a
15792  * RX/TX ring pair.
15793  */
15794 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
15795 {
15796 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
15797 	bp->rx_nr_rings = bp->cp_nr_rings;
15798 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
15799 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15800 }
15801 
15802 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
15803 {
15804 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
15805 	int avail_msix;
15806 
15807 	if (!bnxt_can_reserve_rings(bp))
15808 		return 0;
15809 
15810 	if (sh)
15811 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
15812 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
15813 	/* Reduce default rings on multi-port cards so that total default
15814 	 * rings do not exceed CPU count.
15815 	 */
15816 	if (bp->port_count > 1) {
15817 		int max_rings =
15818 			max_t(int, num_online_cpus() / bp->port_count, 1);
15819 
15820 		dflt_rings = min_t(int, dflt_rings, max_rings);
15821 	}
15822 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
15823 	if (rc)
15824 		return rc;
15825 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
15826 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
15827 	if (sh)
15828 		bnxt_trim_dflt_sh_rings(bp);
15829 	else
15830 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
15831 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15832 
15833 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
15834 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
15835 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
15836 
15837 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
15838 		bnxt_set_dflt_ulp_stat_ctxs(bp);
15839 	}
15840 
15841 	rc = __bnxt_reserve_rings(bp);
15842 	if (rc && rc != -ENODEV)
15843 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
15844 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15845 	if (sh)
15846 		bnxt_trim_dflt_sh_rings(bp);
15847 
15848 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
15849 	if (bnxt_need_reserve_rings(bp)) {
15850 		rc = __bnxt_reserve_rings(bp);
15851 		if (rc && rc != -ENODEV)
15852 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
15853 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15854 	}
15855 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
15856 		bp->rx_nr_rings++;
15857 		bp->cp_nr_rings++;
15858 	}
15859 	if (rc) {
15860 		bp->tx_nr_rings = 0;
15861 		bp->rx_nr_rings = 0;
15862 	}
15863 	return rc;
15864 }
15865 
15866 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
15867 {
15868 	int rc;
15869 
15870 	if (bp->tx_nr_rings)
15871 		return 0;
15872 
15873 	bnxt_ulp_irq_stop(bp);
15874 	bnxt_clear_int_mode(bp);
15875 	rc = bnxt_set_dflt_rings(bp, true);
15876 	if (rc) {
15877 		if (BNXT_VF(bp) && rc == -ENODEV)
15878 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15879 		else
15880 			netdev_err(bp->dev, "Not enough rings available.\n");
15881 		goto init_dflt_ring_err;
15882 	}
15883 	rc = bnxt_init_int_mode(bp);
15884 	if (rc)
15885 		goto init_dflt_ring_err;
15886 
15887 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15888 
15889 	bnxt_set_dflt_rfs(bp);
15890 
15891 init_dflt_ring_err:
15892 	bnxt_ulp_irq_restart(bp, rc);
15893 	return rc;
15894 }
15895 
15896 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
15897 {
15898 	int rc;
15899 
15900 	ASSERT_RTNL();
15901 	bnxt_hwrm_func_qcaps(bp);
15902 
15903 	if (netif_running(bp->dev))
15904 		__bnxt_close_nic(bp, true, false);
15905 
15906 	bnxt_ulp_irq_stop(bp);
15907 	bnxt_clear_int_mode(bp);
15908 	rc = bnxt_init_int_mode(bp);
15909 	bnxt_ulp_irq_restart(bp, rc);
15910 
15911 	if (netif_running(bp->dev)) {
15912 		if (rc)
15913 			dev_close(bp->dev);
15914 		else
15915 			rc = bnxt_open_nic(bp, true, false);
15916 	}
15917 
15918 	return rc;
15919 }
15920 
15921 static int bnxt_init_mac_addr(struct bnxt *bp)
15922 {
15923 	int rc = 0;
15924 
15925 	if (BNXT_PF(bp)) {
15926 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
15927 	} else {
15928 #ifdef CONFIG_BNXT_SRIOV
15929 		struct bnxt_vf_info *vf = &bp->vf;
15930 		bool strict_approval = true;
15931 
15932 		if (is_valid_ether_addr(vf->mac_addr)) {
15933 			/* overwrite netdev dev_addr with admin VF MAC */
15934 			eth_hw_addr_set(bp->dev, vf->mac_addr);
15935 			/* Older PF driver or firmware may not approve this
15936 			 * correctly.
15937 			 */
15938 			strict_approval = false;
15939 		} else {
15940 			eth_hw_addr_random(bp->dev);
15941 		}
15942 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
15943 #endif
15944 	}
15945 	return rc;
15946 }
15947 
15948 static void bnxt_vpd_read_info(struct bnxt *bp)
15949 {
15950 	struct pci_dev *pdev = bp->pdev;
15951 	unsigned int vpd_size, kw_len;
15952 	int pos, size;
15953 	u8 *vpd_data;
15954 
15955 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
15956 	if (IS_ERR(vpd_data)) {
15957 		pci_warn(pdev, "Unable to read VPD\n");
15958 		return;
15959 	}
15960 
15961 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15962 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
15963 	if (pos < 0)
15964 		goto read_sn;
15965 
15966 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15967 	memcpy(bp->board_partno, &vpd_data[pos], size);
15968 
15969 read_sn:
15970 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15971 					   PCI_VPD_RO_KEYWORD_SERIALNO,
15972 					   &kw_len);
15973 	if (pos < 0)
15974 		goto exit;
15975 
15976 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15977 	memcpy(bp->board_serialno, &vpd_data[pos], size);
15978 exit:
15979 	kfree(vpd_data);
15980 }
15981 
15982 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
15983 {
15984 	struct pci_dev *pdev = bp->pdev;
15985 	u64 qword;
15986 
15987 	qword = pci_get_dsn(pdev);
15988 	if (!qword) {
15989 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
15990 		return -EOPNOTSUPP;
15991 	}
15992 
15993 	put_unaligned_le64(qword, dsn);
15994 
15995 	bp->flags |= BNXT_FLAG_DSN_VALID;
15996 	return 0;
15997 }
15998 
15999 static int bnxt_map_db_bar(struct bnxt *bp)
16000 {
16001 	if (!bp->db_size)
16002 		return -ENODEV;
16003 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16004 	if (!bp->bar1)
16005 		return -ENOMEM;
16006 	return 0;
16007 }
16008 
16009 void bnxt_print_device_info(struct bnxt *bp)
16010 {
16011 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16012 		    board_info[bp->board_idx].name,
16013 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16014 
16015 	pcie_print_link_status(bp->pdev);
16016 }
16017 
16018 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16019 {
16020 	struct bnxt_hw_resc *hw_resc;
16021 	struct net_device *dev;
16022 	struct bnxt *bp;
16023 	int rc, max_irqs;
16024 
16025 	if (pci_is_bridge(pdev))
16026 		return -ENODEV;
16027 
16028 	if (!pdev->msix_cap) {
16029 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16030 		return -ENODEV;
16031 	}
16032 
16033 	/* Clear any pending DMA transactions from crash kernel
16034 	 * while loading driver in capture kernel.
16035 	 */
16036 	if (is_kdump_kernel()) {
16037 		pci_clear_master(pdev);
16038 		pcie_flr(pdev);
16039 	}
16040 
16041 	max_irqs = bnxt_get_max_irq(pdev);
16042 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16043 				 max_irqs);
16044 	if (!dev)
16045 		return -ENOMEM;
16046 
16047 	bp = netdev_priv(dev);
16048 	bp->board_idx = ent->driver_data;
16049 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16050 	bnxt_set_max_func_irqs(bp, max_irqs);
16051 
16052 	if (bnxt_vf_pciid(bp->board_idx))
16053 		bp->flags |= BNXT_FLAG_VF;
16054 
16055 	/* No devlink port registration in case of a VF */
16056 	if (BNXT_PF(bp))
16057 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16058 
16059 	rc = bnxt_init_board(pdev, dev);
16060 	if (rc < 0)
16061 		goto init_err_free;
16062 
16063 	dev->netdev_ops = &bnxt_netdev_ops;
16064 	dev->stat_ops = &bnxt_stat_ops;
16065 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16066 	dev->ethtool_ops = &bnxt_ethtool_ops;
16067 	pci_set_drvdata(pdev, dev);
16068 
16069 	rc = bnxt_alloc_hwrm_resources(bp);
16070 	if (rc)
16071 		goto init_err_pci_clean;
16072 
16073 	mutex_init(&bp->hwrm_cmd_lock);
16074 	mutex_init(&bp->link_lock);
16075 
16076 	rc = bnxt_fw_init_one_p1(bp);
16077 	if (rc)
16078 		goto init_err_pci_clean;
16079 
16080 	if (BNXT_PF(bp))
16081 		bnxt_vpd_read_info(bp);
16082 
16083 	if (BNXT_CHIP_P5_PLUS(bp)) {
16084 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16085 		if (BNXT_CHIP_P7(bp))
16086 			bp->flags |= BNXT_FLAG_CHIP_P7;
16087 	}
16088 
16089 	rc = bnxt_alloc_rss_indir_tbl(bp);
16090 	if (rc)
16091 		goto init_err_pci_clean;
16092 
16093 	rc = bnxt_fw_init_one_p2(bp);
16094 	if (rc)
16095 		goto init_err_pci_clean;
16096 
16097 	rc = bnxt_map_db_bar(bp);
16098 	if (rc) {
16099 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16100 			rc);
16101 		goto init_err_pci_clean;
16102 	}
16103 
16104 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16105 			   NETIF_F_TSO | NETIF_F_TSO6 |
16106 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16107 			   NETIF_F_GSO_IPXIP4 |
16108 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16109 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16110 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16111 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16112 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16113 
16114 	if (BNXT_SUPPORTS_TPA(bp))
16115 		dev->hw_features |= NETIF_F_LRO;
16116 
16117 	dev->hw_enc_features =
16118 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16119 			NETIF_F_TSO | NETIF_F_TSO6 |
16120 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16121 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16122 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16123 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16124 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16125 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16126 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16127 	else
16128 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16129 
16130 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16131 				    NETIF_F_GSO_GRE_CSUM;
16132 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16133 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16134 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16135 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16136 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16137 	if (BNXT_SUPPORTS_TPA(bp))
16138 		dev->hw_features |= NETIF_F_GRO_HW;
16139 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16140 	if (dev->features & NETIF_F_GRO_HW)
16141 		dev->features &= ~NETIF_F_LRO;
16142 	dev->priv_flags |= IFF_UNICAST_FLT;
16143 
16144 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16145 	if (bp->tso_max_segs)
16146 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16147 
16148 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16149 			    NETDEV_XDP_ACT_RX_SG;
16150 
16151 #ifdef CONFIG_BNXT_SRIOV
16152 	init_waitqueue_head(&bp->sriov_cfg_wait);
16153 #endif
16154 	if (BNXT_SUPPORTS_TPA(bp)) {
16155 		bp->gro_func = bnxt_gro_func_5730x;
16156 		if (BNXT_CHIP_P4(bp))
16157 			bp->gro_func = bnxt_gro_func_5731x;
16158 		else if (BNXT_CHIP_P5_PLUS(bp))
16159 			bp->gro_func = bnxt_gro_func_5750x;
16160 	}
16161 	if (!BNXT_CHIP_P4_PLUS(bp))
16162 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16163 
16164 	rc = bnxt_init_mac_addr(bp);
16165 	if (rc) {
16166 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16167 		rc = -EADDRNOTAVAIL;
16168 		goto init_err_pci_clean;
16169 	}
16170 
16171 	if (BNXT_PF(bp)) {
16172 		/* Read the adapter's DSN to use as the eswitch switch_id */
16173 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16174 	}
16175 
16176 	/* MTU range: 60 - FW defined max */
16177 	dev->min_mtu = ETH_ZLEN;
16178 	dev->max_mtu = bp->max_mtu;
16179 
16180 	rc = bnxt_probe_phy(bp, true);
16181 	if (rc)
16182 		goto init_err_pci_clean;
16183 
16184 	hw_resc = &bp->hw_resc;
16185 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16186 		       BNXT_L2_FLTR_MAX_FLTR;
16187 	/* Older firmware may not report these filters properly */
16188 	if (bp->max_fltr < BNXT_MAX_FLTR)
16189 		bp->max_fltr = BNXT_MAX_FLTR;
16190 	bnxt_init_l2_fltr_tbl(bp);
16191 	bnxt_set_rx_skb_mode(bp, false);
16192 	bnxt_set_tpa_flags(bp);
16193 	bnxt_set_ring_params(bp);
16194 	bnxt_rdma_aux_device_init(bp);
16195 	rc = bnxt_set_dflt_rings(bp, true);
16196 	if (rc) {
16197 		if (BNXT_VF(bp) && rc == -ENODEV) {
16198 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16199 		} else {
16200 			netdev_err(bp->dev, "Not enough rings available.\n");
16201 			rc = -ENOMEM;
16202 		}
16203 		goto init_err_pci_clean;
16204 	}
16205 
16206 	bnxt_fw_init_one_p3(bp);
16207 
16208 	bnxt_init_dflt_coal(bp);
16209 
16210 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16211 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16212 
16213 	rc = bnxt_init_int_mode(bp);
16214 	if (rc)
16215 		goto init_err_pci_clean;
16216 
16217 	/* No TC has been set yet and rings may have been trimmed due to
16218 	 * limited MSIX, so we re-initialize the TX rings per TC.
16219 	 */
16220 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16221 
16222 	if (BNXT_PF(bp)) {
16223 		if (!bnxt_pf_wq) {
16224 			bnxt_pf_wq =
16225 				create_singlethread_workqueue("bnxt_pf_wq");
16226 			if (!bnxt_pf_wq) {
16227 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16228 				rc = -ENOMEM;
16229 				goto init_err_pci_clean;
16230 			}
16231 		}
16232 		rc = bnxt_init_tc(bp);
16233 		if (rc)
16234 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16235 				   rc);
16236 	}
16237 
16238 	bnxt_inv_fw_health_reg(bp);
16239 	rc = bnxt_dl_register(bp);
16240 	if (rc)
16241 		goto init_err_dl;
16242 
16243 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16244 
16245 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16246 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16247 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16248 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16249 
16250 	rc = register_netdev(dev);
16251 	if (rc)
16252 		goto init_err_cleanup;
16253 
16254 	bnxt_dl_fw_reporters_create(bp);
16255 
16256 	bnxt_rdma_aux_device_add(bp);
16257 
16258 	bnxt_print_device_info(bp);
16259 
16260 	pci_save_state(pdev);
16261 
16262 	return 0;
16263 init_err_cleanup:
16264 	bnxt_rdma_aux_device_uninit(bp);
16265 	bnxt_dl_unregister(bp);
16266 init_err_dl:
16267 	bnxt_shutdown_tc(bp);
16268 	bnxt_clear_int_mode(bp);
16269 
16270 init_err_pci_clean:
16271 	bnxt_hwrm_func_drv_unrgtr(bp);
16272 	bnxt_free_hwrm_resources(bp);
16273 	bnxt_hwmon_uninit(bp);
16274 	bnxt_ethtool_free(bp);
16275 	bnxt_ptp_clear(bp);
16276 	kfree(bp->ptp_cfg);
16277 	bp->ptp_cfg = NULL;
16278 	kfree(bp->fw_health);
16279 	bp->fw_health = NULL;
16280 	bnxt_cleanup_pci(bp);
16281 	bnxt_free_ctx_mem(bp, true);
16282 	bnxt_free_crash_dump_mem(bp);
16283 	kfree(bp->rss_indir_tbl);
16284 	bp->rss_indir_tbl = NULL;
16285 
16286 init_err_free:
16287 	free_netdev(dev);
16288 	return rc;
16289 }
16290 
16291 static void bnxt_shutdown(struct pci_dev *pdev)
16292 {
16293 	struct net_device *dev = pci_get_drvdata(pdev);
16294 	struct bnxt *bp;
16295 
16296 	if (!dev)
16297 		return;
16298 
16299 	rtnl_lock();
16300 	bp = netdev_priv(dev);
16301 	if (!bp)
16302 		goto shutdown_exit;
16303 
16304 	if (netif_running(dev))
16305 		dev_close(dev);
16306 
16307 	bnxt_ptp_clear(bp);
16308 	bnxt_clear_int_mode(bp);
16309 	pci_disable_device(pdev);
16310 
16311 	if (system_state == SYSTEM_POWER_OFF) {
16312 		pci_wake_from_d3(pdev, bp->wol);
16313 		pci_set_power_state(pdev, PCI_D3hot);
16314 	}
16315 
16316 shutdown_exit:
16317 	rtnl_unlock();
16318 }
16319 
16320 #ifdef CONFIG_PM_SLEEP
16321 static int bnxt_suspend(struct device *device)
16322 {
16323 	struct net_device *dev = dev_get_drvdata(device);
16324 	struct bnxt *bp = netdev_priv(dev);
16325 	int rc = 0;
16326 
16327 	bnxt_ulp_stop(bp);
16328 
16329 	rtnl_lock();
16330 	if (netif_running(dev)) {
16331 		netif_device_detach(dev);
16332 		rc = bnxt_close(dev);
16333 	}
16334 	bnxt_hwrm_func_drv_unrgtr(bp);
16335 	bnxt_ptp_clear(bp);
16336 	pci_disable_device(bp->pdev);
16337 	bnxt_free_ctx_mem(bp, false);
16338 	rtnl_unlock();
16339 	return rc;
16340 }
16341 
16342 static int bnxt_resume(struct device *device)
16343 {
16344 	struct net_device *dev = dev_get_drvdata(device);
16345 	struct bnxt *bp = netdev_priv(dev);
16346 	int rc = 0;
16347 
16348 	rtnl_lock();
16349 	rc = pci_enable_device(bp->pdev);
16350 	if (rc) {
16351 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16352 			   rc);
16353 		goto resume_exit;
16354 	}
16355 	pci_set_master(bp->pdev);
16356 	if (bnxt_hwrm_ver_get(bp)) {
16357 		rc = -ENODEV;
16358 		goto resume_exit;
16359 	}
16360 	rc = bnxt_hwrm_func_reset(bp);
16361 	if (rc) {
16362 		rc = -EBUSY;
16363 		goto resume_exit;
16364 	}
16365 
16366 	rc = bnxt_hwrm_func_qcaps(bp);
16367 	if (rc)
16368 		goto resume_exit;
16369 
16370 	bnxt_clear_reservations(bp, true);
16371 
16372 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16373 		rc = -ENODEV;
16374 		goto resume_exit;
16375 	}
16376 	if (bp->fw_crash_mem)
16377 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16378 
16379 	if (bnxt_ptp_init(bp)) {
16380 		kfree(bp->ptp_cfg);
16381 		bp->ptp_cfg = NULL;
16382 	}
16383 	bnxt_get_wol_settings(bp);
16384 	if (netif_running(dev)) {
16385 		rc = bnxt_open(dev);
16386 		if (!rc)
16387 			netif_device_attach(dev);
16388 	}
16389 
16390 resume_exit:
16391 	rtnl_unlock();
16392 	bnxt_ulp_start(bp, rc);
16393 	if (!rc)
16394 		bnxt_reenable_sriov(bp);
16395 	return rc;
16396 }
16397 
16398 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16399 #define BNXT_PM_OPS (&bnxt_pm_ops)
16400 
16401 #else
16402 
16403 #define BNXT_PM_OPS NULL
16404 
16405 #endif /* CONFIG_PM_SLEEP */
16406 
16407 /**
16408  * bnxt_io_error_detected - called when PCI error is detected
16409  * @pdev: Pointer to PCI device
16410  * @state: The current pci connection state
16411  *
16412  * This function is called after a PCI bus error affecting
16413  * this device has been detected.
16414  */
16415 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16416 					       pci_channel_state_t state)
16417 {
16418 	struct net_device *netdev = pci_get_drvdata(pdev);
16419 	struct bnxt *bp = netdev_priv(netdev);
16420 	bool abort = false;
16421 
16422 	netdev_info(netdev, "PCI I/O error detected\n");
16423 
16424 	bnxt_ulp_stop(bp);
16425 
16426 	rtnl_lock();
16427 	netif_device_detach(netdev);
16428 
16429 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16430 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16431 		abort = true;
16432 	}
16433 
16434 	if (abort || state == pci_channel_io_perm_failure) {
16435 		rtnl_unlock();
16436 		return PCI_ERS_RESULT_DISCONNECT;
16437 	}
16438 
16439 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16440 	 * so we disable bus master to prevent any potential bad DMAs before
16441 	 * freeing kernel memory.
16442 	 */
16443 	if (state == pci_channel_io_frozen) {
16444 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16445 		bnxt_fw_fatal_close(bp);
16446 	}
16447 
16448 	if (netif_running(netdev))
16449 		__bnxt_close_nic(bp, true, true);
16450 
16451 	if (pci_is_enabled(pdev))
16452 		pci_disable_device(pdev);
16453 	bnxt_free_ctx_mem(bp, false);
16454 	rtnl_unlock();
16455 
16456 	/* Request a slot slot reset. */
16457 	return PCI_ERS_RESULT_NEED_RESET;
16458 }
16459 
16460 /**
16461  * bnxt_io_slot_reset - called after the pci bus has been reset.
16462  * @pdev: Pointer to PCI device
16463  *
16464  * Restart the card from scratch, as if from a cold-boot.
16465  * At this point, the card has experienced a hard reset,
16466  * followed by fixups by BIOS, and has its config space
16467  * set up identically to what it was at cold boot.
16468  */
16469 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
16470 {
16471 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
16472 	struct net_device *netdev = pci_get_drvdata(pdev);
16473 	struct bnxt *bp = netdev_priv(netdev);
16474 	int retry = 0;
16475 	int err = 0;
16476 	int off;
16477 
16478 	netdev_info(bp->dev, "PCI Slot Reset\n");
16479 
16480 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16481 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
16482 		msleep(900);
16483 
16484 	rtnl_lock();
16485 
16486 	if (pci_enable_device(pdev)) {
16487 		dev_err(&pdev->dev,
16488 			"Cannot re-enable PCI device after reset.\n");
16489 	} else {
16490 		pci_set_master(pdev);
16491 		/* Upon fatal error, our device internal logic that latches to
16492 		 * BAR value is getting reset and will restore only upon
16493 		 * rewriting the BARs.
16494 		 *
16495 		 * As pci_restore_state() does not re-write the BARs if the
16496 		 * value is same as saved value earlier, driver needs to
16497 		 * write the BARs to 0 to force restore, in case of fatal error.
16498 		 */
16499 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
16500 				       &bp->state)) {
16501 			for (off = PCI_BASE_ADDRESS_0;
16502 			     off <= PCI_BASE_ADDRESS_5; off += 4)
16503 				pci_write_config_dword(bp->pdev, off, 0);
16504 		}
16505 		pci_restore_state(pdev);
16506 		pci_save_state(pdev);
16507 
16508 		bnxt_inv_fw_health_reg(bp);
16509 		bnxt_try_map_fw_health_reg(bp);
16510 
16511 		/* In some PCIe AER scenarios, firmware may take up to
16512 		 * 10 seconds to become ready in the worst case.
16513 		 */
16514 		do {
16515 			err = bnxt_try_recover_fw(bp);
16516 			if (!err)
16517 				break;
16518 			retry++;
16519 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
16520 
16521 		if (err) {
16522 			dev_err(&pdev->dev, "Firmware not ready\n");
16523 			goto reset_exit;
16524 		}
16525 
16526 		err = bnxt_hwrm_func_reset(bp);
16527 		if (!err)
16528 			result = PCI_ERS_RESULT_RECOVERED;
16529 
16530 		bnxt_ulp_irq_stop(bp);
16531 		bnxt_clear_int_mode(bp);
16532 		err = bnxt_init_int_mode(bp);
16533 		bnxt_ulp_irq_restart(bp, err);
16534 	}
16535 
16536 reset_exit:
16537 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16538 	bnxt_clear_reservations(bp, true);
16539 	rtnl_unlock();
16540 
16541 	return result;
16542 }
16543 
16544 /**
16545  * bnxt_io_resume - called when traffic can start flowing again.
16546  * @pdev: Pointer to PCI device
16547  *
16548  * This callback is called when the error recovery driver tells
16549  * us that its OK to resume normal operation.
16550  */
16551 static void bnxt_io_resume(struct pci_dev *pdev)
16552 {
16553 	struct net_device *netdev = pci_get_drvdata(pdev);
16554 	struct bnxt *bp = netdev_priv(netdev);
16555 	int err;
16556 
16557 	netdev_info(bp->dev, "PCI Slot Resume\n");
16558 	rtnl_lock();
16559 
16560 	err = bnxt_hwrm_func_qcaps(bp);
16561 	if (!err) {
16562 		if (netif_running(netdev))
16563 			err = bnxt_open(netdev);
16564 		else
16565 			err = bnxt_reserve_rings(bp, true);
16566 	}
16567 
16568 	if (!err)
16569 		netif_device_attach(netdev);
16570 
16571 	rtnl_unlock();
16572 	bnxt_ulp_start(bp, err);
16573 	if (!err)
16574 		bnxt_reenable_sriov(bp);
16575 }
16576 
16577 static const struct pci_error_handlers bnxt_err_handler = {
16578 	.error_detected	= bnxt_io_error_detected,
16579 	.slot_reset	= bnxt_io_slot_reset,
16580 	.resume		= bnxt_io_resume
16581 };
16582 
16583 static struct pci_driver bnxt_pci_driver = {
16584 	.name		= DRV_MODULE_NAME,
16585 	.id_table	= bnxt_pci_tbl,
16586 	.probe		= bnxt_init_one,
16587 	.remove		= bnxt_remove_one,
16588 	.shutdown	= bnxt_shutdown,
16589 	.driver.pm	= BNXT_PM_OPS,
16590 	.err_handler	= &bnxt_err_handler,
16591 #if defined(CONFIG_BNXT_SRIOV)
16592 	.sriov_configure = bnxt_sriov_configure,
16593 #endif
16594 };
16595 
16596 static int __init bnxt_init(void)
16597 {
16598 	int err;
16599 
16600 	bnxt_debug_init();
16601 	err = pci_register_driver(&bnxt_pci_driver);
16602 	if (err) {
16603 		bnxt_debug_exit();
16604 		return err;
16605 	}
16606 
16607 	return 0;
16608 }
16609 
16610 static void __exit bnxt_exit(void)
16611 {
16612 	pci_unregister_driver(&bnxt_pci_driver);
16613 	if (bnxt_pf_wq)
16614 		destroy_workqueue(bnxt_pf_wq);
16615 	bnxt_debug_exit();
16616 }
16617 
16618 module_init(bnxt_init);
16619 module_exit(bnxt_exit);
16620