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