xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 150b567e0d572342ef08bace7ee7aff80fd75327)
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 
bnxt_vf_pciid(enum board_idx idx)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 
bnxt_db_nq(struct bnxt * bp,struct bnxt_db_info * db,u32 idx)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 
bnxt_db_nq_arm(struct bnxt * bp,struct bnxt_db_info * db,u32 idx)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 
bnxt_db_cq(struct bnxt * bp,struct bnxt_db_info * db,u32 idx)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 
bnxt_queue_fw_reset_work(struct bnxt * bp,unsigned long delay)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 
__bnxt_queue_sp_work(struct bnxt * bp)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 
bnxt_queue_sp_work(struct bnxt * bp,unsigned int event)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 
bnxt_sched_reset_rxr(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)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 
bnxt_sched_reset_txr(struct bnxt * bp,struct bnxt_tx_ring_info * txr,u16 curr)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 
bnxt_xmit_get_cfa_action(struct sk_buff * skb)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 
bnxt_txr_db_kick(struct bnxt * bp,struct bnxt_tx_ring_info * txr,u16 prod)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 
bnxt_start_xmit(struct sk_buff * skb,struct net_device * dev)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. */
__bnxt_tx_int(struct bnxt * bp,struct bnxt_tx_ring_info * txr,int budget)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 
bnxt_tx_int(struct bnxt * bp,struct bnxt_napi * bnapi,int budget)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 
bnxt_separate_head_pool(void)882 static bool bnxt_separate_head_pool(void)
883 {
884 	return PAGE_SIZE > BNXT_RX_PAGE_SIZE;
885 }
886 
__bnxt_alloc_rx_page(struct bnxt * bp,dma_addr_t * mapping,struct bnxt_rx_ring_info * rxr,unsigned int * offset,gfp_t gfp)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 
__bnxt_alloc_rx_frag(struct bnxt * bp,dma_addr_t * mapping,struct bnxt_rx_ring_info * rxr,gfp_t gfp)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 
bnxt_alloc_rx_data(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,u16 prod,gfp_t gfp)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 
bnxt_reuse_rx_data(struct bnxt_rx_ring_info * rxr,u16 cons,void * data)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 
bnxt_find_next_agg_idx(struct bnxt_rx_ring_info * rxr,u16 idx)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 
bnxt_alloc_rx_page(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,u16 prod,gfp_t gfp)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 
bnxt_get_agg(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,u16 cp_cons,u16 curr)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 
bnxt_get_tpa_agg_p5(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,u16 agg_id,u16 curr)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 
bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info * cpr,u16 idx,u16 start,u32 agg_bufs,bool tpa)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 
bnxt_rx_multi_page_skb(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,u16 cons,void * data,u8 * data_ptr,dma_addr_t dma_addr,unsigned int offset_and_len)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 
bnxt_rx_page_skb(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,u16 cons,void * data,u8 * data_ptr,dma_addr_t dma_addr,unsigned int offset_and_len)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 
bnxt_rx_skb(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,u16 cons,void * data,u8 * data_ptr,dma_addr_t dma_addr,unsigned int offset_and_len)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 
__bnxt_rx_agg_pages(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,struct skb_shared_info * shinfo,u16 idx,u32 agg_bufs,bool tpa,struct xdp_buff * xdp)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 
bnxt_rx_agg_pages_skb(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,struct sk_buff * skb,u16 idx,u32 agg_bufs,bool tpa)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 
bnxt_rx_agg_pages_xdp(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,struct xdp_buff * xdp,u16 idx,u32 agg_bufs,bool tpa)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 
bnxt_agg_bufs_valid(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,u8 agg_bufs,u32 * raw_cons)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 
bnxt_copy_data(struct bnxt_napi * bnapi,u8 * data,unsigned int len,dma_addr_t mapping)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 
bnxt_copy_skb(struct bnxt_napi * bnapi,u8 * data,unsigned int len,dma_addr_t mapping)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 
bnxt_copy_xdp(struct bnxt_napi * bnapi,struct xdp_buff * xdp,unsigned int len,dma_addr_t mapping)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 
bnxt_discard_rx(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,u32 * raw_cons,void * cmp)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 
bnxt_alloc_agg_idx(struct bnxt_rx_ring_info * rxr,u16 agg_id)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 
bnxt_free_agg_idx(struct bnxt_rx_ring_info * rxr,u16 idx)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 
bnxt_lookup_agg_idx(struct bnxt_rx_ring_info * rxr,u16 agg_id)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 
bnxt_tpa_metadata(struct bnxt_tpa_info * tpa_info,struct rx_tpa_start_cmp * tpa_start,struct rx_tpa_start_cmp_ext * tpa_start1)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 
bnxt_tpa_metadata_v2(struct bnxt_tpa_info * tpa_info,struct rx_tpa_start_cmp * tpa_start,struct rx_tpa_start_cmp_ext * tpa_start1)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 
bnxt_tpa_start(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,u8 cmp_type,struct rx_tpa_start_cmp * tpa_start,struct rx_tpa_start_cmp_ext * tpa_start1)1480 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1481 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1482 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1483 {
1484 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1485 	struct bnxt_tpa_info *tpa_info;
1486 	u16 cons, prod, agg_id;
1487 	struct rx_bd *prod_bd;
1488 	dma_addr_t mapping;
1489 
1490 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1491 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1492 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1493 	} else {
1494 		agg_id = TPA_START_AGG_ID(tpa_start);
1495 	}
1496 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1497 	prod = rxr->rx_prod;
1498 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1499 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1500 	tpa_info = &rxr->rx_tpa[agg_id];
1501 
1502 	if (unlikely(cons != rxr->rx_next_cons ||
1503 		     TPA_START_ERROR(tpa_start))) {
1504 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1505 			    cons, rxr->rx_next_cons,
1506 			    TPA_START_ERROR_CODE(tpa_start1));
1507 		bnxt_sched_reset_rxr(bp, rxr);
1508 		return;
1509 	}
1510 	prod_rx_buf->data = tpa_info->data;
1511 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1512 
1513 	mapping = tpa_info->mapping;
1514 	prod_rx_buf->mapping = mapping;
1515 
1516 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1517 
1518 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1519 
1520 	tpa_info->data = cons_rx_buf->data;
1521 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1522 	cons_rx_buf->data = NULL;
1523 	tpa_info->mapping = cons_rx_buf->mapping;
1524 
1525 	tpa_info->len =
1526 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1527 				RX_TPA_START_CMP_LEN_SHIFT;
1528 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1529 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1530 		tpa_info->gso_type = SKB_GSO_TCPV4;
1531 		if (TPA_START_IS_IPV6(tpa_start1))
1532 			tpa_info->gso_type = SKB_GSO_TCPV6;
1533 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1534 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1535 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1536 			tpa_info->gso_type = SKB_GSO_TCPV6;
1537 		tpa_info->rss_hash =
1538 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1539 	} else {
1540 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1541 		tpa_info->gso_type = 0;
1542 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1543 	}
1544 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1545 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1546 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1547 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1548 	else
1549 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1550 	tpa_info->agg_count = 0;
1551 
1552 	rxr->rx_prod = NEXT_RX(prod);
1553 	cons = RING_RX(bp, NEXT_RX(cons));
1554 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1555 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1556 
1557 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1558 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1559 	cons_rx_buf->data = NULL;
1560 }
1561 
bnxt_abort_tpa(struct bnxt_cp_ring_info * cpr,u16 idx,u32 agg_bufs)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
bnxt_gro_tunnel(struct sk_buff * skb,__be16 ip_proto)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 
bnxt_gro_func_5731x(struct bnxt_tpa_info * tpa_info,int payload_off,int tcp_ts,struct sk_buff * skb)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 
bnxt_gro_func_5750x(struct bnxt_tpa_info * tpa_info,int payload_off,int tcp_ts,struct sk_buff * skb)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 
bnxt_gro_func_5730x(struct bnxt_tpa_info * tpa_info,int payload_off,int tcp_ts,struct sk_buff * skb)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 
bnxt_gro_skb(struct bnxt * bp,struct bnxt_tpa_info * tpa_info,struct rx_tpa_end_cmp * tpa_end,struct rx_tpa_end_cmp_ext * tpa_end1,struct sk_buff * skb)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  */
bnxt_get_pkt_dev(struct bnxt * bp,u16 cfa_code)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 
bnxt_tpa_end(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,u32 * raw_cons,struct rx_tpa_end_cmp * tpa_end,struct rx_tpa_end_cmp_ext * tpa_end1,u8 * event)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 
bnxt_tpa_agg(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,struct rx_agg_cmp * rx_agg)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 
bnxt_deliver_skb(struct bnxt * bp,struct bnxt_napi * bnapi,struct sk_buff * skb)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 
bnxt_rx_ts_valid(struct bnxt * bp,u32 flags,struct rx_cmp_ext * rxcmp1,u32 * cmpl_ts)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 
bnxt_rx_vlan(struct sk_buff * skb,u8 cmp_type,struct rx_cmp * rxcmp,struct rx_cmp_ext * rxcmp1)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 
bnxt_rss_ext_op(struct bnxt * bp,struct rx_cmp * rxcmp)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  */
bnxt_rx_pkt(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,u32 * raw_cons,u8 * event)2032 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2033 		       u32 *raw_cons, u8 *event)
2034 {
2035 	struct bnxt_napi *bnapi = cpr->bnapi;
2036 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2037 	struct net_device *dev = bp->dev;
2038 	struct rx_cmp *rxcmp;
2039 	struct rx_cmp_ext *rxcmp1;
2040 	u32 tmp_raw_cons = *raw_cons;
2041 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2042 	struct bnxt_sw_rx_bd *rx_buf;
2043 	unsigned int len;
2044 	u8 *data_ptr, agg_bufs, cmp_type;
2045 	bool xdp_active = false;
2046 	dma_addr_t dma_addr;
2047 	struct sk_buff *skb;
2048 	struct xdp_buff xdp;
2049 	u32 flags, misc;
2050 	u32 cmpl_ts;
2051 	void *data;
2052 	int rc = 0;
2053 
2054 	rxcmp = (struct rx_cmp *)
2055 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2056 
2057 	cmp_type = RX_CMP_TYPE(rxcmp);
2058 
2059 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2060 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2061 		goto next_rx_no_prod_no_len;
2062 	}
2063 
2064 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2065 	cp_cons = RING_CMP(tmp_raw_cons);
2066 	rxcmp1 = (struct rx_cmp_ext *)
2067 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2068 
2069 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2070 		return -EBUSY;
2071 
2072 	/* The valid test of the entry must be done first before
2073 	 * reading any further.
2074 	 */
2075 	dma_rmb();
2076 	prod = rxr->rx_prod;
2077 
2078 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2079 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2080 		bnxt_tpa_start(bp, rxr, cmp_type,
2081 			       (struct rx_tpa_start_cmp *)rxcmp,
2082 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2083 
2084 		*event |= BNXT_RX_EVENT;
2085 		goto next_rx_no_prod_no_len;
2086 
2087 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2088 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2089 				   (struct rx_tpa_end_cmp *)rxcmp,
2090 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2091 
2092 		if (IS_ERR(skb))
2093 			return -EBUSY;
2094 
2095 		rc = -ENOMEM;
2096 		if (likely(skb)) {
2097 			bnxt_deliver_skb(bp, bnapi, skb);
2098 			rc = 1;
2099 		}
2100 		*event |= BNXT_RX_EVENT;
2101 		goto next_rx_no_prod_no_len;
2102 	}
2103 
2104 	cons = rxcmp->rx_cmp_opaque;
2105 	if (unlikely(cons != rxr->rx_next_cons)) {
2106 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2107 
2108 		/* 0xffff is forced error, don't print it */
2109 		if (rxr->rx_next_cons != 0xffff)
2110 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2111 				    cons, rxr->rx_next_cons);
2112 		bnxt_sched_reset_rxr(bp, rxr);
2113 		if (rc1)
2114 			return rc1;
2115 		goto next_rx_no_prod_no_len;
2116 	}
2117 	rx_buf = &rxr->rx_buf_ring[cons];
2118 	data = rx_buf->data;
2119 	data_ptr = rx_buf->data_ptr;
2120 	prefetch(data_ptr);
2121 
2122 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2123 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2124 
2125 	if (agg_bufs) {
2126 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2127 			return -EBUSY;
2128 
2129 		cp_cons = NEXT_CMP(cp_cons);
2130 		*event |= BNXT_AGG_EVENT;
2131 	}
2132 	*event |= BNXT_RX_EVENT;
2133 
2134 	rx_buf->data = NULL;
2135 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2136 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2137 
2138 		bnxt_reuse_rx_data(rxr, cons, data);
2139 		if (agg_bufs)
2140 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2141 					       false);
2142 
2143 		rc = -EIO;
2144 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2145 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2146 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2147 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2148 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2149 						 rx_err);
2150 				bnxt_sched_reset_rxr(bp, rxr);
2151 			}
2152 		}
2153 		goto next_rx_no_len;
2154 	}
2155 
2156 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2157 	len = flags >> RX_CMP_LEN_SHIFT;
2158 	dma_addr = rx_buf->mapping;
2159 
2160 	if (bnxt_xdp_attached(bp, rxr)) {
2161 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2162 		if (agg_bufs) {
2163 			u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
2164 							     cp_cons, agg_bufs,
2165 							     false);
2166 			if (!frag_len)
2167 				goto oom_next_rx;
2168 		}
2169 		xdp_active = true;
2170 	}
2171 
2172 	if (xdp_active) {
2173 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2174 			rc = 1;
2175 			goto next_rx;
2176 		}
2177 	}
2178 
2179 	if (len <= bp->rx_copy_thresh) {
2180 		if (!xdp_active)
2181 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2182 		else
2183 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2184 		bnxt_reuse_rx_data(rxr, cons, data);
2185 		if (!skb) {
2186 			if (agg_bufs) {
2187 				if (!xdp_active)
2188 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2189 							       agg_bufs, false);
2190 				else
2191 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2192 			}
2193 			goto oom_next_rx;
2194 		}
2195 	} else {
2196 		u32 payload;
2197 
2198 		if (rx_buf->data_ptr == data_ptr)
2199 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2200 		else
2201 			payload = 0;
2202 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2203 				      payload | len);
2204 		if (!skb)
2205 			goto oom_next_rx;
2206 	}
2207 
2208 	if (agg_bufs) {
2209 		if (!xdp_active) {
2210 			skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
2211 			if (!skb)
2212 				goto oom_next_rx;
2213 		} else {
2214 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1);
2215 			if (!skb) {
2216 				/* we should be able to free the old skb here */
2217 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2218 				goto oom_next_rx;
2219 			}
2220 		}
2221 	}
2222 
2223 	if (RX_CMP_HASH_VALID(rxcmp)) {
2224 		enum pkt_hash_types type;
2225 
2226 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2227 			type = bnxt_rss_ext_op(bp, rxcmp);
2228 		} else {
2229 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2230 
2231 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2232 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2233 				type = PKT_HASH_TYPE_L4;
2234 			else
2235 				type = PKT_HASH_TYPE_L3;
2236 		}
2237 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2238 	}
2239 
2240 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2241 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2242 	skb->protocol = eth_type_trans(skb, dev);
2243 
2244 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2245 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2246 		if (!skb)
2247 			goto next_rx;
2248 	}
2249 
2250 	skb_checksum_none_assert(skb);
2251 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2252 		if (dev->features & NETIF_F_RXCSUM) {
2253 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2254 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2255 		}
2256 	} else {
2257 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2258 			if (dev->features & NETIF_F_RXCSUM)
2259 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2260 		}
2261 	}
2262 
2263 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2264 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2265 			u64 ns, ts;
2266 
2267 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2268 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2269 
2270 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2271 				memset(skb_hwtstamps(skb), 0,
2272 				       sizeof(*skb_hwtstamps(skb)));
2273 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2274 			}
2275 		}
2276 	}
2277 	bnxt_deliver_skb(bp, bnapi, skb);
2278 	rc = 1;
2279 
2280 next_rx:
2281 	cpr->rx_packets += 1;
2282 	cpr->rx_bytes += len;
2283 
2284 next_rx_no_len:
2285 	rxr->rx_prod = NEXT_RX(prod);
2286 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2287 
2288 next_rx_no_prod_no_len:
2289 	*raw_cons = tmp_raw_cons;
2290 
2291 	return rc;
2292 
2293 oom_next_rx:
2294 	cpr->sw_stats->rx.rx_oom_discards += 1;
2295 	rc = -ENOMEM;
2296 	goto next_rx;
2297 }
2298 
2299 /* In netpoll mode, if we are using a combined completion ring, we need to
2300  * discard the rx packets and recycle the buffers.
2301  */
bnxt_force_rx_discard(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,u32 * raw_cons,u8 * event)2302 static int bnxt_force_rx_discard(struct bnxt *bp,
2303 				 struct bnxt_cp_ring_info *cpr,
2304 				 u32 *raw_cons, u8 *event)
2305 {
2306 	u32 tmp_raw_cons = *raw_cons;
2307 	struct rx_cmp_ext *rxcmp1;
2308 	struct rx_cmp *rxcmp;
2309 	u16 cp_cons;
2310 	u8 cmp_type;
2311 	int rc;
2312 
2313 	cp_cons = RING_CMP(tmp_raw_cons);
2314 	rxcmp = (struct rx_cmp *)
2315 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2316 
2317 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2318 	cp_cons = RING_CMP(tmp_raw_cons);
2319 	rxcmp1 = (struct rx_cmp_ext *)
2320 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2321 
2322 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2323 		return -EBUSY;
2324 
2325 	/* The valid test of the entry must be done first before
2326 	 * reading any further.
2327 	 */
2328 	dma_rmb();
2329 	cmp_type = RX_CMP_TYPE(rxcmp);
2330 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2331 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2332 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2333 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2334 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2335 		struct rx_tpa_end_cmp_ext *tpa_end1;
2336 
2337 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2338 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2339 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2340 	}
2341 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2342 	if (rc && rc != -EBUSY)
2343 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2344 	return rc;
2345 }
2346 
bnxt_fw_health_readl(struct bnxt * bp,int reg_idx)2347 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2348 {
2349 	struct bnxt_fw_health *fw_health = bp->fw_health;
2350 	u32 reg = fw_health->regs[reg_idx];
2351 	u32 reg_type, reg_off, val = 0;
2352 
2353 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2354 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2355 	switch (reg_type) {
2356 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2357 		pci_read_config_dword(bp->pdev, reg_off, &val);
2358 		break;
2359 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2360 		reg_off = fw_health->mapped_regs[reg_idx];
2361 		fallthrough;
2362 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2363 		val = readl(bp->bar0 + reg_off);
2364 		break;
2365 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2366 		val = readl(bp->bar1 + reg_off);
2367 		break;
2368 	}
2369 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2370 		val &= fw_health->fw_reset_inprog_reg_mask;
2371 	return val;
2372 }
2373 
bnxt_agg_ring_id_to_grp_idx(struct bnxt * bp,u16 ring_id)2374 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2375 {
2376 	int i;
2377 
2378 	for (i = 0; i < bp->rx_nr_rings; i++) {
2379 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2380 		struct bnxt_ring_grp_info *grp_info;
2381 
2382 		grp_info = &bp->grp_info[grp_idx];
2383 		if (grp_info->agg_fw_ring_id == ring_id)
2384 			return grp_idx;
2385 	}
2386 	return INVALID_HW_RING_ID;
2387 }
2388 
bnxt_get_force_speed(struct bnxt_link_info * link_info)2389 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2390 {
2391 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2392 
2393 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2394 		return link_info->force_link_speed2;
2395 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2396 		return link_info->force_pam4_link_speed;
2397 	return link_info->force_link_speed;
2398 }
2399 
bnxt_set_force_speed(struct bnxt_link_info * link_info)2400 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2401 {
2402 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2403 
2404 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2405 		link_info->req_link_speed = link_info->force_link_speed2;
2406 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2407 		switch (link_info->req_link_speed) {
2408 		case BNXT_LINK_SPEED_50GB_PAM4:
2409 		case BNXT_LINK_SPEED_100GB_PAM4:
2410 		case BNXT_LINK_SPEED_200GB_PAM4:
2411 		case BNXT_LINK_SPEED_400GB_PAM4:
2412 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2413 			break;
2414 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2415 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2416 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2417 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2418 			break;
2419 		default:
2420 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2421 		}
2422 		return;
2423 	}
2424 	link_info->req_link_speed = link_info->force_link_speed;
2425 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2426 	if (link_info->force_pam4_link_speed) {
2427 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2428 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2429 	}
2430 }
2431 
bnxt_set_auto_speed(struct bnxt_link_info * link_info)2432 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2433 {
2434 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2435 
2436 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2437 		link_info->advertising = link_info->auto_link_speeds2;
2438 		return;
2439 	}
2440 	link_info->advertising = link_info->auto_link_speeds;
2441 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2442 }
2443 
bnxt_force_speed_updated(struct bnxt_link_info * link_info)2444 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2445 {
2446 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2447 
2448 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2449 		if (link_info->req_link_speed != link_info->force_link_speed2)
2450 			return true;
2451 		return false;
2452 	}
2453 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2454 	    link_info->req_link_speed != link_info->force_link_speed)
2455 		return true;
2456 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2457 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2458 		return true;
2459 	return false;
2460 }
2461 
bnxt_auto_speed_updated(struct bnxt_link_info * link_info)2462 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2463 {
2464 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2465 
2466 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2467 		if (link_info->advertising != link_info->auto_link_speeds2)
2468 			return true;
2469 		return false;
2470 	}
2471 	if (link_info->advertising != link_info->auto_link_speeds ||
2472 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2473 		return true;
2474 	return false;
2475 }
2476 
bnxt_bs_trace_avail(struct bnxt * bp,u16 type)2477 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2478 {
2479 	u32 flags = bp->ctx->ctx_arr[type].flags;
2480 
2481 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2482 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2483 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2484 }
2485 
bnxt_bs_trace_init(struct bnxt * bp,struct bnxt_ctx_mem_type * ctxm)2486 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2487 {
2488 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2489 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2490 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2491 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2492 	struct bnxt_bs_trace_info *bs_trace;
2493 	int last_pg;
2494 
2495 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2496 		return;
2497 
2498 	mem_size = ctxm->max_entries * ctxm->entry_size;
2499 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2500 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2501 
2502 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2503 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2504 
2505 	rmem = &ctx_pg[0].ring_mem;
2506 	bs_trace = &bp->bs_trace[trace_type];
2507 	bs_trace->ctx_type = ctxm->type;
2508 	bs_trace->trace_type = trace_type;
2509 	if (pages > MAX_CTX_PAGES) {
2510 		int last_pg_dir = rmem->nr_pages - 1;
2511 
2512 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2513 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2514 	} else {
2515 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2516 	}
2517 	bs_trace->magic_byte += magic_byte_offset;
2518 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2519 }
2520 
2521 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2522 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2523 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2524 
2525 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2526 	(((data2) &							\
2527 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2528 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2529 
2530 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2531 	((data2) &							\
2532 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2533 
2534 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2535 	(((data2) &							\
2536 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2537 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2538 
2539 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2540 	((data1) &							\
2541 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2542 
2543 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2544 	(((data1) &							\
2545 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2546 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2547 
2548 /* Return true if the workqueue has to be scheduled */
bnxt_event_error_report(struct bnxt * bp,u32 data1,u32 data2)2549 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2550 {
2551 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2552 
2553 	switch (err_type) {
2554 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2555 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2556 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2557 		break;
2558 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2559 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2560 		break;
2561 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2562 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2563 		break;
2564 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2565 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2566 		char *threshold_type;
2567 		bool notify = false;
2568 		char *dir_str;
2569 
2570 		switch (type) {
2571 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2572 			threshold_type = "warning";
2573 			break;
2574 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2575 			threshold_type = "critical";
2576 			break;
2577 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2578 			threshold_type = "fatal";
2579 			break;
2580 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2581 			threshold_type = "shutdown";
2582 			break;
2583 		default:
2584 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2585 			return false;
2586 		}
2587 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2588 			dir_str = "above";
2589 			notify = true;
2590 		} else {
2591 			dir_str = "below";
2592 		}
2593 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2594 			    dir_str, threshold_type);
2595 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2596 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2597 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2598 		if (notify) {
2599 			bp->thermal_threshold_type = type;
2600 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2601 			return true;
2602 		}
2603 		return false;
2604 	}
2605 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2606 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2607 		break;
2608 	default:
2609 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2610 			   err_type);
2611 		break;
2612 	}
2613 	return false;
2614 }
2615 
2616 #define BNXT_GET_EVENT_PORT(data)	\
2617 	((data) &			\
2618 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2619 
2620 #define BNXT_EVENT_RING_TYPE(data2)	\
2621 	((data2) &			\
2622 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2623 
2624 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2625 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2626 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2627 
2628 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2629 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2630 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2631 
2632 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2633 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2634 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2635 
2636 #define BNXT_PHC_BITS	48
2637 
bnxt_async_event_process(struct bnxt * bp,struct hwrm_async_event_cmpl * cmpl)2638 static int bnxt_async_event_process(struct bnxt *bp,
2639 				    struct hwrm_async_event_cmpl *cmpl)
2640 {
2641 	u16 event_id = le16_to_cpu(cmpl->event_id);
2642 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2643 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2644 
2645 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2646 		   event_id, data1, data2);
2647 
2648 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2649 	switch (event_id) {
2650 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2651 		struct bnxt_link_info *link_info = &bp->link_info;
2652 
2653 		if (BNXT_VF(bp))
2654 			goto async_event_process_exit;
2655 
2656 		/* print unsupported speed warning in forced speed mode only */
2657 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2658 		    (data1 & 0x20000)) {
2659 			u16 fw_speed = bnxt_get_force_speed(link_info);
2660 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2661 
2662 			if (speed != SPEED_UNKNOWN)
2663 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2664 					    speed);
2665 		}
2666 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2667 	}
2668 		fallthrough;
2669 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2670 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2671 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2672 		fallthrough;
2673 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2674 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2675 		break;
2676 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2677 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2678 		break;
2679 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2680 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2681 
2682 		if (BNXT_VF(bp))
2683 			break;
2684 
2685 		if (bp->pf.port_id != port_id)
2686 			break;
2687 
2688 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2689 		break;
2690 	}
2691 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2692 		if (BNXT_PF(bp))
2693 			goto async_event_process_exit;
2694 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2695 		break;
2696 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2697 		char *type_str = "Solicited";
2698 
2699 		if (!bp->fw_health)
2700 			goto async_event_process_exit;
2701 
2702 		bp->fw_reset_timestamp = jiffies;
2703 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2704 		if (!bp->fw_reset_min_dsecs)
2705 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2706 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2707 		if (!bp->fw_reset_max_dsecs)
2708 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2709 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2710 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2711 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2712 			type_str = "Fatal";
2713 			bp->fw_health->fatalities++;
2714 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2715 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2716 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2717 			type_str = "Non-fatal";
2718 			bp->fw_health->survivals++;
2719 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2720 		}
2721 		netif_warn(bp, hw, bp->dev,
2722 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2723 			   type_str, data1, data2,
2724 			   bp->fw_reset_min_dsecs * 100,
2725 			   bp->fw_reset_max_dsecs * 100);
2726 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2727 		break;
2728 	}
2729 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2730 		struct bnxt_fw_health *fw_health = bp->fw_health;
2731 		char *status_desc = "healthy";
2732 		u32 status;
2733 
2734 		if (!fw_health)
2735 			goto async_event_process_exit;
2736 
2737 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2738 			fw_health->enabled = false;
2739 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2740 			break;
2741 		}
2742 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2743 		fw_health->tmr_multiplier =
2744 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2745 				     bp->current_interval * 10);
2746 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2747 		if (!fw_health->enabled)
2748 			fw_health->last_fw_heartbeat =
2749 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2750 		fw_health->last_fw_reset_cnt =
2751 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2752 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2753 		if (status != BNXT_FW_STATUS_HEALTHY)
2754 			status_desc = "unhealthy";
2755 		netif_info(bp, drv, bp->dev,
2756 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2757 			   fw_health->primary ? "primary" : "backup", status,
2758 			   status_desc, fw_health->last_fw_reset_cnt);
2759 		if (!fw_health->enabled) {
2760 			/* Make sure tmr_counter is set and visible to
2761 			 * bnxt_health_check() before setting enabled to true.
2762 			 */
2763 			smp_wmb();
2764 			fw_health->enabled = true;
2765 		}
2766 		goto async_event_process_exit;
2767 	}
2768 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2769 		netif_notice(bp, hw, bp->dev,
2770 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2771 			     data1, data2);
2772 		goto async_event_process_exit;
2773 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2774 		struct bnxt_rx_ring_info *rxr;
2775 		u16 grp_idx;
2776 
2777 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2778 			goto async_event_process_exit;
2779 
2780 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2781 			    BNXT_EVENT_RING_TYPE(data2), data1);
2782 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2783 			goto async_event_process_exit;
2784 
2785 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2786 		if (grp_idx == INVALID_HW_RING_ID) {
2787 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2788 				    data1);
2789 			goto async_event_process_exit;
2790 		}
2791 		rxr = bp->bnapi[grp_idx]->rx_ring;
2792 		bnxt_sched_reset_rxr(bp, rxr);
2793 		goto async_event_process_exit;
2794 	}
2795 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2796 		struct bnxt_fw_health *fw_health = bp->fw_health;
2797 
2798 		netif_notice(bp, hw, bp->dev,
2799 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2800 			     data1, data2);
2801 		if (fw_health) {
2802 			fw_health->echo_req_data1 = data1;
2803 			fw_health->echo_req_data2 = data2;
2804 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2805 			break;
2806 		}
2807 		goto async_event_process_exit;
2808 	}
2809 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2810 		bnxt_ptp_pps_event(bp, data1, data2);
2811 		goto async_event_process_exit;
2812 	}
2813 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2814 		if (bnxt_event_error_report(bp, data1, data2))
2815 			break;
2816 		goto async_event_process_exit;
2817 	}
2818 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2819 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2820 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2821 			if (BNXT_PTP_USE_RTC(bp)) {
2822 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2823 				unsigned long flags;
2824 				u64 ns;
2825 
2826 				if (!ptp)
2827 					goto async_event_process_exit;
2828 
2829 				bnxt_ptp_update_current_time(bp);
2830 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2831 				       BNXT_PHC_BITS) | ptp->current_time);
2832 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2833 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2834 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2835 			}
2836 			break;
2837 		}
2838 		goto async_event_process_exit;
2839 	}
2840 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2841 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2842 
2843 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2844 		goto async_event_process_exit;
2845 	}
2846 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2847 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2848 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2849 
2850 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2851 		goto async_event_process_exit;
2852 	}
2853 	default:
2854 		goto async_event_process_exit;
2855 	}
2856 	__bnxt_queue_sp_work(bp);
2857 async_event_process_exit:
2858 	return 0;
2859 }
2860 
bnxt_hwrm_handler(struct bnxt * bp,struct tx_cmp * txcmp)2861 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2862 {
2863 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2864 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2865 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2866 				(struct hwrm_fwd_req_cmpl *)txcmp;
2867 
2868 	switch (cmpl_type) {
2869 	case CMPL_BASE_TYPE_HWRM_DONE:
2870 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2871 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2872 		break;
2873 
2874 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2875 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2876 
2877 		if ((vf_id < bp->pf.first_vf_id) ||
2878 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2879 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2880 				   vf_id);
2881 			return -EINVAL;
2882 		}
2883 
2884 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2885 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2886 		break;
2887 
2888 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2889 		bnxt_async_event_process(bp,
2890 					 (struct hwrm_async_event_cmpl *)txcmp);
2891 		break;
2892 
2893 	default:
2894 		break;
2895 	}
2896 
2897 	return 0;
2898 }
2899 
bnxt_msix(int irq,void * dev_instance)2900 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2901 {
2902 	struct bnxt_napi *bnapi = dev_instance;
2903 	struct bnxt *bp = bnapi->bp;
2904 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2905 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2906 
2907 	cpr->event_ctr++;
2908 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2909 	napi_schedule(&bnapi->napi);
2910 	return IRQ_HANDLED;
2911 }
2912 
bnxt_has_work(struct bnxt * bp,struct bnxt_cp_ring_info * cpr)2913 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2914 {
2915 	u32 raw_cons = cpr->cp_raw_cons;
2916 	u16 cons = RING_CMP(raw_cons);
2917 	struct tx_cmp *txcmp;
2918 
2919 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2920 
2921 	return TX_CMP_VALID(txcmp, raw_cons);
2922 }
2923 
__bnxt_poll_work(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,int budget)2924 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2925 			    int budget)
2926 {
2927 	struct bnxt_napi *bnapi = cpr->bnapi;
2928 	u32 raw_cons = cpr->cp_raw_cons;
2929 	u32 cons;
2930 	int rx_pkts = 0;
2931 	u8 event = 0;
2932 	struct tx_cmp *txcmp;
2933 
2934 	cpr->has_more_work = 0;
2935 	cpr->had_work_done = 1;
2936 	while (1) {
2937 		u8 cmp_type;
2938 		int rc;
2939 
2940 		cons = RING_CMP(raw_cons);
2941 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2942 
2943 		if (!TX_CMP_VALID(txcmp, raw_cons))
2944 			break;
2945 
2946 		/* The valid test of the entry must be done first before
2947 		 * reading any further.
2948 		 */
2949 		dma_rmb();
2950 		cmp_type = TX_CMP_TYPE(txcmp);
2951 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
2952 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
2953 			u32 opaque = txcmp->tx_cmp_opaque;
2954 			struct bnxt_tx_ring_info *txr;
2955 			u16 tx_freed;
2956 
2957 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
2958 			event |= BNXT_TX_CMP_EVENT;
2959 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
2960 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
2961 			else
2962 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
2963 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
2964 				   bp->tx_ring_mask;
2965 			/* return full budget so NAPI will complete. */
2966 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
2967 				rx_pkts = budget;
2968 				raw_cons = NEXT_RAW_CMP(raw_cons);
2969 				if (budget)
2970 					cpr->has_more_work = 1;
2971 				break;
2972 			}
2973 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
2974 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
2975 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
2976 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2977 			if (likely(budget))
2978 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2979 			else
2980 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2981 							   &event);
2982 			if (likely(rc >= 0))
2983 				rx_pkts += rc;
2984 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2985 			 * the NAPI budget.  Otherwise, we may potentially loop
2986 			 * here forever if we consistently cannot allocate
2987 			 * buffers.
2988 			 */
2989 			else if (rc == -ENOMEM && budget)
2990 				rx_pkts++;
2991 			else if (rc == -EBUSY)	/* partial completion */
2992 				break;
2993 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
2994 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
2995 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
2996 			bnxt_hwrm_handler(bp, txcmp);
2997 		}
2998 		raw_cons = NEXT_RAW_CMP(raw_cons);
2999 
3000 		if (rx_pkts && rx_pkts == budget) {
3001 			cpr->has_more_work = 1;
3002 			break;
3003 		}
3004 	}
3005 
3006 	if (event & BNXT_REDIRECT_EVENT) {
3007 		xdp_do_flush();
3008 		event &= ~BNXT_REDIRECT_EVENT;
3009 	}
3010 
3011 	if (event & BNXT_TX_EVENT) {
3012 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3013 		u16 prod = txr->tx_prod;
3014 
3015 		/* Sync BD data before updating doorbell */
3016 		wmb();
3017 
3018 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3019 		event &= ~BNXT_TX_EVENT;
3020 	}
3021 
3022 	cpr->cp_raw_cons = raw_cons;
3023 	bnapi->events |= event;
3024 	return rx_pkts;
3025 }
3026 
__bnxt_poll_work_done(struct bnxt * bp,struct bnxt_napi * bnapi,int budget)3027 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3028 				  int budget)
3029 {
3030 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3031 		bnapi->tx_int(bp, bnapi, budget);
3032 
3033 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3034 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3035 
3036 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3037 		bnapi->events &= ~BNXT_RX_EVENT;
3038 	}
3039 	if (bnapi->events & BNXT_AGG_EVENT) {
3040 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3041 
3042 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3043 		bnapi->events &= ~BNXT_AGG_EVENT;
3044 	}
3045 }
3046 
bnxt_poll_work(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,int budget)3047 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3048 			  int budget)
3049 {
3050 	struct bnxt_napi *bnapi = cpr->bnapi;
3051 	int rx_pkts;
3052 
3053 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3054 
3055 	/* ACK completion ring before freeing tx ring and producing new
3056 	 * buffers in rx/agg rings to prevent overflowing the completion
3057 	 * ring.
3058 	 */
3059 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3060 
3061 	__bnxt_poll_work_done(bp, bnapi, budget);
3062 	return rx_pkts;
3063 }
3064 
bnxt_poll_nitroa0(struct napi_struct * napi,int budget)3065 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3066 {
3067 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3068 	struct bnxt *bp = bnapi->bp;
3069 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3070 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3071 	struct tx_cmp *txcmp;
3072 	struct rx_cmp_ext *rxcmp1;
3073 	u32 cp_cons, tmp_raw_cons;
3074 	u32 raw_cons = cpr->cp_raw_cons;
3075 	bool flush_xdp = false;
3076 	u32 rx_pkts = 0;
3077 	u8 event = 0;
3078 
3079 	while (1) {
3080 		int rc;
3081 
3082 		cp_cons = RING_CMP(raw_cons);
3083 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3084 
3085 		if (!TX_CMP_VALID(txcmp, raw_cons))
3086 			break;
3087 
3088 		/* The valid test of the entry must be done first before
3089 		 * reading any further.
3090 		 */
3091 		dma_rmb();
3092 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3093 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3094 			cp_cons = RING_CMP(tmp_raw_cons);
3095 			rxcmp1 = (struct rx_cmp_ext *)
3096 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3097 
3098 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3099 				break;
3100 
3101 			/* force an error to recycle the buffer */
3102 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3103 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3104 
3105 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3106 			if (likely(rc == -EIO) && budget)
3107 				rx_pkts++;
3108 			else if (rc == -EBUSY)	/* partial completion */
3109 				break;
3110 			if (event & BNXT_REDIRECT_EVENT)
3111 				flush_xdp = true;
3112 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3113 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3114 			bnxt_hwrm_handler(bp, txcmp);
3115 		} else {
3116 			netdev_err(bp->dev,
3117 				   "Invalid completion received on special ring\n");
3118 		}
3119 		raw_cons = NEXT_RAW_CMP(raw_cons);
3120 
3121 		if (rx_pkts == budget)
3122 			break;
3123 	}
3124 
3125 	cpr->cp_raw_cons = raw_cons;
3126 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3127 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3128 
3129 	if (event & BNXT_AGG_EVENT)
3130 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3131 	if (flush_xdp)
3132 		xdp_do_flush();
3133 
3134 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3135 		napi_complete_done(napi, rx_pkts);
3136 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3137 	}
3138 	return rx_pkts;
3139 }
3140 
bnxt_poll(struct napi_struct * napi,int budget)3141 static int bnxt_poll(struct napi_struct *napi, int budget)
3142 {
3143 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3144 	struct bnxt *bp = bnapi->bp;
3145 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3146 	int work_done = 0;
3147 
3148 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3149 		napi_complete(napi);
3150 		return 0;
3151 	}
3152 	while (1) {
3153 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3154 
3155 		if (work_done >= budget) {
3156 			if (!budget)
3157 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3158 			break;
3159 		}
3160 
3161 		if (!bnxt_has_work(bp, cpr)) {
3162 			if (napi_complete_done(napi, work_done))
3163 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3164 			break;
3165 		}
3166 	}
3167 	if (bp->flags & BNXT_FLAG_DIM) {
3168 		struct dim_sample dim_sample = {};
3169 
3170 		dim_update_sample(cpr->event_ctr,
3171 				  cpr->rx_packets,
3172 				  cpr->rx_bytes,
3173 				  &dim_sample);
3174 		net_dim(&cpr->dim, &dim_sample);
3175 	}
3176 	return work_done;
3177 }
3178 
__bnxt_poll_cqs(struct bnxt * bp,struct bnxt_napi * bnapi,int budget)3179 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3180 {
3181 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3182 	int i, work_done = 0;
3183 
3184 	for (i = 0; i < cpr->cp_ring_count; i++) {
3185 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3186 
3187 		if (cpr2->had_nqe_notify) {
3188 			work_done += __bnxt_poll_work(bp, cpr2,
3189 						      budget - work_done);
3190 			cpr->has_more_work |= cpr2->has_more_work;
3191 		}
3192 	}
3193 	return work_done;
3194 }
3195 
__bnxt_poll_cqs_done(struct bnxt * bp,struct bnxt_napi * bnapi,u64 dbr_type,int budget)3196 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3197 				 u64 dbr_type, int budget)
3198 {
3199 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3200 	int i;
3201 
3202 	for (i = 0; i < cpr->cp_ring_count; i++) {
3203 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3204 		struct bnxt_db_info *db;
3205 
3206 		if (cpr2->had_work_done) {
3207 			u32 tgl = 0;
3208 
3209 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3210 				cpr2->had_nqe_notify = 0;
3211 				tgl = cpr2->toggle;
3212 			}
3213 			db = &cpr2->cp_db;
3214 			bnxt_writeq(bp,
3215 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3216 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3217 				    db->doorbell);
3218 			cpr2->had_work_done = 0;
3219 		}
3220 	}
3221 	__bnxt_poll_work_done(bp, bnapi, budget);
3222 }
3223 
bnxt_poll_p5(struct napi_struct * napi,int budget)3224 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3225 {
3226 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3227 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3228 	struct bnxt_cp_ring_info *cpr_rx;
3229 	u32 raw_cons = cpr->cp_raw_cons;
3230 	struct bnxt *bp = bnapi->bp;
3231 	struct nqe_cn *nqcmp;
3232 	int work_done = 0;
3233 	u32 cons;
3234 
3235 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3236 		napi_complete(napi);
3237 		return 0;
3238 	}
3239 	if (cpr->has_more_work) {
3240 		cpr->has_more_work = 0;
3241 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3242 	}
3243 	while (1) {
3244 		u16 type;
3245 
3246 		cons = RING_CMP(raw_cons);
3247 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3248 
3249 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3250 			if (cpr->has_more_work)
3251 				break;
3252 
3253 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3254 					     budget);
3255 			cpr->cp_raw_cons = raw_cons;
3256 			if (napi_complete_done(napi, work_done))
3257 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3258 						  cpr->cp_raw_cons);
3259 			goto poll_done;
3260 		}
3261 
3262 		/* The valid test of the entry must be done first before
3263 		 * reading any further.
3264 		 */
3265 		dma_rmb();
3266 
3267 		type = le16_to_cpu(nqcmp->type);
3268 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3269 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3270 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3271 			struct bnxt_cp_ring_info *cpr2;
3272 
3273 			/* No more budget for RX work */
3274 			if (budget && work_done >= budget &&
3275 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3276 				break;
3277 
3278 			idx = BNXT_NQ_HDL_IDX(idx);
3279 			cpr2 = &cpr->cp_ring_arr[idx];
3280 			cpr2->had_nqe_notify = 1;
3281 			cpr2->toggle = NQE_CN_TOGGLE(type);
3282 			work_done += __bnxt_poll_work(bp, cpr2,
3283 						      budget - work_done);
3284 			cpr->has_more_work |= cpr2->has_more_work;
3285 		} else {
3286 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3287 		}
3288 		raw_cons = NEXT_RAW_CMP(raw_cons);
3289 	}
3290 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3291 	if (raw_cons != cpr->cp_raw_cons) {
3292 		cpr->cp_raw_cons = raw_cons;
3293 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3294 	}
3295 poll_done:
3296 	cpr_rx = &cpr->cp_ring_arr[0];
3297 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3298 	    (bp->flags & BNXT_FLAG_DIM)) {
3299 		struct dim_sample dim_sample = {};
3300 
3301 		dim_update_sample(cpr->event_ctr,
3302 				  cpr_rx->rx_packets,
3303 				  cpr_rx->rx_bytes,
3304 				  &dim_sample);
3305 		net_dim(&cpr->dim, &dim_sample);
3306 	}
3307 	return work_done;
3308 }
3309 
bnxt_free_tx_skbs(struct bnxt * bp)3310 static void bnxt_free_tx_skbs(struct bnxt *bp)
3311 {
3312 	int i, max_idx;
3313 	struct pci_dev *pdev = bp->pdev;
3314 
3315 	if (!bp->tx_ring)
3316 		return;
3317 
3318 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3319 	for (i = 0; i < bp->tx_nr_rings; i++) {
3320 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3321 		int j;
3322 
3323 		if (!txr->tx_buf_ring)
3324 			continue;
3325 
3326 		for (j = 0; j < max_idx;) {
3327 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
3328 			struct sk_buff *skb;
3329 			int k, last;
3330 
3331 			if (i < bp->tx_nr_rings_xdp &&
3332 			    tx_buf->action == XDP_REDIRECT) {
3333 				dma_unmap_single(&pdev->dev,
3334 					dma_unmap_addr(tx_buf, mapping),
3335 					dma_unmap_len(tx_buf, len),
3336 					DMA_TO_DEVICE);
3337 				xdp_return_frame(tx_buf->xdpf);
3338 				tx_buf->action = 0;
3339 				tx_buf->xdpf = NULL;
3340 				j++;
3341 				continue;
3342 			}
3343 
3344 			skb = tx_buf->skb;
3345 			if (!skb) {
3346 				j++;
3347 				continue;
3348 			}
3349 
3350 			tx_buf->skb = NULL;
3351 
3352 			if (tx_buf->is_push) {
3353 				dev_kfree_skb(skb);
3354 				j += 2;
3355 				continue;
3356 			}
3357 
3358 			dma_unmap_single(&pdev->dev,
3359 					 dma_unmap_addr(tx_buf, mapping),
3360 					 skb_headlen(skb),
3361 					 DMA_TO_DEVICE);
3362 
3363 			last = tx_buf->nr_frags;
3364 			j += 2;
3365 			for (k = 0; k < last; k++, j++) {
3366 				int ring_idx = j & bp->tx_ring_mask;
3367 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
3368 
3369 				tx_buf = &txr->tx_buf_ring[ring_idx];
3370 				dma_unmap_page(
3371 					&pdev->dev,
3372 					dma_unmap_addr(tx_buf, mapping),
3373 					skb_frag_size(frag), DMA_TO_DEVICE);
3374 			}
3375 			dev_kfree_skb(skb);
3376 		}
3377 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
3378 	}
3379 }
3380 
bnxt_free_one_rx_ring(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)3381 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3382 {
3383 	int i, max_idx;
3384 
3385 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3386 
3387 	for (i = 0; i < max_idx; i++) {
3388 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3389 		void *data = rx_buf->data;
3390 
3391 		if (!data)
3392 			continue;
3393 
3394 		rx_buf->data = NULL;
3395 		if (BNXT_RX_PAGE_MODE(bp))
3396 			page_pool_recycle_direct(rxr->page_pool, data);
3397 		else
3398 			page_pool_free_va(rxr->head_pool, data, true);
3399 	}
3400 }
3401 
bnxt_free_one_rx_agg_ring(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)3402 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3403 {
3404 	int i, max_idx;
3405 
3406 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3407 
3408 	for (i = 0; i < max_idx; i++) {
3409 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3410 		struct page *page = rx_agg_buf->page;
3411 
3412 		if (!page)
3413 			continue;
3414 
3415 		rx_agg_buf->page = NULL;
3416 		__clear_bit(i, rxr->rx_agg_bmap);
3417 
3418 		page_pool_recycle_direct(rxr->page_pool, page);
3419 	}
3420 }
3421 
bnxt_free_one_tpa_info_data(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)3422 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3423 					struct bnxt_rx_ring_info *rxr)
3424 {
3425 	int i;
3426 
3427 	for (i = 0; i < bp->max_tpa; i++) {
3428 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3429 		u8 *data = tpa_info->data;
3430 
3431 		if (!data)
3432 			continue;
3433 
3434 		tpa_info->data = NULL;
3435 		page_pool_free_va(rxr->head_pool, data, false);
3436 	}
3437 }
3438 
bnxt_free_one_rx_ring_skbs(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)3439 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3440 				       struct bnxt_rx_ring_info *rxr)
3441 {
3442 	struct bnxt_tpa_idx_map *map;
3443 
3444 	if (!rxr->rx_tpa)
3445 		goto skip_rx_tpa_free;
3446 
3447 	bnxt_free_one_tpa_info_data(bp, rxr);
3448 
3449 skip_rx_tpa_free:
3450 	if (!rxr->rx_buf_ring)
3451 		goto skip_rx_buf_free;
3452 
3453 	bnxt_free_one_rx_ring(bp, rxr);
3454 
3455 skip_rx_buf_free:
3456 	if (!rxr->rx_agg_ring)
3457 		goto skip_rx_agg_free;
3458 
3459 	bnxt_free_one_rx_agg_ring(bp, rxr);
3460 
3461 skip_rx_agg_free:
3462 	map = rxr->rx_tpa_idx_map;
3463 	if (map)
3464 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3465 }
3466 
bnxt_free_rx_skbs(struct bnxt * bp)3467 static void bnxt_free_rx_skbs(struct bnxt *bp)
3468 {
3469 	int i;
3470 
3471 	if (!bp->rx_ring)
3472 		return;
3473 
3474 	for (i = 0; i < bp->rx_nr_rings; i++)
3475 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3476 }
3477 
bnxt_free_skbs(struct bnxt * bp)3478 static void bnxt_free_skbs(struct bnxt *bp)
3479 {
3480 	bnxt_free_tx_skbs(bp);
3481 	bnxt_free_rx_skbs(bp);
3482 }
3483 
bnxt_init_ctx_mem(struct bnxt_ctx_mem_type * ctxm,void * p,int len)3484 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3485 {
3486 	u8 init_val = ctxm->init_value;
3487 	u16 offset = ctxm->init_offset;
3488 	u8 *p2 = p;
3489 	int i;
3490 
3491 	if (!init_val)
3492 		return;
3493 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3494 		memset(p, init_val, len);
3495 		return;
3496 	}
3497 	for (i = 0; i < len; i += ctxm->entry_size)
3498 		*(p2 + i + offset) = init_val;
3499 }
3500 
__bnxt_copy_ring(struct bnxt * bp,struct bnxt_ring_mem_info * rmem,void * buf,size_t offset,size_t head,size_t tail)3501 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3502 			       void *buf, size_t offset, size_t head,
3503 			       size_t tail)
3504 {
3505 	int i, head_page, start_idx, source_offset;
3506 	size_t len, rem_len, total_len, max_bytes;
3507 
3508 	head_page = head / rmem->page_size;
3509 	source_offset = head % rmem->page_size;
3510 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3511 	if (!total_len)
3512 		total_len = MAX_CTX_BYTES;
3513 	start_idx = head_page % MAX_CTX_PAGES;
3514 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3515 		    source_offset;
3516 	total_len = min(total_len, max_bytes);
3517 	rem_len = total_len;
3518 
3519 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3520 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3521 		if (buf)
3522 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3523 			       len);
3524 		offset += len;
3525 		rem_len -= len;
3526 	}
3527 	return total_len;
3528 }
3529 
bnxt_free_ring(struct bnxt * bp,struct bnxt_ring_mem_info * rmem)3530 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3531 {
3532 	struct pci_dev *pdev = bp->pdev;
3533 	int i;
3534 
3535 	if (!rmem->pg_arr)
3536 		goto skip_pages;
3537 
3538 	for (i = 0; i < rmem->nr_pages; i++) {
3539 		if (!rmem->pg_arr[i])
3540 			continue;
3541 
3542 		dma_free_coherent(&pdev->dev, rmem->page_size,
3543 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3544 
3545 		rmem->pg_arr[i] = NULL;
3546 	}
3547 skip_pages:
3548 	if (rmem->pg_tbl) {
3549 		size_t pg_tbl_size = rmem->nr_pages * 8;
3550 
3551 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3552 			pg_tbl_size = rmem->page_size;
3553 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3554 				  rmem->pg_tbl, rmem->pg_tbl_map);
3555 		rmem->pg_tbl = NULL;
3556 	}
3557 	if (rmem->vmem_size && *rmem->vmem) {
3558 		vfree(*rmem->vmem);
3559 		*rmem->vmem = NULL;
3560 	}
3561 }
3562 
bnxt_alloc_ring(struct bnxt * bp,struct bnxt_ring_mem_info * rmem)3563 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3564 {
3565 	struct pci_dev *pdev = bp->pdev;
3566 	u64 valid_bit = 0;
3567 	int i;
3568 
3569 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3570 		valid_bit = PTU_PTE_VALID;
3571 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3572 		size_t pg_tbl_size = rmem->nr_pages * 8;
3573 
3574 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3575 			pg_tbl_size = rmem->page_size;
3576 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3577 						  &rmem->pg_tbl_map,
3578 						  GFP_KERNEL);
3579 		if (!rmem->pg_tbl)
3580 			return -ENOMEM;
3581 	}
3582 
3583 	for (i = 0; i < rmem->nr_pages; i++) {
3584 		u64 extra_bits = valid_bit;
3585 
3586 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3587 						     rmem->page_size,
3588 						     &rmem->dma_arr[i],
3589 						     GFP_KERNEL);
3590 		if (!rmem->pg_arr[i])
3591 			return -ENOMEM;
3592 
3593 		if (rmem->ctx_mem)
3594 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3595 					  rmem->page_size);
3596 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3597 			if (i == rmem->nr_pages - 2 &&
3598 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3599 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3600 			else if (i == rmem->nr_pages - 1 &&
3601 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3602 				extra_bits |= PTU_PTE_LAST;
3603 			rmem->pg_tbl[i] =
3604 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3605 		}
3606 	}
3607 
3608 	if (rmem->vmem_size) {
3609 		*rmem->vmem = vzalloc(rmem->vmem_size);
3610 		if (!(*rmem->vmem))
3611 			return -ENOMEM;
3612 	}
3613 	return 0;
3614 }
3615 
bnxt_free_one_tpa_info(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)3616 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3617 				   struct bnxt_rx_ring_info *rxr)
3618 {
3619 	int i;
3620 
3621 	kfree(rxr->rx_tpa_idx_map);
3622 	rxr->rx_tpa_idx_map = NULL;
3623 	if (rxr->rx_tpa) {
3624 		for (i = 0; i < bp->max_tpa; i++) {
3625 			kfree(rxr->rx_tpa[i].agg_arr);
3626 			rxr->rx_tpa[i].agg_arr = NULL;
3627 		}
3628 	}
3629 	kfree(rxr->rx_tpa);
3630 	rxr->rx_tpa = NULL;
3631 }
3632 
bnxt_free_tpa_info(struct bnxt * bp)3633 static void bnxt_free_tpa_info(struct bnxt *bp)
3634 {
3635 	int i;
3636 
3637 	for (i = 0; i < bp->rx_nr_rings; i++) {
3638 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3639 
3640 		bnxt_free_one_tpa_info(bp, rxr);
3641 	}
3642 }
3643 
bnxt_alloc_one_tpa_info(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)3644 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3645 				   struct bnxt_rx_ring_info *rxr)
3646 {
3647 	struct rx_agg_cmp *agg;
3648 	int i;
3649 
3650 	rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3651 			      GFP_KERNEL);
3652 	if (!rxr->rx_tpa)
3653 		return -ENOMEM;
3654 
3655 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3656 		return 0;
3657 	for (i = 0; i < bp->max_tpa; i++) {
3658 		agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3659 		if (!agg)
3660 			return -ENOMEM;
3661 		rxr->rx_tpa[i].agg_arr = agg;
3662 	}
3663 	rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3664 				      GFP_KERNEL);
3665 	if (!rxr->rx_tpa_idx_map)
3666 		return -ENOMEM;
3667 
3668 	return 0;
3669 }
3670 
bnxt_alloc_tpa_info(struct bnxt * bp)3671 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3672 {
3673 	int i, rc;
3674 
3675 	bp->max_tpa = MAX_TPA;
3676 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3677 		if (!bp->max_tpa_v2)
3678 			return 0;
3679 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3680 	}
3681 
3682 	for (i = 0; i < bp->rx_nr_rings; i++) {
3683 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3684 
3685 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3686 		if (rc)
3687 			return rc;
3688 	}
3689 	return 0;
3690 }
3691 
bnxt_free_rx_rings(struct bnxt * bp)3692 static void bnxt_free_rx_rings(struct bnxt *bp)
3693 {
3694 	int i;
3695 
3696 	if (!bp->rx_ring)
3697 		return;
3698 
3699 	bnxt_free_tpa_info(bp);
3700 	for (i = 0; i < bp->rx_nr_rings; i++) {
3701 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3702 		struct bnxt_ring_struct *ring;
3703 
3704 		if (rxr->xdp_prog)
3705 			bpf_prog_put(rxr->xdp_prog);
3706 
3707 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3708 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3709 
3710 		page_pool_destroy(rxr->page_pool);
3711 		if (bnxt_separate_head_pool())
3712 			page_pool_destroy(rxr->head_pool);
3713 		rxr->page_pool = rxr->head_pool = NULL;
3714 
3715 		kfree(rxr->rx_agg_bmap);
3716 		rxr->rx_agg_bmap = NULL;
3717 
3718 		ring = &rxr->rx_ring_struct;
3719 		bnxt_free_ring(bp, &ring->ring_mem);
3720 
3721 		ring = &rxr->rx_agg_ring_struct;
3722 		bnxt_free_ring(bp, &ring->ring_mem);
3723 	}
3724 }
3725 
bnxt_alloc_rx_page_pool(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,int numa_node)3726 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3727 				   struct bnxt_rx_ring_info *rxr,
3728 				   int numa_node)
3729 {
3730 	struct page_pool_params pp = { 0 };
3731 	struct page_pool *pool;
3732 
3733 	pp.pool_size = bp->rx_agg_ring_size;
3734 	if (BNXT_RX_PAGE_MODE(bp))
3735 		pp.pool_size += bp->rx_ring_size;
3736 	pp.nid = numa_node;
3737 	pp.napi = &rxr->bnapi->napi;
3738 	pp.netdev = bp->dev;
3739 	pp.dev = &bp->pdev->dev;
3740 	pp.dma_dir = bp->rx_dir;
3741 	pp.max_len = PAGE_SIZE;
3742 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3743 
3744 	pool = page_pool_create(&pp);
3745 	if (IS_ERR(pool))
3746 		return PTR_ERR(pool);
3747 	rxr->page_pool = pool;
3748 
3749 	if (bnxt_separate_head_pool()) {
3750 		pp.pool_size = max(bp->rx_ring_size, 1024);
3751 		pool = page_pool_create(&pp);
3752 		if (IS_ERR(pool))
3753 			goto err_destroy_pp;
3754 	}
3755 	rxr->head_pool = pool;
3756 
3757 	return 0;
3758 
3759 err_destroy_pp:
3760 	page_pool_destroy(rxr->page_pool);
3761 	rxr->page_pool = NULL;
3762 	return PTR_ERR(pool);
3763 }
3764 
bnxt_alloc_rx_agg_bmap(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)3765 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3766 {
3767 	u16 mem_size;
3768 
3769 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3770 	mem_size = rxr->rx_agg_bmap_size / 8;
3771 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3772 	if (!rxr->rx_agg_bmap)
3773 		return -ENOMEM;
3774 
3775 	return 0;
3776 }
3777 
bnxt_alloc_rx_rings(struct bnxt * bp)3778 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3779 {
3780 	int numa_node = dev_to_node(&bp->pdev->dev);
3781 	int i, rc = 0, agg_rings = 0, cpu;
3782 
3783 	if (!bp->rx_ring)
3784 		return -ENOMEM;
3785 
3786 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3787 		agg_rings = 1;
3788 
3789 	for (i = 0; i < bp->rx_nr_rings; i++) {
3790 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3791 		struct bnxt_ring_struct *ring;
3792 		int cpu_node;
3793 
3794 		ring = &rxr->rx_ring_struct;
3795 
3796 		cpu = cpumask_local_spread(i, numa_node);
3797 		cpu_node = cpu_to_node(cpu);
3798 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3799 			   i, cpu_node);
3800 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3801 		if (rc)
3802 			return rc;
3803 
3804 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3805 		if (rc < 0)
3806 			return rc;
3807 
3808 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3809 						MEM_TYPE_PAGE_POOL,
3810 						rxr->page_pool);
3811 		if (rc) {
3812 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3813 			return rc;
3814 		}
3815 
3816 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3817 		if (rc)
3818 			return rc;
3819 
3820 		ring->grp_idx = i;
3821 		if (agg_rings) {
3822 			ring = &rxr->rx_agg_ring_struct;
3823 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3824 			if (rc)
3825 				return rc;
3826 
3827 			ring->grp_idx = i;
3828 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3829 			if (rc)
3830 				return rc;
3831 		}
3832 	}
3833 	if (bp->flags & BNXT_FLAG_TPA)
3834 		rc = bnxt_alloc_tpa_info(bp);
3835 	return rc;
3836 }
3837 
bnxt_free_tx_rings(struct bnxt * bp)3838 static void bnxt_free_tx_rings(struct bnxt *bp)
3839 {
3840 	int i;
3841 	struct pci_dev *pdev = bp->pdev;
3842 
3843 	if (!bp->tx_ring)
3844 		return;
3845 
3846 	for (i = 0; i < bp->tx_nr_rings; i++) {
3847 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3848 		struct bnxt_ring_struct *ring;
3849 
3850 		if (txr->tx_push) {
3851 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3852 					  txr->tx_push, txr->tx_push_mapping);
3853 			txr->tx_push = NULL;
3854 		}
3855 
3856 		ring = &txr->tx_ring_struct;
3857 
3858 		bnxt_free_ring(bp, &ring->ring_mem);
3859 	}
3860 }
3861 
3862 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3863 	((tc) * (bp)->tx_nr_rings_per_tc)
3864 
3865 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3866 	((tx) % (bp)->tx_nr_rings_per_tc)
3867 
3868 #define BNXT_RING_TO_TC(bp, tx)		\
3869 	((tx) / (bp)->tx_nr_rings_per_tc)
3870 
bnxt_alloc_tx_rings(struct bnxt * bp)3871 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3872 {
3873 	int i, j, rc;
3874 	struct pci_dev *pdev = bp->pdev;
3875 
3876 	bp->tx_push_size = 0;
3877 	if (bp->tx_push_thresh) {
3878 		int push_size;
3879 
3880 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3881 					bp->tx_push_thresh);
3882 
3883 		if (push_size > 256) {
3884 			push_size = 0;
3885 			bp->tx_push_thresh = 0;
3886 		}
3887 
3888 		bp->tx_push_size = push_size;
3889 	}
3890 
3891 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3892 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3893 		struct bnxt_ring_struct *ring;
3894 		u8 qidx;
3895 
3896 		ring = &txr->tx_ring_struct;
3897 
3898 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3899 		if (rc)
3900 			return rc;
3901 
3902 		ring->grp_idx = txr->bnapi->index;
3903 		if (bp->tx_push_size) {
3904 			dma_addr_t mapping;
3905 
3906 			/* One pre-allocated DMA buffer to backup
3907 			 * TX push operation
3908 			 */
3909 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3910 						bp->tx_push_size,
3911 						&txr->tx_push_mapping,
3912 						GFP_KERNEL);
3913 
3914 			if (!txr->tx_push)
3915 				return -ENOMEM;
3916 
3917 			mapping = txr->tx_push_mapping +
3918 				sizeof(struct tx_push_bd);
3919 			txr->data_mapping = cpu_to_le64(mapping);
3920 		}
3921 		qidx = bp->tc_to_qidx[j];
3922 		ring->queue_id = bp->q_info[qidx].queue_id;
3923 		spin_lock_init(&txr->xdp_tx_lock);
3924 		if (i < bp->tx_nr_rings_xdp)
3925 			continue;
3926 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
3927 			j++;
3928 	}
3929 	return 0;
3930 }
3931 
bnxt_free_cp_arrays(struct bnxt_cp_ring_info * cpr)3932 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3933 {
3934 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3935 
3936 	kfree(cpr->cp_desc_ring);
3937 	cpr->cp_desc_ring = NULL;
3938 	ring->ring_mem.pg_arr = NULL;
3939 	kfree(cpr->cp_desc_mapping);
3940 	cpr->cp_desc_mapping = NULL;
3941 	ring->ring_mem.dma_arr = NULL;
3942 }
3943 
bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info * cpr,int n)3944 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3945 {
3946 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3947 	if (!cpr->cp_desc_ring)
3948 		return -ENOMEM;
3949 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3950 				       GFP_KERNEL);
3951 	if (!cpr->cp_desc_mapping)
3952 		return -ENOMEM;
3953 	return 0;
3954 }
3955 
bnxt_free_all_cp_arrays(struct bnxt * bp)3956 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3957 {
3958 	int i;
3959 
3960 	if (!bp->bnapi)
3961 		return;
3962 	for (i = 0; i < bp->cp_nr_rings; i++) {
3963 		struct bnxt_napi *bnapi = bp->bnapi[i];
3964 
3965 		if (!bnapi)
3966 			continue;
3967 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3968 	}
3969 }
3970 
bnxt_alloc_all_cp_arrays(struct bnxt * bp)3971 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3972 {
3973 	int i, n = bp->cp_nr_pages;
3974 
3975 	for (i = 0; i < bp->cp_nr_rings; i++) {
3976 		struct bnxt_napi *bnapi = bp->bnapi[i];
3977 		int rc;
3978 
3979 		if (!bnapi)
3980 			continue;
3981 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3982 		if (rc)
3983 			return rc;
3984 	}
3985 	return 0;
3986 }
3987 
bnxt_free_cp_rings(struct bnxt * bp)3988 static void bnxt_free_cp_rings(struct bnxt *bp)
3989 {
3990 	int i;
3991 
3992 	if (!bp->bnapi)
3993 		return;
3994 
3995 	for (i = 0; i < bp->cp_nr_rings; i++) {
3996 		struct bnxt_napi *bnapi = bp->bnapi[i];
3997 		struct bnxt_cp_ring_info *cpr;
3998 		struct bnxt_ring_struct *ring;
3999 		int j;
4000 
4001 		if (!bnapi)
4002 			continue;
4003 
4004 		cpr = &bnapi->cp_ring;
4005 		ring = &cpr->cp_ring_struct;
4006 
4007 		bnxt_free_ring(bp, &ring->ring_mem);
4008 
4009 		if (!cpr->cp_ring_arr)
4010 			continue;
4011 
4012 		for (j = 0; j < cpr->cp_ring_count; j++) {
4013 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4014 
4015 			ring = &cpr2->cp_ring_struct;
4016 			bnxt_free_ring(bp, &ring->ring_mem);
4017 			bnxt_free_cp_arrays(cpr2);
4018 		}
4019 		kfree(cpr->cp_ring_arr);
4020 		cpr->cp_ring_arr = NULL;
4021 		cpr->cp_ring_count = 0;
4022 	}
4023 }
4024 
bnxt_alloc_cp_sub_ring(struct bnxt * bp,struct bnxt_cp_ring_info * cpr)4025 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4026 				  struct bnxt_cp_ring_info *cpr)
4027 {
4028 	struct bnxt_ring_mem_info *rmem;
4029 	struct bnxt_ring_struct *ring;
4030 	int rc;
4031 
4032 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4033 	if (rc) {
4034 		bnxt_free_cp_arrays(cpr);
4035 		return -ENOMEM;
4036 	}
4037 	ring = &cpr->cp_ring_struct;
4038 	rmem = &ring->ring_mem;
4039 	rmem->nr_pages = bp->cp_nr_pages;
4040 	rmem->page_size = HW_CMPD_RING_SIZE;
4041 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4042 	rmem->dma_arr = cpr->cp_desc_mapping;
4043 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4044 	rc = bnxt_alloc_ring(bp, rmem);
4045 	if (rc) {
4046 		bnxt_free_ring(bp, rmem);
4047 		bnxt_free_cp_arrays(cpr);
4048 	}
4049 	return rc;
4050 }
4051 
bnxt_alloc_cp_rings(struct bnxt * bp)4052 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4053 {
4054 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4055 	int i, j, rc, ulp_msix;
4056 	int tcs = bp->num_tc;
4057 
4058 	if (!tcs)
4059 		tcs = 1;
4060 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4061 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4062 		struct bnxt_napi *bnapi = bp->bnapi[i];
4063 		struct bnxt_cp_ring_info *cpr, *cpr2;
4064 		struct bnxt_ring_struct *ring;
4065 		int cp_count = 0, k;
4066 		int rx = 0, tx = 0;
4067 
4068 		if (!bnapi)
4069 			continue;
4070 
4071 		cpr = &bnapi->cp_ring;
4072 		cpr->bnapi = bnapi;
4073 		ring = &cpr->cp_ring_struct;
4074 
4075 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4076 		if (rc)
4077 			return rc;
4078 
4079 		ring->map_idx = ulp_msix + i;
4080 
4081 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4082 			continue;
4083 
4084 		if (i < bp->rx_nr_rings) {
4085 			cp_count++;
4086 			rx = 1;
4087 		}
4088 		if (i < bp->tx_nr_rings_xdp) {
4089 			cp_count++;
4090 			tx = 1;
4091 		} else if ((sh && i < bp->tx_nr_rings) ||
4092 			 (!sh && i >= bp->rx_nr_rings)) {
4093 			cp_count += tcs;
4094 			tx = 1;
4095 		}
4096 
4097 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
4098 					   GFP_KERNEL);
4099 		if (!cpr->cp_ring_arr)
4100 			return -ENOMEM;
4101 		cpr->cp_ring_count = cp_count;
4102 
4103 		for (k = 0; k < cp_count; k++) {
4104 			cpr2 = &cpr->cp_ring_arr[k];
4105 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4106 			if (rc)
4107 				return rc;
4108 			cpr2->bnapi = bnapi;
4109 			cpr2->sw_stats = cpr->sw_stats;
4110 			cpr2->cp_idx = k;
4111 			if (!k && rx) {
4112 				bp->rx_ring[i].rx_cpr = cpr2;
4113 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4114 			} else {
4115 				int n, tc = k - rx;
4116 
4117 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4118 				bp->tx_ring[n].tx_cpr = cpr2;
4119 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4120 			}
4121 		}
4122 		if (tx)
4123 			j++;
4124 	}
4125 	return 0;
4126 }
4127 
bnxt_init_rx_ring_struct(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)4128 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4129 				     struct bnxt_rx_ring_info *rxr)
4130 {
4131 	struct bnxt_ring_mem_info *rmem;
4132 	struct bnxt_ring_struct *ring;
4133 
4134 	ring = &rxr->rx_ring_struct;
4135 	rmem = &ring->ring_mem;
4136 	rmem->nr_pages = bp->rx_nr_pages;
4137 	rmem->page_size = HW_RXBD_RING_SIZE;
4138 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4139 	rmem->dma_arr = rxr->rx_desc_mapping;
4140 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4141 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4142 
4143 	ring = &rxr->rx_agg_ring_struct;
4144 	rmem = &ring->ring_mem;
4145 	rmem->nr_pages = bp->rx_agg_nr_pages;
4146 	rmem->page_size = HW_RXBD_RING_SIZE;
4147 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4148 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4149 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4150 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4151 }
4152 
bnxt_reset_rx_ring_struct(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)4153 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4154 				      struct bnxt_rx_ring_info *rxr)
4155 {
4156 	struct bnxt_ring_mem_info *rmem;
4157 	struct bnxt_ring_struct *ring;
4158 	int i;
4159 
4160 	rxr->page_pool->p.napi = NULL;
4161 	rxr->page_pool = NULL;
4162 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4163 
4164 	ring = &rxr->rx_ring_struct;
4165 	rmem = &ring->ring_mem;
4166 	rmem->pg_tbl = NULL;
4167 	rmem->pg_tbl_map = 0;
4168 	for (i = 0; i < rmem->nr_pages; i++) {
4169 		rmem->pg_arr[i] = NULL;
4170 		rmem->dma_arr[i] = 0;
4171 	}
4172 	*rmem->vmem = NULL;
4173 
4174 	ring = &rxr->rx_agg_ring_struct;
4175 	rmem = &ring->ring_mem;
4176 	rmem->pg_tbl = NULL;
4177 	rmem->pg_tbl_map = 0;
4178 	for (i = 0; i < rmem->nr_pages; i++) {
4179 		rmem->pg_arr[i] = NULL;
4180 		rmem->dma_arr[i] = 0;
4181 	}
4182 	*rmem->vmem = NULL;
4183 }
4184 
bnxt_init_ring_struct(struct bnxt * bp)4185 static void bnxt_init_ring_struct(struct bnxt *bp)
4186 {
4187 	int i, j;
4188 
4189 	for (i = 0; i < bp->cp_nr_rings; i++) {
4190 		struct bnxt_napi *bnapi = bp->bnapi[i];
4191 		struct bnxt_ring_mem_info *rmem;
4192 		struct bnxt_cp_ring_info *cpr;
4193 		struct bnxt_rx_ring_info *rxr;
4194 		struct bnxt_tx_ring_info *txr;
4195 		struct bnxt_ring_struct *ring;
4196 
4197 		if (!bnapi)
4198 			continue;
4199 
4200 		cpr = &bnapi->cp_ring;
4201 		ring = &cpr->cp_ring_struct;
4202 		rmem = &ring->ring_mem;
4203 		rmem->nr_pages = bp->cp_nr_pages;
4204 		rmem->page_size = HW_CMPD_RING_SIZE;
4205 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4206 		rmem->dma_arr = cpr->cp_desc_mapping;
4207 		rmem->vmem_size = 0;
4208 
4209 		rxr = bnapi->rx_ring;
4210 		if (!rxr)
4211 			goto skip_rx;
4212 
4213 		ring = &rxr->rx_ring_struct;
4214 		rmem = &ring->ring_mem;
4215 		rmem->nr_pages = bp->rx_nr_pages;
4216 		rmem->page_size = HW_RXBD_RING_SIZE;
4217 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4218 		rmem->dma_arr = rxr->rx_desc_mapping;
4219 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4220 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4221 
4222 		ring = &rxr->rx_agg_ring_struct;
4223 		rmem = &ring->ring_mem;
4224 		rmem->nr_pages = bp->rx_agg_nr_pages;
4225 		rmem->page_size = HW_RXBD_RING_SIZE;
4226 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4227 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4228 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4229 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4230 
4231 skip_rx:
4232 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4233 			ring = &txr->tx_ring_struct;
4234 			rmem = &ring->ring_mem;
4235 			rmem->nr_pages = bp->tx_nr_pages;
4236 			rmem->page_size = HW_TXBD_RING_SIZE;
4237 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4238 			rmem->dma_arr = txr->tx_desc_mapping;
4239 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4240 			rmem->vmem = (void **)&txr->tx_buf_ring;
4241 		}
4242 	}
4243 }
4244 
bnxt_init_rxbd_pages(struct bnxt_ring_struct * ring,u32 type)4245 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4246 {
4247 	int i;
4248 	u32 prod;
4249 	struct rx_bd **rx_buf_ring;
4250 
4251 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4252 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4253 		int j;
4254 		struct rx_bd *rxbd;
4255 
4256 		rxbd = rx_buf_ring[i];
4257 		if (!rxbd)
4258 			continue;
4259 
4260 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4261 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4262 			rxbd->rx_bd_opaque = prod;
4263 		}
4264 	}
4265 }
4266 
bnxt_alloc_one_rx_ring_skb(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,int ring_nr)4267 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4268 				       struct bnxt_rx_ring_info *rxr,
4269 				       int ring_nr)
4270 {
4271 	u32 prod;
4272 	int i;
4273 
4274 	prod = rxr->rx_prod;
4275 	for (i = 0; i < bp->rx_ring_size; i++) {
4276 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4277 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4278 				    ring_nr, i, bp->rx_ring_size);
4279 			break;
4280 		}
4281 		prod = NEXT_RX(prod);
4282 	}
4283 	rxr->rx_prod = prod;
4284 }
4285 
bnxt_alloc_one_rx_ring_page(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,int ring_nr)4286 static void bnxt_alloc_one_rx_ring_page(struct bnxt *bp,
4287 					struct bnxt_rx_ring_info *rxr,
4288 					int ring_nr)
4289 {
4290 	u32 prod;
4291 	int i;
4292 
4293 	prod = rxr->rx_agg_prod;
4294 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4295 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
4296 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4297 				    ring_nr, i, bp->rx_ring_size);
4298 			break;
4299 		}
4300 		prod = NEXT_RX_AGG(prod);
4301 	}
4302 	rxr->rx_agg_prod = prod;
4303 }
4304 
bnxt_alloc_one_tpa_info_data(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)4305 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4306 					struct bnxt_rx_ring_info *rxr)
4307 {
4308 	dma_addr_t mapping;
4309 	u8 *data;
4310 	int i;
4311 
4312 	for (i = 0; i < bp->max_tpa; i++) {
4313 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4314 					    GFP_KERNEL);
4315 		if (!data)
4316 			return -ENOMEM;
4317 
4318 		rxr->rx_tpa[i].data = data;
4319 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4320 		rxr->rx_tpa[i].mapping = mapping;
4321 	}
4322 
4323 	return 0;
4324 }
4325 
bnxt_alloc_one_rx_ring(struct bnxt * bp,int ring_nr)4326 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4327 {
4328 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4329 	int rc;
4330 
4331 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4332 
4333 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4334 		return 0;
4335 
4336 	bnxt_alloc_one_rx_ring_page(bp, rxr, ring_nr);
4337 
4338 	if (rxr->rx_tpa) {
4339 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4340 		if (rc)
4341 			return rc;
4342 	}
4343 	return 0;
4344 }
4345 
bnxt_init_one_rx_ring_rxbd(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)4346 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4347 				       struct bnxt_rx_ring_info *rxr)
4348 {
4349 	struct bnxt_ring_struct *ring;
4350 	u32 type;
4351 
4352 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4353 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4354 
4355 	if (NET_IP_ALIGN == 2)
4356 		type |= RX_BD_FLAGS_SOP;
4357 
4358 	ring = &rxr->rx_ring_struct;
4359 	bnxt_init_rxbd_pages(ring, type);
4360 	ring->fw_ring_id = INVALID_HW_RING_ID;
4361 }
4362 
bnxt_init_one_rx_agg_ring_rxbd(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)4363 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4364 					   struct bnxt_rx_ring_info *rxr)
4365 {
4366 	struct bnxt_ring_struct *ring;
4367 	u32 type;
4368 
4369 	ring = &rxr->rx_agg_ring_struct;
4370 	ring->fw_ring_id = INVALID_HW_RING_ID;
4371 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4372 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4373 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4374 
4375 		bnxt_init_rxbd_pages(ring, type);
4376 	}
4377 }
4378 
bnxt_init_one_rx_ring(struct bnxt * bp,int ring_nr)4379 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4380 {
4381 	struct bnxt_rx_ring_info *rxr;
4382 
4383 	rxr = &bp->rx_ring[ring_nr];
4384 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4385 
4386 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4387 			     &rxr->bnapi->napi);
4388 
4389 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4390 		bpf_prog_add(bp->xdp_prog, 1);
4391 		rxr->xdp_prog = bp->xdp_prog;
4392 	}
4393 
4394 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4395 
4396 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4397 }
4398 
bnxt_init_cp_rings(struct bnxt * bp)4399 static void bnxt_init_cp_rings(struct bnxt *bp)
4400 {
4401 	int i, j;
4402 
4403 	for (i = 0; i < bp->cp_nr_rings; i++) {
4404 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4405 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4406 
4407 		ring->fw_ring_id = INVALID_HW_RING_ID;
4408 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4409 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4410 		if (!cpr->cp_ring_arr)
4411 			continue;
4412 		for (j = 0; j < cpr->cp_ring_count; j++) {
4413 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4414 
4415 			ring = &cpr2->cp_ring_struct;
4416 			ring->fw_ring_id = INVALID_HW_RING_ID;
4417 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4418 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4419 		}
4420 	}
4421 }
4422 
bnxt_init_rx_rings(struct bnxt * bp)4423 static int bnxt_init_rx_rings(struct bnxt *bp)
4424 {
4425 	int i, rc = 0;
4426 
4427 	if (BNXT_RX_PAGE_MODE(bp)) {
4428 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4429 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4430 	} else {
4431 		bp->rx_offset = BNXT_RX_OFFSET;
4432 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4433 	}
4434 
4435 	for (i = 0; i < bp->rx_nr_rings; i++) {
4436 		rc = bnxt_init_one_rx_ring(bp, i);
4437 		if (rc)
4438 			break;
4439 	}
4440 
4441 	return rc;
4442 }
4443 
bnxt_init_tx_rings(struct bnxt * bp)4444 static int bnxt_init_tx_rings(struct bnxt *bp)
4445 {
4446 	u16 i;
4447 
4448 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4449 				   BNXT_MIN_TX_DESC_CNT);
4450 
4451 	for (i = 0; i < bp->tx_nr_rings; i++) {
4452 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4453 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4454 
4455 		ring->fw_ring_id = INVALID_HW_RING_ID;
4456 
4457 		if (i >= bp->tx_nr_rings_xdp)
4458 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4459 					     NETDEV_QUEUE_TYPE_TX,
4460 					     &txr->bnapi->napi);
4461 	}
4462 
4463 	return 0;
4464 }
4465 
bnxt_free_ring_grps(struct bnxt * bp)4466 static void bnxt_free_ring_grps(struct bnxt *bp)
4467 {
4468 	kfree(bp->grp_info);
4469 	bp->grp_info = NULL;
4470 }
4471 
bnxt_init_ring_grps(struct bnxt * bp,bool irq_re_init)4472 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4473 {
4474 	int i;
4475 
4476 	if (irq_re_init) {
4477 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4478 				       sizeof(struct bnxt_ring_grp_info),
4479 				       GFP_KERNEL);
4480 		if (!bp->grp_info)
4481 			return -ENOMEM;
4482 	}
4483 	for (i = 0; i < bp->cp_nr_rings; i++) {
4484 		if (irq_re_init)
4485 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4486 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4487 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4488 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4489 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4490 	}
4491 	return 0;
4492 }
4493 
bnxt_free_vnics(struct bnxt * bp)4494 static void bnxt_free_vnics(struct bnxt *bp)
4495 {
4496 	kfree(bp->vnic_info);
4497 	bp->vnic_info = NULL;
4498 	bp->nr_vnics = 0;
4499 }
4500 
bnxt_alloc_vnics(struct bnxt * bp)4501 static int bnxt_alloc_vnics(struct bnxt *bp)
4502 {
4503 	int num_vnics = 1;
4504 
4505 #ifdef CONFIG_RFS_ACCEL
4506 	if (bp->flags & BNXT_FLAG_RFS) {
4507 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4508 			num_vnics++;
4509 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4510 			num_vnics += bp->rx_nr_rings;
4511 	}
4512 #endif
4513 
4514 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4515 		num_vnics++;
4516 
4517 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4518 				GFP_KERNEL);
4519 	if (!bp->vnic_info)
4520 		return -ENOMEM;
4521 
4522 	bp->nr_vnics = num_vnics;
4523 	return 0;
4524 }
4525 
bnxt_init_vnics(struct bnxt * bp)4526 static void bnxt_init_vnics(struct bnxt *bp)
4527 {
4528 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4529 	int i;
4530 
4531 	for (i = 0; i < bp->nr_vnics; i++) {
4532 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4533 		int j;
4534 
4535 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4536 		vnic->vnic_id = i;
4537 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4538 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4539 
4540 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4541 
4542 		if (bp->vnic_info[i].rss_hash_key) {
4543 			if (i == BNXT_VNIC_DEFAULT) {
4544 				u8 *key = (void *)vnic->rss_hash_key;
4545 				int k;
4546 
4547 				if (!bp->rss_hash_key_valid &&
4548 				    !bp->rss_hash_key_updated) {
4549 					get_random_bytes(bp->rss_hash_key,
4550 							 HW_HASH_KEY_SIZE);
4551 					bp->rss_hash_key_updated = true;
4552 				}
4553 
4554 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4555 				       HW_HASH_KEY_SIZE);
4556 
4557 				if (!bp->rss_hash_key_updated)
4558 					continue;
4559 
4560 				bp->rss_hash_key_updated = false;
4561 				bp->rss_hash_key_valid = true;
4562 
4563 				bp->toeplitz_prefix = 0;
4564 				for (k = 0; k < 8; k++) {
4565 					bp->toeplitz_prefix <<= 8;
4566 					bp->toeplitz_prefix |= key[k];
4567 				}
4568 			} else {
4569 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4570 				       HW_HASH_KEY_SIZE);
4571 			}
4572 		}
4573 	}
4574 }
4575 
bnxt_calc_nr_ring_pages(u32 ring_size,int desc_per_pg)4576 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4577 {
4578 	int pages;
4579 
4580 	pages = ring_size / desc_per_pg;
4581 
4582 	if (!pages)
4583 		return 1;
4584 
4585 	pages++;
4586 
4587 	while (pages & (pages - 1))
4588 		pages++;
4589 
4590 	return pages;
4591 }
4592 
bnxt_set_tpa_flags(struct bnxt * bp)4593 void bnxt_set_tpa_flags(struct bnxt *bp)
4594 {
4595 	bp->flags &= ~BNXT_FLAG_TPA;
4596 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4597 		return;
4598 	if (bp->dev->features & NETIF_F_LRO)
4599 		bp->flags |= BNXT_FLAG_LRO;
4600 	else if (bp->dev->features & NETIF_F_GRO_HW)
4601 		bp->flags |= BNXT_FLAG_GRO;
4602 }
4603 
4604 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4605  * be set on entry.
4606  */
bnxt_set_ring_params(struct bnxt * bp)4607 void bnxt_set_ring_params(struct bnxt *bp)
4608 {
4609 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4610 	u32 agg_factor = 0, agg_ring_size = 0;
4611 
4612 	/* 8 for CRC and VLAN */
4613 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4614 
4615 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4616 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4617 
4618 	bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
4619 	ring_size = bp->rx_ring_size;
4620 	bp->rx_agg_ring_size = 0;
4621 	bp->rx_agg_nr_pages = 0;
4622 
4623 	if (bp->flags & BNXT_FLAG_TPA)
4624 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4625 
4626 	bp->flags &= ~BNXT_FLAG_JUMBO;
4627 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4628 		u32 jumbo_factor;
4629 
4630 		bp->flags |= BNXT_FLAG_JUMBO;
4631 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4632 		if (jumbo_factor > agg_factor)
4633 			agg_factor = jumbo_factor;
4634 	}
4635 	if (agg_factor) {
4636 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4637 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4638 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4639 				    bp->rx_ring_size, ring_size);
4640 			bp->rx_ring_size = ring_size;
4641 		}
4642 		agg_ring_size = ring_size * agg_factor;
4643 
4644 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4645 							RX_DESC_CNT);
4646 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4647 			u32 tmp = agg_ring_size;
4648 
4649 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4650 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4651 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4652 				    tmp, agg_ring_size);
4653 		}
4654 		bp->rx_agg_ring_size = agg_ring_size;
4655 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4656 
4657 		if (BNXT_RX_PAGE_MODE(bp)) {
4658 			rx_space = PAGE_SIZE;
4659 			rx_size = PAGE_SIZE -
4660 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4661 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4662 		} else {
4663 			rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
4664 			rx_space = rx_size + NET_SKB_PAD +
4665 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4666 		}
4667 	}
4668 
4669 	bp->rx_buf_use_size = rx_size;
4670 	bp->rx_buf_size = rx_space;
4671 
4672 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4673 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4674 
4675 	ring_size = bp->tx_ring_size;
4676 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4677 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4678 
4679 	max_rx_cmpl = bp->rx_ring_size;
4680 	/* MAX TPA needs to be added because TPA_START completions are
4681 	 * immediately recycled, so the TPA completions are not bound by
4682 	 * the RX ring size.
4683 	 */
4684 	if (bp->flags & BNXT_FLAG_TPA)
4685 		max_rx_cmpl += bp->max_tpa;
4686 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4687 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4688 	bp->cp_ring_size = ring_size;
4689 
4690 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4691 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4692 		bp->cp_nr_pages = MAX_CP_PAGES;
4693 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4694 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4695 			    ring_size, bp->cp_ring_size);
4696 	}
4697 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4698 	bp->cp_ring_mask = bp->cp_bit - 1;
4699 }
4700 
4701 /* Changing allocation mode of RX rings.
4702  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4703  */
bnxt_set_rx_skb_mode(struct bnxt * bp,bool page_mode)4704 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4705 {
4706 	struct net_device *dev = bp->dev;
4707 
4708 	if (page_mode) {
4709 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4710 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4711 
4712 		if (bp->xdp_prog->aux->xdp_has_frags)
4713 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4714 		else
4715 			dev->max_mtu =
4716 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4717 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4718 			bp->flags |= BNXT_FLAG_JUMBO;
4719 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4720 		} else {
4721 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4722 			bp->rx_skb_func = bnxt_rx_page_skb;
4723 		}
4724 		bp->rx_dir = DMA_BIDIRECTIONAL;
4725 		/* Disable LRO or GRO_HW */
4726 		netdev_update_features(dev);
4727 	} else {
4728 		dev->max_mtu = bp->max_mtu;
4729 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4730 		bp->rx_dir = DMA_FROM_DEVICE;
4731 		bp->rx_skb_func = bnxt_rx_skb;
4732 	}
4733 	return 0;
4734 }
4735 
bnxt_free_vnic_attributes(struct bnxt * bp)4736 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4737 {
4738 	int i;
4739 	struct bnxt_vnic_info *vnic;
4740 	struct pci_dev *pdev = bp->pdev;
4741 
4742 	if (!bp->vnic_info)
4743 		return;
4744 
4745 	for (i = 0; i < bp->nr_vnics; i++) {
4746 		vnic = &bp->vnic_info[i];
4747 
4748 		kfree(vnic->fw_grp_ids);
4749 		vnic->fw_grp_ids = NULL;
4750 
4751 		kfree(vnic->uc_list);
4752 		vnic->uc_list = NULL;
4753 
4754 		if (vnic->mc_list) {
4755 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4756 					  vnic->mc_list, vnic->mc_list_mapping);
4757 			vnic->mc_list = NULL;
4758 		}
4759 
4760 		if (vnic->rss_table) {
4761 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4762 					  vnic->rss_table,
4763 					  vnic->rss_table_dma_addr);
4764 			vnic->rss_table = NULL;
4765 		}
4766 
4767 		vnic->rss_hash_key = NULL;
4768 		vnic->flags = 0;
4769 	}
4770 }
4771 
bnxt_alloc_vnic_attributes(struct bnxt * bp)4772 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4773 {
4774 	int i, rc = 0, size;
4775 	struct bnxt_vnic_info *vnic;
4776 	struct pci_dev *pdev = bp->pdev;
4777 	int max_rings;
4778 
4779 	for (i = 0; i < bp->nr_vnics; i++) {
4780 		vnic = &bp->vnic_info[i];
4781 
4782 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4783 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4784 
4785 			if (mem_size > 0) {
4786 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4787 				if (!vnic->uc_list) {
4788 					rc = -ENOMEM;
4789 					goto out;
4790 				}
4791 			}
4792 		}
4793 
4794 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4795 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4796 			vnic->mc_list =
4797 				dma_alloc_coherent(&pdev->dev,
4798 						   vnic->mc_list_size,
4799 						   &vnic->mc_list_mapping,
4800 						   GFP_KERNEL);
4801 			if (!vnic->mc_list) {
4802 				rc = -ENOMEM;
4803 				goto out;
4804 			}
4805 		}
4806 
4807 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4808 			goto vnic_skip_grps;
4809 
4810 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4811 			max_rings = bp->rx_nr_rings;
4812 		else
4813 			max_rings = 1;
4814 
4815 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4816 		if (!vnic->fw_grp_ids) {
4817 			rc = -ENOMEM;
4818 			goto out;
4819 		}
4820 vnic_skip_grps:
4821 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4822 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4823 			continue;
4824 
4825 		/* Allocate rss table and hash key */
4826 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4827 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4828 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4829 
4830 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4831 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4832 						     vnic->rss_table_size,
4833 						     &vnic->rss_table_dma_addr,
4834 						     GFP_KERNEL);
4835 		if (!vnic->rss_table) {
4836 			rc = -ENOMEM;
4837 			goto out;
4838 		}
4839 
4840 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4841 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4842 	}
4843 	return 0;
4844 
4845 out:
4846 	return rc;
4847 }
4848 
bnxt_free_hwrm_resources(struct bnxt * bp)4849 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4850 {
4851 	struct bnxt_hwrm_wait_token *token;
4852 
4853 	dma_pool_destroy(bp->hwrm_dma_pool);
4854 	bp->hwrm_dma_pool = NULL;
4855 
4856 	rcu_read_lock();
4857 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4858 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4859 	rcu_read_unlock();
4860 }
4861 
bnxt_alloc_hwrm_resources(struct bnxt * bp)4862 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4863 {
4864 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4865 					    BNXT_HWRM_DMA_SIZE,
4866 					    BNXT_HWRM_DMA_ALIGN, 0);
4867 	if (!bp->hwrm_dma_pool)
4868 		return -ENOMEM;
4869 
4870 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4871 
4872 	return 0;
4873 }
4874 
bnxt_free_stats_mem(struct bnxt * bp,struct bnxt_stats_mem * stats)4875 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4876 {
4877 	kfree(stats->hw_masks);
4878 	stats->hw_masks = NULL;
4879 	kfree(stats->sw_stats);
4880 	stats->sw_stats = NULL;
4881 	if (stats->hw_stats) {
4882 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4883 				  stats->hw_stats_map);
4884 		stats->hw_stats = NULL;
4885 	}
4886 }
4887 
bnxt_alloc_stats_mem(struct bnxt * bp,struct bnxt_stats_mem * stats,bool alloc_masks)4888 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4889 				bool alloc_masks)
4890 {
4891 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4892 					     &stats->hw_stats_map, GFP_KERNEL);
4893 	if (!stats->hw_stats)
4894 		return -ENOMEM;
4895 
4896 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4897 	if (!stats->sw_stats)
4898 		goto stats_mem_err;
4899 
4900 	if (alloc_masks) {
4901 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4902 		if (!stats->hw_masks)
4903 			goto stats_mem_err;
4904 	}
4905 	return 0;
4906 
4907 stats_mem_err:
4908 	bnxt_free_stats_mem(bp, stats);
4909 	return -ENOMEM;
4910 }
4911 
bnxt_fill_masks(u64 * mask_arr,u64 mask,int count)4912 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4913 {
4914 	int i;
4915 
4916 	for (i = 0; i < count; i++)
4917 		mask_arr[i] = mask;
4918 }
4919 
bnxt_copy_hw_masks(u64 * mask_arr,__le64 * hw_mask_arr,int count)4920 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4921 {
4922 	int i;
4923 
4924 	for (i = 0; i < count; i++)
4925 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4926 }
4927 
bnxt_hwrm_func_qstat_ext(struct bnxt * bp,struct bnxt_stats_mem * stats)4928 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4929 				    struct bnxt_stats_mem *stats)
4930 {
4931 	struct hwrm_func_qstats_ext_output *resp;
4932 	struct hwrm_func_qstats_ext_input *req;
4933 	__le64 *hw_masks;
4934 	int rc;
4935 
4936 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4937 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4938 		return -EOPNOTSUPP;
4939 
4940 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4941 	if (rc)
4942 		return rc;
4943 
4944 	req->fid = cpu_to_le16(0xffff);
4945 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4946 
4947 	resp = hwrm_req_hold(bp, req);
4948 	rc = hwrm_req_send(bp, req);
4949 	if (!rc) {
4950 		hw_masks = &resp->rx_ucast_pkts;
4951 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4952 	}
4953 	hwrm_req_drop(bp, req);
4954 	return rc;
4955 }
4956 
4957 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4958 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4959 
bnxt_init_stats(struct bnxt * bp)4960 static void bnxt_init_stats(struct bnxt *bp)
4961 {
4962 	struct bnxt_napi *bnapi = bp->bnapi[0];
4963 	struct bnxt_cp_ring_info *cpr;
4964 	struct bnxt_stats_mem *stats;
4965 	__le64 *rx_stats, *tx_stats;
4966 	int rc, rx_count, tx_count;
4967 	u64 *rx_masks, *tx_masks;
4968 	u64 mask;
4969 	u8 flags;
4970 
4971 	cpr = &bnapi->cp_ring;
4972 	stats = &cpr->stats;
4973 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4974 	if (rc) {
4975 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4976 			mask = (1ULL << 48) - 1;
4977 		else
4978 			mask = -1ULL;
4979 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
4980 	}
4981 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
4982 		stats = &bp->port_stats;
4983 		rx_stats = stats->hw_stats;
4984 		rx_masks = stats->hw_masks;
4985 		rx_count = sizeof(struct rx_port_stats) / 8;
4986 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4987 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4988 		tx_count = sizeof(struct tx_port_stats) / 8;
4989 
4990 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
4991 		rc = bnxt_hwrm_port_qstats(bp, flags);
4992 		if (rc) {
4993 			mask = (1ULL << 40) - 1;
4994 
4995 			bnxt_fill_masks(rx_masks, mask, rx_count);
4996 			bnxt_fill_masks(tx_masks, mask, tx_count);
4997 		} else {
4998 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4999 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5000 			bnxt_hwrm_port_qstats(bp, 0);
5001 		}
5002 	}
5003 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5004 		stats = &bp->rx_port_stats_ext;
5005 		rx_stats = stats->hw_stats;
5006 		rx_masks = stats->hw_masks;
5007 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5008 		stats = &bp->tx_port_stats_ext;
5009 		tx_stats = stats->hw_stats;
5010 		tx_masks = stats->hw_masks;
5011 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5012 
5013 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5014 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5015 		if (rc) {
5016 			mask = (1ULL << 40) - 1;
5017 
5018 			bnxt_fill_masks(rx_masks, mask, rx_count);
5019 			if (tx_stats)
5020 				bnxt_fill_masks(tx_masks, mask, tx_count);
5021 		} else {
5022 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5023 			if (tx_stats)
5024 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5025 						   tx_count);
5026 			bnxt_hwrm_port_qstats_ext(bp, 0);
5027 		}
5028 	}
5029 }
5030 
bnxt_free_port_stats(struct bnxt * bp)5031 static void bnxt_free_port_stats(struct bnxt *bp)
5032 {
5033 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5034 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5035 
5036 	bnxt_free_stats_mem(bp, &bp->port_stats);
5037 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5038 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5039 }
5040 
bnxt_free_ring_stats(struct bnxt * bp)5041 static void bnxt_free_ring_stats(struct bnxt *bp)
5042 {
5043 	int i;
5044 
5045 	if (!bp->bnapi)
5046 		return;
5047 
5048 	for (i = 0; i < bp->cp_nr_rings; i++) {
5049 		struct bnxt_napi *bnapi = bp->bnapi[i];
5050 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5051 
5052 		bnxt_free_stats_mem(bp, &cpr->stats);
5053 
5054 		kfree(cpr->sw_stats);
5055 		cpr->sw_stats = NULL;
5056 	}
5057 }
5058 
bnxt_alloc_stats(struct bnxt * bp)5059 static int bnxt_alloc_stats(struct bnxt *bp)
5060 {
5061 	u32 size, i;
5062 	int rc;
5063 
5064 	size = bp->hw_ring_stats_size;
5065 
5066 	for (i = 0; i < bp->cp_nr_rings; i++) {
5067 		struct bnxt_napi *bnapi = bp->bnapi[i];
5068 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5069 
5070 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
5071 		if (!cpr->sw_stats)
5072 			return -ENOMEM;
5073 
5074 		cpr->stats.len = size;
5075 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5076 		if (rc)
5077 			return rc;
5078 
5079 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5080 	}
5081 
5082 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5083 		return 0;
5084 
5085 	if (bp->port_stats.hw_stats)
5086 		goto alloc_ext_stats;
5087 
5088 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5089 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5090 	if (rc)
5091 		return rc;
5092 
5093 	bp->flags |= BNXT_FLAG_PORT_STATS;
5094 
5095 alloc_ext_stats:
5096 	/* Display extended statistics only if FW supports it */
5097 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5098 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5099 			return 0;
5100 
5101 	if (bp->rx_port_stats_ext.hw_stats)
5102 		goto alloc_tx_ext_stats;
5103 
5104 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5105 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5106 	/* Extended stats are optional */
5107 	if (rc)
5108 		return 0;
5109 
5110 alloc_tx_ext_stats:
5111 	if (bp->tx_port_stats_ext.hw_stats)
5112 		return 0;
5113 
5114 	if (bp->hwrm_spec_code >= 0x10902 ||
5115 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5116 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5117 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5118 		/* Extended stats are optional */
5119 		if (rc)
5120 			return 0;
5121 	}
5122 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5123 	return 0;
5124 }
5125 
bnxt_clear_ring_indices(struct bnxt * bp)5126 static void bnxt_clear_ring_indices(struct bnxt *bp)
5127 {
5128 	int i, j;
5129 
5130 	if (!bp->bnapi)
5131 		return;
5132 
5133 	for (i = 0; i < bp->cp_nr_rings; i++) {
5134 		struct bnxt_napi *bnapi = bp->bnapi[i];
5135 		struct bnxt_cp_ring_info *cpr;
5136 		struct bnxt_rx_ring_info *rxr;
5137 		struct bnxt_tx_ring_info *txr;
5138 
5139 		if (!bnapi)
5140 			continue;
5141 
5142 		cpr = &bnapi->cp_ring;
5143 		cpr->cp_raw_cons = 0;
5144 
5145 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5146 			txr->tx_prod = 0;
5147 			txr->tx_cons = 0;
5148 			txr->tx_hw_cons = 0;
5149 		}
5150 
5151 		rxr = bnapi->rx_ring;
5152 		if (rxr) {
5153 			rxr->rx_prod = 0;
5154 			rxr->rx_agg_prod = 0;
5155 			rxr->rx_sw_agg_prod = 0;
5156 			rxr->rx_next_cons = 0;
5157 		}
5158 		bnapi->events = 0;
5159 	}
5160 }
5161 
bnxt_insert_usr_fltr(struct bnxt * bp,struct bnxt_filter_base * fltr)5162 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5163 {
5164 	u8 type = fltr->type, flags = fltr->flags;
5165 
5166 	INIT_LIST_HEAD(&fltr->list);
5167 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5168 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5169 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5170 }
5171 
bnxt_del_one_usr_fltr(struct bnxt * bp,struct bnxt_filter_base * fltr)5172 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5173 {
5174 	if (!list_empty(&fltr->list))
5175 		list_del_init(&fltr->list);
5176 }
5177 
bnxt_clear_usr_fltrs(struct bnxt * bp,bool all)5178 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5179 {
5180 	struct bnxt_filter_base *usr_fltr, *tmp;
5181 
5182 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5183 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5184 			continue;
5185 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5186 	}
5187 }
5188 
bnxt_del_fltr(struct bnxt * bp,struct bnxt_filter_base * fltr)5189 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5190 {
5191 	hlist_del(&fltr->hash);
5192 	bnxt_del_one_usr_fltr(bp, fltr);
5193 	if (fltr->flags) {
5194 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5195 		bp->ntp_fltr_count--;
5196 	}
5197 	kfree(fltr);
5198 }
5199 
bnxt_free_ntp_fltrs(struct bnxt * bp,bool all)5200 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5201 {
5202 	int i;
5203 
5204 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
5205 	 * safe to delete the hash table.
5206 	 */
5207 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5208 		struct hlist_head *head;
5209 		struct hlist_node *tmp;
5210 		struct bnxt_ntuple_filter *fltr;
5211 
5212 		head = &bp->ntp_fltr_hash_tbl[i];
5213 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5214 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5215 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5216 				     !list_empty(&fltr->base.list)))
5217 				continue;
5218 			bnxt_del_fltr(bp, &fltr->base);
5219 		}
5220 	}
5221 	if (!all)
5222 		return;
5223 
5224 	bitmap_free(bp->ntp_fltr_bmap);
5225 	bp->ntp_fltr_bmap = NULL;
5226 	bp->ntp_fltr_count = 0;
5227 }
5228 
bnxt_alloc_ntp_fltrs(struct bnxt * bp)5229 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5230 {
5231 	int i, rc = 0;
5232 
5233 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5234 		return 0;
5235 
5236 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5237 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5238 
5239 	bp->ntp_fltr_count = 0;
5240 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5241 
5242 	if (!bp->ntp_fltr_bmap)
5243 		rc = -ENOMEM;
5244 
5245 	return rc;
5246 }
5247 
bnxt_free_l2_filters(struct bnxt * bp,bool all)5248 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5249 {
5250 	int i;
5251 
5252 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5253 		struct hlist_head *head;
5254 		struct hlist_node *tmp;
5255 		struct bnxt_l2_filter *fltr;
5256 
5257 		head = &bp->l2_fltr_hash_tbl[i];
5258 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5259 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5260 				     !list_empty(&fltr->base.list)))
5261 				continue;
5262 			bnxt_del_fltr(bp, &fltr->base);
5263 		}
5264 	}
5265 }
5266 
bnxt_init_l2_fltr_tbl(struct bnxt * bp)5267 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5268 {
5269 	int i;
5270 
5271 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5272 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5273 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5274 }
5275 
bnxt_free_mem(struct bnxt * bp,bool irq_re_init)5276 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5277 {
5278 	bnxt_free_vnic_attributes(bp);
5279 	bnxt_free_tx_rings(bp);
5280 	bnxt_free_rx_rings(bp);
5281 	bnxt_free_cp_rings(bp);
5282 	bnxt_free_all_cp_arrays(bp);
5283 	bnxt_free_ntp_fltrs(bp, false);
5284 	bnxt_free_l2_filters(bp, false);
5285 	if (irq_re_init) {
5286 		bnxt_free_ring_stats(bp);
5287 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5288 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5289 			bnxt_free_port_stats(bp);
5290 		bnxt_free_ring_grps(bp);
5291 		bnxt_free_vnics(bp);
5292 		kfree(bp->tx_ring_map);
5293 		bp->tx_ring_map = NULL;
5294 		kfree(bp->tx_ring);
5295 		bp->tx_ring = NULL;
5296 		kfree(bp->rx_ring);
5297 		bp->rx_ring = NULL;
5298 		kfree(bp->bnapi);
5299 		bp->bnapi = NULL;
5300 	} else {
5301 		bnxt_clear_ring_indices(bp);
5302 	}
5303 }
5304 
bnxt_alloc_mem(struct bnxt * bp,bool irq_re_init)5305 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5306 {
5307 	int i, j, rc, size, arr_size;
5308 	void *bnapi;
5309 
5310 	if (irq_re_init) {
5311 		/* Allocate bnapi mem pointer array and mem block for
5312 		 * all queues
5313 		 */
5314 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5315 				bp->cp_nr_rings);
5316 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5317 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5318 		if (!bnapi)
5319 			return -ENOMEM;
5320 
5321 		bp->bnapi = bnapi;
5322 		bnapi += arr_size;
5323 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5324 			bp->bnapi[i] = bnapi;
5325 			bp->bnapi[i]->index = i;
5326 			bp->bnapi[i]->bp = bp;
5327 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5328 				struct bnxt_cp_ring_info *cpr =
5329 					&bp->bnapi[i]->cp_ring;
5330 
5331 				cpr->cp_ring_struct.ring_mem.flags =
5332 					BNXT_RMEM_RING_PTE_FLAG;
5333 			}
5334 		}
5335 
5336 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5337 				      sizeof(struct bnxt_rx_ring_info),
5338 				      GFP_KERNEL);
5339 		if (!bp->rx_ring)
5340 			return -ENOMEM;
5341 
5342 		for (i = 0; i < bp->rx_nr_rings; i++) {
5343 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5344 
5345 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5346 				rxr->rx_ring_struct.ring_mem.flags =
5347 					BNXT_RMEM_RING_PTE_FLAG;
5348 				rxr->rx_agg_ring_struct.ring_mem.flags =
5349 					BNXT_RMEM_RING_PTE_FLAG;
5350 			} else {
5351 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5352 			}
5353 			rxr->bnapi = bp->bnapi[i];
5354 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5355 		}
5356 
5357 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5358 				      sizeof(struct bnxt_tx_ring_info),
5359 				      GFP_KERNEL);
5360 		if (!bp->tx_ring)
5361 			return -ENOMEM;
5362 
5363 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5364 					  GFP_KERNEL);
5365 
5366 		if (!bp->tx_ring_map)
5367 			return -ENOMEM;
5368 
5369 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5370 			j = 0;
5371 		else
5372 			j = bp->rx_nr_rings;
5373 
5374 		for (i = 0; i < bp->tx_nr_rings; i++) {
5375 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5376 			struct bnxt_napi *bnapi2;
5377 
5378 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5379 				txr->tx_ring_struct.ring_mem.flags =
5380 					BNXT_RMEM_RING_PTE_FLAG;
5381 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5382 			if (i >= bp->tx_nr_rings_xdp) {
5383 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5384 
5385 				bnapi2 = bp->bnapi[k];
5386 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5387 				txr->tx_napi_idx =
5388 					BNXT_RING_TO_TC(bp, txr->txq_index);
5389 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5390 				bnapi2->tx_int = bnxt_tx_int;
5391 			} else {
5392 				bnapi2 = bp->bnapi[j];
5393 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5394 				bnapi2->tx_ring[0] = txr;
5395 				bnapi2->tx_int = bnxt_tx_int_xdp;
5396 				j++;
5397 			}
5398 			txr->bnapi = bnapi2;
5399 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5400 				txr->tx_cpr = &bnapi2->cp_ring;
5401 		}
5402 
5403 		rc = bnxt_alloc_stats(bp);
5404 		if (rc)
5405 			goto alloc_mem_err;
5406 		bnxt_init_stats(bp);
5407 
5408 		rc = bnxt_alloc_ntp_fltrs(bp);
5409 		if (rc)
5410 			goto alloc_mem_err;
5411 
5412 		rc = bnxt_alloc_vnics(bp);
5413 		if (rc)
5414 			goto alloc_mem_err;
5415 	}
5416 
5417 	rc = bnxt_alloc_all_cp_arrays(bp);
5418 	if (rc)
5419 		goto alloc_mem_err;
5420 
5421 	bnxt_init_ring_struct(bp);
5422 
5423 	rc = bnxt_alloc_rx_rings(bp);
5424 	if (rc)
5425 		goto alloc_mem_err;
5426 
5427 	rc = bnxt_alloc_tx_rings(bp);
5428 	if (rc)
5429 		goto alloc_mem_err;
5430 
5431 	rc = bnxt_alloc_cp_rings(bp);
5432 	if (rc)
5433 		goto alloc_mem_err;
5434 
5435 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5436 						  BNXT_VNIC_MCAST_FLAG |
5437 						  BNXT_VNIC_UCAST_FLAG;
5438 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5439 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5440 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5441 
5442 	rc = bnxt_alloc_vnic_attributes(bp);
5443 	if (rc)
5444 		goto alloc_mem_err;
5445 	return 0;
5446 
5447 alloc_mem_err:
5448 	bnxt_free_mem(bp, true);
5449 	return rc;
5450 }
5451 
bnxt_disable_int(struct bnxt * bp)5452 static void bnxt_disable_int(struct bnxt *bp)
5453 {
5454 	int i;
5455 
5456 	if (!bp->bnapi)
5457 		return;
5458 
5459 	for (i = 0; i < bp->cp_nr_rings; i++) {
5460 		struct bnxt_napi *bnapi = bp->bnapi[i];
5461 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5462 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5463 
5464 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5465 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5466 	}
5467 }
5468 
bnxt_cp_num_to_irq_num(struct bnxt * bp,int n)5469 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5470 {
5471 	struct bnxt_napi *bnapi = bp->bnapi[n];
5472 	struct bnxt_cp_ring_info *cpr;
5473 
5474 	cpr = &bnapi->cp_ring;
5475 	return cpr->cp_ring_struct.map_idx;
5476 }
5477 
bnxt_disable_int_sync(struct bnxt * bp)5478 static void bnxt_disable_int_sync(struct bnxt *bp)
5479 {
5480 	int i;
5481 
5482 	if (!bp->irq_tbl)
5483 		return;
5484 
5485 	atomic_inc(&bp->intr_sem);
5486 
5487 	bnxt_disable_int(bp);
5488 	for (i = 0; i < bp->cp_nr_rings; i++) {
5489 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5490 
5491 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5492 	}
5493 }
5494 
bnxt_enable_int(struct bnxt * bp)5495 static void bnxt_enable_int(struct bnxt *bp)
5496 {
5497 	int i;
5498 
5499 	atomic_set(&bp->intr_sem, 0);
5500 	for (i = 0; i < bp->cp_nr_rings; i++) {
5501 		struct bnxt_napi *bnapi = bp->bnapi[i];
5502 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5503 
5504 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5505 	}
5506 }
5507 
bnxt_hwrm_func_drv_rgtr(struct bnxt * bp,unsigned long * bmap,int bmap_size,bool async_only)5508 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5509 			    bool async_only)
5510 {
5511 	DECLARE_BITMAP(async_events_bmap, 256);
5512 	u32 *events = (u32 *)async_events_bmap;
5513 	struct hwrm_func_drv_rgtr_output *resp;
5514 	struct hwrm_func_drv_rgtr_input *req;
5515 	u32 flags;
5516 	int rc, i;
5517 
5518 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5519 	if (rc)
5520 		return rc;
5521 
5522 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5523 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5524 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5525 
5526 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5527 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5528 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5529 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5530 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5531 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5532 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5533 	req->flags = cpu_to_le32(flags);
5534 	req->ver_maj_8b = DRV_VER_MAJ;
5535 	req->ver_min_8b = DRV_VER_MIN;
5536 	req->ver_upd_8b = DRV_VER_UPD;
5537 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5538 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5539 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5540 
5541 	if (BNXT_PF(bp)) {
5542 		u32 data[8];
5543 		int i;
5544 
5545 		memset(data, 0, sizeof(data));
5546 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5547 			u16 cmd = bnxt_vf_req_snif[i];
5548 			unsigned int bit, idx;
5549 
5550 			idx = cmd / 32;
5551 			bit = cmd % 32;
5552 			data[idx] |= 1 << bit;
5553 		}
5554 
5555 		for (i = 0; i < 8; i++)
5556 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5557 
5558 		req->enables |=
5559 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5560 	}
5561 
5562 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5563 		req->flags |= cpu_to_le32(
5564 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5565 
5566 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5567 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5568 		u16 event_id = bnxt_async_events_arr[i];
5569 
5570 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5571 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5572 			continue;
5573 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5574 		    !bp->ptp_cfg)
5575 			continue;
5576 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5577 	}
5578 	if (bmap && bmap_size) {
5579 		for (i = 0; i < bmap_size; i++) {
5580 			if (test_bit(i, bmap))
5581 				__set_bit(i, async_events_bmap);
5582 		}
5583 	}
5584 	for (i = 0; i < 8; i++)
5585 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5586 
5587 	if (async_only)
5588 		req->enables =
5589 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5590 
5591 	resp = hwrm_req_hold(bp, req);
5592 	rc = hwrm_req_send(bp, req);
5593 	if (!rc) {
5594 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5595 		if (resp->flags &
5596 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5597 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5598 	}
5599 	hwrm_req_drop(bp, req);
5600 	return rc;
5601 }
5602 
bnxt_hwrm_func_drv_unrgtr(struct bnxt * bp)5603 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5604 {
5605 	struct hwrm_func_drv_unrgtr_input *req;
5606 	int rc;
5607 
5608 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5609 		return 0;
5610 
5611 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5612 	if (rc)
5613 		return rc;
5614 	return hwrm_req_send(bp, req);
5615 }
5616 
5617 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5618 
bnxt_hwrm_tunnel_dst_port_free(struct bnxt * bp,u8 tunnel_type)5619 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5620 {
5621 	struct hwrm_tunnel_dst_port_free_input *req;
5622 	int rc;
5623 
5624 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5625 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5626 		return 0;
5627 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5628 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5629 		return 0;
5630 
5631 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5632 	if (rc)
5633 		return rc;
5634 
5635 	req->tunnel_type = tunnel_type;
5636 
5637 	switch (tunnel_type) {
5638 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5639 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5640 		bp->vxlan_port = 0;
5641 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5642 		break;
5643 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5644 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5645 		bp->nge_port = 0;
5646 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5647 		break;
5648 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5649 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5650 		bp->vxlan_gpe_port = 0;
5651 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5652 		break;
5653 	default:
5654 		break;
5655 	}
5656 
5657 	rc = hwrm_req_send(bp, req);
5658 	if (rc)
5659 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5660 			   rc);
5661 	if (bp->flags & BNXT_FLAG_TPA)
5662 		bnxt_set_tpa(bp, true);
5663 	return rc;
5664 }
5665 
bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt * bp,__be16 port,u8 tunnel_type)5666 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5667 					   u8 tunnel_type)
5668 {
5669 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5670 	struct hwrm_tunnel_dst_port_alloc_input *req;
5671 	int rc;
5672 
5673 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5674 	if (rc)
5675 		return rc;
5676 
5677 	req->tunnel_type = tunnel_type;
5678 	req->tunnel_dst_port_val = port;
5679 
5680 	resp = hwrm_req_hold(bp, req);
5681 	rc = hwrm_req_send(bp, req);
5682 	if (rc) {
5683 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5684 			   rc);
5685 		goto err_out;
5686 	}
5687 
5688 	switch (tunnel_type) {
5689 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5690 		bp->vxlan_port = port;
5691 		bp->vxlan_fw_dst_port_id =
5692 			le16_to_cpu(resp->tunnel_dst_port_id);
5693 		break;
5694 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5695 		bp->nge_port = port;
5696 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5697 		break;
5698 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5699 		bp->vxlan_gpe_port = port;
5700 		bp->vxlan_gpe_fw_dst_port_id =
5701 			le16_to_cpu(resp->tunnel_dst_port_id);
5702 		break;
5703 	default:
5704 		break;
5705 	}
5706 	if (bp->flags & BNXT_FLAG_TPA)
5707 		bnxt_set_tpa(bp, true);
5708 
5709 err_out:
5710 	hwrm_req_drop(bp, req);
5711 	return rc;
5712 }
5713 
bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt * bp,u16 vnic_id)5714 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5715 {
5716 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5717 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5718 	int rc;
5719 
5720 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5721 	if (rc)
5722 		return rc;
5723 
5724 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5725 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5726 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5727 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5728 	}
5729 	req->mask = cpu_to_le32(vnic->rx_mask);
5730 	return hwrm_req_send_silent(bp, req);
5731 }
5732 
bnxt_del_l2_filter(struct bnxt * bp,struct bnxt_l2_filter * fltr)5733 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5734 {
5735 	if (!atomic_dec_and_test(&fltr->refcnt))
5736 		return;
5737 	spin_lock_bh(&bp->ntp_fltr_lock);
5738 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5739 		spin_unlock_bh(&bp->ntp_fltr_lock);
5740 		return;
5741 	}
5742 	hlist_del_rcu(&fltr->base.hash);
5743 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5744 	if (fltr->base.flags) {
5745 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5746 		bp->ntp_fltr_count--;
5747 	}
5748 	spin_unlock_bh(&bp->ntp_fltr_lock);
5749 	kfree_rcu(fltr, base.rcu);
5750 }
5751 
__bnxt_lookup_l2_filter(struct bnxt * bp,struct bnxt_l2_key * key,u32 idx)5752 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5753 						      struct bnxt_l2_key *key,
5754 						      u32 idx)
5755 {
5756 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5757 	struct bnxt_l2_filter *fltr;
5758 
5759 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5760 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5761 
5762 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5763 		    l2_key->vlan == key->vlan)
5764 			return fltr;
5765 	}
5766 	return NULL;
5767 }
5768 
bnxt_lookup_l2_filter(struct bnxt * bp,struct bnxt_l2_key * key,u32 idx)5769 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5770 						    struct bnxt_l2_key *key,
5771 						    u32 idx)
5772 {
5773 	struct bnxt_l2_filter *fltr = NULL;
5774 
5775 	rcu_read_lock();
5776 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5777 	if (fltr)
5778 		atomic_inc(&fltr->refcnt);
5779 	rcu_read_unlock();
5780 	return fltr;
5781 }
5782 
5783 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5784 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5785 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5786 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5787 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5788 
5789 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5790 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5791 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5792 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5793 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5794 
bnxt_get_rss_flow_tuple_len(struct bnxt * bp,struct flow_keys * fkeys)5795 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5796 {
5797 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5798 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5799 			return sizeof(fkeys->addrs.v4addrs) +
5800 			       sizeof(fkeys->ports);
5801 
5802 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5803 			return sizeof(fkeys->addrs.v4addrs);
5804 	}
5805 
5806 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5807 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5808 			return sizeof(fkeys->addrs.v6addrs) +
5809 			       sizeof(fkeys->ports);
5810 
5811 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5812 			return sizeof(fkeys->addrs.v6addrs);
5813 	}
5814 
5815 	return 0;
5816 }
5817 
bnxt_toeplitz(struct bnxt * bp,struct flow_keys * fkeys,const unsigned char * key)5818 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5819 			 const unsigned char *key)
5820 {
5821 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5822 	struct bnxt_ipv4_tuple tuple4;
5823 	struct bnxt_ipv6_tuple tuple6;
5824 	int i, j, len = 0;
5825 	u8 *four_tuple;
5826 
5827 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5828 	if (!len)
5829 		return 0;
5830 
5831 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5832 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5833 		tuple4.ports = fkeys->ports;
5834 		four_tuple = (unsigned char *)&tuple4;
5835 	} else {
5836 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5837 		tuple6.ports = fkeys->ports;
5838 		four_tuple = (unsigned char *)&tuple6;
5839 	}
5840 
5841 	for (i = 0, j = 8; i < len; i++, j++) {
5842 		u8 byte = four_tuple[i];
5843 		int bit;
5844 
5845 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5846 			if (byte & 0x80)
5847 				hash ^= prefix;
5848 		}
5849 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5850 	}
5851 
5852 	/* The valid part of the hash is in the upper 32 bits. */
5853 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5854 }
5855 
5856 #ifdef CONFIG_RFS_ACCEL
5857 static struct bnxt_l2_filter *
bnxt_lookup_l2_filter_from_key(struct bnxt * bp,struct bnxt_l2_key * key)5858 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5859 {
5860 	struct bnxt_l2_filter *fltr;
5861 	u32 idx;
5862 
5863 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5864 	      BNXT_L2_FLTR_HASH_MASK;
5865 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5866 	return fltr;
5867 }
5868 #endif
5869 
bnxt_init_l2_filter(struct bnxt * bp,struct bnxt_l2_filter * fltr,struct bnxt_l2_key * key,u32 idx)5870 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5871 			       struct bnxt_l2_key *key, u32 idx)
5872 {
5873 	struct hlist_head *head;
5874 
5875 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5876 	fltr->l2_key.vlan = key->vlan;
5877 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5878 	if (fltr->base.flags) {
5879 		int bit_id;
5880 
5881 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5882 						 bp->max_fltr, 0);
5883 		if (bit_id < 0)
5884 			return -ENOMEM;
5885 		fltr->base.sw_id = (u16)bit_id;
5886 		bp->ntp_fltr_count++;
5887 	}
5888 	head = &bp->l2_fltr_hash_tbl[idx];
5889 	hlist_add_head_rcu(&fltr->base.hash, head);
5890 	bnxt_insert_usr_fltr(bp, &fltr->base);
5891 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5892 	atomic_set(&fltr->refcnt, 1);
5893 	return 0;
5894 }
5895 
bnxt_alloc_l2_filter(struct bnxt * bp,struct bnxt_l2_key * key,gfp_t gfp)5896 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
5897 						   struct bnxt_l2_key *key,
5898 						   gfp_t gfp)
5899 {
5900 	struct bnxt_l2_filter *fltr;
5901 	u32 idx;
5902 	int rc;
5903 
5904 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5905 	      BNXT_L2_FLTR_HASH_MASK;
5906 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5907 	if (fltr)
5908 		return fltr;
5909 
5910 	fltr = kzalloc(sizeof(*fltr), gfp);
5911 	if (!fltr)
5912 		return ERR_PTR(-ENOMEM);
5913 	spin_lock_bh(&bp->ntp_fltr_lock);
5914 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5915 	spin_unlock_bh(&bp->ntp_fltr_lock);
5916 	if (rc) {
5917 		bnxt_del_l2_filter(bp, fltr);
5918 		fltr = ERR_PTR(rc);
5919 	}
5920 	return fltr;
5921 }
5922 
bnxt_alloc_new_l2_filter(struct bnxt * bp,struct bnxt_l2_key * key,u16 flags)5923 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
5924 						struct bnxt_l2_key *key,
5925 						u16 flags)
5926 {
5927 	struct bnxt_l2_filter *fltr;
5928 	u32 idx;
5929 	int rc;
5930 
5931 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5932 	      BNXT_L2_FLTR_HASH_MASK;
5933 	spin_lock_bh(&bp->ntp_fltr_lock);
5934 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5935 	if (fltr) {
5936 		fltr = ERR_PTR(-EEXIST);
5937 		goto l2_filter_exit;
5938 	}
5939 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
5940 	if (!fltr) {
5941 		fltr = ERR_PTR(-ENOMEM);
5942 		goto l2_filter_exit;
5943 	}
5944 	fltr->base.flags = flags;
5945 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5946 	if (rc) {
5947 		spin_unlock_bh(&bp->ntp_fltr_lock);
5948 		bnxt_del_l2_filter(bp, fltr);
5949 		return ERR_PTR(rc);
5950 	}
5951 
5952 l2_filter_exit:
5953 	spin_unlock_bh(&bp->ntp_fltr_lock);
5954 	return fltr;
5955 }
5956 
bnxt_vf_target_id(struct bnxt_pf_info * pf,u16 vf_idx)5957 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
5958 {
5959 #ifdef CONFIG_BNXT_SRIOV
5960 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
5961 
5962 	return vf->fw_fid;
5963 #else
5964 	return INVALID_HW_RING_ID;
5965 #endif
5966 }
5967 
bnxt_hwrm_l2_filter_free(struct bnxt * bp,struct bnxt_l2_filter * fltr)5968 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5969 {
5970 	struct hwrm_cfa_l2_filter_free_input *req;
5971 	u16 target_id = 0xffff;
5972 	int rc;
5973 
5974 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5975 		struct bnxt_pf_info *pf = &bp->pf;
5976 
5977 		if (fltr->base.vf_idx >= pf->active_vfs)
5978 			return -EINVAL;
5979 
5980 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5981 		if (target_id == INVALID_HW_RING_ID)
5982 			return -EINVAL;
5983 	}
5984 
5985 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
5986 	if (rc)
5987 		return rc;
5988 
5989 	req->target_id = cpu_to_le16(target_id);
5990 	req->l2_filter_id = fltr->base.filter_id;
5991 	return hwrm_req_send(bp, req);
5992 }
5993 
bnxt_hwrm_l2_filter_alloc(struct bnxt * bp,struct bnxt_l2_filter * fltr)5994 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5995 {
5996 	struct hwrm_cfa_l2_filter_alloc_output *resp;
5997 	struct hwrm_cfa_l2_filter_alloc_input *req;
5998 	u16 target_id = 0xffff;
5999 	int rc;
6000 
6001 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6002 		struct bnxt_pf_info *pf = &bp->pf;
6003 
6004 		if (fltr->base.vf_idx >= pf->active_vfs)
6005 			return -EINVAL;
6006 
6007 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6008 	}
6009 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6010 	if (rc)
6011 		return rc;
6012 
6013 	req->target_id = cpu_to_le16(target_id);
6014 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6015 
6016 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6017 		req->flags |=
6018 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6019 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6020 	req->enables =
6021 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6022 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6023 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6024 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6025 	eth_broadcast_addr(req->l2_addr_mask);
6026 
6027 	if (fltr->l2_key.vlan) {
6028 		req->enables |=
6029 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6030 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6031 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6032 		req->num_vlans = 1;
6033 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6034 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6035 	}
6036 
6037 	resp = hwrm_req_hold(bp, req);
6038 	rc = hwrm_req_send(bp, req);
6039 	if (!rc) {
6040 		fltr->base.filter_id = resp->l2_filter_id;
6041 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6042 	}
6043 	hwrm_req_drop(bp, req);
6044 	return rc;
6045 }
6046 
bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt * bp,struct bnxt_ntuple_filter * fltr)6047 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6048 				     struct bnxt_ntuple_filter *fltr)
6049 {
6050 	struct hwrm_cfa_ntuple_filter_free_input *req;
6051 	int rc;
6052 
6053 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6054 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6055 	if (rc)
6056 		return rc;
6057 
6058 	req->ntuple_filter_id = fltr->base.filter_id;
6059 	return hwrm_req_send(bp, req);
6060 }
6061 
6062 #define BNXT_NTP_FLTR_FLAGS					\
6063 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6064 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6065 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6066 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6067 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6068 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6069 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6070 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6071 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6072 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6073 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6074 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6075 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6076 
6077 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6078 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6079 
bnxt_fill_ipv6_mask(__be32 mask[4])6080 void bnxt_fill_ipv6_mask(__be32 mask[4])
6081 {
6082 	int i;
6083 
6084 	for (i = 0; i < 4; i++)
6085 		mask[i] = cpu_to_be32(~0);
6086 }
6087 
6088 static void
bnxt_cfg_rfs_ring_tbl_idx(struct bnxt * bp,struct hwrm_cfa_ntuple_filter_alloc_input * req,struct bnxt_ntuple_filter * fltr)6089 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6090 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6091 			  struct bnxt_ntuple_filter *fltr)
6092 {
6093 	u16 rxq = fltr->base.rxq;
6094 
6095 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6096 		struct ethtool_rxfh_context *ctx;
6097 		struct bnxt_rss_ctx *rss_ctx;
6098 		struct bnxt_vnic_info *vnic;
6099 
6100 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6101 			      fltr->base.fw_vnic_id);
6102 		if (ctx) {
6103 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6104 			vnic = &rss_ctx->vnic;
6105 
6106 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6107 		}
6108 		return;
6109 	}
6110 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6111 		struct bnxt_vnic_info *vnic;
6112 		u32 enables;
6113 
6114 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6115 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6116 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6117 		req->enables |= cpu_to_le32(enables);
6118 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6119 	} else {
6120 		u32 flags;
6121 
6122 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6123 		req->flags |= cpu_to_le32(flags);
6124 		req->dst_id = cpu_to_le16(rxq);
6125 	}
6126 }
6127 
bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt * bp,struct bnxt_ntuple_filter * fltr)6128 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6129 				      struct bnxt_ntuple_filter *fltr)
6130 {
6131 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6132 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6133 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6134 	struct flow_keys *keys = &fltr->fkeys;
6135 	struct bnxt_l2_filter *l2_fltr;
6136 	struct bnxt_vnic_info *vnic;
6137 	int rc;
6138 
6139 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6140 	if (rc)
6141 		return rc;
6142 
6143 	l2_fltr = fltr->l2_fltr;
6144 	req->l2_filter_id = l2_fltr->base.filter_id;
6145 
6146 	if (fltr->base.flags & BNXT_ACT_DROP) {
6147 		req->flags =
6148 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6149 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6150 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6151 	} else {
6152 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6153 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6154 	}
6155 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6156 
6157 	req->ethertype = htons(ETH_P_IP);
6158 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6159 	req->ip_protocol = keys->basic.ip_proto;
6160 
6161 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6162 		req->ethertype = htons(ETH_P_IPV6);
6163 		req->ip_addr_type =
6164 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6165 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6166 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6167 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6168 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6169 	} else {
6170 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6171 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6172 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6173 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6174 	}
6175 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6176 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6177 		req->tunnel_type =
6178 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6179 	}
6180 
6181 	req->src_port = keys->ports.src;
6182 	req->src_port_mask = masks->ports.src;
6183 	req->dst_port = keys->ports.dst;
6184 	req->dst_port_mask = masks->ports.dst;
6185 
6186 	resp = hwrm_req_hold(bp, req);
6187 	rc = hwrm_req_send(bp, req);
6188 	if (!rc)
6189 		fltr->base.filter_id = resp->ntuple_filter_id;
6190 	hwrm_req_drop(bp, req);
6191 	return rc;
6192 }
6193 
bnxt_hwrm_set_vnic_filter(struct bnxt * bp,u16 vnic_id,u16 idx,const u8 * mac_addr)6194 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6195 				     const u8 *mac_addr)
6196 {
6197 	struct bnxt_l2_filter *fltr;
6198 	struct bnxt_l2_key key;
6199 	int rc;
6200 
6201 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6202 	key.vlan = 0;
6203 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6204 	if (IS_ERR(fltr))
6205 		return PTR_ERR(fltr);
6206 
6207 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6208 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6209 	if (rc)
6210 		bnxt_del_l2_filter(bp, fltr);
6211 	else
6212 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6213 	return rc;
6214 }
6215 
bnxt_hwrm_clear_vnic_filter(struct bnxt * bp)6216 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6217 {
6218 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6219 
6220 	/* Any associated ntuple filters will also be cleared by firmware. */
6221 	for (i = 0; i < num_of_vnics; i++) {
6222 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6223 
6224 		for (j = 0; j < vnic->uc_filter_count; j++) {
6225 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6226 
6227 			bnxt_hwrm_l2_filter_free(bp, fltr);
6228 			bnxt_del_l2_filter(bp, fltr);
6229 		}
6230 		vnic->uc_filter_count = 0;
6231 	}
6232 }
6233 
6234 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6235 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6236 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6237 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6238 
bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt * bp,struct hwrm_vnic_tpa_cfg_input * req)6239 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6240 					   struct hwrm_vnic_tpa_cfg_input *req)
6241 {
6242 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6243 
6244 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6245 		return;
6246 
6247 	if (bp->vxlan_port)
6248 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6249 	if (bp->vxlan_gpe_port)
6250 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6251 	if (bp->nge_port)
6252 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6253 
6254 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6255 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6256 }
6257 
bnxt_hwrm_vnic_set_tpa(struct bnxt * bp,struct bnxt_vnic_info * vnic,u32 tpa_flags)6258 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6259 			   u32 tpa_flags)
6260 {
6261 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6262 	struct hwrm_vnic_tpa_cfg_input *req;
6263 	int rc;
6264 
6265 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6266 		return 0;
6267 
6268 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6269 	if (rc)
6270 		return rc;
6271 
6272 	if (tpa_flags) {
6273 		u16 mss = bp->dev->mtu - 40;
6274 		u32 nsegs, n, segs = 0, flags;
6275 
6276 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6277 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6278 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6279 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6280 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6281 		if (tpa_flags & BNXT_FLAG_GRO)
6282 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6283 
6284 		req->flags = cpu_to_le32(flags);
6285 
6286 		req->enables =
6287 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6288 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6289 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6290 
6291 		/* Number of segs are log2 units, and first packet is not
6292 		 * included as part of this units.
6293 		 */
6294 		if (mss <= BNXT_RX_PAGE_SIZE) {
6295 			n = BNXT_RX_PAGE_SIZE / mss;
6296 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6297 		} else {
6298 			n = mss / BNXT_RX_PAGE_SIZE;
6299 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6300 				n++;
6301 			nsegs = (MAX_SKB_FRAGS - n) / n;
6302 		}
6303 
6304 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6305 			segs = MAX_TPA_SEGS_P5;
6306 			max_aggs = bp->max_tpa;
6307 		} else {
6308 			segs = ilog2(nsegs);
6309 		}
6310 		req->max_agg_segs = cpu_to_le16(segs);
6311 		req->max_aggs = cpu_to_le16(max_aggs);
6312 
6313 		req->min_agg_len = cpu_to_le32(512);
6314 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6315 	}
6316 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6317 
6318 	return hwrm_req_send(bp, req);
6319 }
6320 
bnxt_cp_ring_from_grp(struct bnxt * bp,struct bnxt_ring_struct * ring)6321 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6322 {
6323 	struct bnxt_ring_grp_info *grp_info;
6324 
6325 	grp_info = &bp->grp_info[ring->grp_idx];
6326 	return grp_info->cp_fw_ring_id;
6327 }
6328 
bnxt_cp_ring_for_rx(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)6329 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6330 {
6331 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6332 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6333 	else
6334 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6335 }
6336 
bnxt_cp_ring_for_tx(struct bnxt * bp,struct bnxt_tx_ring_info * txr)6337 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6338 {
6339 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6340 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6341 	else
6342 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6343 }
6344 
bnxt_alloc_rss_indir_tbl(struct bnxt * bp)6345 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6346 {
6347 	int entries;
6348 
6349 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6350 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6351 	else
6352 		entries = HW_HASH_INDEX_SIZE;
6353 
6354 	bp->rss_indir_tbl_entries = entries;
6355 	bp->rss_indir_tbl =
6356 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6357 	if (!bp->rss_indir_tbl)
6358 		return -ENOMEM;
6359 
6360 	return 0;
6361 }
6362 
bnxt_set_dflt_rss_indir_tbl(struct bnxt * bp,struct ethtool_rxfh_context * rss_ctx)6363 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6364 				 struct ethtool_rxfh_context *rss_ctx)
6365 {
6366 	u16 max_rings, max_entries, pad, i;
6367 	u32 *rss_indir_tbl;
6368 
6369 	if (!bp->rx_nr_rings)
6370 		return;
6371 
6372 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6373 		max_rings = bp->rx_nr_rings - 1;
6374 	else
6375 		max_rings = bp->rx_nr_rings;
6376 
6377 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6378 	if (rss_ctx)
6379 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6380 	else
6381 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6382 
6383 	for (i = 0; i < max_entries; i++)
6384 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6385 
6386 	pad = bp->rss_indir_tbl_entries - max_entries;
6387 	if (pad)
6388 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6389 }
6390 
bnxt_get_max_rss_ring(struct bnxt * bp)6391 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6392 {
6393 	u32 i, tbl_size, max_ring = 0;
6394 
6395 	if (!bp->rss_indir_tbl)
6396 		return 0;
6397 
6398 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6399 	for (i = 0; i < tbl_size; i++)
6400 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6401 	return max_ring;
6402 }
6403 
bnxt_get_nr_rss_ctxs(struct bnxt * bp,int rx_rings)6404 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6405 {
6406 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6407 		if (!rx_rings)
6408 			return 0;
6409 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6410 					       BNXT_RSS_TABLE_ENTRIES_P5);
6411 	}
6412 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6413 		return 2;
6414 	return 1;
6415 }
6416 
bnxt_fill_hw_rss_tbl(struct bnxt * bp,struct bnxt_vnic_info * vnic)6417 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6418 {
6419 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6420 	u16 i, j;
6421 
6422 	/* Fill the RSS indirection table with ring group ids */
6423 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6424 		if (!no_rss)
6425 			j = bp->rss_indir_tbl[i];
6426 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6427 	}
6428 }
6429 
bnxt_fill_hw_rss_tbl_p5(struct bnxt * bp,struct bnxt_vnic_info * vnic)6430 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6431 				    struct bnxt_vnic_info *vnic)
6432 {
6433 	__le16 *ring_tbl = vnic->rss_table;
6434 	struct bnxt_rx_ring_info *rxr;
6435 	u16 tbl_size, i;
6436 
6437 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6438 
6439 	for (i = 0; i < tbl_size; i++) {
6440 		u16 ring_id, j;
6441 
6442 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6443 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6444 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6445 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6446 		else
6447 			j = bp->rss_indir_tbl[i];
6448 		rxr = &bp->rx_ring[j];
6449 
6450 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6451 		*ring_tbl++ = cpu_to_le16(ring_id);
6452 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6453 		*ring_tbl++ = cpu_to_le16(ring_id);
6454 	}
6455 }
6456 
6457 static void
__bnxt_hwrm_vnic_set_rss(struct bnxt * bp,struct hwrm_vnic_rss_cfg_input * req,struct bnxt_vnic_info * vnic)6458 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6459 			 struct bnxt_vnic_info *vnic)
6460 {
6461 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6462 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6463 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6464 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6465 	} else {
6466 		bnxt_fill_hw_rss_tbl(bp, vnic);
6467 	}
6468 
6469 	if (bp->rss_hash_delta) {
6470 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6471 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6472 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6473 		else
6474 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6475 	} else {
6476 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6477 	}
6478 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6479 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6480 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6481 }
6482 
bnxt_hwrm_vnic_set_rss(struct bnxt * bp,struct bnxt_vnic_info * vnic,bool set_rss)6483 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6484 				  bool set_rss)
6485 {
6486 	struct hwrm_vnic_rss_cfg_input *req;
6487 	int rc;
6488 
6489 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6490 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6491 		return 0;
6492 
6493 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6494 	if (rc)
6495 		return rc;
6496 
6497 	if (set_rss)
6498 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6499 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6500 	return hwrm_req_send(bp, req);
6501 }
6502 
bnxt_hwrm_vnic_set_rss_p5(struct bnxt * bp,struct bnxt_vnic_info * vnic,bool set_rss)6503 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6504 				     struct bnxt_vnic_info *vnic, bool set_rss)
6505 {
6506 	struct hwrm_vnic_rss_cfg_input *req;
6507 	dma_addr_t ring_tbl_map;
6508 	u32 i, nr_ctxs;
6509 	int rc;
6510 
6511 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6512 	if (rc)
6513 		return rc;
6514 
6515 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6516 	if (!set_rss)
6517 		return hwrm_req_send(bp, req);
6518 
6519 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6520 	ring_tbl_map = vnic->rss_table_dma_addr;
6521 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6522 
6523 	hwrm_req_hold(bp, req);
6524 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6525 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6526 		req->ring_table_pair_index = i;
6527 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6528 		rc = hwrm_req_send(bp, req);
6529 		if (rc)
6530 			goto exit;
6531 	}
6532 
6533 exit:
6534 	hwrm_req_drop(bp, req);
6535 	return rc;
6536 }
6537 
bnxt_hwrm_update_rss_hash_cfg(struct bnxt * bp)6538 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6539 {
6540 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6541 	struct hwrm_vnic_rss_qcfg_output *resp;
6542 	struct hwrm_vnic_rss_qcfg_input *req;
6543 
6544 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6545 		return;
6546 
6547 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6548 	/* all contexts configured to same hash_type, zero always exists */
6549 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6550 	resp = hwrm_req_hold(bp, req);
6551 	if (!hwrm_req_send(bp, req)) {
6552 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6553 		bp->rss_hash_delta = 0;
6554 	}
6555 	hwrm_req_drop(bp, req);
6556 }
6557 
bnxt_hwrm_vnic_set_hds(struct bnxt * bp,struct bnxt_vnic_info * vnic)6558 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6559 {
6560 	struct hwrm_vnic_plcmodes_cfg_input *req;
6561 	int rc;
6562 
6563 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6564 	if (rc)
6565 		return rc;
6566 
6567 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6568 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6569 
6570 	if (BNXT_RX_PAGE_MODE(bp)) {
6571 		req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6572 	} else {
6573 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6574 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6575 		req->enables |=
6576 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6577 		req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
6578 		req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
6579 	}
6580 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6581 	return hwrm_req_send(bp, req);
6582 }
6583 
bnxt_hwrm_vnic_ctx_free_one(struct bnxt * bp,struct bnxt_vnic_info * vnic,u16 ctx_idx)6584 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6585 					struct bnxt_vnic_info *vnic,
6586 					u16 ctx_idx)
6587 {
6588 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6589 
6590 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6591 		return;
6592 
6593 	req->rss_cos_lb_ctx_id =
6594 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6595 
6596 	hwrm_req_send(bp, req);
6597 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6598 }
6599 
bnxt_hwrm_vnic_ctx_free(struct bnxt * bp)6600 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6601 {
6602 	int i, j;
6603 
6604 	for (i = 0; i < bp->nr_vnics; i++) {
6605 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6606 
6607 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6608 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6609 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6610 		}
6611 	}
6612 	bp->rsscos_nr_ctxs = 0;
6613 }
6614 
bnxt_hwrm_vnic_ctx_alloc(struct bnxt * bp,struct bnxt_vnic_info * vnic,u16 ctx_idx)6615 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6616 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6617 {
6618 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6619 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6620 	int rc;
6621 
6622 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6623 	if (rc)
6624 		return rc;
6625 
6626 	resp = hwrm_req_hold(bp, req);
6627 	rc = hwrm_req_send(bp, req);
6628 	if (!rc)
6629 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6630 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6631 	hwrm_req_drop(bp, req);
6632 
6633 	return rc;
6634 }
6635 
bnxt_get_roce_vnic_mode(struct bnxt * bp)6636 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6637 {
6638 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6639 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6640 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6641 }
6642 
bnxt_hwrm_vnic_cfg(struct bnxt * bp,struct bnxt_vnic_info * vnic)6643 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6644 {
6645 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6646 	struct hwrm_vnic_cfg_input *req;
6647 	unsigned int ring = 0, grp_idx;
6648 	u16 def_vlan = 0;
6649 	int rc;
6650 
6651 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6652 	if (rc)
6653 		return rc;
6654 
6655 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6656 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6657 
6658 		req->default_rx_ring_id =
6659 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6660 		req->default_cmpl_ring_id =
6661 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6662 		req->enables =
6663 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6664 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6665 		goto vnic_mru;
6666 	}
6667 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6668 	/* Only RSS support for now TBD: COS & LB */
6669 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6670 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6671 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6672 					   VNIC_CFG_REQ_ENABLES_MRU);
6673 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6674 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6675 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6676 					   VNIC_CFG_REQ_ENABLES_MRU);
6677 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6678 	} else {
6679 		req->rss_rule = cpu_to_le16(0xffff);
6680 	}
6681 
6682 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6683 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6684 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6685 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6686 	} else {
6687 		req->cos_rule = cpu_to_le16(0xffff);
6688 	}
6689 
6690 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6691 		ring = 0;
6692 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6693 		ring = vnic->vnic_id - 1;
6694 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6695 		ring = bp->rx_nr_rings - 1;
6696 
6697 	grp_idx = bp->rx_ring[ring].bnapi->index;
6698 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6699 	req->lb_rule = cpu_to_le16(0xffff);
6700 vnic_mru:
6701 	vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
6702 	req->mru = cpu_to_le16(vnic->mru);
6703 
6704 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6705 #ifdef CONFIG_BNXT_SRIOV
6706 	if (BNXT_VF(bp))
6707 		def_vlan = bp->vf.vlan;
6708 #endif
6709 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6710 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6711 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6712 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6713 
6714 	return hwrm_req_send(bp, req);
6715 }
6716 
bnxt_hwrm_vnic_free_one(struct bnxt * bp,struct bnxt_vnic_info * vnic)6717 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6718 				    struct bnxt_vnic_info *vnic)
6719 {
6720 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6721 		struct hwrm_vnic_free_input *req;
6722 
6723 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6724 			return;
6725 
6726 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6727 
6728 		hwrm_req_send(bp, req);
6729 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6730 	}
6731 }
6732 
bnxt_hwrm_vnic_free(struct bnxt * bp)6733 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6734 {
6735 	u16 i;
6736 
6737 	for (i = 0; i < bp->nr_vnics; i++)
6738 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6739 }
6740 
bnxt_hwrm_vnic_alloc(struct bnxt * bp,struct bnxt_vnic_info * vnic,unsigned int start_rx_ring_idx,unsigned int nr_rings)6741 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6742 			 unsigned int start_rx_ring_idx,
6743 			 unsigned int nr_rings)
6744 {
6745 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6746 	struct hwrm_vnic_alloc_output *resp;
6747 	struct hwrm_vnic_alloc_input *req;
6748 	int rc;
6749 
6750 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6751 	if (rc)
6752 		return rc;
6753 
6754 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6755 		goto vnic_no_ring_grps;
6756 
6757 	/* map ring groups to this vnic */
6758 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6759 		grp_idx = bp->rx_ring[i].bnapi->index;
6760 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6761 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6762 				   j, nr_rings);
6763 			break;
6764 		}
6765 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6766 	}
6767 
6768 vnic_no_ring_grps:
6769 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6770 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6771 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6772 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6773 
6774 	resp = hwrm_req_hold(bp, req);
6775 	rc = hwrm_req_send(bp, req);
6776 	if (!rc)
6777 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6778 	hwrm_req_drop(bp, req);
6779 	return rc;
6780 }
6781 
bnxt_hwrm_vnic_qcaps(struct bnxt * bp)6782 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6783 {
6784 	struct hwrm_vnic_qcaps_output *resp;
6785 	struct hwrm_vnic_qcaps_input *req;
6786 	int rc;
6787 
6788 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6789 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6790 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6791 	if (bp->hwrm_spec_code < 0x10600)
6792 		return 0;
6793 
6794 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6795 	if (rc)
6796 		return rc;
6797 
6798 	resp = hwrm_req_hold(bp, req);
6799 	rc = hwrm_req_send(bp, req);
6800 	if (!rc) {
6801 		u32 flags = le32_to_cpu(resp->flags);
6802 
6803 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6804 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6805 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6806 		if (flags &
6807 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6808 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6809 
6810 		/* Older P5 fw before EXT_HW_STATS support did not set
6811 		 * VLAN_STRIP_CAP properly.
6812 		 */
6813 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6814 		    (BNXT_CHIP_P5(bp) &&
6815 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6816 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6817 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6818 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6819 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6820 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6821 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6822 		if (bp->max_tpa_v2) {
6823 			if (BNXT_CHIP_P5(bp))
6824 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6825 			else
6826 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6827 		}
6828 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6829 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6830 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6831 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6832 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6833 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6834 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6835 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6836 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6837 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6838 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6839 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6840 	}
6841 	hwrm_req_drop(bp, req);
6842 	return rc;
6843 }
6844 
bnxt_hwrm_ring_grp_alloc(struct bnxt * bp)6845 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6846 {
6847 	struct hwrm_ring_grp_alloc_output *resp;
6848 	struct hwrm_ring_grp_alloc_input *req;
6849 	int rc;
6850 	u16 i;
6851 
6852 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6853 		return 0;
6854 
6855 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6856 	if (rc)
6857 		return rc;
6858 
6859 	resp = hwrm_req_hold(bp, req);
6860 	for (i = 0; i < bp->rx_nr_rings; i++) {
6861 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6862 
6863 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6864 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6865 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6866 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6867 
6868 		rc = hwrm_req_send(bp, req);
6869 
6870 		if (rc)
6871 			break;
6872 
6873 		bp->grp_info[grp_idx].fw_grp_id =
6874 			le32_to_cpu(resp->ring_group_id);
6875 	}
6876 	hwrm_req_drop(bp, req);
6877 	return rc;
6878 }
6879 
bnxt_hwrm_ring_grp_free(struct bnxt * bp)6880 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6881 {
6882 	struct hwrm_ring_grp_free_input *req;
6883 	u16 i;
6884 
6885 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6886 		return;
6887 
6888 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6889 		return;
6890 
6891 	hwrm_req_hold(bp, req);
6892 	for (i = 0; i < bp->cp_nr_rings; i++) {
6893 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
6894 			continue;
6895 		req->ring_group_id =
6896 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
6897 
6898 		hwrm_req_send(bp, req);
6899 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
6900 	}
6901 	hwrm_req_drop(bp, req);
6902 }
6903 
hwrm_ring_alloc_send_msg(struct bnxt * bp,struct bnxt_ring_struct * ring,u32 ring_type,u32 map_index)6904 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
6905 				    struct bnxt_ring_struct *ring,
6906 				    u32 ring_type, u32 map_index)
6907 {
6908 	struct hwrm_ring_alloc_output *resp;
6909 	struct hwrm_ring_alloc_input *req;
6910 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
6911 	struct bnxt_ring_grp_info *grp_info;
6912 	int rc, err = 0;
6913 	u16 ring_id;
6914 
6915 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
6916 	if (rc)
6917 		goto exit;
6918 
6919 	req->enables = 0;
6920 	if (rmem->nr_pages > 1) {
6921 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
6922 		/* Page size is in log2 units */
6923 		req->page_size = BNXT_PAGE_SHIFT;
6924 		req->page_tbl_depth = 1;
6925 	} else {
6926 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
6927 	}
6928 	req->fbo = 0;
6929 	/* Association of ring index with doorbell index and MSIX number */
6930 	req->logical_id = cpu_to_le16(map_index);
6931 
6932 	switch (ring_type) {
6933 	case HWRM_RING_ALLOC_TX: {
6934 		struct bnxt_tx_ring_info *txr;
6935 		u16 flags = 0;
6936 
6937 		txr = container_of(ring, struct bnxt_tx_ring_info,
6938 				   tx_ring_struct);
6939 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
6940 		/* Association of transmit ring with completion ring */
6941 		grp_info = &bp->grp_info[ring->grp_idx];
6942 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
6943 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
6944 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6945 		req->queue_id = cpu_to_le16(ring->queue_id);
6946 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
6947 			req->cmpl_coal_cnt =
6948 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
6949 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
6950 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
6951 		req->flags = cpu_to_le16(flags);
6952 		break;
6953 	}
6954 	case HWRM_RING_ALLOC_RX:
6955 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6956 		req->length = cpu_to_le32(bp->rx_ring_mask + 1);
6957 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6958 			u16 flags = 0;
6959 
6960 			/* Association of rx ring with stats context */
6961 			grp_info = &bp->grp_info[ring->grp_idx];
6962 			req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
6963 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6964 			req->enables |= cpu_to_le32(
6965 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6966 			if (NET_IP_ALIGN == 2)
6967 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
6968 			req->flags = cpu_to_le16(flags);
6969 		}
6970 		break;
6971 	case HWRM_RING_ALLOC_AGG:
6972 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6973 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
6974 			/* Association of agg ring with rx ring */
6975 			grp_info = &bp->grp_info[ring->grp_idx];
6976 			req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
6977 			req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
6978 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6979 			req->enables |= cpu_to_le32(
6980 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
6981 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6982 		} else {
6983 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6984 		}
6985 		req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
6986 		break;
6987 	case HWRM_RING_ALLOC_CMPL:
6988 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
6989 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6990 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6991 			/* Association of cp ring with nq */
6992 			grp_info = &bp->grp_info[map_index];
6993 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
6994 			req->cq_handle = cpu_to_le64(ring->handle);
6995 			req->enables |= cpu_to_le32(
6996 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
6997 		} else {
6998 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
6999 		}
7000 		break;
7001 	case HWRM_RING_ALLOC_NQ:
7002 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7003 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7004 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7005 		break;
7006 	default:
7007 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7008 			   ring_type);
7009 		return -1;
7010 	}
7011 
7012 	resp = hwrm_req_hold(bp, req);
7013 	rc = hwrm_req_send(bp, req);
7014 	err = le16_to_cpu(resp->error_code);
7015 	ring_id = le16_to_cpu(resp->ring_id);
7016 	hwrm_req_drop(bp, req);
7017 
7018 exit:
7019 	if (rc || err) {
7020 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7021 			   ring_type, rc, err);
7022 		return -EIO;
7023 	}
7024 	ring->fw_ring_id = ring_id;
7025 	return rc;
7026 }
7027 
bnxt_hwrm_set_async_event_cr(struct bnxt * bp,int idx)7028 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7029 {
7030 	int rc;
7031 
7032 	if (BNXT_PF(bp)) {
7033 		struct hwrm_func_cfg_input *req;
7034 
7035 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7036 		if (rc)
7037 			return rc;
7038 
7039 		req->fid = cpu_to_le16(0xffff);
7040 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7041 		req->async_event_cr = cpu_to_le16(idx);
7042 		return hwrm_req_send(bp, req);
7043 	} else {
7044 		struct hwrm_func_vf_cfg_input *req;
7045 
7046 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7047 		if (rc)
7048 			return rc;
7049 
7050 		req->enables =
7051 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7052 		req->async_event_cr = cpu_to_le16(idx);
7053 		return hwrm_req_send(bp, req);
7054 	}
7055 }
7056 
bnxt_set_db_mask(struct bnxt * bp,struct bnxt_db_info * db,u32 ring_type)7057 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7058 			     u32 ring_type)
7059 {
7060 	switch (ring_type) {
7061 	case HWRM_RING_ALLOC_TX:
7062 		db->db_ring_mask = bp->tx_ring_mask;
7063 		break;
7064 	case HWRM_RING_ALLOC_RX:
7065 		db->db_ring_mask = bp->rx_ring_mask;
7066 		break;
7067 	case HWRM_RING_ALLOC_AGG:
7068 		db->db_ring_mask = bp->rx_agg_ring_mask;
7069 		break;
7070 	case HWRM_RING_ALLOC_CMPL:
7071 	case HWRM_RING_ALLOC_NQ:
7072 		db->db_ring_mask = bp->cp_ring_mask;
7073 		break;
7074 	}
7075 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7076 		db->db_epoch_mask = db->db_ring_mask + 1;
7077 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7078 	}
7079 }
7080 
bnxt_set_db(struct bnxt * bp,struct bnxt_db_info * db,u32 ring_type,u32 map_idx,u32 xid)7081 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7082 			u32 map_idx, u32 xid)
7083 {
7084 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7085 		switch (ring_type) {
7086 		case HWRM_RING_ALLOC_TX:
7087 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7088 			break;
7089 		case HWRM_RING_ALLOC_RX:
7090 		case HWRM_RING_ALLOC_AGG:
7091 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7092 			break;
7093 		case HWRM_RING_ALLOC_CMPL:
7094 			db->db_key64 = DBR_PATH_L2;
7095 			break;
7096 		case HWRM_RING_ALLOC_NQ:
7097 			db->db_key64 = DBR_PATH_L2;
7098 			break;
7099 		}
7100 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7101 
7102 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7103 			db->db_key64 |= DBR_VALID;
7104 
7105 		db->doorbell = bp->bar1 + bp->db_offset;
7106 	} else {
7107 		db->doorbell = bp->bar1 + map_idx * 0x80;
7108 		switch (ring_type) {
7109 		case HWRM_RING_ALLOC_TX:
7110 			db->db_key32 = DB_KEY_TX;
7111 			break;
7112 		case HWRM_RING_ALLOC_RX:
7113 		case HWRM_RING_ALLOC_AGG:
7114 			db->db_key32 = DB_KEY_RX;
7115 			break;
7116 		case HWRM_RING_ALLOC_CMPL:
7117 			db->db_key32 = DB_KEY_CP;
7118 			break;
7119 		}
7120 	}
7121 	bnxt_set_db_mask(bp, db, ring_type);
7122 }
7123 
bnxt_hwrm_rx_ring_alloc(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)7124 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7125 				   struct bnxt_rx_ring_info *rxr)
7126 {
7127 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7128 	struct bnxt_napi *bnapi = rxr->bnapi;
7129 	u32 type = HWRM_RING_ALLOC_RX;
7130 	u32 map_idx = bnapi->index;
7131 	int rc;
7132 
7133 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7134 	if (rc)
7135 		return rc;
7136 
7137 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7138 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7139 
7140 	return 0;
7141 }
7142 
bnxt_hwrm_rx_agg_ring_alloc(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)7143 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7144 				       struct bnxt_rx_ring_info *rxr)
7145 {
7146 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7147 	u32 type = HWRM_RING_ALLOC_AGG;
7148 	u32 grp_idx = ring->grp_idx;
7149 	u32 map_idx;
7150 	int rc;
7151 
7152 	map_idx = grp_idx + bp->rx_nr_rings;
7153 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7154 	if (rc)
7155 		return rc;
7156 
7157 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7158 		    ring->fw_ring_id);
7159 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7160 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7161 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7162 
7163 	return 0;
7164 }
7165 
bnxt_hwrm_ring_alloc(struct bnxt * bp)7166 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7167 {
7168 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7169 	int i, rc = 0;
7170 	u32 type;
7171 
7172 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7173 		type = HWRM_RING_ALLOC_NQ;
7174 	else
7175 		type = HWRM_RING_ALLOC_CMPL;
7176 	for (i = 0; i < bp->cp_nr_rings; i++) {
7177 		struct bnxt_napi *bnapi = bp->bnapi[i];
7178 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7179 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7180 		u32 map_idx = ring->map_idx;
7181 		unsigned int vector;
7182 
7183 		vector = bp->irq_tbl[map_idx].vector;
7184 		disable_irq_nosync(vector);
7185 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7186 		if (rc) {
7187 			enable_irq(vector);
7188 			goto err_out;
7189 		}
7190 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7191 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7192 		enable_irq(vector);
7193 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7194 
7195 		if (!i) {
7196 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7197 			if (rc)
7198 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7199 		}
7200 	}
7201 
7202 	type = HWRM_RING_ALLOC_TX;
7203 	for (i = 0; i < bp->tx_nr_rings; i++) {
7204 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7205 		struct bnxt_ring_struct *ring;
7206 		u32 map_idx;
7207 
7208 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7209 			struct bnxt_cp_ring_info *cpr2 = txr->tx_cpr;
7210 			struct bnxt_napi *bnapi = txr->bnapi;
7211 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7212 
7213 			ring = &cpr2->cp_ring_struct;
7214 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7215 			map_idx = bnapi->index;
7216 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7217 			if (rc)
7218 				goto err_out;
7219 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7220 				    ring->fw_ring_id);
7221 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7222 		}
7223 		ring = &txr->tx_ring_struct;
7224 		map_idx = i;
7225 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7226 		if (rc)
7227 			goto err_out;
7228 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
7229 	}
7230 
7231 	for (i = 0; i < bp->rx_nr_rings; i++) {
7232 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7233 
7234 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7235 		if (rc)
7236 			goto err_out;
7237 		/* If we have agg rings, post agg buffers first. */
7238 		if (!agg_rings)
7239 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7240 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7241 			struct bnxt_cp_ring_info *cpr2 = rxr->rx_cpr;
7242 			struct bnxt_napi *bnapi = rxr->bnapi;
7243 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7244 			struct bnxt_ring_struct *ring;
7245 			u32 map_idx = bnapi->index;
7246 
7247 			ring = &cpr2->cp_ring_struct;
7248 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7249 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7250 			if (rc)
7251 				goto err_out;
7252 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7253 				    ring->fw_ring_id);
7254 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7255 		}
7256 	}
7257 
7258 	if (agg_rings) {
7259 		for (i = 0; i < bp->rx_nr_rings; i++) {
7260 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7261 			if (rc)
7262 				goto err_out;
7263 		}
7264 	}
7265 err_out:
7266 	return rc;
7267 }
7268 
hwrm_ring_free_send_msg(struct bnxt * bp,struct bnxt_ring_struct * ring,u32 ring_type,int cmpl_ring_id)7269 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7270 				   struct bnxt_ring_struct *ring,
7271 				   u32 ring_type, int cmpl_ring_id)
7272 {
7273 	struct hwrm_ring_free_output *resp;
7274 	struct hwrm_ring_free_input *req;
7275 	u16 error_code = 0;
7276 	int rc;
7277 
7278 	if (BNXT_NO_FW_ACCESS(bp))
7279 		return 0;
7280 
7281 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7282 	if (rc)
7283 		goto exit;
7284 
7285 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7286 	req->ring_type = ring_type;
7287 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7288 
7289 	resp = hwrm_req_hold(bp, req);
7290 	rc = hwrm_req_send(bp, req);
7291 	error_code = le16_to_cpu(resp->error_code);
7292 	hwrm_req_drop(bp, req);
7293 exit:
7294 	if (rc || error_code) {
7295 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7296 			   ring_type, rc, error_code);
7297 		return -EIO;
7298 	}
7299 	return 0;
7300 }
7301 
bnxt_hwrm_rx_ring_free(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,bool close_path)7302 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7303 				   struct bnxt_rx_ring_info *rxr,
7304 				   bool close_path)
7305 {
7306 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7307 	u32 grp_idx = rxr->bnapi->index;
7308 	u32 cmpl_ring_id;
7309 
7310 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7311 		return;
7312 
7313 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7314 	hwrm_ring_free_send_msg(bp, ring,
7315 				RING_FREE_REQ_RING_TYPE_RX,
7316 				close_path ? cmpl_ring_id :
7317 				INVALID_HW_RING_ID);
7318 	ring->fw_ring_id = INVALID_HW_RING_ID;
7319 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7320 }
7321 
bnxt_hwrm_rx_agg_ring_free(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,bool close_path)7322 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7323 				       struct bnxt_rx_ring_info *rxr,
7324 				       bool close_path)
7325 {
7326 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7327 	u32 grp_idx = rxr->bnapi->index;
7328 	u32 type, cmpl_ring_id;
7329 
7330 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7331 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7332 	else
7333 		type = RING_FREE_REQ_RING_TYPE_RX;
7334 
7335 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7336 		return;
7337 
7338 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7339 	hwrm_ring_free_send_msg(bp, ring, type,
7340 				close_path ? cmpl_ring_id :
7341 				INVALID_HW_RING_ID);
7342 	ring->fw_ring_id = INVALID_HW_RING_ID;
7343 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7344 }
7345 
bnxt_hwrm_ring_free(struct bnxt * bp,bool close_path)7346 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7347 {
7348 	u32 type;
7349 	int i;
7350 
7351 	if (!bp->bnapi)
7352 		return;
7353 
7354 	for (i = 0; i < bp->tx_nr_rings; i++) {
7355 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7356 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7357 
7358 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7359 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
7360 
7361 			hwrm_ring_free_send_msg(bp, ring,
7362 						RING_FREE_REQ_RING_TYPE_TX,
7363 						close_path ? cmpl_ring_id :
7364 						INVALID_HW_RING_ID);
7365 			ring->fw_ring_id = INVALID_HW_RING_ID;
7366 		}
7367 	}
7368 
7369 	for (i = 0; i < bp->rx_nr_rings; i++) {
7370 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7371 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7372 	}
7373 
7374 	/* The completion rings are about to be freed.  After that the
7375 	 * IRQ doorbell will not work anymore.  So we need to disable
7376 	 * IRQ here.
7377 	 */
7378 	bnxt_disable_int_sync(bp);
7379 
7380 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7381 		type = RING_FREE_REQ_RING_TYPE_NQ;
7382 	else
7383 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7384 	for (i = 0; i < bp->cp_nr_rings; i++) {
7385 		struct bnxt_napi *bnapi = bp->bnapi[i];
7386 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7387 		struct bnxt_ring_struct *ring;
7388 		int j;
7389 
7390 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) {
7391 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
7392 
7393 			ring = &cpr2->cp_ring_struct;
7394 			if (ring->fw_ring_id == INVALID_HW_RING_ID)
7395 				continue;
7396 			hwrm_ring_free_send_msg(bp, ring,
7397 						RING_FREE_REQ_RING_TYPE_L2_CMPL,
7398 						INVALID_HW_RING_ID);
7399 			ring->fw_ring_id = INVALID_HW_RING_ID;
7400 		}
7401 		ring = &cpr->cp_ring_struct;
7402 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7403 			hwrm_ring_free_send_msg(bp, ring, type,
7404 						INVALID_HW_RING_ID);
7405 			ring->fw_ring_id = INVALID_HW_RING_ID;
7406 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7407 		}
7408 	}
7409 }
7410 
7411 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7412 			     bool shared);
7413 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7414 			   bool shared);
7415 
bnxt_hwrm_get_rings(struct bnxt * bp)7416 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7417 {
7418 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7419 	struct hwrm_func_qcfg_output *resp;
7420 	struct hwrm_func_qcfg_input *req;
7421 	int rc;
7422 
7423 	if (bp->hwrm_spec_code < 0x10601)
7424 		return 0;
7425 
7426 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7427 	if (rc)
7428 		return rc;
7429 
7430 	req->fid = cpu_to_le16(0xffff);
7431 	resp = hwrm_req_hold(bp, req);
7432 	rc = hwrm_req_send(bp, req);
7433 	if (rc) {
7434 		hwrm_req_drop(bp, req);
7435 		return rc;
7436 	}
7437 
7438 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7439 	if (BNXT_NEW_RM(bp)) {
7440 		u16 cp, stats;
7441 
7442 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7443 		hw_resc->resv_hw_ring_grps =
7444 			le32_to_cpu(resp->alloc_hw_ring_grps);
7445 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7446 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7447 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7448 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7449 		hw_resc->resv_irqs = cp;
7450 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7451 			int rx = hw_resc->resv_rx_rings;
7452 			int tx = hw_resc->resv_tx_rings;
7453 
7454 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7455 				rx >>= 1;
7456 			if (cp < (rx + tx)) {
7457 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7458 				if (rc)
7459 					goto get_rings_exit;
7460 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7461 					rx <<= 1;
7462 				hw_resc->resv_rx_rings = rx;
7463 				hw_resc->resv_tx_rings = tx;
7464 			}
7465 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7466 			hw_resc->resv_hw_ring_grps = rx;
7467 		}
7468 		hw_resc->resv_cp_rings = cp;
7469 		hw_resc->resv_stat_ctxs = stats;
7470 	}
7471 get_rings_exit:
7472 	hwrm_req_drop(bp, req);
7473 	return rc;
7474 }
7475 
__bnxt_hwrm_get_tx_rings(struct bnxt * bp,u16 fid,int * tx_rings)7476 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7477 {
7478 	struct hwrm_func_qcfg_output *resp;
7479 	struct hwrm_func_qcfg_input *req;
7480 	int rc;
7481 
7482 	if (bp->hwrm_spec_code < 0x10601)
7483 		return 0;
7484 
7485 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7486 	if (rc)
7487 		return rc;
7488 
7489 	req->fid = cpu_to_le16(fid);
7490 	resp = hwrm_req_hold(bp, req);
7491 	rc = hwrm_req_send(bp, req);
7492 	if (!rc)
7493 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7494 
7495 	hwrm_req_drop(bp, req);
7496 	return rc;
7497 }
7498 
7499 static bool bnxt_rfs_supported(struct bnxt *bp);
7500 
7501 static struct hwrm_func_cfg_input *
__bnxt_hwrm_reserve_pf_rings(struct bnxt * bp,struct bnxt_hw_rings * hwr)7502 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7503 {
7504 	struct hwrm_func_cfg_input *req;
7505 	u32 enables = 0;
7506 
7507 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7508 		return NULL;
7509 
7510 	req->fid = cpu_to_le16(0xffff);
7511 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7512 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7513 	if (BNXT_NEW_RM(bp)) {
7514 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7515 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7516 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7517 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7518 			enables |= hwr->cp_p5 ?
7519 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7520 		} else {
7521 			enables |= hwr->cp ?
7522 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7523 			enables |= hwr->grp ?
7524 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7525 		}
7526 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7527 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7528 					  0;
7529 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7530 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7531 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7532 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7533 			req->num_msix = cpu_to_le16(hwr->cp);
7534 		} else {
7535 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7536 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7537 		}
7538 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7539 		req->num_vnics = cpu_to_le16(hwr->vnic);
7540 	}
7541 	req->enables = cpu_to_le32(enables);
7542 	return req;
7543 }
7544 
7545 static struct hwrm_func_vf_cfg_input *
__bnxt_hwrm_reserve_vf_rings(struct bnxt * bp,struct bnxt_hw_rings * hwr)7546 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7547 {
7548 	struct hwrm_func_vf_cfg_input *req;
7549 	u32 enables = 0;
7550 
7551 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7552 		return NULL;
7553 
7554 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7555 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7556 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7557 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7558 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7559 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7560 		enables |= hwr->cp_p5 ?
7561 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7562 	} else {
7563 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7564 		enables |= hwr->grp ?
7565 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7566 	}
7567 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7568 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7569 
7570 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7571 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7572 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7573 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7574 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7575 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7576 	} else {
7577 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7578 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7579 	}
7580 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7581 	req->num_vnics = cpu_to_le16(hwr->vnic);
7582 
7583 	req->enables = cpu_to_le32(enables);
7584 	return req;
7585 }
7586 
7587 static int
bnxt_hwrm_reserve_pf_rings(struct bnxt * bp,struct bnxt_hw_rings * hwr)7588 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7589 {
7590 	struct hwrm_func_cfg_input *req;
7591 	int rc;
7592 
7593 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7594 	if (!req)
7595 		return -ENOMEM;
7596 
7597 	if (!req->enables) {
7598 		hwrm_req_drop(bp, req);
7599 		return 0;
7600 	}
7601 
7602 	rc = hwrm_req_send(bp, req);
7603 	if (rc)
7604 		return rc;
7605 
7606 	if (bp->hwrm_spec_code < 0x10601)
7607 		bp->hw_resc.resv_tx_rings = hwr->tx;
7608 
7609 	return bnxt_hwrm_get_rings(bp);
7610 }
7611 
7612 static int
bnxt_hwrm_reserve_vf_rings(struct bnxt * bp,struct bnxt_hw_rings * hwr)7613 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7614 {
7615 	struct hwrm_func_vf_cfg_input *req;
7616 	int rc;
7617 
7618 	if (!BNXT_NEW_RM(bp)) {
7619 		bp->hw_resc.resv_tx_rings = hwr->tx;
7620 		return 0;
7621 	}
7622 
7623 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7624 	if (!req)
7625 		return -ENOMEM;
7626 
7627 	rc = hwrm_req_send(bp, req);
7628 	if (rc)
7629 		return rc;
7630 
7631 	return bnxt_hwrm_get_rings(bp);
7632 }
7633 
bnxt_hwrm_reserve_rings(struct bnxt * bp,struct bnxt_hw_rings * hwr)7634 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7635 {
7636 	if (BNXT_PF(bp))
7637 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7638 	else
7639 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7640 }
7641 
bnxt_nq_rings_in_use(struct bnxt * bp)7642 int bnxt_nq_rings_in_use(struct bnxt *bp)
7643 {
7644 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7645 }
7646 
bnxt_cp_rings_in_use(struct bnxt * bp)7647 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7648 {
7649 	int cp;
7650 
7651 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7652 		return bnxt_nq_rings_in_use(bp);
7653 
7654 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7655 	return cp;
7656 }
7657 
bnxt_get_func_stat_ctxs(struct bnxt * bp)7658 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7659 {
7660 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7661 }
7662 
bnxt_get_total_rss_ctxs(struct bnxt * bp,struct bnxt_hw_rings * hwr)7663 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7664 {
7665 	if (!hwr->grp)
7666 		return 0;
7667 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7668 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7669 
7670 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7671 			rss_ctx *= hwr->vnic;
7672 		return rss_ctx;
7673 	}
7674 	if (BNXT_VF(bp))
7675 		return BNXT_VF_MAX_RSS_CTX;
7676 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7677 		return hwr->grp + 1;
7678 	return 1;
7679 }
7680 
7681 /* Check if a default RSS map needs to be setup.  This function is only
7682  * used on older firmware that does not require reserving RX rings.
7683  */
bnxt_check_rss_tbl_no_rmgr(struct bnxt * bp)7684 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7685 {
7686 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7687 
7688 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7689 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7690 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7691 		if (!netif_is_rxfh_configured(bp->dev))
7692 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7693 	}
7694 }
7695 
bnxt_get_total_vnics(struct bnxt * bp,int rx_rings)7696 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7697 {
7698 	if (bp->flags & BNXT_FLAG_RFS) {
7699 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7700 			return 2 + bp->num_rss_ctx;
7701 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7702 			return rx_rings + 1;
7703 	}
7704 	return 1;
7705 }
7706 
bnxt_need_reserve_rings(struct bnxt * bp)7707 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7708 {
7709 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7710 	int cp = bnxt_cp_rings_in_use(bp);
7711 	int nq = bnxt_nq_rings_in_use(bp);
7712 	int rx = bp->rx_nr_rings, stat;
7713 	int vnic, grp = rx;
7714 
7715 	/* Old firmware does not need RX ring reservations but we still
7716 	 * need to setup a default RSS map when needed.  With new firmware
7717 	 * we go through RX ring reservations first and then set up the
7718 	 * RSS map for the successfully reserved RX rings when needed.
7719 	 */
7720 	if (!BNXT_NEW_RM(bp))
7721 		bnxt_check_rss_tbl_no_rmgr(bp);
7722 
7723 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7724 	    bp->hwrm_spec_code >= 0x10601)
7725 		return true;
7726 
7727 	if (!BNXT_NEW_RM(bp))
7728 		return false;
7729 
7730 	vnic = bnxt_get_total_vnics(bp, rx);
7731 
7732 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7733 		rx <<= 1;
7734 	stat = bnxt_get_func_stat_ctxs(bp);
7735 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7736 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7737 	    (hw_resc->resv_hw_ring_grps != grp &&
7738 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7739 		return true;
7740 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7741 	    hw_resc->resv_irqs != nq)
7742 		return true;
7743 	return false;
7744 }
7745 
bnxt_copy_reserved_rings(struct bnxt * bp,struct bnxt_hw_rings * hwr)7746 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7747 {
7748 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7749 
7750 	hwr->tx = hw_resc->resv_tx_rings;
7751 	if (BNXT_NEW_RM(bp)) {
7752 		hwr->rx = hw_resc->resv_rx_rings;
7753 		hwr->cp = hw_resc->resv_irqs;
7754 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7755 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7756 		hwr->grp = hw_resc->resv_hw_ring_grps;
7757 		hwr->vnic = hw_resc->resv_vnics;
7758 		hwr->stat = hw_resc->resv_stat_ctxs;
7759 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7760 	}
7761 }
7762 
bnxt_rings_ok(struct bnxt * bp,struct bnxt_hw_rings * hwr)7763 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7764 {
7765 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7766 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7767 }
7768 
7769 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7770 
__bnxt_reserve_rings(struct bnxt * bp)7771 static int __bnxt_reserve_rings(struct bnxt *bp)
7772 {
7773 	struct bnxt_hw_rings hwr = {0};
7774 	int rx_rings, old_rx_rings, rc;
7775 	int cp = bp->cp_nr_rings;
7776 	int ulp_msix = 0;
7777 	bool sh = false;
7778 	int tx_cp;
7779 
7780 	if (!bnxt_need_reserve_rings(bp))
7781 		return 0;
7782 
7783 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7784 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7785 		if (!ulp_msix)
7786 			bnxt_set_ulp_stat_ctxs(bp, 0);
7787 
7788 		if (ulp_msix > bp->ulp_num_msix_want)
7789 			ulp_msix = bp->ulp_num_msix_want;
7790 		hwr.cp = cp + ulp_msix;
7791 	} else {
7792 		hwr.cp = bnxt_nq_rings_in_use(bp);
7793 	}
7794 
7795 	hwr.tx = bp->tx_nr_rings;
7796 	hwr.rx = bp->rx_nr_rings;
7797 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7798 		sh = true;
7799 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7800 		hwr.cp_p5 = hwr.rx + hwr.tx;
7801 
7802 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7803 
7804 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7805 		hwr.rx <<= 1;
7806 	hwr.grp = bp->rx_nr_rings;
7807 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7808 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7809 	old_rx_rings = bp->hw_resc.resv_rx_rings;
7810 
7811 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7812 	if (rc)
7813 		return rc;
7814 
7815 	bnxt_copy_reserved_rings(bp, &hwr);
7816 
7817 	rx_rings = hwr.rx;
7818 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7819 		if (hwr.rx >= 2) {
7820 			rx_rings = hwr.rx >> 1;
7821 		} else {
7822 			if (netif_running(bp->dev))
7823 				return -ENOMEM;
7824 
7825 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7826 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7827 			bp->dev->hw_features &= ~NETIF_F_LRO;
7828 			bp->dev->features &= ~NETIF_F_LRO;
7829 			bnxt_set_ring_params(bp);
7830 		}
7831 	}
7832 	rx_rings = min_t(int, rx_rings, hwr.grp);
7833 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7834 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7835 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7836 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7837 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7838 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7839 		hwr.rx = rx_rings << 1;
7840 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
7841 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
7842 	bp->tx_nr_rings = hwr.tx;
7843 
7844 	/* If we cannot reserve all the RX rings, reset the RSS map only
7845 	 * if absolutely necessary
7846 	 */
7847 	if (rx_rings != bp->rx_nr_rings) {
7848 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
7849 			    rx_rings, bp->rx_nr_rings);
7850 		if (netif_is_rxfh_configured(bp->dev) &&
7851 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
7852 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
7853 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
7854 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
7855 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
7856 		}
7857 	}
7858 	bp->rx_nr_rings = rx_rings;
7859 	bp->cp_nr_rings = hwr.cp;
7860 
7861 	if (!bnxt_rings_ok(bp, &hwr))
7862 		return -ENOMEM;
7863 
7864 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
7865 	    !netif_is_rxfh_configured(bp->dev))
7866 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7867 
7868 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
7869 		int resv_msix, resv_ctx, ulp_ctxs;
7870 		struct bnxt_hw_resc *hw_resc;
7871 
7872 		hw_resc = &bp->hw_resc;
7873 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
7874 		ulp_msix = min_t(int, resv_msix, ulp_msix);
7875 		bnxt_set_ulp_msix_num(bp, ulp_msix);
7876 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
7877 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
7878 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
7879 	}
7880 
7881 	return rc;
7882 }
7883 
bnxt_hwrm_check_vf_rings(struct bnxt * bp,struct bnxt_hw_rings * hwr)7884 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7885 {
7886 	struct hwrm_func_vf_cfg_input *req;
7887 	u32 flags;
7888 
7889 	if (!BNXT_NEW_RM(bp))
7890 		return 0;
7891 
7892 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7893 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
7894 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7895 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7896 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7897 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
7898 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
7899 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7900 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7901 
7902 	req->flags = cpu_to_le32(flags);
7903 	return hwrm_req_send_silent(bp, req);
7904 }
7905 
bnxt_hwrm_check_pf_rings(struct bnxt * bp,struct bnxt_hw_rings * hwr)7906 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7907 {
7908 	struct hwrm_func_cfg_input *req;
7909 	u32 flags;
7910 
7911 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7912 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
7913 	if (BNXT_NEW_RM(bp)) {
7914 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7915 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7916 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7917 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
7918 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7919 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
7920 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
7921 		else
7922 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7923 	}
7924 
7925 	req->flags = cpu_to_le32(flags);
7926 	return hwrm_req_send_silent(bp, req);
7927 }
7928 
bnxt_hwrm_check_rings(struct bnxt * bp,struct bnxt_hw_rings * hwr)7929 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7930 {
7931 	if (bp->hwrm_spec_code < 0x10801)
7932 		return 0;
7933 
7934 	if (BNXT_PF(bp))
7935 		return bnxt_hwrm_check_pf_rings(bp, hwr);
7936 
7937 	return bnxt_hwrm_check_vf_rings(bp, hwr);
7938 }
7939 
bnxt_hwrm_coal_params_qcaps(struct bnxt * bp)7940 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
7941 {
7942 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7943 	struct hwrm_ring_aggint_qcaps_output *resp;
7944 	struct hwrm_ring_aggint_qcaps_input *req;
7945 	int rc;
7946 
7947 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
7948 	coal_cap->num_cmpl_dma_aggr_max = 63;
7949 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
7950 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
7951 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
7952 	coal_cap->int_lat_tmr_min_max = 65535;
7953 	coal_cap->int_lat_tmr_max_max = 65535;
7954 	coal_cap->num_cmpl_aggr_int_max = 65535;
7955 	coal_cap->timer_units = 80;
7956 
7957 	if (bp->hwrm_spec_code < 0x10902)
7958 		return;
7959 
7960 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
7961 		return;
7962 
7963 	resp = hwrm_req_hold(bp, req);
7964 	rc = hwrm_req_send_silent(bp, req);
7965 	if (!rc) {
7966 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
7967 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
7968 		coal_cap->num_cmpl_dma_aggr_max =
7969 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
7970 		coal_cap->num_cmpl_dma_aggr_during_int_max =
7971 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
7972 		coal_cap->cmpl_aggr_dma_tmr_max =
7973 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
7974 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
7975 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
7976 		coal_cap->int_lat_tmr_min_max =
7977 			le16_to_cpu(resp->int_lat_tmr_min_max);
7978 		coal_cap->int_lat_tmr_max_max =
7979 			le16_to_cpu(resp->int_lat_tmr_max_max);
7980 		coal_cap->num_cmpl_aggr_int_max =
7981 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
7982 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
7983 	}
7984 	hwrm_req_drop(bp, req);
7985 }
7986 
bnxt_usec_to_coal_tmr(struct bnxt * bp,u16 usec)7987 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
7988 {
7989 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7990 
7991 	return usec * 1000 / coal_cap->timer_units;
7992 }
7993 
bnxt_hwrm_set_coal_params(struct bnxt * bp,struct bnxt_coal * hw_coal,struct hwrm_ring_cmpl_ring_cfg_aggint_params_input * req)7994 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
7995 	struct bnxt_coal *hw_coal,
7996 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
7997 {
7998 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7999 	u16 val, tmr, max, flags = hw_coal->flags;
8000 	u32 cmpl_params = coal_cap->cmpl_params;
8001 
8002 	max = hw_coal->bufs_per_record * 128;
8003 	if (hw_coal->budget)
8004 		max = hw_coal->bufs_per_record * hw_coal->budget;
8005 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8006 
8007 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8008 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8009 
8010 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8011 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8012 
8013 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8014 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8015 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8016 
8017 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8018 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8019 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8020 
8021 	/* min timer set to 1/2 of interrupt timer */
8022 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8023 		val = tmr / 2;
8024 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8025 		req->int_lat_tmr_min = cpu_to_le16(val);
8026 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8027 	}
8028 
8029 	/* buf timer set to 1/4 of interrupt timer */
8030 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8031 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8032 
8033 	if (cmpl_params &
8034 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8035 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8036 		val = clamp_t(u16, tmr, 1,
8037 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8038 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8039 		req->enables |=
8040 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8041 	}
8042 
8043 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8044 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8045 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8046 	req->flags = cpu_to_le16(flags);
8047 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8048 }
8049 
__bnxt_hwrm_set_coal_nq(struct bnxt * bp,struct bnxt_napi * bnapi,struct bnxt_coal * hw_coal)8050 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8051 				   struct bnxt_coal *hw_coal)
8052 {
8053 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8054 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8055 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8056 	u32 nq_params = coal_cap->nq_params;
8057 	u16 tmr;
8058 	int rc;
8059 
8060 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8061 		return 0;
8062 
8063 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8064 	if (rc)
8065 		return rc;
8066 
8067 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8068 	req->flags =
8069 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8070 
8071 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8072 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8073 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8074 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8075 	return hwrm_req_send(bp, req);
8076 }
8077 
bnxt_hwrm_set_ring_coal(struct bnxt * bp,struct bnxt_napi * bnapi)8078 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8079 {
8080 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8081 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8082 	struct bnxt_coal coal;
8083 	int rc;
8084 
8085 	/* Tick values in micro seconds.
8086 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8087 	 */
8088 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8089 
8090 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8091 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8092 
8093 	if (!bnapi->rx_ring)
8094 		return -ENODEV;
8095 
8096 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8097 	if (rc)
8098 		return rc;
8099 
8100 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8101 
8102 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8103 
8104 	return hwrm_req_send(bp, req_rx);
8105 }
8106 
8107 static int
bnxt_hwrm_set_rx_coal(struct bnxt * bp,struct bnxt_napi * bnapi,struct hwrm_ring_cmpl_ring_cfg_aggint_params_input * req)8108 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8109 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8110 {
8111 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8112 
8113 	req->ring_id = cpu_to_le16(ring_id);
8114 	return hwrm_req_send(bp, req);
8115 }
8116 
8117 static int
bnxt_hwrm_set_tx_coal(struct bnxt * bp,struct bnxt_napi * bnapi,struct hwrm_ring_cmpl_ring_cfg_aggint_params_input * req)8118 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8119 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8120 {
8121 	struct bnxt_tx_ring_info *txr;
8122 	int i, rc;
8123 
8124 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8125 		u16 ring_id;
8126 
8127 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8128 		req->ring_id = cpu_to_le16(ring_id);
8129 		rc = hwrm_req_send(bp, req);
8130 		if (rc)
8131 			return rc;
8132 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8133 			return 0;
8134 	}
8135 	return 0;
8136 }
8137 
bnxt_hwrm_set_coal(struct bnxt * bp)8138 int bnxt_hwrm_set_coal(struct bnxt *bp)
8139 {
8140 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8141 	int i, rc;
8142 
8143 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8144 	if (rc)
8145 		return rc;
8146 
8147 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8148 	if (rc) {
8149 		hwrm_req_drop(bp, req_rx);
8150 		return rc;
8151 	}
8152 
8153 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8154 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8155 
8156 	hwrm_req_hold(bp, req_rx);
8157 	hwrm_req_hold(bp, req_tx);
8158 	for (i = 0; i < bp->cp_nr_rings; i++) {
8159 		struct bnxt_napi *bnapi = bp->bnapi[i];
8160 		struct bnxt_coal *hw_coal;
8161 
8162 		if (!bnapi->rx_ring)
8163 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8164 		else
8165 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8166 		if (rc)
8167 			break;
8168 
8169 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8170 			continue;
8171 
8172 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8173 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8174 			if (rc)
8175 				break;
8176 		}
8177 		if (bnapi->rx_ring)
8178 			hw_coal = &bp->rx_coal;
8179 		else
8180 			hw_coal = &bp->tx_coal;
8181 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8182 	}
8183 	hwrm_req_drop(bp, req_rx);
8184 	hwrm_req_drop(bp, req_tx);
8185 	return rc;
8186 }
8187 
bnxt_hwrm_stat_ctx_free(struct bnxt * bp)8188 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8189 {
8190 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8191 	struct hwrm_stat_ctx_free_input *req;
8192 	int i;
8193 
8194 	if (!bp->bnapi)
8195 		return;
8196 
8197 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8198 		return;
8199 
8200 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8201 		return;
8202 	if (BNXT_FW_MAJ(bp) <= 20) {
8203 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8204 			hwrm_req_drop(bp, req);
8205 			return;
8206 		}
8207 		hwrm_req_hold(bp, req0);
8208 	}
8209 	hwrm_req_hold(bp, req);
8210 	for (i = 0; i < bp->cp_nr_rings; i++) {
8211 		struct bnxt_napi *bnapi = bp->bnapi[i];
8212 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8213 
8214 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8215 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8216 			if (req0) {
8217 				req0->stat_ctx_id = req->stat_ctx_id;
8218 				hwrm_req_send(bp, req0);
8219 			}
8220 			hwrm_req_send(bp, req);
8221 
8222 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8223 		}
8224 	}
8225 	hwrm_req_drop(bp, req);
8226 	if (req0)
8227 		hwrm_req_drop(bp, req0);
8228 }
8229 
bnxt_hwrm_stat_ctx_alloc(struct bnxt * bp)8230 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8231 {
8232 	struct hwrm_stat_ctx_alloc_output *resp;
8233 	struct hwrm_stat_ctx_alloc_input *req;
8234 	int rc, i;
8235 
8236 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8237 		return 0;
8238 
8239 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8240 	if (rc)
8241 		return rc;
8242 
8243 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8244 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8245 
8246 	resp = hwrm_req_hold(bp, req);
8247 	for (i = 0; i < bp->cp_nr_rings; i++) {
8248 		struct bnxt_napi *bnapi = bp->bnapi[i];
8249 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8250 
8251 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8252 
8253 		rc = hwrm_req_send(bp, req);
8254 		if (rc)
8255 			break;
8256 
8257 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8258 
8259 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8260 	}
8261 	hwrm_req_drop(bp, req);
8262 	return rc;
8263 }
8264 
bnxt_hwrm_func_qcfg(struct bnxt * bp)8265 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8266 {
8267 	struct hwrm_func_qcfg_output *resp;
8268 	struct hwrm_func_qcfg_input *req;
8269 	u16 flags;
8270 	int rc;
8271 
8272 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8273 	if (rc)
8274 		return rc;
8275 
8276 	req->fid = cpu_to_le16(0xffff);
8277 	resp = hwrm_req_hold(bp, req);
8278 	rc = hwrm_req_send(bp, req);
8279 	if (rc)
8280 		goto func_qcfg_exit;
8281 
8282 #ifdef CONFIG_BNXT_SRIOV
8283 	if (BNXT_VF(bp)) {
8284 		struct bnxt_vf_info *vf = &bp->vf;
8285 
8286 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8287 	} else {
8288 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8289 	}
8290 #endif
8291 	flags = le16_to_cpu(resp->flags);
8292 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8293 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8294 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8295 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8296 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8297 	}
8298 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8299 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8300 
8301 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8302 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8303 
8304 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8305 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8306 
8307 	switch (resp->port_partition_type) {
8308 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8309 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8310 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8311 		bp->port_partition_type = resp->port_partition_type;
8312 		break;
8313 	}
8314 	if (bp->hwrm_spec_code < 0x10707 ||
8315 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8316 		bp->br_mode = BRIDGE_MODE_VEB;
8317 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8318 		bp->br_mode = BRIDGE_MODE_VEPA;
8319 	else
8320 		bp->br_mode = BRIDGE_MODE_UNDEF;
8321 
8322 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8323 	if (!bp->max_mtu)
8324 		bp->max_mtu = BNXT_MAX_MTU;
8325 
8326 	if (bp->db_size)
8327 		goto func_qcfg_exit;
8328 
8329 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8330 	if (BNXT_CHIP_P5(bp)) {
8331 		if (BNXT_PF(bp))
8332 			bp->db_offset = DB_PF_OFFSET_P5;
8333 		else
8334 			bp->db_offset = DB_VF_OFFSET_P5;
8335 	}
8336 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8337 				 1024);
8338 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8339 	    bp->db_size <= bp->db_offset)
8340 		bp->db_size = pci_resource_len(bp->pdev, 2);
8341 
8342 func_qcfg_exit:
8343 	hwrm_req_drop(bp, req);
8344 	return rc;
8345 }
8346 
bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type * ctxm,u8 init_val,u8 init_offset,bool init_mask_set)8347 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8348 				      u8 init_val, u8 init_offset,
8349 				      bool init_mask_set)
8350 {
8351 	ctxm->init_value = init_val;
8352 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8353 	if (init_mask_set)
8354 		ctxm->init_offset = init_offset * 4;
8355 	else
8356 		ctxm->init_value = 0;
8357 }
8358 
bnxt_alloc_all_ctx_pg_info(struct bnxt * bp,int ctx_max)8359 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8360 {
8361 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8362 	u16 type;
8363 
8364 	for (type = 0; type < ctx_max; type++) {
8365 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8366 		int n = 1;
8367 
8368 		if (!ctxm->max_entries || ctxm->pg_info)
8369 			continue;
8370 
8371 		if (ctxm->instance_bmap)
8372 			n = hweight32(ctxm->instance_bmap);
8373 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8374 		if (!ctxm->pg_info)
8375 			return -ENOMEM;
8376 	}
8377 	return 0;
8378 }
8379 
8380 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8381 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8382 
8383 #define BNXT_CTX_INIT_VALID(flags)	\
8384 	(!!((flags) &			\
8385 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8386 
bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt * bp)8387 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8388 {
8389 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8390 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8391 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8392 	u16 type;
8393 	int rc;
8394 
8395 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8396 	if (rc)
8397 		return rc;
8398 
8399 	if (!ctx) {
8400 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8401 		if (!ctx)
8402 			return -ENOMEM;
8403 		bp->ctx = ctx;
8404 	}
8405 
8406 	resp = hwrm_req_hold(bp, req);
8407 
8408 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8409 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8410 		u8 init_val, init_off, i;
8411 		u32 max_entries;
8412 		u16 entry_size;
8413 		__le32 *p;
8414 		u32 flags;
8415 
8416 		req->type = cpu_to_le16(type);
8417 		rc = hwrm_req_send(bp, req);
8418 		if (rc)
8419 			goto ctx_done;
8420 		flags = le32_to_cpu(resp->flags);
8421 		type = le16_to_cpu(resp->next_valid_type);
8422 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8423 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8424 			continue;
8425 		}
8426 		entry_size = le16_to_cpu(resp->entry_size);
8427 		max_entries = le32_to_cpu(resp->max_num_entries);
8428 		if (ctxm->mem_valid) {
8429 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8430 			    ctxm->entry_size != entry_size ||
8431 			    ctxm->max_entries != max_entries)
8432 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8433 			else
8434 				continue;
8435 		}
8436 		ctxm->type = le16_to_cpu(resp->type);
8437 		ctxm->entry_size = entry_size;
8438 		ctxm->flags = flags;
8439 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8440 		ctxm->entry_multiple = resp->entry_multiple;
8441 		ctxm->max_entries = max_entries;
8442 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8443 		init_val = resp->ctx_init_value;
8444 		init_off = resp->ctx_init_offset;
8445 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8446 					  BNXT_CTX_INIT_VALID(flags));
8447 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8448 					      BNXT_MAX_SPLIT_ENTRY);
8449 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8450 		     i++, p++)
8451 			ctxm->split[i] = le32_to_cpu(*p);
8452 	}
8453 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8454 
8455 ctx_done:
8456 	hwrm_req_drop(bp, req);
8457 	return rc;
8458 }
8459 
bnxt_hwrm_func_backing_store_qcaps(struct bnxt * bp)8460 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8461 {
8462 	struct hwrm_func_backing_store_qcaps_output *resp;
8463 	struct hwrm_func_backing_store_qcaps_input *req;
8464 	int rc;
8465 
8466 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8467 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8468 		return 0;
8469 
8470 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8471 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8472 
8473 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8474 	if (rc)
8475 		return rc;
8476 
8477 	resp = hwrm_req_hold(bp, req);
8478 	rc = hwrm_req_send_silent(bp, req);
8479 	if (!rc) {
8480 		struct bnxt_ctx_mem_type *ctxm;
8481 		struct bnxt_ctx_mem_info *ctx;
8482 		u8 init_val, init_idx = 0;
8483 		u16 init_mask;
8484 
8485 		ctx = bp->ctx;
8486 		if (!ctx) {
8487 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8488 			if (!ctx) {
8489 				rc = -ENOMEM;
8490 				goto ctx_err;
8491 			}
8492 			bp->ctx = ctx;
8493 		}
8494 		init_val = resp->ctx_kind_initializer;
8495 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8496 
8497 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8498 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8499 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8500 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8501 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8502 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8503 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8504 					  (init_mask & (1 << init_idx++)) != 0);
8505 
8506 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8507 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8508 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8509 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8510 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8511 					  (init_mask & (1 << init_idx++)) != 0);
8512 
8513 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8514 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8515 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8516 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8517 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8518 					  (init_mask & (1 << init_idx++)) != 0);
8519 
8520 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8521 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8522 		ctxm->max_entries = ctxm->vnic_entries +
8523 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8524 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8525 		bnxt_init_ctx_initializer(ctxm, init_val,
8526 					  resp->vnic_init_offset,
8527 					  (init_mask & (1 << init_idx++)) != 0);
8528 
8529 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8530 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8531 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8532 		bnxt_init_ctx_initializer(ctxm, init_val,
8533 					  resp->stat_init_offset,
8534 					  (init_mask & (1 << init_idx++)) != 0);
8535 
8536 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8537 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8538 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8539 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8540 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8541 		if (!ctxm->entry_multiple)
8542 			ctxm->entry_multiple = 1;
8543 
8544 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8545 
8546 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8547 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8548 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8549 		ctxm->mrav_num_entries_units =
8550 			le16_to_cpu(resp->mrav_num_entries_units);
8551 		bnxt_init_ctx_initializer(ctxm, init_val,
8552 					  resp->mrav_init_offset,
8553 					  (init_mask & (1 << init_idx++)) != 0);
8554 
8555 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8556 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8557 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8558 
8559 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8560 		if (!ctx->tqm_fp_rings_count)
8561 			ctx->tqm_fp_rings_count = bp->max_q;
8562 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8563 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8564 
8565 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8566 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8567 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8568 
8569 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8570 	} else {
8571 		rc = 0;
8572 	}
8573 ctx_err:
8574 	hwrm_req_drop(bp, req);
8575 	return rc;
8576 }
8577 
bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info * rmem,u8 * pg_attr,__le64 * pg_dir)8578 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8579 				  __le64 *pg_dir)
8580 {
8581 	if (!rmem->nr_pages)
8582 		return;
8583 
8584 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8585 	if (rmem->depth >= 1) {
8586 		if (rmem->depth == 2)
8587 			*pg_attr |= 2;
8588 		else
8589 			*pg_attr |= 1;
8590 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8591 	} else {
8592 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8593 	}
8594 }
8595 
8596 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8597 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8598 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8599 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8600 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8601 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8602 
bnxt_hwrm_func_backing_store_cfg(struct bnxt * bp,u32 enables)8603 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8604 {
8605 	struct hwrm_func_backing_store_cfg_input *req;
8606 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8607 	struct bnxt_ctx_pg_info *ctx_pg;
8608 	struct bnxt_ctx_mem_type *ctxm;
8609 	void **__req = (void **)&req;
8610 	u32 req_len = sizeof(*req);
8611 	__le32 *num_entries;
8612 	__le64 *pg_dir;
8613 	u32 flags = 0;
8614 	u8 *pg_attr;
8615 	u32 ena;
8616 	int rc;
8617 	int i;
8618 
8619 	if (!ctx)
8620 		return 0;
8621 
8622 	if (req_len > bp->hwrm_max_ext_req_len)
8623 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8624 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8625 	if (rc)
8626 		return rc;
8627 
8628 	req->enables = cpu_to_le32(enables);
8629 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8630 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8631 		ctx_pg = ctxm->pg_info;
8632 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8633 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8634 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8635 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8636 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8637 				      &req->qpc_pg_size_qpc_lvl,
8638 				      &req->qpc_page_dir);
8639 
8640 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8641 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8642 	}
8643 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8644 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8645 		ctx_pg = ctxm->pg_info;
8646 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8647 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8648 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8649 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8650 				      &req->srq_pg_size_srq_lvl,
8651 				      &req->srq_page_dir);
8652 	}
8653 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8654 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8655 		ctx_pg = ctxm->pg_info;
8656 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8657 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8658 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8659 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8660 				      &req->cq_pg_size_cq_lvl,
8661 				      &req->cq_page_dir);
8662 	}
8663 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8664 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8665 		ctx_pg = ctxm->pg_info;
8666 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8667 		req->vnic_num_ring_table_entries =
8668 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8669 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8670 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8671 				      &req->vnic_pg_size_vnic_lvl,
8672 				      &req->vnic_page_dir);
8673 	}
8674 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8675 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8676 		ctx_pg = ctxm->pg_info;
8677 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8678 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8679 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8680 				      &req->stat_pg_size_stat_lvl,
8681 				      &req->stat_page_dir);
8682 	}
8683 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8684 		u32 units;
8685 
8686 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8687 		ctx_pg = ctxm->pg_info;
8688 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8689 		units = ctxm->mrav_num_entries_units;
8690 		if (units) {
8691 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8692 			u32 entries;
8693 
8694 			num_mr = ctx_pg->entries - num_ah;
8695 			entries = ((num_mr / units) << 16) | (num_ah / units);
8696 			req->mrav_num_entries = cpu_to_le32(entries);
8697 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8698 		}
8699 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8700 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8701 				      &req->mrav_pg_size_mrav_lvl,
8702 				      &req->mrav_page_dir);
8703 	}
8704 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8705 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8706 		ctx_pg = ctxm->pg_info;
8707 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8708 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8709 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8710 				      &req->tim_pg_size_tim_lvl,
8711 				      &req->tim_page_dir);
8712 	}
8713 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8714 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8715 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8716 	     pg_dir = &req->tqm_sp_page_dir,
8717 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8718 	     ctx_pg = ctxm->pg_info;
8719 	     i < BNXT_MAX_TQM_RINGS;
8720 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8721 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8722 		if (!(enables & ena))
8723 			continue;
8724 
8725 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8726 		*num_entries = cpu_to_le32(ctx_pg->entries);
8727 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8728 	}
8729 	req->flags = cpu_to_le32(flags);
8730 	return hwrm_req_send(bp, req);
8731 }
8732 
bnxt_alloc_ctx_mem_blk(struct bnxt * bp,struct bnxt_ctx_pg_info * ctx_pg)8733 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8734 				  struct bnxt_ctx_pg_info *ctx_pg)
8735 {
8736 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8737 
8738 	rmem->page_size = BNXT_PAGE_SIZE;
8739 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8740 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8741 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8742 	if (rmem->depth >= 1)
8743 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8744 	return bnxt_alloc_ring(bp, rmem);
8745 }
8746 
bnxt_alloc_ctx_pg_tbls(struct bnxt * bp,struct bnxt_ctx_pg_info * ctx_pg,u32 mem_size,u8 depth,struct bnxt_ctx_mem_type * ctxm)8747 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8748 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8749 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8750 {
8751 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8752 	int rc;
8753 
8754 	if (!mem_size)
8755 		return -EINVAL;
8756 
8757 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8758 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8759 		ctx_pg->nr_pages = 0;
8760 		return -EINVAL;
8761 	}
8762 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8763 		int nr_tbls, i;
8764 
8765 		rmem->depth = 2;
8766 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8767 					     GFP_KERNEL);
8768 		if (!ctx_pg->ctx_pg_tbl)
8769 			return -ENOMEM;
8770 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8771 		rmem->nr_pages = nr_tbls;
8772 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8773 		if (rc)
8774 			return rc;
8775 		for (i = 0; i < nr_tbls; i++) {
8776 			struct bnxt_ctx_pg_info *pg_tbl;
8777 
8778 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8779 			if (!pg_tbl)
8780 				return -ENOMEM;
8781 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8782 			rmem = &pg_tbl->ring_mem;
8783 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8784 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8785 			rmem->depth = 1;
8786 			rmem->nr_pages = MAX_CTX_PAGES;
8787 			rmem->ctx_mem = ctxm;
8788 			if (i == (nr_tbls - 1)) {
8789 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8790 
8791 				if (rem)
8792 					rmem->nr_pages = rem;
8793 			}
8794 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8795 			if (rc)
8796 				break;
8797 		}
8798 	} else {
8799 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8800 		if (rmem->nr_pages > 1 || depth)
8801 			rmem->depth = 1;
8802 		rmem->ctx_mem = ctxm;
8803 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8804 	}
8805 	return rc;
8806 }
8807 
bnxt_copy_ctx_pg_tbls(struct bnxt * bp,struct bnxt_ctx_pg_info * ctx_pg,void * buf,size_t offset,size_t head,size_t tail)8808 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
8809 				    struct bnxt_ctx_pg_info *ctx_pg,
8810 				    void *buf, size_t offset, size_t head,
8811 				    size_t tail)
8812 {
8813 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8814 	size_t nr_pages = ctx_pg->nr_pages;
8815 	int page_size = rmem->page_size;
8816 	size_t len = 0, total_len = 0;
8817 	u16 depth = rmem->depth;
8818 
8819 	tail %= nr_pages * page_size;
8820 	do {
8821 		if (depth > 1) {
8822 			int i = head / (page_size * MAX_CTX_PAGES);
8823 			struct bnxt_ctx_pg_info *pg_tbl;
8824 
8825 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8826 			rmem = &pg_tbl->ring_mem;
8827 		}
8828 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
8829 		head += len;
8830 		offset += len;
8831 		total_len += len;
8832 		if (head >= nr_pages * page_size)
8833 			head = 0;
8834 	} while (head != tail);
8835 	return total_len;
8836 }
8837 
bnxt_free_ctx_pg_tbls(struct bnxt * bp,struct bnxt_ctx_pg_info * ctx_pg)8838 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
8839 				  struct bnxt_ctx_pg_info *ctx_pg)
8840 {
8841 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8842 
8843 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
8844 	    ctx_pg->ctx_pg_tbl) {
8845 		int i, nr_tbls = rmem->nr_pages;
8846 
8847 		for (i = 0; i < nr_tbls; i++) {
8848 			struct bnxt_ctx_pg_info *pg_tbl;
8849 			struct bnxt_ring_mem_info *rmem2;
8850 
8851 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8852 			if (!pg_tbl)
8853 				continue;
8854 			rmem2 = &pg_tbl->ring_mem;
8855 			bnxt_free_ring(bp, rmem2);
8856 			ctx_pg->ctx_pg_arr[i] = NULL;
8857 			kfree(pg_tbl);
8858 			ctx_pg->ctx_pg_tbl[i] = NULL;
8859 		}
8860 		kfree(ctx_pg->ctx_pg_tbl);
8861 		ctx_pg->ctx_pg_tbl = NULL;
8862 	}
8863 	bnxt_free_ring(bp, rmem);
8864 	ctx_pg->nr_pages = 0;
8865 }
8866 
bnxt_setup_ctxm_pg_tbls(struct bnxt * bp,struct bnxt_ctx_mem_type * ctxm,u32 entries,u8 pg_lvl)8867 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
8868 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
8869 				   u8 pg_lvl)
8870 {
8871 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8872 	int i, rc = 0, n = 1;
8873 	u32 mem_size;
8874 
8875 	if (!ctxm->entry_size || !ctx_pg)
8876 		return -EINVAL;
8877 	if (ctxm->instance_bmap)
8878 		n = hweight32(ctxm->instance_bmap);
8879 	if (ctxm->entry_multiple)
8880 		entries = roundup(entries, ctxm->entry_multiple);
8881 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
8882 	mem_size = entries * ctxm->entry_size;
8883 	for (i = 0; i < n && !rc; i++) {
8884 		ctx_pg[i].entries = entries;
8885 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
8886 					    ctxm->init_value ? ctxm : NULL);
8887 	}
8888 	if (!rc)
8889 		ctxm->mem_valid = 1;
8890 	return rc;
8891 }
8892 
bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt * bp,struct bnxt_ctx_mem_type * ctxm,bool last)8893 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
8894 					       struct bnxt_ctx_mem_type *ctxm,
8895 					       bool last)
8896 {
8897 	struct hwrm_func_backing_store_cfg_v2_input *req;
8898 	u32 instance_bmap = ctxm->instance_bmap;
8899 	int i, j, rc = 0, n = 1;
8900 	__le32 *p;
8901 
8902 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
8903 		return 0;
8904 
8905 	if (instance_bmap)
8906 		n = hweight32(ctxm->instance_bmap);
8907 	else
8908 		instance_bmap = 1;
8909 
8910 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
8911 	if (rc)
8912 		return rc;
8913 	hwrm_req_hold(bp, req);
8914 	req->type = cpu_to_le16(ctxm->type);
8915 	req->entry_size = cpu_to_le16(ctxm->entry_size);
8916 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
8917 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
8918 		struct bnxt_bs_trace_info *bs_trace;
8919 		u32 enables;
8920 
8921 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
8922 		req->enables = cpu_to_le32(enables);
8923 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
8924 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
8925 	}
8926 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
8927 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
8928 		p[i] = cpu_to_le32(ctxm->split[i]);
8929 	for (i = 0, j = 0; j < n && !rc; i++) {
8930 		struct bnxt_ctx_pg_info *ctx_pg;
8931 
8932 		if (!(instance_bmap & (1 << i)))
8933 			continue;
8934 		req->instance = cpu_to_le16(i);
8935 		ctx_pg = &ctxm->pg_info[j++];
8936 		if (!ctx_pg->entries)
8937 			continue;
8938 		req->num_entries = cpu_to_le32(ctx_pg->entries);
8939 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8940 				      &req->page_size_pbl_level,
8941 				      &req->page_dir);
8942 		if (last && j == n)
8943 			req->flags =
8944 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
8945 		rc = hwrm_req_send(bp, req);
8946 	}
8947 	hwrm_req_drop(bp, req);
8948 	return rc;
8949 }
8950 
bnxt_backing_store_cfg_v2(struct bnxt * bp,u32 ena)8951 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
8952 {
8953 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8954 	struct bnxt_ctx_mem_type *ctxm;
8955 	u16 last_type = BNXT_CTX_INV;
8956 	int rc = 0;
8957 	u16 type;
8958 
8959 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) {
8960 		ctxm = &ctx->ctx_arr[type];
8961 		if (!bnxt_bs_trace_avail(bp, type))
8962 			continue;
8963 		if (!ctxm->mem_valid) {
8964 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
8965 						     ctxm->max_entries, 1);
8966 			if (rc) {
8967 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
8968 					    type);
8969 				continue;
8970 			}
8971 			bnxt_bs_trace_init(bp, ctxm);
8972 		}
8973 		last_type = type;
8974 	}
8975 
8976 	if (last_type == BNXT_CTX_INV) {
8977 		if (!ena)
8978 			return 0;
8979 		else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
8980 			last_type = BNXT_CTX_MAX - 1;
8981 		else
8982 			last_type = BNXT_CTX_L2_MAX - 1;
8983 	}
8984 	ctx->ctx_arr[last_type].last = 1;
8985 
8986 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
8987 		ctxm = &ctx->ctx_arr[type];
8988 
8989 		if (!ctxm->mem_valid)
8990 			continue;
8991 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
8992 		if (rc)
8993 			return rc;
8994 	}
8995 	return 0;
8996 }
8997 
8998 /**
8999  * __bnxt_copy_ctx_mem - copy host context memory
9000  * @bp: The driver context
9001  * @ctxm: The pointer to the context memory type
9002  * @buf: The destination buffer or NULL to just obtain the length
9003  * @offset: The buffer offset to copy the data to
9004  * @head: The head offset of context memory to copy from
9005  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9006  *
9007  * This function is called for debugging purposes to dump the host context
9008  * used by the chip.
9009  *
9010  * Return: Length of memory copied
9011  */
__bnxt_copy_ctx_mem(struct bnxt * bp,struct bnxt_ctx_mem_type * ctxm,void * buf,size_t offset,size_t head,size_t tail)9012 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9013 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9014 				  size_t offset, size_t head, size_t tail)
9015 {
9016 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9017 	size_t len = 0, total_len = 0;
9018 	int i, n = 1;
9019 
9020 	if (!ctx_pg)
9021 		return 0;
9022 
9023 	if (ctxm->instance_bmap)
9024 		n = hweight32(ctxm->instance_bmap);
9025 	for (i = 0; i < n; i++) {
9026 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9027 					    tail);
9028 		offset += len;
9029 		total_len += len;
9030 	}
9031 	return total_len;
9032 }
9033 
bnxt_copy_ctx_mem(struct bnxt * bp,struct bnxt_ctx_mem_type * ctxm,void * buf,size_t offset)9034 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9035 			 void *buf, size_t offset)
9036 {
9037 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9038 
9039 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9040 }
9041 
bnxt_free_one_ctx_mem(struct bnxt * bp,struct bnxt_ctx_mem_type * ctxm,bool force)9042 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9043 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9044 {
9045 	struct bnxt_ctx_pg_info *ctx_pg;
9046 	int i, n = 1;
9047 
9048 	ctxm->last = 0;
9049 
9050 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9051 		return;
9052 
9053 	ctx_pg = ctxm->pg_info;
9054 	if (ctx_pg) {
9055 		if (ctxm->instance_bmap)
9056 			n = hweight32(ctxm->instance_bmap);
9057 		for (i = 0; i < n; i++)
9058 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9059 
9060 		kfree(ctx_pg);
9061 		ctxm->pg_info = NULL;
9062 		ctxm->mem_valid = 0;
9063 	}
9064 	memset(ctxm, 0, sizeof(*ctxm));
9065 }
9066 
bnxt_free_ctx_mem(struct bnxt * bp,bool force)9067 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9068 {
9069 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9070 	u16 type;
9071 
9072 	if (!ctx)
9073 		return;
9074 
9075 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9076 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9077 
9078 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9079 	if (force) {
9080 		kfree(ctx);
9081 		bp->ctx = NULL;
9082 	}
9083 }
9084 
bnxt_alloc_ctx_mem(struct bnxt * bp)9085 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9086 {
9087 	struct bnxt_ctx_mem_type *ctxm;
9088 	struct bnxt_ctx_mem_info *ctx;
9089 	u32 l2_qps, qp1_qps, max_qps;
9090 	u32 ena, entries_sp, entries;
9091 	u32 srqs, max_srqs, min;
9092 	u32 num_mr, num_ah;
9093 	u32 extra_srqs = 0;
9094 	u32 extra_qps = 0;
9095 	u32 fast_qpmd_qps;
9096 	u8 pg_lvl = 1;
9097 	int i, rc;
9098 
9099 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9100 	if (rc) {
9101 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9102 			   rc);
9103 		return rc;
9104 	}
9105 	ctx = bp->ctx;
9106 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9107 		return 0;
9108 
9109 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9110 	l2_qps = ctxm->qp_l2_entries;
9111 	qp1_qps = ctxm->qp_qp1_entries;
9112 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9113 	max_qps = ctxm->max_entries;
9114 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9115 	srqs = ctxm->srq_l2_entries;
9116 	max_srqs = ctxm->max_entries;
9117 	ena = 0;
9118 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9119 		pg_lvl = 2;
9120 		extra_qps = min_t(u32, 65536, max_qps - l2_qps - qp1_qps);
9121 		/* allocate extra qps if fw supports RoCE fast qp destroy feature */
9122 		extra_qps += fast_qpmd_qps;
9123 		extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9124 		if (fast_qpmd_qps)
9125 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9126 	}
9127 
9128 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9129 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9130 				     pg_lvl);
9131 	if (rc)
9132 		return rc;
9133 
9134 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9135 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9136 	if (rc)
9137 		return rc;
9138 
9139 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9140 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9141 				     extra_qps * 2, pg_lvl);
9142 	if (rc)
9143 		return rc;
9144 
9145 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9146 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9147 	if (rc)
9148 		return rc;
9149 
9150 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9151 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9152 	if (rc)
9153 		return rc;
9154 
9155 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9156 		goto skip_rdma;
9157 
9158 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9159 	/* 128K extra is needed to accommodate static AH context
9160 	 * allocation by f/w.
9161 	 */
9162 	num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9163 	num_ah = min_t(u32, num_mr, 1024 * 128);
9164 	ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9165 	if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9166 		ctxm->mrav_av_entries = num_ah;
9167 
9168 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9169 	if (rc)
9170 		return rc;
9171 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9172 
9173 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9174 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9175 	if (rc)
9176 		return rc;
9177 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9178 
9179 skip_rdma:
9180 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9181 	min = ctxm->min_entries;
9182 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9183 		     2 * (extra_qps + qp1_qps) + min;
9184 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9185 	if (rc)
9186 		return rc;
9187 
9188 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9189 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9190 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9191 	if (rc)
9192 		return rc;
9193 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9194 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9195 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9196 
9197 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9198 		rc = bnxt_backing_store_cfg_v2(bp, ena);
9199 	else
9200 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9201 	if (rc) {
9202 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9203 			   rc);
9204 		return rc;
9205 	}
9206 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9207 	return 0;
9208 }
9209 
bnxt_hwrm_crash_dump_mem_cfg(struct bnxt * bp)9210 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9211 {
9212 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9213 	u16 page_attr;
9214 	int rc;
9215 
9216 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9217 		return 0;
9218 
9219 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9220 	if (rc)
9221 		return rc;
9222 
9223 	if (BNXT_PAGE_SIZE == 0x2000)
9224 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9225 	else if (BNXT_PAGE_SIZE == 0x10000)
9226 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9227 	else
9228 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9229 	req->pg_size_lvl = cpu_to_le16(page_attr |
9230 				       bp->fw_crash_mem->ring_mem.depth);
9231 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9232 	req->size = cpu_to_le32(bp->fw_crash_len);
9233 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9234 	return hwrm_req_send(bp, req);
9235 }
9236 
bnxt_free_crash_dump_mem(struct bnxt * bp)9237 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9238 {
9239 	if (bp->fw_crash_mem) {
9240 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9241 		kfree(bp->fw_crash_mem);
9242 		bp->fw_crash_mem = NULL;
9243 	}
9244 }
9245 
bnxt_alloc_crash_dump_mem(struct bnxt * bp)9246 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9247 {
9248 	u32 mem_size = 0;
9249 	int rc;
9250 
9251 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9252 		return 0;
9253 
9254 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9255 	if (rc)
9256 		return rc;
9257 
9258 	mem_size = round_up(mem_size, 4);
9259 
9260 	/* keep and use the existing pages */
9261 	if (bp->fw_crash_mem &&
9262 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9263 		goto alloc_done;
9264 
9265 	if (bp->fw_crash_mem)
9266 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9267 	else
9268 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
9269 					   GFP_KERNEL);
9270 	if (!bp->fw_crash_mem)
9271 		return -ENOMEM;
9272 
9273 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9274 	if (rc) {
9275 		bnxt_free_crash_dump_mem(bp);
9276 		return rc;
9277 	}
9278 
9279 alloc_done:
9280 	bp->fw_crash_len = mem_size;
9281 	return 0;
9282 }
9283 
bnxt_hwrm_func_resc_qcaps(struct bnxt * bp,bool all)9284 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9285 {
9286 	struct hwrm_func_resource_qcaps_output *resp;
9287 	struct hwrm_func_resource_qcaps_input *req;
9288 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9289 	int rc;
9290 
9291 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9292 	if (rc)
9293 		return rc;
9294 
9295 	req->fid = cpu_to_le16(0xffff);
9296 	resp = hwrm_req_hold(bp, req);
9297 	rc = hwrm_req_send_silent(bp, req);
9298 	if (rc)
9299 		goto hwrm_func_resc_qcaps_exit;
9300 
9301 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9302 	if (!all)
9303 		goto hwrm_func_resc_qcaps_exit;
9304 
9305 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9306 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9307 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9308 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9309 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9310 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9311 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9312 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9313 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9314 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9315 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9316 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9317 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9318 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9319 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9320 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9321 
9322 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9323 		u16 max_msix = le16_to_cpu(resp->max_msix);
9324 
9325 		hw_resc->max_nqs = max_msix;
9326 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9327 	}
9328 
9329 	if (BNXT_PF(bp)) {
9330 		struct bnxt_pf_info *pf = &bp->pf;
9331 
9332 		pf->vf_resv_strategy =
9333 			le16_to_cpu(resp->vf_reservation_strategy);
9334 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9335 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9336 	}
9337 hwrm_func_resc_qcaps_exit:
9338 	hwrm_req_drop(bp, req);
9339 	return rc;
9340 }
9341 
__bnxt_hwrm_ptp_qcfg(struct bnxt * bp)9342 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9343 {
9344 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9345 	struct hwrm_port_mac_ptp_qcfg_input *req;
9346 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9347 	u8 flags;
9348 	int rc;
9349 
9350 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9351 		rc = -ENODEV;
9352 		goto no_ptp;
9353 	}
9354 
9355 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9356 	if (rc)
9357 		goto no_ptp;
9358 
9359 	req->port_id = cpu_to_le16(bp->pf.port_id);
9360 	resp = hwrm_req_hold(bp, req);
9361 	rc = hwrm_req_send(bp, req);
9362 	if (rc)
9363 		goto exit;
9364 
9365 	flags = resp->flags;
9366 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9367 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9368 		rc = -ENODEV;
9369 		goto exit;
9370 	}
9371 	if (!ptp) {
9372 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9373 		if (!ptp) {
9374 			rc = -ENOMEM;
9375 			goto exit;
9376 		}
9377 		ptp->bp = bp;
9378 		bp->ptp_cfg = ptp;
9379 	}
9380 
9381 	if (flags &
9382 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9383 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9384 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9385 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9386 	} else if (BNXT_CHIP_P5(bp)) {
9387 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9388 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9389 	} else {
9390 		rc = -ENODEV;
9391 		goto exit;
9392 	}
9393 	ptp->rtc_configured =
9394 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9395 	rc = bnxt_ptp_init(bp);
9396 	if (rc)
9397 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9398 exit:
9399 	hwrm_req_drop(bp, req);
9400 	if (!rc)
9401 		return 0;
9402 
9403 no_ptp:
9404 	bnxt_ptp_clear(bp);
9405 	kfree(ptp);
9406 	bp->ptp_cfg = NULL;
9407 	return rc;
9408 }
9409 
__bnxt_hwrm_func_qcaps(struct bnxt * bp)9410 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9411 {
9412 	struct hwrm_func_qcaps_output *resp;
9413 	struct hwrm_func_qcaps_input *req;
9414 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9415 	u32 flags, flags_ext, flags_ext2;
9416 	int rc;
9417 
9418 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9419 	if (rc)
9420 		return rc;
9421 
9422 	req->fid = cpu_to_le16(0xffff);
9423 	resp = hwrm_req_hold(bp, req);
9424 	rc = hwrm_req_send(bp, req);
9425 	if (rc)
9426 		goto hwrm_func_qcaps_exit;
9427 
9428 	flags = le32_to_cpu(resp->flags);
9429 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9430 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9431 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9432 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9433 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9434 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9435 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9436 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9437 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9438 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9439 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9440 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9441 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9442 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9443 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9444 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9445 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9446 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9447 
9448 	flags_ext = le32_to_cpu(resp->flags_ext);
9449 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9450 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9451 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9452 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9453 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9454 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9455 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9456 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9457 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9458 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9459 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9460 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9461 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9462 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9463 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9464 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9465 
9466 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9467 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9468 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9469 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9470 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9471 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9472 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9473 	if (BNXT_PF(bp) &&
9474 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9475 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9476 
9477 	bp->tx_push_thresh = 0;
9478 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9479 	    BNXT_FW_MAJ(bp) > 217)
9480 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9481 
9482 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9483 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9484 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9485 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9486 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9487 	if (!hw_resc->max_hw_ring_grps)
9488 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9489 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9490 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9491 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9492 
9493 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9494 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9495 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9496 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9497 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9498 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9499 
9500 	if (BNXT_PF(bp)) {
9501 		struct bnxt_pf_info *pf = &bp->pf;
9502 
9503 		pf->fw_fid = le16_to_cpu(resp->fid);
9504 		pf->port_id = le16_to_cpu(resp->port_id);
9505 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9506 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9507 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9508 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9509 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9510 			bp->flags |= BNXT_FLAG_WOL_CAP;
9511 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9512 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9513 		} else {
9514 			bnxt_ptp_clear(bp);
9515 			kfree(bp->ptp_cfg);
9516 			bp->ptp_cfg = NULL;
9517 		}
9518 	} else {
9519 #ifdef CONFIG_BNXT_SRIOV
9520 		struct bnxt_vf_info *vf = &bp->vf;
9521 
9522 		vf->fw_fid = le16_to_cpu(resp->fid);
9523 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9524 #endif
9525 	}
9526 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9527 
9528 hwrm_func_qcaps_exit:
9529 	hwrm_req_drop(bp, req);
9530 	return rc;
9531 }
9532 
bnxt_hwrm_dbg_qcaps(struct bnxt * bp)9533 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9534 {
9535 	struct hwrm_dbg_qcaps_output *resp;
9536 	struct hwrm_dbg_qcaps_input *req;
9537 	int rc;
9538 
9539 	bp->fw_dbg_cap = 0;
9540 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9541 		return;
9542 
9543 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9544 	if (rc)
9545 		return;
9546 
9547 	req->fid = cpu_to_le16(0xffff);
9548 	resp = hwrm_req_hold(bp, req);
9549 	rc = hwrm_req_send(bp, req);
9550 	if (rc)
9551 		goto hwrm_dbg_qcaps_exit;
9552 
9553 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9554 
9555 hwrm_dbg_qcaps_exit:
9556 	hwrm_req_drop(bp, req);
9557 }
9558 
9559 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9560 
bnxt_hwrm_func_qcaps(struct bnxt * bp)9561 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9562 {
9563 	int rc;
9564 
9565 	rc = __bnxt_hwrm_func_qcaps(bp);
9566 	if (rc)
9567 		return rc;
9568 
9569 	bnxt_hwrm_dbg_qcaps(bp);
9570 
9571 	rc = bnxt_hwrm_queue_qportcfg(bp);
9572 	if (rc) {
9573 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9574 		return rc;
9575 	}
9576 	if (bp->hwrm_spec_code >= 0x10803) {
9577 		rc = bnxt_alloc_ctx_mem(bp);
9578 		if (rc)
9579 			return rc;
9580 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9581 		if (!rc)
9582 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9583 	}
9584 	return 0;
9585 }
9586 
bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt * bp)9587 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9588 {
9589 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9590 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9591 	u32 flags;
9592 	int rc;
9593 
9594 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9595 		return 0;
9596 
9597 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9598 	if (rc)
9599 		return rc;
9600 
9601 	resp = hwrm_req_hold(bp, req);
9602 	rc = hwrm_req_send(bp, req);
9603 	if (rc)
9604 		goto hwrm_cfa_adv_qcaps_exit;
9605 
9606 	flags = le32_to_cpu(resp->flags);
9607 	if (flags &
9608 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9609 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9610 
9611 	if (flags &
9612 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9613 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9614 
9615 	if (flags &
9616 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9617 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9618 
9619 hwrm_cfa_adv_qcaps_exit:
9620 	hwrm_req_drop(bp, req);
9621 	return rc;
9622 }
9623 
__bnxt_alloc_fw_health(struct bnxt * bp)9624 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9625 {
9626 	if (bp->fw_health)
9627 		return 0;
9628 
9629 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9630 	if (!bp->fw_health)
9631 		return -ENOMEM;
9632 
9633 	mutex_init(&bp->fw_health->lock);
9634 	return 0;
9635 }
9636 
bnxt_alloc_fw_health(struct bnxt * bp)9637 static int bnxt_alloc_fw_health(struct bnxt *bp)
9638 {
9639 	int rc;
9640 
9641 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9642 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9643 		return 0;
9644 
9645 	rc = __bnxt_alloc_fw_health(bp);
9646 	if (rc) {
9647 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9648 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9649 		return rc;
9650 	}
9651 
9652 	return 0;
9653 }
9654 
__bnxt_map_fw_health_reg(struct bnxt * bp,u32 reg)9655 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9656 {
9657 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9658 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9659 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9660 }
9661 
bnxt_inv_fw_health_reg(struct bnxt * bp)9662 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9663 {
9664 	struct bnxt_fw_health *fw_health = bp->fw_health;
9665 	u32 reg_type;
9666 
9667 	if (!fw_health)
9668 		return;
9669 
9670 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9671 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9672 		fw_health->status_reliable = false;
9673 
9674 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9675 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9676 		fw_health->resets_reliable = false;
9677 }
9678 
bnxt_try_map_fw_health_reg(struct bnxt * bp)9679 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9680 {
9681 	void __iomem *hs;
9682 	u32 status_loc;
9683 	u32 reg_type;
9684 	u32 sig;
9685 
9686 	if (bp->fw_health)
9687 		bp->fw_health->status_reliable = false;
9688 
9689 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9690 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9691 
9692 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9693 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9694 		if (!bp->chip_num) {
9695 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9696 			bp->chip_num = readl(bp->bar0 +
9697 					     BNXT_FW_HEALTH_WIN_BASE +
9698 					     BNXT_GRC_REG_CHIP_NUM);
9699 		}
9700 		if (!BNXT_CHIP_P5_PLUS(bp))
9701 			return;
9702 
9703 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9704 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9705 	} else {
9706 		status_loc = readl(hs + offsetof(struct hcomm_status,
9707 						 fw_status_loc));
9708 	}
9709 
9710 	if (__bnxt_alloc_fw_health(bp)) {
9711 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9712 		return;
9713 	}
9714 
9715 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9716 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9717 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9718 		__bnxt_map_fw_health_reg(bp, status_loc);
9719 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9720 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9721 	}
9722 
9723 	bp->fw_health->status_reliable = true;
9724 }
9725 
bnxt_map_fw_health_regs(struct bnxt * bp)9726 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9727 {
9728 	struct bnxt_fw_health *fw_health = bp->fw_health;
9729 	u32 reg_base = 0xffffffff;
9730 	int i;
9731 
9732 	bp->fw_health->status_reliable = false;
9733 	bp->fw_health->resets_reliable = false;
9734 	/* Only pre-map the monitoring GRC registers using window 3 */
9735 	for (i = 0; i < 4; i++) {
9736 		u32 reg = fw_health->regs[i];
9737 
9738 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9739 			continue;
9740 		if (reg_base == 0xffffffff)
9741 			reg_base = reg & BNXT_GRC_BASE_MASK;
9742 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9743 			return -ERANGE;
9744 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9745 	}
9746 	bp->fw_health->status_reliable = true;
9747 	bp->fw_health->resets_reliable = true;
9748 	if (reg_base == 0xffffffff)
9749 		return 0;
9750 
9751 	__bnxt_map_fw_health_reg(bp, reg_base);
9752 	return 0;
9753 }
9754 
bnxt_remap_fw_health_regs(struct bnxt * bp)9755 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9756 {
9757 	if (!bp->fw_health)
9758 		return;
9759 
9760 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9761 		bp->fw_health->status_reliable = true;
9762 		bp->fw_health->resets_reliable = true;
9763 	} else {
9764 		bnxt_try_map_fw_health_reg(bp);
9765 	}
9766 }
9767 
bnxt_hwrm_error_recovery_qcfg(struct bnxt * bp)9768 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9769 {
9770 	struct bnxt_fw_health *fw_health = bp->fw_health;
9771 	struct hwrm_error_recovery_qcfg_output *resp;
9772 	struct hwrm_error_recovery_qcfg_input *req;
9773 	int rc, i;
9774 
9775 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9776 		return 0;
9777 
9778 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9779 	if (rc)
9780 		return rc;
9781 
9782 	resp = hwrm_req_hold(bp, req);
9783 	rc = hwrm_req_send(bp, req);
9784 	if (rc)
9785 		goto err_recovery_out;
9786 	fw_health->flags = le32_to_cpu(resp->flags);
9787 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9788 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9789 		rc = -EINVAL;
9790 		goto err_recovery_out;
9791 	}
9792 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9793 	fw_health->master_func_wait_dsecs =
9794 		le32_to_cpu(resp->master_func_wait_period);
9795 	fw_health->normal_func_wait_dsecs =
9796 		le32_to_cpu(resp->normal_func_wait_period);
9797 	fw_health->post_reset_wait_dsecs =
9798 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9799 	fw_health->post_reset_max_wait_dsecs =
9800 		le32_to_cpu(resp->max_bailout_time_after_reset);
9801 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9802 		le32_to_cpu(resp->fw_health_status_reg);
9803 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9804 		le32_to_cpu(resp->fw_heartbeat_reg);
9805 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9806 		le32_to_cpu(resp->fw_reset_cnt_reg);
9807 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9808 		le32_to_cpu(resp->reset_inprogress_reg);
9809 	fw_health->fw_reset_inprog_reg_mask =
9810 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9811 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9812 	if (fw_health->fw_reset_seq_cnt >= 16) {
9813 		rc = -EINVAL;
9814 		goto err_recovery_out;
9815 	}
9816 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9817 		fw_health->fw_reset_seq_regs[i] =
9818 			le32_to_cpu(resp->reset_reg[i]);
9819 		fw_health->fw_reset_seq_vals[i] =
9820 			le32_to_cpu(resp->reset_reg_val[i]);
9821 		fw_health->fw_reset_seq_delay_msec[i] =
9822 			resp->delay_after_reset[i];
9823 	}
9824 err_recovery_out:
9825 	hwrm_req_drop(bp, req);
9826 	if (!rc)
9827 		rc = bnxt_map_fw_health_regs(bp);
9828 	if (rc)
9829 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9830 	return rc;
9831 }
9832 
bnxt_hwrm_func_reset(struct bnxt * bp)9833 static int bnxt_hwrm_func_reset(struct bnxt *bp)
9834 {
9835 	struct hwrm_func_reset_input *req;
9836 	int rc;
9837 
9838 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
9839 	if (rc)
9840 		return rc;
9841 
9842 	req->enables = 0;
9843 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
9844 	return hwrm_req_send(bp, req);
9845 }
9846 
bnxt_nvm_cfg_ver_get(struct bnxt * bp)9847 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
9848 {
9849 	struct hwrm_nvm_get_dev_info_output nvm_info;
9850 
9851 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
9852 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
9853 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
9854 			 nvm_info.nvm_cfg_ver_upd);
9855 }
9856 
bnxt_hwrm_queue_qportcfg(struct bnxt * bp)9857 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
9858 {
9859 	struct hwrm_queue_qportcfg_output *resp;
9860 	struct hwrm_queue_qportcfg_input *req;
9861 	u8 i, j, *qptr;
9862 	bool no_rdma;
9863 	int rc = 0;
9864 
9865 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
9866 	if (rc)
9867 		return rc;
9868 
9869 	resp = hwrm_req_hold(bp, req);
9870 	rc = hwrm_req_send(bp, req);
9871 	if (rc)
9872 		goto qportcfg_exit;
9873 
9874 	if (!resp->max_configurable_queues) {
9875 		rc = -EINVAL;
9876 		goto qportcfg_exit;
9877 	}
9878 	bp->max_tc = resp->max_configurable_queues;
9879 	bp->max_lltc = resp->max_configurable_lossless_queues;
9880 	if (bp->max_tc > BNXT_MAX_QUEUE)
9881 		bp->max_tc = BNXT_MAX_QUEUE;
9882 
9883 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
9884 	qptr = &resp->queue_id0;
9885 	for (i = 0, j = 0; i < bp->max_tc; i++) {
9886 		bp->q_info[j].queue_id = *qptr;
9887 		bp->q_ids[i] = *qptr++;
9888 		bp->q_info[j].queue_profile = *qptr++;
9889 		bp->tc_to_qidx[j] = j;
9890 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
9891 		    (no_rdma && BNXT_PF(bp)))
9892 			j++;
9893 	}
9894 	bp->max_q = bp->max_tc;
9895 	bp->max_tc = max_t(u8, j, 1);
9896 
9897 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
9898 		bp->max_tc = 1;
9899 
9900 	if (bp->max_lltc > bp->max_tc)
9901 		bp->max_lltc = bp->max_tc;
9902 
9903 qportcfg_exit:
9904 	hwrm_req_drop(bp, req);
9905 	return rc;
9906 }
9907 
bnxt_hwrm_poll(struct bnxt * bp)9908 static int bnxt_hwrm_poll(struct bnxt *bp)
9909 {
9910 	struct hwrm_ver_get_input *req;
9911 	int rc;
9912 
9913 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9914 	if (rc)
9915 		return rc;
9916 
9917 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9918 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9919 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9920 
9921 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
9922 	rc = hwrm_req_send(bp, req);
9923 	return rc;
9924 }
9925 
bnxt_hwrm_ver_get(struct bnxt * bp)9926 static int bnxt_hwrm_ver_get(struct bnxt *bp)
9927 {
9928 	struct hwrm_ver_get_output *resp;
9929 	struct hwrm_ver_get_input *req;
9930 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
9931 	u32 dev_caps_cfg, hwrm_ver;
9932 	int rc, len;
9933 
9934 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9935 	if (rc)
9936 		return rc;
9937 
9938 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
9939 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
9940 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9941 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9942 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9943 
9944 	resp = hwrm_req_hold(bp, req);
9945 	rc = hwrm_req_send(bp, req);
9946 	if (rc)
9947 		goto hwrm_ver_get_exit;
9948 
9949 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
9950 
9951 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
9952 			     resp->hwrm_intf_min_8b << 8 |
9953 			     resp->hwrm_intf_upd_8b;
9954 	if (resp->hwrm_intf_maj_8b < 1) {
9955 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
9956 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9957 			    resp->hwrm_intf_upd_8b);
9958 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
9959 	}
9960 
9961 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
9962 			HWRM_VERSION_UPDATE;
9963 
9964 	if (bp->hwrm_spec_code > hwrm_ver)
9965 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9966 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
9967 			 HWRM_VERSION_UPDATE);
9968 	else
9969 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9970 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9971 			 resp->hwrm_intf_upd_8b);
9972 
9973 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
9974 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
9975 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
9976 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
9977 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
9978 		len = FW_VER_STR_LEN;
9979 	} else {
9980 		fw_maj = resp->hwrm_fw_maj_8b;
9981 		fw_min = resp->hwrm_fw_min_8b;
9982 		fw_bld = resp->hwrm_fw_bld_8b;
9983 		fw_rsv = resp->hwrm_fw_rsvd_8b;
9984 		len = BC_HWRM_STR_LEN;
9985 	}
9986 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
9987 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
9988 		 fw_rsv);
9989 
9990 	if (strlen(resp->active_pkg_name)) {
9991 		int fw_ver_len = strlen(bp->fw_ver_str);
9992 
9993 		snprintf(bp->fw_ver_str + fw_ver_len,
9994 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
9995 			 resp->active_pkg_name);
9996 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
9997 	}
9998 
9999 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10000 	if (!bp->hwrm_cmd_timeout)
10001 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10002 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10003 	if (!bp->hwrm_cmd_max_timeout)
10004 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10005 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
10006 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
10007 			    bp->hwrm_cmd_max_timeout / 1000);
10008 
10009 	if (resp->hwrm_intf_maj_8b >= 1) {
10010 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10011 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10012 	}
10013 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10014 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10015 
10016 	bp->chip_num = le16_to_cpu(resp->chip_num);
10017 	bp->chip_rev = resp->chip_rev;
10018 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10019 	    !resp->chip_metal)
10020 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10021 
10022 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10023 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10024 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10025 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10026 
10027 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10028 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10029 
10030 	if (dev_caps_cfg &
10031 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10032 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10033 
10034 	if (dev_caps_cfg &
10035 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10036 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10037 
10038 	if (dev_caps_cfg &
10039 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10040 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10041 
10042 hwrm_ver_get_exit:
10043 	hwrm_req_drop(bp, req);
10044 	return rc;
10045 }
10046 
bnxt_hwrm_fw_set_time(struct bnxt * bp)10047 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10048 {
10049 	struct hwrm_fw_set_time_input *req;
10050 	struct tm tm;
10051 	time64_t now = ktime_get_real_seconds();
10052 	int rc;
10053 
10054 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10055 	    bp->hwrm_spec_code < 0x10400)
10056 		return -EOPNOTSUPP;
10057 
10058 	time64_to_tm(now, 0, &tm);
10059 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10060 	if (rc)
10061 		return rc;
10062 
10063 	req->year = cpu_to_le16(1900 + tm.tm_year);
10064 	req->month = 1 + tm.tm_mon;
10065 	req->day = tm.tm_mday;
10066 	req->hour = tm.tm_hour;
10067 	req->minute = tm.tm_min;
10068 	req->second = tm.tm_sec;
10069 	return hwrm_req_send(bp, req);
10070 }
10071 
bnxt_add_one_ctr(u64 hw,u64 * sw,u64 mask)10072 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10073 {
10074 	u64 sw_tmp;
10075 
10076 	hw &= mask;
10077 	sw_tmp = (*sw & ~mask) | hw;
10078 	if (hw < (*sw & mask))
10079 		sw_tmp += mask + 1;
10080 	WRITE_ONCE(*sw, sw_tmp);
10081 }
10082 
__bnxt_accumulate_stats(__le64 * hw_stats,u64 * sw_stats,u64 * masks,int count,bool ignore_zero)10083 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10084 				    int count, bool ignore_zero)
10085 {
10086 	int i;
10087 
10088 	for (i = 0; i < count; i++) {
10089 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10090 
10091 		if (ignore_zero && !hw)
10092 			continue;
10093 
10094 		if (masks[i] == -1ULL)
10095 			sw_stats[i] = hw;
10096 		else
10097 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10098 	}
10099 }
10100 
bnxt_accumulate_stats(struct bnxt_stats_mem * stats)10101 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10102 {
10103 	if (!stats->hw_stats)
10104 		return;
10105 
10106 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10107 				stats->hw_masks, stats->len / 8, false);
10108 }
10109 
bnxt_accumulate_all_stats(struct bnxt * bp)10110 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10111 {
10112 	struct bnxt_stats_mem *ring0_stats;
10113 	bool ignore_zero = false;
10114 	int i;
10115 
10116 	/* Chip bug.  Counter intermittently becomes 0. */
10117 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10118 		ignore_zero = true;
10119 
10120 	for (i = 0; i < bp->cp_nr_rings; i++) {
10121 		struct bnxt_napi *bnapi = bp->bnapi[i];
10122 		struct bnxt_cp_ring_info *cpr;
10123 		struct bnxt_stats_mem *stats;
10124 
10125 		cpr = &bnapi->cp_ring;
10126 		stats = &cpr->stats;
10127 		if (!i)
10128 			ring0_stats = stats;
10129 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10130 					ring0_stats->hw_masks,
10131 					ring0_stats->len / 8, ignore_zero);
10132 	}
10133 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10134 		struct bnxt_stats_mem *stats = &bp->port_stats;
10135 		__le64 *hw_stats = stats->hw_stats;
10136 		u64 *sw_stats = stats->sw_stats;
10137 		u64 *masks = stats->hw_masks;
10138 		int cnt;
10139 
10140 		cnt = sizeof(struct rx_port_stats) / 8;
10141 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10142 
10143 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10144 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10145 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10146 		cnt = sizeof(struct tx_port_stats) / 8;
10147 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10148 	}
10149 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10150 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10151 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10152 	}
10153 }
10154 
bnxt_hwrm_port_qstats(struct bnxt * bp,u8 flags)10155 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10156 {
10157 	struct hwrm_port_qstats_input *req;
10158 	struct bnxt_pf_info *pf = &bp->pf;
10159 	int rc;
10160 
10161 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10162 		return 0;
10163 
10164 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10165 		return -EOPNOTSUPP;
10166 
10167 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10168 	if (rc)
10169 		return rc;
10170 
10171 	req->flags = flags;
10172 	req->port_id = cpu_to_le16(pf->port_id);
10173 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10174 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10175 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10176 	return hwrm_req_send(bp, req);
10177 }
10178 
bnxt_hwrm_port_qstats_ext(struct bnxt * bp,u8 flags)10179 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10180 {
10181 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10182 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10183 	struct hwrm_port_qstats_ext_output *resp_qs;
10184 	struct hwrm_port_qstats_ext_input *req_qs;
10185 	struct bnxt_pf_info *pf = &bp->pf;
10186 	u32 tx_stat_size;
10187 	int rc;
10188 
10189 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10190 		return 0;
10191 
10192 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10193 		return -EOPNOTSUPP;
10194 
10195 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10196 	if (rc)
10197 		return rc;
10198 
10199 	req_qs->flags = flags;
10200 	req_qs->port_id = cpu_to_le16(pf->port_id);
10201 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10202 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10203 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10204 		       sizeof(struct tx_port_stats_ext) : 0;
10205 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10206 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10207 	resp_qs = hwrm_req_hold(bp, req_qs);
10208 	rc = hwrm_req_send(bp, req_qs);
10209 	if (!rc) {
10210 		bp->fw_rx_stats_ext_size =
10211 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10212 		if (BNXT_FW_MAJ(bp) < 220 &&
10213 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10214 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10215 
10216 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10217 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10218 	} else {
10219 		bp->fw_rx_stats_ext_size = 0;
10220 		bp->fw_tx_stats_ext_size = 0;
10221 	}
10222 	hwrm_req_drop(bp, req_qs);
10223 
10224 	if (flags)
10225 		return rc;
10226 
10227 	if (bp->fw_tx_stats_ext_size <=
10228 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10229 		bp->pri2cos_valid = 0;
10230 		return rc;
10231 	}
10232 
10233 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10234 	if (rc)
10235 		return rc;
10236 
10237 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10238 
10239 	resp_qc = hwrm_req_hold(bp, req_qc);
10240 	rc = hwrm_req_send(bp, req_qc);
10241 	if (!rc) {
10242 		u8 *pri2cos;
10243 		int i, j;
10244 
10245 		pri2cos = &resp_qc->pri0_cos_queue_id;
10246 		for (i = 0; i < 8; i++) {
10247 			u8 queue_id = pri2cos[i];
10248 			u8 queue_idx;
10249 
10250 			/* Per port queue IDs start from 0, 10, 20, etc */
10251 			queue_idx = queue_id % 10;
10252 			if (queue_idx > BNXT_MAX_QUEUE) {
10253 				bp->pri2cos_valid = false;
10254 				hwrm_req_drop(bp, req_qc);
10255 				return rc;
10256 			}
10257 			for (j = 0; j < bp->max_q; j++) {
10258 				if (bp->q_ids[j] == queue_id)
10259 					bp->pri2cos_idx[i] = queue_idx;
10260 			}
10261 		}
10262 		bp->pri2cos_valid = true;
10263 	}
10264 	hwrm_req_drop(bp, req_qc);
10265 
10266 	return rc;
10267 }
10268 
bnxt_hwrm_free_tunnel_ports(struct bnxt * bp)10269 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10270 {
10271 	bnxt_hwrm_tunnel_dst_port_free(bp,
10272 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10273 	bnxt_hwrm_tunnel_dst_port_free(bp,
10274 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10275 }
10276 
bnxt_set_tpa(struct bnxt * bp,bool set_tpa)10277 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10278 {
10279 	int rc, i;
10280 	u32 tpa_flags = 0;
10281 
10282 	if (set_tpa)
10283 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10284 	else if (BNXT_NO_FW_ACCESS(bp))
10285 		return 0;
10286 	for (i = 0; i < bp->nr_vnics; i++) {
10287 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10288 		if (rc) {
10289 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10290 				   i, rc);
10291 			return rc;
10292 		}
10293 	}
10294 	return 0;
10295 }
10296 
bnxt_hwrm_clear_vnic_rss(struct bnxt * bp)10297 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10298 {
10299 	int i;
10300 
10301 	for (i = 0; i < bp->nr_vnics; i++)
10302 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10303 }
10304 
bnxt_clear_vnic(struct bnxt * bp)10305 static void bnxt_clear_vnic(struct bnxt *bp)
10306 {
10307 	if (!bp->vnic_info)
10308 		return;
10309 
10310 	bnxt_hwrm_clear_vnic_filter(bp);
10311 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10312 		/* clear all RSS setting before free vnic ctx */
10313 		bnxt_hwrm_clear_vnic_rss(bp);
10314 		bnxt_hwrm_vnic_ctx_free(bp);
10315 	}
10316 	/* before free the vnic, undo the vnic tpa settings */
10317 	if (bp->flags & BNXT_FLAG_TPA)
10318 		bnxt_set_tpa(bp, false);
10319 	bnxt_hwrm_vnic_free(bp);
10320 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10321 		bnxt_hwrm_vnic_ctx_free(bp);
10322 }
10323 
bnxt_hwrm_resource_free(struct bnxt * bp,bool close_path,bool irq_re_init)10324 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10325 				    bool irq_re_init)
10326 {
10327 	bnxt_clear_vnic(bp);
10328 	bnxt_hwrm_ring_free(bp, close_path);
10329 	bnxt_hwrm_ring_grp_free(bp);
10330 	if (irq_re_init) {
10331 		bnxt_hwrm_stat_ctx_free(bp);
10332 		bnxt_hwrm_free_tunnel_ports(bp);
10333 	}
10334 }
10335 
bnxt_hwrm_set_br_mode(struct bnxt * bp,u16 br_mode)10336 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10337 {
10338 	struct hwrm_func_cfg_input *req;
10339 	u8 evb_mode;
10340 	int rc;
10341 
10342 	if (br_mode == BRIDGE_MODE_VEB)
10343 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10344 	else if (br_mode == BRIDGE_MODE_VEPA)
10345 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10346 	else
10347 		return -EINVAL;
10348 
10349 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10350 	if (rc)
10351 		return rc;
10352 
10353 	req->fid = cpu_to_le16(0xffff);
10354 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10355 	req->evb_mode = evb_mode;
10356 	return hwrm_req_send(bp, req);
10357 }
10358 
bnxt_hwrm_set_cache_line_size(struct bnxt * bp,int size)10359 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10360 {
10361 	struct hwrm_func_cfg_input *req;
10362 	int rc;
10363 
10364 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10365 		return 0;
10366 
10367 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10368 	if (rc)
10369 		return rc;
10370 
10371 	req->fid = cpu_to_le16(0xffff);
10372 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10373 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10374 	if (size == 128)
10375 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10376 
10377 	return hwrm_req_send(bp, req);
10378 }
10379 
__bnxt_setup_vnic(struct bnxt * bp,struct bnxt_vnic_info * vnic)10380 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10381 {
10382 	int rc;
10383 
10384 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10385 		goto skip_rss_ctx;
10386 
10387 	/* allocate context for vnic */
10388 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10389 	if (rc) {
10390 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10391 			   vnic->vnic_id, rc);
10392 		goto vnic_setup_err;
10393 	}
10394 	bp->rsscos_nr_ctxs++;
10395 
10396 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10397 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10398 		if (rc) {
10399 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10400 				   vnic->vnic_id, rc);
10401 			goto vnic_setup_err;
10402 		}
10403 		bp->rsscos_nr_ctxs++;
10404 	}
10405 
10406 skip_rss_ctx:
10407 	/* configure default vnic, ring grp */
10408 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10409 	if (rc) {
10410 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10411 			   vnic->vnic_id, rc);
10412 		goto vnic_setup_err;
10413 	}
10414 
10415 	/* Enable RSS hashing on vnic */
10416 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10417 	if (rc) {
10418 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10419 			   vnic->vnic_id, rc);
10420 		goto vnic_setup_err;
10421 	}
10422 
10423 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10424 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10425 		if (rc) {
10426 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10427 				   vnic->vnic_id, rc);
10428 		}
10429 	}
10430 
10431 vnic_setup_err:
10432 	return rc;
10433 }
10434 
bnxt_hwrm_vnic_update(struct bnxt * bp,struct bnxt_vnic_info * vnic,u8 valid)10435 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10436 			  u8 valid)
10437 {
10438 	struct hwrm_vnic_update_input *req;
10439 	int rc;
10440 
10441 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10442 	if (rc)
10443 		return rc;
10444 
10445 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10446 
10447 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10448 		req->mru = cpu_to_le16(vnic->mru);
10449 
10450 	req->enables = cpu_to_le32(valid);
10451 
10452 	return hwrm_req_send(bp, req);
10453 }
10454 
bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt * bp,struct bnxt_vnic_info * vnic)10455 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10456 {
10457 	int rc;
10458 
10459 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10460 	if (rc) {
10461 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10462 			   vnic->vnic_id, rc);
10463 		return rc;
10464 	}
10465 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10466 	if (rc)
10467 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10468 			   vnic->vnic_id, rc);
10469 	return rc;
10470 }
10471 
__bnxt_setup_vnic_p5(struct bnxt * bp,struct bnxt_vnic_info * vnic)10472 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10473 {
10474 	int rc, i, nr_ctxs;
10475 
10476 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10477 	for (i = 0; i < nr_ctxs; i++) {
10478 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10479 		if (rc) {
10480 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10481 				   vnic->vnic_id, i, rc);
10482 			break;
10483 		}
10484 		bp->rsscos_nr_ctxs++;
10485 	}
10486 	if (i < nr_ctxs)
10487 		return -ENOMEM;
10488 
10489 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10490 	if (rc)
10491 		return rc;
10492 
10493 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10494 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10495 		if (rc) {
10496 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10497 				   vnic->vnic_id, rc);
10498 		}
10499 	}
10500 	return rc;
10501 }
10502 
bnxt_setup_vnic(struct bnxt * bp,struct bnxt_vnic_info * vnic)10503 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10504 {
10505 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10506 		return __bnxt_setup_vnic_p5(bp, vnic);
10507 	else
10508 		return __bnxt_setup_vnic(bp, vnic);
10509 }
10510 
bnxt_alloc_and_setup_vnic(struct bnxt * bp,struct bnxt_vnic_info * vnic,u16 start_rx_ring_idx,int rx_rings)10511 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10512 				     struct bnxt_vnic_info *vnic,
10513 				     u16 start_rx_ring_idx, int rx_rings)
10514 {
10515 	int rc;
10516 
10517 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10518 	if (rc) {
10519 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10520 			   vnic->vnic_id, rc);
10521 		return rc;
10522 	}
10523 	return bnxt_setup_vnic(bp, vnic);
10524 }
10525 
bnxt_alloc_rfs_vnics(struct bnxt * bp)10526 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10527 {
10528 	struct bnxt_vnic_info *vnic;
10529 	int i, rc = 0;
10530 
10531 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10532 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10533 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10534 	}
10535 
10536 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10537 		return 0;
10538 
10539 	for (i = 0; i < bp->rx_nr_rings; i++) {
10540 		u16 vnic_id = i + 1;
10541 		u16 ring_id = i;
10542 
10543 		if (vnic_id >= bp->nr_vnics)
10544 			break;
10545 
10546 		vnic = &bp->vnic_info[vnic_id];
10547 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10548 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10549 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10550 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10551 			break;
10552 	}
10553 	return rc;
10554 }
10555 
bnxt_del_one_rss_ctx(struct bnxt * bp,struct bnxt_rss_ctx * rss_ctx,bool all)10556 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10557 			  bool all)
10558 {
10559 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10560 	struct bnxt_filter_base *usr_fltr, *tmp;
10561 	struct bnxt_ntuple_filter *ntp_fltr;
10562 	int i;
10563 
10564 	if (netif_running(bp->dev)) {
10565 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10566 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10567 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10568 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10569 		}
10570 	}
10571 	if (!all)
10572 		return;
10573 
10574 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10575 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10576 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10577 			ntp_fltr = container_of(usr_fltr,
10578 						struct bnxt_ntuple_filter,
10579 						base);
10580 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10581 			bnxt_del_ntp_filter(bp, ntp_fltr);
10582 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10583 		}
10584 	}
10585 
10586 	if (vnic->rss_table)
10587 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10588 				  vnic->rss_table,
10589 				  vnic->rss_table_dma_addr);
10590 	bp->num_rss_ctx--;
10591 }
10592 
bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt * bp)10593 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10594 {
10595 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10596 	struct ethtool_rxfh_context *ctx;
10597 	unsigned long context;
10598 
10599 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10600 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10601 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10602 
10603 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10604 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10605 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10606 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10607 				   rss_ctx->index);
10608 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10609 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10610 		}
10611 	}
10612 }
10613 
bnxt_clear_rss_ctxs(struct bnxt * bp)10614 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10615 {
10616 	struct ethtool_rxfh_context *ctx;
10617 	unsigned long context;
10618 
10619 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10620 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10621 
10622 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10623 	}
10624 }
10625 
10626 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
bnxt_promisc_ok(struct bnxt * bp)10627 static bool bnxt_promisc_ok(struct bnxt *bp)
10628 {
10629 #ifdef CONFIG_BNXT_SRIOV
10630 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10631 		return false;
10632 #endif
10633 	return true;
10634 }
10635 
bnxt_setup_nitroa0_vnic(struct bnxt * bp)10636 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10637 {
10638 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10639 	unsigned int rc = 0;
10640 
10641 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10642 	if (rc) {
10643 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10644 			   rc);
10645 		return rc;
10646 	}
10647 
10648 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10649 	if (rc) {
10650 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10651 			   rc);
10652 		return rc;
10653 	}
10654 	return rc;
10655 }
10656 
10657 static int bnxt_cfg_rx_mode(struct bnxt *);
10658 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10659 
bnxt_init_chip(struct bnxt * bp,bool irq_re_init)10660 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10661 {
10662 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10663 	int rc = 0;
10664 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10665 
10666 	if (irq_re_init) {
10667 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10668 		if (rc) {
10669 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10670 				   rc);
10671 			goto err_out;
10672 		}
10673 	}
10674 
10675 	rc = bnxt_hwrm_ring_alloc(bp);
10676 	if (rc) {
10677 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10678 		goto err_out;
10679 	}
10680 
10681 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10682 	if (rc) {
10683 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10684 		goto err_out;
10685 	}
10686 
10687 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10688 		rx_nr_rings--;
10689 
10690 	/* default vnic 0 */
10691 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10692 	if (rc) {
10693 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10694 		goto err_out;
10695 	}
10696 
10697 	if (BNXT_VF(bp))
10698 		bnxt_hwrm_func_qcfg(bp);
10699 
10700 	rc = bnxt_setup_vnic(bp, vnic);
10701 	if (rc)
10702 		goto err_out;
10703 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10704 		bnxt_hwrm_update_rss_hash_cfg(bp);
10705 
10706 	if (bp->flags & BNXT_FLAG_RFS) {
10707 		rc = bnxt_alloc_rfs_vnics(bp);
10708 		if (rc)
10709 			goto err_out;
10710 	}
10711 
10712 	if (bp->flags & BNXT_FLAG_TPA) {
10713 		rc = bnxt_set_tpa(bp, true);
10714 		if (rc)
10715 			goto err_out;
10716 	}
10717 
10718 	if (BNXT_VF(bp))
10719 		bnxt_update_vf_mac(bp);
10720 
10721 	/* Filter for default vnic 0 */
10722 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10723 	if (rc) {
10724 		if (BNXT_VF(bp) && rc == -ENODEV)
10725 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10726 		else
10727 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10728 		goto err_out;
10729 	}
10730 	vnic->uc_filter_count = 1;
10731 
10732 	vnic->rx_mask = 0;
10733 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10734 		goto skip_rx_mask;
10735 
10736 	if (bp->dev->flags & IFF_BROADCAST)
10737 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10738 
10739 	if (bp->dev->flags & IFF_PROMISC)
10740 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10741 
10742 	if (bp->dev->flags & IFF_ALLMULTI) {
10743 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10744 		vnic->mc_list_count = 0;
10745 	} else if (bp->dev->flags & IFF_MULTICAST) {
10746 		u32 mask = 0;
10747 
10748 		bnxt_mc_list_updated(bp, &mask);
10749 		vnic->rx_mask |= mask;
10750 	}
10751 
10752 	rc = bnxt_cfg_rx_mode(bp);
10753 	if (rc)
10754 		goto err_out;
10755 
10756 skip_rx_mask:
10757 	rc = bnxt_hwrm_set_coal(bp);
10758 	if (rc)
10759 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10760 				rc);
10761 
10762 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10763 		rc = bnxt_setup_nitroa0_vnic(bp);
10764 		if (rc)
10765 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10766 				   rc);
10767 	}
10768 
10769 	if (BNXT_VF(bp)) {
10770 		bnxt_hwrm_func_qcfg(bp);
10771 		netdev_update_features(bp->dev);
10772 	}
10773 
10774 	return 0;
10775 
10776 err_out:
10777 	bnxt_hwrm_resource_free(bp, 0, true);
10778 
10779 	return rc;
10780 }
10781 
bnxt_shutdown_nic(struct bnxt * bp,bool irq_re_init)10782 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10783 {
10784 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10785 	return 0;
10786 }
10787 
bnxt_init_nic(struct bnxt * bp,bool irq_re_init)10788 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10789 {
10790 	bnxt_init_cp_rings(bp);
10791 	bnxt_init_rx_rings(bp);
10792 	bnxt_init_tx_rings(bp);
10793 	bnxt_init_ring_grps(bp, irq_re_init);
10794 	bnxt_init_vnics(bp);
10795 
10796 	return bnxt_init_chip(bp, irq_re_init);
10797 }
10798 
bnxt_set_real_num_queues(struct bnxt * bp)10799 static int bnxt_set_real_num_queues(struct bnxt *bp)
10800 {
10801 	int rc;
10802 	struct net_device *dev = bp->dev;
10803 
10804 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10805 					  bp->tx_nr_rings_xdp);
10806 	if (rc)
10807 		return rc;
10808 
10809 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10810 	if (rc)
10811 		return rc;
10812 
10813 #ifdef CONFIG_RFS_ACCEL
10814 	if (bp->flags & BNXT_FLAG_RFS)
10815 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
10816 #endif
10817 
10818 	return rc;
10819 }
10820 
__bnxt_trim_rings(struct bnxt * bp,int * rx,int * tx,int max,bool shared)10821 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10822 			     bool shared)
10823 {
10824 	int _rx = *rx, _tx = *tx;
10825 
10826 	if (shared) {
10827 		*rx = min_t(int, _rx, max);
10828 		*tx = min_t(int, _tx, max);
10829 	} else {
10830 		if (max < 2)
10831 			return -ENOMEM;
10832 
10833 		while (_rx + _tx > max) {
10834 			if (_rx > _tx && _rx > 1)
10835 				_rx--;
10836 			else if (_tx > 1)
10837 				_tx--;
10838 		}
10839 		*rx = _rx;
10840 		*tx = _tx;
10841 	}
10842 	return 0;
10843 }
10844 
__bnxt_num_tx_to_cp(struct bnxt * bp,int tx,int tx_sets,int tx_xdp)10845 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
10846 {
10847 	return (tx - tx_xdp) / tx_sets + tx_xdp;
10848 }
10849 
bnxt_num_tx_to_cp(struct bnxt * bp,int tx)10850 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
10851 {
10852 	int tcs = bp->num_tc;
10853 
10854 	if (!tcs)
10855 		tcs = 1;
10856 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
10857 }
10858 
bnxt_num_cp_to_tx(struct bnxt * bp,int tx_cp)10859 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
10860 {
10861 	int tcs = bp->num_tc;
10862 
10863 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
10864 	       bp->tx_nr_rings_xdp;
10865 }
10866 
bnxt_trim_rings(struct bnxt * bp,int * rx,int * tx,int max,bool sh)10867 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10868 			   bool sh)
10869 {
10870 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
10871 
10872 	if (tx_cp != *tx) {
10873 		int tx_saved = tx_cp, rc;
10874 
10875 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
10876 		if (rc)
10877 			return rc;
10878 		if (tx_cp != tx_saved)
10879 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
10880 		return 0;
10881 	}
10882 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
10883 }
10884 
bnxt_setup_msix(struct bnxt * bp)10885 static void bnxt_setup_msix(struct bnxt *bp)
10886 {
10887 	const int len = sizeof(bp->irq_tbl[0].name);
10888 	struct net_device *dev = bp->dev;
10889 	int tcs, i;
10890 
10891 	tcs = bp->num_tc;
10892 	if (tcs) {
10893 		int i, off, count;
10894 
10895 		for (i = 0; i < tcs; i++) {
10896 			count = bp->tx_nr_rings_per_tc;
10897 			off = BNXT_TC_TO_RING_BASE(bp, i);
10898 			netdev_set_tc_queue(dev, i, count, off);
10899 		}
10900 	}
10901 
10902 	for (i = 0; i < bp->cp_nr_rings; i++) {
10903 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10904 		char *attr;
10905 
10906 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
10907 			attr = "TxRx";
10908 		else if (i < bp->rx_nr_rings)
10909 			attr = "rx";
10910 		else
10911 			attr = "tx";
10912 
10913 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
10914 			 attr, i);
10915 		bp->irq_tbl[map_idx].handler = bnxt_msix;
10916 	}
10917 }
10918 
10919 static int bnxt_init_int_mode(struct bnxt *bp);
10920 
bnxt_change_msix(struct bnxt * bp,int total)10921 static int bnxt_change_msix(struct bnxt *bp, int total)
10922 {
10923 	struct msi_map map;
10924 	int i;
10925 
10926 	/* add MSIX to the end if needed */
10927 	for (i = bp->total_irqs; i < total; i++) {
10928 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
10929 		if (map.index < 0)
10930 			return bp->total_irqs;
10931 		bp->irq_tbl[i].vector = map.virq;
10932 		bp->total_irqs++;
10933 	}
10934 
10935 	/* trim MSIX from the end if needed */
10936 	for (i = bp->total_irqs; i > total; i--) {
10937 		map.index = i - 1;
10938 		map.virq = bp->irq_tbl[i - 1].vector;
10939 		pci_msix_free_irq(bp->pdev, map);
10940 		bp->total_irqs--;
10941 	}
10942 	return bp->total_irqs;
10943 }
10944 
bnxt_setup_int_mode(struct bnxt * bp)10945 static int bnxt_setup_int_mode(struct bnxt *bp)
10946 {
10947 	int rc;
10948 
10949 	if (!bp->irq_tbl) {
10950 		rc = bnxt_init_int_mode(bp);
10951 		if (rc || !bp->irq_tbl)
10952 			return rc ?: -ENODEV;
10953 	}
10954 
10955 	bnxt_setup_msix(bp);
10956 
10957 	rc = bnxt_set_real_num_queues(bp);
10958 	return rc;
10959 }
10960 
bnxt_get_max_func_rss_ctxs(struct bnxt * bp)10961 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
10962 {
10963 	return bp->hw_resc.max_rsscos_ctxs;
10964 }
10965 
bnxt_get_max_func_vnics(struct bnxt * bp)10966 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
10967 {
10968 	return bp->hw_resc.max_vnics;
10969 }
10970 
bnxt_get_max_func_stat_ctxs(struct bnxt * bp)10971 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
10972 {
10973 	return bp->hw_resc.max_stat_ctxs;
10974 }
10975 
bnxt_get_max_func_cp_rings(struct bnxt * bp)10976 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
10977 {
10978 	return bp->hw_resc.max_cp_rings;
10979 }
10980 
bnxt_get_max_func_cp_rings_for_en(struct bnxt * bp)10981 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
10982 {
10983 	unsigned int cp = bp->hw_resc.max_cp_rings;
10984 
10985 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
10986 		cp -= bnxt_get_ulp_msix_num(bp);
10987 
10988 	return cp;
10989 }
10990 
bnxt_get_max_func_irqs(struct bnxt * bp)10991 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
10992 {
10993 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10994 
10995 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10996 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
10997 
10998 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
10999 }
11000 
bnxt_set_max_func_irqs(struct bnxt * bp,unsigned int max_irqs)11001 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11002 {
11003 	bp->hw_resc.max_irqs = max_irqs;
11004 }
11005 
bnxt_get_avail_cp_rings_for_en(struct bnxt * bp)11006 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11007 {
11008 	unsigned int cp;
11009 
11010 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11011 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11012 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11013 	else
11014 		return cp - bp->cp_nr_rings;
11015 }
11016 
bnxt_get_avail_stat_ctxs_for_en(struct bnxt * bp)11017 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11018 {
11019 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11020 }
11021 
bnxt_get_avail_msix(struct bnxt * bp,int num)11022 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11023 {
11024 	int max_irq = bnxt_get_max_func_irqs(bp);
11025 	int total_req = bp->cp_nr_rings + num;
11026 
11027 	if (max_irq < total_req) {
11028 		num = max_irq - bp->cp_nr_rings;
11029 		if (num <= 0)
11030 			return 0;
11031 	}
11032 	return num;
11033 }
11034 
bnxt_get_num_msix(struct bnxt * bp)11035 static int bnxt_get_num_msix(struct bnxt *bp)
11036 {
11037 	if (!BNXT_NEW_RM(bp))
11038 		return bnxt_get_max_func_irqs(bp);
11039 
11040 	return bnxt_nq_rings_in_use(bp);
11041 }
11042 
bnxt_init_int_mode(struct bnxt * bp)11043 static int bnxt_init_int_mode(struct bnxt *bp)
11044 {
11045 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11046 
11047 	total_vecs = bnxt_get_num_msix(bp);
11048 	max = bnxt_get_max_func_irqs(bp);
11049 	if (total_vecs > max)
11050 		total_vecs = max;
11051 
11052 	if (!total_vecs)
11053 		return 0;
11054 
11055 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11056 		min = 2;
11057 
11058 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11059 					   PCI_IRQ_MSIX);
11060 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11061 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11062 		rc = -ENODEV;
11063 		goto msix_setup_exit;
11064 	}
11065 
11066 	tbl_size = total_vecs;
11067 	if (pci_msix_can_alloc_dyn(bp->pdev))
11068 		tbl_size = max;
11069 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
11070 	if (bp->irq_tbl) {
11071 		for (i = 0; i < total_vecs; i++)
11072 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11073 
11074 		bp->total_irqs = total_vecs;
11075 		/* Trim rings based upon num of vectors allocated */
11076 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11077 				     total_vecs - ulp_msix, min == 1);
11078 		if (rc)
11079 			goto msix_setup_exit;
11080 
11081 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11082 		bp->cp_nr_rings = (min == 1) ?
11083 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11084 				  tx_cp + bp->rx_nr_rings;
11085 
11086 	} else {
11087 		rc = -ENOMEM;
11088 		goto msix_setup_exit;
11089 	}
11090 	return 0;
11091 
11092 msix_setup_exit:
11093 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11094 	kfree(bp->irq_tbl);
11095 	bp->irq_tbl = NULL;
11096 	pci_free_irq_vectors(bp->pdev);
11097 	return rc;
11098 }
11099 
bnxt_clear_int_mode(struct bnxt * bp)11100 static void bnxt_clear_int_mode(struct bnxt *bp)
11101 {
11102 	pci_free_irq_vectors(bp->pdev);
11103 
11104 	kfree(bp->irq_tbl);
11105 	bp->irq_tbl = NULL;
11106 }
11107 
bnxt_reserve_rings(struct bnxt * bp,bool irq_re_init)11108 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11109 {
11110 	bool irq_cleared = false;
11111 	bool irq_change = false;
11112 	int tcs = bp->num_tc;
11113 	int irqs_required;
11114 	int rc;
11115 
11116 	if (!bnxt_need_reserve_rings(bp))
11117 		return 0;
11118 
11119 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11120 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11121 
11122 		if (ulp_msix > bp->ulp_num_msix_want)
11123 			ulp_msix = bp->ulp_num_msix_want;
11124 		irqs_required = ulp_msix + bp->cp_nr_rings;
11125 	} else {
11126 		irqs_required = bnxt_get_num_msix(bp);
11127 	}
11128 
11129 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11130 		irq_change = true;
11131 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11132 			bnxt_ulp_irq_stop(bp);
11133 			bnxt_clear_int_mode(bp);
11134 			irq_cleared = true;
11135 		}
11136 	}
11137 	rc = __bnxt_reserve_rings(bp);
11138 	if (irq_cleared) {
11139 		if (!rc)
11140 			rc = bnxt_init_int_mode(bp);
11141 		bnxt_ulp_irq_restart(bp, rc);
11142 	} else if (irq_change && !rc) {
11143 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11144 			rc = -ENOSPC;
11145 	}
11146 	if (rc) {
11147 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11148 		return rc;
11149 	}
11150 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11151 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11152 		netdev_err(bp->dev, "tx ring reservation failure\n");
11153 		netdev_reset_tc(bp->dev);
11154 		bp->num_tc = 0;
11155 		if (bp->tx_nr_rings_xdp)
11156 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11157 		else
11158 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11159 		return -ENOMEM;
11160 	}
11161 	return 0;
11162 }
11163 
bnxt_free_irq(struct bnxt * bp)11164 static void bnxt_free_irq(struct bnxt *bp)
11165 {
11166 	struct bnxt_irq *irq;
11167 	int i;
11168 
11169 #ifdef CONFIG_RFS_ACCEL
11170 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11171 	bp->dev->rx_cpu_rmap = NULL;
11172 #endif
11173 	if (!bp->irq_tbl || !bp->bnapi)
11174 		return;
11175 
11176 	for (i = 0; i < bp->cp_nr_rings; i++) {
11177 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11178 
11179 		irq = &bp->irq_tbl[map_idx];
11180 		if (irq->requested) {
11181 			if (irq->have_cpumask) {
11182 				irq_update_affinity_hint(irq->vector, NULL);
11183 				free_cpumask_var(irq->cpu_mask);
11184 				irq->have_cpumask = 0;
11185 			}
11186 			free_irq(irq->vector, bp->bnapi[i]);
11187 		}
11188 
11189 		irq->requested = 0;
11190 	}
11191 }
11192 
bnxt_request_irq(struct bnxt * bp)11193 static int bnxt_request_irq(struct bnxt *bp)
11194 {
11195 	int i, j, rc = 0;
11196 	unsigned long flags = 0;
11197 #ifdef CONFIG_RFS_ACCEL
11198 	struct cpu_rmap *rmap;
11199 #endif
11200 
11201 	rc = bnxt_setup_int_mode(bp);
11202 	if (rc) {
11203 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11204 			   rc);
11205 		return rc;
11206 	}
11207 #ifdef CONFIG_RFS_ACCEL
11208 	rmap = bp->dev->rx_cpu_rmap;
11209 #endif
11210 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11211 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11212 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11213 
11214 #ifdef CONFIG_RFS_ACCEL
11215 		if (rmap && bp->bnapi[i]->rx_ring) {
11216 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11217 			if (rc)
11218 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11219 					    j);
11220 			j++;
11221 		}
11222 #endif
11223 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11224 				 bp->bnapi[i]);
11225 		if (rc)
11226 			break;
11227 
11228 		netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector);
11229 		irq->requested = 1;
11230 
11231 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11232 			int numa_node = dev_to_node(&bp->pdev->dev);
11233 
11234 			irq->have_cpumask = 1;
11235 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11236 					irq->cpu_mask);
11237 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11238 			if (rc) {
11239 				netdev_warn(bp->dev,
11240 					    "Update affinity hint failed, IRQ = %d\n",
11241 					    irq->vector);
11242 				break;
11243 			}
11244 		}
11245 	}
11246 	return rc;
11247 }
11248 
bnxt_del_napi(struct bnxt * bp)11249 static void bnxt_del_napi(struct bnxt *bp)
11250 {
11251 	int i;
11252 
11253 	if (!bp->bnapi)
11254 		return;
11255 
11256 	for (i = 0; i < bp->rx_nr_rings; i++)
11257 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11258 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11259 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11260 
11261 	for (i = 0; i < bp->cp_nr_rings; i++) {
11262 		struct bnxt_napi *bnapi = bp->bnapi[i];
11263 
11264 		__netif_napi_del(&bnapi->napi);
11265 	}
11266 	/* We called __netif_napi_del(), we need
11267 	 * to respect an RCU grace period before freeing napi structures.
11268 	 */
11269 	synchronize_net();
11270 }
11271 
bnxt_init_napi(struct bnxt * bp)11272 static void bnxt_init_napi(struct bnxt *bp)
11273 {
11274 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11275 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11276 	struct bnxt_napi *bnapi;
11277 	int i;
11278 
11279 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11280 		poll_fn = bnxt_poll_p5;
11281 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11282 		cp_nr_rings--;
11283 	for (i = 0; i < cp_nr_rings; i++) {
11284 		bnapi = bp->bnapi[i];
11285 		netif_napi_add_config(bp->dev, &bnapi->napi, poll_fn,
11286 				      bnapi->index);
11287 	}
11288 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11289 		bnapi = bp->bnapi[cp_nr_rings];
11290 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11291 	}
11292 }
11293 
bnxt_disable_napi(struct bnxt * bp)11294 static void bnxt_disable_napi(struct bnxt *bp)
11295 {
11296 	int i;
11297 
11298 	if (!bp->bnapi ||
11299 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11300 		return;
11301 
11302 	for (i = 0; i < bp->cp_nr_rings; i++) {
11303 		struct bnxt_napi *bnapi = bp->bnapi[i];
11304 		struct bnxt_cp_ring_info *cpr;
11305 
11306 		cpr = &bnapi->cp_ring;
11307 		if (bnapi->tx_fault)
11308 			cpr->sw_stats->tx.tx_resets++;
11309 		if (bnapi->in_reset)
11310 			cpr->sw_stats->rx.rx_resets++;
11311 		napi_disable(&bnapi->napi);
11312 		if (bnapi->rx_ring)
11313 			cancel_work_sync(&cpr->dim.work);
11314 	}
11315 }
11316 
bnxt_enable_napi(struct bnxt * bp)11317 static void bnxt_enable_napi(struct bnxt *bp)
11318 {
11319 	int i;
11320 
11321 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11322 	for (i = 0; i < bp->cp_nr_rings; i++) {
11323 		struct bnxt_napi *bnapi = bp->bnapi[i];
11324 		struct bnxt_cp_ring_info *cpr;
11325 
11326 		bnapi->tx_fault = 0;
11327 
11328 		cpr = &bnapi->cp_ring;
11329 		bnapi->in_reset = false;
11330 
11331 		if (bnapi->rx_ring) {
11332 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11333 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11334 		}
11335 		napi_enable(&bnapi->napi);
11336 	}
11337 }
11338 
bnxt_tx_disable(struct bnxt * bp)11339 void bnxt_tx_disable(struct bnxt *bp)
11340 {
11341 	int i;
11342 	struct bnxt_tx_ring_info *txr;
11343 
11344 	if (bp->tx_ring) {
11345 		for (i = 0; i < bp->tx_nr_rings; i++) {
11346 			txr = &bp->tx_ring[i];
11347 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11348 		}
11349 	}
11350 	/* Make sure napi polls see @dev_state change */
11351 	synchronize_net();
11352 	/* Drop carrier first to prevent TX timeout */
11353 	netif_carrier_off(bp->dev);
11354 	/* Stop all TX queues */
11355 	netif_tx_disable(bp->dev);
11356 }
11357 
bnxt_tx_enable(struct bnxt * bp)11358 void bnxt_tx_enable(struct bnxt *bp)
11359 {
11360 	int i;
11361 	struct bnxt_tx_ring_info *txr;
11362 
11363 	for (i = 0; i < bp->tx_nr_rings; i++) {
11364 		txr = &bp->tx_ring[i];
11365 		WRITE_ONCE(txr->dev_state, 0);
11366 	}
11367 	/* Make sure napi polls see @dev_state change */
11368 	synchronize_net();
11369 	netif_tx_wake_all_queues(bp->dev);
11370 	if (BNXT_LINK_IS_UP(bp))
11371 		netif_carrier_on(bp->dev);
11372 }
11373 
bnxt_report_fec(struct bnxt_link_info * link_info)11374 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11375 {
11376 	u8 active_fec = link_info->active_fec_sig_mode &
11377 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11378 
11379 	switch (active_fec) {
11380 	default:
11381 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11382 		return "None";
11383 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11384 		return "Clause 74 BaseR";
11385 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11386 		return "Clause 91 RS(528,514)";
11387 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11388 		return "Clause 91 RS544_1XN";
11389 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11390 		return "Clause 91 RS(544,514)";
11391 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11392 		return "Clause 91 RS272_1XN";
11393 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11394 		return "Clause 91 RS(272,257)";
11395 	}
11396 }
11397 
bnxt_report_link(struct bnxt * bp)11398 void bnxt_report_link(struct bnxt *bp)
11399 {
11400 	if (BNXT_LINK_IS_UP(bp)) {
11401 		const char *signal = "";
11402 		const char *flow_ctrl;
11403 		const char *duplex;
11404 		u32 speed;
11405 		u16 fec;
11406 
11407 		netif_carrier_on(bp->dev);
11408 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11409 		if (speed == SPEED_UNKNOWN) {
11410 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11411 			return;
11412 		}
11413 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11414 			duplex = "full";
11415 		else
11416 			duplex = "half";
11417 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11418 			flow_ctrl = "ON - receive & transmit";
11419 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11420 			flow_ctrl = "ON - transmit";
11421 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11422 			flow_ctrl = "ON - receive";
11423 		else
11424 			flow_ctrl = "none";
11425 		if (bp->link_info.phy_qcfg_resp.option_flags &
11426 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11427 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11428 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11429 			switch (sig_mode) {
11430 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11431 				signal = "(NRZ) ";
11432 				break;
11433 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11434 				signal = "(PAM4 56Gbps) ";
11435 				break;
11436 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11437 				signal = "(PAM4 112Gbps) ";
11438 				break;
11439 			default:
11440 				break;
11441 			}
11442 		}
11443 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11444 			    speed, signal, duplex, flow_ctrl);
11445 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11446 			netdev_info(bp->dev, "EEE is %s\n",
11447 				    bp->eee.eee_active ? "active" :
11448 							 "not active");
11449 		fec = bp->link_info.fec_cfg;
11450 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11451 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11452 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11453 				    bnxt_report_fec(&bp->link_info));
11454 	} else {
11455 		netif_carrier_off(bp->dev);
11456 		netdev_err(bp->dev, "NIC Link is Down\n");
11457 	}
11458 }
11459 
bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output * resp)11460 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11461 {
11462 	if (!resp->supported_speeds_auto_mode &&
11463 	    !resp->supported_speeds_force_mode &&
11464 	    !resp->supported_pam4_speeds_auto_mode &&
11465 	    !resp->supported_pam4_speeds_force_mode &&
11466 	    !resp->supported_speeds2_auto_mode &&
11467 	    !resp->supported_speeds2_force_mode)
11468 		return true;
11469 	return false;
11470 }
11471 
bnxt_hwrm_phy_qcaps(struct bnxt * bp)11472 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11473 {
11474 	struct bnxt_link_info *link_info = &bp->link_info;
11475 	struct hwrm_port_phy_qcaps_output *resp;
11476 	struct hwrm_port_phy_qcaps_input *req;
11477 	int rc = 0;
11478 
11479 	if (bp->hwrm_spec_code < 0x10201)
11480 		return 0;
11481 
11482 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11483 	if (rc)
11484 		return rc;
11485 
11486 	resp = hwrm_req_hold(bp, req);
11487 	rc = hwrm_req_send(bp, req);
11488 	if (rc)
11489 		goto hwrm_phy_qcaps_exit;
11490 
11491 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11492 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11493 		struct ethtool_keee *eee = &bp->eee;
11494 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11495 
11496 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11497 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11498 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11499 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11500 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11501 	}
11502 
11503 	if (bp->hwrm_spec_code >= 0x10a01) {
11504 		if (bnxt_phy_qcaps_no_speed(resp)) {
11505 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11506 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11507 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11508 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11509 			netdev_info(bp->dev, "Ethernet link enabled\n");
11510 			/* Phy re-enabled, reprobe the speeds */
11511 			link_info->support_auto_speeds = 0;
11512 			link_info->support_pam4_auto_speeds = 0;
11513 			link_info->support_auto_speeds2 = 0;
11514 		}
11515 	}
11516 	if (resp->supported_speeds_auto_mode)
11517 		link_info->support_auto_speeds =
11518 			le16_to_cpu(resp->supported_speeds_auto_mode);
11519 	if (resp->supported_pam4_speeds_auto_mode)
11520 		link_info->support_pam4_auto_speeds =
11521 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11522 	if (resp->supported_speeds2_auto_mode)
11523 		link_info->support_auto_speeds2 =
11524 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11525 
11526 	bp->port_count = resp->port_cnt;
11527 
11528 hwrm_phy_qcaps_exit:
11529 	hwrm_req_drop(bp, req);
11530 	return rc;
11531 }
11532 
bnxt_support_dropped(u16 advertising,u16 supported)11533 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11534 {
11535 	u16 diff = advertising ^ supported;
11536 
11537 	return ((supported | diff) != supported);
11538 }
11539 
bnxt_support_speed_dropped(struct bnxt_link_info * link_info)11540 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11541 {
11542 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11543 
11544 	/* Check if any advertised speeds are no longer supported. The caller
11545 	 * holds the link_lock mutex, so we can modify link_info settings.
11546 	 */
11547 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11548 		if (bnxt_support_dropped(link_info->advertising,
11549 					 link_info->support_auto_speeds2)) {
11550 			link_info->advertising = link_info->support_auto_speeds2;
11551 			return true;
11552 		}
11553 		return false;
11554 	}
11555 	if (bnxt_support_dropped(link_info->advertising,
11556 				 link_info->support_auto_speeds)) {
11557 		link_info->advertising = link_info->support_auto_speeds;
11558 		return true;
11559 	}
11560 	if (bnxt_support_dropped(link_info->advertising_pam4,
11561 				 link_info->support_pam4_auto_speeds)) {
11562 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
11563 		return true;
11564 	}
11565 	return false;
11566 }
11567 
bnxt_update_link(struct bnxt * bp,bool chng_link_state)11568 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
11569 {
11570 	struct bnxt_link_info *link_info = &bp->link_info;
11571 	struct hwrm_port_phy_qcfg_output *resp;
11572 	struct hwrm_port_phy_qcfg_input *req;
11573 	u8 link_state = link_info->link_state;
11574 	bool support_changed;
11575 	int rc;
11576 
11577 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
11578 	if (rc)
11579 		return rc;
11580 
11581 	resp = hwrm_req_hold(bp, req);
11582 	rc = hwrm_req_send(bp, req);
11583 	if (rc) {
11584 		hwrm_req_drop(bp, req);
11585 		if (BNXT_VF(bp) && rc == -ENODEV) {
11586 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
11587 			rc = 0;
11588 		}
11589 		return rc;
11590 	}
11591 
11592 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
11593 	link_info->phy_link_status = resp->link;
11594 	link_info->duplex = resp->duplex_cfg;
11595 	if (bp->hwrm_spec_code >= 0x10800)
11596 		link_info->duplex = resp->duplex_state;
11597 	link_info->pause = resp->pause;
11598 	link_info->auto_mode = resp->auto_mode;
11599 	link_info->auto_pause_setting = resp->auto_pause;
11600 	link_info->lp_pause = resp->link_partner_adv_pause;
11601 	link_info->force_pause_setting = resp->force_pause;
11602 	link_info->duplex_setting = resp->duplex_cfg;
11603 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
11604 		link_info->link_speed = le16_to_cpu(resp->link_speed);
11605 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
11606 			link_info->active_lanes = resp->active_lanes;
11607 	} else {
11608 		link_info->link_speed = 0;
11609 		link_info->active_lanes = 0;
11610 	}
11611 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
11612 	link_info->force_pam4_link_speed =
11613 		le16_to_cpu(resp->force_pam4_link_speed);
11614 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
11615 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
11616 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
11617 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
11618 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
11619 	link_info->auto_pam4_link_speeds =
11620 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
11621 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
11622 	link_info->lp_auto_link_speeds =
11623 		le16_to_cpu(resp->link_partner_adv_speeds);
11624 	link_info->lp_auto_pam4_link_speeds =
11625 		resp->link_partner_pam4_adv_speeds;
11626 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
11627 	link_info->phy_ver[0] = resp->phy_maj;
11628 	link_info->phy_ver[1] = resp->phy_min;
11629 	link_info->phy_ver[2] = resp->phy_bld;
11630 	link_info->media_type = resp->media_type;
11631 	link_info->phy_type = resp->phy_type;
11632 	link_info->transceiver = resp->xcvr_pkg_type;
11633 	link_info->phy_addr = resp->eee_config_phy_addr &
11634 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
11635 	link_info->module_status = resp->module_status;
11636 
11637 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
11638 		struct ethtool_keee *eee = &bp->eee;
11639 		u16 fw_speeds;
11640 
11641 		eee->eee_active = 0;
11642 		if (resp->eee_config_phy_addr &
11643 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
11644 			eee->eee_active = 1;
11645 			fw_speeds = le16_to_cpu(
11646 				resp->link_partner_adv_eee_link_speed_mask);
11647 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
11648 		}
11649 
11650 		/* Pull initial EEE config */
11651 		if (!chng_link_state) {
11652 			if (resp->eee_config_phy_addr &
11653 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
11654 				eee->eee_enabled = 1;
11655 
11656 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
11657 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
11658 
11659 			if (resp->eee_config_phy_addr &
11660 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
11661 				__le32 tmr;
11662 
11663 				eee->tx_lpi_enabled = 1;
11664 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
11665 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
11666 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
11667 			}
11668 		}
11669 	}
11670 
11671 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
11672 	if (bp->hwrm_spec_code >= 0x10504) {
11673 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
11674 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
11675 	}
11676 	/* TODO: need to add more logic to report VF link */
11677 	if (chng_link_state) {
11678 		if (link_info->phy_link_status == BNXT_LINK_LINK)
11679 			link_info->link_state = BNXT_LINK_STATE_UP;
11680 		else
11681 			link_info->link_state = BNXT_LINK_STATE_DOWN;
11682 		if (link_state != link_info->link_state)
11683 			bnxt_report_link(bp);
11684 	} else {
11685 		/* always link down if not require to update link state */
11686 		link_info->link_state = BNXT_LINK_STATE_DOWN;
11687 	}
11688 	hwrm_req_drop(bp, req);
11689 
11690 	if (!BNXT_PHY_CFG_ABLE(bp))
11691 		return 0;
11692 
11693 	support_changed = bnxt_support_speed_dropped(link_info);
11694 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
11695 		bnxt_hwrm_set_link_setting(bp, true, false);
11696 	return 0;
11697 }
11698 
bnxt_get_port_module_status(struct bnxt * bp)11699 static void bnxt_get_port_module_status(struct bnxt *bp)
11700 {
11701 	struct bnxt_link_info *link_info = &bp->link_info;
11702 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
11703 	u8 module_status;
11704 
11705 	if (bnxt_update_link(bp, true))
11706 		return;
11707 
11708 	module_status = link_info->module_status;
11709 	switch (module_status) {
11710 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
11711 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
11712 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
11713 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
11714 			    bp->pf.port_id);
11715 		if (bp->hwrm_spec_code >= 0x10201) {
11716 			netdev_warn(bp->dev, "Module part number %s\n",
11717 				    resp->phy_vendor_partnumber);
11718 		}
11719 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
11720 			netdev_warn(bp->dev, "TX is disabled\n");
11721 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
11722 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
11723 	}
11724 }
11725 
11726 static void
bnxt_hwrm_set_pause_common(struct bnxt * bp,struct hwrm_port_phy_cfg_input * req)11727 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11728 {
11729 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
11730 		if (bp->hwrm_spec_code >= 0x10201)
11731 			req->auto_pause =
11732 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
11733 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11734 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
11735 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11736 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
11737 		req->enables |=
11738 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11739 	} else {
11740 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11741 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
11742 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11743 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
11744 		req->enables |=
11745 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
11746 		if (bp->hwrm_spec_code >= 0x10201) {
11747 			req->auto_pause = req->force_pause;
11748 			req->enables |= cpu_to_le32(
11749 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11750 		}
11751 	}
11752 }
11753 
bnxt_hwrm_set_link_common(struct bnxt * bp,struct hwrm_port_phy_cfg_input * req)11754 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11755 {
11756 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
11757 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
11758 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11759 			req->enables |=
11760 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
11761 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
11762 		} else if (bp->link_info.advertising) {
11763 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
11764 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
11765 		}
11766 		if (bp->link_info.advertising_pam4) {
11767 			req->enables |=
11768 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
11769 			req->auto_link_pam4_speed_mask =
11770 				cpu_to_le16(bp->link_info.advertising_pam4);
11771 		}
11772 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
11773 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
11774 	} else {
11775 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
11776 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11777 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
11778 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
11779 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
11780 				   (u32)bp->link_info.req_link_speed);
11781 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
11782 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11783 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
11784 		} else {
11785 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11786 		}
11787 	}
11788 
11789 	/* tell chimp that the setting takes effect immediately */
11790 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
11791 }
11792 
bnxt_hwrm_set_pause(struct bnxt * bp)11793 int bnxt_hwrm_set_pause(struct bnxt *bp)
11794 {
11795 	struct hwrm_port_phy_cfg_input *req;
11796 	int rc;
11797 
11798 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11799 	if (rc)
11800 		return rc;
11801 
11802 	bnxt_hwrm_set_pause_common(bp, req);
11803 
11804 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
11805 	    bp->link_info.force_link_chng)
11806 		bnxt_hwrm_set_link_common(bp, req);
11807 
11808 	rc = hwrm_req_send(bp, req);
11809 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
11810 		/* since changing of pause setting doesn't trigger any link
11811 		 * change event, the driver needs to update the current pause
11812 		 * result upon successfully return of the phy_cfg command
11813 		 */
11814 		bp->link_info.pause =
11815 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
11816 		bp->link_info.auto_pause_setting = 0;
11817 		if (!bp->link_info.force_link_chng)
11818 			bnxt_report_link(bp);
11819 	}
11820 	bp->link_info.force_link_chng = false;
11821 	return rc;
11822 }
11823 
bnxt_hwrm_set_eee(struct bnxt * bp,struct hwrm_port_phy_cfg_input * req)11824 static void bnxt_hwrm_set_eee(struct bnxt *bp,
11825 			      struct hwrm_port_phy_cfg_input *req)
11826 {
11827 	struct ethtool_keee *eee = &bp->eee;
11828 
11829 	if (eee->eee_enabled) {
11830 		u16 eee_speeds;
11831 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
11832 
11833 		if (eee->tx_lpi_enabled)
11834 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
11835 		else
11836 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
11837 
11838 		req->flags |= cpu_to_le32(flags);
11839 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
11840 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
11841 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
11842 	} else {
11843 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
11844 	}
11845 }
11846 
bnxt_hwrm_set_link_setting(struct bnxt * bp,bool set_pause,bool set_eee)11847 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
11848 {
11849 	struct hwrm_port_phy_cfg_input *req;
11850 	int rc;
11851 
11852 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11853 	if (rc)
11854 		return rc;
11855 
11856 	if (set_pause)
11857 		bnxt_hwrm_set_pause_common(bp, req);
11858 
11859 	bnxt_hwrm_set_link_common(bp, req);
11860 
11861 	if (set_eee)
11862 		bnxt_hwrm_set_eee(bp, req);
11863 	return hwrm_req_send(bp, req);
11864 }
11865 
bnxt_hwrm_shutdown_link(struct bnxt * bp)11866 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
11867 {
11868 	struct hwrm_port_phy_cfg_input *req;
11869 	int rc;
11870 
11871 	if (!BNXT_SINGLE_PF(bp))
11872 		return 0;
11873 
11874 	if (pci_num_vf(bp->pdev) &&
11875 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
11876 		return 0;
11877 
11878 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11879 	if (rc)
11880 		return rc;
11881 
11882 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
11883 	rc = hwrm_req_send(bp, req);
11884 	if (!rc) {
11885 		mutex_lock(&bp->link_lock);
11886 		/* Device is not obliged link down in certain scenarios, even
11887 		 * when forced. Setting the state unknown is consistent with
11888 		 * driver startup and will force link state to be reported
11889 		 * during subsequent open based on PORT_PHY_QCFG.
11890 		 */
11891 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
11892 		mutex_unlock(&bp->link_lock);
11893 	}
11894 	return rc;
11895 }
11896 
bnxt_fw_reset_via_optee(struct bnxt * bp)11897 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
11898 {
11899 #ifdef CONFIG_TEE_BNXT_FW
11900 	int rc = tee_bnxt_fw_load();
11901 
11902 	if (rc)
11903 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
11904 
11905 	return rc;
11906 #else
11907 	netdev_err(bp->dev, "OP-TEE not supported\n");
11908 	return -ENODEV;
11909 #endif
11910 }
11911 
bnxt_try_recover_fw(struct bnxt * bp)11912 static int bnxt_try_recover_fw(struct bnxt *bp)
11913 {
11914 	if (bp->fw_health && bp->fw_health->status_reliable) {
11915 		int retry = 0, rc;
11916 		u32 sts;
11917 
11918 		do {
11919 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11920 			rc = bnxt_hwrm_poll(bp);
11921 			if (!BNXT_FW_IS_BOOTING(sts) &&
11922 			    !BNXT_FW_IS_RECOVERING(sts))
11923 				break;
11924 			retry++;
11925 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
11926 
11927 		if (!BNXT_FW_IS_HEALTHY(sts)) {
11928 			netdev_err(bp->dev,
11929 				   "Firmware not responding, status: 0x%x\n",
11930 				   sts);
11931 			rc = -ENODEV;
11932 		}
11933 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
11934 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
11935 			return bnxt_fw_reset_via_optee(bp);
11936 		}
11937 		return rc;
11938 	}
11939 
11940 	return -ENODEV;
11941 }
11942 
bnxt_clear_reservations(struct bnxt * bp,bool fw_reset)11943 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
11944 {
11945 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11946 
11947 	if (!BNXT_NEW_RM(bp))
11948 		return; /* no resource reservations required */
11949 
11950 	hw_resc->resv_cp_rings = 0;
11951 	hw_resc->resv_stat_ctxs = 0;
11952 	hw_resc->resv_irqs = 0;
11953 	hw_resc->resv_tx_rings = 0;
11954 	hw_resc->resv_rx_rings = 0;
11955 	hw_resc->resv_hw_ring_grps = 0;
11956 	hw_resc->resv_vnics = 0;
11957 	hw_resc->resv_rsscos_ctxs = 0;
11958 	if (!fw_reset) {
11959 		bp->tx_nr_rings = 0;
11960 		bp->rx_nr_rings = 0;
11961 	}
11962 }
11963 
bnxt_cancel_reservations(struct bnxt * bp,bool fw_reset)11964 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
11965 {
11966 	int rc;
11967 
11968 	if (!BNXT_NEW_RM(bp))
11969 		return 0; /* no resource reservations required */
11970 
11971 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
11972 	if (rc)
11973 		netdev_err(bp->dev, "resc_qcaps failed\n");
11974 
11975 	bnxt_clear_reservations(bp, fw_reset);
11976 
11977 	return rc;
11978 }
11979 
bnxt_hwrm_if_change(struct bnxt * bp,bool up)11980 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
11981 {
11982 	struct hwrm_func_drv_if_change_output *resp;
11983 	struct hwrm_func_drv_if_change_input *req;
11984 	bool fw_reset = !bp->irq_tbl;
11985 	bool resc_reinit = false;
11986 	int rc, retry = 0;
11987 	u32 flags = 0;
11988 
11989 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
11990 		return 0;
11991 
11992 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
11993 	if (rc)
11994 		return rc;
11995 
11996 	if (up)
11997 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
11998 	resp = hwrm_req_hold(bp, req);
11999 
12000 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12001 	while (retry < BNXT_FW_IF_RETRY) {
12002 		rc = hwrm_req_send(bp, req);
12003 		if (rc != -EAGAIN)
12004 			break;
12005 
12006 		msleep(50);
12007 		retry++;
12008 	}
12009 
12010 	if (rc == -EAGAIN) {
12011 		hwrm_req_drop(bp, req);
12012 		return rc;
12013 	} else if (!rc) {
12014 		flags = le32_to_cpu(resp->flags);
12015 	} else if (up) {
12016 		rc = bnxt_try_recover_fw(bp);
12017 		fw_reset = true;
12018 	}
12019 	hwrm_req_drop(bp, req);
12020 	if (rc)
12021 		return rc;
12022 
12023 	if (!up) {
12024 		bnxt_inv_fw_health_reg(bp);
12025 		return 0;
12026 	}
12027 
12028 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12029 		resc_reinit = true;
12030 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12031 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12032 		fw_reset = true;
12033 	else
12034 		bnxt_remap_fw_health_regs(bp);
12035 
12036 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12037 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12038 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12039 		return -ENODEV;
12040 	}
12041 	if (resc_reinit || fw_reset) {
12042 		if (fw_reset) {
12043 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12044 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12045 				bnxt_ulp_irq_stop(bp);
12046 			bnxt_free_ctx_mem(bp, false);
12047 			bnxt_dcb_free(bp);
12048 			rc = bnxt_fw_init_one(bp);
12049 			if (rc) {
12050 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12051 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12052 				return rc;
12053 			}
12054 			bnxt_clear_int_mode(bp);
12055 			rc = bnxt_init_int_mode(bp);
12056 			if (rc) {
12057 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12058 				netdev_err(bp->dev, "init int mode failed\n");
12059 				return rc;
12060 			}
12061 		}
12062 		rc = bnxt_cancel_reservations(bp, fw_reset);
12063 	}
12064 	return rc;
12065 }
12066 
bnxt_hwrm_port_led_qcaps(struct bnxt * bp)12067 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12068 {
12069 	struct hwrm_port_led_qcaps_output *resp;
12070 	struct hwrm_port_led_qcaps_input *req;
12071 	struct bnxt_pf_info *pf = &bp->pf;
12072 	int rc;
12073 
12074 	bp->num_leds = 0;
12075 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12076 		return 0;
12077 
12078 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12079 	if (rc)
12080 		return rc;
12081 
12082 	req->port_id = cpu_to_le16(pf->port_id);
12083 	resp = hwrm_req_hold(bp, req);
12084 	rc = hwrm_req_send(bp, req);
12085 	if (rc) {
12086 		hwrm_req_drop(bp, req);
12087 		return rc;
12088 	}
12089 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12090 		int i;
12091 
12092 		bp->num_leds = resp->num_leds;
12093 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12094 						 bp->num_leds);
12095 		for (i = 0; i < bp->num_leds; i++) {
12096 			struct bnxt_led_info *led = &bp->leds[i];
12097 			__le16 caps = led->led_state_caps;
12098 
12099 			if (!led->led_group_id ||
12100 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12101 				bp->num_leds = 0;
12102 				break;
12103 			}
12104 		}
12105 	}
12106 	hwrm_req_drop(bp, req);
12107 	return 0;
12108 }
12109 
bnxt_hwrm_alloc_wol_fltr(struct bnxt * bp)12110 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12111 {
12112 	struct hwrm_wol_filter_alloc_output *resp;
12113 	struct hwrm_wol_filter_alloc_input *req;
12114 	int rc;
12115 
12116 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12117 	if (rc)
12118 		return rc;
12119 
12120 	req->port_id = cpu_to_le16(bp->pf.port_id);
12121 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12122 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12123 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12124 
12125 	resp = hwrm_req_hold(bp, req);
12126 	rc = hwrm_req_send(bp, req);
12127 	if (!rc)
12128 		bp->wol_filter_id = resp->wol_filter_id;
12129 	hwrm_req_drop(bp, req);
12130 	return rc;
12131 }
12132 
bnxt_hwrm_free_wol_fltr(struct bnxt * bp)12133 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12134 {
12135 	struct hwrm_wol_filter_free_input *req;
12136 	int rc;
12137 
12138 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12139 	if (rc)
12140 		return rc;
12141 
12142 	req->port_id = cpu_to_le16(bp->pf.port_id);
12143 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12144 	req->wol_filter_id = bp->wol_filter_id;
12145 
12146 	return hwrm_req_send(bp, req);
12147 }
12148 
bnxt_hwrm_get_wol_fltrs(struct bnxt * bp,u16 handle)12149 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12150 {
12151 	struct hwrm_wol_filter_qcfg_output *resp;
12152 	struct hwrm_wol_filter_qcfg_input *req;
12153 	u16 next_handle = 0;
12154 	int rc;
12155 
12156 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12157 	if (rc)
12158 		return rc;
12159 
12160 	req->port_id = cpu_to_le16(bp->pf.port_id);
12161 	req->handle = cpu_to_le16(handle);
12162 	resp = hwrm_req_hold(bp, req);
12163 	rc = hwrm_req_send(bp, req);
12164 	if (!rc) {
12165 		next_handle = le16_to_cpu(resp->next_handle);
12166 		if (next_handle != 0) {
12167 			if (resp->wol_type ==
12168 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12169 				bp->wol = 1;
12170 				bp->wol_filter_id = resp->wol_filter_id;
12171 			}
12172 		}
12173 	}
12174 	hwrm_req_drop(bp, req);
12175 	return next_handle;
12176 }
12177 
bnxt_get_wol_settings(struct bnxt * bp)12178 static void bnxt_get_wol_settings(struct bnxt *bp)
12179 {
12180 	u16 handle = 0;
12181 
12182 	bp->wol = 0;
12183 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12184 		return;
12185 
12186 	do {
12187 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12188 	} while (handle && handle != 0xffff);
12189 }
12190 
bnxt_eee_config_ok(struct bnxt * bp)12191 static bool bnxt_eee_config_ok(struct bnxt *bp)
12192 {
12193 	struct ethtool_keee *eee = &bp->eee;
12194 	struct bnxt_link_info *link_info = &bp->link_info;
12195 
12196 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12197 		return true;
12198 
12199 	if (eee->eee_enabled) {
12200 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12201 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12202 
12203 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12204 
12205 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12206 			eee->eee_enabled = 0;
12207 			return false;
12208 		}
12209 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12210 			linkmode_and(eee->advertised, advertising,
12211 				     eee->supported);
12212 			return false;
12213 		}
12214 	}
12215 	return true;
12216 }
12217 
bnxt_update_phy_setting(struct bnxt * bp)12218 static int bnxt_update_phy_setting(struct bnxt *bp)
12219 {
12220 	int rc;
12221 	bool update_link = false;
12222 	bool update_pause = false;
12223 	bool update_eee = false;
12224 	struct bnxt_link_info *link_info = &bp->link_info;
12225 
12226 	rc = bnxt_update_link(bp, true);
12227 	if (rc) {
12228 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12229 			   rc);
12230 		return rc;
12231 	}
12232 	if (!BNXT_SINGLE_PF(bp))
12233 		return 0;
12234 
12235 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12236 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12237 	    link_info->req_flow_ctrl)
12238 		update_pause = true;
12239 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12240 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12241 		update_pause = true;
12242 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12243 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12244 			update_link = true;
12245 		if (bnxt_force_speed_updated(link_info))
12246 			update_link = true;
12247 		if (link_info->req_duplex != link_info->duplex_setting)
12248 			update_link = true;
12249 	} else {
12250 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12251 			update_link = true;
12252 		if (bnxt_auto_speed_updated(link_info))
12253 			update_link = true;
12254 	}
12255 
12256 	/* The last close may have shutdown the link, so need to call
12257 	 * PHY_CFG to bring it back up.
12258 	 */
12259 	if (!BNXT_LINK_IS_UP(bp))
12260 		update_link = true;
12261 
12262 	if (!bnxt_eee_config_ok(bp))
12263 		update_eee = true;
12264 
12265 	if (update_link)
12266 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12267 	else if (update_pause)
12268 		rc = bnxt_hwrm_set_pause(bp);
12269 	if (rc) {
12270 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12271 			   rc);
12272 		return rc;
12273 	}
12274 
12275 	return rc;
12276 }
12277 
12278 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12279 
bnxt_reinit_after_abort(struct bnxt * bp)12280 static int bnxt_reinit_after_abort(struct bnxt *bp)
12281 {
12282 	int rc;
12283 
12284 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12285 		return -EBUSY;
12286 
12287 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12288 		return -ENODEV;
12289 
12290 	rc = bnxt_fw_init_one(bp);
12291 	if (!rc) {
12292 		bnxt_clear_int_mode(bp);
12293 		rc = bnxt_init_int_mode(bp);
12294 		if (!rc) {
12295 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12296 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12297 		}
12298 	}
12299 	return rc;
12300 }
12301 
bnxt_cfg_one_usr_fltr(struct bnxt * bp,struct bnxt_filter_base * fltr)12302 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12303 {
12304 	struct bnxt_ntuple_filter *ntp_fltr;
12305 	struct bnxt_l2_filter *l2_fltr;
12306 
12307 	if (list_empty(&fltr->list))
12308 		return;
12309 
12310 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12311 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12312 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12313 		atomic_inc(&l2_fltr->refcnt);
12314 		ntp_fltr->l2_fltr = l2_fltr;
12315 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12316 			bnxt_del_ntp_filter(bp, ntp_fltr);
12317 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12318 				   fltr->sw_id);
12319 		}
12320 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12321 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12322 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12323 			bnxt_del_l2_filter(bp, l2_fltr);
12324 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12325 				   fltr->sw_id);
12326 		}
12327 	}
12328 }
12329 
bnxt_cfg_usr_fltrs(struct bnxt * bp)12330 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12331 {
12332 	struct bnxt_filter_base *usr_fltr, *tmp;
12333 
12334 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12335 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12336 }
12337 
bnxt_set_xps_mapping(struct bnxt * bp)12338 static int bnxt_set_xps_mapping(struct bnxt *bp)
12339 {
12340 	int numa_node = dev_to_node(&bp->pdev->dev);
12341 	unsigned int q_idx, map_idx, cpu, i;
12342 	const struct cpumask *cpu_mask_ptr;
12343 	int nr_cpus = num_online_cpus();
12344 	cpumask_t *q_map;
12345 	int rc = 0;
12346 
12347 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12348 	if (!q_map)
12349 		return -ENOMEM;
12350 
12351 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12352 	 * Each TC has the same number of TX queues. The nth TX queue for each
12353 	 * TC will have the same CPU mask.
12354 	 */
12355 	for (i = 0; i < nr_cpus; i++) {
12356 		map_idx = i % bp->tx_nr_rings_per_tc;
12357 		cpu = cpumask_local_spread(i, numa_node);
12358 		cpu_mask_ptr = get_cpu_mask(cpu);
12359 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12360 	}
12361 
12362 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12363 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12364 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12365 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12366 		if (rc) {
12367 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12368 				    q_idx);
12369 			break;
12370 		}
12371 	}
12372 
12373 	kfree(q_map);
12374 
12375 	return rc;
12376 }
12377 
__bnxt_open_nic(struct bnxt * bp,bool irq_re_init,bool link_re_init)12378 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12379 {
12380 	int rc = 0;
12381 
12382 	netif_carrier_off(bp->dev);
12383 	if (irq_re_init) {
12384 		/* Reserve rings now if none were reserved at driver probe. */
12385 		rc = bnxt_init_dflt_ring_mode(bp);
12386 		if (rc) {
12387 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12388 			return rc;
12389 		}
12390 	}
12391 	rc = bnxt_reserve_rings(bp, irq_re_init);
12392 	if (rc)
12393 		return rc;
12394 
12395 	rc = bnxt_alloc_mem(bp, irq_re_init);
12396 	if (rc) {
12397 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12398 		goto open_err_free_mem;
12399 	}
12400 
12401 	if (irq_re_init) {
12402 		bnxt_init_napi(bp);
12403 		rc = bnxt_request_irq(bp);
12404 		if (rc) {
12405 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12406 			goto open_err_irq;
12407 		}
12408 	}
12409 
12410 	rc = bnxt_init_nic(bp, irq_re_init);
12411 	if (rc) {
12412 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12413 		goto open_err_irq;
12414 	}
12415 
12416 	bnxt_enable_napi(bp);
12417 	bnxt_debug_dev_init(bp);
12418 
12419 	if (link_re_init) {
12420 		mutex_lock(&bp->link_lock);
12421 		rc = bnxt_update_phy_setting(bp);
12422 		mutex_unlock(&bp->link_lock);
12423 		if (rc) {
12424 			netdev_warn(bp->dev, "failed to update phy settings\n");
12425 			if (BNXT_SINGLE_PF(bp)) {
12426 				bp->link_info.phy_retry = true;
12427 				bp->link_info.phy_retry_expires =
12428 					jiffies + 5 * HZ;
12429 			}
12430 		}
12431 	}
12432 
12433 	if (irq_re_init) {
12434 		udp_tunnel_nic_reset_ntf(bp->dev);
12435 		rc = bnxt_set_xps_mapping(bp);
12436 		if (rc)
12437 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12438 	}
12439 
12440 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12441 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12442 			static_branch_enable(&bnxt_xdp_locking_key);
12443 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12444 		static_branch_disable(&bnxt_xdp_locking_key);
12445 	}
12446 	set_bit(BNXT_STATE_OPEN, &bp->state);
12447 	bnxt_enable_int(bp);
12448 	/* Enable TX queues */
12449 	bnxt_tx_enable(bp);
12450 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12451 	/* Poll link status and check for SFP+ module status */
12452 	mutex_lock(&bp->link_lock);
12453 	bnxt_get_port_module_status(bp);
12454 	mutex_unlock(&bp->link_lock);
12455 
12456 	/* VF-reps may need to be re-opened after the PF is re-opened */
12457 	if (BNXT_PF(bp))
12458 		bnxt_vf_reps_open(bp);
12459 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
12460 		WRITE_ONCE(bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS);
12461 	bnxt_ptp_init_rtc(bp, true);
12462 	bnxt_ptp_cfg_tstamp_filters(bp);
12463 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12464 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12465 	bnxt_cfg_usr_fltrs(bp);
12466 	return 0;
12467 
12468 open_err_irq:
12469 	bnxt_del_napi(bp);
12470 
12471 open_err_free_mem:
12472 	bnxt_free_skbs(bp);
12473 	bnxt_free_irq(bp);
12474 	bnxt_free_mem(bp, true);
12475 	return rc;
12476 }
12477 
12478 /* rtnl_lock held */
bnxt_open_nic(struct bnxt * bp,bool irq_re_init,bool link_re_init)12479 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12480 {
12481 	int rc = 0;
12482 
12483 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12484 		rc = -EIO;
12485 	if (!rc)
12486 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12487 	if (rc) {
12488 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12489 		dev_close(bp->dev);
12490 	}
12491 	return rc;
12492 }
12493 
12494 /* rtnl_lock held, open the NIC half way by allocating all resources, but
12495  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
12496  * self tests.
12497  */
bnxt_half_open_nic(struct bnxt * bp)12498 int bnxt_half_open_nic(struct bnxt *bp)
12499 {
12500 	int rc = 0;
12501 
12502 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12503 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12504 		rc = -ENODEV;
12505 		goto half_open_err;
12506 	}
12507 
12508 	rc = bnxt_alloc_mem(bp, true);
12509 	if (rc) {
12510 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12511 		goto half_open_err;
12512 	}
12513 	bnxt_init_napi(bp);
12514 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12515 	rc = bnxt_init_nic(bp, true);
12516 	if (rc) {
12517 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12518 		bnxt_del_napi(bp);
12519 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12520 		goto half_open_err;
12521 	}
12522 	return 0;
12523 
12524 half_open_err:
12525 	bnxt_free_skbs(bp);
12526 	bnxt_free_mem(bp, true);
12527 	dev_close(bp->dev);
12528 	return rc;
12529 }
12530 
12531 /* rtnl_lock held, this call can only be made after a previous successful
12532  * call to bnxt_half_open_nic().
12533  */
bnxt_half_close_nic(struct bnxt * bp)12534 void bnxt_half_close_nic(struct bnxt *bp)
12535 {
12536 	bnxt_hwrm_resource_free(bp, false, true);
12537 	bnxt_del_napi(bp);
12538 	bnxt_free_skbs(bp);
12539 	bnxt_free_mem(bp, true);
12540 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12541 }
12542 
bnxt_reenable_sriov(struct bnxt * bp)12543 void bnxt_reenable_sriov(struct bnxt *bp)
12544 {
12545 	if (BNXT_PF(bp)) {
12546 		struct bnxt_pf_info *pf = &bp->pf;
12547 		int n = pf->active_vfs;
12548 
12549 		if (n)
12550 			bnxt_cfg_hw_sriov(bp, &n, true);
12551 	}
12552 }
12553 
bnxt_open(struct net_device * dev)12554 static int bnxt_open(struct net_device *dev)
12555 {
12556 	struct bnxt *bp = netdev_priv(dev);
12557 	int rc;
12558 
12559 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12560 		rc = bnxt_reinit_after_abort(bp);
12561 		if (rc) {
12562 			if (rc == -EBUSY)
12563 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
12564 			else
12565 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
12566 			return -ENODEV;
12567 		}
12568 	}
12569 
12570 	rc = bnxt_hwrm_if_change(bp, true);
12571 	if (rc)
12572 		return rc;
12573 
12574 	rc = __bnxt_open_nic(bp, true, true);
12575 	if (rc) {
12576 		bnxt_hwrm_if_change(bp, false);
12577 	} else {
12578 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
12579 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12580 				bnxt_queue_sp_work(bp,
12581 						   BNXT_RESTART_ULP_SP_EVENT);
12582 		}
12583 	}
12584 
12585 	return rc;
12586 }
12587 
bnxt_drv_busy(struct bnxt * bp)12588 static bool bnxt_drv_busy(struct bnxt *bp)
12589 {
12590 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
12591 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
12592 }
12593 
12594 static void bnxt_get_ring_stats(struct bnxt *bp,
12595 				struct rtnl_link_stats64 *stats);
12596 
__bnxt_close_nic(struct bnxt * bp,bool irq_re_init,bool link_re_init)12597 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
12598 			     bool link_re_init)
12599 {
12600 	/* Close the VF-reps before closing PF */
12601 	if (BNXT_PF(bp))
12602 		bnxt_vf_reps_close(bp);
12603 
12604 	/* Change device state to avoid TX queue wake up's */
12605 	bnxt_tx_disable(bp);
12606 
12607 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12608 	smp_mb__after_atomic();
12609 	while (bnxt_drv_busy(bp))
12610 		msleep(20);
12611 
12612 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12613 		bnxt_clear_rss_ctxs(bp);
12614 	/* Flush rings and disable interrupts */
12615 	bnxt_shutdown_nic(bp, irq_re_init);
12616 
12617 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
12618 
12619 	bnxt_debug_dev_exit(bp);
12620 	bnxt_disable_napi(bp);
12621 	del_timer_sync(&bp->timer);
12622 	bnxt_free_skbs(bp);
12623 
12624 	/* Save ring stats before shutdown */
12625 	if (bp->bnapi && irq_re_init) {
12626 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
12627 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
12628 	}
12629 	if (irq_re_init) {
12630 		bnxt_free_irq(bp);
12631 		bnxt_del_napi(bp);
12632 	}
12633 	bnxt_free_mem(bp, irq_re_init);
12634 }
12635 
bnxt_close_nic(struct bnxt * bp,bool irq_re_init,bool link_re_init)12636 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12637 {
12638 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12639 		/* If we get here, it means firmware reset is in progress
12640 		 * while we are trying to close.  We can safely proceed with
12641 		 * the close because we are holding rtnl_lock().  Some firmware
12642 		 * messages may fail as we proceed to close.  We set the
12643 		 * ABORT_ERR flag here so that the FW reset thread will later
12644 		 * abort when it gets the rtnl_lock() and sees the flag.
12645 		 */
12646 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
12647 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12648 	}
12649 
12650 #ifdef CONFIG_BNXT_SRIOV
12651 	if (bp->sriov_cfg) {
12652 		int rc;
12653 
12654 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
12655 						      !bp->sriov_cfg,
12656 						      BNXT_SRIOV_CFG_WAIT_TMO);
12657 		if (!rc)
12658 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
12659 		else if (rc < 0)
12660 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
12661 	}
12662 #endif
12663 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
12664 }
12665 
bnxt_close(struct net_device * dev)12666 static int bnxt_close(struct net_device *dev)
12667 {
12668 	struct bnxt *bp = netdev_priv(dev);
12669 
12670 	bnxt_close_nic(bp, true, true);
12671 	bnxt_hwrm_shutdown_link(bp);
12672 	bnxt_hwrm_if_change(bp, false);
12673 	return 0;
12674 }
12675 
bnxt_hwrm_port_phy_read(struct bnxt * bp,u16 phy_addr,u16 reg,u16 * val)12676 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
12677 				   u16 *val)
12678 {
12679 	struct hwrm_port_phy_mdio_read_output *resp;
12680 	struct hwrm_port_phy_mdio_read_input *req;
12681 	int rc;
12682 
12683 	if (bp->hwrm_spec_code < 0x10a00)
12684 		return -EOPNOTSUPP;
12685 
12686 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
12687 	if (rc)
12688 		return rc;
12689 
12690 	req->port_id = cpu_to_le16(bp->pf.port_id);
12691 	req->phy_addr = phy_addr;
12692 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12693 	if (mdio_phy_id_is_c45(phy_addr)) {
12694 		req->cl45_mdio = 1;
12695 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12696 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12697 		req->reg_addr = cpu_to_le16(reg);
12698 	}
12699 
12700 	resp = hwrm_req_hold(bp, req);
12701 	rc = hwrm_req_send(bp, req);
12702 	if (!rc)
12703 		*val = le16_to_cpu(resp->reg_data);
12704 	hwrm_req_drop(bp, req);
12705 	return rc;
12706 }
12707 
bnxt_hwrm_port_phy_write(struct bnxt * bp,u16 phy_addr,u16 reg,u16 val)12708 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
12709 				    u16 val)
12710 {
12711 	struct hwrm_port_phy_mdio_write_input *req;
12712 	int rc;
12713 
12714 	if (bp->hwrm_spec_code < 0x10a00)
12715 		return -EOPNOTSUPP;
12716 
12717 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
12718 	if (rc)
12719 		return rc;
12720 
12721 	req->port_id = cpu_to_le16(bp->pf.port_id);
12722 	req->phy_addr = phy_addr;
12723 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12724 	if (mdio_phy_id_is_c45(phy_addr)) {
12725 		req->cl45_mdio = 1;
12726 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12727 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12728 		req->reg_addr = cpu_to_le16(reg);
12729 	}
12730 	req->reg_data = cpu_to_le16(val);
12731 
12732 	return hwrm_req_send(bp, req);
12733 }
12734 
12735 /* rtnl_lock held */
bnxt_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)12736 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12737 {
12738 	struct mii_ioctl_data *mdio = if_mii(ifr);
12739 	struct bnxt *bp = netdev_priv(dev);
12740 	int rc;
12741 
12742 	switch (cmd) {
12743 	case SIOCGMIIPHY:
12744 		mdio->phy_id = bp->link_info.phy_addr;
12745 
12746 		fallthrough;
12747 	case SIOCGMIIREG: {
12748 		u16 mii_regval = 0;
12749 
12750 		if (!netif_running(dev))
12751 			return -EAGAIN;
12752 
12753 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
12754 					     &mii_regval);
12755 		mdio->val_out = mii_regval;
12756 		return rc;
12757 	}
12758 
12759 	case SIOCSMIIREG:
12760 		if (!netif_running(dev))
12761 			return -EAGAIN;
12762 
12763 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
12764 						mdio->val_in);
12765 
12766 	case SIOCSHWTSTAMP:
12767 		return bnxt_hwtstamp_set(dev, ifr);
12768 
12769 	case SIOCGHWTSTAMP:
12770 		return bnxt_hwtstamp_get(dev, ifr);
12771 
12772 	default:
12773 		/* do nothing */
12774 		break;
12775 	}
12776 	return -EOPNOTSUPP;
12777 }
12778 
bnxt_get_ring_stats(struct bnxt * bp,struct rtnl_link_stats64 * stats)12779 static void bnxt_get_ring_stats(struct bnxt *bp,
12780 				struct rtnl_link_stats64 *stats)
12781 {
12782 	int i;
12783 
12784 	for (i = 0; i < bp->cp_nr_rings; i++) {
12785 		struct bnxt_napi *bnapi = bp->bnapi[i];
12786 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
12787 		u64 *sw = cpr->stats.sw_stats;
12788 
12789 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
12790 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12791 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
12792 
12793 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
12794 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
12795 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
12796 
12797 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
12798 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
12799 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
12800 
12801 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
12802 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
12803 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
12804 
12805 		stats->rx_missed_errors +=
12806 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
12807 
12808 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12809 
12810 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
12811 
12812 		stats->rx_dropped +=
12813 			cpr->sw_stats->rx.rx_netpoll_discards +
12814 			cpr->sw_stats->rx.rx_oom_discards;
12815 	}
12816 }
12817 
bnxt_add_prev_stats(struct bnxt * bp,struct rtnl_link_stats64 * stats)12818 static void bnxt_add_prev_stats(struct bnxt *bp,
12819 				struct rtnl_link_stats64 *stats)
12820 {
12821 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
12822 
12823 	stats->rx_packets += prev_stats->rx_packets;
12824 	stats->tx_packets += prev_stats->tx_packets;
12825 	stats->rx_bytes += prev_stats->rx_bytes;
12826 	stats->tx_bytes += prev_stats->tx_bytes;
12827 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
12828 	stats->multicast += prev_stats->multicast;
12829 	stats->rx_dropped += prev_stats->rx_dropped;
12830 	stats->tx_dropped += prev_stats->tx_dropped;
12831 }
12832 
12833 static void
bnxt_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)12834 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
12835 {
12836 	struct bnxt *bp = netdev_priv(dev);
12837 
12838 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
12839 	/* Make sure bnxt_close_nic() sees that we are reading stats before
12840 	 * we check the BNXT_STATE_OPEN flag.
12841 	 */
12842 	smp_mb__after_atomic();
12843 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12844 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12845 		*stats = bp->net_stats_prev;
12846 		return;
12847 	}
12848 
12849 	bnxt_get_ring_stats(bp, stats);
12850 	bnxt_add_prev_stats(bp, stats);
12851 
12852 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
12853 		u64 *rx = bp->port_stats.sw_stats;
12854 		u64 *tx = bp->port_stats.sw_stats +
12855 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
12856 
12857 		stats->rx_crc_errors =
12858 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
12859 		stats->rx_frame_errors =
12860 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
12861 		stats->rx_length_errors =
12862 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
12863 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
12864 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
12865 		stats->rx_errors =
12866 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
12867 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
12868 		stats->collisions =
12869 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
12870 		stats->tx_fifo_errors =
12871 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
12872 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
12873 	}
12874 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12875 }
12876 
bnxt_get_one_ring_err_stats(struct bnxt * bp,struct bnxt_total_ring_err_stats * stats,struct bnxt_cp_ring_info * cpr)12877 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
12878 					struct bnxt_total_ring_err_stats *stats,
12879 					struct bnxt_cp_ring_info *cpr)
12880 {
12881 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
12882 	u64 *hw_stats = cpr->stats.sw_stats;
12883 
12884 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
12885 	stats->rx_total_resets += sw_stats->rx.rx_resets;
12886 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
12887 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
12888 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
12889 	stats->rx_total_ring_discards +=
12890 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
12891 	stats->tx_total_resets += sw_stats->tx.tx_resets;
12892 	stats->tx_total_ring_discards +=
12893 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
12894 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
12895 }
12896 
bnxt_get_ring_err_stats(struct bnxt * bp,struct bnxt_total_ring_err_stats * stats)12897 void bnxt_get_ring_err_stats(struct bnxt *bp,
12898 			     struct bnxt_total_ring_err_stats *stats)
12899 {
12900 	int i;
12901 
12902 	for (i = 0; i < bp->cp_nr_rings; i++)
12903 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
12904 }
12905 
bnxt_mc_list_updated(struct bnxt * bp,u32 * rx_mask)12906 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
12907 {
12908 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12909 	struct net_device *dev = bp->dev;
12910 	struct netdev_hw_addr *ha;
12911 	u8 *haddr;
12912 	int mc_count = 0;
12913 	bool update = false;
12914 	int off = 0;
12915 
12916 	netdev_for_each_mc_addr(ha, dev) {
12917 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
12918 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12919 			vnic->mc_list_count = 0;
12920 			return false;
12921 		}
12922 		haddr = ha->addr;
12923 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
12924 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
12925 			update = true;
12926 		}
12927 		off += ETH_ALEN;
12928 		mc_count++;
12929 	}
12930 	if (mc_count)
12931 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12932 
12933 	if (mc_count != vnic->mc_list_count) {
12934 		vnic->mc_list_count = mc_count;
12935 		update = true;
12936 	}
12937 	return update;
12938 }
12939 
bnxt_uc_list_updated(struct bnxt * bp)12940 static bool bnxt_uc_list_updated(struct bnxt *bp)
12941 {
12942 	struct net_device *dev = bp->dev;
12943 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12944 	struct netdev_hw_addr *ha;
12945 	int off = 0;
12946 
12947 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
12948 		return true;
12949 
12950 	netdev_for_each_uc_addr(ha, dev) {
12951 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
12952 			return true;
12953 
12954 		off += ETH_ALEN;
12955 	}
12956 	return false;
12957 }
12958 
bnxt_set_rx_mode(struct net_device * dev)12959 static void bnxt_set_rx_mode(struct net_device *dev)
12960 {
12961 	struct bnxt *bp = netdev_priv(dev);
12962 	struct bnxt_vnic_info *vnic;
12963 	bool mc_update = false;
12964 	bool uc_update;
12965 	u32 mask;
12966 
12967 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
12968 		return;
12969 
12970 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12971 	mask = vnic->rx_mask;
12972 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
12973 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
12974 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
12975 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
12976 
12977 	if (dev->flags & IFF_PROMISC)
12978 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
12979 
12980 	uc_update = bnxt_uc_list_updated(bp);
12981 
12982 	if (dev->flags & IFF_BROADCAST)
12983 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
12984 	if (dev->flags & IFF_ALLMULTI) {
12985 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12986 		vnic->mc_list_count = 0;
12987 	} else if (dev->flags & IFF_MULTICAST) {
12988 		mc_update = bnxt_mc_list_updated(bp, &mask);
12989 	}
12990 
12991 	if (mask != vnic->rx_mask || uc_update || mc_update) {
12992 		vnic->rx_mask = mask;
12993 
12994 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
12995 	}
12996 }
12997 
bnxt_cfg_rx_mode(struct bnxt * bp)12998 static int bnxt_cfg_rx_mode(struct bnxt *bp)
12999 {
13000 	struct net_device *dev = bp->dev;
13001 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13002 	struct netdev_hw_addr *ha;
13003 	int i, off = 0, rc;
13004 	bool uc_update;
13005 
13006 	netif_addr_lock_bh(dev);
13007 	uc_update = bnxt_uc_list_updated(bp);
13008 	netif_addr_unlock_bh(dev);
13009 
13010 	if (!uc_update)
13011 		goto skip_uc;
13012 
13013 	for (i = 1; i < vnic->uc_filter_count; i++) {
13014 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13015 
13016 		bnxt_hwrm_l2_filter_free(bp, fltr);
13017 		bnxt_del_l2_filter(bp, fltr);
13018 	}
13019 
13020 	vnic->uc_filter_count = 1;
13021 
13022 	netif_addr_lock_bh(dev);
13023 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13024 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13025 	} else {
13026 		netdev_for_each_uc_addr(ha, dev) {
13027 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13028 			off += ETH_ALEN;
13029 			vnic->uc_filter_count++;
13030 		}
13031 	}
13032 	netif_addr_unlock_bh(dev);
13033 
13034 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13035 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13036 		if (rc) {
13037 			if (BNXT_VF(bp) && rc == -ENODEV) {
13038 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13039 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13040 				else
13041 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13042 				rc = 0;
13043 			} else {
13044 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13045 			}
13046 			vnic->uc_filter_count = i;
13047 			return rc;
13048 		}
13049 	}
13050 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13051 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13052 
13053 skip_uc:
13054 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13055 	    !bnxt_promisc_ok(bp))
13056 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13057 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13058 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13059 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13060 			    rc);
13061 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13062 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13063 		vnic->mc_list_count = 0;
13064 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13065 	}
13066 	if (rc)
13067 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13068 			   rc);
13069 
13070 	return rc;
13071 }
13072 
bnxt_can_reserve_rings(struct bnxt * bp)13073 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13074 {
13075 #ifdef CONFIG_BNXT_SRIOV
13076 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13077 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13078 
13079 		/* No minimum rings were provisioned by the PF.  Don't
13080 		 * reserve rings by default when device is down.
13081 		 */
13082 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13083 			return true;
13084 
13085 		if (!netif_running(bp->dev))
13086 			return false;
13087 	}
13088 #endif
13089 	return true;
13090 }
13091 
13092 /* If the chip and firmware supports RFS */
bnxt_rfs_supported(struct bnxt * bp)13093 static bool bnxt_rfs_supported(struct bnxt *bp)
13094 {
13095 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13096 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13097 			return true;
13098 		return false;
13099 	}
13100 	/* 212 firmware is broken for aRFS */
13101 	if (BNXT_FW_MAJ(bp) == 212)
13102 		return false;
13103 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13104 		return true;
13105 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13106 		return true;
13107 	return false;
13108 }
13109 
13110 /* If runtime conditions support RFS */
bnxt_rfs_capable(struct bnxt * bp,bool new_rss_ctx)13111 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13112 {
13113 	struct bnxt_hw_rings hwr = {0};
13114 	int max_vnics, max_rss_ctxs;
13115 
13116 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13117 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13118 		return bnxt_rfs_supported(bp);
13119 
13120 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13121 		return false;
13122 
13123 	hwr.grp = bp->rx_nr_rings;
13124 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13125 	if (new_rss_ctx)
13126 		hwr.vnic++;
13127 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13128 	max_vnics = bnxt_get_max_func_vnics(bp);
13129 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13130 
13131 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13132 		if (bp->rx_nr_rings > 1)
13133 			netdev_warn(bp->dev,
13134 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13135 				    min(max_rss_ctxs - 1, max_vnics - 1));
13136 		return false;
13137 	}
13138 
13139 	if (!BNXT_NEW_RM(bp))
13140 		return true;
13141 
13142 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13143 	 * issue that will mess up the default VNIC if we reduce the
13144 	 * reservations.
13145 	 */
13146 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13147 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13148 		return true;
13149 
13150 	bnxt_hwrm_reserve_rings(bp, &hwr);
13151 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13152 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13153 		return true;
13154 
13155 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13156 	hwr.vnic = 1;
13157 	hwr.rss_ctx = 0;
13158 	bnxt_hwrm_reserve_rings(bp, &hwr);
13159 	return false;
13160 }
13161 
bnxt_fix_features(struct net_device * dev,netdev_features_t features)13162 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13163 					   netdev_features_t features)
13164 {
13165 	struct bnxt *bp = netdev_priv(dev);
13166 	netdev_features_t vlan_features;
13167 
13168 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13169 		features &= ~NETIF_F_NTUPLE;
13170 
13171 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13172 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13173 
13174 	if (!(features & NETIF_F_GRO))
13175 		features &= ~NETIF_F_GRO_HW;
13176 
13177 	if (features & NETIF_F_GRO_HW)
13178 		features &= ~NETIF_F_LRO;
13179 
13180 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13181 	 * turned on or off together.
13182 	 */
13183 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13184 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13185 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13186 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13187 		else if (vlan_features)
13188 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13189 	}
13190 #ifdef CONFIG_BNXT_SRIOV
13191 	if (BNXT_VF(bp) && bp->vf.vlan)
13192 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13193 #endif
13194 	return features;
13195 }
13196 
bnxt_reinit_features(struct bnxt * bp,bool irq_re_init,bool link_re_init,u32 flags,bool update_tpa)13197 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13198 				bool link_re_init, u32 flags, bool update_tpa)
13199 {
13200 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13201 	bp->flags = flags;
13202 	if (update_tpa)
13203 		bnxt_set_ring_params(bp);
13204 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13205 }
13206 
bnxt_set_features(struct net_device * dev,netdev_features_t features)13207 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13208 {
13209 	bool update_tpa = false, update_ntuple = false;
13210 	struct bnxt *bp = netdev_priv(dev);
13211 	u32 flags = bp->flags;
13212 	u32 changes;
13213 	int rc = 0;
13214 	bool re_init = false;
13215 
13216 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13217 	if (features & NETIF_F_GRO_HW)
13218 		flags |= BNXT_FLAG_GRO;
13219 	else if (features & NETIF_F_LRO)
13220 		flags |= BNXT_FLAG_LRO;
13221 
13222 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13223 		flags &= ~BNXT_FLAG_TPA;
13224 
13225 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13226 		flags |= BNXT_FLAG_STRIP_VLAN;
13227 
13228 	if (features & NETIF_F_NTUPLE)
13229 		flags |= BNXT_FLAG_RFS;
13230 	else
13231 		bnxt_clear_usr_fltrs(bp, true);
13232 
13233 	changes = flags ^ bp->flags;
13234 	if (changes & BNXT_FLAG_TPA) {
13235 		update_tpa = true;
13236 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13237 		    (flags & BNXT_FLAG_TPA) == 0 ||
13238 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13239 			re_init = true;
13240 	}
13241 
13242 	if (changes & ~BNXT_FLAG_TPA)
13243 		re_init = true;
13244 
13245 	if (changes & BNXT_FLAG_RFS)
13246 		update_ntuple = true;
13247 
13248 	if (flags != bp->flags) {
13249 		u32 old_flags = bp->flags;
13250 
13251 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13252 			bp->flags = flags;
13253 			if (update_tpa)
13254 				bnxt_set_ring_params(bp);
13255 			return rc;
13256 		}
13257 
13258 		if (update_ntuple)
13259 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13260 
13261 		if (re_init)
13262 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13263 
13264 		if (update_tpa) {
13265 			bp->flags = flags;
13266 			rc = bnxt_set_tpa(bp,
13267 					  (flags & BNXT_FLAG_TPA) ?
13268 					  true : false);
13269 			if (rc)
13270 				bp->flags = old_flags;
13271 		}
13272 	}
13273 	return rc;
13274 }
13275 
bnxt_exthdr_check(struct bnxt * bp,struct sk_buff * skb,int nw_off,u8 ** nextp)13276 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13277 			      u8 **nextp)
13278 {
13279 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13280 	struct hop_jumbo_hdr *jhdr;
13281 	int hdr_count = 0;
13282 	u8 *nexthdr;
13283 	int start;
13284 
13285 	/* Check that there are at most 2 IPv6 extension headers, no
13286 	 * fragment header, and each is <= 64 bytes.
13287 	 */
13288 	start = nw_off + sizeof(*ip6h);
13289 	nexthdr = &ip6h->nexthdr;
13290 	while (ipv6_ext_hdr(*nexthdr)) {
13291 		struct ipv6_opt_hdr *hp;
13292 		int hdrlen;
13293 
13294 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13295 		    *nexthdr == NEXTHDR_FRAGMENT)
13296 			return false;
13297 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13298 					  skb_headlen(skb), NULL);
13299 		if (!hp)
13300 			return false;
13301 		if (*nexthdr == NEXTHDR_AUTH)
13302 			hdrlen = ipv6_authlen(hp);
13303 		else
13304 			hdrlen = ipv6_optlen(hp);
13305 
13306 		if (hdrlen > 64)
13307 			return false;
13308 
13309 		/* The ext header may be a hop-by-hop header inserted for
13310 		 * big TCP purposes. This will be removed before sending
13311 		 * from NIC, so do not count it.
13312 		 */
13313 		if (*nexthdr == NEXTHDR_HOP) {
13314 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13315 				goto increment_hdr;
13316 
13317 			jhdr = (struct hop_jumbo_hdr *)hp;
13318 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13319 			    jhdr->nexthdr != IPPROTO_TCP)
13320 				goto increment_hdr;
13321 
13322 			goto next_hdr;
13323 		}
13324 increment_hdr:
13325 		hdr_count++;
13326 next_hdr:
13327 		nexthdr = &hp->nexthdr;
13328 		start += hdrlen;
13329 	}
13330 	if (nextp) {
13331 		/* Caller will check inner protocol */
13332 		if (skb->encapsulation) {
13333 			*nextp = nexthdr;
13334 			return true;
13335 		}
13336 		*nextp = NULL;
13337 	}
13338 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13339 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13340 }
13341 
13342 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
bnxt_udp_tunl_check(struct bnxt * bp,struct sk_buff * skb)13343 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13344 {
13345 	struct udphdr *uh = udp_hdr(skb);
13346 	__be16 udp_port = uh->dest;
13347 
13348 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13349 	    udp_port != bp->vxlan_gpe_port)
13350 		return false;
13351 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13352 		struct ethhdr *eh = inner_eth_hdr(skb);
13353 
13354 		switch (eh->h_proto) {
13355 		case htons(ETH_P_IP):
13356 			return true;
13357 		case htons(ETH_P_IPV6):
13358 			return bnxt_exthdr_check(bp, skb,
13359 						 skb_inner_network_offset(skb),
13360 						 NULL);
13361 		}
13362 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13363 		return true;
13364 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13365 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13366 					 NULL);
13367 	}
13368 	return false;
13369 }
13370 
bnxt_tunl_check(struct bnxt * bp,struct sk_buff * skb,u8 l4_proto)13371 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13372 {
13373 	switch (l4_proto) {
13374 	case IPPROTO_UDP:
13375 		return bnxt_udp_tunl_check(bp, skb);
13376 	case IPPROTO_IPIP:
13377 		return true;
13378 	case IPPROTO_GRE: {
13379 		switch (skb->inner_protocol) {
13380 		default:
13381 			return false;
13382 		case htons(ETH_P_IP):
13383 			return true;
13384 		case htons(ETH_P_IPV6):
13385 			fallthrough;
13386 		}
13387 	}
13388 	case IPPROTO_IPV6:
13389 		/* Check ext headers of inner ipv6 */
13390 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13391 					 NULL);
13392 	}
13393 	return false;
13394 }
13395 
bnxt_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)13396 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13397 					     struct net_device *dev,
13398 					     netdev_features_t features)
13399 {
13400 	struct bnxt *bp = netdev_priv(dev);
13401 	u8 *l4_proto;
13402 
13403 	features = vlan_features_check(skb, features);
13404 	switch (vlan_get_protocol(skb)) {
13405 	case htons(ETH_P_IP):
13406 		if (!skb->encapsulation)
13407 			return features;
13408 		l4_proto = &ip_hdr(skb)->protocol;
13409 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13410 			return features;
13411 		break;
13412 	case htons(ETH_P_IPV6):
13413 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13414 				       &l4_proto))
13415 			break;
13416 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13417 			return features;
13418 		break;
13419 	}
13420 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13421 }
13422 
bnxt_dbg_hwrm_rd_reg(struct bnxt * bp,u32 reg_off,u16 num_words,u32 * reg_buf)13423 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13424 			 u32 *reg_buf)
13425 {
13426 	struct hwrm_dbg_read_direct_output *resp;
13427 	struct hwrm_dbg_read_direct_input *req;
13428 	__le32 *dbg_reg_buf;
13429 	dma_addr_t mapping;
13430 	int rc, i;
13431 
13432 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13433 	if (rc)
13434 		return rc;
13435 
13436 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13437 					 &mapping);
13438 	if (!dbg_reg_buf) {
13439 		rc = -ENOMEM;
13440 		goto dbg_rd_reg_exit;
13441 	}
13442 
13443 	req->host_dest_addr = cpu_to_le64(mapping);
13444 
13445 	resp = hwrm_req_hold(bp, req);
13446 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13447 	req->read_len32 = cpu_to_le32(num_words);
13448 
13449 	rc = hwrm_req_send(bp, req);
13450 	if (rc || resp->error_code) {
13451 		rc = -EIO;
13452 		goto dbg_rd_reg_exit;
13453 	}
13454 	for (i = 0; i < num_words; i++)
13455 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13456 
13457 dbg_rd_reg_exit:
13458 	hwrm_req_drop(bp, req);
13459 	return rc;
13460 }
13461 
bnxt_dbg_hwrm_ring_info_get(struct bnxt * bp,u8 ring_type,u32 ring_id,u32 * prod,u32 * cons)13462 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13463 				       u32 ring_id, u32 *prod, u32 *cons)
13464 {
13465 	struct hwrm_dbg_ring_info_get_output *resp;
13466 	struct hwrm_dbg_ring_info_get_input *req;
13467 	int rc;
13468 
13469 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13470 	if (rc)
13471 		return rc;
13472 
13473 	req->ring_type = ring_type;
13474 	req->fw_ring_id = cpu_to_le32(ring_id);
13475 	resp = hwrm_req_hold(bp, req);
13476 	rc = hwrm_req_send(bp, req);
13477 	if (!rc) {
13478 		*prod = le32_to_cpu(resp->producer_index);
13479 		*cons = le32_to_cpu(resp->consumer_index);
13480 	}
13481 	hwrm_req_drop(bp, req);
13482 	return rc;
13483 }
13484 
bnxt_dump_tx_sw_state(struct bnxt_napi * bnapi)13485 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13486 {
13487 	struct bnxt_tx_ring_info *txr;
13488 	int i = bnapi->index, j;
13489 
13490 	bnxt_for_each_napi_tx(j, bnapi, txr)
13491 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13492 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13493 			    txr->tx_cons);
13494 }
13495 
bnxt_dump_rx_sw_state(struct bnxt_napi * bnapi)13496 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13497 {
13498 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13499 	int i = bnapi->index;
13500 
13501 	if (!rxr)
13502 		return;
13503 
13504 	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",
13505 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13506 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13507 		    rxr->rx_sw_agg_prod);
13508 }
13509 
bnxt_dump_cp_sw_state(struct bnxt_napi * bnapi)13510 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13511 {
13512 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13513 	int i = bnapi->index;
13514 
13515 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13516 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13517 }
13518 
bnxt_dbg_dump_states(struct bnxt * bp)13519 static void bnxt_dbg_dump_states(struct bnxt *bp)
13520 {
13521 	int i;
13522 	struct bnxt_napi *bnapi;
13523 
13524 	for (i = 0; i < bp->cp_nr_rings; i++) {
13525 		bnapi = bp->bnapi[i];
13526 		if (netif_msg_drv(bp)) {
13527 			bnxt_dump_tx_sw_state(bnapi);
13528 			bnxt_dump_rx_sw_state(bnapi);
13529 			bnxt_dump_cp_sw_state(bnapi);
13530 		}
13531 	}
13532 }
13533 
bnxt_hwrm_rx_ring_reset(struct bnxt * bp,int ring_nr)13534 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13535 {
13536 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13537 	struct hwrm_ring_reset_input *req;
13538 	struct bnxt_napi *bnapi = rxr->bnapi;
13539 	struct bnxt_cp_ring_info *cpr;
13540 	u16 cp_ring_id;
13541 	int rc;
13542 
13543 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13544 	if (rc)
13545 		return rc;
13546 
13547 	cpr = &bnapi->cp_ring;
13548 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
13549 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
13550 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
13551 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
13552 	return hwrm_req_send_silent(bp, req);
13553 }
13554 
bnxt_reset_task(struct bnxt * bp,bool silent)13555 static void bnxt_reset_task(struct bnxt *bp, bool silent)
13556 {
13557 	if (!silent)
13558 		bnxt_dbg_dump_states(bp);
13559 	if (netif_running(bp->dev)) {
13560 		bnxt_close_nic(bp, !silent, false);
13561 		bnxt_open_nic(bp, !silent, false);
13562 	}
13563 }
13564 
bnxt_tx_timeout(struct net_device * dev,unsigned int txqueue)13565 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
13566 {
13567 	struct bnxt *bp = netdev_priv(dev);
13568 
13569 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
13570 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
13571 }
13572 
bnxt_fw_health_check(struct bnxt * bp)13573 static void bnxt_fw_health_check(struct bnxt *bp)
13574 {
13575 	struct bnxt_fw_health *fw_health = bp->fw_health;
13576 	struct pci_dev *pdev = bp->pdev;
13577 	u32 val;
13578 
13579 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13580 		return;
13581 
13582 	/* Make sure it is enabled before checking the tmr_counter. */
13583 	smp_rmb();
13584 	if (fw_health->tmr_counter) {
13585 		fw_health->tmr_counter--;
13586 		return;
13587 	}
13588 
13589 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13590 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
13591 		fw_health->arrests++;
13592 		goto fw_reset;
13593 	}
13594 
13595 	fw_health->last_fw_heartbeat = val;
13596 
13597 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13598 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
13599 		fw_health->discoveries++;
13600 		goto fw_reset;
13601 	}
13602 
13603 	fw_health->tmr_counter = fw_health->tmr_multiplier;
13604 	return;
13605 
13606 fw_reset:
13607 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
13608 }
13609 
bnxt_timer(struct timer_list * t)13610 static void bnxt_timer(struct timer_list *t)
13611 {
13612 	struct bnxt *bp = from_timer(bp, t, timer);
13613 	struct net_device *dev = bp->dev;
13614 
13615 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
13616 		return;
13617 
13618 	if (atomic_read(&bp->intr_sem) != 0)
13619 		goto bnxt_restart_timer;
13620 
13621 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
13622 		bnxt_fw_health_check(bp);
13623 
13624 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
13625 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
13626 
13627 	if (bnxt_tc_flower_enabled(bp))
13628 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
13629 
13630 #ifdef CONFIG_RFS_ACCEL
13631 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
13632 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
13633 #endif /*CONFIG_RFS_ACCEL*/
13634 
13635 	if (bp->link_info.phy_retry) {
13636 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
13637 			bp->link_info.phy_retry = false;
13638 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
13639 		} else {
13640 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
13641 		}
13642 	}
13643 
13644 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13645 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13646 
13647 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
13648 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
13649 
13650 bnxt_restart_timer:
13651 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13652 }
13653 
bnxt_rtnl_lock_sp(struct bnxt * bp)13654 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
13655 {
13656 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
13657 	 * set.  If the device is being closed, bnxt_close() may be holding
13658 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
13659 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
13660 	 */
13661 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13662 	rtnl_lock();
13663 }
13664 
bnxt_rtnl_unlock_sp(struct bnxt * bp)13665 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
13666 {
13667 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13668 	rtnl_unlock();
13669 }
13670 
13671 /* Only called from bnxt_sp_task() */
bnxt_reset(struct bnxt * bp,bool silent)13672 static void bnxt_reset(struct bnxt *bp, bool silent)
13673 {
13674 	bnxt_rtnl_lock_sp(bp);
13675 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
13676 		bnxt_reset_task(bp, silent);
13677 	bnxt_rtnl_unlock_sp(bp);
13678 }
13679 
13680 /* Only called from bnxt_sp_task() */
bnxt_rx_ring_reset(struct bnxt * bp)13681 static void bnxt_rx_ring_reset(struct bnxt *bp)
13682 {
13683 	int i;
13684 
13685 	bnxt_rtnl_lock_sp(bp);
13686 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13687 		bnxt_rtnl_unlock_sp(bp);
13688 		return;
13689 	}
13690 	/* Disable and flush TPA before resetting the RX ring */
13691 	if (bp->flags & BNXT_FLAG_TPA)
13692 		bnxt_set_tpa(bp, false);
13693 	for (i = 0; i < bp->rx_nr_rings; i++) {
13694 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
13695 		struct bnxt_cp_ring_info *cpr;
13696 		int rc;
13697 
13698 		if (!rxr->bnapi->in_reset)
13699 			continue;
13700 
13701 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
13702 		if (rc) {
13703 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
13704 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
13705 			else
13706 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
13707 					    rc);
13708 			bnxt_reset_task(bp, true);
13709 			break;
13710 		}
13711 		bnxt_free_one_rx_ring_skbs(bp, rxr);
13712 		rxr->rx_prod = 0;
13713 		rxr->rx_agg_prod = 0;
13714 		rxr->rx_sw_agg_prod = 0;
13715 		rxr->rx_next_cons = 0;
13716 		rxr->bnapi->in_reset = false;
13717 		bnxt_alloc_one_rx_ring(bp, i);
13718 		cpr = &rxr->bnapi->cp_ring;
13719 		cpr->sw_stats->rx.rx_resets++;
13720 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
13721 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
13722 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
13723 	}
13724 	if (bp->flags & BNXT_FLAG_TPA)
13725 		bnxt_set_tpa(bp, true);
13726 	bnxt_rtnl_unlock_sp(bp);
13727 }
13728 
bnxt_fw_fatal_close(struct bnxt * bp)13729 static void bnxt_fw_fatal_close(struct bnxt *bp)
13730 {
13731 	bnxt_tx_disable(bp);
13732 	bnxt_disable_napi(bp);
13733 	bnxt_disable_int_sync(bp);
13734 	bnxt_free_irq(bp);
13735 	bnxt_clear_int_mode(bp);
13736 	pci_disable_device(bp->pdev);
13737 }
13738 
bnxt_fw_reset_close(struct bnxt * bp)13739 static void bnxt_fw_reset_close(struct bnxt *bp)
13740 {
13741 	/* When firmware is in fatal state, quiesce device and disable
13742 	 * bus master to prevent any potential bad DMAs before freeing
13743 	 * kernel memory.
13744 	 */
13745 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
13746 		u16 val = 0;
13747 
13748 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
13749 		if (val == 0xffff)
13750 			bp->fw_reset_min_dsecs = 0;
13751 		bnxt_fw_fatal_close(bp);
13752 	}
13753 	__bnxt_close_nic(bp, true, false);
13754 	bnxt_vf_reps_free(bp);
13755 	bnxt_clear_int_mode(bp);
13756 	bnxt_hwrm_func_drv_unrgtr(bp);
13757 	if (pci_is_enabled(bp->pdev))
13758 		pci_disable_device(bp->pdev);
13759 	bnxt_free_ctx_mem(bp, false);
13760 }
13761 
is_bnxt_fw_ok(struct bnxt * bp)13762 static bool is_bnxt_fw_ok(struct bnxt *bp)
13763 {
13764 	struct bnxt_fw_health *fw_health = bp->fw_health;
13765 	bool no_heartbeat = false, has_reset = false;
13766 	u32 val;
13767 
13768 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13769 	if (val == fw_health->last_fw_heartbeat)
13770 		no_heartbeat = true;
13771 
13772 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13773 	if (val != fw_health->last_fw_reset_cnt)
13774 		has_reset = true;
13775 
13776 	if (!no_heartbeat && has_reset)
13777 		return true;
13778 
13779 	return false;
13780 }
13781 
13782 /* rtnl_lock is acquired before calling this function */
bnxt_force_fw_reset(struct bnxt * bp)13783 static void bnxt_force_fw_reset(struct bnxt *bp)
13784 {
13785 	struct bnxt_fw_health *fw_health = bp->fw_health;
13786 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13787 	u32 wait_dsecs;
13788 
13789 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
13790 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13791 		return;
13792 
13793 	/* we have to serialize with bnxt_refclk_read()*/
13794 	if (ptp) {
13795 		unsigned long flags;
13796 
13797 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
13798 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13799 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
13800 	} else {
13801 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13802 	}
13803 	bnxt_fw_reset_close(bp);
13804 	wait_dsecs = fw_health->master_func_wait_dsecs;
13805 	if (fw_health->primary) {
13806 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
13807 			wait_dsecs = 0;
13808 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
13809 	} else {
13810 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
13811 		wait_dsecs = fw_health->normal_func_wait_dsecs;
13812 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13813 	}
13814 
13815 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
13816 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
13817 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
13818 }
13819 
bnxt_fw_exception(struct bnxt * bp)13820 void bnxt_fw_exception(struct bnxt *bp)
13821 {
13822 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
13823 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
13824 	bnxt_ulp_stop(bp);
13825 	bnxt_rtnl_lock_sp(bp);
13826 	bnxt_force_fw_reset(bp);
13827 	bnxt_rtnl_unlock_sp(bp);
13828 }
13829 
13830 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
13831  * < 0 on error.
13832  */
bnxt_get_registered_vfs(struct bnxt * bp)13833 static int bnxt_get_registered_vfs(struct bnxt *bp)
13834 {
13835 #ifdef CONFIG_BNXT_SRIOV
13836 	int rc;
13837 
13838 	if (!BNXT_PF(bp))
13839 		return 0;
13840 
13841 	rc = bnxt_hwrm_func_qcfg(bp);
13842 	if (rc) {
13843 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
13844 		return rc;
13845 	}
13846 	if (bp->pf.registered_vfs)
13847 		return bp->pf.registered_vfs;
13848 	if (bp->sriov_cfg)
13849 		return 1;
13850 #endif
13851 	return 0;
13852 }
13853 
bnxt_fw_reset(struct bnxt * bp)13854 void bnxt_fw_reset(struct bnxt *bp)
13855 {
13856 	bnxt_ulp_stop(bp);
13857 	bnxt_rtnl_lock_sp(bp);
13858 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
13859 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13860 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13861 		int n = 0, tmo;
13862 
13863 		/* we have to serialize with bnxt_refclk_read()*/
13864 		if (ptp) {
13865 			unsigned long flags;
13866 
13867 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
13868 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13869 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
13870 		} else {
13871 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13872 		}
13873 		if (bp->pf.active_vfs &&
13874 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
13875 			n = bnxt_get_registered_vfs(bp);
13876 		if (n < 0) {
13877 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
13878 				   n);
13879 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13880 			dev_close(bp->dev);
13881 			goto fw_reset_exit;
13882 		} else if (n > 0) {
13883 			u16 vf_tmo_dsecs = n * 10;
13884 
13885 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
13886 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
13887 			bp->fw_reset_state =
13888 				BNXT_FW_RESET_STATE_POLL_VF;
13889 			bnxt_queue_fw_reset_work(bp, HZ / 10);
13890 			goto fw_reset_exit;
13891 		}
13892 		bnxt_fw_reset_close(bp);
13893 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13894 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
13895 			tmo = HZ / 10;
13896 		} else {
13897 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13898 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
13899 		}
13900 		bnxt_queue_fw_reset_work(bp, tmo);
13901 	}
13902 fw_reset_exit:
13903 	bnxt_rtnl_unlock_sp(bp);
13904 }
13905 
bnxt_chk_missed_irq(struct bnxt * bp)13906 static void bnxt_chk_missed_irq(struct bnxt *bp)
13907 {
13908 	int i;
13909 
13910 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13911 		return;
13912 
13913 	for (i = 0; i < bp->cp_nr_rings; i++) {
13914 		struct bnxt_napi *bnapi = bp->bnapi[i];
13915 		struct bnxt_cp_ring_info *cpr;
13916 		u32 fw_ring_id;
13917 		int j;
13918 
13919 		if (!bnapi)
13920 			continue;
13921 
13922 		cpr = &bnapi->cp_ring;
13923 		for (j = 0; j < cpr->cp_ring_count; j++) {
13924 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
13925 			u32 val[2];
13926 
13927 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
13928 				continue;
13929 
13930 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
13931 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
13932 				continue;
13933 			}
13934 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
13935 			bnxt_dbg_hwrm_ring_info_get(bp,
13936 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
13937 				fw_ring_id, &val[0], &val[1]);
13938 			cpr->sw_stats->cmn.missed_irqs++;
13939 		}
13940 	}
13941 }
13942 
13943 static void bnxt_cfg_ntp_filters(struct bnxt *);
13944 
bnxt_init_ethtool_link_settings(struct bnxt * bp)13945 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
13946 {
13947 	struct bnxt_link_info *link_info = &bp->link_info;
13948 
13949 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
13950 		link_info->autoneg = BNXT_AUTONEG_SPEED;
13951 		if (bp->hwrm_spec_code >= 0x10201) {
13952 			if (link_info->auto_pause_setting &
13953 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
13954 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13955 		} else {
13956 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13957 		}
13958 		bnxt_set_auto_speed(link_info);
13959 	} else {
13960 		bnxt_set_force_speed(link_info);
13961 		link_info->req_duplex = link_info->duplex_setting;
13962 	}
13963 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
13964 		link_info->req_flow_ctrl =
13965 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
13966 	else
13967 		link_info->req_flow_ctrl = link_info->force_pause_setting;
13968 }
13969 
bnxt_fw_echo_reply(struct bnxt * bp)13970 static void bnxt_fw_echo_reply(struct bnxt *bp)
13971 {
13972 	struct bnxt_fw_health *fw_health = bp->fw_health;
13973 	struct hwrm_func_echo_response_input *req;
13974 	int rc;
13975 
13976 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
13977 	if (rc)
13978 		return;
13979 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
13980 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
13981 	hwrm_req_send(bp, req);
13982 }
13983 
bnxt_ulp_restart(struct bnxt * bp)13984 static void bnxt_ulp_restart(struct bnxt *bp)
13985 {
13986 	bnxt_ulp_stop(bp);
13987 	bnxt_ulp_start(bp, 0);
13988 }
13989 
bnxt_sp_task(struct work_struct * work)13990 static void bnxt_sp_task(struct work_struct *work)
13991 {
13992 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
13993 
13994 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13995 	smp_mb__after_atomic();
13996 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13997 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13998 		return;
13999 	}
14000 
14001 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14002 		bnxt_ulp_restart(bp);
14003 		bnxt_reenable_sriov(bp);
14004 	}
14005 
14006 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14007 		bnxt_cfg_rx_mode(bp);
14008 
14009 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14010 		bnxt_cfg_ntp_filters(bp);
14011 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14012 		bnxt_hwrm_exec_fwd_req(bp);
14013 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14014 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14015 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14016 		bnxt_hwrm_port_qstats(bp, 0);
14017 		bnxt_hwrm_port_qstats_ext(bp, 0);
14018 		bnxt_accumulate_all_stats(bp);
14019 	}
14020 
14021 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14022 		int rc;
14023 
14024 		mutex_lock(&bp->link_lock);
14025 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14026 				       &bp->sp_event))
14027 			bnxt_hwrm_phy_qcaps(bp);
14028 
14029 		rc = bnxt_update_link(bp, true);
14030 		if (rc)
14031 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14032 				   rc);
14033 
14034 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14035 				       &bp->sp_event))
14036 			bnxt_init_ethtool_link_settings(bp);
14037 		mutex_unlock(&bp->link_lock);
14038 	}
14039 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14040 		int rc;
14041 
14042 		mutex_lock(&bp->link_lock);
14043 		rc = bnxt_update_phy_setting(bp);
14044 		mutex_unlock(&bp->link_lock);
14045 		if (rc) {
14046 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14047 		} else {
14048 			bp->link_info.phy_retry = false;
14049 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14050 		}
14051 	}
14052 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14053 		mutex_lock(&bp->link_lock);
14054 		bnxt_get_port_module_status(bp);
14055 		mutex_unlock(&bp->link_lock);
14056 	}
14057 
14058 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14059 		bnxt_tc_flow_stats_work(bp);
14060 
14061 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14062 		bnxt_chk_missed_irq(bp);
14063 
14064 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14065 		bnxt_fw_echo_reply(bp);
14066 
14067 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14068 		bnxt_hwmon_notify_event(bp);
14069 
14070 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14071 	 * must be the last functions to be called before exiting.
14072 	 */
14073 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14074 		bnxt_reset(bp, false);
14075 
14076 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14077 		bnxt_reset(bp, true);
14078 
14079 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14080 		bnxt_rx_ring_reset(bp);
14081 
14082 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14083 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14084 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14085 			bnxt_devlink_health_fw_report(bp);
14086 		else
14087 			bnxt_fw_reset(bp);
14088 	}
14089 
14090 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14091 		if (!is_bnxt_fw_ok(bp))
14092 			bnxt_devlink_health_fw_report(bp);
14093 	}
14094 
14095 	smp_mb__before_atomic();
14096 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14097 }
14098 
14099 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14100 				int *max_cp);
14101 
14102 /* Under rtnl_lock */
bnxt_check_rings(struct bnxt * bp,int tx,int rx,bool sh,int tcs,int tx_xdp)14103 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14104 		     int tx_xdp)
14105 {
14106 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14107 	struct bnxt_hw_rings hwr = {0};
14108 	int rx_rings = rx;
14109 	int rc;
14110 
14111 	if (tcs)
14112 		tx_sets = tcs;
14113 
14114 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14115 
14116 	if (max_rx < rx_rings)
14117 		return -ENOMEM;
14118 
14119 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14120 		rx_rings <<= 1;
14121 
14122 	hwr.rx = rx_rings;
14123 	hwr.tx = tx * tx_sets + tx_xdp;
14124 	if (max_tx < hwr.tx)
14125 		return -ENOMEM;
14126 
14127 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14128 
14129 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14130 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14131 	if (max_cp < hwr.cp)
14132 		return -ENOMEM;
14133 	hwr.stat = hwr.cp;
14134 	if (BNXT_NEW_RM(bp)) {
14135 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14136 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14137 		hwr.grp = rx;
14138 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14139 	}
14140 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14141 		hwr.cp_p5 = hwr.tx + rx;
14142 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14143 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14144 		if (!bnxt_ulp_registered(bp->edev)) {
14145 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14146 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14147 		}
14148 		if (hwr.cp > bp->total_irqs) {
14149 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14150 
14151 			if (total_msix < hwr.cp) {
14152 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14153 					    hwr.cp, total_msix);
14154 				rc = -ENOSPC;
14155 			}
14156 		}
14157 	}
14158 	return rc;
14159 }
14160 
bnxt_unmap_bars(struct bnxt * bp,struct pci_dev * pdev)14161 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14162 {
14163 	if (bp->bar2) {
14164 		pci_iounmap(pdev, bp->bar2);
14165 		bp->bar2 = NULL;
14166 	}
14167 
14168 	if (bp->bar1) {
14169 		pci_iounmap(pdev, bp->bar1);
14170 		bp->bar1 = NULL;
14171 	}
14172 
14173 	if (bp->bar0) {
14174 		pci_iounmap(pdev, bp->bar0);
14175 		bp->bar0 = NULL;
14176 	}
14177 }
14178 
bnxt_cleanup_pci(struct bnxt * bp)14179 static void bnxt_cleanup_pci(struct bnxt *bp)
14180 {
14181 	bnxt_unmap_bars(bp, bp->pdev);
14182 	pci_release_regions(bp->pdev);
14183 	if (pci_is_enabled(bp->pdev))
14184 		pci_disable_device(bp->pdev);
14185 }
14186 
bnxt_init_dflt_coal(struct bnxt * bp)14187 static void bnxt_init_dflt_coal(struct bnxt *bp)
14188 {
14189 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14190 	struct bnxt_coal *coal;
14191 	u16 flags = 0;
14192 
14193 	if (coal_cap->cmpl_params &
14194 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14195 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14196 
14197 	/* Tick values in micro seconds.
14198 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14199 	 */
14200 	coal = &bp->rx_coal;
14201 	coal->coal_ticks = 10;
14202 	coal->coal_bufs = 30;
14203 	coal->coal_ticks_irq = 1;
14204 	coal->coal_bufs_irq = 2;
14205 	coal->idle_thresh = 50;
14206 	coal->bufs_per_record = 2;
14207 	coal->budget = 64;		/* NAPI budget */
14208 	coal->flags = flags;
14209 
14210 	coal = &bp->tx_coal;
14211 	coal->coal_ticks = 28;
14212 	coal->coal_bufs = 30;
14213 	coal->coal_ticks_irq = 2;
14214 	coal->coal_bufs_irq = 2;
14215 	coal->bufs_per_record = 1;
14216 	coal->flags = flags;
14217 
14218 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14219 }
14220 
14221 /* FW that pre-reserves 1 VNIC per function */
bnxt_fw_pre_resv_vnics(struct bnxt * bp)14222 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14223 {
14224 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14225 
14226 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14227 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14228 		return true;
14229 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14230 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14231 		return true;
14232 	return false;
14233 }
14234 
bnxt_fw_init_one_p1(struct bnxt * bp)14235 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14236 {
14237 	int rc;
14238 
14239 	bp->fw_cap = 0;
14240 	rc = bnxt_hwrm_ver_get(bp);
14241 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14242 	 * so wait before continuing with recovery.
14243 	 */
14244 	if (rc)
14245 		msleep(100);
14246 	bnxt_try_map_fw_health_reg(bp);
14247 	if (rc) {
14248 		rc = bnxt_try_recover_fw(bp);
14249 		if (rc)
14250 			return rc;
14251 		rc = bnxt_hwrm_ver_get(bp);
14252 		if (rc)
14253 			return rc;
14254 	}
14255 
14256 	bnxt_nvm_cfg_ver_get(bp);
14257 
14258 	rc = bnxt_hwrm_func_reset(bp);
14259 	if (rc)
14260 		return -ENODEV;
14261 
14262 	bnxt_hwrm_fw_set_time(bp);
14263 	return 0;
14264 }
14265 
bnxt_fw_init_one_p2(struct bnxt * bp)14266 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14267 {
14268 	int rc;
14269 
14270 	/* Get the MAX capabilities for this function */
14271 	rc = bnxt_hwrm_func_qcaps(bp);
14272 	if (rc) {
14273 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14274 			   rc);
14275 		return -ENODEV;
14276 	}
14277 
14278 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14279 	if (rc)
14280 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14281 			    rc);
14282 
14283 	if (bnxt_alloc_fw_health(bp)) {
14284 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14285 	} else {
14286 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14287 		if (rc)
14288 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14289 				    rc);
14290 	}
14291 
14292 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14293 	if (rc)
14294 		return -ENODEV;
14295 
14296 	rc = bnxt_alloc_crash_dump_mem(bp);
14297 	if (rc)
14298 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14299 			    rc);
14300 	if (!rc) {
14301 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14302 		if (rc) {
14303 			bnxt_free_crash_dump_mem(bp);
14304 			netdev_warn(bp->dev,
14305 				    "hwrm crash dump mem failure rc: %d\n", rc);
14306 		}
14307 	}
14308 
14309 	if (bnxt_fw_pre_resv_vnics(bp))
14310 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14311 
14312 	bnxt_hwrm_func_qcfg(bp);
14313 	bnxt_hwrm_vnic_qcaps(bp);
14314 	bnxt_hwrm_port_led_qcaps(bp);
14315 	bnxt_ethtool_init(bp);
14316 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14317 		__bnxt_hwrm_ptp_qcfg(bp);
14318 	bnxt_dcb_init(bp);
14319 	bnxt_hwmon_init(bp);
14320 	return 0;
14321 }
14322 
bnxt_set_dflt_rss_hash_type(struct bnxt * bp)14323 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14324 {
14325 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14326 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14327 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14328 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14329 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14330 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14331 		bp->rss_hash_delta = bp->rss_hash_cfg;
14332 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14333 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14334 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14335 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14336 	}
14337 }
14338 
bnxt_set_dflt_rfs(struct bnxt * bp)14339 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14340 {
14341 	struct net_device *dev = bp->dev;
14342 
14343 	dev->hw_features &= ~NETIF_F_NTUPLE;
14344 	dev->features &= ~NETIF_F_NTUPLE;
14345 	bp->flags &= ~BNXT_FLAG_RFS;
14346 	if (bnxt_rfs_supported(bp)) {
14347 		dev->hw_features |= NETIF_F_NTUPLE;
14348 		if (bnxt_rfs_capable(bp, false)) {
14349 			bp->flags |= BNXT_FLAG_RFS;
14350 			dev->features |= NETIF_F_NTUPLE;
14351 		}
14352 	}
14353 }
14354 
bnxt_fw_init_one_p3(struct bnxt * bp)14355 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14356 {
14357 	struct pci_dev *pdev = bp->pdev;
14358 
14359 	bnxt_set_dflt_rss_hash_type(bp);
14360 	bnxt_set_dflt_rfs(bp);
14361 
14362 	bnxt_get_wol_settings(bp);
14363 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14364 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14365 	else
14366 		device_set_wakeup_capable(&pdev->dev, false);
14367 
14368 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14369 	bnxt_hwrm_coal_params_qcaps(bp);
14370 }
14371 
14372 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14373 
bnxt_fw_init_one(struct bnxt * bp)14374 int bnxt_fw_init_one(struct bnxt *bp)
14375 {
14376 	int rc;
14377 
14378 	rc = bnxt_fw_init_one_p1(bp);
14379 	if (rc) {
14380 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14381 		return rc;
14382 	}
14383 	rc = bnxt_fw_init_one_p2(bp);
14384 	if (rc) {
14385 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14386 		return rc;
14387 	}
14388 	rc = bnxt_probe_phy(bp, false);
14389 	if (rc)
14390 		return rc;
14391 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14392 	if (rc)
14393 		return rc;
14394 
14395 	bnxt_fw_init_one_p3(bp);
14396 	return 0;
14397 }
14398 
bnxt_fw_reset_writel(struct bnxt * bp,int reg_idx)14399 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14400 {
14401 	struct bnxt_fw_health *fw_health = bp->fw_health;
14402 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14403 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14404 	u32 reg_type, reg_off, delay_msecs;
14405 
14406 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14407 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14408 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14409 	switch (reg_type) {
14410 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14411 		pci_write_config_dword(bp->pdev, reg_off, val);
14412 		break;
14413 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14414 		writel(reg_off & BNXT_GRC_BASE_MASK,
14415 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14416 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14417 		fallthrough;
14418 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14419 		writel(val, bp->bar0 + reg_off);
14420 		break;
14421 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14422 		writel(val, bp->bar1 + reg_off);
14423 		break;
14424 	}
14425 	if (delay_msecs) {
14426 		pci_read_config_dword(bp->pdev, 0, &val);
14427 		msleep(delay_msecs);
14428 	}
14429 }
14430 
bnxt_hwrm_reset_permitted(struct bnxt * bp)14431 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14432 {
14433 	struct hwrm_func_qcfg_output *resp;
14434 	struct hwrm_func_qcfg_input *req;
14435 	bool result = true; /* firmware will enforce if unknown */
14436 
14437 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14438 		return result;
14439 
14440 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14441 		return result;
14442 
14443 	req->fid = cpu_to_le16(0xffff);
14444 	resp = hwrm_req_hold(bp, req);
14445 	if (!hwrm_req_send(bp, req))
14446 		result = !!(le16_to_cpu(resp->flags) &
14447 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14448 	hwrm_req_drop(bp, req);
14449 	return result;
14450 }
14451 
bnxt_reset_all(struct bnxt * bp)14452 static void bnxt_reset_all(struct bnxt *bp)
14453 {
14454 	struct bnxt_fw_health *fw_health = bp->fw_health;
14455 	int i, rc;
14456 
14457 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14458 		bnxt_fw_reset_via_optee(bp);
14459 		bp->fw_reset_timestamp = jiffies;
14460 		return;
14461 	}
14462 
14463 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14464 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14465 			bnxt_fw_reset_writel(bp, i);
14466 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14467 		struct hwrm_fw_reset_input *req;
14468 
14469 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14470 		if (!rc) {
14471 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14472 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14473 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14474 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14475 			rc = hwrm_req_send(bp, req);
14476 		}
14477 		if (rc != -ENODEV)
14478 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14479 	}
14480 	bp->fw_reset_timestamp = jiffies;
14481 }
14482 
bnxt_fw_reset_timeout(struct bnxt * bp)14483 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14484 {
14485 	return time_after(jiffies, bp->fw_reset_timestamp +
14486 			  (bp->fw_reset_max_dsecs * HZ / 10));
14487 }
14488 
bnxt_fw_reset_abort(struct bnxt * bp,int rc)14489 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14490 {
14491 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14492 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14493 		bnxt_dl_health_fw_status_update(bp, false);
14494 	bp->fw_reset_state = 0;
14495 	dev_close(bp->dev);
14496 }
14497 
bnxt_fw_reset_task(struct work_struct * work)14498 static void bnxt_fw_reset_task(struct work_struct *work)
14499 {
14500 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14501 	int rc = 0;
14502 
14503 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14504 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14505 		return;
14506 	}
14507 
14508 	switch (bp->fw_reset_state) {
14509 	case BNXT_FW_RESET_STATE_POLL_VF: {
14510 		int n = bnxt_get_registered_vfs(bp);
14511 		int tmo;
14512 
14513 		if (n < 0) {
14514 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14515 				   n, jiffies_to_msecs(jiffies -
14516 				   bp->fw_reset_timestamp));
14517 			goto fw_reset_abort;
14518 		} else if (n > 0) {
14519 			if (bnxt_fw_reset_timeout(bp)) {
14520 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14521 				bp->fw_reset_state = 0;
14522 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14523 					   n);
14524 				goto ulp_start;
14525 			}
14526 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14527 			return;
14528 		}
14529 		bp->fw_reset_timestamp = jiffies;
14530 		rtnl_lock();
14531 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14532 			bnxt_fw_reset_abort(bp, rc);
14533 			rtnl_unlock();
14534 			goto ulp_start;
14535 		}
14536 		bnxt_fw_reset_close(bp);
14537 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14538 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14539 			tmo = HZ / 10;
14540 		} else {
14541 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14542 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14543 		}
14544 		rtnl_unlock();
14545 		bnxt_queue_fw_reset_work(bp, tmo);
14546 		return;
14547 	}
14548 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
14549 		u32 val;
14550 
14551 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14552 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
14553 		    !bnxt_fw_reset_timeout(bp)) {
14554 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14555 			return;
14556 		}
14557 
14558 		if (!bp->fw_health->primary) {
14559 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
14560 
14561 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14562 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14563 			return;
14564 		}
14565 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14566 	}
14567 		fallthrough;
14568 	case BNXT_FW_RESET_STATE_RESET_FW:
14569 		bnxt_reset_all(bp);
14570 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14571 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
14572 		return;
14573 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
14574 		bnxt_inv_fw_health_reg(bp);
14575 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
14576 		    !bp->fw_reset_min_dsecs) {
14577 			u16 val;
14578 
14579 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14580 			if (val == 0xffff) {
14581 				if (bnxt_fw_reset_timeout(bp)) {
14582 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
14583 					rc = -ETIMEDOUT;
14584 					goto fw_reset_abort;
14585 				}
14586 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
14587 				return;
14588 			}
14589 		}
14590 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14591 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
14592 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
14593 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
14594 			bnxt_dl_remote_reload(bp);
14595 		if (pci_enable_device(bp->pdev)) {
14596 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
14597 			rc = -ENODEV;
14598 			goto fw_reset_abort;
14599 		}
14600 		pci_set_master(bp->pdev);
14601 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
14602 		fallthrough;
14603 	case BNXT_FW_RESET_STATE_POLL_FW:
14604 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
14605 		rc = bnxt_hwrm_poll(bp);
14606 		if (rc) {
14607 			if (bnxt_fw_reset_timeout(bp)) {
14608 				netdev_err(bp->dev, "Firmware reset aborted\n");
14609 				goto fw_reset_abort_status;
14610 			}
14611 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14612 			return;
14613 		}
14614 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
14615 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
14616 		fallthrough;
14617 	case BNXT_FW_RESET_STATE_OPENING:
14618 		while (!rtnl_trylock()) {
14619 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14620 			return;
14621 		}
14622 		rc = bnxt_open(bp->dev);
14623 		if (rc) {
14624 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
14625 			bnxt_fw_reset_abort(bp, rc);
14626 			rtnl_unlock();
14627 			goto ulp_start;
14628 		}
14629 
14630 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
14631 		    bp->fw_health->enabled) {
14632 			bp->fw_health->last_fw_reset_cnt =
14633 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14634 		}
14635 		bp->fw_reset_state = 0;
14636 		/* Make sure fw_reset_state is 0 before clearing the flag */
14637 		smp_mb__before_atomic();
14638 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14639 		bnxt_ptp_reapply_pps(bp);
14640 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
14641 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
14642 			bnxt_dl_health_fw_recovery_done(bp);
14643 			bnxt_dl_health_fw_status_update(bp, true);
14644 		}
14645 		rtnl_unlock();
14646 		bnxt_ulp_start(bp, 0);
14647 		bnxt_reenable_sriov(bp);
14648 		rtnl_lock();
14649 		bnxt_vf_reps_alloc(bp);
14650 		bnxt_vf_reps_open(bp);
14651 		rtnl_unlock();
14652 		break;
14653 	}
14654 	return;
14655 
14656 fw_reset_abort_status:
14657 	if (bp->fw_health->status_reliable ||
14658 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
14659 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14660 
14661 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
14662 	}
14663 fw_reset_abort:
14664 	rtnl_lock();
14665 	bnxt_fw_reset_abort(bp, rc);
14666 	rtnl_unlock();
14667 ulp_start:
14668 	bnxt_ulp_start(bp, rc);
14669 }
14670 
bnxt_init_board(struct pci_dev * pdev,struct net_device * dev)14671 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
14672 {
14673 	int rc;
14674 	struct bnxt *bp = netdev_priv(dev);
14675 
14676 	SET_NETDEV_DEV(dev, &pdev->dev);
14677 
14678 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
14679 	rc = pci_enable_device(pdev);
14680 	if (rc) {
14681 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
14682 		goto init_err;
14683 	}
14684 
14685 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
14686 		dev_err(&pdev->dev,
14687 			"Cannot find PCI device base address, aborting\n");
14688 		rc = -ENODEV;
14689 		goto init_err_disable;
14690 	}
14691 
14692 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
14693 	if (rc) {
14694 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
14695 		goto init_err_disable;
14696 	}
14697 
14698 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
14699 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
14700 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
14701 		rc = -EIO;
14702 		goto init_err_release;
14703 	}
14704 
14705 	pci_set_master(pdev);
14706 
14707 	bp->dev = dev;
14708 	bp->pdev = pdev;
14709 
14710 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
14711 	 * determines the BAR size.
14712 	 */
14713 	bp->bar0 = pci_ioremap_bar(pdev, 0);
14714 	if (!bp->bar0) {
14715 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
14716 		rc = -ENOMEM;
14717 		goto init_err_release;
14718 	}
14719 
14720 	bp->bar2 = pci_ioremap_bar(pdev, 4);
14721 	if (!bp->bar2) {
14722 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
14723 		rc = -ENOMEM;
14724 		goto init_err_release;
14725 	}
14726 
14727 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
14728 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
14729 
14730 	spin_lock_init(&bp->ntp_fltr_lock);
14731 #if BITS_PER_LONG == 32
14732 	spin_lock_init(&bp->db_lock);
14733 #endif
14734 
14735 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
14736 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
14737 
14738 	timer_setup(&bp->timer, bnxt_timer, 0);
14739 	bp->current_interval = BNXT_TIMER_INTERVAL;
14740 
14741 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
14742 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
14743 
14744 	clear_bit(BNXT_STATE_OPEN, &bp->state);
14745 	return 0;
14746 
14747 init_err_release:
14748 	bnxt_unmap_bars(bp, pdev);
14749 	pci_release_regions(pdev);
14750 
14751 init_err_disable:
14752 	pci_disable_device(pdev);
14753 
14754 init_err:
14755 	return rc;
14756 }
14757 
14758 /* rtnl_lock held */
bnxt_change_mac_addr(struct net_device * dev,void * p)14759 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
14760 {
14761 	struct sockaddr *addr = p;
14762 	struct bnxt *bp = netdev_priv(dev);
14763 	int rc = 0;
14764 
14765 	if (!is_valid_ether_addr(addr->sa_data))
14766 		return -EADDRNOTAVAIL;
14767 
14768 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
14769 		return 0;
14770 
14771 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
14772 	if (rc)
14773 		return rc;
14774 
14775 	eth_hw_addr_set(dev, addr->sa_data);
14776 	bnxt_clear_usr_fltrs(bp, true);
14777 	if (netif_running(dev)) {
14778 		bnxt_close_nic(bp, false, false);
14779 		rc = bnxt_open_nic(bp, false, false);
14780 	}
14781 
14782 	return rc;
14783 }
14784 
14785 /* rtnl_lock held */
bnxt_change_mtu(struct net_device * dev,int new_mtu)14786 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
14787 {
14788 	struct bnxt *bp = netdev_priv(dev);
14789 
14790 	if (netif_running(dev))
14791 		bnxt_close_nic(bp, true, false);
14792 
14793 	WRITE_ONCE(dev->mtu, new_mtu);
14794 
14795 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
14796 	 * program is attached.  We need to set the AGG rings settings and
14797 	 * rx_skb_func accordingly.
14798 	 */
14799 	if (READ_ONCE(bp->xdp_prog))
14800 		bnxt_set_rx_skb_mode(bp, true);
14801 
14802 	bnxt_set_ring_params(bp);
14803 
14804 	if (netif_running(dev))
14805 		return bnxt_open_nic(bp, true, false);
14806 
14807 	return 0;
14808 }
14809 
bnxt_setup_mq_tc(struct net_device * dev,u8 tc)14810 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
14811 {
14812 	struct bnxt *bp = netdev_priv(dev);
14813 	bool sh = false;
14814 	int rc, tx_cp;
14815 
14816 	if (tc > bp->max_tc) {
14817 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
14818 			   tc, bp->max_tc);
14819 		return -EINVAL;
14820 	}
14821 
14822 	if (bp->num_tc == tc)
14823 		return 0;
14824 
14825 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
14826 		sh = true;
14827 
14828 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
14829 			      sh, tc, bp->tx_nr_rings_xdp);
14830 	if (rc)
14831 		return rc;
14832 
14833 	/* Needs to close the device and do hw resource re-allocations */
14834 	if (netif_running(bp->dev))
14835 		bnxt_close_nic(bp, true, false);
14836 
14837 	if (tc) {
14838 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
14839 		netdev_set_num_tc(dev, tc);
14840 		bp->num_tc = tc;
14841 	} else {
14842 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
14843 		netdev_reset_tc(dev);
14844 		bp->num_tc = 0;
14845 	}
14846 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
14847 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
14848 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
14849 			       tx_cp + bp->rx_nr_rings;
14850 
14851 	if (netif_running(bp->dev))
14852 		return bnxt_open_nic(bp, true, false);
14853 
14854 	return 0;
14855 }
14856 
bnxt_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)14857 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
14858 				  void *cb_priv)
14859 {
14860 	struct bnxt *bp = cb_priv;
14861 
14862 	if (!bnxt_tc_flower_enabled(bp) ||
14863 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
14864 		return -EOPNOTSUPP;
14865 
14866 	switch (type) {
14867 	case TC_SETUP_CLSFLOWER:
14868 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
14869 	default:
14870 		return -EOPNOTSUPP;
14871 	}
14872 }
14873 
14874 LIST_HEAD(bnxt_block_cb_list);
14875 
bnxt_setup_tc(struct net_device * dev,enum tc_setup_type type,void * type_data)14876 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
14877 			 void *type_data)
14878 {
14879 	struct bnxt *bp = netdev_priv(dev);
14880 
14881 	switch (type) {
14882 	case TC_SETUP_BLOCK:
14883 		return flow_block_cb_setup_simple(type_data,
14884 						  &bnxt_block_cb_list,
14885 						  bnxt_setup_tc_block_cb,
14886 						  bp, bp, true);
14887 	case TC_SETUP_QDISC_MQPRIO: {
14888 		struct tc_mqprio_qopt *mqprio = type_data;
14889 
14890 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
14891 
14892 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
14893 	}
14894 	default:
14895 		return -EOPNOTSUPP;
14896 	}
14897 }
14898 
bnxt_get_ntp_filter_idx(struct bnxt * bp,struct flow_keys * fkeys,const struct sk_buff * skb)14899 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
14900 			    const struct sk_buff *skb)
14901 {
14902 	struct bnxt_vnic_info *vnic;
14903 
14904 	if (skb)
14905 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
14906 
14907 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
14908 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
14909 }
14910 
bnxt_insert_ntp_filter(struct bnxt * bp,struct bnxt_ntuple_filter * fltr,u32 idx)14911 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
14912 			   u32 idx)
14913 {
14914 	struct hlist_head *head;
14915 	int bit_id;
14916 
14917 	spin_lock_bh(&bp->ntp_fltr_lock);
14918 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
14919 	if (bit_id < 0) {
14920 		spin_unlock_bh(&bp->ntp_fltr_lock);
14921 		return -ENOMEM;
14922 	}
14923 
14924 	fltr->base.sw_id = (u16)bit_id;
14925 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
14926 	fltr->base.flags |= BNXT_ACT_RING_DST;
14927 	head = &bp->ntp_fltr_hash_tbl[idx];
14928 	hlist_add_head_rcu(&fltr->base.hash, head);
14929 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
14930 	bnxt_insert_usr_fltr(bp, &fltr->base);
14931 	bp->ntp_fltr_count++;
14932 	spin_unlock_bh(&bp->ntp_fltr_lock);
14933 	return 0;
14934 }
14935 
bnxt_fltr_match(struct bnxt_ntuple_filter * f1,struct bnxt_ntuple_filter * f2)14936 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
14937 			    struct bnxt_ntuple_filter *f2)
14938 {
14939 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
14940 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
14941 	struct flow_keys *keys1 = &f1->fkeys;
14942 	struct flow_keys *keys2 = &f2->fkeys;
14943 
14944 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
14945 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
14946 		return false;
14947 
14948 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
14949 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
14950 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
14951 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
14952 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
14953 			return false;
14954 	} else {
14955 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
14956 				     &keys2->addrs.v6addrs.src) ||
14957 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
14958 				     &masks2->addrs.v6addrs.src) ||
14959 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
14960 				     &keys2->addrs.v6addrs.dst) ||
14961 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
14962 				     &masks2->addrs.v6addrs.dst))
14963 			return false;
14964 	}
14965 
14966 	return keys1->ports.src == keys2->ports.src &&
14967 	       masks1->ports.src == masks2->ports.src &&
14968 	       keys1->ports.dst == keys2->ports.dst &&
14969 	       masks1->ports.dst == masks2->ports.dst &&
14970 	       keys1->control.flags == keys2->control.flags &&
14971 	       f1->l2_fltr == f2->l2_fltr;
14972 }
14973 
14974 struct bnxt_ntuple_filter *
bnxt_lookup_ntp_filter_from_idx(struct bnxt * bp,struct bnxt_ntuple_filter * fltr,u32 idx)14975 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
14976 				struct bnxt_ntuple_filter *fltr, u32 idx)
14977 {
14978 	struct bnxt_ntuple_filter *f;
14979 	struct hlist_head *head;
14980 
14981 	head = &bp->ntp_fltr_hash_tbl[idx];
14982 	hlist_for_each_entry_rcu(f, head, base.hash) {
14983 		if (bnxt_fltr_match(f, fltr))
14984 			return f;
14985 	}
14986 	return NULL;
14987 }
14988 
14989 #ifdef CONFIG_RFS_ACCEL
bnxt_rx_flow_steer(struct net_device * dev,const struct sk_buff * skb,u16 rxq_index,u32 flow_id)14990 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
14991 			      u16 rxq_index, u32 flow_id)
14992 {
14993 	struct bnxt *bp = netdev_priv(dev);
14994 	struct bnxt_ntuple_filter *fltr, *new_fltr;
14995 	struct flow_keys *fkeys;
14996 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
14997 	struct bnxt_l2_filter *l2_fltr;
14998 	int rc = 0, idx;
14999 	u32 flags;
15000 
15001 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15002 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15003 		atomic_inc(&l2_fltr->refcnt);
15004 	} else {
15005 		struct bnxt_l2_key key;
15006 
15007 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15008 		key.vlan = 0;
15009 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15010 		if (!l2_fltr)
15011 			return -EINVAL;
15012 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15013 			bnxt_del_l2_filter(bp, l2_fltr);
15014 			return -EINVAL;
15015 		}
15016 	}
15017 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
15018 	if (!new_fltr) {
15019 		bnxt_del_l2_filter(bp, l2_fltr);
15020 		return -ENOMEM;
15021 	}
15022 
15023 	fkeys = &new_fltr->fkeys;
15024 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15025 		rc = -EPROTONOSUPPORT;
15026 		goto err_free;
15027 	}
15028 
15029 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15030 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15031 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15032 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15033 		rc = -EPROTONOSUPPORT;
15034 		goto err_free;
15035 	}
15036 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15037 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15038 		if (bp->hwrm_spec_code < 0x10601) {
15039 			rc = -EPROTONOSUPPORT;
15040 			goto err_free;
15041 		}
15042 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15043 	}
15044 	flags = fkeys->control.flags;
15045 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15046 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15047 		rc = -EPROTONOSUPPORT;
15048 		goto err_free;
15049 	}
15050 	new_fltr->l2_fltr = l2_fltr;
15051 
15052 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15053 	rcu_read_lock();
15054 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15055 	if (fltr) {
15056 		rc = fltr->base.sw_id;
15057 		rcu_read_unlock();
15058 		goto err_free;
15059 	}
15060 	rcu_read_unlock();
15061 
15062 	new_fltr->flow_id = flow_id;
15063 	new_fltr->base.rxq = rxq_index;
15064 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15065 	if (!rc) {
15066 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15067 		return new_fltr->base.sw_id;
15068 	}
15069 
15070 err_free:
15071 	bnxt_del_l2_filter(bp, l2_fltr);
15072 	kfree(new_fltr);
15073 	return rc;
15074 }
15075 #endif
15076 
bnxt_del_ntp_filter(struct bnxt * bp,struct bnxt_ntuple_filter * fltr)15077 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15078 {
15079 	spin_lock_bh(&bp->ntp_fltr_lock);
15080 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15081 		spin_unlock_bh(&bp->ntp_fltr_lock);
15082 		return;
15083 	}
15084 	hlist_del_rcu(&fltr->base.hash);
15085 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15086 	bp->ntp_fltr_count--;
15087 	spin_unlock_bh(&bp->ntp_fltr_lock);
15088 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15089 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15090 	kfree_rcu(fltr, base.rcu);
15091 }
15092 
bnxt_cfg_ntp_filters(struct bnxt * bp)15093 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15094 {
15095 #ifdef CONFIG_RFS_ACCEL
15096 	int i;
15097 
15098 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15099 		struct hlist_head *head;
15100 		struct hlist_node *tmp;
15101 		struct bnxt_ntuple_filter *fltr;
15102 		int rc;
15103 
15104 		head = &bp->ntp_fltr_hash_tbl[i];
15105 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15106 			bool del = false;
15107 
15108 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15109 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15110 					continue;
15111 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15112 							fltr->flow_id,
15113 							fltr->base.sw_id)) {
15114 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15115 									 fltr);
15116 					del = true;
15117 				}
15118 			} else {
15119 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15120 								       fltr);
15121 				if (rc)
15122 					del = true;
15123 				else
15124 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15125 			}
15126 
15127 			if (del)
15128 				bnxt_del_ntp_filter(bp, fltr);
15129 		}
15130 	}
15131 #endif
15132 }
15133 
bnxt_udp_tunnel_set_port(struct net_device * netdev,unsigned int table,unsigned int entry,struct udp_tunnel_info * ti)15134 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15135 				    unsigned int entry, struct udp_tunnel_info *ti)
15136 {
15137 	struct bnxt *bp = netdev_priv(netdev);
15138 	unsigned int cmd;
15139 
15140 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15141 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15142 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15143 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15144 	else
15145 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15146 
15147 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15148 }
15149 
bnxt_udp_tunnel_unset_port(struct net_device * netdev,unsigned int table,unsigned int entry,struct udp_tunnel_info * ti)15150 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15151 				      unsigned int entry, struct udp_tunnel_info *ti)
15152 {
15153 	struct bnxt *bp = netdev_priv(netdev);
15154 	unsigned int cmd;
15155 
15156 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15157 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15158 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15159 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15160 	else
15161 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15162 
15163 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15164 }
15165 
15166 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15167 	.set_port	= bnxt_udp_tunnel_set_port,
15168 	.unset_port	= bnxt_udp_tunnel_unset_port,
15169 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15170 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15171 	.tables		= {
15172 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15173 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15174 	},
15175 }, bnxt_udp_tunnels_p7 = {
15176 	.set_port	= bnxt_udp_tunnel_set_port,
15177 	.unset_port	= bnxt_udp_tunnel_unset_port,
15178 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15179 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15180 	.tables		= {
15181 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15182 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15183 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15184 	},
15185 };
15186 
bnxt_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 filter_mask,int nlflags)15187 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15188 			       struct net_device *dev, u32 filter_mask,
15189 			       int nlflags)
15190 {
15191 	struct bnxt *bp = netdev_priv(dev);
15192 
15193 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15194 				       nlflags, filter_mask, NULL);
15195 }
15196 
bnxt_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags,struct netlink_ext_ack * extack)15197 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15198 			       u16 flags, struct netlink_ext_ack *extack)
15199 {
15200 	struct bnxt *bp = netdev_priv(dev);
15201 	struct nlattr *attr, *br_spec;
15202 	int rem, rc = 0;
15203 
15204 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15205 		return -EOPNOTSUPP;
15206 
15207 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15208 	if (!br_spec)
15209 		return -EINVAL;
15210 
15211 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15212 		u16 mode;
15213 
15214 		mode = nla_get_u16(attr);
15215 		if (mode == bp->br_mode)
15216 			break;
15217 
15218 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15219 		if (!rc)
15220 			bp->br_mode = mode;
15221 		break;
15222 	}
15223 	return rc;
15224 }
15225 
bnxt_get_port_parent_id(struct net_device * dev,struct netdev_phys_item_id * ppid)15226 int bnxt_get_port_parent_id(struct net_device *dev,
15227 			    struct netdev_phys_item_id *ppid)
15228 {
15229 	struct bnxt *bp = netdev_priv(dev);
15230 
15231 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15232 		return -EOPNOTSUPP;
15233 
15234 	/* The PF and it's VF-reps only support the switchdev framework */
15235 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15236 		return -EOPNOTSUPP;
15237 
15238 	ppid->id_len = sizeof(bp->dsn);
15239 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15240 
15241 	return 0;
15242 }
15243 
15244 static const struct net_device_ops bnxt_netdev_ops = {
15245 	.ndo_open		= bnxt_open,
15246 	.ndo_start_xmit		= bnxt_start_xmit,
15247 	.ndo_stop		= bnxt_close,
15248 	.ndo_get_stats64	= bnxt_get_stats64,
15249 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15250 	.ndo_eth_ioctl		= bnxt_ioctl,
15251 	.ndo_validate_addr	= eth_validate_addr,
15252 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15253 	.ndo_change_mtu		= bnxt_change_mtu,
15254 	.ndo_fix_features	= bnxt_fix_features,
15255 	.ndo_set_features	= bnxt_set_features,
15256 	.ndo_features_check	= bnxt_features_check,
15257 	.ndo_tx_timeout		= bnxt_tx_timeout,
15258 #ifdef CONFIG_BNXT_SRIOV
15259 	.ndo_get_vf_config	= bnxt_get_vf_config,
15260 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15261 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15262 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15263 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15264 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15265 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15266 #endif
15267 	.ndo_setup_tc           = bnxt_setup_tc,
15268 #ifdef CONFIG_RFS_ACCEL
15269 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15270 #endif
15271 	.ndo_bpf		= bnxt_xdp,
15272 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15273 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15274 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15275 };
15276 
bnxt_get_queue_stats_rx(struct net_device * dev,int i,struct netdev_queue_stats_rx * stats)15277 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15278 				    struct netdev_queue_stats_rx *stats)
15279 {
15280 	struct bnxt *bp = netdev_priv(dev);
15281 	struct bnxt_cp_ring_info *cpr;
15282 	u64 *sw;
15283 
15284 	cpr = &bp->bnapi[i]->cp_ring;
15285 	sw = cpr->stats.sw_stats;
15286 
15287 	stats->packets = 0;
15288 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15289 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15290 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15291 
15292 	stats->bytes = 0;
15293 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15294 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15295 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15296 
15297 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15298 }
15299 
bnxt_get_queue_stats_tx(struct net_device * dev,int i,struct netdev_queue_stats_tx * stats)15300 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15301 				    struct netdev_queue_stats_tx *stats)
15302 {
15303 	struct bnxt *bp = netdev_priv(dev);
15304 	struct bnxt_napi *bnapi;
15305 	u64 *sw;
15306 
15307 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15308 	sw = bnapi->cp_ring.stats.sw_stats;
15309 
15310 	stats->packets = 0;
15311 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15312 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15313 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15314 
15315 	stats->bytes = 0;
15316 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15317 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15318 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15319 }
15320 
bnxt_get_base_stats(struct net_device * dev,struct netdev_queue_stats_rx * rx,struct netdev_queue_stats_tx * tx)15321 static void bnxt_get_base_stats(struct net_device *dev,
15322 				struct netdev_queue_stats_rx *rx,
15323 				struct netdev_queue_stats_tx *tx)
15324 {
15325 	struct bnxt *bp = netdev_priv(dev);
15326 
15327 	rx->packets = bp->net_stats_prev.rx_packets;
15328 	rx->bytes = bp->net_stats_prev.rx_bytes;
15329 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15330 
15331 	tx->packets = bp->net_stats_prev.tx_packets;
15332 	tx->bytes = bp->net_stats_prev.tx_bytes;
15333 }
15334 
15335 static const struct netdev_stat_ops bnxt_stat_ops = {
15336 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15337 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15338 	.get_base_stats		= bnxt_get_base_stats,
15339 };
15340 
bnxt_queue_mem_alloc(struct net_device * dev,void * qmem,int idx)15341 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15342 {
15343 	struct bnxt_rx_ring_info *rxr, *clone;
15344 	struct bnxt *bp = netdev_priv(dev);
15345 	struct bnxt_ring_struct *ring;
15346 	int rc;
15347 
15348 	rxr = &bp->rx_ring[idx];
15349 	clone = qmem;
15350 	memcpy(clone, rxr, sizeof(*rxr));
15351 	bnxt_init_rx_ring_struct(bp, clone);
15352 	bnxt_reset_rx_ring_struct(bp, clone);
15353 
15354 	clone->rx_prod = 0;
15355 	clone->rx_agg_prod = 0;
15356 	clone->rx_sw_agg_prod = 0;
15357 	clone->rx_next_cons = 0;
15358 
15359 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15360 	if (rc)
15361 		return rc;
15362 
15363 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15364 	if (rc < 0)
15365 		goto err_page_pool_destroy;
15366 
15367 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15368 					MEM_TYPE_PAGE_POOL,
15369 					clone->page_pool);
15370 	if (rc)
15371 		goto err_rxq_info_unreg;
15372 
15373 	ring = &clone->rx_ring_struct;
15374 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15375 	if (rc)
15376 		goto err_free_rx_ring;
15377 
15378 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15379 		ring = &clone->rx_agg_ring_struct;
15380 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15381 		if (rc)
15382 			goto err_free_rx_agg_ring;
15383 
15384 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15385 		if (rc)
15386 			goto err_free_rx_agg_ring;
15387 	}
15388 
15389 	if (bp->flags & BNXT_FLAG_TPA) {
15390 		rc = bnxt_alloc_one_tpa_info(bp, clone);
15391 		if (rc)
15392 			goto err_free_tpa_info;
15393 	}
15394 
15395 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15396 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15397 
15398 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15399 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15400 		bnxt_alloc_one_rx_ring_page(bp, clone, idx);
15401 	if (bp->flags & BNXT_FLAG_TPA)
15402 		bnxt_alloc_one_tpa_info_data(bp, clone);
15403 
15404 	return 0;
15405 
15406 err_free_tpa_info:
15407 	bnxt_free_one_tpa_info(bp, clone);
15408 err_free_rx_agg_ring:
15409 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15410 err_free_rx_ring:
15411 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15412 err_rxq_info_unreg:
15413 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15414 err_page_pool_destroy:
15415 	page_pool_destroy(clone->page_pool);
15416 	if (bnxt_separate_head_pool())
15417 		page_pool_destroy(clone->head_pool);
15418 	clone->page_pool = NULL;
15419 	clone->head_pool = NULL;
15420 	return rc;
15421 }
15422 
bnxt_queue_mem_free(struct net_device * dev,void * qmem)15423 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15424 {
15425 	struct bnxt_rx_ring_info *rxr = qmem;
15426 	struct bnxt *bp = netdev_priv(dev);
15427 	struct bnxt_ring_struct *ring;
15428 
15429 	bnxt_free_one_rx_ring_skbs(bp, rxr);
15430 
15431 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15432 
15433 	page_pool_destroy(rxr->page_pool);
15434 	if (bnxt_separate_head_pool())
15435 		page_pool_destroy(rxr->head_pool);
15436 	rxr->page_pool = NULL;
15437 	rxr->head_pool = NULL;
15438 
15439 	ring = &rxr->rx_ring_struct;
15440 	bnxt_free_ring(bp, &ring->ring_mem);
15441 
15442 	ring = &rxr->rx_agg_ring_struct;
15443 	bnxt_free_ring(bp, &ring->ring_mem);
15444 
15445 	kfree(rxr->rx_agg_bmap);
15446 	rxr->rx_agg_bmap = NULL;
15447 }
15448 
bnxt_copy_rx_ring(struct bnxt * bp,struct bnxt_rx_ring_info * dst,struct bnxt_rx_ring_info * src)15449 static void bnxt_copy_rx_ring(struct bnxt *bp,
15450 			      struct bnxt_rx_ring_info *dst,
15451 			      struct bnxt_rx_ring_info *src)
15452 {
15453 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15454 	struct bnxt_ring_struct *dst_ring, *src_ring;
15455 	int i;
15456 
15457 	dst_ring = &dst->rx_ring_struct;
15458 	dst_rmem = &dst_ring->ring_mem;
15459 	src_ring = &src->rx_ring_struct;
15460 	src_rmem = &src_ring->ring_mem;
15461 
15462 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15463 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15464 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15465 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15466 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15467 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15468 
15469 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15470 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15471 	*dst_rmem->vmem = *src_rmem->vmem;
15472 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15473 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15474 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15475 	}
15476 
15477 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15478 		return;
15479 
15480 	dst_ring = &dst->rx_agg_ring_struct;
15481 	dst_rmem = &dst_ring->ring_mem;
15482 	src_ring = &src->rx_agg_ring_struct;
15483 	src_rmem = &src_ring->ring_mem;
15484 
15485 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15486 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15487 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15488 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15489 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15490 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15491 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15492 
15493 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15494 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15495 	*dst_rmem->vmem = *src_rmem->vmem;
15496 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15497 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15498 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15499 	}
15500 
15501 	dst->rx_agg_bmap = src->rx_agg_bmap;
15502 }
15503 
bnxt_queue_start(struct net_device * dev,void * qmem,int idx)15504 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15505 {
15506 	struct bnxt *bp = netdev_priv(dev);
15507 	struct bnxt_rx_ring_info *rxr, *clone;
15508 	struct bnxt_cp_ring_info *cpr;
15509 	struct bnxt_vnic_info *vnic;
15510 	int i, rc;
15511 
15512 	rxr = &bp->rx_ring[idx];
15513 	clone = qmem;
15514 
15515 	rxr->rx_prod = clone->rx_prod;
15516 	rxr->rx_agg_prod = clone->rx_agg_prod;
15517 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
15518 	rxr->rx_next_cons = clone->rx_next_cons;
15519 	rxr->rx_tpa = clone->rx_tpa;
15520 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
15521 	rxr->page_pool = clone->page_pool;
15522 	rxr->head_pool = clone->head_pool;
15523 	rxr->xdp_rxq = clone->xdp_rxq;
15524 
15525 	bnxt_copy_rx_ring(bp, rxr, clone);
15526 
15527 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
15528 	if (rc)
15529 		return rc;
15530 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
15531 	if (rc)
15532 		goto err_free_hwrm_rx_ring;
15533 
15534 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
15535 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15536 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
15537 
15538 	cpr = &rxr->bnapi->cp_ring;
15539 	cpr->sw_stats->rx.rx_resets++;
15540 
15541 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15542 		vnic = &bp->vnic_info[i];
15543 
15544 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
15545 		if (rc) {
15546 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
15547 				   vnic->vnic_id, rc);
15548 			return rc;
15549 		}
15550 		vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
15551 		bnxt_hwrm_vnic_update(bp, vnic,
15552 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15553 	}
15554 
15555 	return 0;
15556 
15557 err_free_hwrm_rx_ring:
15558 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15559 	return rc;
15560 }
15561 
bnxt_queue_stop(struct net_device * dev,void * qmem,int idx)15562 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
15563 {
15564 	struct bnxt *bp = netdev_priv(dev);
15565 	struct bnxt_rx_ring_info *rxr;
15566 	struct bnxt_vnic_info *vnic;
15567 	int i;
15568 
15569 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15570 		vnic = &bp->vnic_info[i];
15571 		vnic->mru = 0;
15572 		bnxt_hwrm_vnic_update(bp, vnic,
15573 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15574 	}
15575 
15576 	rxr = &bp->rx_ring[idx];
15577 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15578 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
15579 	rxr->rx_next_cons = 0;
15580 	page_pool_disable_direct_recycling(rxr->page_pool);
15581 	if (bnxt_separate_head_pool())
15582 		page_pool_disable_direct_recycling(rxr->head_pool);
15583 
15584 	memcpy(qmem, rxr, sizeof(*rxr));
15585 	bnxt_init_rx_ring_struct(bp, qmem);
15586 
15587 	return 0;
15588 }
15589 
15590 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
15591 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
15592 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
15593 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
15594 	.ndo_queue_start	= bnxt_queue_start,
15595 	.ndo_queue_stop		= bnxt_queue_stop,
15596 };
15597 
bnxt_remove_one(struct pci_dev * pdev)15598 static void bnxt_remove_one(struct pci_dev *pdev)
15599 {
15600 	struct net_device *dev = pci_get_drvdata(pdev);
15601 	struct bnxt *bp = netdev_priv(dev);
15602 
15603 	if (BNXT_PF(bp))
15604 		bnxt_sriov_disable(bp);
15605 
15606 	bnxt_rdma_aux_device_del(bp);
15607 
15608 	bnxt_ptp_clear(bp);
15609 	unregister_netdev(dev);
15610 
15611 	bnxt_rdma_aux_device_uninit(bp);
15612 
15613 	bnxt_free_l2_filters(bp, true);
15614 	bnxt_free_ntp_fltrs(bp, true);
15615 	WARN_ON(bp->num_rss_ctx);
15616 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15617 	/* Flush any pending tasks */
15618 	cancel_work_sync(&bp->sp_task);
15619 	cancel_delayed_work_sync(&bp->fw_reset_task);
15620 	bp->sp_event = 0;
15621 
15622 	bnxt_dl_fw_reporters_destroy(bp);
15623 	bnxt_dl_unregister(bp);
15624 	bnxt_shutdown_tc(bp);
15625 
15626 	bnxt_clear_int_mode(bp);
15627 	bnxt_hwrm_func_drv_unrgtr(bp);
15628 	bnxt_free_hwrm_resources(bp);
15629 	bnxt_hwmon_uninit(bp);
15630 	bnxt_ethtool_free(bp);
15631 	bnxt_dcb_free(bp);
15632 	kfree(bp->ptp_cfg);
15633 	bp->ptp_cfg = NULL;
15634 	kfree(bp->fw_health);
15635 	bp->fw_health = NULL;
15636 	bnxt_cleanup_pci(bp);
15637 	bnxt_free_ctx_mem(bp, true);
15638 	bnxt_free_crash_dump_mem(bp);
15639 	kfree(bp->rss_indir_tbl);
15640 	bp->rss_indir_tbl = NULL;
15641 	bnxt_free_port_stats(bp);
15642 	free_netdev(dev);
15643 }
15644 
bnxt_probe_phy(struct bnxt * bp,bool fw_dflt)15645 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
15646 {
15647 	int rc = 0;
15648 	struct bnxt_link_info *link_info = &bp->link_info;
15649 
15650 	bp->phy_flags = 0;
15651 	rc = bnxt_hwrm_phy_qcaps(bp);
15652 	if (rc) {
15653 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
15654 			   rc);
15655 		return rc;
15656 	}
15657 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
15658 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
15659 	else
15660 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
15661 	if (!fw_dflt)
15662 		return 0;
15663 
15664 	mutex_lock(&bp->link_lock);
15665 	rc = bnxt_update_link(bp, false);
15666 	if (rc) {
15667 		mutex_unlock(&bp->link_lock);
15668 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
15669 			   rc);
15670 		return rc;
15671 	}
15672 
15673 	/* Older firmware does not have supported_auto_speeds, so assume
15674 	 * that all supported speeds can be autonegotiated.
15675 	 */
15676 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
15677 		link_info->support_auto_speeds = link_info->support_speeds;
15678 
15679 	bnxt_init_ethtool_link_settings(bp);
15680 	mutex_unlock(&bp->link_lock);
15681 	return 0;
15682 }
15683 
bnxt_get_max_irq(struct pci_dev * pdev)15684 static int bnxt_get_max_irq(struct pci_dev *pdev)
15685 {
15686 	u16 ctrl;
15687 
15688 	if (!pdev->msix_cap)
15689 		return 1;
15690 
15691 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
15692 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
15693 }
15694 
_bnxt_get_max_rings(struct bnxt * bp,int * max_rx,int * max_tx,int * max_cp)15695 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15696 				int *max_cp)
15697 {
15698 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
15699 	int max_ring_grps = 0, max_irq;
15700 
15701 	*max_tx = hw_resc->max_tx_rings;
15702 	*max_rx = hw_resc->max_rx_rings;
15703 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
15704 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
15705 			bnxt_get_ulp_msix_num_in_use(bp),
15706 			hw_resc->max_stat_ctxs -
15707 			bnxt_get_ulp_stat_ctxs_in_use(bp));
15708 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
15709 		*max_cp = min_t(int, *max_cp, max_irq);
15710 	max_ring_grps = hw_resc->max_hw_ring_grps;
15711 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
15712 		*max_cp -= 1;
15713 		*max_rx -= 2;
15714 	}
15715 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15716 		*max_rx >>= 1;
15717 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
15718 		int rc;
15719 
15720 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
15721 		if (rc) {
15722 			*max_rx = 0;
15723 			*max_tx = 0;
15724 		}
15725 		/* On P5 chips, max_cp output param should be available NQs */
15726 		*max_cp = max_irq;
15727 	}
15728 	*max_rx = min_t(int, *max_rx, max_ring_grps);
15729 }
15730 
bnxt_get_max_rings(struct bnxt * bp,int * max_rx,int * max_tx,bool shared)15731 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
15732 {
15733 	int rx, tx, cp;
15734 
15735 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
15736 	*max_rx = rx;
15737 	*max_tx = tx;
15738 	if (!rx || !tx || !cp)
15739 		return -ENOMEM;
15740 
15741 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
15742 }
15743 
bnxt_get_dflt_rings(struct bnxt * bp,int * max_rx,int * max_tx,bool shared)15744 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15745 			       bool shared)
15746 {
15747 	int rc;
15748 
15749 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15750 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
15751 		/* Not enough rings, try disabling agg rings. */
15752 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
15753 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15754 		if (rc) {
15755 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
15756 			bp->flags |= BNXT_FLAG_AGG_RINGS;
15757 			return rc;
15758 		}
15759 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
15760 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15761 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15762 		bnxt_set_ring_params(bp);
15763 	}
15764 
15765 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
15766 		int max_cp, max_stat, max_irq;
15767 
15768 		/* Reserve minimum resources for RoCE */
15769 		max_cp = bnxt_get_max_func_cp_rings(bp);
15770 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
15771 		max_irq = bnxt_get_max_func_irqs(bp);
15772 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
15773 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
15774 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
15775 			return 0;
15776 
15777 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
15778 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
15779 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
15780 		max_cp = min_t(int, max_cp, max_irq);
15781 		max_cp = min_t(int, max_cp, max_stat);
15782 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
15783 		if (rc)
15784 			rc = 0;
15785 	}
15786 	return rc;
15787 }
15788 
15789 /* In initial default shared ring setting, each shared ring must have a
15790  * RX/TX ring pair.
15791  */
bnxt_trim_dflt_sh_rings(struct bnxt * bp)15792 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
15793 {
15794 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
15795 	bp->rx_nr_rings = bp->cp_nr_rings;
15796 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
15797 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15798 }
15799 
bnxt_set_dflt_rings(struct bnxt * bp,bool sh)15800 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
15801 {
15802 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
15803 	int avail_msix;
15804 
15805 	if (!bnxt_can_reserve_rings(bp))
15806 		return 0;
15807 
15808 	if (sh)
15809 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
15810 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
15811 	/* Reduce default rings on multi-port cards so that total default
15812 	 * rings do not exceed CPU count.
15813 	 */
15814 	if (bp->port_count > 1) {
15815 		int max_rings =
15816 			max_t(int, num_online_cpus() / bp->port_count, 1);
15817 
15818 		dflt_rings = min_t(int, dflt_rings, max_rings);
15819 	}
15820 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
15821 	if (rc)
15822 		return rc;
15823 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
15824 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
15825 	if (sh)
15826 		bnxt_trim_dflt_sh_rings(bp);
15827 	else
15828 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
15829 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15830 
15831 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
15832 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
15833 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
15834 
15835 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
15836 		bnxt_set_dflt_ulp_stat_ctxs(bp);
15837 	}
15838 
15839 	rc = __bnxt_reserve_rings(bp);
15840 	if (rc && rc != -ENODEV)
15841 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
15842 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15843 	if (sh)
15844 		bnxt_trim_dflt_sh_rings(bp);
15845 
15846 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
15847 	if (bnxt_need_reserve_rings(bp)) {
15848 		rc = __bnxt_reserve_rings(bp);
15849 		if (rc && rc != -ENODEV)
15850 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
15851 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15852 	}
15853 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
15854 		bp->rx_nr_rings++;
15855 		bp->cp_nr_rings++;
15856 	}
15857 	if (rc) {
15858 		bp->tx_nr_rings = 0;
15859 		bp->rx_nr_rings = 0;
15860 	}
15861 	return rc;
15862 }
15863 
bnxt_init_dflt_ring_mode(struct bnxt * bp)15864 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
15865 {
15866 	int rc;
15867 
15868 	if (bp->tx_nr_rings)
15869 		return 0;
15870 
15871 	bnxt_ulp_irq_stop(bp);
15872 	bnxt_clear_int_mode(bp);
15873 	rc = bnxt_set_dflt_rings(bp, true);
15874 	if (rc) {
15875 		if (BNXT_VF(bp) && rc == -ENODEV)
15876 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15877 		else
15878 			netdev_err(bp->dev, "Not enough rings available.\n");
15879 		goto init_dflt_ring_err;
15880 	}
15881 	rc = bnxt_init_int_mode(bp);
15882 	if (rc)
15883 		goto init_dflt_ring_err;
15884 
15885 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15886 
15887 	bnxt_set_dflt_rfs(bp);
15888 
15889 init_dflt_ring_err:
15890 	bnxt_ulp_irq_restart(bp, rc);
15891 	return rc;
15892 }
15893 
bnxt_restore_pf_fw_resources(struct bnxt * bp)15894 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
15895 {
15896 	int rc;
15897 
15898 	ASSERT_RTNL();
15899 	bnxt_hwrm_func_qcaps(bp);
15900 
15901 	if (netif_running(bp->dev))
15902 		__bnxt_close_nic(bp, true, false);
15903 
15904 	bnxt_ulp_irq_stop(bp);
15905 	bnxt_clear_int_mode(bp);
15906 	rc = bnxt_init_int_mode(bp);
15907 	bnxt_ulp_irq_restart(bp, rc);
15908 
15909 	if (netif_running(bp->dev)) {
15910 		if (rc)
15911 			dev_close(bp->dev);
15912 		else
15913 			rc = bnxt_open_nic(bp, true, false);
15914 	}
15915 
15916 	return rc;
15917 }
15918 
bnxt_init_mac_addr(struct bnxt * bp)15919 static int bnxt_init_mac_addr(struct bnxt *bp)
15920 {
15921 	int rc = 0;
15922 
15923 	if (BNXT_PF(bp)) {
15924 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
15925 	} else {
15926 #ifdef CONFIG_BNXT_SRIOV
15927 		struct bnxt_vf_info *vf = &bp->vf;
15928 		bool strict_approval = true;
15929 
15930 		if (is_valid_ether_addr(vf->mac_addr)) {
15931 			/* overwrite netdev dev_addr with admin VF MAC */
15932 			eth_hw_addr_set(bp->dev, vf->mac_addr);
15933 			/* Older PF driver or firmware may not approve this
15934 			 * correctly.
15935 			 */
15936 			strict_approval = false;
15937 		} else {
15938 			eth_hw_addr_random(bp->dev);
15939 		}
15940 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
15941 #endif
15942 	}
15943 	return rc;
15944 }
15945 
bnxt_vpd_read_info(struct bnxt * bp)15946 static void bnxt_vpd_read_info(struct bnxt *bp)
15947 {
15948 	struct pci_dev *pdev = bp->pdev;
15949 	unsigned int vpd_size, kw_len;
15950 	int pos, size;
15951 	u8 *vpd_data;
15952 
15953 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
15954 	if (IS_ERR(vpd_data)) {
15955 		pci_warn(pdev, "Unable to read VPD\n");
15956 		return;
15957 	}
15958 
15959 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15960 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
15961 	if (pos < 0)
15962 		goto read_sn;
15963 
15964 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15965 	memcpy(bp->board_partno, &vpd_data[pos], size);
15966 
15967 read_sn:
15968 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15969 					   PCI_VPD_RO_KEYWORD_SERIALNO,
15970 					   &kw_len);
15971 	if (pos < 0)
15972 		goto exit;
15973 
15974 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15975 	memcpy(bp->board_serialno, &vpd_data[pos], size);
15976 exit:
15977 	kfree(vpd_data);
15978 }
15979 
bnxt_pcie_dsn_get(struct bnxt * bp,u8 dsn[])15980 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
15981 {
15982 	struct pci_dev *pdev = bp->pdev;
15983 	u64 qword;
15984 
15985 	qword = pci_get_dsn(pdev);
15986 	if (!qword) {
15987 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
15988 		return -EOPNOTSUPP;
15989 	}
15990 
15991 	put_unaligned_le64(qword, dsn);
15992 
15993 	bp->flags |= BNXT_FLAG_DSN_VALID;
15994 	return 0;
15995 }
15996 
bnxt_map_db_bar(struct bnxt * bp)15997 static int bnxt_map_db_bar(struct bnxt *bp)
15998 {
15999 	if (!bp->db_size)
16000 		return -ENODEV;
16001 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16002 	if (!bp->bar1)
16003 		return -ENOMEM;
16004 	return 0;
16005 }
16006 
bnxt_print_device_info(struct bnxt * bp)16007 void bnxt_print_device_info(struct bnxt *bp)
16008 {
16009 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16010 		    board_info[bp->board_idx].name,
16011 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16012 
16013 	pcie_print_link_status(bp->pdev);
16014 }
16015 
bnxt_init_one(struct pci_dev * pdev,const struct pci_device_id * ent)16016 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16017 {
16018 	struct bnxt_hw_resc *hw_resc;
16019 	struct net_device *dev;
16020 	struct bnxt *bp;
16021 	int rc, max_irqs;
16022 
16023 	if (pci_is_bridge(pdev))
16024 		return -ENODEV;
16025 
16026 	if (!pdev->msix_cap) {
16027 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16028 		return -ENODEV;
16029 	}
16030 
16031 	/* Clear any pending DMA transactions from crash kernel
16032 	 * while loading driver in capture kernel.
16033 	 */
16034 	if (is_kdump_kernel()) {
16035 		pci_clear_master(pdev);
16036 		pcie_flr(pdev);
16037 	}
16038 
16039 	max_irqs = bnxt_get_max_irq(pdev);
16040 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16041 				 max_irqs);
16042 	if (!dev)
16043 		return -ENOMEM;
16044 
16045 	bp = netdev_priv(dev);
16046 	bp->board_idx = ent->driver_data;
16047 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16048 	bnxt_set_max_func_irqs(bp, max_irqs);
16049 
16050 	if (bnxt_vf_pciid(bp->board_idx))
16051 		bp->flags |= BNXT_FLAG_VF;
16052 
16053 	/* No devlink port registration in case of a VF */
16054 	if (BNXT_PF(bp))
16055 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16056 
16057 	rc = bnxt_init_board(pdev, dev);
16058 	if (rc < 0)
16059 		goto init_err_free;
16060 
16061 	dev->netdev_ops = &bnxt_netdev_ops;
16062 	dev->stat_ops = &bnxt_stat_ops;
16063 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16064 	dev->ethtool_ops = &bnxt_ethtool_ops;
16065 	pci_set_drvdata(pdev, dev);
16066 
16067 	rc = bnxt_alloc_hwrm_resources(bp);
16068 	if (rc)
16069 		goto init_err_pci_clean;
16070 
16071 	mutex_init(&bp->hwrm_cmd_lock);
16072 	mutex_init(&bp->link_lock);
16073 
16074 	rc = bnxt_fw_init_one_p1(bp);
16075 	if (rc)
16076 		goto init_err_pci_clean;
16077 
16078 	if (BNXT_PF(bp))
16079 		bnxt_vpd_read_info(bp);
16080 
16081 	if (BNXT_CHIP_P5_PLUS(bp)) {
16082 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16083 		if (BNXT_CHIP_P7(bp))
16084 			bp->flags |= BNXT_FLAG_CHIP_P7;
16085 	}
16086 
16087 	rc = bnxt_alloc_rss_indir_tbl(bp);
16088 	if (rc)
16089 		goto init_err_pci_clean;
16090 
16091 	rc = bnxt_fw_init_one_p2(bp);
16092 	if (rc)
16093 		goto init_err_pci_clean;
16094 
16095 	rc = bnxt_map_db_bar(bp);
16096 	if (rc) {
16097 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16098 			rc);
16099 		goto init_err_pci_clean;
16100 	}
16101 
16102 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16103 			   NETIF_F_TSO | NETIF_F_TSO6 |
16104 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16105 			   NETIF_F_GSO_IPXIP4 |
16106 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16107 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16108 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16109 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16110 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16111 
16112 	if (BNXT_SUPPORTS_TPA(bp))
16113 		dev->hw_features |= NETIF_F_LRO;
16114 
16115 	dev->hw_enc_features =
16116 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16117 			NETIF_F_TSO | NETIF_F_TSO6 |
16118 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16119 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16120 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16121 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16122 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16123 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16124 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16125 	else
16126 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16127 
16128 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16129 				    NETIF_F_GSO_GRE_CSUM;
16130 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16131 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16132 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16133 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16134 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16135 	if (BNXT_SUPPORTS_TPA(bp))
16136 		dev->hw_features |= NETIF_F_GRO_HW;
16137 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16138 	if (dev->features & NETIF_F_GRO_HW)
16139 		dev->features &= ~NETIF_F_LRO;
16140 	dev->priv_flags |= IFF_UNICAST_FLT;
16141 
16142 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16143 	if (bp->tso_max_segs)
16144 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16145 
16146 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16147 			    NETDEV_XDP_ACT_RX_SG;
16148 
16149 #ifdef CONFIG_BNXT_SRIOV
16150 	init_waitqueue_head(&bp->sriov_cfg_wait);
16151 #endif
16152 	if (BNXT_SUPPORTS_TPA(bp)) {
16153 		bp->gro_func = bnxt_gro_func_5730x;
16154 		if (BNXT_CHIP_P4(bp))
16155 			bp->gro_func = bnxt_gro_func_5731x;
16156 		else if (BNXT_CHIP_P5_PLUS(bp))
16157 			bp->gro_func = bnxt_gro_func_5750x;
16158 	}
16159 	if (!BNXT_CHIP_P4_PLUS(bp))
16160 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16161 
16162 	rc = bnxt_init_mac_addr(bp);
16163 	if (rc) {
16164 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16165 		rc = -EADDRNOTAVAIL;
16166 		goto init_err_pci_clean;
16167 	}
16168 
16169 	if (BNXT_PF(bp)) {
16170 		/* Read the adapter's DSN to use as the eswitch switch_id */
16171 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16172 	}
16173 
16174 	/* MTU range: 60 - FW defined max */
16175 	dev->min_mtu = ETH_ZLEN;
16176 	dev->max_mtu = bp->max_mtu;
16177 
16178 	rc = bnxt_probe_phy(bp, true);
16179 	if (rc)
16180 		goto init_err_pci_clean;
16181 
16182 	hw_resc = &bp->hw_resc;
16183 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16184 		       BNXT_L2_FLTR_MAX_FLTR;
16185 	/* Older firmware may not report these filters properly */
16186 	if (bp->max_fltr < BNXT_MAX_FLTR)
16187 		bp->max_fltr = BNXT_MAX_FLTR;
16188 	bnxt_init_l2_fltr_tbl(bp);
16189 	bnxt_set_rx_skb_mode(bp, false);
16190 	bnxt_set_tpa_flags(bp);
16191 	bnxt_set_ring_params(bp);
16192 	bnxt_rdma_aux_device_init(bp);
16193 	rc = bnxt_set_dflt_rings(bp, true);
16194 	if (rc) {
16195 		if (BNXT_VF(bp) && rc == -ENODEV) {
16196 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16197 		} else {
16198 			netdev_err(bp->dev, "Not enough rings available.\n");
16199 			rc = -ENOMEM;
16200 		}
16201 		goto init_err_pci_clean;
16202 	}
16203 
16204 	bnxt_fw_init_one_p3(bp);
16205 
16206 	bnxt_init_dflt_coal(bp);
16207 
16208 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16209 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16210 
16211 	rc = bnxt_init_int_mode(bp);
16212 	if (rc)
16213 		goto init_err_pci_clean;
16214 
16215 	/* No TC has been set yet and rings may have been trimmed due to
16216 	 * limited MSIX, so we re-initialize the TX rings per TC.
16217 	 */
16218 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16219 
16220 	if (BNXT_PF(bp)) {
16221 		if (!bnxt_pf_wq) {
16222 			bnxt_pf_wq =
16223 				create_singlethread_workqueue("bnxt_pf_wq");
16224 			if (!bnxt_pf_wq) {
16225 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16226 				rc = -ENOMEM;
16227 				goto init_err_pci_clean;
16228 			}
16229 		}
16230 		rc = bnxt_init_tc(bp);
16231 		if (rc)
16232 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16233 				   rc);
16234 	}
16235 
16236 	bnxt_inv_fw_health_reg(bp);
16237 	rc = bnxt_dl_register(bp);
16238 	if (rc)
16239 		goto init_err_dl;
16240 
16241 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16242 
16243 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16244 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16245 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16246 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16247 
16248 	rc = register_netdev(dev);
16249 	if (rc)
16250 		goto init_err_cleanup;
16251 
16252 	bnxt_dl_fw_reporters_create(bp);
16253 
16254 	bnxt_rdma_aux_device_add(bp);
16255 
16256 	bnxt_print_device_info(bp);
16257 
16258 	pci_save_state(pdev);
16259 
16260 	return 0;
16261 init_err_cleanup:
16262 	bnxt_rdma_aux_device_uninit(bp);
16263 	bnxt_dl_unregister(bp);
16264 init_err_dl:
16265 	bnxt_shutdown_tc(bp);
16266 	bnxt_clear_int_mode(bp);
16267 
16268 init_err_pci_clean:
16269 	bnxt_hwrm_func_drv_unrgtr(bp);
16270 	bnxt_free_hwrm_resources(bp);
16271 	bnxt_hwmon_uninit(bp);
16272 	bnxt_ethtool_free(bp);
16273 	bnxt_ptp_clear(bp);
16274 	kfree(bp->ptp_cfg);
16275 	bp->ptp_cfg = NULL;
16276 	kfree(bp->fw_health);
16277 	bp->fw_health = NULL;
16278 	bnxt_cleanup_pci(bp);
16279 	bnxt_free_ctx_mem(bp, true);
16280 	bnxt_free_crash_dump_mem(bp);
16281 	kfree(bp->rss_indir_tbl);
16282 	bp->rss_indir_tbl = NULL;
16283 
16284 init_err_free:
16285 	free_netdev(dev);
16286 	return rc;
16287 }
16288 
bnxt_shutdown(struct pci_dev * pdev)16289 static void bnxt_shutdown(struct pci_dev *pdev)
16290 {
16291 	struct net_device *dev = pci_get_drvdata(pdev);
16292 	struct bnxt *bp;
16293 
16294 	if (!dev)
16295 		return;
16296 
16297 	rtnl_lock();
16298 	bp = netdev_priv(dev);
16299 	if (!bp)
16300 		goto shutdown_exit;
16301 
16302 	if (netif_running(dev))
16303 		dev_close(dev);
16304 
16305 	bnxt_ptp_clear(bp);
16306 	bnxt_clear_int_mode(bp);
16307 	pci_disable_device(pdev);
16308 
16309 	if (system_state == SYSTEM_POWER_OFF) {
16310 		pci_wake_from_d3(pdev, bp->wol);
16311 		pci_set_power_state(pdev, PCI_D3hot);
16312 	}
16313 
16314 shutdown_exit:
16315 	rtnl_unlock();
16316 }
16317 
16318 #ifdef CONFIG_PM_SLEEP
bnxt_suspend(struct device * device)16319 static int bnxt_suspend(struct device *device)
16320 {
16321 	struct net_device *dev = dev_get_drvdata(device);
16322 	struct bnxt *bp = netdev_priv(dev);
16323 	int rc = 0;
16324 
16325 	bnxt_ulp_stop(bp);
16326 
16327 	rtnl_lock();
16328 	if (netif_running(dev)) {
16329 		netif_device_detach(dev);
16330 		rc = bnxt_close(dev);
16331 	}
16332 	bnxt_hwrm_func_drv_unrgtr(bp);
16333 	bnxt_ptp_clear(bp);
16334 	pci_disable_device(bp->pdev);
16335 	bnxt_free_ctx_mem(bp, false);
16336 	rtnl_unlock();
16337 	return rc;
16338 }
16339 
bnxt_resume(struct device * device)16340 static int bnxt_resume(struct device *device)
16341 {
16342 	struct net_device *dev = dev_get_drvdata(device);
16343 	struct bnxt *bp = netdev_priv(dev);
16344 	int rc = 0;
16345 
16346 	rtnl_lock();
16347 	rc = pci_enable_device(bp->pdev);
16348 	if (rc) {
16349 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16350 			   rc);
16351 		goto resume_exit;
16352 	}
16353 	pci_set_master(bp->pdev);
16354 	if (bnxt_hwrm_ver_get(bp)) {
16355 		rc = -ENODEV;
16356 		goto resume_exit;
16357 	}
16358 	rc = bnxt_hwrm_func_reset(bp);
16359 	if (rc) {
16360 		rc = -EBUSY;
16361 		goto resume_exit;
16362 	}
16363 
16364 	rc = bnxt_hwrm_func_qcaps(bp);
16365 	if (rc)
16366 		goto resume_exit;
16367 
16368 	bnxt_clear_reservations(bp, true);
16369 
16370 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16371 		rc = -ENODEV;
16372 		goto resume_exit;
16373 	}
16374 	if (bp->fw_crash_mem)
16375 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16376 
16377 	if (bnxt_ptp_init(bp)) {
16378 		kfree(bp->ptp_cfg);
16379 		bp->ptp_cfg = NULL;
16380 	}
16381 	bnxt_get_wol_settings(bp);
16382 	if (netif_running(dev)) {
16383 		rc = bnxt_open(dev);
16384 		if (!rc)
16385 			netif_device_attach(dev);
16386 	}
16387 
16388 resume_exit:
16389 	rtnl_unlock();
16390 	bnxt_ulp_start(bp, rc);
16391 	if (!rc)
16392 		bnxt_reenable_sriov(bp);
16393 	return rc;
16394 }
16395 
16396 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16397 #define BNXT_PM_OPS (&bnxt_pm_ops)
16398 
16399 #else
16400 
16401 #define BNXT_PM_OPS NULL
16402 
16403 #endif /* CONFIG_PM_SLEEP */
16404 
16405 /**
16406  * bnxt_io_error_detected - called when PCI error is detected
16407  * @pdev: Pointer to PCI device
16408  * @state: The current pci connection state
16409  *
16410  * This function is called after a PCI bus error affecting
16411  * this device has been detected.
16412  */
bnxt_io_error_detected(struct pci_dev * pdev,pci_channel_state_t state)16413 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16414 					       pci_channel_state_t state)
16415 {
16416 	struct net_device *netdev = pci_get_drvdata(pdev);
16417 	struct bnxt *bp = netdev_priv(netdev);
16418 	bool abort = false;
16419 
16420 	netdev_info(netdev, "PCI I/O error detected\n");
16421 
16422 	bnxt_ulp_stop(bp);
16423 
16424 	rtnl_lock();
16425 	netif_device_detach(netdev);
16426 
16427 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16428 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16429 		abort = true;
16430 	}
16431 
16432 	if (abort || state == pci_channel_io_perm_failure) {
16433 		rtnl_unlock();
16434 		return PCI_ERS_RESULT_DISCONNECT;
16435 	}
16436 
16437 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16438 	 * so we disable bus master to prevent any potential bad DMAs before
16439 	 * freeing kernel memory.
16440 	 */
16441 	if (state == pci_channel_io_frozen) {
16442 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16443 		bnxt_fw_fatal_close(bp);
16444 	}
16445 
16446 	if (netif_running(netdev))
16447 		__bnxt_close_nic(bp, true, true);
16448 
16449 	if (pci_is_enabled(pdev))
16450 		pci_disable_device(pdev);
16451 	bnxt_free_ctx_mem(bp, false);
16452 	rtnl_unlock();
16453 
16454 	/* Request a slot slot reset. */
16455 	return PCI_ERS_RESULT_NEED_RESET;
16456 }
16457 
16458 /**
16459  * bnxt_io_slot_reset - called after the pci bus has been reset.
16460  * @pdev: Pointer to PCI device
16461  *
16462  * Restart the card from scratch, as if from a cold-boot.
16463  * At this point, the card has experienced a hard reset,
16464  * followed by fixups by BIOS, and has its config space
16465  * set up identically to what it was at cold boot.
16466  */
bnxt_io_slot_reset(struct pci_dev * pdev)16467 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
16468 {
16469 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
16470 	struct net_device *netdev = pci_get_drvdata(pdev);
16471 	struct bnxt *bp = netdev_priv(netdev);
16472 	int retry = 0;
16473 	int err = 0;
16474 	int off;
16475 
16476 	netdev_info(bp->dev, "PCI Slot Reset\n");
16477 
16478 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16479 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
16480 		msleep(900);
16481 
16482 	rtnl_lock();
16483 
16484 	if (pci_enable_device(pdev)) {
16485 		dev_err(&pdev->dev,
16486 			"Cannot re-enable PCI device after reset.\n");
16487 	} else {
16488 		pci_set_master(pdev);
16489 		/* Upon fatal error, our device internal logic that latches to
16490 		 * BAR value is getting reset and will restore only upon
16491 		 * rewriting the BARs.
16492 		 *
16493 		 * As pci_restore_state() does not re-write the BARs if the
16494 		 * value is same as saved value earlier, driver needs to
16495 		 * write the BARs to 0 to force restore, in case of fatal error.
16496 		 */
16497 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
16498 				       &bp->state)) {
16499 			for (off = PCI_BASE_ADDRESS_0;
16500 			     off <= PCI_BASE_ADDRESS_5; off += 4)
16501 				pci_write_config_dword(bp->pdev, off, 0);
16502 		}
16503 		pci_restore_state(pdev);
16504 		pci_save_state(pdev);
16505 
16506 		bnxt_inv_fw_health_reg(bp);
16507 		bnxt_try_map_fw_health_reg(bp);
16508 
16509 		/* In some PCIe AER scenarios, firmware may take up to
16510 		 * 10 seconds to become ready in the worst case.
16511 		 */
16512 		do {
16513 			err = bnxt_try_recover_fw(bp);
16514 			if (!err)
16515 				break;
16516 			retry++;
16517 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
16518 
16519 		if (err) {
16520 			dev_err(&pdev->dev, "Firmware not ready\n");
16521 			goto reset_exit;
16522 		}
16523 
16524 		err = bnxt_hwrm_func_reset(bp);
16525 		if (!err)
16526 			result = PCI_ERS_RESULT_RECOVERED;
16527 
16528 		bnxt_ulp_irq_stop(bp);
16529 		bnxt_clear_int_mode(bp);
16530 		err = bnxt_init_int_mode(bp);
16531 		bnxt_ulp_irq_restart(bp, err);
16532 	}
16533 
16534 reset_exit:
16535 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16536 	bnxt_clear_reservations(bp, true);
16537 	rtnl_unlock();
16538 
16539 	return result;
16540 }
16541 
16542 /**
16543  * bnxt_io_resume - called when traffic can start flowing again.
16544  * @pdev: Pointer to PCI device
16545  *
16546  * This callback is called when the error recovery driver tells
16547  * us that its OK to resume normal operation.
16548  */
bnxt_io_resume(struct pci_dev * pdev)16549 static void bnxt_io_resume(struct pci_dev *pdev)
16550 {
16551 	struct net_device *netdev = pci_get_drvdata(pdev);
16552 	struct bnxt *bp = netdev_priv(netdev);
16553 	int err;
16554 
16555 	netdev_info(bp->dev, "PCI Slot Resume\n");
16556 	rtnl_lock();
16557 
16558 	err = bnxt_hwrm_func_qcaps(bp);
16559 	if (!err) {
16560 		if (netif_running(netdev))
16561 			err = bnxt_open(netdev);
16562 		else
16563 			err = bnxt_reserve_rings(bp, true);
16564 	}
16565 
16566 	if (!err)
16567 		netif_device_attach(netdev);
16568 
16569 	rtnl_unlock();
16570 	bnxt_ulp_start(bp, err);
16571 	if (!err)
16572 		bnxt_reenable_sriov(bp);
16573 }
16574 
16575 static const struct pci_error_handlers bnxt_err_handler = {
16576 	.error_detected	= bnxt_io_error_detected,
16577 	.slot_reset	= bnxt_io_slot_reset,
16578 	.resume		= bnxt_io_resume
16579 };
16580 
16581 static struct pci_driver bnxt_pci_driver = {
16582 	.name		= DRV_MODULE_NAME,
16583 	.id_table	= bnxt_pci_tbl,
16584 	.probe		= bnxt_init_one,
16585 	.remove		= bnxt_remove_one,
16586 	.shutdown	= bnxt_shutdown,
16587 	.driver.pm	= BNXT_PM_OPS,
16588 	.err_handler	= &bnxt_err_handler,
16589 #if defined(CONFIG_BNXT_SRIOV)
16590 	.sriov_configure = bnxt_sriov_configure,
16591 #endif
16592 };
16593 
bnxt_init(void)16594 static int __init bnxt_init(void)
16595 {
16596 	int err;
16597 
16598 	bnxt_debug_init();
16599 	err = pci_register_driver(&bnxt_pci_driver);
16600 	if (err) {
16601 		bnxt_debug_exit();
16602 		return err;
16603 	}
16604 
16605 	return 0;
16606 }
16607 
bnxt_exit(void)16608 static void __exit bnxt_exit(void)
16609 {
16610 	pci_unregister_driver(&bnxt_pci_driver);
16611 	if (bnxt_pf_wq)
16612 		destroy_workqueue(bnxt_pf_wq);
16613 	bnxt_debug_exit();
16614 }
16615 
16616 module_init(bnxt_init);
16617 module_exit(bnxt_exit);
16618