• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ip.h>
41 #include <net/tcp.h>
42 #include <net/udp.h>
43 #include <net/checksum.h>
44 #include <net/ip6_checksum.h>
45 #include <net/udp_tunnel.h>
46 #include <linux/workqueue.h>
47 #include <linux/prefetch.h>
48 #include <linux/cache.h>
49 #include <linux/log2.h>
50 #include <linux/aer.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 <linux/hwmon.h>
56 #include <linux/hwmon-sysfs.h>
57 #include <net/page_pool.h>
58 
59 #include "bnxt_hsi.h"
60 #include "bnxt.h"
61 #include "bnxt_ulp.h"
62 #include "bnxt_sriov.h"
63 #include "bnxt_ethtool.h"
64 #include "bnxt_dcb.h"
65 #include "bnxt_xdp.h"
66 #include "bnxt_vfr.h"
67 #include "bnxt_tc.h"
68 #include "bnxt_devlink.h"
69 #include "bnxt_debugfs.h"
70 
71 #define BNXT_TX_TIMEOUT		(5 * HZ)
72 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
73 				 NETIF_MSG_TX_ERR)
74 
75 MODULE_LICENSE("GPL");
76 MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
77 
78 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
79 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
80 #define BNXT_RX_COPY_THRESH 256
81 
82 #define BNXT_TX_PUSH_THRESH 164
83 
84 enum board_idx {
85 	BCM57301,
86 	BCM57302,
87 	BCM57304,
88 	BCM57417_NPAR,
89 	BCM58700,
90 	BCM57311,
91 	BCM57312,
92 	BCM57402,
93 	BCM57404,
94 	BCM57406,
95 	BCM57402_NPAR,
96 	BCM57407,
97 	BCM57412,
98 	BCM57414,
99 	BCM57416,
100 	BCM57417,
101 	BCM57412_NPAR,
102 	BCM57314,
103 	BCM57417_SFP,
104 	BCM57416_SFP,
105 	BCM57404_NPAR,
106 	BCM57406_NPAR,
107 	BCM57407_SFP,
108 	BCM57407_NPAR,
109 	BCM57414_NPAR,
110 	BCM57416_NPAR,
111 	BCM57452,
112 	BCM57454,
113 	BCM5745x_NPAR,
114 	BCM57508,
115 	BCM57504,
116 	BCM57502,
117 	BCM57508_NPAR,
118 	BCM57504_NPAR,
119 	BCM57502_NPAR,
120 	BCM58802,
121 	BCM58804,
122 	BCM58808,
123 	NETXTREME_E_VF,
124 	NETXTREME_C_VF,
125 	NETXTREME_S_VF,
126 	NETXTREME_C_VF_HV,
127 	NETXTREME_E_VF_HV,
128 	NETXTREME_E_P5_VF,
129 	NETXTREME_E_P5_VF_HV,
130 };
131 
132 /* indexed by enum above */
133 static const struct {
134 	char *name;
135 } board_info[] = {
136 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
137 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
138 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
139 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
140 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
141 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
142 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
143 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
144 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
145 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
146 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
147 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
148 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
149 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
150 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
151 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
152 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
153 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
154 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
155 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
156 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
157 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
158 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
159 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
160 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
161 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
162 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
163 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
164 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
165 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
166 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
167 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
168 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
169 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
170 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
171 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
172 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
173 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
174 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
175 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
176 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
177 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
178 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
179 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
180 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
181 };
182 
183 static const struct pci_device_id bnxt_pci_tbl[] = {
184 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
185 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
186 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
187 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
188 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
189 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
190 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
191 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
193 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
194 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
195 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
196 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
197 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
198 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
199 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
200 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
201 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
202 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
203 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
204 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
205 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
206 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
207 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
208 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
209 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
210 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
211 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
212 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
213 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
214 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
215 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
216 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
217 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
218 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
219 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
220 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
221 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
222 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57508_NPAR },
223 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
224 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57502_NPAR },
225 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57508_NPAR },
226 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
227 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57502_NPAR },
228 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
229 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
230 #ifdef CONFIG_BNXT_SRIOV
231 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
232 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
233 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
234 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
235 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
236 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
237 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
238 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
239 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
240 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
241 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
242 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
243 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
244 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
245 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
246 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
247 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
248 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
249 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
250 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
251 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
252 #endif
253 	{ 0 }
254 };
255 
256 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
257 
258 static const u16 bnxt_vf_req_snif[] = {
259 	HWRM_FUNC_CFG,
260 	HWRM_FUNC_VF_CFG,
261 	HWRM_PORT_PHY_QCFG,
262 	HWRM_CFA_L2_FILTER_ALLOC,
263 };
264 
265 static const u16 bnxt_async_events_arr[] = {
266 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
267 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
268 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
269 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
270 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
271 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
272 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
273 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
274 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
275 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
276 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
277 };
278 
279 static struct workqueue_struct *bnxt_pf_wq;
280 
bnxt_vf_pciid(enum board_idx idx)281 static bool bnxt_vf_pciid(enum board_idx idx)
282 {
283 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
284 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
285 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
286 		idx == NETXTREME_E_P5_VF_HV);
287 }
288 
289 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
290 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
291 #define DB_CP_IRQ_DIS_FLAGS	(DB_KEY_CP | DB_IRQ_DIS)
292 
293 #define BNXT_CP_DB_IRQ_DIS(db)						\
294 		writel(DB_CP_IRQ_DIS_FLAGS, db)
295 
296 #define BNXT_DB_CQ(db, idx)						\
297 	writel(DB_CP_FLAGS | RING_CMP(idx), (db)->doorbell)
298 
299 #define BNXT_DB_NQ_P5(db, idx)						\
300 	writeq((db)->db_key64 | DBR_TYPE_NQ | RING_CMP(idx), (db)->doorbell)
301 
302 #define BNXT_DB_CQ_ARM(db, idx)						\
303 	writel(DB_CP_REARM_FLAGS | RING_CMP(idx), (db)->doorbell)
304 
305 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
306 	writeq((db)->db_key64 | DBR_TYPE_NQ_ARM | RING_CMP(idx), (db)->doorbell)
307 
bnxt_db_nq(struct bnxt * bp,struct bnxt_db_info * db,u32 idx)308 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
309 {
310 	if (bp->flags & BNXT_FLAG_CHIP_P5)
311 		BNXT_DB_NQ_P5(db, idx);
312 	else
313 		BNXT_DB_CQ(db, idx);
314 }
315 
bnxt_db_nq_arm(struct bnxt * bp,struct bnxt_db_info * db,u32 idx)316 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
317 {
318 	if (bp->flags & BNXT_FLAG_CHIP_P5)
319 		BNXT_DB_NQ_ARM_P5(db, idx);
320 	else
321 		BNXT_DB_CQ_ARM(db, idx);
322 }
323 
bnxt_db_cq(struct bnxt * bp,struct bnxt_db_info * db,u32 idx)324 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
325 {
326 	if (bp->flags & BNXT_FLAG_CHIP_P5)
327 		writeq(db->db_key64 | DBR_TYPE_CQ_ARMALL | RING_CMP(idx),
328 		       db->doorbell);
329 	else
330 		BNXT_DB_CQ(db, idx);
331 }
332 
333 const u16 bnxt_lhint_arr[] = {
334 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
335 	TX_BD_FLAGS_LHINT_512_TO_1023,
336 	TX_BD_FLAGS_LHINT_1024_TO_2047,
337 	TX_BD_FLAGS_LHINT_1024_TO_2047,
338 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
339 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
340 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
341 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
342 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
343 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
344 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
345 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
346 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
347 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
348 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
349 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
350 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
351 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
352 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
353 };
354 
bnxt_xmit_get_cfa_action(struct sk_buff * skb)355 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
356 {
357 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
358 
359 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
360 		return 0;
361 
362 	return md_dst->u.port_info.port_id;
363 }
364 
bnxt_txr_db_kick(struct bnxt * bp,struct bnxt_tx_ring_info * txr,u16 prod)365 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
366 			     u16 prod)
367 {
368 	bnxt_db_write(bp, &txr->tx_db, prod);
369 	txr->kick_pending = 0;
370 }
371 
bnxt_txr_netif_try_stop_queue(struct bnxt * bp,struct bnxt_tx_ring_info * txr,struct netdev_queue * txq)372 static bool bnxt_txr_netif_try_stop_queue(struct bnxt *bp,
373 					  struct bnxt_tx_ring_info *txr,
374 					  struct netdev_queue *txq)
375 {
376 	netif_tx_stop_queue(txq);
377 
378 	/* netif_tx_stop_queue() must be done before checking
379 	 * tx index in bnxt_tx_avail() below, because in
380 	 * bnxt_tx_int(), we update tx index before checking for
381 	 * netif_tx_queue_stopped().
382 	 */
383 	smp_mb();
384 	if (bnxt_tx_avail(bp, txr) >= bp->tx_wake_thresh) {
385 		netif_tx_wake_queue(txq);
386 		return false;
387 	}
388 
389 	return true;
390 }
391 
bnxt_start_xmit(struct sk_buff * skb,struct net_device * dev)392 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
393 {
394 	struct bnxt *bp = netdev_priv(dev);
395 	struct tx_bd *txbd;
396 	struct tx_bd_ext *txbd1;
397 	struct netdev_queue *txq;
398 	int i;
399 	dma_addr_t mapping;
400 	unsigned int length, pad = 0;
401 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
402 	u16 prod, last_frag;
403 	struct pci_dev *pdev = bp->pdev;
404 	struct bnxt_tx_ring_info *txr;
405 	struct bnxt_sw_tx_bd *tx_buf;
406 
407 	i = skb_get_queue_mapping(skb);
408 	if (unlikely(i >= bp->tx_nr_rings)) {
409 		dev_kfree_skb_any(skb);
410 		atomic_long_inc(&dev->tx_dropped);
411 		return NETDEV_TX_OK;
412 	}
413 
414 	txq = netdev_get_tx_queue(dev, i);
415 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
416 	prod = txr->tx_prod;
417 
418 	free_size = bnxt_tx_avail(bp, txr);
419 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
420 		/* We must have raced with NAPI cleanup */
421 		if (net_ratelimit() && txr->kick_pending)
422 			netif_warn(bp, tx_err, dev,
423 				   "bnxt: ring busy w/ flush pending!\n");
424 		if (bnxt_txr_netif_try_stop_queue(bp, txr, txq))
425 			return NETDEV_TX_BUSY;
426 	}
427 
428 	length = skb->len;
429 	len = skb_headlen(skb);
430 	last_frag = skb_shinfo(skb)->nr_frags;
431 
432 	txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
433 
434 	txbd->tx_bd_opaque = prod;
435 
436 	tx_buf = &txr->tx_buf_ring[prod];
437 	tx_buf->skb = skb;
438 	tx_buf->nr_frags = last_frag;
439 
440 	vlan_tag_flags = 0;
441 	cfa_action = bnxt_xmit_get_cfa_action(skb);
442 	if (skb_vlan_tag_present(skb)) {
443 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
444 				 skb_vlan_tag_get(skb);
445 		/* Currently supports 8021Q, 8021AD vlan offloads
446 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
447 		 */
448 		if (skb->vlan_proto == htons(ETH_P_8021Q))
449 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
450 	}
451 
452 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
453 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
454 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
455 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
456 		void __iomem *db = txr->tx_db.doorbell;
457 		void *pdata = tx_push_buf->data;
458 		u64 *end;
459 		int j, push_len;
460 
461 		/* Set COAL_NOW to be ready quickly for the next push */
462 		tx_push->tx_bd_len_flags_type =
463 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
464 					TX_BD_TYPE_LONG_TX_BD |
465 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
466 					TX_BD_FLAGS_COAL_NOW |
467 					TX_BD_FLAGS_PACKET_END |
468 					(2 << TX_BD_FLAGS_BD_CNT_SHIFT));
469 
470 		if (skb->ip_summed == CHECKSUM_PARTIAL)
471 			tx_push1->tx_bd_hsize_lflags =
472 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
473 		else
474 			tx_push1->tx_bd_hsize_lflags = 0;
475 
476 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
477 		tx_push1->tx_bd_cfa_action =
478 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
479 
480 		end = pdata + length;
481 		end = PTR_ALIGN(end, 8) - 1;
482 		*end = 0;
483 
484 		skb_copy_from_linear_data(skb, pdata, len);
485 		pdata += len;
486 		for (j = 0; j < last_frag; j++) {
487 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
488 			void *fptr;
489 
490 			fptr = skb_frag_address_safe(frag);
491 			if (!fptr)
492 				goto normal_tx;
493 
494 			memcpy(pdata, fptr, skb_frag_size(frag));
495 			pdata += skb_frag_size(frag);
496 		}
497 
498 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
499 		txbd->tx_bd_haddr = txr->data_mapping;
500 		prod = NEXT_TX(prod);
501 		txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
502 		memcpy(txbd, tx_push1, sizeof(*txbd));
503 		prod = NEXT_TX(prod);
504 		tx_push->doorbell =
505 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
506 		txr->tx_prod = prod;
507 
508 		tx_buf->is_push = 1;
509 		netdev_tx_sent_queue(txq, skb->len);
510 		wmb();	/* Sync is_push and byte queue before pushing data */
511 
512 		push_len = (length + sizeof(*tx_push) + 7) / 8;
513 		if (push_len > 16) {
514 			__iowrite64_copy(db, tx_push_buf, 16);
515 			__iowrite32_copy(db + 4, tx_push_buf + 1,
516 					 (push_len - 16) << 1);
517 		} else {
518 			__iowrite64_copy(db, tx_push_buf, push_len);
519 		}
520 
521 		goto tx_done;
522 	}
523 
524 normal_tx:
525 	if (length < BNXT_MIN_PKT_SIZE) {
526 		pad = BNXT_MIN_PKT_SIZE - length;
527 		if (skb_pad(skb, pad))
528 			/* SKB already freed. */
529 			goto tx_kick_pending;
530 		length = BNXT_MIN_PKT_SIZE;
531 	}
532 
533 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
534 
535 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
536 		goto tx_free;
537 
538 	dma_unmap_addr_set(tx_buf, mapping, mapping);
539 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
540 		((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
541 
542 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
543 
544 	prod = NEXT_TX(prod);
545 	txbd1 = (struct tx_bd_ext *)
546 		&txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
547 
548 	txbd1->tx_bd_hsize_lflags = 0;
549 	if (skb_is_gso(skb)) {
550 		u32 hdr_len;
551 
552 		if (skb->encapsulation)
553 			hdr_len = skb_inner_network_offset(skb) +
554 				skb_inner_network_header_len(skb) +
555 				inner_tcp_hdrlen(skb);
556 		else
557 			hdr_len = skb_transport_offset(skb) +
558 				tcp_hdrlen(skb);
559 
560 		txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
561 					TX_BD_FLAGS_T_IPID |
562 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
563 		length = skb_shinfo(skb)->gso_size;
564 		txbd1->tx_bd_mss = cpu_to_le32(length);
565 		length += hdr_len;
566 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
567 		txbd1->tx_bd_hsize_lflags =
568 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
569 		txbd1->tx_bd_mss = 0;
570 	}
571 
572 	length >>= 9;
573 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
574 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
575 				     skb->len);
576 		i = 0;
577 		goto tx_dma_error;
578 	}
579 	flags |= bnxt_lhint_arr[length];
580 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
581 
582 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
583 	txbd1->tx_bd_cfa_action =
584 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
585 	for (i = 0; i < last_frag; i++) {
586 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
587 
588 		prod = NEXT_TX(prod);
589 		txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
590 
591 		len = skb_frag_size(frag);
592 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
593 					   DMA_TO_DEVICE);
594 
595 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
596 			goto tx_dma_error;
597 
598 		tx_buf = &txr->tx_buf_ring[prod];
599 		dma_unmap_addr_set(tx_buf, mapping, mapping);
600 
601 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
602 
603 		flags = len << TX_BD_LEN_SHIFT;
604 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
605 	}
606 
607 	flags &= ~TX_BD_LEN;
608 	txbd->tx_bd_len_flags_type =
609 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
610 			    TX_BD_FLAGS_PACKET_END);
611 
612 	netdev_tx_sent_queue(txq, skb->len);
613 
614 	/* Sync BD data before updating doorbell */
615 	wmb();
616 
617 	prod = NEXT_TX(prod);
618 	txr->tx_prod = prod;
619 
620 	if (!netdev_xmit_more() || netif_xmit_stopped(txq))
621 		bnxt_txr_db_kick(bp, txr, prod);
622 	else
623 		txr->kick_pending = 1;
624 
625 tx_done:
626 
627 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
628 		if (netdev_xmit_more() && !tx_buf->is_push)
629 			bnxt_txr_db_kick(bp, txr, prod);
630 
631 		bnxt_txr_netif_try_stop_queue(bp, txr, txq);
632 	}
633 	return NETDEV_TX_OK;
634 
635 tx_dma_error:
636 	last_frag = i;
637 
638 	/* start back at beginning and unmap skb */
639 	prod = txr->tx_prod;
640 	tx_buf = &txr->tx_buf_ring[prod];
641 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
642 			 skb_headlen(skb), PCI_DMA_TODEVICE);
643 	prod = NEXT_TX(prod);
644 
645 	/* unmap remaining mapped pages */
646 	for (i = 0; i < last_frag; i++) {
647 		prod = NEXT_TX(prod);
648 		tx_buf = &txr->tx_buf_ring[prod];
649 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
650 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
651 			       PCI_DMA_TODEVICE);
652 	}
653 
654 tx_free:
655 	dev_kfree_skb_any(skb);
656 tx_kick_pending:
657 	if (txr->kick_pending)
658 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
659 	txr->tx_buf_ring[txr->tx_prod].skb = NULL;
660 	atomic_long_inc(&dev->tx_dropped);
661 	return NETDEV_TX_OK;
662 }
663 
bnxt_tx_int(struct bnxt * bp,struct bnxt_napi * bnapi,int nr_pkts)664 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
665 {
666 	struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
667 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
668 	u16 cons = txr->tx_cons;
669 	struct pci_dev *pdev = bp->pdev;
670 	int i;
671 	unsigned int tx_bytes = 0;
672 
673 	for (i = 0; i < nr_pkts; i++) {
674 		struct bnxt_sw_tx_bd *tx_buf;
675 		struct sk_buff *skb;
676 		int j, last;
677 
678 		tx_buf = &txr->tx_buf_ring[cons];
679 		cons = NEXT_TX(cons);
680 		skb = tx_buf->skb;
681 		tx_buf->skb = NULL;
682 
683 		if (tx_buf->is_push) {
684 			tx_buf->is_push = 0;
685 			goto next_tx_int;
686 		}
687 
688 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
689 				 skb_headlen(skb), PCI_DMA_TODEVICE);
690 		last = tx_buf->nr_frags;
691 
692 		for (j = 0; j < last; j++) {
693 			cons = NEXT_TX(cons);
694 			tx_buf = &txr->tx_buf_ring[cons];
695 			dma_unmap_page(
696 				&pdev->dev,
697 				dma_unmap_addr(tx_buf, mapping),
698 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
699 				PCI_DMA_TODEVICE);
700 		}
701 
702 next_tx_int:
703 		cons = NEXT_TX(cons);
704 
705 		tx_bytes += skb->len;
706 		dev_kfree_skb_any(skb);
707 	}
708 
709 	netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
710 	txr->tx_cons = cons;
711 
712 	/* Need to make the tx_cons update visible to bnxt_start_xmit()
713 	 * before checking for netif_tx_queue_stopped().  Without the
714 	 * memory barrier, there is a small possibility that bnxt_start_xmit()
715 	 * will miss it and cause the queue to be stopped forever.
716 	 */
717 	smp_mb();
718 
719 	if (unlikely(netif_tx_queue_stopped(txq)) &&
720 	    bnxt_tx_avail(bp, txr) >= bp->tx_wake_thresh &&
721 	    READ_ONCE(txr->dev_state) != BNXT_DEV_STATE_CLOSING)
722 		netif_tx_wake_queue(txq);
723 }
724 
__bnxt_alloc_rx_page(struct bnxt * bp,dma_addr_t * mapping,struct bnxt_rx_ring_info * rxr,gfp_t gfp)725 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
726 					 struct bnxt_rx_ring_info *rxr,
727 					 gfp_t gfp)
728 {
729 	struct device *dev = &bp->pdev->dev;
730 	struct page *page;
731 
732 	page = page_pool_dev_alloc_pages(rxr->page_pool);
733 	if (!page)
734 		return NULL;
735 
736 	*mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
737 				      DMA_ATTR_WEAK_ORDERING);
738 	if (dma_mapping_error(dev, *mapping)) {
739 		page_pool_recycle_direct(rxr->page_pool, page);
740 		return NULL;
741 	}
742 	*mapping += bp->rx_dma_offset;
743 	return page;
744 }
745 
__bnxt_alloc_rx_data(struct bnxt * bp,dma_addr_t * mapping,gfp_t gfp)746 static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
747 				       gfp_t gfp)
748 {
749 	u8 *data;
750 	struct pci_dev *pdev = bp->pdev;
751 
752 	data = kmalloc(bp->rx_buf_size, gfp);
753 	if (!data)
754 		return NULL;
755 
756 	*mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
757 					bp->rx_buf_use_size, bp->rx_dir,
758 					DMA_ATTR_WEAK_ORDERING);
759 
760 	if (dma_mapping_error(&pdev->dev, *mapping)) {
761 		kfree(data);
762 		data = NULL;
763 	}
764 	return data;
765 }
766 
bnxt_alloc_rx_data(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,u16 prod,gfp_t gfp)767 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
768 		       u16 prod, gfp_t gfp)
769 {
770 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
771 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
772 	dma_addr_t mapping;
773 
774 	if (BNXT_RX_PAGE_MODE(bp)) {
775 		struct page *page =
776 			__bnxt_alloc_rx_page(bp, &mapping, rxr, gfp);
777 
778 		if (!page)
779 			return -ENOMEM;
780 
781 		rx_buf->data = page;
782 		rx_buf->data_ptr = page_address(page) + bp->rx_offset;
783 	} else {
784 		u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
785 
786 		if (!data)
787 			return -ENOMEM;
788 
789 		rx_buf->data = data;
790 		rx_buf->data_ptr = data + bp->rx_offset;
791 	}
792 	rx_buf->mapping = mapping;
793 
794 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
795 	return 0;
796 }
797 
bnxt_reuse_rx_data(struct bnxt_rx_ring_info * rxr,u16 cons,void * data)798 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
799 {
800 	u16 prod = rxr->rx_prod;
801 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
802 	struct rx_bd *cons_bd, *prod_bd;
803 
804 	prod_rx_buf = &rxr->rx_buf_ring[prod];
805 	cons_rx_buf = &rxr->rx_buf_ring[cons];
806 
807 	prod_rx_buf->data = data;
808 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
809 
810 	prod_rx_buf->mapping = cons_rx_buf->mapping;
811 
812 	prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
813 	cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
814 
815 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
816 }
817 
bnxt_find_next_agg_idx(struct bnxt_rx_ring_info * rxr,u16 idx)818 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
819 {
820 	u16 next, max = rxr->rx_agg_bmap_size;
821 
822 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
823 	if (next >= max)
824 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
825 	return next;
826 }
827 
bnxt_alloc_rx_page(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,u16 prod,gfp_t gfp)828 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
829 				     struct bnxt_rx_ring_info *rxr,
830 				     u16 prod, gfp_t gfp)
831 {
832 	struct rx_bd *rxbd =
833 		&rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
834 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
835 	struct pci_dev *pdev = bp->pdev;
836 	struct page *page;
837 	dma_addr_t mapping;
838 	u16 sw_prod = rxr->rx_sw_agg_prod;
839 	unsigned int offset = 0;
840 
841 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
842 		page = rxr->rx_page;
843 		if (!page) {
844 			page = alloc_page(gfp);
845 			if (!page)
846 				return -ENOMEM;
847 			rxr->rx_page = page;
848 			rxr->rx_page_offset = 0;
849 		}
850 		offset = rxr->rx_page_offset;
851 		rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
852 		if (rxr->rx_page_offset == PAGE_SIZE)
853 			rxr->rx_page = NULL;
854 		else
855 			get_page(page);
856 	} else {
857 		page = alloc_page(gfp);
858 		if (!page)
859 			return -ENOMEM;
860 	}
861 
862 	mapping = dma_map_page_attrs(&pdev->dev, page, offset,
863 				     BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
864 				     DMA_ATTR_WEAK_ORDERING);
865 	if (dma_mapping_error(&pdev->dev, mapping)) {
866 		__free_page(page);
867 		return -EIO;
868 	}
869 
870 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
871 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
872 
873 	__set_bit(sw_prod, rxr->rx_agg_bmap);
874 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
875 	rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
876 
877 	rx_agg_buf->page = page;
878 	rx_agg_buf->offset = offset;
879 	rx_agg_buf->mapping = mapping;
880 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
881 	rxbd->rx_bd_opaque = sw_prod;
882 	return 0;
883 }
884 
bnxt_get_agg(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,u16 cp_cons,u16 curr)885 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
886 				       struct bnxt_cp_ring_info *cpr,
887 				       u16 cp_cons, u16 curr)
888 {
889 	struct rx_agg_cmp *agg;
890 
891 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
892 	agg = (struct rx_agg_cmp *)
893 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
894 	return agg;
895 }
896 
bnxt_get_tpa_agg_p5(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,u16 agg_id,u16 curr)897 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
898 					      struct bnxt_rx_ring_info *rxr,
899 					      u16 agg_id, u16 curr)
900 {
901 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
902 
903 	return &tpa_info->agg_arr[curr];
904 }
905 
bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info * cpr,u16 idx,u16 start,u32 agg_bufs,bool tpa)906 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
907 				   u16 start, u32 agg_bufs, bool tpa)
908 {
909 	struct bnxt_napi *bnapi = cpr->bnapi;
910 	struct bnxt *bp = bnapi->bp;
911 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
912 	u16 prod = rxr->rx_agg_prod;
913 	u16 sw_prod = rxr->rx_sw_agg_prod;
914 	bool p5_tpa = false;
915 	u32 i;
916 
917 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
918 		p5_tpa = true;
919 
920 	for (i = 0; i < agg_bufs; i++) {
921 		u16 cons;
922 		struct rx_agg_cmp *agg;
923 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
924 		struct rx_bd *prod_bd;
925 		struct page *page;
926 
927 		if (p5_tpa)
928 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
929 		else
930 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
931 		cons = agg->rx_agg_cmp_opaque;
932 		__clear_bit(cons, rxr->rx_agg_bmap);
933 
934 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
935 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
936 
937 		__set_bit(sw_prod, rxr->rx_agg_bmap);
938 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
939 		cons_rx_buf = &rxr->rx_agg_ring[cons];
940 
941 		/* It is possible for sw_prod to be equal to cons, so
942 		 * set cons_rx_buf->page to NULL first.
943 		 */
944 		page = cons_rx_buf->page;
945 		cons_rx_buf->page = NULL;
946 		prod_rx_buf->page = page;
947 		prod_rx_buf->offset = cons_rx_buf->offset;
948 
949 		prod_rx_buf->mapping = cons_rx_buf->mapping;
950 
951 		prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
952 
953 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
954 		prod_bd->rx_bd_opaque = sw_prod;
955 
956 		prod = NEXT_RX_AGG(prod);
957 		sw_prod = NEXT_RX_AGG(sw_prod);
958 	}
959 	rxr->rx_agg_prod = prod;
960 	rxr->rx_sw_agg_prod = sw_prod;
961 }
962 
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)963 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
964 					struct bnxt_rx_ring_info *rxr,
965 					u16 cons, void *data, u8 *data_ptr,
966 					dma_addr_t dma_addr,
967 					unsigned int offset_and_len)
968 {
969 	unsigned int payload = offset_and_len >> 16;
970 	unsigned int len = offset_and_len & 0xffff;
971 	skb_frag_t *frag;
972 	struct page *page = data;
973 	u16 prod = rxr->rx_prod;
974 	struct sk_buff *skb;
975 	int off, err;
976 
977 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
978 	if (unlikely(err)) {
979 		bnxt_reuse_rx_data(rxr, cons, data);
980 		return NULL;
981 	}
982 	dma_addr -= bp->rx_dma_offset;
983 	dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
984 			     DMA_ATTR_WEAK_ORDERING);
985 	page_pool_release_page(rxr->page_pool, page);
986 
987 	if (unlikely(!payload))
988 		payload = eth_get_headlen(bp->dev, data_ptr, len);
989 
990 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
991 	if (!skb) {
992 		__free_page(page);
993 		return NULL;
994 	}
995 
996 	off = (void *)data_ptr - page_address(page);
997 	skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
998 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
999 	       payload + NET_IP_ALIGN);
1000 
1001 	frag = &skb_shinfo(skb)->frags[0];
1002 	skb_frag_size_sub(frag, payload);
1003 	skb_frag_off_add(frag, payload);
1004 	skb->data_len -= payload;
1005 	skb->tail += payload;
1006 
1007 	return skb;
1008 }
1009 
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)1010 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1011 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1012 				   void *data, u8 *data_ptr,
1013 				   dma_addr_t dma_addr,
1014 				   unsigned int offset_and_len)
1015 {
1016 	u16 prod = rxr->rx_prod;
1017 	struct sk_buff *skb;
1018 	int err;
1019 
1020 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1021 	if (unlikely(err)) {
1022 		bnxt_reuse_rx_data(rxr, cons, data);
1023 		return NULL;
1024 	}
1025 
1026 	skb = build_skb(data, 0);
1027 	dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1028 			       bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
1029 	if (!skb) {
1030 		kfree(data);
1031 		return NULL;
1032 	}
1033 
1034 	skb_reserve(skb, bp->rx_offset);
1035 	skb_put(skb, offset_and_len & 0xffff);
1036 	return skb;
1037 }
1038 
bnxt_rx_pages(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,struct sk_buff * skb,u16 idx,u32 agg_bufs,bool tpa)1039 static struct sk_buff *bnxt_rx_pages(struct bnxt *bp,
1040 				     struct bnxt_cp_ring_info *cpr,
1041 				     struct sk_buff *skb, u16 idx,
1042 				     u32 agg_bufs, bool tpa)
1043 {
1044 	struct bnxt_napi *bnapi = cpr->bnapi;
1045 	struct pci_dev *pdev = bp->pdev;
1046 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1047 	u16 prod = rxr->rx_agg_prod;
1048 	bool p5_tpa = false;
1049 	u32 i;
1050 
1051 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
1052 		p5_tpa = true;
1053 
1054 	for (i = 0; i < agg_bufs; i++) {
1055 		u16 cons, frag_len;
1056 		struct rx_agg_cmp *agg;
1057 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1058 		struct page *page;
1059 		dma_addr_t mapping;
1060 
1061 		if (p5_tpa)
1062 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1063 		else
1064 			agg = bnxt_get_agg(bp, cpr, idx, i);
1065 		cons = agg->rx_agg_cmp_opaque;
1066 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1067 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1068 
1069 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1070 		skb_fill_page_desc(skb, i, cons_rx_buf->page,
1071 				   cons_rx_buf->offset, frag_len);
1072 		__clear_bit(cons, rxr->rx_agg_bmap);
1073 
1074 		/* It is possible for bnxt_alloc_rx_page() to allocate
1075 		 * a sw_prod index that equals the cons index, so we
1076 		 * need to clear the cons entry now.
1077 		 */
1078 		mapping = cons_rx_buf->mapping;
1079 		page = cons_rx_buf->page;
1080 		cons_rx_buf->page = NULL;
1081 
1082 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1083 			struct skb_shared_info *shinfo;
1084 			unsigned int nr_frags;
1085 
1086 			shinfo = skb_shinfo(skb);
1087 			nr_frags = --shinfo->nr_frags;
1088 			__skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
1089 
1090 			dev_kfree_skb(skb);
1091 
1092 			cons_rx_buf->page = page;
1093 
1094 			/* Update prod since possibly some pages have been
1095 			 * allocated already.
1096 			 */
1097 			rxr->rx_agg_prod = prod;
1098 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1099 			return NULL;
1100 		}
1101 
1102 		dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1103 				     PCI_DMA_FROMDEVICE,
1104 				     DMA_ATTR_WEAK_ORDERING);
1105 
1106 		skb->data_len += frag_len;
1107 		skb->len += frag_len;
1108 		skb->truesize += PAGE_SIZE;
1109 
1110 		prod = NEXT_RX_AGG(prod);
1111 	}
1112 	rxr->rx_agg_prod = prod;
1113 	return skb;
1114 }
1115 
bnxt_agg_bufs_valid(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,u8 agg_bufs,u32 * raw_cons)1116 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1117 			       u8 agg_bufs, u32 *raw_cons)
1118 {
1119 	u16 last;
1120 	struct rx_agg_cmp *agg;
1121 
1122 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1123 	last = RING_CMP(*raw_cons);
1124 	agg = (struct rx_agg_cmp *)
1125 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1126 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1127 }
1128 
bnxt_copy_skb(struct bnxt_napi * bnapi,u8 * data,unsigned int len,dma_addr_t mapping)1129 static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1130 					    unsigned int len,
1131 					    dma_addr_t mapping)
1132 {
1133 	struct bnxt *bp = bnapi->bp;
1134 	struct pci_dev *pdev = bp->pdev;
1135 	struct sk_buff *skb;
1136 
1137 	skb = napi_alloc_skb(&bnapi->napi, len);
1138 	if (!skb)
1139 		return NULL;
1140 
1141 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1142 				bp->rx_dir);
1143 
1144 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1145 	       len + NET_IP_ALIGN);
1146 
1147 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1148 				   bp->rx_dir);
1149 
1150 	skb_put(skb, len);
1151 	return skb;
1152 }
1153 
bnxt_discard_rx(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,u32 * raw_cons,void * cmp)1154 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1155 			   u32 *raw_cons, void *cmp)
1156 {
1157 	struct rx_cmp *rxcmp = cmp;
1158 	u32 tmp_raw_cons = *raw_cons;
1159 	u8 cmp_type, agg_bufs = 0;
1160 
1161 	cmp_type = RX_CMP_TYPE(rxcmp);
1162 
1163 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1164 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1165 			    RX_CMP_AGG_BUFS) >>
1166 			   RX_CMP_AGG_BUFS_SHIFT;
1167 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1168 		struct rx_tpa_end_cmp *tpa_end = cmp;
1169 
1170 		if (bp->flags & BNXT_FLAG_CHIP_P5)
1171 			return 0;
1172 
1173 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1174 	}
1175 
1176 	if (agg_bufs) {
1177 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1178 			return -EBUSY;
1179 	}
1180 	*raw_cons = tmp_raw_cons;
1181 	return 0;
1182 }
1183 
bnxt_queue_fw_reset_work(struct bnxt * bp,unsigned long delay)1184 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
1185 {
1186 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
1187 		return;
1188 
1189 	if (BNXT_PF(bp))
1190 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
1191 	else
1192 		schedule_delayed_work(&bp->fw_reset_task, delay);
1193 }
1194 
bnxt_queue_sp_work(struct bnxt * bp)1195 static void bnxt_queue_sp_work(struct bnxt *bp)
1196 {
1197 	if (BNXT_PF(bp))
1198 		queue_work(bnxt_pf_wq, &bp->sp_task);
1199 	else
1200 		schedule_work(&bp->sp_task);
1201 }
1202 
bnxt_sched_reset(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)1203 static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1204 {
1205 	if (!rxr->bnapi->in_reset) {
1206 		rxr->bnapi->in_reset = true;
1207 		if (bp->flags & BNXT_FLAG_CHIP_P5)
1208 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1209 		else
1210 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
1211 		bnxt_queue_sp_work(bp);
1212 	}
1213 	rxr->rx_next_cons = 0xffff;
1214 }
1215 
bnxt_alloc_agg_idx(struct bnxt_rx_ring_info * rxr,u16 agg_id)1216 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1217 {
1218 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1219 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1220 
1221 	if (test_bit(idx, map->agg_idx_bmap))
1222 		idx = find_first_zero_bit(map->agg_idx_bmap,
1223 					  BNXT_AGG_IDX_BMAP_SIZE);
1224 	__set_bit(idx, map->agg_idx_bmap);
1225 	map->agg_id_tbl[agg_id] = idx;
1226 	return idx;
1227 }
1228 
bnxt_free_agg_idx(struct bnxt_rx_ring_info * rxr,u16 idx)1229 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1230 {
1231 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1232 
1233 	__clear_bit(idx, map->agg_idx_bmap);
1234 }
1235 
bnxt_lookup_agg_idx(struct bnxt_rx_ring_info * rxr,u16 agg_id)1236 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1237 {
1238 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1239 
1240 	return map->agg_id_tbl[agg_id];
1241 }
1242 
bnxt_tpa_start(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,struct rx_tpa_start_cmp * tpa_start,struct rx_tpa_start_cmp_ext * tpa_start1)1243 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1244 			   struct rx_tpa_start_cmp *tpa_start,
1245 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1246 {
1247 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1248 	struct bnxt_tpa_info *tpa_info;
1249 	u16 cons, prod, agg_id;
1250 	struct rx_bd *prod_bd;
1251 	dma_addr_t mapping;
1252 
1253 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
1254 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1255 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1256 	} else {
1257 		agg_id = TPA_START_AGG_ID(tpa_start);
1258 	}
1259 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1260 	prod = rxr->rx_prod;
1261 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1262 	prod_rx_buf = &rxr->rx_buf_ring[prod];
1263 	tpa_info = &rxr->rx_tpa[agg_id];
1264 
1265 	if (unlikely(cons != rxr->rx_next_cons ||
1266 		     TPA_START_ERROR(tpa_start))) {
1267 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1268 			    cons, rxr->rx_next_cons,
1269 			    TPA_START_ERROR_CODE(tpa_start1));
1270 		bnxt_sched_reset(bp, rxr);
1271 		return;
1272 	}
1273 	/* Store cfa_code in tpa_info to use in tpa_end
1274 	 * completion processing.
1275 	 */
1276 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1277 	prod_rx_buf->data = tpa_info->data;
1278 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1279 
1280 	mapping = tpa_info->mapping;
1281 	prod_rx_buf->mapping = mapping;
1282 
1283 	prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1284 
1285 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1286 
1287 	tpa_info->data = cons_rx_buf->data;
1288 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1289 	cons_rx_buf->data = NULL;
1290 	tpa_info->mapping = cons_rx_buf->mapping;
1291 
1292 	tpa_info->len =
1293 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1294 				RX_TPA_START_CMP_LEN_SHIFT;
1295 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1296 		u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1297 
1298 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1299 		tpa_info->gso_type = SKB_GSO_TCPV4;
1300 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1301 		if (hash_type == 3 || TPA_START_IS_IPV6(tpa_start1))
1302 			tpa_info->gso_type = SKB_GSO_TCPV6;
1303 		tpa_info->rss_hash =
1304 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1305 	} else {
1306 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1307 		tpa_info->gso_type = 0;
1308 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1309 	}
1310 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1311 	tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1312 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1313 	tpa_info->agg_count = 0;
1314 
1315 	rxr->rx_prod = NEXT_RX(prod);
1316 	cons = NEXT_RX(cons);
1317 	rxr->rx_next_cons = NEXT_RX(cons);
1318 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1319 
1320 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1321 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1322 	cons_rx_buf->data = NULL;
1323 }
1324 
bnxt_abort_tpa(struct bnxt_cp_ring_info * cpr,u16 idx,u32 agg_bufs)1325 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1326 {
1327 	if (agg_bufs)
1328 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1329 }
1330 
1331 #ifdef CONFIG_INET
bnxt_gro_tunnel(struct sk_buff * skb,__be16 ip_proto)1332 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1333 {
1334 	struct udphdr *uh = NULL;
1335 
1336 	if (ip_proto == htons(ETH_P_IP)) {
1337 		struct iphdr *iph = (struct iphdr *)skb->data;
1338 
1339 		if (iph->protocol == IPPROTO_UDP)
1340 			uh = (struct udphdr *)(iph + 1);
1341 	} else {
1342 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1343 
1344 		if (iph->nexthdr == IPPROTO_UDP)
1345 			uh = (struct udphdr *)(iph + 1);
1346 	}
1347 	if (uh) {
1348 		if (uh->check)
1349 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1350 		else
1351 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1352 	}
1353 }
1354 #endif
1355 
bnxt_gro_func_5731x(struct bnxt_tpa_info * tpa_info,int payload_off,int tcp_ts,struct sk_buff * skb)1356 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1357 					   int payload_off, int tcp_ts,
1358 					   struct sk_buff *skb)
1359 {
1360 #ifdef CONFIG_INET
1361 	struct tcphdr *th;
1362 	int len, nw_off;
1363 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1364 	u32 hdr_info = tpa_info->hdr_info;
1365 	bool loopback = false;
1366 
1367 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1368 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1369 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1370 
1371 	/* If the packet is an internal loopback packet, the offsets will
1372 	 * have an extra 4 bytes.
1373 	 */
1374 	if (inner_mac_off == 4) {
1375 		loopback = true;
1376 	} else if (inner_mac_off > 4) {
1377 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1378 					    ETH_HLEN - 2));
1379 
1380 		/* We only support inner iPv4/ipv6.  If we don't see the
1381 		 * correct protocol ID, it must be a loopback packet where
1382 		 * the offsets are off by 4.
1383 		 */
1384 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1385 			loopback = true;
1386 	}
1387 	if (loopback) {
1388 		/* internal loopback packet, subtract all offsets by 4 */
1389 		inner_ip_off -= 4;
1390 		inner_mac_off -= 4;
1391 		outer_ip_off -= 4;
1392 	}
1393 
1394 	nw_off = inner_ip_off - ETH_HLEN;
1395 	skb_set_network_header(skb, nw_off);
1396 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1397 		struct ipv6hdr *iph = ipv6_hdr(skb);
1398 
1399 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1400 		len = skb->len - skb_transport_offset(skb);
1401 		th = tcp_hdr(skb);
1402 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1403 	} else {
1404 		struct iphdr *iph = ip_hdr(skb);
1405 
1406 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1407 		len = skb->len - skb_transport_offset(skb);
1408 		th = tcp_hdr(skb);
1409 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1410 	}
1411 
1412 	if (inner_mac_off) { /* tunnel */
1413 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1414 					    ETH_HLEN - 2));
1415 
1416 		bnxt_gro_tunnel(skb, proto);
1417 	}
1418 #endif
1419 	return skb;
1420 }
1421 
bnxt_gro_func_5750x(struct bnxt_tpa_info * tpa_info,int payload_off,int tcp_ts,struct sk_buff * skb)1422 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1423 					   int payload_off, int tcp_ts,
1424 					   struct sk_buff *skb)
1425 {
1426 #ifdef CONFIG_INET
1427 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1428 	u32 hdr_info = tpa_info->hdr_info;
1429 	int iphdr_len, nw_off;
1430 
1431 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1432 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1433 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1434 
1435 	nw_off = inner_ip_off - ETH_HLEN;
1436 	skb_set_network_header(skb, nw_off);
1437 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1438 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1439 	skb_set_transport_header(skb, nw_off + iphdr_len);
1440 
1441 	if (inner_mac_off) { /* tunnel */
1442 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1443 					    ETH_HLEN - 2));
1444 
1445 		bnxt_gro_tunnel(skb, proto);
1446 	}
1447 #endif
1448 	return skb;
1449 }
1450 
1451 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1452 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1453 
bnxt_gro_func_5730x(struct bnxt_tpa_info * tpa_info,int payload_off,int tcp_ts,struct sk_buff * skb)1454 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1455 					   int payload_off, int tcp_ts,
1456 					   struct sk_buff *skb)
1457 {
1458 #ifdef CONFIG_INET
1459 	struct tcphdr *th;
1460 	int len, nw_off, tcp_opt_len = 0;
1461 
1462 	if (tcp_ts)
1463 		tcp_opt_len = 12;
1464 
1465 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1466 		struct iphdr *iph;
1467 
1468 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1469 			 ETH_HLEN;
1470 		skb_set_network_header(skb, nw_off);
1471 		iph = ip_hdr(skb);
1472 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1473 		len = skb->len - skb_transport_offset(skb);
1474 		th = tcp_hdr(skb);
1475 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1476 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1477 		struct ipv6hdr *iph;
1478 
1479 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1480 			 ETH_HLEN;
1481 		skb_set_network_header(skb, nw_off);
1482 		iph = ipv6_hdr(skb);
1483 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1484 		len = skb->len - skb_transport_offset(skb);
1485 		th = tcp_hdr(skb);
1486 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1487 	} else {
1488 		dev_kfree_skb_any(skb);
1489 		return NULL;
1490 	}
1491 
1492 	if (nw_off) /* tunnel */
1493 		bnxt_gro_tunnel(skb, skb->protocol);
1494 #endif
1495 	return skb;
1496 }
1497 
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)1498 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1499 					   struct bnxt_tpa_info *tpa_info,
1500 					   struct rx_tpa_end_cmp *tpa_end,
1501 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1502 					   struct sk_buff *skb)
1503 {
1504 #ifdef CONFIG_INET
1505 	int payload_off;
1506 	u16 segs;
1507 
1508 	segs = TPA_END_TPA_SEGS(tpa_end);
1509 	if (segs == 1)
1510 		return skb;
1511 
1512 	NAPI_GRO_CB(skb)->count = segs;
1513 	skb_shinfo(skb)->gso_size =
1514 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1515 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1516 	if (bp->flags & BNXT_FLAG_CHIP_P5)
1517 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1518 	else
1519 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1520 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1521 	if (likely(skb))
1522 		tcp_gro_complete(skb);
1523 #endif
1524 	return skb;
1525 }
1526 
1527 /* Given the cfa_code of a received packet determine which
1528  * netdev (vf-rep or PF) the packet is destined to.
1529  */
bnxt_get_pkt_dev(struct bnxt * bp,u16 cfa_code)1530 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1531 {
1532 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1533 
1534 	/* if vf-rep dev is NULL, the must belongs to the PF */
1535 	return dev ? dev : bp->dev;
1536 }
1537 
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)1538 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1539 					   struct bnxt_cp_ring_info *cpr,
1540 					   u32 *raw_cons,
1541 					   struct rx_tpa_end_cmp *tpa_end,
1542 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1543 					   u8 *event)
1544 {
1545 	struct bnxt_napi *bnapi = cpr->bnapi;
1546 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1547 	u8 *data_ptr, agg_bufs;
1548 	unsigned int len;
1549 	struct bnxt_tpa_info *tpa_info;
1550 	dma_addr_t mapping;
1551 	struct sk_buff *skb;
1552 	u16 idx = 0, agg_id;
1553 	void *data;
1554 	bool gro;
1555 
1556 	if (unlikely(bnapi->in_reset)) {
1557 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1558 
1559 		if (rc < 0)
1560 			return ERR_PTR(-EBUSY);
1561 		return NULL;
1562 	}
1563 
1564 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
1565 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1566 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1567 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1568 		tpa_info = &rxr->rx_tpa[agg_id];
1569 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1570 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1571 				    agg_bufs, tpa_info->agg_count);
1572 			agg_bufs = tpa_info->agg_count;
1573 		}
1574 		tpa_info->agg_count = 0;
1575 		*event |= BNXT_AGG_EVENT;
1576 		bnxt_free_agg_idx(rxr, agg_id);
1577 		idx = agg_id;
1578 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1579 	} else {
1580 		agg_id = TPA_END_AGG_ID(tpa_end);
1581 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1582 		tpa_info = &rxr->rx_tpa[agg_id];
1583 		idx = RING_CMP(*raw_cons);
1584 		if (agg_bufs) {
1585 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1586 				return ERR_PTR(-EBUSY);
1587 
1588 			*event |= BNXT_AGG_EVENT;
1589 			idx = NEXT_CMP(idx);
1590 		}
1591 		gro = !!TPA_END_GRO(tpa_end);
1592 	}
1593 	data = tpa_info->data;
1594 	data_ptr = tpa_info->data_ptr;
1595 	prefetch(data_ptr);
1596 	len = tpa_info->len;
1597 	mapping = tpa_info->mapping;
1598 
1599 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1600 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1601 		if (agg_bufs > MAX_SKB_FRAGS)
1602 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1603 				    agg_bufs, (int)MAX_SKB_FRAGS);
1604 		return NULL;
1605 	}
1606 
1607 	if (len <= bp->rx_copy_thresh) {
1608 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1609 		if (!skb) {
1610 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1611 			return NULL;
1612 		}
1613 	} else {
1614 		u8 *new_data;
1615 		dma_addr_t new_mapping;
1616 
1617 		new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1618 		if (!new_data) {
1619 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1620 			return NULL;
1621 		}
1622 
1623 		tpa_info->data = new_data;
1624 		tpa_info->data_ptr = new_data + bp->rx_offset;
1625 		tpa_info->mapping = new_mapping;
1626 
1627 		skb = build_skb(data, 0);
1628 		dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1629 				       bp->rx_buf_use_size, bp->rx_dir,
1630 				       DMA_ATTR_WEAK_ORDERING);
1631 
1632 		if (!skb) {
1633 			kfree(data);
1634 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1635 			return NULL;
1636 		}
1637 		skb_reserve(skb, bp->rx_offset);
1638 		skb_put(skb, len);
1639 	}
1640 
1641 	if (agg_bufs) {
1642 		skb = bnxt_rx_pages(bp, cpr, skb, idx, agg_bufs, true);
1643 		if (!skb) {
1644 			/* Page reuse already handled by bnxt_rx_pages(). */
1645 			return NULL;
1646 		}
1647 	}
1648 
1649 	skb->protocol =
1650 		eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
1651 
1652 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1653 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1654 
1655 	if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1656 	    (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1657 		__be16 vlan_proto = htons(tpa_info->metadata >>
1658 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1659 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1660 
1661 		if (eth_type_vlan(vlan_proto)) {
1662 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1663 		} else {
1664 			dev_kfree_skb(skb);
1665 			return NULL;
1666 		}
1667 	}
1668 
1669 	skb_checksum_none_assert(skb);
1670 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1671 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1672 		skb->csum_level =
1673 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1674 	}
1675 
1676 	if (gro)
1677 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1678 
1679 	return skb;
1680 }
1681 
bnxt_tpa_agg(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,struct rx_agg_cmp * rx_agg)1682 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1683 			 struct rx_agg_cmp *rx_agg)
1684 {
1685 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1686 	struct bnxt_tpa_info *tpa_info;
1687 
1688 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1689 	tpa_info = &rxr->rx_tpa[agg_id];
1690 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1691 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1692 }
1693 
bnxt_deliver_skb(struct bnxt * bp,struct bnxt_napi * bnapi,struct sk_buff * skb)1694 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1695 			     struct sk_buff *skb)
1696 {
1697 	if (skb->dev != bp->dev) {
1698 		/* this packet belongs to a vf-rep */
1699 		bnxt_vf_rep_rx(bp, skb);
1700 		return;
1701 	}
1702 	skb_record_rx_queue(skb, bnapi->index);
1703 	napi_gro_receive(&bnapi->napi, skb);
1704 }
1705 
1706 /* returns the following:
1707  * 1       - 1 packet successfully received
1708  * 0       - successful TPA_START, packet not completed yet
1709  * -EBUSY  - completion ring does not have all the agg buffers yet
1710  * -ENOMEM - packet aborted due to out of memory
1711  * -EIO    - packet aborted due to hw error indicated in BD
1712  */
bnxt_rx_pkt(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,u32 * raw_cons,u8 * event)1713 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1714 		       u32 *raw_cons, u8 *event)
1715 {
1716 	struct bnxt_napi *bnapi = cpr->bnapi;
1717 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1718 	struct net_device *dev = bp->dev;
1719 	struct rx_cmp *rxcmp;
1720 	struct rx_cmp_ext *rxcmp1;
1721 	u32 tmp_raw_cons = *raw_cons;
1722 	u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1723 	struct bnxt_sw_rx_bd *rx_buf;
1724 	unsigned int len;
1725 	u8 *data_ptr, agg_bufs, cmp_type;
1726 	dma_addr_t dma_addr;
1727 	struct sk_buff *skb;
1728 	void *data;
1729 	int rc = 0;
1730 	u32 misc;
1731 
1732 	rxcmp = (struct rx_cmp *)
1733 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1734 
1735 	cmp_type = RX_CMP_TYPE(rxcmp);
1736 
1737 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
1738 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
1739 		goto next_rx_no_prod_no_len;
1740 	}
1741 
1742 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1743 	cp_cons = RING_CMP(tmp_raw_cons);
1744 	rxcmp1 = (struct rx_cmp_ext *)
1745 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1746 
1747 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1748 		return -EBUSY;
1749 
1750 	/* The valid test of the entry must be done first before
1751 	 * reading any further.
1752 	 */
1753 	dma_rmb();
1754 	prod = rxr->rx_prod;
1755 
1756 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1757 		bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1758 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
1759 
1760 		*event |= BNXT_RX_EVENT;
1761 		goto next_rx_no_prod_no_len;
1762 
1763 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1764 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
1765 				   (struct rx_tpa_end_cmp *)rxcmp,
1766 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
1767 
1768 		if (IS_ERR(skb))
1769 			return -EBUSY;
1770 
1771 		rc = -ENOMEM;
1772 		if (likely(skb)) {
1773 			bnxt_deliver_skb(bp, bnapi, skb);
1774 			rc = 1;
1775 		}
1776 		*event |= BNXT_RX_EVENT;
1777 		goto next_rx_no_prod_no_len;
1778 	}
1779 
1780 	cons = rxcmp->rx_cmp_opaque;
1781 	if (unlikely(cons != rxr->rx_next_cons)) {
1782 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
1783 
1784 		/* 0xffff is forced error, don't print it */
1785 		if (rxr->rx_next_cons != 0xffff)
1786 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
1787 				    cons, rxr->rx_next_cons);
1788 		bnxt_sched_reset(bp, rxr);
1789 		if (rc1)
1790 			return rc1;
1791 		goto next_rx_no_prod_no_len;
1792 	}
1793 	rx_buf = &rxr->rx_buf_ring[cons];
1794 	data = rx_buf->data;
1795 	data_ptr = rx_buf->data_ptr;
1796 	prefetch(data_ptr);
1797 
1798 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1799 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
1800 
1801 	if (agg_bufs) {
1802 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1803 			return -EBUSY;
1804 
1805 		cp_cons = NEXT_CMP(cp_cons);
1806 		*event |= BNXT_AGG_EVENT;
1807 	}
1808 	*event |= BNXT_RX_EVENT;
1809 
1810 	rx_buf->data = NULL;
1811 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1812 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
1813 
1814 		bnxt_reuse_rx_data(rxr, cons, data);
1815 		if (agg_bufs)
1816 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
1817 					       false);
1818 
1819 		rc = -EIO;
1820 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
1821 			bnapi->cp_ring.sw_stats.rx.rx_buf_errors++;
1822 			if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
1823 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
1824 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
1825 						 rx_err);
1826 				bnxt_sched_reset(bp, rxr);
1827 			}
1828 		}
1829 		goto next_rx_no_len;
1830 	}
1831 
1832 	len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
1833 	dma_addr = rx_buf->mapping;
1834 
1835 	if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1836 		rc = 1;
1837 		goto next_rx;
1838 	}
1839 
1840 	if (len <= bp->rx_copy_thresh) {
1841 		skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
1842 		bnxt_reuse_rx_data(rxr, cons, data);
1843 		if (!skb) {
1844 			if (agg_bufs)
1845 				bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
1846 						       agg_bufs, false);
1847 			rc = -ENOMEM;
1848 			goto next_rx;
1849 		}
1850 	} else {
1851 		u32 payload;
1852 
1853 		if (rx_buf->data_ptr == data_ptr)
1854 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
1855 		else
1856 			payload = 0;
1857 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
1858 				      payload | len);
1859 		if (!skb) {
1860 			rc = -ENOMEM;
1861 			goto next_rx;
1862 		}
1863 	}
1864 
1865 	if (agg_bufs) {
1866 		skb = bnxt_rx_pages(bp, cpr, skb, cp_cons, agg_bufs, false);
1867 		if (!skb) {
1868 			rc = -ENOMEM;
1869 			goto next_rx;
1870 		}
1871 	}
1872 
1873 	if (RX_CMP_HASH_VALID(rxcmp)) {
1874 		u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1875 		enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1876 
1877 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1878 		if (hash_type != 1 && hash_type != 3)
1879 			type = PKT_HASH_TYPE_L3;
1880 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1881 	}
1882 
1883 	cfa_code = RX_CMP_CFA_CODE(rxcmp1);
1884 	skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
1885 
1886 	if ((rxcmp1->rx_cmp_flags2 &
1887 	     cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1888 	    (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1889 		u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1890 		u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1891 		__be16 vlan_proto = htons(meta_data >>
1892 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1893 
1894 		if (eth_type_vlan(vlan_proto)) {
1895 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1896 		} else {
1897 			dev_kfree_skb(skb);
1898 			goto next_rx;
1899 		}
1900 	}
1901 
1902 	skb_checksum_none_assert(skb);
1903 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
1904 		if (dev->features & NETIF_F_RXCSUM) {
1905 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1906 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1907 		}
1908 	} else {
1909 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1910 			if (dev->features & NETIF_F_RXCSUM)
1911 				bnapi->cp_ring.sw_stats.rx.rx_l4_csum_errors++;
1912 		}
1913 	}
1914 
1915 	bnxt_deliver_skb(bp, bnapi, skb);
1916 	rc = 1;
1917 
1918 next_rx:
1919 	cpr->rx_packets += 1;
1920 	cpr->rx_bytes += len;
1921 
1922 next_rx_no_len:
1923 	rxr->rx_prod = NEXT_RX(prod);
1924 	rxr->rx_next_cons = NEXT_RX(cons);
1925 
1926 next_rx_no_prod_no_len:
1927 	*raw_cons = tmp_raw_cons;
1928 
1929 	return rc;
1930 }
1931 
1932 /* In netpoll mode, if we are using a combined completion ring, we need to
1933  * discard the rx packets and recycle the buffers.
1934  */
bnxt_force_rx_discard(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,u32 * raw_cons,u8 * event)1935 static int bnxt_force_rx_discard(struct bnxt *bp,
1936 				 struct bnxt_cp_ring_info *cpr,
1937 				 u32 *raw_cons, u8 *event)
1938 {
1939 	u32 tmp_raw_cons = *raw_cons;
1940 	struct rx_cmp_ext *rxcmp1;
1941 	struct rx_cmp *rxcmp;
1942 	u16 cp_cons;
1943 	u8 cmp_type;
1944 
1945 	cp_cons = RING_CMP(tmp_raw_cons);
1946 	rxcmp = (struct rx_cmp *)
1947 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1948 
1949 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1950 	cp_cons = RING_CMP(tmp_raw_cons);
1951 	rxcmp1 = (struct rx_cmp_ext *)
1952 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1953 
1954 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1955 		return -EBUSY;
1956 
1957 	/* The valid test of the entry must be done first before
1958 	 * reading any further.
1959 	 */
1960 	dma_rmb();
1961 	cmp_type = RX_CMP_TYPE(rxcmp);
1962 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1963 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1964 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1965 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1966 		struct rx_tpa_end_cmp_ext *tpa_end1;
1967 
1968 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
1969 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
1970 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
1971 	}
1972 	return bnxt_rx_pkt(bp, cpr, raw_cons, event);
1973 }
1974 
bnxt_fw_health_readl(struct bnxt * bp,int reg_idx)1975 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
1976 {
1977 	struct bnxt_fw_health *fw_health = bp->fw_health;
1978 	u32 reg = fw_health->regs[reg_idx];
1979 	u32 reg_type, reg_off, val = 0;
1980 
1981 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
1982 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
1983 	switch (reg_type) {
1984 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
1985 		pci_read_config_dword(bp->pdev, reg_off, &val);
1986 		break;
1987 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
1988 		reg_off = fw_health->mapped_regs[reg_idx];
1989 		fallthrough;
1990 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
1991 		val = readl(bp->bar0 + reg_off);
1992 		break;
1993 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
1994 		val = readl(bp->bar1 + reg_off);
1995 		break;
1996 	}
1997 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
1998 		val &= fw_health->fw_reset_inprog_reg_mask;
1999 	return val;
2000 }
2001 
bnxt_agg_ring_id_to_grp_idx(struct bnxt * bp,u16 ring_id)2002 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2003 {
2004 	int i;
2005 
2006 	for (i = 0; i < bp->rx_nr_rings; i++) {
2007 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2008 		struct bnxt_ring_grp_info *grp_info;
2009 
2010 		grp_info = &bp->grp_info[grp_idx];
2011 		if (grp_info->agg_fw_ring_id == ring_id)
2012 			return grp_idx;
2013 	}
2014 	return INVALID_HW_RING_ID;
2015 }
2016 
2017 #define BNXT_GET_EVENT_PORT(data)	\
2018 	((data) &			\
2019 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2020 
2021 #define BNXT_EVENT_RING_TYPE(data2)	\
2022 	((data2) &			\
2023 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2024 
2025 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2026 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2027 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2028 
bnxt_async_event_process(struct bnxt * bp,struct hwrm_async_event_cmpl * cmpl)2029 static int bnxt_async_event_process(struct bnxt *bp,
2030 				    struct hwrm_async_event_cmpl *cmpl)
2031 {
2032 	u16 event_id = le16_to_cpu(cmpl->event_id);
2033 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2034 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2035 
2036 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2037 	switch (event_id) {
2038 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2039 		struct bnxt_link_info *link_info = &bp->link_info;
2040 
2041 		if (BNXT_VF(bp))
2042 			goto async_event_process_exit;
2043 
2044 		/* print unsupported speed warning in forced speed mode only */
2045 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2046 		    (data1 & 0x20000)) {
2047 			u16 fw_speed = link_info->force_link_speed;
2048 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2049 
2050 			if (speed != SPEED_UNKNOWN)
2051 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2052 					    speed);
2053 		}
2054 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2055 	}
2056 		fallthrough;
2057 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2058 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2059 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2060 		fallthrough;
2061 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2062 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2063 		break;
2064 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2065 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2066 		break;
2067 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2068 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2069 
2070 		if (BNXT_VF(bp))
2071 			break;
2072 
2073 		if (bp->pf.port_id != port_id)
2074 			break;
2075 
2076 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2077 		break;
2078 	}
2079 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2080 		if (BNXT_PF(bp))
2081 			goto async_event_process_exit;
2082 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2083 		break;
2084 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2085 		char *fatal_str = "non-fatal";
2086 
2087 		if (!bp->fw_health)
2088 			goto async_event_process_exit;
2089 
2090 		bp->fw_reset_timestamp = jiffies;
2091 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2092 		if (!bp->fw_reset_min_dsecs)
2093 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2094 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2095 		if (!bp->fw_reset_max_dsecs)
2096 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2097 		if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2098 			fatal_str = "fatal";
2099 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2100 		}
2101 		netif_warn(bp, hw, bp->dev,
2102 			   "Firmware %s reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2103 			   fatal_str, data1, data2,
2104 			   bp->fw_reset_min_dsecs * 100,
2105 			   bp->fw_reset_max_dsecs * 100);
2106 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2107 		break;
2108 	}
2109 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2110 		struct bnxt_fw_health *fw_health = bp->fw_health;
2111 
2112 		if (!fw_health)
2113 			goto async_event_process_exit;
2114 
2115 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2116 			fw_health->enabled = false;
2117 			netif_info(bp, drv, bp->dev,
2118 				   "Error recovery info: error recovery[0]\n");
2119 			break;
2120 		}
2121 		fw_health->master = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2122 		fw_health->tmr_multiplier =
2123 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2124 				     bp->current_interval * 10);
2125 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2126 		if (!fw_health->enabled)
2127 			fw_health->last_fw_heartbeat =
2128 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2129 		fw_health->last_fw_reset_cnt =
2130 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2131 		netif_info(bp, drv, bp->dev,
2132 			   "Error recovery info: error recovery[1], master[%d], reset count[%u], health status: 0x%x\n",
2133 			   fw_health->master, fw_health->last_fw_reset_cnt,
2134 			   bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG));
2135 		if (!fw_health->enabled) {
2136 			/* Make sure tmr_counter is set and visible to
2137 			 * bnxt_health_check() before setting enabled to true.
2138 			 */
2139 			smp_wmb();
2140 			fw_health->enabled = true;
2141 		}
2142 		goto async_event_process_exit;
2143 	}
2144 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2145 		netif_notice(bp, hw, bp->dev,
2146 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2147 			     data1, data2);
2148 		goto async_event_process_exit;
2149 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2150 		struct bnxt_rx_ring_info *rxr;
2151 		u16 grp_idx;
2152 
2153 		if (bp->flags & BNXT_FLAG_CHIP_P5)
2154 			goto async_event_process_exit;
2155 
2156 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2157 			    BNXT_EVENT_RING_TYPE(data2), data1);
2158 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2159 			goto async_event_process_exit;
2160 
2161 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2162 		if (grp_idx == INVALID_HW_RING_ID) {
2163 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2164 				    data1);
2165 			goto async_event_process_exit;
2166 		}
2167 		rxr = bp->bnapi[grp_idx]->rx_ring;
2168 		bnxt_sched_reset(bp, rxr);
2169 		goto async_event_process_exit;
2170 	}
2171 	default:
2172 		goto async_event_process_exit;
2173 	}
2174 	bnxt_queue_sp_work(bp);
2175 async_event_process_exit:
2176 	bnxt_ulp_async_events(bp, cmpl);
2177 	return 0;
2178 }
2179 
bnxt_hwrm_handler(struct bnxt * bp,struct tx_cmp * txcmp)2180 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2181 {
2182 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2183 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2184 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2185 				(struct hwrm_fwd_req_cmpl *)txcmp;
2186 
2187 	switch (cmpl_type) {
2188 	case CMPL_BASE_TYPE_HWRM_DONE:
2189 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2190 		if (seq_id == bp->hwrm_intr_seq_id)
2191 			bp->hwrm_intr_seq_id = (u16)~bp->hwrm_intr_seq_id;
2192 		else
2193 			netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
2194 		break;
2195 
2196 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2197 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2198 
2199 		if ((vf_id < bp->pf.first_vf_id) ||
2200 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2201 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2202 				   vf_id);
2203 			return -EINVAL;
2204 		}
2205 
2206 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2207 		set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
2208 		bnxt_queue_sp_work(bp);
2209 		break;
2210 
2211 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2212 		bnxt_async_event_process(bp,
2213 					 (struct hwrm_async_event_cmpl *)txcmp);
2214 
2215 	default:
2216 		break;
2217 	}
2218 
2219 	return 0;
2220 }
2221 
bnxt_msix(int irq,void * dev_instance)2222 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2223 {
2224 	struct bnxt_napi *bnapi = dev_instance;
2225 	struct bnxt *bp = bnapi->bp;
2226 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2227 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2228 
2229 	cpr->event_ctr++;
2230 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2231 	napi_schedule(&bnapi->napi);
2232 	return IRQ_HANDLED;
2233 }
2234 
bnxt_has_work(struct bnxt * bp,struct bnxt_cp_ring_info * cpr)2235 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2236 {
2237 	u32 raw_cons = cpr->cp_raw_cons;
2238 	u16 cons = RING_CMP(raw_cons);
2239 	struct tx_cmp *txcmp;
2240 
2241 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2242 
2243 	return TX_CMP_VALID(txcmp, raw_cons);
2244 }
2245 
bnxt_inta(int irq,void * dev_instance)2246 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
2247 {
2248 	struct bnxt_napi *bnapi = dev_instance;
2249 	struct bnxt *bp = bnapi->bp;
2250 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2251 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2252 	u32 int_status;
2253 
2254 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2255 
2256 	if (!bnxt_has_work(bp, cpr)) {
2257 		int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
2258 		/* return if erroneous interrupt */
2259 		if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
2260 			return IRQ_NONE;
2261 	}
2262 
2263 	/* disable ring IRQ */
2264 	BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
2265 
2266 	/* Return here if interrupt is shared and is disabled. */
2267 	if (unlikely(atomic_read(&bp->intr_sem) != 0))
2268 		return IRQ_HANDLED;
2269 
2270 	napi_schedule(&bnapi->napi);
2271 	return IRQ_HANDLED;
2272 }
2273 
__bnxt_poll_work(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,int budget)2274 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2275 			    int budget)
2276 {
2277 	struct bnxt_napi *bnapi = cpr->bnapi;
2278 	u32 raw_cons = cpr->cp_raw_cons;
2279 	u32 cons;
2280 	int tx_pkts = 0;
2281 	int rx_pkts = 0;
2282 	u8 event = 0;
2283 	struct tx_cmp *txcmp;
2284 
2285 	cpr->has_more_work = 0;
2286 	cpr->had_work_done = 1;
2287 	while (1) {
2288 		int rc;
2289 
2290 		cons = RING_CMP(raw_cons);
2291 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2292 
2293 		if (!TX_CMP_VALID(txcmp, raw_cons))
2294 			break;
2295 
2296 		/* The valid test of the entry must be done first before
2297 		 * reading any further.
2298 		 */
2299 		dma_rmb();
2300 		if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
2301 			tx_pkts++;
2302 			/* return full budget so NAPI will complete. */
2303 			if (unlikely(tx_pkts >= bp->tx_wake_thresh)) {
2304 				rx_pkts = budget;
2305 				raw_cons = NEXT_RAW_CMP(raw_cons);
2306 				if (budget)
2307 					cpr->has_more_work = 1;
2308 				break;
2309 			}
2310 		} else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2311 			if (likely(budget))
2312 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2313 			else
2314 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2315 							   &event);
2316 			if (likely(rc >= 0))
2317 				rx_pkts += rc;
2318 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2319 			 * the NAPI budget.  Otherwise, we may potentially loop
2320 			 * here forever if we consistently cannot allocate
2321 			 * buffers.
2322 			 */
2323 			else if (rc == -ENOMEM && budget)
2324 				rx_pkts++;
2325 			else if (rc == -EBUSY)	/* partial completion */
2326 				break;
2327 		} else if (unlikely((TX_CMP_TYPE(txcmp) ==
2328 				     CMPL_BASE_TYPE_HWRM_DONE) ||
2329 				    (TX_CMP_TYPE(txcmp) ==
2330 				     CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
2331 				    (TX_CMP_TYPE(txcmp) ==
2332 				     CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
2333 			bnxt_hwrm_handler(bp, txcmp);
2334 		}
2335 		raw_cons = NEXT_RAW_CMP(raw_cons);
2336 
2337 		if (rx_pkts && rx_pkts == budget) {
2338 			cpr->has_more_work = 1;
2339 			break;
2340 		}
2341 	}
2342 
2343 	if (event & BNXT_REDIRECT_EVENT)
2344 		xdp_do_flush_map();
2345 
2346 	if (event & BNXT_TX_EVENT) {
2347 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
2348 		u16 prod = txr->tx_prod;
2349 
2350 		/* Sync BD data before updating doorbell */
2351 		wmb();
2352 
2353 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
2354 	}
2355 
2356 	cpr->cp_raw_cons = raw_cons;
2357 	bnapi->tx_pkts += tx_pkts;
2358 	bnapi->events |= event;
2359 	return rx_pkts;
2360 }
2361 
__bnxt_poll_work_done(struct bnxt * bp,struct bnxt_napi * bnapi)2362 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi)
2363 {
2364 	if (bnapi->tx_pkts) {
2365 		bnapi->tx_int(bp, bnapi, bnapi->tx_pkts);
2366 		bnapi->tx_pkts = 0;
2367 	}
2368 
2369 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
2370 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2371 
2372 		if (bnapi->events & BNXT_AGG_EVENT)
2373 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2374 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2375 	}
2376 	bnapi->events = 0;
2377 }
2378 
bnxt_poll_work(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,int budget)2379 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2380 			  int budget)
2381 {
2382 	struct bnxt_napi *bnapi = cpr->bnapi;
2383 	int rx_pkts;
2384 
2385 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2386 
2387 	/* ACK completion ring before freeing tx ring and producing new
2388 	 * buffers in rx/agg rings to prevent overflowing the completion
2389 	 * ring.
2390 	 */
2391 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
2392 
2393 	__bnxt_poll_work_done(bp, bnapi);
2394 	return rx_pkts;
2395 }
2396 
bnxt_poll_nitroa0(struct napi_struct * napi,int budget)2397 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
2398 {
2399 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2400 	struct bnxt *bp = bnapi->bp;
2401 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2402 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2403 	struct tx_cmp *txcmp;
2404 	struct rx_cmp_ext *rxcmp1;
2405 	u32 cp_cons, tmp_raw_cons;
2406 	u32 raw_cons = cpr->cp_raw_cons;
2407 	u32 rx_pkts = 0;
2408 	u8 event = 0;
2409 
2410 	while (1) {
2411 		int rc;
2412 
2413 		cp_cons = RING_CMP(raw_cons);
2414 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2415 
2416 		if (!TX_CMP_VALID(txcmp, raw_cons))
2417 			break;
2418 
2419 		/* The valid test of the entry must be done first before
2420 		 * reading any further.
2421 		 */
2422 		dma_rmb();
2423 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2424 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
2425 			cp_cons = RING_CMP(tmp_raw_cons);
2426 			rxcmp1 = (struct rx_cmp_ext *)
2427 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2428 
2429 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2430 				break;
2431 
2432 			/* force an error to recycle the buffer */
2433 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2434 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2435 
2436 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2437 			if (likely(rc == -EIO) && budget)
2438 				rx_pkts++;
2439 			else if (rc == -EBUSY)	/* partial completion */
2440 				break;
2441 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
2442 				    CMPL_BASE_TYPE_HWRM_DONE)) {
2443 			bnxt_hwrm_handler(bp, txcmp);
2444 		} else {
2445 			netdev_err(bp->dev,
2446 				   "Invalid completion received on special ring\n");
2447 		}
2448 		raw_cons = NEXT_RAW_CMP(raw_cons);
2449 
2450 		if (rx_pkts == budget)
2451 			break;
2452 	}
2453 
2454 	cpr->cp_raw_cons = raw_cons;
2455 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
2456 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2457 
2458 	if (event & BNXT_AGG_EVENT)
2459 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2460 
2461 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
2462 		napi_complete_done(napi, rx_pkts);
2463 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2464 	}
2465 	return rx_pkts;
2466 }
2467 
bnxt_poll(struct napi_struct * napi,int budget)2468 static int bnxt_poll(struct napi_struct *napi, int budget)
2469 {
2470 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2471 	struct bnxt *bp = bnapi->bp;
2472 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2473 	int work_done = 0;
2474 
2475 	while (1) {
2476 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
2477 
2478 		if (work_done >= budget) {
2479 			if (!budget)
2480 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2481 			break;
2482 		}
2483 
2484 		if (!bnxt_has_work(bp, cpr)) {
2485 			if (napi_complete_done(napi, work_done))
2486 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2487 			break;
2488 		}
2489 	}
2490 	if (bp->flags & BNXT_FLAG_DIM) {
2491 		struct dim_sample dim_sample = {};
2492 
2493 		dim_update_sample(cpr->event_ctr,
2494 				  cpr->rx_packets,
2495 				  cpr->rx_bytes,
2496 				  &dim_sample);
2497 		net_dim(&cpr->dim, dim_sample);
2498 	}
2499 	return work_done;
2500 }
2501 
__bnxt_poll_cqs(struct bnxt * bp,struct bnxt_napi * bnapi,int budget)2502 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
2503 {
2504 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2505 	int i, work_done = 0;
2506 
2507 	for (i = 0; i < 2; i++) {
2508 		struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2509 
2510 		if (cpr2) {
2511 			work_done += __bnxt_poll_work(bp, cpr2,
2512 						      budget - work_done);
2513 			cpr->has_more_work |= cpr2->has_more_work;
2514 		}
2515 	}
2516 	return work_done;
2517 }
2518 
__bnxt_poll_cqs_done(struct bnxt * bp,struct bnxt_napi * bnapi,u64 dbr_type)2519 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2520 				 u64 dbr_type)
2521 {
2522 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2523 	int i;
2524 
2525 	for (i = 0; i < 2; i++) {
2526 		struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2527 		struct bnxt_db_info *db;
2528 
2529 		if (cpr2 && cpr2->had_work_done) {
2530 			db = &cpr2->cp_db;
2531 			writeq(db->db_key64 | dbr_type |
2532 			       RING_CMP(cpr2->cp_raw_cons), db->doorbell);
2533 			cpr2->had_work_done = 0;
2534 		}
2535 	}
2536 	__bnxt_poll_work_done(bp, bnapi);
2537 }
2538 
bnxt_poll_p5(struct napi_struct * napi,int budget)2539 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
2540 {
2541 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2542 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2543 	u32 raw_cons = cpr->cp_raw_cons;
2544 	struct bnxt *bp = bnapi->bp;
2545 	struct nqe_cn *nqcmp;
2546 	int work_done = 0;
2547 	u32 cons;
2548 
2549 	if (cpr->has_more_work) {
2550 		cpr->has_more_work = 0;
2551 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
2552 	}
2553 	while (1) {
2554 		cons = RING_CMP(raw_cons);
2555 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2556 
2557 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
2558 			if (cpr->has_more_work)
2559 				break;
2560 
2561 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL);
2562 			cpr->cp_raw_cons = raw_cons;
2563 			if (napi_complete_done(napi, work_done))
2564 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
2565 						  cpr->cp_raw_cons);
2566 			return work_done;
2567 		}
2568 
2569 		/* The valid test of the entry must be done first before
2570 		 * reading any further.
2571 		 */
2572 		dma_rmb();
2573 
2574 		if (nqcmp->type == cpu_to_le16(NQ_CN_TYPE_CQ_NOTIFICATION)) {
2575 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
2576 			struct bnxt_cp_ring_info *cpr2;
2577 
2578 			/* No more budget for RX work */
2579 			if (budget && work_done >= budget && idx == BNXT_RX_HDL)
2580 				break;
2581 
2582 			cpr2 = cpr->cp_ring_arr[idx];
2583 			work_done += __bnxt_poll_work(bp, cpr2,
2584 						      budget - work_done);
2585 			cpr->has_more_work |= cpr2->has_more_work;
2586 		} else {
2587 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
2588 		}
2589 		raw_cons = NEXT_RAW_CMP(raw_cons);
2590 	}
2591 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ);
2592 	if (raw_cons != cpr->cp_raw_cons) {
2593 		cpr->cp_raw_cons = raw_cons;
2594 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
2595 	}
2596 	return work_done;
2597 }
2598 
bnxt_free_tx_skbs(struct bnxt * bp)2599 static void bnxt_free_tx_skbs(struct bnxt *bp)
2600 {
2601 	int i, max_idx;
2602 	struct pci_dev *pdev = bp->pdev;
2603 
2604 	if (!bp->tx_ring)
2605 		return;
2606 
2607 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
2608 	for (i = 0; i < bp->tx_nr_rings; i++) {
2609 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2610 		int j;
2611 
2612 		if (!txr->tx_buf_ring)
2613 			continue;
2614 
2615 		for (j = 0; j < max_idx;) {
2616 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
2617 			struct sk_buff *skb;
2618 			int k, last;
2619 
2620 			if (i < bp->tx_nr_rings_xdp &&
2621 			    tx_buf->action == XDP_REDIRECT) {
2622 				dma_unmap_single(&pdev->dev,
2623 					dma_unmap_addr(tx_buf, mapping),
2624 					dma_unmap_len(tx_buf, len),
2625 					PCI_DMA_TODEVICE);
2626 				xdp_return_frame(tx_buf->xdpf);
2627 				tx_buf->action = 0;
2628 				tx_buf->xdpf = NULL;
2629 				j++;
2630 				continue;
2631 			}
2632 
2633 			skb = tx_buf->skb;
2634 			if (!skb) {
2635 				j++;
2636 				continue;
2637 			}
2638 
2639 			tx_buf->skb = NULL;
2640 
2641 			if (tx_buf->is_push) {
2642 				dev_kfree_skb(skb);
2643 				j += 2;
2644 				continue;
2645 			}
2646 
2647 			dma_unmap_single(&pdev->dev,
2648 					 dma_unmap_addr(tx_buf, mapping),
2649 					 skb_headlen(skb),
2650 					 PCI_DMA_TODEVICE);
2651 
2652 			last = tx_buf->nr_frags;
2653 			j += 2;
2654 			for (k = 0; k < last; k++, j++) {
2655 				int ring_idx = j & bp->tx_ring_mask;
2656 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2657 
2658 				tx_buf = &txr->tx_buf_ring[ring_idx];
2659 				dma_unmap_page(
2660 					&pdev->dev,
2661 					dma_unmap_addr(tx_buf, mapping),
2662 					skb_frag_size(frag), PCI_DMA_TODEVICE);
2663 			}
2664 			dev_kfree_skb(skb);
2665 		}
2666 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
2667 	}
2668 }
2669 
bnxt_free_one_rx_ring_skbs(struct bnxt * bp,int ring_nr)2670 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr)
2671 {
2672 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
2673 	struct pci_dev *pdev = bp->pdev;
2674 	struct bnxt_tpa_idx_map *map;
2675 	int i, max_idx, max_agg_idx;
2676 
2677 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2678 	max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2679 	if (!rxr->rx_tpa)
2680 		goto skip_rx_tpa_free;
2681 
2682 	for (i = 0; i < bp->max_tpa; i++) {
2683 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
2684 		u8 *data = tpa_info->data;
2685 
2686 		if (!data)
2687 			continue;
2688 
2689 		dma_unmap_single_attrs(&pdev->dev, tpa_info->mapping,
2690 				       bp->rx_buf_use_size, bp->rx_dir,
2691 				       DMA_ATTR_WEAK_ORDERING);
2692 
2693 		tpa_info->data = NULL;
2694 
2695 		kfree(data);
2696 	}
2697 
2698 skip_rx_tpa_free:
2699 	if (!rxr->rx_buf_ring)
2700 		goto skip_rx_buf_free;
2701 
2702 	for (i = 0; i < max_idx; i++) {
2703 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
2704 		dma_addr_t mapping = rx_buf->mapping;
2705 		void *data = rx_buf->data;
2706 
2707 		if (!data)
2708 			continue;
2709 
2710 		rx_buf->data = NULL;
2711 		if (BNXT_RX_PAGE_MODE(bp)) {
2712 			mapping -= bp->rx_dma_offset;
2713 			dma_unmap_page_attrs(&pdev->dev, mapping, PAGE_SIZE,
2714 					     bp->rx_dir,
2715 					     DMA_ATTR_WEAK_ORDERING);
2716 			page_pool_recycle_direct(rxr->page_pool, data);
2717 		} else {
2718 			dma_unmap_single_attrs(&pdev->dev, mapping,
2719 					       bp->rx_buf_use_size, bp->rx_dir,
2720 					       DMA_ATTR_WEAK_ORDERING);
2721 			kfree(data);
2722 		}
2723 	}
2724 
2725 skip_rx_buf_free:
2726 	if (!rxr->rx_agg_ring)
2727 		goto skip_rx_agg_free;
2728 
2729 	for (i = 0; i < max_agg_idx; i++) {
2730 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
2731 		struct page *page = rx_agg_buf->page;
2732 
2733 		if (!page)
2734 			continue;
2735 
2736 		dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2737 				     BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
2738 				     DMA_ATTR_WEAK_ORDERING);
2739 
2740 		rx_agg_buf->page = NULL;
2741 		__clear_bit(i, rxr->rx_agg_bmap);
2742 
2743 		__free_page(page);
2744 	}
2745 
2746 skip_rx_agg_free:
2747 	if (rxr->rx_page) {
2748 		__free_page(rxr->rx_page);
2749 		rxr->rx_page = NULL;
2750 	}
2751 	map = rxr->rx_tpa_idx_map;
2752 	if (map)
2753 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
2754 }
2755 
bnxt_free_rx_skbs(struct bnxt * bp)2756 static void bnxt_free_rx_skbs(struct bnxt *bp)
2757 {
2758 	int i;
2759 
2760 	if (!bp->rx_ring)
2761 		return;
2762 
2763 	for (i = 0; i < bp->rx_nr_rings; i++)
2764 		bnxt_free_one_rx_ring_skbs(bp, i);
2765 }
2766 
bnxt_free_skbs(struct bnxt * bp)2767 static void bnxt_free_skbs(struct bnxt *bp)
2768 {
2769 	bnxt_free_tx_skbs(bp);
2770 	bnxt_free_rx_skbs(bp);
2771 }
2772 
bnxt_free_ring(struct bnxt * bp,struct bnxt_ring_mem_info * rmem)2773 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
2774 {
2775 	struct pci_dev *pdev = bp->pdev;
2776 	int i;
2777 
2778 	for (i = 0; i < rmem->nr_pages; i++) {
2779 		if (!rmem->pg_arr[i])
2780 			continue;
2781 
2782 		dma_free_coherent(&pdev->dev, rmem->page_size,
2783 				  rmem->pg_arr[i], rmem->dma_arr[i]);
2784 
2785 		rmem->pg_arr[i] = NULL;
2786 	}
2787 	if (rmem->pg_tbl) {
2788 		size_t pg_tbl_size = rmem->nr_pages * 8;
2789 
2790 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
2791 			pg_tbl_size = rmem->page_size;
2792 		dma_free_coherent(&pdev->dev, pg_tbl_size,
2793 				  rmem->pg_tbl, rmem->pg_tbl_map);
2794 		rmem->pg_tbl = NULL;
2795 	}
2796 	if (rmem->vmem_size && *rmem->vmem) {
2797 		vfree(*rmem->vmem);
2798 		*rmem->vmem = NULL;
2799 	}
2800 }
2801 
bnxt_alloc_ring(struct bnxt * bp,struct bnxt_ring_mem_info * rmem)2802 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
2803 {
2804 	struct pci_dev *pdev = bp->pdev;
2805 	u64 valid_bit = 0;
2806 	int i;
2807 
2808 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
2809 		valid_bit = PTU_PTE_VALID;
2810 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
2811 		size_t pg_tbl_size = rmem->nr_pages * 8;
2812 
2813 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
2814 			pg_tbl_size = rmem->page_size;
2815 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
2816 						  &rmem->pg_tbl_map,
2817 						  GFP_KERNEL);
2818 		if (!rmem->pg_tbl)
2819 			return -ENOMEM;
2820 	}
2821 
2822 	for (i = 0; i < rmem->nr_pages; i++) {
2823 		u64 extra_bits = valid_bit;
2824 
2825 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2826 						     rmem->page_size,
2827 						     &rmem->dma_arr[i],
2828 						     GFP_KERNEL);
2829 		if (!rmem->pg_arr[i])
2830 			return -ENOMEM;
2831 
2832 		if (rmem->init_val)
2833 			memset(rmem->pg_arr[i], rmem->init_val,
2834 			       rmem->page_size);
2835 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
2836 			if (i == rmem->nr_pages - 2 &&
2837 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2838 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
2839 			else if (i == rmem->nr_pages - 1 &&
2840 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2841 				extra_bits |= PTU_PTE_LAST;
2842 			rmem->pg_tbl[i] =
2843 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
2844 		}
2845 	}
2846 
2847 	if (rmem->vmem_size) {
2848 		*rmem->vmem = vzalloc(rmem->vmem_size);
2849 		if (!(*rmem->vmem))
2850 			return -ENOMEM;
2851 	}
2852 	return 0;
2853 }
2854 
bnxt_free_tpa_info(struct bnxt * bp)2855 static void bnxt_free_tpa_info(struct bnxt *bp)
2856 {
2857 	int i;
2858 
2859 	for (i = 0; i < bp->rx_nr_rings; i++) {
2860 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2861 
2862 		kfree(rxr->rx_tpa_idx_map);
2863 		rxr->rx_tpa_idx_map = NULL;
2864 		if (rxr->rx_tpa) {
2865 			kfree(rxr->rx_tpa[0].agg_arr);
2866 			rxr->rx_tpa[0].agg_arr = NULL;
2867 		}
2868 		kfree(rxr->rx_tpa);
2869 		rxr->rx_tpa = NULL;
2870 	}
2871 }
2872 
bnxt_alloc_tpa_info(struct bnxt * bp)2873 static int bnxt_alloc_tpa_info(struct bnxt *bp)
2874 {
2875 	int i, j, total_aggs = 0;
2876 
2877 	bp->max_tpa = MAX_TPA;
2878 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
2879 		if (!bp->max_tpa_v2)
2880 			return 0;
2881 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
2882 		total_aggs = bp->max_tpa * MAX_SKB_FRAGS;
2883 	}
2884 
2885 	for (i = 0; i < bp->rx_nr_rings; i++) {
2886 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2887 		struct rx_agg_cmp *agg;
2888 
2889 		rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
2890 				      GFP_KERNEL);
2891 		if (!rxr->rx_tpa)
2892 			return -ENOMEM;
2893 
2894 		if (!(bp->flags & BNXT_FLAG_CHIP_P5))
2895 			continue;
2896 		agg = kcalloc(total_aggs, sizeof(*agg), GFP_KERNEL);
2897 		rxr->rx_tpa[0].agg_arr = agg;
2898 		if (!agg)
2899 			return -ENOMEM;
2900 		for (j = 1; j < bp->max_tpa; j++)
2901 			rxr->rx_tpa[j].agg_arr = agg + j * MAX_SKB_FRAGS;
2902 		rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
2903 					      GFP_KERNEL);
2904 		if (!rxr->rx_tpa_idx_map)
2905 			return -ENOMEM;
2906 	}
2907 	return 0;
2908 }
2909 
bnxt_free_rx_rings(struct bnxt * bp)2910 static void bnxt_free_rx_rings(struct bnxt *bp)
2911 {
2912 	int i;
2913 
2914 	if (!bp->rx_ring)
2915 		return;
2916 
2917 	bnxt_free_tpa_info(bp);
2918 	for (i = 0; i < bp->rx_nr_rings; i++) {
2919 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2920 		struct bnxt_ring_struct *ring;
2921 
2922 		if (rxr->xdp_prog)
2923 			bpf_prog_put(rxr->xdp_prog);
2924 
2925 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
2926 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
2927 
2928 		page_pool_destroy(rxr->page_pool);
2929 		rxr->page_pool = NULL;
2930 
2931 		kfree(rxr->rx_agg_bmap);
2932 		rxr->rx_agg_bmap = NULL;
2933 
2934 		ring = &rxr->rx_ring_struct;
2935 		bnxt_free_ring(bp, &ring->ring_mem);
2936 
2937 		ring = &rxr->rx_agg_ring_struct;
2938 		bnxt_free_ring(bp, &ring->ring_mem);
2939 	}
2940 }
2941 
bnxt_alloc_rx_page_pool(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)2942 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
2943 				   struct bnxt_rx_ring_info *rxr)
2944 {
2945 	struct page_pool_params pp = { 0 };
2946 
2947 	pp.pool_size = bp->rx_ring_size;
2948 	pp.nid = dev_to_node(&bp->pdev->dev);
2949 	pp.dev = &bp->pdev->dev;
2950 	pp.dma_dir = DMA_BIDIRECTIONAL;
2951 
2952 	rxr->page_pool = page_pool_create(&pp);
2953 	if (IS_ERR(rxr->page_pool)) {
2954 		int err = PTR_ERR(rxr->page_pool);
2955 
2956 		rxr->page_pool = NULL;
2957 		return err;
2958 	}
2959 	return 0;
2960 }
2961 
bnxt_alloc_rx_rings(struct bnxt * bp)2962 static int bnxt_alloc_rx_rings(struct bnxt *bp)
2963 {
2964 	int i, rc = 0, agg_rings = 0;
2965 
2966 	if (!bp->rx_ring)
2967 		return -ENOMEM;
2968 
2969 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
2970 		agg_rings = 1;
2971 
2972 	for (i = 0; i < bp->rx_nr_rings; i++) {
2973 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2974 		struct bnxt_ring_struct *ring;
2975 
2976 		ring = &rxr->rx_ring_struct;
2977 
2978 		rc = bnxt_alloc_rx_page_pool(bp, rxr);
2979 		if (rc)
2980 			return rc;
2981 
2982 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i);
2983 		if (rc < 0)
2984 			return rc;
2985 
2986 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
2987 						MEM_TYPE_PAGE_POOL,
2988 						rxr->page_pool);
2989 		if (rc) {
2990 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
2991 			return rc;
2992 		}
2993 
2994 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
2995 		if (rc)
2996 			return rc;
2997 
2998 		ring->grp_idx = i;
2999 		if (agg_rings) {
3000 			u16 mem_size;
3001 
3002 			ring = &rxr->rx_agg_ring_struct;
3003 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3004 			if (rc)
3005 				return rc;
3006 
3007 			ring->grp_idx = i;
3008 			rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3009 			mem_size = rxr->rx_agg_bmap_size / 8;
3010 			rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3011 			if (!rxr->rx_agg_bmap)
3012 				return -ENOMEM;
3013 		}
3014 	}
3015 	if (bp->flags & BNXT_FLAG_TPA)
3016 		rc = bnxt_alloc_tpa_info(bp);
3017 	return rc;
3018 }
3019 
bnxt_free_tx_rings(struct bnxt * bp)3020 static void bnxt_free_tx_rings(struct bnxt *bp)
3021 {
3022 	int i;
3023 	struct pci_dev *pdev = bp->pdev;
3024 
3025 	if (!bp->tx_ring)
3026 		return;
3027 
3028 	for (i = 0; i < bp->tx_nr_rings; i++) {
3029 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3030 		struct bnxt_ring_struct *ring;
3031 
3032 		if (txr->tx_push) {
3033 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3034 					  txr->tx_push, txr->tx_push_mapping);
3035 			txr->tx_push = NULL;
3036 		}
3037 
3038 		ring = &txr->tx_ring_struct;
3039 
3040 		bnxt_free_ring(bp, &ring->ring_mem);
3041 	}
3042 }
3043 
bnxt_alloc_tx_rings(struct bnxt * bp)3044 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3045 {
3046 	int i, j, rc;
3047 	struct pci_dev *pdev = bp->pdev;
3048 
3049 	bp->tx_push_size = 0;
3050 	if (bp->tx_push_thresh) {
3051 		int push_size;
3052 
3053 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3054 					bp->tx_push_thresh);
3055 
3056 		if (push_size > 256) {
3057 			push_size = 0;
3058 			bp->tx_push_thresh = 0;
3059 		}
3060 
3061 		bp->tx_push_size = push_size;
3062 	}
3063 
3064 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3065 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3066 		struct bnxt_ring_struct *ring;
3067 		u8 qidx;
3068 
3069 		ring = &txr->tx_ring_struct;
3070 
3071 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3072 		if (rc)
3073 			return rc;
3074 
3075 		ring->grp_idx = txr->bnapi->index;
3076 		if (bp->tx_push_size) {
3077 			dma_addr_t mapping;
3078 
3079 			/* One pre-allocated DMA buffer to backup
3080 			 * TX push operation
3081 			 */
3082 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3083 						bp->tx_push_size,
3084 						&txr->tx_push_mapping,
3085 						GFP_KERNEL);
3086 
3087 			if (!txr->tx_push)
3088 				return -ENOMEM;
3089 
3090 			mapping = txr->tx_push_mapping +
3091 				sizeof(struct tx_push_bd);
3092 			txr->data_mapping = cpu_to_le64(mapping);
3093 		}
3094 		qidx = bp->tc_to_qidx[j];
3095 		ring->queue_id = bp->q_info[qidx].queue_id;
3096 		if (i < bp->tx_nr_rings_xdp)
3097 			continue;
3098 		if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
3099 			j++;
3100 	}
3101 	return 0;
3102 }
3103 
bnxt_free_cp_rings(struct bnxt * bp)3104 static void bnxt_free_cp_rings(struct bnxt *bp)
3105 {
3106 	int i;
3107 
3108 	if (!bp->bnapi)
3109 		return;
3110 
3111 	for (i = 0; i < bp->cp_nr_rings; i++) {
3112 		struct bnxt_napi *bnapi = bp->bnapi[i];
3113 		struct bnxt_cp_ring_info *cpr;
3114 		struct bnxt_ring_struct *ring;
3115 		int j;
3116 
3117 		if (!bnapi)
3118 			continue;
3119 
3120 		cpr = &bnapi->cp_ring;
3121 		ring = &cpr->cp_ring_struct;
3122 
3123 		bnxt_free_ring(bp, &ring->ring_mem);
3124 
3125 		for (j = 0; j < 2; j++) {
3126 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3127 
3128 			if (cpr2) {
3129 				ring = &cpr2->cp_ring_struct;
3130 				bnxt_free_ring(bp, &ring->ring_mem);
3131 				kfree(cpr2);
3132 				cpr->cp_ring_arr[j] = NULL;
3133 			}
3134 		}
3135 	}
3136 }
3137 
bnxt_alloc_cp_sub_ring(struct bnxt * bp)3138 static struct bnxt_cp_ring_info *bnxt_alloc_cp_sub_ring(struct bnxt *bp)
3139 {
3140 	struct bnxt_ring_mem_info *rmem;
3141 	struct bnxt_ring_struct *ring;
3142 	struct bnxt_cp_ring_info *cpr;
3143 	int rc;
3144 
3145 	cpr = kzalloc(sizeof(*cpr), GFP_KERNEL);
3146 	if (!cpr)
3147 		return NULL;
3148 
3149 	ring = &cpr->cp_ring_struct;
3150 	rmem = &ring->ring_mem;
3151 	rmem->nr_pages = bp->cp_nr_pages;
3152 	rmem->page_size = HW_CMPD_RING_SIZE;
3153 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
3154 	rmem->dma_arr = cpr->cp_desc_mapping;
3155 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
3156 	rc = bnxt_alloc_ring(bp, rmem);
3157 	if (rc) {
3158 		bnxt_free_ring(bp, rmem);
3159 		kfree(cpr);
3160 		cpr = NULL;
3161 	}
3162 	return cpr;
3163 }
3164 
bnxt_alloc_cp_rings(struct bnxt * bp)3165 static int bnxt_alloc_cp_rings(struct bnxt *bp)
3166 {
3167 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
3168 	int i, rc, ulp_base_vec, ulp_msix;
3169 
3170 	ulp_msix = bnxt_get_ulp_msix_num(bp);
3171 	ulp_base_vec = bnxt_get_ulp_msix_base(bp);
3172 	for (i = 0; i < bp->cp_nr_rings; i++) {
3173 		struct bnxt_napi *bnapi = bp->bnapi[i];
3174 		struct bnxt_cp_ring_info *cpr;
3175 		struct bnxt_ring_struct *ring;
3176 
3177 		if (!bnapi)
3178 			continue;
3179 
3180 		cpr = &bnapi->cp_ring;
3181 		cpr->bnapi = bnapi;
3182 		ring = &cpr->cp_ring_struct;
3183 
3184 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3185 		if (rc)
3186 			return rc;
3187 
3188 		if (ulp_msix && i >= ulp_base_vec)
3189 			ring->map_idx = i + ulp_msix;
3190 		else
3191 			ring->map_idx = i;
3192 
3193 		if (!(bp->flags & BNXT_FLAG_CHIP_P5))
3194 			continue;
3195 
3196 		if (i < bp->rx_nr_rings) {
3197 			struct bnxt_cp_ring_info *cpr2 =
3198 				bnxt_alloc_cp_sub_ring(bp);
3199 
3200 			cpr->cp_ring_arr[BNXT_RX_HDL] = cpr2;
3201 			if (!cpr2)
3202 				return -ENOMEM;
3203 			cpr2->bnapi = bnapi;
3204 		}
3205 		if ((sh && i < bp->tx_nr_rings) ||
3206 		    (!sh && i >= bp->rx_nr_rings)) {
3207 			struct bnxt_cp_ring_info *cpr2 =
3208 				bnxt_alloc_cp_sub_ring(bp);
3209 
3210 			cpr->cp_ring_arr[BNXT_TX_HDL] = cpr2;
3211 			if (!cpr2)
3212 				return -ENOMEM;
3213 			cpr2->bnapi = bnapi;
3214 		}
3215 	}
3216 	return 0;
3217 }
3218 
bnxt_init_ring_struct(struct bnxt * bp)3219 static void bnxt_init_ring_struct(struct bnxt *bp)
3220 {
3221 	int i;
3222 
3223 	for (i = 0; i < bp->cp_nr_rings; i++) {
3224 		struct bnxt_napi *bnapi = bp->bnapi[i];
3225 		struct bnxt_ring_mem_info *rmem;
3226 		struct bnxt_cp_ring_info *cpr;
3227 		struct bnxt_rx_ring_info *rxr;
3228 		struct bnxt_tx_ring_info *txr;
3229 		struct bnxt_ring_struct *ring;
3230 
3231 		if (!bnapi)
3232 			continue;
3233 
3234 		cpr = &bnapi->cp_ring;
3235 		ring = &cpr->cp_ring_struct;
3236 		rmem = &ring->ring_mem;
3237 		rmem->nr_pages = bp->cp_nr_pages;
3238 		rmem->page_size = HW_CMPD_RING_SIZE;
3239 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
3240 		rmem->dma_arr = cpr->cp_desc_mapping;
3241 		rmem->vmem_size = 0;
3242 
3243 		rxr = bnapi->rx_ring;
3244 		if (!rxr)
3245 			goto skip_rx;
3246 
3247 		ring = &rxr->rx_ring_struct;
3248 		rmem = &ring->ring_mem;
3249 		rmem->nr_pages = bp->rx_nr_pages;
3250 		rmem->page_size = HW_RXBD_RING_SIZE;
3251 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
3252 		rmem->dma_arr = rxr->rx_desc_mapping;
3253 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
3254 		rmem->vmem = (void **)&rxr->rx_buf_ring;
3255 
3256 		ring = &rxr->rx_agg_ring_struct;
3257 		rmem = &ring->ring_mem;
3258 		rmem->nr_pages = bp->rx_agg_nr_pages;
3259 		rmem->page_size = HW_RXBD_RING_SIZE;
3260 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
3261 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
3262 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
3263 		rmem->vmem = (void **)&rxr->rx_agg_ring;
3264 
3265 skip_rx:
3266 		txr = bnapi->tx_ring;
3267 		if (!txr)
3268 			continue;
3269 
3270 		ring = &txr->tx_ring_struct;
3271 		rmem = &ring->ring_mem;
3272 		rmem->nr_pages = bp->tx_nr_pages;
3273 		rmem->page_size = HW_RXBD_RING_SIZE;
3274 		rmem->pg_arr = (void **)txr->tx_desc_ring;
3275 		rmem->dma_arr = txr->tx_desc_mapping;
3276 		rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
3277 		rmem->vmem = (void **)&txr->tx_buf_ring;
3278 	}
3279 }
3280 
bnxt_init_rxbd_pages(struct bnxt_ring_struct * ring,u32 type)3281 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
3282 {
3283 	int i;
3284 	u32 prod;
3285 	struct rx_bd **rx_buf_ring;
3286 
3287 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
3288 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
3289 		int j;
3290 		struct rx_bd *rxbd;
3291 
3292 		rxbd = rx_buf_ring[i];
3293 		if (!rxbd)
3294 			continue;
3295 
3296 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
3297 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
3298 			rxbd->rx_bd_opaque = prod;
3299 		}
3300 	}
3301 }
3302 
bnxt_alloc_one_rx_ring(struct bnxt * bp,int ring_nr)3303 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
3304 {
3305 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
3306 	struct net_device *dev = bp->dev;
3307 	u32 prod;
3308 	int i;
3309 
3310 	prod = rxr->rx_prod;
3311 	for (i = 0; i < bp->rx_ring_size; i++) {
3312 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
3313 			netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
3314 				    ring_nr, i, bp->rx_ring_size);
3315 			break;
3316 		}
3317 		prod = NEXT_RX(prod);
3318 	}
3319 	rxr->rx_prod = prod;
3320 
3321 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
3322 		return 0;
3323 
3324 	prod = rxr->rx_agg_prod;
3325 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
3326 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
3327 			netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
3328 				    ring_nr, i, bp->rx_ring_size);
3329 			break;
3330 		}
3331 		prod = NEXT_RX_AGG(prod);
3332 	}
3333 	rxr->rx_agg_prod = prod;
3334 
3335 	if (rxr->rx_tpa) {
3336 		dma_addr_t mapping;
3337 		u8 *data;
3338 
3339 		for (i = 0; i < bp->max_tpa; i++) {
3340 			data = __bnxt_alloc_rx_data(bp, &mapping, GFP_KERNEL);
3341 			if (!data)
3342 				return -ENOMEM;
3343 
3344 			rxr->rx_tpa[i].data = data;
3345 			rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
3346 			rxr->rx_tpa[i].mapping = mapping;
3347 		}
3348 	}
3349 	return 0;
3350 }
3351 
bnxt_init_one_rx_ring(struct bnxt * bp,int ring_nr)3352 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
3353 {
3354 	struct bnxt_rx_ring_info *rxr;
3355 	struct bnxt_ring_struct *ring;
3356 	u32 type;
3357 
3358 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
3359 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
3360 
3361 	if (NET_IP_ALIGN == 2)
3362 		type |= RX_BD_FLAGS_SOP;
3363 
3364 	rxr = &bp->rx_ring[ring_nr];
3365 	ring = &rxr->rx_ring_struct;
3366 	bnxt_init_rxbd_pages(ring, type);
3367 
3368 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
3369 		bpf_prog_add(bp->xdp_prog, 1);
3370 		rxr->xdp_prog = bp->xdp_prog;
3371 	}
3372 	ring->fw_ring_id = INVALID_HW_RING_ID;
3373 
3374 	ring = &rxr->rx_agg_ring_struct;
3375 	ring->fw_ring_id = INVALID_HW_RING_ID;
3376 
3377 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
3378 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
3379 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
3380 
3381 		bnxt_init_rxbd_pages(ring, type);
3382 	}
3383 
3384 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
3385 }
3386 
bnxt_init_cp_rings(struct bnxt * bp)3387 static void bnxt_init_cp_rings(struct bnxt *bp)
3388 {
3389 	int i, j;
3390 
3391 	for (i = 0; i < bp->cp_nr_rings; i++) {
3392 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
3393 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3394 
3395 		ring->fw_ring_id = INVALID_HW_RING_ID;
3396 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3397 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3398 		for (j = 0; j < 2; j++) {
3399 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3400 
3401 			if (!cpr2)
3402 				continue;
3403 
3404 			ring = &cpr2->cp_ring_struct;
3405 			ring->fw_ring_id = INVALID_HW_RING_ID;
3406 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3407 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3408 		}
3409 	}
3410 }
3411 
bnxt_init_rx_rings(struct bnxt * bp)3412 static int bnxt_init_rx_rings(struct bnxt *bp)
3413 {
3414 	int i, rc = 0;
3415 
3416 	if (BNXT_RX_PAGE_MODE(bp)) {
3417 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
3418 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
3419 	} else {
3420 		bp->rx_offset = BNXT_RX_OFFSET;
3421 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
3422 	}
3423 
3424 	for (i = 0; i < bp->rx_nr_rings; i++) {
3425 		rc = bnxt_init_one_rx_ring(bp, i);
3426 		if (rc)
3427 			break;
3428 	}
3429 
3430 	return rc;
3431 }
3432 
bnxt_init_tx_rings(struct bnxt * bp)3433 static int bnxt_init_tx_rings(struct bnxt *bp)
3434 {
3435 	u16 i;
3436 
3437 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
3438 				   BNXT_MIN_TX_DESC_CNT);
3439 
3440 	for (i = 0; i < bp->tx_nr_rings; i++) {
3441 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3442 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
3443 
3444 		ring->fw_ring_id = INVALID_HW_RING_ID;
3445 	}
3446 
3447 	return 0;
3448 }
3449 
bnxt_free_ring_grps(struct bnxt * bp)3450 static void bnxt_free_ring_grps(struct bnxt *bp)
3451 {
3452 	kfree(bp->grp_info);
3453 	bp->grp_info = NULL;
3454 }
3455 
bnxt_init_ring_grps(struct bnxt * bp,bool irq_re_init)3456 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
3457 {
3458 	int i;
3459 
3460 	if (irq_re_init) {
3461 		bp->grp_info = kcalloc(bp->cp_nr_rings,
3462 				       sizeof(struct bnxt_ring_grp_info),
3463 				       GFP_KERNEL);
3464 		if (!bp->grp_info)
3465 			return -ENOMEM;
3466 	}
3467 	for (i = 0; i < bp->cp_nr_rings; i++) {
3468 		if (irq_re_init)
3469 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
3470 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3471 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
3472 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
3473 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
3474 	}
3475 	return 0;
3476 }
3477 
bnxt_free_vnics(struct bnxt * bp)3478 static void bnxt_free_vnics(struct bnxt *bp)
3479 {
3480 	kfree(bp->vnic_info);
3481 	bp->vnic_info = NULL;
3482 	bp->nr_vnics = 0;
3483 }
3484 
bnxt_alloc_vnics(struct bnxt * bp)3485 static int bnxt_alloc_vnics(struct bnxt *bp)
3486 {
3487 	int num_vnics = 1;
3488 
3489 #ifdef CONFIG_RFS_ACCEL
3490 	if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
3491 		num_vnics += bp->rx_nr_rings;
3492 #endif
3493 
3494 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3495 		num_vnics++;
3496 
3497 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
3498 				GFP_KERNEL);
3499 	if (!bp->vnic_info)
3500 		return -ENOMEM;
3501 
3502 	bp->nr_vnics = num_vnics;
3503 	return 0;
3504 }
3505 
bnxt_init_vnics(struct bnxt * bp)3506 static void bnxt_init_vnics(struct bnxt *bp)
3507 {
3508 	int i;
3509 
3510 	for (i = 0; i < bp->nr_vnics; i++) {
3511 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3512 		int j;
3513 
3514 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
3515 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
3516 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
3517 
3518 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
3519 
3520 		if (bp->vnic_info[i].rss_hash_key) {
3521 			if (i == 0)
3522 				prandom_bytes(vnic->rss_hash_key,
3523 					      HW_HASH_KEY_SIZE);
3524 			else
3525 				memcpy(vnic->rss_hash_key,
3526 				       bp->vnic_info[0].rss_hash_key,
3527 				       HW_HASH_KEY_SIZE);
3528 		}
3529 	}
3530 }
3531 
bnxt_calc_nr_ring_pages(u32 ring_size,int desc_per_pg)3532 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
3533 {
3534 	int pages;
3535 
3536 	pages = ring_size / desc_per_pg;
3537 
3538 	if (!pages)
3539 		return 1;
3540 
3541 	pages++;
3542 
3543 	while (pages & (pages - 1))
3544 		pages++;
3545 
3546 	return pages;
3547 }
3548 
bnxt_set_tpa_flags(struct bnxt * bp)3549 void bnxt_set_tpa_flags(struct bnxt *bp)
3550 {
3551 	bp->flags &= ~BNXT_FLAG_TPA;
3552 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
3553 		return;
3554 	if (bp->dev->features & NETIF_F_LRO)
3555 		bp->flags |= BNXT_FLAG_LRO;
3556 	else if (bp->dev->features & NETIF_F_GRO_HW)
3557 		bp->flags |= BNXT_FLAG_GRO;
3558 }
3559 
3560 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
3561  * be set on entry.
3562  */
bnxt_set_ring_params(struct bnxt * bp)3563 void bnxt_set_ring_params(struct bnxt *bp)
3564 {
3565 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
3566 	u32 agg_factor = 0, agg_ring_size = 0;
3567 
3568 	/* 8 for CRC and VLAN */
3569 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
3570 
3571 	rx_space = rx_size + NET_SKB_PAD +
3572 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3573 
3574 	bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
3575 	ring_size = bp->rx_ring_size;
3576 	bp->rx_agg_ring_size = 0;
3577 	bp->rx_agg_nr_pages = 0;
3578 
3579 	if (bp->flags & BNXT_FLAG_TPA)
3580 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
3581 
3582 	bp->flags &= ~BNXT_FLAG_JUMBO;
3583 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
3584 		u32 jumbo_factor;
3585 
3586 		bp->flags |= BNXT_FLAG_JUMBO;
3587 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
3588 		if (jumbo_factor > agg_factor)
3589 			agg_factor = jumbo_factor;
3590 	}
3591 	agg_ring_size = ring_size * agg_factor;
3592 
3593 	if (agg_ring_size) {
3594 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
3595 							RX_DESC_CNT);
3596 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
3597 			u32 tmp = agg_ring_size;
3598 
3599 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
3600 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
3601 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
3602 				    tmp, agg_ring_size);
3603 		}
3604 		bp->rx_agg_ring_size = agg_ring_size;
3605 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
3606 		rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
3607 		rx_space = rx_size + NET_SKB_PAD +
3608 			SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3609 	}
3610 
3611 	bp->rx_buf_use_size = rx_size;
3612 	bp->rx_buf_size = rx_space;
3613 
3614 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
3615 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
3616 
3617 	ring_size = bp->tx_ring_size;
3618 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
3619 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
3620 
3621 	max_rx_cmpl = bp->rx_ring_size;
3622 	/* MAX TPA needs to be added because TPA_START completions are
3623 	 * immediately recycled, so the TPA completions are not bound by
3624 	 * the RX ring size.
3625 	 */
3626 	if (bp->flags & BNXT_FLAG_TPA)
3627 		max_rx_cmpl += bp->max_tpa;
3628 	/* RX and TPA completions are 32-byte, all others are 16-byte */
3629 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
3630 	bp->cp_ring_size = ring_size;
3631 
3632 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
3633 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
3634 		bp->cp_nr_pages = MAX_CP_PAGES;
3635 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
3636 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
3637 			    ring_size, bp->cp_ring_size);
3638 	}
3639 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
3640 	bp->cp_ring_mask = bp->cp_bit - 1;
3641 }
3642 
3643 /* Changing allocation mode of RX rings.
3644  * TODO: Update when extending xdp_rxq_info to support allocation modes.
3645  */
bnxt_set_rx_skb_mode(struct bnxt * bp,bool page_mode)3646 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
3647 {
3648 	if (page_mode) {
3649 		if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
3650 			return -EOPNOTSUPP;
3651 		bp->dev->max_mtu =
3652 			min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
3653 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
3654 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
3655 		bp->rx_dir = DMA_BIDIRECTIONAL;
3656 		bp->rx_skb_func = bnxt_rx_page_skb;
3657 		/* Disable LRO or GRO_HW */
3658 		netdev_update_features(bp->dev);
3659 	} else {
3660 		bp->dev->max_mtu = bp->max_mtu;
3661 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
3662 		bp->rx_dir = DMA_FROM_DEVICE;
3663 		bp->rx_skb_func = bnxt_rx_skb;
3664 	}
3665 	return 0;
3666 }
3667 
bnxt_free_vnic_attributes(struct bnxt * bp)3668 static void bnxt_free_vnic_attributes(struct bnxt *bp)
3669 {
3670 	int i;
3671 	struct bnxt_vnic_info *vnic;
3672 	struct pci_dev *pdev = bp->pdev;
3673 
3674 	if (!bp->vnic_info)
3675 		return;
3676 
3677 	for (i = 0; i < bp->nr_vnics; i++) {
3678 		vnic = &bp->vnic_info[i];
3679 
3680 		kfree(vnic->fw_grp_ids);
3681 		vnic->fw_grp_ids = NULL;
3682 
3683 		kfree(vnic->uc_list);
3684 		vnic->uc_list = NULL;
3685 
3686 		if (vnic->mc_list) {
3687 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
3688 					  vnic->mc_list, vnic->mc_list_mapping);
3689 			vnic->mc_list = NULL;
3690 		}
3691 
3692 		if (vnic->rss_table) {
3693 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
3694 					  vnic->rss_table,
3695 					  vnic->rss_table_dma_addr);
3696 			vnic->rss_table = NULL;
3697 		}
3698 
3699 		vnic->rss_hash_key = NULL;
3700 		vnic->flags = 0;
3701 	}
3702 }
3703 
bnxt_alloc_vnic_attributes(struct bnxt * bp)3704 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
3705 {
3706 	int i, rc = 0, size;
3707 	struct bnxt_vnic_info *vnic;
3708 	struct pci_dev *pdev = bp->pdev;
3709 	int max_rings;
3710 
3711 	for (i = 0; i < bp->nr_vnics; i++) {
3712 		vnic = &bp->vnic_info[i];
3713 
3714 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
3715 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
3716 
3717 			if (mem_size > 0) {
3718 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
3719 				if (!vnic->uc_list) {
3720 					rc = -ENOMEM;
3721 					goto out;
3722 				}
3723 			}
3724 		}
3725 
3726 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
3727 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
3728 			vnic->mc_list =
3729 				dma_alloc_coherent(&pdev->dev,
3730 						   vnic->mc_list_size,
3731 						   &vnic->mc_list_mapping,
3732 						   GFP_KERNEL);
3733 			if (!vnic->mc_list) {
3734 				rc = -ENOMEM;
3735 				goto out;
3736 			}
3737 		}
3738 
3739 		if (bp->flags & BNXT_FLAG_CHIP_P5)
3740 			goto vnic_skip_grps;
3741 
3742 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
3743 			max_rings = bp->rx_nr_rings;
3744 		else
3745 			max_rings = 1;
3746 
3747 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
3748 		if (!vnic->fw_grp_ids) {
3749 			rc = -ENOMEM;
3750 			goto out;
3751 		}
3752 vnic_skip_grps:
3753 		if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
3754 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
3755 			continue;
3756 
3757 		/* Allocate rss table and hash key */
3758 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
3759 		if (bp->flags & BNXT_FLAG_CHIP_P5)
3760 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
3761 
3762 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
3763 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
3764 						     vnic->rss_table_size,
3765 						     &vnic->rss_table_dma_addr,
3766 						     GFP_KERNEL);
3767 		if (!vnic->rss_table) {
3768 			rc = -ENOMEM;
3769 			goto out;
3770 		}
3771 
3772 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
3773 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
3774 	}
3775 	return 0;
3776 
3777 out:
3778 	return rc;
3779 }
3780 
bnxt_free_hwrm_resources(struct bnxt * bp)3781 static void bnxt_free_hwrm_resources(struct bnxt *bp)
3782 {
3783 	struct pci_dev *pdev = bp->pdev;
3784 
3785 	if (bp->hwrm_cmd_resp_addr) {
3786 		dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
3787 				  bp->hwrm_cmd_resp_dma_addr);
3788 		bp->hwrm_cmd_resp_addr = NULL;
3789 	}
3790 
3791 	if (bp->hwrm_cmd_kong_resp_addr) {
3792 		dma_free_coherent(&pdev->dev, PAGE_SIZE,
3793 				  bp->hwrm_cmd_kong_resp_addr,
3794 				  bp->hwrm_cmd_kong_resp_dma_addr);
3795 		bp->hwrm_cmd_kong_resp_addr = NULL;
3796 	}
3797 }
3798 
bnxt_alloc_kong_hwrm_resources(struct bnxt * bp)3799 static int bnxt_alloc_kong_hwrm_resources(struct bnxt *bp)
3800 {
3801 	struct pci_dev *pdev = bp->pdev;
3802 
3803 	if (bp->hwrm_cmd_kong_resp_addr)
3804 		return 0;
3805 
3806 	bp->hwrm_cmd_kong_resp_addr =
3807 		dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3808 				   &bp->hwrm_cmd_kong_resp_dma_addr,
3809 				   GFP_KERNEL);
3810 	if (!bp->hwrm_cmd_kong_resp_addr)
3811 		return -ENOMEM;
3812 
3813 	return 0;
3814 }
3815 
bnxt_alloc_hwrm_resources(struct bnxt * bp)3816 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
3817 {
3818 	struct pci_dev *pdev = bp->pdev;
3819 
3820 	bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3821 						   &bp->hwrm_cmd_resp_dma_addr,
3822 						   GFP_KERNEL);
3823 	if (!bp->hwrm_cmd_resp_addr)
3824 		return -ENOMEM;
3825 
3826 	return 0;
3827 }
3828 
bnxt_free_hwrm_short_cmd_req(struct bnxt * bp)3829 static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
3830 {
3831 	if (bp->hwrm_short_cmd_req_addr) {
3832 		struct pci_dev *pdev = bp->pdev;
3833 
3834 		dma_free_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
3835 				  bp->hwrm_short_cmd_req_addr,
3836 				  bp->hwrm_short_cmd_req_dma_addr);
3837 		bp->hwrm_short_cmd_req_addr = NULL;
3838 	}
3839 }
3840 
bnxt_alloc_hwrm_short_cmd_req(struct bnxt * bp)3841 static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
3842 {
3843 	struct pci_dev *pdev = bp->pdev;
3844 
3845 	if (bp->hwrm_short_cmd_req_addr)
3846 		return 0;
3847 
3848 	bp->hwrm_short_cmd_req_addr =
3849 		dma_alloc_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
3850 				   &bp->hwrm_short_cmd_req_dma_addr,
3851 				   GFP_KERNEL);
3852 	if (!bp->hwrm_short_cmd_req_addr)
3853 		return -ENOMEM;
3854 
3855 	return 0;
3856 }
3857 
bnxt_free_stats_mem(struct bnxt * bp,struct bnxt_stats_mem * stats)3858 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
3859 {
3860 	kfree(stats->hw_masks);
3861 	stats->hw_masks = NULL;
3862 	kfree(stats->sw_stats);
3863 	stats->sw_stats = NULL;
3864 	if (stats->hw_stats) {
3865 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
3866 				  stats->hw_stats_map);
3867 		stats->hw_stats = NULL;
3868 	}
3869 }
3870 
bnxt_alloc_stats_mem(struct bnxt * bp,struct bnxt_stats_mem * stats,bool alloc_masks)3871 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
3872 				bool alloc_masks)
3873 {
3874 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
3875 					     &stats->hw_stats_map, GFP_KERNEL);
3876 	if (!stats->hw_stats)
3877 		return -ENOMEM;
3878 
3879 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
3880 	if (!stats->sw_stats)
3881 		goto stats_mem_err;
3882 
3883 	if (alloc_masks) {
3884 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
3885 		if (!stats->hw_masks)
3886 			goto stats_mem_err;
3887 	}
3888 	return 0;
3889 
3890 stats_mem_err:
3891 	bnxt_free_stats_mem(bp, stats);
3892 	return -ENOMEM;
3893 }
3894 
bnxt_fill_masks(u64 * mask_arr,u64 mask,int count)3895 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
3896 {
3897 	int i;
3898 
3899 	for (i = 0; i < count; i++)
3900 		mask_arr[i] = mask;
3901 }
3902 
bnxt_copy_hw_masks(u64 * mask_arr,__le64 * hw_mask_arr,int count)3903 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
3904 {
3905 	int i;
3906 
3907 	for (i = 0; i < count; i++)
3908 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
3909 }
3910 
bnxt_hwrm_func_qstat_ext(struct bnxt * bp,struct bnxt_stats_mem * stats)3911 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
3912 				    struct bnxt_stats_mem *stats)
3913 {
3914 	struct hwrm_func_qstats_ext_output *resp = bp->hwrm_cmd_resp_addr;
3915 	struct hwrm_func_qstats_ext_input req = {0};
3916 	__le64 *hw_masks;
3917 	int rc;
3918 
3919 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
3920 	    !(bp->flags & BNXT_FLAG_CHIP_P5))
3921 		return -EOPNOTSUPP;
3922 
3923 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QSTATS_EXT, -1, -1);
3924 	req.fid = cpu_to_le16(0xffff);
3925 	req.flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
3926 	mutex_lock(&bp->hwrm_cmd_lock);
3927 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3928 	if (rc)
3929 		goto qstat_exit;
3930 
3931 	hw_masks = &resp->rx_ucast_pkts;
3932 	bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
3933 
3934 qstat_exit:
3935 	mutex_unlock(&bp->hwrm_cmd_lock);
3936 	return rc;
3937 }
3938 
3939 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
3940 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
3941 
bnxt_init_stats(struct bnxt * bp)3942 static void bnxt_init_stats(struct bnxt *bp)
3943 {
3944 	struct bnxt_napi *bnapi = bp->bnapi[0];
3945 	struct bnxt_cp_ring_info *cpr;
3946 	struct bnxt_stats_mem *stats;
3947 	__le64 *rx_stats, *tx_stats;
3948 	int rc, rx_count, tx_count;
3949 	u64 *rx_masks, *tx_masks;
3950 	u64 mask;
3951 	u8 flags;
3952 
3953 	cpr = &bnapi->cp_ring;
3954 	stats = &cpr->stats;
3955 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
3956 	if (rc) {
3957 		if (bp->flags & BNXT_FLAG_CHIP_P5)
3958 			mask = (1ULL << 48) - 1;
3959 		else
3960 			mask = -1ULL;
3961 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
3962 	}
3963 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
3964 		stats = &bp->port_stats;
3965 		rx_stats = stats->hw_stats;
3966 		rx_masks = stats->hw_masks;
3967 		rx_count = sizeof(struct rx_port_stats) / 8;
3968 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
3969 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
3970 		tx_count = sizeof(struct tx_port_stats) / 8;
3971 
3972 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
3973 		rc = bnxt_hwrm_port_qstats(bp, flags);
3974 		if (rc) {
3975 			mask = (1ULL << 40) - 1;
3976 
3977 			bnxt_fill_masks(rx_masks, mask, rx_count);
3978 			bnxt_fill_masks(tx_masks, mask, tx_count);
3979 		} else {
3980 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
3981 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
3982 			bnxt_hwrm_port_qstats(bp, 0);
3983 		}
3984 	}
3985 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
3986 		stats = &bp->rx_port_stats_ext;
3987 		rx_stats = stats->hw_stats;
3988 		rx_masks = stats->hw_masks;
3989 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
3990 		stats = &bp->tx_port_stats_ext;
3991 		tx_stats = stats->hw_stats;
3992 		tx_masks = stats->hw_masks;
3993 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
3994 
3995 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
3996 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
3997 		if (rc) {
3998 			mask = (1ULL << 40) - 1;
3999 
4000 			bnxt_fill_masks(rx_masks, mask, rx_count);
4001 			if (tx_stats)
4002 				bnxt_fill_masks(tx_masks, mask, tx_count);
4003 		} else {
4004 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4005 			if (tx_stats)
4006 				bnxt_copy_hw_masks(tx_masks, tx_stats,
4007 						   tx_count);
4008 			bnxt_hwrm_port_qstats_ext(bp, 0);
4009 		}
4010 	}
4011 }
4012 
bnxt_free_port_stats(struct bnxt * bp)4013 static void bnxt_free_port_stats(struct bnxt *bp)
4014 {
4015 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
4016 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
4017 
4018 	bnxt_free_stats_mem(bp, &bp->port_stats);
4019 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
4020 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
4021 }
4022 
bnxt_free_ring_stats(struct bnxt * bp)4023 static void bnxt_free_ring_stats(struct bnxt *bp)
4024 {
4025 	int i;
4026 
4027 	if (!bp->bnapi)
4028 		return;
4029 
4030 	for (i = 0; i < bp->cp_nr_rings; i++) {
4031 		struct bnxt_napi *bnapi = bp->bnapi[i];
4032 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4033 
4034 		bnxt_free_stats_mem(bp, &cpr->stats);
4035 	}
4036 }
4037 
bnxt_alloc_stats(struct bnxt * bp)4038 static int bnxt_alloc_stats(struct bnxt *bp)
4039 {
4040 	u32 size, i;
4041 	int rc;
4042 
4043 	size = bp->hw_ring_stats_size;
4044 
4045 	for (i = 0; i < bp->cp_nr_rings; i++) {
4046 		struct bnxt_napi *bnapi = bp->bnapi[i];
4047 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4048 
4049 		cpr->stats.len = size;
4050 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
4051 		if (rc)
4052 			return rc;
4053 
4054 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4055 	}
4056 
4057 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
4058 		return 0;
4059 
4060 	if (bp->port_stats.hw_stats)
4061 		goto alloc_ext_stats;
4062 
4063 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
4064 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
4065 	if (rc)
4066 		return rc;
4067 
4068 	bp->flags |= BNXT_FLAG_PORT_STATS;
4069 
4070 alloc_ext_stats:
4071 	/* Display extended statistics only if FW supports it */
4072 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
4073 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
4074 			return 0;
4075 
4076 	if (bp->rx_port_stats_ext.hw_stats)
4077 		goto alloc_tx_ext_stats;
4078 
4079 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
4080 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
4081 	/* Extended stats are optional */
4082 	if (rc)
4083 		return 0;
4084 
4085 alloc_tx_ext_stats:
4086 	if (bp->tx_port_stats_ext.hw_stats)
4087 		return 0;
4088 
4089 	if (bp->hwrm_spec_code >= 0x10902 ||
4090 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
4091 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
4092 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
4093 		/* Extended stats are optional */
4094 		if (rc)
4095 			return 0;
4096 	}
4097 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
4098 	return 0;
4099 }
4100 
bnxt_clear_ring_indices(struct bnxt * bp)4101 static void bnxt_clear_ring_indices(struct bnxt *bp)
4102 {
4103 	int i;
4104 
4105 	if (!bp->bnapi)
4106 		return;
4107 
4108 	for (i = 0; i < bp->cp_nr_rings; i++) {
4109 		struct bnxt_napi *bnapi = bp->bnapi[i];
4110 		struct bnxt_cp_ring_info *cpr;
4111 		struct bnxt_rx_ring_info *rxr;
4112 		struct bnxt_tx_ring_info *txr;
4113 
4114 		if (!bnapi)
4115 			continue;
4116 
4117 		cpr = &bnapi->cp_ring;
4118 		cpr->cp_raw_cons = 0;
4119 
4120 		txr = bnapi->tx_ring;
4121 		if (txr) {
4122 			txr->tx_prod = 0;
4123 			txr->tx_cons = 0;
4124 		}
4125 
4126 		rxr = bnapi->rx_ring;
4127 		if (rxr) {
4128 			rxr->rx_prod = 0;
4129 			rxr->rx_agg_prod = 0;
4130 			rxr->rx_sw_agg_prod = 0;
4131 			rxr->rx_next_cons = 0;
4132 		}
4133 	}
4134 }
4135 
bnxt_free_ntp_fltrs(struct bnxt * bp,bool irq_reinit)4136 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
4137 {
4138 #ifdef CONFIG_RFS_ACCEL
4139 	int i;
4140 
4141 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
4142 	 * safe to delete the hash table.
4143 	 */
4144 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
4145 		struct hlist_head *head;
4146 		struct hlist_node *tmp;
4147 		struct bnxt_ntuple_filter *fltr;
4148 
4149 		head = &bp->ntp_fltr_hash_tbl[i];
4150 		hlist_for_each_entry_safe(fltr, tmp, head, hash) {
4151 			hlist_del(&fltr->hash);
4152 			kfree(fltr);
4153 		}
4154 	}
4155 	if (irq_reinit) {
4156 		kfree(bp->ntp_fltr_bmap);
4157 		bp->ntp_fltr_bmap = NULL;
4158 	}
4159 	bp->ntp_fltr_count = 0;
4160 #endif
4161 }
4162 
bnxt_alloc_ntp_fltrs(struct bnxt * bp)4163 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
4164 {
4165 #ifdef CONFIG_RFS_ACCEL
4166 	int i, rc = 0;
4167 
4168 	if (!(bp->flags & BNXT_FLAG_RFS))
4169 		return 0;
4170 
4171 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
4172 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
4173 
4174 	bp->ntp_fltr_count = 0;
4175 	bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
4176 				    sizeof(long),
4177 				    GFP_KERNEL);
4178 
4179 	if (!bp->ntp_fltr_bmap)
4180 		rc = -ENOMEM;
4181 
4182 	return rc;
4183 #else
4184 	return 0;
4185 #endif
4186 }
4187 
bnxt_free_mem(struct bnxt * bp,bool irq_re_init)4188 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
4189 {
4190 	bnxt_free_vnic_attributes(bp);
4191 	bnxt_free_tx_rings(bp);
4192 	bnxt_free_rx_rings(bp);
4193 	bnxt_free_cp_rings(bp);
4194 	bnxt_free_ntp_fltrs(bp, irq_re_init);
4195 	if (irq_re_init) {
4196 		bnxt_free_ring_stats(bp);
4197 		if (!(bp->fw_cap & BNXT_FW_CAP_PORT_STATS_NO_RESET) ||
4198 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
4199 			bnxt_free_port_stats(bp);
4200 		bnxt_free_ring_grps(bp);
4201 		bnxt_free_vnics(bp);
4202 		kfree(bp->tx_ring_map);
4203 		bp->tx_ring_map = NULL;
4204 		kfree(bp->tx_ring);
4205 		bp->tx_ring = NULL;
4206 		kfree(bp->rx_ring);
4207 		bp->rx_ring = NULL;
4208 		kfree(bp->bnapi);
4209 		bp->bnapi = NULL;
4210 	} else {
4211 		bnxt_clear_ring_indices(bp);
4212 	}
4213 }
4214 
bnxt_alloc_mem(struct bnxt * bp,bool irq_re_init)4215 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
4216 {
4217 	int i, j, rc, size, arr_size;
4218 	void *bnapi;
4219 
4220 	if (irq_re_init) {
4221 		/* Allocate bnapi mem pointer array and mem block for
4222 		 * all queues
4223 		 */
4224 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
4225 				bp->cp_nr_rings);
4226 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
4227 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
4228 		if (!bnapi)
4229 			return -ENOMEM;
4230 
4231 		bp->bnapi = bnapi;
4232 		bnapi += arr_size;
4233 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
4234 			bp->bnapi[i] = bnapi;
4235 			bp->bnapi[i]->index = i;
4236 			bp->bnapi[i]->bp = bp;
4237 			if (bp->flags & BNXT_FLAG_CHIP_P5) {
4238 				struct bnxt_cp_ring_info *cpr =
4239 					&bp->bnapi[i]->cp_ring;
4240 
4241 				cpr->cp_ring_struct.ring_mem.flags =
4242 					BNXT_RMEM_RING_PTE_FLAG;
4243 			}
4244 		}
4245 
4246 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
4247 				      sizeof(struct bnxt_rx_ring_info),
4248 				      GFP_KERNEL);
4249 		if (!bp->rx_ring)
4250 			return -ENOMEM;
4251 
4252 		for (i = 0; i < bp->rx_nr_rings; i++) {
4253 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4254 
4255 			if (bp->flags & BNXT_FLAG_CHIP_P5) {
4256 				rxr->rx_ring_struct.ring_mem.flags =
4257 					BNXT_RMEM_RING_PTE_FLAG;
4258 				rxr->rx_agg_ring_struct.ring_mem.flags =
4259 					BNXT_RMEM_RING_PTE_FLAG;
4260 			}
4261 			rxr->bnapi = bp->bnapi[i];
4262 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
4263 		}
4264 
4265 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
4266 				      sizeof(struct bnxt_tx_ring_info),
4267 				      GFP_KERNEL);
4268 		if (!bp->tx_ring)
4269 			return -ENOMEM;
4270 
4271 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
4272 					  GFP_KERNEL);
4273 
4274 		if (!bp->tx_ring_map)
4275 			return -ENOMEM;
4276 
4277 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4278 			j = 0;
4279 		else
4280 			j = bp->rx_nr_rings;
4281 
4282 		for (i = 0; i < bp->tx_nr_rings; i++, j++) {
4283 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4284 
4285 			if (bp->flags & BNXT_FLAG_CHIP_P5)
4286 				txr->tx_ring_struct.ring_mem.flags =
4287 					BNXT_RMEM_RING_PTE_FLAG;
4288 			txr->bnapi = bp->bnapi[j];
4289 			bp->bnapi[j]->tx_ring = txr;
4290 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
4291 			if (i >= bp->tx_nr_rings_xdp) {
4292 				txr->txq_index = i - bp->tx_nr_rings_xdp;
4293 				bp->bnapi[j]->tx_int = bnxt_tx_int;
4294 			} else {
4295 				bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
4296 				bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
4297 			}
4298 		}
4299 
4300 		rc = bnxt_alloc_stats(bp);
4301 		if (rc)
4302 			goto alloc_mem_err;
4303 		bnxt_init_stats(bp);
4304 
4305 		rc = bnxt_alloc_ntp_fltrs(bp);
4306 		if (rc)
4307 			goto alloc_mem_err;
4308 
4309 		rc = bnxt_alloc_vnics(bp);
4310 		if (rc)
4311 			goto alloc_mem_err;
4312 	}
4313 
4314 	bnxt_init_ring_struct(bp);
4315 
4316 	rc = bnxt_alloc_rx_rings(bp);
4317 	if (rc)
4318 		goto alloc_mem_err;
4319 
4320 	rc = bnxt_alloc_tx_rings(bp);
4321 	if (rc)
4322 		goto alloc_mem_err;
4323 
4324 	rc = bnxt_alloc_cp_rings(bp);
4325 	if (rc)
4326 		goto alloc_mem_err;
4327 
4328 	bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
4329 				  BNXT_VNIC_UCAST_FLAG;
4330 	rc = bnxt_alloc_vnic_attributes(bp);
4331 	if (rc)
4332 		goto alloc_mem_err;
4333 	return 0;
4334 
4335 alloc_mem_err:
4336 	bnxt_free_mem(bp, true);
4337 	return rc;
4338 }
4339 
bnxt_disable_int(struct bnxt * bp)4340 static void bnxt_disable_int(struct bnxt *bp)
4341 {
4342 	int i;
4343 
4344 	if (!bp->bnapi)
4345 		return;
4346 
4347 	for (i = 0; i < bp->cp_nr_rings; i++) {
4348 		struct bnxt_napi *bnapi = bp->bnapi[i];
4349 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4350 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4351 
4352 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
4353 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
4354 	}
4355 }
4356 
bnxt_cp_num_to_irq_num(struct bnxt * bp,int n)4357 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
4358 {
4359 	struct bnxt_napi *bnapi = bp->bnapi[n];
4360 	struct bnxt_cp_ring_info *cpr;
4361 
4362 	cpr = &bnapi->cp_ring;
4363 	return cpr->cp_ring_struct.map_idx;
4364 }
4365 
bnxt_disable_int_sync(struct bnxt * bp)4366 static void bnxt_disable_int_sync(struct bnxt *bp)
4367 {
4368 	int i;
4369 
4370 	atomic_inc(&bp->intr_sem);
4371 
4372 	bnxt_disable_int(bp);
4373 	for (i = 0; i < bp->cp_nr_rings; i++) {
4374 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
4375 
4376 		synchronize_irq(bp->irq_tbl[map_idx].vector);
4377 	}
4378 }
4379 
bnxt_enable_int(struct bnxt * bp)4380 static void bnxt_enable_int(struct bnxt *bp)
4381 {
4382 	int i;
4383 
4384 	atomic_set(&bp->intr_sem, 0);
4385 	for (i = 0; i < bp->cp_nr_rings; i++) {
4386 		struct bnxt_napi *bnapi = bp->bnapi[i];
4387 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4388 
4389 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
4390 	}
4391 }
4392 
bnxt_hwrm_cmd_hdr_init(struct bnxt * bp,void * request,u16 req_type,u16 cmpl_ring,u16 target_id)4393 void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
4394 			    u16 cmpl_ring, u16 target_id)
4395 {
4396 	struct input *req = request;
4397 
4398 	req->req_type = cpu_to_le16(req_type);
4399 	req->cmpl_ring = cpu_to_le16(cmpl_ring);
4400 	req->target_id = cpu_to_le16(target_id);
4401 	if (bnxt_kong_hwrm_message(bp, req))
4402 		req->resp_addr = cpu_to_le64(bp->hwrm_cmd_kong_resp_dma_addr);
4403 	else
4404 		req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
4405 }
4406 
bnxt_hwrm_to_stderr(u32 hwrm_err)4407 static int bnxt_hwrm_to_stderr(u32 hwrm_err)
4408 {
4409 	switch (hwrm_err) {
4410 	case HWRM_ERR_CODE_SUCCESS:
4411 		return 0;
4412 	case HWRM_ERR_CODE_RESOURCE_LOCKED:
4413 		return -EROFS;
4414 	case HWRM_ERR_CODE_RESOURCE_ACCESS_DENIED:
4415 		return -EACCES;
4416 	case HWRM_ERR_CODE_RESOURCE_ALLOC_ERROR:
4417 		return -ENOSPC;
4418 	case HWRM_ERR_CODE_INVALID_PARAMS:
4419 	case HWRM_ERR_CODE_INVALID_FLAGS:
4420 	case HWRM_ERR_CODE_INVALID_ENABLES:
4421 	case HWRM_ERR_CODE_UNSUPPORTED_TLV:
4422 	case HWRM_ERR_CODE_UNSUPPORTED_OPTION_ERR:
4423 		return -EINVAL;
4424 	case HWRM_ERR_CODE_NO_BUFFER:
4425 		return -ENOMEM;
4426 	case HWRM_ERR_CODE_HOT_RESET_PROGRESS:
4427 	case HWRM_ERR_CODE_BUSY:
4428 		return -EAGAIN;
4429 	case HWRM_ERR_CODE_CMD_NOT_SUPPORTED:
4430 		return -EOPNOTSUPP;
4431 	default:
4432 		return -EIO;
4433 	}
4434 }
4435 
bnxt_hwrm_do_send_msg(struct bnxt * bp,void * msg,u32 msg_len,int timeout,bool silent)4436 static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
4437 				 int timeout, bool silent)
4438 {
4439 	int i, intr_process, rc, tmo_count;
4440 	struct input *req = msg;
4441 	u32 *data = msg;
4442 	u8 *valid;
4443 	u16 cp_ring_id, len = 0;
4444 	struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
4445 	u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
4446 	struct hwrm_short_input short_input = {0};
4447 	u32 doorbell_offset = BNXT_GRCPF_REG_CHIMP_COMM_TRIGGER;
4448 	u32 bar_offset = BNXT_GRCPF_REG_CHIMP_COMM;
4449 	u16 dst = BNXT_HWRM_CHNL_CHIMP;
4450 
4451 	if (BNXT_NO_FW_ACCESS(bp) &&
4452 	    le16_to_cpu(req->req_type) != HWRM_FUNC_RESET)
4453 		return -EBUSY;
4454 
4455 	if (msg_len > BNXT_HWRM_MAX_REQ_LEN) {
4456 		if (msg_len > bp->hwrm_max_ext_req_len ||
4457 		    !bp->hwrm_short_cmd_req_addr)
4458 			return -EINVAL;
4459 	}
4460 
4461 	if (bnxt_hwrm_kong_chnl(bp, req)) {
4462 		dst = BNXT_HWRM_CHNL_KONG;
4463 		bar_offset = BNXT_GRCPF_REG_KONG_COMM;
4464 		doorbell_offset = BNXT_GRCPF_REG_KONG_COMM_TRIGGER;
4465 		resp = bp->hwrm_cmd_kong_resp_addr;
4466 	}
4467 
4468 	memset(resp, 0, PAGE_SIZE);
4469 	cp_ring_id = le16_to_cpu(req->cmpl_ring);
4470 	intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
4471 
4472 	req->seq_id = cpu_to_le16(bnxt_get_hwrm_seq_id(bp, dst));
4473 	/* currently supports only one outstanding message */
4474 	if (intr_process)
4475 		bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
4476 
4477 	if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
4478 	    msg_len > BNXT_HWRM_MAX_REQ_LEN) {
4479 		void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
4480 		u16 max_msg_len;
4481 
4482 		/* Set boundary for maximum extended request length for short
4483 		 * cmd format. If passed up from device use the max supported
4484 		 * internal req length.
4485 		 */
4486 		max_msg_len = bp->hwrm_max_ext_req_len;
4487 
4488 		memcpy(short_cmd_req, req, msg_len);
4489 		if (msg_len < max_msg_len)
4490 			memset(short_cmd_req + msg_len, 0,
4491 			       max_msg_len - msg_len);
4492 
4493 		short_input.req_type = req->req_type;
4494 		short_input.signature =
4495 				cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
4496 		short_input.size = cpu_to_le16(msg_len);
4497 		short_input.req_addr =
4498 			cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
4499 
4500 		data = (u32 *)&short_input;
4501 		msg_len = sizeof(short_input);
4502 
4503 		/* Sync memory write before updating doorbell */
4504 		wmb();
4505 
4506 		max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
4507 	}
4508 
4509 	/* Write request msg to hwrm channel */
4510 	__iowrite32_copy(bp->bar0 + bar_offset, data, msg_len / 4);
4511 
4512 	for (i = msg_len; i < max_req_len; i += 4)
4513 		writel(0, bp->bar0 + bar_offset + i);
4514 
4515 	/* Ring channel doorbell */
4516 	writel(1, bp->bar0 + doorbell_offset);
4517 
4518 	if (!pci_is_enabled(bp->pdev))
4519 		return 0;
4520 
4521 	if (!timeout)
4522 		timeout = DFLT_HWRM_CMD_TIMEOUT;
4523 	/* convert timeout to usec */
4524 	timeout *= 1000;
4525 
4526 	i = 0;
4527 	/* Short timeout for the first few iterations:
4528 	 * number of loops = number of loops for short timeout +
4529 	 * number of loops for standard timeout.
4530 	 */
4531 	tmo_count = HWRM_SHORT_TIMEOUT_COUNTER;
4532 	timeout = timeout - HWRM_SHORT_MIN_TIMEOUT * HWRM_SHORT_TIMEOUT_COUNTER;
4533 	tmo_count += DIV_ROUND_UP(timeout, HWRM_MIN_TIMEOUT);
4534 
4535 	if (intr_process) {
4536 		u16 seq_id = bp->hwrm_intr_seq_id;
4537 
4538 		/* Wait until hwrm response cmpl interrupt is processed */
4539 		while (bp->hwrm_intr_seq_id != (u16)~seq_id &&
4540 		       i++ < tmo_count) {
4541 			/* Abort the wait for completion if the FW health
4542 			 * check has failed.
4543 			 */
4544 			if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
4545 				return -EBUSY;
4546 			/* on first few passes, just barely sleep */
4547 			if (i < HWRM_SHORT_TIMEOUT_COUNTER)
4548 				usleep_range(HWRM_SHORT_MIN_TIMEOUT,
4549 					     HWRM_SHORT_MAX_TIMEOUT);
4550 			else
4551 				usleep_range(HWRM_MIN_TIMEOUT,
4552 					     HWRM_MAX_TIMEOUT);
4553 		}
4554 
4555 		if (bp->hwrm_intr_seq_id != (u16)~seq_id) {
4556 			if (!silent)
4557 				netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
4558 					   le16_to_cpu(req->req_type));
4559 			return -EBUSY;
4560 		}
4561 		len = le16_to_cpu(resp->resp_len);
4562 		valid = ((u8 *)resp) + len - 1;
4563 	} else {
4564 		int j;
4565 
4566 		/* Check if response len is updated */
4567 		for (i = 0; i < tmo_count; i++) {
4568 			/* Abort the wait for completion if the FW health
4569 			 * check has failed.
4570 			 */
4571 			if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
4572 				return -EBUSY;
4573 			len = le16_to_cpu(resp->resp_len);
4574 			if (len)
4575 				break;
4576 			/* on first few passes, just barely sleep */
4577 			if (i < HWRM_SHORT_TIMEOUT_COUNTER)
4578 				usleep_range(HWRM_SHORT_MIN_TIMEOUT,
4579 					     HWRM_SHORT_MAX_TIMEOUT);
4580 			else
4581 				usleep_range(HWRM_MIN_TIMEOUT,
4582 					     HWRM_MAX_TIMEOUT);
4583 		}
4584 
4585 		if (i >= tmo_count) {
4586 			if (!silent)
4587 				netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
4588 					   HWRM_TOTAL_TIMEOUT(i),
4589 					   le16_to_cpu(req->req_type),
4590 					   le16_to_cpu(req->seq_id), len);
4591 			return -EBUSY;
4592 		}
4593 
4594 		/* Last byte of resp contains valid bit */
4595 		valid = ((u8 *)resp) + len - 1;
4596 		for (j = 0; j < HWRM_VALID_BIT_DELAY_USEC; j++) {
4597 			/* make sure we read from updated DMA memory */
4598 			dma_rmb();
4599 			if (*valid)
4600 				break;
4601 			usleep_range(1, 5);
4602 		}
4603 
4604 		if (j >= HWRM_VALID_BIT_DELAY_USEC) {
4605 			if (!silent)
4606 				netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
4607 					   HWRM_TOTAL_TIMEOUT(i),
4608 					   le16_to_cpu(req->req_type),
4609 					   le16_to_cpu(req->seq_id), len,
4610 					   *valid);
4611 			return -EBUSY;
4612 		}
4613 	}
4614 
4615 	/* Zero valid bit for compatibility.  Valid bit in an older spec
4616 	 * may become a new field in a newer spec.  We must make sure that
4617 	 * a new field not implemented by old spec will read zero.
4618 	 */
4619 	*valid = 0;
4620 	rc = le16_to_cpu(resp->error_code);
4621 	if (rc && !silent)
4622 		netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
4623 			   le16_to_cpu(resp->req_type),
4624 			   le16_to_cpu(resp->seq_id), rc);
4625 	return bnxt_hwrm_to_stderr(rc);
4626 }
4627 
_hwrm_send_message(struct bnxt * bp,void * msg,u32 msg_len,int timeout)4628 int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
4629 {
4630 	return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
4631 }
4632 
_hwrm_send_message_silent(struct bnxt * bp,void * msg,u32 msg_len,int timeout)4633 int _hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
4634 			      int timeout)
4635 {
4636 	return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
4637 }
4638 
hwrm_send_message(struct bnxt * bp,void * msg,u32 msg_len,int timeout)4639 int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
4640 {
4641 	int rc;
4642 
4643 	mutex_lock(&bp->hwrm_cmd_lock);
4644 	rc = _hwrm_send_message(bp, msg, msg_len, timeout);
4645 	mutex_unlock(&bp->hwrm_cmd_lock);
4646 	return rc;
4647 }
4648 
hwrm_send_message_silent(struct bnxt * bp,void * msg,u32 msg_len,int timeout)4649 int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
4650 			     int timeout)
4651 {
4652 	int rc;
4653 
4654 	mutex_lock(&bp->hwrm_cmd_lock);
4655 	rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
4656 	mutex_unlock(&bp->hwrm_cmd_lock);
4657 	return rc;
4658 }
4659 
bnxt_hwrm_func_drv_rgtr(struct bnxt * bp,unsigned long * bmap,int bmap_size,bool async_only)4660 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
4661 			    bool async_only)
4662 {
4663 	struct hwrm_func_drv_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
4664 	struct hwrm_func_drv_rgtr_input req = {0};
4665 	DECLARE_BITMAP(async_events_bmap, 256);
4666 	u32 *events = (u32 *)async_events_bmap;
4667 	u32 flags;
4668 	int rc, i;
4669 
4670 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
4671 
4672 	req.enables =
4673 		cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
4674 			    FUNC_DRV_RGTR_REQ_ENABLES_VER |
4675 			    FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4676 
4677 	req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
4678 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
4679 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
4680 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
4681 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
4682 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
4683 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
4684 	req.flags = cpu_to_le32(flags);
4685 	req.ver_maj_8b = DRV_VER_MAJ;
4686 	req.ver_min_8b = DRV_VER_MIN;
4687 	req.ver_upd_8b = DRV_VER_UPD;
4688 	req.ver_maj = cpu_to_le16(DRV_VER_MAJ);
4689 	req.ver_min = cpu_to_le16(DRV_VER_MIN);
4690 	req.ver_upd = cpu_to_le16(DRV_VER_UPD);
4691 
4692 	if (BNXT_PF(bp)) {
4693 		u32 data[8];
4694 		int i;
4695 
4696 		memset(data, 0, sizeof(data));
4697 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
4698 			u16 cmd = bnxt_vf_req_snif[i];
4699 			unsigned int bit, idx;
4700 
4701 			idx = cmd / 32;
4702 			bit = cmd % 32;
4703 			data[idx] |= 1 << bit;
4704 		}
4705 
4706 		for (i = 0; i < 8; i++)
4707 			req.vf_req_fwd[i] = cpu_to_le32(data[i]);
4708 
4709 		req.enables |=
4710 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
4711 	}
4712 
4713 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
4714 		req.flags |= cpu_to_le32(
4715 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
4716 
4717 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
4718 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
4719 		u16 event_id = bnxt_async_events_arr[i];
4720 
4721 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
4722 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
4723 			continue;
4724 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
4725 	}
4726 	if (bmap && bmap_size) {
4727 		for (i = 0; i < bmap_size; i++) {
4728 			if (test_bit(i, bmap))
4729 				__set_bit(i, async_events_bmap);
4730 		}
4731 	}
4732 	for (i = 0; i < 8; i++)
4733 		req.async_event_fwd[i] |= cpu_to_le32(events[i]);
4734 
4735 	if (async_only)
4736 		req.enables =
4737 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4738 
4739 	mutex_lock(&bp->hwrm_cmd_lock);
4740 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4741 	if (!rc) {
4742 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
4743 		if (resp->flags &
4744 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
4745 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
4746 	}
4747 	mutex_unlock(&bp->hwrm_cmd_lock);
4748 	return rc;
4749 }
4750 
bnxt_hwrm_func_drv_unrgtr(struct bnxt * bp)4751 static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
4752 {
4753 	struct hwrm_func_drv_unrgtr_input req = {0};
4754 
4755 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
4756 		return 0;
4757 
4758 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
4759 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4760 }
4761 
bnxt_hwrm_tunnel_dst_port_free(struct bnxt * bp,u8 tunnel_type)4762 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
4763 {
4764 	u32 rc = 0;
4765 	struct hwrm_tunnel_dst_port_free_input req = {0};
4766 
4767 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
4768 	req.tunnel_type = tunnel_type;
4769 
4770 	switch (tunnel_type) {
4771 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
4772 		req.tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
4773 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
4774 		break;
4775 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
4776 		req.tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
4777 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
4778 		break;
4779 	default:
4780 		break;
4781 	}
4782 
4783 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4784 	if (rc)
4785 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
4786 			   rc);
4787 	return rc;
4788 }
4789 
bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt * bp,__be16 port,u8 tunnel_type)4790 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
4791 					   u8 tunnel_type)
4792 {
4793 	u32 rc = 0;
4794 	struct hwrm_tunnel_dst_port_alloc_input req = {0};
4795 	struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4796 
4797 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
4798 
4799 	req.tunnel_type = tunnel_type;
4800 	req.tunnel_dst_port_val = port;
4801 
4802 	mutex_lock(&bp->hwrm_cmd_lock);
4803 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4804 	if (rc) {
4805 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
4806 			   rc);
4807 		goto err_out;
4808 	}
4809 
4810 	switch (tunnel_type) {
4811 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
4812 		bp->vxlan_fw_dst_port_id =
4813 			le16_to_cpu(resp->tunnel_dst_port_id);
4814 		break;
4815 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
4816 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
4817 		break;
4818 	default:
4819 		break;
4820 	}
4821 
4822 err_out:
4823 	mutex_unlock(&bp->hwrm_cmd_lock);
4824 	return rc;
4825 }
4826 
bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt * bp,u16 vnic_id)4827 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
4828 {
4829 	struct hwrm_cfa_l2_set_rx_mask_input req = {0};
4830 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4831 
4832 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
4833 	req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4834 
4835 	req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
4836 	req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
4837 	req.mask = cpu_to_le32(vnic->rx_mask);
4838 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4839 }
4840 
4841 #ifdef CONFIG_RFS_ACCEL
bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt * bp,struct bnxt_ntuple_filter * fltr)4842 static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
4843 					    struct bnxt_ntuple_filter *fltr)
4844 {
4845 	struct hwrm_cfa_ntuple_filter_free_input req = {0};
4846 
4847 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
4848 	req.ntuple_filter_id = fltr->filter_id;
4849 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4850 }
4851 
4852 #define BNXT_NTP_FLTR_FLAGS					\
4853 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
4854 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
4855 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR |	\
4856 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
4857 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
4858 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
4859 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
4860 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
4861 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
4862 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
4863 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
4864 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
4865 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
4866 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
4867 
4868 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
4869 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
4870 
bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt * bp,struct bnxt_ntuple_filter * fltr)4871 static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
4872 					     struct bnxt_ntuple_filter *fltr)
4873 {
4874 	struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
4875 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
4876 	struct flow_keys *keys = &fltr->fkeys;
4877 	struct bnxt_vnic_info *vnic;
4878 	u32 flags = 0;
4879 	int rc = 0;
4880 
4881 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
4882 	req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
4883 
4884 	if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
4885 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
4886 		req.dst_id = cpu_to_le16(fltr->rxq);
4887 	} else {
4888 		vnic = &bp->vnic_info[fltr->rxq + 1];
4889 		req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
4890 	}
4891 	req.flags = cpu_to_le32(flags);
4892 	req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
4893 
4894 	req.ethertype = htons(ETH_P_IP);
4895 	memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
4896 	req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
4897 	req.ip_protocol = keys->basic.ip_proto;
4898 
4899 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
4900 		int i;
4901 
4902 		req.ethertype = htons(ETH_P_IPV6);
4903 		req.ip_addr_type =
4904 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
4905 		*(struct in6_addr *)&req.src_ipaddr[0] =
4906 			keys->addrs.v6addrs.src;
4907 		*(struct in6_addr *)&req.dst_ipaddr[0] =
4908 			keys->addrs.v6addrs.dst;
4909 		for (i = 0; i < 4; i++) {
4910 			req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4911 			req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4912 		}
4913 	} else {
4914 		req.src_ipaddr[0] = keys->addrs.v4addrs.src;
4915 		req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4916 		req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
4917 		req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4918 	}
4919 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
4920 		req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
4921 		req.tunnel_type =
4922 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
4923 	}
4924 
4925 	req.src_port = keys->ports.src;
4926 	req.src_port_mask = cpu_to_be16(0xffff);
4927 	req.dst_port = keys->ports.dst;
4928 	req.dst_port_mask = cpu_to_be16(0xffff);
4929 
4930 	mutex_lock(&bp->hwrm_cmd_lock);
4931 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4932 	if (!rc) {
4933 		resp = bnxt_get_hwrm_resp_addr(bp, &req);
4934 		fltr->filter_id = resp->ntuple_filter_id;
4935 	}
4936 	mutex_unlock(&bp->hwrm_cmd_lock);
4937 	return rc;
4938 }
4939 #endif
4940 
bnxt_hwrm_set_vnic_filter(struct bnxt * bp,u16 vnic_id,u16 idx,u8 * mac_addr)4941 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
4942 				     u8 *mac_addr)
4943 {
4944 	u32 rc = 0;
4945 	struct hwrm_cfa_l2_filter_alloc_input req = {0};
4946 	struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4947 
4948 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
4949 	req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
4950 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
4951 		req.flags |=
4952 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
4953 	req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
4954 	req.enables =
4955 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
4956 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
4957 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
4958 	memcpy(req.l2_addr, mac_addr, ETH_ALEN);
4959 	req.l2_addr_mask[0] = 0xff;
4960 	req.l2_addr_mask[1] = 0xff;
4961 	req.l2_addr_mask[2] = 0xff;
4962 	req.l2_addr_mask[3] = 0xff;
4963 	req.l2_addr_mask[4] = 0xff;
4964 	req.l2_addr_mask[5] = 0xff;
4965 
4966 	mutex_lock(&bp->hwrm_cmd_lock);
4967 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4968 	if (!rc)
4969 		bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
4970 							resp->l2_filter_id;
4971 	mutex_unlock(&bp->hwrm_cmd_lock);
4972 	return rc;
4973 }
4974 
bnxt_hwrm_clear_vnic_filter(struct bnxt * bp)4975 static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
4976 {
4977 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
4978 	int rc = 0;
4979 
4980 	/* Any associated ntuple filters will also be cleared by firmware. */
4981 	mutex_lock(&bp->hwrm_cmd_lock);
4982 	for (i = 0; i < num_of_vnics; i++) {
4983 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4984 
4985 		for (j = 0; j < vnic->uc_filter_count; j++) {
4986 			struct hwrm_cfa_l2_filter_free_input req = {0};
4987 
4988 			bnxt_hwrm_cmd_hdr_init(bp, &req,
4989 					       HWRM_CFA_L2_FILTER_FREE, -1, -1);
4990 
4991 			req.l2_filter_id = vnic->fw_l2_filter_id[j];
4992 
4993 			rc = _hwrm_send_message(bp, &req, sizeof(req),
4994 						HWRM_CMD_TIMEOUT);
4995 		}
4996 		vnic->uc_filter_count = 0;
4997 	}
4998 	mutex_unlock(&bp->hwrm_cmd_lock);
4999 
5000 	return rc;
5001 }
5002 
bnxt_hwrm_vnic_set_tpa(struct bnxt * bp,u16 vnic_id,u32 tpa_flags)5003 static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
5004 {
5005 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5006 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
5007 	struct hwrm_vnic_tpa_cfg_input req = {0};
5008 
5009 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
5010 		return 0;
5011 
5012 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
5013 
5014 	if (tpa_flags) {
5015 		u16 mss = bp->dev->mtu - 40;
5016 		u32 nsegs, n, segs = 0, flags;
5017 
5018 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
5019 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
5020 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
5021 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
5022 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
5023 		if (tpa_flags & BNXT_FLAG_GRO)
5024 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
5025 
5026 		req.flags = cpu_to_le32(flags);
5027 
5028 		req.enables =
5029 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
5030 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
5031 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
5032 
5033 		/* Number of segs are log2 units, and first packet is not
5034 		 * included as part of this units.
5035 		 */
5036 		if (mss <= BNXT_RX_PAGE_SIZE) {
5037 			n = BNXT_RX_PAGE_SIZE / mss;
5038 			nsegs = (MAX_SKB_FRAGS - 1) * n;
5039 		} else {
5040 			n = mss / BNXT_RX_PAGE_SIZE;
5041 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
5042 				n++;
5043 			nsegs = (MAX_SKB_FRAGS - n) / n;
5044 		}
5045 
5046 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5047 			segs = MAX_TPA_SEGS_P5;
5048 			max_aggs = bp->max_tpa;
5049 		} else {
5050 			segs = ilog2(nsegs);
5051 		}
5052 		req.max_agg_segs = cpu_to_le16(segs);
5053 		req.max_aggs = cpu_to_le16(max_aggs);
5054 
5055 		req.min_agg_len = cpu_to_le32(512);
5056 	}
5057 	req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5058 
5059 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5060 }
5061 
bnxt_cp_ring_from_grp(struct bnxt * bp,struct bnxt_ring_struct * ring)5062 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
5063 {
5064 	struct bnxt_ring_grp_info *grp_info;
5065 
5066 	grp_info = &bp->grp_info[ring->grp_idx];
5067 	return grp_info->cp_fw_ring_id;
5068 }
5069 
bnxt_cp_ring_for_rx(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)5070 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
5071 {
5072 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5073 		struct bnxt_napi *bnapi = rxr->bnapi;
5074 		struct bnxt_cp_ring_info *cpr;
5075 
5076 		cpr = bnapi->cp_ring.cp_ring_arr[BNXT_RX_HDL];
5077 		return cpr->cp_ring_struct.fw_ring_id;
5078 	} else {
5079 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
5080 	}
5081 }
5082 
bnxt_cp_ring_for_tx(struct bnxt * bp,struct bnxt_tx_ring_info * txr)5083 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
5084 {
5085 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5086 		struct bnxt_napi *bnapi = txr->bnapi;
5087 		struct bnxt_cp_ring_info *cpr;
5088 
5089 		cpr = bnapi->cp_ring.cp_ring_arr[BNXT_TX_HDL];
5090 		return cpr->cp_ring_struct.fw_ring_id;
5091 	} else {
5092 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
5093 	}
5094 }
5095 
bnxt_alloc_rss_indir_tbl(struct bnxt * bp)5096 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
5097 {
5098 	int entries;
5099 
5100 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5101 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
5102 	else
5103 		entries = HW_HASH_INDEX_SIZE;
5104 
5105 	bp->rss_indir_tbl_entries = entries;
5106 	bp->rss_indir_tbl = kmalloc_array(entries, sizeof(*bp->rss_indir_tbl),
5107 					  GFP_KERNEL);
5108 	if (!bp->rss_indir_tbl)
5109 		return -ENOMEM;
5110 	return 0;
5111 }
5112 
bnxt_set_dflt_rss_indir_tbl(struct bnxt * bp)5113 static void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp)
5114 {
5115 	u16 max_rings, max_entries, pad, i;
5116 
5117 	if (!bp->rx_nr_rings)
5118 		return;
5119 
5120 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5121 		max_rings = bp->rx_nr_rings - 1;
5122 	else
5123 		max_rings = bp->rx_nr_rings;
5124 
5125 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
5126 
5127 	for (i = 0; i < max_entries; i++)
5128 		bp->rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
5129 
5130 	pad = bp->rss_indir_tbl_entries - max_entries;
5131 	if (pad)
5132 		memset(&bp->rss_indir_tbl[i], 0, pad * sizeof(u16));
5133 }
5134 
bnxt_get_max_rss_ring(struct bnxt * bp)5135 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
5136 {
5137 	u16 i, tbl_size, max_ring = 0;
5138 
5139 	if (!bp->rss_indir_tbl)
5140 		return 0;
5141 
5142 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
5143 	for (i = 0; i < tbl_size; i++)
5144 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
5145 	return max_ring;
5146 }
5147 
bnxt_get_nr_rss_ctxs(struct bnxt * bp,int rx_rings)5148 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
5149 {
5150 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5151 		return DIV_ROUND_UP(rx_rings, BNXT_RSS_TABLE_ENTRIES_P5);
5152 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5153 		return 2;
5154 	return 1;
5155 }
5156 
__bnxt_fill_hw_rss_tbl(struct bnxt * bp,struct bnxt_vnic_info * vnic)5157 static void __bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
5158 {
5159 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
5160 	u16 i, j;
5161 
5162 	/* Fill the RSS indirection table with ring group ids */
5163 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
5164 		if (!no_rss)
5165 			j = bp->rss_indir_tbl[i];
5166 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
5167 	}
5168 }
5169 
__bnxt_fill_hw_rss_tbl_p5(struct bnxt * bp,struct bnxt_vnic_info * vnic)5170 static void __bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
5171 				      struct bnxt_vnic_info *vnic)
5172 {
5173 	__le16 *ring_tbl = vnic->rss_table;
5174 	struct bnxt_rx_ring_info *rxr;
5175 	u16 tbl_size, i;
5176 
5177 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
5178 
5179 	for (i = 0; i < tbl_size; i++) {
5180 		u16 ring_id, j;
5181 
5182 		j = bp->rss_indir_tbl[i];
5183 		rxr = &bp->rx_ring[j];
5184 
5185 		ring_id = rxr->rx_ring_struct.fw_ring_id;
5186 		*ring_tbl++ = cpu_to_le16(ring_id);
5187 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5188 		*ring_tbl++ = cpu_to_le16(ring_id);
5189 	}
5190 }
5191 
bnxt_fill_hw_rss_tbl(struct bnxt * bp,struct bnxt_vnic_info * vnic)5192 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
5193 {
5194 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5195 		__bnxt_fill_hw_rss_tbl_p5(bp, vnic);
5196 	else
5197 		__bnxt_fill_hw_rss_tbl(bp, vnic);
5198 }
5199 
bnxt_hwrm_vnic_set_rss(struct bnxt * bp,u16 vnic_id,bool set_rss)5200 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
5201 {
5202 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5203 	struct hwrm_vnic_rss_cfg_input req = {0};
5204 
5205 	if ((bp->flags & BNXT_FLAG_CHIP_P5) ||
5206 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
5207 		return 0;
5208 
5209 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
5210 	if (set_rss) {
5211 		bnxt_fill_hw_rss_tbl(bp, vnic);
5212 		req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
5213 		req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
5214 		req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
5215 		req.hash_key_tbl_addr =
5216 			cpu_to_le64(vnic->rss_hash_key_dma_addr);
5217 	}
5218 	req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5219 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5220 }
5221 
bnxt_hwrm_vnic_set_rss_p5(struct bnxt * bp,u16 vnic_id,bool set_rss)5222 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, u16 vnic_id, bool set_rss)
5223 {
5224 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5225 	struct hwrm_vnic_rss_cfg_input req = {0};
5226 	dma_addr_t ring_tbl_map;
5227 	u32 i, nr_ctxs;
5228 
5229 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
5230 	req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5231 	if (!set_rss) {
5232 		hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5233 		return 0;
5234 	}
5235 	bnxt_fill_hw_rss_tbl(bp, vnic);
5236 	req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
5237 	req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
5238 	req.hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
5239 	ring_tbl_map = vnic->rss_table_dma_addr;
5240 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
5241 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
5242 		int rc;
5243 
5244 		req.ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
5245 		req.ring_table_pair_index = i;
5246 		req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
5247 		rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5248 		if (rc)
5249 			return rc;
5250 	}
5251 	return 0;
5252 }
5253 
bnxt_hwrm_vnic_set_hds(struct bnxt * bp,u16 vnic_id)5254 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
5255 {
5256 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5257 	struct hwrm_vnic_plcmodes_cfg_input req = {0};
5258 
5259 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
5260 	req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
5261 				VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
5262 				VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
5263 	req.enables =
5264 		cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
5265 			    VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
5266 	/* thresholds not implemented in firmware yet */
5267 	req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
5268 	req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
5269 	req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5270 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5271 }
5272 
bnxt_hwrm_vnic_ctx_free_one(struct bnxt * bp,u16 vnic_id,u16 ctx_idx)5273 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
5274 					u16 ctx_idx)
5275 {
5276 	struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
5277 
5278 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
5279 	req.rss_cos_lb_ctx_id =
5280 		cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
5281 
5282 	hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5283 	bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
5284 }
5285 
bnxt_hwrm_vnic_ctx_free(struct bnxt * bp)5286 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
5287 {
5288 	int i, j;
5289 
5290 	for (i = 0; i < bp->nr_vnics; i++) {
5291 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
5292 
5293 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
5294 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
5295 				bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
5296 		}
5297 	}
5298 	bp->rsscos_nr_ctxs = 0;
5299 }
5300 
bnxt_hwrm_vnic_ctx_alloc(struct bnxt * bp,u16 vnic_id,u16 ctx_idx)5301 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
5302 {
5303 	int rc;
5304 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
5305 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
5306 						bp->hwrm_cmd_resp_addr;
5307 
5308 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
5309 			       -1);
5310 
5311 	mutex_lock(&bp->hwrm_cmd_lock);
5312 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5313 	if (!rc)
5314 		bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
5315 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
5316 	mutex_unlock(&bp->hwrm_cmd_lock);
5317 
5318 	return rc;
5319 }
5320 
bnxt_get_roce_vnic_mode(struct bnxt * bp)5321 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
5322 {
5323 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
5324 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
5325 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
5326 }
5327 
bnxt_hwrm_vnic_cfg(struct bnxt * bp,u16 vnic_id)5328 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
5329 {
5330 	unsigned int ring = 0, grp_idx;
5331 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5332 	struct hwrm_vnic_cfg_input req = {0};
5333 	u16 def_vlan = 0;
5334 
5335 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
5336 
5337 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5338 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
5339 
5340 		req.default_rx_ring_id =
5341 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
5342 		req.default_cmpl_ring_id =
5343 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
5344 		req.enables =
5345 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
5346 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
5347 		goto vnic_mru;
5348 	}
5349 	req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
5350 	/* Only RSS support for now TBD: COS & LB */
5351 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
5352 		req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5353 		req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5354 					   VNIC_CFG_REQ_ENABLES_MRU);
5355 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
5356 		req.rss_rule =
5357 			cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
5358 		req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5359 					   VNIC_CFG_REQ_ENABLES_MRU);
5360 		req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
5361 	} else {
5362 		req.rss_rule = cpu_to_le16(0xffff);
5363 	}
5364 
5365 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
5366 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
5367 		req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
5368 		req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
5369 	} else {
5370 		req.cos_rule = cpu_to_le16(0xffff);
5371 	}
5372 
5373 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
5374 		ring = 0;
5375 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
5376 		ring = vnic_id - 1;
5377 	else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
5378 		ring = bp->rx_nr_rings - 1;
5379 
5380 	grp_idx = bp->rx_ring[ring].bnapi->index;
5381 	req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
5382 	req.lb_rule = cpu_to_le16(0xffff);
5383 vnic_mru:
5384 	req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + VLAN_HLEN);
5385 
5386 	req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5387 #ifdef CONFIG_BNXT_SRIOV
5388 	if (BNXT_VF(bp))
5389 		def_vlan = bp->vf.vlan;
5390 #endif
5391 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
5392 		req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
5393 	if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
5394 		req.flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
5395 
5396 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5397 }
5398 
bnxt_hwrm_vnic_free_one(struct bnxt * bp,u16 vnic_id)5399 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
5400 {
5401 	if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
5402 		struct hwrm_vnic_free_input req = {0};
5403 
5404 		bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
5405 		req.vnic_id =
5406 			cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
5407 
5408 		hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5409 		bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
5410 	}
5411 }
5412 
bnxt_hwrm_vnic_free(struct bnxt * bp)5413 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
5414 {
5415 	u16 i;
5416 
5417 	for (i = 0; i < bp->nr_vnics; i++)
5418 		bnxt_hwrm_vnic_free_one(bp, i);
5419 }
5420 
bnxt_hwrm_vnic_alloc(struct bnxt * bp,u16 vnic_id,unsigned int start_rx_ring_idx,unsigned int nr_rings)5421 static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
5422 				unsigned int start_rx_ring_idx,
5423 				unsigned int nr_rings)
5424 {
5425 	int rc = 0;
5426 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
5427 	struct hwrm_vnic_alloc_input req = {0};
5428 	struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5429 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5430 
5431 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5432 		goto vnic_no_ring_grps;
5433 
5434 	/* map ring groups to this vnic */
5435 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
5436 		grp_idx = bp->rx_ring[i].bnapi->index;
5437 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
5438 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
5439 				   j, nr_rings);
5440 			break;
5441 		}
5442 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
5443 	}
5444 
5445 vnic_no_ring_grps:
5446 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
5447 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
5448 	if (vnic_id == 0)
5449 		req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
5450 
5451 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
5452 
5453 	mutex_lock(&bp->hwrm_cmd_lock);
5454 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5455 	if (!rc)
5456 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
5457 	mutex_unlock(&bp->hwrm_cmd_lock);
5458 	return rc;
5459 }
5460 
bnxt_hwrm_vnic_qcaps(struct bnxt * bp)5461 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
5462 {
5463 	struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5464 	struct hwrm_vnic_qcaps_input req = {0};
5465 	int rc;
5466 
5467 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
5468 	bp->flags &= ~(BNXT_FLAG_NEW_RSS_CAP | BNXT_FLAG_ROCE_MIRROR_CAP);
5469 	if (bp->hwrm_spec_code < 0x10600)
5470 		return 0;
5471 
5472 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
5473 	mutex_lock(&bp->hwrm_cmd_lock);
5474 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5475 	if (!rc) {
5476 		u32 flags = le32_to_cpu(resp->flags);
5477 
5478 		if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
5479 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
5480 			bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
5481 		if (flags &
5482 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
5483 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
5484 
5485 		/* Older P5 fw before EXT_HW_STATS support did not set
5486 		 * VLAN_STRIP_CAP properly.
5487 		 */
5488 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
5489 		    (BNXT_CHIP_P5_THOR(bp) &&
5490 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
5491 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
5492 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
5493 		if (bp->max_tpa_v2) {
5494 			if (BNXT_CHIP_P5_THOR(bp))
5495 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
5496 			else
5497 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5_SR2;
5498 		}
5499 	}
5500 	mutex_unlock(&bp->hwrm_cmd_lock);
5501 	return rc;
5502 }
5503 
bnxt_hwrm_ring_grp_alloc(struct bnxt * bp)5504 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
5505 {
5506 	u16 i;
5507 	u32 rc = 0;
5508 
5509 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5510 		return 0;
5511 
5512 	mutex_lock(&bp->hwrm_cmd_lock);
5513 	for (i = 0; i < bp->rx_nr_rings; i++) {
5514 		struct hwrm_ring_grp_alloc_input req = {0};
5515 		struct hwrm_ring_grp_alloc_output *resp =
5516 					bp->hwrm_cmd_resp_addr;
5517 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
5518 
5519 		bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
5520 
5521 		req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
5522 		req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
5523 		req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
5524 		req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
5525 
5526 		rc = _hwrm_send_message(bp, &req, sizeof(req),
5527 					HWRM_CMD_TIMEOUT);
5528 		if (rc)
5529 			break;
5530 
5531 		bp->grp_info[grp_idx].fw_grp_id =
5532 			le32_to_cpu(resp->ring_group_id);
5533 	}
5534 	mutex_unlock(&bp->hwrm_cmd_lock);
5535 	return rc;
5536 }
5537 
bnxt_hwrm_ring_grp_free(struct bnxt * bp)5538 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
5539 {
5540 	u16 i;
5541 	struct hwrm_ring_grp_free_input req = {0};
5542 
5543 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5))
5544 		return;
5545 
5546 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
5547 
5548 	mutex_lock(&bp->hwrm_cmd_lock);
5549 	for (i = 0; i < bp->cp_nr_rings; i++) {
5550 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
5551 			continue;
5552 		req.ring_group_id =
5553 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
5554 
5555 		_hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5556 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
5557 	}
5558 	mutex_unlock(&bp->hwrm_cmd_lock);
5559 }
5560 
hwrm_ring_alloc_send_msg(struct bnxt * bp,struct bnxt_ring_struct * ring,u32 ring_type,u32 map_index)5561 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
5562 				    struct bnxt_ring_struct *ring,
5563 				    u32 ring_type, u32 map_index)
5564 {
5565 	int rc = 0, err = 0;
5566 	struct hwrm_ring_alloc_input req = {0};
5567 	struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5568 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
5569 	struct bnxt_ring_grp_info *grp_info;
5570 	u16 ring_id;
5571 
5572 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
5573 
5574 	req.enables = 0;
5575 	if (rmem->nr_pages > 1) {
5576 		req.page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
5577 		/* Page size is in log2 units */
5578 		req.page_size = BNXT_PAGE_SHIFT;
5579 		req.page_tbl_depth = 1;
5580 	} else {
5581 		req.page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
5582 	}
5583 	req.fbo = 0;
5584 	/* Association of ring index with doorbell index and MSIX number */
5585 	req.logical_id = cpu_to_le16(map_index);
5586 
5587 	switch (ring_type) {
5588 	case HWRM_RING_ALLOC_TX: {
5589 		struct bnxt_tx_ring_info *txr;
5590 
5591 		txr = container_of(ring, struct bnxt_tx_ring_info,
5592 				   tx_ring_struct);
5593 		req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
5594 		/* Association of transmit ring with completion ring */
5595 		grp_info = &bp->grp_info[ring->grp_idx];
5596 		req.cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
5597 		req.length = cpu_to_le32(bp->tx_ring_mask + 1);
5598 		req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5599 		req.queue_id = cpu_to_le16(ring->queue_id);
5600 		break;
5601 	}
5602 	case HWRM_RING_ALLOC_RX:
5603 		req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5604 		req.length = cpu_to_le32(bp->rx_ring_mask + 1);
5605 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5606 			u16 flags = 0;
5607 
5608 			/* Association of rx ring with stats context */
5609 			grp_info = &bp->grp_info[ring->grp_idx];
5610 			req.rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
5611 			req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5612 			req.enables |= cpu_to_le32(
5613 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5614 			if (NET_IP_ALIGN == 2)
5615 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
5616 			req.flags = cpu_to_le16(flags);
5617 		}
5618 		break;
5619 	case HWRM_RING_ALLOC_AGG:
5620 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5621 			req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
5622 			/* Association of agg ring with rx ring */
5623 			grp_info = &bp->grp_info[ring->grp_idx];
5624 			req.rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
5625 			req.rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
5626 			req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5627 			req.enables |= cpu_to_le32(
5628 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
5629 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5630 		} else {
5631 			req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5632 		}
5633 		req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
5634 		break;
5635 	case HWRM_RING_ALLOC_CMPL:
5636 		req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
5637 		req.length = cpu_to_le32(bp->cp_ring_mask + 1);
5638 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5639 			/* Association of cp ring with nq */
5640 			grp_info = &bp->grp_info[map_index];
5641 			req.nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
5642 			req.cq_handle = cpu_to_le64(ring->handle);
5643 			req.enables |= cpu_to_le32(
5644 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
5645 		} else if (bp->flags & BNXT_FLAG_USING_MSIX) {
5646 			req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5647 		}
5648 		break;
5649 	case HWRM_RING_ALLOC_NQ:
5650 		req.ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
5651 		req.length = cpu_to_le32(bp->cp_ring_mask + 1);
5652 		if (bp->flags & BNXT_FLAG_USING_MSIX)
5653 			req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5654 		break;
5655 	default:
5656 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
5657 			   ring_type);
5658 		return -1;
5659 	}
5660 
5661 	mutex_lock(&bp->hwrm_cmd_lock);
5662 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5663 	err = le16_to_cpu(resp->error_code);
5664 	ring_id = le16_to_cpu(resp->ring_id);
5665 	mutex_unlock(&bp->hwrm_cmd_lock);
5666 
5667 	if (rc || err) {
5668 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
5669 			   ring_type, rc, err);
5670 		return -EIO;
5671 	}
5672 	ring->fw_ring_id = ring_id;
5673 	return rc;
5674 }
5675 
bnxt_hwrm_set_async_event_cr(struct bnxt * bp,int idx)5676 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
5677 {
5678 	int rc;
5679 
5680 	if (BNXT_PF(bp)) {
5681 		struct hwrm_func_cfg_input req = {0};
5682 
5683 		bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
5684 		req.fid = cpu_to_le16(0xffff);
5685 		req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5686 		req.async_event_cr = cpu_to_le16(idx);
5687 		rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5688 	} else {
5689 		struct hwrm_func_vf_cfg_input req = {0};
5690 
5691 		bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
5692 		req.enables =
5693 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5694 		req.async_event_cr = cpu_to_le16(idx);
5695 		rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5696 	}
5697 	return rc;
5698 }
5699 
bnxt_set_db(struct bnxt * bp,struct bnxt_db_info * db,u32 ring_type,u32 map_idx,u32 xid)5700 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
5701 			u32 map_idx, u32 xid)
5702 {
5703 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5704 		if (BNXT_PF(bp))
5705 			db->doorbell = bp->bar1 + DB_PF_OFFSET_P5;
5706 		else
5707 			db->doorbell = bp->bar1 + DB_VF_OFFSET_P5;
5708 		switch (ring_type) {
5709 		case HWRM_RING_ALLOC_TX:
5710 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
5711 			break;
5712 		case HWRM_RING_ALLOC_RX:
5713 		case HWRM_RING_ALLOC_AGG:
5714 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
5715 			break;
5716 		case HWRM_RING_ALLOC_CMPL:
5717 			db->db_key64 = DBR_PATH_L2;
5718 			break;
5719 		case HWRM_RING_ALLOC_NQ:
5720 			db->db_key64 = DBR_PATH_L2;
5721 			break;
5722 		}
5723 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
5724 	} else {
5725 		db->doorbell = bp->bar1 + map_idx * 0x80;
5726 		switch (ring_type) {
5727 		case HWRM_RING_ALLOC_TX:
5728 			db->db_key32 = DB_KEY_TX;
5729 			break;
5730 		case HWRM_RING_ALLOC_RX:
5731 		case HWRM_RING_ALLOC_AGG:
5732 			db->db_key32 = DB_KEY_RX;
5733 			break;
5734 		case HWRM_RING_ALLOC_CMPL:
5735 			db->db_key32 = DB_KEY_CP;
5736 			break;
5737 		}
5738 	}
5739 }
5740 
bnxt_hwrm_ring_alloc(struct bnxt * bp)5741 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
5742 {
5743 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
5744 	int i, rc = 0;
5745 	u32 type;
5746 
5747 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5748 		type = HWRM_RING_ALLOC_NQ;
5749 	else
5750 		type = HWRM_RING_ALLOC_CMPL;
5751 	for (i = 0; i < bp->cp_nr_rings; i++) {
5752 		struct bnxt_napi *bnapi = bp->bnapi[i];
5753 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5754 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5755 		u32 map_idx = ring->map_idx;
5756 		unsigned int vector;
5757 
5758 		vector = bp->irq_tbl[map_idx].vector;
5759 		disable_irq_nosync(vector);
5760 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5761 		if (rc) {
5762 			enable_irq(vector);
5763 			goto err_out;
5764 		}
5765 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
5766 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5767 		enable_irq(vector);
5768 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
5769 
5770 		if (!i) {
5771 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
5772 			if (rc)
5773 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
5774 		}
5775 	}
5776 
5777 	type = HWRM_RING_ALLOC_TX;
5778 	for (i = 0; i < bp->tx_nr_rings; i++) {
5779 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5780 		struct bnxt_ring_struct *ring;
5781 		u32 map_idx;
5782 
5783 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5784 			struct bnxt_napi *bnapi = txr->bnapi;
5785 			struct bnxt_cp_ring_info *cpr, *cpr2;
5786 			u32 type2 = HWRM_RING_ALLOC_CMPL;
5787 
5788 			cpr = &bnapi->cp_ring;
5789 			cpr2 = cpr->cp_ring_arr[BNXT_TX_HDL];
5790 			ring = &cpr2->cp_ring_struct;
5791 			ring->handle = BNXT_TX_HDL;
5792 			map_idx = bnapi->index;
5793 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5794 			if (rc)
5795 				goto err_out;
5796 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5797 				    ring->fw_ring_id);
5798 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5799 		}
5800 		ring = &txr->tx_ring_struct;
5801 		map_idx = i;
5802 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5803 		if (rc)
5804 			goto err_out;
5805 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
5806 	}
5807 
5808 	type = HWRM_RING_ALLOC_RX;
5809 	for (i = 0; i < bp->rx_nr_rings; i++) {
5810 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5811 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5812 		struct bnxt_napi *bnapi = rxr->bnapi;
5813 		u32 map_idx = bnapi->index;
5814 
5815 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5816 		if (rc)
5817 			goto err_out;
5818 		bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
5819 		/* If we have agg rings, post agg buffers first. */
5820 		if (!agg_rings)
5821 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
5822 		bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
5823 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5824 			struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5825 			u32 type2 = HWRM_RING_ALLOC_CMPL;
5826 			struct bnxt_cp_ring_info *cpr2;
5827 
5828 			cpr2 = cpr->cp_ring_arr[BNXT_RX_HDL];
5829 			ring = &cpr2->cp_ring_struct;
5830 			ring->handle = BNXT_RX_HDL;
5831 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5832 			if (rc)
5833 				goto err_out;
5834 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5835 				    ring->fw_ring_id);
5836 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5837 		}
5838 	}
5839 
5840 	if (agg_rings) {
5841 		type = HWRM_RING_ALLOC_AGG;
5842 		for (i = 0; i < bp->rx_nr_rings; i++) {
5843 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5844 			struct bnxt_ring_struct *ring =
5845 						&rxr->rx_agg_ring_struct;
5846 			u32 grp_idx = ring->grp_idx;
5847 			u32 map_idx = grp_idx + bp->rx_nr_rings;
5848 
5849 			rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5850 			if (rc)
5851 				goto err_out;
5852 
5853 			bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
5854 				    ring->fw_ring_id);
5855 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
5856 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
5857 			bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
5858 		}
5859 	}
5860 err_out:
5861 	return rc;
5862 }
5863 
hwrm_ring_free_send_msg(struct bnxt * bp,struct bnxt_ring_struct * ring,u32 ring_type,int cmpl_ring_id)5864 static int hwrm_ring_free_send_msg(struct bnxt *bp,
5865 				   struct bnxt_ring_struct *ring,
5866 				   u32 ring_type, int cmpl_ring_id)
5867 {
5868 	int rc;
5869 	struct hwrm_ring_free_input req = {0};
5870 	struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
5871 	u16 error_code;
5872 
5873 	if (BNXT_NO_FW_ACCESS(bp))
5874 		return 0;
5875 
5876 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
5877 	req.ring_type = ring_type;
5878 	req.ring_id = cpu_to_le16(ring->fw_ring_id);
5879 
5880 	mutex_lock(&bp->hwrm_cmd_lock);
5881 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5882 	error_code = le16_to_cpu(resp->error_code);
5883 	mutex_unlock(&bp->hwrm_cmd_lock);
5884 
5885 	if (rc || error_code) {
5886 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
5887 			   ring_type, rc, error_code);
5888 		return -EIO;
5889 	}
5890 	return 0;
5891 }
5892 
bnxt_hwrm_ring_free(struct bnxt * bp,bool close_path)5893 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
5894 {
5895 	u32 type;
5896 	int i;
5897 
5898 	if (!bp->bnapi)
5899 		return;
5900 
5901 	for (i = 0; i < bp->tx_nr_rings; i++) {
5902 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5903 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
5904 
5905 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5906 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
5907 
5908 			hwrm_ring_free_send_msg(bp, ring,
5909 						RING_FREE_REQ_RING_TYPE_TX,
5910 						close_path ? cmpl_ring_id :
5911 						INVALID_HW_RING_ID);
5912 			ring->fw_ring_id = INVALID_HW_RING_ID;
5913 		}
5914 	}
5915 
5916 	for (i = 0; i < bp->rx_nr_rings; i++) {
5917 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5918 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5919 		u32 grp_idx = rxr->bnapi->index;
5920 
5921 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5922 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5923 
5924 			hwrm_ring_free_send_msg(bp, ring,
5925 						RING_FREE_REQ_RING_TYPE_RX,
5926 						close_path ? cmpl_ring_id :
5927 						INVALID_HW_RING_ID);
5928 			ring->fw_ring_id = INVALID_HW_RING_ID;
5929 			bp->grp_info[grp_idx].rx_fw_ring_id =
5930 				INVALID_HW_RING_ID;
5931 		}
5932 	}
5933 
5934 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5935 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
5936 	else
5937 		type = RING_FREE_REQ_RING_TYPE_RX;
5938 	for (i = 0; i < bp->rx_nr_rings; i++) {
5939 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5940 		struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
5941 		u32 grp_idx = rxr->bnapi->index;
5942 
5943 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5944 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5945 
5946 			hwrm_ring_free_send_msg(bp, ring, type,
5947 						close_path ? cmpl_ring_id :
5948 						INVALID_HW_RING_ID);
5949 			ring->fw_ring_id = INVALID_HW_RING_ID;
5950 			bp->grp_info[grp_idx].agg_fw_ring_id =
5951 				INVALID_HW_RING_ID;
5952 		}
5953 	}
5954 
5955 	/* The completion rings are about to be freed.  After that the
5956 	 * IRQ doorbell will not work anymore.  So we need to disable
5957 	 * IRQ here.
5958 	 */
5959 	bnxt_disable_int_sync(bp);
5960 
5961 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5962 		type = RING_FREE_REQ_RING_TYPE_NQ;
5963 	else
5964 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
5965 	for (i = 0; i < bp->cp_nr_rings; i++) {
5966 		struct bnxt_napi *bnapi = bp->bnapi[i];
5967 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5968 		struct bnxt_ring_struct *ring;
5969 		int j;
5970 
5971 		for (j = 0; j < 2; j++) {
5972 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
5973 
5974 			if (cpr2) {
5975 				ring = &cpr2->cp_ring_struct;
5976 				if (ring->fw_ring_id == INVALID_HW_RING_ID)
5977 					continue;
5978 				hwrm_ring_free_send_msg(bp, ring,
5979 					RING_FREE_REQ_RING_TYPE_L2_CMPL,
5980 					INVALID_HW_RING_ID);
5981 				ring->fw_ring_id = INVALID_HW_RING_ID;
5982 			}
5983 		}
5984 		ring = &cpr->cp_ring_struct;
5985 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5986 			hwrm_ring_free_send_msg(bp, ring, type,
5987 						INVALID_HW_RING_ID);
5988 			ring->fw_ring_id = INVALID_HW_RING_ID;
5989 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
5990 		}
5991 	}
5992 }
5993 
5994 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5995 			   bool shared);
5996 
bnxt_hwrm_get_rings(struct bnxt * bp)5997 static int bnxt_hwrm_get_rings(struct bnxt *bp)
5998 {
5999 	struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6000 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6001 	struct hwrm_func_qcfg_input req = {0};
6002 	int rc;
6003 
6004 	if (bp->hwrm_spec_code < 0x10601)
6005 		return 0;
6006 
6007 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
6008 	req.fid = cpu_to_le16(0xffff);
6009 	mutex_lock(&bp->hwrm_cmd_lock);
6010 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6011 	if (rc) {
6012 		mutex_unlock(&bp->hwrm_cmd_lock);
6013 		return rc;
6014 	}
6015 
6016 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
6017 	if (BNXT_NEW_RM(bp)) {
6018 		u16 cp, stats;
6019 
6020 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
6021 		hw_resc->resv_hw_ring_grps =
6022 			le32_to_cpu(resp->alloc_hw_ring_grps);
6023 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
6024 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
6025 		stats = le16_to_cpu(resp->alloc_stat_ctx);
6026 		hw_resc->resv_irqs = cp;
6027 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
6028 			int rx = hw_resc->resv_rx_rings;
6029 			int tx = hw_resc->resv_tx_rings;
6030 
6031 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
6032 				rx >>= 1;
6033 			if (cp < (rx + tx)) {
6034 				bnxt_trim_rings(bp, &rx, &tx, cp, false);
6035 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
6036 					rx <<= 1;
6037 				hw_resc->resv_rx_rings = rx;
6038 				hw_resc->resv_tx_rings = tx;
6039 			}
6040 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
6041 			hw_resc->resv_hw_ring_grps = rx;
6042 		}
6043 		hw_resc->resv_cp_rings = cp;
6044 		hw_resc->resv_stat_ctxs = stats;
6045 	}
6046 	mutex_unlock(&bp->hwrm_cmd_lock);
6047 	return 0;
6048 }
6049 
6050 /* Caller must hold bp->hwrm_cmd_lock */
__bnxt_hwrm_get_tx_rings(struct bnxt * bp,u16 fid,int * tx_rings)6051 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
6052 {
6053 	struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6054 	struct hwrm_func_qcfg_input req = {0};
6055 	int rc;
6056 
6057 	if (bp->hwrm_spec_code < 0x10601)
6058 		return 0;
6059 
6060 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
6061 	req.fid = cpu_to_le16(fid);
6062 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6063 	if (!rc)
6064 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
6065 
6066 	return rc;
6067 }
6068 
6069 static bool bnxt_rfs_supported(struct bnxt *bp);
6070 
6071 static void
__bnxt_hwrm_reserve_pf_rings(struct bnxt * bp,struct hwrm_func_cfg_input * req,int tx_rings,int rx_rings,int ring_grps,int cp_rings,int stats,int vnics)6072 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct hwrm_func_cfg_input *req,
6073 			     int tx_rings, int rx_rings, int ring_grps,
6074 			     int cp_rings, int stats, int vnics)
6075 {
6076 	u32 enables = 0;
6077 
6078 	bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_CFG, -1, -1);
6079 	req->fid = cpu_to_le16(0xffff);
6080 	enables |= tx_rings ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
6081 	req->num_tx_rings = cpu_to_le16(tx_rings);
6082 	if (BNXT_NEW_RM(bp)) {
6083 		enables |= rx_rings ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
6084 		enables |= stats ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
6085 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
6086 			enables |= cp_rings ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
6087 			enables |= tx_rings + ring_grps ?
6088 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6089 			enables |= rx_rings ?
6090 				FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6091 		} else {
6092 			enables |= cp_rings ?
6093 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6094 			enables |= ring_grps ?
6095 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS |
6096 				   FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6097 		}
6098 		enables |= vnics ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
6099 
6100 		req->num_rx_rings = cpu_to_le16(rx_rings);
6101 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
6102 			req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
6103 			req->num_msix = cpu_to_le16(cp_rings);
6104 			req->num_rsscos_ctxs =
6105 				cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
6106 		} else {
6107 			req->num_cmpl_rings = cpu_to_le16(cp_rings);
6108 			req->num_hw_ring_grps = cpu_to_le16(ring_grps);
6109 			req->num_rsscos_ctxs = cpu_to_le16(1);
6110 			if (!(bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
6111 			    bnxt_rfs_supported(bp))
6112 				req->num_rsscos_ctxs =
6113 					cpu_to_le16(ring_grps + 1);
6114 		}
6115 		req->num_stat_ctxs = cpu_to_le16(stats);
6116 		req->num_vnics = cpu_to_le16(vnics);
6117 	}
6118 	req->enables = cpu_to_le32(enables);
6119 }
6120 
6121 static void
__bnxt_hwrm_reserve_vf_rings(struct bnxt * bp,struct hwrm_func_vf_cfg_input * req,int tx_rings,int rx_rings,int ring_grps,int cp_rings,int stats,int vnics)6122 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp,
6123 			     struct hwrm_func_vf_cfg_input *req, int tx_rings,
6124 			     int rx_rings, int ring_grps, int cp_rings,
6125 			     int stats, int vnics)
6126 {
6127 	u32 enables = 0;
6128 
6129 	bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_VF_CFG, -1, -1);
6130 	enables |= tx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
6131 	enables |= rx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
6132 			      FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6133 	enables |= stats ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
6134 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
6135 		enables |= tx_rings + ring_grps ?
6136 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6137 	} else {
6138 		enables |= cp_rings ?
6139 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6140 		enables |= ring_grps ?
6141 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
6142 	}
6143 	enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
6144 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
6145 
6146 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
6147 	req->num_tx_rings = cpu_to_le16(tx_rings);
6148 	req->num_rx_rings = cpu_to_le16(rx_rings);
6149 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
6150 		req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
6151 		req->num_rsscos_ctxs = cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
6152 	} else {
6153 		req->num_cmpl_rings = cpu_to_le16(cp_rings);
6154 		req->num_hw_ring_grps = cpu_to_le16(ring_grps);
6155 		req->num_rsscos_ctxs = cpu_to_le16(BNXT_VF_MAX_RSS_CTX);
6156 	}
6157 	req->num_stat_ctxs = cpu_to_le16(stats);
6158 	req->num_vnics = cpu_to_le16(vnics);
6159 
6160 	req->enables = cpu_to_le32(enables);
6161 }
6162 
6163 static int
bnxt_hwrm_reserve_pf_rings(struct bnxt * bp,int tx_rings,int rx_rings,int ring_grps,int cp_rings,int stats,int vnics)6164 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6165 			   int ring_grps, int cp_rings, int stats, int vnics)
6166 {
6167 	struct hwrm_func_cfg_input req = {0};
6168 	int rc;
6169 
6170 	__bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6171 				     cp_rings, stats, vnics);
6172 	if (!req.enables)
6173 		return 0;
6174 
6175 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6176 	if (rc)
6177 		return rc;
6178 
6179 	if (bp->hwrm_spec_code < 0x10601)
6180 		bp->hw_resc.resv_tx_rings = tx_rings;
6181 
6182 	return bnxt_hwrm_get_rings(bp);
6183 }
6184 
6185 static int
bnxt_hwrm_reserve_vf_rings(struct bnxt * bp,int tx_rings,int rx_rings,int ring_grps,int cp_rings,int stats,int vnics)6186 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6187 			   int ring_grps, int cp_rings, int stats, int vnics)
6188 {
6189 	struct hwrm_func_vf_cfg_input req = {0};
6190 	int rc;
6191 
6192 	if (!BNXT_NEW_RM(bp)) {
6193 		bp->hw_resc.resv_tx_rings = tx_rings;
6194 		return 0;
6195 	}
6196 
6197 	__bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6198 				     cp_rings, stats, vnics);
6199 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6200 	if (rc)
6201 		return rc;
6202 
6203 	return bnxt_hwrm_get_rings(bp);
6204 }
6205 
bnxt_hwrm_reserve_rings(struct bnxt * bp,int tx,int rx,int grp,int cp,int stat,int vnic)6206 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, int tx, int rx, int grp,
6207 				   int cp, int stat, int vnic)
6208 {
6209 	if (BNXT_PF(bp))
6210 		return bnxt_hwrm_reserve_pf_rings(bp, tx, rx, grp, cp, stat,
6211 						  vnic);
6212 	else
6213 		return bnxt_hwrm_reserve_vf_rings(bp, tx, rx, grp, cp, stat,
6214 						  vnic);
6215 }
6216 
bnxt_nq_rings_in_use(struct bnxt * bp)6217 int bnxt_nq_rings_in_use(struct bnxt *bp)
6218 {
6219 	int cp = bp->cp_nr_rings;
6220 	int ulp_msix, ulp_base;
6221 
6222 	ulp_msix = bnxt_get_ulp_msix_num(bp);
6223 	if (ulp_msix) {
6224 		ulp_base = bnxt_get_ulp_msix_base(bp);
6225 		cp += ulp_msix;
6226 		if ((ulp_base + ulp_msix) > cp)
6227 			cp = ulp_base + ulp_msix;
6228 	}
6229 	return cp;
6230 }
6231 
bnxt_cp_rings_in_use(struct bnxt * bp)6232 static int bnxt_cp_rings_in_use(struct bnxt *bp)
6233 {
6234 	int cp;
6235 
6236 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6237 		return bnxt_nq_rings_in_use(bp);
6238 
6239 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
6240 	return cp;
6241 }
6242 
bnxt_get_func_stat_ctxs(struct bnxt * bp)6243 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
6244 {
6245 	int ulp_stat = bnxt_get_ulp_stat_ctxs(bp);
6246 	int cp = bp->cp_nr_rings;
6247 
6248 	if (!ulp_stat)
6249 		return cp;
6250 
6251 	if (bnxt_nq_rings_in_use(bp) > cp + bnxt_get_ulp_msix_num(bp))
6252 		return bnxt_get_ulp_msix_base(bp) + ulp_stat;
6253 
6254 	return cp + ulp_stat;
6255 }
6256 
6257 /* Check if a default RSS map needs to be setup.  This function is only
6258  * used on older firmware that does not require reserving RX rings.
6259  */
bnxt_check_rss_tbl_no_rmgr(struct bnxt * bp)6260 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
6261 {
6262 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6263 
6264 	/* The RSS map is valid for RX rings set to resv_rx_rings */
6265 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
6266 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
6267 		if (!netif_is_rxfh_configured(bp->dev))
6268 			bnxt_set_dflt_rss_indir_tbl(bp);
6269 	}
6270 }
6271 
bnxt_need_reserve_rings(struct bnxt * bp)6272 static bool bnxt_need_reserve_rings(struct bnxt *bp)
6273 {
6274 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6275 	int cp = bnxt_cp_rings_in_use(bp);
6276 	int nq = bnxt_nq_rings_in_use(bp);
6277 	int rx = bp->rx_nr_rings, stat;
6278 	int vnic = 1, grp = rx;
6279 
6280 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
6281 	    bp->hwrm_spec_code >= 0x10601)
6282 		return true;
6283 
6284 	/* Old firmware does not need RX ring reservations but we still
6285 	 * need to setup a default RSS map when needed.  With new firmware
6286 	 * we go through RX ring reservations first and then set up the
6287 	 * RSS map for the successfully reserved RX rings when needed.
6288 	 */
6289 	if (!BNXT_NEW_RM(bp)) {
6290 		bnxt_check_rss_tbl_no_rmgr(bp);
6291 		return false;
6292 	}
6293 	if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
6294 		vnic = rx + 1;
6295 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
6296 		rx <<= 1;
6297 	stat = bnxt_get_func_stat_ctxs(bp);
6298 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
6299 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
6300 	    (hw_resc->resv_hw_ring_grps != grp &&
6301 	     !(bp->flags & BNXT_FLAG_CHIP_P5)))
6302 		return true;
6303 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && BNXT_PF(bp) &&
6304 	    hw_resc->resv_irqs != nq)
6305 		return true;
6306 	return false;
6307 }
6308 
__bnxt_reserve_rings(struct bnxt * bp)6309 static int __bnxt_reserve_rings(struct bnxt *bp)
6310 {
6311 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6312 	int cp = bnxt_nq_rings_in_use(bp);
6313 	int tx = bp->tx_nr_rings;
6314 	int rx = bp->rx_nr_rings;
6315 	int grp, rx_rings, rc;
6316 	int vnic = 1, stat;
6317 	bool sh = false;
6318 
6319 	if (!bnxt_need_reserve_rings(bp))
6320 		return 0;
6321 
6322 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6323 		sh = true;
6324 	if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
6325 		vnic = rx + 1;
6326 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
6327 		rx <<= 1;
6328 	grp = bp->rx_nr_rings;
6329 	stat = bnxt_get_func_stat_ctxs(bp);
6330 
6331 	rc = bnxt_hwrm_reserve_rings(bp, tx, rx, grp, cp, stat, vnic);
6332 	if (rc)
6333 		return rc;
6334 
6335 	tx = hw_resc->resv_tx_rings;
6336 	if (BNXT_NEW_RM(bp)) {
6337 		rx = hw_resc->resv_rx_rings;
6338 		cp = hw_resc->resv_irqs;
6339 		grp = hw_resc->resv_hw_ring_grps;
6340 		vnic = hw_resc->resv_vnics;
6341 		stat = hw_resc->resv_stat_ctxs;
6342 	}
6343 
6344 	rx_rings = rx;
6345 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
6346 		if (rx >= 2) {
6347 			rx_rings = rx >> 1;
6348 		} else {
6349 			if (netif_running(bp->dev))
6350 				return -ENOMEM;
6351 
6352 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
6353 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
6354 			bp->dev->hw_features &= ~NETIF_F_LRO;
6355 			bp->dev->features &= ~NETIF_F_LRO;
6356 			bnxt_set_ring_params(bp);
6357 		}
6358 	}
6359 	rx_rings = min_t(int, rx_rings, grp);
6360 	cp = min_t(int, cp, bp->cp_nr_rings);
6361 	if (stat > bnxt_get_ulp_stat_ctxs(bp))
6362 		stat -= bnxt_get_ulp_stat_ctxs(bp);
6363 	cp = min_t(int, cp, stat);
6364 	rc = bnxt_trim_rings(bp, &rx_rings, &tx, cp, sh);
6365 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
6366 		rx = rx_rings << 1;
6367 	cp = sh ? max_t(int, tx, rx_rings) : tx + rx_rings;
6368 	bp->tx_nr_rings = tx;
6369 
6370 	/* If we cannot reserve all the RX rings, reset the RSS map only
6371 	 * if absolutely necessary
6372 	 */
6373 	if (rx_rings != bp->rx_nr_rings) {
6374 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
6375 			    rx_rings, bp->rx_nr_rings);
6376 		if ((bp->dev->priv_flags & IFF_RXFH_CONFIGURED) &&
6377 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
6378 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
6379 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
6380 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
6381 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
6382 		}
6383 	}
6384 	bp->rx_nr_rings = rx_rings;
6385 	bp->cp_nr_rings = cp;
6386 
6387 	if (!tx || !rx || !cp || !grp || !vnic || !stat)
6388 		return -ENOMEM;
6389 
6390 	if (!netif_is_rxfh_configured(bp->dev))
6391 		bnxt_set_dflt_rss_indir_tbl(bp);
6392 
6393 	return rc;
6394 }
6395 
bnxt_hwrm_check_vf_rings(struct bnxt * bp,int tx_rings,int rx_rings,int ring_grps,int cp_rings,int stats,int vnics)6396 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6397 				    int ring_grps, int cp_rings, int stats,
6398 				    int vnics)
6399 {
6400 	struct hwrm_func_vf_cfg_input req = {0};
6401 	u32 flags;
6402 
6403 	if (!BNXT_NEW_RM(bp))
6404 		return 0;
6405 
6406 	__bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6407 				     cp_rings, stats, vnics);
6408 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
6409 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6410 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6411 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6412 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
6413 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
6414 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6415 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6416 
6417 	req.flags = cpu_to_le32(flags);
6418 	return hwrm_send_message_silent(bp, &req, sizeof(req),
6419 					HWRM_CMD_TIMEOUT);
6420 }
6421 
bnxt_hwrm_check_pf_rings(struct bnxt * bp,int tx_rings,int rx_rings,int ring_grps,int cp_rings,int stats,int vnics)6422 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6423 				    int ring_grps, int cp_rings, int stats,
6424 				    int vnics)
6425 {
6426 	struct hwrm_func_cfg_input req = {0};
6427 	u32 flags;
6428 
6429 	__bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6430 				     cp_rings, stats, vnics);
6431 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
6432 	if (BNXT_NEW_RM(bp)) {
6433 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6434 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6435 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6436 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
6437 		if (bp->flags & BNXT_FLAG_CHIP_P5)
6438 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
6439 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
6440 		else
6441 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6442 	}
6443 
6444 	req.flags = cpu_to_le32(flags);
6445 	return hwrm_send_message_silent(bp, &req, sizeof(req),
6446 					HWRM_CMD_TIMEOUT);
6447 }
6448 
bnxt_hwrm_check_rings(struct bnxt * bp,int tx_rings,int rx_rings,int ring_grps,int cp_rings,int stats,int vnics)6449 static int bnxt_hwrm_check_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6450 				 int ring_grps, int cp_rings, int stats,
6451 				 int vnics)
6452 {
6453 	if (bp->hwrm_spec_code < 0x10801)
6454 		return 0;
6455 
6456 	if (BNXT_PF(bp))
6457 		return bnxt_hwrm_check_pf_rings(bp, tx_rings, rx_rings,
6458 						ring_grps, cp_rings, stats,
6459 						vnics);
6460 
6461 	return bnxt_hwrm_check_vf_rings(bp, tx_rings, rx_rings, ring_grps,
6462 					cp_rings, stats, vnics);
6463 }
6464 
bnxt_hwrm_coal_params_qcaps(struct bnxt * bp)6465 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
6466 {
6467 	struct hwrm_ring_aggint_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6468 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6469 	struct hwrm_ring_aggint_qcaps_input req = {0};
6470 	int rc;
6471 
6472 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
6473 	coal_cap->num_cmpl_dma_aggr_max = 63;
6474 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
6475 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
6476 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
6477 	coal_cap->int_lat_tmr_min_max = 65535;
6478 	coal_cap->int_lat_tmr_max_max = 65535;
6479 	coal_cap->num_cmpl_aggr_int_max = 65535;
6480 	coal_cap->timer_units = 80;
6481 
6482 	if (bp->hwrm_spec_code < 0x10902)
6483 		return;
6484 
6485 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_AGGINT_QCAPS, -1, -1);
6486 	mutex_lock(&bp->hwrm_cmd_lock);
6487 	rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6488 	if (!rc) {
6489 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
6490 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
6491 		coal_cap->num_cmpl_dma_aggr_max =
6492 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
6493 		coal_cap->num_cmpl_dma_aggr_during_int_max =
6494 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
6495 		coal_cap->cmpl_aggr_dma_tmr_max =
6496 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
6497 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
6498 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
6499 		coal_cap->int_lat_tmr_min_max =
6500 			le16_to_cpu(resp->int_lat_tmr_min_max);
6501 		coal_cap->int_lat_tmr_max_max =
6502 			le16_to_cpu(resp->int_lat_tmr_max_max);
6503 		coal_cap->num_cmpl_aggr_int_max =
6504 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
6505 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
6506 	}
6507 	mutex_unlock(&bp->hwrm_cmd_lock);
6508 }
6509 
bnxt_usec_to_coal_tmr(struct bnxt * bp,u16 usec)6510 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
6511 {
6512 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6513 
6514 	return usec * 1000 / coal_cap->timer_units;
6515 }
6516 
bnxt_hwrm_set_coal_params(struct bnxt * bp,struct bnxt_coal * hw_coal,struct hwrm_ring_cmpl_ring_cfg_aggint_params_input * req)6517 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
6518 	struct bnxt_coal *hw_coal,
6519 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
6520 {
6521 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6522 	u32 cmpl_params = coal_cap->cmpl_params;
6523 	u16 val, tmr, max, flags = 0;
6524 
6525 	max = hw_coal->bufs_per_record * 128;
6526 	if (hw_coal->budget)
6527 		max = hw_coal->bufs_per_record * hw_coal->budget;
6528 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
6529 
6530 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
6531 	req->num_cmpl_aggr_int = cpu_to_le16(val);
6532 
6533 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
6534 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
6535 
6536 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
6537 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
6538 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
6539 
6540 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
6541 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
6542 	req->int_lat_tmr_max = cpu_to_le16(tmr);
6543 
6544 	/* min timer set to 1/2 of interrupt timer */
6545 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
6546 		val = tmr / 2;
6547 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
6548 		req->int_lat_tmr_min = cpu_to_le16(val);
6549 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6550 	}
6551 
6552 	/* buf timer set to 1/4 of interrupt timer */
6553 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
6554 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
6555 
6556 	if (cmpl_params &
6557 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
6558 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
6559 		val = clamp_t(u16, tmr, 1,
6560 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
6561 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
6562 		req->enables |=
6563 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
6564 	}
6565 
6566 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
6567 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
6568 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
6569 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
6570 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
6571 	req->flags = cpu_to_le16(flags);
6572 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
6573 }
6574 
6575 /* Caller holds bp->hwrm_cmd_lock */
__bnxt_hwrm_set_coal_nq(struct bnxt * bp,struct bnxt_napi * bnapi,struct bnxt_coal * hw_coal)6576 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
6577 				   struct bnxt_coal *hw_coal)
6578 {
6579 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req = {0};
6580 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6581 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6582 	u32 nq_params = coal_cap->nq_params;
6583 	u16 tmr;
6584 
6585 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
6586 		return 0;
6587 
6588 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS,
6589 			       -1, -1);
6590 	req.ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
6591 	req.flags =
6592 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
6593 
6594 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
6595 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
6596 	req.int_lat_tmr_min = cpu_to_le16(tmr);
6597 	req.enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6598 	return _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6599 }
6600 
bnxt_hwrm_set_ring_coal(struct bnxt * bp,struct bnxt_napi * bnapi)6601 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
6602 {
6603 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0};
6604 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6605 	struct bnxt_coal coal;
6606 
6607 	/* Tick values in micro seconds.
6608 	 * 1 coal_buf x bufs_per_record = 1 completion record.
6609 	 */
6610 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
6611 
6612 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
6613 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
6614 
6615 	if (!bnapi->rx_ring)
6616 		return -ENODEV;
6617 
6618 	bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
6619 			       HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6620 
6621 	bnxt_hwrm_set_coal_params(bp, &coal, &req_rx);
6622 
6623 	req_rx.ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
6624 
6625 	return hwrm_send_message(bp, &req_rx, sizeof(req_rx),
6626 				 HWRM_CMD_TIMEOUT);
6627 }
6628 
bnxt_hwrm_set_coal(struct bnxt * bp)6629 int bnxt_hwrm_set_coal(struct bnxt *bp)
6630 {
6631 	int i, rc = 0;
6632 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
6633 							   req_tx = {0}, *req;
6634 
6635 	bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
6636 			       HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6637 	bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
6638 			       HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6639 
6640 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, &req_rx);
6641 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, &req_tx);
6642 
6643 	mutex_lock(&bp->hwrm_cmd_lock);
6644 	for (i = 0; i < bp->cp_nr_rings; i++) {
6645 		struct bnxt_napi *bnapi = bp->bnapi[i];
6646 		struct bnxt_coal *hw_coal;
6647 		u16 ring_id;
6648 
6649 		req = &req_rx;
6650 		if (!bnapi->rx_ring) {
6651 			ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6652 			req = &req_tx;
6653 		} else {
6654 			ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
6655 		}
6656 		req->ring_id = cpu_to_le16(ring_id);
6657 
6658 		rc = _hwrm_send_message(bp, req, sizeof(*req),
6659 					HWRM_CMD_TIMEOUT);
6660 		if (rc)
6661 			break;
6662 
6663 		if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6664 			continue;
6665 
6666 		if (bnapi->rx_ring && bnapi->tx_ring) {
6667 			req = &req_tx;
6668 			ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6669 			req->ring_id = cpu_to_le16(ring_id);
6670 			rc = _hwrm_send_message(bp, req, sizeof(*req),
6671 						HWRM_CMD_TIMEOUT);
6672 			if (rc)
6673 				break;
6674 		}
6675 		if (bnapi->rx_ring)
6676 			hw_coal = &bp->rx_coal;
6677 		else
6678 			hw_coal = &bp->tx_coal;
6679 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
6680 	}
6681 	mutex_unlock(&bp->hwrm_cmd_lock);
6682 	return rc;
6683 }
6684 
bnxt_hwrm_stat_ctx_free(struct bnxt * bp)6685 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
6686 {
6687 	struct hwrm_stat_ctx_clr_stats_input req0 = {0};
6688 	struct hwrm_stat_ctx_free_input req = {0};
6689 	int i;
6690 
6691 	if (!bp->bnapi)
6692 		return;
6693 
6694 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6695 		return;
6696 
6697 	bnxt_hwrm_cmd_hdr_init(bp, &req0, HWRM_STAT_CTX_CLR_STATS, -1, -1);
6698 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
6699 
6700 	mutex_lock(&bp->hwrm_cmd_lock);
6701 	for (i = 0; i < bp->cp_nr_rings; i++) {
6702 		struct bnxt_napi *bnapi = bp->bnapi[i];
6703 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6704 
6705 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
6706 			req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
6707 			if (BNXT_FW_MAJ(bp) <= 20) {
6708 				req0.stat_ctx_id = req.stat_ctx_id;
6709 				_hwrm_send_message(bp, &req0, sizeof(req0),
6710 						   HWRM_CMD_TIMEOUT);
6711 			}
6712 			_hwrm_send_message(bp, &req, sizeof(req),
6713 					   HWRM_CMD_TIMEOUT);
6714 
6715 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
6716 		}
6717 	}
6718 	mutex_unlock(&bp->hwrm_cmd_lock);
6719 }
6720 
bnxt_hwrm_stat_ctx_alloc(struct bnxt * bp)6721 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
6722 {
6723 	int rc = 0, i;
6724 	struct hwrm_stat_ctx_alloc_input req = {0};
6725 	struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
6726 
6727 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6728 		return 0;
6729 
6730 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
6731 
6732 	req.stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
6733 	req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
6734 
6735 	mutex_lock(&bp->hwrm_cmd_lock);
6736 	for (i = 0; i < bp->cp_nr_rings; i++) {
6737 		struct bnxt_napi *bnapi = bp->bnapi[i];
6738 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6739 
6740 		req.stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
6741 
6742 		rc = _hwrm_send_message(bp, &req, sizeof(req),
6743 					HWRM_CMD_TIMEOUT);
6744 		if (rc)
6745 			break;
6746 
6747 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
6748 
6749 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
6750 	}
6751 	mutex_unlock(&bp->hwrm_cmd_lock);
6752 	return rc;
6753 }
6754 
bnxt_hwrm_func_qcfg(struct bnxt * bp)6755 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
6756 {
6757 	struct hwrm_func_qcfg_input req = {0};
6758 	struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6759 	u32 min_db_offset = 0;
6760 	u16 flags;
6761 	int rc;
6762 
6763 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
6764 	req.fid = cpu_to_le16(0xffff);
6765 	mutex_lock(&bp->hwrm_cmd_lock);
6766 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6767 	if (rc)
6768 		goto func_qcfg_exit;
6769 
6770 #ifdef CONFIG_BNXT_SRIOV
6771 	if (BNXT_VF(bp)) {
6772 		struct bnxt_vf_info *vf = &bp->vf;
6773 
6774 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
6775 	} else {
6776 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
6777 	}
6778 #endif
6779 	flags = le16_to_cpu(resp->flags);
6780 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
6781 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
6782 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
6783 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
6784 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
6785 	}
6786 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
6787 		bp->flags |= BNXT_FLAG_MULTI_HOST;
6788 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
6789 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
6790 
6791 	switch (resp->port_partition_type) {
6792 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
6793 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
6794 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
6795 		bp->port_partition_type = resp->port_partition_type;
6796 		break;
6797 	}
6798 	if (bp->hwrm_spec_code < 0x10707 ||
6799 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
6800 		bp->br_mode = BRIDGE_MODE_VEB;
6801 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
6802 		bp->br_mode = BRIDGE_MODE_VEPA;
6803 	else
6804 		bp->br_mode = BRIDGE_MODE_UNDEF;
6805 
6806 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
6807 	if (!bp->max_mtu)
6808 		bp->max_mtu = BNXT_MAX_MTU;
6809 
6810 	if (bp->db_size)
6811 		goto func_qcfg_exit;
6812 
6813 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
6814 		if (BNXT_PF(bp))
6815 			min_db_offset = DB_PF_OFFSET_P5;
6816 		else
6817 			min_db_offset = DB_VF_OFFSET_P5;
6818 	}
6819 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
6820 				 1024);
6821 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
6822 	    bp->db_size <= min_db_offset)
6823 		bp->db_size = pci_resource_len(bp->pdev, 2);
6824 
6825 func_qcfg_exit:
6826 	mutex_unlock(&bp->hwrm_cmd_lock);
6827 	return rc;
6828 }
6829 
bnxt_hwrm_func_backing_store_qcaps(struct bnxt * bp)6830 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
6831 {
6832 	struct hwrm_func_backing_store_qcaps_input req = {0};
6833 	struct hwrm_func_backing_store_qcaps_output *resp =
6834 		bp->hwrm_cmd_resp_addr;
6835 	int rc;
6836 
6837 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
6838 		return 0;
6839 
6840 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_QCAPS, -1, -1);
6841 	mutex_lock(&bp->hwrm_cmd_lock);
6842 	rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6843 	if (!rc) {
6844 		struct bnxt_ctx_pg_info *ctx_pg;
6845 		struct bnxt_ctx_mem_info *ctx;
6846 		int i, tqm_rings;
6847 
6848 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
6849 		if (!ctx) {
6850 			rc = -ENOMEM;
6851 			goto ctx_err;
6852 		}
6853 		ctx->qp_max_entries = le32_to_cpu(resp->qp_max_entries);
6854 		ctx->qp_min_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
6855 		ctx->qp_max_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
6856 		ctx->qp_entry_size = le16_to_cpu(resp->qp_entry_size);
6857 		ctx->srq_max_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
6858 		ctx->srq_max_entries = le32_to_cpu(resp->srq_max_entries);
6859 		ctx->srq_entry_size = le16_to_cpu(resp->srq_entry_size);
6860 		ctx->cq_max_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
6861 		ctx->cq_max_entries = le32_to_cpu(resp->cq_max_entries);
6862 		ctx->cq_entry_size = le16_to_cpu(resp->cq_entry_size);
6863 		ctx->vnic_max_vnic_entries =
6864 			le16_to_cpu(resp->vnic_max_vnic_entries);
6865 		ctx->vnic_max_ring_table_entries =
6866 			le16_to_cpu(resp->vnic_max_ring_table_entries);
6867 		ctx->vnic_entry_size = le16_to_cpu(resp->vnic_entry_size);
6868 		ctx->stat_max_entries = le32_to_cpu(resp->stat_max_entries);
6869 		ctx->stat_entry_size = le16_to_cpu(resp->stat_entry_size);
6870 		ctx->tqm_entry_size = le16_to_cpu(resp->tqm_entry_size);
6871 		ctx->tqm_min_entries_per_ring =
6872 			le32_to_cpu(resp->tqm_min_entries_per_ring);
6873 		ctx->tqm_max_entries_per_ring =
6874 			le32_to_cpu(resp->tqm_max_entries_per_ring);
6875 		ctx->tqm_entries_multiple = resp->tqm_entries_multiple;
6876 		if (!ctx->tqm_entries_multiple)
6877 			ctx->tqm_entries_multiple = 1;
6878 		ctx->mrav_max_entries = le32_to_cpu(resp->mrav_max_entries);
6879 		ctx->mrav_entry_size = le16_to_cpu(resp->mrav_entry_size);
6880 		ctx->mrav_num_entries_units =
6881 			le16_to_cpu(resp->mrav_num_entries_units);
6882 		ctx->tim_entry_size = le16_to_cpu(resp->tim_entry_size);
6883 		ctx->tim_max_entries = le32_to_cpu(resp->tim_max_entries);
6884 		ctx->ctx_kind_initializer = resp->ctx_kind_initializer;
6885 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
6886 		if (!ctx->tqm_fp_rings_count)
6887 			ctx->tqm_fp_rings_count = bp->max_q;
6888 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
6889 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
6890 
6891 		tqm_rings = ctx->tqm_fp_rings_count + BNXT_MAX_TQM_SP_RINGS;
6892 		ctx_pg = kcalloc(tqm_rings, sizeof(*ctx_pg), GFP_KERNEL);
6893 		if (!ctx_pg) {
6894 			kfree(ctx);
6895 			rc = -ENOMEM;
6896 			goto ctx_err;
6897 		}
6898 		for (i = 0; i < tqm_rings; i++, ctx_pg++)
6899 			ctx->tqm_mem[i] = ctx_pg;
6900 		bp->ctx = ctx;
6901 	} else {
6902 		rc = 0;
6903 	}
6904 ctx_err:
6905 	mutex_unlock(&bp->hwrm_cmd_lock);
6906 	return rc;
6907 }
6908 
bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info * rmem,u8 * pg_attr,__le64 * pg_dir)6909 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
6910 				  __le64 *pg_dir)
6911 {
6912 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
6913 	if (rmem->depth >= 1) {
6914 		if (rmem->depth == 2)
6915 			*pg_attr |= 2;
6916 		else
6917 			*pg_attr |= 1;
6918 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
6919 	} else {
6920 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
6921 	}
6922 }
6923 
6924 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
6925 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
6926 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
6927 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
6928 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
6929 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
6930 
bnxt_hwrm_func_backing_store_cfg(struct bnxt * bp,u32 enables)6931 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
6932 {
6933 	struct hwrm_func_backing_store_cfg_input req = {0};
6934 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
6935 	struct bnxt_ctx_pg_info *ctx_pg;
6936 	__le32 *num_entries;
6937 	__le64 *pg_dir;
6938 	u32 flags = 0;
6939 	u8 *pg_attr;
6940 	u32 ena;
6941 	int i;
6942 
6943 	if (!ctx)
6944 		return 0;
6945 
6946 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_CFG, -1, -1);
6947 	req.enables = cpu_to_le32(enables);
6948 
6949 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
6950 		ctx_pg = &ctx->qp_mem;
6951 		req.qp_num_entries = cpu_to_le32(ctx_pg->entries);
6952 		req.qp_num_qp1_entries = cpu_to_le16(ctx->qp_min_qp1_entries);
6953 		req.qp_num_l2_entries = cpu_to_le16(ctx->qp_max_l2_entries);
6954 		req.qp_entry_size = cpu_to_le16(ctx->qp_entry_size);
6955 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6956 				      &req.qpc_pg_size_qpc_lvl,
6957 				      &req.qpc_page_dir);
6958 	}
6959 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
6960 		ctx_pg = &ctx->srq_mem;
6961 		req.srq_num_entries = cpu_to_le32(ctx_pg->entries);
6962 		req.srq_num_l2_entries = cpu_to_le16(ctx->srq_max_l2_entries);
6963 		req.srq_entry_size = cpu_to_le16(ctx->srq_entry_size);
6964 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6965 				      &req.srq_pg_size_srq_lvl,
6966 				      &req.srq_page_dir);
6967 	}
6968 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
6969 		ctx_pg = &ctx->cq_mem;
6970 		req.cq_num_entries = cpu_to_le32(ctx_pg->entries);
6971 		req.cq_num_l2_entries = cpu_to_le16(ctx->cq_max_l2_entries);
6972 		req.cq_entry_size = cpu_to_le16(ctx->cq_entry_size);
6973 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, &req.cq_pg_size_cq_lvl,
6974 				      &req.cq_page_dir);
6975 	}
6976 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
6977 		ctx_pg = &ctx->vnic_mem;
6978 		req.vnic_num_vnic_entries =
6979 			cpu_to_le16(ctx->vnic_max_vnic_entries);
6980 		req.vnic_num_ring_table_entries =
6981 			cpu_to_le16(ctx->vnic_max_ring_table_entries);
6982 		req.vnic_entry_size = cpu_to_le16(ctx->vnic_entry_size);
6983 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6984 				      &req.vnic_pg_size_vnic_lvl,
6985 				      &req.vnic_page_dir);
6986 	}
6987 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
6988 		ctx_pg = &ctx->stat_mem;
6989 		req.stat_num_entries = cpu_to_le32(ctx->stat_max_entries);
6990 		req.stat_entry_size = cpu_to_le16(ctx->stat_entry_size);
6991 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6992 				      &req.stat_pg_size_stat_lvl,
6993 				      &req.stat_page_dir);
6994 	}
6995 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
6996 		ctx_pg = &ctx->mrav_mem;
6997 		req.mrav_num_entries = cpu_to_le32(ctx_pg->entries);
6998 		if (ctx->mrav_num_entries_units)
6999 			flags |=
7000 			FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
7001 		req.mrav_entry_size = cpu_to_le16(ctx->mrav_entry_size);
7002 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7003 				      &req.mrav_pg_size_mrav_lvl,
7004 				      &req.mrav_page_dir);
7005 	}
7006 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
7007 		ctx_pg = &ctx->tim_mem;
7008 		req.tim_num_entries = cpu_to_le32(ctx_pg->entries);
7009 		req.tim_entry_size = cpu_to_le16(ctx->tim_entry_size);
7010 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7011 				      &req.tim_pg_size_tim_lvl,
7012 				      &req.tim_page_dir);
7013 	}
7014 	for (i = 0, num_entries = &req.tqm_sp_num_entries,
7015 	     pg_attr = &req.tqm_sp_pg_size_tqm_sp_lvl,
7016 	     pg_dir = &req.tqm_sp_page_dir,
7017 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP;
7018 	     i < BNXT_MAX_TQM_RINGS;
7019 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
7020 		if (!(enables & ena))
7021 			continue;
7022 
7023 		req.tqm_entry_size = cpu_to_le16(ctx->tqm_entry_size);
7024 		ctx_pg = ctx->tqm_mem[i];
7025 		*num_entries = cpu_to_le32(ctx_pg->entries);
7026 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
7027 	}
7028 	req.flags = cpu_to_le32(flags);
7029 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7030 }
7031 
bnxt_alloc_ctx_mem_blk(struct bnxt * bp,struct bnxt_ctx_pg_info * ctx_pg)7032 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
7033 				  struct bnxt_ctx_pg_info *ctx_pg)
7034 {
7035 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7036 
7037 	rmem->page_size = BNXT_PAGE_SIZE;
7038 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
7039 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
7040 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
7041 	if (rmem->depth >= 1)
7042 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
7043 	return bnxt_alloc_ring(bp, rmem);
7044 }
7045 
bnxt_alloc_ctx_pg_tbls(struct bnxt * bp,struct bnxt_ctx_pg_info * ctx_pg,u32 mem_size,u8 depth,bool use_init_val)7046 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
7047 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
7048 				  u8 depth, bool use_init_val)
7049 {
7050 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7051 	int rc;
7052 
7053 	if (!mem_size)
7054 		return -EINVAL;
7055 
7056 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
7057 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
7058 		ctx_pg->nr_pages = 0;
7059 		return -EINVAL;
7060 	}
7061 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
7062 		int nr_tbls, i;
7063 
7064 		rmem->depth = 2;
7065 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
7066 					     GFP_KERNEL);
7067 		if (!ctx_pg->ctx_pg_tbl)
7068 			return -ENOMEM;
7069 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
7070 		rmem->nr_pages = nr_tbls;
7071 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
7072 		if (rc)
7073 			return rc;
7074 		for (i = 0; i < nr_tbls; i++) {
7075 			struct bnxt_ctx_pg_info *pg_tbl;
7076 
7077 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
7078 			if (!pg_tbl)
7079 				return -ENOMEM;
7080 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
7081 			rmem = &pg_tbl->ring_mem;
7082 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
7083 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
7084 			rmem->depth = 1;
7085 			rmem->nr_pages = MAX_CTX_PAGES;
7086 			if (use_init_val)
7087 				rmem->init_val = bp->ctx->ctx_kind_initializer;
7088 			if (i == (nr_tbls - 1)) {
7089 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
7090 
7091 				if (rem)
7092 					rmem->nr_pages = rem;
7093 			}
7094 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
7095 			if (rc)
7096 				break;
7097 		}
7098 	} else {
7099 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
7100 		if (rmem->nr_pages > 1 || depth)
7101 			rmem->depth = 1;
7102 		if (use_init_val)
7103 			rmem->init_val = bp->ctx->ctx_kind_initializer;
7104 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
7105 	}
7106 	return rc;
7107 }
7108 
bnxt_free_ctx_pg_tbls(struct bnxt * bp,struct bnxt_ctx_pg_info * ctx_pg)7109 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
7110 				  struct bnxt_ctx_pg_info *ctx_pg)
7111 {
7112 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7113 
7114 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
7115 	    ctx_pg->ctx_pg_tbl) {
7116 		int i, nr_tbls = rmem->nr_pages;
7117 
7118 		for (i = 0; i < nr_tbls; i++) {
7119 			struct bnxt_ctx_pg_info *pg_tbl;
7120 			struct bnxt_ring_mem_info *rmem2;
7121 
7122 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
7123 			if (!pg_tbl)
7124 				continue;
7125 			rmem2 = &pg_tbl->ring_mem;
7126 			bnxt_free_ring(bp, rmem2);
7127 			ctx_pg->ctx_pg_arr[i] = NULL;
7128 			kfree(pg_tbl);
7129 			ctx_pg->ctx_pg_tbl[i] = NULL;
7130 		}
7131 		kfree(ctx_pg->ctx_pg_tbl);
7132 		ctx_pg->ctx_pg_tbl = NULL;
7133 	}
7134 	bnxt_free_ring(bp, rmem);
7135 	ctx_pg->nr_pages = 0;
7136 }
7137 
bnxt_free_ctx_mem(struct bnxt * bp)7138 static void bnxt_free_ctx_mem(struct bnxt *bp)
7139 {
7140 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
7141 	int i;
7142 
7143 	if (!ctx)
7144 		return;
7145 
7146 	if (ctx->tqm_mem[0]) {
7147 		for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
7148 			bnxt_free_ctx_pg_tbls(bp, ctx->tqm_mem[i]);
7149 		kfree(ctx->tqm_mem[0]);
7150 		ctx->tqm_mem[0] = NULL;
7151 	}
7152 
7153 	bnxt_free_ctx_pg_tbls(bp, &ctx->tim_mem);
7154 	bnxt_free_ctx_pg_tbls(bp, &ctx->mrav_mem);
7155 	bnxt_free_ctx_pg_tbls(bp, &ctx->stat_mem);
7156 	bnxt_free_ctx_pg_tbls(bp, &ctx->vnic_mem);
7157 	bnxt_free_ctx_pg_tbls(bp, &ctx->cq_mem);
7158 	bnxt_free_ctx_pg_tbls(bp, &ctx->srq_mem);
7159 	bnxt_free_ctx_pg_tbls(bp, &ctx->qp_mem);
7160 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
7161 }
7162 
bnxt_alloc_ctx_mem(struct bnxt * bp)7163 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
7164 {
7165 	struct bnxt_ctx_pg_info *ctx_pg;
7166 	struct bnxt_ctx_mem_info *ctx;
7167 	u32 mem_size, ena, entries;
7168 	u32 entries_sp, min;
7169 	u32 num_mr, num_ah;
7170 	u32 extra_srqs = 0;
7171 	u32 extra_qps = 0;
7172 	u8 pg_lvl = 1;
7173 	int i, rc;
7174 
7175 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
7176 	if (rc) {
7177 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
7178 			   rc);
7179 		return rc;
7180 	}
7181 	ctx = bp->ctx;
7182 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
7183 		return 0;
7184 
7185 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
7186 		pg_lvl = 2;
7187 		extra_qps = 65536;
7188 		extra_srqs = 8192;
7189 	}
7190 
7191 	ctx_pg = &ctx->qp_mem;
7192 	ctx_pg->entries = ctx->qp_min_qp1_entries + ctx->qp_max_l2_entries +
7193 			  extra_qps;
7194 	mem_size = ctx->qp_entry_size * ctx_pg->entries;
7195 	rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, true);
7196 	if (rc)
7197 		return rc;
7198 
7199 	ctx_pg = &ctx->srq_mem;
7200 	ctx_pg->entries = ctx->srq_max_l2_entries + extra_srqs;
7201 	mem_size = ctx->srq_entry_size * ctx_pg->entries;
7202 	rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, true);
7203 	if (rc)
7204 		return rc;
7205 
7206 	ctx_pg = &ctx->cq_mem;
7207 	ctx_pg->entries = ctx->cq_max_l2_entries + extra_qps * 2;
7208 	mem_size = ctx->cq_entry_size * ctx_pg->entries;
7209 	rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, true);
7210 	if (rc)
7211 		return rc;
7212 
7213 	ctx_pg = &ctx->vnic_mem;
7214 	ctx_pg->entries = ctx->vnic_max_vnic_entries +
7215 			  ctx->vnic_max_ring_table_entries;
7216 	mem_size = ctx->vnic_entry_size * ctx_pg->entries;
7217 	rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, true);
7218 	if (rc)
7219 		return rc;
7220 
7221 	ctx_pg = &ctx->stat_mem;
7222 	ctx_pg->entries = ctx->stat_max_entries;
7223 	mem_size = ctx->stat_entry_size * ctx_pg->entries;
7224 	rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, true);
7225 	if (rc)
7226 		return rc;
7227 
7228 	ena = 0;
7229 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
7230 		goto skip_rdma;
7231 
7232 	ctx_pg = &ctx->mrav_mem;
7233 	/* 128K extra is needed to accommodate static AH context
7234 	 * allocation by f/w.
7235 	 */
7236 	num_mr = 1024 * 256;
7237 	num_ah = 1024 * 128;
7238 	ctx_pg->entries = num_mr + num_ah;
7239 	mem_size = ctx->mrav_entry_size * ctx_pg->entries;
7240 	rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 2, true);
7241 	if (rc)
7242 		return rc;
7243 	ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
7244 	if (ctx->mrav_num_entries_units)
7245 		ctx_pg->entries =
7246 			((num_mr / ctx->mrav_num_entries_units) << 16) |
7247 			 (num_ah / ctx->mrav_num_entries_units);
7248 
7249 	ctx_pg = &ctx->tim_mem;
7250 	ctx_pg->entries = ctx->qp_mem.entries;
7251 	mem_size = ctx->tim_entry_size * ctx_pg->entries;
7252 	rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, false);
7253 	if (rc)
7254 		return rc;
7255 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
7256 
7257 skip_rdma:
7258 	min = ctx->tqm_min_entries_per_ring;
7259 	entries_sp = ctx->vnic_max_vnic_entries + ctx->qp_max_l2_entries +
7260 		     2 * (extra_qps + ctx->qp_min_qp1_entries) + min;
7261 	entries_sp = roundup(entries_sp, ctx->tqm_entries_multiple);
7262 	entries = ctx->qp_max_l2_entries + 2 * (extra_qps + ctx->qp_min_qp1_entries);
7263 	entries = roundup(entries, ctx->tqm_entries_multiple);
7264 	entries = clamp_t(u32, entries, min, ctx->tqm_max_entries_per_ring);
7265 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++) {
7266 		ctx_pg = ctx->tqm_mem[i];
7267 		ctx_pg->entries = i ? entries : entries_sp;
7268 		mem_size = ctx->tqm_entry_size * ctx_pg->entries;
7269 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, false);
7270 		if (rc)
7271 			return rc;
7272 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
7273 	}
7274 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
7275 	rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
7276 	if (rc) {
7277 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
7278 			   rc);
7279 		return rc;
7280 	}
7281 	ctx->flags |= BNXT_CTX_FLAG_INITED;
7282 	return 0;
7283 }
7284 
bnxt_hwrm_func_resc_qcaps(struct bnxt * bp,bool all)7285 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
7286 {
7287 	struct hwrm_func_resource_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
7288 	struct hwrm_func_resource_qcaps_input req = {0};
7289 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7290 	int rc;
7291 
7292 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESOURCE_QCAPS, -1, -1);
7293 	req.fid = cpu_to_le16(0xffff);
7294 
7295 	mutex_lock(&bp->hwrm_cmd_lock);
7296 	rc = _hwrm_send_message_silent(bp, &req, sizeof(req),
7297 				       HWRM_CMD_TIMEOUT);
7298 	if (rc)
7299 		goto hwrm_func_resc_qcaps_exit;
7300 
7301 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
7302 	if (!all)
7303 		goto hwrm_func_resc_qcaps_exit;
7304 
7305 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
7306 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
7307 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
7308 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
7309 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
7310 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
7311 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
7312 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
7313 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
7314 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
7315 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
7316 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
7317 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
7318 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
7319 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
7320 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
7321 
7322 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
7323 		u16 max_msix = le16_to_cpu(resp->max_msix);
7324 
7325 		hw_resc->max_nqs = max_msix;
7326 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
7327 	}
7328 
7329 	if (BNXT_PF(bp)) {
7330 		struct bnxt_pf_info *pf = &bp->pf;
7331 
7332 		pf->vf_resv_strategy =
7333 			le16_to_cpu(resp->vf_reservation_strategy);
7334 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
7335 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
7336 	}
7337 hwrm_func_resc_qcaps_exit:
7338 	mutex_unlock(&bp->hwrm_cmd_lock);
7339 	return rc;
7340 }
7341 
__bnxt_hwrm_func_qcaps(struct bnxt * bp)7342 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
7343 {
7344 	int rc = 0;
7345 	struct hwrm_func_qcaps_input req = {0};
7346 	struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
7347 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7348 	u32 flags, flags_ext;
7349 
7350 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
7351 	req.fid = cpu_to_le16(0xffff);
7352 
7353 	mutex_lock(&bp->hwrm_cmd_lock);
7354 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7355 	if (rc)
7356 		goto hwrm_func_qcaps_exit;
7357 
7358 	flags = le32_to_cpu(resp->flags);
7359 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
7360 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
7361 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
7362 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
7363 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
7364 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
7365 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
7366 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
7367 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
7368 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
7369 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
7370 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
7371 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
7372 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
7373 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
7374 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
7375 
7376 	flags_ext = le32_to_cpu(resp->flags_ext);
7377 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
7378 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
7379 
7380 	bp->tx_push_thresh = 0;
7381 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
7382 	    BNXT_FW_MAJ(bp) > 217)
7383 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
7384 
7385 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
7386 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
7387 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
7388 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
7389 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
7390 	if (!hw_resc->max_hw_ring_grps)
7391 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
7392 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
7393 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
7394 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
7395 
7396 	if (BNXT_PF(bp)) {
7397 		struct bnxt_pf_info *pf = &bp->pf;
7398 
7399 		pf->fw_fid = le16_to_cpu(resp->fid);
7400 		pf->port_id = le16_to_cpu(resp->port_id);
7401 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
7402 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
7403 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
7404 		pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
7405 		pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
7406 		pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
7407 		pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
7408 		pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
7409 		pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
7410 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
7411 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
7412 			bp->flags |= BNXT_FLAG_WOL_CAP;
7413 	} else {
7414 #ifdef CONFIG_BNXT_SRIOV
7415 		struct bnxt_vf_info *vf = &bp->vf;
7416 
7417 		vf->fw_fid = le16_to_cpu(resp->fid);
7418 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
7419 #endif
7420 	}
7421 
7422 hwrm_func_qcaps_exit:
7423 	mutex_unlock(&bp->hwrm_cmd_lock);
7424 	return rc;
7425 }
7426 
7427 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
7428 
bnxt_hwrm_func_qcaps(struct bnxt * bp)7429 static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
7430 {
7431 	int rc;
7432 
7433 	rc = __bnxt_hwrm_func_qcaps(bp);
7434 	if (rc)
7435 		return rc;
7436 	rc = bnxt_hwrm_queue_qportcfg(bp);
7437 	if (rc) {
7438 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
7439 		return rc;
7440 	}
7441 	if (bp->hwrm_spec_code >= 0x10803) {
7442 		rc = bnxt_alloc_ctx_mem(bp);
7443 		if (rc)
7444 			return rc;
7445 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
7446 		if (!rc)
7447 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
7448 	}
7449 	return 0;
7450 }
7451 
bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt * bp)7452 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
7453 {
7454 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input req = {0};
7455 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
7456 	int rc = 0;
7457 	u32 flags;
7458 
7459 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
7460 		return 0;
7461 
7462 	resp = bp->hwrm_cmd_resp_addr;
7463 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS, -1, -1);
7464 
7465 	mutex_lock(&bp->hwrm_cmd_lock);
7466 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7467 	if (rc)
7468 		goto hwrm_cfa_adv_qcaps_exit;
7469 
7470 	flags = le32_to_cpu(resp->flags);
7471 	if (flags &
7472 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
7473 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
7474 
7475 hwrm_cfa_adv_qcaps_exit:
7476 	mutex_unlock(&bp->hwrm_cmd_lock);
7477 	return rc;
7478 }
7479 
__bnxt_alloc_fw_health(struct bnxt * bp)7480 static int __bnxt_alloc_fw_health(struct bnxt *bp)
7481 {
7482 	if (bp->fw_health)
7483 		return 0;
7484 
7485 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
7486 	if (!bp->fw_health)
7487 		return -ENOMEM;
7488 
7489 	return 0;
7490 }
7491 
bnxt_alloc_fw_health(struct bnxt * bp)7492 static int bnxt_alloc_fw_health(struct bnxt *bp)
7493 {
7494 	int rc;
7495 
7496 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
7497 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
7498 		return 0;
7499 
7500 	rc = __bnxt_alloc_fw_health(bp);
7501 	if (rc) {
7502 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
7503 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
7504 		return rc;
7505 	}
7506 
7507 	return 0;
7508 }
7509 
__bnxt_map_fw_health_reg(struct bnxt * bp,u32 reg)7510 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
7511 {
7512 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
7513 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
7514 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
7515 }
7516 
bnxt_try_map_fw_health_reg(struct bnxt * bp)7517 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
7518 {
7519 	void __iomem *hs;
7520 	u32 status_loc;
7521 	u32 reg_type;
7522 	u32 sig;
7523 
7524 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
7525 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
7526 
7527 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
7528 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
7529 		if (bp->fw_health)
7530 			bp->fw_health->status_reliable = false;
7531 		return;
7532 	}
7533 
7534 	if (__bnxt_alloc_fw_health(bp)) {
7535 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
7536 		return;
7537 	}
7538 
7539 	status_loc = readl(hs + offsetof(struct hcomm_status, fw_status_loc));
7540 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
7541 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
7542 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
7543 		__bnxt_map_fw_health_reg(bp, status_loc);
7544 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
7545 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
7546 	}
7547 
7548 	bp->fw_health->status_reliable = true;
7549 }
7550 
bnxt_map_fw_health_regs(struct bnxt * bp)7551 static int bnxt_map_fw_health_regs(struct bnxt *bp)
7552 {
7553 	struct bnxt_fw_health *fw_health = bp->fw_health;
7554 	u32 reg_base = 0xffffffff;
7555 	int i;
7556 
7557 	/* Only pre-map the monitoring GRC registers using window 3 */
7558 	for (i = 0; i < 4; i++) {
7559 		u32 reg = fw_health->regs[i];
7560 
7561 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
7562 			continue;
7563 		if (reg_base == 0xffffffff)
7564 			reg_base = reg & BNXT_GRC_BASE_MASK;
7565 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
7566 			return -ERANGE;
7567 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
7568 	}
7569 	if (reg_base == 0xffffffff)
7570 		return 0;
7571 
7572 	__bnxt_map_fw_health_reg(bp, reg_base);
7573 	return 0;
7574 }
7575 
bnxt_hwrm_error_recovery_qcfg(struct bnxt * bp)7576 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
7577 {
7578 	struct hwrm_error_recovery_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
7579 	struct bnxt_fw_health *fw_health = bp->fw_health;
7580 	struct hwrm_error_recovery_qcfg_input req = {0};
7581 	int rc, i;
7582 
7583 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
7584 		return 0;
7585 
7586 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_ERROR_RECOVERY_QCFG, -1, -1);
7587 	mutex_lock(&bp->hwrm_cmd_lock);
7588 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7589 	if (rc)
7590 		goto err_recovery_out;
7591 	fw_health->flags = le32_to_cpu(resp->flags);
7592 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
7593 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
7594 		rc = -EINVAL;
7595 		goto err_recovery_out;
7596 	}
7597 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
7598 	fw_health->master_func_wait_dsecs =
7599 		le32_to_cpu(resp->master_func_wait_period);
7600 	fw_health->normal_func_wait_dsecs =
7601 		le32_to_cpu(resp->normal_func_wait_period);
7602 	fw_health->post_reset_wait_dsecs =
7603 		le32_to_cpu(resp->master_func_wait_period_after_reset);
7604 	fw_health->post_reset_max_wait_dsecs =
7605 		le32_to_cpu(resp->max_bailout_time_after_reset);
7606 	fw_health->regs[BNXT_FW_HEALTH_REG] =
7607 		le32_to_cpu(resp->fw_health_status_reg);
7608 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
7609 		le32_to_cpu(resp->fw_heartbeat_reg);
7610 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
7611 		le32_to_cpu(resp->fw_reset_cnt_reg);
7612 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
7613 		le32_to_cpu(resp->reset_inprogress_reg);
7614 	fw_health->fw_reset_inprog_reg_mask =
7615 		le32_to_cpu(resp->reset_inprogress_reg_mask);
7616 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
7617 	if (fw_health->fw_reset_seq_cnt >= 16) {
7618 		rc = -EINVAL;
7619 		goto err_recovery_out;
7620 	}
7621 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
7622 		fw_health->fw_reset_seq_regs[i] =
7623 			le32_to_cpu(resp->reset_reg[i]);
7624 		fw_health->fw_reset_seq_vals[i] =
7625 			le32_to_cpu(resp->reset_reg_val[i]);
7626 		fw_health->fw_reset_seq_delay_msec[i] =
7627 			resp->delay_after_reset[i];
7628 	}
7629 err_recovery_out:
7630 	mutex_unlock(&bp->hwrm_cmd_lock);
7631 	if (!rc)
7632 		rc = bnxt_map_fw_health_regs(bp);
7633 	if (rc)
7634 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
7635 	return rc;
7636 }
7637 
bnxt_hwrm_func_reset(struct bnxt * bp)7638 static int bnxt_hwrm_func_reset(struct bnxt *bp)
7639 {
7640 	struct hwrm_func_reset_input req = {0};
7641 
7642 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
7643 	req.enables = 0;
7644 
7645 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
7646 }
7647 
bnxt_nvm_cfg_ver_get(struct bnxt * bp)7648 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
7649 {
7650 	struct hwrm_nvm_get_dev_info_output nvm_info;
7651 
7652 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
7653 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
7654 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
7655 			 nvm_info.nvm_cfg_ver_upd);
7656 }
7657 
bnxt_hwrm_queue_qportcfg(struct bnxt * bp)7658 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
7659 {
7660 	int rc = 0;
7661 	struct hwrm_queue_qportcfg_input req = {0};
7662 	struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
7663 	u8 i, j, *qptr;
7664 	bool no_rdma;
7665 
7666 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
7667 
7668 	mutex_lock(&bp->hwrm_cmd_lock);
7669 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7670 	if (rc)
7671 		goto qportcfg_exit;
7672 
7673 	if (!resp->max_configurable_queues) {
7674 		rc = -EINVAL;
7675 		goto qportcfg_exit;
7676 	}
7677 	bp->max_tc = resp->max_configurable_queues;
7678 	bp->max_lltc = resp->max_configurable_lossless_queues;
7679 	if (bp->max_tc > BNXT_MAX_QUEUE)
7680 		bp->max_tc = BNXT_MAX_QUEUE;
7681 
7682 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
7683 	qptr = &resp->queue_id0;
7684 	for (i = 0, j = 0; i < bp->max_tc; i++) {
7685 		bp->q_info[j].queue_id = *qptr;
7686 		bp->q_ids[i] = *qptr++;
7687 		bp->q_info[j].queue_profile = *qptr++;
7688 		bp->tc_to_qidx[j] = j;
7689 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
7690 		    (no_rdma && BNXT_PF(bp)))
7691 			j++;
7692 	}
7693 	bp->max_q = bp->max_tc;
7694 	bp->max_tc = max_t(u8, j, 1);
7695 
7696 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
7697 		bp->max_tc = 1;
7698 
7699 	if (bp->max_lltc > bp->max_tc)
7700 		bp->max_lltc = bp->max_tc;
7701 
7702 qportcfg_exit:
7703 	mutex_unlock(&bp->hwrm_cmd_lock);
7704 	return rc;
7705 }
7706 
__bnxt_hwrm_ver_get(struct bnxt * bp,bool silent)7707 static int __bnxt_hwrm_ver_get(struct bnxt *bp, bool silent)
7708 {
7709 	struct hwrm_ver_get_input req = {0};
7710 	int rc;
7711 
7712 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
7713 	req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
7714 	req.hwrm_intf_min = HWRM_VERSION_MINOR;
7715 	req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
7716 
7717 	rc = bnxt_hwrm_do_send_msg(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT,
7718 				   silent);
7719 	return rc;
7720 }
7721 
bnxt_hwrm_ver_get(struct bnxt * bp)7722 static int bnxt_hwrm_ver_get(struct bnxt *bp)
7723 {
7724 	struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
7725 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
7726 	u32 dev_caps_cfg, hwrm_ver;
7727 	int rc, len;
7728 
7729 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
7730 	mutex_lock(&bp->hwrm_cmd_lock);
7731 	rc = __bnxt_hwrm_ver_get(bp, false);
7732 	if (rc)
7733 		goto hwrm_ver_get_exit;
7734 
7735 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
7736 
7737 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
7738 			     resp->hwrm_intf_min_8b << 8 |
7739 			     resp->hwrm_intf_upd_8b;
7740 	if (resp->hwrm_intf_maj_8b < 1) {
7741 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
7742 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
7743 			    resp->hwrm_intf_upd_8b);
7744 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
7745 	}
7746 
7747 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
7748 			HWRM_VERSION_UPDATE;
7749 
7750 	if (bp->hwrm_spec_code > hwrm_ver)
7751 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
7752 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
7753 			 HWRM_VERSION_UPDATE);
7754 	else
7755 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
7756 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
7757 			 resp->hwrm_intf_upd_8b);
7758 
7759 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
7760 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
7761 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
7762 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
7763 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
7764 		len = FW_VER_STR_LEN;
7765 	} else {
7766 		fw_maj = resp->hwrm_fw_maj_8b;
7767 		fw_min = resp->hwrm_fw_min_8b;
7768 		fw_bld = resp->hwrm_fw_bld_8b;
7769 		fw_rsv = resp->hwrm_fw_rsvd_8b;
7770 		len = BC_HWRM_STR_LEN;
7771 	}
7772 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
7773 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
7774 		 fw_rsv);
7775 
7776 	if (strlen(resp->active_pkg_name)) {
7777 		int fw_ver_len = strlen(bp->fw_ver_str);
7778 
7779 		snprintf(bp->fw_ver_str + fw_ver_len,
7780 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
7781 			 resp->active_pkg_name);
7782 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
7783 	}
7784 
7785 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
7786 	if (!bp->hwrm_cmd_timeout)
7787 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
7788 
7789 	if (resp->hwrm_intf_maj_8b >= 1) {
7790 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
7791 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
7792 	}
7793 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
7794 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
7795 
7796 	bp->chip_num = le16_to_cpu(resp->chip_num);
7797 	bp->chip_rev = resp->chip_rev;
7798 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
7799 	    !resp->chip_metal)
7800 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
7801 
7802 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
7803 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
7804 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
7805 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
7806 
7807 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
7808 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
7809 
7810 	if (dev_caps_cfg &
7811 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
7812 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
7813 
7814 	if (dev_caps_cfg &
7815 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
7816 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
7817 
7818 	if (dev_caps_cfg &
7819 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
7820 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
7821 
7822 hwrm_ver_get_exit:
7823 	mutex_unlock(&bp->hwrm_cmd_lock);
7824 	return rc;
7825 }
7826 
bnxt_hwrm_fw_set_time(struct bnxt * bp)7827 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
7828 {
7829 	struct hwrm_fw_set_time_input req = {0};
7830 	struct tm tm;
7831 	time64_t now = ktime_get_real_seconds();
7832 
7833 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
7834 	    bp->hwrm_spec_code < 0x10400)
7835 		return -EOPNOTSUPP;
7836 
7837 	time64_to_tm(now, 0, &tm);
7838 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
7839 	req.year = cpu_to_le16(1900 + tm.tm_year);
7840 	req.month = 1 + tm.tm_mon;
7841 	req.day = tm.tm_mday;
7842 	req.hour = tm.tm_hour;
7843 	req.minute = tm.tm_min;
7844 	req.second = tm.tm_sec;
7845 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7846 }
7847 
bnxt_add_one_ctr(u64 hw,u64 * sw,u64 mask)7848 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
7849 {
7850 	u64 sw_tmp;
7851 
7852 	hw &= mask;
7853 	sw_tmp = (*sw & ~mask) | hw;
7854 	if (hw < (*sw & mask))
7855 		sw_tmp += mask + 1;
7856 	WRITE_ONCE(*sw, sw_tmp);
7857 }
7858 
__bnxt_accumulate_stats(__le64 * hw_stats,u64 * sw_stats,u64 * masks,int count,bool ignore_zero)7859 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
7860 				    int count, bool ignore_zero)
7861 {
7862 	int i;
7863 
7864 	for (i = 0; i < count; i++) {
7865 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
7866 
7867 		if (ignore_zero && !hw)
7868 			continue;
7869 
7870 		if (masks[i] == -1ULL)
7871 			sw_stats[i] = hw;
7872 		else
7873 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
7874 	}
7875 }
7876 
bnxt_accumulate_stats(struct bnxt_stats_mem * stats)7877 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
7878 {
7879 	if (!stats->hw_stats)
7880 		return;
7881 
7882 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
7883 				stats->hw_masks, stats->len / 8, false);
7884 }
7885 
bnxt_accumulate_all_stats(struct bnxt * bp)7886 static void bnxt_accumulate_all_stats(struct bnxt *bp)
7887 {
7888 	struct bnxt_stats_mem *ring0_stats;
7889 	bool ignore_zero = false;
7890 	int i;
7891 
7892 	/* Chip bug.  Counter intermittently becomes 0. */
7893 	if (bp->flags & BNXT_FLAG_CHIP_P5)
7894 		ignore_zero = true;
7895 
7896 	for (i = 0; i < bp->cp_nr_rings; i++) {
7897 		struct bnxt_napi *bnapi = bp->bnapi[i];
7898 		struct bnxt_cp_ring_info *cpr;
7899 		struct bnxt_stats_mem *stats;
7900 
7901 		cpr = &bnapi->cp_ring;
7902 		stats = &cpr->stats;
7903 		if (!i)
7904 			ring0_stats = stats;
7905 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
7906 					ring0_stats->hw_masks,
7907 					ring0_stats->len / 8, ignore_zero);
7908 	}
7909 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
7910 		struct bnxt_stats_mem *stats = &bp->port_stats;
7911 		__le64 *hw_stats = stats->hw_stats;
7912 		u64 *sw_stats = stats->sw_stats;
7913 		u64 *masks = stats->hw_masks;
7914 		int cnt;
7915 
7916 		cnt = sizeof(struct rx_port_stats) / 8;
7917 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
7918 
7919 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
7920 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
7921 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
7922 		cnt = sizeof(struct tx_port_stats) / 8;
7923 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
7924 	}
7925 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
7926 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
7927 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
7928 	}
7929 }
7930 
bnxt_hwrm_port_qstats(struct bnxt * bp,u8 flags)7931 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
7932 {
7933 	struct bnxt_pf_info *pf = &bp->pf;
7934 	struct hwrm_port_qstats_input req = {0};
7935 
7936 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
7937 		return 0;
7938 
7939 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
7940 		return -EOPNOTSUPP;
7941 
7942 	req.flags = flags;
7943 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
7944 	req.port_id = cpu_to_le16(pf->port_id);
7945 	req.tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
7946 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
7947 	req.rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
7948 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7949 }
7950 
bnxt_hwrm_port_qstats_ext(struct bnxt * bp,u8 flags)7951 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
7952 {
7953 	struct hwrm_port_qstats_ext_output *resp = bp->hwrm_cmd_resp_addr;
7954 	struct hwrm_queue_pri2cos_qcfg_input req2 = {0};
7955 	struct hwrm_port_qstats_ext_input req = {0};
7956 	struct bnxt_pf_info *pf = &bp->pf;
7957 	u32 tx_stat_size;
7958 	int rc;
7959 
7960 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
7961 		return 0;
7962 
7963 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
7964 		return -EOPNOTSUPP;
7965 
7966 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS_EXT, -1, -1);
7967 	req.flags = flags;
7968 	req.port_id = cpu_to_le16(pf->port_id);
7969 	req.rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
7970 	req.rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
7971 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
7972 		       sizeof(struct tx_port_stats_ext) : 0;
7973 	req.tx_stat_size = cpu_to_le16(tx_stat_size);
7974 	req.tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
7975 	mutex_lock(&bp->hwrm_cmd_lock);
7976 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7977 	if (!rc) {
7978 		bp->fw_rx_stats_ext_size = le16_to_cpu(resp->rx_stat_size) / 8;
7979 		bp->fw_tx_stats_ext_size = tx_stat_size ?
7980 			le16_to_cpu(resp->tx_stat_size) / 8 : 0;
7981 	} else {
7982 		bp->fw_rx_stats_ext_size = 0;
7983 		bp->fw_tx_stats_ext_size = 0;
7984 	}
7985 	if (flags)
7986 		goto qstats_done;
7987 
7988 	if (bp->fw_tx_stats_ext_size <=
7989 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
7990 		mutex_unlock(&bp->hwrm_cmd_lock);
7991 		bp->pri2cos_valid = 0;
7992 		return rc;
7993 	}
7994 
7995 	bnxt_hwrm_cmd_hdr_init(bp, &req2, HWRM_QUEUE_PRI2COS_QCFG, -1, -1);
7996 	req2.flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
7997 
7998 	rc = _hwrm_send_message(bp, &req2, sizeof(req2), HWRM_CMD_TIMEOUT);
7999 	if (!rc) {
8000 		struct hwrm_queue_pri2cos_qcfg_output *resp2;
8001 		u8 *pri2cos;
8002 		int i, j;
8003 
8004 		resp2 = bp->hwrm_cmd_resp_addr;
8005 		pri2cos = &resp2->pri0_cos_queue_id;
8006 		for (i = 0; i < 8; i++) {
8007 			u8 queue_id = pri2cos[i];
8008 			u8 queue_idx;
8009 
8010 			/* Per port queue IDs start from 0, 10, 20, etc */
8011 			queue_idx = queue_id % 10;
8012 			if (queue_idx > BNXT_MAX_QUEUE) {
8013 				bp->pri2cos_valid = false;
8014 				goto qstats_done;
8015 			}
8016 			for (j = 0; j < bp->max_q; j++) {
8017 				if (bp->q_ids[j] == queue_id)
8018 					bp->pri2cos_idx[i] = queue_idx;
8019 			}
8020 		}
8021 		bp->pri2cos_valid = 1;
8022 	}
8023 qstats_done:
8024 	mutex_unlock(&bp->hwrm_cmd_lock);
8025 	return rc;
8026 }
8027 
bnxt_hwrm_free_tunnel_ports(struct bnxt * bp)8028 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
8029 {
8030 	if (bp->vxlan_fw_dst_port_id != INVALID_HW_RING_ID)
8031 		bnxt_hwrm_tunnel_dst_port_free(
8032 			bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
8033 	if (bp->nge_fw_dst_port_id != INVALID_HW_RING_ID)
8034 		bnxt_hwrm_tunnel_dst_port_free(
8035 			bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
8036 }
8037 
bnxt_set_tpa(struct bnxt * bp,bool set_tpa)8038 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
8039 {
8040 	int rc, i;
8041 	u32 tpa_flags = 0;
8042 
8043 	if (set_tpa)
8044 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
8045 	else if (BNXT_NO_FW_ACCESS(bp))
8046 		return 0;
8047 	for (i = 0; i < bp->nr_vnics; i++) {
8048 		rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
8049 		if (rc) {
8050 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
8051 				   i, rc);
8052 			return rc;
8053 		}
8054 	}
8055 	return 0;
8056 }
8057 
bnxt_hwrm_clear_vnic_rss(struct bnxt * bp)8058 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
8059 {
8060 	int i;
8061 
8062 	for (i = 0; i < bp->nr_vnics; i++)
8063 		bnxt_hwrm_vnic_set_rss(bp, i, false);
8064 }
8065 
bnxt_clear_vnic(struct bnxt * bp)8066 static void bnxt_clear_vnic(struct bnxt *bp)
8067 {
8068 	if (!bp->vnic_info)
8069 		return;
8070 
8071 	bnxt_hwrm_clear_vnic_filter(bp);
8072 	if (!(bp->flags & BNXT_FLAG_CHIP_P5)) {
8073 		/* clear all RSS setting before free vnic ctx */
8074 		bnxt_hwrm_clear_vnic_rss(bp);
8075 		bnxt_hwrm_vnic_ctx_free(bp);
8076 	}
8077 	/* before free the vnic, undo the vnic tpa settings */
8078 	if (bp->flags & BNXT_FLAG_TPA)
8079 		bnxt_set_tpa(bp, false);
8080 	bnxt_hwrm_vnic_free(bp);
8081 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8082 		bnxt_hwrm_vnic_ctx_free(bp);
8083 }
8084 
bnxt_hwrm_resource_free(struct bnxt * bp,bool close_path,bool irq_re_init)8085 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
8086 				    bool irq_re_init)
8087 {
8088 	bnxt_clear_vnic(bp);
8089 	bnxt_hwrm_ring_free(bp, close_path);
8090 	bnxt_hwrm_ring_grp_free(bp);
8091 	if (irq_re_init) {
8092 		bnxt_hwrm_stat_ctx_free(bp);
8093 		bnxt_hwrm_free_tunnel_ports(bp);
8094 	}
8095 }
8096 
bnxt_hwrm_set_br_mode(struct bnxt * bp,u16 br_mode)8097 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
8098 {
8099 	struct hwrm_func_cfg_input req = {0};
8100 
8101 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
8102 	req.fid = cpu_to_le16(0xffff);
8103 	req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
8104 	if (br_mode == BRIDGE_MODE_VEB)
8105 		req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
8106 	else if (br_mode == BRIDGE_MODE_VEPA)
8107 		req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
8108 	else
8109 		return -EINVAL;
8110 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8111 }
8112 
bnxt_hwrm_set_cache_line_size(struct bnxt * bp,int size)8113 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
8114 {
8115 	struct hwrm_func_cfg_input req = {0};
8116 
8117 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
8118 		return 0;
8119 
8120 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
8121 	req.fid = cpu_to_le16(0xffff);
8122 	req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
8123 	req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
8124 	if (size == 128)
8125 		req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
8126 
8127 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8128 }
8129 
__bnxt_setup_vnic(struct bnxt * bp,u16 vnic_id)8130 static int __bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
8131 {
8132 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
8133 	int rc;
8134 
8135 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
8136 		goto skip_rss_ctx;
8137 
8138 	/* allocate context for vnic */
8139 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
8140 	if (rc) {
8141 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
8142 			   vnic_id, rc);
8143 		goto vnic_setup_err;
8144 	}
8145 	bp->rsscos_nr_ctxs++;
8146 
8147 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8148 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
8149 		if (rc) {
8150 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
8151 				   vnic_id, rc);
8152 			goto vnic_setup_err;
8153 		}
8154 		bp->rsscos_nr_ctxs++;
8155 	}
8156 
8157 skip_rss_ctx:
8158 	/* configure default vnic, ring grp */
8159 	rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
8160 	if (rc) {
8161 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
8162 			   vnic_id, rc);
8163 		goto vnic_setup_err;
8164 	}
8165 
8166 	/* Enable RSS hashing on vnic */
8167 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
8168 	if (rc) {
8169 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
8170 			   vnic_id, rc);
8171 		goto vnic_setup_err;
8172 	}
8173 
8174 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8175 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
8176 		if (rc) {
8177 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
8178 				   vnic_id, rc);
8179 		}
8180 	}
8181 
8182 vnic_setup_err:
8183 	return rc;
8184 }
8185 
__bnxt_setup_vnic_p5(struct bnxt * bp,u16 vnic_id)8186 static int __bnxt_setup_vnic_p5(struct bnxt *bp, u16 vnic_id)
8187 {
8188 	int rc, i, nr_ctxs;
8189 
8190 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
8191 	for (i = 0; i < nr_ctxs; i++) {
8192 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, i);
8193 		if (rc) {
8194 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
8195 				   vnic_id, i, rc);
8196 			break;
8197 		}
8198 		bp->rsscos_nr_ctxs++;
8199 	}
8200 	if (i < nr_ctxs)
8201 		return -ENOMEM;
8202 
8203 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic_id, true);
8204 	if (rc) {
8205 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
8206 			   vnic_id, rc);
8207 		return rc;
8208 	}
8209 	rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
8210 	if (rc) {
8211 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
8212 			   vnic_id, rc);
8213 		return rc;
8214 	}
8215 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8216 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
8217 		if (rc) {
8218 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
8219 				   vnic_id, rc);
8220 		}
8221 	}
8222 	return rc;
8223 }
8224 
bnxt_setup_vnic(struct bnxt * bp,u16 vnic_id)8225 static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
8226 {
8227 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8228 		return __bnxt_setup_vnic_p5(bp, vnic_id);
8229 	else
8230 		return __bnxt_setup_vnic(bp, vnic_id);
8231 }
8232 
bnxt_alloc_rfs_vnics(struct bnxt * bp)8233 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
8234 {
8235 #ifdef CONFIG_RFS_ACCEL
8236 	int i, rc = 0;
8237 
8238 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8239 		return 0;
8240 
8241 	for (i = 0; i < bp->rx_nr_rings; i++) {
8242 		struct bnxt_vnic_info *vnic;
8243 		u16 vnic_id = i + 1;
8244 		u16 ring_id = i;
8245 
8246 		if (vnic_id >= bp->nr_vnics)
8247 			break;
8248 
8249 		vnic = &bp->vnic_info[vnic_id];
8250 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
8251 		if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
8252 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
8253 		rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
8254 		if (rc) {
8255 			netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
8256 				   vnic_id, rc);
8257 			break;
8258 		}
8259 		rc = bnxt_setup_vnic(bp, vnic_id);
8260 		if (rc)
8261 			break;
8262 	}
8263 	return rc;
8264 #else
8265 	return 0;
8266 #endif
8267 }
8268 
8269 /* Allow PF and VF with default VLAN to be in promiscuous mode */
bnxt_promisc_ok(struct bnxt * bp)8270 static bool bnxt_promisc_ok(struct bnxt *bp)
8271 {
8272 #ifdef CONFIG_BNXT_SRIOV
8273 	if (BNXT_VF(bp) && !bp->vf.vlan)
8274 		return false;
8275 #endif
8276 	return true;
8277 }
8278 
bnxt_setup_nitroa0_vnic(struct bnxt * bp)8279 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
8280 {
8281 	unsigned int rc = 0;
8282 
8283 	rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
8284 	if (rc) {
8285 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
8286 			   rc);
8287 		return rc;
8288 	}
8289 
8290 	rc = bnxt_hwrm_vnic_cfg(bp, 1);
8291 	if (rc) {
8292 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
8293 			   rc);
8294 		return rc;
8295 	}
8296 	return rc;
8297 }
8298 
8299 static int bnxt_cfg_rx_mode(struct bnxt *);
8300 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
8301 
bnxt_init_chip(struct bnxt * bp,bool irq_re_init)8302 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
8303 {
8304 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8305 	int rc = 0;
8306 	unsigned int rx_nr_rings = bp->rx_nr_rings;
8307 
8308 	if (irq_re_init) {
8309 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
8310 		if (rc) {
8311 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
8312 				   rc);
8313 			goto err_out;
8314 		}
8315 	}
8316 
8317 	rc = bnxt_hwrm_ring_alloc(bp);
8318 	if (rc) {
8319 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
8320 		goto err_out;
8321 	}
8322 
8323 	rc = bnxt_hwrm_ring_grp_alloc(bp);
8324 	if (rc) {
8325 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
8326 		goto err_out;
8327 	}
8328 
8329 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8330 		rx_nr_rings--;
8331 
8332 	/* default vnic 0 */
8333 	rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
8334 	if (rc) {
8335 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
8336 		goto err_out;
8337 	}
8338 
8339 	rc = bnxt_setup_vnic(bp, 0);
8340 	if (rc)
8341 		goto err_out;
8342 
8343 	if (bp->flags & BNXT_FLAG_RFS) {
8344 		rc = bnxt_alloc_rfs_vnics(bp);
8345 		if (rc)
8346 			goto err_out;
8347 	}
8348 
8349 	if (bp->flags & BNXT_FLAG_TPA) {
8350 		rc = bnxt_set_tpa(bp, true);
8351 		if (rc)
8352 			goto err_out;
8353 	}
8354 
8355 	if (BNXT_VF(bp))
8356 		bnxt_update_vf_mac(bp);
8357 
8358 	/* Filter for default vnic 0 */
8359 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
8360 	if (rc) {
8361 		netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
8362 		goto err_out;
8363 	}
8364 	vnic->uc_filter_count = 1;
8365 
8366 	vnic->rx_mask = 0;
8367 	if (bp->dev->flags & IFF_BROADCAST)
8368 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
8369 
8370 	if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
8371 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
8372 
8373 	if (bp->dev->flags & IFF_ALLMULTI) {
8374 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
8375 		vnic->mc_list_count = 0;
8376 	} else {
8377 		u32 mask = 0;
8378 
8379 		bnxt_mc_list_updated(bp, &mask);
8380 		vnic->rx_mask |= mask;
8381 	}
8382 
8383 	rc = bnxt_cfg_rx_mode(bp);
8384 	if (rc)
8385 		goto err_out;
8386 
8387 	rc = bnxt_hwrm_set_coal(bp);
8388 	if (rc)
8389 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
8390 				rc);
8391 
8392 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8393 		rc = bnxt_setup_nitroa0_vnic(bp);
8394 		if (rc)
8395 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
8396 				   rc);
8397 	}
8398 
8399 	if (BNXT_VF(bp)) {
8400 		bnxt_hwrm_func_qcfg(bp);
8401 		netdev_update_features(bp->dev);
8402 	}
8403 
8404 	return 0;
8405 
8406 err_out:
8407 	bnxt_hwrm_resource_free(bp, 0, true);
8408 
8409 	return rc;
8410 }
8411 
bnxt_shutdown_nic(struct bnxt * bp,bool irq_re_init)8412 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
8413 {
8414 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
8415 	return 0;
8416 }
8417 
bnxt_init_nic(struct bnxt * bp,bool irq_re_init)8418 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
8419 {
8420 	bnxt_init_cp_rings(bp);
8421 	bnxt_init_rx_rings(bp);
8422 	bnxt_init_tx_rings(bp);
8423 	bnxt_init_ring_grps(bp, irq_re_init);
8424 	bnxt_init_vnics(bp);
8425 
8426 	return bnxt_init_chip(bp, irq_re_init);
8427 }
8428 
bnxt_set_real_num_queues(struct bnxt * bp)8429 static int bnxt_set_real_num_queues(struct bnxt *bp)
8430 {
8431 	int rc;
8432 	struct net_device *dev = bp->dev;
8433 
8434 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
8435 					  bp->tx_nr_rings_xdp);
8436 	if (rc)
8437 		return rc;
8438 
8439 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
8440 	if (rc)
8441 		return rc;
8442 
8443 #ifdef CONFIG_RFS_ACCEL
8444 	if (bp->flags & BNXT_FLAG_RFS)
8445 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
8446 #endif
8447 
8448 	return rc;
8449 }
8450 
bnxt_trim_rings(struct bnxt * bp,int * rx,int * tx,int max,bool shared)8451 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
8452 			   bool shared)
8453 {
8454 	int _rx = *rx, _tx = *tx;
8455 
8456 	if (shared) {
8457 		*rx = min_t(int, _rx, max);
8458 		*tx = min_t(int, _tx, max);
8459 	} else {
8460 		if (max < 2)
8461 			return -ENOMEM;
8462 
8463 		while (_rx + _tx > max) {
8464 			if (_rx > _tx && _rx > 1)
8465 				_rx--;
8466 			else if (_tx > 1)
8467 				_tx--;
8468 		}
8469 		*rx = _rx;
8470 		*tx = _tx;
8471 	}
8472 	return 0;
8473 }
8474 
bnxt_setup_msix(struct bnxt * bp)8475 static void bnxt_setup_msix(struct bnxt *bp)
8476 {
8477 	const int len = sizeof(bp->irq_tbl[0].name);
8478 	struct net_device *dev = bp->dev;
8479 	int tcs, i;
8480 
8481 	tcs = netdev_get_num_tc(dev);
8482 	if (tcs) {
8483 		int i, off, count;
8484 
8485 		for (i = 0; i < tcs; i++) {
8486 			count = bp->tx_nr_rings_per_tc;
8487 			off = i * count;
8488 			netdev_set_tc_queue(dev, i, count, off);
8489 		}
8490 	}
8491 
8492 	for (i = 0; i < bp->cp_nr_rings; i++) {
8493 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
8494 		char *attr;
8495 
8496 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8497 			attr = "TxRx";
8498 		else if (i < bp->rx_nr_rings)
8499 			attr = "rx";
8500 		else
8501 			attr = "tx";
8502 
8503 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
8504 			 attr, i);
8505 		bp->irq_tbl[map_idx].handler = bnxt_msix;
8506 	}
8507 }
8508 
bnxt_setup_inta(struct bnxt * bp)8509 static void bnxt_setup_inta(struct bnxt *bp)
8510 {
8511 	const int len = sizeof(bp->irq_tbl[0].name);
8512 
8513 	if (netdev_get_num_tc(bp->dev))
8514 		netdev_reset_tc(bp->dev);
8515 
8516 	snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
8517 		 0);
8518 	bp->irq_tbl[0].handler = bnxt_inta;
8519 }
8520 
8521 static int bnxt_init_int_mode(struct bnxt *bp);
8522 
bnxt_setup_int_mode(struct bnxt * bp)8523 static int bnxt_setup_int_mode(struct bnxt *bp)
8524 {
8525 	int rc;
8526 
8527 	if (!bp->irq_tbl) {
8528 		rc = bnxt_init_int_mode(bp);
8529 		if (rc || !bp->irq_tbl)
8530 			return rc ?: -ENODEV;
8531 	}
8532 
8533 	if (bp->flags & BNXT_FLAG_USING_MSIX)
8534 		bnxt_setup_msix(bp);
8535 	else
8536 		bnxt_setup_inta(bp);
8537 
8538 	rc = bnxt_set_real_num_queues(bp);
8539 	return rc;
8540 }
8541 
8542 #ifdef CONFIG_RFS_ACCEL
bnxt_get_max_func_rss_ctxs(struct bnxt * bp)8543 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
8544 {
8545 	return bp->hw_resc.max_rsscos_ctxs;
8546 }
8547 
bnxt_get_max_func_vnics(struct bnxt * bp)8548 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
8549 {
8550 	return bp->hw_resc.max_vnics;
8551 }
8552 #endif
8553 
bnxt_get_max_func_stat_ctxs(struct bnxt * bp)8554 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
8555 {
8556 	return bp->hw_resc.max_stat_ctxs;
8557 }
8558 
bnxt_get_max_func_cp_rings(struct bnxt * bp)8559 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
8560 {
8561 	return bp->hw_resc.max_cp_rings;
8562 }
8563 
bnxt_get_max_func_cp_rings_for_en(struct bnxt * bp)8564 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
8565 {
8566 	unsigned int cp = bp->hw_resc.max_cp_rings;
8567 
8568 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
8569 		cp -= bnxt_get_ulp_msix_num(bp);
8570 
8571 	return cp;
8572 }
8573 
bnxt_get_max_func_irqs(struct bnxt * bp)8574 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
8575 {
8576 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8577 
8578 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8579 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
8580 
8581 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
8582 }
8583 
bnxt_set_max_func_irqs(struct bnxt * bp,unsigned int max_irqs)8584 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
8585 {
8586 	bp->hw_resc.max_irqs = max_irqs;
8587 }
8588 
bnxt_get_avail_cp_rings_for_en(struct bnxt * bp)8589 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
8590 {
8591 	unsigned int cp;
8592 
8593 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
8594 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8595 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
8596 	else
8597 		return cp - bp->cp_nr_rings;
8598 }
8599 
bnxt_get_avail_stat_ctxs_for_en(struct bnxt * bp)8600 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
8601 {
8602 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
8603 }
8604 
bnxt_get_avail_msix(struct bnxt * bp,int num)8605 int bnxt_get_avail_msix(struct bnxt *bp, int num)
8606 {
8607 	int max_cp = bnxt_get_max_func_cp_rings(bp);
8608 	int max_irq = bnxt_get_max_func_irqs(bp);
8609 	int total_req = bp->cp_nr_rings + num;
8610 	int max_idx, avail_msix;
8611 
8612 	max_idx = bp->total_irqs;
8613 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
8614 		max_idx = min_t(int, bp->total_irqs, max_cp);
8615 	avail_msix = max_idx - bp->cp_nr_rings;
8616 	if (!BNXT_NEW_RM(bp) || avail_msix >= num)
8617 		return avail_msix;
8618 
8619 	if (max_irq < total_req) {
8620 		num = max_irq - bp->cp_nr_rings;
8621 		if (num <= 0)
8622 			return 0;
8623 	}
8624 	return num;
8625 }
8626 
bnxt_get_num_msix(struct bnxt * bp)8627 static int bnxt_get_num_msix(struct bnxt *bp)
8628 {
8629 	if (!BNXT_NEW_RM(bp))
8630 		return bnxt_get_max_func_irqs(bp);
8631 
8632 	return bnxt_nq_rings_in_use(bp);
8633 }
8634 
bnxt_init_msix(struct bnxt * bp)8635 static int bnxt_init_msix(struct bnxt *bp)
8636 {
8637 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix;
8638 	struct msix_entry *msix_ent;
8639 
8640 	total_vecs = bnxt_get_num_msix(bp);
8641 	max = bnxt_get_max_func_irqs(bp);
8642 	if (total_vecs > max)
8643 		total_vecs = max;
8644 
8645 	if (!total_vecs)
8646 		return 0;
8647 
8648 	msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
8649 	if (!msix_ent)
8650 		return -ENOMEM;
8651 
8652 	for (i = 0; i < total_vecs; i++) {
8653 		msix_ent[i].entry = i;
8654 		msix_ent[i].vector = 0;
8655 	}
8656 
8657 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
8658 		min = 2;
8659 
8660 	total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
8661 	ulp_msix = bnxt_get_ulp_msix_num(bp);
8662 	if (total_vecs < 0 || total_vecs < ulp_msix) {
8663 		rc = -ENODEV;
8664 		goto msix_setup_exit;
8665 	}
8666 
8667 	bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
8668 	if (bp->irq_tbl) {
8669 		for (i = 0; i < total_vecs; i++)
8670 			bp->irq_tbl[i].vector = msix_ent[i].vector;
8671 
8672 		bp->total_irqs = total_vecs;
8673 		/* Trim rings based upon num of vectors allocated */
8674 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
8675 				     total_vecs - ulp_msix, min == 1);
8676 		if (rc)
8677 			goto msix_setup_exit;
8678 
8679 		bp->cp_nr_rings = (min == 1) ?
8680 				  max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
8681 				  bp->tx_nr_rings + bp->rx_nr_rings;
8682 
8683 	} else {
8684 		rc = -ENOMEM;
8685 		goto msix_setup_exit;
8686 	}
8687 	bp->flags |= BNXT_FLAG_USING_MSIX;
8688 	kfree(msix_ent);
8689 	return 0;
8690 
8691 msix_setup_exit:
8692 	netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
8693 	kfree(bp->irq_tbl);
8694 	bp->irq_tbl = NULL;
8695 	pci_disable_msix(bp->pdev);
8696 	kfree(msix_ent);
8697 	return rc;
8698 }
8699 
bnxt_init_inta(struct bnxt * bp)8700 static int bnxt_init_inta(struct bnxt *bp)
8701 {
8702 	bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
8703 	if (!bp->irq_tbl)
8704 		return -ENOMEM;
8705 
8706 	bp->total_irqs = 1;
8707 	bp->rx_nr_rings = 1;
8708 	bp->tx_nr_rings = 1;
8709 	bp->cp_nr_rings = 1;
8710 	bp->flags |= BNXT_FLAG_SHARED_RINGS;
8711 	bp->irq_tbl[0].vector = bp->pdev->irq;
8712 	return 0;
8713 }
8714 
bnxt_init_int_mode(struct bnxt * bp)8715 static int bnxt_init_int_mode(struct bnxt *bp)
8716 {
8717 	int rc = -ENODEV;
8718 
8719 	if (bp->flags & BNXT_FLAG_MSIX_CAP)
8720 		rc = bnxt_init_msix(bp);
8721 
8722 	if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
8723 		/* fallback to INTA */
8724 		rc = bnxt_init_inta(bp);
8725 	}
8726 	return rc;
8727 }
8728 
bnxt_clear_int_mode(struct bnxt * bp)8729 static void bnxt_clear_int_mode(struct bnxt *bp)
8730 {
8731 	if (bp->flags & BNXT_FLAG_USING_MSIX)
8732 		pci_disable_msix(bp->pdev);
8733 
8734 	kfree(bp->irq_tbl);
8735 	bp->irq_tbl = NULL;
8736 	bp->flags &= ~BNXT_FLAG_USING_MSIX;
8737 }
8738 
bnxt_reserve_rings(struct bnxt * bp,bool irq_re_init)8739 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
8740 {
8741 	int tcs = netdev_get_num_tc(bp->dev);
8742 	bool irq_cleared = false;
8743 	int rc;
8744 
8745 	if (!bnxt_need_reserve_rings(bp))
8746 		return 0;
8747 
8748 	if (irq_re_init && BNXT_NEW_RM(bp) &&
8749 	    bnxt_get_num_msix(bp) != bp->total_irqs) {
8750 		bnxt_ulp_irq_stop(bp);
8751 		bnxt_clear_int_mode(bp);
8752 		irq_cleared = true;
8753 	}
8754 	rc = __bnxt_reserve_rings(bp);
8755 	if (irq_cleared) {
8756 		if (!rc)
8757 			rc = bnxt_init_int_mode(bp);
8758 		bnxt_ulp_irq_restart(bp, rc);
8759 	}
8760 	if (rc) {
8761 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
8762 		return rc;
8763 	}
8764 	if (tcs && (bp->tx_nr_rings_per_tc * tcs != bp->tx_nr_rings)) {
8765 		netdev_err(bp->dev, "tx ring reservation failure\n");
8766 		netdev_reset_tc(bp->dev);
8767 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
8768 		return -ENOMEM;
8769 	}
8770 	return 0;
8771 }
8772 
bnxt_free_irq(struct bnxt * bp)8773 static void bnxt_free_irq(struct bnxt *bp)
8774 {
8775 	struct bnxt_irq *irq;
8776 	int i;
8777 
8778 #ifdef CONFIG_RFS_ACCEL
8779 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
8780 	bp->dev->rx_cpu_rmap = NULL;
8781 #endif
8782 	if (!bp->irq_tbl || !bp->bnapi)
8783 		return;
8784 
8785 	for (i = 0; i < bp->cp_nr_rings; i++) {
8786 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
8787 
8788 		irq = &bp->irq_tbl[map_idx];
8789 		if (irq->requested) {
8790 			if (irq->have_cpumask) {
8791 				irq_set_affinity_hint(irq->vector, NULL);
8792 				free_cpumask_var(irq->cpu_mask);
8793 				irq->have_cpumask = 0;
8794 			}
8795 			free_irq(irq->vector, bp->bnapi[i]);
8796 		}
8797 
8798 		irq->requested = 0;
8799 	}
8800 }
8801 
bnxt_request_irq(struct bnxt * bp)8802 static int bnxt_request_irq(struct bnxt *bp)
8803 {
8804 	int i, j, rc = 0;
8805 	unsigned long flags = 0;
8806 #ifdef CONFIG_RFS_ACCEL
8807 	struct cpu_rmap *rmap;
8808 #endif
8809 
8810 	rc = bnxt_setup_int_mode(bp);
8811 	if (rc) {
8812 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
8813 			   rc);
8814 		return rc;
8815 	}
8816 #ifdef CONFIG_RFS_ACCEL
8817 	rmap = bp->dev->rx_cpu_rmap;
8818 #endif
8819 	if (!(bp->flags & BNXT_FLAG_USING_MSIX))
8820 		flags = IRQF_SHARED;
8821 
8822 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
8823 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
8824 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
8825 
8826 #ifdef CONFIG_RFS_ACCEL
8827 		if (rmap && bp->bnapi[i]->rx_ring) {
8828 			rc = irq_cpu_rmap_add(rmap, irq->vector);
8829 			if (rc)
8830 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
8831 					    j);
8832 			j++;
8833 		}
8834 #endif
8835 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
8836 				 bp->bnapi[i]);
8837 		if (rc)
8838 			break;
8839 
8840 		irq->requested = 1;
8841 
8842 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
8843 			int numa_node = dev_to_node(&bp->pdev->dev);
8844 
8845 			irq->have_cpumask = 1;
8846 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
8847 					irq->cpu_mask);
8848 			rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
8849 			if (rc) {
8850 				netdev_warn(bp->dev,
8851 					    "Set affinity failed, IRQ = %d\n",
8852 					    irq->vector);
8853 				break;
8854 			}
8855 		}
8856 	}
8857 	return rc;
8858 }
8859 
bnxt_del_napi(struct bnxt * bp)8860 static void bnxt_del_napi(struct bnxt *bp)
8861 {
8862 	int i;
8863 
8864 	if (!bp->bnapi)
8865 		return;
8866 
8867 	for (i = 0; i < bp->cp_nr_rings; i++) {
8868 		struct bnxt_napi *bnapi = bp->bnapi[i];
8869 
8870 		__netif_napi_del(&bnapi->napi);
8871 	}
8872 	/* We called __netif_napi_del(), we need
8873 	 * to respect an RCU grace period before freeing napi structures.
8874 	 */
8875 	synchronize_net();
8876 }
8877 
bnxt_init_napi(struct bnxt * bp)8878 static void bnxt_init_napi(struct bnxt *bp)
8879 {
8880 	int i;
8881 	unsigned int cp_nr_rings = bp->cp_nr_rings;
8882 	struct bnxt_napi *bnapi;
8883 
8884 	if (bp->flags & BNXT_FLAG_USING_MSIX) {
8885 		int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
8886 
8887 		if (bp->flags & BNXT_FLAG_CHIP_P5)
8888 			poll_fn = bnxt_poll_p5;
8889 		else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8890 			cp_nr_rings--;
8891 		for (i = 0; i < cp_nr_rings; i++) {
8892 			bnapi = bp->bnapi[i];
8893 			netif_napi_add(bp->dev, &bnapi->napi, poll_fn, 64);
8894 		}
8895 		if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8896 			bnapi = bp->bnapi[cp_nr_rings];
8897 			netif_napi_add(bp->dev, &bnapi->napi,
8898 				       bnxt_poll_nitroa0, 64);
8899 		}
8900 	} else {
8901 		bnapi = bp->bnapi[0];
8902 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
8903 	}
8904 }
8905 
bnxt_disable_napi(struct bnxt * bp)8906 static void bnxt_disable_napi(struct bnxt *bp)
8907 {
8908 	int i;
8909 
8910 	if (!bp->bnapi)
8911 		return;
8912 
8913 	for (i = 0; i < bp->cp_nr_rings; i++) {
8914 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
8915 
8916 		napi_disable(&bp->bnapi[i]->napi);
8917 		if (bp->bnapi[i]->rx_ring)
8918 			cancel_work_sync(&cpr->dim.work);
8919 	}
8920 }
8921 
bnxt_enable_napi(struct bnxt * bp)8922 static void bnxt_enable_napi(struct bnxt *bp)
8923 {
8924 	int i;
8925 
8926 	for (i = 0; i < bp->cp_nr_rings; i++) {
8927 		struct bnxt_napi *bnapi = bp->bnapi[i];
8928 		struct bnxt_cp_ring_info *cpr;
8929 
8930 		cpr = &bnapi->cp_ring;
8931 		if (bnapi->in_reset)
8932 			cpr->sw_stats.rx.rx_resets++;
8933 		bnapi->in_reset = false;
8934 
8935 		if (bnapi->rx_ring) {
8936 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
8937 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
8938 		}
8939 		napi_enable(&bnapi->napi);
8940 	}
8941 }
8942 
bnxt_tx_disable(struct bnxt * bp)8943 void bnxt_tx_disable(struct bnxt *bp)
8944 {
8945 	int i;
8946 	struct bnxt_tx_ring_info *txr;
8947 
8948 	if (bp->tx_ring) {
8949 		for (i = 0; i < bp->tx_nr_rings; i++) {
8950 			txr = &bp->tx_ring[i];
8951 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
8952 		}
8953 	}
8954 	/* Make sure napi polls see @dev_state change */
8955 	synchronize_net();
8956 	/* Drop carrier first to prevent TX timeout */
8957 	netif_carrier_off(bp->dev);
8958 	/* Stop all TX queues */
8959 	netif_tx_disable(bp->dev);
8960 }
8961 
bnxt_tx_enable(struct bnxt * bp)8962 void bnxt_tx_enable(struct bnxt *bp)
8963 {
8964 	int i;
8965 	struct bnxt_tx_ring_info *txr;
8966 
8967 	for (i = 0; i < bp->tx_nr_rings; i++) {
8968 		txr = &bp->tx_ring[i];
8969 		WRITE_ONCE(txr->dev_state, 0);
8970 	}
8971 	/* Make sure napi polls see @dev_state change */
8972 	synchronize_net();
8973 	netif_tx_wake_all_queues(bp->dev);
8974 	if (bp->link_info.link_up)
8975 		netif_carrier_on(bp->dev);
8976 }
8977 
bnxt_report_fec(struct bnxt_link_info * link_info)8978 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
8979 {
8980 	u8 active_fec = link_info->active_fec_sig_mode &
8981 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
8982 
8983 	switch (active_fec) {
8984 	default:
8985 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
8986 		return "None";
8987 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
8988 		return "Clause 74 BaseR";
8989 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
8990 		return "Clause 91 RS(528,514)";
8991 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
8992 		return "Clause 91 RS544_1XN";
8993 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
8994 		return "Clause 91 RS(544,514)";
8995 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
8996 		return "Clause 91 RS272_1XN";
8997 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
8998 		return "Clause 91 RS(272,257)";
8999 	}
9000 }
9001 
bnxt_report_link(struct bnxt * bp)9002 static void bnxt_report_link(struct bnxt *bp)
9003 {
9004 	if (bp->link_info.link_up) {
9005 		const char *duplex;
9006 		const char *flow_ctrl;
9007 		u32 speed;
9008 		u16 fec;
9009 
9010 		netif_carrier_on(bp->dev);
9011 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
9012 		if (speed == SPEED_UNKNOWN) {
9013 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
9014 			return;
9015 		}
9016 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
9017 			duplex = "full";
9018 		else
9019 			duplex = "half";
9020 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
9021 			flow_ctrl = "ON - receive & transmit";
9022 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
9023 			flow_ctrl = "ON - transmit";
9024 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
9025 			flow_ctrl = "ON - receive";
9026 		else
9027 			flow_ctrl = "none";
9028 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
9029 			    speed, duplex, flow_ctrl);
9030 		if (bp->flags & BNXT_FLAG_EEE_CAP)
9031 			netdev_info(bp->dev, "EEE is %s\n",
9032 				    bp->eee.eee_active ? "active" :
9033 							 "not active");
9034 		fec = bp->link_info.fec_cfg;
9035 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
9036 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
9037 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
9038 				    bnxt_report_fec(&bp->link_info));
9039 	} else {
9040 		netif_carrier_off(bp->dev);
9041 		netdev_err(bp->dev, "NIC Link is Down\n");
9042 	}
9043 }
9044 
bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output * resp)9045 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
9046 {
9047 	if (!resp->supported_speeds_auto_mode &&
9048 	    !resp->supported_speeds_force_mode &&
9049 	    !resp->supported_pam4_speeds_auto_mode &&
9050 	    !resp->supported_pam4_speeds_force_mode)
9051 		return true;
9052 	return false;
9053 }
9054 
bnxt_hwrm_phy_qcaps(struct bnxt * bp)9055 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
9056 {
9057 	int rc = 0;
9058 	struct hwrm_port_phy_qcaps_input req = {0};
9059 	struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
9060 	struct bnxt_link_info *link_info = &bp->link_info;
9061 
9062 	bp->flags &= ~BNXT_FLAG_EEE_CAP;
9063 	if (bp->test_info)
9064 		bp->test_info->flags &= ~(BNXT_TEST_FL_EXT_LPBK |
9065 					  BNXT_TEST_FL_AN_PHY_LPBK);
9066 	if (bp->hwrm_spec_code < 0x10201)
9067 		return 0;
9068 
9069 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
9070 
9071 	mutex_lock(&bp->hwrm_cmd_lock);
9072 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9073 	if (rc)
9074 		goto hwrm_phy_qcaps_exit;
9075 
9076 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
9077 		struct ethtool_eee *eee = &bp->eee;
9078 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
9079 
9080 		bp->flags |= BNXT_FLAG_EEE_CAP;
9081 		eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9082 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
9083 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
9084 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
9085 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
9086 	}
9087 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EXTERNAL_LPBK_SUPPORTED) {
9088 		if (bp->test_info)
9089 			bp->test_info->flags |= BNXT_TEST_FL_EXT_LPBK;
9090 	}
9091 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_AUTONEG_LPBK_SUPPORTED) {
9092 		if (bp->test_info)
9093 			bp->test_info->flags |= BNXT_TEST_FL_AN_PHY_LPBK;
9094 	}
9095 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_SHARED_PHY_CFG_SUPPORTED) {
9096 		if (BNXT_PF(bp))
9097 			bp->fw_cap |= BNXT_FW_CAP_SHARED_PORT_CFG;
9098 	}
9099 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_CUMULATIVE_COUNTERS_ON_RESET)
9100 		bp->fw_cap |= BNXT_FW_CAP_PORT_STATS_NO_RESET;
9101 
9102 	if (bp->hwrm_spec_code >= 0x10a01) {
9103 		if (bnxt_phy_qcaps_no_speed(resp)) {
9104 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
9105 			netdev_warn(bp->dev, "Ethernet link disabled\n");
9106 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
9107 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
9108 			netdev_info(bp->dev, "Ethernet link enabled\n");
9109 			/* Phy re-enabled, reprobe the speeds */
9110 			link_info->support_auto_speeds = 0;
9111 			link_info->support_pam4_auto_speeds = 0;
9112 		}
9113 	}
9114 	if (resp->supported_speeds_auto_mode)
9115 		link_info->support_auto_speeds =
9116 			le16_to_cpu(resp->supported_speeds_auto_mode);
9117 	if (resp->supported_pam4_speeds_auto_mode)
9118 		link_info->support_pam4_auto_speeds =
9119 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
9120 
9121 	bp->port_count = resp->port_cnt;
9122 
9123 hwrm_phy_qcaps_exit:
9124 	mutex_unlock(&bp->hwrm_cmd_lock);
9125 	return rc;
9126 }
9127 
bnxt_support_dropped(u16 advertising,u16 supported)9128 static bool bnxt_support_dropped(u16 advertising, u16 supported)
9129 {
9130 	u16 diff = advertising ^ supported;
9131 
9132 	return ((supported | diff) != supported);
9133 }
9134 
bnxt_update_link(struct bnxt * bp,bool chng_link_state)9135 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
9136 {
9137 	int rc = 0;
9138 	struct bnxt_link_info *link_info = &bp->link_info;
9139 	struct hwrm_port_phy_qcfg_input req = {0};
9140 	struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
9141 	u8 link_up = link_info->link_up;
9142 	bool support_changed = false;
9143 
9144 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
9145 
9146 	mutex_lock(&bp->hwrm_cmd_lock);
9147 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9148 	if (rc) {
9149 		mutex_unlock(&bp->hwrm_cmd_lock);
9150 		return rc;
9151 	}
9152 
9153 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
9154 	link_info->phy_link_status = resp->link;
9155 	link_info->duplex = resp->duplex_cfg;
9156 	if (bp->hwrm_spec_code >= 0x10800)
9157 		link_info->duplex = resp->duplex_state;
9158 	link_info->pause = resp->pause;
9159 	link_info->auto_mode = resp->auto_mode;
9160 	link_info->auto_pause_setting = resp->auto_pause;
9161 	link_info->lp_pause = resp->link_partner_adv_pause;
9162 	link_info->force_pause_setting = resp->force_pause;
9163 	link_info->duplex_setting = resp->duplex_cfg;
9164 	if (link_info->phy_link_status == BNXT_LINK_LINK)
9165 		link_info->link_speed = le16_to_cpu(resp->link_speed);
9166 	else
9167 		link_info->link_speed = 0;
9168 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
9169 	link_info->force_pam4_link_speed =
9170 		le16_to_cpu(resp->force_pam4_link_speed);
9171 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
9172 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
9173 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
9174 	link_info->auto_pam4_link_speeds =
9175 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
9176 	link_info->lp_auto_link_speeds =
9177 		le16_to_cpu(resp->link_partner_adv_speeds);
9178 	link_info->lp_auto_pam4_link_speeds =
9179 		resp->link_partner_pam4_adv_speeds;
9180 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
9181 	link_info->phy_ver[0] = resp->phy_maj;
9182 	link_info->phy_ver[1] = resp->phy_min;
9183 	link_info->phy_ver[2] = resp->phy_bld;
9184 	link_info->media_type = resp->media_type;
9185 	link_info->phy_type = resp->phy_type;
9186 	link_info->transceiver = resp->xcvr_pkg_type;
9187 	link_info->phy_addr = resp->eee_config_phy_addr &
9188 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
9189 	link_info->module_status = resp->module_status;
9190 
9191 	if (bp->flags & BNXT_FLAG_EEE_CAP) {
9192 		struct ethtool_eee *eee = &bp->eee;
9193 		u16 fw_speeds;
9194 
9195 		eee->eee_active = 0;
9196 		if (resp->eee_config_phy_addr &
9197 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
9198 			eee->eee_active = 1;
9199 			fw_speeds = le16_to_cpu(
9200 				resp->link_partner_adv_eee_link_speed_mask);
9201 			eee->lp_advertised =
9202 				_bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9203 		}
9204 
9205 		/* Pull initial EEE config */
9206 		if (!chng_link_state) {
9207 			if (resp->eee_config_phy_addr &
9208 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
9209 				eee->eee_enabled = 1;
9210 
9211 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
9212 			eee->advertised =
9213 				_bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9214 
9215 			if (resp->eee_config_phy_addr &
9216 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
9217 				__le32 tmr;
9218 
9219 				eee->tx_lpi_enabled = 1;
9220 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
9221 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
9222 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
9223 			}
9224 		}
9225 	}
9226 
9227 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
9228 	if (bp->hwrm_spec_code >= 0x10504) {
9229 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
9230 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
9231 	}
9232 	/* TODO: need to add more logic to report VF link */
9233 	if (chng_link_state) {
9234 		if (link_info->phy_link_status == BNXT_LINK_LINK)
9235 			link_info->link_up = 1;
9236 		else
9237 			link_info->link_up = 0;
9238 		if (link_up != link_info->link_up)
9239 			bnxt_report_link(bp);
9240 	} else {
9241 		/* alwasy link down if not require to update link state */
9242 		link_info->link_up = 0;
9243 	}
9244 	mutex_unlock(&bp->hwrm_cmd_lock);
9245 
9246 	if (!BNXT_PHY_CFG_ABLE(bp))
9247 		return 0;
9248 
9249 	/* Check if any advertised speeds are no longer supported. The caller
9250 	 * holds the link_lock mutex, so we can modify link_info settings.
9251 	 */
9252 	if (bnxt_support_dropped(link_info->advertising,
9253 				 link_info->support_auto_speeds)) {
9254 		link_info->advertising = link_info->support_auto_speeds;
9255 		support_changed = true;
9256 	}
9257 	if (bnxt_support_dropped(link_info->advertising_pam4,
9258 				 link_info->support_pam4_auto_speeds)) {
9259 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
9260 		support_changed = true;
9261 	}
9262 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
9263 		bnxt_hwrm_set_link_setting(bp, true, false);
9264 	return 0;
9265 }
9266 
bnxt_get_port_module_status(struct bnxt * bp)9267 static void bnxt_get_port_module_status(struct bnxt *bp)
9268 {
9269 	struct bnxt_link_info *link_info = &bp->link_info;
9270 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
9271 	u8 module_status;
9272 
9273 	if (bnxt_update_link(bp, true))
9274 		return;
9275 
9276 	module_status = link_info->module_status;
9277 	switch (module_status) {
9278 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
9279 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
9280 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
9281 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
9282 			    bp->pf.port_id);
9283 		if (bp->hwrm_spec_code >= 0x10201) {
9284 			netdev_warn(bp->dev, "Module part number %s\n",
9285 				    resp->phy_vendor_partnumber);
9286 		}
9287 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
9288 			netdev_warn(bp->dev, "TX is disabled\n");
9289 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
9290 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
9291 	}
9292 }
9293 
9294 static void
bnxt_hwrm_set_pause_common(struct bnxt * bp,struct hwrm_port_phy_cfg_input * req)9295 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
9296 {
9297 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
9298 		if (bp->hwrm_spec_code >= 0x10201)
9299 			req->auto_pause =
9300 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
9301 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
9302 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
9303 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
9304 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
9305 		req->enables |=
9306 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
9307 	} else {
9308 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
9309 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
9310 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
9311 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
9312 		req->enables |=
9313 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
9314 		if (bp->hwrm_spec_code >= 0x10201) {
9315 			req->auto_pause = req->force_pause;
9316 			req->enables |= cpu_to_le32(
9317 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
9318 		}
9319 	}
9320 }
9321 
bnxt_hwrm_set_link_common(struct bnxt * bp,struct hwrm_port_phy_cfg_input * req)9322 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
9323 {
9324 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
9325 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
9326 		if (bp->link_info.advertising) {
9327 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
9328 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
9329 		}
9330 		if (bp->link_info.advertising_pam4) {
9331 			req->enables |=
9332 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
9333 			req->auto_link_pam4_speed_mask =
9334 				cpu_to_le16(bp->link_info.advertising_pam4);
9335 		}
9336 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
9337 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
9338 	} else {
9339 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
9340 		if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
9341 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
9342 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
9343 		} else {
9344 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
9345 		}
9346 	}
9347 
9348 	/* tell chimp that the setting takes effect immediately */
9349 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
9350 }
9351 
bnxt_hwrm_set_pause(struct bnxt * bp)9352 int bnxt_hwrm_set_pause(struct bnxt *bp)
9353 {
9354 	struct hwrm_port_phy_cfg_input req = {0};
9355 	int rc;
9356 
9357 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
9358 	bnxt_hwrm_set_pause_common(bp, &req);
9359 
9360 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
9361 	    bp->link_info.force_link_chng)
9362 		bnxt_hwrm_set_link_common(bp, &req);
9363 
9364 	mutex_lock(&bp->hwrm_cmd_lock);
9365 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9366 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
9367 		/* since changing of pause setting doesn't trigger any link
9368 		 * change event, the driver needs to update the current pause
9369 		 * result upon successfully return of the phy_cfg command
9370 		 */
9371 		bp->link_info.pause =
9372 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
9373 		bp->link_info.auto_pause_setting = 0;
9374 		if (!bp->link_info.force_link_chng)
9375 			bnxt_report_link(bp);
9376 	}
9377 	bp->link_info.force_link_chng = false;
9378 	mutex_unlock(&bp->hwrm_cmd_lock);
9379 	return rc;
9380 }
9381 
bnxt_hwrm_set_eee(struct bnxt * bp,struct hwrm_port_phy_cfg_input * req)9382 static void bnxt_hwrm_set_eee(struct bnxt *bp,
9383 			      struct hwrm_port_phy_cfg_input *req)
9384 {
9385 	struct ethtool_eee *eee = &bp->eee;
9386 
9387 	if (eee->eee_enabled) {
9388 		u16 eee_speeds;
9389 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
9390 
9391 		if (eee->tx_lpi_enabled)
9392 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
9393 		else
9394 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
9395 
9396 		req->flags |= cpu_to_le32(flags);
9397 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
9398 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
9399 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
9400 	} else {
9401 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
9402 	}
9403 }
9404 
bnxt_hwrm_set_link_setting(struct bnxt * bp,bool set_pause,bool set_eee)9405 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
9406 {
9407 	struct hwrm_port_phy_cfg_input req = {0};
9408 
9409 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
9410 	if (set_pause)
9411 		bnxt_hwrm_set_pause_common(bp, &req);
9412 
9413 	bnxt_hwrm_set_link_common(bp, &req);
9414 
9415 	if (set_eee)
9416 		bnxt_hwrm_set_eee(bp, &req);
9417 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9418 }
9419 
bnxt_hwrm_shutdown_link(struct bnxt * bp)9420 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
9421 {
9422 	struct hwrm_port_phy_cfg_input req = {0};
9423 
9424 	if (!BNXT_SINGLE_PF(bp))
9425 		return 0;
9426 
9427 	if (pci_num_vf(bp->pdev))
9428 		return 0;
9429 
9430 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
9431 	req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
9432 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9433 }
9434 
9435 static int bnxt_fw_init_one(struct bnxt *bp);
9436 
bnxt_hwrm_if_change(struct bnxt * bp,bool up)9437 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
9438 {
9439 	struct hwrm_func_drv_if_change_output *resp = bp->hwrm_cmd_resp_addr;
9440 	struct hwrm_func_drv_if_change_input req = {0};
9441 	bool fw_reset = !bp->irq_tbl;
9442 	bool resc_reinit = false;
9443 	u32 flags = 0;
9444 	int rc;
9445 
9446 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
9447 		return 0;
9448 
9449 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_IF_CHANGE, -1, -1);
9450 	if (up)
9451 		req.flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
9452 	mutex_lock(&bp->hwrm_cmd_lock);
9453 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9454 	if (!rc)
9455 		flags = le32_to_cpu(resp->flags);
9456 	mutex_unlock(&bp->hwrm_cmd_lock);
9457 	if (rc)
9458 		return rc;
9459 
9460 	if (!up)
9461 		return 0;
9462 
9463 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
9464 		resc_reinit = true;
9465 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE)
9466 		fw_reset = true;
9467 
9468 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
9469 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
9470 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
9471 		return -ENODEV;
9472 	}
9473 	if (resc_reinit || fw_reset) {
9474 		if (fw_reset) {
9475 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
9476 				bnxt_ulp_stop(bp);
9477 			bnxt_free_ctx_mem(bp);
9478 			kfree(bp->ctx);
9479 			bp->ctx = NULL;
9480 			bnxt_dcb_free(bp);
9481 			rc = bnxt_fw_init_one(bp);
9482 			if (rc) {
9483 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
9484 				return rc;
9485 			}
9486 			bnxt_clear_int_mode(bp);
9487 			rc = bnxt_init_int_mode(bp);
9488 			if (rc) {
9489 				netdev_err(bp->dev, "init int mode failed\n");
9490 				return rc;
9491 			}
9492 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
9493 		}
9494 		if (BNXT_NEW_RM(bp)) {
9495 			struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9496 
9497 			rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9498 			hw_resc->resv_cp_rings = 0;
9499 			hw_resc->resv_stat_ctxs = 0;
9500 			hw_resc->resv_irqs = 0;
9501 			hw_resc->resv_tx_rings = 0;
9502 			hw_resc->resv_rx_rings = 0;
9503 			hw_resc->resv_hw_ring_grps = 0;
9504 			hw_resc->resv_vnics = 0;
9505 			if (!fw_reset) {
9506 				bp->tx_nr_rings = 0;
9507 				bp->rx_nr_rings = 0;
9508 			}
9509 		}
9510 	}
9511 	return 0;
9512 }
9513 
bnxt_hwrm_port_led_qcaps(struct bnxt * bp)9514 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
9515 {
9516 	struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
9517 	struct hwrm_port_led_qcaps_input req = {0};
9518 	struct bnxt_pf_info *pf = &bp->pf;
9519 	int rc;
9520 
9521 	bp->num_leds = 0;
9522 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
9523 		return 0;
9524 
9525 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
9526 	req.port_id = cpu_to_le16(pf->port_id);
9527 	mutex_lock(&bp->hwrm_cmd_lock);
9528 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9529 	if (rc) {
9530 		mutex_unlock(&bp->hwrm_cmd_lock);
9531 		return rc;
9532 	}
9533 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
9534 		int i;
9535 
9536 		bp->num_leds = resp->num_leds;
9537 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
9538 						 bp->num_leds);
9539 		for (i = 0; i < bp->num_leds; i++) {
9540 			struct bnxt_led_info *led = &bp->leds[i];
9541 			__le16 caps = led->led_state_caps;
9542 
9543 			if (!led->led_group_id ||
9544 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
9545 				bp->num_leds = 0;
9546 				break;
9547 			}
9548 		}
9549 	}
9550 	mutex_unlock(&bp->hwrm_cmd_lock);
9551 	return 0;
9552 }
9553 
bnxt_hwrm_alloc_wol_fltr(struct bnxt * bp)9554 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
9555 {
9556 	struct hwrm_wol_filter_alloc_input req = {0};
9557 	struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
9558 	int rc;
9559 
9560 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
9561 	req.port_id = cpu_to_le16(bp->pf.port_id);
9562 	req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
9563 	req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
9564 	memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
9565 	mutex_lock(&bp->hwrm_cmd_lock);
9566 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9567 	if (!rc)
9568 		bp->wol_filter_id = resp->wol_filter_id;
9569 	mutex_unlock(&bp->hwrm_cmd_lock);
9570 	return rc;
9571 }
9572 
bnxt_hwrm_free_wol_fltr(struct bnxt * bp)9573 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
9574 {
9575 	struct hwrm_wol_filter_free_input req = {0};
9576 
9577 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
9578 	req.port_id = cpu_to_le16(bp->pf.port_id);
9579 	req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
9580 	req.wol_filter_id = bp->wol_filter_id;
9581 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9582 }
9583 
bnxt_hwrm_get_wol_fltrs(struct bnxt * bp,u16 handle)9584 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
9585 {
9586 	struct hwrm_wol_filter_qcfg_input req = {0};
9587 	struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
9588 	u16 next_handle = 0;
9589 	int rc;
9590 
9591 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
9592 	req.port_id = cpu_to_le16(bp->pf.port_id);
9593 	req.handle = cpu_to_le16(handle);
9594 	mutex_lock(&bp->hwrm_cmd_lock);
9595 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9596 	if (!rc) {
9597 		next_handle = le16_to_cpu(resp->next_handle);
9598 		if (next_handle != 0) {
9599 			if (resp->wol_type ==
9600 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
9601 				bp->wol = 1;
9602 				bp->wol_filter_id = resp->wol_filter_id;
9603 			}
9604 		}
9605 	}
9606 	mutex_unlock(&bp->hwrm_cmd_lock);
9607 	return next_handle;
9608 }
9609 
bnxt_get_wol_settings(struct bnxt * bp)9610 static void bnxt_get_wol_settings(struct bnxt *bp)
9611 {
9612 	u16 handle = 0;
9613 
9614 	bp->wol = 0;
9615 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
9616 		return;
9617 
9618 	do {
9619 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
9620 	} while (handle && handle != 0xffff);
9621 }
9622 
9623 #ifdef CONFIG_BNXT_HWMON
bnxt_show_temp(struct device * dev,struct device_attribute * devattr,char * buf)9624 static ssize_t bnxt_show_temp(struct device *dev,
9625 			      struct device_attribute *devattr, char *buf)
9626 {
9627 	struct hwrm_temp_monitor_query_input req = {0};
9628 	struct hwrm_temp_monitor_query_output *resp;
9629 	struct bnxt *bp = dev_get_drvdata(dev);
9630 	u32 len = 0;
9631 	int rc;
9632 
9633 	resp = bp->hwrm_cmd_resp_addr;
9634 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TEMP_MONITOR_QUERY, -1, -1);
9635 	mutex_lock(&bp->hwrm_cmd_lock);
9636 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9637 	if (!rc)
9638 		len = sprintf(buf, "%u\n", resp->temp * 1000); /* display millidegree */
9639 	mutex_unlock(&bp->hwrm_cmd_lock);
9640 	if (rc)
9641 		return rc;
9642 	return len;
9643 }
9644 static SENSOR_DEVICE_ATTR(temp1_input, 0444, bnxt_show_temp, NULL, 0);
9645 
9646 static struct attribute *bnxt_attrs[] = {
9647 	&sensor_dev_attr_temp1_input.dev_attr.attr,
9648 	NULL
9649 };
9650 ATTRIBUTE_GROUPS(bnxt);
9651 
bnxt_hwmon_close(struct bnxt * bp)9652 static void bnxt_hwmon_close(struct bnxt *bp)
9653 {
9654 	if (bp->hwmon_dev) {
9655 		hwmon_device_unregister(bp->hwmon_dev);
9656 		bp->hwmon_dev = NULL;
9657 	}
9658 }
9659 
bnxt_hwmon_open(struct bnxt * bp)9660 static void bnxt_hwmon_open(struct bnxt *bp)
9661 {
9662 	struct hwrm_temp_monitor_query_input req = {0};
9663 	struct pci_dev *pdev = bp->pdev;
9664 	int rc;
9665 
9666 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TEMP_MONITOR_QUERY, -1, -1);
9667 	rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9668 	if (rc == -EACCES || rc == -EOPNOTSUPP) {
9669 		bnxt_hwmon_close(bp);
9670 		return;
9671 	}
9672 
9673 	if (bp->hwmon_dev)
9674 		return;
9675 
9676 	bp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev,
9677 							  DRV_MODULE_NAME, bp,
9678 							  bnxt_groups);
9679 	if (IS_ERR(bp->hwmon_dev)) {
9680 		bp->hwmon_dev = NULL;
9681 		dev_warn(&pdev->dev, "Cannot register hwmon device\n");
9682 	}
9683 }
9684 #else
bnxt_hwmon_close(struct bnxt * bp)9685 static void bnxt_hwmon_close(struct bnxt *bp)
9686 {
9687 }
9688 
bnxt_hwmon_open(struct bnxt * bp)9689 static void bnxt_hwmon_open(struct bnxt *bp)
9690 {
9691 }
9692 #endif
9693 
bnxt_eee_config_ok(struct bnxt * bp)9694 static bool bnxt_eee_config_ok(struct bnxt *bp)
9695 {
9696 	struct ethtool_eee *eee = &bp->eee;
9697 	struct bnxt_link_info *link_info = &bp->link_info;
9698 
9699 	if (!(bp->flags & BNXT_FLAG_EEE_CAP))
9700 		return true;
9701 
9702 	if (eee->eee_enabled) {
9703 		u32 advertising =
9704 			_bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
9705 
9706 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
9707 			eee->eee_enabled = 0;
9708 			return false;
9709 		}
9710 		if (eee->advertised & ~advertising) {
9711 			eee->advertised = advertising & eee->supported;
9712 			return false;
9713 		}
9714 	}
9715 	return true;
9716 }
9717 
bnxt_update_phy_setting(struct bnxt * bp)9718 static int bnxt_update_phy_setting(struct bnxt *bp)
9719 {
9720 	int rc;
9721 	bool update_link = false;
9722 	bool update_pause = false;
9723 	bool update_eee = false;
9724 	struct bnxt_link_info *link_info = &bp->link_info;
9725 
9726 	rc = bnxt_update_link(bp, true);
9727 	if (rc) {
9728 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
9729 			   rc);
9730 		return rc;
9731 	}
9732 	if (!BNXT_SINGLE_PF(bp))
9733 		return 0;
9734 
9735 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
9736 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
9737 	    link_info->req_flow_ctrl)
9738 		update_pause = true;
9739 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
9740 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
9741 		update_pause = true;
9742 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
9743 		if (BNXT_AUTO_MODE(link_info->auto_mode))
9744 			update_link = true;
9745 		if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
9746 		    link_info->req_link_speed != link_info->force_link_speed)
9747 			update_link = true;
9748 		else if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
9749 			 link_info->req_link_speed != link_info->force_pam4_link_speed)
9750 			update_link = true;
9751 		if (link_info->req_duplex != link_info->duplex_setting)
9752 			update_link = true;
9753 	} else {
9754 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
9755 			update_link = true;
9756 		if (link_info->advertising != link_info->auto_link_speeds ||
9757 		    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
9758 			update_link = true;
9759 	}
9760 
9761 	/* The last close may have shutdown the link, so need to call
9762 	 * PHY_CFG to bring it back up.
9763 	 */
9764 	if (!bp->link_info.link_up)
9765 		update_link = true;
9766 
9767 	if (!bnxt_eee_config_ok(bp))
9768 		update_eee = true;
9769 
9770 	if (update_link)
9771 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
9772 	else if (update_pause)
9773 		rc = bnxt_hwrm_set_pause(bp);
9774 	if (rc) {
9775 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
9776 			   rc);
9777 		return rc;
9778 	}
9779 
9780 	return rc;
9781 }
9782 
9783 /* Common routine to pre-map certain register block to different GRC window.
9784  * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
9785  * in PF and 3 windows in VF that can be customized to map in different
9786  * register blocks.
9787  */
bnxt_preset_reg_win(struct bnxt * bp)9788 static void bnxt_preset_reg_win(struct bnxt *bp)
9789 {
9790 	if (BNXT_PF(bp)) {
9791 		/* CAG registers map to GRC window #4 */
9792 		writel(BNXT_CAG_REG_BASE,
9793 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
9794 	}
9795 }
9796 
9797 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
9798 
__bnxt_open_nic(struct bnxt * bp,bool irq_re_init,bool link_re_init)9799 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
9800 {
9801 	int rc = 0;
9802 
9803 	bnxt_preset_reg_win(bp);
9804 	netif_carrier_off(bp->dev);
9805 	if (irq_re_init) {
9806 		/* Reserve rings now if none were reserved at driver probe. */
9807 		rc = bnxt_init_dflt_ring_mode(bp);
9808 		if (rc) {
9809 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
9810 			return rc;
9811 		}
9812 	}
9813 	rc = bnxt_reserve_rings(bp, irq_re_init);
9814 	if (rc)
9815 		return rc;
9816 	if ((bp->flags & BNXT_FLAG_RFS) &&
9817 	    !(bp->flags & BNXT_FLAG_USING_MSIX)) {
9818 		/* disable RFS if falling back to INTA */
9819 		bp->dev->hw_features &= ~NETIF_F_NTUPLE;
9820 		bp->flags &= ~BNXT_FLAG_RFS;
9821 	}
9822 
9823 	rc = bnxt_alloc_mem(bp, irq_re_init);
9824 	if (rc) {
9825 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
9826 		goto open_err_free_mem;
9827 	}
9828 
9829 	if (irq_re_init) {
9830 		bnxt_init_napi(bp);
9831 		rc = bnxt_request_irq(bp);
9832 		if (rc) {
9833 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
9834 			goto open_err_irq;
9835 		}
9836 	}
9837 
9838 	rc = bnxt_init_nic(bp, irq_re_init);
9839 	if (rc) {
9840 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
9841 		goto open_err_irq;
9842 	}
9843 
9844 	bnxt_enable_napi(bp);
9845 	bnxt_debug_dev_init(bp);
9846 
9847 	if (link_re_init) {
9848 		mutex_lock(&bp->link_lock);
9849 		rc = bnxt_update_phy_setting(bp);
9850 		mutex_unlock(&bp->link_lock);
9851 		if (rc) {
9852 			netdev_warn(bp->dev, "failed to update phy settings\n");
9853 			if (BNXT_SINGLE_PF(bp)) {
9854 				bp->link_info.phy_retry = true;
9855 				bp->link_info.phy_retry_expires =
9856 					jiffies + 5 * HZ;
9857 			}
9858 		}
9859 	}
9860 
9861 	if (irq_re_init)
9862 		udp_tunnel_nic_reset_ntf(bp->dev);
9863 
9864 	set_bit(BNXT_STATE_OPEN, &bp->state);
9865 	bnxt_enable_int(bp);
9866 	/* Enable TX queues */
9867 	bnxt_tx_enable(bp);
9868 	mod_timer(&bp->timer, jiffies + bp->current_interval);
9869 	/* Poll link status and check for SFP+ module status */
9870 	bnxt_get_port_module_status(bp);
9871 
9872 	/* VF-reps may need to be re-opened after the PF is re-opened */
9873 	if (BNXT_PF(bp))
9874 		bnxt_vf_reps_open(bp);
9875 	return 0;
9876 
9877 open_err_irq:
9878 	bnxt_del_napi(bp);
9879 
9880 open_err_free_mem:
9881 	bnxt_free_skbs(bp);
9882 	bnxt_free_irq(bp);
9883 	bnxt_free_mem(bp, true);
9884 	return rc;
9885 }
9886 
9887 /* rtnl_lock held */
bnxt_open_nic(struct bnxt * bp,bool irq_re_init,bool link_re_init)9888 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
9889 {
9890 	int rc = 0;
9891 
9892 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
9893 		rc = -EIO;
9894 	if (!rc)
9895 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
9896 	if (rc) {
9897 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
9898 		dev_close(bp->dev);
9899 	}
9900 	return rc;
9901 }
9902 
9903 /* rtnl_lock held, open the NIC half way by allocating all resources, but
9904  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
9905  * self tests.
9906  */
bnxt_half_open_nic(struct bnxt * bp)9907 int bnxt_half_open_nic(struct bnxt *bp)
9908 {
9909 	int rc = 0;
9910 
9911 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
9912 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
9913 		rc = -ENODEV;
9914 		goto half_open_err;
9915 	}
9916 
9917 	rc = bnxt_alloc_mem(bp, false);
9918 	if (rc) {
9919 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
9920 		goto half_open_err;
9921 	}
9922 	rc = bnxt_init_nic(bp, false);
9923 	if (rc) {
9924 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
9925 		goto half_open_err;
9926 	}
9927 	return 0;
9928 
9929 half_open_err:
9930 	bnxt_free_skbs(bp);
9931 	bnxt_free_mem(bp, false);
9932 	dev_close(bp->dev);
9933 	return rc;
9934 }
9935 
9936 /* rtnl_lock held, this call can only be made after a previous successful
9937  * call to bnxt_half_open_nic().
9938  */
bnxt_half_close_nic(struct bnxt * bp)9939 void bnxt_half_close_nic(struct bnxt *bp)
9940 {
9941 	bnxt_hwrm_resource_free(bp, false, false);
9942 	bnxt_free_skbs(bp);
9943 	bnxt_free_mem(bp, false);
9944 }
9945 
bnxt_reenable_sriov(struct bnxt * bp)9946 static void bnxt_reenable_sriov(struct bnxt *bp)
9947 {
9948 	if (BNXT_PF(bp)) {
9949 		struct bnxt_pf_info *pf = &bp->pf;
9950 		int n = pf->active_vfs;
9951 
9952 		if (n)
9953 			bnxt_cfg_hw_sriov(bp, &n, true);
9954 	}
9955 }
9956 
bnxt_open(struct net_device * dev)9957 static int bnxt_open(struct net_device *dev)
9958 {
9959 	struct bnxt *bp = netdev_priv(dev);
9960 	int rc;
9961 
9962 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
9963 		netdev_err(bp->dev, "A previous firmware reset did not complete, aborting\n");
9964 		return -ENODEV;
9965 	}
9966 
9967 	rc = bnxt_hwrm_if_change(bp, true);
9968 	if (rc)
9969 		return rc;
9970 	rc = __bnxt_open_nic(bp, true, true);
9971 	if (rc) {
9972 		bnxt_hwrm_if_change(bp, false);
9973 	} else {
9974 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
9975 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
9976 				bnxt_ulp_start(bp, 0);
9977 				bnxt_reenable_sriov(bp);
9978 			}
9979 		}
9980 		bnxt_hwmon_open(bp);
9981 	}
9982 
9983 	return rc;
9984 }
9985 
bnxt_drv_busy(struct bnxt * bp)9986 static bool bnxt_drv_busy(struct bnxt *bp)
9987 {
9988 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
9989 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
9990 }
9991 
9992 static void bnxt_get_ring_stats(struct bnxt *bp,
9993 				struct rtnl_link_stats64 *stats);
9994 
__bnxt_close_nic(struct bnxt * bp,bool irq_re_init,bool link_re_init)9995 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
9996 			     bool link_re_init)
9997 {
9998 	/* Close the VF-reps before closing PF */
9999 	if (BNXT_PF(bp))
10000 		bnxt_vf_reps_close(bp);
10001 
10002 	/* Change device state to avoid TX queue wake up's */
10003 	bnxt_tx_disable(bp);
10004 
10005 	clear_bit(BNXT_STATE_OPEN, &bp->state);
10006 	smp_mb__after_atomic();
10007 	while (bnxt_drv_busy(bp))
10008 		msleep(20);
10009 
10010 	/* Flush rings and and disable interrupts */
10011 	bnxt_shutdown_nic(bp, irq_re_init);
10012 
10013 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
10014 
10015 	bnxt_debug_dev_exit(bp);
10016 	bnxt_disable_napi(bp);
10017 	del_timer_sync(&bp->timer);
10018 	bnxt_free_skbs(bp);
10019 
10020 	/* Save ring stats before shutdown */
10021 	if (bp->bnapi && irq_re_init)
10022 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
10023 	if (irq_re_init) {
10024 		bnxt_free_irq(bp);
10025 		bnxt_del_napi(bp);
10026 	}
10027 	bnxt_free_mem(bp, irq_re_init);
10028 }
10029 
bnxt_close_nic(struct bnxt * bp,bool irq_re_init,bool link_re_init)10030 int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10031 {
10032 	int rc = 0;
10033 
10034 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
10035 		/* If we get here, it means firmware reset is in progress
10036 		 * while we are trying to close.  We can safely proceed with
10037 		 * the close because we are holding rtnl_lock().  Some firmware
10038 		 * messages may fail as we proceed to close.  We set the
10039 		 * ABORT_ERR flag here so that the FW reset thread will later
10040 		 * abort when it gets the rtnl_lock() and sees the flag.
10041 		 */
10042 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
10043 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10044 	}
10045 
10046 #ifdef CONFIG_BNXT_SRIOV
10047 	if (bp->sriov_cfg) {
10048 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
10049 						      !bp->sriov_cfg,
10050 						      BNXT_SRIOV_CFG_WAIT_TMO);
10051 		if (rc)
10052 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
10053 	}
10054 #endif
10055 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
10056 	return rc;
10057 }
10058 
bnxt_close(struct net_device * dev)10059 static int bnxt_close(struct net_device *dev)
10060 {
10061 	struct bnxt *bp = netdev_priv(dev);
10062 
10063 	bnxt_hwmon_close(bp);
10064 	bnxt_close_nic(bp, true, true);
10065 	bnxt_hwrm_shutdown_link(bp);
10066 	bnxt_hwrm_if_change(bp, false);
10067 	return 0;
10068 }
10069 
bnxt_hwrm_port_phy_read(struct bnxt * bp,u16 phy_addr,u16 reg,u16 * val)10070 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
10071 				   u16 *val)
10072 {
10073 	struct hwrm_port_phy_mdio_read_output *resp = bp->hwrm_cmd_resp_addr;
10074 	struct hwrm_port_phy_mdio_read_input req = {0};
10075 	int rc;
10076 
10077 	if (bp->hwrm_spec_code < 0x10a00)
10078 		return -EOPNOTSUPP;
10079 
10080 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_MDIO_READ, -1, -1);
10081 	req.port_id = cpu_to_le16(bp->pf.port_id);
10082 	req.phy_addr = phy_addr;
10083 	req.reg_addr = cpu_to_le16(reg & 0x1f);
10084 	if (mdio_phy_id_is_c45(phy_addr)) {
10085 		req.cl45_mdio = 1;
10086 		req.phy_addr = mdio_phy_id_prtad(phy_addr);
10087 		req.dev_addr = mdio_phy_id_devad(phy_addr);
10088 		req.reg_addr = cpu_to_le16(reg);
10089 	}
10090 
10091 	mutex_lock(&bp->hwrm_cmd_lock);
10092 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10093 	if (!rc)
10094 		*val = le16_to_cpu(resp->reg_data);
10095 	mutex_unlock(&bp->hwrm_cmd_lock);
10096 	return rc;
10097 }
10098 
bnxt_hwrm_port_phy_write(struct bnxt * bp,u16 phy_addr,u16 reg,u16 val)10099 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
10100 				    u16 val)
10101 {
10102 	struct hwrm_port_phy_mdio_write_input req = {0};
10103 
10104 	if (bp->hwrm_spec_code < 0x10a00)
10105 		return -EOPNOTSUPP;
10106 
10107 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_MDIO_WRITE, -1, -1);
10108 	req.port_id = cpu_to_le16(bp->pf.port_id);
10109 	req.phy_addr = phy_addr;
10110 	req.reg_addr = cpu_to_le16(reg & 0x1f);
10111 	if (mdio_phy_id_is_c45(phy_addr)) {
10112 		req.cl45_mdio = 1;
10113 		req.phy_addr = mdio_phy_id_prtad(phy_addr);
10114 		req.dev_addr = mdio_phy_id_devad(phy_addr);
10115 		req.reg_addr = cpu_to_le16(reg);
10116 	}
10117 	req.reg_data = cpu_to_le16(val);
10118 
10119 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10120 }
10121 
10122 /* rtnl_lock held */
bnxt_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)10123 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10124 {
10125 	struct mii_ioctl_data *mdio = if_mii(ifr);
10126 	struct bnxt *bp = netdev_priv(dev);
10127 	int rc;
10128 
10129 	switch (cmd) {
10130 	case SIOCGMIIPHY:
10131 		mdio->phy_id = bp->link_info.phy_addr;
10132 
10133 		fallthrough;
10134 	case SIOCGMIIREG: {
10135 		u16 mii_regval = 0;
10136 
10137 		if (!netif_running(dev))
10138 			return -EAGAIN;
10139 
10140 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
10141 					     &mii_regval);
10142 		mdio->val_out = mii_regval;
10143 		return rc;
10144 	}
10145 
10146 	case SIOCSMIIREG:
10147 		if (!netif_running(dev))
10148 			return -EAGAIN;
10149 
10150 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
10151 						mdio->val_in);
10152 
10153 	default:
10154 		/* do nothing */
10155 		break;
10156 	}
10157 	return -EOPNOTSUPP;
10158 }
10159 
bnxt_get_ring_stats(struct bnxt * bp,struct rtnl_link_stats64 * stats)10160 static void bnxt_get_ring_stats(struct bnxt *bp,
10161 				struct rtnl_link_stats64 *stats)
10162 {
10163 	int i;
10164 
10165 	for (i = 0; i < bp->cp_nr_rings; i++) {
10166 		struct bnxt_napi *bnapi = bp->bnapi[i];
10167 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
10168 		u64 *sw = cpr->stats.sw_stats;
10169 
10170 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
10171 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
10172 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
10173 
10174 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
10175 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
10176 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
10177 
10178 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
10179 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
10180 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
10181 
10182 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
10183 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
10184 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
10185 
10186 		stats->rx_missed_errors +=
10187 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
10188 
10189 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
10190 
10191 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
10192 	}
10193 }
10194 
bnxt_add_prev_stats(struct bnxt * bp,struct rtnl_link_stats64 * stats)10195 static void bnxt_add_prev_stats(struct bnxt *bp,
10196 				struct rtnl_link_stats64 *stats)
10197 {
10198 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
10199 
10200 	stats->rx_packets += prev_stats->rx_packets;
10201 	stats->tx_packets += prev_stats->tx_packets;
10202 	stats->rx_bytes += prev_stats->rx_bytes;
10203 	stats->tx_bytes += prev_stats->tx_bytes;
10204 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
10205 	stats->multicast += prev_stats->multicast;
10206 	stats->tx_dropped += prev_stats->tx_dropped;
10207 }
10208 
10209 static void
bnxt_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)10210 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
10211 {
10212 	struct bnxt *bp = netdev_priv(dev);
10213 
10214 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
10215 	/* Make sure bnxt_close_nic() sees that we are reading stats before
10216 	 * we check the BNXT_STATE_OPEN flag.
10217 	 */
10218 	smp_mb__after_atomic();
10219 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10220 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
10221 		*stats = bp->net_stats_prev;
10222 		return;
10223 	}
10224 
10225 	bnxt_get_ring_stats(bp, stats);
10226 	bnxt_add_prev_stats(bp, stats);
10227 
10228 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10229 		u64 *rx = bp->port_stats.sw_stats;
10230 		u64 *tx = bp->port_stats.sw_stats +
10231 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10232 
10233 		stats->rx_crc_errors =
10234 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
10235 		stats->rx_frame_errors =
10236 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
10237 		stats->rx_length_errors =
10238 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
10239 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
10240 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
10241 		stats->rx_errors =
10242 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
10243 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
10244 		stats->collisions =
10245 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
10246 		stats->tx_fifo_errors =
10247 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
10248 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
10249 	}
10250 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
10251 }
10252 
bnxt_mc_list_updated(struct bnxt * bp,u32 * rx_mask)10253 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
10254 {
10255 	struct net_device *dev = bp->dev;
10256 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10257 	struct netdev_hw_addr *ha;
10258 	u8 *haddr;
10259 	int mc_count = 0;
10260 	bool update = false;
10261 	int off = 0;
10262 
10263 	netdev_for_each_mc_addr(ha, dev) {
10264 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
10265 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10266 			vnic->mc_list_count = 0;
10267 			return false;
10268 		}
10269 		haddr = ha->addr;
10270 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
10271 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
10272 			update = true;
10273 		}
10274 		off += ETH_ALEN;
10275 		mc_count++;
10276 	}
10277 	if (mc_count)
10278 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
10279 
10280 	if (mc_count != vnic->mc_list_count) {
10281 		vnic->mc_list_count = mc_count;
10282 		update = true;
10283 	}
10284 	return update;
10285 }
10286 
bnxt_uc_list_updated(struct bnxt * bp)10287 static bool bnxt_uc_list_updated(struct bnxt *bp)
10288 {
10289 	struct net_device *dev = bp->dev;
10290 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10291 	struct netdev_hw_addr *ha;
10292 	int off = 0;
10293 
10294 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
10295 		return true;
10296 
10297 	netdev_for_each_uc_addr(ha, dev) {
10298 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
10299 			return true;
10300 
10301 		off += ETH_ALEN;
10302 	}
10303 	return false;
10304 }
10305 
bnxt_set_rx_mode(struct net_device * dev)10306 static void bnxt_set_rx_mode(struct net_device *dev)
10307 {
10308 	struct bnxt *bp = netdev_priv(dev);
10309 	struct bnxt_vnic_info *vnic;
10310 	bool mc_update = false;
10311 	bool uc_update;
10312 	u32 mask;
10313 
10314 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
10315 		return;
10316 
10317 	vnic = &bp->vnic_info[0];
10318 	mask = vnic->rx_mask;
10319 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
10320 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
10321 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
10322 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
10323 
10324 	if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
10325 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10326 
10327 	uc_update = bnxt_uc_list_updated(bp);
10328 
10329 	if (dev->flags & IFF_BROADCAST)
10330 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10331 	if (dev->flags & IFF_ALLMULTI) {
10332 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10333 		vnic->mc_list_count = 0;
10334 	} else {
10335 		mc_update = bnxt_mc_list_updated(bp, &mask);
10336 	}
10337 
10338 	if (mask != vnic->rx_mask || uc_update || mc_update) {
10339 		vnic->rx_mask = mask;
10340 
10341 		set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
10342 		bnxt_queue_sp_work(bp);
10343 	}
10344 }
10345 
bnxt_cfg_rx_mode(struct bnxt * bp)10346 static int bnxt_cfg_rx_mode(struct bnxt *bp)
10347 {
10348 	struct net_device *dev = bp->dev;
10349 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10350 	struct netdev_hw_addr *ha;
10351 	int i, off = 0, rc;
10352 	bool uc_update;
10353 
10354 	netif_addr_lock_bh(dev);
10355 	uc_update = bnxt_uc_list_updated(bp);
10356 	netif_addr_unlock_bh(dev);
10357 
10358 	if (!uc_update)
10359 		goto skip_uc;
10360 
10361 	mutex_lock(&bp->hwrm_cmd_lock);
10362 	for (i = 1; i < vnic->uc_filter_count; i++) {
10363 		struct hwrm_cfa_l2_filter_free_input req = {0};
10364 
10365 		bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
10366 				       -1);
10367 
10368 		req.l2_filter_id = vnic->fw_l2_filter_id[i];
10369 
10370 		rc = _hwrm_send_message(bp, &req, sizeof(req),
10371 					HWRM_CMD_TIMEOUT);
10372 	}
10373 	mutex_unlock(&bp->hwrm_cmd_lock);
10374 
10375 	vnic->uc_filter_count = 1;
10376 
10377 	netif_addr_lock_bh(dev);
10378 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
10379 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10380 	} else {
10381 		netdev_for_each_uc_addr(ha, dev) {
10382 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
10383 			off += ETH_ALEN;
10384 			vnic->uc_filter_count++;
10385 		}
10386 	}
10387 	netif_addr_unlock_bh(dev);
10388 
10389 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
10390 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
10391 		if (rc) {
10392 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
10393 				   rc);
10394 			vnic->uc_filter_count = i;
10395 			return rc;
10396 		}
10397 	}
10398 
10399 skip_uc:
10400 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
10401 	if (rc && vnic->mc_list_count) {
10402 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
10403 			    rc);
10404 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10405 		vnic->mc_list_count = 0;
10406 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
10407 	}
10408 	if (rc)
10409 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
10410 			   rc);
10411 
10412 	return rc;
10413 }
10414 
bnxt_can_reserve_rings(struct bnxt * bp)10415 static bool bnxt_can_reserve_rings(struct bnxt *bp)
10416 {
10417 #ifdef CONFIG_BNXT_SRIOV
10418 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
10419 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10420 
10421 		/* No minimum rings were provisioned by the PF.  Don't
10422 		 * reserve rings by default when device is down.
10423 		 */
10424 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
10425 			return true;
10426 
10427 		if (!netif_running(bp->dev))
10428 			return false;
10429 	}
10430 #endif
10431 	return true;
10432 }
10433 
10434 /* If the chip and firmware supports RFS */
bnxt_rfs_supported(struct bnxt * bp)10435 static bool bnxt_rfs_supported(struct bnxt *bp)
10436 {
10437 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
10438 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
10439 			return true;
10440 		return false;
10441 	}
10442 	/* 212 firmware is broken for aRFS */
10443 	if (BNXT_FW_MAJ(bp) == 212)
10444 		return false;
10445 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
10446 		return true;
10447 	if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
10448 		return true;
10449 	return false;
10450 }
10451 
10452 /* If runtime conditions support RFS */
bnxt_rfs_capable(struct bnxt * bp)10453 static bool bnxt_rfs_capable(struct bnxt *bp)
10454 {
10455 #ifdef CONFIG_RFS_ACCEL
10456 	int vnics, max_vnics, max_rss_ctxs;
10457 
10458 	if (bp->flags & BNXT_FLAG_CHIP_P5)
10459 		return bnxt_rfs_supported(bp);
10460 	if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
10461 		return false;
10462 
10463 	vnics = 1 + bp->rx_nr_rings;
10464 	max_vnics = bnxt_get_max_func_vnics(bp);
10465 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
10466 
10467 	/* RSS contexts not a limiting factor */
10468 	if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
10469 		max_rss_ctxs = max_vnics;
10470 	if (vnics > max_vnics || vnics > max_rss_ctxs) {
10471 		if (bp->rx_nr_rings > 1)
10472 			netdev_warn(bp->dev,
10473 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
10474 				    min(max_rss_ctxs - 1, max_vnics - 1));
10475 		return false;
10476 	}
10477 
10478 	if (!BNXT_NEW_RM(bp))
10479 		return true;
10480 
10481 	if (vnics == bp->hw_resc.resv_vnics)
10482 		return true;
10483 
10484 	bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, vnics);
10485 	if (vnics <= bp->hw_resc.resv_vnics)
10486 		return true;
10487 
10488 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
10489 	bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, 1);
10490 	return false;
10491 #else
10492 	return false;
10493 #endif
10494 }
10495 
bnxt_fix_features(struct net_device * dev,netdev_features_t features)10496 static netdev_features_t bnxt_fix_features(struct net_device *dev,
10497 					   netdev_features_t features)
10498 {
10499 	struct bnxt *bp = netdev_priv(dev);
10500 	netdev_features_t vlan_features;
10501 
10502 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
10503 		features &= ~NETIF_F_NTUPLE;
10504 
10505 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
10506 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
10507 
10508 	if (!(features & NETIF_F_GRO))
10509 		features &= ~NETIF_F_GRO_HW;
10510 
10511 	if (features & NETIF_F_GRO_HW)
10512 		features &= ~NETIF_F_LRO;
10513 
10514 	/* Both CTAG and STAG VLAN accelaration on the RX side have to be
10515 	 * turned on or off together.
10516 	 */
10517 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
10518 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
10519 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
10520 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
10521 		else if (vlan_features)
10522 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
10523 	}
10524 #ifdef CONFIG_BNXT_SRIOV
10525 	if (BNXT_VF(bp) && bp->vf.vlan)
10526 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
10527 #endif
10528 	return features;
10529 }
10530 
bnxt_set_features(struct net_device * dev,netdev_features_t features)10531 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
10532 {
10533 	struct bnxt *bp = netdev_priv(dev);
10534 	u32 flags = bp->flags;
10535 	u32 changes;
10536 	int rc = 0;
10537 	bool re_init = false;
10538 	bool update_tpa = false;
10539 
10540 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
10541 	if (features & NETIF_F_GRO_HW)
10542 		flags |= BNXT_FLAG_GRO;
10543 	else if (features & NETIF_F_LRO)
10544 		flags |= BNXT_FLAG_LRO;
10545 
10546 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
10547 		flags &= ~BNXT_FLAG_TPA;
10548 
10549 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
10550 		flags |= BNXT_FLAG_STRIP_VLAN;
10551 
10552 	if (features & NETIF_F_NTUPLE)
10553 		flags |= BNXT_FLAG_RFS;
10554 
10555 	changes = flags ^ bp->flags;
10556 	if (changes & BNXT_FLAG_TPA) {
10557 		update_tpa = true;
10558 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
10559 		    (flags & BNXT_FLAG_TPA) == 0 ||
10560 		    (bp->flags & BNXT_FLAG_CHIP_P5))
10561 			re_init = true;
10562 	}
10563 
10564 	if (changes & ~BNXT_FLAG_TPA)
10565 		re_init = true;
10566 
10567 	if (flags != bp->flags) {
10568 		u32 old_flags = bp->flags;
10569 
10570 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10571 			bp->flags = flags;
10572 			if (update_tpa)
10573 				bnxt_set_ring_params(bp);
10574 			return rc;
10575 		}
10576 
10577 		if (re_init) {
10578 			bnxt_close_nic(bp, false, false);
10579 			bp->flags = flags;
10580 			if (update_tpa)
10581 				bnxt_set_ring_params(bp);
10582 
10583 			return bnxt_open_nic(bp, false, false);
10584 		}
10585 		if (update_tpa) {
10586 			bp->flags = flags;
10587 			rc = bnxt_set_tpa(bp,
10588 					  (flags & BNXT_FLAG_TPA) ?
10589 					  true : false);
10590 			if (rc)
10591 				bp->flags = old_flags;
10592 		}
10593 	}
10594 	return rc;
10595 }
10596 
bnxt_dbg_hwrm_rd_reg(struct bnxt * bp,u32 reg_off,u16 num_words,u32 * reg_buf)10597 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
10598 			 u32 *reg_buf)
10599 {
10600 	struct hwrm_dbg_read_direct_output *resp = bp->hwrm_cmd_resp_addr;
10601 	struct hwrm_dbg_read_direct_input req = {0};
10602 	__le32 *dbg_reg_buf;
10603 	dma_addr_t mapping;
10604 	int rc, i;
10605 
10606 	dbg_reg_buf = dma_alloc_coherent(&bp->pdev->dev, num_words * 4,
10607 					 &mapping, GFP_KERNEL);
10608 	if (!dbg_reg_buf)
10609 		return -ENOMEM;
10610 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_READ_DIRECT, -1, -1);
10611 	req.host_dest_addr = cpu_to_le64(mapping);
10612 	req.read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
10613 	req.read_len32 = cpu_to_le32(num_words);
10614 	mutex_lock(&bp->hwrm_cmd_lock);
10615 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10616 	if (rc || resp->error_code) {
10617 		rc = -EIO;
10618 		goto dbg_rd_reg_exit;
10619 	}
10620 	for (i = 0; i < num_words; i++)
10621 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
10622 
10623 dbg_rd_reg_exit:
10624 	mutex_unlock(&bp->hwrm_cmd_lock);
10625 	dma_free_coherent(&bp->pdev->dev, num_words * 4, dbg_reg_buf, mapping);
10626 	return rc;
10627 }
10628 
bnxt_dbg_hwrm_ring_info_get(struct bnxt * bp,u8 ring_type,u32 ring_id,u32 * prod,u32 * cons)10629 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
10630 				       u32 ring_id, u32 *prod, u32 *cons)
10631 {
10632 	struct hwrm_dbg_ring_info_get_output *resp = bp->hwrm_cmd_resp_addr;
10633 	struct hwrm_dbg_ring_info_get_input req = {0};
10634 	int rc;
10635 
10636 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_RING_INFO_GET, -1, -1);
10637 	req.ring_type = ring_type;
10638 	req.fw_ring_id = cpu_to_le32(ring_id);
10639 	mutex_lock(&bp->hwrm_cmd_lock);
10640 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10641 	if (!rc) {
10642 		*prod = le32_to_cpu(resp->producer_index);
10643 		*cons = le32_to_cpu(resp->consumer_index);
10644 	}
10645 	mutex_unlock(&bp->hwrm_cmd_lock);
10646 	return rc;
10647 }
10648 
bnxt_dump_tx_sw_state(struct bnxt_napi * bnapi)10649 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
10650 {
10651 	struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
10652 	int i = bnapi->index;
10653 
10654 	if (!txr)
10655 		return;
10656 
10657 	netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
10658 		    i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
10659 		    txr->tx_cons);
10660 }
10661 
bnxt_dump_rx_sw_state(struct bnxt_napi * bnapi)10662 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
10663 {
10664 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
10665 	int i = bnapi->index;
10666 
10667 	if (!rxr)
10668 		return;
10669 
10670 	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",
10671 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
10672 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
10673 		    rxr->rx_sw_agg_prod);
10674 }
10675 
bnxt_dump_cp_sw_state(struct bnxt_napi * bnapi)10676 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
10677 {
10678 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
10679 	int i = bnapi->index;
10680 
10681 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
10682 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
10683 }
10684 
bnxt_dbg_dump_states(struct bnxt * bp)10685 static void bnxt_dbg_dump_states(struct bnxt *bp)
10686 {
10687 	int i;
10688 	struct bnxt_napi *bnapi;
10689 
10690 	for (i = 0; i < bp->cp_nr_rings; i++) {
10691 		bnapi = bp->bnapi[i];
10692 		if (netif_msg_drv(bp)) {
10693 			bnxt_dump_tx_sw_state(bnapi);
10694 			bnxt_dump_rx_sw_state(bnapi);
10695 			bnxt_dump_cp_sw_state(bnapi);
10696 		}
10697 	}
10698 }
10699 
bnxt_hwrm_rx_ring_reset(struct bnxt * bp,int ring_nr)10700 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
10701 {
10702 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
10703 	struct hwrm_ring_reset_input req = {0};
10704 	struct bnxt_napi *bnapi = rxr->bnapi;
10705 	struct bnxt_cp_ring_info *cpr;
10706 	u16 cp_ring_id;
10707 
10708 	cpr = &bnapi->cp_ring;
10709 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
10710 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_RESET, cp_ring_id, -1);
10711 	req.ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
10712 	req.ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
10713 	return hwrm_send_message_silent(bp, &req, sizeof(req),
10714 					HWRM_CMD_TIMEOUT);
10715 }
10716 
bnxt_reset_task(struct bnxt * bp,bool silent)10717 static void bnxt_reset_task(struct bnxt *bp, bool silent)
10718 {
10719 	if (!silent)
10720 		bnxt_dbg_dump_states(bp);
10721 	if (netif_running(bp->dev)) {
10722 		int rc;
10723 
10724 		if (silent) {
10725 			bnxt_close_nic(bp, false, false);
10726 			bnxt_open_nic(bp, false, false);
10727 		} else {
10728 			bnxt_ulp_stop(bp);
10729 			bnxt_close_nic(bp, true, false);
10730 			rc = bnxt_open_nic(bp, true, false);
10731 			bnxt_ulp_start(bp, rc);
10732 		}
10733 	}
10734 }
10735 
bnxt_tx_timeout(struct net_device * dev,unsigned int txqueue)10736 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
10737 {
10738 	struct bnxt *bp = netdev_priv(dev);
10739 
10740 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
10741 	set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
10742 	bnxt_queue_sp_work(bp);
10743 }
10744 
bnxt_fw_health_check(struct bnxt * bp)10745 static void bnxt_fw_health_check(struct bnxt *bp)
10746 {
10747 	struct bnxt_fw_health *fw_health = bp->fw_health;
10748 	u32 val;
10749 
10750 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
10751 		return;
10752 
10753 	/* Make sure it is enabled before checking the tmr_counter. */
10754 	smp_rmb();
10755 	if (fw_health->tmr_counter) {
10756 		fw_health->tmr_counter--;
10757 		return;
10758 	}
10759 
10760 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
10761 	if (val == fw_health->last_fw_heartbeat)
10762 		goto fw_reset;
10763 
10764 	fw_health->last_fw_heartbeat = val;
10765 
10766 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
10767 	if (val != fw_health->last_fw_reset_cnt)
10768 		goto fw_reset;
10769 
10770 	fw_health->tmr_counter = fw_health->tmr_multiplier;
10771 	return;
10772 
10773 fw_reset:
10774 	set_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event);
10775 	bnxt_queue_sp_work(bp);
10776 }
10777 
bnxt_timer(struct timer_list * t)10778 static void bnxt_timer(struct timer_list *t)
10779 {
10780 	struct bnxt *bp = from_timer(bp, t, timer);
10781 	struct net_device *dev = bp->dev;
10782 
10783 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
10784 		return;
10785 
10786 	if (atomic_read(&bp->intr_sem) != 0)
10787 		goto bnxt_restart_timer;
10788 
10789 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
10790 		bnxt_fw_health_check(bp);
10791 
10792 	if (bp->link_info.link_up && bp->stats_coal_ticks) {
10793 		set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
10794 		bnxt_queue_sp_work(bp);
10795 	}
10796 
10797 	if (bnxt_tc_flower_enabled(bp)) {
10798 		set_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event);
10799 		bnxt_queue_sp_work(bp);
10800 	}
10801 
10802 #ifdef CONFIG_RFS_ACCEL
10803 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count) {
10804 		set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
10805 		bnxt_queue_sp_work(bp);
10806 	}
10807 #endif /*CONFIG_RFS_ACCEL*/
10808 
10809 	if (bp->link_info.phy_retry) {
10810 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
10811 			bp->link_info.phy_retry = false;
10812 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
10813 		} else {
10814 			set_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event);
10815 			bnxt_queue_sp_work(bp);
10816 		}
10817 	}
10818 
10819 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && !bp->chip_rev &&
10820 	    netif_carrier_ok(dev)) {
10821 		set_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event);
10822 		bnxt_queue_sp_work(bp);
10823 	}
10824 bnxt_restart_timer:
10825 	mod_timer(&bp->timer, jiffies + bp->current_interval);
10826 }
10827 
bnxt_rtnl_lock_sp(struct bnxt * bp)10828 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
10829 {
10830 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
10831 	 * set.  If the device is being closed, bnxt_close() may be holding
10832 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
10833 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
10834 	 */
10835 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
10836 	rtnl_lock();
10837 }
10838 
bnxt_rtnl_unlock_sp(struct bnxt * bp)10839 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
10840 {
10841 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
10842 	rtnl_unlock();
10843 }
10844 
10845 /* Only called from bnxt_sp_task() */
bnxt_reset(struct bnxt * bp,bool silent)10846 static void bnxt_reset(struct bnxt *bp, bool silent)
10847 {
10848 	bnxt_rtnl_lock_sp(bp);
10849 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
10850 		bnxt_reset_task(bp, silent);
10851 	bnxt_rtnl_unlock_sp(bp);
10852 }
10853 
10854 /* Only called from bnxt_sp_task() */
bnxt_rx_ring_reset(struct bnxt * bp)10855 static void bnxt_rx_ring_reset(struct bnxt *bp)
10856 {
10857 	int i;
10858 
10859 	bnxt_rtnl_lock_sp(bp);
10860 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10861 		bnxt_rtnl_unlock_sp(bp);
10862 		return;
10863 	}
10864 	/* Disable and flush TPA before resetting the RX ring */
10865 	if (bp->flags & BNXT_FLAG_TPA)
10866 		bnxt_set_tpa(bp, false);
10867 	for (i = 0; i < bp->rx_nr_rings; i++) {
10868 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
10869 		struct bnxt_cp_ring_info *cpr;
10870 		int rc;
10871 
10872 		if (!rxr->bnapi->in_reset)
10873 			continue;
10874 
10875 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
10876 		if (rc) {
10877 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
10878 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
10879 			else
10880 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
10881 					    rc);
10882 			bnxt_reset_task(bp, true);
10883 			break;
10884 		}
10885 		bnxt_free_one_rx_ring_skbs(bp, i);
10886 		rxr->rx_prod = 0;
10887 		rxr->rx_agg_prod = 0;
10888 		rxr->rx_sw_agg_prod = 0;
10889 		rxr->rx_next_cons = 0;
10890 		rxr->bnapi->in_reset = false;
10891 		bnxt_alloc_one_rx_ring(bp, i);
10892 		cpr = &rxr->bnapi->cp_ring;
10893 		cpr->sw_stats.rx.rx_resets++;
10894 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
10895 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
10896 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
10897 	}
10898 	if (bp->flags & BNXT_FLAG_TPA)
10899 		bnxt_set_tpa(bp, true);
10900 	bnxt_rtnl_unlock_sp(bp);
10901 }
10902 
bnxt_fw_reset_close(struct bnxt * bp)10903 static void bnxt_fw_reset_close(struct bnxt *bp)
10904 {
10905 	bnxt_ulp_stop(bp);
10906 	/* When firmware is fatal state, disable PCI device to prevent
10907 	 * any potential bad DMAs before freeing kernel memory.
10908 	 */
10909 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
10910 		pci_disable_device(bp->pdev);
10911 	__bnxt_close_nic(bp, true, false);
10912 	bnxt_clear_int_mode(bp);
10913 	bnxt_hwrm_func_drv_unrgtr(bp);
10914 	if (pci_is_enabled(bp->pdev))
10915 		pci_disable_device(bp->pdev);
10916 	bnxt_free_ctx_mem(bp);
10917 	kfree(bp->ctx);
10918 	bp->ctx = NULL;
10919 }
10920 
is_bnxt_fw_ok(struct bnxt * bp)10921 static bool is_bnxt_fw_ok(struct bnxt *bp)
10922 {
10923 	struct bnxt_fw_health *fw_health = bp->fw_health;
10924 	bool no_heartbeat = false, has_reset = false;
10925 	u32 val;
10926 
10927 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
10928 	if (val == fw_health->last_fw_heartbeat)
10929 		no_heartbeat = true;
10930 
10931 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
10932 	if (val != fw_health->last_fw_reset_cnt)
10933 		has_reset = true;
10934 
10935 	if (!no_heartbeat && has_reset)
10936 		return true;
10937 
10938 	return false;
10939 }
10940 
10941 /* rtnl_lock is acquired before calling this function */
bnxt_force_fw_reset(struct bnxt * bp)10942 static void bnxt_force_fw_reset(struct bnxt *bp)
10943 {
10944 	struct bnxt_fw_health *fw_health = bp->fw_health;
10945 	u32 wait_dsecs;
10946 
10947 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
10948 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
10949 		return;
10950 
10951 	set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
10952 	bnxt_fw_reset_close(bp);
10953 	wait_dsecs = fw_health->master_func_wait_dsecs;
10954 	if (fw_health->master) {
10955 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
10956 			wait_dsecs = 0;
10957 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
10958 	} else {
10959 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
10960 		wait_dsecs = fw_health->normal_func_wait_dsecs;
10961 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
10962 	}
10963 
10964 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
10965 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
10966 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
10967 }
10968 
bnxt_fw_exception(struct bnxt * bp)10969 void bnxt_fw_exception(struct bnxt *bp)
10970 {
10971 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
10972 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
10973 	bnxt_rtnl_lock_sp(bp);
10974 	bnxt_force_fw_reset(bp);
10975 	bnxt_rtnl_unlock_sp(bp);
10976 }
10977 
10978 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
10979  * < 0 on error.
10980  */
bnxt_get_registered_vfs(struct bnxt * bp)10981 static int bnxt_get_registered_vfs(struct bnxt *bp)
10982 {
10983 #ifdef CONFIG_BNXT_SRIOV
10984 	int rc;
10985 
10986 	if (!BNXT_PF(bp))
10987 		return 0;
10988 
10989 	rc = bnxt_hwrm_func_qcfg(bp);
10990 	if (rc) {
10991 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
10992 		return rc;
10993 	}
10994 	if (bp->pf.registered_vfs)
10995 		return bp->pf.registered_vfs;
10996 	if (bp->sriov_cfg)
10997 		return 1;
10998 #endif
10999 	return 0;
11000 }
11001 
bnxt_fw_reset(struct bnxt * bp)11002 void bnxt_fw_reset(struct bnxt *bp)
11003 {
11004 	bnxt_rtnl_lock_sp(bp);
11005 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
11006 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
11007 		int n = 0, tmo;
11008 
11009 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11010 		if (bp->pf.active_vfs &&
11011 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
11012 			n = bnxt_get_registered_vfs(bp);
11013 		if (n < 0) {
11014 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
11015 				   n);
11016 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11017 			dev_close(bp->dev);
11018 			goto fw_reset_exit;
11019 		} else if (n > 0) {
11020 			u16 vf_tmo_dsecs = n * 10;
11021 
11022 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
11023 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
11024 			bp->fw_reset_state =
11025 				BNXT_FW_RESET_STATE_POLL_VF;
11026 			bnxt_queue_fw_reset_work(bp, HZ / 10);
11027 			goto fw_reset_exit;
11028 		}
11029 		bnxt_fw_reset_close(bp);
11030 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
11031 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
11032 			tmo = HZ / 10;
11033 		} else {
11034 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11035 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
11036 		}
11037 		bnxt_queue_fw_reset_work(bp, tmo);
11038 	}
11039 fw_reset_exit:
11040 	bnxt_rtnl_unlock_sp(bp);
11041 }
11042 
bnxt_chk_missed_irq(struct bnxt * bp)11043 static void bnxt_chk_missed_irq(struct bnxt *bp)
11044 {
11045 	int i;
11046 
11047 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
11048 		return;
11049 
11050 	for (i = 0; i < bp->cp_nr_rings; i++) {
11051 		struct bnxt_napi *bnapi = bp->bnapi[i];
11052 		struct bnxt_cp_ring_info *cpr;
11053 		u32 fw_ring_id;
11054 		int j;
11055 
11056 		if (!bnapi)
11057 			continue;
11058 
11059 		cpr = &bnapi->cp_ring;
11060 		for (j = 0; j < 2; j++) {
11061 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
11062 			u32 val[2];
11063 
11064 			if (!cpr2 || cpr2->has_more_work ||
11065 			    !bnxt_has_work(bp, cpr2))
11066 				continue;
11067 
11068 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
11069 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
11070 				continue;
11071 			}
11072 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
11073 			bnxt_dbg_hwrm_ring_info_get(bp,
11074 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
11075 				fw_ring_id, &val[0], &val[1]);
11076 			cpr->sw_stats.cmn.missed_irqs++;
11077 		}
11078 	}
11079 }
11080 
11081 static void bnxt_cfg_ntp_filters(struct bnxt *);
11082 
bnxt_init_ethtool_link_settings(struct bnxt * bp)11083 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
11084 {
11085 	struct bnxt_link_info *link_info = &bp->link_info;
11086 
11087 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
11088 		link_info->autoneg = BNXT_AUTONEG_SPEED;
11089 		if (bp->hwrm_spec_code >= 0x10201) {
11090 			if (link_info->auto_pause_setting &
11091 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
11092 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
11093 		} else {
11094 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
11095 		}
11096 		link_info->advertising = link_info->auto_link_speeds;
11097 		link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
11098 	} else {
11099 		link_info->req_link_speed = link_info->force_link_speed;
11100 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
11101 		if (link_info->force_pam4_link_speed) {
11102 			link_info->req_link_speed =
11103 				link_info->force_pam4_link_speed;
11104 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
11105 		}
11106 		link_info->req_duplex = link_info->duplex_setting;
11107 	}
11108 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
11109 		link_info->req_flow_ctrl =
11110 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
11111 	else
11112 		link_info->req_flow_ctrl = link_info->force_pause_setting;
11113 }
11114 
bnxt_sp_task(struct work_struct * work)11115 static void bnxt_sp_task(struct work_struct *work)
11116 {
11117 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
11118 
11119 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11120 	smp_mb__after_atomic();
11121 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
11122 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11123 		return;
11124 	}
11125 
11126 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
11127 		bnxt_cfg_rx_mode(bp);
11128 
11129 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
11130 		bnxt_cfg_ntp_filters(bp);
11131 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
11132 		bnxt_hwrm_exec_fwd_req(bp);
11133 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
11134 		bnxt_hwrm_port_qstats(bp, 0);
11135 		bnxt_hwrm_port_qstats_ext(bp, 0);
11136 		bnxt_accumulate_all_stats(bp);
11137 	}
11138 
11139 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
11140 		int rc;
11141 
11142 		mutex_lock(&bp->link_lock);
11143 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
11144 				       &bp->sp_event))
11145 			bnxt_hwrm_phy_qcaps(bp);
11146 
11147 		rc = bnxt_update_link(bp, true);
11148 		if (rc)
11149 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
11150 				   rc);
11151 
11152 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
11153 				       &bp->sp_event))
11154 			bnxt_init_ethtool_link_settings(bp);
11155 		mutex_unlock(&bp->link_lock);
11156 	}
11157 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
11158 		int rc;
11159 
11160 		mutex_lock(&bp->link_lock);
11161 		rc = bnxt_update_phy_setting(bp);
11162 		mutex_unlock(&bp->link_lock);
11163 		if (rc) {
11164 			netdev_warn(bp->dev, "update phy settings retry failed\n");
11165 		} else {
11166 			bp->link_info.phy_retry = false;
11167 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
11168 		}
11169 	}
11170 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
11171 		mutex_lock(&bp->link_lock);
11172 		bnxt_get_port_module_status(bp);
11173 		mutex_unlock(&bp->link_lock);
11174 	}
11175 
11176 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
11177 		bnxt_tc_flow_stats_work(bp);
11178 
11179 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
11180 		bnxt_chk_missed_irq(bp);
11181 
11182 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
11183 	 * must be the last functions to be called before exiting.
11184 	 */
11185 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
11186 		bnxt_reset(bp, false);
11187 
11188 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
11189 		bnxt_reset(bp, true);
11190 
11191 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
11192 		bnxt_rx_ring_reset(bp);
11193 
11194 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event))
11195 		bnxt_devlink_health_report(bp, BNXT_FW_RESET_NOTIFY_SP_EVENT);
11196 
11197 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
11198 		if (!is_bnxt_fw_ok(bp))
11199 			bnxt_devlink_health_report(bp,
11200 						   BNXT_FW_EXCEPTION_SP_EVENT);
11201 	}
11202 
11203 	smp_mb__before_atomic();
11204 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11205 }
11206 
11207 /* Under rtnl_lock */
bnxt_check_rings(struct bnxt * bp,int tx,int rx,bool sh,int tcs,int tx_xdp)11208 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
11209 		     int tx_xdp)
11210 {
11211 	int max_rx, max_tx, tx_sets = 1;
11212 	int tx_rings_needed, stats;
11213 	int rx_rings = rx;
11214 	int cp, vnics, rc;
11215 
11216 	if (tcs)
11217 		tx_sets = tcs;
11218 
11219 	rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
11220 	if (rc)
11221 		return rc;
11222 
11223 	if (max_rx < rx)
11224 		return -ENOMEM;
11225 
11226 	tx_rings_needed = tx * tx_sets + tx_xdp;
11227 	if (max_tx < tx_rings_needed)
11228 		return -ENOMEM;
11229 
11230 	vnics = 1;
11231 	if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
11232 		vnics += rx_rings;
11233 
11234 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
11235 		rx_rings <<= 1;
11236 	cp = sh ? max_t(int, tx_rings_needed, rx) : tx_rings_needed + rx;
11237 	stats = cp;
11238 	if (BNXT_NEW_RM(bp)) {
11239 		cp += bnxt_get_ulp_msix_num(bp);
11240 		stats += bnxt_get_ulp_stat_ctxs(bp);
11241 	}
11242 	return bnxt_hwrm_check_rings(bp, tx_rings_needed, rx_rings, rx, cp,
11243 				     stats, vnics);
11244 }
11245 
bnxt_unmap_bars(struct bnxt * bp,struct pci_dev * pdev)11246 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
11247 {
11248 	if (bp->bar2) {
11249 		pci_iounmap(pdev, bp->bar2);
11250 		bp->bar2 = NULL;
11251 	}
11252 
11253 	if (bp->bar1) {
11254 		pci_iounmap(pdev, bp->bar1);
11255 		bp->bar1 = NULL;
11256 	}
11257 
11258 	if (bp->bar0) {
11259 		pci_iounmap(pdev, bp->bar0);
11260 		bp->bar0 = NULL;
11261 	}
11262 }
11263 
bnxt_cleanup_pci(struct bnxt * bp)11264 static void bnxt_cleanup_pci(struct bnxt *bp)
11265 {
11266 	bnxt_unmap_bars(bp, bp->pdev);
11267 	pci_release_regions(bp->pdev);
11268 	if (pci_is_enabled(bp->pdev))
11269 		pci_disable_device(bp->pdev);
11270 }
11271 
bnxt_init_dflt_coal(struct bnxt * bp)11272 static void bnxt_init_dflt_coal(struct bnxt *bp)
11273 {
11274 	struct bnxt_coal *coal;
11275 
11276 	/* Tick values in micro seconds.
11277 	 * 1 coal_buf x bufs_per_record = 1 completion record.
11278 	 */
11279 	coal = &bp->rx_coal;
11280 	coal->coal_ticks = 10;
11281 	coal->coal_bufs = 30;
11282 	coal->coal_ticks_irq = 1;
11283 	coal->coal_bufs_irq = 2;
11284 	coal->idle_thresh = 50;
11285 	coal->bufs_per_record = 2;
11286 	coal->budget = 64;		/* NAPI budget */
11287 
11288 	coal = &bp->tx_coal;
11289 	coal->coal_ticks = 28;
11290 	coal->coal_bufs = 30;
11291 	coal->coal_ticks_irq = 2;
11292 	coal->coal_bufs_irq = 2;
11293 	coal->bufs_per_record = 1;
11294 
11295 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
11296 }
11297 
bnxt_fw_reset_via_optee(struct bnxt * bp)11298 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
11299 {
11300 #ifdef CONFIG_TEE_BNXT_FW
11301 	int rc = tee_bnxt_fw_load();
11302 
11303 	if (rc)
11304 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
11305 
11306 	return rc;
11307 #else
11308 	netdev_err(bp->dev, "OP-TEE not supported\n");
11309 	return -ENODEV;
11310 #endif
11311 }
11312 
bnxt_fw_init_one_p1(struct bnxt * bp)11313 static int bnxt_fw_init_one_p1(struct bnxt *bp)
11314 {
11315 	int rc;
11316 
11317 	bp->fw_cap = 0;
11318 	rc = bnxt_hwrm_ver_get(bp);
11319 	bnxt_try_map_fw_health_reg(bp);
11320 	if (rc) {
11321 		if (bp->fw_health && bp->fw_health->status_reliable) {
11322 			u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11323 
11324 			netdev_err(bp->dev,
11325 				   "Firmware not responding, status: 0x%x\n",
11326 				   sts);
11327 			if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
11328 				netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
11329 				rc = bnxt_fw_reset_via_optee(bp);
11330 				if (!rc)
11331 					rc = bnxt_hwrm_ver_get(bp);
11332 			}
11333 		}
11334 		if (rc)
11335 			return rc;
11336 	}
11337 
11338 	if (bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL) {
11339 		rc = bnxt_alloc_kong_hwrm_resources(bp);
11340 		if (rc)
11341 			bp->fw_cap &= ~BNXT_FW_CAP_KONG_MB_CHNL;
11342 	}
11343 
11344 	if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
11345 	    bp->hwrm_max_ext_req_len > BNXT_HWRM_MAX_REQ_LEN) {
11346 		rc = bnxt_alloc_hwrm_short_cmd_req(bp);
11347 		if (rc)
11348 			return rc;
11349 	}
11350 	bnxt_nvm_cfg_ver_get(bp);
11351 
11352 	rc = bnxt_hwrm_func_reset(bp);
11353 	if (rc)
11354 		return -ENODEV;
11355 
11356 	bnxt_hwrm_fw_set_time(bp);
11357 	return 0;
11358 }
11359 
bnxt_fw_init_one_p2(struct bnxt * bp)11360 static int bnxt_fw_init_one_p2(struct bnxt *bp)
11361 {
11362 	int rc;
11363 
11364 	/* Get the MAX capabilities for this function */
11365 	rc = bnxt_hwrm_func_qcaps(bp);
11366 	if (rc) {
11367 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
11368 			   rc);
11369 		return -ENODEV;
11370 	}
11371 
11372 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
11373 	if (rc)
11374 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
11375 			    rc);
11376 
11377 	if (bnxt_alloc_fw_health(bp)) {
11378 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
11379 	} else {
11380 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
11381 		if (rc)
11382 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
11383 				    rc);
11384 	}
11385 
11386 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
11387 	if (rc)
11388 		return -ENODEV;
11389 
11390 	bnxt_hwrm_func_qcfg(bp);
11391 	bnxt_hwrm_vnic_qcaps(bp);
11392 	bnxt_hwrm_port_led_qcaps(bp);
11393 	bnxt_ethtool_init(bp);
11394 	bnxt_dcb_init(bp);
11395 	return 0;
11396 }
11397 
bnxt_set_dflt_rss_hash_type(struct bnxt * bp)11398 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
11399 {
11400 	bp->flags &= ~BNXT_FLAG_UDP_RSS_CAP;
11401 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
11402 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
11403 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
11404 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
11405 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
11406 		bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
11407 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
11408 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
11409 	}
11410 }
11411 
bnxt_set_dflt_rfs(struct bnxt * bp)11412 static void bnxt_set_dflt_rfs(struct bnxt *bp)
11413 {
11414 	struct net_device *dev = bp->dev;
11415 
11416 	dev->hw_features &= ~NETIF_F_NTUPLE;
11417 	dev->features &= ~NETIF_F_NTUPLE;
11418 	bp->flags &= ~BNXT_FLAG_RFS;
11419 	if (bnxt_rfs_supported(bp)) {
11420 		dev->hw_features |= NETIF_F_NTUPLE;
11421 		if (bnxt_rfs_capable(bp)) {
11422 			bp->flags |= BNXT_FLAG_RFS;
11423 			dev->features |= NETIF_F_NTUPLE;
11424 		}
11425 	}
11426 }
11427 
bnxt_fw_init_one_p3(struct bnxt * bp)11428 static void bnxt_fw_init_one_p3(struct bnxt *bp)
11429 {
11430 	struct pci_dev *pdev = bp->pdev;
11431 
11432 	bnxt_set_dflt_rss_hash_type(bp);
11433 	bnxt_set_dflt_rfs(bp);
11434 
11435 	bnxt_get_wol_settings(bp);
11436 	if (bp->flags & BNXT_FLAG_WOL_CAP)
11437 		device_set_wakeup_enable(&pdev->dev, bp->wol);
11438 	else
11439 		device_set_wakeup_capable(&pdev->dev, false);
11440 
11441 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
11442 	bnxt_hwrm_coal_params_qcaps(bp);
11443 }
11444 
11445 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
11446 
bnxt_fw_init_one(struct bnxt * bp)11447 static int bnxt_fw_init_one(struct bnxt *bp)
11448 {
11449 	int rc;
11450 
11451 	rc = bnxt_fw_init_one_p1(bp);
11452 	if (rc) {
11453 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
11454 		return rc;
11455 	}
11456 	rc = bnxt_fw_init_one_p2(bp);
11457 	if (rc) {
11458 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
11459 		return rc;
11460 	}
11461 	rc = bnxt_probe_phy(bp, false);
11462 	if (rc)
11463 		return rc;
11464 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
11465 	if (rc)
11466 		return rc;
11467 
11468 	/* In case fw capabilities have changed, destroy the unneeded
11469 	 * reporters and create newly capable ones.
11470 	 */
11471 	bnxt_dl_fw_reporters_destroy(bp, false);
11472 	bnxt_dl_fw_reporters_create(bp);
11473 	bnxt_fw_init_one_p3(bp);
11474 	return 0;
11475 }
11476 
bnxt_fw_reset_writel(struct bnxt * bp,int reg_idx)11477 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
11478 {
11479 	struct bnxt_fw_health *fw_health = bp->fw_health;
11480 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
11481 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
11482 	u32 reg_type, reg_off, delay_msecs;
11483 
11484 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
11485 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
11486 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
11487 	switch (reg_type) {
11488 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
11489 		pci_write_config_dword(bp->pdev, reg_off, val);
11490 		break;
11491 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
11492 		writel(reg_off & BNXT_GRC_BASE_MASK,
11493 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
11494 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
11495 		fallthrough;
11496 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
11497 		writel(val, bp->bar0 + reg_off);
11498 		break;
11499 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
11500 		writel(val, bp->bar1 + reg_off);
11501 		break;
11502 	}
11503 	if (delay_msecs) {
11504 		pci_read_config_dword(bp->pdev, 0, &val);
11505 		msleep(delay_msecs);
11506 	}
11507 }
11508 
bnxt_reset_all(struct bnxt * bp)11509 static void bnxt_reset_all(struct bnxt *bp)
11510 {
11511 	struct bnxt_fw_health *fw_health = bp->fw_health;
11512 	int i, rc;
11513 
11514 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
11515 		bnxt_fw_reset_via_optee(bp);
11516 		bp->fw_reset_timestamp = jiffies;
11517 		return;
11518 	}
11519 
11520 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
11521 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
11522 			bnxt_fw_reset_writel(bp, i);
11523 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
11524 		struct hwrm_fw_reset_input req = {0};
11525 
11526 		bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
11527 		req.resp_addr = cpu_to_le64(bp->hwrm_cmd_kong_resp_dma_addr);
11528 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
11529 		req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
11530 		req.flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
11531 		rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
11532 		if (rc)
11533 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
11534 	}
11535 	bp->fw_reset_timestamp = jiffies;
11536 }
11537 
bnxt_fw_reset_task(struct work_struct * work)11538 static void bnxt_fw_reset_task(struct work_struct *work)
11539 {
11540 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
11541 	int rc;
11542 
11543 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
11544 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
11545 		return;
11546 	}
11547 
11548 	switch (bp->fw_reset_state) {
11549 	case BNXT_FW_RESET_STATE_POLL_VF: {
11550 		int n = bnxt_get_registered_vfs(bp);
11551 		int tmo;
11552 
11553 		if (n < 0) {
11554 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
11555 				   n, jiffies_to_msecs(jiffies -
11556 				   bp->fw_reset_timestamp));
11557 			goto fw_reset_abort;
11558 		} else if (n > 0) {
11559 			if (time_after(jiffies, bp->fw_reset_timestamp +
11560 				       (bp->fw_reset_max_dsecs * HZ / 10))) {
11561 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11562 				bp->fw_reset_state = 0;
11563 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
11564 					   n);
11565 				return;
11566 			}
11567 			bnxt_queue_fw_reset_work(bp, HZ / 10);
11568 			return;
11569 		}
11570 		bp->fw_reset_timestamp = jiffies;
11571 		rtnl_lock();
11572 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
11573 			rtnl_unlock();
11574 			goto fw_reset_abort;
11575 		}
11576 		bnxt_fw_reset_close(bp);
11577 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
11578 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
11579 			tmo = HZ / 10;
11580 		} else {
11581 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11582 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
11583 		}
11584 		rtnl_unlock();
11585 		bnxt_queue_fw_reset_work(bp, tmo);
11586 		return;
11587 	}
11588 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
11589 		u32 val;
11590 
11591 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11592 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
11593 		    !time_after(jiffies, bp->fw_reset_timestamp +
11594 		    (bp->fw_reset_max_dsecs * HZ / 10))) {
11595 			bnxt_queue_fw_reset_work(bp, HZ / 5);
11596 			return;
11597 		}
11598 
11599 		if (!bp->fw_health->master) {
11600 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
11601 
11602 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11603 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
11604 			return;
11605 		}
11606 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
11607 	}
11608 		fallthrough;
11609 	case BNXT_FW_RESET_STATE_RESET_FW:
11610 		bnxt_reset_all(bp);
11611 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11612 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
11613 		return;
11614 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
11615 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
11616 			u32 val;
11617 
11618 			val = bnxt_fw_health_readl(bp,
11619 						   BNXT_FW_RESET_INPROG_REG);
11620 			if (val)
11621 				netdev_warn(bp->dev, "FW reset inprog %x after min wait time.\n",
11622 					    val);
11623 		}
11624 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
11625 		if (pci_enable_device(bp->pdev)) {
11626 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
11627 			goto fw_reset_abort;
11628 		}
11629 		pci_set_master(bp->pdev);
11630 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
11631 		fallthrough;
11632 	case BNXT_FW_RESET_STATE_POLL_FW:
11633 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
11634 		rc = __bnxt_hwrm_ver_get(bp, true);
11635 		if (rc) {
11636 			if (time_after(jiffies, bp->fw_reset_timestamp +
11637 				       (bp->fw_reset_max_dsecs * HZ / 10))) {
11638 				netdev_err(bp->dev, "Firmware reset aborted\n");
11639 				goto fw_reset_abort_status;
11640 			}
11641 			bnxt_queue_fw_reset_work(bp, HZ / 5);
11642 			return;
11643 		}
11644 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
11645 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
11646 		fallthrough;
11647 	case BNXT_FW_RESET_STATE_OPENING:
11648 		while (!rtnl_trylock()) {
11649 			bnxt_queue_fw_reset_work(bp, HZ / 10);
11650 			return;
11651 		}
11652 		rc = bnxt_open(bp->dev);
11653 		if (rc) {
11654 			netdev_err(bp->dev, "bnxt_open_nic() failed\n");
11655 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11656 			dev_close(bp->dev);
11657 		}
11658 
11659 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
11660 		    bp->fw_health->enabled) {
11661 			bp->fw_health->last_fw_reset_cnt =
11662 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
11663 		}
11664 		bp->fw_reset_state = 0;
11665 		/* Make sure fw_reset_state is 0 before clearing the flag */
11666 		smp_mb__before_atomic();
11667 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11668 		bnxt_ulp_start(bp, rc);
11669 		if (!rc)
11670 			bnxt_reenable_sriov(bp);
11671 		bnxt_dl_health_recovery_done(bp);
11672 		bnxt_dl_health_status_update(bp, true);
11673 		rtnl_unlock();
11674 		break;
11675 	}
11676 	return;
11677 
11678 fw_reset_abort_status:
11679 	if (bp->fw_health->status_reliable ||
11680 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
11681 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11682 
11683 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
11684 	}
11685 fw_reset_abort:
11686 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11687 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
11688 		bnxt_dl_health_status_update(bp, false);
11689 	bp->fw_reset_state = 0;
11690 	rtnl_lock();
11691 	dev_close(bp->dev);
11692 	rtnl_unlock();
11693 }
11694 
bnxt_init_board(struct pci_dev * pdev,struct net_device * dev)11695 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
11696 {
11697 	int rc;
11698 	struct bnxt *bp = netdev_priv(dev);
11699 
11700 	SET_NETDEV_DEV(dev, &pdev->dev);
11701 
11702 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
11703 	rc = pci_enable_device(pdev);
11704 	if (rc) {
11705 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
11706 		goto init_err;
11707 	}
11708 
11709 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
11710 		dev_err(&pdev->dev,
11711 			"Cannot find PCI device base address, aborting\n");
11712 		rc = -ENODEV;
11713 		goto init_err_disable;
11714 	}
11715 
11716 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
11717 	if (rc) {
11718 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
11719 		goto init_err_disable;
11720 	}
11721 
11722 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
11723 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
11724 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
11725 		rc = -EIO;
11726 		goto init_err_release;
11727 	}
11728 
11729 	pci_set_master(pdev);
11730 
11731 	bp->dev = dev;
11732 	bp->pdev = pdev;
11733 
11734 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
11735 	 * determines the BAR size.
11736 	 */
11737 	bp->bar0 = pci_ioremap_bar(pdev, 0);
11738 	if (!bp->bar0) {
11739 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
11740 		rc = -ENOMEM;
11741 		goto init_err_release;
11742 	}
11743 
11744 	bp->bar2 = pci_ioremap_bar(pdev, 4);
11745 	if (!bp->bar2) {
11746 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
11747 		rc = -ENOMEM;
11748 		goto init_err_release;
11749 	}
11750 
11751 	pci_enable_pcie_error_reporting(pdev);
11752 
11753 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
11754 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
11755 
11756 	spin_lock_init(&bp->ntp_fltr_lock);
11757 #if BITS_PER_LONG == 32
11758 	spin_lock_init(&bp->db_lock);
11759 #endif
11760 
11761 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
11762 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
11763 
11764 	bnxt_init_dflt_coal(bp);
11765 
11766 	timer_setup(&bp->timer, bnxt_timer, 0);
11767 	bp->current_interval = BNXT_TIMER_INTERVAL;
11768 
11769 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
11770 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
11771 
11772 	clear_bit(BNXT_STATE_OPEN, &bp->state);
11773 	return 0;
11774 
11775 init_err_release:
11776 	bnxt_unmap_bars(bp, pdev);
11777 	pci_release_regions(pdev);
11778 
11779 init_err_disable:
11780 	pci_disable_device(pdev);
11781 
11782 init_err:
11783 	return rc;
11784 }
11785 
11786 /* rtnl_lock held */
bnxt_change_mac_addr(struct net_device * dev,void * p)11787 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
11788 {
11789 	struct sockaddr *addr = p;
11790 	struct bnxt *bp = netdev_priv(dev);
11791 	int rc = 0;
11792 
11793 	if (!is_valid_ether_addr(addr->sa_data))
11794 		return -EADDRNOTAVAIL;
11795 
11796 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
11797 		return 0;
11798 
11799 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
11800 	if (rc)
11801 		return rc;
11802 
11803 	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
11804 	if (netif_running(dev)) {
11805 		bnxt_close_nic(bp, false, false);
11806 		rc = bnxt_open_nic(bp, false, false);
11807 	}
11808 
11809 	return rc;
11810 }
11811 
11812 /* rtnl_lock held */
bnxt_change_mtu(struct net_device * dev,int new_mtu)11813 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
11814 {
11815 	struct bnxt *bp = netdev_priv(dev);
11816 
11817 	if (netif_running(dev))
11818 		bnxt_close_nic(bp, true, false);
11819 
11820 	dev->mtu = new_mtu;
11821 	bnxt_set_ring_params(bp);
11822 
11823 	if (netif_running(dev))
11824 		return bnxt_open_nic(bp, true, false);
11825 
11826 	return 0;
11827 }
11828 
bnxt_setup_mq_tc(struct net_device * dev,u8 tc)11829 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
11830 {
11831 	struct bnxt *bp = netdev_priv(dev);
11832 	bool sh = false;
11833 	int rc;
11834 
11835 	if (tc > bp->max_tc) {
11836 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
11837 			   tc, bp->max_tc);
11838 		return -EINVAL;
11839 	}
11840 
11841 	if (netdev_get_num_tc(dev) == tc)
11842 		return 0;
11843 
11844 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11845 		sh = true;
11846 
11847 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
11848 			      sh, tc, bp->tx_nr_rings_xdp);
11849 	if (rc)
11850 		return rc;
11851 
11852 	/* Needs to close the device and do hw resource re-allocations */
11853 	if (netif_running(bp->dev))
11854 		bnxt_close_nic(bp, true, false);
11855 
11856 	if (tc) {
11857 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
11858 		netdev_set_num_tc(dev, tc);
11859 	} else {
11860 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
11861 		netdev_reset_tc(dev);
11862 	}
11863 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
11864 	bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
11865 			       bp->tx_nr_rings + bp->rx_nr_rings;
11866 
11867 	if (netif_running(bp->dev))
11868 		return bnxt_open_nic(bp, true, false);
11869 
11870 	return 0;
11871 }
11872 
bnxt_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)11873 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
11874 				  void *cb_priv)
11875 {
11876 	struct bnxt *bp = cb_priv;
11877 
11878 	if (!bnxt_tc_flower_enabled(bp) ||
11879 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
11880 		return -EOPNOTSUPP;
11881 
11882 	switch (type) {
11883 	case TC_SETUP_CLSFLOWER:
11884 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
11885 	default:
11886 		return -EOPNOTSUPP;
11887 	}
11888 }
11889 
11890 LIST_HEAD(bnxt_block_cb_list);
11891 
bnxt_setup_tc(struct net_device * dev,enum tc_setup_type type,void * type_data)11892 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
11893 			 void *type_data)
11894 {
11895 	struct bnxt *bp = netdev_priv(dev);
11896 
11897 	switch (type) {
11898 	case TC_SETUP_BLOCK:
11899 		return flow_block_cb_setup_simple(type_data,
11900 						  &bnxt_block_cb_list,
11901 						  bnxt_setup_tc_block_cb,
11902 						  bp, bp, true);
11903 	case TC_SETUP_QDISC_MQPRIO: {
11904 		struct tc_mqprio_qopt *mqprio = type_data;
11905 
11906 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
11907 
11908 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
11909 	}
11910 	default:
11911 		return -EOPNOTSUPP;
11912 	}
11913 }
11914 
11915 #ifdef CONFIG_RFS_ACCEL
bnxt_fltr_match(struct bnxt_ntuple_filter * f1,struct bnxt_ntuple_filter * f2)11916 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
11917 			    struct bnxt_ntuple_filter *f2)
11918 {
11919 	struct flow_keys *keys1 = &f1->fkeys;
11920 	struct flow_keys *keys2 = &f2->fkeys;
11921 
11922 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
11923 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
11924 		return false;
11925 
11926 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
11927 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
11928 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst)
11929 			return false;
11930 	} else {
11931 		if (memcmp(&keys1->addrs.v6addrs.src, &keys2->addrs.v6addrs.src,
11932 			   sizeof(keys1->addrs.v6addrs.src)) ||
11933 		    memcmp(&keys1->addrs.v6addrs.dst, &keys2->addrs.v6addrs.dst,
11934 			   sizeof(keys1->addrs.v6addrs.dst)))
11935 			return false;
11936 	}
11937 
11938 	if (keys1->ports.ports == keys2->ports.ports &&
11939 	    keys1->control.flags == keys2->control.flags &&
11940 	    ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
11941 	    ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
11942 		return true;
11943 
11944 	return false;
11945 }
11946 
bnxt_rx_flow_steer(struct net_device * dev,const struct sk_buff * skb,u16 rxq_index,u32 flow_id)11947 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
11948 			      u16 rxq_index, u32 flow_id)
11949 {
11950 	struct bnxt *bp = netdev_priv(dev);
11951 	struct bnxt_ntuple_filter *fltr, *new_fltr;
11952 	struct flow_keys *fkeys;
11953 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
11954 	int rc = 0, idx, bit_id, l2_idx = 0;
11955 	struct hlist_head *head;
11956 	u32 flags;
11957 
11958 	if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
11959 		struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
11960 		int off = 0, j;
11961 
11962 		netif_addr_lock_bh(dev);
11963 		for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
11964 			if (ether_addr_equal(eth->h_dest,
11965 					     vnic->uc_list + off)) {
11966 				l2_idx = j + 1;
11967 				break;
11968 			}
11969 		}
11970 		netif_addr_unlock_bh(dev);
11971 		if (!l2_idx)
11972 			return -EINVAL;
11973 	}
11974 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
11975 	if (!new_fltr)
11976 		return -ENOMEM;
11977 
11978 	fkeys = &new_fltr->fkeys;
11979 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
11980 		rc = -EPROTONOSUPPORT;
11981 		goto err_free;
11982 	}
11983 
11984 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
11985 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
11986 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
11987 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
11988 		rc = -EPROTONOSUPPORT;
11989 		goto err_free;
11990 	}
11991 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
11992 	    bp->hwrm_spec_code < 0x10601) {
11993 		rc = -EPROTONOSUPPORT;
11994 		goto err_free;
11995 	}
11996 	flags = fkeys->control.flags;
11997 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
11998 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
11999 		rc = -EPROTONOSUPPORT;
12000 		goto err_free;
12001 	}
12002 
12003 	memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
12004 	memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
12005 
12006 	idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
12007 	head = &bp->ntp_fltr_hash_tbl[idx];
12008 	rcu_read_lock();
12009 	hlist_for_each_entry_rcu(fltr, head, hash) {
12010 		if (bnxt_fltr_match(fltr, new_fltr)) {
12011 			rc = fltr->sw_id;
12012 			rcu_read_unlock();
12013 			goto err_free;
12014 		}
12015 	}
12016 	rcu_read_unlock();
12017 
12018 	spin_lock_bh(&bp->ntp_fltr_lock);
12019 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
12020 					 BNXT_NTP_FLTR_MAX_FLTR, 0);
12021 	if (bit_id < 0) {
12022 		spin_unlock_bh(&bp->ntp_fltr_lock);
12023 		rc = -ENOMEM;
12024 		goto err_free;
12025 	}
12026 
12027 	new_fltr->sw_id = (u16)bit_id;
12028 	new_fltr->flow_id = flow_id;
12029 	new_fltr->l2_fltr_idx = l2_idx;
12030 	new_fltr->rxq = rxq_index;
12031 	hlist_add_head_rcu(&new_fltr->hash, head);
12032 	bp->ntp_fltr_count++;
12033 	spin_unlock_bh(&bp->ntp_fltr_lock);
12034 
12035 	set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
12036 	bnxt_queue_sp_work(bp);
12037 
12038 	return new_fltr->sw_id;
12039 
12040 err_free:
12041 	kfree(new_fltr);
12042 	return rc;
12043 }
12044 
bnxt_cfg_ntp_filters(struct bnxt * bp)12045 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
12046 {
12047 	int i;
12048 
12049 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
12050 		struct hlist_head *head;
12051 		struct hlist_node *tmp;
12052 		struct bnxt_ntuple_filter *fltr;
12053 		int rc;
12054 
12055 		head = &bp->ntp_fltr_hash_tbl[i];
12056 		hlist_for_each_entry_safe(fltr, tmp, head, hash) {
12057 			bool del = false;
12058 
12059 			if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
12060 				if (rps_may_expire_flow(bp->dev, fltr->rxq,
12061 							fltr->flow_id,
12062 							fltr->sw_id)) {
12063 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
12064 									 fltr);
12065 					del = true;
12066 				}
12067 			} else {
12068 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
12069 								       fltr);
12070 				if (rc)
12071 					del = true;
12072 				else
12073 					set_bit(BNXT_FLTR_VALID, &fltr->state);
12074 			}
12075 
12076 			if (del) {
12077 				spin_lock_bh(&bp->ntp_fltr_lock);
12078 				hlist_del_rcu(&fltr->hash);
12079 				bp->ntp_fltr_count--;
12080 				spin_unlock_bh(&bp->ntp_fltr_lock);
12081 				synchronize_rcu();
12082 				clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
12083 				kfree(fltr);
12084 			}
12085 		}
12086 	}
12087 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
12088 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
12089 }
12090 
12091 #else
12092 
bnxt_cfg_ntp_filters(struct bnxt * bp)12093 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
12094 {
12095 }
12096 
12097 #endif /* CONFIG_RFS_ACCEL */
12098 
bnxt_udp_tunnel_sync(struct net_device * netdev,unsigned int table)12099 static int bnxt_udp_tunnel_sync(struct net_device *netdev, unsigned int table)
12100 {
12101 	struct bnxt *bp = netdev_priv(netdev);
12102 	struct udp_tunnel_info ti;
12103 	unsigned int cmd;
12104 
12105 	udp_tunnel_nic_get_port(netdev, table, 0, &ti);
12106 	if (ti.type == UDP_TUNNEL_TYPE_VXLAN)
12107 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
12108 	else
12109 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
12110 
12111 	if (ti.port)
12112 		return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti.port, cmd);
12113 
12114 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
12115 }
12116 
12117 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
12118 	.sync_table	= bnxt_udp_tunnel_sync,
12119 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
12120 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
12121 	.tables		= {
12122 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
12123 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
12124 	},
12125 };
12126 
bnxt_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 filter_mask,int nlflags)12127 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12128 			       struct net_device *dev, u32 filter_mask,
12129 			       int nlflags)
12130 {
12131 	struct bnxt *bp = netdev_priv(dev);
12132 
12133 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
12134 				       nlflags, filter_mask, NULL);
12135 }
12136 
bnxt_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags,struct netlink_ext_ack * extack)12137 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
12138 			       u16 flags, struct netlink_ext_ack *extack)
12139 {
12140 	struct bnxt *bp = netdev_priv(dev);
12141 	struct nlattr *attr, *br_spec;
12142 	int rem, rc = 0;
12143 
12144 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
12145 		return -EOPNOTSUPP;
12146 
12147 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12148 	if (!br_spec)
12149 		return -EINVAL;
12150 
12151 	nla_for_each_nested(attr, br_spec, rem) {
12152 		u16 mode;
12153 
12154 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
12155 			continue;
12156 
12157 		if (nla_len(attr) < sizeof(mode))
12158 			return -EINVAL;
12159 
12160 		mode = nla_get_u16(attr);
12161 		if (mode == bp->br_mode)
12162 			break;
12163 
12164 		rc = bnxt_hwrm_set_br_mode(bp, mode);
12165 		if (!rc)
12166 			bp->br_mode = mode;
12167 		break;
12168 	}
12169 	return rc;
12170 }
12171 
bnxt_get_port_parent_id(struct net_device * dev,struct netdev_phys_item_id * ppid)12172 int bnxt_get_port_parent_id(struct net_device *dev,
12173 			    struct netdev_phys_item_id *ppid)
12174 {
12175 	struct bnxt *bp = netdev_priv(dev);
12176 
12177 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
12178 		return -EOPNOTSUPP;
12179 
12180 	/* The PF and it's VF-reps only support the switchdev framework */
12181 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
12182 		return -EOPNOTSUPP;
12183 
12184 	ppid->id_len = sizeof(bp->dsn);
12185 	memcpy(ppid->id, bp->dsn, ppid->id_len);
12186 
12187 	return 0;
12188 }
12189 
bnxt_get_devlink_port(struct net_device * dev)12190 static struct devlink_port *bnxt_get_devlink_port(struct net_device *dev)
12191 {
12192 	struct bnxt *bp = netdev_priv(dev);
12193 
12194 	return &bp->dl_port;
12195 }
12196 
12197 static const struct net_device_ops bnxt_netdev_ops = {
12198 	.ndo_open		= bnxt_open,
12199 	.ndo_start_xmit		= bnxt_start_xmit,
12200 	.ndo_stop		= bnxt_close,
12201 	.ndo_get_stats64	= bnxt_get_stats64,
12202 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
12203 	.ndo_do_ioctl		= bnxt_ioctl,
12204 	.ndo_validate_addr	= eth_validate_addr,
12205 	.ndo_set_mac_address	= bnxt_change_mac_addr,
12206 	.ndo_change_mtu		= bnxt_change_mtu,
12207 	.ndo_fix_features	= bnxt_fix_features,
12208 	.ndo_set_features	= bnxt_set_features,
12209 	.ndo_tx_timeout		= bnxt_tx_timeout,
12210 #ifdef CONFIG_BNXT_SRIOV
12211 	.ndo_get_vf_config	= bnxt_get_vf_config,
12212 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
12213 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
12214 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
12215 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
12216 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
12217 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
12218 #endif
12219 	.ndo_setup_tc           = bnxt_setup_tc,
12220 #ifdef CONFIG_RFS_ACCEL
12221 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
12222 #endif
12223 	.ndo_udp_tunnel_add	= udp_tunnel_nic_add_port,
12224 	.ndo_udp_tunnel_del	= udp_tunnel_nic_del_port,
12225 	.ndo_bpf		= bnxt_xdp,
12226 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
12227 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
12228 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
12229 	.ndo_get_devlink_port	= bnxt_get_devlink_port,
12230 };
12231 
bnxt_remove_one(struct pci_dev * pdev)12232 static void bnxt_remove_one(struct pci_dev *pdev)
12233 {
12234 	struct net_device *dev = pci_get_drvdata(pdev);
12235 	struct bnxt *bp = netdev_priv(dev);
12236 
12237 	if (BNXT_PF(bp))
12238 		bnxt_sriov_disable(bp);
12239 
12240 	if (BNXT_PF(bp))
12241 		devlink_port_type_clear(&bp->dl_port);
12242 	pci_disable_pcie_error_reporting(pdev);
12243 	unregister_netdev(dev);
12244 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12245 	/* Flush any pending tasks */
12246 	cancel_work_sync(&bp->sp_task);
12247 	cancel_delayed_work_sync(&bp->fw_reset_task);
12248 	bp->sp_event = 0;
12249 
12250 	bnxt_dl_fw_reporters_destroy(bp, true);
12251 	bnxt_dl_unregister(bp);
12252 	bnxt_shutdown_tc(bp);
12253 
12254 	bnxt_clear_int_mode(bp);
12255 	bnxt_hwrm_func_drv_unrgtr(bp);
12256 	bnxt_free_hwrm_resources(bp);
12257 	bnxt_free_hwrm_short_cmd_req(bp);
12258 	bnxt_ethtool_free(bp);
12259 	bnxt_dcb_free(bp);
12260 	kfree(bp->edev);
12261 	bp->edev = NULL;
12262 	kfree(bp->fw_health);
12263 	bp->fw_health = NULL;
12264 	bnxt_cleanup_pci(bp);
12265 	bnxt_free_ctx_mem(bp);
12266 	kfree(bp->ctx);
12267 	bp->ctx = NULL;
12268 	kfree(bp->rss_indir_tbl);
12269 	bp->rss_indir_tbl = NULL;
12270 	bnxt_free_port_stats(bp);
12271 	free_netdev(dev);
12272 }
12273 
bnxt_probe_phy(struct bnxt * bp,bool fw_dflt)12274 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
12275 {
12276 	int rc = 0;
12277 	struct bnxt_link_info *link_info = &bp->link_info;
12278 
12279 	rc = bnxt_hwrm_phy_qcaps(bp);
12280 	if (rc) {
12281 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
12282 			   rc);
12283 		return rc;
12284 	}
12285 	if (!fw_dflt)
12286 		return 0;
12287 
12288 	rc = bnxt_update_link(bp, false);
12289 	if (rc) {
12290 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
12291 			   rc);
12292 		return rc;
12293 	}
12294 
12295 	/* Older firmware does not have supported_auto_speeds, so assume
12296 	 * that all supported speeds can be autonegotiated.
12297 	 */
12298 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
12299 		link_info->support_auto_speeds = link_info->support_speeds;
12300 
12301 	bnxt_init_ethtool_link_settings(bp);
12302 	return 0;
12303 }
12304 
bnxt_get_max_irq(struct pci_dev * pdev)12305 static int bnxt_get_max_irq(struct pci_dev *pdev)
12306 {
12307 	u16 ctrl;
12308 
12309 	if (!pdev->msix_cap)
12310 		return 1;
12311 
12312 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
12313 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
12314 }
12315 
_bnxt_get_max_rings(struct bnxt * bp,int * max_rx,int * max_tx,int * max_cp)12316 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
12317 				int *max_cp)
12318 {
12319 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12320 	int max_ring_grps = 0, max_irq;
12321 
12322 	*max_tx = hw_resc->max_tx_rings;
12323 	*max_rx = hw_resc->max_rx_rings;
12324 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
12325 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
12326 			bnxt_get_ulp_msix_num(bp),
12327 			hw_resc->max_stat_ctxs - bnxt_get_ulp_stat_ctxs(bp));
12328 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
12329 		*max_cp = min_t(int, *max_cp, max_irq);
12330 	max_ring_grps = hw_resc->max_hw_ring_grps;
12331 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
12332 		*max_cp -= 1;
12333 		*max_rx -= 2;
12334 	}
12335 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
12336 		*max_rx >>= 1;
12337 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
12338 		bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
12339 		/* On P5 chips, max_cp output param should be available NQs */
12340 		*max_cp = max_irq;
12341 	}
12342 	*max_rx = min_t(int, *max_rx, max_ring_grps);
12343 }
12344 
bnxt_get_max_rings(struct bnxt * bp,int * max_rx,int * max_tx,bool shared)12345 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
12346 {
12347 	int rx, tx, cp;
12348 
12349 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
12350 	*max_rx = rx;
12351 	*max_tx = tx;
12352 	if (!rx || !tx || !cp)
12353 		return -ENOMEM;
12354 
12355 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
12356 }
12357 
bnxt_get_dflt_rings(struct bnxt * bp,int * max_rx,int * max_tx,bool shared)12358 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
12359 			       bool shared)
12360 {
12361 	int rc;
12362 
12363 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
12364 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
12365 		/* Not enough rings, try disabling agg rings. */
12366 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
12367 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
12368 		if (rc) {
12369 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
12370 			bp->flags |= BNXT_FLAG_AGG_RINGS;
12371 			return rc;
12372 		}
12373 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
12374 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12375 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12376 		bnxt_set_ring_params(bp);
12377 	}
12378 
12379 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
12380 		int max_cp, max_stat, max_irq;
12381 
12382 		/* Reserve minimum resources for RoCE */
12383 		max_cp = bnxt_get_max_func_cp_rings(bp);
12384 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
12385 		max_irq = bnxt_get_max_func_irqs(bp);
12386 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
12387 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
12388 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
12389 			return 0;
12390 
12391 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
12392 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
12393 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
12394 		max_cp = min_t(int, max_cp, max_irq);
12395 		max_cp = min_t(int, max_cp, max_stat);
12396 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
12397 		if (rc)
12398 			rc = 0;
12399 	}
12400 	return rc;
12401 }
12402 
12403 /* In initial default shared ring setting, each shared ring must have a
12404  * RX/TX ring pair.
12405  */
bnxt_trim_dflt_sh_rings(struct bnxt * bp)12406 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
12407 {
12408 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
12409 	bp->rx_nr_rings = bp->cp_nr_rings;
12410 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
12411 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
12412 }
12413 
bnxt_set_dflt_rings(struct bnxt * bp,bool sh)12414 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
12415 {
12416 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
12417 
12418 	if (!bnxt_can_reserve_rings(bp))
12419 		return 0;
12420 
12421 	if (sh)
12422 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
12423 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
12424 	/* Reduce default rings on multi-port cards so that total default
12425 	 * rings do not exceed CPU count.
12426 	 */
12427 	if (bp->port_count > 1) {
12428 		int max_rings =
12429 			max_t(int, num_online_cpus() / bp->port_count, 1);
12430 
12431 		dflt_rings = min_t(int, dflt_rings, max_rings);
12432 	}
12433 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
12434 	if (rc)
12435 		return rc;
12436 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
12437 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
12438 	if (sh)
12439 		bnxt_trim_dflt_sh_rings(bp);
12440 	else
12441 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
12442 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
12443 
12444 	rc = __bnxt_reserve_rings(bp);
12445 	if (rc)
12446 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
12447 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
12448 	if (sh)
12449 		bnxt_trim_dflt_sh_rings(bp);
12450 
12451 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
12452 	if (bnxt_need_reserve_rings(bp)) {
12453 		rc = __bnxt_reserve_rings(bp);
12454 		if (rc)
12455 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
12456 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
12457 	}
12458 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
12459 		bp->rx_nr_rings++;
12460 		bp->cp_nr_rings++;
12461 	}
12462 	if (rc) {
12463 		bp->tx_nr_rings = 0;
12464 		bp->rx_nr_rings = 0;
12465 	}
12466 	return rc;
12467 }
12468 
bnxt_init_dflt_ring_mode(struct bnxt * bp)12469 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
12470 {
12471 	int rc;
12472 
12473 	if (bp->tx_nr_rings)
12474 		return 0;
12475 
12476 	bnxt_ulp_irq_stop(bp);
12477 	bnxt_clear_int_mode(bp);
12478 	rc = bnxt_set_dflt_rings(bp, true);
12479 	if (rc) {
12480 		netdev_err(bp->dev, "Not enough rings available.\n");
12481 		goto init_dflt_ring_err;
12482 	}
12483 	rc = bnxt_init_int_mode(bp);
12484 	if (rc)
12485 		goto init_dflt_ring_err;
12486 
12487 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
12488 
12489 	bnxt_set_dflt_rfs(bp);
12490 
12491 init_dflt_ring_err:
12492 	bnxt_ulp_irq_restart(bp, rc);
12493 	return rc;
12494 }
12495 
bnxt_restore_pf_fw_resources(struct bnxt * bp)12496 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
12497 {
12498 	int rc;
12499 
12500 	ASSERT_RTNL();
12501 	bnxt_hwrm_func_qcaps(bp);
12502 
12503 	if (netif_running(bp->dev))
12504 		__bnxt_close_nic(bp, true, false);
12505 
12506 	bnxt_ulp_irq_stop(bp);
12507 	bnxt_clear_int_mode(bp);
12508 	rc = bnxt_init_int_mode(bp);
12509 	bnxt_ulp_irq_restart(bp, rc);
12510 
12511 	if (netif_running(bp->dev)) {
12512 		if (rc)
12513 			dev_close(bp->dev);
12514 		else
12515 			rc = bnxt_open_nic(bp, true, false);
12516 	}
12517 
12518 	return rc;
12519 }
12520 
bnxt_init_mac_addr(struct bnxt * bp)12521 static int bnxt_init_mac_addr(struct bnxt *bp)
12522 {
12523 	int rc = 0;
12524 
12525 	if (BNXT_PF(bp)) {
12526 		memcpy(bp->dev->dev_addr, bp->pf.mac_addr, ETH_ALEN);
12527 	} else {
12528 #ifdef CONFIG_BNXT_SRIOV
12529 		struct bnxt_vf_info *vf = &bp->vf;
12530 		bool strict_approval = true;
12531 
12532 		if (is_valid_ether_addr(vf->mac_addr)) {
12533 			/* overwrite netdev dev_addr with admin VF MAC */
12534 			memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
12535 			/* Older PF driver or firmware may not approve this
12536 			 * correctly.
12537 			 */
12538 			strict_approval = false;
12539 		} else {
12540 			eth_hw_addr_random(bp->dev);
12541 		}
12542 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
12543 #endif
12544 	}
12545 	return rc;
12546 }
12547 
12548 #define BNXT_VPD_LEN	512
bnxt_vpd_read_info(struct bnxt * bp)12549 static void bnxt_vpd_read_info(struct bnxt *bp)
12550 {
12551 	struct pci_dev *pdev = bp->pdev;
12552 	int i, len, pos, ro_size, size;
12553 	ssize_t vpd_size;
12554 	u8 *vpd_data;
12555 
12556 	vpd_data = kmalloc(BNXT_VPD_LEN, GFP_KERNEL);
12557 	if (!vpd_data)
12558 		return;
12559 
12560 	vpd_size = pci_read_vpd(pdev, 0, BNXT_VPD_LEN, vpd_data);
12561 	if (vpd_size <= 0) {
12562 		netdev_err(bp->dev, "Unable to read VPD\n");
12563 		goto exit;
12564 	}
12565 
12566 	i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
12567 	if (i < 0) {
12568 		netdev_err(bp->dev, "VPD READ-Only not found\n");
12569 		goto exit;
12570 	}
12571 
12572 	ro_size = pci_vpd_lrdt_size(&vpd_data[i]);
12573 	i += PCI_VPD_LRDT_TAG_SIZE;
12574 	if (i + ro_size > vpd_size)
12575 		goto exit;
12576 
12577 	pos = pci_vpd_find_info_keyword(vpd_data, i, ro_size,
12578 					PCI_VPD_RO_KEYWORD_PARTNO);
12579 	if (pos < 0)
12580 		goto read_sn;
12581 
12582 	len = pci_vpd_info_field_size(&vpd_data[pos]);
12583 	pos += PCI_VPD_INFO_FLD_HDR_SIZE;
12584 	if (len + pos > vpd_size)
12585 		goto read_sn;
12586 
12587 	size = min(len, BNXT_VPD_FLD_LEN - 1);
12588 	memcpy(bp->board_partno, &vpd_data[pos], size);
12589 
12590 read_sn:
12591 	pos = pci_vpd_find_info_keyword(vpd_data, i, ro_size,
12592 					PCI_VPD_RO_KEYWORD_SERIALNO);
12593 	if (pos < 0)
12594 		goto exit;
12595 
12596 	len = pci_vpd_info_field_size(&vpd_data[pos]);
12597 	pos += PCI_VPD_INFO_FLD_HDR_SIZE;
12598 	if (len + pos > vpd_size)
12599 		goto exit;
12600 
12601 	size = min(len, BNXT_VPD_FLD_LEN - 1);
12602 	memcpy(bp->board_serialno, &vpd_data[pos], size);
12603 exit:
12604 	kfree(vpd_data);
12605 }
12606 
bnxt_pcie_dsn_get(struct bnxt * bp,u8 dsn[])12607 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
12608 {
12609 	struct pci_dev *pdev = bp->pdev;
12610 	u64 qword;
12611 
12612 	qword = pci_get_dsn(pdev);
12613 	if (!qword) {
12614 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
12615 		return -EOPNOTSUPP;
12616 	}
12617 
12618 	put_unaligned_le64(qword, dsn);
12619 
12620 	bp->flags |= BNXT_FLAG_DSN_VALID;
12621 	return 0;
12622 }
12623 
bnxt_map_db_bar(struct bnxt * bp)12624 static int bnxt_map_db_bar(struct bnxt *bp)
12625 {
12626 	if (!bp->db_size)
12627 		return -ENODEV;
12628 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
12629 	if (!bp->bar1)
12630 		return -ENOMEM;
12631 	return 0;
12632 }
12633 
bnxt_init_one(struct pci_dev * pdev,const struct pci_device_id * ent)12634 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
12635 {
12636 	struct net_device *dev;
12637 	struct bnxt *bp;
12638 	int rc, max_irqs;
12639 
12640 	if (pci_is_bridge(pdev))
12641 		return -ENODEV;
12642 
12643 	/* Clear any pending DMA transactions from crash kernel
12644 	 * while loading driver in capture kernel.
12645 	 */
12646 	if (is_kdump_kernel()) {
12647 		pci_clear_master(pdev);
12648 		pcie_flr(pdev);
12649 	}
12650 
12651 	max_irqs = bnxt_get_max_irq(pdev);
12652 	dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
12653 	if (!dev)
12654 		return -ENOMEM;
12655 
12656 	bp = netdev_priv(dev);
12657 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
12658 	bnxt_set_max_func_irqs(bp, max_irqs);
12659 
12660 	if (bnxt_vf_pciid(ent->driver_data))
12661 		bp->flags |= BNXT_FLAG_VF;
12662 
12663 	if (pdev->msix_cap)
12664 		bp->flags |= BNXT_FLAG_MSIX_CAP;
12665 
12666 	rc = bnxt_init_board(pdev, dev);
12667 	if (rc < 0)
12668 		goto init_err_free;
12669 
12670 	dev->netdev_ops = &bnxt_netdev_ops;
12671 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
12672 	dev->ethtool_ops = &bnxt_ethtool_ops;
12673 	pci_set_drvdata(pdev, dev);
12674 
12675 	if (BNXT_PF(bp))
12676 		bnxt_vpd_read_info(bp);
12677 
12678 	rc = bnxt_alloc_hwrm_resources(bp);
12679 	if (rc)
12680 		goto init_err_pci_clean;
12681 
12682 	mutex_init(&bp->hwrm_cmd_lock);
12683 	mutex_init(&bp->link_lock);
12684 
12685 	rc = bnxt_fw_init_one_p1(bp);
12686 	if (rc)
12687 		goto init_err_pci_clean;
12688 
12689 	if (BNXT_CHIP_P5(bp)) {
12690 		bp->flags |= BNXT_FLAG_CHIP_P5;
12691 		if (BNXT_CHIP_SR2(bp))
12692 			bp->flags |= BNXT_FLAG_CHIP_SR2;
12693 	}
12694 
12695 	rc = bnxt_alloc_rss_indir_tbl(bp);
12696 	if (rc)
12697 		goto init_err_pci_clean;
12698 
12699 	rc = bnxt_fw_init_one_p2(bp);
12700 	if (rc)
12701 		goto init_err_pci_clean;
12702 
12703 	rc = bnxt_map_db_bar(bp);
12704 	if (rc) {
12705 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
12706 			rc);
12707 		goto init_err_pci_clean;
12708 	}
12709 
12710 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
12711 			   NETIF_F_TSO | NETIF_F_TSO6 |
12712 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
12713 			   NETIF_F_GSO_IPXIP4 |
12714 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
12715 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
12716 			   NETIF_F_RXCSUM | NETIF_F_GRO;
12717 
12718 	if (BNXT_SUPPORTS_TPA(bp))
12719 		dev->hw_features |= NETIF_F_LRO;
12720 
12721 	dev->hw_enc_features =
12722 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
12723 			NETIF_F_TSO | NETIF_F_TSO6 |
12724 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
12725 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
12726 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
12727 	dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
12728 
12729 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
12730 				    NETIF_F_GSO_GRE_CSUM;
12731 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
12732 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
12733 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
12734 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
12735 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
12736 	if (BNXT_SUPPORTS_TPA(bp))
12737 		dev->hw_features |= NETIF_F_GRO_HW;
12738 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
12739 	if (dev->features & NETIF_F_GRO_HW)
12740 		dev->features &= ~NETIF_F_LRO;
12741 	dev->priv_flags |= IFF_UNICAST_FLT;
12742 
12743 #ifdef CONFIG_BNXT_SRIOV
12744 	init_waitqueue_head(&bp->sriov_cfg_wait);
12745 	mutex_init(&bp->sriov_lock);
12746 #endif
12747 	if (BNXT_SUPPORTS_TPA(bp)) {
12748 		bp->gro_func = bnxt_gro_func_5730x;
12749 		if (BNXT_CHIP_P4(bp))
12750 			bp->gro_func = bnxt_gro_func_5731x;
12751 		else if (BNXT_CHIP_P5(bp))
12752 			bp->gro_func = bnxt_gro_func_5750x;
12753 	}
12754 	if (!BNXT_CHIP_P4_PLUS(bp))
12755 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
12756 
12757 	bp->ulp_probe = bnxt_ulp_probe;
12758 
12759 	rc = bnxt_init_mac_addr(bp);
12760 	if (rc) {
12761 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
12762 		rc = -EADDRNOTAVAIL;
12763 		goto init_err_pci_clean;
12764 	}
12765 
12766 	if (BNXT_PF(bp)) {
12767 		/* Read the adapter's DSN to use as the eswitch switch_id */
12768 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
12769 	}
12770 
12771 	/* MTU range: 60 - FW defined max */
12772 	dev->min_mtu = ETH_ZLEN;
12773 	dev->max_mtu = bp->max_mtu;
12774 
12775 	rc = bnxt_probe_phy(bp, true);
12776 	if (rc)
12777 		goto init_err_pci_clean;
12778 
12779 	bnxt_set_rx_skb_mode(bp, false);
12780 	bnxt_set_tpa_flags(bp);
12781 	bnxt_set_ring_params(bp);
12782 	rc = bnxt_set_dflt_rings(bp, true);
12783 	if (rc) {
12784 		netdev_err(bp->dev, "Not enough rings available.\n");
12785 		rc = -ENOMEM;
12786 		goto init_err_pci_clean;
12787 	}
12788 
12789 	bnxt_fw_init_one_p3(bp);
12790 
12791 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12792 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
12793 
12794 	rc = bnxt_init_int_mode(bp);
12795 	if (rc)
12796 		goto init_err_pci_clean;
12797 
12798 	/* No TC has been set yet and rings may have been trimmed due to
12799 	 * limited MSIX, so we re-initialize the TX rings per TC.
12800 	 */
12801 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
12802 
12803 	if (BNXT_PF(bp)) {
12804 		if (!bnxt_pf_wq) {
12805 			bnxt_pf_wq =
12806 				create_singlethread_workqueue("bnxt_pf_wq");
12807 			if (!bnxt_pf_wq) {
12808 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
12809 				rc = -ENOMEM;
12810 				goto init_err_pci_clean;
12811 			}
12812 		}
12813 		rc = bnxt_init_tc(bp);
12814 		if (rc)
12815 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
12816 				   rc);
12817 	}
12818 
12819 	bnxt_dl_register(bp);
12820 
12821 	rc = register_netdev(dev);
12822 	if (rc)
12823 		goto init_err_cleanup;
12824 
12825 	if (BNXT_PF(bp))
12826 		devlink_port_type_eth_set(&bp->dl_port, bp->dev);
12827 	bnxt_dl_fw_reporters_create(bp);
12828 
12829 	netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
12830 		    board_info[ent->driver_data].name,
12831 		    (long)pci_resource_start(pdev, 0), dev->dev_addr);
12832 	pcie_print_link_status(pdev);
12833 
12834 	pci_save_state(pdev);
12835 	return 0;
12836 
12837 init_err_cleanup:
12838 	bnxt_dl_unregister(bp);
12839 	bnxt_shutdown_tc(bp);
12840 	bnxt_clear_int_mode(bp);
12841 
12842 init_err_pci_clean:
12843 	bnxt_hwrm_func_drv_unrgtr(bp);
12844 	bnxt_free_hwrm_short_cmd_req(bp);
12845 	bnxt_free_hwrm_resources(bp);
12846 	bnxt_ethtool_free(bp);
12847 	kfree(bp->fw_health);
12848 	bp->fw_health = NULL;
12849 	bnxt_cleanup_pci(bp);
12850 	bnxt_free_ctx_mem(bp);
12851 	kfree(bp->ctx);
12852 	bp->ctx = NULL;
12853 	kfree(bp->rss_indir_tbl);
12854 	bp->rss_indir_tbl = NULL;
12855 
12856 init_err_free:
12857 	free_netdev(dev);
12858 	return rc;
12859 }
12860 
bnxt_shutdown(struct pci_dev * pdev)12861 static void bnxt_shutdown(struct pci_dev *pdev)
12862 {
12863 	struct net_device *dev = pci_get_drvdata(pdev);
12864 	struct bnxt *bp;
12865 
12866 	if (!dev)
12867 		return;
12868 
12869 	rtnl_lock();
12870 	bp = netdev_priv(dev);
12871 	if (!bp)
12872 		goto shutdown_exit;
12873 
12874 	if (netif_running(dev))
12875 		dev_close(dev);
12876 
12877 	bnxt_ulp_shutdown(bp);
12878 	bnxt_clear_int_mode(bp);
12879 	pci_disable_device(pdev);
12880 
12881 	if (system_state == SYSTEM_POWER_OFF) {
12882 		pci_wake_from_d3(pdev, bp->wol);
12883 		pci_set_power_state(pdev, PCI_D3hot);
12884 	}
12885 
12886 shutdown_exit:
12887 	rtnl_unlock();
12888 }
12889 
12890 #ifdef CONFIG_PM_SLEEP
bnxt_suspend(struct device * device)12891 static int bnxt_suspend(struct device *device)
12892 {
12893 	struct net_device *dev = dev_get_drvdata(device);
12894 	struct bnxt *bp = netdev_priv(dev);
12895 	int rc = 0;
12896 
12897 	rtnl_lock();
12898 	bnxt_ulp_stop(bp);
12899 	if (netif_running(dev)) {
12900 		netif_device_detach(dev);
12901 		rc = bnxt_close(dev);
12902 	}
12903 	bnxt_hwrm_func_drv_unrgtr(bp);
12904 	pci_disable_device(bp->pdev);
12905 	bnxt_free_ctx_mem(bp);
12906 	kfree(bp->ctx);
12907 	bp->ctx = NULL;
12908 	rtnl_unlock();
12909 	return rc;
12910 }
12911 
bnxt_resume(struct device * device)12912 static int bnxt_resume(struct device *device)
12913 {
12914 	struct net_device *dev = dev_get_drvdata(device);
12915 	struct bnxt *bp = netdev_priv(dev);
12916 	int rc = 0;
12917 
12918 	rtnl_lock();
12919 	rc = pci_enable_device(bp->pdev);
12920 	if (rc) {
12921 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
12922 			   rc);
12923 		goto resume_exit;
12924 	}
12925 	pci_set_master(bp->pdev);
12926 	if (bnxt_hwrm_ver_get(bp)) {
12927 		rc = -ENODEV;
12928 		goto resume_exit;
12929 	}
12930 	rc = bnxt_hwrm_func_reset(bp);
12931 	if (rc) {
12932 		rc = -EBUSY;
12933 		goto resume_exit;
12934 	}
12935 
12936 	rc = bnxt_hwrm_func_qcaps(bp);
12937 	if (rc)
12938 		goto resume_exit;
12939 
12940 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
12941 		rc = -ENODEV;
12942 		goto resume_exit;
12943 	}
12944 
12945 	bnxt_get_wol_settings(bp);
12946 	if (netif_running(dev)) {
12947 		rc = bnxt_open(dev);
12948 		if (!rc)
12949 			netif_device_attach(dev);
12950 	}
12951 
12952 resume_exit:
12953 	bnxt_ulp_start(bp, rc);
12954 	if (!rc)
12955 		bnxt_reenable_sriov(bp);
12956 	rtnl_unlock();
12957 	return rc;
12958 }
12959 
12960 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
12961 #define BNXT_PM_OPS (&bnxt_pm_ops)
12962 
12963 #else
12964 
12965 #define BNXT_PM_OPS NULL
12966 
12967 #endif /* CONFIG_PM_SLEEP */
12968 
12969 /**
12970  * bnxt_io_error_detected - called when PCI error is detected
12971  * @pdev: Pointer to PCI device
12972  * @state: The current pci connection state
12973  *
12974  * This function is called after a PCI bus error affecting
12975  * this device has been detected.
12976  */
bnxt_io_error_detected(struct pci_dev * pdev,pci_channel_state_t state)12977 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
12978 					       pci_channel_state_t state)
12979 {
12980 	struct net_device *netdev = pci_get_drvdata(pdev);
12981 	struct bnxt *bp = netdev_priv(netdev);
12982 
12983 	netdev_info(netdev, "PCI I/O error detected\n");
12984 
12985 	rtnl_lock();
12986 	netif_device_detach(netdev);
12987 
12988 	bnxt_ulp_stop(bp);
12989 
12990 	if (state == pci_channel_io_perm_failure) {
12991 		rtnl_unlock();
12992 		return PCI_ERS_RESULT_DISCONNECT;
12993 	}
12994 
12995 	if (state == pci_channel_io_frozen)
12996 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
12997 
12998 	if (netif_running(netdev))
12999 		bnxt_close(netdev);
13000 
13001 	if (pci_is_enabled(pdev))
13002 		pci_disable_device(pdev);
13003 	bnxt_free_ctx_mem(bp);
13004 	kfree(bp->ctx);
13005 	bp->ctx = NULL;
13006 	rtnl_unlock();
13007 
13008 	/* Request a slot slot reset. */
13009 	return PCI_ERS_RESULT_NEED_RESET;
13010 }
13011 
13012 /**
13013  * bnxt_io_slot_reset - called after the pci bus has been reset.
13014  * @pdev: Pointer to PCI device
13015  *
13016  * Restart the card from scratch, as if from a cold-boot.
13017  * At this point, the card has exprienced a hard reset,
13018  * followed by fixups by BIOS, and has its config space
13019  * set up identically to what it was at cold boot.
13020  */
bnxt_io_slot_reset(struct pci_dev * pdev)13021 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
13022 {
13023 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
13024 	struct net_device *netdev = pci_get_drvdata(pdev);
13025 	struct bnxt *bp = netdev_priv(netdev);
13026 	int err = 0, off;
13027 
13028 	netdev_info(bp->dev, "PCI Slot Reset\n");
13029 
13030 	rtnl_lock();
13031 
13032 	if (pci_enable_device(pdev)) {
13033 		dev_err(&pdev->dev,
13034 			"Cannot re-enable PCI device after reset.\n");
13035 	} else {
13036 		pci_set_master(pdev);
13037 		/* Upon fatal error, our device internal logic that latches to
13038 		 * BAR value is getting reset and will restore only upon
13039 		 * rewritting the BARs.
13040 		 *
13041 		 * As pci_restore_state() does not re-write the BARs if the
13042 		 * value is same as saved value earlier, driver needs to
13043 		 * write the BARs to 0 to force restore, in case of fatal error.
13044 		 */
13045 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
13046 				       &bp->state)) {
13047 			for (off = PCI_BASE_ADDRESS_0;
13048 			     off <= PCI_BASE_ADDRESS_5; off += 4)
13049 				pci_write_config_dword(bp->pdev, off, 0);
13050 		}
13051 		pci_restore_state(pdev);
13052 		pci_save_state(pdev);
13053 
13054 		err = bnxt_hwrm_func_reset(bp);
13055 		if (!err)
13056 			result = PCI_ERS_RESULT_RECOVERED;
13057 	}
13058 
13059 	rtnl_unlock();
13060 
13061 	return result;
13062 }
13063 
13064 /**
13065  * bnxt_io_resume - called when traffic can start flowing again.
13066  * @pdev: Pointer to PCI device
13067  *
13068  * This callback is called when the error recovery driver tells
13069  * us that its OK to resume normal operation.
13070  */
bnxt_io_resume(struct pci_dev * pdev)13071 static void bnxt_io_resume(struct pci_dev *pdev)
13072 {
13073 	struct net_device *netdev = pci_get_drvdata(pdev);
13074 	struct bnxt *bp = netdev_priv(netdev);
13075 	int err;
13076 
13077 	netdev_info(bp->dev, "PCI Slot Resume\n");
13078 	rtnl_lock();
13079 
13080 	err = bnxt_hwrm_func_qcaps(bp);
13081 	if (!err && netif_running(netdev))
13082 		err = bnxt_open(netdev);
13083 
13084 	bnxt_ulp_start(bp, err);
13085 	if (!err) {
13086 		bnxt_reenable_sriov(bp);
13087 		netif_device_attach(netdev);
13088 	}
13089 
13090 	rtnl_unlock();
13091 }
13092 
13093 static const struct pci_error_handlers bnxt_err_handler = {
13094 	.error_detected	= bnxt_io_error_detected,
13095 	.slot_reset	= bnxt_io_slot_reset,
13096 	.resume		= bnxt_io_resume
13097 };
13098 
13099 static struct pci_driver bnxt_pci_driver = {
13100 	.name		= DRV_MODULE_NAME,
13101 	.id_table	= bnxt_pci_tbl,
13102 	.probe		= bnxt_init_one,
13103 	.remove		= bnxt_remove_one,
13104 	.shutdown	= bnxt_shutdown,
13105 	.driver.pm	= BNXT_PM_OPS,
13106 	.err_handler	= &bnxt_err_handler,
13107 #if defined(CONFIG_BNXT_SRIOV)
13108 	.sriov_configure = bnxt_sriov_configure,
13109 #endif
13110 };
13111 
bnxt_init(void)13112 static int __init bnxt_init(void)
13113 {
13114 	int err;
13115 
13116 	bnxt_debug_init();
13117 	err = pci_register_driver(&bnxt_pci_driver);
13118 	if (err) {
13119 		bnxt_debug_exit();
13120 		return err;
13121 	}
13122 
13123 	return 0;
13124 }
13125 
bnxt_exit(void)13126 static void __exit bnxt_exit(void)
13127 {
13128 	pci_unregister_driver(&bnxt_pci_driver);
13129 	if (bnxt_pf_wq)
13130 		destroy_workqueue(bnxt_pf_wq);
13131 	bnxt_debug_exit();
13132 }
13133 
13134 module_init(bnxt_init);
13135 module_exit(bnxt_exit);
13136