xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision c77cd47cee041bc1664b8e5fcd23036e5aab8e2a)
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_vnic_is_active(struct bnxt * bp)2900 static bool bnxt_vnic_is_active(struct bnxt *bp)
2901 {
2902 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
2903 
2904 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
2905 }
2906 
bnxt_msix(int irq,void * dev_instance)2907 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2908 {
2909 	struct bnxt_napi *bnapi = dev_instance;
2910 	struct bnxt *bp = bnapi->bp;
2911 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2912 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2913 
2914 	cpr->event_ctr++;
2915 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2916 	napi_schedule(&bnapi->napi);
2917 	return IRQ_HANDLED;
2918 }
2919 
bnxt_has_work(struct bnxt * bp,struct bnxt_cp_ring_info * cpr)2920 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2921 {
2922 	u32 raw_cons = cpr->cp_raw_cons;
2923 	u16 cons = RING_CMP(raw_cons);
2924 	struct tx_cmp *txcmp;
2925 
2926 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2927 
2928 	return TX_CMP_VALID(txcmp, raw_cons);
2929 }
2930 
__bnxt_poll_work(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,int budget)2931 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2932 			    int budget)
2933 {
2934 	struct bnxt_napi *bnapi = cpr->bnapi;
2935 	u32 raw_cons = cpr->cp_raw_cons;
2936 	u32 cons;
2937 	int rx_pkts = 0;
2938 	u8 event = 0;
2939 	struct tx_cmp *txcmp;
2940 
2941 	cpr->has_more_work = 0;
2942 	cpr->had_work_done = 1;
2943 	while (1) {
2944 		u8 cmp_type;
2945 		int rc;
2946 
2947 		cons = RING_CMP(raw_cons);
2948 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2949 
2950 		if (!TX_CMP_VALID(txcmp, raw_cons))
2951 			break;
2952 
2953 		/* The valid test of the entry must be done first before
2954 		 * reading any further.
2955 		 */
2956 		dma_rmb();
2957 		cmp_type = TX_CMP_TYPE(txcmp);
2958 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
2959 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
2960 			u32 opaque = txcmp->tx_cmp_opaque;
2961 			struct bnxt_tx_ring_info *txr;
2962 			u16 tx_freed;
2963 
2964 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
2965 			event |= BNXT_TX_CMP_EVENT;
2966 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
2967 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
2968 			else
2969 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
2970 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
2971 				   bp->tx_ring_mask;
2972 			/* return full budget so NAPI will complete. */
2973 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
2974 				rx_pkts = budget;
2975 				raw_cons = NEXT_RAW_CMP(raw_cons);
2976 				if (budget)
2977 					cpr->has_more_work = 1;
2978 				break;
2979 			}
2980 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
2981 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
2982 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
2983 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2984 			if (likely(budget))
2985 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2986 			else
2987 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2988 							   &event);
2989 			if (likely(rc >= 0))
2990 				rx_pkts += rc;
2991 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2992 			 * the NAPI budget.  Otherwise, we may potentially loop
2993 			 * here forever if we consistently cannot allocate
2994 			 * buffers.
2995 			 */
2996 			else if (rc == -ENOMEM && budget)
2997 				rx_pkts++;
2998 			else if (rc == -EBUSY)	/* partial completion */
2999 				break;
3000 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3001 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3002 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3003 			bnxt_hwrm_handler(bp, txcmp);
3004 		}
3005 		raw_cons = NEXT_RAW_CMP(raw_cons);
3006 
3007 		if (rx_pkts && rx_pkts == budget) {
3008 			cpr->has_more_work = 1;
3009 			break;
3010 		}
3011 	}
3012 
3013 	if (event & BNXT_REDIRECT_EVENT) {
3014 		xdp_do_flush();
3015 		event &= ~BNXT_REDIRECT_EVENT;
3016 	}
3017 
3018 	if (event & BNXT_TX_EVENT) {
3019 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3020 		u16 prod = txr->tx_prod;
3021 
3022 		/* Sync BD data before updating doorbell */
3023 		wmb();
3024 
3025 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3026 		event &= ~BNXT_TX_EVENT;
3027 	}
3028 
3029 	cpr->cp_raw_cons = raw_cons;
3030 	bnapi->events |= event;
3031 	return rx_pkts;
3032 }
3033 
__bnxt_poll_work_done(struct bnxt * bp,struct bnxt_napi * bnapi,int budget)3034 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3035 				  int budget)
3036 {
3037 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3038 		bnapi->tx_int(bp, bnapi, budget);
3039 
3040 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3041 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3042 
3043 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3044 		bnapi->events &= ~BNXT_RX_EVENT;
3045 	}
3046 	if (bnapi->events & BNXT_AGG_EVENT) {
3047 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3048 
3049 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3050 		bnapi->events &= ~BNXT_AGG_EVENT;
3051 	}
3052 }
3053 
bnxt_poll_work(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,int budget)3054 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3055 			  int budget)
3056 {
3057 	struct bnxt_napi *bnapi = cpr->bnapi;
3058 	int rx_pkts;
3059 
3060 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3061 
3062 	/* ACK completion ring before freeing tx ring and producing new
3063 	 * buffers in rx/agg rings to prevent overflowing the completion
3064 	 * ring.
3065 	 */
3066 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3067 
3068 	__bnxt_poll_work_done(bp, bnapi, budget);
3069 	return rx_pkts;
3070 }
3071 
bnxt_poll_nitroa0(struct napi_struct * napi,int budget)3072 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3073 {
3074 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3075 	struct bnxt *bp = bnapi->bp;
3076 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3077 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3078 	struct tx_cmp *txcmp;
3079 	struct rx_cmp_ext *rxcmp1;
3080 	u32 cp_cons, tmp_raw_cons;
3081 	u32 raw_cons = cpr->cp_raw_cons;
3082 	bool flush_xdp = false;
3083 	u32 rx_pkts = 0;
3084 	u8 event = 0;
3085 
3086 	while (1) {
3087 		int rc;
3088 
3089 		cp_cons = RING_CMP(raw_cons);
3090 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3091 
3092 		if (!TX_CMP_VALID(txcmp, raw_cons))
3093 			break;
3094 
3095 		/* The valid test of the entry must be done first before
3096 		 * reading any further.
3097 		 */
3098 		dma_rmb();
3099 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3100 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3101 			cp_cons = RING_CMP(tmp_raw_cons);
3102 			rxcmp1 = (struct rx_cmp_ext *)
3103 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3104 
3105 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3106 				break;
3107 
3108 			/* force an error to recycle the buffer */
3109 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3110 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3111 
3112 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3113 			if (likely(rc == -EIO) && budget)
3114 				rx_pkts++;
3115 			else if (rc == -EBUSY)	/* partial completion */
3116 				break;
3117 			if (event & BNXT_REDIRECT_EVENT)
3118 				flush_xdp = true;
3119 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3120 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3121 			bnxt_hwrm_handler(bp, txcmp);
3122 		} else {
3123 			netdev_err(bp->dev,
3124 				   "Invalid completion received on special ring\n");
3125 		}
3126 		raw_cons = NEXT_RAW_CMP(raw_cons);
3127 
3128 		if (rx_pkts == budget)
3129 			break;
3130 	}
3131 
3132 	cpr->cp_raw_cons = raw_cons;
3133 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3134 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3135 
3136 	if (event & BNXT_AGG_EVENT)
3137 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3138 	if (flush_xdp)
3139 		xdp_do_flush();
3140 
3141 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3142 		napi_complete_done(napi, rx_pkts);
3143 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3144 	}
3145 	return rx_pkts;
3146 }
3147 
bnxt_poll(struct napi_struct * napi,int budget)3148 static int bnxt_poll(struct napi_struct *napi, int budget)
3149 {
3150 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3151 	struct bnxt *bp = bnapi->bp;
3152 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3153 	int work_done = 0;
3154 
3155 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3156 		napi_complete(napi);
3157 		return 0;
3158 	}
3159 	while (1) {
3160 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3161 
3162 		if (work_done >= budget) {
3163 			if (!budget)
3164 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3165 			break;
3166 		}
3167 
3168 		if (!bnxt_has_work(bp, cpr)) {
3169 			if (napi_complete_done(napi, work_done))
3170 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3171 			break;
3172 		}
3173 	}
3174 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3175 		struct dim_sample dim_sample = {};
3176 
3177 		dim_update_sample(cpr->event_ctr,
3178 				  cpr->rx_packets,
3179 				  cpr->rx_bytes,
3180 				  &dim_sample);
3181 		net_dim(&cpr->dim, &dim_sample);
3182 	}
3183 	return work_done;
3184 }
3185 
__bnxt_poll_cqs(struct bnxt * bp,struct bnxt_napi * bnapi,int budget)3186 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3187 {
3188 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3189 	int i, work_done = 0;
3190 
3191 	for (i = 0; i < cpr->cp_ring_count; i++) {
3192 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3193 
3194 		if (cpr2->had_nqe_notify) {
3195 			work_done += __bnxt_poll_work(bp, cpr2,
3196 						      budget - work_done);
3197 			cpr->has_more_work |= cpr2->has_more_work;
3198 		}
3199 	}
3200 	return work_done;
3201 }
3202 
__bnxt_poll_cqs_done(struct bnxt * bp,struct bnxt_napi * bnapi,u64 dbr_type,int budget)3203 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3204 				 u64 dbr_type, int budget)
3205 {
3206 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3207 	int i;
3208 
3209 	for (i = 0; i < cpr->cp_ring_count; i++) {
3210 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3211 		struct bnxt_db_info *db;
3212 
3213 		if (cpr2->had_work_done) {
3214 			u32 tgl = 0;
3215 
3216 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3217 				cpr2->had_nqe_notify = 0;
3218 				tgl = cpr2->toggle;
3219 			}
3220 			db = &cpr2->cp_db;
3221 			bnxt_writeq(bp,
3222 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3223 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3224 				    db->doorbell);
3225 			cpr2->had_work_done = 0;
3226 		}
3227 	}
3228 	__bnxt_poll_work_done(bp, bnapi, budget);
3229 }
3230 
bnxt_poll_p5(struct napi_struct * napi,int budget)3231 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3232 {
3233 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3234 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3235 	struct bnxt_cp_ring_info *cpr_rx;
3236 	u32 raw_cons = cpr->cp_raw_cons;
3237 	struct bnxt *bp = bnapi->bp;
3238 	struct nqe_cn *nqcmp;
3239 	int work_done = 0;
3240 	u32 cons;
3241 
3242 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3243 		napi_complete(napi);
3244 		return 0;
3245 	}
3246 	if (cpr->has_more_work) {
3247 		cpr->has_more_work = 0;
3248 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3249 	}
3250 	while (1) {
3251 		u16 type;
3252 
3253 		cons = RING_CMP(raw_cons);
3254 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3255 
3256 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3257 			if (cpr->has_more_work)
3258 				break;
3259 
3260 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3261 					     budget);
3262 			cpr->cp_raw_cons = raw_cons;
3263 			if (napi_complete_done(napi, work_done))
3264 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3265 						  cpr->cp_raw_cons);
3266 			goto poll_done;
3267 		}
3268 
3269 		/* The valid test of the entry must be done first before
3270 		 * reading any further.
3271 		 */
3272 		dma_rmb();
3273 
3274 		type = le16_to_cpu(nqcmp->type);
3275 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3276 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3277 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3278 			struct bnxt_cp_ring_info *cpr2;
3279 
3280 			/* No more budget for RX work */
3281 			if (budget && work_done >= budget &&
3282 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3283 				break;
3284 
3285 			idx = BNXT_NQ_HDL_IDX(idx);
3286 			cpr2 = &cpr->cp_ring_arr[idx];
3287 			cpr2->had_nqe_notify = 1;
3288 			cpr2->toggle = NQE_CN_TOGGLE(type);
3289 			work_done += __bnxt_poll_work(bp, cpr2,
3290 						      budget - work_done);
3291 			cpr->has_more_work |= cpr2->has_more_work;
3292 		} else {
3293 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3294 		}
3295 		raw_cons = NEXT_RAW_CMP(raw_cons);
3296 	}
3297 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3298 	if (raw_cons != cpr->cp_raw_cons) {
3299 		cpr->cp_raw_cons = raw_cons;
3300 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3301 	}
3302 poll_done:
3303 	cpr_rx = &cpr->cp_ring_arr[0];
3304 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3305 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3306 		struct dim_sample dim_sample = {};
3307 
3308 		dim_update_sample(cpr->event_ctr,
3309 				  cpr_rx->rx_packets,
3310 				  cpr_rx->rx_bytes,
3311 				  &dim_sample);
3312 		net_dim(&cpr->dim, &dim_sample);
3313 	}
3314 	return work_done;
3315 }
3316 
bnxt_free_tx_skbs(struct bnxt * bp)3317 static void bnxt_free_tx_skbs(struct bnxt *bp)
3318 {
3319 	int i, max_idx;
3320 	struct pci_dev *pdev = bp->pdev;
3321 
3322 	if (!bp->tx_ring)
3323 		return;
3324 
3325 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3326 	for (i = 0; i < bp->tx_nr_rings; i++) {
3327 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3328 		int j;
3329 
3330 		if (!txr->tx_buf_ring)
3331 			continue;
3332 
3333 		for (j = 0; j < max_idx;) {
3334 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
3335 			struct sk_buff *skb;
3336 			int k, last;
3337 
3338 			if (i < bp->tx_nr_rings_xdp &&
3339 			    tx_buf->action == XDP_REDIRECT) {
3340 				dma_unmap_single(&pdev->dev,
3341 					dma_unmap_addr(tx_buf, mapping),
3342 					dma_unmap_len(tx_buf, len),
3343 					DMA_TO_DEVICE);
3344 				xdp_return_frame(tx_buf->xdpf);
3345 				tx_buf->action = 0;
3346 				tx_buf->xdpf = NULL;
3347 				j++;
3348 				continue;
3349 			}
3350 
3351 			skb = tx_buf->skb;
3352 			if (!skb) {
3353 				j++;
3354 				continue;
3355 			}
3356 
3357 			tx_buf->skb = NULL;
3358 
3359 			if (tx_buf->is_push) {
3360 				dev_kfree_skb(skb);
3361 				j += 2;
3362 				continue;
3363 			}
3364 
3365 			dma_unmap_single(&pdev->dev,
3366 					 dma_unmap_addr(tx_buf, mapping),
3367 					 skb_headlen(skb),
3368 					 DMA_TO_DEVICE);
3369 
3370 			last = tx_buf->nr_frags;
3371 			j += 2;
3372 			for (k = 0; k < last; k++, j++) {
3373 				int ring_idx = j & bp->tx_ring_mask;
3374 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
3375 
3376 				tx_buf = &txr->tx_buf_ring[ring_idx];
3377 				dma_unmap_page(
3378 					&pdev->dev,
3379 					dma_unmap_addr(tx_buf, mapping),
3380 					skb_frag_size(frag), DMA_TO_DEVICE);
3381 			}
3382 			dev_kfree_skb(skb);
3383 		}
3384 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
3385 	}
3386 }
3387 
bnxt_free_one_rx_ring(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)3388 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3389 {
3390 	int i, max_idx;
3391 
3392 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3393 
3394 	for (i = 0; i < max_idx; i++) {
3395 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3396 		void *data = rx_buf->data;
3397 
3398 		if (!data)
3399 			continue;
3400 
3401 		rx_buf->data = NULL;
3402 		if (BNXT_RX_PAGE_MODE(bp))
3403 			page_pool_recycle_direct(rxr->page_pool, data);
3404 		else
3405 			page_pool_free_va(rxr->head_pool, data, true);
3406 	}
3407 }
3408 
bnxt_free_one_rx_agg_ring(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)3409 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3410 {
3411 	int i, max_idx;
3412 
3413 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3414 
3415 	for (i = 0; i < max_idx; i++) {
3416 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3417 		struct page *page = rx_agg_buf->page;
3418 
3419 		if (!page)
3420 			continue;
3421 
3422 		rx_agg_buf->page = NULL;
3423 		__clear_bit(i, rxr->rx_agg_bmap);
3424 
3425 		page_pool_recycle_direct(rxr->page_pool, page);
3426 	}
3427 }
3428 
bnxt_free_one_tpa_info_data(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)3429 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3430 					struct bnxt_rx_ring_info *rxr)
3431 {
3432 	int i;
3433 
3434 	for (i = 0; i < bp->max_tpa; i++) {
3435 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3436 		u8 *data = tpa_info->data;
3437 
3438 		if (!data)
3439 			continue;
3440 
3441 		tpa_info->data = NULL;
3442 		page_pool_free_va(rxr->head_pool, data, false);
3443 	}
3444 }
3445 
bnxt_free_one_rx_ring_skbs(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)3446 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3447 				       struct bnxt_rx_ring_info *rxr)
3448 {
3449 	struct bnxt_tpa_idx_map *map;
3450 
3451 	if (!rxr->rx_tpa)
3452 		goto skip_rx_tpa_free;
3453 
3454 	bnxt_free_one_tpa_info_data(bp, rxr);
3455 
3456 skip_rx_tpa_free:
3457 	if (!rxr->rx_buf_ring)
3458 		goto skip_rx_buf_free;
3459 
3460 	bnxt_free_one_rx_ring(bp, rxr);
3461 
3462 skip_rx_buf_free:
3463 	if (!rxr->rx_agg_ring)
3464 		goto skip_rx_agg_free;
3465 
3466 	bnxt_free_one_rx_agg_ring(bp, rxr);
3467 
3468 skip_rx_agg_free:
3469 	map = rxr->rx_tpa_idx_map;
3470 	if (map)
3471 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3472 }
3473 
bnxt_free_rx_skbs(struct bnxt * bp)3474 static void bnxt_free_rx_skbs(struct bnxt *bp)
3475 {
3476 	int i;
3477 
3478 	if (!bp->rx_ring)
3479 		return;
3480 
3481 	for (i = 0; i < bp->rx_nr_rings; i++)
3482 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3483 }
3484 
bnxt_free_skbs(struct bnxt * bp)3485 static void bnxt_free_skbs(struct bnxt *bp)
3486 {
3487 	bnxt_free_tx_skbs(bp);
3488 	bnxt_free_rx_skbs(bp);
3489 }
3490 
bnxt_init_ctx_mem(struct bnxt_ctx_mem_type * ctxm,void * p,int len)3491 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3492 {
3493 	u8 init_val = ctxm->init_value;
3494 	u16 offset = ctxm->init_offset;
3495 	u8 *p2 = p;
3496 	int i;
3497 
3498 	if (!init_val)
3499 		return;
3500 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3501 		memset(p, init_val, len);
3502 		return;
3503 	}
3504 	for (i = 0; i < len; i += ctxm->entry_size)
3505 		*(p2 + i + offset) = init_val;
3506 }
3507 
__bnxt_copy_ring(struct bnxt * bp,struct bnxt_ring_mem_info * rmem,void * buf,size_t offset,size_t head,size_t tail)3508 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3509 			       void *buf, size_t offset, size_t head,
3510 			       size_t tail)
3511 {
3512 	int i, head_page, start_idx, source_offset;
3513 	size_t len, rem_len, total_len, max_bytes;
3514 
3515 	head_page = head / rmem->page_size;
3516 	source_offset = head % rmem->page_size;
3517 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3518 	if (!total_len)
3519 		total_len = MAX_CTX_BYTES;
3520 	start_idx = head_page % MAX_CTX_PAGES;
3521 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3522 		    source_offset;
3523 	total_len = min(total_len, max_bytes);
3524 	rem_len = total_len;
3525 
3526 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3527 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3528 		if (buf)
3529 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3530 			       len);
3531 		offset += len;
3532 		rem_len -= len;
3533 	}
3534 	return total_len;
3535 }
3536 
bnxt_free_ring(struct bnxt * bp,struct bnxt_ring_mem_info * rmem)3537 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3538 {
3539 	struct pci_dev *pdev = bp->pdev;
3540 	int i;
3541 
3542 	if (!rmem->pg_arr)
3543 		goto skip_pages;
3544 
3545 	for (i = 0; i < rmem->nr_pages; i++) {
3546 		if (!rmem->pg_arr[i])
3547 			continue;
3548 
3549 		dma_free_coherent(&pdev->dev, rmem->page_size,
3550 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3551 
3552 		rmem->pg_arr[i] = NULL;
3553 	}
3554 skip_pages:
3555 	if (rmem->pg_tbl) {
3556 		size_t pg_tbl_size = rmem->nr_pages * 8;
3557 
3558 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3559 			pg_tbl_size = rmem->page_size;
3560 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3561 				  rmem->pg_tbl, rmem->pg_tbl_map);
3562 		rmem->pg_tbl = NULL;
3563 	}
3564 	if (rmem->vmem_size && *rmem->vmem) {
3565 		vfree(*rmem->vmem);
3566 		*rmem->vmem = NULL;
3567 	}
3568 }
3569 
bnxt_alloc_ring(struct bnxt * bp,struct bnxt_ring_mem_info * rmem)3570 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3571 {
3572 	struct pci_dev *pdev = bp->pdev;
3573 	u64 valid_bit = 0;
3574 	int i;
3575 
3576 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3577 		valid_bit = PTU_PTE_VALID;
3578 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3579 		size_t pg_tbl_size = rmem->nr_pages * 8;
3580 
3581 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3582 			pg_tbl_size = rmem->page_size;
3583 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3584 						  &rmem->pg_tbl_map,
3585 						  GFP_KERNEL);
3586 		if (!rmem->pg_tbl)
3587 			return -ENOMEM;
3588 	}
3589 
3590 	for (i = 0; i < rmem->nr_pages; i++) {
3591 		u64 extra_bits = valid_bit;
3592 
3593 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3594 						     rmem->page_size,
3595 						     &rmem->dma_arr[i],
3596 						     GFP_KERNEL);
3597 		if (!rmem->pg_arr[i])
3598 			return -ENOMEM;
3599 
3600 		if (rmem->ctx_mem)
3601 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3602 					  rmem->page_size);
3603 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3604 			if (i == rmem->nr_pages - 2 &&
3605 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3606 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3607 			else if (i == rmem->nr_pages - 1 &&
3608 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3609 				extra_bits |= PTU_PTE_LAST;
3610 			rmem->pg_tbl[i] =
3611 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3612 		}
3613 	}
3614 
3615 	if (rmem->vmem_size) {
3616 		*rmem->vmem = vzalloc(rmem->vmem_size);
3617 		if (!(*rmem->vmem))
3618 			return -ENOMEM;
3619 	}
3620 	return 0;
3621 }
3622 
bnxt_free_one_tpa_info(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)3623 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3624 				   struct bnxt_rx_ring_info *rxr)
3625 {
3626 	int i;
3627 
3628 	kfree(rxr->rx_tpa_idx_map);
3629 	rxr->rx_tpa_idx_map = NULL;
3630 	if (rxr->rx_tpa) {
3631 		for (i = 0; i < bp->max_tpa; i++) {
3632 			kfree(rxr->rx_tpa[i].agg_arr);
3633 			rxr->rx_tpa[i].agg_arr = NULL;
3634 		}
3635 	}
3636 	kfree(rxr->rx_tpa);
3637 	rxr->rx_tpa = NULL;
3638 }
3639 
bnxt_free_tpa_info(struct bnxt * bp)3640 static void bnxt_free_tpa_info(struct bnxt *bp)
3641 {
3642 	int i;
3643 
3644 	for (i = 0; i < bp->rx_nr_rings; i++) {
3645 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3646 
3647 		bnxt_free_one_tpa_info(bp, rxr);
3648 	}
3649 }
3650 
bnxt_alloc_one_tpa_info(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)3651 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3652 				   struct bnxt_rx_ring_info *rxr)
3653 {
3654 	struct rx_agg_cmp *agg;
3655 	int i;
3656 
3657 	rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3658 			      GFP_KERNEL);
3659 	if (!rxr->rx_tpa)
3660 		return -ENOMEM;
3661 
3662 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3663 		return 0;
3664 	for (i = 0; i < bp->max_tpa; i++) {
3665 		agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3666 		if (!agg)
3667 			return -ENOMEM;
3668 		rxr->rx_tpa[i].agg_arr = agg;
3669 	}
3670 	rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3671 				      GFP_KERNEL);
3672 	if (!rxr->rx_tpa_idx_map)
3673 		return -ENOMEM;
3674 
3675 	return 0;
3676 }
3677 
bnxt_alloc_tpa_info(struct bnxt * bp)3678 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3679 {
3680 	int i, rc;
3681 
3682 	bp->max_tpa = MAX_TPA;
3683 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3684 		if (!bp->max_tpa_v2)
3685 			return 0;
3686 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3687 	}
3688 
3689 	for (i = 0; i < bp->rx_nr_rings; i++) {
3690 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3691 
3692 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3693 		if (rc)
3694 			return rc;
3695 	}
3696 	return 0;
3697 }
3698 
bnxt_free_rx_rings(struct bnxt * bp)3699 static void bnxt_free_rx_rings(struct bnxt *bp)
3700 {
3701 	int i;
3702 
3703 	if (!bp->rx_ring)
3704 		return;
3705 
3706 	bnxt_free_tpa_info(bp);
3707 	for (i = 0; i < bp->rx_nr_rings; i++) {
3708 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3709 		struct bnxt_ring_struct *ring;
3710 
3711 		if (rxr->xdp_prog)
3712 			bpf_prog_put(rxr->xdp_prog);
3713 
3714 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3715 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3716 
3717 		page_pool_destroy(rxr->page_pool);
3718 		if (bnxt_separate_head_pool())
3719 			page_pool_destroy(rxr->head_pool);
3720 		rxr->page_pool = rxr->head_pool = NULL;
3721 
3722 		kfree(rxr->rx_agg_bmap);
3723 		rxr->rx_agg_bmap = NULL;
3724 
3725 		ring = &rxr->rx_ring_struct;
3726 		bnxt_free_ring(bp, &ring->ring_mem);
3727 
3728 		ring = &rxr->rx_agg_ring_struct;
3729 		bnxt_free_ring(bp, &ring->ring_mem);
3730 	}
3731 }
3732 
bnxt_alloc_rx_page_pool(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,int numa_node)3733 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3734 				   struct bnxt_rx_ring_info *rxr,
3735 				   int numa_node)
3736 {
3737 	struct page_pool_params pp = { 0 };
3738 	struct page_pool *pool;
3739 
3740 	pp.pool_size = bp->rx_agg_ring_size;
3741 	if (BNXT_RX_PAGE_MODE(bp))
3742 		pp.pool_size += bp->rx_ring_size;
3743 	pp.nid = numa_node;
3744 	pp.napi = &rxr->bnapi->napi;
3745 	pp.netdev = bp->dev;
3746 	pp.dev = &bp->pdev->dev;
3747 	pp.dma_dir = bp->rx_dir;
3748 	pp.max_len = PAGE_SIZE;
3749 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3750 
3751 	pool = page_pool_create(&pp);
3752 	if (IS_ERR(pool))
3753 		return PTR_ERR(pool);
3754 	rxr->page_pool = pool;
3755 
3756 	if (bnxt_separate_head_pool()) {
3757 		pp.pool_size = max(bp->rx_ring_size, 1024);
3758 		pool = page_pool_create(&pp);
3759 		if (IS_ERR(pool))
3760 			goto err_destroy_pp;
3761 	}
3762 	rxr->head_pool = pool;
3763 
3764 	return 0;
3765 
3766 err_destroy_pp:
3767 	page_pool_destroy(rxr->page_pool);
3768 	rxr->page_pool = NULL;
3769 	return PTR_ERR(pool);
3770 }
3771 
bnxt_alloc_rx_agg_bmap(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)3772 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3773 {
3774 	u16 mem_size;
3775 
3776 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3777 	mem_size = rxr->rx_agg_bmap_size / 8;
3778 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3779 	if (!rxr->rx_agg_bmap)
3780 		return -ENOMEM;
3781 
3782 	return 0;
3783 }
3784 
bnxt_alloc_rx_rings(struct bnxt * bp)3785 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3786 {
3787 	int numa_node = dev_to_node(&bp->pdev->dev);
3788 	int i, rc = 0, agg_rings = 0, cpu;
3789 
3790 	if (!bp->rx_ring)
3791 		return -ENOMEM;
3792 
3793 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3794 		agg_rings = 1;
3795 
3796 	for (i = 0; i < bp->rx_nr_rings; i++) {
3797 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3798 		struct bnxt_ring_struct *ring;
3799 		int cpu_node;
3800 
3801 		ring = &rxr->rx_ring_struct;
3802 
3803 		cpu = cpumask_local_spread(i, numa_node);
3804 		cpu_node = cpu_to_node(cpu);
3805 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3806 			   i, cpu_node);
3807 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3808 		if (rc)
3809 			return rc;
3810 
3811 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3812 		if (rc < 0)
3813 			return rc;
3814 
3815 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3816 						MEM_TYPE_PAGE_POOL,
3817 						rxr->page_pool);
3818 		if (rc) {
3819 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3820 			return rc;
3821 		}
3822 
3823 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3824 		if (rc)
3825 			return rc;
3826 
3827 		ring->grp_idx = i;
3828 		if (agg_rings) {
3829 			ring = &rxr->rx_agg_ring_struct;
3830 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3831 			if (rc)
3832 				return rc;
3833 
3834 			ring->grp_idx = i;
3835 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3836 			if (rc)
3837 				return rc;
3838 		}
3839 	}
3840 	if (bp->flags & BNXT_FLAG_TPA)
3841 		rc = bnxt_alloc_tpa_info(bp);
3842 	return rc;
3843 }
3844 
bnxt_free_tx_rings(struct bnxt * bp)3845 static void bnxt_free_tx_rings(struct bnxt *bp)
3846 {
3847 	int i;
3848 	struct pci_dev *pdev = bp->pdev;
3849 
3850 	if (!bp->tx_ring)
3851 		return;
3852 
3853 	for (i = 0; i < bp->tx_nr_rings; i++) {
3854 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3855 		struct bnxt_ring_struct *ring;
3856 
3857 		if (txr->tx_push) {
3858 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3859 					  txr->tx_push, txr->tx_push_mapping);
3860 			txr->tx_push = NULL;
3861 		}
3862 
3863 		ring = &txr->tx_ring_struct;
3864 
3865 		bnxt_free_ring(bp, &ring->ring_mem);
3866 	}
3867 }
3868 
3869 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3870 	((tc) * (bp)->tx_nr_rings_per_tc)
3871 
3872 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3873 	((tx) % (bp)->tx_nr_rings_per_tc)
3874 
3875 #define BNXT_RING_TO_TC(bp, tx)		\
3876 	((tx) / (bp)->tx_nr_rings_per_tc)
3877 
bnxt_alloc_tx_rings(struct bnxt * bp)3878 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3879 {
3880 	int i, j, rc;
3881 	struct pci_dev *pdev = bp->pdev;
3882 
3883 	bp->tx_push_size = 0;
3884 	if (bp->tx_push_thresh) {
3885 		int push_size;
3886 
3887 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3888 					bp->tx_push_thresh);
3889 
3890 		if (push_size > 256) {
3891 			push_size = 0;
3892 			bp->tx_push_thresh = 0;
3893 		}
3894 
3895 		bp->tx_push_size = push_size;
3896 	}
3897 
3898 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3899 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3900 		struct bnxt_ring_struct *ring;
3901 		u8 qidx;
3902 
3903 		ring = &txr->tx_ring_struct;
3904 
3905 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3906 		if (rc)
3907 			return rc;
3908 
3909 		ring->grp_idx = txr->bnapi->index;
3910 		if (bp->tx_push_size) {
3911 			dma_addr_t mapping;
3912 
3913 			/* One pre-allocated DMA buffer to backup
3914 			 * TX push operation
3915 			 */
3916 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3917 						bp->tx_push_size,
3918 						&txr->tx_push_mapping,
3919 						GFP_KERNEL);
3920 
3921 			if (!txr->tx_push)
3922 				return -ENOMEM;
3923 
3924 			mapping = txr->tx_push_mapping +
3925 				sizeof(struct tx_push_bd);
3926 			txr->data_mapping = cpu_to_le64(mapping);
3927 		}
3928 		qidx = bp->tc_to_qidx[j];
3929 		ring->queue_id = bp->q_info[qidx].queue_id;
3930 		spin_lock_init(&txr->xdp_tx_lock);
3931 		if (i < bp->tx_nr_rings_xdp)
3932 			continue;
3933 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
3934 			j++;
3935 	}
3936 	return 0;
3937 }
3938 
bnxt_free_cp_arrays(struct bnxt_cp_ring_info * cpr)3939 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3940 {
3941 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3942 
3943 	kfree(cpr->cp_desc_ring);
3944 	cpr->cp_desc_ring = NULL;
3945 	ring->ring_mem.pg_arr = NULL;
3946 	kfree(cpr->cp_desc_mapping);
3947 	cpr->cp_desc_mapping = NULL;
3948 	ring->ring_mem.dma_arr = NULL;
3949 }
3950 
bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info * cpr,int n)3951 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3952 {
3953 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3954 	if (!cpr->cp_desc_ring)
3955 		return -ENOMEM;
3956 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3957 				       GFP_KERNEL);
3958 	if (!cpr->cp_desc_mapping)
3959 		return -ENOMEM;
3960 	return 0;
3961 }
3962 
bnxt_free_all_cp_arrays(struct bnxt * bp)3963 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3964 {
3965 	int i;
3966 
3967 	if (!bp->bnapi)
3968 		return;
3969 	for (i = 0; i < bp->cp_nr_rings; i++) {
3970 		struct bnxt_napi *bnapi = bp->bnapi[i];
3971 
3972 		if (!bnapi)
3973 			continue;
3974 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3975 	}
3976 }
3977 
bnxt_alloc_all_cp_arrays(struct bnxt * bp)3978 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3979 {
3980 	int i, n = bp->cp_nr_pages;
3981 
3982 	for (i = 0; i < bp->cp_nr_rings; i++) {
3983 		struct bnxt_napi *bnapi = bp->bnapi[i];
3984 		int rc;
3985 
3986 		if (!bnapi)
3987 			continue;
3988 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3989 		if (rc)
3990 			return rc;
3991 	}
3992 	return 0;
3993 }
3994 
bnxt_free_cp_rings(struct bnxt * bp)3995 static void bnxt_free_cp_rings(struct bnxt *bp)
3996 {
3997 	int i;
3998 
3999 	if (!bp->bnapi)
4000 		return;
4001 
4002 	for (i = 0; i < bp->cp_nr_rings; i++) {
4003 		struct bnxt_napi *bnapi = bp->bnapi[i];
4004 		struct bnxt_cp_ring_info *cpr;
4005 		struct bnxt_ring_struct *ring;
4006 		int j;
4007 
4008 		if (!bnapi)
4009 			continue;
4010 
4011 		cpr = &bnapi->cp_ring;
4012 		ring = &cpr->cp_ring_struct;
4013 
4014 		bnxt_free_ring(bp, &ring->ring_mem);
4015 
4016 		if (!cpr->cp_ring_arr)
4017 			continue;
4018 
4019 		for (j = 0; j < cpr->cp_ring_count; j++) {
4020 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4021 
4022 			ring = &cpr2->cp_ring_struct;
4023 			bnxt_free_ring(bp, &ring->ring_mem);
4024 			bnxt_free_cp_arrays(cpr2);
4025 		}
4026 		kfree(cpr->cp_ring_arr);
4027 		cpr->cp_ring_arr = NULL;
4028 		cpr->cp_ring_count = 0;
4029 	}
4030 }
4031 
bnxt_alloc_cp_sub_ring(struct bnxt * bp,struct bnxt_cp_ring_info * cpr)4032 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4033 				  struct bnxt_cp_ring_info *cpr)
4034 {
4035 	struct bnxt_ring_mem_info *rmem;
4036 	struct bnxt_ring_struct *ring;
4037 	int rc;
4038 
4039 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4040 	if (rc) {
4041 		bnxt_free_cp_arrays(cpr);
4042 		return -ENOMEM;
4043 	}
4044 	ring = &cpr->cp_ring_struct;
4045 	rmem = &ring->ring_mem;
4046 	rmem->nr_pages = bp->cp_nr_pages;
4047 	rmem->page_size = HW_CMPD_RING_SIZE;
4048 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4049 	rmem->dma_arr = cpr->cp_desc_mapping;
4050 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4051 	rc = bnxt_alloc_ring(bp, rmem);
4052 	if (rc) {
4053 		bnxt_free_ring(bp, rmem);
4054 		bnxt_free_cp_arrays(cpr);
4055 	}
4056 	return rc;
4057 }
4058 
bnxt_alloc_cp_rings(struct bnxt * bp)4059 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4060 {
4061 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4062 	int i, j, rc, ulp_msix;
4063 	int tcs = bp->num_tc;
4064 
4065 	if (!tcs)
4066 		tcs = 1;
4067 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4068 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4069 		struct bnxt_napi *bnapi = bp->bnapi[i];
4070 		struct bnxt_cp_ring_info *cpr, *cpr2;
4071 		struct bnxt_ring_struct *ring;
4072 		int cp_count = 0, k;
4073 		int rx = 0, tx = 0;
4074 
4075 		if (!bnapi)
4076 			continue;
4077 
4078 		cpr = &bnapi->cp_ring;
4079 		cpr->bnapi = bnapi;
4080 		ring = &cpr->cp_ring_struct;
4081 
4082 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4083 		if (rc)
4084 			return rc;
4085 
4086 		ring->map_idx = ulp_msix + i;
4087 
4088 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4089 			continue;
4090 
4091 		if (i < bp->rx_nr_rings) {
4092 			cp_count++;
4093 			rx = 1;
4094 		}
4095 		if (i < bp->tx_nr_rings_xdp) {
4096 			cp_count++;
4097 			tx = 1;
4098 		} else if ((sh && i < bp->tx_nr_rings) ||
4099 			 (!sh && i >= bp->rx_nr_rings)) {
4100 			cp_count += tcs;
4101 			tx = 1;
4102 		}
4103 
4104 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
4105 					   GFP_KERNEL);
4106 		if (!cpr->cp_ring_arr)
4107 			return -ENOMEM;
4108 		cpr->cp_ring_count = cp_count;
4109 
4110 		for (k = 0; k < cp_count; k++) {
4111 			cpr2 = &cpr->cp_ring_arr[k];
4112 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4113 			if (rc)
4114 				return rc;
4115 			cpr2->bnapi = bnapi;
4116 			cpr2->sw_stats = cpr->sw_stats;
4117 			cpr2->cp_idx = k;
4118 			if (!k && rx) {
4119 				bp->rx_ring[i].rx_cpr = cpr2;
4120 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4121 			} else {
4122 				int n, tc = k - rx;
4123 
4124 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4125 				bp->tx_ring[n].tx_cpr = cpr2;
4126 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4127 			}
4128 		}
4129 		if (tx)
4130 			j++;
4131 	}
4132 	return 0;
4133 }
4134 
bnxt_init_rx_ring_struct(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)4135 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4136 				     struct bnxt_rx_ring_info *rxr)
4137 {
4138 	struct bnxt_ring_mem_info *rmem;
4139 	struct bnxt_ring_struct *ring;
4140 
4141 	ring = &rxr->rx_ring_struct;
4142 	rmem = &ring->ring_mem;
4143 	rmem->nr_pages = bp->rx_nr_pages;
4144 	rmem->page_size = HW_RXBD_RING_SIZE;
4145 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4146 	rmem->dma_arr = rxr->rx_desc_mapping;
4147 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4148 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4149 
4150 	ring = &rxr->rx_agg_ring_struct;
4151 	rmem = &ring->ring_mem;
4152 	rmem->nr_pages = bp->rx_agg_nr_pages;
4153 	rmem->page_size = HW_RXBD_RING_SIZE;
4154 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4155 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4156 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4157 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4158 }
4159 
bnxt_reset_rx_ring_struct(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)4160 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4161 				      struct bnxt_rx_ring_info *rxr)
4162 {
4163 	struct bnxt_ring_mem_info *rmem;
4164 	struct bnxt_ring_struct *ring;
4165 	int i;
4166 
4167 	rxr->page_pool->p.napi = NULL;
4168 	rxr->page_pool = NULL;
4169 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4170 
4171 	ring = &rxr->rx_ring_struct;
4172 	rmem = &ring->ring_mem;
4173 	rmem->pg_tbl = NULL;
4174 	rmem->pg_tbl_map = 0;
4175 	for (i = 0; i < rmem->nr_pages; i++) {
4176 		rmem->pg_arr[i] = NULL;
4177 		rmem->dma_arr[i] = 0;
4178 	}
4179 	*rmem->vmem = NULL;
4180 
4181 	ring = &rxr->rx_agg_ring_struct;
4182 	rmem = &ring->ring_mem;
4183 	rmem->pg_tbl = NULL;
4184 	rmem->pg_tbl_map = 0;
4185 	for (i = 0; i < rmem->nr_pages; i++) {
4186 		rmem->pg_arr[i] = NULL;
4187 		rmem->dma_arr[i] = 0;
4188 	}
4189 	*rmem->vmem = NULL;
4190 }
4191 
bnxt_init_ring_struct(struct bnxt * bp)4192 static void bnxt_init_ring_struct(struct bnxt *bp)
4193 {
4194 	int i, j;
4195 
4196 	for (i = 0; i < bp->cp_nr_rings; i++) {
4197 		struct bnxt_napi *bnapi = bp->bnapi[i];
4198 		struct bnxt_ring_mem_info *rmem;
4199 		struct bnxt_cp_ring_info *cpr;
4200 		struct bnxt_rx_ring_info *rxr;
4201 		struct bnxt_tx_ring_info *txr;
4202 		struct bnxt_ring_struct *ring;
4203 
4204 		if (!bnapi)
4205 			continue;
4206 
4207 		cpr = &bnapi->cp_ring;
4208 		ring = &cpr->cp_ring_struct;
4209 		rmem = &ring->ring_mem;
4210 		rmem->nr_pages = bp->cp_nr_pages;
4211 		rmem->page_size = HW_CMPD_RING_SIZE;
4212 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4213 		rmem->dma_arr = cpr->cp_desc_mapping;
4214 		rmem->vmem_size = 0;
4215 
4216 		rxr = bnapi->rx_ring;
4217 		if (!rxr)
4218 			goto skip_rx;
4219 
4220 		ring = &rxr->rx_ring_struct;
4221 		rmem = &ring->ring_mem;
4222 		rmem->nr_pages = bp->rx_nr_pages;
4223 		rmem->page_size = HW_RXBD_RING_SIZE;
4224 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4225 		rmem->dma_arr = rxr->rx_desc_mapping;
4226 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4227 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4228 
4229 		ring = &rxr->rx_agg_ring_struct;
4230 		rmem = &ring->ring_mem;
4231 		rmem->nr_pages = bp->rx_agg_nr_pages;
4232 		rmem->page_size = HW_RXBD_RING_SIZE;
4233 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4234 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4235 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4236 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4237 
4238 skip_rx:
4239 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4240 			ring = &txr->tx_ring_struct;
4241 			rmem = &ring->ring_mem;
4242 			rmem->nr_pages = bp->tx_nr_pages;
4243 			rmem->page_size = HW_TXBD_RING_SIZE;
4244 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4245 			rmem->dma_arr = txr->tx_desc_mapping;
4246 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4247 			rmem->vmem = (void **)&txr->tx_buf_ring;
4248 		}
4249 	}
4250 }
4251 
bnxt_init_rxbd_pages(struct bnxt_ring_struct * ring,u32 type)4252 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4253 {
4254 	int i;
4255 	u32 prod;
4256 	struct rx_bd **rx_buf_ring;
4257 
4258 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4259 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4260 		int j;
4261 		struct rx_bd *rxbd;
4262 
4263 		rxbd = rx_buf_ring[i];
4264 		if (!rxbd)
4265 			continue;
4266 
4267 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4268 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4269 			rxbd->rx_bd_opaque = prod;
4270 		}
4271 	}
4272 }
4273 
bnxt_alloc_one_rx_ring_skb(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,int ring_nr)4274 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4275 				       struct bnxt_rx_ring_info *rxr,
4276 				       int ring_nr)
4277 {
4278 	u32 prod;
4279 	int i;
4280 
4281 	prod = rxr->rx_prod;
4282 	for (i = 0; i < bp->rx_ring_size; i++) {
4283 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4284 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4285 				    ring_nr, i, bp->rx_ring_size);
4286 			break;
4287 		}
4288 		prod = NEXT_RX(prod);
4289 	}
4290 	rxr->rx_prod = prod;
4291 }
4292 
bnxt_alloc_one_rx_ring_page(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,int ring_nr)4293 static void bnxt_alloc_one_rx_ring_page(struct bnxt *bp,
4294 					struct bnxt_rx_ring_info *rxr,
4295 					int ring_nr)
4296 {
4297 	u32 prod;
4298 	int i;
4299 
4300 	prod = rxr->rx_agg_prod;
4301 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4302 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
4303 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4304 				    ring_nr, i, bp->rx_ring_size);
4305 			break;
4306 		}
4307 		prod = NEXT_RX_AGG(prod);
4308 	}
4309 	rxr->rx_agg_prod = prod;
4310 }
4311 
bnxt_alloc_one_tpa_info_data(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)4312 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4313 					struct bnxt_rx_ring_info *rxr)
4314 {
4315 	dma_addr_t mapping;
4316 	u8 *data;
4317 	int i;
4318 
4319 	for (i = 0; i < bp->max_tpa; i++) {
4320 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4321 					    GFP_KERNEL);
4322 		if (!data)
4323 			return -ENOMEM;
4324 
4325 		rxr->rx_tpa[i].data = data;
4326 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4327 		rxr->rx_tpa[i].mapping = mapping;
4328 	}
4329 
4330 	return 0;
4331 }
4332 
bnxt_alloc_one_rx_ring(struct bnxt * bp,int ring_nr)4333 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4334 {
4335 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4336 	int rc;
4337 
4338 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4339 
4340 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4341 		return 0;
4342 
4343 	bnxt_alloc_one_rx_ring_page(bp, rxr, ring_nr);
4344 
4345 	if (rxr->rx_tpa) {
4346 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4347 		if (rc)
4348 			return rc;
4349 	}
4350 	return 0;
4351 }
4352 
bnxt_init_one_rx_ring_rxbd(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)4353 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4354 				       struct bnxt_rx_ring_info *rxr)
4355 {
4356 	struct bnxt_ring_struct *ring;
4357 	u32 type;
4358 
4359 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4360 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4361 
4362 	if (NET_IP_ALIGN == 2)
4363 		type |= RX_BD_FLAGS_SOP;
4364 
4365 	ring = &rxr->rx_ring_struct;
4366 	bnxt_init_rxbd_pages(ring, type);
4367 	ring->fw_ring_id = INVALID_HW_RING_ID;
4368 }
4369 
bnxt_init_one_rx_agg_ring_rxbd(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)4370 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4371 					   struct bnxt_rx_ring_info *rxr)
4372 {
4373 	struct bnxt_ring_struct *ring;
4374 	u32 type;
4375 
4376 	ring = &rxr->rx_agg_ring_struct;
4377 	ring->fw_ring_id = INVALID_HW_RING_ID;
4378 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4379 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4380 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4381 
4382 		bnxt_init_rxbd_pages(ring, type);
4383 	}
4384 }
4385 
bnxt_init_one_rx_ring(struct bnxt * bp,int ring_nr)4386 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4387 {
4388 	struct bnxt_rx_ring_info *rxr;
4389 
4390 	rxr = &bp->rx_ring[ring_nr];
4391 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4392 
4393 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4394 			     &rxr->bnapi->napi);
4395 
4396 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4397 		bpf_prog_add(bp->xdp_prog, 1);
4398 		rxr->xdp_prog = bp->xdp_prog;
4399 	}
4400 
4401 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4402 
4403 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4404 }
4405 
bnxt_init_cp_rings(struct bnxt * bp)4406 static void bnxt_init_cp_rings(struct bnxt *bp)
4407 {
4408 	int i, j;
4409 
4410 	for (i = 0; i < bp->cp_nr_rings; i++) {
4411 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4412 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4413 
4414 		ring->fw_ring_id = INVALID_HW_RING_ID;
4415 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4416 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4417 		if (!cpr->cp_ring_arr)
4418 			continue;
4419 		for (j = 0; j < cpr->cp_ring_count; j++) {
4420 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4421 
4422 			ring = &cpr2->cp_ring_struct;
4423 			ring->fw_ring_id = INVALID_HW_RING_ID;
4424 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4425 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4426 		}
4427 	}
4428 }
4429 
bnxt_init_rx_rings(struct bnxt * bp)4430 static int bnxt_init_rx_rings(struct bnxt *bp)
4431 {
4432 	int i, rc = 0;
4433 
4434 	if (BNXT_RX_PAGE_MODE(bp)) {
4435 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4436 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4437 	} else {
4438 		bp->rx_offset = BNXT_RX_OFFSET;
4439 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4440 	}
4441 
4442 	for (i = 0; i < bp->rx_nr_rings; i++) {
4443 		rc = bnxt_init_one_rx_ring(bp, i);
4444 		if (rc)
4445 			break;
4446 	}
4447 
4448 	return rc;
4449 }
4450 
bnxt_init_tx_rings(struct bnxt * bp)4451 static int bnxt_init_tx_rings(struct bnxt *bp)
4452 {
4453 	u16 i;
4454 
4455 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4456 				   BNXT_MIN_TX_DESC_CNT);
4457 
4458 	for (i = 0; i < bp->tx_nr_rings; i++) {
4459 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4460 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4461 
4462 		ring->fw_ring_id = INVALID_HW_RING_ID;
4463 
4464 		if (i >= bp->tx_nr_rings_xdp)
4465 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4466 					     NETDEV_QUEUE_TYPE_TX,
4467 					     &txr->bnapi->napi);
4468 	}
4469 
4470 	return 0;
4471 }
4472 
bnxt_free_ring_grps(struct bnxt * bp)4473 static void bnxt_free_ring_grps(struct bnxt *bp)
4474 {
4475 	kfree(bp->grp_info);
4476 	bp->grp_info = NULL;
4477 }
4478 
bnxt_init_ring_grps(struct bnxt * bp,bool irq_re_init)4479 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4480 {
4481 	int i;
4482 
4483 	if (irq_re_init) {
4484 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4485 				       sizeof(struct bnxt_ring_grp_info),
4486 				       GFP_KERNEL);
4487 		if (!bp->grp_info)
4488 			return -ENOMEM;
4489 	}
4490 	for (i = 0; i < bp->cp_nr_rings; i++) {
4491 		if (irq_re_init)
4492 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4493 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4494 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4495 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4496 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4497 	}
4498 	return 0;
4499 }
4500 
bnxt_free_vnics(struct bnxt * bp)4501 static void bnxt_free_vnics(struct bnxt *bp)
4502 {
4503 	kfree(bp->vnic_info);
4504 	bp->vnic_info = NULL;
4505 	bp->nr_vnics = 0;
4506 }
4507 
bnxt_alloc_vnics(struct bnxt * bp)4508 static int bnxt_alloc_vnics(struct bnxt *bp)
4509 {
4510 	int num_vnics = 1;
4511 
4512 #ifdef CONFIG_RFS_ACCEL
4513 	if (bp->flags & BNXT_FLAG_RFS) {
4514 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4515 			num_vnics++;
4516 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4517 			num_vnics += bp->rx_nr_rings;
4518 	}
4519 #endif
4520 
4521 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4522 		num_vnics++;
4523 
4524 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4525 				GFP_KERNEL);
4526 	if (!bp->vnic_info)
4527 		return -ENOMEM;
4528 
4529 	bp->nr_vnics = num_vnics;
4530 	return 0;
4531 }
4532 
bnxt_init_vnics(struct bnxt * bp)4533 static void bnxt_init_vnics(struct bnxt *bp)
4534 {
4535 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4536 	int i;
4537 
4538 	for (i = 0; i < bp->nr_vnics; i++) {
4539 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4540 		int j;
4541 
4542 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4543 		vnic->vnic_id = i;
4544 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4545 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4546 
4547 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4548 
4549 		if (bp->vnic_info[i].rss_hash_key) {
4550 			if (i == BNXT_VNIC_DEFAULT) {
4551 				u8 *key = (void *)vnic->rss_hash_key;
4552 				int k;
4553 
4554 				if (!bp->rss_hash_key_valid &&
4555 				    !bp->rss_hash_key_updated) {
4556 					get_random_bytes(bp->rss_hash_key,
4557 							 HW_HASH_KEY_SIZE);
4558 					bp->rss_hash_key_updated = true;
4559 				}
4560 
4561 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4562 				       HW_HASH_KEY_SIZE);
4563 
4564 				if (!bp->rss_hash_key_updated)
4565 					continue;
4566 
4567 				bp->rss_hash_key_updated = false;
4568 				bp->rss_hash_key_valid = true;
4569 
4570 				bp->toeplitz_prefix = 0;
4571 				for (k = 0; k < 8; k++) {
4572 					bp->toeplitz_prefix <<= 8;
4573 					bp->toeplitz_prefix |= key[k];
4574 				}
4575 			} else {
4576 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4577 				       HW_HASH_KEY_SIZE);
4578 			}
4579 		}
4580 	}
4581 }
4582 
bnxt_calc_nr_ring_pages(u32 ring_size,int desc_per_pg)4583 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4584 {
4585 	int pages;
4586 
4587 	pages = ring_size / desc_per_pg;
4588 
4589 	if (!pages)
4590 		return 1;
4591 
4592 	pages++;
4593 
4594 	while (pages & (pages - 1))
4595 		pages++;
4596 
4597 	return pages;
4598 }
4599 
bnxt_set_tpa_flags(struct bnxt * bp)4600 void bnxt_set_tpa_flags(struct bnxt *bp)
4601 {
4602 	bp->flags &= ~BNXT_FLAG_TPA;
4603 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4604 		return;
4605 	if (bp->dev->features & NETIF_F_LRO)
4606 		bp->flags |= BNXT_FLAG_LRO;
4607 	else if (bp->dev->features & NETIF_F_GRO_HW)
4608 		bp->flags |= BNXT_FLAG_GRO;
4609 }
4610 
4611 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4612  * be set on entry.
4613  */
bnxt_set_ring_params(struct bnxt * bp)4614 void bnxt_set_ring_params(struct bnxt *bp)
4615 {
4616 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4617 	u32 agg_factor = 0, agg_ring_size = 0;
4618 
4619 	/* 8 for CRC and VLAN */
4620 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4621 
4622 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4623 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4624 
4625 	bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
4626 	ring_size = bp->rx_ring_size;
4627 	bp->rx_agg_ring_size = 0;
4628 	bp->rx_agg_nr_pages = 0;
4629 
4630 	if (bp->flags & BNXT_FLAG_TPA)
4631 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4632 
4633 	bp->flags &= ~BNXT_FLAG_JUMBO;
4634 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4635 		u32 jumbo_factor;
4636 
4637 		bp->flags |= BNXT_FLAG_JUMBO;
4638 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4639 		if (jumbo_factor > agg_factor)
4640 			agg_factor = jumbo_factor;
4641 	}
4642 	if (agg_factor) {
4643 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4644 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4645 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4646 				    bp->rx_ring_size, ring_size);
4647 			bp->rx_ring_size = ring_size;
4648 		}
4649 		agg_ring_size = ring_size * agg_factor;
4650 
4651 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4652 							RX_DESC_CNT);
4653 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4654 			u32 tmp = agg_ring_size;
4655 
4656 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4657 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4658 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4659 				    tmp, agg_ring_size);
4660 		}
4661 		bp->rx_agg_ring_size = agg_ring_size;
4662 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4663 
4664 		if (BNXT_RX_PAGE_MODE(bp)) {
4665 			rx_space = PAGE_SIZE;
4666 			rx_size = PAGE_SIZE -
4667 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4668 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4669 		} else {
4670 			rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
4671 			rx_space = rx_size + NET_SKB_PAD +
4672 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4673 		}
4674 	}
4675 
4676 	bp->rx_buf_use_size = rx_size;
4677 	bp->rx_buf_size = rx_space;
4678 
4679 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4680 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4681 
4682 	ring_size = bp->tx_ring_size;
4683 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4684 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4685 
4686 	max_rx_cmpl = bp->rx_ring_size;
4687 	/* MAX TPA needs to be added because TPA_START completions are
4688 	 * immediately recycled, so the TPA completions are not bound by
4689 	 * the RX ring size.
4690 	 */
4691 	if (bp->flags & BNXT_FLAG_TPA)
4692 		max_rx_cmpl += bp->max_tpa;
4693 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4694 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4695 	bp->cp_ring_size = ring_size;
4696 
4697 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4698 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4699 		bp->cp_nr_pages = MAX_CP_PAGES;
4700 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4701 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4702 			    ring_size, bp->cp_ring_size);
4703 	}
4704 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4705 	bp->cp_ring_mask = bp->cp_bit - 1;
4706 }
4707 
4708 /* Changing allocation mode of RX rings.
4709  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4710  */
bnxt_set_rx_skb_mode(struct bnxt * bp,bool page_mode)4711 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4712 {
4713 	struct net_device *dev = bp->dev;
4714 
4715 	if (page_mode) {
4716 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4717 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4718 
4719 		if (bp->xdp_prog->aux->xdp_has_frags)
4720 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4721 		else
4722 			dev->max_mtu =
4723 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4724 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4725 			bp->flags |= BNXT_FLAG_JUMBO;
4726 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4727 		} else {
4728 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4729 			bp->rx_skb_func = bnxt_rx_page_skb;
4730 		}
4731 		bp->rx_dir = DMA_BIDIRECTIONAL;
4732 		/* Disable LRO or GRO_HW */
4733 		netdev_update_features(dev);
4734 	} else {
4735 		dev->max_mtu = bp->max_mtu;
4736 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4737 		bp->rx_dir = DMA_FROM_DEVICE;
4738 		bp->rx_skb_func = bnxt_rx_skb;
4739 	}
4740 	return 0;
4741 }
4742 
bnxt_free_vnic_attributes(struct bnxt * bp)4743 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4744 {
4745 	int i;
4746 	struct bnxt_vnic_info *vnic;
4747 	struct pci_dev *pdev = bp->pdev;
4748 
4749 	if (!bp->vnic_info)
4750 		return;
4751 
4752 	for (i = 0; i < bp->nr_vnics; i++) {
4753 		vnic = &bp->vnic_info[i];
4754 
4755 		kfree(vnic->fw_grp_ids);
4756 		vnic->fw_grp_ids = NULL;
4757 
4758 		kfree(vnic->uc_list);
4759 		vnic->uc_list = NULL;
4760 
4761 		if (vnic->mc_list) {
4762 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4763 					  vnic->mc_list, vnic->mc_list_mapping);
4764 			vnic->mc_list = NULL;
4765 		}
4766 
4767 		if (vnic->rss_table) {
4768 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4769 					  vnic->rss_table,
4770 					  vnic->rss_table_dma_addr);
4771 			vnic->rss_table = NULL;
4772 		}
4773 
4774 		vnic->rss_hash_key = NULL;
4775 		vnic->flags = 0;
4776 	}
4777 }
4778 
bnxt_alloc_vnic_attributes(struct bnxt * bp)4779 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4780 {
4781 	int i, rc = 0, size;
4782 	struct bnxt_vnic_info *vnic;
4783 	struct pci_dev *pdev = bp->pdev;
4784 	int max_rings;
4785 
4786 	for (i = 0; i < bp->nr_vnics; i++) {
4787 		vnic = &bp->vnic_info[i];
4788 
4789 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4790 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4791 
4792 			if (mem_size > 0) {
4793 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4794 				if (!vnic->uc_list) {
4795 					rc = -ENOMEM;
4796 					goto out;
4797 				}
4798 			}
4799 		}
4800 
4801 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4802 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4803 			vnic->mc_list =
4804 				dma_alloc_coherent(&pdev->dev,
4805 						   vnic->mc_list_size,
4806 						   &vnic->mc_list_mapping,
4807 						   GFP_KERNEL);
4808 			if (!vnic->mc_list) {
4809 				rc = -ENOMEM;
4810 				goto out;
4811 			}
4812 		}
4813 
4814 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4815 			goto vnic_skip_grps;
4816 
4817 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4818 			max_rings = bp->rx_nr_rings;
4819 		else
4820 			max_rings = 1;
4821 
4822 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4823 		if (!vnic->fw_grp_ids) {
4824 			rc = -ENOMEM;
4825 			goto out;
4826 		}
4827 vnic_skip_grps:
4828 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4829 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4830 			continue;
4831 
4832 		/* Allocate rss table and hash key */
4833 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4834 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4835 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4836 
4837 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4838 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4839 						     vnic->rss_table_size,
4840 						     &vnic->rss_table_dma_addr,
4841 						     GFP_KERNEL);
4842 		if (!vnic->rss_table) {
4843 			rc = -ENOMEM;
4844 			goto out;
4845 		}
4846 
4847 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4848 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4849 	}
4850 	return 0;
4851 
4852 out:
4853 	return rc;
4854 }
4855 
bnxt_free_hwrm_resources(struct bnxt * bp)4856 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4857 {
4858 	struct bnxt_hwrm_wait_token *token;
4859 
4860 	dma_pool_destroy(bp->hwrm_dma_pool);
4861 	bp->hwrm_dma_pool = NULL;
4862 
4863 	rcu_read_lock();
4864 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4865 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4866 	rcu_read_unlock();
4867 }
4868 
bnxt_alloc_hwrm_resources(struct bnxt * bp)4869 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4870 {
4871 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4872 					    BNXT_HWRM_DMA_SIZE,
4873 					    BNXT_HWRM_DMA_ALIGN, 0);
4874 	if (!bp->hwrm_dma_pool)
4875 		return -ENOMEM;
4876 
4877 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4878 
4879 	return 0;
4880 }
4881 
bnxt_free_stats_mem(struct bnxt * bp,struct bnxt_stats_mem * stats)4882 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4883 {
4884 	kfree(stats->hw_masks);
4885 	stats->hw_masks = NULL;
4886 	kfree(stats->sw_stats);
4887 	stats->sw_stats = NULL;
4888 	if (stats->hw_stats) {
4889 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4890 				  stats->hw_stats_map);
4891 		stats->hw_stats = NULL;
4892 	}
4893 }
4894 
bnxt_alloc_stats_mem(struct bnxt * bp,struct bnxt_stats_mem * stats,bool alloc_masks)4895 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4896 				bool alloc_masks)
4897 {
4898 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4899 					     &stats->hw_stats_map, GFP_KERNEL);
4900 	if (!stats->hw_stats)
4901 		return -ENOMEM;
4902 
4903 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4904 	if (!stats->sw_stats)
4905 		goto stats_mem_err;
4906 
4907 	if (alloc_masks) {
4908 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4909 		if (!stats->hw_masks)
4910 			goto stats_mem_err;
4911 	}
4912 	return 0;
4913 
4914 stats_mem_err:
4915 	bnxt_free_stats_mem(bp, stats);
4916 	return -ENOMEM;
4917 }
4918 
bnxt_fill_masks(u64 * mask_arr,u64 mask,int count)4919 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4920 {
4921 	int i;
4922 
4923 	for (i = 0; i < count; i++)
4924 		mask_arr[i] = mask;
4925 }
4926 
bnxt_copy_hw_masks(u64 * mask_arr,__le64 * hw_mask_arr,int count)4927 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4928 {
4929 	int i;
4930 
4931 	for (i = 0; i < count; i++)
4932 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4933 }
4934 
bnxt_hwrm_func_qstat_ext(struct bnxt * bp,struct bnxt_stats_mem * stats)4935 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4936 				    struct bnxt_stats_mem *stats)
4937 {
4938 	struct hwrm_func_qstats_ext_output *resp;
4939 	struct hwrm_func_qstats_ext_input *req;
4940 	__le64 *hw_masks;
4941 	int rc;
4942 
4943 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4944 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4945 		return -EOPNOTSUPP;
4946 
4947 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4948 	if (rc)
4949 		return rc;
4950 
4951 	req->fid = cpu_to_le16(0xffff);
4952 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4953 
4954 	resp = hwrm_req_hold(bp, req);
4955 	rc = hwrm_req_send(bp, req);
4956 	if (!rc) {
4957 		hw_masks = &resp->rx_ucast_pkts;
4958 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4959 	}
4960 	hwrm_req_drop(bp, req);
4961 	return rc;
4962 }
4963 
4964 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4965 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4966 
bnxt_init_stats(struct bnxt * bp)4967 static void bnxt_init_stats(struct bnxt *bp)
4968 {
4969 	struct bnxt_napi *bnapi = bp->bnapi[0];
4970 	struct bnxt_cp_ring_info *cpr;
4971 	struct bnxt_stats_mem *stats;
4972 	__le64 *rx_stats, *tx_stats;
4973 	int rc, rx_count, tx_count;
4974 	u64 *rx_masks, *tx_masks;
4975 	u64 mask;
4976 	u8 flags;
4977 
4978 	cpr = &bnapi->cp_ring;
4979 	stats = &cpr->stats;
4980 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4981 	if (rc) {
4982 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4983 			mask = (1ULL << 48) - 1;
4984 		else
4985 			mask = -1ULL;
4986 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
4987 	}
4988 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
4989 		stats = &bp->port_stats;
4990 		rx_stats = stats->hw_stats;
4991 		rx_masks = stats->hw_masks;
4992 		rx_count = sizeof(struct rx_port_stats) / 8;
4993 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4994 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4995 		tx_count = sizeof(struct tx_port_stats) / 8;
4996 
4997 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
4998 		rc = bnxt_hwrm_port_qstats(bp, flags);
4999 		if (rc) {
5000 			mask = (1ULL << 40) - 1;
5001 
5002 			bnxt_fill_masks(rx_masks, mask, rx_count);
5003 			bnxt_fill_masks(tx_masks, mask, tx_count);
5004 		} else {
5005 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5006 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5007 			bnxt_hwrm_port_qstats(bp, 0);
5008 		}
5009 	}
5010 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5011 		stats = &bp->rx_port_stats_ext;
5012 		rx_stats = stats->hw_stats;
5013 		rx_masks = stats->hw_masks;
5014 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5015 		stats = &bp->tx_port_stats_ext;
5016 		tx_stats = stats->hw_stats;
5017 		tx_masks = stats->hw_masks;
5018 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5019 
5020 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5021 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5022 		if (rc) {
5023 			mask = (1ULL << 40) - 1;
5024 
5025 			bnxt_fill_masks(rx_masks, mask, rx_count);
5026 			if (tx_stats)
5027 				bnxt_fill_masks(tx_masks, mask, tx_count);
5028 		} else {
5029 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5030 			if (tx_stats)
5031 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5032 						   tx_count);
5033 			bnxt_hwrm_port_qstats_ext(bp, 0);
5034 		}
5035 	}
5036 }
5037 
bnxt_free_port_stats(struct bnxt * bp)5038 static void bnxt_free_port_stats(struct bnxt *bp)
5039 {
5040 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5041 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5042 
5043 	bnxt_free_stats_mem(bp, &bp->port_stats);
5044 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5045 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5046 }
5047 
bnxt_free_ring_stats(struct bnxt * bp)5048 static void bnxt_free_ring_stats(struct bnxt *bp)
5049 {
5050 	int i;
5051 
5052 	if (!bp->bnapi)
5053 		return;
5054 
5055 	for (i = 0; i < bp->cp_nr_rings; i++) {
5056 		struct bnxt_napi *bnapi = bp->bnapi[i];
5057 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5058 
5059 		bnxt_free_stats_mem(bp, &cpr->stats);
5060 
5061 		kfree(cpr->sw_stats);
5062 		cpr->sw_stats = NULL;
5063 	}
5064 }
5065 
bnxt_alloc_stats(struct bnxt * bp)5066 static int bnxt_alloc_stats(struct bnxt *bp)
5067 {
5068 	u32 size, i;
5069 	int rc;
5070 
5071 	size = bp->hw_ring_stats_size;
5072 
5073 	for (i = 0; i < bp->cp_nr_rings; i++) {
5074 		struct bnxt_napi *bnapi = bp->bnapi[i];
5075 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5076 
5077 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
5078 		if (!cpr->sw_stats)
5079 			return -ENOMEM;
5080 
5081 		cpr->stats.len = size;
5082 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5083 		if (rc)
5084 			return rc;
5085 
5086 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5087 	}
5088 
5089 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5090 		return 0;
5091 
5092 	if (bp->port_stats.hw_stats)
5093 		goto alloc_ext_stats;
5094 
5095 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5096 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5097 	if (rc)
5098 		return rc;
5099 
5100 	bp->flags |= BNXT_FLAG_PORT_STATS;
5101 
5102 alloc_ext_stats:
5103 	/* Display extended statistics only if FW supports it */
5104 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5105 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5106 			return 0;
5107 
5108 	if (bp->rx_port_stats_ext.hw_stats)
5109 		goto alloc_tx_ext_stats;
5110 
5111 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5112 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5113 	/* Extended stats are optional */
5114 	if (rc)
5115 		return 0;
5116 
5117 alloc_tx_ext_stats:
5118 	if (bp->tx_port_stats_ext.hw_stats)
5119 		return 0;
5120 
5121 	if (bp->hwrm_spec_code >= 0x10902 ||
5122 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5123 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5124 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5125 		/* Extended stats are optional */
5126 		if (rc)
5127 			return 0;
5128 	}
5129 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5130 	return 0;
5131 }
5132 
bnxt_clear_ring_indices(struct bnxt * bp)5133 static void bnxt_clear_ring_indices(struct bnxt *bp)
5134 {
5135 	int i, j;
5136 
5137 	if (!bp->bnapi)
5138 		return;
5139 
5140 	for (i = 0; i < bp->cp_nr_rings; i++) {
5141 		struct bnxt_napi *bnapi = bp->bnapi[i];
5142 		struct bnxt_cp_ring_info *cpr;
5143 		struct bnxt_rx_ring_info *rxr;
5144 		struct bnxt_tx_ring_info *txr;
5145 
5146 		if (!bnapi)
5147 			continue;
5148 
5149 		cpr = &bnapi->cp_ring;
5150 		cpr->cp_raw_cons = 0;
5151 
5152 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5153 			txr->tx_prod = 0;
5154 			txr->tx_cons = 0;
5155 			txr->tx_hw_cons = 0;
5156 		}
5157 
5158 		rxr = bnapi->rx_ring;
5159 		if (rxr) {
5160 			rxr->rx_prod = 0;
5161 			rxr->rx_agg_prod = 0;
5162 			rxr->rx_sw_agg_prod = 0;
5163 			rxr->rx_next_cons = 0;
5164 		}
5165 		bnapi->events = 0;
5166 	}
5167 }
5168 
bnxt_insert_usr_fltr(struct bnxt * bp,struct bnxt_filter_base * fltr)5169 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5170 {
5171 	u8 type = fltr->type, flags = fltr->flags;
5172 
5173 	INIT_LIST_HEAD(&fltr->list);
5174 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5175 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5176 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5177 }
5178 
bnxt_del_one_usr_fltr(struct bnxt * bp,struct bnxt_filter_base * fltr)5179 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5180 {
5181 	if (!list_empty(&fltr->list))
5182 		list_del_init(&fltr->list);
5183 }
5184 
bnxt_clear_usr_fltrs(struct bnxt * bp,bool all)5185 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5186 {
5187 	struct bnxt_filter_base *usr_fltr, *tmp;
5188 
5189 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5190 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5191 			continue;
5192 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5193 	}
5194 }
5195 
bnxt_del_fltr(struct bnxt * bp,struct bnxt_filter_base * fltr)5196 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5197 {
5198 	hlist_del(&fltr->hash);
5199 	bnxt_del_one_usr_fltr(bp, fltr);
5200 	if (fltr->flags) {
5201 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5202 		bp->ntp_fltr_count--;
5203 	}
5204 	kfree(fltr);
5205 }
5206 
bnxt_free_ntp_fltrs(struct bnxt * bp,bool all)5207 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5208 {
5209 	int i;
5210 
5211 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
5212 	 * safe to delete the hash table.
5213 	 */
5214 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5215 		struct hlist_head *head;
5216 		struct hlist_node *tmp;
5217 		struct bnxt_ntuple_filter *fltr;
5218 
5219 		head = &bp->ntp_fltr_hash_tbl[i];
5220 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5221 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5222 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5223 				     !list_empty(&fltr->base.list)))
5224 				continue;
5225 			bnxt_del_fltr(bp, &fltr->base);
5226 		}
5227 	}
5228 	if (!all)
5229 		return;
5230 
5231 	bitmap_free(bp->ntp_fltr_bmap);
5232 	bp->ntp_fltr_bmap = NULL;
5233 	bp->ntp_fltr_count = 0;
5234 }
5235 
bnxt_alloc_ntp_fltrs(struct bnxt * bp)5236 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5237 {
5238 	int i, rc = 0;
5239 
5240 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5241 		return 0;
5242 
5243 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5244 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5245 
5246 	bp->ntp_fltr_count = 0;
5247 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5248 
5249 	if (!bp->ntp_fltr_bmap)
5250 		rc = -ENOMEM;
5251 
5252 	return rc;
5253 }
5254 
bnxt_free_l2_filters(struct bnxt * bp,bool all)5255 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5256 {
5257 	int i;
5258 
5259 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5260 		struct hlist_head *head;
5261 		struct hlist_node *tmp;
5262 		struct bnxt_l2_filter *fltr;
5263 
5264 		head = &bp->l2_fltr_hash_tbl[i];
5265 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5266 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5267 				     !list_empty(&fltr->base.list)))
5268 				continue;
5269 			bnxt_del_fltr(bp, &fltr->base);
5270 		}
5271 	}
5272 }
5273 
bnxt_init_l2_fltr_tbl(struct bnxt * bp)5274 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5275 {
5276 	int i;
5277 
5278 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5279 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5280 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5281 }
5282 
bnxt_free_mem(struct bnxt * bp,bool irq_re_init)5283 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5284 {
5285 	bnxt_free_vnic_attributes(bp);
5286 	bnxt_free_tx_rings(bp);
5287 	bnxt_free_rx_rings(bp);
5288 	bnxt_free_cp_rings(bp);
5289 	bnxt_free_all_cp_arrays(bp);
5290 	bnxt_free_ntp_fltrs(bp, false);
5291 	bnxt_free_l2_filters(bp, false);
5292 	if (irq_re_init) {
5293 		bnxt_free_ring_stats(bp);
5294 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5295 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5296 			bnxt_free_port_stats(bp);
5297 		bnxt_free_ring_grps(bp);
5298 		bnxt_free_vnics(bp);
5299 		kfree(bp->tx_ring_map);
5300 		bp->tx_ring_map = NULL;
5301 		kfree(bp->tx_ring);
5302 		bp->tx_ring = NULL;
5303 		kfree(bp->rx_ring);
5304 		bp->rx_ring = NULL;
5305 		kfree(bp->bnapi);
5306 		bp->bnapi = NULL;
5307 	} else {
5308 		bnxt_clear_ring_indices(bp);
5309 	}
5310 }
5311 
bnxt_alloc_mem(struct bnxt * bp,bool irq_re_init)5312 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5313 {
5314 	int i, j, rc, size, arr_size;
5315 	void *bnapi;
5316 
5317 	if (irq_re_init) {
5318 		/* Allocate bnapi mem pointer array and mem block for
5319 		 * all queues
5320 		 */
5321 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5322 				bp->cp_nr_rings);
5323 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5324 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5325 		if (!bnapi)
5326 			return -ENOMEM;
5327 
5328 		bp->bnapi = bnapi;
5329 		bnapi += arr_size;
5330 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5331 			bp->bnapi[i] = bnapi;
5332 			bp->bnapi[i]->index = i;
5333 			bp->bnapi[i]->bp = bp;
5334 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5335 				struct bnxt_cp_ring_info *cpr =
5336 					&bp->bnapi[i]->cp_ring;
5337 
5338 				cpr->cp_ring_struct.ring_mem.flags =
5339 					BNXT_RMEM_RING_PTE_FLAG;
5340 			}
5341 		}
5342 
5343 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5344 				      sizeof(struct bnxt_rx_ring_info),
5345 				      GFP_KERNEL);
5346 		if (!bp->rx_ring)
5347 			return -ENOMEM;
5348 
5349 		for (i = 0; i < bp->rx_nr_rings; i++) {
5350 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5351 
5352 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5353 				rxr->rx_ring_struct.ring_mem.flags =
5354 					BNXT_RMEM_RING_PTE_FLAG;
5355 				rxr->rx_agg_ring_struct.ring_mem.flags =
5356 					BNXT_RMEM_RING_PTE_FLAG;
5357 			} else {
5358 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5359 			}
5360 			rxr->bnapi = bp->bnapi[i];
5361 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5362 		}
5363 
5364 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5365 				      sizeof(struct bnxt_tx_ring_info),
5366 				      GFP_KERNEL);
5367 		if (!bp->tx_ring)
5368 			return -ENOMEM;
5369 
5370 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5371 					  GFP_KERNEL);
5372 
5373 		if (!bp->tx_ring_map)
5374 			return -ENOMEM;
5375 
5376 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5377 			j = 0;
5378 		else
5379 			j = bp->rx_nr_rings;
5380 
5381 		for (i = 0; i < bp->tx_nr_rings; i++) {
5382 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5383 			struct bnxt_napi *bnapi2;
5384 
5385 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5386 				txr->tx_ring_struct.ring_mem.flags =
5387 					BNXT_RMEM_RING_PTE_FLAG;
5388 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5389 			if (i >= bp->tx_nr_rings_xdp) {
5390 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5391 
5392 				bnapi2 = bp->bnapi[k];
5393 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5394 				txr->tx_napi_idx =
5395 					BNXT_RING_TO_TC(bp, txr->txq_index);
5396 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5397 				bnapi2->tx_int = bnxt_tx_int;
5398 			} else {
5399 				bnapi2 = bp->bnapi[j];
5400 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5401 				bnapi2->tx_ring[0] = txr;
5402 				bnapi2->tx_int = bnxt_tx_int_xdp;
5403 				j++;
5404 			}
5405 			txr->bnapi = bnapi2;
5406 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5407 				txr->tx_cpr = &bnapi2->cp_ring;
5408 		}
5409 
5410 		rc = bnxt_alloc_stats(bp);
5411 		if (rc)
5412 			goto alloc_mem_err;
5413 		bnxt_init_stats(bp);
5414 
5415 		rc = bnxt_alloc_ntp_fltrs(bp);
5416 		if (rc)
5417 			goto alloc_mem_err;
5418 
5419 		rc = bnxt_alloc_vnics(bp);
5420 		if (rc)
5421 			goto alloc_mem_err;
5422 	}
5423 
5424 	rc = bnxt_alloc_all_cp_arrays(bp);
5425 	if (rc)
5426 		goto alloc_mem_err;
5427 
5428 	bnxt_init_ring_struct(bp);
5429 
5430 	rc = bnxt_alloc_rx_rings(bp);
5431 	if (rc)
5432 		goto alloc_mem_err;
5433 
5434 	rc = bnxt_alloc_tx_rings(bp);
5435 	if (rc)
5436 		goto alloc_mem_err;
5437 
5438 	rc = bnxt_alloc_cp_rings(bp);
5439 	if (rc)
5440 		goto alloc_mem_err;
5441 
5442 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5443 						  BNXT_VNIC_MCAST_FLAG |
5444 						  BNXT_VNIC_UCAST_FLAG;
5445 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5446 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5447 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5448 
5449 	rc = bnxt_alloc_vnic_attributes(bp);
5450 	if (rc)
5451 		goto alloc_mem_err;
5452 	return 0;
5453 
5454 alloc_mem_err:
5455 	bnxt_free_mem(bp, true);
5456 	return rc;
5457 }
5458 
bnxt_disable_int(struct bnxt * bp)5459 static void bnxt_disable_int(struct bnxt *bp)
5460 {
5461 	int i;
5462 
5463 	if (!bp->bnapi)
5464 		return;
5465 
5466 	for (i = 0; i < bp->cp_nr_rings; i++) {
5467 		struct bnxt_napi *bnapi = bp->bnapi[i];
5468 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5469 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5470 
5471 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5472 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5473 	}
5474 }
5475 
bnxt_cp_num_to_irq_num(struct bnxt * bp,int n)5476 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5477 {
5478 	struct bnxt_napi *bnapi = bp->bnapi[n];
5479 	struct bnxt_cp_ring_info *cpr;
5480 
5481 	cpr = &bnapi->cp_ring;
5482 	return cpr->cp_ring_struct.map_idx;
5483 }
5484 
bnxt_disable_int_sync(struct bnxt * bp)5485 static void bnxt_disable_int_sync(struct bnxt *bp)
5486 {
5487 	int i;
5488 
5489 	if (!bp->irq_tbl)
5490 		return;
5491 
5492 	atomic_inc(&bp->intr_sem);
5493 
5494 	bnxt_disable_int(bp);
5495 	for (i = 0; i < bp->cp_nr_rings; i++) {
5496 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5497 
5498 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5499 	}
5500 }
5501 
bnxt_enable_int(struct bnxt * bp)5502 static void bnxt_enable_int(struct bnxt *bp)
5503 {
5504 	int i;
5505 
5506 	atomic_set(&bp->intr_sem, 0);
5507 	for (i = 0; i < bp->cp_nr_rings; i++) {
5508 		struct bnxt_napi *bnapi = bp->bnapi[i];
5509 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5510 
5511 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5512 	}
5513 }
5514 
bnxt_hwrm_func_drv_rgtr(struct bnxt * bp,unsigned long * bmap,int bmap_size,bool async_only)5515 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5516 			    bool async_only)
5517 {
5518 	DECLARE_BITMAP(async_events_bmap, 256);
5519 	u32 *events = (u32 *)async_events_bmap;
5520 	struct hwrm_func_drv_rgtr_output *resp;
5521 	struct hwrm_func_drv_rgtr_input *req;
5522 	u32 flags;
5523 	int rc, i;
5524 
5525 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5526 	if (rc)
5527 		return rc;
5528 
5529 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5530 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5531 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5532 
5533 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5534 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5535 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5536 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5537 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5538 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5539 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5540 	req->flags = cpu_to_le32(flags);
5541 	req->ver_maj_8b = DRV_VER_MAJ;
5542 	req->ver_min_8b = DRV_VER_MIN;
5543 	req->ver_upd_8b = DRV_VER_UPD;
5544 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5545 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5546 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5547 
5548 	if (BNXT_PF(bp)) {
5549 		u32 data[8];
5550 		int i;
5551 
5552 		memset(data, 0, sizeof(data));
5553 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5554 			u16 cmd = bnxt_vf_req_snif[i];
5555 			unsigned int bit, idx;
5556 
5557 			idx = cmd / 32;
5558 			bit = cmd % 32;
5559 			data[idx] |= 1 << bit;
5560 		}
5561 
5562 		for (i = 0; i < 8; i++)
5563 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5564 
5565 		req->enables |=
5566 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5567 	}
5568 
5569 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5570 		req->flags |= cpu_to_le32(
5571 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5572 
5573 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5574 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5575 		u16 event_id = bnxt_async_events_arr[i];
5576 
5577 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5578 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5579 			continue;
5580 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5581 		    !bp->ptp_cfg)
5582 			continue;
5583 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5584 	}
5585 	if (bmap && bmap_size) {
5586 		for (i = 0; i < bmap_size; i++) {
5587 			if (test_bit(i, bmap))
5588 				__set_bit(i, async_events_bmap);
5589 		}
5590 	}
5591 	for (i = 0; i < 8; i++)
5592 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5593 
5594 	if (async_only)
5595 		req->enables =
5596 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5597 
5598 	resp = hwrm_req_hold(bp, req);
5599 	rc = hwrm_req_send(bp, req);
5600 	if (!rc) {
5601 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5602 		if (resp->flags &
5603 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5604 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5605 	}
5606 	hwrm_req_drop(bp, req);
5607 	return rc;
5608 }
5609 
bnxt_hwrm_func_drv_unrgtr(struct bnxt * bp)5610 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5611 {
5612 	struct hwrm_func_drv_unrgtr_input *req;
5613 	int rc;
5614 
5615 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5616 		return 0;
5617 
5618 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5619 	if (rc)
5620 		return rc;
5621 	return hwrm_req_send(bp, req);
5622 }
5623 
5624 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5625 
bnxt_hwrm_tunnel_dst_port_free(struct bnxt * bp,u8 tunnel_type)5626 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5627 {
5628 	struct hwrm_tunnel_dst_port_free_input *req;
5629 	int rc;
5630 
5631 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5632 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5633 		return 0;
5634 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5635 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5636 		return 0;
5637 
5638 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5639 	if (rc)
5640 		return rc;
5641 
5642 	req->tunnel_type = tunnel_type;
5643 
5644 	switch (tunnel_type) {
5645 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5646 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5647 		bp->vxlan_port = 0;
5648 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5649 		break;
5650 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5651 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5652 		bp->nge_port = 0;
5653 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5654 		break;
5655 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5656 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5657 		bp->vxlan_gpe_port = 0;
5658 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5659 		break;
5660 	default:
5661 		break;
5662 	}
5663 
5664 	rc = hwrm_req_send(bp, req);
5665 	if (rc)
5666 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5667 			   rc);
5668 	if (bp->flags & BNXT_FLAG_TPA)
5669 		bnxt_set_tpa(bp, true);
5670 	return rc;
5671 }
5672 
bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt * bp,__be16 port,u8 tunnel_type)5673 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5674 					   u8 tunnel_type)
5675 {
5676 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5677 	struct hwrm_tunnel_dst_port_alloc_input *req;
5678 	int rc;
5679 
5680 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5681 	if (rc)
5682 		return rc;
5683 
5684 	req->tunnel_type = tunnel_type;
5685 	req->tunnel_dst_port_val = port;
5686 
5687 	resp = hwrm_req_hold(bp, req);
5688 	rc = hwrm_req_send(bp, req);
5689 	if (rc) {
5690 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5691 			   rc);
5692 		goto err_out;
5693 	}
5694 
5695 	switch (tunnel_type) {
5696 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5697 		bp->vxlan_port = port;
5698 		bp->vxlan_fw_dst_port_id =
5699 			le16_to_cpu(resp->tunnel_dst_port_id);
5700 		break;
5701 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5702 		bp->nge_port = port;
5703 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5704 		break;
5705 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5706 		bp->vxlan_gpe_port = port;
5707 		bp->vxlan_gpe_fw_dst_port_id =
5708 			le16_to_cpu(resp->tunnel_dst_port_id);
5709 		break;
5710 	default:
5711 		break;
5712 	}
5713 	if (bp->flags & BNXT_FLAG_TPA)
5714 		bnxt_set_tpa(bp, true);
5715 
5716 err_out:
5717 	hwrm_req_drop(bp, req);
5718 	return rc;
5719 }
5720 
bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt * bp,u16 vnic_id)5721 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5722 {
5723 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5724 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5725 	int rc;
5726 
5727 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5728 	if (rc)
5729 		return rc;
5730 
5731 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5732 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5733 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5734 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5735 	}
5736 	req->mask = cpu_to_le32(vnic->rx_mask);
5737 	return hwrm_req_send_silent(bp, req);
5738 }
5739 
bnxt_del_l2_filter(struct bnxt * bp,struct bnxt_l2_filter * fltr)5740 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5741 {
5742 	if (!atomic_dec_and_test(&fltr->refcnt))
5743 		return;
5744 	spin_lock_bh(&bp->ntp_fltr_lock);
5745 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5746 		spin_unlock_bh(&bp->ntp_fltr_lock);
5747 		return;
5748 	}
5749 	hlist_del_rcu(&fltr->base.hash);
5750 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5751 	if (fltr->base.flags) {
5752 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5753 		bp->ntp_fltr_count--;
5754 	}
5755 	spin_unlock_bh(&bp->ntp_fltr_lock);
5756 	kfree_rcu(fltr, base.rcu);
5757 }
5758 
__bnxt_lookup_l2_filter(struct bnxt * bp,struct bnxt_l2_key * key,u32 idx)5759 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5760 						      struct bnxt_l2_key *key,
5761 						      u32 idx)
5762 {
5763 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5764 	struct bnxt_l2_filter *fltr;
5765 
5766 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5767 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5768 
5769 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5770 		    l2_key->vlan == key->vlan)
5771 			return fltr;
5772 	}
5773 	return NULL;
5774 }
5775 
bnxt_lookup_l2_filter(struct bnxt * bp,struct bnxt_l2_key * key,u32 idx)5776 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5777 						    struct bnxt_l2_key *key,
5778 						    u32 idx)
5779 {
5780 	struct bnxt_l2_filter *fltr = NULL;
5781 
5782 	rcu_read_lock();
5783 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5784 	if (fltr)
5785 		atomic_inc(&fltr->refcnt);
5786 	rcu_read_unlock();
5787 	return fltr;
5788 }
5789 
5790 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5791 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5792 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5793 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5794 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5795 
5796 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5797 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5798 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5799 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5800 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5801 
bnxt_get_rss_flow_tuple_len(struct bnxt * bp,struct flow_keys * fkeys)5802 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5803 {
5804 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5805 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5806 			return sizeof(fkeys->addrs.v4addrs) +
5807 			       sizeof(fkeys->ports);
5808 
5809 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5810 			return sizeof(fkeys->addrs.v4addrs);
5811 	}
5812 
5813 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5814 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5815 			return sizeof(fkeys->addrs.v6addrs) +
5816 			       sizeof(fkeys->ports);
5817 
5818 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5819 			return sizeof(fkeys->addrs.v6addrs);
5820 	}
5821 
5822 	return 0;
5823 }
5824 
bnxt_toeplitz(struct bnxt * bp,struct flow_keys * fkeys,const unsigned char * key)5825 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5826 			 const unsigned char *key)
5827 {
5828 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5829 	struct bnxt_ipv4_tuple tuple4;
5830 	struct bnxt_ipv6_tuple tuple6;
5831 	int i, j, len = 0;
5832 	u8 *four_tuple;
5833 
5834 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5835 	if (!len)
5836 		return 0;
5837 
5838 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5839 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5840 		tuple4.ports = fkeys->ports;
5841 		four_tuple = (unsigned char *)&tuple4;
5842 	} else {
5843 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5844 		tuple6.ports = fkeys->ports;
5845 		four_tuple = (unsigned char *)&tuple6;
5846 	}
5847 
5848 	for (i = 0, j = 8; i < len; i++, j++) {
5849 		u8 byte = four_tuple[i];
5850 		int bit;
5851 
5852 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5853 			if (byte & 0x80)
5854 				hash ^= prefix;
5855 		}
5856 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5857 	}
5858 
5859 	/* The valid part of the hash is in the upper 32 bits. */
5860 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5861 }
5862 
5863 #ifdef CONFIG_RFS_ACCEL
5864 static struct bnxt_l2_filter *
bnxt_lookup_l2_filter_from_key(struct bnxt * bp,struct bnxt_l2_key * key)5865 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5866 {
5867 	struct bnxt_l2_filter *fltr;
5868 	u32 idx;
5869 
5870 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5871 	      BNXT_L2_FLTR_HASH_MASK;
5872 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5873 	return fltr;
5874 }
5875 #endif
5876 
bnxt_init_l2_filter(struct bnxt * bp,struct bnxt_l2_filter * fltr,struct bnxt_l2_key * key,u32 idx)5877 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5878 			       struct bnxt_l2_key *key, u32 idx)
5879 {
5880 	struct hlist_head *head;
5881 
5882 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5883 	fltr->l2_key.vlan = key->vlan;
5884 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5885 	if (fltr->base.flags) {
5886 		int bit_id;
5887 
5888 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5889 						 bp->max_fltr, 0);
5890 		if (bit_id < 0)
5891 			return -ENOMEM;
5892 		fltr->base.sw_id = (u16)bit_id;
5893 		bp->ntp_fltr_count++;
5894 	}
5895 	head = &bp->l2_fltr_hash_tbl[idx];
5896 	hlist_add_head_rcu(&fltr->base.hash, head);
5897 	bnxt_insert_usr_fltr(bp, &fltr->base);
5898 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5899 	atomic_set(&fltr->refcnt, 1);
5900 	return 0;
5901 }
5902 
bnxt_alloc_l2_filter(struct bnxt * bp,struct bnxt_l2_key * key,gfp_t gfp)5903 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
5904 						   struct bnxt_l2_key *key,
5905 						   gfp_t gfp)
5906 {
5907 	struct bnxt_l2_filter *fltr;
5908 	u32 idx;
5909 	int rc;
5910 
5911 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5912 	      BNXT_L2_FLTR_HASH_MASK;
5913 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5914 	if (fltr)
5915 		return fltr;
5916 
5917 	fltr = kzalloc(sizeof(*fltr), gfp);
5918 	if (!fltr)
5919 		return ERR_PTR(-ENOMEM);
5920 	spin_lock_bh(&bp->ntp_fltr_lock);
5921 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5922 	spin_unlock_bh(&bp->ntp_fltr_lock);
5923 	if (rc) {
5924 		bnxt_del_l2_filter(bp, fltr);
5925 		fltr = ERR_PTR(rc);
5926 	}
5927 	return fltr;
5928 }
5929 
bnxt_alloc_new_l2_filter(struct bnxt * bp,struct bnxt_l2_key * key,u16 flags)5930 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
5931 						struct bnxt_l2_key *key,
5932 						u16 flags)
5933 {
5934 	struct bnxt_l2_filter *fltr;
5935 	u32 idx;
5936 	int rc;
5937 
5938 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5939 	      BNXT_L2_FLTR_HASH_MASK;
5940 	spin_lock_bh(&bp->ntp_fltr_lock);
5941 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5942 	if (fltr) {
5943 		fltr = ERR_PTR(-EEXIST);
5944 		goto l2_filter_exit;
5945 	}
5946 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
5947 	if (!fltr) {
5948 		fltr = ERR_PTR(-ENOMEM);
5949 		goto l2_filter_exit;
5950 	}
5951 	fltr->base.flags = flags;
5952 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5953 	if (rc) {
5954 		spin_unlock_bh(&bp->ntp_fltr_lock);
5955 		bnxt_del_l2_filter(bp, fltr);
5956 		return ERR_PTR(rc);
5957 	}
5958 
5959 l2_filter_exit:
5960 	spin_unlock_bh(&bp->ntp_fltr_lock);
5961 	return fltr;
5962 }
5963 
bnxt_vf_target_id(struct bnxt_pf_info * pf,u16 vf_idx)5964 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
5965 {
5966 #ifdef CONFIG_BNXT_SRIOV
5967 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
5968 
5969 	return vf->fw_fid;
5970 #else
5971 	return INVALID_HW_RING_ID;
5972 #endif
5973 }
5974 
bnxt_hwrm_l2_filter_free(struct bnxt * bp,struct bnxt_l2_filter * fltr)5975 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5976 {
5977 	struct hwrm_cfa_l2_filter_free_input *req;
5978 	u16 target_id = 0xffff;
5979 	int rc;
5980 
5981 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5982 		struct bnxt_pf_info *pf = &bp->pf;
5983 
5984 		if (fltr->base.vf_idx >= pf->active_vfs)
5985 			return -EINVAL;
5986 
5987 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5988 		if (target_id == INVALID_HW_RING_ID)
5989 			return -EINVAL;
5990 	}
5991 
5992 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
5993 	if (rc)
5994 		return rc;
5995 
5996 	req->target_id = cpu_to_le16(target_id);
5997 	req->l2_filter_id = fltr->base.filter_id;
5998 	return hwrm_req_send(bp, req);
5999 }
6000 
bnxt_hwrm_l2_filter_alloc(struct bnxt * bp,struct bnxt_l2_filter * fltr)6001 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6002 {
6003 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6004 	struct hwrm_cfa_l2_filter_alloc_input *req;
6005 	u16 target_id = 0xffff;
6006 	int rc;
6007 
6008 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6009 		struct bnxt_pf_info *pf = &bp->pf;
6010 
6011 		if (fltr->base.vf_idx >= pf->active_vfs)
6012 			return -EINVAL;
6013 
6014 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6015 	}
6016 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6017 	if (rc)
6018 		return rc;
6019 
6020 	req->target_id = cpu_to_le16(target_id);
6021 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6022 
6023 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6024 		req->flags |=
6025 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6026 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6027 	req->enables =
6028 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6029 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6030 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6031 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6032 	eth_broadcast_addr(req->l2_addr_mask);
6033 
6034 	if (fltr->l2_key.vlan) {
6035 		req->enables |=
6036 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6037 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6038 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6039 		req->num_vlans = 1;
6040 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6041 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6042 	}
6043 
6044 	resp = hwrm_req_hold(bp, req);
6045 	rc = hwrm_req_send(bp, req);
6046 	if (!rc) {
6047 		fltr->base.filter_id = resp->l2_filter_id;
6048 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6049 	}
6050 	hwrm_req_drop(bp, req);
6051 	return rc;
6052 }
6053 
bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt * bp,struct bnxt_ntuple_filter * fltr)6054 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6055 				     struct bnxt_ntuple_filter *fltr)
6056 {
6057 	struct hwrm_cfa_ntuple_filter_free_input *req;
6058 	int rc;
6059 
6060 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6061 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6062 	if (rc)
6063 		return rc;
6064 
6065 	req->ntuple_filter_id = fltr->base.filter_id;
6066 	return hwrm_req_send(bp, req);
6067 }
6068 
6069 #define BNXT_NTP_FLTR_FLAGS					\
6070 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6071 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6072 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6073 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6074 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6075 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6076 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6077 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6078 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6079 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6080 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6081 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6082 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6083 
6084 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6085 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6086 
bnxt_fill_ipv6_mask(__be32 mask[4])6087 void bnxt_fill_ipv6_mask(__be32 mask[4])
6088 {
6089 	int i;
6090 
6091 	for (i = 0; i < 4; i++)
6092 		mask[i] = cpu_to_be32(~0);
6093 }
6094 
6095 static void
bnxt_cfg_rfs_ring_tbl_idx(struct bnxt * bp,struct hwrm_cfa_ntuple_filter_alloc_input * req,struct bnxt_ntuple_filter * fltr)6096 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6097 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6098 			  struct bnxt_ntuple_filter *fltr)
6099 {
6100 	u16 rxq = fltr->base.rxq;
6101 
6102 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6103 		struct ethtool_rxfh_context *ctx;
6104 		struct bnxt_rss_ctx *rss_ctx;
6105 		struct bnxt_vnic_info *vnic;
6106 
6107 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6108 			      fltr->base.fw_vnic_id);
6109 		if (ctx) {
6110 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6111 			vnic = &rss_ctx->vnic;
6112 
6113 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6114 		}
6115 		return;
6116 	}
6117 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6118 		struct bnxt_vnic_info *vnic;
6119 		u32 enables;
6120 
6121 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6122 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6123 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6124 		req->enables |= cpu_to_le32(enables);
6125 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6126 	} else {
6127 		u32 flags;
6128 
6129 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6130 		req->flags |= cpu_to_le32(flags);
6131 		req->dst_id = cpu_to_le16(rxq);
6132 	}
6133 }
6134 
bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt * bp,struct bnxt_ntuple_filter * fltr)6135 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6136 				      struct bnxt_ntuple_filter *fltr)
6137 {
6138 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6139 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6140 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6141 	struct flow_keys *keys = &fltr->fkeys;
6142 	struct bnxt_l2_filter *l2_fltr;
6143 	struct bnxt_vnic_info *vnic;
6144 	int rc;
6145 
6146 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6147 	if (rc)
6148 		return rc;
6149 
6150 	l2_fltr = fltr->l2_fltr;
6151 	req->l2_filter_id = l2_fltr->base.filter_id;
6152 
6153 	if (fltr->base.flags & BNXT_ACT_DROP) {
6154 		req->flags =
6155 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6156 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6157 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6158 	} else {
6159 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6160 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6161 	}
6162 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6163 
6164 	req->ethertype = htons(ETH_P_IP);
6165 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6166 	req->ip_protocol = keys->basic.ip_proto;
6167 
6168 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6169 		req->ethertype = htons(ETH_P_IPV6);
6170 		req->ip_addr_type =
6171 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6172 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6173 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6174 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6175 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6176 	} else {
6177 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6178 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6179 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6180 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6181 	}
6182 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6183 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6184 		req->tunnel_type =
6185 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6186 	}
6187 
6188 	req->src_port = keys->ports.src;
6189 	req->src_port_mask = masks->ports.src;
6190 	req->dst_port = keys->ports.dst;
6191 	req->dst_port_mask = masks->ports.dst;
6192 
6193 	resp = hwrm_req_hold(bp, req);
6194 	rc = hwrm_req_send(bp, req);
6195 	if (!rc)
6196 		fltr->base.filter_id = resp->ntuple_filter_id;
6197 	hwrm_req_drop(bp, req);
6198 	return rc;
6199 }
6200 
bnxt_hwrm_set_vnic_filter(struct bnxt * bp,u16 vnic_id,u16 idx,const u8 * mac_addr)6201 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6202 				     const u8 *mac_addr)
6203 {
6204 	struct bnxt_l2_filter *fltr;
6205 	struct bnxt_l2_key key;
6206 	int rc;
6207 
6208 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6209 	key.vlan = 0;
6210 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6211 	if (IS_ERR(fltr))
6212 		return PTR_ERR(fltr);
6213 
6214 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6215 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6216 	if (rc)
6217 		bnxt_del_l2_filter(bp, fltr);
6218 	else
6219 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6220 	return rc;
6221 }
6222 
bnxt_hwrm_clear_vnic_filter(struct bnxt * bp)6223 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6224 {
6225 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6226 
6227 	/* Any associated ntuple filters will also be cleared by firmware. */
6228 	for (i = 0; i < num_of_vnics; i++) {
6229 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6230 
6231 		for (j = 0; j < vnic->uc_filter_count; j++) {
6232 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6233 
6234 			bnxt_hwrm_l2_filter_free(bp, fltr);
6235 			bnxt_del_l2_filter(bp, fltr);
6236 		}
6237 		vnic->uc_filter_count = 0;
6238 	}
6239 }
6240 
6241 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6242 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6243 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6244 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6245 
bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt * bp,struct hwrm_vnic_tpa_cfg_input * req)6246 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6247 					   struct hwrm_vnic_tpa_cfg_input *req)
6248 {
6249 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6250 
6251 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6252 		return;
6253 
6254 	if (bp->vxlan_port)
6255 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6256 	if (bp->vxlan_gpe_port)
6257 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6258 	if (bp->nge_port)
6259 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6260 
6261 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6262 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6263 }
6264 
bnxt_hwrm_vnic_set_tpa(struct bnxt * bp,struct bnxt_vnic_info * vnic,u32 tpa_flags)6265 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6266 			   u32 tpa_flags)
6267 {
6268 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6269 	struct hwrm_vnic_tpa_cfg_input *req;
6270 	int rc;
6271 
6272 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6273 		return 0;
6274 
6275 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6276 	if (rc)
6277 		return rc;
6278 
6279 	if (tpa_flags) {
6280 		u16 mss = bp->dev->mtu - 40;
6281 		u32 nsegs, n, segs = 0, flags;
6282 
6283 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6284 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6285 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6286 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6287 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6288 		if (tpa_flags & BNXT_FLAG_GRO)
6289 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6290 
6291 		req->flags = cpu_to_le32(flags);
6292 
6293 		req->enables =
6294 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6295 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6296 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6297 
6298 		/* Number of segs are log2 units, and first packet is not
6299 		 * included as part of this units.
6300 		 */
6301 		if (mss <= BNXT_RX_PAGE_SIZE) {
6302 			n = BNXT_RX_PAGE_SIZE / mss;
6303 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6304 		} else {
6305 			n = mss / BNXT_RX_PAGE_SIZE;
6306 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6307 				n++;
6308 			nsegs = (MAX_SKB_FRAGS - n) / n;
6309 		}
6310 
6311 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6312 			segs = MAX_TPA_SEGS_P5;
6313 			max_aggs = bp->max_tpa;
6314 		} else {
6315 			segs = ilog2(nsegs);
6316 		}
6317 		req->max_agg_segs = cpu_to_le16(segs);
6318 		req->max_aggs = cpu_to_le16(max_aggs);
6319 
6320 		req->min_agg_len = cpu_to_le32(512);
6321 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6322 	}
6323 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6324 
6325 	return hwrm_req_send(bp, req);
6326 }
6327 
bnxt_cp_ring_from_grp(struct bnxt * bp,struct bnxt_ring_struct * ring)6328 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6329 {
6330 	struct bnxt_ring_grp_info *grp_info;
6331 
6332 	grp_info = &bp->grp_info[ring->grp_idx];
6333 	return grp_info->cp_fw_ring_id;
6334 }
6335 
bnxt_cp_ring_for_rx(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)6336 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6337 {
6338 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6339 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6340 	else
6341 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6342 }
6343 
bnxt_cp_ring_for_tx(struct bnxt * bp,struct bnxt_tx_ring_info * txr)6344 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6345 {
6346 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6347 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6348 	else
6349 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6350 }
6351 
bnxt_alloc_rss_indir_tbl(struct bnxt * bp)6352 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6353 {
6354 	int entries;
6355 
6356 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6357 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6358 	else
6359 		entries = HW_HASH_INDEX_SIZE;
6360 
6361 	bp->rss_indir_tbl_entries = entries;
6362 	bp->rss_indir_tbl =
6363 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6364 	if (!bp->rss_indir_tbl)
6365 		return -ENOMEM;
6366 
6367 	return 0;
6368 }
6369 
bnxt_set_dflt_rss_indir_tbl(struct bnxt * bp,struct ethtool_rxfh_context * rss_ctx)6370 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6371 				 struct ethtool_rxfh_context *rss_ctx)
6372 {
6373 	u16 max_rings, max_entries, pad, i;
6374 	u32 *rss_indir_tbl;
6375 
6376 	if (!bp->rx_nr_rings)
6377 		return;
6378 
6379 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6380 		max_rings = bp->rx_nr_rings - 1;
6381 	else
6382 		max_rings = bp->rx_nr_rings;
6383 
6384 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6385 	if (rss_ctx)
6386 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6387 	else
6388 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6389 
6390 	for (i = 0; i < max_entries; i++)
6391 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6392 
6393 	pad = bp->rss_indir_tbl_entries - max_entries;
6394 	if (pad)
6395 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6396 }
6397 
bnxt_get_max_rss_ring(struct bnxt * bp)6398 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6399 {
6400 	u32 i, tbl_size, max_ring = 0;
6401 
6402 	if (!bp->rss_indir_tbl)
6403 		return 0;
6404 
6405 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6406 	for (i = 0; i < tbl_size; i++)
6407 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6408 	return max_ring;
6409 }
6410 
bnxt_get_nr_rss_ctxs(struct bnxt * bp,int rx_rings)6411 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6412 {
6413 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6414 		if (!rx_rings)
6415 			return 0;
6416 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6417 					       BNXT_RSS_TABLE_ENTRIES_P5);
6418 	}
6419 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6420 		return 2;
6421 	return 1;
6422 }
6423 
bnxt_fill_hw_rss_tbl(struct bnxt * bp,struct bnxt_vnic_info * vnic)6424 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6425 {
6426 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6427 	u16 i, j;
6428 
6429 	/* Fill the RSS indirection table with ring group ids */
6430 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6431 		if (!no_rss)
6432 			j = bp->rss_indir_tbl[i];
6433 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6434 	}
6435 }
6436 
bnxt_fill_hw_rss_tbl_p5(struct bnxt * bp,struct bnxt_vnic_info * vnic)6437 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6438 				    struct bnxt_vnic_info *vnic)
6439 {
6440 	__le16 *ring_tbl = vnic->rss_table;
6441 	struct bnxt_rx_ring_info *rxr;
6442 	u16 tbl_size, i;
6443 
6444 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6445 
6446 	for (i = 0; i < tbl_size; i++) {
6447 		u16 ring_id, j;
6448 
6449 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6450 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6451 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6452 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6453 		else
6454 			j = bp->rss_indir_tbl[i];
6455 		rxr = &bp->rx_ring[j];
6456 
6457 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6458 		*ring_tbl++ = cpu_to_le16(ring_id);
6459 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6460 		*ring_tbl++ = cpu_to_le16(ring_id);
6461 	}
6462 }
6463 
6464 static void
__bnxt_hwrm_vnic_set_rss(struct bnxt * bp,struct hwrm_vnic_rss_cfg_input * req,struct bnxt_vnic_info * vnic)6465 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6466 			 struct bnxt_vnic_info *vnic)
6467 {
6468 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6469 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6470 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6471 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6472 	} else {
6473 		bnxt_fill_hw_rss_tbl(bp, vnic);
6474 	}
6475 
6476 	if (bp->rss_hash_delta) {
6477 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6478 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6479 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6480 		else
6481 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6482 	} else {
6483 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6484 	}
6485 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6486 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6487 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6488 }
6489 
bnxt_hwrm_vnic_set_rss(struct bnxt * bp,struct bnxt_vnic_info * vnic,bool set_rss)6490 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6491 				  bool set_rss)
6492 {
6493 	struct hwrm_vnic_rss_cfg_input *req;
6494 	int rc;
6495 
6496 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6497 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6498 		return 0;
6499 
6500 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6501 	if (rc)
6502 		return rc;
6503 
6504 	if (set_rss)
6505 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6506 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6507 	return hwrm_req_send(bp, req);
6508 }
6509 
bnxt_hwrm_vnic_set_rss_p5(struct bnxt * bp,struct bnxt_vnic_info * vnic,bool set_rss)6510 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6511 				     struct bnxt_vnic_info *vnic, bool set_rss)
6512 {
6513 	struct hwrm_vnic_rss_cfg_input *req;
6514 	dma_addr_t ring_tbl_map;
6515 	u32 i, nr_ctxs;
6516 	int rc;
6517 
6518 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6519 	if (rc)
6520 		return rc;
6521 
6522 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6523 	if (!set_rss)
6524 		return hwrm_req_send(bp, req);
6525 
6526 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6527 	ring_tbl_map = vnic->rss_table_dma_addr;
6528 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6529 
6530 	hwrm_req_hold(bp, req);
6531 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6532 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6533 		req->ring_table_pair_index = i;
6534 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6535 		rc = hwrm_req_send(bp, req);
6536 		if (rc)
6537 			goto exit;
6538 	}
6539 
6540 exit:
6541 	hwrm_req_drop(bp, req);
6542 	return rc;
6543 }
6544 
bnxt_hwrm_update_rss_hash_cfg(struct bnxt * bp)6545 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6546 {
6547 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6548 	struct hwrm_vnic_rss_qcfg_output *resp;
6549 	struct hwrm_vnic_rss_qcfg_input *req;
6550 
6551 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6552 		return;
6553 
6554 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6555 	/* all contexts configured to same hash_type, zero always exists */
6556 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6557 	resp = hwrm_req_hold(bp, req);
6558 	if (!hwrm_req_send(bp, req)) {
6559 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6560 		bp->rss_hash_delta = 0;
6561 	}
6562 	hwrm_req_drop(bp, req);
6563 }
6564 
bnxt_hwrm_vnic_set_hds(struct bnxt * bp,struct bnxt_vnic_info * vnic)6565 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6566 {
6567 	struct hwrm_vnic_plcmodes_cfg_input *req;
6568 	int rc;
6569 
6570 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6571 	if (rc)
6572 		return rc;
6573 
6574 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6575 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6576 
6577 	if (BNXT_RX_PAGE_MODE(bp)) {
6578 		req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6579 	} else {
6580 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6581 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6582 		req->enables |=
6583 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6584 		req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
6585 		req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
6586 	}
6587 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6588 	return hwrm_req_send(bp, req);
6589 }
6590 
bnxt_hwrm_vnic_ctx_free_one(struct bnxt * bp,struct bnxt_vnic_info * vnic,u16 ctx_idx)6591 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6592 					struct bnxt_vnic_info *vnic,
6593 					u16 ctx_idx)
6594 {
6595 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6596 
6597 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6598 		return;
6599 
6600 	req->rss_cos_lb_ctx_id =
6601 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6602 
6603 	hwrm_req_send(bp, req);
6604 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6605 }
6606 
bnxt_hwrm_vnic_ctx_free(struct bnxt * bp)6607 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6608 {
6609 	int i, j;
6610 
6611 	for (i = 0; i < bp->nr_vnics; i++) {
6612 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6613 
6614 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6615 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6616 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6617 		}
6618 	}
6619 	bp->rsscos_nr_ctxs = 0;
6620 }
6621 
bnxt_hwrm_vnic_ctx_alloc(struct bnxt * bp,struct bnxt_vnic_info * vnic,u16 ctx_idx)6622 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6623 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6624 {
6625 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6626 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6627 	int rc;
6628 
6629 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6630 	if (rc)
6631 		return rc;
6632 
6633 	resp = hwrm_req_hold(bp, req);
6634 	rc = hwrm_req_send(bp, req);
6635 	if (!rc)
6636 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6637 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6638 	hwrm_req_drop(bp, req);
6639 
6640 	return rc;
6641 }
6642 
bnxt_get_roce_vnic_mode(struct bnxt * bp)6643 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6644 {
6645 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6646 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6647 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6648 }
6649 
bnxt_hwrm_vnic_cfg(struct bnxt * bp,struct bnxt_vnic_info * vnic)6650 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6651 {
6652 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6653 	struct hwrm_vnic_cfg_input *req;
6654 	unsigned int ring = 0, grp_idx;
6655 	u16 def_vlan = 0;
6656 	int rc;
6657 
6658 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6659 	if (rc)
6660 		return rc;
6661 
6662 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6663 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6664 
6665 		req->default_rx_ring_id =
6666 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6667 		req->default_cmpl_ring_id =
6668 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6669 		req->enables =
6670 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6671 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6672 		goto vnic_mru;
6673 	}
6674 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6675 	/* Only RSS support for now TBD: COS & LB */
6676 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6677 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6678 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6679 					   VNIC_CFG_REQ_ENABLES_MRU);
6680 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6681 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6682 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6683 					   VNIC_CFG_REQ_ENABLES_MRU);
6684 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6685 	} else {
6686 		req->rss_rule = cpu_to_le16(0xffff);
6687 	}
6688 
6689 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6690 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6691 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6692 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6693 	} else {
6694 		req->cos_rule = cpu_to_le16(0xffff);
6695 	}
6696 
6697 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6698 		ring = 0;
6699 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6700 		ring = vnic->vnic_id - 1;
6701 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6702 		ring = bp->rx_nr_rings - 1;
6703 
6704 	grp_idx = bp->rx_ring[ring].bnapi->index;
6705 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6706 	req->lb_rule = cpu_to_le16(0xffff);
6707 vnic_mru:
6708 	vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
6709 	req->mru = cpu_to_le16(vnic->mru);
6710 
6711 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6712 #ifdef CONFIG_BNXT_SRIOV
6713 	if (BNXT_VF(bp))
6714 		def_vlan = bp->vf.vlan;
6715 #endif
6716 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6717 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6718 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6719 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6720 
6721 	return hwrm_req_send(bp, req);
6722 }
6723 
bnxt_hwrm_vnic_free_one(struct bnxt * bp,struct bnxt_vnic_info * vnic)6724 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6725 				    struct bnxt_vnic_info *vnic)
6726 {
6727 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6728 		struct hwrm_vnic_free_input *req;
6729 
6730 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6731 			return;
6732 
6733 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6734 
6735 		hwrm_req_send(bp, req);
6736 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6737 	}
6738 }
6739 
bnxt_hwrm_vnic_free(struct bnxt * bp)6740 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6741 {
6742 	u16 i;
6743 
6744 	for (i = 0; i < bp->nr_vnics; i++)
6745 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6746 }
6747 
bnxt_hwrm_vnic_alloc(struct bnxt * bp,struct bnxt_vnic_info * vnic,unsigned int start_rx_ring_idx,unsigned int nr_rings)6748 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6749 			 unsigned int start_rx_ring_idx,
6750 			 unsigned int nr_rings)
6751 {
6752 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6753 	struct hwrm_vnic_alloc_output *resp;
6754 	struct hwrm_vnic_alloc_input *req;
6755 	int rc;
6756 
6757 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6758 	if (rc)
6759 		return rc;
6760 
6761 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6762 		goto vnic_no_ring_grps;
6763 
6764 	/* map ring groups to this vnic */
6765 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6766 		grp_idx = bp->rx_ring[i].bnapi->index;
6767 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6768 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6769 				   j, nr_rings);
6770 			break;
6771 		}
6772 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6773 	}
6774 
6775 vnic_no_ring_grps:
6776 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6777 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6778 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6779 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6780 
6781 	resp = hwrm_req_hold(bp, req);
6782 	rc = hwrm_req_send(bp, req);
6783 	if (!rc)
6784 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6785 	hwrm_req_drop(bp, req);
6786 	return rc;
6787 }
6788 
bnxt_hwrm_vnic_qcaps(struct bnxt * bp)6789 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6790 {
6791 	struct hwrm_vnic_qcaps_output *resp;
6792 	struct hwrm_vnic_qcaps_input *req;
6793 	int rc;
6794 
6795 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6796 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6797 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6798 	if (bp->hwrm_spec_code < 0x10600)
6799 		return 0;
6800 
6801 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6802 	if (rc)
6803 		return rc;
6804 
6805 	resp = hwrm_req_hold(bp, req);
6806 	rc = hwrm_req_send(bp, req);
6807 	if (!rc) {
6808 		u32 flags = le32_to_cpu(resp->flags);
6809 
6810 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6811 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6812 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6813 		if (flags &
6814 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6815 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6816 
6817 		/* Older P5 fw before EXT_HW_STATS support did not set
6818 		 * VLAN_STRIP_CAP properly.
6819 		 */
6820 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6821 		    (BNXT_CHIP_P5(bp) &&
6822 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6823 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6824 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6825 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6826 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6827 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6828 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6829 		if (bp->max_tpa_v2) {
6830 			if (BNXT_CHIP_P5(bp))
6831 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6832 			else
6833 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6834 		}
6835 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6836 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6837 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6838 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6839 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6840 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6841 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6842 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6843 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6844 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6845 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6846 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6847 	}
6848 	hwrm_req_drop(bp, req);
6849 	return rc;
6850 }
6851 
bnxt_hwrm_ring_grp_alloc(struct bnxt * bp)6852 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6853 {
6854 	struct hwrm_ring_grp_alloc_output *resp;
6855 	struct hwrm_ring_grp_alloc_input *req;
6856 	int rc;
6857 	u16 i;
6858 
6859 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6860 		return 0;
6861 
6862 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6863 	if (rc)
6864 		return rc;
6865 
6866 	resp = hwrm_req_hold(bp, req);
6867 	for (i = 0; i < bp->rx_nr_rings; i++) {
6868 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6869 
6870 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6871 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6872 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6873 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6874 
6875 		rc = hwrm_req_send(bp, req);
6876 
6877 		if (rc)
6878 			break;
6879 
6880 		bp->grp_info[grp_idx].fw_grp_id =
6881 			le32_to_cpu(resp->ring_group_id);
6882 	}
6883 	hwrm_req_drop(bp, req);
6884 	return rc;
6885 }
6886 
bnxt_hwrm_ring_grp_free(struct bnxt * bp)6887 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6888 {
6889 	struct hwrm_ring_grp_free_input *req;
6890 	u16 i;
6891 
6892 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6893 		return;
6894 
6895 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6896 		return;
6897 
6898 	hwrm_req_hold(bp, req);
6899 	for (i = 0; i < bp->cp_nr_rings; i++) {
6900 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
6901 			continue;
6902 		req->ring_group_id =
6903 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
6904 
6905 		hwrm_req_send(bp, req);
6906 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
6907 	}
6908 	hwrm_req_drop(bp, req);
6909 }
6910 
hwrm_ring_alloc_send_msg(struct bnxt * bp,struct bnxt_ring_struct * ring,u32 ring_type,u32 map_index)6911 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
6912 				    struct bnxt_ring_struct *ring,
6913 				    u32 ring_type, u32 map_index)
6914 {
6915 	struct hwrm_ring_alloc_output *resp;
6916 	struct hwrm_ring_alloc_input *req;
6917 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
6918 	struct bnxt_ring_grp_info *grp_info;
6919 	int rc, err = 0;
6920 	u16 ring_id;
6921 
6922 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
6923 	if (rc)
6924 		goto exit;
6925 
6926 	req->enables = 0;
6927 	if (rmem->nr_pages > 1) {
6928 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
6929 		/* Page size is in log2 units */
6930 		req->page_size = BNXT_PAGE_SHIFT;
6931 		req->page_tbl_depth = 1;
6932 	} else {
6933 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
6934 	}
6935 	req->fbo = 0;
6936 	/* Association of ring index with doorbell index and MSIX number */
6937 	req->logical_id = cpu_to_le16(map_index);
6938 
6939 	switch (ring_type) {
6940 	case HWRM_RING_ALLOC_TX: {
6941 		struct bnxt_tx_ring_info *txr;
6942 		u16 flags = 0;
6943 
6944 		txr = container_of(ring, struct bnxt_tx_ring_info,
6945 				   tx_ring_struct);
6946 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
6947 		/* Association of transmit ring with completion ring */
6948 		grp_info = &bp->grp_info[ring->grp_idx];
6949 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
6950 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
6951 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6952 		req->queue_id = cpu_to_le16(ring->queue_id);
6953 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
6954 			req->cmpl_coal_cnt =
6955 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
6956 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
6957 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
6958 		req->flags = cpu_to_le16(flags);
6959 		break;
6960 	}
6961 	case HWRM_RING_ALLOC_RX:
6962 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6963 		req->length = cpu_to_le32(bp->rx_ring_mask + 1);
6964 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6965 			u16 flags = 0;
6966 
6967 			/* Association of rx ring with stats context */
6968 			grp_info = &bp->grp_info[ring->grp_idx];
6969 			req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
6970 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6971 			req->enables |= cpu_to_le32(
6972 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6973 			if (NET_IP_ALIGN == 2)
6974 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
6975 			req->flags = cpu_to_le16(flags);
6976 		}
6977 		break;
6978 	case HWRM_RING_ALLOC_AGG:
6979 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6980 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
6981 			/* Association of agg ring with rx ring */
6982 			grp_info = &bp->grp_info[ring->grp_idx];
6983 			req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
6984 			req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
6985 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6986 			req->enables |= cpu_to_le32(
6987 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
6988 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6989 		} else {
6990 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6991 		}
6992 		req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
6993 		break;
6994 	case HWRM_RING_ALLOC_CMPL:
6995 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
6996 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
6997 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6998 			/* Association of cp ring with nq */
6999 			grp_info = &bp->grp_info[map_index];
7000 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7001 			req->cq_handle = cpu_to_le64(ring->handle);
7002 			req->enables |= cpu_to_le32(
7003 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7004 		} else {
7005 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7006 		}
7007 		break;
7008 	case HWRM_RING_ALLOC_NQ:
7009 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7010 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7011 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7012 		break;
7013 	default:
7014 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7015 			   ring_type);
7016 		return -1;
7017 	}
7018 
7019 	resp = hwrm_req_hold(bp, req);
7020 	rc = hwrm_req_send(bp, req);
7021 	err = le16_to_cpu(resp->error_code);
7022 	ring_id = le16_to_cpu(resp->ring_id);
7023 	hwrm_req_drop(bp, req);
7024 
7025 exit:
7026 	if (rc || err) {
7027 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7028 			   ring_type, rc, err);
7029 		return -EIO;
7030 	}
7031 	ring->fw_ring_id = ring_id;
7032 	return rc;
7033 }
7034 
bnxt_hwrm_set_async_event_cr(struct bnxt * bp,int idx)7035 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7036 {
7037 	int rc;
7038 
7039 	if (BNXT_PF(bp)) {
7040 		struct hwrm_func_cfg_input *req;
7041 
7042 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7043 		if (rc)
7044 			return rc;
7045 
7046 		req->fid = cpu_to_le16(0xffff);
7047 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7048 		req->async_event_cr = cpu_to_le16(idx);
7049 		return hwrm_req_send(bp, req);
7050 	} else {
7051 		struct hwrm_func_vf_cfg_input *req;
7052 
7053 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7054 		if (rc)
7055 			return rc;
7056 
7057 		req->enables =
7058 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7059 		req->async_event_cr = cpu_to_le16(idx);
7060 		return hwrm_req_send(bp, req);
7061 	}
7062 }
7063 
bnxt_set_db_mask(struct bnxt * bp,struct bnxt_db_info * db,u32 ring_type)7064 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7065 			     u32 ring_type)
7066 {
7067 	switch (ring_type) {
7068 	case HWRM_RING_ALLOC_TX:
7069 		db->db_ring_mask = bp->tx_ring_mask;
7070 		break;
7071 	case HWRM_RING_ALLOC_RX:
7072 		db->db_ring_mask = bp->rx_ring_mask;
7073 		break;
7074 	case HWRM_RING_ALLOC_AGG:
7075 		db->db_ring_mask = bp->rx_agg_ring_mask;
7076 		break;
7077 	case HWRM_RING_ALLOC_CMPL:
7078 	case HWRM_RING_ALLOC_NQ:
7079 		db->db_ring_mask = bp->cp_ring_mask;
7080 		break;
7081 	}
7082 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7083 		db->db_epoch_mask = db->db_ring_mask + 1;
7084 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7085 	}
7086 }
7087 
bnxt_set_db(struct bnxt * bp,struct bnxt_db_info * db,u32 ring_type,u32 map_idx,u32 xid)7088 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7089 			u32 map_idx, u32 xid)
7090 {
7091 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7092 		switch (ring_type) {
7093 		case HWRM_RING_ALLOC_TX:
7094 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7095 			break;
7096 		case HWRM_RING_ALLOC_RX:
7097 		case HWRM_RING_ALLOC_AGG:
7098 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7099 			break;
7100 		case HWRM_RING_ALLOC_CMPL:
7101 			db->db_key64 = DBR_PATH_L2;
7102 			break;
7103 		case HWRM_RING_ALLOC_NQ:
7104 			db->db_key64 = DBR_PATH_L2;
7105 			break;
7106 		}
7107 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7108 
7109 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7110 			db->db_key64 |= DBR_VALID;
7111 
7112 		db->doorbell = bp->bar1 + bp->db_offset;
7113 	} else {
7114 		db->doorbell = bp->bar1 + map_idx * 0x80;
7115 		switch (ring_type) {
7116 		case HWRM_RING_ALLOC_TX:
7117 			db->db_key32 = DB_KEY_TX;
7118 			break;
7119 		case HWRM_RING_ALLOC_RX:
7120 		case HWRM_RING_ALLOC_AGG:
7121 			db->db_key32 = DB_KEY_RX;
7122 			break;
7123 		case HWRM_RING_ALLOC_CMPL:
7124 			db->db_key32 = DB_KEY_CP;
7125 			break;
7126 		}
7127 	}
7128 	bnxt_set_db_mask(bp, db, ring_type);
7129 }
7130 
bnxt_hwrm_rx_ring_alloc(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)7131 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7132 				   struct bnxt_rx_ring_info *rxr)
7133 {
7134 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7135 	struct bnxt_napi *bnapi = rxr->bnapi;
7136 	u32 type = HWRM_RING_ALLOC_RX;
7137 	u32 map_idx = bnapi->index;
7138 	int rc;
7139 
7140 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7141 	if (rc)
7142 		return rc;
7143 
7144 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7145 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7146 
7147 	return 0;
7148 }
7149 
bnxt_hwrm_rx_agg_ring_alloc(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)7150 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7151 				       struct bnxt_rx_ring_info *rxr)
7152 {
7153 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7154 	u32 type = HWRM_RING_ALLOC_AGG;
7155 	u32 grp_idx = ring->grp_idx;
7156 	u32 map_idx;
7157 	int rc;
7158 
7159 	map_idx = grp_idx + bp->rx_nr_rings;
7160 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7161 	if (rc)
7162 		return rc;
7163 
7164 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7165 		    ring->fw_ring_id);
7166 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7167 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7168 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7169 
7170 	return 0;
7171 }
7172 
bnxt_hwrm_ring_alloc(struct bnxt * bp)7173 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7174 {
7175 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7176 	int i, rc = 0;
7177 	u32 type;
7178 
7179 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7180 		type = HWRM_RING_ALLOC_NQ;
7181 	else
7182 		type = HWRM_RING_ALLOC_CMPL;
7183 	for (i = 0; i < bp->cp_nr_rings; i++) {
7184 		struct bnxt_napi *bnapi = bp->bnapi[i];
7185 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7186 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7187 		u32 map_idx = ring->map_idx;
7188 		unsigned int vector;
7189 
7190 		vector = bp->irq_tbl[map_idx].vector;
7191 		disable_irq_nosync(vector);
7192 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7193 		if (rc) {
7194 			enable_irq(vector);
7195 			goto err_out;
7196 		}
7197 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7198 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7199 		enable_irq(vector);
7200 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7201 
7202 		if (!i) {
7203 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7204 			if (rc)
7205 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7206 		}
7207 	}
7208 
7209 	type = HWRM_RING_ALLOC_TX;
7210 	for (i = 0; i < bp->tx_nr_rings; i++) {
7211 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7212 		struct bnxt_ring_struct *ring;
7213 		u32 map_idx;
7214 
7215 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7216 			struct bnxt_cp_ring_info *cpr2 = txr->tx_cpr;
7217 			struct bnxt_napi *bnapi = txr->bnapi;
7218 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7219 
7220 			ring = &cpr2->cp_ring_struct;
7221 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7222 			map_idx = bnapi->index;
7223 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7224 			if (rc)
7225 				goto err_out;
7226 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7227 				    ring->fw_ring_id);
7228 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7229 		}
7230 		ring = &txr->tx_ring_struct;
7231 		map_idx = i;
7232 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7233 		if (rc)
7234 			goto err_out;
7235 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
7236 	}
7237 
7238 	for (i = 0; i < bp->rx_nr_rings; i++) {
7239 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7240 
7241 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7242 		if (rc)
7243 			goto err_out;
7244 		/* If we have agg rings, post agg buffers first. */
7245 		if (!agg_rings)
7246 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7247 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7248 			struct bnxt_cp_ring_info *cpr2 = rxr->rx_cpr;
7249 			struct bnxt_napi *bnapi = rxr->bnapi;
7250 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7251 			struct bnxt_ring_struct *ring;
7252 			u32 map_idx = bnapi->index;
7253 
7254 			ring = &cpr2->cp_ring_struct;
7255 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7256 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7257 			if (rc)
7258 				goto err_out;
7259 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7260 				    ring->fw_ring_id);
7261 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7262 		}
7263 	}
7264 
7265 	if (agg_rings) {
7266 		for (i = 0; i < bp->rx_nr_rings; i++) {
7267 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7268 			if (rc)
7269 				goto err_out;
7270 		}
7271 	}
7272 err_out:
7273 	return rc;
7274 }
7275 
bnxt_cancel_dim(struct bnxt * bp)7276 static void bnxt_cancel_dim(struct bnxt *bp)
7277 {
7278 	int i;
7279 
7280 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7281 	 * if NAPI is enabled.
7282 	 */
7283 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7284 		return;
7285 
7286 	/* Make sure NAPI sees that the VNIC is disabled */
7287 	synchronize_net();
7288 	for (i = 0; i < bp->rx_nr_rings; i++) {
7289 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7290 		struct bnxt_napi *bnapi = rxr->bnapi;
7291 
7292 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7293 	}
7294 }
7295 
hwrm_ring_free_send_msg(struct bnxt * bp,struct bnxt_ring_struct * ring,u32 ring_type,int cmpl_ring_id)7296 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7297 				   struct bnxt_ring_struct *ring,
7298 				   u32 ring_type, int cmpl_ring_id)
7299 {
7300 	struct hwrm_ring_free_output *resp;
7301 	struct hwrm_ring_free_input *req;
7302 	u16 error_code = 0;
7303 	int rc;
7304 
7305 	if (BNXT_NO_FW_ACCESS(bp))
7306 		return 0;
7307 
7308 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7309 	if (rc)
7310 		goto exit;
7311 
7312 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7313 	req->ring_type = ring_type;
7314 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7315 
7316 	resp = hwrm_req_hold(bp, req);
7317 	rc = hwrm_req_send(bp, req);
7318 	error_code = le16_to_cpu(resp->error_code);
7319 	hwrm_req_drop(bp, req);
7320 exit:
7321 	if (rc || error_code) {
7322 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7323 			   ring_type, rc, error_code);
7324 		return -EIO;
7325 	}
7326 	return 0;
7327 }
7328 
bnxt_hwrm_rx_ring_free(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,bool close_path)7329 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7330 				   struct bnxt_rx_ring_info *rxr,
7331 				   bool close_path)
7332 {
7333 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7334 	u32 grp_idx = rxr->bnapi->index;
7335 	u32 cmpl_ring_id;
7336 
7337 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7338 		return;
7339 
7340 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7341 	hwrm_ring_free_send_msg(bp, ring,
7342 				RING_FREE_REQ_RING_TYPE_RX,
7343 				close_path ? cmpl_ring_id :
7344 				INVALID_HW_RING_ID);
7345 	ring->fw_ring_id = INVALID_HW_RING_ID;
7346 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7347 }
7348 
bnxt_hwrm_rx_agg_ring_free(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,bool close_path)7349 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7350 				       struct bnxt_rx_ring_info *rxr,
7351 				       bool close_path)
7352 {
7353 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7354 	u32 grp_idx = rxr->bnapi->index;
7355 	u32 type, cmpl_ring_id;
7356 
7357 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7358 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7359 	else
7360 		type = RING_FREE_REQ_RING_TYPE_RX;
7361 
7362 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7363 		return;
7364 
7365 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7366 	hwrm_ring_free_send_msg(bp, ring, type,
7367 				close_path ? cmpl_ring_id :
7368 				INVALID_HW_RING_ID);
7369 	ring->fw_ring_id = INVALID_HW_RING_ID;
7370 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7371 }
7372 
bnxt_hwrm_ring_free(struct bnxt * bp,bool close_path)7373 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7374 {
7375 	u32 type;
7376 	int i;
7377 
7378 	if (!bp->bnapi)
7379 		return;
7380 
7381 	for (i = 0; i < bp->tx_nr_rings; i++) {
7382 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7383 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7384 
7385 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7386 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
7387 
7388 			hwrm_ring_free_send_msg(bp, ring,
7389 						RING_FREE_REQ_RING_TYPE_TX,
7390 						close_path ? cmpl_ring_id :
7391 						INVALID_HW_RING_ID);
7392 			ring->fw_ring_id = INVALID_HW_RING_ID;
7393 		}
7394 	}
7395 
7396 	bnxt_cancel_dim(bp);
7397 	for (i = 0; i < bp->rx_nr_rings; i++) {
7398 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7399 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7400 	}
7401 
7402 	/* The completion rings are about to be freed.  After that the
7403 	 * IRQ doorbell will not work anymore.  So we need to disable
7404 	 * IRQ here.
7405 	 */
7406 	bnxt_disable_int_sync(bp);
7407 
7408 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7409 		type = RING_FREE_REQ_RING_TYPE_NQ;
7410 	else
7411 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7412 	for (i = 0; i < bp->cp_nr_rings; i++) {
7413 		struct bnxt_napi *bnapi = bp->bnapi[i];
7414 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7415 		struct bnxt_ring_struct *ring;
7416 		int j;
7417 
7418 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) {
7419 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
7420 
7421 			ring = &cpr2->cp_ring_struct;
7422 			if (ring->fw_ring_id == INVALID_HW_RING_ID)
7423 				continue;
7424 			hwrm_ring_free_send_msg(bp, ring,
7425 						RING_FREE_REQ_RING_TYPE_L2_CMPL,
7426 						INVALID_HW_RING_ID);
7427 			ring->fw_ring_id = INVALID_HW_RING_ID;
7428 		}
7429 		ring = &cpr->cp_ring_struct;
7430 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7431 			hwrm_ring_free_send_msg(bp, ring, type,
7432 						INVALID_HW_RING_ID);
7433 			ring->fw_ring_id = INVALID_HW_RING_ID;
7434 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7435 		}
7436 	}
7437 }
7438 
7439 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7440 			     bool shared);
7441 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7442 			   bool shared);
7443 
bnxt_hwrm_get_rings(struct bnxt * bp)7444 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7445 {
7446 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7447 	struct hwrm_func_qcfg_output *resp;
7448 	struct hwrm_func_qcfg_input *req;
7449 	int rc;
7450 
7451 	if (bp->hwrm_spec_code < 0x10601)
7452 		return 0;
7453 
7454 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7455 	if (rc)
7456 		return rc;
7457 
7458 	req->fid = cpu_to_le16(0xffff);
7459 	resp = hwrm_req_hold(bp, req);
7460 	rc = hwrm_req_send(bp, req);
7461 	if (rc) {
7462 		hwrm_req_drop(bp, req);
7463 		return rc;
7464 	}
7465 
7466 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7467 	if (BNXT_NEW_RM(bp)) {
7468 		u16 cp, stats;
7469 
7470 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7471 		hw_resc->resv_hw_ring_grps =
7472 			le32_to_cpu(resp->alloc_hw_ring_grps);
7473 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7474 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7475 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7476 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7477 		hw_resc->resv_irqs = cp;
7478 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7479 			int rx = hw_resc->resv_rx_rings;
7480 			int tx = hw_resc->resv_tx_rings;
7481 
7482 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7483 				rx >>= 1;
7484 			if (cp < (rx + tx)) {
7485 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7486 				if (rc)
7487 					goto get_rings_exit;
7488 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7489 					rx <<= 1;
7490 				hw_resc->resv_rx_rings = rx;
7491 				hw_resc->resv_tx_rings = tx;
7492 			}
7493 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7494 			hw_resc->resv_hw_ring_grps = rx;
7495 		}
7496 		hw_resc->resv_cp_rings = cp;
7497 		hw_resc->resv_stat_ctxs = stats;
7498 	}
7499 get_rings_exit:
7500 	hwrm_req_drop(bp, req);
7501 	return rc;
7502 }
7503 
__bnxt_hwrm_get_tx_rings(struct bnxt * bp,u16 fid,int * tx_rings)7504 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7505 {
7506 	struct hwrm_func_qcfg_output *resp;
7507 	struct hwrm_func_qcfg_input *req;
7508 	int rc;
7509 
7510 	if (bp->hwrm_spec_code < 0x10601)
7511 		return 0;
7512 
7513 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7514 	if (rc)
7515 		return rc;
7516 
7517 	req->fid = cpu_to_le16(fid);
7518 	resp = hwrm_req_hold(bp, req);
7519 	rc = hwrm_req_send(bp, req);
7520 	if (!rc)
7521 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7522 
7523 	hwrm_req_drop(bp, req);
7524 	return rc;
7525 }
7526 
7527 static bool bnxt_rfs_supported(struct bnxt *bp);
7528 
7529 static struct hwrm_func_cfg_input *
__bnxt_hwrm_reserve_pf_rings(struct bnxt * bp,struct bnxt_hw_rings * hwr)7530 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7531 {
7532 	struct hwrm_func_cfg_input *req;
7533 	u32 enables = 0;
7534 
7535 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7536 		return NULL;
7537 
7538 	req->fid = cpu_to_le16(0xffff);
7539 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7540 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7541 	if (BNXT_NEW_RM(bp)) {
7542 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7543 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7544 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7545 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7546 			enables |= hwr->cp_p5 ?
7547 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7548 		} else {
7549 			enables |= hwr->cp ?
7550 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7551 			enables |= hwr->grp ?
7552 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7553 		}
7554 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7555 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7556 					  0;
7557 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7558 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7559 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7560 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7561 			req->num_msix = cpu_to_le16(hwr->cp);
7562 		} else {
7563 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7564 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7565 		}
7566 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7567 		req->num_vnics = cpu_to_le16(hwr->vnic);
7568 	}
7569 	req->enables = cpu_to_le32(enables);
7570 	return req;
7571 }
7572 
7573 static struct hwrm_func_vf_cfg_input *
__bnxt_hwrm_reserve_vf_rings(struct bnxt * bp,struct bnxt_hw_rings * hwr)7574 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7575 {
7576 	struct hwrm_func_vf_cfg_input *req;
7577 	u32 enables = 0;
7578 
7579 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7580 		return NULL;
7581 
7582 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7583 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7584 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7585 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7586 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7587 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7588 		enables |= hwr->cp_p5 ?
7589 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7590 	} else {
7591 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7592 		enables |= hwr->grp ?
7593 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7594 	}
7595 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7596 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7597 
7598 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7599 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7600 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7601 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7602 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7603 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7604 	} else {
7605 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7606 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7607 	}
7608 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7609 	req->num_vnics = cpu_to_le16(hwr->vnic);
7610 
7611 	req->enables = cpu_to_le32(enables);
7612 	return req;
7613 }
7614 
7615 static int
bnxt_hwrm_reserve_pf_rings(struct bnxt * bp,struct bnxt_hw_rings * hwr)7616 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7617 {
7618 	struct hwrm_func_cfg_input *req;
7619 	int rc;
7620 
7621 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7622 	if (!req)
7623 		return -ENOMEM;
7624 
7625 	if (!req->enables) {
7626 		hwrm_req_drop(bp, req);
7627 		return 0;
7628 	}
7629 
7630 	rc = hwrm_req_send(bp, req);
7631 	if (rc)
7632 		return rc;
7633 
7634 	if (bp->hwrm_spec_code < 0x10601)
7635 		bp->hw_resc.resv_tx_rings = hwr->tx;
7636 
7637 	return bnxt_hwrm_get_rings(bp);
7638 }
7639 
7640 static int
bnxt_hwrm_reserve_vf_rings(struct bnxt * bp,struct bnxt_hw_rings * hwr)7641 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7642 {
7643 	struct hwrm_func_vf_cfg_input *req;
7644 	int rc;
7645 
7646 	if (!BNXT_NEW_RM(bp)) {
7647 		bp->hw_resc.resv_tx_rings = hwr->tx;
7648 		return 0;
7649 	}
7650 
7651 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7652 	if (!req)
7653 		return -ENOMEM;
7654 
7655 	rc = hwrm_req_send(bp, req);
7656 	if (rc)
7657 		return rc;
7658 
7659 	return bnxt_hwrm_get_rings(bp);
7660 }
7661 
bnxt_hwrm_reserve_rings(struct bnxt * bp,struct bnxt_hw_rings * hwr)7662 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7663 {
7664 	if (BNXT_PF(bp))
7665 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7666 	else
7667 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7668 }
7669 
bnxt_nq_rings_in_use(struct bnxt * bp)7670 int bnxt_nq_rings_in_use(struct bnxt *bp)
7671 {
7672 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7673 }
7674 
bnxt_cp_rings_in_use(struct bnxt * bp)7675 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7676 {
7677 	int cp;
7678 
7679 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7680 		return bnxt_nq_rings_in_use(bp);
7681 
7682 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7683 	return cp;
7684 }
7685 
bnxt_get_func_stat_ctxs(struct bnxt * bp)7686 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7687 {
7688 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7689 }
7690 
bnxt_get_total_rss_ctxs(struct bnxt * bp,struct bnxt_hw_rings * hwr)7691 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7692 {
7693 	if (!hwr->grp)
7694 		return 0;
7695 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7696 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7697 
7698 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7699 			rss_ctx *= hwr->vnic;
7700 		return rss_ctx;
7701 	}
7702 	if (BNXT_VF(bp))
7703 		return BNXT_VF_MAX_RSS_CTX;
7704 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7705 		return hwr->grp + 1;
7706 	return 1;
7707 }
7708 
7709 /* Check if a default RSS map needs to be setup.  This function is only
7710  * used on older firmware that does not require reserving RX rings.
7711  */
bnxt_check_rss_tbl_no_rmgr(struct bnxt * bp)7712 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7713 {
7714 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7715 
7716 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7717 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7718 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7719 		if (!netif_is_rxfh_configured(bp->dev))
7720 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7721 	}
7722 }
7723 
bnxt_get_total_vnics(struct bnxt * bp,int rx_rings)7724 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7725 {
7726 	if (bp->flags & BNXT_FLAG_RFS) {
7727 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7728 			return 2 + bp->num_rss_ctx;
7729 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7730 			return rx_rings + 1;
7731 	}
7732 	return 1;
7733 }
7734 
bnxt_need_reserve_rings(struct bnxt * bp)7735 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7736 {
7737 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7738 	int cp = bnxt_cp_rings_in_use(bp);
7739 	int nq = bnxt_nq_rings_in_use(bp);
7740 	int rx = bp->rx_nr_rings, stat;
7741 	int vnic, grp = rx;
7742 
7743 	/* Old firmware does not need RX ring reservations but we still
7744 	 * need to setup a default RSS map when needed.  With new firmware
7745 	 * we go through RX ring reservations first and then set up the
7746 	 * RSS map for the successfully reserved RX rings when needed.
7747 	 */
7748 	if (!BNXT_NEW_RM(bp))
7749 		bnxt_check_rss_tbl_no_rmgr(bp);
7750 
7751 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7752 	    bp->hwrm_spec_code >= 0x10601)
7753 		return true;
7754 
7755 	if (!BNXT_NEW_RM(bp))
7756 		return false;
7757 
7758 	vnic = bnxt_get_total_vnics(bp, rx);
7759 
7760 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7761 		rx <<= 1;
7762 	stat = bnxt_get_func_stat_ctxs(bp);
7763 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7764 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7765 	    (hw_resc->resv_hw_ring_grps != grp &&
7766 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7767 		return true;
7768 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7769 	    hw_resc->resv_irqs != nq)
7770 		return true;
7771 	return false;
7772 }
7773 
bnxt_copy_reserved_rings(struct bnxt * bp,struct bnxt_hw_rings * hwr)7774 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7775 {
7776 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7777 
7778 	hwr->tx = hw_resc->resv_tx_rings;
7779 	if (BNXT_NEW_RM(bp)) {
7780 		hwr->rx = hw_resc->resv_rx_rings;
7781 		hwr->cp = hw_resc->resv_irqs;
7782 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7783 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7784 		hwr->grp = hw_resc->resv_hw_ring_grps;
7785 		hwr->vnic = hw_resc->resv_vnics;
7786 		hwr->stat = hw_resc->resv_stat_ctxs;
7787 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7788 	}
7789 }
7790 
bnxt_rings_ok(struct bnxt * bp,struct bnxt_hw_rings * hwr)7791 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7792 {
7793 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7794 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7795 }
7796 
7797 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7798 
__bnxt_reserve_rings(struct bnxt * bp)7799 static int __bnxt_reserve_rings(struct bnxt *bp)
7800 {
7801 	struct bnxt_hw_rings hwr = {0};
7802 	int rx_rings, old_rx_rings, rc;
7803 	int cp = bp->cp_nr_rings;
7804 	int ulp_msix = 0;
7805 	bool sh = false;
7806 	int tx_cp;
7807 
7808 	if (!bnxt_need_reserve_rings(bp))
7809 		return 0;
7810 
7811 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7812 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7813 		if (!ulp_msix)
7814 			bnxt_set_ulp_stat_ctxs(bp, 0);
7815 
7816 		if (ulp_msix > bp->ulp_num_msix_want)
7817 			ulp_msix = bp->ulp_num_msix_want;
7818 		hwr.cp = cp + ulp_msix;
7819 	} else {
7820 		hwr.cp = bnxt_nq_rings_in_use(bp);
7821 	}
7822 
7823 	hwr.tx = bp->tx_nr_rings;
7824 	hwr.rx = bp->rx_nr_rings;
7825 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7826 		sh = true;
7827 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7828 		hwr.cp_p5 = hwr.rx + hwr.tx;
7829 
7830 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7831 
7832 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7833 		hwr.rx <<= 1;
7834 	hwr.grp = bp->rx_nr_rings;
7835 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7836 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7837 	old_rx_rings = bp->hw_resc.resv_rx_rings;
7838 
7839 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7840 	if (rc)
7841 		return rc;
7842 
7843 	bnxt_copy_reserved_rings(bp, &hwr);
7844 
7845 	rx_rings = hwr.rx;
7846 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7847 		if (hwr.rx >= 2) {
7848 			rx_rings = hwr.rx >> 1;
7849 		} else {
7850 			if (netif_running(bp->dev))
7851 				return -ENOMEM;
7852 
7853 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7854 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7855 			bp->dev->hw_features &= ~NETIF_F_LRO;
7856 			bp->dev->features &= ~NETIF_F_LRO;
7857 			bnxt_set_ring_params(bp);
7858 		}
7859 	}
7860 	rx_rings = min_t(int, rx_rings, hwr.grp);
7861 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7862 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7863 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7864 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7865 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7866 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7867 		hwr.rx = rx_rings << 1;
7868 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
7869 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
7870 	bp->tx_nr_rings = hwr.tx;
7871 
7872 	/* If we cannot reserve all the RX rings, reset the RSS map only
7873 	 * if absolutely necessary
7874 	 */
7875 	if (rx_rings != bp->rx_nr_rings) {
7876 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
7877 			    rx_rings, bp->rx_nr_rings);
7878 		if (netif_is_rxfh_configured(bp->dev) &&
7879 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
7880 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
7881 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
7882 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
7883 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
7884 		}
7885 	}
7886 	bp->rx_nr_rings = rx_rings;
7887 	bp->cp_nr_rings = hwr.cp;
7888 
7889 	if (!bnxt_rings_ok(bp, &hwr))
7890 		return -ENOMEM;
7891 
7892 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
7893 	    !netif_is_rxfh_configured(bp->dev))
7894 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7895 
7896 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
7897 		int resv_msix, resv_ctx, ulp_ctxs;
7898 		struct bnxt_hw_resc *hw_resc;
7899 
7900 		hw_resc = &bp->hw_resc;
7901 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
7902 		ulp_msix = min_t(int, resv_msix, ulp_msix);
7903 		bnxt_set_ulp_msix_num(bp, ulp_msix);
7904 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
7905 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
7906 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
7907 	}
7908 
7909 	return rc;
7910 }
7911 
bnxt_hwrm_check_vf_rings(struct bnxt * bp,struct bnxt_hw_rings * hwr)7912 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7913 {
7914 	struct hwrm_func_vf_cfg_input *req;
7915 	u32 flags;
7916 
7917 	if (!BNXT_NEW_RM(bp))
7918 		return 0;
7919 
7920 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7921 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
7922 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7923 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7924 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7925 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
7926 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
7927 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7928 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7929 
7930 	req->flags = cpu_to_le32(flags);
7931 	return hwrm_req_send_silent(bp, req);
7932 }
7933 
bnxt_hwrm_check_pf_rings(struct bnxt * bp,struct bnxt_hw_rings * hwr)7934 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7935 {
7936 	struct hwrm_func_cfg_input *req;
7937 	u32 flags;
7938 
7939 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7940 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
7941 	if (BNXT_NEW_RM(bp)) {
7942 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7943 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7944 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7945 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
7946 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7947 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
7948 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
7949 		else
7950 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7951 	}
7952 
7953 	req->flags = cpu_to_le32(flags);
7954 	return hwrm_req_send_silent(bp, req);
7955 }
7956 
bnxt_hwrm_check_rings(struct bnxt * bp,struct bnxt_hw_rings * hwr)7957 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7958 {
7959 	if (bp->hwrm_spec_code < 0x10801)
7960 		return 0;
7961 
7962 	if (BNXT_PF(bp))
7963 		return bnxt_hwrm_check_pf_rings(bp, hwr);
7964 
7965 	return bnxt_hwrm_check_vf_rings(bp, hwr);
7966 }
7967 
bnxt_hwrm_coal_params_qcaps(struct bnxt * bp)7968 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
7969 {
7970 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7971 	struct hwrm_ring_aggint_qcaps_output *resp;
7972 	struct hwrm_ring_aggint_qcaps_input *req;
7973 	int rc;
7974 
7975 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
7976 	coal_cap->num_cmpl_dma_aggr_max = 63;
7977 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
7978 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
7979 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
7980 	coal_cap->int_lat_tmr_min_max = 65535;
7981 	coal_cap->int_lat_tmr_max_max = 65535;
7982 	coal_cap->num_cmpl_aggr_int_max = 65535;
7983 	coal_cap->timer_units = 80;
7984 
7985 	if (bp->hwrm_spec_code < 0x10902)
7986 		return;
7987 
7988 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
7989 		return;
7990 
7991 	resp = hwrm_req_hold(bp, req);
7992 	rc = hwrm_req_send_silent(bp, req);
7993 	if (!rc) {
7994 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
7995 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
7996 		coal_cap->num_cmpl_dma_aggr_max =
7997 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
7998 		coal_cap->num_cmpl_dma_aggr_during_int_max =
7999 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8000 		coal_cap->cmpl_aggr_dma_tmr_max =
8001 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8002 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8003 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8004 		coal_cap->int_lat_tmr_min_max =
8005 			le16_to_cpu(resp->int_lat_tmr_min_max);
8006 		coal_cap->int_lat_tmr_max_max =
8007 			le16_to_cpu(resp->int_lat_tmr_max_max);
8008 		coal_cap->num_cmpl_aggr_int_max =
8009 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8010 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8011 	}
8012 	hwrm_req_drop(bp, req);
8013 }
8014 
bnxt_usec_to_coal_tmr(struct bnxt * bp,u16 usec)8015 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8016 {
8017 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8018 
8019 	return usec * 1000 / coal_cap->timer_units;
8020 }
8021 
bnxt_hwrm_set_coal_params(struct bnxt * bp,struct bnxt_coal * hw_coal,struct hwrm_ring_cmpl_ring_cfg_aggint_params_input * req)8022 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8023 	struct bnxt_coal *hw_coal,
8024 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8025 {
8026 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8027 	u16 val, tmr, max, flags = hw_coal->flags;
8028 	u32 cmpl_params = coal_cap->cmpl_params;
8029 
8030 	max = hw_coal->bufs_per_record * 128;
8031 	if (hw_coal->budget)
8032 		max = hw_coal->bufs_per_record * hw_coal->budget;
8033 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8034 
8035 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8036 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8037 
8038 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8039 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8040 
8041 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8042 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8043 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8044 
8045 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8046 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8047 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8048 
8049 	/* min timer set to 1/2 of interrupt timer */
8050 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8051 		val = tmr / 2;
8052 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8053 		req->int_lat_tmr_min = cpu_to_le16(val);
8054 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8055 	}
8056 
8057 	/* buf timer set to 1/4 of interrupt timer */
8058 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8059 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8060 
8061 	if (cmpl_params &
8062 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8063 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8064 		val = clamp_t(u16, tmr, 1,
8065 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8066 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8067 		req->enables |=
8068 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8069 	}
8070 
8071 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8072 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8073 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8074 	req->flags = cpu_to_le16(flags);
8075 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8076 }
8077 
__bnxt_hwrm_set_coal_nq(struct bnxt * bp,struct bnxt_napi * bnapi,struct bnxt_coal * hw_coal)8078 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8079 				   struct bnxt_coal *hw_coal)
8080 {
8081 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8082 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8083 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8084 	u32 nq_params = coal_cap->nq_params;
8085 	u16 tmr;
8086 	int rc;
8087 
8088 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8089 		return 0;
8090 
8091 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8092 	if (rc)
8093 		return rc;
8094 
8095 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8096 	req->flags =
8097 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8098 
8099 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8100 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8101 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8102 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8103 	return hwrm_req_send(bp, req);
8104 }
8105 
bnxt_hwrm_set_ring_coal(struct bnxt * bp,struct bnxt_napi * bnapi)8106 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8107 {
8108 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8109 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8110 	struct bnxt_coal coal;
8111 	int rc;
8112 
8113 	/* Tick values in micro seconds.
8114 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8115 	 */
8116 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8117 
8118 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8119 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8120 
8121 	if (!bnapi->rx_ring)
8122 		return -ENODEV;
8123 
8124 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8125 	if (rc)
8126 		return rc;
8127 
8128 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8129 
8130 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8131 
8132 	return hwrm_req_send(bp, req_rx);
8133 }
8134 
8135 static int
bnxt_hwrm_set_rx_coal(struct bnxt * bp,struct bnxt_napi * bnapi,struct hwrm_ring_cmpl_ring_cfg_aggint_params_input * req)8136 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8137 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8138 {
8139 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8140 
8141 	req->ring_id = cpu_to_le16(ring_id);
8142 	return hwrm_req_send(bp, req);
8143 }
8144 
8145 static int
bnxt_hwrm_set_tx_coal(struct bnxt * bp,struct bnxt_napi * bnapi,struct hwrm_ring_cmpl_ring_cfg_aggint_params_input * req)8146 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8147 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8148 {
8149 	struct bnxt_tx_ring_info *txr;
8150 	int i, rc;
8151 
8152 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8153 		u16 ring_id;
8154 
8155 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8156 		req->ring_id = cpu_to_le16(ring_id);
8157 		rc = hwrm_req_send(bp, req);
8158 		if (rc)
8159 			return rc;
8160 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8161 			return 0;
8162 	}
8163 	return 0;
8164 }
8165 
bnxt_hwrm_set_coal(struct bnxt * bp)8166 int bnxt_hwrm_set_coal(struct bnxt *bp)
8167 {
8168 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8169 	int i, rc;
8170 
8171 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8172 	if (rc)
8173 		return rc;
8174 
8175 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8176 	if (rc) {
8177 		hwrm_req_drop(bp, req_rx);
8178 		return rc;
8179 	}
8180 
8181 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8182 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8183 
8184 	hwrm_req_hold(bp, req_rx);
8185 	hwrm_req_hold(bp, req_tx);
8186 	for (i = 0; i < bp->cp_nr_rings; i++) {
8187 		struct bnxt_napi *bnapi = bp->bnapi[i];
8188 		struct bnxt_coal *hw_coal;
8189 
8190 		if (!bnapi->rx_ring)
8191 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8192 		else
8193 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8194 		if (rc)
8195 			break;
8196 
8197 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8198 			continue;
8199 
8200 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8201 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8202 			if (rc)
8203 				break;
8204 		}
8205 		if (bnapi->rx_ring)
8206 			hw_coal = &bp->rx_coal;
8207 		else
8208 			hw_coal = &bp->tx_coal;
8209 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8210 	}
8211 	hwrm_req_drop(bp, req_rx);
8212 	hwrm_req_drop(bp, req_tx);
8213 	return rc;
8214 }
8215 
bnxt_hwrm_stat_ctx_free(struct bnxt * bp)8216 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8217 {
8218 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8219 	struct hwrm_stat_ctx_free_input *req;
8220 	int i;
8221 
8222 	if (!bp->bnapi)
8223 		return;
8224 
8225 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8226 		return;
8227 
8228 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8229 		return;
8230 	if (BNXT_FW_MAJ(bp) <= 20) {
8231 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8232 			hwrm_req_drop(bp, req);
8233 			return;
8234 		}
8235 		hwrm_req_hold(bp, req0);
8236 	}
8237 	hwrm_req_hold(bp, req);
8238 	for (i = 0; i < bp->cp_nr_rings; i++) {
8239 		struct bnxt_napi *bnapi = bp->bnapi[i];
8240 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8241 
8242 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8243 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8244 			if (req0) {
8245 				req0->stat_ctx_id = req->stat_ctx_id;
8246 				hwrm_req_send(bp, req0);
8247 			}
8248 			hwrm_req_send(bp, req);
8249 
8250 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8251 		}
8252 	}
8253 	hwrm_req_drop(bp, req);
8254 	if (req0)
8255 		hwrm_req_drop(bp, req0);
8256 }
8257 
bnxt_hwrm_stat_ctx_alloc(struct bnxt * bp)8258 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8259 {
8260 	struct hwrm_stat_ctx_alloc_output *resp;
8261 	struct hwrm_stat_ctx_alloc_input *req;
8262 	int rc, i;
8263 
8264 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8265 		return 0;
8266 
8267 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8268 	if (rc)
8269 		return rc;
8270 
8271 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8272 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8273 
8274 	resp = hwrm_req_hold(bp, req);
8275 	for (i = 0; i < bp->cp_nr_rings; i++) {
8276 		struct bnxt_napi *bnapi = bp->bnapi[i];
8277 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8278 
8279 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8280 
8281 		rc = hwrm_req_send(bp, req);
8282 		if (rc)
8283 			break;
8284 
8285 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8286 
8287 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8288 	}
8289 	hwrm_req_drop(bp, req);
8290 	return rc;
8291 }
8292 
bnxt_hwrm_func_qcfg(struct bnxt * bp)8293 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8294 {
8295 	struct hwrm_func_qcfg_output *resp;
8296 	struct hwrm_func_qcfg_input *req;
8297 	u16 flags;
8298 	int rc;
8299 
8300 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8301 	if (rc)
8302 		return rc;
8303 
8304 	req->fid = cpu_to_le16(0xffff);
8305 	resp = hwrm_req_hold(bp, req);
8306 	rc = hwrm_req_send(bp, req);
8307 	if (rc)
8308 		goto func_qcfg_exit;
8309 
8310 #ifdef CONFIG_BNXT_SRIOV
8311 	if (BNXT_VF(bp)) {
8312 		struct bnxt_vf_info *vf = &bp->vf;
8313 
8314 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8315 	} else {
8316 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8317 	}
8318 #endif
8319 	flags = le16_to_cpu(resp->flags);
8320 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8321 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8322 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8323 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8324 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8325 	}
8326 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8327 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8328 
8329 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8330 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8331 
8332 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8333 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8334 
8335 	switch (resp->port_partition_type) {
8336 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8337 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8338 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8339 		bp->port_partition_type = resp->port_partition_type;
8340 		break;
8341 	}
8342 	if (bp->hwrm_spec_code < 0x10707 ||
8343 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8344 		bp->br_mode = BRIDGE_MODE_VEB;
8345 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8346 		bp->br_mode = BRIDGE_MODE_VEPA;
8347 	else
8348 		bp->br_mode = BRIDGE_MODE_UNDEF;
8349 
8350 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8351 	if (!bp->max_mtu)
8352 		bp->max_mtu = BNXT_MAX_MTU;
8353 
8354 	if (bp->db_size)
8355 		goto func_qcfg_exit;
8356 
8357 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8358 	if (BNXT_CHIP_P5(bp)) {
8359 		if (BNXT_PF(bp))
8360 			bp->db_offset = DB_PF_OFFSET_P5;
8361 		else
8362 			bp->db_offset = DB_VF_OFFSET_P5;
8363 	}
8364 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8365 				 1024);
8366 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8367 	    bp->db_size <= bp->db_offset)
8368 		bp->db_size = pci_resource_len(bp->pdev, 2);
8369 
8370 func_qcfg_exit:
8371 	hwrm_req_drop(bp, req);
8372 	return rc;
8373 }
8374 
bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type * ctxm,u8 init_val,u8 init_offset,bool init_mask_set)8375 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8376 				      u8 init_val, u8 init_offset,
8377 				      bool init_mask_set)
8378 {
8379 	ctxm->init_value = init_val;
8380 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8381 	if (init_mask_set)
8382 		ctxm->init_offset = init_offset * 4;
8383 	else
8384 		ctxm->init_value = 0;
8385 }
8386 
bnxt_alloc_all_ctx_pg_info(struct bnxt * bp,int ctx_max)8387 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8388 {
8389 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8390 	u16 type;
8391 
8392 	for (type = 0; type < ctx_max; type++) {
8393 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8394 		int n = 1;
8395 
8396 		if (!ctxm->max_entries || ctxm->pg_info)
8397 			continue;
8398 
8399 		if (ctxm->instance_bmap)
8400 			n = hweight32(ctxm->instance_bmap);
8401 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8402 		if (!ctxm->pg_info)
8403 			return -ENOMEM;
8404 	}
8405 	return 0;
8406 }
8407 
8408 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8409 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8410 
8411 #define BNXT_CTX_INIT_VALID(flags)	\
8412 	(!!((flags) &			\
8413 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8414 
bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt * bp)8415 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8416 {
8417 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8418 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8419 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8420 	u16 type;
8421 	int rc;
8422 
8423 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8424 	if (rc)
8425 		return rc;
8426 
8427 	if (!ctx) {
8428 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8429 		if (!ctx)
8430 			return -ENOMEM;
8431 		bp->ctx = ctx;
8432 	}
8433 
8434 	resp = hwrm_req_hold(bp, req);
8435 
8436 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8437 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8438 		u8 init_val, init_off, i;
8439 		u32 max_entries;
8440 		u16 entry_size;
8441 		__le32 *p;
8442 		u32 flags;
8443 
8444 		req->type = cpu_to_le16(type);
8445 		rc = hwrm_req_send(bp, req);
8446 		if (rc)
8447 			goto ctx_done;
8448 		flags = le32_to_cpu(resp->flags);
8449 		type = le16_to_cpu(resp->next_valid_type);
8450 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8451 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8452 			continue;
8453 		}
8454 		entry_size = le16_to_cpu(resp->entry_size);
8455 		max_entries = le32_to_cpu(resp->max_num_entries);
8456 		if (ctxm->mem_valid) {
8457 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8458 			    ctxm->entry_size != entry_size ||
8459 			    ctxm->max_entries != max_entries)
8460 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8461 			else
8462 				continue;
8463 		}
8464 		ctxm->type = le16_to_cpu(resp->type);
8465 		ctxm->entry_size = entry_size;
8466 		ctxm->flags = flags;
8467 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8468 		ctxm->entry_multiple = resp->entry_multiple;
8469 		ctxm->max_entries = max_entries;
8470 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8471 		init_val = resp->ctx_init_value;
8472 		init_off = resp->ctx_init_offset;
8473 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8474 					  BNXT_CTX_INIT_VALID(flags));
8475 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8476 					      BNXT_MAX_SPLIT_ENTRY);
8477 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8478 		     i++, p++)
8479 			ctxm->split[i] = le32_to_cpu(*p);
8480 	}
8481 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8482 
8483 ctx_done:
8484 	hwrm_req_drop(bp, req);
8485 	return rc;
8486 }
8487 
bnxt_hwrm_func_backing_store_qcaps(struct bnxt * bp)8488 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8489 {
8490 	struct hwrm_func_backing_store_qcaps_output *resp;
8491 	struct hwrm_func_backing_store_qcaps_input *req;
8492 	int rc;
8493 
8494 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8495 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8496 		return 0;
8497 
8498 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8499 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8500 
8501 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8502 	if (rc)
8503 		return rc;
8504 
8505 	resp = hwrm_req_hold(bp, req);
8506 	rc = hwrm_req_send_silent(bp, req);
8507 	if (!rc) {
8508 		struct bnxt_ctx_mem_type *ctxm;
8509 		struct bnxt_ctx_mem_info *ctx;
8510 		u8 init_val, init_idx = 0;
8511 		u16 init_mask;
8512 
8513 		ctx = bp->ctx;
8514 		if (!ctx) {
8515 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8516 			if (!ctx) {
8517 				rc = -ENOMEM;
8518 				goto ctx_err;
8519 			}
8520 			bp->ctx = ctx;
8521 		}
8522 		init_val = resp->ctx_kind_initializer;
8523 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8524 
8525 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8526 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8527 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8528 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8529 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8530 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8531 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8532 					  (init_mask & (1 << init_idx++)) != 0);
8533 
8534 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8535 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8536 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8537 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8538 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8539 					  (init_mask & (1 << init_idx++)) != 0);
8540 
8541 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8542 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8543 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8544 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8545 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8546 					  (init_mask & (1 << init_idx++)) != 0);
8547 
8548 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8549 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8550 		ctxm->max_entries = ctxm->vnic_entries +
8551 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8552 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8553 		bnxt_init_ctx_initializer(ctxm, init_val,
8554 					  resp->vnic_init_offset,
8555 					  (init_mask & (1 << init_idx++)) != 0);
8556 
8557 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8558 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8559 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8560 		bnxt_init_ctx_initializer(ctxm, init_val,
8561 					  resp->stat_init_offset,
8562 					  (init_mask & (1 << init_idx++)) != 0);
8563 
8564 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8565 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8566 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8567 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8568 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8569 		if (!ctxm->entry_multiple)
8570 			ctxm->entry_multiple = 1;
8571 
8572 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8573 
8574 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8575 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8576 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8577 		ctxm->mrav_num_entries_units =
8578 			le16_to_cpu(resp->mrav_num_entries_units);
8579 		bnxt_init_ctx_initializer(ctxm, init_val,
8580 					  resp->mrav_init_offset,
8581 					  (init_mask & (1 << init_idx++)) != 0);
8582 
8583 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8584 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8585 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8586 
8587 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8588 		if (!ctx->tqm_fp_rings_count)
8589 			ctx->tqm_fp_rings_count = bp->max_q;
8590 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8591 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8592 
8593 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8594 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8595 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8596 
8597 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8598 	} else {
8599 		rc = 0;
8600 	}
8601 ctx_err:
8602 	hwrm_req_drop(bp, req);
8603 	return rc;
8604 }
8605 
bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info * rmem,u8 * pg_attr,__le64 * pg_dir)8606 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8607 				  __le64 *pg_dir)
8608 {
8609 	if (!rmem->nr_pages)
8610 		return;
8611 
8612 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8613 	if (rmem->depth >= 1) {
8614 		if (rmem->depth == 2)
8615 			*pg_attr |= 2;
8616 		else
8617 			*pg_attr |= 1;
8618 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8619 	} else {
8620 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8621 	}
8622 }
8623 
8624 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8625 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8626 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8627 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8628 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8629 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8630 
bnxt_hwrm_func_backing_store_cfg(struct bnxt * bp,u32 enables)8631 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8632 {
8633 	struct hwrm_func_backing_store_cfg_input *req;
8634 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8635 	struct bnxt_ctx_pg_info *ctx_pg;
8636 	struct bnxt_ctx_mem_type *ctxm;
8637 	void **__req = (void **)&req;
8638 	u32 req_len = sizeof(*req);
8639 	__le32 *num_entries;
8640 	__le64 *pg_dir;
8641 	u32 flags = 0;
8642 	u8 *pg_attr;
8643 	u32 ena;
8644 	int rc;
8645 	int i;
8646 
8647 	if (!ctx)
8648 		return 0;
8649 
8650 	if (req_len > bp->hwrm_max_ext_req_len)
8651 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8652 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8653 	if (rc)
8654 		return rc;
8655 
8656 	req->enables = cpu_to_le32(enables);
8657 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8658 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8659 		ctx_pg = ctxm->pg_info;
8660 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8661 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8662 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8663 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8664 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8665 				      &req->qpc_pg_size_qpc_lvl,
8666 				      &req->qpc_page_dir);
8667 
8668 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8669 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8670 	}
8671 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8672 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8673 		ctx_pg = ctxm->pg_info;
8674 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8675 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8676 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8677 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8678 				      &req->srq_pg_size_srq_lvl,
8679 				      &req->srq_page_dir);
8680 	}
8681 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8682 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8683 		ctx_pg = ctxm->pg_info;
8684 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8685 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8686 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8687 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8688 				      &req->cq_pg_size_cq_lvl,
8689 				      &req->cq_page_dir);
8690 	}
8691 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8692 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8693 		ctx_pg = ctxm->pg_info;
8694 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8695 		req->vnic_num_ring_table_entries =
8696 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8697 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8698 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8699 				      &req->vnic_pg_size_vnic_lvl,
8700 				      &req->vnic_page_dir);
8701 	}
8702 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8703 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8704 		ctx_pg = ctxm->pg_info;
8705 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8706 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8707 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8708 				      &req->stat_pg_size_stat_lvl,
8709 				      &req->stat_page_dir);
8710 	}
8711 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8712 		u32 units;
8713 
8714 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8715 		ctx_pg = ctxm->pg_info;
8716 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8717 		units = ctxm->mrav_num_entries_units;
8718 		if (units) {
8719 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8720 			u32 entries;
8721 
8722 			num_mr = ctx_pg->entries - num_ah;
8723 			entries = ((num_mr / units) << 16) | (num_ah / units);
8724 			req->mrav_num_entries = cpu_to_le32(entries);
8725 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8726 		}
8727 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8728 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8729 				      &req->mrav_pg_size_mrav_lvl,
8730 				      &req->mrav_page_dir);
8731 	}
8732 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8733 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8734 		ctx_pg = ctxm->pg_info;
8735 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8736 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8737 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8738 				      &req->tim_pg_size_tim_lvl,
8739 				      &req->tim_page_dir);
8740 	}
8741 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8742 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8743 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8744 	     pg_dir = &req->tqm_sp_page_dir,
8745 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8746 	     ctx_pg = ctxm->pg_info;
8747 	     i < BNXT_MAX_TQM_RINGS;
8748 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8749 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8750 		if (!(enables & ena))
8751 			continue;
8752 
8753 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8754 		*num_entries = cpu_to_le32(ctx_pg->entries);
8755 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8756 	}
8757 	req->flags = cpu_to_le32(flags);
8758 	return hwrm_req_send(bp, req);
8759 }
8760 
bnxt_alloc_ctx_mem_blk(struct bnxt * bp,struct bnxt_ctx_pg_info * ctx_pg)8761 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8762 				  struct bnxt_ctx_pg_info *ctx_pg)
8763 {
8764 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8765 
8766 	rmem->page_size = BNXT_PAGE_SIZE;
8767 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8768 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8769 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8770 	if (rmem->depth >= 1)
8771 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8772 	return bnxt_alloc_ring(bp, rmem);
8773 }
8774 
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)8775 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8776 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8777 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8778 {
8779 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8780 	int rc;
8781 
8782 	if (!mem_size)
8783 		return -EINVAL;
8784 
8785 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8786 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8787 		ctx_pg->nr_pages = 0;
8788 		return -EINVAL;
8789 	}
8790 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8791 		int nr_tbls, i;
8792 
8793 		rmem->depth = 2;
8794 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8795 					     GFP_KERNEL);
8796 		if (!ctx_pg->ctx_pg_tbl)
8797 			return -ENOMEM;
8798 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8799 		rmem->nr_pages = nr_tbls;
8800 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8801 		if (rc)
8802 			return rc;
8803 		for (i = 0; i < nr_tbls; i++) {
8804 			struct bnxt_ctx_pg_info *pg_tbl;
8805 
8806 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8807 			if (!pg_tbl)
8808 				return -ENOMEM;
8809 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8810 			rmem = &pg_tbl->ring_mem;
8811 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8812 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8813 			rmem->depth = 1;
8814 			rmem->nr_pages = MAX_CTX_PAGES;
8815 			rmem->ctx_mem = ctxm;
8816 			if (i == (nr_tbls - 1)) {
8817 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8818 
8819 				if (rem)
8820 					rmem->nr_pages = rem;
8821 			}
8822 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8823 			if (rc)
8824 				break;
8825 		}
8826 	} else {
8827 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8828 		if (rmem->nr_pages > 1 || depth)
8829 			rmem->depth = 1;
8830 		rmem->ctx_mem = ctxm;
8831 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8832 	}
8833 	return rc;
8834 }
8835 
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)8836 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
8837 				    struct bnxt_ctx_pg_info *ctx_pg,
8838 				    void *buf, size_t offset, size_t head,
8839 				    size_t tail)
8840 {
8841 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8842 	size_t nr_pages = ctx_pg->nr_pages;
8843 	int page_size = rmem->page_size;
8844 	size_t len = 0, total_len = 0;
8845 	u16 depth = rmem->depth;
8846 
8847 	tail %= nr_pages * page_size;
8848 	do {
8849 		if (depth > 1) {
8850 			int i = head / (page_size * MAX_CTX_PAGES);
8851 			struct bnxt_ctx_pg_info *pg_tbl;
8852 
8853 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8854 			rmem = &pg_tbl->ring_mem;
8855 		}
8856 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
8857 		head += len;
8858 		offset += len;
8859 		total_len += len;
8860 		if (head >= nr_pages * page_size)
8861 			head = 0;
8862 	} while (head != tail);
8863 	return total_len;
8864 }
8865 
bnxt_free_ctx_pg_tbls(struct bnxt * bp,struct bnxt_ctx_pg_info * ctx_pg)8866 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
8867 				  struct bnxt_ctx_pg_info *ctx_pg)
8868 {
8869 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8870 
8871 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
8872 	    ctx_pg->ctx_pg_tbl) {
8873 		int i, nr_tbls = rmem->nr_pages;
8874 
8875 		for (i = 0; i < nr_tbls; i++) {
8876 			struct bnxt_ctx_pg_info *pg_tbl;
8877 			struct bnxt_ring_mem_info *rmem2;
8878 
8879 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8880 			if (!pg_tbl)
8881 				continue;
8882 			rmem2 = &pg_tbl->ring_mem;
8883 			bnxt_free_ring(bp, rmem2);
8884 			ctx_pg->ctx_pg_arr[i] = NULL;
8885 			kfree(pg_tbl);
8886 			ctx_pg->ctx_pg_tbl[i] = NULL;
8887 		}
8888 		kfree(ctx_pg->ctx_pg_tbl);
8889 		ctx_pg->ctx_pg_tbl = NULL;
8890 	}
8891 	bnxt_free_ring(bp, rmem);
8892 	ctx_pg->nr_pages = 0;
8893 }
8894 
bnxt_setup_ctxm_pg_tbls(struct bnxt * bp,struct bnxt_ctx_mem_type * ctxm,u32 entries,u8 pg_lvl)8895 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
8896 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
8897 				   u8 pg_lvl)
8898 {
8899 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8900 	int i, rc = 0, n = 1;
8901 	u32 mem_size;
8902 
8903 	if (!ctxm->entry_size || !ctx_pg)
8904 		return -EINVAL;
8905 	if (ctxm->instance_bmap)
8906 		n = hweight32(ctxm->instance_bmap);
8907 	if (ctxm->entry_multiple)
8908 		entries = roundup(entries, ctxm->entry_multiple);
8909 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
8910 	mem_size = entries * ctxm->entry_size;
8911 	for (i = 0; i < n && !rc; i++) {
8912 		ctx_pg[i].entries = entries;
8913 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
8914 					    ctxm->init_value ? ctxm : NULL);
8915 	}
8916 	if (!rc)
8917 		ctxm->mem_valid = 1;
8918 	return rc;
8919 }
8920 
bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt * bp,struct bnxt_ctx_mem_type * ctxm,bool last)8921 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
8922 					       struct bnxt_ctx_mem_type *ctxm,
8923 					       bool last)
8924 {
8925 	struct hwrm_func_backing_store_cfg_v2_input *req;
8926 	u32 instance_bmap = ctxm->instance_bmap;
8927 	int i, j, rc = 0, n = 1;
8928 	__le32 *p;
8929 
8930 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
8931 		return 0;
8932 
8933 	if (instance_bmap)
8934 		n = hweight32(ctxm->instance_bmap);
8935 	else
8936 		instance_bmap = 1;
8937 
8938 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
8939 	if (rc)
8940 		return rc;
8941 	hwrm_req_hold(bp, req);
8942 	req->type = cpu_to_le16(ctxm->type);
8943 	req->entry_size = cpu_to_le16(ctxm->entry_size);
8944 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
8945 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
8946 		struct bnxt_bs_trace_info *bs_trace;
8947 		u32 enables;
8948 
8949 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
8950 		req->enables = cpu_to_le32(enables);
8951 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
8952 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
8953 	}
8954 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
8955 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
8956 		p[i] = cpu_to_le32(ctxm->split[i]);
8957 	for (i = 0, j = 0; j < n && !rc; i++) {
8958 		struct bnxt_ctx_pg_info *ctx_pg;
8959 
8960 		if (!(instance_bmap & (1 << i)))
8961 			continue;
8962 		req->instance = cpu_to_le16(i);
8963 		ctx_pg = &ctxm->pg_info[j++];
8964 		if (!ctx_pg->entries)
8965 			continue;
8966 		req->num_entries = cpu_to_le32(ctx_pg->entries);
8967 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8968 				      &req->page_size_pbl_level,
8969 				      &req->page_dir);
8970 		if (last && j == n)
8971 			req->flags =
8972 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
8973 		rc = hwrm_req_send(bp, req);
8974 	}
8975 	hwrm_req_drop(bp, req);
8976 	return rc;
8977 }
8978 
bnxt_backing_store_cfg_v2(struct bnxt * bp,u32 ena)8979 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
8980 {
8981 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8982 	struct bnxt_ctx_mem_type *ctxm;
8983 	u16 last_type = BNXT_CTX_INV;
8984 	int rc = 0;
8985 	u16 type;
8986 
8987 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) {
8988 		ctxm = &ctx->ctx_arr[type];
8989 		if (!bnxt_bs_trace_avail(bp, type))
8990 			continue;
8991 		if (!ctxm->mem_valid) {
8992 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
8993 						     ctxm->max_entries, 1);
8994 			if (rc) {
8995 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
8996 					    type);
8997 				continue;
8998 			}
8999 			bnxt_bs_trace_init(bp, ctxm);
9000 		}
9001 		last_type = type;
9002 	}
9003 
9004 	if (last_type == BNXT_CTX_INV) {
9005 		if (!ena)
9006 			return 0;
9007 		else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
9008 			last_type = BNXT_CTX_MAX - 1;
9009 		else
9010 			last_type = BNXT_CTX_L2_MAX - 1;
9011 	}
9012 	ctx->ctx_arr[last_type].last = 1;
9013 
9014 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9015 		ctxm = &ctx->ctx_arr[type];
9016 
9017 		if (!ctxm->mem_valid)
9018 			continue;
9019 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9020 		if (rc)
9021 			return rc;
9022 	}
9023 	return 0;
9024 }
9025 
9026 /**
9027  * __bnxt_copy_ctx_mem - copy host context memory
9028  * @bp: The driver context
9029  * @ctxm: The pointer to the context memory type
9030  * @buf: The destination buffer or NULL to just obtain the length
9031  * @offset: The buffer offset to copy the data to
9032  * @head: The head offset of context memory to copy from
9033  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9034  *
9035  * This function is called for debugging purposes to dump the host context
9036  * used by the chip.
9037  *
9038  * Return: Length of memory copied
9039  */
__bnxt_copy_ctx_mem(struct bnxt * bp,struct bnxt_ctx_mem_type * ctxm,void * buf,size_t offset,size_t head,size_t tail)9040 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9041 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9042 				  size_t offset, size_t head, size_t tail)
9043 {
9044 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9045 	size_t len = 0, total_len = 0;
9046 	int i, n = 1;
9047 
9048 	if (!ctx_pg)
9049 		return 0;
9050 
9051 	if (ctxm->instance_bmap)
9052 		n = hweight32(ctxm->instance_bmap);
9053 	for (i = 0; i < n; i++) {
9054 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9055 					    tail);
9056 		offset += len;
9057 		total_len += len;
9058 	}
9059 	return total_len;
9060 }
9061 
bnxt_copy_ctx_mem(struct bnxt * bp,struct bnxt_ctx_mem_type * ctxm,void * buf,size_t offset)9062 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9063 			 void *buf, size_t offset)
9064 {
9065 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9066 
9067 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9068 }
9069 
bnxt_free_one_ctx_mem(struct bnxt * bp,struct bnxt_ctx_mem_type * ctxm,bool force)9070 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9071 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9072 {
9073 	struct bnxt_ctx_pg_info *ctx_pg;
9074 	int i, n = 1;
9075 
9076 	ctxm->last = 0;
9077 
9078 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9079 		return;
9080 
9081 	ctx_pg = ctxm->pg_info;
9082 	if (ctx_pg) {
9083 		if (ctxm->instance_bmap)
9084 			n = hweight32(ctxm->instance_bmap);
9085 		for (i = 0; i < n; i++)
9086 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9087 
9088 		kfree(ctx_pg);
9089 		ctxm->pg_info = NULL;
9090 		ctxm->mem_valid = 0;
9091 	}
9092 	memset(ctxm, 0, sizeof(*ctxm));
9093 }
9094 
bnxt_free_ctx_mem(struct bnxt * bp,bool force)9095 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9096 {
9097 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9098 	u16 type;
9099 
9100 	if (!ctx)
9101 		return;
9102 
9103 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9104 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9105 
9106 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9107 	if (force) {
9108 		kfree(ctx);
9109 		bp->ctx = NULL;
9110 	}
9111 }
9112 
bnxt_alloc_ctx_mem(struct bnxt * bp)9113 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9114 {
9115 	struct bnxt_ctx_mem_type *ctxm;
9116 	struct bnxt_ctx_mem_info *ctx;
9117 	u32 l2_qps, qp1_qps, max_qps;
9118 	u32 ena, entries_sp, entries;
9119 	u32 srqs, max_srqs, min;
9120 	u32 num_mr, num_ah;
9121 	u32 extra_srqs = 0;
9122 	u32 extra_qps = 0;
9123 	u32 fast_qpmd_qps;
9124 	u8 pg_lvl = 1;
9125 	int i, rc;
9126 
9127 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9128 	if (rc) {
9129 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9130 			   rc);
9131 		return rc;
9132 	}
9133 	ctx = bp->ctx;
9134 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9135 		return 0;
9136 
9137 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9138 	l2_qps = ctxm->qp_l2_entries;
9139 	qp1_qps = ctxm->qp_qp1_entries;
9140 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9141 	max_qps = ctxm->max_entries;
9142 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9143 	srqs = ctxm->srq_l2_entries;
9144 	max_srqs = ctxm->max_entries;
9145 	ena = 0;
9146 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9147 		pg_lvl = 2;
9148 		extra_qps = min_t(u32, 65536, max_qps - l2_qps - qp1_qps);
9149 		/* allocate extra qps if fw supports RoCE fast qp destroy feature */
9150 		extra_qps += fast_qpmd_qps;
9151 		extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9152 		if (fast_qpmd_qps)
9153 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9154 	}
9155 
9156 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9157 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9158 				     pg_lvl);
9159 	if (rc)
9160 		return rc;
9161 
9162 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9163 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9164 	if (rc)
9165 		return rc;
9166 
9167 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9168 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9169 				     extra_qps * 2, pg_lvl);
9170 	if (rc)
9171 		return rc;
9172 
9173 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9174 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9175 	if (rc)
9176 		return rc;
9177 
9178 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9179 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9180 	if (rc)
9181 		return rc;
9182 
9183 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9184 		goto skip_rdma;
9185 
9186 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9187 	/* 128K extra is needed to accommodate static AH context
9188 	 * allocation by f/w.
9189 	 */
9190 	num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9191 	num_ah = min_t(u32, num_mr, 1024 * 128);
9192 	ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9193 	if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9194 		ctxm->mrav_av_entries = num_ah;
9195 
9196 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9197 	if (rc)
9198 		return rc;
9199 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9200 
9201 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9202 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9203 	if (rc)
9204 		return rc;
9205 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9206 
9207 skip_rdma:
9208 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9209 	min = ctxm->min_entries;
9210 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9211 		     2 * (extra_qps + qp1_qps) + min;
9212 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9213 	if (rc)
9214 		return rc;
9215 
9216 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9217 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9218 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9219 	if (rc)
9220 		return rc;
9221 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9222 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9223 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9224 
9225 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9226 		rc = bnxt_backing_store_cfg_v2(bp, ena);
9227 	else
9228 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9229 	if (rc) {
9230 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9231 			   rc);
9232 		return rc;
9233 	}
9234 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9235 	return 0;
9236 }
9237 
bnxt_hwrm_crash_dump_mem_cfg(struct bnxt * bp)9238 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9239 {
9240 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9241 	u16 page_attr;
9242 	int rc;
9243 
9244 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9245 		return 0;
9246 
9247 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9248 	if (rc)
9249 		return rc;
9250 
9251 	if (BNXT_PAGE_SIZE == 0x2000)
9252 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9253 	else if (BNXT_PAGE_SIZE == 0x10000)
9254 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9255 	else
9256 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9257 	req->pg_size_lvl = cpu_to_le16(page_attr |
9258 				       bp->fw_crash_mem->ring_mem.depth);
9259 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9260 	req->size = cpu_to_le32(bp->fw_crash_len);
9261 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9262 	return hwrm_req_send(bp, req);
9263 }
9264 
bnxt_free_crash_dump_mem(struct bnxt * bp)9265 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9266 {
9267 	if (bp->fw_crash_mem) {
9268 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9269 		kfree(bp->fw_crash_mem);
9270 		bp->fw_crash_mem = NULL;
9271 	}
9272 }
9273 
bnxt_alloc_crash_dump_mem(struct bnxt * bp)9274 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9275 {
9276 	u32 mem_size = 0;
9277 	int rc;
9278 
9279 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9280 		return 0;
9281 
9282 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9283 	if (rc)
9284 		return rc;
9285 
9286 	mem_size = round_up(mem_size, 4);
9287 
9288 	/* keep and use the existing pages */
9289 	if (bp->fw_crash_mem &&
9290 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9291 		goto alloc_done;
9292 
9293 	if (bp->fw_crash_mem)
9294 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9295 	else
9296 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
9297 					   GFP_KERNEL);
9298 	if (!bp->fw_crash_mem)
9299 		return -ENOMEM;
9300 
9301 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9302 	if (rc) {
9303 		bnxt_free_crash_dump_mem(bp);
9304 		return rc;
9305 	}
9306 
9307 alloc_done:
9308 	bp->fw_crash_len = mem_size;
9309 	return 0;
9310 }
9311 
bnxt_hwrm_func_resc_qcaps(struct bnxt * bp,bool all)9312 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9313 {
9314 	struct hwrm_func_resource_qcaps_output *resp;
9315 	struct hwrm_func_resource_qcaps_input *req;
9316 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9317 	int rc;
9318 
9319 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9320 	if (rc)
9321 		return rc;
9322 
9323 	req->fid = cpu_to_le16(0xffff);
9324 	resp = hwrm_req_hold(bp, req);
9325 	rc = hwrm_req_send_silent(bp, req);
9326 	if (rc)
9327 		goto hwrm_func_resc_qcaps_exit;
9328 
9329 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9330 	if (!all)
9331 		goto hwrm_func_resc_qcaps_exit;
9332 
9333 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9334 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9335 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9336 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9337 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9338 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9339 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9340 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9341 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9342 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9343 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9344 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9345 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9346 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9347 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9348 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9349 
9350 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9351 		u16 max_msix = le16_to_cpu(resp->max_msix);
9352 
9353 		hw_resc->max_nqs = max_msix;
9354 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9355 	}
9356 
9357 	if (BNXT_PF(bp)) {
9358 		struct bnxt_pf_info *pf = &bp->pf;
9359 
9360 		pf->vf_resv_strategy =
9361 			le16_to_cpu(resp->vf_reservation_strategy);
9362 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9363 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9364 	}
9365 hwrm_func_resc_qcaps_exit:
9366 	hwrm_req_drop(bp, req);
9367 	return rc;
9368 }
9369 
__bnxt_hwrm_ptp_qcfg(struct bnxt * bp)9370 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9371 {
9372 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9373 	struct hwrm_port_mac_ptp_qcfg_input *req;
9374 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9375 	u8 flags;
9376 	int rc;
9377 
9378 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9379 		rc = -ENODEV;
9380 		goto no_ptp;
9381 	}
9382 
9383 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9384 	if (rc)
9385 		goto no_ptp;
9386 
9387 	req->port_id = cpu_to_le16(bp->pf.port_id);
9388 	resp = hwrm_req_hold(bp, req);
9389 	rc = hwrm_req_send(bp, req);
9390 	if (rc)
9391 		goto exit;
9392 
9393 	flags = resp->flags;
9394 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9395 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9396 		rc = -ENODEV;
9397 		goto exit;
9398 	}
9399 	if (!ptp) {
9400 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9401 		if (!ptp) {
9402 			rc = -ENOMEM;
9403 			goto exit;
9404 		}
9405 		ptp->bp = bp;
9406 		bp->ptp_cfg = ptp;
9407 	}
9408 
9409 	if (flags &
9410 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9411 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9412 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9413 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9414 	} else if (BNXT_CHIP_P5(bp)) {
9415 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9416 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9417 	} else {
9418 		rc = -ENODEV;
9419 		goto exit;
9420 	}
9421 	ptp->rtc_configured =
9422 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9423 	rc = bnxt_ptp_init(bp);
9424 	if (rc)
9425 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9426 exit:
9427 	hwrm_req_drop(bp, req);
9428 	if (!rc)
9429 		return 0;
9430 
9431 no_ptp:
9432 	bnxt_ptp_clear(bp);
9433 	kfree(ptp);
9434 	bp->ptp_cfg = NULL;
9435 	return rc;
9436 }
9437 
__bnxt_hwrm_func_qcaps(struct bnxt * bp)9438 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9439 {
9440 	struct hwrm_func_qcaps_output *resp;
9441 	struct hwrm_func_qcaps_input *req;
9442 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9443 	u32 flags, flags_ext, flags_ext2;
9444 	int rc;
9445 
9446 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9447 	if (rc)
9448 		return rc;
9449 
9450 	req->fid = cpu_to_le16(0xffff);
9451 	resp = hwrm_req_hold(bp, req);
9452 	rc = hwrm_req_send(bp, req);
9453 	if (rc)
9454 		goto hwrm_func_qcaps_exit;
9455 
9456 	flags = le32_to_cpu(resp->flags);
9457 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9458 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9459 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9460 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9461 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9462 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9463 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9464 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9465 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9466 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9467 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9468 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9469 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9470 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9471 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9472 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9473 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9474 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9475 
9476 	flags_ext = le32_to_cpu(resp->flags_ext);
9477 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9478 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9479 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9480 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9481 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9482 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9483 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9484 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9485 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9486 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9487 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9488 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9489 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9490 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9491 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9492 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9493 
9494 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9495 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9496 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9497 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9498 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9499 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9500 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9501 	if (BNXT_PF(bp) &&
9502 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9503 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9504 
9505 	bp->tx_push_thresh = 0;
9506 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9507 	    BNXT_FW_MAJ(bp) > 217)
9508 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9509 
9510 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9511 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9512 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9513 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9514 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9515 	if (!hw_resc->max_hw_ring_grps)
9516 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9517 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9518 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9519 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9520 
9521 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9522 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9523 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9524 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9525 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9526 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9527 
9528 	if (BNXT_PF(bp)) {
9529 		struct bnxt_pf_info *pf = &bp->pf;
9530 
9531 		pf->fw_fid = le16_to_cpu(resp->fid);
9532 		pf->port_id = le16_to_cpu(resp->port_id);
9533 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9534 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9535 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9536 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9537 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9538 			bp->flags |= BNXT_FLAG_WOL_CAP;
9539 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9540 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9541 		} else {
9542 			bnxt_ptp_clear(bp);
9543 			kfree(bp->ptp_cfg);
9544 			bp->ptp_cfg = NULL;
9545 		}
9546 	} else {
9547 #ifdef CONFIG_BNXT_SRIOV
9548 		struct bnxt_vf_info *vf = &bp->vf;
9549 
9550 		vf->fw_fid = le16_to_cpu(resp->fid);
9551 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9552 #endif
9553 	}
9554 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9555 
9556 hwrm_func_qcaps_exit:
9557 	hwrm_req_drop(bp, req);
9558 	return rc;
9559 }
9560 
bnxt_hwrm_dbg_qcaps(struct bnxt * bp)9561 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9562 {
9563 	struct hwrm_dbg_qcaps_output *resp;
9564 	struct hwrm_dbg_qcaps_input *req;
9565 	int rc;
9566 
9567 	bp->fw_dbg_cap = 0;
9568 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9569 		return;
9570 
9571 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9572 	if (rc)
9573 		return;
9574 
9575 	req->fid = cpu_to_le16(0xffff);
9576 	resp = hwrm_req_hold(bp, req);
9577 	rc = hwrm_req_send(bp, req);
9578 	if (rc)
9579 		goto hwrm_dbg_qcaps_exit;
9580 
9581 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9582 
9583 hwrm_dbg_qcaps_exit:
9584 	hwrm_req_drop(bp, req);
9585 }
9586 
9587 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9588 
bnxt_hwrm_func_qcaps(struct bnxt * bp)9589 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9590 {
9591 	int rc;
9592 
9593 	rc = __bnxt_hwrm_func_qcaps(bp);
9594 	if (rc)
9595 		return rc;
9596 
9597 	bnxt_hwrm_dbg_qcaps(bp);
9598 
9599 	rc = bnxt_hwrm_queue_qportcfg(bp);
9600 	if (rc) {
9601 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9602 		return rc;
9603 	}
9604 	if (bp->hwrm_spec_code >= 0x10803) {
9605 		rc = bnxt_alloc_ctx_mem(bp);
9606 		if (rc)
9607 			return rc;
9608 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9609 		if (!rc)
9610 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9611 	}
9612 	return 0;
9613 }
9614 
bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt * bp)9615 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9616 {
9617 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9618 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9619 	u32 flags;
9620 	int rc;
9621 
9622 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9623 		return 0;
9624 
9625 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9626 	if (rc)
9627 		return rc;
9628 
9629 	resp = hwrm_req_hold(bp, req);
9630 	rc = hwrm_req_send(bp, req);
9631 	if (rc)
9632 		goto hwrm_cfa_adv_qcaps_exit;
9633 
9634 	flags = le32_to_cpu(resp->flags);
9635 	if (flags &
9636 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9637 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9638 
9639 	if (flags &
9640 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9641 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9642 
9643 	if (flags &
9644 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9645 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9646 
9647 hwrm_cfa_adv_qcaps_exit:
9648 	hwrm_req_drop(bp, req);
9649 	return rc;
9650 }
9651 
__bnxt_alloc_fw_health(struct bnxt * bp)9652 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9653 {
9654 	if (bp->fw_health)
9655 		return 0;
9656 
9657 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9658 	if (!bp->fw_health)
9659 		return -ENOMEM;
9660 
9661 	mutex_init(&bp->fw_health->lock);
9662 	return 0;
9663 }
9664 
bnxt_alloc_fw_health(struct bnxt * bp)9665 static int bnxt_alloc_fw_health(struct bnxt *bp)
9666 {
9667 	int rc;
9668 
9669 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9670 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9671 		return 0;
9672 
9673 	rc = __bnxt_alloc_fw_health(bp);
9674 	if (rc) {
9675 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9676 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9677 		return rc;
9678 	}
9679 
9680 	return 0;
9681 }
9682 
__bnxt_map_fw_health_reg(struct bnxt * bp,u32 reg)9683 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9684 {
9685 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9686 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9687 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9688 }
9689 
bnxt_inv_fw_health_reg(struct bnxt * bp)9690 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9691 {
9692 	struct bnxt_fw_health *fw_health = bp->fw_health;
9693 	u32 reg_type;
9694 
9695 	if (!fw_health)
9696 		return;
9697 
9698 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9699 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9700 		fw_health->status_reliable = false;
9701 
9702 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9703 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9704 		fw_health->resets_reliable = false;
9705 }
9706 
bnxt_try_map_fw_health_reg(struct bnxt * bp)9707 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9708 {
9709 	void __iomem *hs;
9710 	u32 status_loc;
9711 	u32 reg_type;
9712 	u32 sig;
9713 
9714 	if (bp->fw_health)
9715 		bp->fw_health->status_reliable = false;
9716 
9717 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9718 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9719 
9720 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9721 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9722 		if (!bp->chip_num) {
9723 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9724 			bp->chip_num = readl(bp->bar0 +
9725 					     BNXT_FW_HEALTH_WIN_BASE +
9726 					     BNXT_GRC_REG_CHIP_NUM);
9727 		}
9728 		if (!BNXT_CHIP_P5_PLUS(bp))
9729 			return;
9730 
9731 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9732 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9733 	} else {
9734 		status_loc = readl(hs + offsetof(struct hcomm_status,
9735 						 fw_status_loc));
9736 	}
9737 
9738 	if (__bnxt_alloc_fw_health(bp)) {
9739 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9740 		return;
9741 	}
9742 
9743 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9744 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9745 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9746 		__bnxt_map_fw_health_reg(bp, status_loc);
9747 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9748 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9749 	}
9750 
9751 	bp->fw_health->status_reliable = true;
9752 }
9753 
bnxt_map_fw_health_regs(struct bnxt * bp)9754 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9755 {
9756 	struct bnxt_fw_health *fw_health = bp->fw_health;
9757 	u32 reg_base = 0xffffffff;
9758 	int i;
9759 
9760 	bp->fw_health->status_reliable = false;
9761 	bp->fw_health->resets_reliable = false;
9762 	/* Only pre-map the monitoring GRC registers using window 3 */
9763 	for (i = 0; i < 4; i++) {
9764 		u32 reg = fw_health->regs[i];
9765 
9766 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9767 			continue;
9768 		if (reg_base == 0xffffffff)
9769 			reg_base = reg & BNXT_GRC_BASE_MASK;
9770 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9771 			return -ERANGE;
9772 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9773 	}
9774 	bp->fw_health->status_reliable = true;
9775 	bp->fw_health->resets_reliable = true;
9776 	if (reg_base == 0xffffffff)
9777 		return 0;
9778 
9779 	__bnxt_map_fw_health_reg(bp, reg_base);
9780 	return 0;
9781 }
9782 
bnxt_remap_fw_health_regs(struct bnxt * bp)9783 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9784 {
9785 	if (!bp->fw_health)
9786 		return;
9787 
9788 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9789 		bp->fw_health->status_reliable = true;
9790 		bp->fw_health->resets_reliable = true;
9791 	} else {
9792 		bnxt_try_map_fw_health_reg(bp);
9793 	}
9794 }
9795 
bnxt_hwrm_error_recovery_qcfg(struct bnxt * bp)9796 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9797 {
9798 	struct bnxt_fw_health *fw_health = bp->fw_health;
9799 	struct hwrm_error_recovery_qcfg_output *resp;
9800 	struct hwrm_error_recovery_qcfg_input *req;
9801 	int rc, i;
9802 
9803 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9804 		return 0;
9805 
9806 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9807 	if (rc)
9808 		return rc;
9809 
9810 	resp = hwrm_req_hold(bp, req);
9811 	rc = hwrm_req_send(bp, req);
9812 	if (rc)
9813 		goto err_recovery_out;
9814 	fw_health->flags = le32_to_cpu(resp->flags);
9815 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9816 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9817 		rc = -EINVAL;
9818 		goto err_recovery_out;
9819 	}
9820 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9821 	fw_health->master_func_wait_dsecs =
9822 		le32_to_cpu(resp->master_func_wait_period);
9823 	fw_health->normal_func_wait_dsecs =
9824 		le32_to_cpu(resp->normal_func_wait_period);
9825 	fw_health->post_reset_wait_dsecs =
9826 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9827 	fw_health->post_reset_max_wait_dsecs =
9828 		le32_to_cpu(resp->max_bailout_time_after_reset);
9829 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9830 		le32_to_cpu(resp->fw_health_status_reg);
9831 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9832 		le32_to_cpu(resp->fw_heartbeat_reg);
9833 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9834 		le32_to_cpu(resp->fw_reset_cnt_reg);
9835 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9836 		le32_to_cpu(resp->reset_inprogress_reg);
9837 	fw_health->fw_reset_inprog_reg_mask =
9838 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9839 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9840 	if (fw_health->fw_reset_seq_cnt >= 16) {
9841 		rc = -EINVAL;
9842 		goto err_recovery_out;
9843 	}
9844 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9845 		fw_health->fw_reset_seq_regs[i] =
9846 			le32_to_cpu(resp->reset_reg[i]);
9847 		fw_health->fw_reset_seq_vals[i] =
9848 			le32_to_cpu(resp->reset_reg_val[i]);
9849 		fw_health->fw_reset_seq_delay_msec[i] =
9850 			resp->delay_after_reset[i];
9851 	}
9852 err_recovery_out:
9853 	hwrm_req_drop(bp, req);
9854 	if (!rc)
9855 		rc = bnxt_map_fw_health_regs(bp);
9856 	if (rc)
9857 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9858 	return rc;
9859 }
9860 
bnxt_hwrm_func_reset(struct bnxt * bp)9861 static int bnxt_hwrm_func_reset(struct bnxt *bp)
9862 {
9863 	struct hwrm_func_reset_input *req;
9864 	int rc;
9865 
9866 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
9867 	if (rc)
9868 		return rc;
9869 
9870 	req->enables = 0;
9871 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
9872 	return hwrm_req_send(bp, req);
9873 }
9874 
bnxt_nvm_cfg_ver_get(struct bnxt * bp)9875 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
9876 {
9877 	struct hwrm_nvm_get_dev_info_output nvm_info;
9878 
9879 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
9880 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
9881 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
9882 			 nvm_info.nvm_cfg_ver_upd);
9883 }
9884 
bnxt_hwrm_queue_qportcfg(struct bnxt * bp)9885 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
9886 {
9887 	struct hwrm_queue_qportcfg_output *resp;
9888 	struct hwrm_queue_qportcfg_input *req;
9889 	u8 i, j, *qptr;
9890 	bool no_rdma;
9891 	int rc = 0;
9892 
9893 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
9894 	if (rc)
9895 		return rc;
9896 
9897 	resp = hwrm_req_hold(bp, req);
9898 	rc = hwrm_req_send(bp, req);
9899 	if (rc)
9900 		goto qportcfg_exit;
9901 
9902 	if (!resp->max_configurable_queues) {
9903 		rc = -EINVAL;
9904 		goto qportcfg_exit;
9905 	}
9906 	bp->max_tc = resp->max_configurable_queues;
9907 	bp->max_lltc = resp->max_configurable_lossless_queues;
9908 	if (bp->max_tc > BNXT_MAX_QUEUE)
9909 		bp->max_tc = BNXT_MAX_QUEUE;
9910 
9911 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
9912 	qptr = &resp->queue_id0;
9913 	for (i = 0, j = 0; i < bp->max_tc; i++) {
9914 		bp->q_info[j].queue_id = *qptr;
9915 		bp->q_ids[i] = *qptr++;
9916 		bp->q_info[j].queue_profile = *qptr++;
9917 		bp->tc_to_qidx[j] = j;
9918 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
9919 		    (no_rdma && BNXT_PF(bp)))
9920 			j++;
9921 	}
9922 	bp->max_q = bp->max_tc;
9923 	bp->max_tc = max_t(u8, j, 1);
9924 
9925 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
9926 		bp->max_tc = 1;
9927 
9928 	if (bp->max_lltc > bp->max_tc)
9929 		bp->max_lltc = bp->max_tc;
9930 
9931 qportcfg_exit:
9932 	hwrm_req_drop(bp, req);
9933 	return rc;
9934 }
9935 
bnxt_hwrm_poll(struct bnxt * bp)9936 static int bnxt_hwrm_poll(struct bnxt *bp)
9937 {
9938 	struct hwrm_ver_get_input *req;
9939 	int rc;
9940 
9941 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9942 	if (rc)
9943 		return rc;
9944 
9945 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9946 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9947 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9948 
9949 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
9950 	rc = hwrm_req_send(bp, req);
9951 	return rc;
9952 }
9953 
bnxt_hwrm_ver_get(struct bnxt * bp)9954 static int bnxt_hwrm_ver_get(struct bnxt *bp)
9955 {
9956 	struct hwrm_ver_get_output *resp;
9957 	struct hwrm_ver_get_input *req;
9958 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
9959 	u32 dev_caps_cfg, hwrm_ver;
9960 	int rc, len;
9961 
9962 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9963 	if (rc)
9964 		return rc;
9965 
9966 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
9967 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
9968 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9969 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9970 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9971 
9972 	resp = hwrm_req_hold(bp, req);
9973 	rc = hwrm_req_send(bp, req);
9974 	if (rc)
9975 		goto hwrm_ver_get_exit;
9976 
9977 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
9978 
9979 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
9980 			     resp->hwrm_intf_min_8b << 8 |
9981 			     resp->hwrm_intf_upd_8b;
9982 	if (resp->hwrm_intf_maj_8b < 1) {
9983 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
9984 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9985 			    resp->hwrm_intf_upd_8b);
9986 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
9987 	}
9988 
9989 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
9990 			HWRM_VERSION_UPDATE;
9991 
9992 	if (bp->hwrm_spec_code > hwrm_ver)
9993 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9994 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
9995 			 HWRM_VERSION_UPDATE);
9996 	else
9997 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
9998 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
9999 			 resp->hwrm_intf_upd_8b);
10000 
10001 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10002 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10003 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10004 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10005 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10006 		len = FW_VER_STR_LEN;
10007 	} else {
10008 		fw_maj = resp->hwrm_fw_maj_8b;
10009 		fw_min = resp->hwrm_fw_min_8b;
10010 		fw_bld = resp->hwrm_fw_bld_8b;
10011 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10012 		len = BC_HWRM_STR_LEN;
10013 	}
10014 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10015 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10016 		 fw_rsv);
10017 
10018 	if (strlen(resp->active_pkg_name)) {
10019 		int fw_ver_len = strlen(bp->fw_ver_str);
10020 
10021 		snprintf(bp->fw_ver_str + fw_ver_len,
10022 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10023 			 resp->active_pkg_name);
10024 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10025 	}
10026 
10027 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10028 	if (!bp->hwrm_cmd_timeout)
10029 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10030 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10031 	if (!bp->hwrm_cmd_max_timeout)
10032 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10033 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
10034 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
10035 			    bp->hwrm_cmd_max_timeout / 1000);
10036 
10037 	if (resp->hwrm_intf_maj_8b >= 1) {
10038 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10039 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10040 	}
10041 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10042 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10043 
10044 	bp->chip_num = le16_to_cpu(resp->chip_num);
10045 	bp->chip_rev = resp->chip_rev;
10046 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10047 	    !resp->chip_metal)
10048 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10049 
10050 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10051 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10052 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10053 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10054 
10055 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10056 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10057 
10058 	if (dev_caps_cfg &
10059 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10060 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10061 
10062 	if (dev_caps_cfg &
10063 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10064 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10065 
10066 	if (dev_caps_cfg &
10067 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10068 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10069 
10070 hwrm_ver_get_exit:
10071 	hwrm_req_drop(bp, req);
10072 	return rc;
10073 }
10074 
bnxt_hwrm_fw_set_time(struct bnxt * bp)10075 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10076 {
10077 	struct hwrm_fw_set_time_input *req;
10078 	struct tm tm;
10079 	time64_t now = ktime_get_real_seconds();
10080 	int rc;
10081 
10082 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10083 	    bp->hwrm_spec_code < 0x10400)
10084 		return -EOPNOTSUPP;
10085 
10086 	time64_to_tm(now, 0, &tm);
10087 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10088 	if (rc)
10089 		return rc;
10090 
10091 	req->year = cpu_to_le16(1900 + tm.tm_year);
10092 	req->month = 1 + tm.tm_mon;
10093 	req->day = tm.tm_mday;
10094 	req->hour = tm.tm_hour;
10095 	req->minute = tm.tm_min;
10096 	req->second = tm.tm_sec;
10097 	return hwrm_req_send(bp, req);
10098 }
10099 
bnxt_add_one_ctr(u64 hw,u64 * sw,u64 mask)10100 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10101 {
10102 	u64 sw_tmp;
10103 
10104 	hw &= mask;
10105 	sw_tmp = (*sw & ~mask) | hw;
10106 	if (hw < (*sw & mask))
10107 		sw_tmp += mask + 1;
10108 	WRITE_ONCE(*sw, sw_tmp);
10109 }
10110 
__bnxt_accumulate_stats(__le64 * hw_stats,u64 * sw_stats,u64 * masks,int count,bool ignore_zero)10111 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10112 				    int count, bool ignore_zero)
10113 {
10114 	int i;
10115 
10116 	for (i = 0; i < count; i++) {
10117 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10118 
10119 		if (ignore_zero && !hw)
10120 			continue;
10121 
10122 		if (masks[i] == -1ULL)
10123 			sw_stats[i] = hw;
10124 		else
10125 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10126 	}
10127 }
10128 
bnxt_accumulate_stats(struct bnxt_stats_mem * stats)10129 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10130 {
10131 	if (!stats->hw_stats)
10132 		return;
10133 
10134 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10135 				stats->hw_masks, stats->len / 8, false);
10136 }
10137 
bnxt_accumulate_all_stats(struct bnxt * bp)10138 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10139 {
10140 	struct bnxt_stats_mem *ring0_stats;
10141 	bool ignore_zero = false;
10142 	int i;
10143 
10144 	/* Chip bug.  Counter intermittently becomes 0. */
10145 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10146 		ignore_zero = true;
10147 
10148 	for (i = 0; i < bp->cp_nr_rings; i++) {
10149 		struct bnxt_napi *bnapi = bp->bnapi[i];
10150 		struct bnxt_cp_ring_info *cpr;
10151 		struct bnxt_stats_mem *stats;
10152 
10153 		cpr = &bnapi->cp_ring;
10154 		stats = &cpr->stats;
10155 		if (!i)
10156 			ring0_stats = stats;
10157 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10158 					ring0_stats->hw_masks,
10159 					ring0_stats->len / 8, ignore_zero);
10160 	}
10161 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10162 		struct bnxt_stats_mem *stats = &bp->port_stats;
10163 		__le64 *hw_stats = stats->hw_stats;
10164 		u64 *sw_stats = stats->sw_stats;
10165 		u64 *masks = stats->hw_masks;
10166 		int cnt;
10167 
10168 		cnt = sizeof(struct rx_port_stats) / 8;
10169 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10170 
10171 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10172 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10173 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10174 		cnt = sizeof(struct tx_port_stats) / 8;
10175 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10176 	}
10177 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10178 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10179 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10180 	}
10181 }
10182 
bnxt_hwrm_port_qstats(struct bnxt * bp,u8 flags)10183 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10184 {
10185 	struct hwrm_port_qstats_input *req;
10186 	struct bnxt_pf_info *pf = &bp->pf;
10187 	int rc;
10188 
10189 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
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, HWRM_PORT_QSTATS);
10196 	if (rc)
10197 		return rc;
10198 
10199 	req->flags = flags;
10200 	req->port_id = cpu_to_le16(pf->port_id);
10201 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10202 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10203 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10204 	return hwrm_req_send(bp, req);
10205 }
10206 
bnxt_hwrm_port_qstats_ext(struct bnxt * bp,u8 flags)10207 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10208 {
10209 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10210 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10211 	struct hwrm_port_qstats_ext_output *resp_qs;
10212 	struct hwrm_port_qstats_ext_input *req_qs;
10213 	struct bnxt_pf_info *pf = &bp->pf;
10214 	u32 tx_stat_size;
10215 	int rc;
10216 
10217 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10218 		return 0;
10219 
10220 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10221 		return -EOPNOTSUPP;
10222 
10223 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10224 	if (rc)
10225 		return rc;
10226 
10227 	req_qs->flags = flags;
10228 	req_qs->port_id = cpu_to_le16(pf->port_id);
10229 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10230 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10231 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10232 		       sizeof(struct tx_port_stats_ext) : 0;
10233 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10234 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10235 	resp_qs = hwrm_req_hold(bp, req_qs);
10236 	rc = hwrm_req_send(bp, req_qs);
10237 	if (!rc) {
10238 		bp->fw_rx_stats_ext_size =
10239 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10240 		if (BNXT_FW_MAJ(bp) < 220 &&
10241 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10242 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10243 
10244 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10245 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10246 	} else {
10247 		bp->fw_rx_stats_ext_size = 0;
10248 		bp->fw_tx_stats_ext_size = 0;
10249 	}
10250 	hwrm_req_drop(bp, req_qs);
10251 
10252 	if (flags)
10253 		return rc;
10254 
10255 	if (bp->fw_tx_stats_ext_size <=
10256 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10257 		bp->pri2cos_valid = 0;
10258 		return rc;
10259 	}
10260 
10261 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10262 	if (rc)
10263 		return rc;
10264 
10265 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10266 
10267 	resp_qc = hwrm_req_hold(bp, req_qc);
10268 	rc = hwrm_req_send(bp, req_qc);
10269 	if (!rc) {
10270 		u8 *pri2cos;
10271 		int i, j;
10272 
10273 		pri2cos = &resp_qc->pri0_cos_queue_id;
10274 		for (i = 0; i < 8; i++) {
10275 			u8 queue_id = pri2cos[i];
10276 			u8 queue_idx;
10277 
10278 			/* Per port queue IDs start from 0, 10, 20, etc */
10279 			queue_idx = queue_id % 10;
10280 			if (queue_idx > BNXT_MAX_QUEUE) {
10281 				bp->pri2cos_valid = false;
10282 				hwrm_req_drop(bp, req_qc);
10283 				return rc;
10284 			}
10285 			for (j = 0; j < bp->max_q; j++) {
10286 				if (bp->q_ids[j] == queue_id)
10287 					bp->pri2cos_idx[i] = queue_idx;
10288 			}
10289 		}
10290 		bp->pri2cos_valid = true;
10291 	}
10292 	hwrm_req_drop(bp, req_qc);
10293 
10294 	return rc;
10295 }
10296 
bnxt_hwrm_free_tunnel_ports(struct bnxt * bp)10297 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10298 {
10299 	bnxt_hwrm_tunnel_dst_port_free(bp,
10300 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10301 	bnxt_hwrm_tunnel_dst_port_free(bp,
10302 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10303 }
10304 
bnxt_set_tpa(struct bnxt * bp,bool set_tpa)10305 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10306 {
10307 	int rc, i;
10308 	u32 tpa_flags = 0;
10309 
10310 	if (set_tpa)
10311 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10312 	else if (BNXT_NO_FW_ACCESS(bp))
10313 		return 0;
10314 	for (i = 0; i < bp->nr_vnics; i++) {
10315 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10316 		if (rc) {
10317 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10318 				   i, rc);
10319 			return rc;
10320 		}
10321 	}
10322 	return 0;
10323 }
10324 
bnxt_hwrm_clear_vnic_rss(struct bnxt * bp)10325 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10326 {
10327 	int i;
10328 
10329 	for (i = 0; i < bp->nr_vnics; i++)
10330 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10331 }
10332 
bnxt_clear_vnic(struct bnxt * bp)10333 static void bnxt_clear_vnic(struct bnxt *bp)
10334 {
10335 	if (!bp->vnic_info)
10336 		return;
10337 
10338 	bnxt_hwrm_clear_vnic_filter(bp);
10339 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10340 		/* clear all RSS setting before free vnic ctx */
10341 		bnxt_hwrm_clear_vnic_rss(bp);
10342 		bnxt_hwrm_vnic_ctx_free(bp);
10343 	}
10344 	/* before free the vnic, undo the vnic tpa settings */
10345 	if (bp->flags & BNXT_FLAG_TPA)
10346 		bnxt_set_tpa(bp, false);
10347 	bnxt_hwrm_vnic_free(bp);
10348 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10349 		bnxt_hwrm_vnic_ctx_free(bp);
10350 }
10351 
bnxt_hwrm_resource_free(struct bnxt * bp,bool close_path,bool irq_re_init)10352 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10353 				    bool irq_re_init)
10354 {
10355 	bnxt_clear_vnic(bp);
10356 	bnxt_hwrm_ring_free(bp, close_path);
10357 	bnxt_hwrm_ring_grp_free(bp);
10358 	if (irq_re_init) {
10359 		bnxt_hwrm_stat_ctx_free(bp);
10360 		bnxt_hwrm_free_tunnel_ports(bp);
10361 	}
10362 }
10363 
bnxt_hwrm_set_br_mode(struct bnxt * bp,u16 br_mode)10364 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10365 {
10366 	struct hwrm_func_cfg_input *req;
10367 	u8 evb_mode;
10368 	int rc;
10369 
10370 	if (br_mode == BRIDGE_MODE_VEB)
10371 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10372 	else if (br_mode == BRIDGE_MODE_VEPA)
10373 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10374 	else
10375 		return -EINVAL;
10376 
10377 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10378 	if (rc)
10379 		return rc;
10380 
10381 	req->fid = cpu_to_le16(0xffff);
10382 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10383 	req->evb_mode = evb_mode;
10384 	return hwrm_req_send(bp, req);
10385 }
10386 
bnxt_hwrm_set_cache_line_size(struct bnxt * bp,int size)10387 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10388 {
10389 	struct hwrm_func_cfg_input *req;
10390 	int rc;
10391 
10392 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10393 		return 0;
10394 
10395 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10396 	if (rc)
10397 		return rc;
10398 
10399 	req->fid = cpu_to_le16(0xffff);
10400 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10401 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10402 	if (size == 128)
10403 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10404 
10405 	return hwrm_req_send(bp, req);
10406 }
10407 
__bnxt_setup_vnic(struct bnxt * bp,struct bnxt_vnic_info * vnic)10408 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10409 {
10410 	int rc;
10411 
10412 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10413 		goto skip_rss_ctx;
10414 
10415 	/* allocate context for vnic */
10416 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10417 	if (rc) {
10418 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10419 			   vnic->vnic_id, rc);
10420 		goto vnic_setup_err;
10421 	}
10422 	bp->rsscos_nr_ctxs++;
10423 
10424 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10425 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10426 		if (rc) {
10427 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10428 				   vnic->vnic_id, rc);
10429 			goto vnic_setup_err;
10430 		}
10431 		bp->rsscos_nr_ctxs++;
10432 	}
10433 
10434 skip_rss_ctx:
10435 	/* configure default vnic, ring grp */
10436 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10437 	if (rc) {
10438 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10439 			   vnic->vnic_id, rc);
10440 		goto vnic_setup_err;
10441 	}
10442 
10443 	/* Enable RSS hashing on vnic */
10444 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10445 	if (rc) {
10446 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10447 			   vnic->vnic_id, rc);
10448 		goto vnic_setup_err;
10449 	}
10450 
10451 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10452 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10453 		if (rc) {
10454 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10455 				   vnic->vnic_id, rc);
10456 		}
10457 	}
10458 
10459 vnic_setup_err:
10460 	return rc;
10461 }
10462 
bnxt_hwrm_vnic_update(struct bnxt * bp,struct bnxt_vnic_info * vnic,u8 valid)10463 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10464 			  u8 valid)
10465 {
10466 	struct hwrm_vnic_update_input *req;
10467 	int rc;
10468 
10469 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10470 	if (rc)
10471 		return rc;
10472 
10473 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10474 
10475 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10476 		req->mru = cpu_to_le16(vnic->mru);
10477 
10478 	req->enables = cpu_to_le32(valid);
10479 
10480 	return hwrm_req_send(bp, req);
10481 }
10482 
bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt * bp,struct bnxt_vnic_info * vnic)10483 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10484 {
10485 	int rc;
10486 
10487 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10488 	if (rc) {
10489 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10490 			   vnic->vnic_id, rc);
10491 		return rc;
10492 	}
10493 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10494 	if (rc)
10495 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10496 			   vnic->vnic_id, rc);
10497 	return rc;
10498 }
10499 
__bnxt_setup_vnic_p5(struct bnxt * bp,struct bnxt_vnic_info * vnic)10500 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10501 {
10502 	int rc, i, nr_ctxs;
10503 
10504 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10505 	for (i = 0; i < nr_ctxs; i++) {
10506 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10507 		if (rc) {
10508 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10509 				   vnic->vnic_id, i, rc);
10510 			break;
10511 		}
10512 		bp->rsscos_nr_ctxs++;
10513 	}
10514 	if (i < nr_ctxs)
10515 		return -ENOMEM;
10516 
10517 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10518 	if (rc)
10519 		return rc;
10520 
10521 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10522 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10523 		if (rc) {
10524 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10525 				   vnic->vnic_id, rc);
10526 		}
10527 	}
10528 	return rc;
10529 }
10530 
bnxt_setup_vnic(struct bnxt * bp,struct bnxt_vnic_info * vnic)10531 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10532 {
10533 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10534 		return __bnxt_setup_vnic_p5(bp, vnic);
10535 	else
10536 		return __bnxt_setup_vnic(bp, vnic);
10537 }
10538 
bnxt_alloc_and_setup_vnic(struct bnxt * bp,struct bnxt_vnic_info * vnic,u16 start_rx_ring_idx,int rx_rings)10539 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10540 				     struct bnxt_vnic_info *vnic,
10541 				     u16 start_rx_ring_idx, int rx_rings)
10542 {
10543 	int rc;
10544 
10545 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10546 	if (rc) {
10547 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10548 			   vnic->vnic_id, rc);
10549 		return rc;
10550 	}
10551 	return bnxt_setup_vnic(bp, vnic);
10552 }
10553 
bnxt_alloc_rfs_vnics(struct bnxt * bp)10554 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10555 {
10556 	struct bnxt_vnic_info *vnic;
10557 	int i, rc = 0;
10558 
10559 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10560 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10561 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10562 	}
10563 
10564 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10565 		return 0;
10566 
10567 	for (i = 0; i < bp->rx_nr_rings; i++) {
10568 		u16 vnic_id = i + 1;
10569 		u16 ring_id = i;
10570 
10571 		if (vnic_id >= bp->nr_vnics)
10572 			break;
10573 
10574 		vnic = &bp->vnic_info[vnic_id];
10575 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10576 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10577 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10578 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10579 			break;
10580 	}
10581 	return rc;
10582 }
10583 
bnxt_del_one_rss_ctx(struct bnxt * bp,struct bnxt_rss_ctx * rss_ctx,bool all)10584 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10585 			  bool all)
10586 {
10587 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10588 	struct bnxt_filter_base *usr_fltr, *tmp;
10589 	struct bnxt_ntuple_filter *ntp_fltr;
10590 	int i;
10591 
10592 	if (netif_running(bp->dev)) {
10593 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10594 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10595 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10596 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10597 		}
10598 	}
10599 	if (!all)
10600 		return;
10601 
10602 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10603 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10604 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10605 			ntp_fltr = container_of(usr_fltr,
10606 						struct bnxt_ntuple_filter,
10607 						base);
10608 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10609 			bnxt_del_ntp_filter(bp, ntp_fltr);
10610 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10611 		}
10612 	}
10613 
10614 	if (vnic->rss_table)
10615 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10616 				  vnic->rss_table,
10617 				  vnic->rss_table_dma_addr);
10618 	bp->num_rss_ctx--;
10619 }
10620 
bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt * bp)10621 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10622 {
10623 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10624 	struct ethtool_rxfh_context *ctx;
10625 	unsigned long context;
10626 
10627 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10628 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10629 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10630 
10631 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10632 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10633 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10634 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10635 				   rss_ctx->index);
10636 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10637 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10638 		}
10639 	}
10640 }
10641 
bnxt_clear_rss_ctxs(struct bnxt * bp)10642 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10643 {
10644 	struct ethtool_rxfh_context *ctx;
10645 	unsigned long context;
10646 
10647 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10648 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10649 
10650 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10651 	}
10652 }
10653 
10654 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
bnxt_promisc_ok(struct bnxt * bp)10655 static bool bnxt_promisc_ok(struct bnxt *bp)
10656 {
10657 #ifdef CONFIG_BNXT_SRIOV
10658 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10659 		return false;
10660 #endif
10661 	return true;
10662 }
10663 
bnxt_setup_nitroa0_vnic(struct bnxt * bp)10664 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10665 {
10666 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10667 	unsigned int rc = 0;
10668 
10669 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10670 	if (rc) {
10671 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10672 			   rc);
10673 		return rc;
10674 	}
10675 
10676 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10677 	if (rc) {
10678 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10679 			   rc);
10680 		return rc;
10681 	}
10682 	return rc;
10683 }
10684 
10685 static int bnxt_cfg_rx_mode(struct bnxt *);
10686 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10687 
bnxt_init_chip(struct bnxt * bp,bool irq_re_init)10688 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10689 {
10690 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10691 	int rc = 0;
10692 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10693 
10694 	if (irq_re_init) {
10695 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10696 		if (rc) {
10697 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10698 				   rc);
10699 			goto err_out;
10700 		}
10701 	}
10702 
10703 	rc = bnxt_hwrm_ring_alloc(bp);
10704 	if (rc) {
10705 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10706 		goto err_out;
10707 	}
10708 
10709 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10710 	if (rc) {
10711 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10712 		goto err_out;
10713 	}
10714 
10715 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10716 		rx_nr_rings--;
10717 
10718 	/* default vnic 0 */
10719 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10720 	if (rc) {
10721 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10722 		goto err_out;
10723 	}
10724 
10725 	if (BNXT_VF(bp))
10726 		bnxt_hwrm_func_qcfg(bp);
10727 
10728 	rc = bnxt_setup_vnic(bp, vnic);
10729 	if (rc)
10730 		goto err_out;
10731 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10732 		bnxt_hwrm_update_rss_hash_cfg(bp);
10733 
10734 	if (bp->flags & BNXT_FLAG_RFS) {
10735 		rc = bnxt_alloc_rfs_vnics(bp);
10736 		if (rc)
10737 			goto err_out;
10738 	}
10739 
10740 	if (bp->flags & BNXT_FLAG_TPA) {
10741 		rc = bnxt_set_tpa(bp, true);
10742 		if (rc)
10743 			goto err_out;
10744 	}
10745 
10746 	if (BNXT_VF(bp))
10747 		bnxt_update_vf_mac(bp);
10748 
10749 	/* Filter for default vnic 0 */
10750 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10751 	if (rc) {
10752 		if (BNXT_VF(bp) && rc == -ENODEV)
10753 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10754 		else
10755 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10756 		goto err_out;
10757 	}
10758 	vnic->uc_filter_count = 1;
10759 
10760 	vnic->rx_mask = 0;
10761 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10762 		goto skip_rx_mask;
10763 
10764 	if (bp->dev->flags & IFF_BROADCAST)
10765 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10766 
10767 	if (bp->dev->flags & IFF_PROMISC)
10768 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10769 
10770 	if (bp->dev->flags & IFF_ALLMULTI) {
10771 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10772 		vnic->mc_list_count = 0;
10773 	} else if (bp->dev->flags & IFF_MULTICAST) {
10774 		u32 mask = 0;
10775 
10776 		bnxt_mc_list_updated(bp, &mask);
10777 		vnic->rx_mask |= mask;
10778 	}
10779 
10780 	rc = bnxt_cfg_rx_mode(bp);
10781 	if (rc)
10782 		goto err_out;
10783 
10784 skip_rx_mask:
10785 	rc = bnxt_hwrm_set_coal(bp);
10786 	if (rc)
10787 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10788 				rc);
10789 
10790 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10791 		rc = bnxt_setup_nitroa0_vnic(bp);
10792 		if (rc)
10793 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10794 				   rc);
10795 	}
10796 
10797 	if (BNXT_VF(bp)) {
10798 		bnxt_hwrm_func_qcfg(bp);
10799 		netdev_update_features(bp->dev);
10800 	}
10801 
10802 	return 0;
10803 
10804 err_out:
10805 	bnxt_hwrm_resource_free(bp, 0, true);
10806 
10807 	return rc;
10808 }
10809 
bnxt_shutdown_nic(struct bnxt * bp,bool irq_re_init)10810 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10811 {
10812 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10813 	return 0;
10814 }
10815 
bnxt_init_nic(struct bnxt * bp,bool irq_re_init)10816 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10817 {
10818 	bnxt_init_cp_rings(bp);
10819 	bnxt_init_rx_rings(bp);
10820 	bnxt_init_tx_rings(bp);
10821 	bnxt_init_ring_grps(bp, irq_re_init);
10822 	bnxt_init_vnics(bp);
10823 
10824 	return bnxt_init_chip(bp, irq_re_init);
10825 }
10826 
bnxt_set_real_num_queues(struct bnxt * bp)10827 static int bnxt_set_real_num_queues(struct bnxt *bp)
10828 {
10829 	int rc;
10830 	struct net_device *dev = bp->dev;
10831 
10832 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10833 					  bp->tx_nr_rings_xdp);
10834 	if (rc)
10835 		return rc;
10836 
10837 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10838 	if (rc)
10839 		return rc;
10840 
10841 #ifdef CONFIG_RFS_ACCEL
10842 	if (bp->flags & BNXT_FLAG_RFS)
10843 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
10844 #endif
10845 
10846 	return rc;
10847 }
10848 
__bnxt_trim_rings(struct bnxt * bp,int * rx,int * tx,int max,bool shared)10849 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10850 			     bool shared)
10851 {
10852 	int _rx = *rx, _tx = *tx;
10853 
10854 	if (shared) {
10855 		*rx = min_t(int, _rx, max);
10856 		*tx = min_t(int, _tx, max);
10857 	} else {
10858 		if (max < 2)
10859 			return -ENOMEM;
10860 
10861 		while (_rx + _tx > max) {
10862 			if (_rx > _tx && _rx > 1)
10863 				_rx--;
10864 			else if (_tx > 1)
10865 				_tx--;
10866 		}
10867 		*rx = _rx;
10868 		*tx = _tx;
10869 	}
10870 	return 0;
10871 }
10872 
__bnxt_num_tx_to_cp(struct bnxt * bp,int tx,int tx_sets,int tx_xdp)10873 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
10874 {
10875 	return (tx - tx_xdp) / tx_sets + tx_xdp;
10876 }
10877 
bnxt_num_tx_to_cp(struct bnxt * bp,int tx)10878 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
10879 {
10880 	int tcs = bp->num_tc;
10881 
10882 	if (!tcs)
10883 		tcs = 1;
10884 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
10885 }
10886 
bnxt_num_cp_to_tx(struct bnxt * bp,int tx_cp)10887 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
10888 {
10889 	int tcs = bp->num_tc;
10890 
10891 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
10892 	       bp->tx_nr_rings_xdp;
10893 }
10894 
bnxt_trim_rings(struct bnxt * bp,int * rx,int * tx,int max,bool sh)10895 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10896 			   bool sh)
10897 {
10898 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
10899 
10900 	if (tx_cp != *tx) {
10901 		int tx_saved = tx_cp, rc;
10902 
10903 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
10904 		if (rc)
10905 			return rc;
10906 		if (tx_cp != tx_saved)
10907 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
10908 		return 0;
10909 	}
10910 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
10911 }
10912 
bnxt_setup_msix(struct bnxt * bp)10913 static void bnxt_setup_msix(struct bnxt *bp)
10914 {
10915 	const int len = sizeof(bp->irq_tbl[0].name);
10916 	struct net_device *dev = bp->dev;
10917 	int tcs, i;
10918 
10919 	tcs = bp->num_tc;
10920 	if (tcs) {
10921 		int i, off, count;
10922 
10923 		for (i = 0; i < tcs; i++) {
10924 			count = bp->tx_nr_rings_per_tc;
10925 			off = BNXT_TC_TO_RING_BASE(bp, i);
10926 			netdev_set_tc_queue(dev, i, count, off);
10927 		}
10928 	}
10929 
10930 	for (i = 0; i < bp->cp_nr_rings; i++) {
10931 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10932 		char *attr;
10933 
10934 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
10935 			attr = "TxRx";
10936 		else if (i < bp->rx_nr_rings)
10937 			attr = "rx";
10938 		else
10939 			attr = "tx";
10940 
10941 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
10942 			 attr, i);
10943 		bp->irq_tbl[map_idx].handler = bnxt_msix;
10944 	}
10945 }
10946 
10947 static int bnxt_init_int_mode(struct bnxt *bp);
10948 
bnxt_change_msix(struct bnxt * bp,int total)10949 static int bnxt_change_msix(struct bnxt *bp, int total)
10950 {
10951 	struct msi_map map;
10952 	int i;
10953 
10954 	/* add MSIX to the end if needed */
10955 	for (i = bp->total_irqs; i < total; i++) {
10956 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
10957 		if (map.index < 0)
10958 			return bp->total_irqs;
10959 		bp->irq_tbl[i].vector = map.virq;
10960 		bp->total_irqs++;
10961 	}
10962 
10963 	/* trim MSIX from the end if needed */
10964 	for (i = bp->total_irqs; i > total; i--) {
10965 		map.index = i - 1;
10966 		map.virq = bp->irq_tbl[i - 1].vector;
10967 		pci_msix_free_irq(bp->pdev, map);
10968 		bp->total_irqs--;
10969 	}
10970 	return bp->total_irqs;
10971 }
10972 
bnxt_setup_int_mode(struct bnxt * bp)10973 static int bnxt_setup_int_mode(struct bnxt *bp)
10974 {
10975 	int rc;
10976 
10977 	if (!bp->irq_tbl) {
10978 		rc = bnxt_init_int_mode(bp);
10979 		if (rc || !bp->irq_tbl)
10980 			return rc ?: -ENODEV;
10981 	}
10982 
10983 	bnxt_setup_msix(bp);
10984 
10985 	rc = bnxt_set_real_num_queues(bp);
10986 	return rc;
10987 }
10988 
bnxt_get_max_func_rss_ctxs(struct bnxt * bp)10989 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
10990 {
10991 	return bp->hw_resc.max_rsscos_ctxs;
10992 }
10993 
bnxt_get_max_func_vnics(struct bnxt * bp)10994 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
10995 {
10996 	return bp->hw_resc.max_vnics;
10997 }
10998 
bnxt_get_max_func_stat_ctxs(struct bnxt * bp)10999 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11000 {
11001 	return bp->hw_resc.max_stat_ctxs;
11002 }
11003 
bnxt_get_max_func_cp_rings(struct bnxt * bp)11004 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11005 {
11006 	return bp->hw_resc.max_cp_rings;
11007 }
11008 
bnxt_get_max_func_cp_rings_for_en(struct bnxt * bp)11009 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11010 {
11011 	unsigned int cp = bp->hw_resc.max_cp_rings;
11012 
11013 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11014 		cp -= bnxt_get_ulp_msix_num(bp);
11015 
11016 	return cp;
11017 }
11018 
bnxt_get_max_func_irqs(struct bnxt * bp)11019 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11020 {
11021 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11022 
11023 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11024 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11025 
11026 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11027 }
11028 
bnxt_set_max_func_irqs(struct bnxt * bp,unsigned int max_irqs)11029 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11030 {
11031 	bp->hw_resc.max_irqs = max_irqs;
11032 }
11033 
bnxt_get_avail_cp_rings_for_en(struct bnxt * bp)11034 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11035 {
11036 	unsigned int cp;
11037 
11038 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11039 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11040 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11041 	else
11042 		return cp - bp->cp_nr_rings;
11043 }
11044 
bnxt_get_avail_stat_ctxs_for_en(struct bnxt * bp)11045 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11046 {
11047 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11048 }
11049 
bnxt_get_avail_msix(struct bnxt * bp,int num)11050 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11051 {
11052 	int max_irq = bnxt_get_max_func_irqs(bp);
11053 	int total_req = bp->cp_nr_rings + num;
11054 
11055 	if (max_irq < total_req) {
11056 		num = max_irq - bp->cp_nr_rings;
11057 		if (num <= 0)
11058 			return 0;
11059 	}
11060 	return num;
11061 }
11062 
bnxt_get_num_msix(struct bnxt * bp)11063 static int bnxt_get_num_msix(struct bnxt *bp)
11064 {
11065 	if (!BNXT_NEW_RM(bp))
11066 		return bnxt_get_max_func_irqs(bp);
11067 
11068 	return bnxt_nq_rings_in_use(bp);
11069 }
11070 
bnxt_init_int_mode(struct bnxt * bp)11071 static int bnxt_init_int_mode(struct bnxt *bp)
11072 {
11073 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11074 
11075 	total_vecs = bnxt_get_num_msix(bp);
11076 	max = bnxt_get_max_func_irqs(bp);
11077 	if (total_vecs > max)
11078 		total_vecs = max;
11079 
11080 	if (!total_vecs)
11081 		return 0;
11082 
11083 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11084 		min = 2;
11085 
11086 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11087 					   PCI_IRQ_MSIX);
11088 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11089 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11090 		rc = -ENODEV;
11091 		goto msix_setup_exit;
11092 	}
11093 
11094 	tbl_size = total_vecs;
11095 	if (pci_msix_can_alloc_dyn(bp->pdev))
11096 		tbl_size = max;
11097 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
11098 	if (bp->irq_tbl) {
11099 		for (i = 0; i < total_vecs; i++)
11100 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11101 
11102 		bp->total_irqs = total_vecs;
11103 		/* Trim rings based upon num of vectors allocated */
11104 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11105 				     total_vecs - ulp_msix, min == 1);
11106 		if (rc)
11107 			goto msix_setup_exit;
11108 
11109 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11110 		bp->cp_nr_rings = (min == 1) ?
11111 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11112 				  tx_cp + bp->rx_nr_rings;
11113 
11114 	} else {
11115 		rc = -ENOMEM;
11116 		goto msix_setup_exit;
11117 	}
11118 	return 0;
11119 
11120 msix_setup_exit:
11121 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11122 	kfree(bp->irq_tbl);
11123 	bp->irq_tbl = NULL;
11124 	pci_free_irq_vectors(bp->pdev);
11125 	return rc;
11126 }
11127 
bnxt_clear_int_mode(struct bnxt * bp)11128 static void bnxt_clear_int_mode(struct bnxt *bp)
11129 {
11130 	pci_free_irq_vectors(bp->pdev);
11131 
11132 	kfree(bp->irq_tbl);
11133 	bp->irq_tbl = NULL;
11134 }
11135 
bnxt_reserve_rings(struct bnxt * bp,bool irq_re_init)11136 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11137 {
11138 	bool irq_cleared = false;
11139 	bool irq_change = false;
11140 	int tcs = bp->num_tc;
11141 	int irqs_required;
11142 	int rc;
11143 
11144 	if (!bnxt_need_reserve_rings(bp))
11145 		return 0;
11146 
11147 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11148 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11149 
11150 		if (ulp_msix > bp->ulp_num_msix_want)
11151 			ulp_msix = bp->ulp_num_msix_want;
11152 		irqs_required = ulp_msix + bp->cp_nr_rings;
11153 	} else {
11154 		irqs_required = bnxt_get_num_msix(bp);
11155 	}
11156 
11157 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11158 		irq_change = true;
11159 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11160 			bnxt_ulp_irq_stop(bp);
11161 			bnxt_clear_int_mode(bp);
11162 			irq_cleared = true;
11163 		}
11164 	}
11165 	rc = __bnxt_reserve_rings(bp);
11166 	if (irq_cleared) {
11167 		if (!rc)
11168 			rc = bnxt_init_int_mode(bp);
11169 		bnxt_ulp_irq_restart(bp, rc);
11170 	} else if (irq_change && !rc) {
11171 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11172 			rc = -ENOSPC;
11173 	}
11174 	if (rc) {
11175 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11176 		return rc;
11177 	}
11178 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11179 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11180 		netdev_err(bp->dev, "tx ring reservation failure\n");
11181 		netdev_reset_tc(bp->dev);
11182 		bp->num_tc = 0;
11183 		if (bp->tx_nr_rings_xdp)
11184 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11185 		else
11186 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11187 		return -ENOMEM;
11188 	}
11189 	return 0;
11190 }
11191 
bnxt_free_irq(struct bnxt * bp)11192 static void bnxt_free_irq(struct bnxt *bp)
11193 {
11194 	struct bnxt_irq *irq;
11195 	int i;
11196 
11197 #ifdef CONFIG_RFS_ACCEL
11198 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11199 	bp->dev->rx_cpu_rmap = NULL;
11200 #endif
11201 	if (!bp->irq_tbl || !bp->bnapi)
11202 		return;
11203 
11204 	for (i = 0; i < bp->cp_nr_rings; i++) {
11205 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11206 
11207 		irq = &bp->irq_tbl[map_idx];
11208 		if (irq->requested) {
11209 			if (irq->have_cpumask) {
11210 				irq_update_affinity_hint(irq->vector, NULL);
11211 				free_cpumask_var(irq->cpu_mask);
11212 				irq->have_cpumask = 0;
11213 			}
11214 			free_irq(irq->vector, bp->bnapi[i]);
11215 		}
11216 
11217 		irq->requested = 0;
11218 	}
11219 }
11220 
bnxt_request_irq(struct bnxt * bp)11221 static int bnxt_request_irq(struct bnxt *bp)
11222 {
11223 	int i, j, rc = 0;
11224 	unsigned long flags = 0;
11225 #ifdef CONFIG_RFS_ACCEL
11226 	struct cpu_rmap *rmap;
11227 #endif
11228 
11229 	rc = bnxt_setup_int_mode(bp);
11230 	if (rc) {
11231 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11232 			   rc);
11233 		return rc;
11234 	}
11235 #ifdef CONFIG_RFS_ACCEL
11236 	rmap = bp->dev->rx_cpu_rmap;
11237 #endif
11238 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11239 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11240 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11241 
11242 #ifdef CONFIG_RFS_ACCEL
11243 		if (rmap && bp->bnapi[i]->rx_ring) {
11244 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11245 			if (rc)
11246 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11247 					    j);
11248 			j++;
11249 		}
11250 #endif
11251 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11252 				 bp->bnapi[i]);
11253 		if (rc)
11254 			break;
11255 
11256 		netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector);
11257 		irq->requested = 1;
11258 
11259 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11260 			int numa_node = dev_to_node(&bp->pdev->dev);
11261 
11262 			irq->have_cpumask = 1;
11263 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11264 					irq->cpu_mask);
11265 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11266 			if (rc) {
11267 				netdev_warn(bp->dev,
11268 					    "Update affinity hint failed, IRQ = %d\n",
11269 					    irq->vector);
11270 				break;
11271 			}
11272 		}
11273 	}
11274 	return rc;
11275 }
11276 
bnxt_del_napi(struct bnxt * bp)11277 static void bnxt_del_napi(struct bnxt *bp)
11278 {
11279 	int i;
11280 
11281 	if (!bp->bnapi)
11282 		return;
11283 
11284 	for (i = 0; i < bp->rx_nr_rings; i++)
11285 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11286 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11287 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11288 
11289 	for (i = 0; i < bp->cp_nr_rings; i++) {
11290 		struct bnxt_napi *bnapi = bp->bnapi[i];
11291 
11292 		__netif_napi_del(&bnapi->napi);
11293 	}
11294 	/* We called __netif_napi_del(), we need
11295 	 * to respect an RCU grace period before freeing napi structures.
11296 	 */
11297 	synchronize_net();
11298 }
11299 
bnxt_init_napi(struct bnxt * bp)11300 static void bnxt_init_napi(struct bnxt *bp)
11301 {
11302 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11303 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11304 	struct bnxt_napi *bnapi;
11305 	int i;
11306 
11307 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11308 		poll_fn = bnxt_poll_p5;
11309 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11310 		cp_nr_rings--;
11311 	for (i = 0; i < cp_nr_rings; i++) {
11312 		bnapi = bp->bnapi[i];
11313 		netif_napi_add_config(bp->dev, &bnapi->napi, poll_fn,
11314 				      bnapi->index);
11315 	}
11316 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11317 		bnapi = bp->bnapi[cp_nr_rings];
11318 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11319 	}
11320 }
11321 
bnxt_disable_napi(struct bnxt * bp)11322 static void bnxt_disable_napi(struct bnxt *bp)
11323 {
11324 	int i;
11325 
11326 	if (!bp->bnapi ||
11327 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11328 		return;
11329 
11330 	for (i = 0; i < bp->cp_nr_rings; i++) {
11331 		struct bnxt_napi *bnapi = bp->bnapi[i];
11332 		struct bnxt_cp_ring_info *cpr;
11333 
11334 		cpr = &bnapi->cp_ring;
11335 		if (bnapi->tx_fault)
11336 			cpr->sw_stats->tx.tx_resets++;
11337 		if (bnapi->in_reset)
11338 			cpr->sw_stats->rx.rx_resets++;
11339 		napi_disable(&bnapi->napi);
11340 	}
11341 }
11342 
bnxt_enable_napi(struct bnxt * bp)11343 static void bnxt_enable_napi(struct bnxt *bp)
11344 {
11345 	int i;
11346 
11347 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11348 	for (i = 0; i < bp->cp_nr_rings; i++) {
11349 		struct bnxt_napi *bnapi = bp->bnapi[i];
11350 		struct bnxt_cp_ring_info *cpr;
11351 
11352 		bnapi->tx_fault = 0;
11353 
11354 		cpr = &bnapi->cp_ring;
11355 		bnapi->in_reset = false;
11356 
11357 		if (bnapi->rx_ring) {
11358 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11359 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11360 		}
11361 		napi_enable(&bnapi->napi);
11362 	}
11363 }
11364 
bnxt_tx_disable(struct bnxt * bp)11365 void bnxt_tx_disable(struct bnxt *bp)
11366 {
11367 	int i;
11368 	struct bnxt_tx_ring_info *txr;
11369 
11370 	if (bp->tx_ring) {
11371 		for (i = 0; i < bp->tx_nr_rings; i++) {
11372 			txr = &bp->tx_ring[i];
11373 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11374 		}
11375 	}
11376 	/* Make sure napi polls see @dev_state change */
11377 	synchronize_net();
11378 	/* Drop carrier first to prevent TX timeout */
11379 	netif_carrier_off(bp->dev);
11380 	/* Stop all TX queues */
11381 	netif_tx_disable(bp->dev);
11382 }
11383 
bnxt_tx_enable(struct bnxt * bp)11384 void bnxt_tx_enable(struct bnxt *bp)
11385 {
11386 	int i;
11387 	struct bnxt_tx_ring_info *txr;
11388 
11389 	for (i = 0; i < bp->tx_nr_rings; i++) {
11390 		txr = &bp->tx_ring[i];
11391 		WRITE_ONCE(txr->dev_state, 0);
11392 	}
11393 	/* Make sure napi polls see @dev_state change */
11394 	synchronize_net();
11395 	netif_tx_wake_all_queues(bp->dev);
11396 	if (BNXT_LINK_IS_UP(bp))
11397 		netif_carrier_on(bp->dev);
11398 }
11399 
bnxt_report_fec(struct bnxt_link_info * link_info)11400 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11401 {
11402 	u8 active_fec = link_info->active_fec_sig_mode &
11403 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11404 
11405 	switch (active_fec) {
11406 	default:
11407 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11408 		return "None";
11409 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11410 		return "Clause 74 BaseR";
11411 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11412 		return "Clause 91 RS(528,514)";
11413 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11414 		return "Clause 91 RS544_1XN";
11415 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11416 		return "Clause 91 RS(544,514)";
11417 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11418 		return "Clause 91 RS272_1XN";
11419 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11420 		return "Clause 91 RS(272,257)";
11421 	}
11422 }
11423 
bnxt_report_link(struct bnxt * bp)11424 void bnxt_report_link(struct bnxt *bp)
11425 {
11426 	if (BNXT_LINK_IS_UP(bp)) {
11427 		const char *signal = "";
11428 		const char *flow_ctrl;
11429 		const char *duplex;
11430 		u32 speed;
11431 		u16 fec;
11432 
11433 		netif_carrier_on(bp->dev);
11434 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11435 		if (speed == SPEED_UNKNOWN) {
11436 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11437 			return;
11438 		}
11439 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11440 			duplex = "full";
11441 		else
11442 			duplex = "half";
11443 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11444 			flow_ctrl = "ON - receive & transmit";
11445 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11446 			flow_ctrl = "ON - transmit";
11447 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11448 			flow_ctrl = "ON - receive";
11449 		else
11450 			flow_ctrl = "none";
11451 		if (bp->link_info.phy_qcfg_resp.option_flags &
11452 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11453 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11454 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11455 			switch (sig_mode) {
11456 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11457 				signal = "(NRZ) ";
11458 				break;
11459 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11460 				signal = "(PAM4 56Gbps) ";
11461 				break;
11462 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11463 				signal = "(PAM4 112Gbps) ";
11464 				break;
11465 			default:
11466 				break;
11467 			}
11468 		}
11469 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11470 			    speed, signal, duplex, flow_ctrl);
11471 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11472 			netdev_info(bp->dev, "EEE is %s\n",
11473 				    bp->eee.eee_active ? "active" :
11474 							 "not active");
11475 		fec = bp->link_info.fec_cfg;
11476 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11477 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11478 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11479 				    bnxt_report_fec(&bp->link_info));
11480 	} else {
11481 		netif_carrier_off(bp->dev);
11482 		netdev_err(bp->dev, "NIC Link is Down\n");
11483 	}
11484 }
11485 
bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output * resp)11486 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11487 {
11488 	if (!resp->supported_speeds_auto_mode &&
11489 	    !resp->supported_speeds_force_mode &&
11490 	    !resp->supported_pam4_speeds_auto_mode &&
11491 	    !resp->supported_pam4_speeds_force_mode &&
11492 	    !resp->supported_speeds2_auto_mode &&
11493 	    !resp->supported_speeds2_force_mode)
11494 		return true;
11495 	return false;
11496 }
11497 
bnxt_hwrm_phy_qcaps(struct bnxt * bp)11498 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11499 {
11500 	struct bnxt_link_info *link_info = &bp->link_info;
11501 	struct hwrm_port_phy_qcaps_output *resp;
11502 	struct hwrm_port_phy_qcaps_input *req;
11503 	int rc = 0;
11504 
11505 	if (bp->hwrm_spec_code < 0x10201)
11506 		return 0;
11507 
11508 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11509 	if (rc)
11510 		return rc;
11511 
11512 	resp = hwrm_req_hold(bp, req);
11513 	rc = hwrm_req_send(bp, req);
11514 	if (rc)
11515 		goto hwrm_phy_qcaps_exit;
11516 
11517 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11518 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11519 		struct ethtool_keee *eee = &bp->eee;
11520 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11521 
11522 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11523 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11524 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11525 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11526 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11527 	}
11528 
11529 	if (bp->hwrm_spec_code >= 0x10a01) {
11530 		if (bnxt_phy_qcaps_no_speed(resp)) {
11531 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11532 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11533 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11534 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11535 			netdev_info(bp->dev, "Ethernet link enabled\n");
11536 			/* Phy re-enabled, reprobe the speeds */
11537 			link_info->support_auto_speeds = 0;
11538 			link_info->support_pam4_auto_speeds = 0;
11539 			link_info->support_auto_speeds2 = 0;
11540 		}
11541 	}
11542 	if (resp->supported_speeds_auto_mode)
11543 		link_info->support_auto_speeds =
11544 			le16_to_cpu(resp->supported_speeds_auto_mode);
11545 	if (resp->supported_pam4_speeds_auto_mode)
11546 		link_info->support_pam4_auto_speeds =
11547 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11548 	if (resp->supported_speeds2_auto_mode)
11549 		link_info->support_auto_speeds2 =
11550 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11551 
11552 	bp->port_count = resp->port_cnt;
11553 
11554 hwrm_phy_qcaps_exit:
11555 	hwrm_req_drop(bp, req);
11556 	return rc;
11557 }
11558 
bnxt_support_dropped(u16 advertising,u16 supported)11559 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11560 {
11561 	u16 diff = advertising ^ supported;
11562 
11563 	return ((supported | diff) != supported);
11564 }
11565 
bnxt_support_speed_dropped(struct bnxt_link_info * link_info)11566 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11567 {
11568 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11569 
11570 	/* Check if any advertised speeds are no longer supported. The caller
11571 	 * holds the link_lock mutex, so we can modify link_info settings.
11572 	 */
11573 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11574 		if (bnxt_support_dropped(link_info->advertising,
11575 					 link_info->support_auto_speeds2)) {
11576 			link_info->advertising = link_info->support_auto_speeds2;
11577 			return true;
11578 		}
11579 		return false;
11580 	}
11581 	if (bnxt_support_dropped(link_info->advertising,
11582 				 link_info->support_auto_speeds)) {
11583 		link_info->advertising = link_info->support_auto_speeds;
11584 		return true;
11585 	}
11586 	if (bnxt_support_dropped(link_info->advertising_pam4,
11587 				 link_info->support_pam4_auto_speeds)) {
11588 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
11589 		return true;
11590 	}
11591 	return false;
11592 }
11593 
bnxt_update_link(struct bnxt * bp,bool chng_link_state)11594 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
11595 {
11596 	struct bnxt_link_info *link_info = &bp->link_info;
11597 	struct hwrm_port_phy_qcfg_output *resp;
11598 	struct hwrm_port_phy_qcfg_input *req;
11599 	u8 link_state = link_info->link_state;
11600 	bool support_changed;
11601 	int rc;
11602 
11603 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
11604 	if (rc)
11605 		return rc;
11606 
11607 	resp = hwrm_req_hold(bp, req);
11608 	rc = hwrm_req_send(bp, req);
11609 	if (rc) {
11610 		hwrm_req_drop(bp, req);
11611 		if (BNXT_VF(bp) && rc == -ENODEV) {
11612 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
11613 			rc = 0;
11614 		}
11615 		return rc;
11616 	}
11617 
11618 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
11619 	link_info->phy_link_status = resp->link;
11620 	link_info->duplex = resp->duplex_cfg;
11621 	if (bp->hwrm_spec_code >= 0x10800)
11622 		link_info->duplex = resp->duplex_state;
11623 	link_info->pause = resp->pause;
11624 	link_info->auto_mode = resp->auto_mode;
11625 	link_info->auto_pause_setting = resp->auto_pause;
11626 	link_info->lp_pause = resp->link_partner_adv_pause;
11627 	link_info->force_pause_setting = resp->force_pause;
11628 	link_info->duplex_setting = resp->duplex_cfg;
11629 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
11630 		link_info->link_speed = le16_to_cpu(resp->link_speed);
11631 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
11632 			link_info->active_lanes = resp->active_lanes;
11633 	} else {
11634 		link_info->link_speed = 0;
11635 		link_info->active_lanes = 0;
11636 	}
11637 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
11638 	link_info->force_pam4_link_speed =
11639 		le16_to_cpu(resp->force_pam4_link_speed);
11640 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
11641 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
11642 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
11643 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
11644 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
11645 	link_info->auto_pam4_link_speeds =
11646 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
11647 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
11648 	link_info->lp_auto_link_speeds =
11649 		le16_to_cpu(resp->link_partner_adv_speeds);
11650 	link_info->lp_auto_pam4_link_speeds =
11651 		resp->link_partner_pam4_adv_speeds;
11652 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
11653 	link_info->phy_ver[0] = resp->phy_maj;
11654 	link_info->phy_ver[1] = resp->phy_min;
11655 	link_info->phy_ver[2] = resp->phy_bld;
11656 	link_info->media_type = resp->media_type;
11657 	link_info->phy_type = resp->phy_type;
11658 	link_info->transceiver = resp->xcvr_pkg_type;
11659 	link_info->phy_addr = resp->eee_config_phy_addr &
11660 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
11661 	link_info->module_status = resp->module_status;
11662 
11663 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
11664 		struct ethtool_keee *eee = &bp->eee;
11665 		u16 fw_speeds;
11666 
11667 		eee->eee_active = 0;
11668 		if (resp->eee_config_phy_addr &
11669 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
11670 			eee->eee_active = 1;
11671 			fw_speeds = le16_to_cpu(
11672 				resp->link_partner_adv_eee_link_speed_mask);
11673 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
11674 		}
11675 
11676 		/* Pull initial EEE config */
11677 		if (!chng_link_state) {
11678 			if (resp->eee_config_phy_addr &
11679 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
11680 				eee->eee_enabled = 1;
11681 
11682 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
11683 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
11684 
11685 			if (resp->eee_config_phy_addr &
11686 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
11687 				__le32 tmr;
11688 
11689 				eee->tx_lpi_enabled = 1;
11690 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
11691 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
11692 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
11693 			}
11694 		}
11695 	}
11696 
11697 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
11698 	if (bp->hwrm_spec_code >= 0x10504) {
11699 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
11700 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
11701 	}
11702 	/* TODO: need to add more logic to report VF link */
11703 	if (chng_link_state) {
11704 		if (link_info->phy_link_status == BNXT_LINK_LINK)
11705 			link_info->link_state = BNXT_LINK_STATE_UP;
11706 		else
11707 			link_info->link_state = BNXT_LINK_STATE_DOWN;
11708 		if (link_state != link_info->link_state)
11709 			bnxt_report_link(bp);
11710 	} else {
11711 		/* always link down if not require to update link state */
11712 		link_info->link_state = BNXT_LINK_STATE_DOWN;
11713 	}
11714 	hwrm_req_drop(bp, req);
11715 
11716 	if (!BNXT_PHY_CFG_ABLE(bp))
11717 		return 0;
11718 
11719 	support_changed = bnxt_support_speed_dropped(link_info);
11720 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
11721 		bnxt_hwrm_set_link_setting(bp, true, false);
11722 	return 0;
11723 }
11724 
bnxt_get_port_module_status(struct bnxt * bp)11725 static void bnxt_get_port_module_status(struct bnxt *bp)
11726 {
11727 	struct bnxt_link_info *link_info = &bp->link_info;
11728 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
11729 	u8 module_status;
11730 
11731 	if (bnxt_update_link(bp, true))
11732 		return;
11733 
11734 	module_status = link_info->module_status;
11735 	switch (module_status) {
11736 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
11737 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
11738 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
11739 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
11740 			    bp->pf.port_id);
11741 		if (bp->hwrm_spec_code >= 0x10201) {
11742 			netdev_warn(bp->dev, "Module part number %s\n",
11743 				    resp->phy_vendor_partnumber);
11744 		}
11745 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
11746 			netdev_warn(bp->dev, "TX is disabled\n");
11747 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
11748 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
11749 	}
11750 }
11751 
11752 static void
bnxt_hwrm_set_pause_common(struct bnxt * bp,struct hwrm_port_phy_cfg_input * req)11753 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11754 {
11755 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
11756 		if (bp->hwrm_spec_code >= 0x10201)
11757 			req->auto_pause =
11758 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
11759 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11760 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
11761 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11762 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
11763 		req->enables |=
11764 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11765 	} else {
11766 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11767 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
11768 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11769 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
11770 		req->enables |=
11771 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
11772 		if (bp->hwrm_spec_code >= 0x10201) {
11773 			req->auto_pause = req->force_pause;
11774 			req->enables |= cpu_to_le32(
11775 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11776 		}
11777 	}
11778 }
11779 
bnxt_hwrm_set_link_common(struct bnxt * bp,struct hwrm_port_phy_cfg_input * req)11780 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11781 {
11782 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
11783 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
11784 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11785 			req->enables |=
11786 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
11787 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
11788 		} else if (bp->link_info.advertising) {
11789 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
11790 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
11791 		}
11792 		if (bp->link_info.advertising_pam4) {
11793 			req->enables |=
11794 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
11795 			req->auto_link_pam4_speed_mask =
11796 				cpu_to_le16(bp->link_info.advertising_pam4);
11797 		}
11798 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
11799 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
11800 	} else {
11801 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
11802 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11803 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
11804 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
11805 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
11806 				   (u32)bp->link_info.req_link_speed);
11807 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
11808 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11809 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
11810 		} else {
11811 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11812 		}
11813 	}
11814 
11815 	/* tell chimp that the setting takes effect immediately */
11816 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
11817 }
11818 
bnxt_hwrm_set_pause(struct bnxt * bp)11819 int bnxt_hwrm_set_pause(struct bnxt *bp)
11820 {
11821 	struct hwrm_port_phy_cfg_input *req;
11822 	int rc;
11823 
11824 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11825 	if (rc)
11826 		return rc;
11827 
11828 	bnxt_hwrm_set_pause_common(bp, req);
11829 
11830 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
11831 	    bp->link_info.force_link_chng)
11832 		bnxt_hwrm_set_link_common(bp, req);
11833 
11834 	rc = hwrm_req_send(bp, req);
11835 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
11836 		/* since changing of pause setting doesn't trigger any link
11837 		 * change event, the driver needs to update the current pause
11838 		 * result upon successfully return of the phy_cfg command
11839 		 */
11840 		bp->link_info.pause =
11841 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
11842 		bp->link_info.auto_pause_setting = 0;
11843 		if (!bp->link_info.force_link_chng)
11844 			bnxt_report_link(bp);
11845 	}
11846 	bp->link_info.force_link_chng = false;
11847 	return rc;
11848 }
11849 
bnxt_hwrm_set_eee(struct bnxt * bp,struct hwrm_port_phy_cfg_input * req)11850 static void bnxt_hwrm_set_eee(struct bnxt *bp,
11851 			      struct hwrm_port_phy_cfg_input *req)
11852 {
11853 	struct ethtool_keee *eee = &bp->eee;
11854 
11855 	if (eee->eee_enabled) {
11856 		u16 eee_speeds;
11857 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
11858 
11859 		if (eee->tx_lpi_enabled)
11860 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
11861 		else
11862 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
11863 
11864 		req->flags |= cpu_to_le32(flags);
11865 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
11866 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
11867 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
11868 	} else {
11869 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
11870 	}
11871 }
11872 
bnxt_hwrm_set_link_setting(struct bnxt * bp,bool set_pause,bool set_eee)11873 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
11874 {
11875 	struct hwrm_port_phy_cfg_input *req;
11876 	int rc;
11877 
11878 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11879 	if (rc)
11880 		return rc;
11881 
11882 	if (set_pause)
11883 		bnxt_hwrm_set_pause_common(bp, req);
11884 
11885 	bnxt_hwrm_set_link_common(bp, req);
11886 
11887 	if (set_eee)
11888 		bnxt_hwrm_set_eee(bp, req);
11889 	return hwrm_req_send(bp, req);
11890 }
11891 
bnxt_hwrm_shutdown_link(struct bnxt * bp)11892 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
11893 {
11894 	struct hwrm_port_phy_cfg_input *req;
11895 	int rc;
11896 
11897 	if (!BNXT_SINGLE_PF(bp))
11898 		return 0;
11899 
11900 	if (pci_num_vf(bp->pdev) &&
11901 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
11902 		return 0;
11903 
11904 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11905 	if (rc)
11906 		return rc;
11907 
11908 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
11909 	rc = hwrm_req_send(bp, req);
11910 	if (!rc) {
11911 		mutex_lock(&bp->link_lock);
11912 		/* Device is not obliged link down in certain scenarios, even
11913 		 * when forced. Setting the state unknown is consistent with
11914 		 * driver startup and will force link state to be reported
11915 		 * during subsequent open based on PORT_PHY_QCFG.
11916 		 */
11917 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
11918 		mutex_unlock(&bp->link_lock);
11919 	}
11920 	return rc;
11921 }
11922 
bnxt_fw_reset_via_optee(struct bnxt * bp)11923 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
11924 {
11925 #ifdef CONFIG_TEE_BNXT_FW
11926 	int rc = tee_bnxt_fw_load();
11927 
11928 	if (rc)
11929 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
11930 
11931 	return rc;
11932 #else
11933 	netdev_err(bp->dev, "OP-TEE not supported\n");
11934 	return -ENODEV;
11935 #endif
11936 }
11937 
bnxt_try_recover_fw(struct bnxt * bp)11938 static int bnxt_try_recover_fw(struct bnxt *bp)
11939 {
11940 	if (bp->fw_health && bp->fw_health->status_reliable) {
11941 		int retry = 0, rc;
11942 		u32 sts;
11943 
11944 		do {
11945 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11946 			rc = bnxt_hwrm_poll(bp);
11947 			if (!BNXT_FW_IS_BOOTING(sts) &&
11948 			    !BNXT_FW_IS_RECOVERING(sts))
11949 				break;
11950 			retry++;
11951 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
11952 
11953 		if (!BNXT_FW_IS_HEALTHY(sts)) {
11954 			netdev_err(bp->dev,
11955 				   "Firmware not responding, status: 0x%x\n",
11956 				   sts);
11957 			rc = -ENODEV;
11958 		}
11959 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
11960 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
11961 			return bnxt_fw_reset_via_optee(bp);
11962 		}
11963 		return rc;
11964 	}
11965 
11966 	return -ENODEV;
11967 }
11968 
bnxt_clear_reservations(struct bnxt * bp,bool fw_reset)11969 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
11970 {
11971 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11972 
11973 	if (!BNXT_NEW_RM(bp))
11974 		return; /* no resource reservations required */
11975 
11976 	hw_resc->resv_cp_rings = 0;
11977 	hw_resc->resv_stat_ctxs = 0;
11978 	hw_resc->resv_irqs = 0;
11979 	hw_resc->resv_tx_rings = 0;
11980 	hw_resc->resv_rx_rings = 0;
11981 	hw_resc->resv_hw_ring_grps = 0;
11982 	hw_resc->resv_vnics = 0;
11983 	hw_resc->resv_rsscos_ctxs = 0;
11984 	if (!fw_reset) {
11985 		bp->tx_nr_rings = 0;
11986 		bp->rx_nr_rings = 0;
11987 	}
11988 }
11989 
bnxt_cancel_reservations(struct bnxt * bp,bool fw_reset)11990 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
11991 {
11992 	int rc;
11993 
11994 	if (!BNXT_NEW_RM(bp))
11995 		return 0; /* no resource reservations required */
11996 
11997 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
11998 	if (rc)
11999 		netdev_err(bp->dev, "resc_qcaps failed\n");
12000 
12001 	bnxt_clear_reservations(bp, fw_reset);
12002 
12003 	return rc;
12004 }
12005 
bnxt_hwrm_if_change(struct bnxt * bp,bool up)12006 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12007 {
12008 	struct hwrm_func_drv_if_change_output *resp;
12009 	struct hwrm_func_drv_if_change_input *req;
12010 	bool fw_reset = !bp->irq_tbl;
12011 	bool resc_reinit = false;
12012 	int rc, retry = 0;
12013 	u32 flags = 0;
12014 
12015 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12016 		return 0;
12017 
12018 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12019 	if (rc)
12020 		return rc;
12021 
12022 	if (up)
12023 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12024 	resp = hwrm_req_hold(bp, req);
12025 
12026 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12027 	while (retry < BNXT_FW_IF_RETRY) {
12028 		rc = hwrm_req_send(bp, req);
12029 		if (rc != -EAGAIN)
12030 			break;
12031 
12032 		msleep(50);
12033 		retry++;
12034 	}
12035 
12036 	if (rc == -EAGAIN) {
12037 		hwrm_req_drop(bp, req);
12038 		return rc;
12039 	} else if (!rc) {
12040 		flags = le32_to_cpu(resp->flags);
12041 	} else if (up) {
12042 		rc = bnxt_try_recover_fw(bp);
12043 		fw_reset = true;
12044 	}
12045 	hwrm_req_drop(bp, req);
12046 	if (rc)
12047 		return rc;
12048 
12049 	if (!up) {
12050 		bnxt_inv_fw_health_reg(bp);
12051 		return 0;
12052 	}
12053 
12054 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12055 		resc_reinit = true;
12056 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12057 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12058 		fw_reset = true;
12059 	else
12060 		bnxt_remap_fw_health_regs(bp);
12061 
12062 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12063 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12064 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12065 		return -ENODEV;
12066 	}
12067 	if (resc_reinit || fw_reset) {
12068 		if (fw_reset) {
12069 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12070 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12071 				bnxt_ulp_irq_stop(bp);
12072 			bnxt_free_ctx_mem(bp, false);
12073 			bnxt_dcb_free(bp);
12074 			rc = bnxt_fw_init_one(bp);
12075 			if (rc) {
12076 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12077 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12078 				return rc;
12079 			}
12080 			bnxt_clear_int_mode(bp);
12081 			rc = bnxt_init_int_mode(bp);
12082 			if (rc) {
12083 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12084 				netdev_err(bp->dev, "init int mode failed\n");
12085 				return rc;
12086 			}
12087 		}
12088 		rc = bnxt_cancel_reservations(bp, fw_reset);
12089 	}
12090 	return rc;
12091 }
12092 
bnxt_hwrm_port_led_qcaps(struct bnxt * bp)12093 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12094 {
12095 	struct hwrm_port_led_qcaps_output *resp;
12096 	struct hwrm_port_led_qcaps_input *req;
12097 	struct bnxt_pf_info *pf = &bp->pf;
12098 	int rc;
12099 
12100 	bp->num_leds = 0;
12101 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12102 		return 0;
12103 
12104 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12105 	if (rc)
12106 		return rc;
12107 
12108 	req->port_id = cpu_to_le16(pf->port_id);
12109 	resp = hwrm_req_hold(bp, req);
12110 	rc = hwrm_req_send(bp, req);
12111 	if (rc) {
12112 		hwrm_req_drop(bp, req);
12113 		return rc;
12114 	}
12115 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12116 		int i;
12117 
12118 		bp->num_leds = resp->num_leds;
12119 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12120 						 bp->num_leds);
12121 		for (i = 0; i < bp->num_leds; i++) {
12122 			struct bnxt_led_info *led = &bp->leds[i];
12123 			__le16 caps = led->led_state_caps;
12124 
12125 			if (!led->led_group_id ||
12126 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12127 				bp->num_leds = 0;
12128 				break;
12129 			}
12130 		}
12131 	}
12132 	hwrm_req_drop(bp, req);
12133 	return 0;
12134 }
12135 
bnxt_hwrm_alloc_wol_fltr(struct bnxt * bp)12136 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12137 {
12138 	struct hwrm_wol_filter_alloc_output *resp;
12139 	struct hwrm_wol_filter_alloc_input *req;
12140 	int rc;
12141 
12142 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12143 	if (rc)
12144 		return rc;
12145 
12146 	req->port_id = cpu_to_le16(bp->pf.port_id);
12147 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12148 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12149 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12150 
12151 	resp = hwrm_req_hold(bp, req);
12152 	rc = hwrm_req_send(bp, req);
12153 	if (!rc)
12154 		bp->wol_filter_id = resp->wol_filter_id;
12155 	hwrm_req_drop(bp, req);
12156 	return rc;
12157 }
12158 
bnxt_hwrm_free_wol_fltr(struct bnxt * bp)12159 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12160 {
12161 	struct hwrm_wol_filter_free_input *req;
12162 	int rc;
12163 
12164 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12165 	if (rc)
12166 		return rc;
12167 
12168 	req->port_id = cpu_to_le16(bp->pf.port_id);
12169 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12170 	req->wol_filter_id = bp->wol_filter_id;
12171 
12172 	return hwrm_req_send(bp, req);
12173 }
12174 
bnxt_hwrm_get_wol_fltrs(struct bnxt * bp,u16 handle)12175 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12176 {
12177 	struct hwrm_wol_filter_qcfg_output *resp;
12178 	struct hwrm_wol_filter_qcfg_input *req;
12179 	u16 next_handle = 0;
12180 	int rc;
12181 
12182 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12183 	if (rc)
12184 		return rc;
12185 
12186 	req->port_id = cpu_to_le16(bp->pf.port_id);
12187 	req->handle = cpu_to_le16(handle);
12188 	resp = hwrm_req_hold(bp, req);
12189 	rc = hwrm_req_send(bp, req);
12190 	if (!rc) {
12191 		next_handle = le16_to_cpu(resp->next_handle);
12192 		if (next_handle != 0) {
12193 			if (resp->wol_type ==
12194 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12195 				bp->wol = 1;
12196 				bp->wol_filter_id = resp->wol_filter_id;
12197 			}
12198 		}
12199 	}
12200 	hwrm_req_drop(bp, req);
12201 	return next_handle;
12202 }
12203 
bnxt_get_wol_settings(struct bnxt * bp)12204 static void bnxt_get_wol_settings(struct bnxt *bp)
12205 {
12206 	u16 handle = 0;
12207 
12208 	bp->wol = 0;
12209 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12210 		return;
12211 
12212 	do {
12213 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12214 	} while (handle && handle != 0xffff);
12215 }
12216 
bnxt_eee_config_ok(struct bnxt * bp)12217 static bool bnxt_eee_config_ok(struct bnxt *bp)
12218 {
12219 	struct ethtool_keee *eee = &bp->eee;
12220 	struct bnxt_link_info *link_info = &bp->link_info;
12221 
12222 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12223 		return true;
12224 
12225 	if (eee->eee_enabled) {
12226 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12227 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12228 
12229 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12230 
12231 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12232 			eee->eee_enabled = 0;
12233 			return false;
12234 		}
12235 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12236 			linkmode_and(eee->advertised, advertising,
12237 				     eee->supported);
12238 			return false;
12239 		}
12240 	}
12241 	return true;
12242 }
12243 
bnxt_update_phy_setting(struct bnxt * bp)12244 static int bnxt_update_phy_setting(struct bnxt *bp)
12245 {
12246 	int rc;
12247 	bool update_link = false;
12248 	bool update_pause = false;
12249 	bool update_eee = false;
12250 	struct bnxt_link_info *link_info = &bp->link_info;
12251 
12252 	rc = bnxt_update_link(bp, true);
12253 	if (rc) {
12254 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12255 			   rc);
12256 		return rc;
12257 	}
12258 	if (!BNXT_SINGLE_PF(bp))
12259 		return 0;
12260 
12261 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12262 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12263 	    link_info->req_flow_ctrl)
12264 		update_pause = true;
12265 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12266 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12267 		update_pause = true;
12268 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12269 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12270 			update_link = true;
12271 		if (bnxt_force_speed_updated(link_info))
12272 			update_link = true;
12273 		if (link_info->req_duplex != link_info->duplex_setting)
12274 			update_link = true;
12275 	} else {
12276 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12277 			update_link = true;
12278 		if (bnxt_auto_speed_updated(link_info))
12279 			update_link = true;
12280 	}
12281 
12282 	/* The last close may have shutdown the link, so need to call
12283 	 * PHY_CFG to bring it back up.
12284 	 */
12285 	if (!BNXT_LINK_IS_UP(bp))
12286 		update_link = true;
12287 
12288 	if (!bnxt_eee_config_ok(bp))
12289 		update_eee = true;
12290 
12291 	if (update_link)
12292 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12293 	else if (update_pause)
12294 		rc = bnxt_hwrm_set_pause(bp);
12295 	if (rc) {
12296 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12297 			   rc);
12298 		return rc;
12299 	}
12300 
12301 	return rc;
12302 }
12303 
12304 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12305 
bnxt_reinit_after_abort(struct bnxt * bp)12306 static int bnxt_reinit_after_abort(struct bnxt *bp)
12307 {
12308 	int rc;
12309 
12310 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12311 		return -EBUSY;
12312 
12313 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12314 		return -ENODEV;
12315 
12316 	rc = bnxt_fw_init_one(bp);
12317 	if (!rc) {
12318 		bnxt_clear_int_mode(bp);
12319 		rc = bnxt_init_int_mode(bp);
12320 		if (!rc) {
12321 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12322 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12323 		}
12324 	}
12325 	return rc;
12326 }
12327 
bnxt_cfg_one_usr_fltr(struct bnxt * bp,struct bnxt_filter_base * fltr)12328 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12329 {
12330 	struct bnxt_ntuple_filter *ntp_fltr;
12331 	struct bnxt_l2_filter *l2_fltr;
12332 
12333 	if (list_empty(&fltr->list))
12334 		return;
12335 
12336 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12337 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12338 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12339 		atomic_inc(&l2_fltr->refcnt);
12340 		ntp_fltr->l2_fltr = l2_fltr;
12341 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12342 			bnxt_del_ntp_filter(bp, ntp_fltr);
12343 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12344 				   fltr->sw_id);
12345 		}
12346 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12347 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12348 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12349 			bnxt_del_l2_filter(bp, l2_fltr);
12350 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12351 				   fltr->sw_id);
12352 		}
12353 	}
12354 }
12355 
bnxt_cfg_usr_fltrs(struct bnxt * bp)12356 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12357 {
12358 	struct bnxt_filter_base *usr_fltr, *tmp;
12359 
12360 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12361 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12362 }
12363 
bnxt_set_xps_mapping(struct bnxt * bp)12364 static int bnxt_set_xps_mapping(struct bnxt *bp)
12365 {
12366 	int numa_node = dev_to_node(&bp->pdev->dev);
12367 	unsigned int q_idx, map_idx, cpu, i;
12368 	const struct cpumask *cpu_mask_ptr;
12369 	int nr_cpus = num_online_cpus();
12370 	cpumask_t *q_map;
12371 	int rc = 0;
12372 
12373 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12374 	if (!q_map)
12375 		return -ENOMEM;
12376 
12377 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12378 	 * Each TC has the same number of TX queues. The nth TX queue for each
12379 	 * TC will have the same CPU mask.
12380 	 */
12381 	for (i = 0; i < nr_cpus; i++) {
12382 		map_idx = i % bp->tx_nr_rings_per_tc;
12383 		cpu = cpumask_local_spread(i, numa_node);
12384 		cpu_mask_ptr = get_cpu_mask(cpu);
12385 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12386 	}
12387 
12388 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12389 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12390 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12391 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12392 		if (rc) {
12393 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12394 				    q_idx);
12395 			break;
12396 		}
12397 	}
12398 
12399 	kfree(q_map);
12400 
12401 	return rc;
12402 }
12403 
__bnxt_open_nic(struct bnxt * bp,bool irq_re_init,bool link_re_init)12404 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12405 {
12406 	int rc = 0;
12407 
12408 	netif_carrier_off(bp->dev);
12409 	if (irq_re_init) {
12410 		/* Reserve rings now if none were reserved at driver probe. */
12411 		rc = bnxt_init_dflt_ring_mode(bp);
12412 		if (rc) {
12413 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12414 			return rc;
12415 		}
12416 	}
12417 	rc = bnxt_reserve_rings(bp, irq_re_init);
12418 	if (rc)
12419 		return rc;
12420 
12421 	rc = bnxt_alloc_mem(bp, irq_re_init);
12422 	if (rc) {
12423 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12424 		goto open_err_free_mem;
12425 	}
12426 
12427 	if (irq_re_init) {
12428 		bnxt_init_napi(bp);
12429 		rc = bnxt_request_irq(bp);
12430 		if (rc) {
12431 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12432 			goto open_err_irq;
12433 		}
12434 	}
12435 
12436 	rc = bnxt_init_nic(bp, irq_re_init);
12437 	if (rc) {
12438 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12439 		goto open_err_irq;
12440 	}
12441 
12442 	bnxt_enable_napi(bp);
12443 	bnxt_debug_dev_init(bp);
12444 
12445 	if (link_re_init) {
12446 		mutex_lock(&bp->link_lock);
12447 		rc = bnxt_update_phy_setting(bp);
12448 		mutex_unlock(&bp->link_lock);
12449 		if (rc) {
12450 			netdev_warn(bp->dev, "failed to update phy settings\n");
12451 			if (BNXT_SINGLE_PF(bp)) {
12452 				bp->link_info.phy_retry = true;
12453 				bp->link_info.phy_retry_expires =
12454 					jiffies + 5 * HZ;
12455 			}
12456 		}
12457 	}
12458 
12459 	if (irq_re_init) {
12460 		udp_tunnel_nic_reset_ntf(bp->dev);
12461 		rc = bnxt_set_xps_mapping(bp);
12462 		if (rc)
12463 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12464 	}
12465 
12466 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12467 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12468 			static_branch_enable(&bnxt_xdp_locking_key);
12469 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12470 		static_branch_disable(&bnxt_xdp_locking_key);
12471 	}
12472 	set_bit(BNXT_STATE_OPEN, &bp->state);
12473 	bnxt_enable_int(bp);
12474 	/* Enable TX queues */
12475 	bnxt_tx_enable(bp);
12476 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12477 	/* Poll link status and check for SFP+ module status */
12478 	mutex_lock(&bp->link_lock);
12479 	bnxt_get_port_module_status(bp);
12480 	mutex_unlock(&bp->link_lock);
12481 
12482 	/* VF-reps may need to be re-opened after the PF is re-opened */
12483 	if (BNXT_PF(bp))
12484 		bnxt_vf_reps_open(bp);
12485 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
12486 		WRITE_ONCE(bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS);
12487 	bnxt_ptp_init_rtc(bp, true);
12488 	bnxt_ptp_cfg_tstamp_filters(bp);
12489 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12490 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12491 	bnxt_cfg_usr_fltrs(bp);
12492 	return 0;
12493 
12494 open_err_irq:
12495 	bnxt_del_napi(bp);
12496 
12497 open_err_free_mem:
12498 	bnxt_free_skbs(bp);
12499 	bnxt_free_irq(bp);
12500 	bnxt_free_mem(bp, true);
12501 	return rc;
12502 }
12503 
12504 /* rtnl_lock held */
bnxt_open_nic(struct bnxt * bp,bool irq_re_init,bool link_re_init)12505 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12506 {
12507 	int rc = 0;
12508 
12509 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12510 		rc = -EIO;
12511 	if (!rc)
12512 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12513 	if (rc) {
12514 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12515 		dev_close(bp->dev);
12516 	}
12517 	return rc;
12518 }
12519 
12520 /* rtnl_lock held, open the NIC half way by allocating all resources, but
12521  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
12522  * self tests.
12523  */
bnxt_half_open_nic(struct bnxt * bp)12524 int bnxt_half_open_nic(struct bnxt *bp)
12525 {
12526 	int rc = 0;
12527 
12528 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12529 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12530 		rc = -ENODEV;
12531 		goto half_open_err;
12532 	}
12533 
12534 	rc = bnxt_alloc_mem(bp, true);
12535 	if (rc) {
12536 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12537 		goto half_open_err;
12538 	}
12539 	bnxt_init_napi(bp);
12540 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12541 	rc = bnxt_init_nic(bp, true);
12542 	if (rc) {
12543 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12544 		bnxt_del_napi(bp);
12545 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12546 		goto half_open_err;
12547 	}
12548 	return 0;
12549 
12550 half_open_err:
12551 	bnxt_free_skbs(bp);
12552 	bnxt_free_mem(bp, true);
12553 	dev_close(bp->dev);
12554 	return rc;
12555 }
12556 
12557 /* rtnl_lock held, this call can only be made after a previous successful
12558  * call to bnxt_half_open_nic().
12559  */
bnxt_half_close_nic(struct bnxt * bp)12560 void bnxt_half_close_nic(struct bnxt *bp)
12561 {
12562 	bnxt_hwrm_resource_free(bp, false, true);
12563 	bnxt_del_napi(bp);
12564 	bnxt_free_skbs(bp);
12565 	bnxt_free_mem(bp, true);
12566 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12567 }
12568 
bnxt_reenable_sriov(struct bnxt * bp)12569 void bnxt_reenable_sriov(struct bnxt *bp)
12570 {
12571 	if (BNXT_PF(bp)) {
12572 		struct bnxt_pf_info *pf = &bp->pf;
12573 		int n = pf->active_vfs;
12574 
12575 		if (n)
12576 			bnxt_cfg_hw_sriov(bp, &n, true);
12577 	}
12578 }
12579 
bnxt_open(struct net_device * dev)12580 static int bnxt_open(struct net_device *dev)
12581 {
12582 	struct bnxt *bp = netdev_priv(dev);
12583 	int rc;
12584 
12585 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12586 		rc = bnxt_reinit_after_abort(bp);
12587 		if (rc) {
12588 			if (rc == -EBUSY)
12589 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
12590 			else
12591 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
12592 			return -ENODEV;
12593 		}
12594 	}
12595 
12596 	rc = bnxt_hwrm_if_change(bp, true);
12597 	if (rc)
12598 		return rc;
12599 
12600 	rc = __bnxt_open_nic(bp, true, true);
12601 	if (rc) {
12602 		bnxt_hwrm_if_change(bp, false);
12603 	} else {
12604 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
12605 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12606 				bnxt_queue_sp_work(bp,
12607 						   BNXT_RESTART_ULP_SP_EVENT);
12608 		}
12609 	}
12610 
12611 	return rc;
12612 }
12613 
bnxt_drv_busy(struct bnxt * bp)12614 static bool bnxt_drv_busy(struct bnxt *bp)
12615 {
12616 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
12617 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
12618 }
12619 
12620 static void bnxt_get_ring_stats(struct bnxt *bp,
12621 				struct rtnl_link_stats64 *stats);
12622 
__bnxt_close_nic(struct bnxt * bp,bool irq_re_init,bool link_re_init)12623 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
12624 			     bool link_re_init)
12625 {
12626 	/* Close the VF-reps before closing PF */
12627 	if (BNXT_PF(bp))
12628 		bnxt_vf_reps_close(bp);
12629 
12630 	/* Change device state to avoid TX queue wake up's */
12631 	bnxt_tx_disable(bp);
12632 
12633 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12634 	smp_mb__after_atomic();
12635 	while (bnxt_drv_busy(bp))
12636 		msleep(20);
12637 
12638 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12639 		bnxt_clear_rss_ctxs(bp);
12640 	/* Flush rings and disable interrupts */
12641 	bnxt_shutdown_nic(bp, irq_re_init);
12642 
12643 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
12644 
12645 	bnxt_debug_dev_exit(bp);
12646 	bnxt_disable_napi(bp);
12647 	del_timer_sync(&bp->timer);
12648 	bnxt_free_skbs(bp);
12649 
12650 	/* Save ring stats before shutdown */
12651 	if (bp->bnapi && irq_re_init) {
12652 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
12653 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
12654 	}
12655 	if (irq_re_init) {
12656 		bnxt_free_irq(bp);
12657 		bnxt_del_napi(bp);
12658 	}
12659 	bnxt_free_mem(bp, irq_re_init);
12660 }
12661 
bnxt_close_nic(struct bnxt * bp,bool irq_re_init,bool link_re_init)12662 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12663 {
12664 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12665 		/* If we get here, it means firmware reset is in progress
12666 		 * while we are trying to close.  We can safely proceed with
12667 		 * the close because we are holding rtnl_lock().  Some firmware
12668 		 * messages may fail as we proceed to close.  We set the
12669 		 * ABORT_ERR flag here so that the FW reset thread will later
12670 		 * abort when it gets the rtnl_lock() and sees the flag.
12671 		 */
12672 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
12673 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12674 	}
12675 
12676 #ifdef CONFIG_BNXT_SRIOV
12677 	if (bp->sriov_cfg) {
12678 		int rc;
12679 
12680 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
12681 						      !bp->sriov_cfg,
12682 						      BNXT_SRIOV_CFG_WAIT_TMO);
12683 		if (!rc)
12684 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
12685 		else if (rc < 0)
12686 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
12687 	}
12688 #endif
12689 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
12690 }
12691 
bnxt_close(struct net_device * dev)12692 static int bnxt_close(struct net_device *dev)
12693 {
12694 	struct bnxt *bp = netdev_priv(dev);
12695 
12696 	bnxt_close_nic(bp, true, true);
12697 	bnxt_hwrm_shutdown_link(bp);
12698 	bnxt_hwrm_if_change(bp, false);
12699 	return 0;
12700 }
12701 
bnxt_hwrm_port_phy_read(struct bnxt * bp,u16 phy_addr,u16 reg,u16 * val)12702 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
12703 				   u16 *val)
12704 {
12705 	struct hwrm_port_phy_mdio_read_output *resp;
12706 	struct hwrm_port_phy_mdio_read_input *req;
12707 	int rc;
12708 
12709 	if (bp->hwrm_spec_code < 0x10a00)
12710 		return -EOPNOTSUPP;
12711 
12712 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
12713 	if (rc)
12714 		return rc;
12715 
12716 	req->port_id = cpu_to_le16(bp->pf.port_id);
12717 	req->phy_addr = phy_addr;
12718 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12719 	if (mdio_phy_id_is_c45(phy_addr)) {
12720 		req->cl45_mdio = 1;
12721 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12722 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12723 		req->reg_addr = cpu_to_le16(reg);
12724 	}
12725 
12726 	resp = hwrm_req_hold(bp, req);
12727 	rc = hwrm_req_send(bp, req);
12728 	if (!rc)
12729 		*val = le16_to_cpu(resp->reg_data);
12730 	hwrm_req_drop(bp, req);
12731 	return rc;
12732 }
12733 
bnxt_hwrm_port_phy_write(struct bnxt * bp,u16 phy_addr,u16 reg,u16 val)12734 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
12735 				    u16 val)
12736 {
12737 	struct hwrm_port_phy_mdio_write_input *req;
12738 	int rc;
12739 
12740 	if (bp->hwrm_spec_code < 0x10a00)
12741 		return -EOPNOTSUPP;
12742 
12743 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
12744 	if (rc)
12745 		return rc;
12746 
12747 	req->port_id = cpu_to_le16(bp->pf.port_id);
12748 	req->phy_addr = phy_addr;
12749 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12750 	if (mdio_phy_id_is_c45(phy_addr)) {
12751 		req->cl45_mdio = 1;
12752 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12753 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12754 		req->reg_addr = cpu_to_le16(reg);
12755 	}
12756 	req->reg_data = cpu_to_le16(val);
12757 
12758 	return hwrm_req_send(bp, req);
12759 }
12760 
12761 /* rtnl_lock held */
bnxt_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)12762 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12763 {
12764 	struct mii_ioctl_data *mdio = if_mii(ifr);
12765 	struct bnxt *bp = netdev_priv(dev);
12766 	int rc;
12767 
12768 	switch (cmd) {
12769 	case SIOCGMIIPHY:
12770 		mdio->phy_id = bp->link_info.phy_addr;
12771 
12772 		fallthrough;
12773 	case SIOCGMIIREG: {
12774 		u16 mii_regval = 0;
12775 
12776 		if (!netif_running(dev))
12777 			return -EAGAIN;
12778 
12779 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
12780 					     &mii_regval);
12781 		mdio->val_out = mii_regval;
12782 		return rc;
12783 	}
12784 
12785 	case SIOCSMIIREG:
12786 		if (!netif_running(dev))
12787 			return -EAGAIN;
12788 
12789 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
12790 						mdio->val_in);
12791 
12792 	case SIOCSHWTSTAMP:
12793 		return bnxt_hwtstamp_set(dev, ifr);
12794 
12795 	case SIOCGHWTSTAMP:
12796 		return bnxt_hwtstamp_get(dev, ifr);
12797 
12798 	default:
12799 		/* do nothing */
12800 		break;
12801 	}
12802 	return -EOPNOTSUPP;
12803 }
12804 
bnxt_get_ring_stats(struct bnxt * bp,struct rtnl_link_stats64 * stats)12805 static void bnxt_get_ring_stats(struct bnxt *bp,
12806 				struct rtnl_link_stats64 *stats)
12807 {
12808 	int i;
12809 
12810 	for (i = 0; i < bp->cp_nr_rings; i++) {
12811 		struct bnxt_napi *bnapi = bp->bnapi[i];
12812 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
12813 		u64 *sw = cpr->stats.sw_stats;
12814 
12815 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
12816 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12817 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
12818 
12819 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
12820 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
12821 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
12822 
12823 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
12824 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
12825 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
12826 
12827 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
12828 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
12829 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
12830 
12831 		stats->rx_missed_errors +=
12832 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
12833 
12834 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12835 
12836 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
12837 
12838 		stats->rx_dropped +=
12839 			cpr->sw_stats->rx.rx_netpoll_discards +
12840 			cpr->sw_stats->rx.rx_oom_discards;
12841 	}
12842 }
12843 
bnxt_add_prev_stats(struct bnxt * bp,struct rtnl_link_stats64 * stats)12844 static void bnxt_add_prev_stats(struct bnxt *bp,
12845 				struct rtnl_link_stats64 *stats)
12846 {
12847 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
12848 
12849 	stats->rx_packets += prev_stats->rx_packets;
12850 	stats->tx_packets += prev_stats->tx_packets;
12851 	stats->rx_bytes += prev_stats->rx_bytes;
12852 	stats->tx_bytes += prev_stats->tx_bytes;
12853 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
12854 	stats->multicast += prev_stats->multicast;
12855 	stats->rx_dropped += prev_stats->rx_dropped;
12856 	stats->tx_dropped += prev_stats->tx_dropped;
12857 }
12858 
12859 static void
bnxt_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)12860 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
12861 {
12862 	struct bnxt *bp = netdev_priv(dev);
12863 
12864 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
12865 	/* Make sure bnxt_close_nic() sees that we are reading stats before
12866 	 * we check the BNXT_STATE_OPEN flag.
12867 	 */
12868 	smp_mb__after_atomic();
12869 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12870 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12871 		*stats = bp->net_stats_prev;
12872 		return;
12873 	}
12874 
12875 	bnxt_get_ring_stats(bp, stats);
12876 	bnxt_add_prev_stats(bp, stats);
12877 
12878 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
12879 		u64 *rx = bp->port_stats.sw_stats;
12880 		u64 *tx = bp->port_stats.sw_stats +
12881 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
12882 
12883 		stats->rx_crc_errors =
12884 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
12885 		stats->rx_frame_errors =
12886 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
12887 		stats->rx_length_errors =
12888 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
12889 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
12890 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
12891 		stats->rx_errors =
12892 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
12893 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
12894 		stats->collisions =
12895 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
12896 		stats->tx_fifo_errors =
12897 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
12898 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
12899 	}
12900 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12901 }
12902 
bnxt_get_one_ring_err_stats(struct bnxt * bp,struct bnxt_total_ring_err_stats * stats,struct bnxt_cp_ring_info * cpr)12903 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
12904 					struct bnxt_total_ring_err_stats *stats,
12905 					struct bnxt_cp_ring_info *cpr)
12906 {
12907 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
12908 	u64 *hw_stats = cpr->stats.sw_stats;
12909 
12910 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
12911 	stats->rx_total_resets += sw_stats->rx.rx_resets;
12912 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
12913 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
12914 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
12915 	stats->rx_total_ring_discards +=
12916 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
12917 	stats->tx_total_resets += sw_stats->tx.tx_resets;
12918 	stats->tx_total_ring_discards +=
12919 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
12920 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
12921 }
12922 
bnxt_get_ring_err_stats(struct bnxt * bp,struct bnxt_total_ring_err_stats * stats)12923 void bnxt_get_ring_err_stats(struct bnxt *bp,
12924 			     struct bnxt_total_ring_err_stats *stats)
12925 {
12926 	int i;
12927 
12928 	for (i = 0; i < bp->cp_nr_rings; i++)
12929 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
12930 }
12931 
bnxt_mc_list_updated(struct bnxt * bp,u32 * rx_mask)12932 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
12933 {
12934 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12935 	struct net_device *dev = bp->dev;
12936 	struct netdev_hw_addr *ha;
12937 	u8 *haddr;
12938 	int mc_count = 0;
12939 	bool update = false;
12940 	int off = 0;
12941 
12942 	netdev_for_each_mc_addr(ha, dev) {
12943 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
12944 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12945 			vnic->mc_list_count = 0;
12946 			return false;
12947 		}
12948 		haddr = ha->addr;
12949 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
12950 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
12951 			update = true;
12952 		}
12953 		off += ETH_ALEN;
12954 		mc_count++;
12955 	}
12956 	if (mc_count)
12957 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12958 
12959 	if (mc_count != vnic->mc_list_count) {
12960 		vnic->mc_list_count = mc_count;
12961 		update = true;
12962 	}
12963 	return update;
12964 }
12965 
bnxt_uc_list_updated(struct bnxt * bp)12966 static bool bnxt_uc_list_updated(struct bnxt *bp)
12967 {
12968 	struct net_device *dev = bp->dev;
12969 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12970 	struct netdev_hw_addr *ha;
12971 	int off = 0;
12972 
12973 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
12974 		return true;
12975 
12976 	netdev_for_each_uc_addr(ha, dev) {
12977 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
12978 			return true;
12979 
12980 		off += ETH_ALEN;
12981 	}
12982 	return false;
12983 }
12984 
bnxt_set_rx_mode(struct net_device * dev)12985 static void bnxt_set_rx_mode(struct net_device *dev)
12986 {
12987 	struct bnxt *bp = netdev_priv(dev);
12988 	struct bnxt_vnic_info *vnic;
12989 	bool mc_update = false;
12990 	bool uc_update;
12991 	u32 mask;
12992 
12993 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
12994 		return;
12995 
12996 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12997 	mask = vnic->rx_mask;
12998 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
12999 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13000 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13001 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13002 
13003 	if (dev->flags & IFF_PROMISC)
13004 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13005 
13006 	uc_update = bnxt_uc_list_updated(bp);
13007 
13008 	if (dev->flags & IFF_BROADCAST)
13009 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13010 	if (dev->flags & IFF_ALLMULTI) {
13011 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13012 		vnic->mc_list_count = 0;
13013 	} else if (dev->flags & IFF_MULTICAST) {
13014 		mc_update = bnxt_mc_list_updated(bp, &mask);
13015 	}
13016 
13017 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13018 		vnic->rx_mask = mask;
13019 
13020 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13021 	}
13022 }
13023 
bnxt_cfg_rx_mode(struct bnxt * bp)13024 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13025 {
13026 	struct net_device *dev = bp->dev;
13027 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13028 	struct netdev_hw_addr *ha;
13029 	int i, off = 0, rc;
13030 	bool uc_update;
13031 
13032 	netif_addr_lock_bh(dev);
13033 	uc_update = bnxt_uc_list_updated(bp);
13034 	netif_addr_unlock_bh(dev);
13035 
13036 	if (!uc_update)
13037 		goto skip_uc;
13038 
13039 	for (i = 1; i < vnic->uc_filter_count; i++) {
13040 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13041 
13042 		bnxt_hwrm_l2_filter_free(bp, fltr);
13043 		bnxt_del_l2_filter(bp, fltr);
13044 	}
13045 
13046 	vnic->uc_filter_count = 1;
13047 
13048 	netif_addr_lock_bh(dev);
13049 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13050 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13051 	} else {
13052 		netdev_for_each_uc_addr(ha, dev) {
13053 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13054 			off += ETH_ALEN;
13055 			vnic->uc_filter_count++;
13056 		}
13057 	}
13058 	netif_addr_unlock_bh(dev);
13059 
13060 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13061 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13062 		if (rc) {
13063 			if (BNXT_VF(bp) && rc == -ENODEV) {
13064 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13065 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13066 				else
13067 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13068 				rc = 0;
13069 			} else {
13070 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13071 			}
13072 			vnic->uc_filter_count = i;
13073 			return rc;
13074 		}
13075 	}
13076 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13077 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13078 
13079 skip_uc:
13080 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13081 	    !bnxt_promisc_ok(bp))
13082 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13083 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13084 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13085 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13086 			    rc);
13087 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13088 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13089 		vnic->mc_list_count = 0;
13090 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13091 	}
13092 	if (rc)
13093 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13094 			   rc);
13095 
13096 	return rc;
13097 }
13098 
bnxt_can_reserve_rings(struct bnxt * bp)13099 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13100 {
13101 #ifdef CONFIG_BNXT_SRIOV
13102 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13103 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13104 
13105 		/* No minimum rings were provisioned by the PF.  Don't
13106 		 * reserve rings by default when device is down.
13107 		 */
13108 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13109 			return true;
13110 
13111 		if (!netif_running(bp->dev))
13112 			return false;
13113 	}
13114 #endif
13115 	return true;
13116 }
13117 
13118 /* If the chip and firmware supports RFS */
bnxt_rfs_supported(struct bnxt * bp)13119 static bool bnxt_rfs_supported(struct bnxt *bp)
13120 {
13121 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13122 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13123 			return true;
13124 		return false;
13125 	}
13126 	/* 212 firmware is broken for aRFS */
13127 	if (BNXT_FW_MAJ(bp) == 212)
13128 		return false;
13129 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13130 		return true;
13131 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13132 		return true;
13133 	return false;
13134 }
13135 
13136 /* If runtime conditions support RFS */
bnxt_rfs_capable(struct bnxt * bp,bool new_rss_ctx)13137 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13138 {
13139 	struct bnxt_hw_rings hwr = {0};
13140 	int max_vnics, max_rss_ctxs;
13141 
13142 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13143 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13144 		return bnxt_rfs_supported(bp);
13145 
13146 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13147 		return false;
13148 
13149 	hwr.grp = bp->rx_nr_rings;
13150 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13151 	if (new_rss_ctx)
13152 		hwr.vnic++;
13153 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13154 	max_vnics = bnxt_get_max_func_vnics(bp);
13155 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13156 
13157 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13158 		if (bp->rx_nr_rings > 1)
13159 			netdev_warn(bp->dev,
13160 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13161 				    min(max_rss_ctxs - 1, max_vnics - 1));
13162 		return false;
13163 	}
13164 
13165 	if (!BNXT_NEW_RM(bp))
13166 		return true;
13167 
13168 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13169 	 * issue that will mess up the default VNIC if we reduce the
13170 	 * reservations.
13171 	 */
13172 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13173 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13174 		return true;
13175 
13176 	bnxt_hwrm_reserve_rings(bp, &hwr);
13177 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13178 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13179 		return true;
13180 
13181 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13182 	hwr.vnic = 1;
13183 	hwr.rss_ctx = 0;
13184 	bnxt_hwrm_reserve_rings(bp, &hwr);
13185 	return false;
13186 }
13187 
bnxt_fix_features(struct net_device * dev,netdev_features_t features)13188 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13189 					   netdev_features_t features)
13190 {
13191 	struct bnxt *bp = netdev_priv(dev);
13192 	netdev_features_t vlan_features;
13193 
13194 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13195 		features &= ~NETIF_F_NTUPLE;
13196 
13197 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13198 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13199 
13200 	if (!(features & NETIF_F_GRO))
13201 		features &= ~NETIF_F_GRO_HW;
13202 
13203 	if (features & NETIF_F_GRO_HW)
13204 		features &= ~NETIF_F_LRO;
13205 
13206 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13207 	 * turned on or off together.
13208 	 */
13209 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13210 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13211 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13212 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13213 		else if (vlan_features)
13214 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13215 	}
13216 #ifdef CONFIG_BNXT_SRIOV
13217 	if (BNXT_VF(bp) && bp->vf.vlan)
13218 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13219 #endif
13220 	return features;
13221 }
13222 
bnxt_reinit_features(struct bnxt * bp,bool irq_re_init,bool link_re_init,u32 flags,bool update_tpa)13223 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13224 				bool link_re_init, u32 flags, bool update_tpa)
13225 {
13226 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13227 	bp->flags = flags;
13228 	if (update_tpa)
13229 		bnxt_set_ring_params(bp);
13230 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13231 }
13232 
bnxt_set_features(struct net_device * dev,netdev_features_t features)13233 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13234 {
13235 	bool update_tpa = false, update_ntuple = false;
13236 	struct bnxt *bp = netdev_priv(dev);
13237 	u32 flags = bp->flags;
13238 	u32 changes;
13239 	int rc = 0;
13240 	bool re_init = false;
13241 
13242 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13243 	if (features & NETIF_F_GRO_HW)
13244 		flags |= BNXT_FLAG_GRO;
13245 	else if (features & NETIF_F_LRO)
13246 		flags |= BNXT_FLAG_LRO;
13247 
13248 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13249 		flags &= ~BNXT_FLAG_TPA;
13250 
13251 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13252 		flags |= BNXT_FLAG_STRIP_VLAN;
13253 
13254 	if (features & NETIF_F_NTUPLE)
13255 		flags |= BNXT_FLAG_RFS;
13256 	else
13257 		bnxt_clear_usr_fltrs(bp, true);
13258 
13259 	changes = flags ^ bp->flags;
13260 	if (changes & BNXT_FLAG_TPA) {
13261 		update_tpa = true;
13262 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13263 		    (flags & BNXT_FLAG_TPA) == 0 ||
13264 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13265 			re_init = true;
13266 	}
13267 
13268 	if (changes & ~BNXT_FLAG_TPA)
13269 		re_init = true;
13270 
13271 	if (changes & BNXT_FLAG_RFS)
13272 		update_ntuple = true;
13273 
13274 	if (flags != bp->flags) {
13275 		u32 old_flags = bp->flags;
13276 
13277 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13278 			bp->flags = flags;
13279 			if (update_tpa)
13280 				bnxt_set_ring_params(bp);
13281 			return rc;
13282 		}
13283 
13284 		if (update_ntuple)
13285 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13286 
13287 		if (re_init)
13288 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13289 
13290 		if (update_tpa) {
13291 			bp->flags = flags;
13292 			rc = bnxt_set_tpa(bp,
13293 					  (flags & BNXT_FLAG_TPA) ?
13294 					  true : false);
13295 			if (rc)
13296 				bp->flags = old_flags;
13297 		}
13298 	}
13299 	return rc;
13300 }
13301 
bnxt_exthdr_check(struct bnxt * bp,struct sk_buff * skb,int nw_off,u8 ** nextp)13302 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13303 			      u8 **nextp)
13304 {
13305 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13306 	struct hop_jumbo_hdr *jhdr;
13307 	int hdr_count = 0;
13308 	u8 *nexthdr;
13309 	int start;
13310 
13311 	/* Check that there are at most 2 IPv6 extension headers, no
13312 	 * fragment header, and each is <= 64 bytes.
13313 	 */
13314 	start = nw_off + sizeof(*ip6h);
13315 	nexthdr = &ip6h->nexthdr;
13316 	while (ipv6_ext_hdr(*nexthdr)) {
13317 		struct ipv6_opt_hdr *hp;
13318 		int hdrlen;
13319 
13320 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13321 		    *nexthdr == NEXTHDR_FRAGMENT)
13322 			return false;
13323 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13324 					  skb_headlen(skb), NULL);
13325 		if (!hp)
13326 			return false;
13327 		if (*nexthdr == NEXTHDR_AUTH)
13328 			hdrlen = ipv6_authlen(hp);
13329 		else
13330 			hdrlen = ipv6_optlen(hp);
13331 
13332 		if (hdrlen > 64)
13333 			return false;
13334 
13335 		/* The ext header may be a hop-by-hop header inserted for
13336 		 * big TCP purposes. This will be removed before sending
13337 		 * from NIC, so do not count it.
13338 		 */
13339 		if (*nexthdr == NEXTHDR_HOP) {
13340 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13341 				goto increment_hdr;
13342 
13343 			jhdr = (struct hop_jumbo_hdr *)hp;
13344 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13345 			    jhdr->nexthdr != IPPROTO_TCP)
13346 				goto increment_hdr;
13347 
13348 			goto next_hdr;
13349 		}
13350 increment_hdr:
13351 		hdr_count++;
13352 next_hdr:
13353 		nexthdr = &hp->nexthdr;
13354 		start += hdrlen;
13355 	}
13356 	if (nextp) {
13357 		/* Caller will check inner protocol */
13358 		if (skb->encapsulation) {
13359 			*nextp = nexthdr;
13360 			return true;
13361 		}
13362 		*nextp = NULL;
13363 	}
13364 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13365 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13366 }
13367 
13368 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
bnxt_udp_tunl_check(struct bnxt * bp,struct sk_buff * skb)13369 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13370 {
13371 	struct udphdr *uh = udp_hdr(skb);
13372 	__be16 udp_port = uh->dest;
13373 
13374 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13375 	    udp_port != bp->vxlan_gpe_port)
13376 		return false;
13377 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13378 		struct ethhdr *eh = inner_eth_hdr(skb);
13379 
13380 		switch (eh->h_proto) {
13381 		case htons(ETH_P_IP):
13382 			return true;
13383 		case htons(ETH_P_IPV6):
13384 			return bnxt_exthdr_check(bp, skb,
13385 						 skb_inner_network_offset(skb),
13386 						 NULL);
13387 		}
13388 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13389 		return true;
13390 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13391 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13392 					 NULL);
13393 	}
13394 	return false;
13395 }
13396 
bnxt_tunl_check(struct bnxt * bp,struct sk_buff * skb,u8 l4_proto)13397 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13398 {
13399 	switch (l4_proto) {
13400 	case IPPROTO_UDP:
13401 		return bnxt_udp_tunl_check(bp, skb);
13402 	case IPPROTO_IPIP:
13403 		return true;
13404 	case IPPROTO_GRE: {
13405 		switch (skb->inner_protocol) {
13406 		default:
13407 			return false;
13408 		case htons(ETH_P_IP):
13409 			return true;
13410 		case htons(ETH_P_IPV6):
13411 			fallthrough;
13412 		}
13413 	}
13414 	case IPPROTO_IPV6:
13415 		/* Check ext headers of inner ipv6 */
13416 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13417 					 NULL);
13418 	}
13419 	return false;
13420 }
13421 
bnxt_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)13422 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13423 					     struct net_device *dev,
13424 					     netdev_features_t features)
13425 {
13426 	struct bnxt *bp = netdev_priv(dev);
13427 	u8 *l4_proto;
13428 
13429 	features = vlan_features_check(skb, features);
13430 	switch (vlan_get_protocol(skb)) {
13431 	case htons(ETH_P_IP):
13432 		if (!skb->encapsulation)
13433 			return features;
13434 		l4_proto = &ip_hdr(skb)->protocol;
13435 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13436 			return features;
13437 		break;
13438 	case htons(ETH_P_IPV6):
13439 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13440 				       &l4_proto))
13441 			break;
13442 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13443 			return features;
13444 		break;
13445 	}
13446 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13447 }
13448 
bnxt_dbg_hwrm_rd_reg(struct bnxt * bp,u32 reg_off,u16 num_words,u32 * reg_buf)13449 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13450 			 u32 *reg_buf)
13451 {
13452 	struct hwrm_dbg_read_direct_output *resp;
13453 	struct hwrm_dbg_read_direct_input *req;
13454 	__le32 *dbg_reg_buf;
13455 	dma_addr_t mapping;
13456 	int rc, i;
13457 
13458 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13459 	if (rc)
13460 		return rc;
13461 
13462 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13463 					 &mapping);
13464 	if (!dbg_reg_buf) {
13465 		rc = -ENOMEM;
13466 		goto dbg_rd_reg_exit;
13467 	}
13468 
13469 	req->host_dest_addr = cpu_to_le64(mapping);
13470 
13471 	resp = hwrm_req_hold(bp, req);
13472 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13473 	req->read_len32 = cpu_to_le32(num_words);
13474 
13475 	rc = hwrm_req_send(bp, req);
13476 	if (rc || resp->error_code) {
13477 		rc = -EIO;
13478 		goto dbg_rd_reg_exit;
13479 	}
13480 	for (i = 0; i < num_words; i++)
13481 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13482 
13483 dbg_rd_reg_exit:
13484 	hwrm_req_drop(bp, req);
13485 	return rc;
13486 }
13487 
bnxt_dbg_hwrm_ring_info_get(struct bnxt * bp,u8 ring_type,u32 ring_id,u32 * prod,u32 * cons)13488 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13489 				       u32 ring_id, u32 *prod, u32 *cons)
13490 {
13491 	struct hwrm_dbg_ring_info_get_output *resp;
13492 	struct hwrm_dbg_ring_info_get_input *req;
13493 	int rc;
13494 
13495 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13496 	if (rc)
13497 		return rc;
13498 
13499 	req->ring_type = ring_type;
13500 	req->fw_ring_id = cpu_to_le32(ring_id);
13501 	resp = hwrm_req_hold(bp, req);
13502 	rc = hwrm_req_send(bp, req);
13503 	if (!rc) {
13504 		*prod = le32_to_cpu(resp->producer_index);
13505 		*cons = le32_to_cpu(resp->consumer_index);
13506 	}
13507 	hwrm_req_drop(bp, req);
13508 	return rc;
13509 }
13510 
bnxt_dump_tx_sw_state(struct bnxt_napi * bnapi)13511 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13512 {
13513 	struct bnxt_tx_ring_info *txr;
13514 	int i = bnapi->index, j;
13515 
13516 	bnxt_for_each_napi_tx(j, bnapi, txr)
13517 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13518 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13519 			    txr->tx_cons);
13520 }
13521 
bnxt_dump_rx_sw_state(struct bnxt_napi * bnapi)13522 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13523 {
13524 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13525 	int i = bnapi->index;
13526 
13527 	if (!rxr)
13528 		return;
13529 
13530 	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",
13531 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13532 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13533 		    rxr->rx_sw_agg_prod);
13534 }
13535 
bnxt_dump_cp_sw_state(struct bnxt_napi * bnapi)13536 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13537 {
13538 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13539 	int i = bnapi->index;
13540 
13541 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13542 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13543 }
13544 
bnxt_dbg_dump_states(struct bnxt * bp)13545 static void bnxt_dbg_dump_states(struct bnxt *bp)
13546 {
13547 	int i;
13548 	struct bnxt_napi *bnapi;
13549 
13550 	for (i = 0; i < bp->cp_nr_rings; i++) {
13551 		bnapi = bp->bnapi[i];
13552 		if (netif_msg_drv(bp)) {
13553 			bnxt_dump_tx_sw_state(bnapi);
13554 			bnxt_dump_rx_sw_state(bnapi);
13555 			bnxt_dump_cp_sw_state(bnapi);
13556 		}
13557 	}
13558 }
13559 
bnxt_hwrm_rx_ring_reset(struct bnxt * bp,int ring_nr)13560 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13561 {
13562 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13563 	struct hwrm_ring_reset_input *req;
13564 	struct bnxt_napi *bnapi = rxr->bnapi;
13565 	struct bnxt_cp_ring_info *cpr;
13566 	u16 cp_ring_id;
13567 	int rc;
13568 
13569 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13570 	if (rc)
13571 		return rc;
13572 
13573 	cpr = &bnapi->cp_ring;
13574 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
13575 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
13576 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
13577 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
13578 	return hwrm_req_send_silent(bp, req);
13579 }
13580 
bnxt_reset_task(struct bnxt * bp,bool silent)13581 static void bnxt_reset_task(struct bnxt *bp, bool silent)
13582 {
13583 	if (!silent)
13584 		bnxt_dbg_dump_states(bp);
13585 	if (netif_running(bp->dev)) {
13586 		bnxt_close_nic(bp, !silent, false);
13587 		bnxt_open_nic(bp, !silent, false);
13588 	}
13589 }
13590 
bnxt_tx_timeout(struct net_device * dev,unsigned int txqueue)13591 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
13592 {
13593 	struct bnxt *bp = netdev_priv(dev);
13594 
13595 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
13596 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
13597 }
13598 
bnxt_fw_health_check(struct bnxt * bp)13599 static void bnxt_fw_health_check(struct bnxt *bp)
13600 {
13601 	struct bnxt_fw_health *fw_health = bp->fw_health;
13602 	struct pci_dev *pdev = bp->pdev;
13603 	u32 val;
13604 
13605 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13606 		return;
13607 
13608 	/* Make sure it is enabled before checking the tmr_counter. */
13609 	smp_rmb();
13610 	if (fw_health->tmr_counter) {
13611 		fw_health->tmr_counter--;
13612 		return;
13613 	}
13614 
13615 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13616 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
13617 		fw_health->arrests++;
13618 		goto fw_reset;
13619 	}
13620 
13621 	fw_health->last_fw_heartbeat = val;
13622 
13623 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13624 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
13625 		fw_health->discoveries++;
13626 		goto fw_reset;
13627 	}
13628 
13629 	fw_health->tmr_counter = fw_health->tmr_multiplier;
13630 	return;
13631 
13632 fw_reset:
13633 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
13634 }
13635 
bnxt_timer(struct timer_list * t)13636 static void bnxt_timer(struct timer_list *t)
13637 {
13638 	struct bnxt *bp = from_timer(bp, t, timer);
13639 	struct net_device *dev = bp->dev;
13640 
13641 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
13642 		return;
13643 
13644 	if (atomic_read(&bp->intr_sem) != 0)
13645 		goto bnxt_restart_timer;
13646 
13647 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
13648 		bnxt_fw_health_check(bp);
13649 
13650 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
13651 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
13652 
13653 	if (bnxt_tc_flower_enabled(bp))
13654 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
13655 
13656 #ifdef CONFIG_RFS_ACCEL
13657 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
13658 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
13659 #endif /*CONFIG_RFS_ACCEL*/
13660 
13661 	if (bp->link_info.phy_retry) {
13662 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
13663 			bp->link_info.phy_retry = false;
13664 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
13665 		} else {
13666 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
13667 		}
13668 	}
13669 
13670 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13671 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13672 
13673 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
13674 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
13675 
13676 bnxt_restart_timer:
13677 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13678 }
13679 
bnxt_rtnl_lock_sp(struct bnxt * bp)13680 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
13681 {
13682 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
13683 	 * set.  If the device is being closed, bnxt_close() may be holding
13684 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
13685 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
13686 	 */
13687 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13688 	rtnl_lock();
13689 }
13690 
bnxt_rtnl_unlock_sp(struct bnxt * bp)13691 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
13692 {
13693 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13694 	rtnl_unlock();
13695 }
13696 
13697 /* Only called from bnxt_sp_task() */
bnxt_reset(struct bnxt * bp,bool silent)13698 static void bnxt_reset(struct bnxt *bp, bool silent)
13699 {
13700 	bnxt_rtnl_lock_sp(bp);
13701 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
13702 		bnxt_reset_task(bp, silent);
13703 	bnxt_rtnl_unlock_sp(bp);
13704 }
13705 
13706 /* Only called from bnxt_sp_task() */
bnxt_rx_ring_reset(struct bnxt * bp)13707 static void bnxt_rx_ring_reset(struct bnxt *bp)
13708 {
13709 	int i;
13710 
13711 	bnxt_rtnl_lock_sp(bp);
13712 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13713 		bnxt_rtnl_unlock_sp(bp);
13714 		return;
13715 	}
13716 	/* Disable and flush TPA before resetting the RX ring */
13717 	if (bp->flags & BNXT_FLAG_TPA)
13718 		bnxt_set_tpa(bp, false);
13719 	for (i = 0; i < bp->rx_nr_rings; i++) {
13720 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
13721 		struct bnxt_cp_ring_info *cpr;
13722 		int rc;
13723 
13724 		if (!rxr->bnapi->in_reset)
13725 			continue;
13726 
13727 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
13728 		if (rc) {
13729 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
13730 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
13731 			else
13732 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
13733 					    rc);
13734 			bnxt_reset_task(bp, true);
13735 			break;
13736 		}
13737 		bnxt_free_one_rx_ring_skbs(bp, rxr);
13738 		rxr->rx_prod = 0;
13739 		rxr->rx_agg_prod = 0;
13740 		rxr->rx_sw_agg_prod = 0;
13741 		rxr->rx_next_cons = 0;
13742 		rxr->bnapi->in_reset = false;
13743 		bnxt_alloc_one_rx_ring(bp, i);
13744 		cpr = &rxr->bnapi->cp_ring;
13745 		cpr->sw_stats->rx.rx_resets++;
13746 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
13747 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
13748 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
13749 	}
13750 	if (bp->flags & BNXT_FLAG_TPA)
13751 		bnxt_set_tpa(bp, true);
13752 	bnxt_rtnl_unlock_sp(bp);
13753 }
13754 
bnxt_fw_fatal_close(struct bnxt * bp)13755 static void bnxt_fw_fatal_close(struct bnxt *bp)
13756 {
13757 	bnxt_tx_disable(bp);
13758 	bnxt_disable_napi(bp);
13759 	bnxt_disable_int_sync(bp);
13760 	bnxt_free_irq(bp);
13761 	bnxt_clear_int_mode(bp);
13762 	pci_disable_device(bp->pdev);
13763 }
13764 
bnxt_fw_reset_close(struct bnxt * bp)13765 static void bnxt_fw_reset_close(struct bnxt *bp)
13766 {
13767 	/* When firmware is in fatal state, quiesce device and disable
13768 	 * bus master to prevent any potential bad DMAs before freeing
13769 	 * kernel memory.
13770 	 */
13771 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
13772 		u16 val = 0;
13773 
13774 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
13775 		if (val == 0xffff)
13776 			bp->fw_reset_min_dsecs = 0;
13777 		bnxt_fw_fatal_close(bp);
13778 	}
13779 	__bnxt_close_nic(bp, true, false);
13780 	bnxt_vf_reps_free(bp);
13781 	bnxt_clear_int_mode(bp);
13782 	bnxt_hwrm_func_drv_unrgtr(bp);
13783 	if (pci_is_enabled(bp->pdev))
13784 		pci_disable_device(bp->pdev);
13785 	bnxt_free_ctx_mem(bp, false);
13786 }
13787 
is_bnxt_fw_ok(struct bnxt * bp)13788 static bool is_bnxt_fw_ok(struct bnxt *bp)
13789 {
13790 	struct bnxt_fw_health *fw_health = bp->fw_health;
13791 	bool no_heartbeat = false, has_reset = false;
13792 	u32 val;
13793 
13794 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13795 	if (val == fw_health->last_fw_heartbeat)
13796 		no_heartbeat = true;
13797 
13798 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13799 	if (val != fw_health->last_fw_reset_cnt)
13800 		has_reset = true;
13801 
13802 	if (!no_heartbeat && has_reset)
13803 		return true;
13804 
13805 	return false;
13806 }
13807 
13808 /* rtnl_lock is acquired before calling this function */
bnxt_force_fw_reset(struct bnxt * bp)13809 static void bnxt_force_fw_reset(struct bnxt *bp)
13810 {
13811 	struct bnxt_fw_health *fw_health = bp->fw_health;
13812 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13813 	u32 wait_dsecs;
13814 
13815 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
13816 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13817 		return;
13818 
13819 	/* we have to serialize with bnxt_refclk_read()*/
13820 	if (ptp) {
13821 		unsigned long flags;
13822 
13823 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
13824 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13825 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
13826 	} else {
13827 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13828 	}
13829 	bnxt_fw_reset_close(bp);
13830 	wait_dsecs = fw_health->master_func_wait_dsecs;
13831 	if (fw_health->primary) {
13832 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
13833 			wait_dsecs = 0;
13834 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
13835 	} else {
13836 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
13837 		wait_dsecs = fw_health->normal_func_wait_dsecs;
13838 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13839 	}
13840 
13841 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
13842 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
13843 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
13844 }
13845 
bnxt_fw_exception(struct bnxt * bp)13846 void bnxt_fw_exception(struct bnxt *bp)
13847 {
13848 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
13849 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
13850 	bnxt_ulp_stop(bp);
13851 	bnxt_rtnl_lock_sp(bp);
13852 	bnxt_force_fw_reset(bp);
13853 	bnxt_rtnl_unlock_sp(bp);
13854 }
13855 
13856 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
13857  * < 0 on error.
13858  */
bnxt_get_registered_vfs(struct bnxt * bp)13859 static int bnxt_get_registered_vfs(struct bnxt *bp)
13860 {
13861 #ifdef CONFIG_BNXT_SRIOV
13862 	int rc;
13863 
13864 	if (!BNXT_PF(bp))
13865 		return 0;
13866 
13867 	rc = bnxt_hwrm_func_qcfg(bp);
13868 	if (rc) {
13869 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
13870 		return rc;
13871 	}
13872 	if (bp->pf.registered_vfs)
13873 		return bp->pf.registered_vfs;
13874 	if (bp->sriov_cfg)
13875 		return 1;
13876 #endif
13877 	return 0;
13878 }
13879 
bnxt_fw_reset(struct bnxt * bp)13880 void bnxt_fw_reset(struct bnxt *bp)
13881 {
13882 	bnxt_ulp_stop(bp);
13883 	bnxt_rtnl_lock_sp(bp);
13884 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
13885 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13886 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13887 		int n = 0, tmo;
13888 
13889 		/* we have to serialize with bnxt_refclk_read()*/
13890 		if (ptp) {
13891 			unsigned long flags;
13892 
13893 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
13894 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13895 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
13896 		} else {
13897 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13898 		}
13899 		if (bp->pf.active_vfs &&
13900 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
13901 			n = bnxt_get_registered_vfs(bp);
13902 		if (n < 0) {
13903 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
13904 				   n);
13905 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13906 			dev_close(bp->dev);
13907 			goto fw_reset_exit;
13908 		} else if (n > 0) {
13909 			u16 vf_tmo_dsecs = n * 10;
13910 
13911 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
13912 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
13913 			bp->fw_reset_state =
13914 				BNXT_FW_RESET_STATE_POLL_VF;
13915 			bnxt_queue_fw_reset_work(bp, HZ / 10);
13916 			goto fw_reset_exit;
13917 		}
13918 		bnxt_fw_reset_close(bp);
13919 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13920 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
13921 			tmo = HZ / 10;
13922 		} else {
13923 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13924 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
13925 		}
13926 		bnxt_queue_fw_reset_work(bp, tmo);
13927 	}
13928 fw_reset_exit:
13929 	bnxt_rtnl_unlock_sp(bp);
13930 }
13931 
bnxt_chk_missed_irq(struct bnxt * bp)13932 static void bnxt_chk_missed_irq(struct bnxt *bp)
13933 {
13934 	int i;
13935 
13936 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13937 		return;
13938 
13939 	for (i = 0; i < bp->cp_nr_rings; i++) {
13940 		struct bnxt_napi *bnapi = bp->bnapi[i];
13941 		struct bnxt_cp_ring_info *cpr;
13942 		u32 fw_ring_id;
13943 		int j;
13944 
13945 		if (!bnapi)
13946 			continue;
13947 
13948 		cpr = &bnapi->cp_ring;
13949 		for (j = 0; j < cpr->cp_ring_count; j++) {
13950 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
13951 			u32 val[2];
13952 
13953 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
13954 				continue;
13955 
13956 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
13957 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
13958 				continue;
13959 			}
13960 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
13961 			bnxt_dbg_hwrm_ring_info_get(bp,
13962 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
13963 				fw_ring_id, &val[0], &val[1]);
13964 			cpr->sw_stats->cmn.missed_irqs++;
13965 		}
13966 	}
13967 }
13968 
13969 static void bnxt_cfg_ntp_filters(struct bnxt *);
13970 
bnxt_init_ethtool_link_settings(struct bnxt * bp)13971 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
13972 {
13973 	struct bnxt_link_info *link_info = &bp->link_info;
13974 
13975 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
13976 		link_info->autoneg = BNXT_AUTONEG_SPEED;
13977 		if (bp->hwrm_spec_code >= 0x10201) {
13978 			if (link_info->auto_pause_setting &
13979 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
13980 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13981 		} else {
13982 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13983 		}
13984 		bnxt_set_auto_speed(link_info);
13985 	} else {
13986 		bnxt_set_force_speed(link_info);
13987 		link_info->req_duplex = link_info->duplex_setting;
13988 	}
13989 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
13990 		link_info->req_flow_ctrl =
13991 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
13992 	else
13993 		link_info->req_flow_ctrl = link_info->force_pause_setting;
13994 }
13995 
bnxt_fw_echo_reply(struct bnxt * bp)13996 static void bnxt_fw_echo_reply(struct bnxt *bp)
13997 {
13998 	struct bnxt_fw_health *fw_health = bp->fw_health;
13999 	struct hwrm_func_echo_response_input *req;
14000 	int rc;
14001 
14002 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14003 	if (rc)
14004 		return;
14005 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14006 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14007 	hwrm_req_send(bp, req);
14008 }
14009 
bnxt_ulp_restart(struct bnxt * bp)14010 static void bnxt_ulp_restart(struct bnxt *bp)
14011 {
14012 	bnxt_ulp_stop(bp);
14013 	bnxt_ulp_start(bp, 0);
14014 }
14015 
bnxt_sp_task(struct work_struct * work)14016 static void bnxt_sp_task(struct work_struct *work)
14017 {
14018 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14019 
14020 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14021 	smp_mb__after_atomic();
14022 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14023 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14024 		return;
14025 	}
14026 
14027 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14028 		bnxt_ulp_restart(bp);
14029 		bnxt_reenable_sriov(bp);
14030 	}
14031 
14032 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14033 		bnxt_cfg_rx_mode(bp);
14034 
14035 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14036 		bnxt_cfg_ntp_filters(bp);
14037 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14038 		bnxt_hwrm_exec_fwd_req(bp);
14039 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14040 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14041 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14042 		bnxt_hwrm_port_qstats(bp, 0);
14043 		bnxt_hwrm_port_qstats_ext(bp, 0);
14044 		bnxt_accumulate_all_stats(bp);
14045 	}
14046 
14047 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14048 		int rc;
14049 
14050 		mutex_lock(&bp->link_lock);
14051 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14052 				       &bp->sp_event))
14053 			bnxt_hwrm_phy_qcaps(bp);
14054 
14055 		rc = bnxt_update_link(bp, true);
14056 		if (rc)
14057 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14058 				   rc);
14059 
14060 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14061 				       &bp->sp_event))
14062 			bnxt_init_ethtool_link_settings(bp);
14063 		mutex_unlock(&bp->link_lock);
14064 	}
14065 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14066 		int rc;
14067 
14068 		mutex_lock(&bp->link_lock);
14069 		rc = bnxt_update_phy_setting(bp);
14070 		mutex_unlock(&bp->link_lock);
14071 		if (rc) {
14072 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14073 		} else {
14074 			bp->link_info.phy_retry = false;
14075 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14076 		}
14077 	}
14078 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14079 		mutex_lock(&bp->link_lock);
14080 		bnxt_get_port_module_status(bp);
14081 		mutex_unlock(&bp->link_lock);
14082 	}
14083 
14084 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14085 		bnxt_tc_flow_stats_work(bp);
14086 
14087 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14088 		bnxt_chk_missed_irq(bp);
14089 
14090 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14091 		bnxt_fw_echo_reply(bp);
14092 
14093 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14094 		bnxt_hwmon_notify_event(bp);
14095 
14096 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14097 	 * must be the last functions to be called before exiting.
14098 	 */
14099 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14100 		bnxt_reset(bp, false);
14101 
14102 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14103 		bnxt_reset(bp, true);
14104 
14105 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14106 		bnxt_rx_ring_reset(bp);
14107 
14108 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14109 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14110 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14111 			bnxt_devlink_health_fw_report(bp);
14112 		else
14113 			bnxt_fw_reset(bp);
14114 	}
14115 
14116 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14117 		if (!is_bnxt_fw_ok(bp))
14118 			bnxt_devlink_health_fw_report(bp);
14119 	}
14120 
14121 	smp_mb__before_atomic();
14122 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14123 }
14124 
14125 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14126 				int *max_cp);
14127 
14128 /* Under rtnl_lock */
bnxt_check_rings(struct bnxt * bp,int tx,int rx,bool sh,int tcs,int tx_xdp)14129 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14130 		     int tx_xdp)
14131 {
14132 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14133 	struct bnxt_hw_rings hwr = {0};
14134 	int rx_rings = rx;
14135 	int rc;
14136 
14137 	if (tcs)
14138 		tx_sets = tcs;
14139 
14140 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14141 
14142 	if (max_rx < rx_rings)
14143 		return -ENOMEM;
14144 
14145 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14146 		rx_rings <<= 1;
14147 
14148 	hwr.rx = rx_rings;
14149 	hwr.tx = tx * tx_sets + tx_xdp;
14150 	if (max_tx < hwr.tx)
14151 		return -ENOMEM;
14152 
14153 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14154 
14155 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14156 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14157 	if (max_cp < hwr.cp)
14158 		return -ENOMEM;
14159 	hwr.stat = hwr.cp;
14160 	if (BNXT_NEW_RM(bp)) {
14161 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14162 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14163 		hwr.grp = rx;
14164 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14165 	}
14166 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14167 		hwr.cp_p5 = hwr.tx + rx;
14168 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14169 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14170 		if (!bnxt_ulp_registered(bp->edev)) {
14171 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14172 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14173 		}
14174 		if (hwr.cp > bp->total_irqs) {
14175 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14176 
14177 			if (total_msix < hwr.cp) {
14178 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14179 					    hwr.cp, total_msix);
14180 				rc = -ENOSPC;
14181 			}
14182 		}
14183 	}
14184 	return rc;
14185 }
14186 
bnxt_unmap_bars(struct bnxt * bp,struct pci_dev * pdev)14187 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14188 {
14189 	if (bp->bar2) {
14190 		pci_iounmap(pdev, bp->bar2);
14191 		bp->bar2 = NULL;
14192 	}
14193 
14194 	if (bp->bar1) {
14195 		pci_iounmap(pdev, bp->bar1);
14196 		bp->bar1 = NULL;
14197 	}
14198 
14199 	if (bp->bar0) {
14200 		pci_iounmap(pdev, bp->bar0);
14201 		bp->bar0 = NULL;
14202 	}
14203 }
14204 
bnxt_cleanup_pci(struct bnxt * bp)14205 static void bnxt_cleanup_pci(struct bnxt *bp)
14206 {
14207 	bnxt_unmap_bars(bp, bp->pdev);
14208 	pci_release_regions(bp->pdev);
14209 	if (pci_is_enabled(bp->pdev))
14210 		pci_disable_device(bp->pdev);
14211 }
14212 
bnxt_init_dflt_coal(struct bnxt * bp)14213 static void bnxt_init_dflt_coal(struct bnxt *bp)
14214 {
14215 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14216 	struct bnxt_coal *coal;
14217 	u16 flags = 0;
14218 
14219 	if (coal_cap->cmpl_params &
14220 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14221 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14222 
14223 	/* Tick values in micro seconds.
14224 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14225 	 */
14226 	coal = &bp->rx_coal;
14227 	coal->coal_ticks = 10;
14228 	coal->coal_bufs = 30;
14229 	coal->coal_ticks_irq = 1;
14230 	coal->coal_bufs_irq = 2;
14231 	coal->idle_thresh = 50;
14232 	coal->bufs_per_record = 2;
14233 	coal->budget = 64;		/* NAPI budget */
14234 	coal->flags = flags;
14235 
14236 	coal = &bp->tx_coal;
14237 	coal->coal_ticks = 28;
14238 	coal->coal_bufs = 30;
14239 	coal->coal_ticks_irq = 2;
14240 	coal->coal_bufs_irq = 2;
14241 	coal->bufs_per_record = 1;
14242 	coal->flags = flags;
14243 
14244 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14245 }
14246 
14247 /* FW that pre-reserves 1 VNIC per function */
bnxt_fw_pre_resv_vnics(struct bnxt * bp)14248 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14249 {
14250 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14251 
14252 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14253 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14254 		return true;
14255 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14256 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14257 		return true;
14258 	return false;
14259 }
14260 
bnxt_fw_init_one_p1(struct bnxt * bp)14261 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14262 {
14263 	int rc;
14264 
14265 	bp->fw_cap = 0;
14266 	rc = bnxt_hwrm_ver_get(bp);
14267 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14268 	 * so wait before continuing with recovery.
14269 	 */
14270 	if (rc)
14271 		msleep(100);
14272 	bnxt_try_map_fw_health_reg(bp);
14273 	if (rc) {
14274 		rc = bnxt_try_recover_fw(bp);
14275 		if (rc)
14276 			return rc;
14277 		rc = bnxt_hwrm_ver_get(bp);
14278 		if (rc)
14279 			return rc;
14280 	}
14281 
14282 	bnxt_nvm_cfg_ver_get(bp);
14283 
14284 	rc = bnxt_hwrm_func_reset(bp);
14285 	if (rc)
14286 		return -ENODEV;
14287 
14288 	bnxt_hwrm_fw_set_time(bp);
14289 	return 0;
14290 }
14291 
bnxt_fw_init_one_p2(struct bnxt * bp)14292 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14293 {
14294 	int rc;
14295 
14296 	/* Get the MAX capabilities for this function */
14297 	rc = bnxt_hwrm_func_qcaps(bp);
14298 	if (rc) {
14299 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14300 			   rc);
14301 		return -ENODEV;
14302 	}
14303 
14304 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14305 	if (rc)
14306 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14307 			    rc);
14308 
14309 	if (bnxt_alloc_fw_health(bp)) {
14310 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14311 	} else {
14312 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14313 		if (rc)
14314 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14315 				    rc);
14316 	}
14317 
14318 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14319 	if (rc)
14320 		return -ENODEV;
14321 
14322 	rc = bnxt_alloc_crash_dump_mem(bp);
14323 	if (rc)
14324 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14325 			    rc);
14326 	if (!rc) {
14327 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14328 		if (rc) {
14329 			bnxt_free_crash_dump_mem(bp);
14330 			netdev_warn(bp->dev,
14331 				    "hwrm crash dump mem failure rc: %d\n", rc);
14332 		}
14333 	}
14334 
14335 	if (bnxt_fw_pre_resv_vnics(bp))
14336 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14337 
14338 	bnxt_hwrm_func_qcfg(bp);
14339 	bnxt_hwrm_vnic_qcaps(bp);
14340 	bnxt_hwrm_port_led_qcaps(bp);
14341 	bnxt_ethtool_init(bp);
14342 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14343 		__bnxt_hwrm_ptp_qcfg(bp);
14344 	bnxt_dcb_init(bp);
14345 	bnxt_hwmon_init(bp);
14346 	return 0;
14347 }
14348 
bnxt_set_dflt_rss_hash_type(struct bnxt * bp)14349 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14350 {
14351 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14352 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14353 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14354 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14355 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14356 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14357 		bp->rss_hash_delta = bp->rss_hash_cfg;
14358 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14359 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14360 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14361 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14362 	}
14363 }
14364 
bnxt_set_dflt_rfs(struct bnxt * bp)14365 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14366 {
14367 	struct net_device *dev = bp->dev;
14368 
14369 	dev->hw_features &= ~NETIF_F_NTUPLE;
14370 	dev->features &= ~NETIF_F_NTUPLE;
14371 	bp->flags &= ~BNXT_FLAG_RFS;
14372 	if (bnxt_rfs_supported(bp)) {
14373 		dev->hw_features |= NETIF_F_NTUPLE;
14374 		if (bnxt_rfs_capable(bp, false)) {
14375 			bp->flags |= BNXT_FLAG_RFS;
14376 			dev->features |= NETIF_F_NTUPLE;
14377 		}
14378 	}
14379 }
14380 
bnxt_fw_init_one_p3(struct bnxt * bp)14381 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14382 {
14383 	struct pci_dev *pdev = bp->pdev;
14384 
14385 	bnxt_set_dflt_rss_hash_type(bp);
14386 	bnxt_set_dflt_rfs(bp);
14387 
14388 	bnxt_get_wol_settings(bp);
14389 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14390 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14391 	else
14392 		device_set_wakeup_capable(&pdev->dev, false);
14393 
14394 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14395 	bnxt_hwrm_coal_params_qcaps(bp);
14396 }
14397 
14398 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14399 
bnxt_fw_init_one(struct bnxt * bp)14400 int bnxt_fw_init_one(struct bnxt *bp)
14401 {
14402 	int rc;
14403 
14404 	rc = bnxt_fw_init_one_p1(bp);
14405 	if (rc) {
14406 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14407 		return rc;
14408 	}
14409 	rc = bnxt_fw_init_one_p2(bp);
14410 	if (rc) {
14411 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14412 		return rc;
14413 	}
14414 	rc = bnxt_probe_phy(bp, false);
14415 	if (rc)
14416 		return rc;
14417 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14418 	if (rc)
14419 		return rc;
14420 
14421 	bnxt_fw_init_one_p3(bp);
14422 	return 0;
14423 }
14424 
bnxt_fw_reset_writel(struct bnxt * bp,int reg_idx)14425 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14426 {
14427 	struct bnxt_fw_health *fw_health = bp->fw_health;
14428 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14429 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14430 	u32 reg_type, reg_off, delay_msecs;
14431 
14432 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14433 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14434 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14435 	switch (reg_type) {
14436 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14437 		pci_write_config_dword(bp->pdev, reg_off, val);
14438 		break;
14439 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14440 		writel(reg_off & BNXT_GRC_BASE_MASK,
14441 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14442 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14443 		fallthrough;
14444 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14445 		writel(val, bp->bar0 + reg_off);
14446 		break;
14447 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14448 		writel(val, bp->bar1 + reg_off);
14449 		break;
14450 	}
14451 	if (delay_msecs) {
14452 		pci_read_config_dword(bp->pdev, 0, &val);
14453 		msleep(delay_msecs);
14454 	}
14455 }
14456 
bnxt_hwrm_reset_permitted(struct bnxt * bp)14457 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14458 {
14459 	struct hwrm_func_qcfg_output *resp;
14460 	struct hwrm_func_qcfg_input *req;
14461 	bool result = true; /* firmware will enforce if unknown */
14462 
14463 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14464 		return result;
14465 
14466 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14467 		return result;
14468 
14469 	req->fid = cpu_to_le16(0xffff);
14470 	resp = hwrm_req_hold(bp, req);
14471 	if (!hwrm_req_send(bp, req))
14472 		result = !!(le16_to_cpu(resp->flags) &
14473 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14474 	hwrm_req_drop(bp, req);
14475 	return result;
14476 }
14477 
bnxt_reset_all(struct bnxt * bp)14478 static void bnxt_reset_all(struct bnxt *bp)
14479 {
14480 	struct bnxt_fw_health *fw_health = bp->fw_health;
14481 	int i, rc;
14482 
14483 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14484 		bnxt_fw_reset_via_optee(bp);
14485 		bp->fw_reset_timestamp = jiffies;
14486 		return;
14487 	}
14488 
14489 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14490 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14491 			bnxt_fw_reset_writel(bp, i);
14492 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14493 		struct hwrm_fw_reset_input *req;
14494 
14495 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14496 		if (!rc) {
14497 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14498 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14499 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14500 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14501 			rc = hwrm_req_send(bp, req);
14502 		}
14503 		if (rc != -ENODEV)
14504 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14505 	}
14506 	bp->fw_reset_timestamp = jiffies;
14507 }
14508 
bnxt_fw_reset_timeout(struct bnxt * bp)14509 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14510 {
14511 	return time_after(jiffies, bp->fw_reset_timestamp +
14512 			  (bp->fw_reset_max_dsecs * HZ / 10));
14513 }
14514 
bnxt_fw_reset_abort(struct bnxt * bp,int rc)14515 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14516 {
14517 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14518 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14519 		bnxt_dl_health_fw_status_update(bp, false);
14520 	bp->fw_reset_state = 0;
14521 	dev_close(bp->dev);
14522 }
14523 
bnxt_fw_reset_task(struct work_struct * work)14524 static void bnxt_fw_reset_task(struct work_struct *work)
14525 {
14526 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14527 	int rc = 0;
14528 
14529 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14530 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14531 		return;
14532 	}
14533 
14534 	switch (bp->fw_reset_state) {
14535 	case BNXT_FW_RESET_STATE_POLL_VF: {
14536 		int n = bnxt_get_registered_vfs(bp);
14537 		int tmo;
14538 
14539 		if (n < 0) {
14540 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14541 				   n, jiffies_to_msecs(jiffies -
14542 				   bp->fw_reset_timestamp));
14543 			goto fw_reset_abort;
14544 		} else if (n > 0) {
14545 			if (bnxt_fw_reset_timeout(bp)) {
14546 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14547 				bp->fw_reset_state = 0;
14548 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14549 					   n);
14550 				goto ulp_start;
14551 			}
14552 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14553 			return;
14554 		}
14555 		bp->fw_reset_timestamp = jiffies;
14556 		rtnl_lock();
14557 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14558 			bnxt_fw_reset_abort(bp, rc);
14559 			rtnl_unlock();
14560 			goto ulp_start;
14561 		}
14562 		bnxt_fw_reset_close(bp);
14563 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14564 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14565 			tmo = HZ / 10;
14566 		} else {
14567 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14568 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14569 		}
14570 		rtnl_unlock();
14571 		bnxt_queue_fw_reset_work(bp, tmo);
14572 		return;
14573 	}
14574 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
14575 		u32 val;
14576 
14577 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14578 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
14579 		    !bnxt_fw_reset_timeout(bp)) {
14580 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14581 			return;
14582 		}
14583 
14584 		if (!bp->fw_health->primary) {
14585 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
14586 
14587 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14588 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14589 			return;
14590 		}
14591 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14592 	}
14593 		fallthrough;
14594 	case BNXT_FW_RESET_STATE_RESET_FW:
14595 		bnxt_reset_all(bp);
14596 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14597 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
14598 		return;
14599 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
14600 		bnxt_inv_fw_health_reg(bp);
14601 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
14602 		    !bp->fw_reset_min_dsecs) {
14603 			u16 val;
14604 
14605 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14606 			if (val == 0xffff) {
14607 				if (bnxt_fw_reset_timeout(bp)) {
14608 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
14609 					rc = -ETIMEDOUT;
14610 					goto fw_reset_abort;
14611 				}
14612 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
14613 				return;
14614 			}
14615 		}
14616 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14617 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
14618 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
14619 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
14620 			bnxt_dl_remote_reload(bp);
14621 		if (pci_enable_device(bp->pdev)) {
14622 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
14623 			rc = -ENODEV;
14624 			goto fw_reset_abort;
14625 		}
14626 		pci_set_master(bp->pdev);
14627 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
14628 		fallthrough;
14629 	case BNXT_FW_RESET_STATE_POLL_FW:
14630 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
14631 		rc = bnxt_hwrm_poll(bp);
14632 		if (rc) {
14633 			if (bnxt_fw_reset_timeout(bp)) {
14634 				netdev_err(bp->dev, "Firmware reset aborted\n");
14635 				goto fw_reset_abort_status;
14636 			}
14637 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14638 			return;
14639 		}
14640 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
14641 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
14642 		fallthrough;
14643 	case BNXT_FW_RESET_STATE_OPENING:
14644 		while (!rtnl_trylock()) {
14645 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14646 			return;
14647 		}
14648 		rc = bnxt_open(bp->dev);
14649 		if (rc) {
14650 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
14651 			bnxt_fw_reset_abort(bp, rc);
14652 			rtnl_unlock();
14653 			goto ulp_start;
14654 		}
14655 
14656 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
14657 		    bp->fw_health->enabled) {
14658 			bp->fw_health->last_fw_reset_cnt =
14659 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14660 		}
14661 		bp->fw_reset_state = 0;
14662 		/* Make sure fw_reset_state is 0 before clearing the flag */
14663 		smp_mb__before_atomic();
14664 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14665 		bnxt_ptp_reapply_pps(bp);
14666 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
14667 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
14668 			bnxt_dl_health_fw_recovery_done(bp);
14669 			bnxt_dl_health_fw_status_update(bp, true);
14670 		}
14671 		rtnl_unlock();
14672 		bnxt_ulp_start(bp, 0);
14673 		bnxt_reenable_sriov(bp);
14674 		rtnl_lock();
14675 		bnxt_vf_reps_alloc(bp);
14676 		bnxt_vf_reps_open(bp);
14677 		rtnl_unlock();
14678 		break;
14679 	}
14680 	return;
14681 
14682 fw_reset_abort_status:
14683 	if (bp->fw_health->status_reliable ||
14684 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
14685 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14686 
14687 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
14688 	}
14689 fw_reset_abort:
14690 	rtnl_lock();
14691 	bnxt_fw_reset_abort(bp, rc);
14692 	rtnl_unlock();
14693 ulp_start:
14694 	bnxt_ulp_start(bp, rc);
14695 }
14696 
bnxt_init_board(struct pci_dev * pdev,struct net_device * dev)14697 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
14698 {
14699 	int rc;
14700 	struct bnxt *bp = netdev_priv(dev);
14701 
14702 	SET_NETDEV_DEV(dev, &pdev->dev);
14703 
14704 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
14705 	rc = pci_enable_device(pdev);
14706 	if (rc) {
14707 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
14708 		goto init_err;
14709 	}
14710 
14711 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
14712 		dev_err(&pdev->dev,
14713 			"Cannot find PCI device base address, aborting\n");
14714 		rc = -ENODEV;
14715 		goto init_err_disable;
14716 	}
14717 
14718 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
14719 	if (rc) {
14720 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
14721 		goto init_err_disable;
14722 	}
14723 
14724 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
14725 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
14726 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
14727 		rc = -EIO;
14728 		goto init_err_release;
14729 	}
14730 
14731 	pci_set_master(pdev);
14732 
14733 	bp->dev = dev;
14734 	bp->pdev = pdev;
14735 
14736 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
14737 	 * determines the BAR size.
14738 	 */
14739 	bp->bar0 = pci_ioremap_bar(pdev, 0);
14740 	if (!bp->bar0) {
14741 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
14742 		rc = -ENOMEM;
14743 		goto init_err_release;
14744 	}
14745 
14746 	bp->bar2 = pci_ioremap_bar(pdev, 4);
14747 	if (!bp->bar2) {
14748 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
14749 		rc = -ENOMEM;
14750 		goto init_err_release;
14751 	}
14752 
14753 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
14754 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
14755 
14756 	spin_lock_init(&bp->ntp_fltr_lock);
14757 #if BITS_PER_LONG == 32
14758 	spin_lock_init(&bp->db_lock);
14759 #endif
14760 
14761 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
14762 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
14763 
14764 	timer_setup(&bp->timer, bnxt_timer, 0);
14765 	bp->current_interval = BNXT_TIMER_INTERVAL;
14766 
14767 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
14768 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
14769 
14770 	clear_bit(BNXT_STATE_OPEN, &bp->state);
14771 	return 0;
14772 
14773 init_err_release:
14774 	bnxt_unmap_bars(bp, pdev);
14775 	pci_release_regions(pdev);
14776 
14777 init_err_disable:
14778 	pci_disable_device(pdev);
14779 
14780 init_err:
14781 	return rc;
14782 }
14783 
14784 /* rtnl_lock held */
bnxt_change_mac_addr(struct net_device * dev,void * p)14785 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
14786 {
14787 	struct sockaddr *addr = p;
14788 	struct bnxt *bp = netdev_priv(dev);
14789 	int rc = 0;
14790 
14791 	if (!is_valid_ether_addr(addr->sa_data))
14792 		return -EADDRNOTAVAIL;
14793 
14794 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
14795 		return 0;
14796 
14797 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
14798 	if (rc)
14799 		return rc;
14800 
14801 	eth_hw_addr_set(dev, addr->sa_data);
14802 	bnxt_clear_usr_fltrs(bp, true);
14803 	if (netif_running(dev)) {
14804 		bnxt_close_nic(bp, false, false);
14805 		rc = bnxt_open_nic(bp, false, false);
14806 	}
14807 
14808 	return rc;
14809 }
14810 
14811 /* rtnl_lock held */
bnxt_change_mtu(struct net_device * dev,int new_mtu)14812 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
14813 {
14814 	struct bnxt *bp = netdev_priv(dev);
14815 
14816 	if (netif_running(dev))
14817 		bnxt_close_nic(bp, true, false);
14818 
14819 	WRITE_ONCE(dev->mtu, new_mtu);
14820 
14821 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
14822 	 * program is attached.  We need to set the AGG rings settings and
14823 	 * rx_skb_func accordingly.
14824 	 */
14825 	if (READ_ONCE(bp->xdp_prog))
14826 		bnxt_set_rx_skb_mode(bp, true);
14827 
14828 	bnxt_set_ring_params(bp);
14829 
14830 	if (netif_running(dev))
14831 		return bnxt_open_nic(bp, true, false);
14832 
14833 	return 0;
14834 }
14835 
bnxt_setup_mq_tc(struct net_device * dev,u8 tc)14836 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
14837 {
14838 	struct bnxt *bp = netdev_priv(dev);
14839 	bool sh = false;
14840 	int rc, tx_cp;
14841 
14842 	if (tc > bp->max_tc) {
14843 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
14844 			   tc, bp->max_tc);
14845 		return -EINVAL;
14846 	}
14847 
14848 	if (bp->num_tc == tc)
14849 		return 0;
14850 
14851 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
14852 		sh = true;
14853 
14854 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
14855 			      sh, tc, bp->tx_nr_rings_xdp);
14856 	if (rc)
14857 		return rc;
14858 
14859 	/* Needs to close the device and do hw resource re-allocations */
14860 	if (netif_running(bp->dev))
14861 		bnxt_close_nic(bp, true, false);
14862 
14863 	if (tc) {
14864 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
14865 		netdev_set_num_tc(dev, tc);
14866 		bp->num_tc = tc;
14867 	} else {
14868 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
14869 		netdev_reset_tc(dev);
14870 		bp->num_tc = 0;
14871 	}
14872 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
14873 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
14874 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
14875 			       tx_cp + bp->rx_nr_rings;
14876 
14877 	if (netif_running(bp->dev))
14878 		return bnxt_open_nic(bp, true, false);
14879 
14880 	return 0;
14881 }
14882 
bnxt_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)14883 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
14884 				  void *cb_priv)
14885 {
14886 	struct bnxt *bp = cb_priv;
14887 
14888 	if (!bnxt_tc_flower_enabled(bp) ||
14889 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
14890 		return -EOPNOTSUPP;
14891 
14892 	switch (type) {
14893 	case TC_SETUP_CLSFLOWER:
14894 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
14895 	default:
14896 		return -EOPNOTSUPP;
14897 	}
14898 }
14899 
14900 LIST_HEAD(bnxt_block_cb_list);
14901 
bnxt_setup_tc(struct net_device * dev,enum tc_setup_type type,void * type_data)14902 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
14903 			 void *type_data)
14904 {
14905 	struct bnxt *bp = netdev_priv(dev);
14906 
14907 	switch (type) {
14908 	case TC_SETUP_BLOCK:
14909 		return flow_block_cb_setup_simple(type_data,
14910 						  &bnxt_block_cb_list,
14911 						  bnxt_setup_tc_block_cb,
14912 						  bp, bp, true);
14913 	case TC_SETUP_QDISC_MQPRIO: {
14914 		struct tc_mqprio_qopt *mqprio = type_data;
14915 
14916 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
14917 
14918 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
14919 	}
14920 	default:
14921 		return -EOPNOTSUPP;
14922 	}
14923 }
14924 
bnxt_get_ntp_filter_idx(struct bnxt * bp,struct flow_keys * fkeys,const struct sk_buff * skb)14925 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
14926 			    const struct sk_buff *skb)
14927 {
14928 	struct bnxt_vnic_info *vnic;
14929 
14930 	if (skb)
14931 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
14932 
14933 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
14934 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
14935 }
14936 
bnxt_insert_ntp_filter(struct bnxt * bp,struct bnxt_ntuple_filter * fltr,u32 idx)14937 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
14938 			   u32 idx)
14939 {
14940 	struct hlist_head *head;
14941 	int bit_id;
14942 
14943 	spin_lock_bh(&bp->ntp_fltr_lock);
14944 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
14945 	if (bit_id < 0) {
14946 		spin_unlock_bh(&bp->ntp_fltr_lock);
14947 		return -ENOMEM;
14948 	}
14949 
14950 	fltr->base.sw_id = (u16)bit_id;
14951 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
14952 	fltr->base.flags |= BNXT_ACT_RING_DST;
14953 	head = &bp->ntp_fltr_hash_tbl[idx];
14954 	hlist_add_head_rcu(&fltr->base.hash, head);
14955 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
14956 	bnxt_insert_usr_fltr(bp, &fltr->base);
14957 	bp->ntp_fltr_count++;
14958 	spin_unlock_bh(&bp->ntp_fltr_lock);
14959 	return 0;
14960 }
14961 
bnxt_fltr_match(struct bnxt_ntuple_filter * f1,struct bnxt_ntuple_filter * f2)14962 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
14963 			    struct bnxt_ntuple_filter *f2)
14964 {
14965 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
14966 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
14967 	struct flow_keys *keys1 = &f1->fkeys;
14968 	struct flow_keys *keys2 = &f2->fkeys;
14969 
14970 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
14971 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
14972 		return false;
14973 
14974 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
14975 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
14976 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
14977 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
14978 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
14979 			return false;
14980 	} else {
14981 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
14982 				     &keys2->addrs.v6addrs.src) ||
14983 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
14984 				     &masks2->addrs.v6addrs.src) ||
14985 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
14986 				     &keys2->addrs.v6addrs.dst) ||
14987 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
14988 				     &masks2->addrs.v6addrs.dst))
14989 			return false;
14990 	}
14991 
14992 	return keys1->ports.src == keys2->ports.src &&
14993 	       masks1->ports.src == masks2->ports.src &&
14994 	       keys1->ports.dst == keys2->ports.dst &&
14995 	       masks1->ports.dst == masks2->ports.dst &&
14996 	       keys1->control.flags == keys2->control.flags &&
14997 	       f1->l2_fltr == f2->l2_fltr;
14998 }
14999 
15000 struct bnxt_ntuple_filter *
bnxt_lookup_ntp_filter_from_idx(struct bnxt * bp,struct bnxt_ntuple_filter * fltr,u32 idx)15001 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15002 				struct bnxt_ntuple_filter *fltr, u32 idx)
15003 {
15004 	struct bnxt_ntuple_filter *f;
15005 	struct hlist_head *head;
15006 
15007 	head = &bp->ntp_fltr_hash_tbl[idx];
15008 	hlist_for_each_entry_rcu(f, head, base.hash) {
15009 		if (bnxt_fltr_match(f, fltr))
15010 			return f;
15011 	}
15012 	return NULL;
15013 }
15014 
15015 #ifdef CONFIG_RFS_ACCEL
bnxt_rx_flow_steer(struct net_device * dev,const struct sk_buff * skb,u16 rxq_index,u32 flow_id)15016 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15017 			      u16 rxq_index, u32 flow_id)
15018 {
15019 	struct bnxt *bp = netdev_priv(dev);
15020 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15021 	struct flow_keys *fkeys;
15022 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15023 	struct bnxt_l2_filter *l2_fltr;
15024 	int rc = 0, idx;
15025 	u32 flags;
15026 
15027 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15028 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15029 		atomic_inc(&l2_fltr->refcnt);
15030 	} else {
15031 		struct bnxt_l2_key key;
15032 
15033 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15034 		key.vlan = 0;
15035 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15036 		if (!l2_fltr)
15037 			return -EINVAL;
15038 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15039 			bnxt_del_l2_filter(bp, l2_fltr);
15040 			return -EINVAL;
15041 		}
15042 	}
15043 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
15044 	if (!new_fltr) {
15045 		bnxt_del_l2_filter(bp, l2_fltr);
15046 		return -ENOMEM;
15047 	}
15048 
15049 	fkeys = &new_fltr->fkeys;
15050 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15051 		rc = -EPROTONOSUPPORT;
15052 		goto err_free;
15053 	}
15054 
15055 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15056 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15057 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15058 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15059 		rc = -EPROTONOSUPPORT;
15060 		goto err_free;
15061 	}
15062 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15063 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15064 		if (bp->hwrm_spec_code < 0x10601) {
15065 			rc = -EPROTONOSUPPORT;
15066 			goto err_free;
15067 		}
15068 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15069 	}
15070 	flags = fkeys->control.flags;
15071 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15072 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15073 		rc = -EPROTONOSUPPORT;
15074 		goto err_free;
15075 	}
15076 	new_fltr->l2_fltr = l2_fltr;
15077 
15078 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15079 	rcu_read_lock();
15080 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15081 	if (fltr) {
15082 		rc = fltr->base.sw_id;
15083 		rcu_read_unlock();
15084 		goto err_free;
15085 	}
15086 	rcu_read_unlock();
15087 
15088 	new_fltr->flow_id = flow_id;
15089 	new_fltr->base.rxq = rxq_index;
15090 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15091 	if (!rc) {
15092 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15093 		return new_fltr->base.sw_id;
15094 	}
15095 
15096 err_free:
15097 	bnxt_del_l2_filter(bp, l2_fltr);
15098 	kfree(new_fltr);
15099 	return rc;
15100 }
15101 #endif
15102 
bnxt_del_ntp_filter(struct bnxt * bp,struct bnxt_ntuple_filter * fltr)15103 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15104 {
15105 	spin_lock_bh(&bp->ntp_fltr_lock);
15106 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15107 		spin_unlock_bh(&bp->ntp_fltr_lock);
15108 		return;
15109 	}
15110 	hlist_del_rcu(&fltr->base.hash);
15111 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15112 	bp->ntp_fltr_count--;
15113 	spin_unlock_bh(&bp->ntp_fltr_lock);
15114 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15115 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15116 	kfree_rcu(fltr, base.rcu);
15117 }
15118 
bnxt_cfg_ntp_filters(struct bnxt * bp)15119 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15120 {
15121 #ifdef CONFIG_RFS_ACCEL
15122 	int i;
15123 
15124 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15125 		struct hlist_head *head;
15126 		struct hlist_node *tmp;
15127 		struct bnxt_ntuple_filter *fltr;
15128 		int rc;
15129 
15130 		head = &bp->ntp_fltr_hash_tbl[i];
15131 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15132 			bool del = false;
15133 
15134 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15135 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15136 					continue;
15137 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15138 							fltr->flow_id,
15139 							fltr->base.sw_id)) {
15140 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15141 									 fltr);
15142 					del = true;
15143 				}
15144 			} else {
15145 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15146 								       fltr);
15147 				if (rc)
15148 					del = true;
15149 				else
15150 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15151 			}
15152 
15153 			if (del)
15154 				bnxt_del_ntp_filter(bp, fltr);
15155 		}
15156 	}
15157 #endif
15158 }
15159 
bnxt_udp_tunnel_set_port(struct net_device * netdev,unsigned int table,unsigned int entry,struct udp_tunnel_info * ti)15160 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15161 				    unsigned int entry, struct udp_tunnel_info *ti)
15162 {
15163 	struct bnxt *bp = netdev_priv(netdev);
15164 	unsigned int cmd;
15165 
15166 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15167 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15168 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15169 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15170 	else
15171 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15172 
15173 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15174 }
15175 
bnxt_udp_tunnel_unset_port(struct net_device * netdev,unsigned int table,unsigned int entry,struct udp_tunnel_info * ti)15176 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15177 				      unsigned int entry, struct udp_tunnel_info *ti)
15178 {
15179 	struct bnxt *bp = netdev_priv(netdev);
15180 	unsigned int cmd;
15181 
15182 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15183 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15184 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15185 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15186 	else
15187 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15188 
15189 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15190 }
15191 
15192 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15193 	.set_port	= bnxt_udp_tunnel_set_port,
15194 	.unset_port	= bnxt_udp_tunnel_unset_port,
15195 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15196 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15197 	.tables		= {
15198 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15199 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15200 	},
15201 }, bnxt_udp_tunnels_p7 = {
15202 	.set_port	= bnxt_udp_tunnel_set_port,
15203 	.unset_port	= bnxt_udp_tunnel_unset_port,
15204 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15205 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15206 	.tables		= {
15207 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15208 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15209 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15210 	},
15211 };
15212 
bnxt_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 filter_mask,int nlflags)15213 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15214 			       struct net_device *dev, u32 filter_mask,
15215 			       int nlflags)
15216 {
15217 	struct bnxt *bp = netdev_priv(dev);
15218 
15219 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15220 				       nlflags, filter_mask, NULL);
15221 }
15222 
bnxt_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags,struct netlink_ext_ack * extack)15223 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15224 			       u16 flags, struct netlink_ext_ack *extack)
15225 {
15226 	struct bnxt *bp = netdev_priv(dev);
15227 	struct nlattr *attr, *br_spec;
15228 	int rem, rc = 0;
15229 
15230 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15231 		return -EOPNOTSUPP;
15232 
15233 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15234 	if (!br_spec)
15235 		return -EINVAL;
15236 
15237 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15238 		u16 mode;
15239 
15240 		mode = nla_get_u16(attr);
15241 		if (mode == bp->br_mode)
15242 			break;
15243 
15244 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15245 		if (!rc)
15246 			bp->br_mode = mode;
15247 		break;
15248 	}
15249 	return rc;
15250 }
15251 
bnxt_get_port_parent_id(struct net_device * dev,struct netdev_phys_item_id * ppid)15252 int bnxt_get_port_parent_id(struct net_device *dev,
15253 			    struct netdev_phys_item_id *ppid)
15254 {
15255 	struct bnxt *bp = netdev_priv(dev);
15256 
15257 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15258 		return -EOPNOTSUPP;
15259 
15260 	/* The PF and it's VF-reps only support the switchdev framework */
15261 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15262 		return -EOPNOTSUPP;
15263 
15264 	ppid->id_len = sizeof(bp->dsn);
15265 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15266 
15267 	return 0;
15268 }
15269 
15270 static const struct net_device_ops bnxt_netdev_ops = {
15271 	.ndo_open		= bnxt_open,
15272 	.ndo_start_xmit		= bnxt_start_xmit,
15273 	.ndo_stop		= bnxt_close,
15274 	.ndo_get_stats64	= bnxt_get_stats64,
15275 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15276 	.ndo_eth_ioctl		= bnxt_ioctl,
15277 	.ndo_validate_addr	= eth_validate_addr,
15278 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15279 	.ndo_change_mtu		= bnxt_change_mtu,
15280 	.ndo_fix_features	= bnxt_fix_features,
15281 	.ndo_set_features	= bnxt_set_features,
15282 	.ndo_features_check	= bnxt_features_check,
15283 	.ndo_tx_timeout		= bnxt_tx_timeout,
15284 #ifdef CONFIG_BNXT_SRIOV
15285 	.ndo_get_vf_config	= bnxt_get_vf_config,
15286 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15287 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15288 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15289 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15290 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15291 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15292 #endif
15293 	.ndo_setup_tc           = bnxt_setup_tc,
15294 #ifdef CONFIG_RFS_ACCEL
15295 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15296 #endif
15297 	.ndo_bpf		= bnxt_xdp,
15298 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15299 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15300 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15301 };
15302 
bnxt_get_queue_stats_rx(struct net_device * dev,int i,struct netdev_queue_stats_rx * stats)15303 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15304 				    struct netdev_queue_stats_rx *stats)
15305 {
15306 	struct bnxt *bp = netdev_priv(dev);
15307 	struct bnxt_cp_ring_info *cpr;
15308 	u64 *sw;
15309 
15310 	cpr = &bp->bnapi[i]->cp_ring;
15311 	sw = cpr->stats.sw_stats;
15312 
15313 	stats->packets = 0;
15314 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15315 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15316 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15317 
15318 	stats->bytes = 0;
15319 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15320 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15321 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15322 
15323 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15324 }
15325 
bnxt_get_queue_stats_tx(struct net_device * dev,int i,struct netdev_queue_stats_tx * stats)15326 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15327 				    struct netdev_queue_stats_tx *stats)
15328 {
15329 	struct bnxt *bp = netdev_priv(dev);
15330 	struct bnxt_napi *bnapi;
15331 	u64 *sw;
15332 
15333 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15334 	sw = bnapi->cp_ring.stats.sw_stats;
15335 
15336 	stats->packets = 0;
15337 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15338 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15339 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15340 
15341 	stats->bytes = 0;
15342 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15343 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15344 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15345 }
15346 
bnxt_get_base_stats(struct net_device * dev,struct netdev_queue_stats_rx * rx,struct netdev_queue_stats_tx * tx)15347 static void bnxt_get_base_stats(struct net_device *dev,
15348 				struct netdev_queue_stats_rx *rx,
15349 				struct netdev_queue_stats_tx *tx)
15350 {
15351 	struct bnxt *bp = netdev_priv(dev);
15352 
15353 	rx->packets = bp->net_stats_prev.rx_packets;
15354 	rx->bytes = bp->net_stats_prev.rx_bytes;
15355 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15356 
15357 	tx->packets = bp->net_stats_prev.tx_packets;
15358 	tx->bytes = bp->net_stats_prev.tx_bytes;
15359 }
15360 
15361 static const struct netdev_stat_ops bnxt_stat_ops = {
15362 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15363 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15364 	.get_base_stats		= bnxt_get_base_stats,
15365 };
15366 
bnxt_queue_mem_alloc(struct net_device * dev,void * qmem,int idx)15367 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15368 {
15369 	struct bnxt_rx_ring_info *rxr, *clone;
15370 	struct bnxt *bp = netdev_priv(dev);
15371 	struct bnxt_ring_struct *ring;
15372 	int rc;
15373 
15374 	rxr = &bp->rx_ring[idx];
15375 	clone = qmem;
15376 	memcpy(clone, rxr, sizeof(*rxr));
15377 	bnxt_init_rx_ring_struct(bp, clone);
15378 	bnxt_reset_rx_ring_struct(bp, clone);
15379 
15380 	clone->rx_prod = 0;
15381 	clone->rx_agg_prod = 0;
15382 	clone->rx_sw_agg_prod = 0;
15383 	clone->rx_next_cons = 0;
15384 
15385 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15386 	if (rc)
15387 		return rc;
15388 
15389 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15390 	if (rc < 0)
15391 		goto err_page_pool_destroy;
15392 
15393 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15394 					MEM_TYPE_PAGE_POOL,
15395 					clone->page_pool);
15396 	if (rc)
15397 		goto err_rxq_info_unreg;
15398 
15399 	ring = &clone->rx_ring_struct;
15400 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15401 	if (rc)
15402 		goto err_free_rx_ring;
15403 
15404 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15405 		ring = &clone->rx_agg_ring_struct;
15406 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15407 		if (rc)
15408 			goto err_free_rx_agg_ring;
15409 
15410 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15411 		if (rc)
15412 			goto err_free_rx_agg_ring;
15413 	}
15414 
15415 	if (bp->flags & BNXT_FLAG_TPA) {
15416 		rc = bnxt_alloc_one_tpa_info(bp, clone);
15417 		if (rc)
15418 			goto err_free_tpa_info;
15419 	}
15420 
15421 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15422 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15423 
15424 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15425 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15426 		bnxt_alloc_one_rx_ring_page(bp, clone, idx);
15427 	if (bp->flags & BNXT_FLAG_TPA)
15428 		bnxt_alloc_one_tpa_info_data(bp, clone);
15429 
15430 	return 0;
15431 
15432 err_free_tpa_info:
15433 	bnxt_free_one_tpa_info(bp, clone);
15434 err_free_rx_agg_ring:
15435 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15436 err_free_rx_ring:
15437 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15438 err_rxq_info_unreg:
15439 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15440 err_page_pool_destroy:
15441 	page_pool_destroy(clone->page_pool);
15442 	if (bnxt_separate_head_pool())
15443 		page_pool_destroy(clone->head_pool);
15444 	clone->page_pool = NULL;
15445 	clone->head_pool = NULL;
15446 	return rc;
15447 }
15448 
bnxt_queue_mem_free(struct net_device * dev,void * qmem)15449 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15450 {
15451 	struct bnxt_rx_ring_info *rxr = qmem;
15452 	struct bnxt *bp = netdev_priv(dev);
15453 	struct bnxt_ring_struct *ring;
15454 
15455 	bnxt_free_one_rx_ring_skbs(bp, rxr);
15456 
15457 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15458 
15459 	page_pool_destroy(rxr->page_pool);
15460 	if (bnxt_separate_head_pool())
15461 		page_pool_destroy(rxr->head_pool);
15462 	rxr->page_pool = NULL;
15463 	rxr->head_pool = NULL;
15464 
15465 	ring = &rxr->rx_ring_struct;
15466 	bnxt_free_ring(bp, &ring->ring_mem);
15467 
15468 	ring = &rxr->rx_agg_ring_struct;
15469 	bnxt_free_ring(bp, &ring->ring_mem);
15470 
15471 	kfree(rxr->rx_agg_bmap);
15472 	rxr->rx_agg_bmap = NULL;
15473 }
15474 
bnxt_copy_rx_ring(struct bnxt * bp,struct bnxt_rx_ring_info * dst,struct bnxt_rx_ring_info * src)15475 static void bnxt_copy_rx_ring(struct bnxt *bp,
15476 			      struct bnxt_rx_ring_info *dst,
15477 			      struct bnxt_rx_ring_info *src)
15478 {
15479 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15480 	struct bnxt_ring_struct *dst_ring, *src_ring;
15481 	int i;
15482 
15483 	dst_ring = &dst->rx_ring_struct;
15484 	dst_rmem = &dst_ring->ring_mem;
15485 	src_ring = &src->rx_ring_struct;
15486 	src_rmem = &src_ring->ring_mem;
15487 
15488 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15489 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15490 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15491 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15492 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15493 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15494 
15495 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15496 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15497 	*dst_rmem->vmem = *src_rmem->vmem;
15498 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15499 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15500 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15501 	}
15502 
15503 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15504 		return;
15505 
15506 	dst_ring = &dst->rx_agg_ring_struct;
15507 	dst_rmem = &dst_ring->ring_mem;
15508 	src_ring = &src->rx_agg_ring_struct;
15509 	src_rmem = &src_ring->ring_mem;
15510 
15511 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15512 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15513 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15514 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15515 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15516 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15517 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15518 
15519 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15520 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15521 	*dst_rmem->vmem = *src_rmem->vmem;
15522 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15523 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15524 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15525 	}
15526 
15527 	dst->rx_agg_bmap = src->rx_agg_bmap;
15528 }
15529 
bnxt_queue_start(struct net_device * dev,void * qmem,int idx)15530 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15531 {
15532 	struct bnxt *bp = netdev_priv(dev);
15533 	struct bnxt_rx_ring_info *rxr, *clone;
15534 	struct bnxt_cp_ring_info *cpr;
15535 	struct bnxt_vnic_info *vnic;
15536 	int i, rc;
15537 
15538 	rxr = &bp->rx_ring[idx];
15539 	clone = qmem;
15540 
15541 	rxr->rx_prod = clone->rx_prod;
15542 	rxr->rx_agg_prod = clone->rx_agg_prod;
15543 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
15544 	rxr->rx_next_cons = clone->rx_next_cons;
15545 	rxr->rx_tpa = clone->rx_tpa;
15546 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
15547 	rxr->page_pool = clone->page_pool;
15548 	rxr->head_pool = clone->head_pool;
15549 	rxr->xdp_rxq = clone->xdp_rxq;
15550 
15551 	bnxt_copy_rx_ring(bp, rxr, clone);
15552 
15553 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
15554 	if (rc)
15555 		return rc;
15556 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
15557 	if (rc)
15558 		goto err_free_hwrm_rx_ring;
15559 
15560 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
15561 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15562 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
15563 
15564 	cpr = &rxr->bnapi->cp_ring;
15565 	cpr->sw_stats->rx.rx_resets++;
15566 
15567 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15568 		vnic = &bp->vnic_info[i];
15569 
15570 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
15571 		if (rc) {
15572 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
15573 				   vnic->vnic_id, rc);
15574 			return rc;
15575 		}
15576 		vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
15577 		bnxt_hwrm_vnic_update(bp, vnic,
15578 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15579 	}
15580 
15581 	return 0;
15582 
15583 err_free_hwrm_rx_ring:
15584 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15585 	return rc;
15586 }
15587 
bnxt_queue_stop(struct net_device * dev,void * qmem,int idx)15588 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
15589 {
15590 	struct bnxt *bp = netdev_priv(dev);
15591 	struct bnxt_rx_ring_info *rxr;
15592 	struct bnxt_vnic_info *vnic;
15593 	int i;
15594 
15595 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15596 		vnic = &bp->vnic_info[i];
15597 		vnic->mru = 0;
15598 		bnxt_hwrm_vnic_update(bp, vnic,
15599 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15600 	}
15601 	/* Make sure NAPI sees that the VNIC is disabled */
15602 	synchronize_net();
15603 	rxr = &bp->rx_ring[idx];
15604 	cancel_work_sync(&rxr->bnapi->cp_ring.dim.work);
15605 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15606 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
15607 	rxr->rx_next_cons = 0;
15608 	page_pool_disable_direct_recycling(rxr->page_pool);
15609 	if (bnxt_separate_head_pool())
15610 		page_pool_disable_direct_recycling(rxr->head_pool);
15611 
15612 	memcpy(qmem, rxr, sizeof(*rxr));
15613 	bnxt_init_rx_ring_struct(bp, qmem);
15614 
15615 	return 0;
15616 }
15617 
15618 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
15619 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
15620 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
15621 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
15622 	.ndo_queue_start	= bnxt_queue_start,
15623 	.ndo_queue_stop		= bnxt_queue_stop,
15624 };
15625 
bnxt_remove_one(struct pci_dev * pdev)15626 static void bnxt_remove_one(struct pci_dev *pdev)
15627 {
15628 	struct net_device *dev = pci_get_drvdata(pdev);
15629 	struct bnxt *bp = netdev_priv(dev);
15630 
15631 	if (BNXT_PF(bp))
15632 		bnxt_sriov_disable(bp);
15633 
15634 	bnxt_rdma_aux_device_del(bp);
15635 
15636 	bnxt_ptp_clear(bp);
15637 	unregister_netdev(dev);
15638 
15639 	bnxt_rdma_aux_device_uninit(bp);
15640 
15641 	bnxt_free_l2_filters(bp, true);
15642 	bnxt_free_ntp_fltrs(bp, true);
15643 	WARN_ON(bp->num_rss_ctx);
15644 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15645 	/* Flush any pending tasks */
15646 	cancel_work_sync(&bp->sp_task);
15647 	cancel_delayed_work_sync(&bp->fw_reset_task);
15648 	bp->sp_event = 0;
15649 
15650 	bnxt_dl_fw_reporters_destroy(bp);
15651 	bnxt_dl_unregister(bp);
15652 	bnxt_shutdown_tc(bp);
15653 
15654 	bnxt_clear_int_mode(bp);
15655 	bnxt_hwrm_func_drv_unrgtr(bp);
15656 	bnxt_free_hwrm_resources(bp);
15657 	bnxt_hwmon_uninit(bp);
15658 	bnxt_ethtool_free(bp);
15659 	bnxt_dcb_free(bp);
15660 	kfree(bp->ptp_cfg);
15661 	bp->ptp_cfg = NULL;
15662 	kfree(bp->fw_health);
15663 	bp->fw_health = NULL;
15664 	bnxt_cleanup_pci(bp);
15665 	bnxt_free_ctx_mem(bp, true);
15666 	bnxt_free_crash_dump_mem(bp);
15667 	kfree(bp->rss_indir_tbl);
15668 	bp->rss_indir_tbl = NULL;
15669 	bnxt_free_port_stats(bp);
15670 	free_netdev(dev);
15671 }
15672 
bnxt_probe_phy(struct bnxt * bp,bool fw_dflt)15673 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
15674 {
15675 	int rc = 0;
15676 	struct bnxt_link_info *link_info = &bp->link_info;
15677 
15678 	bp->phy_flags = 0;
15679 	rc = bnxt_hwrm_phy_qcaps(bp);
15680 	if (rc) {
15681 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
15682 			   rc);
15683 		return rc;
15684 	}
15685 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
15686 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
15687 	else
15688 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
15689 	if (!fw_dflt)
15690 		return 0;
15691 
15692 	mutex_lock(&bp->link_lock);
15693 	rc = bnxt_update_link(bp, false);
15694 	if (rc) {
15695 		mutex_unlock(&bp->link_lock);
15696 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
15697 			   rc);
15698 		return rc;
15699 	}
15700 
15701 	/* Older firmware does not have supported_auto_speeds, so assume
15702 	 * that all supported speeds can be autonegotiated.
15703 	 */
15704 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
15705 		link_info->support_auto_speeds = link_info->support_speeds;
15706 
15707 	bnxt_init_ethtool_link_settings(bp);
15708 	mutex_unlock(&bp->link_lock);
15709 	return 0;
15710 }
15711 
bnxt_get_max_irq(struct pci_dev * pdev)15712 static int bnxt_get_max_irq(struct pci_dev *pdev)
15713 {
15714 	u16 ctrl;
15715 
15716 	if (!pdev->msix_cap)
15717 		return 1;
15718 
15719 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
15720 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
15721 }
15722 
_bnxt_get_max_rings(struct bnxt * bp,int * max_rx,int * max_tx,int * max_cp)15723 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15724 				int *max_cp)
15725 {
15726 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
15727 	int max_ring_grps = 0, max_irq;
15728 
15729 	*max_tx = hw_resc->max_tx_rings;
15730 	*max_rx = hw_resc->max_rx_rings;
15731 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
15732 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
15733 			bnxt_get_ulp_msix_num_in_use(bp),
15734 			hw_resc->max_stat_ctxs -
15735 			bnxt_get_ulp_stat_ctxs_in_use(bp));
15736 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
15737 		*max_cp = min_t(int, *max_cp, max_irq);
15738 	max_ring_grps = hw_resc->max_hw_ring_grps;
15739 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
15740 		*max_cp -= 1;
15741 		*max_rx -= 2;
15742 	}
15743 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15744 		*max_rx >>= 1;
15745 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
15746 		int rc;
15747 
15748 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
15749 		if (rc) {
15750 			*max_rx = 0;
15751 			*max_tx = 0;
15752 		}
15753 		/* On P5 chips, max_cp output param should be available NQs */
15754 		*max_cp = max_irq;
15755 	}
15756 	*max_rx = min_t(int, *max_rx, max_ring_grps);
15757 }
15758 
bnxt_get_max_rings(struct bnxt * bp,int * max_rx,int * max_tx,bool shared)15759 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
15760 {
15761 	int rx, tx, cp;
15762 
15763 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
15764 	*max_rx = rx;
15765 	*max_tx = tx;
15766 	if (!rx || !tx || !cp)
15767 		return -ENOMEM;
15768 
15769 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
15770 }
15771 
bnxt_get_dflt_rings(struct bnxt * bp,int * max_rx,int * max_tx,bool shared)15772 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15773 			       bool shared)
15774 {
15775 	int rc;
15776 
15777 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15778 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
15779 		/* Not enough rings, try disabling agg rings. */
15780 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
15781 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15782 		if (rc) {
15783 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
15784 			bp->flags |= BNXT_FLAG_AGG_RINGS;
15785 			return rc;
15786 		}
15787 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
15788 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15789 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15790 		bnxt_set_ring_params(bp);
15791 	}
15792 
15793 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
15794 		int max_cp, max_stat, max_irq;
15795 
15796 		/* Reserve minimum resources for RoCE */
15797 		max_cp = bnxt_get_max_func_cp_rings(bp);
15798 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
15799 		max_irq = bnxt_get_max_func_irqs(bp);
15800 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
15801 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
15802 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
15803 			return 0;
15804 
15805 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
15806 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
15807 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
15808 		max_cp = min_t(int, max_cp, max_irq);
15809 		max_cp = min_t(int, max_cp, max_stat);
15810 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
15811 		if (rc)
15812 			rc = 0;
15813 	}
15814 	return rc;
15815 }
15816 
15817 /* In initial default shared ring setting, each shared ring must have a
15818  * RX/TX ring pair.
15819  */
bnxt_trim_dflt_sh_rings(struct bnxt * bp)15820 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
15821 {
15822 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
15823 	bp->rx_nr_rings = bp->cp_nr_rings;
15824 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
15825 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15826 }
15827 
bnxt_set_dflt_rings(struct bnxt * bp,bool sh)15828 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
15829 {
15830 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
15831 	int avail_msix;
15832 
15833 	if (!bnxt_can_reserve_rings(bp))
15834 		return 0;
15835 
15836 	if (sh)
15837 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
15838 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
15839 	/* Reduce default rings on multi-port cards so that total default
15840 	 * rings do not exceed CPU count.
15841 	 */
15842 	if (bp->port_count > 1) {
15843 		int max_rings =
15844 			max_t(int, num_online_cpus() / bp->port_count, 1);
15845 
15846 		dflt_rings = min_t(int, dflt_rings, max_rings);
15847 	}
15848 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
15849 	if (rc)
15850 		return rc;
15851 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
15852 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
15853 	if (sh)
15854 		bnxt_trim_dflt_sh_rings(bp);
15855 	else
15856 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
15857 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15858 
15859 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
15860 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
15861 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
15862 
15863 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
15864 		bnxt_set_dflt_ulp_stat_ctxs(bp);
15865 	}
15866 
15867 	rc = __bnxt_reserve_rings(bp);
15868 	if (rc && rc != -ENODEV)
15869 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
15870 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15871 	if (sh)
15872 		bnxt_trim_dflt_sh_rings(bp);
15873 
15874 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
15875 	if (bnxt_need_reserve_rings(bp)) {
15876 		rc = __bnxt_reserve_rings(bp);
15877 		if (rc && rc != -ENODEV)
15878 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
15879 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15880 	}
15881 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
15882 		bp->rx_nr_rings++;
15883 		bp->cp_nr_rings++;
15884 	}
15885 	if (rc) {
15886 		bp->tx_nr_rings = 0;
15887 		bp->rx_nr_rings = 0;
15888 	}
15889 	return rc;
15890 }
15891 
bnxt_init_dflt_ring_mode(struct bnxt * bp)15892 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
15893 {
15894 	int rc;
15895 
15896 	if (bp->tx_nr_rings)
15897 		return 0;
15898 
15899 	bnxt_ulp_irq_stop(bp);
15900 	bnxt_clear_int_mode(bp);
15901 	rc = bnxt_set_dflt_rings(bp, true);
15902 	if (rc) {
15903 		if (BNXT_VF(bp) && rc == -ENODEV)
15904 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15905 		else
15906 			netdev_err(bp->dev, "Not enough rings available.\n");
15907 		goto init_dflt_ring_err;
15908 	}
15909 	rc = bnxt_init_int_mode(bp);
15910 	if (rc)
15911 		goto init_dflt_ring_err;
15912 
15913 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15914 
15915 	bnxt_set_dflt_rfs(bp);
15916 
15917 init_dflt_ring_err:
15918 	bnxt_ulp_irq_restart(bp, rc);
15919 	return rc;
15920 }
15921 
bnxt_restore_pf_fw_resources(struct bnxt * bp)15922 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
15923 {
15924 	int rc;
15925 
15926 	ASSERT_RTNL();
15927 	bnxt_hwrm_func_qcaps(bp);
15928 
15929 	if (netif_running(bp->dev))
15930 		__bnxt_close_nic(bp, true, false);
15931 
15932 	bnxt_ulp_irq_stop(bp);
15933 	bnxt_clear_int_mode(bp);
15934 	rc = bnxt_init_int_mode(bp);
15935 	bnxt_ulp_irq_restart(bp, rc);
15936 
15937 	if (netif_running(bp->dev)) {
15938 		if (rc)
15939 			dev_close(bp->dev);
15940 		else
15941 			rc = bnxt_open_nic(bp, true, false);
15942 	}
15943 
15944 	return rc;
15945 }
15946 
bnxt_init_mac_addr(struct bnxt * bp)15947 static int bnxt_init_mac_addr(struct bnxt *bp)
15948 {
15949 	int rc = 0;
15950 
15951 	if (BNXT_PF(bp)) {
15952 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
15953 	} else {
15954 #ifdef CONFIG_BNXT_SRIOV
15955 		struct bnxt_vf_info *vf = &bp->vf;
15956 		bool strict_approval = true;
15957 
15958 		if (is_valid_ether_addr(vf->mac_addr)) {
15959 			/* overwrite netdev dev_addr with admin VF MAC */
15960 			eth_hw_addr_set(bp->dev, vf->mac_addr);
15961 			/* Older PF driver or firmware may not approve this
15962 			 * correctly.
15963 			 */
15964 			strict_approval = false;
15965 		} else {
15966 			eth_hw_addr_random(bp->dev);
15967 		}
15968 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
15969 #endif
15970 	}
15971 	return rc;
15972 }
15973 
bnxt_vpd_read_info(struct bnxt * bp)15974 static void bnxt_vpd_read_info(struct bnxt *bp)
15975 {
15976 	struct pci_dev *pdev = bp->pdev;
15977 	unsigned int vpd_size, kw_len;
15978 	int pos, size;
15979 	u8 *vpd_data;
15980 
15981 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
15982 	if (IS_ERR(vpd_data)) {
15983 		pci_warn(pdev, "Unable to read VPD\n");
15984 		return;
15985 	}
15986 
15987 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15988 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
15989 	if (pos < 0)
15990 		goto read_sn;
15991 
15992 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
15993 	memcpy(bp->board_partno, &vpd_data[pos], size);
15994 
15995 read_sn:
15996 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
15997 					   PCI_VPD_RO_KEYWORD_SERIALNO,
15998 					   &kw_len);
15999 	if (pos < 0)
16000 		goto exit;
16001 
16002 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16003 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16004 exit:
16005 	kfree(vpd_data);
16006 }
16007 
bnxt_pcie_dsn_get(struct bnxt * bp,u8 dsn[])16008 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16009 {
16010 	struct pci_dev *pdev = bp->pdev;
16011 	u64 qword;
16012 
16013 	qword = pci_get_dsn(pdev);
16014 	if (!qword) {
16015 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16016 		return -EOPNOTSUPP;
16017 	}
16018 
16019 	put_unaligned_le64(qword, dsn);
16020 
16021 	bp->flags |= BNXT_FLAG_DSN_VALID;
16022 	return 0;
16023 }
16024 
bnxt_map_db_bar(struct bnxt * bp)16025 static int bnxt_map_db_bar(struct bnxt *bp)
16026 {
16027 	if (!bp->db_size)
16028 		return -ENODEV;
16029 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16030 	if (!bp->bar1)
16031 		return -ENOMEM;
16032 	return 0;
16033 }
16034 
bnxt_print_device_info(struct bnxt * bp)16035 void bnxt_print_device_info(struct bnxt *bp)
16036 {
16037 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16038 		    board_info[bp->board_idx].name,
16039 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16040 
16041 	pcie_print_link_status(bp->pdev);
16042 }
16043 
bnxt_init_one(struct pci_dev * pdev,const struct pci_device_id * ent)16044 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16045 {
16046 	struct bnxt_hw_resc *hw_resc;
16047 	struct net_device *dev;
16048 	struct bnxt *bp;
16049 	int rc, max_irqs;
16050 
16051 	if (pci_is_bridge(pdev))
16052 		return -ENODEV;
16053 
16054 	if (!pdev->msix_cap) {
16055 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16056 		return -ENODEV;
16057 	}
16058 
16059 	/* Clear any pending DMA transactions from crash kernel
16060 	 * while loading driver in capture kernel.
16061 	 */
16062 	if (is_kdump_kernel()) {
16063 		pci_clear_master(pdev);
16064 		pcie_flr(pdev);
16065 	}
16066 
16067 	max_irqs = bnxt_get_max_irq(pdev);
16068 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16069 				 max_irqs);
16070 	if (!dev)
16071 		return -ENOMEM;
16072 
16073 	bp = netdev_priv(dev);
16074 	bp->board_idx = ent->driver_data;
16075 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16076 	bnxt_set_max_func_irqs(bp, max_irqs);
16077 
16078 	if (bnxt_vf_pciid(bp->board_idx))
16079 		bp->flags |= BNXT_FLAG_VF;
16080 
16081 	/* No devlink port registration in case of a VF */
16082 	if (BNXT_PF(bp))
16083 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16084 
16085 	rc = bnxt_init_board(pdev, dev);
16086 	if (rc < 0)
16087 		goto init_err_free;
16088 
16089 	dev->netdev_ops = &bnxt_netdev_ops;
16090 	dev->stat_ops = &bnxt_stat_ops;
16091 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16092 	dev->ethtool_ops = &bnxt_ethtool_ops;
16093 	pci_set_drvdata(pdev, dev);
16094 
16095 	rc = bnxt_alloc_hwrm_resources(bp);
16096 	if (rc)
16097 		goto init_err_pci_clean;
16098 
16099 	mutex_init(&bp->hwrm_cmd_lock);
16100 	mutex_init(&bp->link_lock);
16101 
16102 	rc = bnxt_fw_init_one_p1(bp);
16103 	if (rc)
16104 		goto init_err_pci_clean;
16105 
16106 	if (BNXT_PF(bp))
16107 		bnxt_vpd_read_info(bp);
16108 
16109 	if (BNXT_CHIP_P5_PLUS(bp)) {
16110 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16111 		if (BNXT_CHIP_P7(bp))
16112 			bp->flags |= BNXT_FLAG_CHIP_P7;
16113 	}
16114 
16115 	rc = bnxt_alloc_rss_indir_tbl(bp);
16116 	if (rc)
16117 		goto init_err_pci_clean;
16118 
16119 	rc = bnxt_fw_init_one_p2(bp);
16120 	if (rc)
16121 		goto init_err_pci_clean;
16122 
16123 	rc = bnxt_map_db_bar(bp);
16124 	if (rc) {
16125 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16126 			rc);
16127 		goto init_err_pci_clean;
16128 	}
16129 
16130 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16131 			   NETIF_F_TSO | NETIF_F_TSO6 |
16132 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16133 			   NETIF_F_GSO_IPXIP4 |
16134 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16135 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16136 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16137 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16138 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16139 
16140 	if (BNXT_SUPPORTS_TPA(bp))
16141 		dev->hw_features |= NETIF_F_LRO;
16142 
16143 	dev->hw_enc_features =
16144 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16145 			NETIF_F_TSO | NETIF_F_TSO6 |
16146 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16147 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16148 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16149 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16150 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16151 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16152 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16153 	else
16154 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16155 
16156 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16157 				    NETIF_F_GSO_GRE_CSUM;
16158 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16159 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16160 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16161 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16162 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16163 	if (BNXT_SUPPORTS_TPA(bp))
16164 		dev->hw_features |= NETIF_F_GRO_HW;
16165 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16166 	if (dev->features & NETIF_F_GRO_HW)
16167 		dev->features &= ~NETIF_F_LRO;
16168 	dev->priv_flags |= IFF_UNICAST_FLT;
16169 
16170 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16171 	if (bp->tso_max_segs)
16172 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16173 
16174 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16175 			    NETDEV_XDP_ACT_RX_SG;
16176 
16177 #ifdef CONFIG_BNXT_SRIOV
16178 	init_waitqueue_head(&bp->sriov_cfg_wait);
16179 #endif
16180 	if (BNXT_SUPPORTS_TPA(bp)) {
16181 		bp->gro_func = bnxt_gro_func_5730x;
16182 		if (BNXT_CHIP_P4(bp))
16183 			bp->gro_func = bnxt_gro_func_5731x;
16184 		else if (BNXT_CHIP_P5_PLUS(bp))
16185 			bp->gro_func = bnxt_gro_func_5750x;
16186 	}
16187 	if (!BNXT_CHIP_P4_PLUS(bp))
16188 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16189 
16190 	rc = bnxt_init_mac_addr(bp);
16191 	if (rc) {
16192 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16193 		rc = -EADDRNOTAVAIL;
16194 		goto init_err_pci_clean;
16195 	}
16196 
16197 	if (BNXT_PF(bp)) {
16198 		/* Read the adapter's DSN to use as the eswitch switch_id */
16199 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16200 	}
16201 
16202 	/* MTU range: 60 - FW defined max */
16203 	dev->min_mtu = ETH_ZLEN;
16204 	dev->max_mtu = bp->max_mtu;
16205 
16206 	rc = bnxt_probe_phy(bp, true);
16207 	if (rc)
16208 		goto init_err_pci_clean;
16209 
16210 	hw_resc = &bp->hw_resc;
16211 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16212 		       BNXT_L2_FLTR_MAX_FLTR;
16213 	/* Older firmware may not report these filters properly */
16214 	if (bp->max_fltr < BNXT_MAX_FLTR)
16215 		bp->max_fltr = BNXT_MAX_FLTR;
16216 	bnxt_init_l2_fltr_tbl(bp);
16217 	bnxt_set_rx_skb_mode(bp, false);
16218 	bnxt_set_tpa_flags(bp);
16219 	bnxt_set_ring_params(bp);
16220 	bnxt_rdma_aux_device_init(bp);
16221 	rc = bnxt_set_dflt_rings(bp, true);
16222 	if (rc) {
16223 		if (BNXT_VF(bp) && rc == -ENODEV) {
16224 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16225 		} else {
16226 			netdev_err(bp->dev, "Not enough rings available.\n");
16227 			rc = -ENOMEM;
16228 		}
16229 		goto init_err_pci_clean;
16230 	}
16231 
16232 	bnxt_fw_init_one_p3(bp);
16233 
16234 	bnxt_init_dflt_coal(bp);
16235 
16236 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16237 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16238 
16239 	rc = bnxt_init_int_mode(bp);
16240 	if (rc)
16241 		goto init_err_pci_clean;
16242 
16243 	/* No TC has been set yet and rings may have been trimmed due to
16244 	 * limited MSIX, so we re-initialize the TX rings per TC.
16245 	 */
16246 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16247 
16248 	if (BNXT_PF(bp)) {
16249 		if (!bnxt_pf_wq) {
16250 			bnxt_pf_wq =
16251 				create_singlethread_workqueue("bnxt_pf_wq");
16252 			if (!bnxt_pf_wq) {
16253 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16254 				rc = -ENOMEM;
16255 				goto init_err_pci_clean;
16256 			}
16257 		}
16258 		rc = bnxt_init_tc(bp);
16259 		if (rc)
16260 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16261 				   rc);
16262 	}
16263 
16264 	bnxt_inv_fw_health_reg(bp);
16265 	rc = bnxt_dl_register(bp);
16266 	if (rc)
16267 		goto init_err_dl;
16268 
16269 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16270 
16271 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16272 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16273 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16274 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16275 
16276 	rc = register_netdev(dev);
16277 	if (rc)
16278 		goto init_err_cleanup;
16279 
16280 	bnxt_dl_fw_reporters_create(bp);
16281 
16282 	bnxt_rdma_aux_device_add(bp);
16283 
16284 	bnxt_print_device_info(bp);
16285 
16286 	pci_save_state(pdev);
16287 
16288 	return 0;
16289 init_err_cleanup:
16290 	bnxt_rdma_aux_device_uninit(bp);
16291 	bnxt_dl_unregister(bp);
16292 init_err_dl:
16293 	bnxt_shutdown_tc(bp);
16294 	bnxt_clear_int_mode(bp);
16295 
16296 init_err_pci_clean:
16297 	bnxt_hwrm_func_drv_unrgtr(bp);
16298 	bnxt_free_hwrm_resources(bp);
16299 	bnxt_hwmon_uninit(bp);
16300 	bnxt_ethtool_free(bp);
16301 	bnxt_ptp_clear(bp);
16302 	kfree(bp->ptp_cfg);
16303 	bp->ptp_cfg = NULL;
16304 	kfree(bp->fw_health);
16305 	bp->fw_health = NULL;
16306 	bnxt_cleanup_pci(bp);
16307 	bnxt_free_ctx_mem(bp, true);
16308 	bnxt_free_crash_dump_mem(bp);
16309 	kfree(bp->rss_indir_tbl);
16310 	bp->rss_indir_tbl = NULL;
16311 
16312 init_err_free:
16313 	free_netdev(dev);
16314 	return rc;
16315 }
16316 
bnxt_shutdown(struct pci_dev * pdev)16317 static void bnxt_shutdown(struct pci_dev *pdev)
16318 {
16319 	struct net_device *dev = pci_get_drvdata(pdev);
16320 	struct bnxt *bp;
16321 
16322 	if (!dev)
16323 		return;
16324 
16325 	rtnl_lock();
16326 	bp = netdev_priv(dev);
16327 	if (!bp)
16328 		goto shutdown_exit;
16329 
16330 	if (netif_running(dev))
16331 		dev_close(dev);
16332 
16333 	bnxt_ptp_clear(bp);
16334 	bnxt_clear_int_mode(bp);
16335 	pci_disable_device(pdev);
16336 
16337 	if (system_state == SYSTEM_POWER_OFF) {
16338 		pci_wake_from_d3(pdev, bp->wol);
16339 		pci_set_power_state(pdev, PCI_D3hot);
16340 	}
16341 
16342 shutdown_exit:
16343 	rtnl_unlock();
16344 }
16345 
16346 #ifdef CONFIG_PM_SLEEP
bnxt_suspend(struct device * device)16347 static int bnxt_suspend(struct device *device)
16348 {
16349 	struct net_device *dev = dev_get_drvdata(device);
16350 	struct bnxt *bp = netdev_priv(dev);
16351 	int rc = 0;
16352 
16353 	bnxt_ulp_stop(bp);
16354 
16355 	rtnl_lock();
16356 	if (netif_running(dev)) {
16357 		netif_device_detach(dev);
16358 		rc = bnxt_close(dev);
16359 	}
16360 	bnxt_hwrm_func_drv_unrgtr(bp);
16361 	bnxt_ptp_clear(bp);
16362 	pci_disable_device(bp->pdev);
16363 	bnxt_free_ctx_mem(bp, false);
16364 	rtnl_unlock();
16365 	return rc;
16366 }
16367 
bnxt_resume(struct device * device)16368 static int bnxt_resume(struct device *device)
16369 {
16370 	struct net_device *dev = dev_get_drvdata(device);
16371 	struct bnxt *bp = netdev_priv(dev);
16372 	int rc = 0;
16373 
16374 	rtnl_lock();
16375 	rc = pci_enable_device(bp->pdev);
16376 	if (rc) {
16377 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16378 			   rc);
16379 		goto resume_exit;
16380 	}
16381 	pci_set_master(bp->pdev);
16382 	if (bnxt_hwrm_ver_get(bp)) {
16383 		rc = -ENODEV;
16384 		goto resume_exit;
16385 	}
16386 	rc = bnxt_hwrm_func_reset(bp);
16387 	if (rc) {
16388 		rc = -EBUSY;
16389 		goto resume_exit;
16390 	}
16391 
16392 	rc = bnxt_hwrm_func_qcaps(bp);
16393 	if (rc)
16394 		goto resume_exit;
16395 
16396 	bnxt_clear_reservations(bp, true);
16397 
16398 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16399 		rc = -ENODEV;
16400 		goto resume_exit;
16401 	}
16402 	if (bp->fw_crash_mem)
16403 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16404 
16405 	if (bnxt_ptp_init(bp)) {
16406 		kfree(bp->ptp_cfg);
16407 		bp->ptp_cfg = NULL;
16408 	}
16409 	bnxt_get_wol_settings(bp);
16410 	if (netif_running(dev)) {
16411 		rc = bnxt_open(dev);
16412 		if (!rc)
16413 			netif_device_attach(dev);
16414 	}
16415 
16416 resume_exit:
16417 	rtnl_unlock();
16418 	bnxt_ulp_start(bp, rc);
16419 	if (!rc)
16420 		bnxt_reenable_sriov(bp);
16421 	return rc;
16422 }
16423 
16424 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16425 #define BNXT_PM_OPS (&bnxt_pm_ops)
16426 
16427 #else
16428 
16429 #define BNXT_PM_OPS NULL
16430 
16431 #endif /* CONFIG_PM_SLEEP */
16432 
16433 /**
16434  * bnxt_io_error_detected - called when PCI error is detected
16435  * @pdev: Pointer to PCI device
16436  * @state: The current pci connection state
16437  *
16438  * This function is called after a PCI bus error affecting
16439  * this device has been detected.
16440  */
bnxt_io_error_detected(struct pci_dev * pdev,pci_channel_state_t state)16441 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16442 					       pci_channel_state_t state)
16443 {
16444 	struct net_device *netdev = pci_get_drvdata(pdev);
16445 	struct bnxt *bp = netdev_priv(netdev);
16446 	bool abort = false;
16447 
16448 	netdev_info(netdev, "PCI I/O error detected\n");
16449 
16450 	bnxt_ulp_stop(bp);
16451 
16452 	rtnl_lock();
16453 	netif_device_detach(netdev);
16454 
16455 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16456 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16457 		abort = true;
16458 	}
16459 
16460 	if (abort || state == pci_channel_io_perm_failure) {
16461 		rtnl_unlock();
16462 		return PCI_ERS_RESULT_DISCONNECT;
16463 	}
16464 
16465 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16466 	 * so we disable bus master to prevent any potential bad DMAs before
16467 	 * freeing kernel memory.
16468 	 */
16469 	if (state == pci_channel_io_frozen) {
16470 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16471 		bnxt_fw_fatal_close(bp);
16472 	}
16473 
16474 	if (netif_running(netdev))
16475 		__bnxt_close_nic(bp, true, true);
16476 
16477 	if (pci_is_enabled(pdev))
16478 		pci_disable_device(pdev);
16479 	bnxt_free_ctx_mem(bp, false);
16480 	rtnl_unlock();
16481 
16482 	/* Request a slot slot reset. */
16483 	return PCI_ERS_RESULT_NEED_RESET;
16484 }
16485 
16486 /**
16487  * bnxt_io_slot_reset - called after the pci bus has been reset.
16488  * @pdev: Pointer to PCI device
16489  *
16490  * Restart the card from scratch, as if from a cold-boot.
16491  * At this point, the card has experienced a hard reset,
16492  * followed by fixups by BIOS, and has its config space
16493  * set up identically to what it was at cold boot.
16494  */
bnxt_io_slot_reset(struct pci_dev * pdev)16495 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
16496 {
16497 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
16498 	struct net_device *netdev = pci_get_drvdata(pdev);
16499 	struct bnxt *bp = netdev_priv(netdev);
16500 	int retry = 0;
16501 	int err = 0;
16502 	int off;
16503 
16504 	netdev_info(bp->dev, "PCI Slot Reset\n");
16505 
16506 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16507 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
16508 		msleep(900);
16509 
16510 	rtnl_lock();
16511 
16512 	if (pci_enable_device(pdev)) {
16513 		dev_err(&pdev->dev,
16514 			"Cannot re-enable PCI device after reset.\n");
16515 	} else {
16516 		pci_set_master(pdev);
16517 		/* Upon fatal error, our device internal logic that latches to
16518 		 * BAR value is getting reset and will restore only upon
16519 		 * rewriting the BARs.
16520 		 *
16521 		 * As pci_restore_state() does not re-write the BARs if the
16522 		 * value is same as saved value earlier, driver needs to
16523 		 * write the BARs to 0 to force restore, in case of fatal error.
16524 		 */
16525 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
16526 				       &bp->state)) {
16527 			for (off = PCI_BASE_ADDRESS_0;
16528 			     off <= PCI_BASE_ADDRESS_5; off += 4)
16529 				pci_write_config_dword(bp->pdev, off, 0);
16530 		}
16531 		pci_restore_state(pdev);
16532 		pci_save_state(pdev);
16533 
16534 		bnxt_inv_fw_health_reg(bp);
16535 		bnxt_try_map_fw_health_reg(bp);
16536 
16537 		/* In some PCIe AER scenarios, firmware may take up to
16538 		 * 10 seconds to become ready in the worst case.
16539 		 */
16540 		do {
16541 			err = bnxt_try_recover_fw(bp);
16542 			if (!err)
16543 				break;
16544 			retry++;
16545 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
16546 
16547 		if (err) {
16548 			dev_err(&pdev->dev, "Firmware not ready\n");
16549 			goto reset_exit;
16550 		}
16551 
16552 		err = bnxt_hwrm_func_reset(bp);
16553 		if (!err)
16554 			result = PCI_ERS_RESULT_RECOVERED;
16555 
16556 		bnxt_ulp_irq_stop(bp);
16557 		bnxt_clear_int_mode(bp);
16558 		err = bnxt_init_int_mode(bp);
16559 		bnxt_ulp_irq_restart(bp, err);
16560 	}
16561 
16562 reset_exit:
16563 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16564 	bnxt_clear_reservations(bp, true);
16565 	rtnl_unlock();
16566 
16567 	return result;
16568 }
16569 
16570 /**
16571  * bnxt_io_resume - called when traffic can start flowing again.
16572  * @pdev: Pointer to PCI device
16573  *
16574  * This callback is called when the error recovery driver tells
16575  * us that its OK to resume normal operation.
16576  */
bnxt_io_resume(struct pci_dev * pdev)16577 static void bnxt_io_resume(struct pci_dev *pdev)
16578 {
16579 	struct net_device *netdev = pci_get_drvdata(pdev);
16580 	struct bnxt *bp = netdev_priv(netdev);
16581 	int err;
16582 
16583 	netdev_info(bp->dev, "PCI Slot Resume\n");
16584 	rtnl_lock();
16585 
16586 	err = bnxt_hwrm_func_qcaps(bp);
16587 	if (!err) {
16588 		if (netif_running(netdev))
16589 			err = bnxt_open(netdev);
16590 		else
16591 			err = bnxt_reserve_rings(bp, true);
16592 	}
16593 
16594 	if (!err)
16595 		netif_device_attach(netdev);
16596 
16597 	rtnl_unlock();
16598 	bnxt_ulp_start(bp, err);
16599 	if (!err)
16600 		bnxt_reenable_sriov(bp);
16601 }
16602 
16603 static const struct pci_error_handlers bnxt_err_handler = {
16604 	.error_detected	= bnxt_io_error_detected,
16605 	.slot_reset	= bnxt_io_slot_reset,
16606 	.resume		= bnxt_io_resume
16607 };
16608 
16609 static struct pci_driver bnxt_pci_driver = {
16610 	.name		= DRV_MODULE_NAME,
16611 	.id_table	= bnxt_pci_tbl,
16612 	.probe		= bnxt_init_one,
16613 	.remove		= bnxt_remove_one,
16614 	.shutdown	= bnxt_shutdown,
16615 	.driver.pm	= BNXT_PM_OPS,
16616 	.err_handler	= &bnxt_err_handler,
16617 #if defined(CONFIG_BNXT_SRIOV)
16618 	.sriov_configure = bnxt_sriov_configure,
16619 #endif
16620 };
16621 
bnxt_init(void)16622 static int __init bnxt_init(void)
16623 {
16624 	int err;
16625 
16626 	bnxt_debug_init();
16627 	err = pci_register_driver(&bnxt_pci_driver);
16628 	if (err) {
16629 		bnxt_debug_exit();
16630 		return err;
16631 	}
16632 
16633 	return 0;
16634 }
16635 
bnxt_exit(void)16636 static void __exit bnxt_exit(void)
16637 {
16638 	pci_unregister_driver(&bnxt_pci_driver);
16639 	if (bnxt_pf_wq)
16640 		destroy_workqueue(bnxt_pf_wq);
16641 	bnxt_debug_exit();
16642 }
16643 
16644 module_init(bnxt_init);
16645 module_exit(bnxt_exit);
16646