• 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 = BCM57502_NPAR },
223 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
224 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
225 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
226 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
227 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_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, j;
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 			for (j = 0; j < bp->max_tpa; j++) {
2866 				kfree(rxr->rx_tpa[j].agg_arr);
2867 				rxr->rx_tpa[j].agg_arr = NULL;
2868 			}
2869 		}
2870 		kfree(rxr->rx_tpa);
2871 		rxr->rx_tpa = NULL;
2872 	}
2873 }
2874 
bnxt_alloc_tpa_info(struct bnxt * bp)2875 static int bnxt_alloc_tpa_info(struct bnxt *bp)
2876 {
2877 	int i, j;
2878 
2879 	bp->max_tpa = MAX_TPA;
2880 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
2881 		if (!bp->max_tpa_v2)
2882 			return 0;
2883 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
2884 	}
2885 
2886 	for (i = 0; i < bp->rx_nr_rings; i++) {
2887 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2888 		struct rx_agg_cmp *agg;
2889 
2890 		rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
2891 				      GFP_KERNEL);
2892 		if (!rxr->rx_tpa)
2893 			return -ENOMEM;
2894 
2895 		if (!(bp->flags & BNXT_FLAG_CHIP_P5))
2896 			continue;
2897 		for (j = 0; j < bp->max_tpa; j++) {
2898 			agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
2899 			if (!agg)
2900 				return -ENOMEM;
2901 			rxr->rx_tpa[j].agg_arr = agg;
2902 		}
2903 		rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
2904 					      GFP_KERNEL);
2905 		if (!rxr->rx_tpa_idx_map)
2906 			return -ENOMEM;
2907 	}
2908 	return 0;
2909 }
2910 
bnxt_free_rx_rings(struct bnxt * bp)2911 static void bnxt_free_rx_rings(struct bnxt *bp)
2912 {
2913 	int i;
2914 
2915 	if (!bp->rx_ring)
2916 		return;
2917 
2918 	bnxt_free_tpa_info(bp);
2919 	for (i = 0; i < bp->rx_nr_rings; i++) {
2920 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2921 		struct bnxt_ring_struct *ring;
2922 
2923 		if (rxr->xdp_prog)
2924 			bpf_prog_put(rxr->xdp_prog);
2925 
2926 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
2927 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
2928 
2929 		page_pool_destroy(rxr->page_pool);
2930 		rxr->page_pool = NULL;
2931 
2932 		kfree(rxr->rx_agg_bmap);
2933 		rxr->rx_agg_bmap = NULL;
2934 
2935 		ring = &rxr->rx_ring_struct;
2936 		bnxt_free_ring(bp, &ring->ring_mem);
2937 
2938 		ring = &rxr->rx_agg_ring_struct;
2939 		bnxt_free_ring(bp, &ring->ring_mem);
2940 	}
2941 }
2942 
bnxt_alloc_rx_page_pool(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)2943 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
2944 				   struct bnxt_rx_ring_info *rxr)
2945 {
2946 	struct page_pool_params pp = { 0 };
2947 
2948 	pp.pool_size = bp->rx_ring_size;
2949 	pp.nid = dev_to_node(&bp->pdev->dev);
2950 	pp.dev = &bp->pdev->dev;
2951 	pp.dma_dir = DMA_BIDIRECTIONAL;
2952 
2953 	rxr->page_pool = page_pool_create(&pp);
2954 	if (IS_ERR(rxr->page_pool)) {
2955 		int err = PTR_ERR(rxr->page_pool);
2956 
2957 		rxr->page_pool = NULL;
2958 		return err;
2959 	}
2960 	return 0;
2961 }
2962 
bnxt_alloc_rx_rings(struct bnxt * bp)2963 static int bnxt_alloc_rx_rings(struct bnxt *bp)
2964 {
2965 	int i, rc = 0, agg_rings = 0;
2966 
2967 	if (!bp->rx_ring)
2968 		return -ENOMEM;
2969 
2970 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
2971 		agg_rings = 1;
2972 
2973 	for (i = 0; i < bp->rx_nr_rings; i++) {
2974 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2975 		struct bnxt_ring_struct *ring;
2976 
2977 		ring = &rxr->rx_ring_struct;
2978 
2979 		rc = bnxt_alloc_rx_page_pool(bp, rxr);
2980 		if (rc)
2981 			return rc;
2982 
2983 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i);
2984 		if (rc < 0)
2985 			return rc;
2986 
2987 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
2988 						MEM_TYPE_PAGE_POOL,
2989 						rxr->page_pool);
2990 		if (rc) {
2991 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
2992 			return rc;
2993 		}
2994 
2995 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
2996 		if (rc)
2997 			return rc;
2998 
2999 		ring->grp_idx = i;
3000 		if (agg_rings) {
3001 			u16 mem_size;
3002 
3003 			ring = &rxr->rx_agg_ring_struct;
3004 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3005 			if (rc)
3006 				return rc;
3007 
3008 			ring->grp_idx = i;
3009 			rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3010 			mem_size = rxr->rx_agg_bmap_size / 8;
3011 			rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3012 			if (!rxr->rx_agg_bmap)
3013 				return -ENOMEM;
3014 		}
3015 	}
3016 	if (bp->flags & BNXT_FLAG_TPA)
3017 		rc = bnxt_alloc_tpa_info(bp);
3018 	return rc;
3019 }
3020 
bnxt_free_tx_rings(struct bnxt * bp)3021 static void bnxt_free_tx_rings(struct bnxt *bp)
3022 {
3023 	int i;
3024 	struct pci_dev *pdev = bp->pdev;
3025 
3026 	if (!bp->tx_ring)
3027 		return;
3028 
3029 	for (i = 0; i < bp->tx_nr_rings; i++) {
3030 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3031 		struct bnxt_ring_struct *ring;
3032 
3033 		if (txr->tx_push) {
3034 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3035 					  txr->tx_push, txr->tx_push_mapping);
3036 			txr->tx_push = NULL;
3037 		}
3038 
3039 		ring = &txr->tx_ring_struct;
3040 
3041 		bnxt_free_ring(bp, &ring->ring_mem);
3042 	}
3043 }
3044 
bnxt_alloc_tx_rings(struct bnxt * bp)3045 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3046 {
3047 	int i, j, rc;
3048 	struct pci_dev *pdev = bp->pdev;
3049 
3050 	bp->tx_push_size = 0;
3051 	if (bp->tx_push_thresh) {
3052 		int push_size;
3053 
3054 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3055 					bp->tx_push_thresh);
3056 
3057 		if (push_size > 256) {
3058 			push_size = 0;
3059 			bp->tx_push_thresh = 0;
3060 		}
3061 
3062 		bp->tx_push_size = push_size;
3063 	}
3064 
3065 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3066 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3067 		struct bnxt_ring_struct *ring;
3068 		u8 qidx;
3069 
3070 		ring = &txr->tx_ring_struct;
3071 
3072 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3073 		if (rc)
3074 			return rc;
3075 
3076 		ring->grp_idx = txr->bnapi->index;
3077 		if (bp->tx_push_size) {
3078 			dma_addr_t mapping;
3079 
3080 			/* One pre-allocated DMA buffer to backup
3081 			 * TX push operation
3082 			 */
3083 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3084 						bp->tx_push_size,
3085 						&txr->tx_push_mapping,
3086 						GFP_KERNEL);
3087 
3088 			if (!txr->tx_push)
3089 				return -ENOMEM;
3090 
3091 			mapping = txr->tx_push_mapping +
3092 				sizeof(struct tx_push_bd);
3093 			txr->data_mapping = cpu_to_le64(mapping);
3094 		}
3095 		qidx = bp->tc_to_qidx[j];
3096 		ring->queue_id = bp->q_info[qidx].queue_id;
3097 		if (i < bp->tx_nr_rings_xdp)
3098 			continue;
3099 		if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
3100 			j++;
3101 	}
3102 	return 0;
3103 }
3104 
bnxt_free_cp_rings(struct bnxt * bp)3105 static void bnxt_free_cp_rings(struct bnxt *bp)
3106 {
3107 	int i;
3108 
3109 	if (!bp->bnapi)
3110 		return;
3111 
3112 	for (i = 0; i < bp->cp_nr_rings; i++) {
3113 		struct bnxt_napi *bnapi = bp->bnapi[i];
3114 		struct bnxt_cp_ring_info *cpr;
3115 		struct bnxt_ring_struct *ring;
3116 		int j;
3117 
3118 		if (!bnapi)
3119 			continue;
3120 
3121 		cpr = &bnapi->cp_ring;
3122 		ring = &cpr->cp_ring_struct;
3123 
3124 		bnxt_free_ring(bp, &ring->ring_mem);
3125 
3126 		for (j = 0; j < 2; j++) {
3127 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3128 
3129 			if (cpr2) {
3130 				ring = &cpr2->cp_ring_struct;
3131 				bnxt_free_ring(bp, &ring->ring_mem);
3132 				kfree(cpr2);
3133 				cpr->cp_ring_arr[j] = NULL;
3134 			}
3135 		}
3136 	}
3137 }
3138 
bnxt_alloc_cp_sub_ring(struct bnxt * bp)3139 static struct bnxt_cp_ring_info *bnxt_alloc_cp_sub_ring(struct bnxt *bp)
3140 {
3141 	struct bnxt_ring_mem_info *rmem;
3142 	struct bnxt_ring_struct *ring;
3143 	struct bnxt_cp_ring_info *cpr;
3144 	int rc;
3145 
3146 	cpr = kzalloc(sizeof(*cpr), GFP_KERNEL);
3147 	if (!cpr)
3148 		return NULL;
3149 
3150 	ring = &cpr->cp_ring_struct;
3151 	rmem = &ring->ring_mem;
3152 	rmem->nr_pages = bp->cp_nr_pages;
3153 	rmem->page_size = HW_CMPD_RING_SIZE;
3154 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
3155 	rmem->dma_arr = cpr->cp_desc_mapping;
3156 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
3157 	rc = bnxt_alloc_ring(bp, rmem);
3158 	if (rc) {
3159 		bnxt_free_ring(bp, rmem);
3160 		kfree(cpr);
3161 		cpr = NULL;
3162 	}
3163 	return cpr;
3164 }
3165 
bnxt_alloc_cp_rings(struct bnxt * bp)3166 static int bnxt_alloc_cp_rings(struct bnxt *bp)
3167 {
3168 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
3169 	int i, rc, ulp_base_vec, ulp_msix;
3170 
3171 	ulp_msix = bnxt_get_ulp_msix_num(bp);
3172 	ulp_base_vec = bnxt_get_ulp_msix_base(bp);
3173 	for (i = 0; i < bp->cp_nr_rings; i++) {
3174 		struct bnxt_napi *bnapi = bp->bnapi[i];
3175 		struct bnxt_cp_ring_info *cpr;
3176 		struct bnxt_ring_struct *ring;
3177 
3178 		if (!bnapi)
3179 			continue;
3180 
3181 		cpr = &bnapi->cp_ring;
3182 		cpr->bnapi = bnapi;
3183 		ring = &cpr->cp_ring_struct;
3184 
3185 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3186 		if (rc)
3187 			return rc;
3188 
3189 		if (ulp_msix && i >= ulp_base_vec)
3190 			ring->map_idx = i + ulp_msix;
3191 		else
3192 			ring->map_idx = i;
3193 
3194 		if (!(bp->flags & BNXT_FLAG_CHIP_P5))
3195 			continue;
3196 
3197 		if (i < bp->rx_nr_rings) {
3198 			struct bnxt_cp_ring_info *cpr2 =
3199 				bnxt_alloc_cp_sub_ring(bp);
3200 
3201 			cpr->cp_ring_arr[BNXT_RX_HDL] = cpr2;
3202 			if (!cpr2)
3203 				return -ENOMEM;
3204 			cpr2->bnapi = bnapi;
3205 		}
3206 		if ((sh && i < bp->tx_nr_rings) ||
3207 		    (!sh && i >= bp->rx_nr_rings)) {
3208 			struct bnxt_cp_ring_info *cpr2 =
3209 				bnxt_alloc_cp_sub_ring(bp);
3210 
3211 			cpr->cp_ring_arr[BNXT_TX_HDL] = cpr2;
3212 			if (!cpr2)
3213 				return -ENOMEM;
3214 			cpr2->bnapi = bnapi;
3215 		}
3216 	}
3217 	return 0;
3218 }
3219 
bnxt_init_ring_struct(struct bnxt * bp)3220 static void bnxt_init_ring_struct(struct bnxt *bp)
3221 {
3222 	int i;
3223 
3224 	for (i = 0; i < bp->cp_nr_rings; i++) {
3225 		struct bnxt_napi *bnapi = bp->bnapi[i];
3226 		struct bnxt_ring_mem_info *rmem;
3227 		struct bnxt_cp_ring_info *cpr;
3228 		struct bnxt_rx_ring_info *rxr;
3229 		struct bnxt_tx_ring_info *txr;
3230 		struct bnxt_ring_struct *ring;
3231 
3232 		if (!bnapi)
3233 			continue;
3234 
3235 		cpr = &bnapi->cp_ring;
3236 		ring = &cpr->cp_ring_struct;
3237 		rmem = &ring->ring_mem;
3238 		rmem->nr_pages = bp->cp_nr_pages;
3239 		rmem->page_size = HW_CMPD_RING_SIZE;
3240 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
3241 		rmem->dma_arr = cpr->cp_desc_mapping;
3242 		rmem->vmem_size = 0;
3243 
3244 		rxr = bnapi->rx_ring;
3245 		if (!rxr)
3246 			goto skip_rx;
3247 
3248 		ring = &rxr->rx_ring_struct;
3249 		rmem = &ring->ring_mem;
3250 		rmem->nr_pages = bp->rx_nr_pages;
3251 		rmem->page_size = HW_RXBD_RING_SIZE;
3252 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
3253 		rmem->dma_arr = rxr->rx_desc_mapping;
3254 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
3255 		rmem->vmem = (void **)&rxr->rx_buf_ring;
3256 
3257 		ring = &rxr->rx_agg_ring_struct;
3258 		rmem = &ring->ring_mem;
3259 		rmem->nr_pages = bp->rx_agg_nr_pages;
3260 		rmem->page_size = HW_RXBD_RING_SIZE;
3261 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
3262 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
3263 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
3264 		rmem->vmem = (void **)&rxr->rx_agg_ring;
3265 
3266 skip_rx:
3267 		txr = bnapi->tx_ring;
3268 		if (!txr)
3269 			continue;
3270 
3271 		ring = &txr->tx_ring_struct;
3272 		rmem = &ring->ring_mem;
3273 		rmem->nr_pages = bp->tx_nr_pages;
3274 		rmem->page_size = HW_RXBD_RING_SIZE;
3275 		rmem->pg_arr = (void **)txr->tx_desc_ring;
3276 		rmem->dma_arr = txr->tx_desc_mapping;
3277 		rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
3278 		rmem->vmem = (void **)&txr->tx_buf_ring;
3279 	}
3280 }
3281 
bnxt_init_rxbd_pages(struct bnxt_ring_struct * ring,u32 type)3282 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
3283 {
3284 	int i;
3285 	u32 prod;
3286 	struct rx_bd **rx_buf_ring;
3287 
3288 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
3289 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
3290 		int j;
3291 		struct rx_bd *rxbd;
3292 
3293 		rxbd = rx_buf_ring[i];
3294 		if (!rxbd)
3295 			continue;
3296 
3297 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
3298 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
3299 			rxbd->rx_bd_opaque = prod;
3300 		}
3301 	}
3302 }
3303 
bnxt_alloc_one_rx_ring(struct bnxt * bp,int ring_nr)3304 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
3305 {
3306 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
3307 	struct net_device *dev = bp->dev;
3308 	u32 prod;
3309 	int i;
3310 
3311 	prod = rxr->rx_prod;
3312 	for (i = 0; i < bp->rx_ring_size; i++) {
3313 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
3314 			netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
3315 				    ring_nr, i, bp->rx_ring_size);
3316 			break;
3317 		}
3318 		prod = NEXT_RX(prod);
3319 	}
3320 	rxr->rx_prod = prod;
3321 
3322 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
3323 		return 0;
3324 
3325 	prod = rxr->rx_agg_prod;
3326 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
3327 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
3328 			netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
3329 				    ring_nr, i, bp->rx_ring_size);
3330 			break;
3331 		}
3332 		prod = NEXT_RX_AGG(prod);
3333 	}
3334 	rxr->rx_agg_prod = prod;
3335 
3336 	if (rxr->rx_tpa) {
3337 		dma_addr_t mapping;
3338 		u8 *data;
3339 
3340 		for (i = 0; i < bp->max_tpa; i++) {
3341 			data = __bnxt_alloc_rx_data(bp, &mapping, GFP_KERNEL);
3342 			if (!data)
3343 				return -ENOMEM;
3344 
3345 			rxr->rx_tpa[i].data = data;
3346 			rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
3347 			rxr->rx_tpa[i].mapping = mapping;
3348 		}
3349 	}
3350 	return 0;
3351 }
3352 
bnxt_init_one_rx_ring(struct bnxt * bp,int ring_nr)3353 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
3354 {
3355 	struct bnxt_rx_ring_info *rxr;
3356 	struct bnxt_ring_struct *ring;
3357 	u32 type;
3358 
3359 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
3360 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
3361 
3362 	if (NET_IP_ALIGN == 2)
3363 		type |= RX_BD_FLAGS_SOP;
3364 
3365 	rxr = &bp->rx_ring[ring_nr];
3366 	ring = &rxr->rx_ring_struct;
3367 	bnxt_init_rxbd_pages(ring, type);
3368 
3369 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
3370 		bpf_prog_add(bp->xdp_prog, 1);
3371 		rxr->xdp_prog = bp->xdp_prog;
3372 	}
3373 	ring->fw_ring_id = INVALID_HW_RING_ID;
3374 
3375 	ring = &rxr->rx_agg_ring_struct;
3376 	ring->fw_ring_id = INVALID_HW_RING_ID;
3377 
3378 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
3379 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
3380 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
3381 
3382 		bnxt_init_rxbd_pages(ring, type);
3383 	}
3384 
3385 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
3386 }
3387 
bnxt_init_cp_rings(struct bnxt * bp)3388 static void bnxt_init_cp_rings(struct bnxt *bp)
3389 {
3390 	int i, j;
3391 
3392 	for (i = 0; i < bp->cp_nr_rings; i++) {
3393 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
3394 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3395 
3396 		ring->fw_ring_id = INVALID_HW_RING_ID;
3397 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3398 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3399 		for (j = 0; j < 2; j++) {
3400 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3401 
3402 			if (!cpr2)
3403 				continue;
3404 
3405 			ring = &cpr2->cp_ring_struct;
3406 			ring->fw_ring_id = INVALID_HW_RING_ID;
3407 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3408 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3409 		}
3410 	}
3411 }
3412 
bnxt_init_rx_rings(struct bnxt * bp)3413 static int bnxt_init_rx_rings(struct bnxt *bp)
3414 {
3415 	int i, rc = 0;
3416 
3417 	if (BNXT_RX_PAGE_MODE(bp)) {
3418 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
3419 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
3420 	} else {
3421 		bp->rx_offset = BNXT_RX_OFFSET;
3422 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
3423 	}
3424 
3425 	for (i = 0; i < bp->rx_nr_rings; i++) {
3426 		rc = bnxt_init_one_rx_ring(bp, i);
3427 		if (rc)
3428 			break;
3429 	}
3430 
3431 	return rc;
3432 }
3433 
bnxt_init_tx_rings(struct bnxt * bp)3434 static int bnxt_init_tx_rings(struct bnxt *bp)
3435 {
3436 	u16 i;
3437 
3438 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
3439 				   BNXT_MIN_TX_DESC_CNT);
3440 
3441 	for (i = 0; i < bp->tx_nr_rings; i++) {
3442 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3443 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
3444 
3445 		ring->fw_ring_id = INVALID_HW_RING_ID;
3446 	}
3447 
3448 	return 0;
3449 }
3450 
bnxt_free_ring_grps(struct bnxt * bp)3451 static void bnxt_free_ring_grps(struct bnxt *bp)
3452 {
3453 	kfree(bp->grp_info);
3454 	bp->grp_info = NULL;
3455 }
3456 
bnxt_init_ring_grps(struct bnxt * bp,bool irq_re_init)3457 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
3458 {
3459 	int i;
3460 
3461 	if (irq_re_init) {
3462 		bp->grp_info = kcalloc(bp->cp_nr_rings,
3463 				       sizeof(struct bnxt_ring_grp_info),
3464 				       GFP_KERNEL);
3465 		if (!bp->grp_info)
3466 			return -ENOMEM;
3467 	}
3468 	for (i = 0; i < bp->cp_nr_rings; i++) {
3469 		if (irq_re_init)
3470 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
3471 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3472 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
3473 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
3474 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
3475 	}
3476 	return 0;
3477 }
3478 
bnxt_free_vnics(struct bnxt * bp)3479 static void bnxt_free_vnics(struct bnxt *bp)
3480 {
3481 	kfree(bp->vnic_info);
3482 	bp->vnic_info = NULL;
3483 	bp->nr_vnics = 0;
3484 }
3485 
bnxt_alloc_vnics(struct bnxt * bp)3486 static int bnxt_alloc_vnics(struct bnxt *bp)
3487 {
3488 	int num_vnics = 1;
3489 
3490 #ifdef CONFIG_RFS_ACCEL
3491 	if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
3492 		num_vnics += bp->rx_nr_rings;
3493 #endif
3494 
3495 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3496 		num_vnics++;
3497 
3498 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
3499 				GFP_KERNEL);
3500 	if (!bp->vnic_info)
3501 		return -ENOMEM;
3502 
3503 	bp->nr_vnics = num_vnics;
3504 	return 0;
3505 }
3506 
bnxt_init_vnics(struct bnxt * bp)3507 static void bnxt_init_vnics(struct bnxt *bp)
3508 {
3509 	int i;
3510 
3511 	for (i = 0; i < bp->nr_vnics; i++) {
3512 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3513 		int j;
3514 
3515 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
3516 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
3517 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
3518 
3519 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
3520 
3521 		if (bp->vnic_info[i].rss_hash_key) {
3522 			if (i == 0)
3523 				prandom_bytes(vnic->rss_hash_key,
3524 					      HW_HASH_KEY_SIZE);
3525 			else
3526 				memcpy(vnic->rss_hash_key,
3527 				       bp->vnic_info[0].rss_hash_key,
3528 				       HW_HASH_KEY_SIZE);
3529 		}
3530 	}
3531 }
3532 
bnxt_calc_nr_ring_pages(u32 ring_size,int desc_per_pg)3533 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
3534 {
3535 	int pages;
3536 
3537 	pages = ring_size / desc_per_pg;
3538 
3539 	if (!pages)
3540 		return 1;
3541 
3542 	pages++;
3543 
3544 	while (pages & (pages - 1))
3545 		pages++;
3546 
3547 	return pages;
3548 }
3549 
bnxt_set_tpa_flags(struct bnxt * bp)3550 void bnxt_set_tpa_flags(struct bnxt *bp)
3551 {
3552 	bp->flags &= ~BNXT_FLAG_TPA;
3553 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
3554 		return;
3555 	if (bp->dev->features & NETIF_F_LRO)
3556 		bp->flags |= BNXT_FLAG_LRO;
3557 	else if (bp->dev->features & NETIF_F_GRO_HW)
3558 		bp->flags |= BNXT_FLAG_GRO;
3559 }
3560 
3561 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
3562  * be set on entry.
3563  */
bnxt_set_ring_params(struct bnxt * bp)3564 void bnxt_set_ring_params(struct bnxt *bp)
3565 {
3566 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
3567 	u32 agg_factor = 0, agg_ring_size = 0;
3568 
3569 	/* 8 for CRC and VLAN */
3570 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
3571 
3572 	rx_space = rx_size + NET_SKB_PAD +
3573 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3574 
3575 	bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
3576 	ring_size = bp->rx_ring_size;
3577 	bp->rx_agg_ring_size = 0;
3578 	bp->rx_agg_nr_pages = 0;
3579 
3580 	if (bp->flags & BNXT_FLAG_TPA)
3581 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
3582 
3583 	bp->flags &= ~BNXT_FLAG_JUMBO;
3584 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
3585 		u32 jumbo_factor;
3586 
3587 		bp->flags |= BNXT_FLAG_JUMBO;
3588 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
3589 		if (jumbo_factor > agg_factor)
3590 			agg_factor = jumbo_factor;
3591 	}
3592 	agg_ring_size = ring_size * agg_factor;
3593 
3594 	if (agg_ring_size) {
3595 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
3596 							RX_DESC_CNT);
3597 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
3598 			u32 tmp = agg_ring_size;
3599 
3600 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
3601 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
3602 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
3603 				    tmp, agg_ring_size);
3604 		}
3605 		bp->rx_agg_ring_size = agg_ring_size;
3606 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
3607 		rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
3608 		rx_space = rx_size + NET_SKB_PAD +
3609 			SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3610 	}
3611 
3612 	bp->rx_buf_use_size = rx_size;
3613 	bp->rx_buf_size = rx_space;
3614 
3615 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
3616 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
3617 
3618 	ring_size = bp->tx_ring_size;
3619 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
3620 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
3621 
3622 	max_rx_cmpl = bp->rx_ring_size;
3623 	/* MAX TPA needs to be added because TPA_START completions are
3624 	 * immediately recycled, so the TPA completions are not bound by
3625 	 * the RX ring size.
3626 	 */
3627 	if (bp->flags & BNXT_FLAG_TPA)
3628 		max_rx_cmpl += bp->max_tpa;
3629 	/* RX and TPA completions are 32-byte, all others are 16-byte */
3630 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
3631 	bp->cp_ring_size = ring_size;
3632 
3633 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
3634 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
3635 		bp->cp_nr_pages = MAX_CP_PAGES;
3636 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
3637 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
3638 			    ring_size, bp->cp_ring_size);
3639 	}
3640 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
3641 	bp->cp_ring_mask = bp->cp_bit - 1;
3642 }
3643 
3644 /* Changing allocation mode of RX rings.
3645  * TODO: Update when extending xdp_rxq_info to support allocation modes.
3646  */
bnxt_set_rx_skb_mode(struct bnxt * bp,bool page_mode)3647 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
3648 {
3649 	if (page_mode) {
3650 		if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
3651 			return -EOPNOTSUPP;
3652 		bp->dev->max_mtu =
3653 			min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
3654 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
3655 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
3656 		bp->rx_dir = DMA_BIDIRECTIONAL;
3657 		bp->rx_skb_func = bnxt_rx_page_skb;
3658 		/* Disable LRO or GRO_HW */
3659 		netdev_update_features(bp->dev);
3660 	} else {
3661 		bp->dev->max_mtu = bp->max_mtu;
3662 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
3663 		bp->rx_dir = DMA_FROM_DEVICE;
3664 		bp->rx_skb_func = bnxt_rx_skb;
3665 	}
3666 	return 0;
3667 }
3668 
bnxt_free_vnic_attributes(struct bnxt * bp)3669 static void bnxt_free_vnic_attributes(struct bnxt *bp)
3670 {
3671 	int i;
3672 	struct bnxt_vnic_info *vnic;
3673 	struct pci_dev *pdev = bp->pdev;
3674 
3675 	if (!bp->vnic_info)
3676 		return;
3677 
3678 	for (i = 0; i < bp->nr_vnics; i++) {
3679 		vnic = &bp->vnic_info[i];
3680 
3681 		kfree(vnic->fw_grp_ids);
3682 		vnic->fw_grp_ids = NULL;
3683 
3684 		kfree(vnic->uc_list);
3685 		vnic->uc_list = NULL;
3686 
3687 		if (vnic->mc_list) {
3688 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
3689 					  vnic->mc_list, vnic->mc_list_mapping);
3690 			vnic->mc_list = NULL;
3691 		}
3692 
3693 		if (vnic->rss_table) {
3694 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
3695 					  vnic->rss_table,
3696 					  vnic->rss_table_dma_addr);
3697 			vnic->rss_table = NULL;
3698 		}
3699 
3700 		vnic->rss_hash_key = NULL;
3701 		vnic->flags = 0;
3702 	}
3703 }
3704 
bnxt_alloc_vnic_attributes(struct bnxt * bp)3705 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
3706 {
3707 	int i, rc = 0, size;
3708 	struct bnxt_vnic_info *vnic;
3709 	struct pci_dev *pdev = bp->pdev;
3710 	int max_rings;
3711 
3712 	for (i = 0; i < bp->nr_vnics; i++) {
3713 		vnic = &bp->vnic_info[i];
3714 
3715 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
3716 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
3717 
3718 			if (mem_size > 0) {
3719 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
3720 				if (!vnic->uc_list) {
3721 					rc = -ENOMEM;
3722 					goto out;
3723 				}
3724 			}
3725 		}
3726 
3727 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
3728 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
3729 			vnic->mc_list =
3730 				dma_alloc_coherent(&pdev->dev,
3731 						   vnic->mc_list_size,
3732 						   &vnic->mc_list_mapping,
3733 						   GFP_KERNEL);
3734 			if (!vnic->mc_list) {
3735 				rc = -ENOMEM;
3736 				goto out;
3737 			}
3738 		}
3739 
3740 		if (bp->flags & BNXT_FLAG_CHIP_P5)
3741 			goto vnic_skip_grps;
3742 
3743 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
3744 			max_rings = bp->rx_nr_rings;
3745 		else
3746 			max_rings = 1;
3747 
3748 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
3749 		if (!vnic->fw_grp_ids) {
3750 			rc = -ENOMEM;
3751 			goto out;
3752 		}
3753 vnic_skip_grps:
3754 		if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
3755 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
3756 			continue;
3757 
3758 		/* Allocate rss table and hash key */
3759 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
3760 		if (bp->flags & BNXT_FLAG_CHIP_P5)
3761 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
3762 
3763 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
3764 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
3765 						     vnic->rss_table_size,
3766 						     &vnic->rss_table_dma_addr,
3767 						     GFP_KERNEL);
3768 		if (!vnic->rss_table) {
3769 			rc = -ENOMEM;
3770 			goto out;
3771 		}
3772 
3773 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
3774 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
3775 	}
3776 	return 0;
3777 
3778 out:
3779 	return rc;
3780 }
3781 
bnxt_free_hwrm_resources(struct bnxt * bp)3782 static void bnxt_free_hwrm_resources(struct bnxt *bp)
3783 {
3784 	struct pci_dev *pdev = bp->pdev;
3785 
3786 	if (bp->hwrm_cmd_resp_addr) {
3787 		dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
3788 				  bp->hwrm_cmd_resp_dma_addr);
3789 		bp->hwrm_cmd_resp_addr = NULL;
3790 	}
3791 
3792 	if (bp->hwrm_cmd_kong_resp_addr) {
3793 		dma_free_coherent(&pdev->dev, PAGE_SIZE,
3794 				  bp->hwrm_cmd_kong_resp_addr,
3795 				  bp->hwrm_cmd_kong_resp_dma_addr);
3796 		bp->hwrm_cmd_kong_resp_addr = NULL;
3797 	}
3798 }
3799 
bnxt_alloc_kong_hwrm_resources(struct bnxt * bp)3800 static int bnxt_alloc_kong_hwrm_resources(struct bnxt *bp)
3801 {
3802 	struct pci_dev *pdev = bp->pdev;
3803 
3804 	if (bp->hwrm_cmd_kong_resp_addr)
3805 		return 0;
3806 
3807 	bp->hwrm_cmd_kong_resp_addr =
3808 		dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3809 				   &bp->hwrm_cmd_kong_resp_dma_addr,
3810 				   GFP_KERNEL);
3811 	if (!bp->hwrm_cmd_kong_resp_addr)
3812 		return -ENOMEM;
3813 
3814 	return 0;
3815 }
3816 
bnxt_alloc_hwrm_resources(struct bnxt * bp)3817 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
3818 {
3819 	struct pci_dev *pdev = bp->pdev;
3820 
3821 	bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3822 						   &bp->hwrm_cmd_resp_dma_addr,
3823 						   GFP_KERNEL);
3824 	if (!bp->hwrm_cmd_resp_addr)
3825 		return -ENOMEM;
3826 
3827 	return 0;
3828 }
3829 
bnxt_free_hwrm_short_cmd_req(struct bnxt * bp)3830 static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
3831 {
3832 	if (bp->hwrm_short_cmd_req_addr) {
3833 		struct pci_dev *pdev = bp->pdev;
3834 
3835 		dma_free_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
3836 				  bp->hwrm_short_cmd_req_addr,
3837 				  bp->hwrm_short_cmd_req_dma_addr);
3838 		bp->hwrm_short_cmd_req_addr = NULL;
3839 	}
3840 }
3841 
bnxt_alloc_hwrm_short_cmd_req(struct bnxt * bp)3842 static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
3843 {
3844 	struct pci_dev *pdev = bp->pdev;
3845 
3846 	if (bp->hwrm_short_cmd_req_addr)
3847 		return 0;
3848 
3849 	bp->hwrm_short_cmd_req_addr =
3850 		dma_alloc_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
3851 				   &bp->hwrm_short_cmd_req_dma_addr,
3852 				   GFP_KERNEL);
3853 	if (!bp->hwrm_short_cmd_req_addr)
3854 		return -ENOMEM;
3855 
3856 	return 0;
3857 }
3858 
bnxt_free_stats_mem(struct bnxt * bp,struct bnxt_stats_mem * stats)3859 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
3860 {
3861 	kfree(stats->hw_masks);
3862 	stats->hw_masks = NULL;
3863 	kfree(stats->sw_stats);
3864 	stats->sw_stats = NULL;
3865 	if (stats->hw_stats) {
3866 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
3867 				  stats->hw_stats_map);
3868 		stats->hw_stats = NULL;
3869 	}
3870 }
3871 
bnxt_alloc_stats_mem(struct bnxt * bp,struct bnxt_stats_mem * stats,bool alloc_masks)3872 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
3873 				bool alloc_masks)
3874 {
3875 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
3876 					     &stats->hw_stats_map, GFP_KERNEL);
3877 	if (!stats->hw_stats)
3878 		return -ENOMEM;
3879 
3880 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
3881 	if (!stats->sw_stats)
3882 		goto stats_mem_err;
3883 
3884 	if (alloc_masks) {
3885 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
3886 		if (!stats->hw_masks)
3887 			goto stats_mem_err;
3888 	}
3889 	return 0;
3890 
3891 stats_mem_err:
3892 	bnxt_free_stats_mem(bp, stats);
3893 	return -ENOMEM;
3894 }
3895 
bnxt_fill_masks(u64 * mask_arr,u64 mask,int count)3896 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
3897 {
3898 	int i;
3899 
3900 	for (i = 0; i < count; i++)
3901 		mask_arr[i] = mask;
3902 }
3903 
bnxt_copy_hw_masks(u64 * mask_arr,__le64 * hw_mask_arr,int count)3904 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
3905 {
3906 	int i;
3907 
3908 	for (i = 0; i < count; i++)
3909 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
3910 }
3911 
bnxt_hwrm_func_qstat_ext(struct bnxt * bp,struct bnxt_stats_mem * stats)3912 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
3913 				    struct bnxt_stats_mem *stats)
3914 {
3915 	struct hwrm_func_qstats_ext_output *resp = bp->hwrm_cmd_resp_addr;
3916 	struct hwrm_func_qstats_ext_input req = {0};
3917 	__le64 *hw_masks;
3918 	int rc;
3919 
3920 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
3921 	    !(bp->flags & BNXT_FLAG_CHIP_P5))
3922 		return -EOPNOTSUPP;
3923 
3924 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QSTATS_EXT, -1, -1);
3925 	req.fid = cpu_to_le16(0xffff);
3926 	req.flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
3927 	mutex_lock(&bp->hwrm_cmd_lock);
3928 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3929 	if (rc)
3930 		goto qstat_exit;
3931 
3932 	hw_masks = &resp->rx_ucast_pkts;
3933 	bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
3934 
3935 qstat_exit:
3936 	mutex_unlock(&bp->hwrm_cmd_lock);
3937 	return rc;
3938 }
3939 
3940 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
3941 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
3942 
bnxt_init_stats(struct bnxt * bp)3943 static void bnxt_init_stats(struct bnxt *bp)
3944 {
3945 	struct bnxt_napi *bnapi = bp->bnapi[0];
3946 	struct bnxt_cp_ring_info *cpr;
3947 	struct bnxt_stats_mem *stats;
3948 	__le64 *rx_stats, *tx_stats;
3949 	int rc, rx_count, tx_count;
3950 	u64 *rx_masks, *tx_masks;
3951 	u64 mask;
3952 	u8 flags;
3953 
3954 	cpr = &bnapi->cp_ring;
3955 	stats = &cpr->stats;
3956 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
3957 	if (rc) {
3958 		if (bp->flags & BNXT_FLAG_CHIP_P5)
3959 			mask = (1ULL << 48) - 1;
3960 		else
3961 			mask = -1ULL;
3962 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
3963 	}
3964 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
3965 		stats = &bp->port_stats;
3966 		rx_stats = stats->hw_stats;
3967 		rx_masks = stats->hw_masks;
3968 		rx_count = sizeof(struct rx_port_stats) / 8;
3969 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
3970 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
3971 		tx_count = sizeof(struct tx_port_stats) / 8;
3972 
3973 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
3974 		rc = bnxt_hwrm_port_qstats(bp, flags);
3975 		if (rc) {
3976 			mask = (1ULL << 40) - 1;
3977 
3978 			bnxt_fill_masks(rx_masks, mask, rx_count);
3979 			bnxt_fill_masks(tx_masks, mask, tx_count);
3980 		} else {
3981 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
3982 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
3983 			bnxt_hwrm_port_qstats(bp, 0);
3984 		}
3985 	}
3986 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
3987 		stats = &bp->rx_port_stats_ext;
3988 		rx_stats = stats->hw_stats;
3989 		rx_masks = stats->hw_masks;
3990 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
3991 		stats = &bp->tx_port_stats_ext;
3992 		tx_stats = stats->hw_stats;
3993 		tx_masks = stats->hw_masks;
3994 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
3995 
3996 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
3997 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
3998 		if (rc) {
3999 			mask = (1ULL << 40) - 1;
4000 
4001 			bnxt_fill_masks(rx_masks, mask, rx_count);
4002 			if (tx_stats)
4003 				bnxt_fill_masks(tx_masks, mask, tx_count);
4004 		} else {
4005 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4006 			if (tx_stats)
4007 				bnxt_copy_hw_masks(tx_masks, tx_stats,
4008 						   tx_count);
4009 			bnxt_hwrm_port_qstats_ext(bp, 0);
4010 		}
4011 	}
4012 }
4013 
bnxt_free_port_stats(struct bnxt * bp)4014 static void bnxt_free_port_stats(struct bnxt *bp)
4015 {
4016 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
4017 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
4018 
4019 	bnxt_free_stats_mem(bp, &bp->port_stats);
4020 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
4021 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
4022 }
4023 
bnxt_free_ring_stats(struct bnxt * bp)4024 static void bnxt_free_ring_stats(struct bnxt *bp)
4025 {
4026 	int i;
4027 
4028 	if (!bp->bnapi)
4029 		return;
4030 
4031 	for (i = 0; i < bp->cp_nr_rings; i++) {
4032 		struct bnxt_napi *bnapi = bp->bnapi[i];
4033 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4034 
4035 		bnxt_free_stats_mem(bp, &cpr->stats);
4036 	}
4037 }
4038 
bnxt_alloc_stats(struct bnxt * bp)4039 static int bnxt_alloc_stats(struct bnxt *bp)
4040 {
4041 	u32 size, i;
4042 	int rc;
4043 
4044 	size = bp->hw_ring_stats_size;
4045 
4046 	for (i = 0; i < bp->cp_nr_rings; i++) {
4047 		struct bnxt_napi *bnapi = bp->bnapi[i];
4048 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4049 
4050 		cpr->stats.len = size;
4051 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
4052 		if (rc)
4053 			return rc;
4054 
4055 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4056 	}
4057 
4058 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
4059 		return 0;
4060 
4061 	if (bp->port_stats.hw_stats)
4062 		goto alloc_ext_stats;
4063 
4064 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
4065 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
4066 	if (rc)
4067 		return rc;
4068 
4069 	bp->flags |= BNXT_FLAG_PORT_STATS;
4070 
4071 alloc_ext_stats:
4072 	/* Display extended statistics only if FW supports it */
4073 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
4074 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
4075 			return 0;
4076 
4077 	if (bp->rx_port_stats_ext.hw_stats)
4078 		goto alloc_tx_ext_stats;
4079 
4080 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
4081 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
4082 	/* Extended stats are optional */
4083 	if (rc)
4084 		return 0;
4085 
4086 alloc_tx_ext_stats:
4087 	if (bp->tx_port_stats_ext.hw_stats)
4088 		return 0;
4089 
4090 	if (bp->hwrm_spec_code >= 0x10902 ||
4091 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
4092 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
4093 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
4094 		/* Extended stats are optional */
4095 		if (rc)
4096 			return 0;
4097 	}
4098 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
4099 	return 0;
4100 }
4101 
bnxt_clear_ring_indices(struct bnxt * bp)4102 static void bnxt_clear_ring_indices(struct bnxt *bp)
4103 {
4104 	int i;
4105 
4106 	if (!bp->bnapi)
4107 		return;
4108 
4109 	for (i = 0; i < bp->cp_nr_rings; i++) {
4110 		struct bnxt_napi *bnapi = bp->bnapi[i];
4111 		struct bnxt_cp_ring_info *cpr;
4112 		struct bnxt_rx_ring_info *rxr;
4113 		struct bnxt_tx_ring_info *txr;
4114 
4115 		if (!bnapi)
4116 			continue;
4117 
4118 		cpr = &bnapi->cp_ring;
4119 		cpr->cp_raw_cons = 0;
4120 
4121 		txr = bnapi->tx_ring;
4122 		if (txr) {
4123 			txr->tx_prod = 0;
4124 			txr->tx_cons = 0;
4125 		}
4126 
4127 		rxr = bnapi->rx_ring;
4128 		if (rxr) {
4129 			rxr->rx_prod = 0;
4130 			rxr->rx_agg_prod = 0;
4131 			rxr->rx_sw_agg_prod = 0;
4132 			rxr->rx_next_cons = 0;
4133 		}
4134 	}
4135 }
4136 
bnxt_free_ntp_fltrs(struct bnxt * bp,bool irq_reinit)4137 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
4138 {
4139 #ifdef CONFIG_RFS_ACCEL
4140 	int i;
4141 
4142 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
4143 	 * safe to delete the hash table.
4144 	 */
4145 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
4146 		struct hlist_head *head;
4147 		struct hlist_node *tmp;
4148 		struct bnxt_ntuple_filter *fltr;
4149 
4150 		head = &bp->ntp_fltr_hash_tbl[i];
4151 		hlist_for_each_entry_safe(fltr, tmp, head, hash) {
4152 			hlist_del(&fltr->hash);
4153 			kfree(fltr);
4154 		}
4155 	}
4156 	if (irq_reinit) {
4157 		kfree(bp->ntp_fltr_bmap);
4158 		bp->ntp_fltr_bmap = NULL;
4159 	}
4160 	bp->ntp_fltr_count = 0;
4161 #endif
4162 }
4163 
bnxt_alloc_ntp_fltrs(struct bnxt * bp)4164 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
4165 {
4166 #ifdef CONFIG_RFS_ACCEL
4167 	int i, rc = 0;
4168 
4169 	if (!(bp->flags & BNXT_FLAG_RFS))
4170 		return 0;
4171 
4172 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
4173 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
4174 
4175 	bp->ntp_fltr_count = 0;
4176 	bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
4177 				    sizeof(long),
4178 				    GFP_KERNEL);
4179 
4180 	if (!bp->ntp_fltr_bmap)
4181 		rc = -ENOMEM;
4182 
4183 	return rc;
4184 #else
4185 	return 0;
4186 #endif
4187 }
4188 
bnxt_free_mem(struct bnxt * bp,bool irq_re_init)4189 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
4190 {
4191 	bnxt_free_vnic_attributes(bp);
4192 	bnxt_free_tx_rings(bp);
4193 	bnxt_free_rx_rings(bp);
4194 	bnxt_free_cp_rings(bp);
4195 	bnxt_free_ntp_fltrs(bp, irq_re_init);
4196 	if (irq_re_init) {
4197 		bnxt_free_ring_stats(bp);
4198 		if (!(bp->fw_cap & BNXT_FW_CAP_PORT_STATS_NO_RESET) ||
4199 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
4200 			bnxt_free_port_stats(bp);
4201 		bnxt_free_ring_grps(bp);
4202 		bnxt_free_vnics(bp);
4203 		kfree(bp->tx_ring_map);
4204 		bp->tx_ring_map = NULL;
4205 		kfree(bp->tx_ring);
4206 		bp->tx_ring = NULL;
4207 		kfree(bp->rx_ring);
4208 		bp->rx_ring = NULL;
4209 		kfree(bp->bnapi);
4210 		bp->bnapi = NULL;
4211 	} else {
4212 		bnxt_clear_ring_indices(bp);
4213 	}
4214 }
4215 
bnxt_alloc_mem(struct bnxt * bp,bool irq_re_init)4216 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
4217 {
4218 	int i, j, rc, size, arr_size;
4219 	void *bnapi;
4220 
4221 	if (irq_re_init) {
4222 		/* Allocate bnapi mem pointer array and mem block for
4223 		 * all queues
4224 		 */
4225 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
4226 				bp->cp_nr_rings);
4227 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
4228 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
4229 		if (!bnapi)
4230 			return -ENOMEM;
4231 
4232 		bp->bnapi = bnapi;
4233 		bnapi += arr_size;
4234 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
4235 			bp->bnapi[i] = bnapi;
4236 			bp->bnapi[i]->index = i;
4237 			bp->bnapi[i]->bp = bp;
4238 			if (bp->flags & BNXT_FLAG_CHIP_P5) {
4239 				struct bnxt_cp_ring_info *cpr =
4240 					&bp->bnapi[i]->cp_ring;
4241 
4242 				cpr->cp_ring_struct.ring_mem.flags =
4243 					BNXT_RMEM_RING_PTE_FLAG;
4244 			}
4245 		}
4246 
4247 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
4248 				      sizeof(struct bnxt_rx_ring_info),
4249 				      GFP_KERNEL);
4250 		if (!bp->rx_ring)
4251 			return -ENOMEM;
4252 
4253 		for (i = 0; i < bp->rx_nr_rings; i++) {
4254 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4255 
4256 			if (bp->flags & BNXT_FLAG_CHIP_P5) {
4257 				rxr->rx_ring_struct.ring_mem.flags =
4258 					BNXT_RMEM_RING_PTE_FLAG;
4259 				rxr->rx_agg_ring_struct.ring_mem.flags =
4260 					BNXT_RMEM_RING_PTE_FLAG;
4261 			}
4262 			rxr->bnapi = bp->bnapi[i];
4263 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
4264 		}
4265 
4266 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
4267 				      sizeof(struct bnxt_tx_ring_info),
4268 				      GFP_KERNEL);
4269 		if (!bp->tx_ring)
4270 			return -ENOMEM;
4271 
4272 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
4273 					  GFP_KERNEL);
4274 
4275 		if (!bp->tx_ring_map)
4276 			return -ENOMEM;
4277 
4278 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4279 			j = 0;
4280 		else
4281 			j = bp->rx_nr_rings;
4282 
4283 		for (i = 0; i < bp->tx_nr_rings; i++, j++) {
4284 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4285 
4286 			if (bp->flags & BNXT_FLAG_CHIP_P5)
4287 				txr->tx_ring_struct.ring_mem.flags =
4288 					BNXT_RMEM_RING_PTE_FLAG;
4289 			txr->bnapi = bp->bnapi[j];
4290 			bp->bnapi[j]->tx_ring = txr;
4291 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
4292 			if (i >= bp->tx_nr_rings_xdp) {
4293 				txr->txq_index = i - bp->tx_nr_rings_xdp;
4294 				bp->bnapi[j]->tx_int = bnxt_tx_int;
4295 			} else {
4296 				bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
4297 				bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
4298 			}
4299 		}
4300 
4301 		rc = bnxt_alloc_stats(bp);
4302 		if (rc)
4303 			goto alloc_mem_err;
4304 		bnxt_init_stats(bp);
4305 
4306 		rc = bnxt_alloc_ntp_fltrs(bp);
4307 		if (rc)
4308 			goto alloc_mem_err;
4309 
4310 		rc = bnxt_alloc_vnics(bp);
4311 		if (rc)
4312 			goto alloc_mem_err;
4313 	}
4314 
4315 	bnxt_init_ring_struct(bp);
4316 
4317 	rc = bnxt_alloc_rx_rings(bp);
4318 	if (rc)
4319 		goto alloc_mem_err;
4320 
4321 	rc = bnxt_alloc_tx_rings(bp);
4322 	if (rc)
4323 		goto alloc_mem_err;
4324 
4325 	rc = bnxt_alloc_cp_rings(bp);
4326 	if (rc)
4327 		goto alloc_mem_err;
4328 
4329 	bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
4330 				  BNXT_VNIC_UCAST_FLAG;
4331 	rc = bnxt_alloc_vnic_attributes(bp);
4332 	if (rc)
4333 		goto alloc_mem_err;
4334 	return 0;
4335 
4336 alloc_mem_err:
4337 	bnxt_free_mem(bp, true);
4338 	return rc;
4339 }
4340 
bnxt_disable_int(struct bnxt * bp)4341 static void bnxt_disable_int(struct bnxt *bp)
4342 {
4343 	int i;
4344 
4345 	if (!bp->bnapi)
4346 		return;
4347 
4348 	for (i = 0; i < bp->cp_nr_rings; i++) {
4349 		struct bnxt_napi *bnapi = bp->bnapi[i];
4350 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4351 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4352 
4353 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
4354 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
4355 	}
4356 }
4357 
bnxt_cp_num_to_irq_num(struct bnxt * bp,int n)4358 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
4359 {
4360 	struct bnxt_napi *bnapi = bp->bnapi[n];
4361 	struct bnxt_cp_ring_info *cpr;
4362 
4363 	cpr = &bnapi->cp_ring;
4364 	return cpr->cp_ring_struct.map_idx;
4365 }
4366 
bnxt_disable_int_sync(struct bnxt * bp)4367 static void bnxt_disable_int_sync(struct bnxt *bp)
4368 {
4369 	int i;
4370 
4371 	atomic_inc(&bp->intr_sem);
4372 
4373 	bnxt_disable_int(bp);
4374 	for (i = 0; i < bp->cp_nr_rings; i++) {
4375 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
4376 
4377 		synchronize_irq(bp->irq_tbl[map_idx].vector);
4378 	}
4379 }
4380 
bnxt_enable_int(struct bnxt * bp)4381 static void bnxt_enable_int(struct bnxt *bp)
4382 {
4383 	int i;
4384 
4385 	atomic_set(&bp->intr_sem, 0);
4386 	for (i = 0; i < bp->cp_nr_rings; i++) {
4387 		struct bnxt_napi *bnapi = bp->bnapi[i];
4388 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4389 
4390 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
4391 	}
4392 }
4393 
bnxt_hwrm_cmd_hdr_init(struct bnxt * bp,void * request,u16 req_type,u16 cmpl_ring,u16 target_id)4394 void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
4395 			    u16 cmpl_ring, u16 target_id)
4396 {
4397 	struct input *req = request;
4398 
4399 	req->req_type = cpu_to_le16(req_type);
4400 	req->cmpl_ring = cpu_to_le16(cmpl_ring);
4401 	req->target_id = cpu_to_le16(target_id);
4402 	if (bnxt_kong_hwrm_message(bp, req))
4403 		req->resp_addr = cpu_to_le64(bp->hwrm_cmd_kong_resp_dma_addr);
4404 	else
4405 		req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
4406 }
4407 
bnxt_hwrm_to_stderr(u32 hwrm_err)4408 static int bnxt_hwrm_to_stderr(u32 hwrm_err)
4409 {
4410 	switch (hwrm_err) {
4411 	case HWRM_ERR_CODE_SUCCESS:
4412 		return 0;
4413 	case HWRM_ERR_CODE_RESOURCE_LOCKED:
4414 		return -EROFS;
4415 	case HWRM_ERR_CODE_RESOURCE_ACCESS_DENIED:
4416 		return -EACCES;
4417 	case HWRM_ERR_CODE_RESOURCE_ALLOC_ERROR:
4418 		return -ENOSPC;
4419 	case HWRM_ERR_CODE_INVALID_PARAMS:
4420 	case HWRM_ERR_CODE_INVALID_FLAGS:
4421 	case HWRM_ERR_CODE_INVALID_ENABLES:
4422 	case HWRM_ERR_CODE_UNSUPPORTED_TLV:
4423 	case HWRM_ERR_CODE_UNSUPPORTED_OPTION_ERR:
4424 		return -EINVAL;
4425 	case HWRM_ERR_CODE_NO_BUFFER:
4426 		return -ENOMEM;
4427 	case HWRM_ERR_CODE_HOT_RESET_PROGRESS:
4428 	case HWRM_ERR_CODE_BUSY:
4429 		return -EAGAIN;
4430 	case HWRM_ERR_CODE_CMD_NOT_SUPPORTED:
4431 		return -EOPNOTSUPP;
4432 	default:
4433 		return -EIO;
4434 	}
4435 }
4436 
bnxt_hwrm_do_send_msg(struct bnxt * bp,void * msg,u32 msg_len,int timeout,bool silent)4437 static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
4438 				 int timeout, bool silent)
4439 {
4440 	int i, intr_process, rc, tmo_count;
4441 	struct input *req = msg;
4442 	u32 *data = msg;
4443 	u8 *valid;
4444 	u16 cp_ring_id, len = 0;
4445 	struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
4446 	u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
4447 	struct hwrm_short_input short_input = {0};
4448 	u32 doorbell_offset = BNXT_GRCPF_REG_CHIMP_COMM_TRIGGER;
4449 	u32 bar_offset = BNXT_GRCPF_REG_CHIMP_COMM;
4450 	u16 dst = BNXT_HWRM_CHNL_CHIMP;
4451 
4452 	if (BNXT_NO_FW_ACCESS(bp) &&
4453 	    le16_to_cpu(req->req_type) != HWRM_FUNC_RESET)
4454 		return -EBUSY;
4455 
4456 	if (msg_len > BNXT_HWRM_MAX_REQ_LEN) {
4457 		if (msg_len > bp->hwrm_max_ext_req_len ||
4458 		    !bp->hwrm_short_cmd_req_addr)
4459 			return -EINVAL;
4460 	}
4461 
4462 	if (bnxt_hwrm_kong_chnl(bp, req)) {
4463 		dst = BNXT_HWRM_CHNL_KONG;
4464 		bar_offset = BNXT_GRCPF_REG_KONG_COMM;
4465 		doorbell_offset = BNXT_GRCPF_REG_KONG_COMM_TRIGGER;
4466 		resp = bp->hwrm_cmd_kong_resp_addr;
4467 	}
4468 
4469 	memset(resp, 0, PAGE_SIZE);
4470 	cp_ring_id = le16_to_cpu(req->cmpl_ring);
4471 	intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
4472 
4473 	req->seq_id = cpu_to_le16(bnxt_get_hwrm_seq_id(bp, dst));
4474 	/* currently supports only one outstanding message */
4475 	if (intr_process)
4476 		bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
4477 
4478 	if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
4479 	    msg_len > BNXT_HWRM_MAX_REQ_LEN) {
4480 		void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
4481 		u16 max_msg_len;
4482 
4483 		/* Set boundary for maximum extended request length for short
4484 		 * cmd format. If passed up from device use the max supported
4485 		 * internal req length.
4486 		 */
4487 		max_msg_len = bp->hwrm_max_ext_req_len;
4488 
4489 		memcpy(short_cmd_req, req, msg_len);
4490 		if (msg_len < max_msg_len)
4491 			memset(short_cmd_req + msg_len, 0,
4492 			       max_msg_len - msg_len);
4493 
4494 		short_input.req_type = req->req_type;
4495 		short_input.signature =
4496 				cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
4497 		short_input.size = cpu_to_le16(msg_len);
4498 		short_input.req_addr =
4499 			cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
4500 
4501 		data = (u32 *)&short_input;
4502 		msg_len = sizeof(short_input);
4503 
4504 		/* Sync memory write before updating doorbell */
4505 		wmb();
4506 
4507 		max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
4508 	}
4509 
4510 	/* Write request msg to hwrm channel */
4511 	__iowrite32_copy(bp->bar0 + bar_offset, data, msg_len / 4);
4512 
4513 	for (i = msg_len; i < max_req_len; i += 4)
4514 		writel(0, bp->bar0 + bar_offset + i);
4515 
4516 	/* Ring channel doorbell */
4517 	writel(1, bp->bar0 + doorbell_offset);
4518 
4519 	if (!pci_is_enabled(bp->pdev))
4520 		return 0;
4521 
4522 	if (!timeout)
4523 		timeout = DFLT_HWRM_CMD_TIMEOUT;
4524 	/* convert timeout to usec */
4525 	timeout *= 1000;
4526 
4527 	i = 0;
4528 	/* Short timeout for the first few iterations:
4529 	 * number of loops = number of loops for short timeout +
4530 	 * number of loops for standard timeout.
4531 	 */
4532 	tmo_count = HWRM_SHORT_TIMEOUT_COUNTER;
4533 	timeout = timeout - HWRM_SHORT_MIN_TIMEOUT * HWRM_SHORT_TIMEOUT_COUNTER;
4534 	tmo_count += DIV_ROUND_UP(timeout, HWRM_MIN_TIMEOUT);
4535 
4536 	if (intr_process) {
4537 		u16 seq_id = bp->hwrm_intr_seq_id;
4538 
4539 		/* Wait until hwrm response cmpl interrupt is processed */
4540 		while (bp->hwrm_intr_seq_id != (u16)~seq_id &&
4541 		       i++ < tmo_count) {
4542 			/* Abort the wait for completion if the FW health
4543 			 * check has failed.
4544 			 */
4545 			if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
4546 				return -EBUSY;
4547 			/* on first few passes, just barely sleep */
4548 			if (i < HWRM_SHORT_TIMEOUT_COUNTER)
4549 				usleep_range(HWRM_SHORT_MIN_TIMEOUT,
4550 					     HWRM_SHORT_MAX_TIMEOUT);
4551 			else
4552 				usleep_range(HWRM_MIN_TIMEOUT,
4553 					     HWRM_MAX_TIMEOUT);
4554 		}
4555 
4556 		if (bp->hwrm_intr_seq_id != (u16)~seq_id) {
4557 			if (!silent)
4558 				netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
4559 					   le16_to_cpu(req->req_type));
4560 			return -EBUSY;
4561 		}
4562 		len = le16_to_cpu(resp->resp_len);
4563 		valid = ((u8 *)resp) + len - 1;
4564 	} else {
4565 		int j;
4566 
4567 		/* Check if response len is updated */
4568 		for (i = 0; i < tmo_count; i++) {
4569 			/* Abort the wait for completion if the FW health
4570 			 * check has failed.
4571 			 */
4572 			if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
4573 				return -EBUSY;
4574 			len = le16_to_cpu(resp->resp_len);
4575 			if (len)
4576 				break;
4577 			/* on first few passes, just barely sleep */
4578 			if (i < HWRM_SHORT_TIMEOUT_COUNTER)
4579 				usleep_range(HWRM_SHORT_MIN_TIMEOUT,
4580 					     HWRM_SHORT_MAX_TIMEOUT);
4581 			else
4582 				usleep_range(HWRM_MIN_TIMEOUT,
4583 					     HWRM_MAX_TIMEOUT);
4584 		}
4585 
4586 		if (i >= tmo_count) {
4587 			if (!silent)
4588 				netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
4589 					   HWRM_TOTAL_TIMEOUT(i),
4590 					   le16_to_cpu(req->req_type),
4591 					   le16_to_cpu(req->seq_id), len);
4592 			return -EBUSY;
4593 		}
4594 
4595 		/* Last byte of resp contains valid bit */
4596 		valid = ((u8 *)resp) + len - 1;
4597 		for (j = 0; j < HWRM_VALID_BIT_DELAY_USEC; j++) {
4598 			/* make sure we read from updated DMA memory */
4599 			dma_rmb();
4600 			if (*valid)
4601 				break;
4602 			usleep_range(1, 5);
4603 		}
4604 
4605 		if (j >= HWRM_VALID_BIT_DELAY_USEC) {
4606 			if (!silent)
4607 				netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
4608 					   HWRM_TOTAL_TIMEOUT(i),
4609 					   le16_to_cpu(req->req_type),
4610 					   le16_to_cpu(req->seq_id), len,
4611 					   *valid);
4612 			return -EBUSY;
4613 		}
4614 	}
4615 
4616 	/* Zero valid bit for compatibility.  Valid bit in an older spec
4617 	 * may become a new field in a newer spec.  We must make sure that
4618 	 * a new field not implemented by old spec will read zero.
4619 	 */
4620 	*valid = 0;
4621 	rc = le16_to_cpu(resp->error_code);
4622 	if (rc && !silent)
4623 		netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
4624 			   le16_to_cpu(resp->req_type),
4625 			   le16_to_cpu(resp->seq_id), rc);
4626 	return bnxt_hwrm_to_stderr(rc);
4627 }
4628 
_hwrm_send_message(struct bnxt * bp,void * msg,u32 msg_len,int timeout)4629 int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
4630 {
4631 	return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
4632 }
4633 
_hwrm_send_message_silent(struct bnxt * bp,void * msg,u32 msg_len,int timeout)4634 int _hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
4635 			      int timeout)
4636 {
4637 	return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
4638 }
4639 
hwrm_send_message(struct bnxt * bp,void * msg,u32 msg_len,int timeout)4640 int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
4641 {
4642 	int rc;
4643 
4644 	mutex_lock(&bp->hwrm_cmd_lock);
4645 	rc = _hwrm_send_message(bp, msg, msg_len, timeout);
4646 	mutex_unlock(&bp->hwrm_cmd_lock);
4647 	return rc;
4648 }
4649 
hwrm_send_message_silent(struct bnxt * bp,void * msg,u32 msg_len,int timeout)4650 int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
4651 			     int timeout)
4652 {
4653 	int rc;
4654 
4655 	mutex_lock(&bp->hwrm_cmd_lock);
4656 	rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
4657 	mutex_unlock(&bp->hwrm_cmd_lock);
4658 	return rc;
4659 }
4660 
bnxt_hwrm_func_drv_rgtr(struct bnxt * bp,unsigned long * bmap,int bmap_size,bool async_only)4661 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
4662 			    bool async_only)
4663 {
4664 	struct hwrm_func_drv_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
4665 	struct hwrm_func_drv_rgtr_input req = {0};
4666 	DECLARE_BITMAP(async_events_bmap, 256);
4667 	u32 *events = (u32 *)async_events_bmap;
4668 	u32 flags;
4669 	int rc, i;
4670 
4671 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
4672 
4673 	req.enables =
4674 		cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
4675 			    FUNC_DRV_RGTR_REQ_ENABLES_VER |
4676 			    FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4677 
4678 	req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
4679 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
4680 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
4681 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
4682 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
4683 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
4684 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
4685 	req.flags = cpu_to_le32(flags);
4686 	req.ver_maj_8b = DRV_VER_MAJ;
4687 	req.ver_min_8b = DRV_VER_MIN;
4688 	req.ver_upd_8b = DRV_VER_UPD;
4689 	req.ver_maj = cpu_to_le16(DRV_VER_MAJ);
4690 	req.ver_min = cpu_to_le16(DRV_VER_MIN);
4691 	req.ver_upd = cpu_to_le16(DRV_VER_UPD);
4692 
4693 	if (BNXT_PF(bp)) {
4694 		u32 data[8];
4695 		int i;
4696 
4697 		memset(data, 0, sizeof(data));
4698 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
4699 			u16 cmd = bnxt_vf_req_snif[i];
4700 			unsigned int bit, idx;
4701 
4702 			idx = cmd / 32;
4703 			bit = cmd % 32;
4704 			data[idx] |= 1 << bit;
4705 		}
4706 
4707 		for (i = 0; i < 8; i++)
4708 			req.vf_req_fwd[i] = cpu_to_le32(data[i]);
4709 
4710 		req.enables |=
4711 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
4712 	}
4713 
4714 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
4715 		req.flags |= cpu_to_le32(
4716 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
4717 
4718 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
4719 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
4720 		u16 event_id = bnxt_async_events_arr[i];
4721 
4722 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
4723 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
4724 			continue;
4725 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
4726 	}
4727 	if (bmap && bmap_size) {
4728 		for (i = 0; i < bmap_size; i++) {
4729 			if (test_bit(i, bmap))
4730 				__set_bit(i, async_events_bmap);
4731 		}
4732 	}
4733 	for (i = 0; i < 8; i++)
4734 		req.async_event_fwd[i] |= cpu_to_le32(events[i]);
4735 
4736 	if (async_only)
4737 		req.enables =
4738 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4739 
4740 	mutex_lock(&bp->hwrm_cmd_lock);
4741 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4742 	if (!rc) {
4743 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
4744 		if (resp->flags &
4745 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
4746 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
4747 	}
4748 	mutex_unlock(&bp->hwrm_cmd_lock);
4749 	return rc;
4750 }
4751 
bnxt_hwrm_func_drv_unrgtr(struct bnxt * bp)4752 static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
4753 {
4754 	struct hwrm_func_drv_unrgtr_input req = {0};
4755 
4756 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
4757 		return 0;
4758 
4759 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
4760 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4761 }
4762 
bnxt_hwrm_tunnel_dst_port_free(struct bnxt * bp,u8 tunnel_type)4763 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
4764 {
4765 	u32 rc = 0;
4766 	struct hwrm_tunnel_dst_port_free_input req = {0};
4767 
4768 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
4769 	req.tunnel_type = tunnel_type;
4770 
4771 	switch (tunnel_type) {
4772 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
4773 		req.tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
4774 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
4775 		break;
4776 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
4777 		req.tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
4778 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
4779 		break;
4780 	default:
4781 		break;
4782 	}
4783 
4784 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4785 	if (rc)
4786 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
4787 			   rc);
4788 	return rc;
4789 }
4790 
bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt * bp,__be16 port,u8 tunnel_type)4791 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
4792 					   u8 tunnel_type)
4793 {
4794 	u32 rc = 0;
4795 	struct hwrm_tunnel_dst_port_alloc_input req = {0};
4796 	struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4797 
4798 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
4799 
4800 	req.tunnel_type = tunnel_type;
4801 	req.tunnel_dst_port_val = port;
4802 
4803 	mutex_lock(&bp->hwrm_cmd_lock);
4804 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4805 	if (rc) {
4806 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
4807 			   rc);
4808 		goto err_out;
4809 	}
4810 
4811 	switch (tunnel_type) {
4812 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
4813 		bp->vxlan_fw_dst_port_id =
4814 			le16_to_cpu(resp->tunnel_dst_port_id);
4815 		break;
4816 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
4817 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
4818 		break;
4819 	default:
4820 		break;
4821 	}
4822 
4823 err_out:
4824 	mutex_unlock(&bp->hwrm_cmd_lock);
4825 	return rc;
4826 }
4827 
bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt * bp,u16 vnic_id)4828 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
4829 {
4830 	struct hwrm_cfa_l2_set_rx_mask_input req = {0};
4831 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4832 
4833 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
4834 	req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4835 
4836 	req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
4837 	req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
4838 	req.mask = cpu_to_le32(vnic->rx_mask);
4839 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4840 }
4841 
4842 #ifdef CONFIG_RFS_ACCEL
bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt * bp,struct bnxt_ntuple_filter * fltr)4843 static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
4844 					    struct bnxt_ntuple_filter *fltr)
4845 {
4846 	struct hwrm_cfa_ntuple_filter_free_input req = {0};
4847 
4848 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
4849 	req.ntuple_filter_id = fltr->filter_id;
4850 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4851 }
4852 
4853 #define BNXT_NTP_FLTR_FLAGS					\
4854 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
4855 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
4856 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR |	\
4857 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
4858 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
4859 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
4860 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
4861 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
4862 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
4863 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
4864 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
4865 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
4866 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
4867 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
4868 
4869 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
4870 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
4871 
bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt * bp,struct bnxt_ntuple_filter * fltr)4872 static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
4873 					     struct bnxt_ntuple_filter *fltr)
4874 {
4875 	struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
4876 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
4877 	struct flow_keys *keys = &fltr->fkeys;
4878 	struct bnxt_vnic_info *vnic;
4879 	u32 flags = 0;
4880 	int rc = 0;
4881 
4882 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
4883 	req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
4884 
4885 	if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
4886 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
4887 		req.dst_id = cpu_to_le16(fltr->rxq);
4888 	} else {
4889 		vnic = &bp->vnic_info[fltr->rxq + 1];
4890 		req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
4891 	}
4892 	req.flags = cpu_to_le32(flags);
4893 	req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
4894 
4895 	req.ethertype = htons(ETH_P_IP);
4896 	memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
4897 	req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
4898 	req.ip_protocol = keys->basic.ip_proto;
4899 
4900 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
4901 		int i;
4902 
4903 		req.ethertype = htons(ETH_P_IPV6);
4904 		req.ip_addr_type =
4905 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
4906 		*(struct in6_addr *)&req.src_ipaddr[0] =
4907 			keys->addrs.v6addrs.src;
4908 		*(struct in6_addr *)&req.dst_ipaddr[0] =
4909 			keys->addrs.v6addrs.dst;
4910 		for (i = 0; i < 4; i++) {
4911 			req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4912 			req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4913 		}
4914 	} else {
4915 		req.src_ipaddr[0] = keys->addrs.v4addrs.src;
4916 		req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4917 		req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
4918 		req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4919 	}
4920 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
4921 		req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
4922 		req.tunnel_type =
4923 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
4924 	}
4925 
4926 	req.src_port = keys->ports.src;
4927 	req.src_port_mask = cpu_to_be16(0xffff);
4928 	req.dst_port = keys->ports.dst;
4929 	req.dst_port_mask = cpu_to_be16(0xffff);
4930 
4931 	mutex_lock(&bp->hwrm_cmd_lock);
4932 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4933 	if (!rc) {
4934 		resp = bnxt_get_hwrm_resp_addr(bp, &req);
4935 		fltr->filter_id = resp->ntuple_filter_id;
4936 	}
4937 	mutex_unlock(&bp->hwrm_cmd_lock);
4938 	return rc;
4939 }
4940 #endif
4941 
bnxt_hwrm_set_vnic_filter(struct bnxt * bp,u16 vnic_id,u16 idx,u8 * mac_addr)4942 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
4943 				     u8 *mac_addr)
4944 {
4945 	u32 rc = 0;
4946 	struct hwrm_cfa_l2_filter_alloc_input req = {0};
4947 	struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4948 
4949 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
4950 	req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
4951 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
4952 		req.flags |=
4953 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
4954 	req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
4955 	req.enables =
4956 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
4957 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
4958 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
4959 	memcpy(req.l2_addr, mac_addr, ETH_ALEN);
4960 	req.l2_addr_mask[0] = 0xff;
4961 	req.l2_addr_mask[1] = 0xff;
4962 	req.l2_addr_mask[2] = 0xff;
4963 	req.l2_addr_mask[3] = 0xff;
4964 	req.l2_addr_mask[4] = 0xff;
4965 	req.l2_addr_mask[5] = 0xff;
4966 
4967 	mutex_lock(&bp->hwrm_cmd_lock);
4968 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4969 	if (!rc)
4970 		bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
4971 							resp->l2_filter_id;
4972 	mutex_unlock(&bp->hwrm_cmd_lock);
4973 	return rc;
4974 }
4975 
bnxt_hwrm_clear_vnic_filter(struct bnxt * bp)4976 static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
4977 {
4978 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
4979 	int rc = 0;
4980 
4981 	/* Any associated ntuple filters will also be cleared by firmware. */
4982 	mutex_lock(&bp->hwrm_cmd_lock);
4983 	for (i = 0; i < num_of_vnics; i++) {
4984 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4985 
4986 		for (j = 0; j < vnic->uc_filter_count; j++) {
4987 			struct hwrm_cfa_l2_filter_free_input req = {0};
4988 
4989 			bnxt_hwrm_cmd_hdr_init(bp, &req,
4990 					       HWRM_CFA_L2_FILTER_FREE, -1, -1);
4991 
4992 			req.l2_filter_id = vnic->fw_l2_filter_id[j];
4993 
4994 			rc = _hwrm_send_message(bp, &req, sizeof(req),
4995 						HWRM_CMD_TIMEOUT);
4996 		}
4997 		vnic->uc_filter_count = 0;
4998 	}
4999 	mutex_unlock(&bp->hwrm_cmd_lock);
5000 
5001 	return rc;
5002 }
5003 
bnxt_hwrm_vnic_set_tpa(struct bnxt * bp,u16 vnic_id,u32 tpa_flags)5004 static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
5005 {
5006 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5007 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
5008 	struct hwrm_vnic_tpa_cfg_input req = {0};
5009 
5010 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
5011 		return 0;
5012 
5013 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
5014 
5015 	if (tpa_flags) {
5016 		u16 mss = bp->dev->mtu - 40;
5017 		u32 nsegs, n, segs = 0, flags;
5018 
5019 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
5020 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
5021 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
5022 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
5023 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
5024 		if (tpa_flags & BNXT_FLAG_GRO)
5025 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
5026 
5027 		req.flags = cpu_to_le32(flags);
5028 
5029 		req.enables =
5030 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
5031 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
5032 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
5033 
5034 		/* Number of segs are log2 units, and first packet is not
5035 		 * included as part of this units.
5036 		 */
5037 		if (mss <= BNXT_RX_PAGE_SIZE) {
5038 			n = BNXT_RX_PAGE_SIZE / mss;
5039 			nsegs = (MAX_SKB_FRAGS - 1) * n;
5040 		} else {
5041 			n = mss / BNXT_RX_PAGE_SIZE;
5042 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
5043 				n++;
5044 			nsegs = (MAX_SKB_FRAGS - n) / n;
5045 		}
5046 
5047 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5048 			segs = MAX_TPA_SEGS_P5;
5049 			max_aggs = bp->max_tpa;
5050 		} else {
5051 			segs = ilog2(nsegs);
5052 		}
5053 		req.max_agg_segs = cpu_to_le16(segs);
5054 		req.max_aggs = cpu_to_le16(max_aggs);
5055 
5056 		req.min_agg_len = cpu_to_le32(512);
5057 	}
5058 	req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5059 
5060 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5061 }
5062 
bnxt_cp_ring_from_grp(struct bnxt * bp,struct bnxt_ring_struct * ring)5063 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
5064 {
5065 	struct bnxt_ring_grp_info *grp_info;
5066 
5067 	grp_info = &bp->grp_info[ring->grp_idx];
5068 	return grp_info->cp_fw_ring_id;
5069 }
5070 
bnxt_cp_ring_for_rx(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)5071 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
5072 {
5073 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5074 		struct bnxt_napi *bnapi = rxr->bnapi;
5075 		struct bnxt_cp_ring_info *cpr;
5076 
5077 		cpr = bnapi->cp_ring.cp_ring_arr[BNXT_RX_HDL];
5078 		return cpr->cp_ring_struct.fw_ring_id;
5079 	} else {
5080 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
5081 	}
5082 }
5083 
bnxt_cp_ring_for_tx(struct bnxt * bp,struct bnxt_tx_ring_info * txr)5084 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
5085 {
5086 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5087 		struct bnxt_napi *bnapi = txr->bnapi;
5088 		struct bnxt_cp_ring_info *cpr;
5089 
5090 		cpr = bnapi->cp_ring.cp_ring_arr[BNXT_TX_HDL];
5091 		return cpr->cp_ring_struct.fw_ring_id;
5092 	} else {
5093 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
5094 	}
5095 }
5096 
bnxt_alloc_rss_indir_tbl(struct bnxt * bp)5097 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
5098 {
5099 	int entries;
5100 
5101 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5102 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
5103 	else
5104 		entries = HW_HASH_INDEX_SIZE;
5105 
5106 	bp->rss_indir_tbl_entries = entries;
5107 	bp->rss_indir_tbl = kmalloc_array(entries, sizeof(*bp->rss_indir_tbl),
5108 					  GFP_KERNEL);
5109 	if (!bp->rss_indir_tbl)
5110 		return -ENOMEM;
5111 	return 0;
5112 }
5113 
bnxt_set_dflt_rss_indir_tbl(struct bnxt * bp)5114 static void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp)
5115 {
5116 	u16 max_rings, max_entries, pad, i;
5117 
5118 	if (!bp->rx_nr_rings)
5119 		return;
5120 
5121 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5122 		max_rings = bp->rx_nr_rings - 1;
5123 	else
5124 		max_rings = bp->rx_nr_rings;
5125 
5126 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
5127 
5128 	for (i = 0; i < max_entries; i++)
5129 		bp->rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
5130 
5131 	pad = bp->rss_indir_tbl_entries - max_entries;
5132 	if (pad)
5133 		memset(&bp->rss_indir_tbl[i], 0, pad * sizeof(u16));
5134 }
5135 
bnxt_get_max_rss_ring(struct bnxt * bp)5136 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
5137 {
5138 	u16 i, tbl_size, max_ring = 0;
5139 
5140 	if (!bp->rss_indir_tbl)
5141 		return 0;
5142 
5143 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
5144 	for (i = 0; i < tbl_size; i++)
5145 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
5146 	return max_ring;
5147 }
5148 
bnxt_get_nr_rss_ctxs(struct bnxt * bp,int rx_rings)5149 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
5150 {
5151 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5152 		return DIV_ROUND_UP(rx_rings, BNXT_RSS_TABLE_ENTRIES_P5);
5153 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5154 		return 2;
5155 	return 1;
5156 }
5157 
__bnxt_fill_hw_rss_tbl(struct bnxt * bp,struct bnxt_vnic_info * vnic)5158 static void __bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
5159 {
5160 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
5161 	u16 i, j;
5162 
5163 	/* Fill the RSS indirection table with ring group ids */
5164 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
5165 		if (!no_rss)
5166 			j = bp->rss_indir_tbl[i];
5167 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
5168 	}
5169 }
5170 
__bnxt_fill_hw_rss_tbl_p5(struct bnxt * bp,struct bnxt_vnic_info * vnic)5171 static void __bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
5172 				      struct bnxt_vnic_info *vnic)
5173 {
5174 	__le16 *ring_tbl = vnic->rss_table;
5175 	struct bnxt_rx_ring_info *rxr;
5176 	u16 tbl_size, i;
5177 
5178 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
5179 
5180 	for (i = 0; i < tbl_size; i++) {
5181 		u16 ring_id, j;
5182 
5183 		j = bp->rss_indir_tbl[i];
5184 		rxr = &bp->rx_ring[j];
5185 
5186 		ring_id = rxr->rx_ring_struct.fw_ring_id;
5187 		*ring_tbl++ = cpu_to_le16(ring_id);
5188 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5189 		*ring_tbl++ = cpu_to_le16(ring_id);
5190 	}
5191 }
5192 
bnxt_fill_hw_rss_tbl(struct bnxt * bp,struct bnxt_vnic_info * vnic)5193 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
5194 {
5195 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5196 		__bnxt_fill_hw_rss_tbl_p5(bp, vnic);
5197 	else
5198 		__bnxt_fill_hw_rss_tbl(bp, vnic);
5199 }
5200 
bnxt_hwrm_vnic_set_rss(struct bnxt * bp,u16 vnic_id,bool set_rss)5201 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
5202 {
5203 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5204 	struct hwrm_vnic_rss_cfg_input req = {0};
5205 
5206 	if ((bp->flags & BNXT_FLAG_CHIP_P5) ||
5207 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
5208 		return 0;
5209 
5210 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
5211 	if (set_rss) {
5212 		bnxt_fill_hw_rss_tbl(bp, vnic);
5213 		req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
5214 		req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
5215 		req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
5216 		req.hash_key_tbl_addr =
5217 			cpu_to_le64(vnic->rss_hash_key_dma_addr);
5218 	}
5219 	req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5220 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5221 }
5222 
bnxt_hwrm_vnic_set_rss_p5(struct bnxt * bp,u16 vnic_id,bool set_rss)5223 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, u16 vnic_id, bool set_rss)
5224 {
5225 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5226 	struct hwrm_vnic_rss_cfg_input req = {0};
5227 	dma_addr_t ring_tbl_map;
5228 	u32 i, nr_ctxs;
5229 
5230 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
5231 	req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5232 	if (!set_rss) {
5233 		hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5234 		return 0;
5235 	}
5236 	bnxt_fill_hw_rss_tbl(bp, vnic);
5237 	req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
5238 	req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
5239 	req.hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
5240 	ring_tbl_map = vnic->rss_table_dma_addr;
5241 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
5242 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
5243 		int rc;
5244 
5245 		req.ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
5246 		req.ring_table_pair_index = i;
5247 		req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
5248 		rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5249 		if (rc)
5250 			return rc;
5251 	}
5252 	return 0;
5253 }
5254 
bnxt_hwrm_vnic_set_hds(struct bnxt * bp,u16 vnic_id)5255 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
5256 {
5257 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5258 	struct hwrm_vnic_plcmodes_cfg_input req = {0};
5259 
5260 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
5261 	req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
5262 				VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
5263 				VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
5264 	req.enables =
5265 		cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
5266 			    VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
5267 	/* thresholds not implemented in firmware yet */
5268 	req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
5269 	req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
5270 	req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5271 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5272 }
5273 
bnxt_hwrm_vnic_ctx_free_one(struct bnxt * bp,u16 vnic_id,u16 ctx_idx)5274 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
5275 					u16 ctx_idx)
5276 {
5277 	struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
5278 
5279 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
5280 	req.rss_cos_lb_ctx_id =
5281 		cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
5282 
5283 	hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5284 	bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
5285 }
5286 
bnxt_hwrm_vnic_ctx_free(struct bnxt * bp)5287 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
5288 {
5289 	int i, j;
5290 
5291 	for (i = 0; i < bp->nr_vnics; i++) {
5292 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
5293 
5294 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
5295 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
5296 				bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
5297 		}
5298 	}
5299 	bp->rsscos_nr_ctxs = 0;
5300 }
5301 
bnxt_hwrm_vnic_ctx_alloc(struct bnxt * bp,u16 vnic_id,u16 ctx_idx)5302 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
5303 {
5304 	int rc;
5305 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
5306 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
5307 						bp->hwrm_cmd_resp_addr;
5308 
5309 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
5310 			       -1);
5311 
5312 	mutex_lock(&bp->hwrm_cmd_lock);
5313 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5314 	if (!rc)
5315 		bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
5316 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
5317 	mutex_unlock(&bp->hwrm_cmd_lock);
5318 
5319 	return rc;
5320 }
5321 
bnxt_get_roce_vnic_mode(struct bnxt * bp)5322 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
5323 {
5324 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
5325 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
5326 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
5327 }
5328 
bnxt_hwrm_vnic_cfg(struct bnxt * bp,u16 vnic_id)5329 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
5330 {
5331 	unsigned int ring = 0, grp_idx;
5332 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5333 	struct hwrm_vnic_cfg_input req = {0};
5334 	u16 def_vlan = 0;
5335 
5336 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
5337 
5338 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5339 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
5340 
5341 		req.default_rx_ring_id =
5342 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
5343 		req.default_cmpl_ring_id =
5344 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
5345 		req.enables =
5346 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
5347 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
5348 		goto vnic_mru;
5349 	}
5350 	req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
5351 	/* Only RSS support for now TBD: COS & LB */
5352 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
5353 		req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5354 		req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5355 					   VNIC_CFG_REQ_ENABLES_MRU);
5356 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
5357 		req.rss_rule =
5358 			cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
5359 		req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5360 					   VNIC_CFG_REQ_ENABLES_MRU);
5361 		req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
5362 	} else {
5363 		req.rss_rule = cpu_to_le16(0xffff);
5364 	}
5365 
5366 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
5367 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
5368 		req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
5369 		req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
5370 	} else {
5371 		req.cos_rule = cpu_to_le16(0xffff);
5372 	}
5373 
5374 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
5375 		ring = 0;
5376 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
5377 		ring = vnic_id - 1;
5378 	else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
5379 		ring = bp->rx_nr_rings - 1;
5380 
5381 	grp_idx = bp->rx_ring[ring].bnapi->index;
5382 	req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
5383 	req.lb_rule = cpu_to_le16(0xffff);
5384 vnic_mru:
5385 	req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + VLAN_HLEN);
5386 
5387 	req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5388 #ifdef CONFIG_BNXT_SRIOV
5389 	if (BNXT_VF(bp))
5390 		def_vlan = bp->vf.vlan;
5391 #endif
5392 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
5393 		req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
5394 	if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
5395 		req.flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
5396 
5397 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5398 }
5399 
bnxt_hwrm_vnic_free_one(struct bnxt * bp,u16 vnic_id)5400 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
5401 {
5402 	if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
5403 		struct hwrm_vnic_free_input req = {0};
5404 
5405 		bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
5406 		req.vnic_id =
5407 			cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
5408 
5409 		hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5410 		bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
5411 	}
5412 }
5413 
bnxt_hwrm_vnic_free(struct bnxt * bp)5414 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
5415 {
5416 	u16 i;
5417 
5418 	for (i = 0; i < bp->nr_vnics; i++)
5419 		bnxt_hwrm_vnic_free_one(bp, i);
5420 }
5421 
bnxt_hwrm_vnic_alloc(struct bnxt * bp,u16 vnic_id,unsigned int start_rx_ring_idx,unsigned int nr_rings)5422 static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
5423 				unsigned int start_rx_ring_idx,
5424 				unsigned int nr_rings)
5425 {
5426 	int rc = 0;
5427 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
5428 	struct hwrm_vnic_alloc_input req = {0};
5429 	struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5430 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5431 
5432 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5433 		goto vnic_no_ring_grps;
5434 
5435 	/* map ring groups to this vnic */
5436 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
5437 		grp_idx = bp->rx_ring[i].bnapi->index;
5438 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
5439 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
5440 				   j, nr_rings);
5441 			break;
5442 		}
5443 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
5444 	}
5445 
5446 vnic_no_ring_grps:
5447 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
5448 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
5449 	if (vnic_id == 0)
5450 		req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
5451 
5452 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
5453 
5454 	mutex_lock(&bp->hwrm_cmd_lock);
5455 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5456 	if (!rc)
5457 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
5458 	mutex_unlock(&bp->hwrm_cmd_lock);
5459 	return rc;
5460 }
5461 
bnxt_hwrm_vnic_qcaps(struct bnxt * bp)5462 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
5463 {
5464 	struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5465 	struct hwrm_vnic_qcaps_input req = {0};
5466 	int rc;
5467 
5468 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
5469 	bp->flags &= ~(BNXT_FLAG_NEW_RSS_CAP | BNXT_FLAG_ROCE_MIRROR_CAP);
5470 	if (bp->hwrm_spec_code < 0x10600)
5471 		return 0;
5472 
5473 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
5474 	mutex_lock(&bp->hwrm_cmd_lock);
5475 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5476 	if (!rc) {
5477 		u32 flags = le32_to_cpu(resp->flags);
5478 
5479 		if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
5480 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
5481 			bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
5482 		if (flags &
5483 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
5484 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
5485 
5486 		/* Older P5 fw before EXT_HW_STATS support did not set
5487 		 * VLAN_STRIP_CAP properly.
5488 		 */
5489 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
5490 		    (BNXT_CHIP_P5_THOR(bp) &&
5491 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
5492 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
5493 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
5494 		if (bp->max_tpa_v2) {
5495 			if (BNXT_CHIP_P5_THOR(bp))
5496 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
5497 			else
5498 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5_SR2;
5499 		}
5500 	}
5501 	mutex_unlock(&bp->hwrm_cmd_lock);
5502 	return rc;
5503 }
5504 
bnxt_hwrm_ring_grp_alloc(struct bnxt * bp)5505 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
5506 {
5507 	u16 i;
5508 	u32 rc = 0;
5509 
5510 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5511 		return 0;
5512 
5513 	mutex_lock(&bp->hwrm_cmd_lock);
5514 	for (i = 0; i < bp->rx_nr_rings; i++) {
5515 		struct hwrm_ring_grp_alloc_input req = {0};
5516 		struct hwrm_ring_grp_alloc_output *resp =
5517 					bp->hwrm_cmd_resp_addr;
5518 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
5519 
5520 		bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
5521 
5522 		req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
5523 		req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
5524 		req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
5525 		req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
5526 
5527 		rc = _hwrm_send_message(bp, &req, sizeof(req),
5528 					HWRM_CMD_TIMEOUT);
5529 		if (rc)
5530 			break;
5531 
5532 		bp->grp_info[grp_idx].fw_grp_id =
5533 			le32_to_cpu(resp->ring_group_id);
5534 	}
5535 	mutex_unlock(&bp->hwrm_cmd_lock);
5536 	return rc;
5537 }
5538 
bnxt_hwrm_ring_grp_free(struct bnxt * bp)5539 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
5540 {
5541 	u16 i;
5542 	struct hwrm_ring_grp_free_input req = {0};
5543 
5544 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5))
5545 		return;
5546 
5547 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
5548 
5549 	mutex_lock(&bp->hwrm_cmd_lock);
5550 	for (i = 0; i < bp->cp_nr_rings; i++) {
5551 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
5552 			continue;
5553 		req.ring_group_id =
5554 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
5555 
5556 		_hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5557 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
5558 	}
5559 	mutex_unlock(&bp->hwrm_cmd_lock);
5560 }
5561 
hwrm_ring_alloc_send_msg(struct bnxt * bp,struct bnxt_ring_struct * ring,u32 ring_type,u32 map_index)5562 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
5563 				    struct bnxt_ring_struct *ring,
5564 				    u32 ring_type, u32 map_index)
5565 {
5566 	int rc = 0, err = 0;
5567 	struct hwrm_ring_alloc_input req = {0};
5568 	struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5569 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
5570 	struct bnxt_ring_grp_info *grp_info;
5571 	u16 ring_id;
5572 
5573 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
5574 
5575 	req.enables = 0;
5576 	if (rmem->nr_pages > 1) {
5577 		req.page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
5578 		/* Page size is in log2 units */
5579 		req.page_size = BNXT_PAGE_SHIFT;
5580 		req.page_tbl_depth = 1;
5581 	} else {
5582 		req.page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
5583 	}
5584 	req.fbo = 0;
5585 	/* Association of ring index with doorbell index and MSIX number */
5586 	req.logical_id = cpu_to_le16(map_index);
5587 
5588 	switch (ring_type) {
5589 	case HWRM_RING_ALLOC_TX: {
5590 		struct bnxt_tx_ring_info *txr;
5591 
5592 		txr = container_of(ring, struct bnxt_tx_ring_info,
5593 				   tx_ring_struct);
5594 		req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
5595 		/* Association of transmit ring with completion ring */
5596 		grp_info = &bp->grp_info[ring->grp_idx];
5597 		req.cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
5598 		req.length = cpu_to_le32(bp->tx_ring_mask + 1);
5599 		req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5600 		req.queue_id = cpu_to_le16(ring->queue_id);
5601 		break;
5602 	}
5603 	case HWRM_RING_ALLOC_RX:
5604 		req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5605 		req.length = cpu_to_le32(bp->rx_ring_mask + 1);
5606 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5607 			u16 flags = 0;
5608 
5609 			/* Association of rx ring with stats context */
5610 			grp_info = &bp->grp_info[ring->grp_idx];
5611 			req.rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
5612 			req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5613 			req.enables |= cpu_to_le32(
5614 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5615 			if (NET_IP_ALIGN == 2)
5616 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
5617 			req.flags = cpu_to_le16(flags);
5618 		}
5619 		break;
5620 	case HWRM_RING_ALLOC_AGG:
5621 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5622 			req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
5623 			/* Association of agg ring with rx ring */
5624 			grp_info = &bp->grp_info[ring->grp_idx];
5625 			req.rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
5626 			req.rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
5627 			req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5628 			req.enables |= cpu_to_le32(
5629 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
5630 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5631 		} else {
5632 			req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5633 		}
5634 		req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
5635 		break;
5636 	case HWRM_RING_ALLOC_CMPL:
5637 		req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
5638 		req.length = cpu_to_le32(bp->cp_ring_mask + 1);
5639 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5640 			/* Association of cp ring with nq */
5641 			grp_info = &bp->grp_info[map_index];
5642 			req.nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
5643 			req.cq_handle = cpu_to_le64(ring->handle);
5644 			req.enables |= cpu_to_le32(
5645 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
5646 		} else if (bp->flags & BNXT_FLAG_USING_MSIX) {
5647 			req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5648 		}
5649 		break;
5650 	case HWRM_RING_ALLOC_NQ:
5651 		req.ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
5652 		req.length = cpu_to_le32(bp->cp_ring_mask + 1);
5653 		if (bp->flags & BNXT_FLAG_USING_MSIX)
5654 			req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5655 		break;
5656 	default:
5657 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
5658 			   ring_type);
5659 		return -1;
5660 	}
5661 
5662 	mutex_lock(&bp->hwrm_cmd_lock);
5663 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5664 	err = le16_to_cpu(resp->error_code);
5665 	ring_id = le16_to_cpu(resp->ring_id);
5666 	mutex_unlock(&bp->hwrm_cmd_lock);
5667 
5668 	if (rc || err) {
5669 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
5670 			   ring_type, rc, err);
5671 		return -EIO;
5672 	}
5673 	ring->fw_ring_id = ring_id;
5674 	return rc;
5675 }
5676 
bnxt_hwrm_set_async_event_cr(struct bnxt * bp,int idx)5677 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
5678 {
5679 	int rc;
5680 
5681 	if (BNXT_PF(bp)) {
5682 		struct hwrm_func_cfg_input req = {0};
5683 
5684 		bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
5685 		req.fid = cpu_to_le16(0xffff);
5686 		req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5687 		req.async_event_cr = cpu_to_le16(idx);
5688 		rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5689 	} else {
5690 		struct hwrm_func_vf_cfg_input req = {0};
5691 
5692 		bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
5693 		req.enables =
5694 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5695 		req.async_event_cr = cpu_to_le16(idx);
5696 		rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5697 	}
5698 	return rc;
5699 }
5700 
bnxt_set_db(struct bnxt * bp,struct bnxt_db_info * db,u32 ring_type,u32 map_idx,u32 xid)5701 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
5702 			u32 map_idx, u32 xid)
5703 {
5704 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5705 		if (BNXT_PF(bp))
5706 			db->doorbell = bp->bar1 + DB_PF_OFFSET_P5;
5707 		else
5708 			db->doorbell = bp->bar1 + DB_VF_OFFSET_P5;
5709 		switch (ring_type) {
5710 		case HWRM_RING_ALLOC_TX:
5711 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
5712 			break;
5713 		case HWRM_RING_ALLOC_RX:
5714 		case HWRM_RING_ALLOC_AGG:
5715 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
5716 			break;
5717 		case HWRM_RING_ALLOC_CMPL:
5718 			db->db_key64 = DBR_PATH_L2;
5719 			break;
5720 		case HWRM_RING_ALLOC_NQ:
5721 			db->db_key64 = DBR_PATH_L2;
5722 			break;
5723 		}
5724 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
5725 	} else {
5726 		db->doorbell = bp->bar1 + map_idx * 0x80;
5727 		switch (ring_type) {
5728 		case HWRM_RING_ALLOC_TX:
5729 			db->db_key32 = DB_KEY_TX;
5730 			break;
5731 		case HWRM_RING_ALLOC_RX:
5732 		case HWRM_RING_ALLOC_AGG:
5733 			db->db_key32 = DB_KEY_RX;
5734 			break;
5735 		case HWRM_RING_ALLOC_CMPL:
5736 			db->db_key32 = DB_KEY_CP;
5737 			break;
5738 		}
5739 	}
5740 }
5741 
bnxt_hwrm_ring_alloc(struct bnxt * bp)5742 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
5743 {
5744 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
5745 	int i, rc = 0;
5746 	u32 type;
5747 
5748 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5749 		type = HWRM_RING_ALLOC_NQ;
5750 	else
5751 		type = HWRM_RING_ALLOC_CMPL;
5752 	for (i = 0; i < bp->cp_nr_rings; i++) {
5753 		struct bnxt_napi *bnapi = bp->bnapi[i];
5754 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5755 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5756 		u32 map_idx = ring->map_idx;
5757 		unsigned int vector;
5758 
5759 		vector = bp->irq_tbl[map_idx].vector;
5760 		disable_irq_nosync(vector);
5761 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5762 		if (rc) {
5763 			enable_irq(vector);
5764 			goto err_out;
5765 		}
5766 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
5767 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5768 		enable_irq(vector);
5769 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
5770 
5771 		if (!i) {
5772 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
5773 			if (rc)
5774 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
5775 		}
5776 	}
5777 
5778 	type = HWRM_RING_ALLOC_TX;
5779 	for (i = 0; i < bp->tx_nr_rings; i++) {
5780 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5781 		struct bnxt_ring_struct *ring;
5782 		u32 map_idx;
5783 
5784 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5785 			struct bnxt_napi *bnapi = txr->bnapi;
5786 			struct bnxt_cp_ring_info *cpr, *cpr2;
5787 			u32 type2 = HWRM_RING_ALLOC_CMPL;
5788 
5789 			cpr = &bnapi->cp_ring;
5790 			cpr2 = cpr->cp_ring_arr[BNXT_TX_HDL];
5791 			ring = &cpr2->cp_ring_struct;
5792 			ring->handle = BNXT_TX_HDL;
5793 			map_idx = bnapi->index;
5794 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5795 			if (rc)
5796 				goto err_out;
5797 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5798 				    ring->fw_ring_id);
5799 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5800 		}
5801 		ring = &txr->tx_ring_struct;
5802 		map_idx = i;
5803 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5804 		if (rc)
5805 			goto err_out;
5806 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
5807 	}
5808 
5809 	type = HWRM_RING_ALLOC_RX;
5810 	for (i = 0; i < bp->rx_nr_rings; i++) {
5811 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5812 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5813 		struct bnxt_napi *bnapi = rxr->bnapi;
5814 		u32 map_idx = bnapi->index;
5815 
5816 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5817 		if (rc)
5818 			goto err_out;
5819 		bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
5820 		/* If we have agg rings, post agg buffers first. */
5821 		if (!agg_rings)
5822 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
5823 		bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
5824 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5825 			struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5826 			u32 type2 = HWRM_RING_ALLOC_CMPL;
5827 			struct bnxt_cp_ring_info *cpr2;
5828 
5829 			cpr2 = cpr->cp_ring_arr[BNXT_RX_HDL];
5830 			ring = &cpr2->cp_ring_struct;
5831 			ring->handle = BNXT_RX_HDL;
5832 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5833 			if (rc)
5834 				goto err_out;
5835 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5836 				    ring->fw_ring_id);
5837 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5838 		}
5839 	}
5840 
5841 	if (agg_rings) {
5842 		type = HWRM_RING_ALLOC_AGG;
5843 		for (i = 0; i < bp->rx_nr_rings; i++) {
5844 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5845 			struct bnxt_ring_struct *ring =
5846 						&rxr->rx_agg_ring_struct;
5847 			u32 grp_idx = ring->grp_idx;
5848 			u32 map_idx = grp_idx + bp->rx_nr_rings;
5849 
5850 			rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5851 			if (rc)
5852 				goto err_out;
5853 
5854 			bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
5855 				    ring->fw_ring_id);
5856 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
5857 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
5858 			bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
5859 		}
5860 	}
5861 err_out:
5862 	return rc;
5863 }
5864 
hwrm_ring_free_send_msg(struct bnxt * bp,struct bnxt_ring_struct * ring,u32 ring_type,int cmpl_ring_id)5865 static int hwrm_ring_free_send_msg(struct bnxt *bp,
5866 				   struct bnxt_ring_struct *ring,
5867 				   u32 ring_type, int cmpl_ring_id)
5868 {
5869 	int rc;
5870 	struct hwrm_ring_free_input req = {0};
5871 	struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
5872 	u16 error_code;
5873 
5874 	if (BNXT_NO_FW_ACCESS(bp))
5875 		return 0;
5876 
5877 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
5878 	req.ring_type = ring_type;
5879 	req.ring_id = cpu_to_le16(ring->fw_ring_id);
5880 
5881 	mutex_lock(&bp->hwrm_cmd_lock);
5882 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5883 	error_code = le16_to_cpu(resp->error_code);
5884 	mutex_unlock(&bp->hwrm_cmd_lock);
5885 
5886 	if (rc || error_code) {
5887 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
5888 			   ring_type, rc, error_code);
5889 		return -EIO;
5890 	}
5891 	return 0;
5892 }
5893 
bnxt_hwrm_ring_free(struct bnxt * bp,bool close_path)5894 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
5895 {
5896 	u32 type;
5897 	int i;
5898 
5899 	if (!bp->bnapi)
5900 		return;
5901 
5902 	for (i = 0; i < bp->tx_nr_rings; i++) {
5903 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5904 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
5905 
5906 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5907 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
5908 
5909 			hwrm_ring_free_send_msg(bp, ring,
5910 						RING_FREE_REQ_RING_TYPE_TX,
5911 						close_path ? cmpl_ring_id :
5912 						INVALID_HW_RING_ID);
5913 			ring->fw_ring_id = INVALID_HW_RING_ID;
5914 		}
5915 	}
5916 
5917 	for (i = 0; i < bp->rx_nr_rings; i++) {
5918 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5919 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5920 		u32 grp_idx = rxr->bnapi->index;
5921 
5922 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5923 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5924 
5925 			hwrm_ring_free_send_msg(bp, ring,
5926 						RING_FREE_REQ_RING_TYPE_RX,
5927 						close_path ? cmpl_ring_id :
5928 						INVALID_HW_RING_ID);
5929 			ring->fw_ring_id = INVALID_HW_RING_ID;
5930 			bp->grp_info[grp_idx].rx_fw_ring_id =
5931 				INVALID_HW_RING_ID;
5932 		}
5933 	}
5934 
5935 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5936 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
5937 	else
5938 		type = RING_FREE_REQ_RING_TYPE_RX;
5939 	for (i = 0; i < bp->rx_nr_rings; i++) {
5940 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5941 		struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
5942 		u32 grp_idx = rxr->bnapi->index;
5943 
5944 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5945 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5946 
5947 			hwrm_ring_free_send_msg(bp, ring, type,
5948 						close_path ? cmpl_ring_id :
5949 						INVALID_HW_RING_ID);
5950 			ring->fw_ring_id = INVALID_HW_RING_ID;
5951 			bp->grp_info[grp_idx].agg_fw_ring_id =
5952 				INVALID_HW_RING_ID;
5953 		}
5954 	}
5955 
5956 	/* The completion rings are about to be freed.  After that the
5957 	 * IRQ doorbell will not work anymore.  So we need to disable
5958 	 * IRQ here.
5959 	 */
5960 	bnxt_disable_int_sync(bp);
5961 
5962 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5963 		type = RING_FREE_REQ_RING_TYPE_NQ;
5964 	else
5965 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
5966 	for (i = 0; i < bp->cp_nr_rings; i++) {
5967 		struct bnxt_napi *bnapi = bp->bnapi[i];
5968 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5969 		struct bnxt_ring_struct *ring;
5970 		int j;
5971 
5972 		for (j = 0; j < 2; j++) {
5973 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
5974 
5975 			if (cpr2) {
5976 				ring = &cpr2->cp_ring_struct;
5977 				if (ring->fw_ring_id == INVALID_HW_RING_ID)
5978 					continue;
5979 				hwrm_ring_free_send_msg(bp, ring,
5980 					RING_FREE_REQ_RING_TYPE_L2_CMPL,
5981 					INVALID_HW_RING_ID);
5982 				ring->fw_ring_id = INVALID_HW_RING_ID;
5983 			}
5984 		}
5985 		ring = &cpr->cp_ring_struct;
5986 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5987 			hwrm_ring_free_send_msg(bp, ring, type,
5988 						INVALID_HW_RING_ID);
5989 			ring->fw_ring_id = INVALID_HW_RING_ID;
5990 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
5991 		}
5992 	}
5993 }
5994 
5995 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5996 			   bool shared);
5997 
bnxt_hwrm_get_rings(struct bnxt * bp)5998 static int bnxt_hwrm_get_rings(struct bnxt *bp)
5999 {
6000 	struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6001 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6002 	struct hwrm_func_qcfg_input req = {0};
6003 	int rc;
6004 
6005 	if (bp->hwrm_spec_code < 0x10601)
6006 		return 0;
6007 
6008 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
6009 	req.fid = cpu_to_le16(0xffff);
6010 	mutex_lock(&bp->hwrm_cmd_lock);
6011 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6012 	if (rc) {
6013 		mutex_unlock(&bp->hwrm_cmd_lock);
6014 		return rc;
6015 	}
6016 
6017 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
6018 	if (BNXT_NEW_RM(bp)) {
6019 		u16 cp, stats;
6020 
6021 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
6022 		hw_resc->resv_hw_ring_grps =
6023 			le32_to_cpu(resp->alloc_hw_ring_grps);
6024 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
6025 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
6026 		stats = le16_to_cpu(resp->alloc_stat_ctx);
6027 		hw_resc->resv_irqs = cp;
6028 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
6029 			int rx = hw_resc->resv_rx_rings;
6030 			int tx = hw_resc->resv_tx_rings;
6031 
6032 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
6033 				rx >>= 1;
6034 			if (cp < (rx + tx)) {
6035 				bnxt_trim_rings(bp, &rx, &tx, cp, false);
6036 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
6037 					rx <<= 1;
6038 				hw_resc->resv_rx_rings = rx;
6039 				hw_resc->resv_tx_rings = tx;
6040 			}
6041 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
6042 			hw_resc->resv_hw_ring_grps = rx;
6043 		}
6044 		hw_resc->resv_cp_rings = cp;
6045 		hw_resc->resv_stat_ctxs = stats;
6046 	}
6047 	mutex_unlock(&bp->hwrm_cmd_lock);
6048 	return 0;
6049 }
6050 
6051 /* Caller must hold bp->hwrm_cmd_lock */
__bnxt_hwrm_get_tx_rings(struct bnxt * bp,u16 fid,int * tx_rings)6052 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
6053 {
6054 	struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6055 	struct hwrm_func_qcfg_input req = {0};
6056 	int rc;
6057 
6058 	if (bp->hwrm_spec_code < 0x10601)
6059 		return 0;
6060 
6061 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
6062 	req.fid = cpu_to_le16(fid);
6063 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6064 	if (!rc)
6065 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
6066 
6067 	return rc;
6068 }
6069 
6070 static bool bnxt_rfs_supported(struct bnxt *bp);
6071 
6072 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)6073 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct hwrm_func_cfg_input *req,
6074 			     int tx_rings, int rx_rings, int ring_grps,
6075 			     int cp_rings, int stats, int vnics)
6076 {
6077 	u32 enables = 0;
6078 
6079 	bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_CFG, -1, -1);
6080 	req->fid = cpu_to_le16(0xffff);
6081 	enables |= tx_rings ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
6082 	req->num_tx_rings = cpu_to_le16(tx_rings);
6083 	if (BNXT_NEW_RM(bp)) {
6084 		enables |= rx_rings ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
6085 		enables |= stats ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
6086 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
6087 			enables |= cp_rings ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
6088 			enables |= tx_rings + ring_grps ?
6089 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6090 			enables |= rx_rings ?
6091 				FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6092 		} else {
6093 			enables |= cp_rings ?
6094 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6095 			enables |= ring_grps ?
6096 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS |
6097 				   FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6098 		}
6099 		enables |= vnics ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
6100 
6101 		req->num_rx_rings = cpu_to_le16(rx_rings);
6102 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
6103 			req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
6104 			req->num_msix = cpu_to_le16(cp_rings);
6105 			req->num_rsscos_ctxs =
6106 				cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
6107 		} else {
6108 			req->num_cmpl_rings = cpu_to_le16(cp_rings);
6109 			req->num_hw_ring_grps = cpu_to_le16(ring_grps);
6110 			req->num_rsscos_ctxs = cpu_to_le16(1);
6111 			if (!(bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
6112 			    bnxt_rfs_supported(bp))
6113 				req->num_rsscos_ctxs =
6114 					cpu_to_le16(ring_grps + 1);
6115 		}
6116 		req->num_stat_ctxs = cpu_to_le16(stats);
6117 		req->num_vnics = cpu_to_le16(vnics);
6118 	}
6119 	req->enables = cpu_to_le32(enables);
6120 }
6121 
6122 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)6123 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp,
6124 			     struct hwrm_func_vf_cfg_input *req, int tx_rings,
6125 			     int rx_rings, int ring_grps, int cp_rings,
6126 			     int stats, int vnics)
6127 {
6128 	u32 enables = 0;
6129 
6130 	bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_VF_CFG, -1, -1);
6131 	enables |= tx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
6132 	enables |= rx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
6133 			      FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6134 	enables |= stats ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
6135 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
6136 		enables |= tx_rings + ring_grps ?
6137 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6138 	} else {
6139 		enables |= cp_rings ?
6140 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6141 		enables |= ring_grps ?
6142 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
6143 	}
6144 	enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
6145 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
6146 
6147 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
6148 	req->num_tx_rings = cpu_to_le16(tx_rings);
6149 	req->num_rx_rings = cpu_to_le16(rx_rings);
6150 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
6151 		req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
6152 		req->num_rsscos_ctxs = cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
6153 	} else {
6154 		req->num_cmpl_rings = cpu_to_le16(cp_rings);
6155 		req->num_hw_ring_grps = cpu_to_le16(ring_grps);
6156 		req->num_rsscos_ctxs = cpu_to_le16(BNXT_VF_MAX_RSS_CTX);
6157 	}
6158 	req->num_stat_ctxs = cpu_to_le16(stats);
6159 	req->num_vnics = cpu_to_le16(vnics);
6160 
6161 	req->enables = cpu_to_le32(enables);
6162 }
6163 
6164 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)6165 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6166 			   int ring_grps, int cp_rings, int stats, int vnics)
6167 {
6168 	struct hwrm_func_cfg_input req = {0};
6169 	int rc;
6170 
6171 	__bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6172 				     cp_rings, stats, vnics);
6173 	if (!req.enables)
6174 		return 0;
6175 
6176 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6177 	if (rc)
6178 		return rc;
6179 
6180 	if (bp->hwrm_spec_code < 0x10601)
6181 		bp->hw_resc.resv_tx_rings = tx_rings;
6182 
6183 	return bnxt_hwrm_get_rings(bp);
6184 }
6185 
6186 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)6187 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6188 			   int ring_grps, int cp_rings, int stats, int vnics)
6189 {
6190 	struct hwrm_func_vf_cfg_input req = {0};
6191 	int rc;
6192 
6193 	if (!BNXT_NEW_RM(bp)) {
6194 		bp->hw_resc.resv_tx_rings = tx_rings;
6195 		return 0;
6196 	}
6197 
6198 	__bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6199 				     cp_rings, stats, vnics);
6200 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6201 	if (rc)
6202 		return rc;
6203 
6204 	return bnxt_hwrm_get_rings(bp);
6205 }
6206 
bnxt_hwrm_reserve_rings(struct bnxt * bp,int tx,int rx,int grp,int cp,int stat,int vnic)6207 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, int tx, int rx, int grp,
6208 				   int cp, int stat, int vnic)
6209 {
6210 	if (BNXT_PF(bp))
6211 		return bnxt_hwrm_reserve_pf_rings(bp, tx, rx, grp, cp, stat,
6212 						  vnic);
6213 	else
6214 		return bnxt_hwrm_reserve_vf_rings(bp, tx, rx, grp, cp, stat,
6215 						  vnic);
6216 }
6217 
bnxt_nq_rings_in_use(struct bnxt * bp)6218 int bnxt_nq_rings_in_use(struct bnxt *bp)
6219 {
6220 	int cp = bp->cp_nr_rings;
6221 	int ulp_msix, ulp_base;
6222 
6223 	ulp_msix = bnxt_get_ulp_msix_num(bp);
6224 	if (ulp_msix) {
6225 		ulp_base = bnxt_get_ulp_msix_base(bp);
6226 		cp += ulp_msix;
6227 		if ((ulp_base + ulp_msix) > cp)
6228 			cp = ulp_base + ulp_msix;
6229 	}
6230 	return cp;
6231 }
6232 
bnxt_cp_rings_in_use(struct bnxt * bp)6233 static int bnxt_cp_rings_in_use(struct bnxt *bp)
6234 {
6235 	int cp;
6236 
6237 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6238 		return bnxt_nq_rings_in_use(bp);
6239 
6240 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
6241 	return cp;
6242 }
6243 
bnxt_get_func_stat_ctxs(struct bnxt * bp)6244 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
6245 {
6246 	int ulp_stat = bnxt_get_ulp_stat_ctxs(bp);
6247 	int cp = bp->cp_nr_rings;
6248 
6249 	if (!ulp_stat)
6250 		return cp;
6251 
6252 	if (bnxt_nq_rings_in_use(bp) > cp + bnxt_get_ulp_msix_num(bp))
6253 		return bnxt_get_ulp_msix_base(bp) + ulp_stat;
6254 
6255 	return cp + ulp_stat;
6256 }
6257 
6258 /* Check if a default RSS map needs to be setup.  This function is only
6259  * used on older firmware that does not require reserving RX rings.
6260  */
bnxt_check_rss_tbl_no_rmgr(struct bnxt * bp)6261 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
6262 {
6263 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6264 
6265 	/* The RSS map is valid for RX rings set to resv_rx_rings */
6266 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
6267 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
6268 		if (!netif_is_rxfh_configured(bp->dev))
6269 			bnxt_set_dflt_rss_indir_tbl(bp);
6270 	}
6271 }
6272 
bnxt_need_reserve_rings(struct bnxt * bp)6273 static bool bnxt_need_reserve_rings(struct bnxt *bp)
6274 {
6275 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6276 	int cp = bnxt_cp_rings_in_use(bp);
6277 	int nq = bnxt_nq_rings_in_use(bp);
6278 	int rx = bp->rx_nr_rings, stat;
6279 	int vnic = 1, grp = rx;
6280 
6281 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
6282 	    bp->hwrm_spec_code >= 0x10601)
6283 		return true;
6284 
6285 	/* Old firmware does not need RX ring reservations but we still
6286 	 * need to setup a default RSS map when needed.  With new firmware
6287 	 * we go through RX ring reservations first and then set up the
6288 	 * RSS map for the successfully reserved RX rings when needed.
6289 	 */
6290 	if (!BNXT_NEW_RM(bp)) {
6291 		bnxt_check_rss_tbl_no_rmgr(bp);
6292 		return false;
6293 	}
6294 	if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
6295 		vnic = rx + 1;
6296 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
6297 		rx <<= 1;
6298 	stat = bnxt_get_func_stat_ctxs(bp);
6299 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
6300 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
6301 	    (hw_resc->resv_hw_ring_grps != grp &&
6302 	     !(bp->flags & BNXT_FLAG_CHIP_P5)))
6303 		return true;
6304 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && BNXT_PF(bp) &&
6305 	    hw_resc->resv_irqs != nq)
6306 		return true;
6307 	return false;
6308 }
6309 
__bnxt_reserve_rings(struct bnxt * bp)6310 static int __bnxt_reserve_rings(struct bnxt *bp)
6311 {
6312 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6313 	int cp = bnxt_nq_rings_in_use(bp);
6314 	int tx = bp->tx_nr_rings;
6315 	int rx = bp->rx_nr_rings;
6316 	int grp, rx_rings, rc;
6317 	int vnic = 1, stat;
6318 	bool sh = false;
6319 
6320 	if (!bnxt_need_reserve_rings(bp))
6321 		return 0;
6322 
6323 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6324 		sh = true;
6325 	if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
6326 		vnic = rx + 1;
6327 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
6328 		rx <<= 1;
6329 	grp = bp->rx_nr_rings;
6330 	stat = bnxt_get_func_stat_ctxs(bp);
6331 
6332 	rc = bnxt_hwrm_reserve_rings(bp, tx, rx, grp, cp, stat, vnic);
6333 	if (rc)
6334 		return rc;
6335 
6336 	tx = hw_resc->resv_tx_rings;
6337 	if (BNXT_NEW_RM(bp)) {
6338 		rx = hw_resc->resv_rx_rings;
6339 		cp = hw_resc->resv_irqs;
6340 		grp = hw_resc->resv_hw_ring_grps;
6341 		vnic = hw_resc->resv_vnics;
6342 		stat = hw_resc->resv_stat_ctxs;
6343 	}
6344 
6345 	rx_rings = rx;
6346 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
6347 		if (rx >= 2) {
6348 			rx_rings = rx >> 1;
6349 		} else {
6350 			if (netif_running(bp->dev))
6351 				return -ENOMEM;
6352 
6353 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
6354 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
6355 			bp->dev->hw_features &= ~NETIF_F_LRO;
6356 			bp->dev->features &= ~NETIF_F_LRO;
6357 			bnxt_set_ring_params(bp);
6358 		}
6359 	}
6360 	rx_rings = min_t(int, rx_rings, grp);
6361 	cp = min_t(int, cp, bp->cp_nr_rings);
6362 	if (stat > bnxt_get_ulp_stat_ctxs(bp))
6363 		stat -= bnxt_get_ulp_stat_ctxs(bp);
6364 	cp = min_t(int, cp, stat);
6365 	rc = bnxt_trim_rings(bp, &rx_rings, &tx, cp, sh);
6366 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
6367 		rx = rx_rings << 1;
6368 	cp = sh ? max_t(int, tx, rx_rings) : tx + rx_rings;
6369 	bp->tx_nr_rings = tx;
6370 
6371 	/* If we cannot reserve all the RX rings, reset the RSS map only
6372 	 * if absolutely necessary
6373 	 */
6374 	if (rx_rings != bp->rx_nr_rings) {
6375 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
6376 			    rx_rings, bp->rx_nr_rings);
6377 		if ((bp->dev->priv_flags & IFF_RXFH_CONFIGURED) &&
6378 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
6379 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
6380 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
6381 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
6382 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
6383 		}
6384 	}
6385 	bp->rx_nr_rings = rx_rings;
6386 	bp->cp_nr_rings = cp;
6387 
6388 	if (!tx || !rx || !cp || !grp || !vnic || !stat)
6389 		return -ENOMEM;
6390 
6391 	if (!netif_is_rxfh_configured(bp->dev))
6392 		bnxt_set_dflt_rss_indir_tbl(bp);
6393 
6394 	return rc;
6395 }
6396 
bnxt_hwrm_check_vf_rings(struct bnxt * bp,int tx_rings,int rx_rings,int ring_grps,int cp_rings,int stats,int vnics)6397 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6398 				    int ring_grps, int cp_rings, int stats,
6399 				    int vnics)
6400 {
6401 	struct hwrm_func_vf_cfg_input req = {0};
6402 	u32 flags;
6403 
6404 	if (!BNXT_NEW_RM(bp))
6405 		return 0;
6406 
6407 	__bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6408 				     cp_rings, stats, vnics);
6409 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
6410 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6411 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6412 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6413 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
6414 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
6415 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6416 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6417 
6418 	req.flags = cpu_to_le32(flags);
6419 	return hwrm_send_message_silent(bp, &req, sizeof(req),
6420 					HWRM_CMD_TIMEOUT);
6421 }
6422 
bnxt_hwrm_check_pf_rings(struct bnxt * bp,int tx_rings,int rx_rings,int ring_grps,int cp_rings,int stats,int vnics)6423 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6424 				    int ring_grps, int cp_rings, int stats,
6425 				    int vnics)
6426 {
6427 	struct hwrm_func_cfg_input req = {0};
6428 	u32 flags;
6429 
6430 	__bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6431 				     cp_rings, stats, vnics);
6432 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
6433 	if (BNXT_NEW_RM(bp)) {
6434 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6435 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6436 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6437 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
6438 		if (bp->flags & BNXT_FLAG_CHIP_P5)
6439 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
6440 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
6441 		else
6442 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6443 	}
6444 
6445 	req.flags = cpu_to_le32(flags);
6446 	return hwrm_send_message_silent(bp, &req, sizeof(req),
6447 					HWRM_CMD_TIMEOUT);
6448 }
6449 
bnxt_hwrm_check_rings(struct bnxt * bp,int tx_rings,int rx_rings,int ring_grps,int cp_rings,int stats,int vnics)6450 static int bnxt_hwrm_check_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6451 				 int ring_grps, int cp_rings, int stats,
6452 				 int vnics)
6453 {
6454 	if (bp->hwrm_spec_code < 0x10801)
6455 		return 0;
6456 
6457 	if (BNXT_PF(bp))
6458 		return bnxt_hwrm_check_pf_rings(bp, tx_rings, rx_rings,
6459 						ring_grps, cp_rings, stats,
6460 						vnics);
6461 
6462 	return bnxt_hwrm_check_vf_rings(bp, tx_rings, rx_rings, ring_grps,
6463 					cp_rings, stats, vnics);
6464 }
6465 
bnxt_hwrm_coal_params_qcaps(struct bnxt * bp)6466 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
6467 {
6468 	struct hwrm_ring_aggint_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6469 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6470 	struct hwrm_ring_aggint_qcaps_input req = {0};
6471 	int rc;
6472 
6473 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
6474 	coal_cap->num_cmpl_dma_aggr_max = 63;
6475 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
6476 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
6477 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
6478 	coal_cap->int_lat_tmr_min_max = 65535;
6479 	coal_cap->int_lat_tmr_max_max = 65535;
6480 	coal_cap->num_cmpl_aggr_int_max = 65535;
6481 	coal_cap->timer_units = 80;
6482 
6483 	if (bp->hwrm_spec_code < 0x10902)
6484 		return;
6485 
6486 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_AGGINT_QCAPS, -1, -1);
6487 	mutex_lock(&bp->hwrm_cmd_lock);
6488 	rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6489 	if (!rc) {
6490 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
6491 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
6492 		coal_cap->num_cmpl_dma_aggr_max =
6493 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
6494 		coal_cap->num_cmpl_dma_aggr_during_int_max =
6495 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
6496 		coal_cap->cmpl_aggr_dma_tmr_max =
6497 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
6498 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
6499 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
6500 		coal_cap->int_lat_tmr_min_max =
6501 			le16_to_cpu(resp->int_lat_tmr_min_max);
6502 		coal_cap->int_lat_tmr_max_max =
6503 			le16_to_cpu(resp->int_lat_tmr_max_max);
6504 		coal_cap->num_cmpl_aggr_int_max =
6505 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
6506 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
6507 	}
6508 	mutex_unlock(&bp->hwrm_cmd_lock);
6509 }
6510 
bnxt_usec_to_coal_tmr(struct bnxt * bp,u16 usec)6511 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
6512 {
6513 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6514 
6515 	return usec * 1000 / coal_cap->timer_units;
6516 }
6517 
bnxt_hwrm_set_coal_params(struct bnxt * bp,struct bnxt_coal * hw_coal,struct hwrm_ring_cmpl_ring_cfg_aggint_params_input * req)6518 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
6519 	struct bnxt_coal *hw_coal,
6520 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
6521 {
6522 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6523 	u32 cmpl_params = coal_cap->cmpl_params;
6524 	u16 val, tmr, max, flags = 0;
6525 
6526 	max = hw_coal->bufs_per_record * 128;
6527 	if (hw_coal->budget)
6528 		max = hw_coal->bufs_per_record * hw_coal->budget;
6529 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
6530 
6531 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
6532 	req->num_cmpl_aggr_int = cpu_to_le16(val);
6533 
6534 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
6535 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
6536 
6537 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
6538 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
6539 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
6540 
6541 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
6542 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
6543 	req->int_lat_tmr_max = cpu_to_le16(tmr);
6544 
6545 	/* min timer set to 1/2 of interrupt timer */
6546 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
6547 		val = tmr / 2;
6548 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
6549 		req->int_lat_tmr_min = cpu_to_le16(val);
6550 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6551 	}
6552 
6553 	/* buf timer set to 1/4 of interrupt timer */
6554 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
6555 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
6556 
6557 	if (cmpl_params &
6558 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
6559 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
6560 		val = clamp_t(u16, tmr, 1,
6561 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
6562 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
6563 		req->enables |=
6564 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
6565 	}
6566 
6567 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
6568 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
6569 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
6570 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
6571 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
6572 	req->flags = cpu_to_le16(flags);
6573 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
6574 }
6575 
6576 /* Caller holds bp->hwrm_cmd_lock */
__bnxt_hwrm_set_coal_nq(struct bnxt * bp,struct bnxt_napi * bnapi,struct bnxt_coal * hw_coal)6577 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
6578 				   struct bnxt_coal *hw_coal)
6579 {
6580 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req = {0};
6581 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6582 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6583 	u32 nq_params = coal_cap->nq_params;
6584 	u16 tmr;
6585 
6586 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
6587 		return 0;
6588 
6589 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS,
6590 			       -1, -1);
6591 	req.ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
6592 	req.flags =
6593 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
6594 
6595 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
6596 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
6597 	req.int_lat_tmr_min = cpu_to_le16(tmr);
6598 	req.enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6599 	return _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6600 }
6601 
bnxt_hwrm_set_ring_coal(struct bnxt * bp,struct bnxt_napi * bnapi)6602 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
6603 {
6604 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0};
6605 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6606 	struct bnxt_coal coal;
6607 
6608 	/* Tick values in micro seconds.
6609 	 * 1 coal_buf x bufs_per_record = 1 completion record.
6610 	 */
6611 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
6612 
6613 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
6614 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
6615 
6616 	if (!bnapi->rx_ring)
6617 		return -ENODEV;
6618 
6619 	bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
6620 			       HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6621 
6622 	bnxt_hwrm_set_coal_params(bp, &coal, &req_rx);
6623 
6624 	req_rx.ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
6625 
6626 	return hwrm_send_message(bp, &req_rx, sizeof(req_rx),
6627 				 HWRM_CMD_TIMEOUT);
6628 }
6629 
bnxt_hwrm_set_coal(struct bnxt * bp)6630 int bnxt_hwrm_set_coal(struct bnxt *bp)
6631 {
6632 	int i, rc = 0;
6633 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
6634 							   req_tx = {0}, *req;
6635 
6636 	bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
6637 			       HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6638 	bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
6639 			       HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6640 
6641 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, &req_rx);
6642 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, &req_tx);
6643 
6644 	mutex_lock(&bp->hwrm_cmd_lock);
6645 	for (i = 0; i < bp->cp_nr_rings; i++) {
6646 		struct bnxt_napi *bnapi = bp->bnapi[i];
6647 		struct bnxt_coal *hw_coal;
6648 		u16 ring_id;
6649 
6650 		req = &req_rx;
6651 		if (!bnapi->rx_ring) {
6652 			ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6653 			req = &req_tx;
6654 		} else {
6655 			ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
6656 		}
6657 		req->ring_id = cpu_to_le16(ring_id);
6658 
6659 		rc = _hwrm_send_message(bp, req, sizeof(*req),
6660 					HWRM_CMD_TIMEOUT);
6661 		if (rc)
6662 			break;
6663 
6664 		if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6665 			continue;
6666 
6667 		if (bnapi->rx_ring && bnapi->tx_ring) {
6668 			req = &req_tx;
6669 			ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6670 			req->ring_id = cpu_to_le16(ring_id);
6671 			rc = _hwrm_send_message(bp, req, sizeof(*req),
6672 						HWRM_CMD_TIMEOUT);
6673 			if (rc)
6674 				break;
6675 		}
6676 		if (bnapi->rx_ring)
6677 			hw_coal = &bp->rx_coal;
6678 		else
6679 			hw_coal = &bp->tx_coal;
6680 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
6681 	}
6682 	mutex_unlock(&bp->hwrm_cmd_lock);
6683 	return rc;
6684 }
6685 
bnxt_hwrm_stat_ctx_free(struct bnxt * bp)6686 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
6687 {
6688 	struct hwrm_stat_ctx_clr_stats_input req0 = {0};
6689 	struct hwrm_stat_ctx_free_input req = {0};
6690 	int i;
6691 
6692 	if (!bp->bnapi)
6693 		return;
6694 
6695 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6696 		return;
6697 
6698 	bnxt_hwrm_cmd_hdr_init(bp, &req0, HWRM_STAT_CTX_CLR_STATS, -1, -1);
6699 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
6700 
6701 	mutex_lock(&bp->hwrm_cmd_lock);
6702 	for (i = 0; i < bp->cp_nr_rings; i++) {
6703 		struct bnxt_napi *bnapi = bp->bnapi[i];
6704 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6705 
6706 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
6707 			req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
6708 			if (BNXT_FW_MAJ(bp) <= 20) {
6709 				req0.stat_ctx_id = req.stat_ctx_id;
6710 				_hwrm_send_message(bp, &req0, sizeof(req0),
6711 						   HWRM_CMD_TIMEOUT);
6712 			}
6713 			_hwrm_send_message(bp, &req, sizeof(req),
6714 					   HWRM_CMD_TIMEOUT);
6715 
6716 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
6717 		}
6718 	}
6719 	mutex_unlock(&bp->hwrm_cmd_lock);
6720 }
6721 
bnxt_hwrm_stat_ctx_alloc(struct bnxt * bp)6722 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
6723 {
6724 	int rc = 0, i;
6725 	struct hwrm_stat_ctx_alloc_input req = {0};
6726 	struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
6727 
6728 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6729 		return 0;
6730 
6731 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
6732 
6733 	req.stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
6734 	req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
6735 
6736 	mutex_lock(&bp->hwrm_cmd_lock);
6737 	for (i = 0; i < bp->cp_nr_rings; i++) {
6738 		struct bnxt_napi *bnapi = bp->bnapi[i];
6739 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6740 
6741 		req.stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
6742 
6743 		rc = _hwrm_send_message(bp, &req, sizeof(req),
6744 					HWRM_CMD_TIMEOUT);
6745 		if (rc)
6746 			break;
6747 
6748 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
6749 
6750 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
6751 	}
6752 	mutex_unlock(&bp->hwrm_cmd_lock);
6753 	return rc;
6754 }
6755 
bnxt_hwrm_func_qcfg(struct bnxt * bp)6756 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
6757 {
6758 	struct hwrm_func_qcfg_input req = {0};
6759 	struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6760 	u32 min_db_offset = 0;
6761 	u16 flags;
6762 	int rc;
6763 
6764 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
6765 	req.fid = cpu_to_le16(0xffff);
6766 	mutex_lock(&bp->hwrm_cmd_lock);
6767 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6768 	if (rc)
6769 		goto func_qcfg_exit;
6770 
6771 #ifdef CONFIG_BNXT_SRIOV
6772 	if (BNXT_VF(bp)) {
6773 		struct bnxt_vf_info *vf = &bp->vf;
6774 
6775 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
6776 	} else {
6777 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
6778 	}
6779 #endif
6780 	flags = le16_to_cpu(resp->flags);
6781 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
6782 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
6783 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
6784 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
6785 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
6786 	}
6787 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
6788 		bp->flags |= BNXT_FLAG_MULTI_HOST;
6789 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
6790 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
6791 
6792 	switch (resp->port_partition_type) {
6793 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
6794 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
6795 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
6796 		bp->port_partition_type = resp->port_partition_type;
6797 		break;
6798 	}
6799 	if (bp->hwrm_spec_code < 0x10707 ||
6800 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
6801 		bp->br_mode = BRIDGE_MODE_VEB;
6802 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
6803 		bp->br_mode = BRIDGE_MODE_VEPA;
6804 	else
6805 		bp->br_mode = BRIDGE_MODE_UNDEF;
6806 
6807 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
6808 	if (!bp->max_mtu)
6809 		bp->max_mtu = BNXT_MAX_MTU;
6810 
6811 	if (bp->db_size)
6812 		goto func_qcfg_exit;
6813 
6814 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
6815 		if (BNXT_PF(bp))
6816 			min_db_offset = DB_PF_OFFSET_P5;
6817 		else
6818 			min_db_offset = DB_VF_OFFSET_P5;
6819 	}
6820 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
6821 				 1024);
6822 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
6823 	    bp->db_size <= min_db_offset)
6824 		bp->db_size = pci_resource_len(bp->pdev, 2);
6825 
6826 func_qcfg_exit:
6827 	mutex_unlock(&bp->hwrm_cmd_lock);
6828 	return rc;
6829 }
6830 
bnxt_hwrm_func_backing_store_qcaps(struct bnxt * bp)6831 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
6832 {
6833 	struct hwrm_func_backing_store_qcaps_input req = {0};
6834 	struct hwrm_func_backing_store_qcaps_output *resp =
6835 		bp->hwrm_cmd_resp_addr;
6836 	int rc;
6837 
6838 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
6839 		return 0;
6840 
6841 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_QCAPS, -1, -1);
6842 	mutex_lock(&bp->hwrm_cmd_lock);
6843 	rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6844 	if (!rc) {
6845 		struct bnxt_ctx_pg_info *ctx_pg;
6846 		struct bnxt_ctx_mem_info *ctx;
6847 		int i, tqm_rings;
6848 
6849 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
6850 		if (!ctx) {
6851 			rc = -ENOMEM;
6852 			goto ctx_err;
6853 		}
6854 		ctx->qp_max_entries = le32_to_cpu(resp->qp_max_entries);
6855 		ctx->qp_min_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
6856 		ctx->qp_max_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
6857 		ctx->qp_entry_size = le16_to_cpu(resp->qp_entry_size);
6858 		ctx->srq_max_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
6859 		ctx->srq_max_entries = le32_to_cpu(resp->srq_max_entries);
6860 		ctx->srq_entry_size = le16_to_cpu(resp->srq_entry_size);
6861 		ctx->cq_max_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
6862 		ctx->cq_max_entries = le32_to_cpu(resp->cq_max_entries);
6863 		ctx->cq_entry_size = le16_to_cpu(resp->cq_entry_size);
6864 		ctx->vnic_max_vnic_entries =
6865 			le16_to_cpu(resp->vnic_max_vnic_entries);
6866 		ctx->vnic_max_ring_table_entries =
6867 			le16_to_cpu(resp->vnic_max_ring_table_entries);
6868 		ctx->vnic_entry_size = le16_to_cpu(resp->vnic_entry_size);
6869 		ctx->stat_max_entries = le32_to_cpu(resp->stat_max_entries);
6870 		ctx->stat_entry_size = le16_to_cpu(resp->stat_entry_size);
6871 		ctx->tqm_entry_size = le16_to_cpu(resp->tqm_entry_size);
6872 		ctx->tqm_min_entries_per_ring =
6873 			le32_to_cpu(resp->tqm_min_entries_per_ring);
6874 		ctx->tqm_max_entries_per_ring =
6875 			le32_to_cpu(resp->tqm_max_entries_per_ring);
6876 		ctx->tqm_entries_multiple = resp->tqm_entries_multiple;
6877 		if (!ctx->tqm_entries_multiple)
6878 			ctx->tqm_entries_multiple = 1;
6879 		ctx->mrav_max_entries = le32_to_cpu(resp->mrav_max_entries);
6880 		ctx->mrav_entry_size = le16_to_cpu(resp->mrav_entry_size);
6881 		ctx->mrav_num_entries_units =
6882 			le16_to_cpu(resp->mrav_num_entries_units);
6883 		ctx->tim_entry_size = le16_to_cpu(resp->tim_entry_size);
6884 		ctx->tim_max_entries = le32_to_cpu(resp->tim_max_entries);
6885 		ctx->ctx_kind_initializer = resp->ctx_kind_initializer;
6886 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
6887 		if (!ctx->tqm_fp_rings_count)
6888 			ctx->tqm_fp_rings_count = bp->max_q;
6889 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
6890 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
6891 
6892 		tqm_rings = ctx->tqm_fp_rings_count + BNXT_MAX_TQM_SP_RINGS;
6893 		ctx_pg = kcalloc(tqm_rings, sizeof(*ctx_pg), GFP_KERNEL);
6894 		if (!ctx_pg) {
6895 			kfree(ctx);
6896 			rc = -ENOMEM;
6897 			goto ctx_err;
6898 		}
6899 		for (i = 0; i < tqm_rings; i++, ctx_pg++)
6900 			ctx->tqm_mem[i] = ctx_pg;
6901 		bp->ctx = ctx;
6902 	} else {
6903 		rc = 0;
6904 	}
6905 ctx_err:
6906 	mutex_unlock(&bp->hwrm_cmd_lock);
6907 	return rc;
6908 }
6909 
bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info * rmem,u8 * pg_attr,__le64 * pg_dir)6910 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
6911 				  __le64 *pg_dir)
6912 {
6913 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
6914 	if (rmem->depth >= 1) {
6915 		if (rmem->depth == 2)
6916 			*pg_attr |= 2;
6917 		else
6918 			*pg_attr |= 1;
6919 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
6920 	} else {
6921 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
6922 	}
6923 }
6924 
6925 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
6926 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
6927 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
6928 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
6929 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
6930 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
6931 
bnxt_hwrm_func_backing_store_cfg(struct bnxt * bp,u32 enables)6932 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
6933 {
6934 	struct hwrm_func_backing_store_cfg_input req = {0};
6935 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
6936 	struct bnxt_ctx_pg_info *ctx_pg;
6937 	__le32 *num_entries;
6938 	__le64 *pg_dir;
6939 	u32 flags = 0;
6940 	u8 *pg_attr;
6941 	u32 ena;
6942 	int i;
6943 
6944 	if (!ctx)
6945 		return 0;
6946 
6947 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_CFG, -1, -1);
6948 	req.enables = cpu_to_le32(enables);
6949 
6950 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
6951 		ctx_pg = &ctx->qp_mem;
6952 		req.qp_num_entries = cpu_to_le32(ctx_pg->entries);
6953 		req.qp_num_qp1_entries = cpu_to_le16(ctx->qp_min_qp1_entries);
6954 		req.qp_num_l2_entries = cpu_to_le16(ctx->qp_max_l2_entries);
6955 		req.qp_entry_size = cpu_to_le16(ctx->qp_entry_size);
6956 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6957 				      &req.qpc_pg_size_qpc_lvl,
6958 				      &req.qpc_page_dir);
6959 	}
6960 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
6961 		ctx_pg = &ctx->srq_mem;
6962 		req.srq_num_entries = cpu_to_le32(ctx_pg->entries);
6963 		req.srq_num_l2_entries = cpu_to_le16(ctx->srq_max_l2_entries);
6964 		req.srq_entry_size = cpu_to_le16(ctx->srq_entry_size);
6965 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6966 				      &req.srq_pg_size_srq_lvl,
6967 				      &req.srq_page_dir);
6968 	}
6969 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
6970 		ctx_pg = &ctx->cq_mem;
6971 		req.cq_num_entries = cpu_to_le32(ctx_pg->entries);
6972 		req.cq_num_l2_entries = cpu_to_le16(ctx->cq_max_l2_entries);
6973 		req.cq_entry_size = cpu_to_le16(ctx->cq_entry_size);
6974 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, &req.cq_pg_size_cq_lvl,
6975 				      &req.cq_page_dir);
6976 	}
6977 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
6978 		ctx_pg = &ctx->vnic_mem;
6979 		req.vnic_num_vnic_entries =
6980 			cpu_to_le16(ctx->vnic_max_vnic_entries);
6981 		req.vnic_num_ring_table_entries =
6982 			cpu_to_le16(ctx->vnic_max_ring_table_entries);
6983 		req.vnic_entry_size = cpu_to_le16(ctx->vnic_entry_size);
6984 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6985 				      &req.vnic_pg_size_vnic_lvl,
6986 				      &req.vnic_page_dir);
6987 	}
6988 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
6989 		ctx_pg = &ctx->stat_mem;
6990 		req.stat_num_entries = cpu_to_le32(ctx->stat_max_entries);
6991 		req.stat_entry_size = cpu_to_le16(ctx->stat_entry_size);
6992 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6993 				      &req.stat_pg_size_stat_lvl,
6994 				      &req.stat_page_dir);
6995 	}
6996 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
6997 		ctx_pg = &ctx->mrav_mem;
6998 		req.mrav_num_entries = cpu_to_le32(ctx_pg->entries);
6999 		if (ctx->mrav_num_entries_units)
7000 			flags |=
7001 			FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
7002 		req.mrav_entry_size = cpu_to_le16(ctx->mrav_entry_size);
7003 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7004 				      &req.mrav_pg_size_mrav_lvl,
7005 				      &req.mrav_page_dir);
7006 	}
7007 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
7008 		ctx_pg = &ctx->tim_mem;
7009 		req.tim_num_entries = cpu_to_le32(ctx_pg->entries);
7010 		req.tim_entry_size = cpu_to_le16(ctx->tim_entry_size);
7011 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7012 				      &req.tim_pg_size_tim_lvl,
7013 				      &req.tim_page_dir);
7014 	}
7015 	for (i = 0, num_entries = &req.tqm_sp_num_entries,
7016 	     pg_attr = &req.tqm_sp_pg_size_tqm_sp_lvl,
7017 	     pg_dir = &req.tqm_sp_page_dir,
7018 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP;
7019 	     i < BNXT_MAX_TQM_RINGS;
7020 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
7021 		if (!(enables & ena))
7022 			continue;
7023 
7024 		req.tqm_entry_size = cpu_to_le16(ctx->tqm_entry_size);
7025 		ctx_pg = ctx->tqm_mem[i];
7026 		*num_entries = cpu_to_le32(ctx_pg->entries);
7027 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
7028 	}
7029 	req.flags = cpu_to_le32(flags);
7030 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7031 }
7032 
bnxt_alloc_ctx_mem_blk(struct bnxt * bp,struct bnxt_ctx_pg_info * ctx_pg)7033 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
7034 				  struct bnxt_ctx_pg_info *ctx_pg)
7035 {
7036 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7037 
7038 	rmem->page_size = BNXT_PAGE_SIZE;
7039 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
7040 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
7041 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
7042 	if (rmem->depth >= 1)
7043 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
7044 	return bnxt_alloc_ring(bp, rmem);
7045 }
7046 
bnxt_alloc_ctx_pg_tbls(struct bnxt * bp,struct bnxt_ctx_pg_info * ctx_pg,u32 mem_size,u8 depth,bool use_init_val)7047 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
7048 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
7049 				  u8 depth, bool use_init_val)
7050 {
7051 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7052 	int rc;
7053 
7054 	if (!mem_size)
7055 		return -EINVAL;
7056 
7057 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
7058 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
7059 		ctx_pg->nr_pages = 0;
7060 		return -EINVAL;
7061 	}
7062 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
7063 		int nr_tbls, i;
7064 
7065 		rmem->depth = 2;
7066 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
7067 					     GFP_KERNEL);
7068 		if (!ctx_pg->ctx_pg_tbl)
7069 			return -ENOMEM;
7070 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
7071 		rmem->nr_pages = nr_tbls;
7072 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
7073 		if (rc)
7074 			return rc;
7075 		for (i = 0; i < nr_tbls; i++) {
7076 			struct bnxt_ctx_pg_info *pg_tbl;
7077 
7078 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
7079 			if (!pg_tbl)
7080 				return -ENOMEM;
7081 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
7082 			rmem = &pg_tbl->ring_mem;
7083 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
7084 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
7085 			rmem->depth = 1;
7086 			rmem->nr_pages = MAX_CTX_PAGES;
7087 			if (use_init_val)
7088 				rmem->init_val = bp->ctx->ctx_kind_initializer;
7089 			if (i == (nr_tbls - 1)) {
7090 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
7091 
7092 				if (rem)
7093 					rmem->nr_pages = rem;
7094 			}
7095 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
7096 			if (rc)
7097 				break;
7098 		}
7099 	} else {
7100 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
7101 		if (rmem->nr_pages > 1 || depth)
7102 			rmem->depth = 1;
7103 		if (use_init_val)
7104 			rmem->init_val = bp->ctx->ctx_kind_initializer;
7105 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
7106 	}
7107 	return rc;
7108 }
7109 
bnxt_free_ctx_pg_tbls(struct bnxt * bp,struct bnxt_ctx_pg_info * ctx_pg)7110 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
7111 				  struct bnxt_ctx_pg_info *ctx_pg)
7112 {
7113 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7114 
7115 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
7116 	    ctx_pg->ctx_pg_tbl) {
7117 		int i, nr_tbls = rmem->nr_pages;
7118 
7119 		for (i = 0; i < nr_tbls; i++) {
7120 			struct bnxt_ctx_pg_info *pg_tbl;
7121 			struct bnxt_ring_mem_info *rmem2;
7122 
7123 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
7124 			if (!pg_tbl)
7125 				continue;
7126 			rmem2 = &pg_tbl->ring_mem;
7127 			bnxt_free_ring(bp, rmem2);
7128 			ctx_pg->ctx_pg_arr[i] = NULL;
7129 			kfree(pg_tbl);
7130 			ctx_pg->ctx_pg_tbl[i] = NULL;
7131 		}
7132 		kfree(ctx_pg->ctx_pg_tbl);
7133 		ctx_pg->ctx_pg_tbl = NULL;
7134 	}
7135 	bnxt_free_ring(bp, rmem);
7136 	ctx_pg->nr_pages = 0;
7137 }
7138 
bnxt_free_ctx_mem(struct bnxt * bp)7139 static void bnxt_free_ctx_mem(struct bnxt *bp)
7140 {
7141 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
7142 	int i;
7143 
7144 	if (!ctx)
7145 		return;
7146 
7147 	if (ctx->tqm_mem[0]) {
7148 		for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
7149 			bnxt_free_ctx_pg_tbls(bp, ctx->tqm_mem[i]);
7150 		kfree(ctx->tqm_mem[0]);
7151 		ctx->tqm_mem[0] = NULL;
7152 	}
7153 
7154 	bnxt_free_ctx_pg_tbls(bp, &ctx->tim_mem);
7155 	bnxt_free_ctx_pg_tbls(bp, &ctx->mrav_mem);
7156 	bnxt_free_ctx_pg_tbls(bp, &ctx->stat_mem);
7157 	bnxt_free_ctx_pg_tbls(bp, &ctx->vnic_mem);
7158 	bnxt_free_ctx_pg_tbls(bp, &ctx->cq_mem);
7159 	bnxt_free_ctx_pg_tbls(bp, &ctx->srq_mem);
7160 	bnxt_free_ctx_pg_tbls(bp, &ctx->qp_mem);
7161 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
7162 }
7163 
bnxt_alloc_ctx_mem(struct bnxt * bp)7164 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
7165 {
7166 	struct bnxt_ctx_pg_info *ctx_pg;
7167 	struct bnxt_ctx_mem_info *ctx;
7168 	u32 mem_size, ena, entries;
7169 	u32 entries_sp, min;
7170 	u32 num_mr, num_ah;
7171 	u32 extra_srqs = 0;
7172 	u32 extra_qps = 0;
7173 	u8 pg_lvl = 1;
7174 	int i, rc;
7175 
7176 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
7177 	if (rc) {
7178 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
7179 			   rc);
7180 		return rc;
7181 	}
7182 	ctx = bp->ctx;
7183 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
7184 		return 0;
7185 
7186 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
7187 		pg_lvl = 2;
7188 		extra_qps = 65536;
7189 		extra_srqs = 8192;
7190 	}
7191 
7192 	ctx_pg = &ctx->qp_mem;
7193 	ctx_pg->entries = ctx->qp_min_qp1_entries + ctx->qp_max_l2_entries +
7194 			  extra_qps;
7195 	mem_size = ctx->qp_entry_size * ctx_pg->entries;
7196 	rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, true);
7197 	if (rc)
7198 		return rc;
7199 
7200 	ctx_pg = &ctx->srq_mem;
7201 	ctx_pg->entries = ctx->srq_max_l2_entries + extra_srqs;
7202 	mem_size = ctx->srq_entry_size * ctx_pg->entries;
7203 	rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, true);
7204 	if (rc)
7205 		return rc;
7206 
7207 	ctx_pg = &ctx->cq_mem;
7208 	ctx_pg->entries = ctx->cq_max_l2_entries + extra_qps * 2;
7209 	mem_size = ctx->cq_entry_size * ctx_pg->entries;
7210 	rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, true);
7211 	if (rc)
7212 		return rc;
7213 
7214 	ctx_pg = &ctx->vnic_mem;
7215 	ctx_pg->entries = ctx->vnic_max_vnic_entries +
7216 			  ctx->vnic_max_ring_table_entries;
7217 	mem_size = ctx->vnic_entry_size * ctx_pg->entries;
7218 	rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, true);
7219 	if (rc)
7220 		return rc;
7221 
7222 	ctx_pg = &ctx->stat_mem;
7223 	ctx_pg->entries = ctx->stat_max_entries;
7224 	mem_size = ctx->stat_entry_size * ctx_pg->entries;
7225 	rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, true);
7226 	if (rc)
7227 		return rc;
7228 
7229 	ena = 0;
7230 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
7231 		goto skip_rdma;
7232 
7233 	ctx_pg = &ctx->mrav_mem;
7234 	/* 128K extra is needed to accommodate static AH context
7235 	 * allocation by f/w.
7236 	 */
7237 	num_mr = 1024 * 256;
7238 	num_ah = 1024 * 128;
7239 	ctx_pg->entries = num_mr + num_ah;
7240 	mem_size = ctx->mrav_entry_size * ctx_pg->entries;
7241 	rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 2, true);
7242 	if (rc)
7243 		return rc;
7244 	ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
7245 	if (ctx->mrav_num_entries_units)
7246 		ctx_pg->entries =
7247 			((num_mr / ctx->mrav_num_entries_units) << 16) |
7248 			 (num_ah / ctx->mrav_num_entries_units);
7249 
7250 	ctx_pg = &ctx->tim_mem;
7251 	ctx_pg->entries = ctx->qp_mem.entries;
7252 	mem_size = ctx->tim_entry_size * ctx_pg->entries;
7253 	rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, false);
7254 	if (rc)
7255 		return rc;
7256 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
7257 
7258 skip_rdma:
7259 	min = ctx->tqm_min_entries_per_ring;
7260 	entries_sp = ctx->vnic_max_vnic_entries + ctx->qp_max_l2_entries +
7261 		     2 * (extra_qps + ctx->qp_min_qp1_entries) + min;
7262 	entries_sp = roundup(entries_sp, ctx->tqm_entries_multiple);
7263 	entries = ctx->qp_max_l2_entries + 2 * (extra_qps + ctx->qp_min_qp1_entries);
7264 	entries = roundup(entries, ctx->tqm_entries_multiple);
7265 	entries = clamp_t(u32, entries, min, ctx->tqm_max_entries_per_ring);
7266 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++) {
7267 		ctx_pg = ctx->tqm_mem[i];
7268 		ctx_pg->entries = i ? entries : entries_sp;
7269 		mem_size = ctx->tqm_entry_size * ctx_pg->entries;
7270 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, false);
7271 		if (rc)
7272 			return rc;
7273 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
7274 	}
7275 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
7276 	rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
7277 	if (rc) {
7278 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
7279 			   rc);
7280 		return rc;
7281 	}
7282 	ctx->flags |= BNXT_CTX_FLAG_INITED;
7283 	return 0;
7284 }
7285 
bnxt_hwrm_func_resc_qcaps(struct bnxt * bp,bool all)7286 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
7287 {
7288 	struct hwrm_func_resource_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
7289 	struct hwrm_func_resource_qcaps_input req = {0};
7290 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7291 	int rc;
7292 
7293 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESOURCE_QCAPS, -1, -1);
7294 	req.fid = cpu_to_le16(0xffff);
7295 
7296 	mutex_lock(&bp->hwrm_cmd_lock);
7297 	rc = _hwrm_send_message_silent(bp, &req, sizeof(req),
7298 				       HWRM_CMD_TIMEOUT);
7299 	if (rc)
7300 		goto hwrm_func_resc_qcaps_exit;
7301 
7302 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
7303 	if (!all)
7304 		goto hwrm_func_resc_qcaps_exit;
7305 
7306 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
7307 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
7308 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
7309 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
7310 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
7311 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
7312 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
7313 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
7314 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
7315 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
7316 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
7317 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
7318 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
7319 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
7320 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
7321 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
7322 
7323 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
7324 		u16 max_msix = le16_to_cpu(resp->max_msix);
7325 
7326 		hw_resc->max_nqs = max_msix;
7327 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
7328 	}
7329 
7330 	if (BNXT_PF(bp)) {
7331 		struct bnxt_pf_info *pf = &bp->pf;
7332 
7333 		pf->vf_resv_strategy =
7334 			le16_to_cpu(resp->vf_reservation_strategy);
7335 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
7336 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
7337 	}
7338 hwrm_func_resc_qcaps_exit:
7339 	mutex_unlock(&bp->hwrm_cmd_lock);
7340 	return rc;
7341 }
7342 
__bnxt_hwrm_func_qcaps(struct bnxt * bp)7343 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
7344 {
7345 	int rc = 0;
7346 	struct hwrm_func_qcaps_input req = {0};
7347 	struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
7348 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7349 	u32 flags, flags_ext;
7350 
7351 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
7352 	req.fid = cpu_to_le16(0xffff);
7353 
7354 	mutex_lock(&bp->hwrm_cmd_lock);
7355 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7356 	if (rc)
7357 		goto hwrm_func_qcaps_exit;
7358 
7359 	flags = le32_to_cpu(resp->flags);
7360 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
7361 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
7362 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
7363 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
7364 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
7365 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
7366 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
7367 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
7368 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
7369 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
7370 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
7371 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
7372 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
7373 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
7374 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
7375 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
7376 
7377 	flags_ext = le32_to_cpu(resp->flags_ext);
7378 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
7379 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
7380 
7381 	bp->tx_push_thresh = 0;
7382 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
7383 	    BNXT_FW_MAJ(bp) > 217)
7384 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
7385 
7386 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
7387 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
7388 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
7389 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
7390 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
7391 	if (!hw_resc->max_hw_ring_grps)
7392 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
7393 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
7394 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
7395 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
7396 
7397 	if (BNXT_PF(bp)) {
7398 		struct bnxt_pf_info *pf = &bp->pf;
7399 
7400 		pf->fw_fid = le16_to_cpu(resp->fid);
7401 		pf->port_id = le16_to_cpu(resp->port_id);
7402 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
7403 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
7404 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
7405 		pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
7406 		pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
7407 		pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
7408 		pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
7409 		pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
7410 		pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
7411 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
7412 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
7413 			bp->flags |= BNXT_FLAG_WOL_CAP;
7414 	} else {
7415 #ifdef CONFIG_BNXT_SRIOV
7416 		struct bnxt_vf_info *vf = &bp->vf;
7417 
7418 		vf->fw_fid = le16_to_cpu(resp->fid);
7419 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
7420 #endif
7421 	}
7422 
7423 hwrm_func_qcaps_exit:
7424 	mutex_unlock(&bp->hwrm_cmd_lock);
7425 	return rc;
7426 }
7427 
7428 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
7429 
bnxt_hwrm_func_qcaps(struct bnxt * bp)7430 static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
7431 {
7432 	int rc;
7433 
7434 	rc = __bnxt_hwrm_func_qcaps(bp);
7435 	if (rc)
7436 		return rc;
7437 	rc = bnxt_hwrm_queue_qportcfg(bp);
7438 	if (rc) {
7439 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
7440 		return rc;
7441 	}
7442 	if (bp->hwrm_spec_code >= 0x10803) {
7443 		rc = bnxt_alloc_ctx_mem(bp);
7444 		if (rc)
7445 			return rc;
7446 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
7447 		if (!rc)
7448 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
7449 	}
7450 	return 0;
7451 }
7452 
bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt * bp)7453 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
7454 {
7455 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input req = {0};
7456 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
7457 	int rc = 0;
7458 	u32 flags;
7459 
7460 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
7461 		return 0;
7462 
7463 	resp = bp->hwrm_cmd_resp_addr;
7464 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS, -1, -1);
7465 
7466 	mutex_lock(&bp->hwrm_cmd_lock);
7467 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7468 	if (rc)
7469 		goto hwrm_cfa_adv_qcaps_exit;
7470 
7471 	flags = le32_to_cpu(resp->flags);
7472 	if (flags &
7473 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
7474 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
7475 
7476 hwrm_cfa_adv_qcaps_exit:
7477 	mutex_unlock(&bp->hwrm_cmd_lock);
7478 	return rc;
7479 }
7480 
__bnxt_alloc_fw_health(struct bnxt * bp)7481 static int __bnxt_alloc_fw_health(struct bnxt *bp)
7482 {
7483 	if (bp->fw_health)
7484 		return 0;
7485 
7486 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
7487 	if (!bp->fw_health)
7488 		return -ENOMEM;
7489 
7490 	return 0;
7491 }
7492 
bnxt_alloc_fw_health(struct bnxt * bp)7493 static int bnxt_alloc_fw_health(struct bnxt *bp)
7494 {
7495 	int rc;
7496 
7497 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
7498 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
7499 		return 0;
7500 
7501 	rc = __bnxt_alloc_fw_health(bp);
7502 	if (rc) {
7503 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
7504 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
7505 		return rc;
7506 	}
7507 
7508 	return 0;
7509 }
7510 
__bnxt_map_fw_health_reg(struct bnxt * bp,u32 reg)7511 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
7512 {
7513 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
7514 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
7515 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
7516 }
7517 
bnxt_try_map_fw_health_reg(struct bnxt * bp)7518 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
7519 {
7520 	void __iomem *hs;
7521 	u32 status_loc;
7522 	u32 reg_type;
7523 	u32 sig;
7524 
7525 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
7526 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
7527 
7528 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
7529 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
7530 		if (bp->fw_health)
7531 			bp->fw_health->status_reliable = false;
7532 		return;
7533 	}
7534 
7535 	if (__bnxt_alloc_fw_health(bp)) {
7536 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
7537 		return;
7538 	}
7539 
7540 	status_loc = readl(hs + offsetof(struct hcomm_status, fw_status_loc));
7541 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
7542 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
7543 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
7544 		__bnxt_map_fw_health_reg(bp, status_loc);
7545 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
7546 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
7547 	}
7548 
7549 	bp->fw_health->status_reliable = true;
7550 }
7551 
bnxt_map_fw_health_regs(struct bnxt * bp)7552 static int bnxt_map_fw_health_regs(struct bnxt *bp)
7553 {
7554 	struct bnxt_fw_health *fw_health = bp->fw_health;
7555 	u32 reg_base = 0xffffffff;
7556 	int i;
7557 
7558 	/* Only pre-map the monitoring GRC registers using window 3 */
7559 	for (i = 0; i < 4; i++) {
7560 		u32 reg = fw_health->regs[i];
7561 
7562 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
7563 			continue;
7564 		if (reg_base == 0xffffffff)
7565 			reg_base = reg & BNXT_GRC_BASE_MASK;
7566 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
7567 			return -ERANGE;
7568 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
7569 	}
7570 	if (reg_base == 0xffffffff)
7571 		return 0;
7572 
7573 	__bnxt_map_fw_health_reg(bp, reg_base);
7574 	return 0;
7575 }
7576 
bnxt_hwrm_error_recovery_qcfg(struct bnxt * bp)7577 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
7578 {
7579 	struct hwrm_error_recovery_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
7580 	struct bnxt_fw_health *fw_health = bp->fw_health;
7581 	struct hwrm_error_recovery_qcfg_input req = {0};
7582 	int rc, i;
7583 
7584 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
7585 		return 0;
7586 
7587 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_ERROR_RECOVERY_QCFG, -1, -1);
7588 	mutex_lock(&bp->hwrm_cmd_lock);
7589 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7590 	if (rc)
7591 		goto err_recovery_out;
7592 	fw_health->flags = le32_to_cpu(resp->flags);
7593 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
7594 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
7595 		rc = -EINVAL;
7596 		goto err_recovery_out;
7597 	}
7598 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
7599 	fw_health->master_func_wait_dsecs =
7600 		le32_to_cpu(resp->master_func_wait_period);
7601 	fw_health->normal_func_wait_dsecs =
7602 		le32_to_cpu(resp->normal_func_wait_period);
7603 	fw_health->post_reset_wait_dsecs =
7604 		le32_to_cpu(resp->master_func_wait_period_after_reset);
7605 	fw_health->post_reset_max_wait_dsecs =
7606 		le32_to_cpu(resp->max_bailout_time_after_reset);
7607 	fw_health->regs[BNXT_FW_HEALTH_REG] =
7608 		le32_to_cpu(resp->fw_health_status_reg);
7609 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
7610 		le32_to_cpu(resp->fw_heartbeat_reg);
7611 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
7612 		le32_to_cpu(resp->fw_reset_cnt_reg);
7613 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
7614 		le32_to_cpu(resp->reset_inprogress_reg);
7615 	fw_health->fw_reset_inprog_reg_mask =
7616 		le32_to_cpu(resp->reset_inprogress_reg_mask);
7617 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
7618 	if (fw_health->fw_reset_seq_cnt >= 16) {
7619 		rc = -EINVAL;
7620 		goto err_recovery_out;
7621 	}
7622 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
7623 		fw_health->fw_reset_seq_regs[i] =
7624 			le32_to_cpu(resp->reset_reg[i]);
7625 		fw_health->fw_reset_seq_vals[i] =
7626 			le32_to_cpu(resp->reset_reg_val[i]);
7627 		fw_health->fw_reset_seq_delay_msec[i] =
7628 			resp->delay_after_reset[i];
7629 	}
7630 err_recovery_out:
7631 	mutex_unlock(&bp->hwrm_cmd_lock);
7632 	if (!rc)
7633 		rc = bnxt_map_fw_health_regs(bp);
7634 	if (rc)
7635 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
7636 	return rc;
7637 }
7638 
bnxt_hwrm_func_reset(struct bnxt * bp)7639 static int bnxt_hwrm_func_reset(struct bnxt *bp)
7640 {
7641 	struct hwrm_func_reset_input req = {0};
7642 
7643 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
7644 	req.enables = 0;
7645 
7646 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
7647 }
7648 
bnxt_nvm_cfg_ver_get(struct bnxt * bp)7649 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
7650 {
7651 	struct hwrm_nvm_get_dev_info_output nvm_info;
7652 
7653 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
7654 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
7655 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
7656 			 nvm_info.nvm_cfg_ver_upd);
7657 }
7658 
bnxt_hwrm_queue_qportcfg(struct bnxt * bp)7659 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
7660 {
7661 	int rc = 0;
7662 	struct hwrm_queue_qportcfg_input req = {0};
7663 	struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
7664 	u8 i, j, *qptr;
7665 	bool no_rdma;
7666 
7667 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
7668 
7669 	mutex_lock(&bp->hwrm_cmd_lock);
7670 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7671 	if (rc)
7672 		goto qportcfg_exit;
7673 
7674 	if (!resp->max_configurable_queues) {
7675 		rc = -EINVAL;
7676 		goto qportcfg_exit;
7677 	}
7678 	bp->max_tc = resp->max_configurable_queues;
7679 	bp->max_lltc = resp->max_configurable_lossless_queues;
7680 	if (bp->max_tc > BNXT_MAX_QUEUE)
7681 		bp->max_tc = BNXT_MAX_QUEUE;
7682 
7683 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
7684 	qptr = &resp->queue_id0;
7685 	for (i = 0, j = 0; i < bp->max_tc; i++) {
7686 		bp->q_info[j].queue_id = *qptr;
7687 		bp->q_ids[i] = *qptr++;
7688 		bp->q_info[j].queue_profile = *qptr++;
7689 		bp->tc_to_qidx[j] = j;
7690 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
7691 		    (no_rdma && BNXT_PF(bp)))
7692 			j++;
7693 	}
7694 	bp->max_q = bp->max_tc;
7695 	bp->max_tc = max_t(u8, j, 1);
7696 
7697 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
7698 		bp->max_tc = 1;
7699 
7700 	if (bp->max_lltc > bp->max_tc)
7701 		bp->max_lltc = bp->max_tc;
7702 
7703 qportcfg_exit:
7704 	mutex_unlock(&bp->hwrm_cmd_lock);
7705 	return rc;
7706 }
7707 
__bnxt_hwrm_ver_get(struct bnxt * bp,bool silent)7708 static int __bnxt_hwrm_ver_get(struct bnxt *bp, bool silent)
7709 {
7710 	struct hwrm_ver_get_input req = {0};
7711 	int rc;
7712 
7713 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
7714 	req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
7715 	req.hwrm_intf_min = HWRM_VERSION_MINOR;
7716 	req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
7717 
7718 	rc = bnxt_hwrm_do_send_msg(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT,
7719 				   silent);
7720 	return rc;
7721 }
7722 
bnxt_hwrm_ver_get(struct bnxt * bp)7723 static int bnxt_hwrm_ver_get(struct bnxt *bp)
7724 {
7725 	struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
7726 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
7727 	u32 dev_caps_cfg, hwrm_ver;
7728 	int rc, len;
7729 
7730 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
7731 	mutex_lock(&bp->hwrm_cmd_lock);
7732 	rc = __bnxt_hwrm_ver_get(bp, false);
7733 	if (rc)
7734 		goto hwrm_ver_get_exit;
7735 
7736 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
7737 
7738 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
7739 			     resp->hwrm_intf_min_8b << 8 |
7740 			     resp->hwrm_intf_upd_8b;
7741 	if (resp->hwrm_intf_maj_8b < 1) {
7742 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
7743 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
7744 			    resp->hwrm_intf_upd_8b);
7745 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
7746 	}
7747 
7748 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
7749 			HWRM_VERSION_UPDATE;
7750 
7751 	if (bp->hwrm_spec_code > hwrm_ver)
7752 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
7753 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
7754 			 HWRM_VERSION_UPDATE);
7755 	else
7756 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
7757 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
7758 			 resp->hwrm_intf_upd_8b);
7759 
7760 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
7761 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
7762 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
7763 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
7764 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
7765 		len = FW_VER_STR_LEN;
7766 	} else {
7767 		fw_maj = resp->hwrm_fw_maj_8b;
7768 		fw_min = resp->hwrm_fw_min_8b;
7769 		fw_bld = resp->hwrm_fw_bld_8b;
7770 		fw_rsv = resp->hwrm_fw_rsvd_8b;
7771 		len = BC_HWRM_STR_LEN;
7772 	}
7773 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
7774 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
7775 		 fw_rsv);
7776 
7777 	if (strlen(resp->active_pkg_name)) {
7778 		int fw_ver_len = strlen(bp->fw_ver_str);
7779 
7780 		snprintf(bp->fw_ver_str + fw_ver_len,
7781 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
7782 			 resp->active_pkg_name);
7783 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
7784 	}
7785 
7786 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
7787 	if (!bp->hwrm_cmd_timeout)
7788 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
7789 
7790 	if (resp->hwrm_intf_maj_8b >= 1) {
7791 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
7792 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
7793 	}
7794 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
7795 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
7796 
7797 	bp->chip_num = le16_to_cpu(resp->chip_num);
7798 	bp->chip_rev = resp->chip_rev;
7799 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
7800 	    !resp->chip_metal)
7801 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
7802 
7803 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
7804 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
7805 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
7806 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
7807 
7808 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
7809 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
7810 
7811 	if (dev_caps_cfg &
7812 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
7813 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
7814 
7815 	if (dev_caps_cfg &
7816 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
7817 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
7818 
7819 	if (dev_caps_cfg &
7820 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
7821 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
7822 
7823 hwrm_ver_get_exit:
7824 	mutex_unlock(&bp->hwrm_cmd_lock);
7825 	return rc;
7826 }
7827 
bnxt_hwrm_fw_set_time(struct bnxt * bp)7828 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
7829 {
7830 	struct hwrm_fw_set_time_input req = {0};
7831 	struct tm tm;
7832 	time64_t now = ktime_get_real_seconds();
7833 
7834 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
7835 	    bp->hwrm_spec_code < 0x10400)
7836 		return -EOPNOTSUPP;
7837 
7838 	time64_to_tm(now, 0, &tm);
7839 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
7840 	req.year = cpu_to_le16(1900 + tm.tm_year);
7841 	req.month = 1 + tm.tm_mon;
7842 	req.day = tm.tm_mday;
7843 	req.hour = tm.tm_hour;
7844 	req.minute = tm.tm_min;
7845 	req.second = tm.tm_sec;
7846 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7847 }
7848 
bnxt_add_one_ctr(u64 hw,u64 * sw,u64 mask)7849 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
7850 {
7851 	u64 sw_tmp;
7852 
7853 	hw &= mask;
7854 	sw_tmp = (*sw & ~mask) | hw;
7855 	if (hw < (*sw & mask))
7856 		sw_tmp += mask + 1;
7857 	WRITE_ONCE(*sw, sw_tmp);
7858 }
7859 
__bnxt_accumulate_stats(__le64 * hw_stats,u64 * sw_stats,u64 * masks,int count,bool ignore_zero)7860 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
7861 				    int count, bool ignore_zero)
7862 {
7863 	int i;
7864 
7865 	for (i = 0; i < count; i++) {
7866 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
7867 
7868 		if (ignore_zero && !hw)
7869 			continue;
7870 
7871 		if (masks[i] == -1ULL)
7872 			sw_stats[i] = hw;
7873 		else
7874 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
7875 	}
7876 }
7877 
bnxt_accumulate_stats(struct bnxt_stats_mem * stats)7878 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
7879 {
7880 	if (!stats->hw_stats)
7881 		return;
7882 
7883 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
7884 				stats->hw_masks, stats->len / 8, false);
7885 }
7886 
bnxt_accumulate_all_stats(struct bnxt * bp)7887 static void bnxt_accumulate_all_stats(struct bnxt *bp)
7888 {
7889 	struct bnxt_stats_mem *ring0_stats;
7890 	bool ignore_zero = false;
7891 	int i;
7892 
7893 	/* Chip bug.  Counter intermittently becomes 0. */
7894 	if (bp->flags & BNXT_FLAG_CHIP_P5)
7895 		ignore_zero = true;
7896 
7897 	for (i = 0; i < bp->cp_nr_rings; i++) {
7898 		struct bnxt_napi *bnapi = bp->bnapi[i];
7899 		struct bnxt_cp_ring_info *cpr;
7900 		struct bnxt_stats_mem *stats;
7901 
7902 		cpr = &bnapi->cp_ring;
7903 		stats = &cpr->stats;
7904 		if (!i)
7905 			ring0_stats = stats;
7906 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
7907 					ring0_stats->hw_masks,
7908 					ring0_stats->len / 8, ignore_zero);
7909 	}
7910 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
7911 		struct bnxt_stats_mem *stats = &bp->port_stats;
7912 		__le64 *hw_stats = stats->hw_stats;
7913 		u64 *sw_stats = stats->sw_stats;
7914 		u64 *masks = stats->hw_masks;
7915 		int cnt;
7916 
7917 		cnt = sizeof(struct rx_port_stats) / 8;
7918 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
7919 
7920 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
7921 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
7922 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
7923 		cnt = sizeof(struct tx_port_stats) / 8;
7924 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
7925 	}
7926 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
7927 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
7928 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
7929 	}
7930 }
7931 
bnxt_hwrm_port_qstats(struct bnxt * bp,u8 flags)7932 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
7933 {
7934 	struct bnxt_pf_info *pf = &bp->pf;
7935 	struct hwrm_port_qstats_input req = {0};
7936 
7937 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
7938 		return 0;
7939 
7940 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
7941 		return -EOPNOTSUPP;
7942 
7943 	req.flags = flags;
7944 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
7945 	req.port_id = cpu_to_le16(pf->port_id);
7946 	req.tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
7947 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
7948 	req.rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
7949 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7950 }
7951 
bnxt_hwrm_port_qstats_ext(struct bnxt * bp,u8 flags)7952 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
7953 {
7954 	struct hwrm_port_qstats_ext_output *resp = bp->hwrm_cmd_resp_addr;
7955 	struct hwrm_queue_pri2cos_qcfg_input req2 = {0};
7956 	struct hwrm_port_qstats_ext_input req = {0};
7957 	struct bnxt_pf_info *pf = &bp->pf;
7958 	u32 tx_stat_size;
7959 	int rc;
7960 
7961 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
7962 		return 0;
7963 
7964 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
7965 		return -EOPNOTSUPP;
7966 
7967 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS_EXT, -1, -1);
7968 	req.flags = flags;
7969 	req.port_id = cpu_to_le16(pf->port_id);
7970 	req.rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
7971 	req.rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
7972 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
7973 		       sizeof(struct tx_port_stats_ext) : 0;
7974 	req.tx_stat_size = cpu_to_le16(tx_stat_size);
7975 	req.tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
7976 	mutex_lock(&bp->hwrm_cmd_lock);
7977 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7978 	if (!rc) {
7979 		bp->fw_rx_stats_ext_size = le16_to_cpu(resp->rx_stat_size) / 8;
7980 		bp->fw_tx_stats_ext_size = tx_stat_size ?
7981 			le16_to_cpu(resp->tx_stat_size) / 8 : 0;
7982 	} else {
7983 		bp->fw_rx_stats_ext_size = 0;
7984 		bp->fw_tx_stats_ext_size = 0;
7985 	}
7986 	if (flags)
7987 		goto qstats_done;
7988 
7989 	if (bp->fw_tx_stats_ext_size <=
7990 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
7991 		mutex_unlock(&bp->hwrm_cmd_lock);
7992 		bp->pri2cos_valid = 0;
7993 		return rc;
7994 	}
7995 
7996 	bnxt_hwrm_cmd_hdr_init(bp, &req2, HWRM_QUEUE_PRI2COS_QCFG, -1, -1);
7997 	req2.flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
7998 
7999 	rc = _hwrm_send_message(bp, &req2, sizeof(req2), HWRM_CMD_TIMEOUT);
8000 	if (!rc) {
8001 		struct hwrm_queue_pri2cos_qcfg_output *resp2;
8002 		u8 *pri2cos;
8003 		int i, j;
8004 
8005 		resp2 = bp->hwrm_cmd_resp_addr;
8006 		pri2cos = &resp2->pri0_cos_queue_id;
8007 		for (i = 0; i < 8; i++) {
8008 			u8 queue_id = pri2cos[i];
8009 			u8 queue_idx;
8010 
8011 			/* Per port queue IDs start from 0, 10, 20, etc */
8012 			queue_idx = queue_id % 10;
8013 			if (queue_idx > BNXT_MAX_QUEUE) {
8014 				bp->pri2cos_valid = false;
8015 				goto qstats_done;
8016 			}
8017 			for (j = 0; j < bp->max_q; j++) {
8018 				if (bp->q_ids[j] == queue_id)
8019 					bp->pri2cos_idx[i] = queue_idx;
8020 			}
8021 		}
8022 		bp->pri2cos_valid = 1;
8023 	}
8024 qstats_done:
8025 	mutex_unlock(&bp->hwrm_cmd_lock);
8026 	return rc;
8027 }
8028 
bnxt_hwrm_free_tunnel_ports(struct bnxt * bp)8029 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
8030 {
8031 	if (bp->vxlan_fw_dst_port_id != INVALID_HW_RING_ID)
8032 		bnxt_hwrm_tunnel_dst_port_free(
8033 			bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
8034 	if (bp->nge_fw_dst_port_id != INVALID_HW_RING_ID)
8035 		bnxt_hwrm_tunnel_dst_port_free(
8036 			bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
8037 }
8038 
bnxt_set_tpa(struct bnxt * bp,bool set_tpa)8039 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
8040 {
8041 	int rc, i;
8042 	u32 tpa_flags = 0;
8043 
8044 	if (set_tpa)
8045 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
8046 	else if (BNXT_NO_FW_ACCESS(bp))
8047 		return 0;
8048 	for (i = 0; i < bp->nr_vnics; i++) {
8049 		rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
8050 		if (rc) {
8051 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
8052 				   i, rc);
8053 			return rc;
8054 		}
8055 	}
8056 	return 0;
8057 }
8058 
bnxt_hwrm_clear_vnic_rss(struct bnxt * bp)8059 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
8060 {
8061 	int i;
8062 
8063 	for (i = 0; i < bp->nr_vnics; i++)
8064 		bnxt_hwrm_vnic_set_rss(bp, i, false);
8065 }
8066 
bnxt_clear_vnic(struct bnxt * bp)8067 static void bnxt_clear_vnic(struct bnxt *bp)
8068 {
8069 	if (!bp->vnic_info)
8070 		return;
8071 
8072 	bnxt_hwrm_clear_vnic_filter(bp);
8073 	if (!(bp->flags & BNXT_FLAG_CHIP_P5)) {
8074 		/* clear all RSS setting before free vnic ctx */
8075 		bnxt_hwrm_clear_vnic_rss(bp);
8076 		bnxt_hwrm_vnic_ctx_free(bp);
8077 	}
8078 	/* before free the vnic, undo the vnic tpa settings */
8079 	if (bp->flags & BNXT_FLAG_TPA)
8080 		bnxt_set_tpa(bp, false);
8081 	bnxt_hwrm_vnic_free(bp);
8082 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8083 		bnxt_hwrm_vnic_ctx_free(bp);
8084 }
8085 
bnxt_hwrm_resource_free(struct bnxt * bp,bool close_path,bool irq_re_init)8086 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
8087 				    bool irq_re_init)
8088 {
8089 	bnxt_clear_vnic(bp);
8090 	bnxt_hwrm_ring_free(bp, close_path);
8091 	bnxt_hwrm_ring_grp_free(bp);
8092 	if (irq_re_init) {
8093 		bnxt_hwrm_stat_ctx_free(bp);
8094 		bnxt_hwrm_free_tunnel_ports(bp);
8095 	}
8096 }
8097 
bnxt_hwrm_set_br_mode(struct bnxt * bp,u16 br_mode)8098 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
8099 {
8100 	struct hwrm_func_cfg_input req = {0};
8101 
8102 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
8103 	req.fid = cpu_to_le16(0xffff);
8104 	req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
8105 	if (br_mode == BRIDGE_MODE_VEB)
8106 		req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
8107 	else if (br_mode == BRIDGE_MODE_VEPA)
8108 		req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
8109 	else
8110 		return -EINVAL;
8111 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8112 }
8113 
bnxt_hwrm_set_cache_line_size(struct bnxt * bp,int size)8114 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
8115 {
8116 	struct hwrm_func_cfg_input req = {0};
8117 
8118 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
8119 		return 0;
8120 
8121 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
8122 	req.fid = cpu_to_le16(0xffff);
8123 	req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
8124 	req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
8125 	if (size == 128)
8126 		req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
8127 
8128 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8129 }
8130 
__bnxt_setup_vnic(struct bnxt * bp,u16 vnic_id)8131 static int __bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
8132 {
8133 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
8134 	int rc;
8135 
8136 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
8137 		goto skip_rss_ctx;
8138 
8139 	/* allocate context for vnic */
8140 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
8141 	if (rc) {
8142 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
8143 			   vnic_id, rc);
8144 		goto vnic_setup_err;
8145 	}
8146 	bp->rsscos_nr_ctxs++;
8147 
8148 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8149 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
8150 		if (rc) {
8151 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
8152 				   vnic_id, rc);
8153 			goto vnic_setup_err;
8154 		}
8155 		bp->rsscos_nr_ctxs++;
8156 	}
8157 
8158 skip_rss_ctx:
8159 	/* configure default vnic, ring grp */
8160 	rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
8161 	if (rc) {
8162 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
8163 			   vnic_id, rc);
8164 		goto vnic_setup_err;
8165 	}
8166 
8167 	/* Enable RSS hashing on vnic */
8168 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
8169 	if (rc) {
8170 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
8171 			   vnic_id, rc);
8172 		goto vnic_setup_err;
8173 	}
8174 
8175 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8176 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
8177 		if (rc) {
8178 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
8179 				   vnic_id, rc);
8180 		}
8181 	}
8182 
8183 vnic_setup_err:
8184 	return rc;
8185 }
8186 
__bnxt_setup_vnic_p5(struct bnxt * bp,u16 vnic_id)8187 static int __bnxt_setup_vnic_p5(struct bnxt *bp, u16 vnic_id)
8188 {
8189 	int rc, i, nr_ctxs;
8190 
8191 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
8192 	for (i = 0; i < nr_ctxs; i++) {
8193 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, i);
8194 		if (rc) {
8195 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
8196 				   vnic_id, i, rc);
8197 			break;
8198 		}
8199 		bp->rsscos_nr_ctxs++;
8200 	}
8201 	if (i < nr_ctxs)
8202 		return -ENOMEM;
8203 
8204 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic_id, true);
8205 	if (rc) {
8206 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
8207 			   vnic_id, rc);
8208 		return rc;
8209 	}
8210 	rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
8211 	if (rc) {
8212 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
8213 			   vnic_id, rc);
8214 		return rc;
8215 	}
8216 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8217 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
8218 		if (rc) {
8219 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
8220 				   vnic_id, rc);
8221 		}
8222 	}
8223 	return rc;
8224 }
8225 
bnxt_setup_vnic(struct bnxt * bp,u16 vnic_id)8226 static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
8227 {
8228 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8229 		return __bnxt_setup_vnic_p5(bp, vnic_id);
8230 	else
8231 		return __bnxt_setup_vnic(bp, vnic_id);
8232 }
8233 
bnxt_alloc_rfs_vnics(struct bnxt * bp)8234 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
8235 {
8236 #ifdef CONFIG_RFS_ACCEL
8237 	int i, rc = 0;
8238 
8239 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8240 		return 0;
8241 
8242 	for (i = 0; i < bp->rx_nr_rings; i++) {
8243 		struct bnxt_vnic_info *vnic;
8244 		u16 vnic_id = i + 1;
8245 		u16 ring_id = i;
8246 
8247 		if (vnic_id >= bp->nr_vnics)
8248 			break;
8249 
8250 		vnic = &bp->vnic_info[vnic_id];
8251 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
8252 		if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
8253 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
8254 		rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
8255 		if (rc) {
8256 			netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
8257 				   vnic_id, rc);
8258 			break;
8259 		}
8260 		rc = bnxt_setup_vnic(bp, vnic_id);
8261 		if (rc)
8262 			break;
8263 	}
8264 	return rc;
8265 #else
8266 	return 0;
8267 #endif
8268 }
8269 
8270 /* Allow PF and VF with default VLAN to be in promiscuous mode */
bnxt_promisc_ok(struct bnxt * bp)8271 static bool bnxt_promisc_ok(struct bnxt *bp)
8272 {
8273 #ifdef CONFIG_BNXT_SRIOV
8274 	if (BNXT_VF(bp) && !bp->vf.vlan)
8275 		return false;
8276 #endif
8277 	return true;
8278 }
8279 
bnxt_setup_nitroa0_vnic(struct bnxt * bp)8280 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
8281 {
8282 	unsigned int rc = 0;
8283 
8284 	rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
8285 	if (rc) {
8286 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
8287 			   rc);
8288 		return rc;
8289 	}
8290 
8291 	rc = bnxt_hwrm_vnic_cfg(bp, 1);
8292 	if (rc) {
8293 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
8294 			   rc);
8295 		return rc;
8296 	}
8297 	return rc;
8298 }
8299 
8300 static int bnxt_cfg_rx_mode(struct bnxt *);
8301 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
8302 
bnxt_init_chip(struct bnxt * bp,bool irq_re_init)8303 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
8304 {
8305 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8306 	int rc = 0;
8307 	unsigned int rx_nr_rings = bp->rx_nr_rings;
8308 
8309 	if (irq_re_init) {
8310 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
8311 		if (rc) {
8312 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
8313 				   rc);
8314 			goto err_out;
8315 		}
8316 	}
8317 
8318 	rc = bnxt_hwrm_ring_alloc(bp);
8319 	if (rc) {
8320 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
8321 		goto err_out;
8322 	}
8323 
8324 	rc = bnxt_hwrm_ring_grp_alloc(bp);
8325 	if (rc) {
8326 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
8327 		goto err_out;
8328 	}
8329 
8330 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8331 		rx_nr_rings--;
8332 
8333 	/* default vnic 0 */
8334 	rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
8335 	if (rc) {
8336 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
8337 		goto err_out;
8338 	}
8339 
8340 	if (BNXT_VF(bp))
8341 		bnxt_hwrm_func_qcfg(bp);
8342 
8343 	rc = bnxt_setup_vnic(bp, 0);
8344 	if (rc)
8345 		goto err_out;
8346 
8347 	if (bp->flags & BNXT_FLAG_RFS) {
8348 		rc = bnxt_alloc_rfs_vnics(bp);
8349 		if (rc)
8350 			goto err_out;
8351 	}
8352 
8353 	if (bp->flags & BNXT_FLAG_TPA) {
8354 		rc = bnxt_set_tpa(bp, true);
8355 		if (rc)
8356 			goto err_out;
8357 	}
8358 
8359 	if (BNXT_VF(bp))
8360 		bnxt_update_vf_mac(bp);
8361 
8362 	/* Filter for default vnic 0 */
8363 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
8364 	if (rc) {
8365 		netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
8366 		goto err_out;
8367 	}
8368 	vnic->uc_filter_count = 1;
8369 
8370 	vnic->rx_mask = 0;
8371 	if (bp->dev->flags & IFF_BROADCAST)
8372 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
8373 
8374 	if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
8375 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
8376 
8377 	if (bp->dev->flags & IFF_ALLMULTI) {
8378 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
8379 		vnic->mc_list_count = 0;
8380 	} else {
8381 		u32 mask = 0;
8382 
8383 		bnxt_mc_list_updated(bp, &mask);
8384 		vnic->rx_mask |= mask;
8385 	}
8386 
8387 	rc = bnxt_cfg_rx_mode(bp);
8388 	if (rc)
8389 		goto err_out;
8390 
8391 	rc = bnxt_hwrm_set_coal(bp);
8392 	if (rc)
8393 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
8394 				rc);
8395 
8396 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8397 		rc = bnxt_setup_nitroa0_vnic(bp);
8398 		if (rc)
8399 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
8400 				   rc);
8401 	}
8402 
8403 	if (BNXT_VF(bp)) {
8404 		bnxt_hwrm_func_qcfg(bp);
8405 		netdev_update_features(bp->dev);
8406 	}
8407 
8408 	return 0;
8409 
8410 err_out:
8411 	bnxt_hwrm_resource_free(bp, 0, true);
8412 
8413 	return rc;
8414 }
8415 
bnxt_shutdown_nic(struct bnxt * bp,bool irq_re_init)8416 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
8417 {
8418 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
8419 	return 0;
8420 }
8421 
bnxt_init_nic(struct bnxt * bp,bool irq_re_init)8422 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
8423 {
8424 	bnxt_init_cp_rings(bp);
8425 	bnxt_init_rx_rings(bp);
8426 	bnxt_init_tx_rings(bp);
8427 	bnxt_init_ring_grps(bp, irq_re_init);
8428 	bnxt_init_vnics(bp);
8429 
8430 	return bnxt_init_chip(bp, irq_re_init);
8431 }
8432 
bnxt_set_real_num_queues(struct bnxt * bp)8433 static int bnxt_set_real_num_queues(struct bnxt *bp)
8434 {
8435 	int rc;
8436 	struct net_device *dev = bp->dev;
8437 
8438 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
8439 					  bp->tx_nr_rings_xdp);
8440 	if (rc)
8441 		return rc;
8442 
8443 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
8444 	if (rc)
8445 		return rc;
8446 
8447 #ifdef CONFIG_RFS_ACCEL
8448 	if (bp->flags & BNXT_FLAG_RFS)
8449 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
8450 #endif
8451 
8452 	return rc;
8453 }
8454 
bnxt_trim_rings(struct bnxt * bp,int * rx,int * tx,int max,bool shared)8455 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
8456 			   bool shared)
8457 {
8458 	int _rx = *rx, _tx = *tx;
8459 
8460 	if (shared) {
8461 		*rx = min_t(int, _rx, max);
8462 		*tx = min_t(int, _tx, max);
8463 	} else {
8464 		if (max < 2)
8465 			return -ENOMEM;
8466 
8467 		while (_rx + _tx > max) {
8468 			if (_rx > _tx && _rx > 1)
8469 				_rx--;
8470 			else if (_tx > 1)
8471 				_tx--;
8472 		}
8473 		*rx = _rx;
8474 		*tx = _tx;
8475 	}
8476 	return 0;
8477 }
8478 
bnxt_setup_msix(struct bnxt * bp)8479 static void bnxt_setup_msix(struct bnxt *bp)
8480 {
8481 	const int len = sizeof(bp->irq_tbl[0].name);
8482 	struct net_device *dev = bp->dev;
8483 	int tcs, i;
8484 
8485 	tcs = netdev_get_num_tc(dev);
8486 	if (tcs) {
8487 		int i, off, count;
8488 
8489 		for (i = 0; i < tcs; i++) {
8490 			count = bp->tx_nr_rings_per_tc;
8491 			off = i * count;
8492 			netdev_set_tc_queue(dev, i, count, off);
8493 		}
8494 	}
8495 
8496 	for (i = 0; i < bp->cp_nr_rings; i++) {
8497 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
8498 		char *attr;
8499 
8500 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8501 			attr = "TxRx";
8502 		else if (i < bp->rx_nr_rings)
8503 			attr = "rx";
8504 		else
8505 			attr = "tx";
8506 
8507 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
8508 			 attr, i);
8509 		bp->irq_tbl[map_idx].handler = bnxt_msix;
8510 	}
8511 }
8512 
bnxt_setup_inta(struct bnxt * bp)8513 static void bnxt_setup_inta(struct bnxt *bp)
8514 {
8515 	const int len = sizeof(bp->irq_tbl[0].name);
8516 
8517 	if (netdev_get_num_tc(bp->dev))
8518 		netdev_reset_tc(bp->dev);
8519 
8520 	snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
8521 		 0);
8522 	bp->irq_tbl[0].handler = bnxt_inta;
8523 }
8524 
8525 static int bnxt_init_int_mode(struct bnxt *bp);
8526 
bnxt_setup_int_mode(struct bnxt * bp)8527 static int bnxt_setup_int_mode(struct bnxt *bp)
8528 {
8529 	int rc;
8530 
8531 	if (!bp->irq_tbl) {
8532 		rc = bnxt_init_int_mode(bp);
8533 		if (rc || !bp->irq_tbl)
8534 			return rc ?: -ENODEV;
8535 	}
8536 
8537 	if (bp->flags & BNXT_FLAG_USING_MSIX)
8538 		bnxt_setup_msix(bp);
8539 	else
8540 		bnxt_setup_inta(bp);
8541 
8542 	rc = bnxt_set_real_num_queues(bp);
8543 	return rc;
8544 }
8545 
8546 #ifdef CONFIG_RFS_ACCEL
bnxt_get_max_func_rss_ctxs(struct bnxt * bp)8547 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
8548 {
8549 	return bp->hw_resc.max_rsscos_ctxs;
8550 }
8551 
bnxt_get_max_func_vnics(struct bnxt * bp)8552 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
8553 {
8554 	return bp->hw_resc.max_vnics;
8555 }
8556 #endif
8557 
bnxt_get_max_func_stat_ctxs(struct bnxt * bp)8558 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
8559 {
8560 	return bp->hw_resc.max_stat_ctxs;
8561 }
8562 
bnxt_get_max_func_cp_rings(struct bnxt * bp)8563 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
8564 {
8565 	return bp->hw_resc.max_cp_rings;
8566 }
8567 
bnxt_get_max_func_cp_rings_for_en(struct bnxt * bp)8568 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
8569 {
8570 	unsigned int cp = bp->hw_resc.max_cp_rings;
8571 
8572 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
8573 		cp -= bnxt_get_ulp_msix_num(bp);
8574 
8575 	return cp;
8576 }
8577 
bnxt_get_max_func_irqs(struct bnxt * bp)8578 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
8579 {
8580 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8581 
8582 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8583 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
8584 
8585 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
8586 }
8587 
bnxt_set_max_func_irqs(struct bnxt * bp,unsigned int max_irqs)8588 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
8589 {
8590 	bp->hw_resc.max_irqs = max_irqs;
8591 }
8592 
bnxt_get_avail_cp_rings_for_en(struct bnxt * bp)8593 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
8594 {
8595 	unsigned int cp;
8596 
8597 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
8598 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8599 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
8600 	else
8601 		return cp - bp->cp_nr_rings;
8602 }
8603 
bnxt_get_avail_stat_ctxs_for_en(struct bnxt * bp)8604 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
8605 {
8606 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
8607 }
8608 
bnxt_get_avail_msix(struct bnxt * bp,int num)8609 int bnxt_get_avail_msix(struct bnxt *bp, int num)
8610 {
8611 	int max_cp = bnxt_get_max_func_cp_rings(bp);
8612 	int max_irq = bnxt_get_max_func_irqs(bp);
8613 	int total_req = bp->cp_nr_rings + num;
8614 	int max_idx, avail_msix;
8615 
8616 	max_idx = bp->total_irqs;
8617 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
8618 		max_idx = min_t(int, bp->total_irqs, max_cp);
8619 	avail_msix = max_idx - bp->cp_nr_rings;
8620 	if (!BNXT_NEW_RM(bp) || avail_msix >= num)
8621 		return avail_msix;
8622 
8623 	if (max_irq < total_req) {
8624 		num = max_irq - bp->cp_nr_rings;
8625 		if (num <= 0)
8626 			return 0;
8627 	}
8628 	return num;
8629 }
8630 
bnxt_get_num_msix(struct bnxt * bp)8631 static int bnxt_get_num_msix(struct bnxt *bp)
8632 {
8633 	if (!BNXT_NEW_RM(bp))
8634 		return bnxt_get_max_func_irqs(bp);
8635 
8636 	return bnxt_nq_rings_in_use(bp);
8637 }
8638 
bnxt_init_msix(struct bnxt * bp)8639 static int bnxt_init_msix(struct bnxt *bp)
8640 {
8641 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix;
8642 	struct msix_entry *msix_ent;
8643 
8644 	total_vecs = bnxt_get_num_msix(bp);
8645 	max = bnxt_get_max_func_irqs(bp);
8646 	if (total_vecs > max)
8647 		total_vecs = max;
8648 
8649 	if (!total_vecs)
8650 		return 0;
8651 
8652 	msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
8653 	if (!msix_ent)
8654 		return -ENOMEM;
8655 
8656 	for (i = 0; i < total_vecs; i++) {
8657 		msix_ent[i].entry = i;
8658 		msix_ent[i].vector = 0;
8659 	}
8660 
8661 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
8662 		min = 2;
8663 
8664 	total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
8665 	ulp_msix = bnxt_get_ulp_msix_num(bp);
8666 	if (total_vecs < 0 || total_vecs < ulp_msix) {
8667 		rc = -ENODEV;
8668 		goto msix_setup_exit;
8669 	}
8670 
8671 	bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
8672 	if (bp->irq_tbl) {
8673 		for (i = 0; i < total_vecs; i++)
8674 			bp->irq_tbl[i].vector = msix_ent[i].vector;
8675 
8676 		bp->total_irqs = total_vecs;
8677 		/* Trim rings based upon num of vectors allocated */
8678 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
8679 				     total_vecs - ulp_msix, min == 1);
8680 		if (rc)
8681 			goto msix_setup_exit;
8682 
8683 		bp->cp_nr_rings = (min == 1) ?
8684 				  max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
8685 				  bp->tx_nr_rings + bp->rx_nr_rings;
8686 
8687 	} else {
8688 		rc = -ENOMEM;
8689 		goto msix_setup_exit;
8690 	}
8691 	bp->flags |= BNXT_FLAG_USING_MSIX;
8692 	kfree(msix_ent);
8693 	return 0;
8694 
8695 msix_setup_exit:
8696 	netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
8697 	kfree(bp->irq_tbl);
8698 	bp->irq_tbl = NULL;
8699 	pci_disable_msix(bp->pdev);
8700 	kfree(msix_ent);
8701 	return rc;
8702 }
8703 
bnxt_init_inta(struct bnxt * bp)8704 static int bnxt_init_inta(struct bnxt *bp)
8705 {
8706 	bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
8707 	if (!bp->irq_tbl)
8708 		return -ENOMEM;
8709 
8710 	bp->total_irqs = 1;
8711 	bp->rx_nr_rings = 1;
8712 	bp->tx_nr_rings = 1;
8713 	bp->cp_nr_rings = 1;
8714 	bp->flags |= BNXT_FLAG_SHARED_RINGS;
8715 	bp->irq_tbl[0].vector = bp->pdev->irq;
8716 	return 0;
8717 }
8718 
bnxt_init_int_mode(struct bnxt * bp)8719 static int bnxt_init_int_mode(struct bnxt *bp)
8720 {
8721 	int rc = -ENODEV;
8722 
8723 	if (bp->flags & BNXT_FLAG_MSIX_CAP)
8724 		rc = bnxt_init_msix(bp);
8725 
8726 	if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
8727 		/* fallback to INTA */
8728 		rc = bnxt_init_inta(bp);
8729 	}
8730 	return rc;
8731 }
8732 
bnxt_clear_int_mode(struct bnxt * bp)8733 static void bnxt_clear_int_mode(struct bnxt *bp)
8734 {
8735 	if (bp->flags & BNXT_FLAG_USING_MSIX)
8736 		pci_disable_msix(bp->pdev);
8737 
8738 	kfree(bp->irq_tbl);
8739 	bp->irq_tbl = NULL;
8740 	bp->flags &= ~BNXT_FLAG_USING_MSIX;
8741 }
8742 
bnxt_reserve_rings(struct bnxt * bp,bool irq_re_init)8743 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
8744 {
8745 	int tcs = netdev_get_num_tc(bp->dev);
8746 	bool irq_cleared = false;
8747 	int rc;
8748 
8749 	if (!bnxt_need_reserve_rings(bp))
8750 		return 0;
8751 
8752 	if (irq_re_init && BNXT_NEW_RM(bp) &&
8753 	    bnxt_get_num_msix(bp) != bp->total_irqs) {
8754 		bnxt_ulp_irq_stop(bp);
8755 		bnxt_clear_int_mode(bp);
8756 		irq_cleared = true;
8757 	}
8758 	rc = __bnxt_reserve_rings(bp);
8759 	if (irq_cleared) {
8760 		if (!rc)
8761 			rc = bnxt_init_int_mode(bp);
8762 		bnxt_ulp_irq_restart(bp, rc);
8763 	}
8764 	if (rc) {
8765 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
8766 		return rc;
8767 	}
8768 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
8769 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
8770 		netdev_err(bp->dev, "tx ring reservation failure\n");
8771 		netdev_reset_tc(bp->dev);
8772 		if (bp->tx_nr_rings_xdp)
8773 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
8774 		else
8775 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
8776 		return -ENOMEM;
8777 	}
8778 	return 0;
8779 }
8780 
bnxt_free_irq(struct bnxt * bp)8781 static void bnxt_free_irq(struct bnxt *bp)
8782 {
8783 	struct bnxt_irq *irq;
8784 	int i;
8785 
8786 #ifdef CONFIG_RFS_ACCEL
8787 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
8788 	bp->dev->rx_cpu_rmap = NULL;
8789 #endif
8790 	if (!bp->irq_tbl || !bp->bnapi)
8791 		return;
8792 
8793 	for (i = 0; i < bp->cp_nr_rings; i++) {
8794 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
8795 
8796 		irq = &bp->irq_tbl[map_idx];
8797 		if (irq->requested) {
8798 			if (irq->have_cpumask) {
8799 				irq_set_affinity_hint(irq->vector, NULL);
8800 				free_cpumask_var(irq->cpu_mask);
8801 				irq->have_cpumask = 0;
8802 			}
8803 			free_irq(irq->vector, bp->bnapi[i]);
8804 		}
8805 
8806 		irq->requested = 0;
8807 	}
8808 }
8809 
bnxt_request_irq(struct bnxt * bp)8810 static int bnxt_request_irq(struct bnxt *bp)
8811 {
8812 	int i, j, rc = 0;
8813 	unsigned long flags = 0;
8814 #ifdef CONFIG_RFS_ACCEL
8815 	struct cpu_rmap *rmap;
8816 #endif
8817 
8818 	rc = bnxt_setup_int_mode(bp);
8819 	if (rc) {
8820 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
8821 			   rc);
8822 		return rc;
8823 	}
8824 #ifdef CONFIG_RFS_ACCEL
8825 	rmap = bp->dev->rx_cpu_rmap;
8826 #endif
8827 	if (!(bp->flags & BNXT_FLAG_USING_MSIX))
8828 		flags = IRQF_SHARED;
8829 
8830 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
8831 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
8832 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
8833 
8834 #ifdef CONFIG_RFS_ACCEL
8835 		if (rmap && bp->bnapi[i]->rx_ring) {
8836 			rc = irq_cpu_rmap_add(rmap, irq->vector);
8837 			if (rc)
8838 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
8839 					    j);
8840 			j++;
8841 		}
8842 #endif
8843 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
8844 				 bp->bnapi[i]);
8845 		if (rc)
8846 			break;
8847 
8848 		irq->requested = 1;
8849 
8850 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
8851 			int numa_node = dev_to_node(&bp->pdev->dev);
8852 
8853 			irq->have_cpumask = 1;
8854 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
8855 					irq->cpu_mask);
8856 			rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
8857 			if (rc) {
8858 				netdev_warn(bp->dev,
8859 					    "Set affinity failed, IRQ = %d\n",
8860 					    irq->vector);
8861 				break;
8862 			}
8863 		}
8864 	}
8865 	return rc;
8866 }
8867 
bnxt_del_napi(struct bnxt * bp)8868 static void bnxt_del_napi(struct bnxt *bp)
8869 {
8870 	int i;
8871 
8872 	if (!bp->bnapi)
8873 		return;
8874 
8875 	for (i = 0; i < bp->cp_nr_rings; i++) {
8876 		struct bnxt_napi *bnapi = bp->bnapi[i];
8877 
8878 		__netif_napi_del(&bnapi->napi);
8879 	}
8880 	/* We called __netif_napi_del(), we need
8881 	 * to respect an RCU grace period before freeing napi structures.
8882 	 */
8883 	synchronize_net();
8884 }
8885 
bnxt_init_napi(struct bnxt * bp)8886 static void bnxt_init_napi(struct bnxt *bp)
8887 {
8888 	int i;
8889 	unsigned int cp_nr_rings = bp->cp_nr_rings;
8890 	struct bnxt_napi *bnapi;
8891 
8892 	if (bp->flags & BNXT_FLAG_USING_MSIX) {
8893 		int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
8894 
8895 		if (bp->flags & BNXT_FLAG_CHIP_P5)
8896 			poll_fn = bnxt_poll_p5;
8897 		else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8898 			cp_nr_rings--;
8899 		for (i = 0; i < cp_nr_rings; i++) {
8900 			bnapi = bp->bnapi[i];
8901 			netif_napi_add(bp->dev, &bnapi->napi, poll_fn, 64);
8902 		}
8903 		if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8904 			bnapi = bp->bnapi[cp_nr_rings];
8905 			netif_napi_add(bp->dev, &bnapi->napi,
8906 				       bnxt_poll_nitroa0, 64);
8907 		}
8908 	} else {
8909 		bnapi = bp->bnapi[0];
8910 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
8911 	}
8912 }
8913 
bnxt_disable_napi(struct bnxt * bp)8914 static void bnxt_disable_napi(struct bnxt *bp)
8915 {
8916 	int i;
8917 
8918 	if (!bp->bnapi)
8919 		return;
8920 
8921 	for (i = 0; i < bp->cp_nr_rings; i++) {
8922 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
8923 
8924 		napi_disable(&bp->bnapi[i]->napi);
8925 		if (bp->bnapi[i]->rx_ring)
8926 			cancel_work_sync(&cpr->dim.work);
8927 	}
8928 }
8929 
bnxt_enable_napi(struct bnxt * bp)8930 static void bnxt_enable_napi(struct bnxt *bp)
8931 {
8932 	int i;
8933 
8934 	for (i = 0; i < bp->cp_nr_rings; i++) {
8935 		struct bnxt_napi *bnapi = bp->bnapi[i];
8936 		struct bnxt_cp_ring_info *cpr;
8937 
8938 		cpr = &bnapi->cp_ring;
8939 		if (bnapi->in_reset)
8940 			cpr->sw_stats.rx.rx_resets++;
8941 		bnapi->in_reset = false;
8942 
8943 		if (bnapi->rx_ring) {
8944 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
8945 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
8946 		}
8947 		napi_enable(&bnapi->napi);
8948 	}
8949 }
8950 
bnxt_tx_disable(struct bnxt * bp)8951 void bnxt_tx_disable(struct bnxt *bp)
8952 {
8953 	int i;
8954 	struct bnxt_tx_ring_info *txr;
8955 
8956 	if (bp->tx_ring) {
8957 		for (i = 0; i < bp->tx_nr_rings; i++) {
8958 			txr = &bp->tx_ring[i];
8959 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
8960 		}
8961 	}
8962 	/* Make sure napi polls see @dev_state change */
8963 	synchronize_net();
8964 	/* Drop carrier first to prevent TX timeout */
8965 	netif_carrier_off(bp->dev);
8966 	/* Stop all TX queues */
8967 	netif_tx_disable(bp->dev);
8968 }
8969 
bnxt_tx_enable(struct bnxt * bp)8970 void bnxt_tx_enable(struct bnxt *bp)
8971 {
8972 	int i;
8973 	struct bnxt_tx_ring_info *txr;
8974 
8975 	for (i = 0; i < bp->tx_nr_rings; i++) {
8976 		txr = &bp->tx_ring[i];
8977 		WRITE_ONCE(txr->dev_state, 0);
8978 	}
8979 	/* Make sure napi polls see @dev_state change */
8980 	synchronize_net();
8981 	netif_tx_wake_all_queues(bp->dev);
8982 	if (bp->link_info.link_up)
8983 		netif_carrier_on(bp->dev);
8984 }
8985 
bnxt_report_fec(struct bnxt_link_info * link_info)8986 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
8987 {
8988 	u8 active_fec = link_info->active_fec_sig_mode &
8989 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
8990 
8991 	switch (active_fec) {
8992 	default:
8993 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
8994 		return "None";
8995 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
8996 		return "Clause 74 BaseR";
8997 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
8998 		return "Clause 91 RS(528,514)";
8999 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
9000 		return "Clause 91 RS544_1XN";
9001 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
9002 		return "Clause 91 RS(544,514)";
9003 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
9004 		return "Clause 91 RS272_1XN";
9005 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
9006 		return "Clause 91 RS(272,257)";
9007 	}
9008 }
9009 
bnxt_report_link(struct bnxt * bp)9010 static void bnxt_report_link(struct bnxt *bp)
9011 {
9012 	if (bp->link_info.link_up) {
9013 		const char *duplex;
9014 		const char *flow_ctrl;
9015 		u32 speed;
9016 		u16 fec;
9017 
9018 		netif_carrier_on(bp->dev);
9019 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
9020 		if (speed == SPEED_UNKNOWN) {
9021 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
9022 			return;
9023 		}
9024 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
9025 			duplex = "full";
9026 		else
9027 			duplex = "half";
9028 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
9029 			flow_ctrl = "ON - receive & transmit";
9030 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
9031 			flow_ctrl = "ON - transmit";
9032 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
9033 			flow_ctrl = "ON - receive";
9034 		else
9035 			flow_ctrl = "none";
9036 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
9037 			    speed, duplex, flow_ctrl);
9038 		if (bp->flags & BNXT_FLAG_EEE_CAP)
9039 			netdev_info(bp->dev, "EEE is %s\n",
9040 				    bp->eee.eee_active ? "active" :
9041 							 "not active");
9042 		fec = bp->link_info.fec_cfg;
9043 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
9044 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
9045 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
9046 				    bnxt_report_fec(&bp->link_info));
9047 	} else {
9048 		netif_carrier_off(bp->dev);
9049 		netdev_err(bp->dev, "NIC Link is Down\n");
9050 	}
9051 }
9052 
bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output * resp)9053 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
9054 {
9055 	if (!resp->supported_speeds_auto_mode &&
9056 	    !resp->supported_speeds_force_mode &&
9057 	    !resp->supported_pam4_speeds_auto_mode &&
9058 	    !resp->supported_pam4_speeds_force_mode)
9059 		return true;
9060 	return false;
9061 }
9062 
bnxt_hwrm_phy_qcaps(struct bnxt * bp)9063 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
9064 {
9065 	int rc = 0;
9066 	struct hwrm_port_phy_qcaps_input req = {0};
9067 	struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
9068 	struct bnxt_link_info *link_info = &bp->link_info;
9069 
9070 	bp->flags &= ~BNXT_FLAG_EEE_CAP;
9071 	if (bp->test_info)
9072 		bp->test_info->flags &= ~(BNXT_TEST_FL_EXT_LPBK |
9073 					  BNXT_TEST_FL_AN_PHY_LPBK);
9074 	if (bp->hwrm_spec_code < 0x10201)
9075 		return 0;
9076 
9077 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
9078 
9079 	mutex_lock(&bp->hwrm_cmd_lock);
9080 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9081 	if (rc)
9082 		goto hwrm_phy_qcaps_exit;
9083 
9084 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
9085 		struct ethtool_eee *eee = &bp->eee;
9086 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
9087 
9088 		bp->flags |= BNXT_FLAG_EEE_CAP;
9089 		eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9090 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
9091 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
9092 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
9093 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
9094 	}
9095 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EXTERNAL_LPBK_SUPPORTED) {
9096 		if (bp->test_info)
9097 			bp->test_info->flags |= BNXT_TEST_FL_EXT_LPBK;
9098 	}
9099 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_AUTONEG_LPBK_SUPPORTED) {
9100 		if (bp->test_info)
9101 			bp->test_info->flags |= BNXT_TEST_FL_AN_PHY_LPBK;
9102 	}
9103 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_SHARED_PHY_CFG_SUPPORTED) {
9104 		if (BNXT_PF(bp))
9105 			bp->fw_cap |= BNXT_FW_CAP_SHARED_PORT_CFG;
9106 	}
9107 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_CUMULATIVE_COUNTERS_ON_RESET)
9108 		bp->fw_cap |= BNXT_FW_CAP_PORT_STATS_NO_RESET;
9109 
9110 	if (bp->hwrm_spec_code >= 0x10a01) {
9111 		if (bnxt_phy_qcaps_no_speed(resp)) {
9112 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
9113 			netdev_warn(bp->dev, "Ethernet link disabled\n");
9114 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
9115 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
9116 			netdev_info(bp->dev, "Ethernet link enabled\n");
9117 			/* Phy re-enabled, reprobe the speeds */
9118 			link_info->support_auto_speeds = 0;
9119 			link_info->support_pam4_auto_speeds = 0;
9120 		}
9121 	}
9122 	if (resp->supported_speeds_auto_mode)
9123 		link_info->support_auto_speeds =
9124 			le16_to_cpu(resp->supported_speeds_auto_mode);
9125 	if (resp->supported_pam4_speeds_auto_mode)
9126 		link_info->support_pam4_auto_speeds =
9127 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
9128 
9129 	bp->port_count = resp->port_cnt;
9130 
9131 hwrm_phy_qcaps_exit:
9132 	mutex_unlock(&bp->hwrm_cmd_lock);
9133 	return rc;
9134 }
9135 
bnxt_support_dropped(u16 advertising,u16 supported)9136 static bool bnxt_support_dropped(u16 advertising, u16 supported)
9137 {
9138 	u16 diff = advertising ^ supported;
9139 
9140 	return ((supported | diff) != supported);
9141 }
9142 
bnxt_update_link(struct bnxt * bp,bool chng_link_state)9143 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
9144 {
9145 	int rc = 0;
9146 	struct bnxt_link_info *link_info = &bp->link_info;
9147 	struct hwrm_port_phy_qcfg_input req = {0};
9148 	struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
9149 	u8 link_up = link_info->link_up;
9150 	bool support_changed = false;
9151 
9152 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
9153 
9154 	mutex_lock(&bp->hwrm_cmd_lock);
9155 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9156 	if (rc) {
9157 		mutex_unlock(&bp->hwrm_cmd_lock);
9158 		return rc;
9159 	}
9160 
9161 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
9162 	link_info->phy_link_status = resp->link;
9163 	link_info->duplex = resp->duplex_cfg;
9164 	if (bp->hwrm_spec_code >= 0x10800)
9165 		link_info->duplex = resp->duplex_state;
9166 	link_info->pause = resp->pause;
9167 	link_info->auto_mode = resp->auto_mode;
9168 	link_info->auto_pause_setting = resp->auto_pause;
9169 	link_info->lp_pause = resp->link_partner_adv_pause;
9170 	link_info->force_pause_setting = resp->force_pause;
9171 	link_info->duplex_setting = resp->duplex_cfg;
9172 	if (link_info->phy_link_status == BNXT_LINK_LINK)
9173 		link_info->link_speed = le16_to_cpu(resp->link_speed);
9174 	else
9175 		link_info->link_speed = 0;
9176 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
9177 	link_info->force_pam4_link_speed =
9178 		le16_to_cpu(resp->force_pam4_link_speed);
9179 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
9180 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
9181 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
9182 	link_info->auto_pam4_link_speeds =
9183 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
9184 	link_info->lp_auto_link_speeds =
9185 		le16_to_cpu(resp->link_partner_adv_speeds);
9186 	link_info->lp_auto_pam4_link_speeds =
9187 		resp->link_partner_pam4_adv_speeds;
9188 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
9189 	link_info->phy_ver[0] = resp->phy_maj;
9190 	link_info->phy_ver[1] = resp->phy_min;
9191 	link_info->phy_ver[2] = resp->phy_bld;
9192 	link_info->media_type = resp->media_type;
9193 	link_info->phy_type = resp->phy_type;
9194 	link_info->transceiver = resp->xcvr_pkg_type;
9195 	link_info->phy_addr = resp->eee_config_phy_addr &
9196 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
9197 	link_info->module_status = resp->module_status;
9198 
9199 	if (bp->flags & BNXT_FLAG_EEE_CAP) {
9200 		struct ethtool_eee *eee = &bp->eee;
9201 		u16 fw_speeds;
9202 
9203 		eee->eee_active = 0;
9204 		if (resp->eee_config_phy_addr &
9205 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
9206 			eee->eee_active = 1;
9207 			fw_speeds = le16_to_cpu(
9208 				resp->link_partner_adv_eee_link_speed_mask);
9209 			eee->lp_advertised =
9210 				_bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9211 		}
9212 
9213 		/* Pull initial EEE config */
9214 		if (!chng_link_state) {
9215 			if (resp->eee_config_phy_addr &
9216 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
9217 				eee->eee_enabled = 1;
9218 
9219 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
9220 			eee->advertised =
9221 				_bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9222 
9223 			if (resp->eee_config_phy_addr &
9224 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
9225 				__le32 tmr;
9226 
9227 				eee->tx_lpi_enabled = 1;
9228 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
9229 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
9230 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
9231 			}
9232 		}
9233 	}
9234 
9235 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
9236 	if (bp->hwrm_spec_code >= 0x10504) {
9237 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
9238 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
9239 	}
9240 	/* TODO: need to add more logic to report VF link */
9241 	if (chng_link_state) {
9242 		if (link_info->phy_link_status == BNXT_LINK_LINK)
9243 			link_info->link_up = 1;
9244 		else
9245 			link_info->link_up = 0;
9246 		if (link_up != link_info->link_up)
9247 			bnxt_report_link(bp);
9248 	} else {
9249 		/* alwasy link down if not require to update link state */
9250 		link_info->link_up = 0;
9251 	}
9252 	mutex_unlock(&bp->hwrm_cmd_lock);
9253 
9254 	if (!BNXT_PHY_CFG_ABLE(bp))
9255 		return 0;
9256 
9257 	/* Check if any advertised speeds are no longer supported. The caller
9258 	 * holds the link_lock mutex, so we can modify link_info settings.
9259 	 */
9260 	if (bnxt_support_dropped(link_info->advertising,
9261 				 link_info->support_auto_speeds)) {
9262 		link_info->advertising = link_info->support_auto_speeds;
9263 		support_changed = true;
9264 	}
9265 	if (bnxt_support_dropped(link_info->advertising_pam4,
9266 				 link_info->support_pam4_auto_speeds)) {
9267 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
9268 		support_changed = true;
9269 	}
9270 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
9271 		bnxt_hwrm_set_link_setting(bp, true, false);
9272 	return 0;
9273 }
9274 
bnxt_get_port_module_status(struct bnxt * bp)9275 static void bnxt_get_port_module_status(struct bnxt *bp)
9276 {
9277 	struct bnxt_link_info *link_info = &bp->link_info;
9278 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
9279 	u8 module_status;
9280 
9281 	if (bnxt_update_link(bp, true))
9282 		return;
9283 
9284 	module_status = link_info->module_status;
9285 	switch (module_status) {
9286 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
9287 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
9288 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
9289 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
9290 			    bp->pf.port_id);
9291 		if (bp->hwrm_spec_code >= 0x10201) {
9292 			netdev_warn(bp->dev, "Module part number %s\n",
9293 				    resp->phy_vendor_partnumber);
9294 		}
9295 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
9296 			netdev_warn(bp->dev, "TX is disabled\n");
9297 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
9298 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
9299 	}
9300 }
9301 
9302 static void
bnxt_hwrm_set_pause_common(struct bnxt * bp,struct hwrm_port_phy_cfg_input * req)9303 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
9304 {
9305 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
9306 		if (bp->hwrm_spec_code >= 0x10201)
9307 			req->auto_pause =
9308 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
9309 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
9310 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
9311 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
9312 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
9313 		req->enables |=
9314 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
9315 	} else {
9316 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
9317 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
9318 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
9319 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
9320 		req->enables |=
9321 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
9322 		if (bp->hwrm_spec_code >= 0x10201) {
9323 			req->auto_pause = req->force_pause;
9324 			req->enables |= cpu_to_le32(
9325 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
9326 		}
9327 	}
9328 }
9329 
bnxt_hwrm_set_link_common(struct bnxt * bp,struct hwrm_port_phy_cfg_input * req)9330 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
9331 {
9332 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
9333 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
9334 		if (bp->link_info.advertising) {
9335 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
9336 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
9337 		}
9338 		if (bp->link_info.advertising_pam4) {
9339 			req->enables |=
9340 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
9341 			req->auto_link_pam4_speed_mask =
9342 				cpu_to_le16(bp->link_info.advertising_pam4);
9343 		}
9344 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
9345 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
9346 	} else {
9347 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
9348 		if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
9349 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
9350 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
9351 		} else {
9352 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
9353 		}
9354 	}
9355 
9356 	/* tell chimp that the setting takes effect immediately */
9357 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
9358 }
9359 
bnxt_hwrm_set_pause(struct bnxt * bp)9360 int bnxt_hwrm_set_pause(struct bnxt *bp)
9361 {
9362 	struct hwrm_port_phy_cfg_input req = {0};
9363 	int rc;
9364 
9365 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
9366 	bnxt_hwrm_set_pause_common(bp, &req);
9367 
9368 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
9369 	    bp->link_info.force_link_chng)
9370 		bnxt_hwrm_set_link_common(bp, &req);
9371 
9372 	mutex_lock(&bp->hwrm_cmd_lock);
9373 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9374 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
9375 		/* since changing of pause setting doesn't trigger any link
9376 		 * change event, the driver needs to update the current pause
9377 		 * result upon successfully return of the phy_cfg command
9378 		 */
9379 		bp->link_info.pause =
9380 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
9381 		bp->link_info.auto_pause_setting = 0;
9382 		if (!bp->link_info.force_link_chng)
9383 			bnxt_report_link(bp);
9384 	}
9385 	bp->link_info.force_link_chng = false;
9386 	mutex_unlock(&bp->hwrm_cmd_lock);
9387 	return rc;
9388 }
9389 
bnxt_hwrm_set_eee(struct bnxt * bp,struct hwrm_port_phy_cfg_input * req)9390 static void bnxt_hwrm_set_eee(struct bnxt *bp,
9391 			      struct hwrm_port_phy_cfg_input *req)
9392 {
9393 	struct ethtool_eee *eee = &bp->eee;
9394 
9395 	if (eee->eee_enabled) {
9396 		u16 eee_speeds;
9397 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
9398 
9399 		if (eee->tx_lpi_enabled)
9400 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
9401 		else
9402 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
9403 
9404 		req->flags |= cpu_to_le32(flags);
9405 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
9406 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
9407 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
9408 	} else {
9409 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
9410 	}
9411 }
9412 
bnxt_hwrm_set_link_setting(struct bnxt * bp,bool set_pause,bool set_eee)9413 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
9414 {
9415 	struct hwrm_port_phy_cfg_input req = {0};
9416 
9417 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
9418 	if (set_pause)
9419 		bnxt_hwrm_set_pause_common(bp, &req);
9420 
9421 	bnxt_hwrm_set_link_common(bp, &req);
9422 
9423 	if (set_eee)
9424 		bnxt_hwrm_set_eee(bp, &req);
9425 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9426 }
9427 
bnxt_hwrm_shutdown_link(struct bnxt * bp)9428 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
9429 {
9430 	struct hwrm_port_phy_cfg_input req = {0};
9431 
9432 	if (!BNXT_SINGLE_PF(bp))
9433 		return 0;
9434 
9435 	if (pci_num_vf(bp->pdev))
9436 		return 0;
9437 
9438 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
9439 	req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
9440 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9441 }
9442 
9443 static int bnxt_fw_init_one(struct bnxt *bp);
9444 
bnxt_hwrm_if_change(struct bnxt * bp,bool up)9445 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
9446 {
9447 	struct hwrm_func_drv_if_change_output *resp = bp->hwrm_cmd_resp_addr;
9448 	struct hwrm_func_drv_if_change_input req = {0};
9449 	bool fw_reset = !bp->irq_tbl;
9450 	bool resc_reinit = false;
9451 	u32 flags = 0;
9452 	int rc;
9453 
9454 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
9455 		return 0;
9456 
9457 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_IF_CHANGE, -1, -1);
9458 	if (up)
9459 		req.flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
9460 	mutex_lock(&bp->hwrm_cmd_lock);
9461 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9462 	if (!rc)
9463 		flags = le32_to_cpu(resp->flags);
9464 	mutex_unlock(&bp->hwrm_cmd_lock);
9465 	if (rc)
9466 		return rc;
9467 
9468 	if (!up)
9469 		return 0;
9470 
9471 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
9472 		resc_reinit = true;
9473 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE)
9474 		fw_reset = true;
9475 
9476 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
9477 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
9478 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
9479 		return -ENODEV;
9480 	}
9481 	if (resc_reinit || fw_reset) {
9482 		if (fw_reset) {
9483 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
9484 				bnxt_ulp_stop(bp);
9485 			bnxt_free_ctx_mem(bp);
9486 			kfree(bp->ctx);
9487 			bp->ctx = NULL;
9488 			bnxt_dcb_free(bp);
9489 			rc = bnxt_fw_init_one(bp);
9490 			if (rc) {
9491 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
9492 				return rc;
9493 			}
9494 			bnxt_clear_int_mode(bp);
9495 			rc = bnxt_init_int_mode(bp);
9496 			if (rc) {
9497 				netdev_err(bp->dev, "init int mode failed\n");
9498 				return rc;
9499 			}
9500 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
9501 		}
9502 		if (BNXT_NEW_RM(bp)) {
9503 			struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9504 
9505 			rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9506 			hw_resc->resv_cp_rings = 0;
9507 			hw_resc->resv_stat_ctxs = 0;
9508 			hw_resc->resv_irqs = 0;
9509 			hw_resc->resv_tx_rings = 0;
9510 			hw_resc->resv_rx_rings = 0;
9511 			hw_resc->resv_hw_ring_grps = 0;
9512 			hw_resc->resv_vnics = 0;
9513 			if (!fw_reset) {
9514 				bp->tx_nr_rings = 0;
9515 				bp->rx_nr_rings = 0;
9516 			}
9517 		}
9518 	}
9519 	return 0;
9520 }
9521 
bnxt_hwrm_port_led_qcaps(struct bnxt * bp)9522 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
9523 {
9524 	struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
9525 	struct hwrm_port_led_qcaps_input req = {0};
9526 	struct bnxt_pf_info *pf = &bp->pf;
9527 	int rc;
9528 
9529 	bp->num_leds = 0;
9530 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
9531 		return 0;
9532 
9533 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
9534 	req.port_id = cpu_to_le16(pf->port_id);
9535 	mutex_lock(&bp->hwrm_cmd_lock);
9536 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9537 	if (rc) {
9538 		mutex_unlock(&bp->hwrm_cmd_lock);
9539 		return rc;
9540 	}
9541 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
9542 		int i;
9543 
9544 		bp->num_leds = resp->num_leds;
9545 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
9546 						 bp->num_leds);
9547 		for (i = 0; i < bp->num_leds; i++) {
9548 			struct bnxt_led_info *led = &bp->leds[i];
9549 			__le16 caps = led->led_state_caps;
9550 
9551 			if (!led->led_group_id ||
9552 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
9553 				bp->num_leds = 0;
9554 				break;
9555 			}
9556 		}
9557 	}
9558 	mutex_unlock(&bp->hwrm_cmd_lock);
9559 	return 0;
9560 }
9561 
bnxt_hwrm_alloc_wol_fltr(struct bnxt * bp)9562 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
9563 {
9564 	struct hwrm_wol_filter_alloc_input req = {0};
9565 	struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
9566 	int rc;
9567 
9568 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
9569 	req.port_id = cpu_to_le16(bp->pf.port_id);
9570 	req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
9571 	req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
9572 	memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
9573 	mutex_lock(&bp->hwrm_cmd_lock);
9574 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9575 	if (!rc)
9576 		bp->wol_filter_id = resp->wol_filter_id;
9577 	mutex_unlock(&bp->hwrm_cmd_lock);
9578 	return rc;
9579 }
9580 
bnxt_hwrm_free_wol_fltr(struct bnxt * bp)9581 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
9582 {
9583 	struct hwrm_wol_filter_free_input req = {0};
9584 
9585 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
9586 	req.port_id = cpu_to_le16(bp->pf.port_id);
9587 	req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
9588 	req.wol_filter_id = bp->wol_filter_id;
9589 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9590 }
9591 
bnxt_hwrm_get_wol_fltrs(struct bnxt * bp,u16 handle)9592 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
9593 {
9594 	struct hwrm_wol_filter_qcfg_input req = {0};
9595 	struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
9596 	u16 next_handle = 0;
9597 	int rc;
9598 
9599 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
9600 	req.port_id = cpu_to_le16(bp->pf.port_id);
9601 	req.handle = cpu_to_le16(handle);
9602 	mutex_lock(&bp->hwrm_cmd_lock);
9603 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9604 	if (!rc) {
9605 		next_handle = le16_to_cpu(resp->next_handle);
9606 		if (next_handle != 0) {
9607 			if (resp->wol_type ==
9608 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
9609 				bp->wol = 1;
9610 				bp->wol_filter_id = resp->wol_filter_id;
9611 			}
9612 		}
9613 	}
9614 	mutex_unlock(&bp->hwrm_cmd_lock);
9615 	return next_handle;
9616 }
9617 
bnxt_get_wol_settings(struct bnxt * bp)9618 static void bnxt_get_wol_settings(struct bnxt *bp)
9619 {
9620 	u16 handle = 0;
9621 
9622 	bp->wol = 0;
9623 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
9624 		return;
9625 
9626 	do {
9627 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
9628 	} while (handle && handle != 0xffff);
9629 }
9630 
9631 #ifdef CONFIG_BNXT_HWMON
bnxt_show_temp(struct device * dev,struct device_attribute * devattr,char * buf)9632 static ssize_t bnxt_show_temp(struct device *dev,
9633 			      struct device_attribute *devattr, char *buf)
9634 {
9635 	struct hwrm_temp_monitor_query_input req = {0};
9636 	struct hwrm_temp_monitor_query_output *resp;
9637 	struct bnxt *bp = dev_get_drvdata(dev);
9638 	u32 len = 0;
9639 	int rc;
9640 
9641 	resp = bp->hwrm_cmd_resp_addr;
9642 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TEMP_MONITOR_QUERY, -1, -1);
9643 	mutex_lock(&bp->hwrm_cmd_lock);
9644 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9645 	if (!rc)
9646 		len = sprintf(buf, "%u\n", resp->temp * 1000); /* display millidegree */
9647 	mutex_unlock(&bp->hwrm_cmd_lock);
9648 	if (rc)
9649 		return rc;
9650 	return len;
9651 }
9652 static SENSOR_DEVICE_ATTR(temp1_input, 0444, bnxt_show_temp, NULL, 0);
9653 
9654 static struct attribute *bnxt_attrs[] = {
9655 	&sensor_dev_attr_temp1_input.dev_attr.attr,
9656 	NULL
9657 };
9658 ATTRIBUTE_GROUPS(bnxt);
9659 
bnxt_hwmon_close(struct bnxt * bp)9660 static void bnxt_hwmon_close(struct bnxt *bp)
9661 {
9662 	if (bp->hwmon_dev) {
9663 		hwmon_device_unregister(bp->hwmon_dev);
9664 		bp->hwmon_dev = NULL;
9665 	}
9666 }
9667 
bnxt_hwmon_open(struct bnxt * bp)9668 static void bnxt_hwmon_open(struct bnxt *bp)
9669 {
9670 	struct hwrm_temp_monitor_query_input req = {0};
9671 	struct pci_dev *pdev = bp->pdev;
9672 	int rc;
9673 
9674 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TEMP_MONITOR_QUERY, -1, -1);
9675 	rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9676 	if (rc == -EACCES || rc == -EOPNOTSUPP) {
9677 		bnxt_hwmon_close(bp);
9678 		return;
9679 	}
9680 
9681 	if (bp->hwmon_dev)
9682 		return;
9683 
9684 	bp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev,
9685 							  DRV_MODULE_NAME, bp,
9686 							  bnxt_groups);
9687 	if (IS_ERR(bp->hwmon_dev)) {
9688 		bp->hwmon_dev = NULL;
9689 		dev_warn(&pdev->dev, "Cannot register hwmon device\n");
9690 	}
9691 }
9692 #else
bnxt_hwmon_close(struct bnxt * bp)9693 static void bnxt_hwmon_close(struct bnxt *bp)
9694 {
9695 }
9696 
bnxt_hwmon_open(struct bnxt * bp)9697 static void bnxt_hwmon_open(struct bnxt *bp)
9698 {
9699 }
9700 #endif
9701 
bnxt_eee_config_ok(struct bnxt * bp)9702 static bool bnxt_eee_config_ok(struct bnxt *bp)
9703 {
9704 	struct ethtool_eee *eee = &bp->eee;
9705 	struct bnxt_link_info *link_info = &bp->link_info;
9706 
9707 	if (!(bp->flags & BNXT_FLAG_EEE_CAP))
9708 		return true;
9709 
9710 	if (eee->eee_enabled) {
9711 		u32 advertising =
9712 			_bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
9713 
9714 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
9715 			eee->eee_enabled = 0;
9716 			return false;
9717 		}
9718 		if (eee->advertised & ~advertising) {
9719 			eee->advertised = advertising & eee->supported;
9720 			return false;
9721 		}
9722 	}
9723 	return true;
9724 }
9725 
bnxt_update_phy_setting(struct bnxt * bp)9726 static int bnxt_update_phy_setting(struct bnxt *bp)
9727 {
9728 	int rc;
9729 	bool update_link = false;
9730 	bool update_pause = false;
9731 	bool update_eee = false;
9732 	struct bnxt_link_info *link_info = &bp->link_info;
9733 
9734 	rc = bnxt_update_link(bp, true);
9735 	if (rc) {
9736 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
9737 			   rc);
9738 		return rc;
9739 	}
9740 	if (!BNXT_SINGLE_PF(bp))
9741 		return 0;
9742 
9743 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
9744 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
9745 	    link_info->req_flow_ctrl)
9746 		update_pause = true;
9747 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
9748 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
9749 		update_pause = true;
9750 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
9751 		if (BNXT_AUTO_MODE(link_info->auto_mode))
9752 			update_link = true;
9753 		if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
9754 		    link_info->req_link_speed != link_info->force_link_speed)
9755 			update_link = true;
9756 		else if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
9757 			 link_info->req_link_speed != link_info->force_pam4_link_speed)
9758 			update_link = true;
9759 		if (link_info->req_duplex != link_info->duplex_setting)
9760 			update_link = true;
9761 	} else {
9762 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
9763 			update_link = true;
9764 		if (link_info->advertising != link_info->auto_link_speeds ||
9765 		    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
9766 			update_link = true;
9767 	}
9768 
9769 	/* The last close may have shutdown the link, so need to call
9770 	 * PHY_CFG to bring it back up.
9771 	 */
9772 	if (!bp->link_info.link_up)
9773 		update_link = true;
9774 
9775 	if (!bnxt_eee_config_ok(bp))
9776 		update_eee = true;
9777 
9778 	if (update_link)
9779 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
9780 	else if (update_pause)
9781 		rc = bnxt_hwrm_set_pause(bp);
9782 	if (rc) {
9783 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
9784 			   rc);
9785 		return rc;
9786 	}
9787 
9788 	return rc;
9789 }
9790 
9791 /* Common routine to pre-map certain register block to different GRC window.
9792  * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
9793  * in PF and 3 windows in VF that can be customized to map in different
9794  * register blocks.
9795  */
bnxt_preset_reg_win(struct bnxt * bp)9796 static void bnxt_preset_reg_win(struct bnxt *bp)
9797 {
9798 	if (BNXT_PF(bp)) {
9799 		/* CAG registers map to GRC window #4 */
9800 		writel(BNXT_CAG_REG_BASE,
9801 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
9802 	}
9803 }
9804 
9805 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
9806 
__bnxt_open_nic(struct bnxt * bp,bool irq_re_init,bool link_re_init)9807 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
9808 {
9809 	int rc = 0;
9810 
9811 	bnxt_preset_reg_win(bp);
9812 	netif_carrier_off(bp->dev);
9813 	if (irq_re_init) {
9814 		/* Reserve rings now if none were reserved at driver probe. */
9815 		rc = bnxt_init_dflt_ring_mode(bp);
9816 		if (rc) {
9817 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
9818 			return rc;
9819 		}
9820 	}
9821 	rc = bnxt_reserve_rings(bp, irq_re_init);
9822 	if (rc)
9823 		return rc;
9824 	if ((bp->flags & BNXT_FLAG_RFS) &&
9825 	    !(bp->flags & BNXT_FLAG_USING_MSIX)) {
9826 		/* disable RFS if falling back to INTA */
9827 		bp->dev->hw_features &= ~NETIF_F_NTUPLE;
9828 		bp->flags &= ~BNXT_FLAG_RFS;
9829 	}
9830 
9831 	rc = bnxt_alloc_mem(bp, irq_re_init);
9832 	if (rc) {
9833 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
9834 		goto open_err_free_mem;
9835 	}
9836 
9837 	if (irq_re_init) {
9838 		bnxt_init_napi(bp);
9839 		rc = bnxt_request_irq(bp);
9840 		if (rc) {
9841 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
9842 			goto open_err_irq;
9843 		}
9844 	}
9845 
9846 	rc = bnxt_init_nic(bp, irq_re_init);
9847 	if (rc) {
9848 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
9849 		goto open_err_irq;
9850 	}
9851 
9852 	bnxt_enable_napi(bp);
9853 	bnxt_debug_dev_init(bp);
9854 
9855 	if (link_re_init) {
9856 		mutex_lock(&bp->link_lock);
9857 		rc = bnxt_update_phy_setting(bp);
9858 		mutex_unlock(&bp->link_lock);
9859 		if (rc) {
9860 			netdev_warn(bp->dev, "failed to update phy settings\n");
9861 			if (BNXT_SINGLE_PF(bp)) {
9862 				bp->link_info.phy_retry = true;
9863 				bp->link_info.phy_retry_expires =
9864 					jiffies + 5 * HZ;
9865 			}
9866 		}
9867 	}
9868 
9869 	if (irq_re_init)
9870 		udp_tunnel_nic_reset_ntf(bp->dev);
9871 
9872 	set_bit(BNXT_STATE_OPEN, &bp->state);
9873 	bnxt_enable_int(bp);
9874 	/* Enable TX queues */
9875 	bnxt_tx_enable(bp);
9876 	mod_timer(&bp->timer, jiffies + bp->current_interval);
9877 	/* Poll link status and check for SFP+ module status */
9878 	bnxt_get_port_module_status(bp);
9879 
9880 	/* VF-reps may need to be re-opened after the PF is re-opened */
9881 	if (BNXT_PF(bp))
9882 		bnxt_vf_reps_open(bp);
9883 	return 0;
9884 
9885 open_err_irq:
9886 	bnxt_del_napi(bp);
9887 
9888 open_err_free_mem:
9889 	bnxt_free_skbs(bp);
9890 	bnxt_free_irq(bp);
9891 	bnxt_free_mem(bp, true);
9892 	return rc;
9893 }
9894 
9895 /* rtnl_lock held */
bnxt_open_nic(struct bnxt * bp,bool irq_re_init,bool link_re_init)9896 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
9897 {
9898 	int rc = 0;
9899 
9900 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
9901 		rc = -EIO;
9902 	if (!rc)
9903 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
9904 	if (rc) {
9905 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
9906 		dev_close(bp->dev);
9907 	}
9908 	return rc;
9909 }
9910 
9911 /* rtnl_lock held, open the NIC half way by allocating all resources, but
9912  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
9913  * self tests.
9914  */
bnxt_half_open_nic(struct bnxt * bp)9915 int bnxt_half_open_nic(struct bnxt *bp)
9916 {
9917 	int rc = 0;
9918 
9919 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
9920 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
9921 		rc = -ENODEV;
9922 		goto half_open_err;
9923 	}
9924 
9925 	rc = bnxt_alloc_mem(bp, false);
9926 	if (rc) {
9927 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
9928 		goto half_open_err;
9929 	}
9930 	rc = bnxt_init_nic(bp, false);
9931 	if (rc) {
9932 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
9933 		goto half_open_err;
9934 	}
9935 	return 0;
9936 
9937 half_open_err:
9938 	bnxt_free_skbs(bp);
9939 	bnxt_free_mem(bp, false);
9940 	dev_close(bp->dev);
9941 	return rc;
9942 }
9943 
9944 /* rtnl_lock held, this call can only be made after a previous successful
9945  * call to bnxt_half_open_nic().
9946  */
bnxt_half_close_nic(struct bnxt * bp)9947 void bnxt_half_close_nic(struct bnxt *bp)
9948 {
9949 	bnxt_hwrm_resource_free(bp, false, false);
9950 	bnxt_free_skbs(bp);
9951 	bnxt_free_mem(bp, false);
9952 }
9953 
bnxt_reenable_sriov(struct bnxt * bp)9954 static void bnxt_reenable_sriov(struct bnxt *bp)
9955 {
9956 	if (BNXT_PF(bp)) {
9957 		struct bnxt_pf_info *pf = &bp->pf;
9958 		int n = pf->active_vfs;
9959 
9960 		if (n)
9961 			bnxt_cfg_hw_sriov(bp, &n, true);
9962 	}
9963 }
9964 
bnxt_open(struct net_device * dev)9965 static int bnxt_open(struct net_device *dev)
9966 {
9967 	struct bnxt *bp = netdev_priv(dev);
9968 	int rc;
9969 
9970 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
9971 		netdev_err(bp->dev, "A previous firmware reset did not complete, aborting\n");
9972 		return -ENODEV;
9973 	}
9974 
9975 	rc = bnxt_hwrm_if_change(bp, true);
9976 	if (rc)
9977 		return rc;
9978 	rc = __bnxt_open_nic(bp, true, true);
9979 	if (rc) {
9980 		bnxt_hwrm_if_change(bp, false);
9981 	} else {
9982 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
9983 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
9984 				bnxt_ulp_start(bp, 0);
9985 				bnxt_reenable_sriov(bp);
9986 			}
9987 		}
9988 		bnxt_hwmon_open(bp);
9989 	}
9990 
9991 	return rc;
9992 }
9993 
bnxt_drv_busy(struct bnxt * bp)9994 static bool bnxt_drv_busy(struct bnxt *bp)
9995 {
9996 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
9997 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
9998 }
9999 
10000 static void bnxt_get_ring_stats(struct bnxt *bp,
10001 				struct rtnl_link_stats64 *stats);
10002 
__bnxt_close_nic(struct bnxt * bp,bool irq_re_init,bool link_re_init)10003 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
10004 			     bool link_re_init)
10005 {
10006 	/* Close the VF-reps before closing PF */
10007 	if (BNXT_PF(bp))
10008 		bnxt_vf_reps_close(bp);
10009 
10010 	/* Change device state to avoid TX queue wake up's */
10011 	bnxt_tx_disable(bp);
10012 
10013 	clear_bit(BNXT_STATE_OPEN, &bp->state);
10014 	smp_mb__after_atomic();
10015 	while (bnxt_drv_busy(bp))
10016 		msleep(20);
10017 
10018 	/* Flush rings and and disable interrupts */
10019 	bnxt_shutdown_nic(bp, irq_re_init);
10020 
10021 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
10022 
10023 	bnxt_debug_dev_exit(bp);
10024 	bnxt_disable_napi(bp);
10025 	del_timer_sync(&bp->timer);
10026 	bnxt_free_skbs(bp);
10027 
10028 	/* Save ring stats before shutdown */
10029 	if (bp->bnapi && irq_re_init)
10030 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
10031 	if (irq_re_init) {
10032 		bnxt_free_irq(bp);
10033 		bnxt_del_napi(bp);
10034 	}
10035 	bnxt_free_mem(bp, irq_re_init);
10036 }
10037 
bnxt_close_nic(struct bnxt * bp,bool irq_re_init,bool link_re_init)10038 int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10039 {
10040 	int rc = 0;
10041 
10042 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
10043 		/* If we get here, it means firmware reset is in progress
10044 		 * while we are trying to close.  We can safely proceed with
10045 		 * the close because we are holding rtnl_lock().  Some firmware
10046 		 * messages may fail as we proceed to close.  We set the
10047 		 * ABORT_ERR flag here so that the FW reset thread will later
10048 		 * abort when it gets the rtnl_lock() and sees the flag.
10049 		 */
10050 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
10051 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10052 	}
10053 
10054 #ifdef CONFIG_BNXT_SRIOV
10055 	if (bp->sriov_cfg) {
10056 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
10057 						      !bp->sriov_cfg,
10058 						      BNXT_SRIOV_CFG_WAIT_TMO);
10059 		if (rc)
10060 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
10061 	}
10062 #endif
10063 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
10064 	return rc;
10065 }
10066 
bnxt_close(struct net_device * dev)10067 static int bnxt_close(struct net_device *dev)
10068 {
10069 	struct bnxt *bp = netdev_priv(dev);
10070 
10071 	bnxt_hwmon_close(bp);
10072 	bnxt_close_nic(bp, true, true);
10073 	bnxt_hwrm_shutdown_link(bp);
10074 	bnxt_hwrm_if_change(bp, false);
10075 	return 0;
10076 }
10077 
bnxt_hwrm_port_phy_read(struct bnxt * bp,u16 phy_addr,u16 reg,u16 * val)10078 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
10079 				   u16 *val)
10080 {
10081 	struct hwrm_port_phy_mdio_read_output *resp = bp->hwrm_cmd_resp_addr;
10082 	struct hwrm_port_phy_mdio_read_input req = {0};
10083 	int rc;
10084 
10085 	if (bp->hwrm_spec_code < 0x10a00)
10086 		return -EOPNOTSUPP;
10087 
10088 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_MDIO_READ, -1, -1);
10089 	req.port_id = cpu_to_le16(bp->pf.port_id);
10090 	req.phy_addr = phy_addr;
10091 	req.reg_addr = cpu_to_le16(reg & 0x1f);
10092 	if (mdio_phy_id_is_c45(phy_addr)) {
10093 		req.cl45_mdio = 1;
10094 		req.phy_addr = mdio_phy_id_prtad(phy_addr);
10095 		req.dev_addr = mdio_phy_id_devad(phy_addr);
10096 		req.reg_addr = cpu_to_le16(reg);
10097 	}
10098 
10099 	mutex_lock(&bp->hwrm_cmd_lock);
10100 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10101 	if (!rc)
10102 		*val = le16_to_cpu(resp->reg_data);
10103 	mutex_unlock(&bp->hwrm_cmd_lock);
10104 	return rc;
10105 }
10106 
bnxt_hwrm_port_phy_write(struct bnxt * bp,u16 phy_addr,u16 reg,u16 val)10107 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
10108 				    u16 val)
10109 {
10110 	struct hwrm_port_phy_mdio_write_input req = {0};
10111 
10112 	if (bp->hwrm_spec_code < 0x10a00)
10113 		return -EOPNOTSUPP;
10114 
10115 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_MDIO_WRITE, -1, -1);
10116 	req.port_id = cpu_to_le16(bp->pf.port_id);
10117 	req.phy_addr = phy_addr;
10118 	req.reg_addr = cpu_to_le16(reg & 0x1f);
10119 	if (mdio_phy_id_is_c45(phy_addr)) {
10120 		req.cl45_mdio = 1;
10121 		req.phy_addr = mdio_phy_id_prtad(phy_addr);
10122 		req.dev_addr = mdio_phy_id_devad(phy_addr);
10123 		req.reg_addr = cpu_to_le16(reg);
10124 	}
10125 	req.reg_data = cpu_to_le16(val);
10126 
10127 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10128 }
10129 
10130 /* rtnl_lock held */
bnxt_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)10131 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10132 {
10133 	struct mii_ioctl_data *mdio = if_mii(ifr);
10134 	struct bnxt *bp = netdev_priv(dev);
10135 	int rc;
10136 
10137 	switch (cmd) {
10138 	case SIOCGMIIPHY:
10139 		mdio->phy_id = bp->link_info.phy_addr;
10140 
10141 		fallthrough;
10142 	case SIOCGMIIREG: {
10143 		u16 mii_regval = 0;
10144 
10145 		if (!netif_running(dev))
10146 			return -EAGAIN;
10147 
10148 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
10149 					     &mii_regval);
10150 		mdio->val_out = mii_regval;
10151 		return rc;
10152 	}
10153 
10154 	case SIOCSMIIREG:
10155 		if (!netif_running(dev))
10156 			return -EAGAIN;
10157 
10158 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
10159 						mdio->val_in);
10160 
10161 	default:
10162 		/* do nothing */
10163 		break;
10164 	}
10165 	return -EOPNOTSUPP;
10166 }
10167 
bnxt_get_ring_stats(struct bnxt * bp,struct rtnl_link_stats64 * stats)10168 static void bnxt_get_ring_stats(struct bnxt *bp,
10169 				struct rtnl_link_stats64 *stats)
10170 {
10171 	int i;
10172 
10173 	for (i = 0; i < bp->cp_nr_rings; i++) {
10174 		struct bnxt_napi *bnapi = bp->bnapi[i];
10175 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
10176 		u64 *sw = cpr->stats.sw_stats;
10177 
10178 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
10179 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
10180 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
10181 
10182 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
10183 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
10184 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
10185 
10186 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
10187 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
10188 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
10189 
10190 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
10191 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
10192 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
10193 
10194 		stats->rx_missed_errors +=
10195 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
10196 
10197 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
10198 
10199 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
10200 	}
10201 }
10202 
bnxt_add_prev_stats(struct bnxt * bp,struct rtnl_link_stats64 * stats)10203 static void bnxt_add_prev_stats(struct bnxt *bp,
10204 				struct rtnl_link_stats64 *stats)
10205 {
10206 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
10207 
10208 	stats->rx_packets += prev_stats->rx_packets;
10209 	stats->tx_packets += prev_stats->tx_packets;
10210 	stats->rx_bytes += prev_stats->rx_bytes;
10211 	stats->tx_bytes += prev_stats->tx_bytes;
10212 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
10213 	stats->multicast += prev_stats->multicast;
10214 	stats->tx_dropped += prev_stats->tx_dropped;
10215 }
10216 
10217 static void
bnxt_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)10218 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
10219 {
10220 	struct bnxt *bp = netdev_priv(dev);
10221 
10222 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
10223 	/* Make sure bnxt_close_nic() sees that we are reading stats before
10224 	 * we check the BNXT_STATE_OPEN flag.
10225 	 */
10226 	smp_mb__after_atomic();
10227 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10228 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
10229 		*stats = bp->net_stats_prev;
10230 		return;
10231 	}
10232 
10233 	bnxt_get_ring_stats(bp, stats);
10234 	bnxt_add_prev_stats(bp, stats);
10235 
10236 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10237 		u64 *rx = bp->port_stats.sw_stats;
10238 		u64 *tx = bp->port_stats.sw_stats +
10239 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10240 
10241 		stats->rx_crc_errors =
10242 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
10243 		stats->rx_frame_errors =
10244 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
10245 		stats->rx_length_errors =
10246 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
10247 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
10248 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
10249 		stats->rx_errors =
10250 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
10251 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
10252 		stats->collisions =
10253 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
10254 		stats->tx_fifo_errors =
10255 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
10256 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
10257 	}
10258 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
10259 }
10260 
bnxt_mc_list_updated(struct bnxt * bp,u32 * rx_mask)10261 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
10262 {
10263 	struct net_device *dev = bp->dev;
10264 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10265 	struct netdev_hw_addr *ha;
10266 	u8 *haddr;
10267 	int mc_count = 0;
10268 	bool update = false;
10269 	int off = 0;
10270 
10271 	netdev_for_each_mc_addr(ha, dev) {
10272 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
10273 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10274 			vnic->mc_list_count = 0;
10275 			return false;
10276 		}
10277 		haddr = ha->addr;
10278 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
10279 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
10280 			update = true;
10281 		}
10282 		off += ETH_ALEN;
10283 		mc_count++;
10284 	}
10285 	if (mc_count)
10286 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
10287 
10288 	if (mc_count != vnic->mc_list_count) {
10289 		vnic->mc_list_count = mc_count;
10290 		update = true;
10291 	}
10292 	return update;
10293 }
10294 
bnxt_uc_list_updated(struct bnxt * bp)10295 static bool bnxt_uc_list_updated(struct bnxt *bp)
10296 {
10297 	struct net_device *dev = bp->dev;
10298 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10299 	struct netdev_hw_addr *ha;
10300 	int off = 0;
10301 
10302 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
10303 		return true;
10304 
10305 	netdev_for_each_uc_addr(ha, dev) {
10306 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
10307 			return true;
10308 
10309 		off += ETH_ALEN;
10310 	}
10311 	return false;
10312 }
10313 
bnxt_set_rx_mode(struct net_device * dev)10314 static void bnxt_set_rx_mode(struct net_device *dev)
10315 {
10316 	struct bnxt *bp = netdev_priv(dev);
10317 	struct bnxt_vnic_info *vnic;
10318 	bool mc_update = false;
10319 	bool uc_update;
10320 	u32 mask;
10321 
10322 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
10323 		return;
10324 
10325 	vnic = &bp->vnic_info[0];
10326 	mask = vnic->rx_mask;
10327 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
10328 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
10329 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
10330 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
10331 
10332 	if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
10333 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10334 
10335 	uc_update = bnxt_uc_list_updated(bp);
10336 
10337 	if (dev->flags & IFF_BROADCAST)
10338 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10339 	if (dev->flags & IFF_ALLMULTI) {
10340 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10341 		vnic->mc_list_count = 0;
10342 	} else {
10343 		mc_update = bnxt_mc_list_updated(bp, &mask);
10344 	}
10345 
10346 	if (mask != vnic->rx_mask || uc_update || mc_update) {
10347 		vnic->rx_mask = mask;
10348 
10349 		set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
10350 		bnxt_queue_sp_work(bp);
10351 	}
10352 }
10353 
bnxt_cfg_rx_mode(struct bnxt * bp)10354 static int bnxt_cfg_rx_mode(struct bnxt *bp)
10355 {
10356 	struct net_device *dev = bp->dev;
10357 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10358 	struct netdev_hw_addr *ha;
10359 	int i, off = 0, rc;
10360 	bool uc_update;
10361 
10362 	netif_addr_lock_bh(dev);
10363 	uc_update = bnxt_uc_list_updated(bp);
10364 	netif_addr_unlock_bh(dev);
10365 
10366 	if (!uc_update)
10367 		goto skip_uc;
10368 
10369 	mutex_lock(&bp->hwrm_cmd_lock);
10370 	for (i = 1; i < vnic->uc_filter_count; i++) {
10371 		struct hwrm_cfa_l2_filter_free_input req = {0};
10372 
10373 		bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
10374 				       -1);
10375 
10376 		req.l2_filter_id = vnic->fw_l2_filter_id[i];
10377 
10378 		rc = _hwrm_send_message(bp, &req, sizeof(req),
10379 					HWRM_CMD_TIMEOUT);
10380 	}
10381 	mutex_unlock(&bp->hwrm_cmd_lock);
10382 
10383 	vnic->uc_filter_count = 1;
10384 
10385 	netif_addr_lock_bh(dev);
10386 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
10387 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10388 	} else {
10389 		netdev_for_each_uc_addr(ha, dev) {
10390 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
10391 			off += ETH_ALEN;
10392 			vnic->uc_filter_count++;
10393 		}
10394 	}
10395 	netif_addr_unlock_bh(dev);
10396 
10397 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
10398 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
10399 		if (rc) {
10400 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
10401 				   rc);
10402 			vnic->uc_filter_count = i;
10403 			return rc;
10404 		}
10405 	}
10406 
10407 skip_uc:
10408 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
10409 	if (rc && vnic->mc_list_count) {
10410 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
10411 			    rc);
10412 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10413 		vnic->mc_list_count = 0;
10414 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
10415 	}
10416 	if (rc)
10417 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
10418 			   rc);
10419 
10420 	return rc;
10421 }
10422 
bnxt_can_reserve_rings(struct bnxt * bp)10423 static bool bnxt_can_reserve_rings(struct bnxt *bp)
10424 {
10425 #ifdef CONFIG_BNXT_SRIOV
10426 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
10427 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10428 
10429 		/* No minimum rings were provisioned by the PF.  Don't
10430 		 * reserve rings by default when device is down.
10431 		 */
10432 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
10433 			return true;
10434 
10435 		if (!netif_running(bp->dev))
10436 			return false;
10437 	}
10438 #endif
10439 	return true;
10440 }
10441 
10442 /* If the chip and firmware supports RFS */
bnxt_rfs_supported(struct bnxt * bp)10443 static bool bnxt_rfs_supported(struct bnxt *bp)
10444 {
10445 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
10446 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
10447 			return true;
10448 		return false;
10449 	}
10450 	/* 212 firmware is broken for aRFS */
10451 	if (BNXT_FW_MAJ(bp) == 212)
10452 		return false;
10453 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
10454 		return true;
10455 	if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
10456 		return true;
10457 	return false;
10458 }
10459 
10460 /* If runtime conditions support RFS */
bnxt_rfs_capable(struct bnxt * bp)10461 static bool bnxt_rfs_capable(struct bnxt *bp)
10462 {
10463 #ifdef CONFIG_RFS_ACCEL
10464 	int vnics, max_vnics, max_rss_ctxs;
10465 
10466 	if (bp->flags & BNXT_FLAG_CHIP_P5)
10467 		return bnxt_rfs_supported(bp);
10468 	if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
10469 		return false;
10470 
10471 	vnics = 1 + bp->rx_nr_rings;
10472 	max_vnics = bnxt_get_max_func_vnics(bp);
10473 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
10474 
10475 	/* RSS contexts not a limiting factor */
10476 	if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
10477 		max_rss_ctxs = max_vnics;
10478 	if (vnics > max_vnics || vnics > max_rss_ctxs) {
10479 		if (bp->rx_nr_rings > 1)
10480 			netdev_warn(bp->dev,
10481 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
10482 				    min(max_rss_ctxs - 1, max_vnics - 1));
10483 		return false;
10484 	}
10485 
10486 	if (!BNXT_NEW_RM(bp))
10487 		return true;
10488 
10489 	if (vnics == bp->hw_resc.resv_vnics)
10490 		return true;
10491 
10492 	bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, vnics);
10493 	if (vnics <= bp->hw_resc.resv_vnics)
10494 		return true;
10495 
10496 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
10497 	bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, 1);
10498 	return false;
10499 #else
10500 	return false;
10501 #endif
10502 }
10503 
bnxt_fix_features(struct net_device * dev,netdev_features_t features)10504 static netdev_features_t bnxt_fix_features(struct net_device *dev,
10505 					   netdev_features_t features)
10506 {
10507 	struct bnxt *bp = netdev_priv(dev);
10508 	netdev_features_t vlan_features;
10509 
10510 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
10511 		features &= ~NETIF_F_NTUPLE;
10512 
10513 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
10514 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
10515 
10516 	if (!(features & NETIF_F_GRO))
10517 		features &= ~NETIF_F_GRO_HW;
10518 
10519 	if (features & NETIF_F_GRO_HW)
10520 		features &= ~NETIF_F_LRO;
10521 
10522 	/* Both CTAG and STAG VLAN accelaration on the RX side have to be
10523 	 * turned on or off together.
10524 	 */
10525 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
10526 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
10527 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
10528 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
10529 		else if (vlan_features)
10530 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
10531 	}
10532 #ifdef CONFIG_BNXT_SRIOV
10533 	if (BNXT_VF(bp) && bp->vf.vlan)
10534 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
10535 #endif
10536 	return features;
10537 }
10538 
bnxt_set_features(struct net_device * dev,netdev_features_t features)10539 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
10540 {
10541 	struct bnxt *bp = netdev_priv(dev);
10542 	u32 flags = bp->flags;
10543 	u32 changes;
10544 	int rc = 0;
10545 	bool re_init = false;
10546 	bool update_tpa = false;
10547 
10548 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
10549 	if (features & NETIF_F_GRO_HW)
10550 		flags |= BNXT_FLAG_GRO;
10551 	else if (features & NETIF_F_LRO)
10552 		flags |= BNXT_FLAG_LRO;
10553 
10554 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
10555 		flags &= ~BNXT_FLAG_TPA;
10556 
10557 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
10558 		flags |= BNXT_FLAG_STRIP_VLAN;
10559 
10560 	if (features & NETIF_F_NTUPLE)
10561 		flags |= BNXT_FLAG_RFS;
10562 
10563 	changes = flags ^ bp->flags;
10564 	if (changes & BNXT_FLAG_TPA) {
10565 		update_tpa = true;
10566 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
10567 		    (flags & BNXT_FLAG_TPA) == 0 ||
10568 		    (bp->flags & BNXT_FLAG_CHIP_P5))
10569 			re_init = true;
10570 	}
10571 
10572 	if (changes & ~BNXT_FLAG_TPA)
10573 		re_init = true;
10574 
10575 	if (flags != bp->flags) {
10576 		u32 old_flags = bp->flags;
10577 
10578 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10579 			bp->flags = flags;
10580 			if (update_tpa)
10581 				bnxt_set_ring_params(bp);
10582 			return rc;
10583 		}
10584 
10585 		if (re_init) {
10586 			bnxt_close_nic(bp, false, false);
10587 			bp->flags = flags;
10588 			if (update_tpa)
10589 				bnxt_set_ring_params(bp);
10590 
10591 			return bnxt_open_nic(bp, false, false);
10592 		}
10593 		if (update_tpa) {
10594 			bp->flags = flags;
10595 			rc = bnxt_set_tpa(bp,
10596 					  (flags & BNXT_FLAG_TPA) ?
10597 					  true : false);
10598 			if (rc)
10599 				bp->flags = old_flags;
10600 		}
10601 	}
10602 	return rc;
10603 }
10604 
bnxt_dbg_hwrm_rd_reg(struct bnxt * bp,u32 reg_off,u16 num_words,u32 * reg_buf)10605 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
10606 			 u32 *reg_buf)
10607 {
10608 	struct hwrm_dbg_read_direct_output *resp = bp->hwrm_cmd_resp_addr;
10609 	struct hwrm_dbg_read_direct_input req = {0};
10610 	__le32 *dbg_reg_buf;
10611 	dma_addr_t mapping;
10612 	int rc, i;
10613 
10614 	dbg_reg_buf = dma_alloc_coherent(&bp->pdev->dev, num_words * 4,
10615 					 &mapping, GFP_KERNEL);
10616 	if (!dbg_reg_buf)
10617 		return -ENOMEM;
10618 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_READ_DIRECT, -1, -1);
10619 	req.host_dest_addr = cpu_to_le64(mapping);
10620 	req.read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
10621 	req.read_len32 = cpu_to_le32(num_words);
10622 	mutex_lock(&bp->hwrm_cmd_lock);
10623 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10624 	if (rc || resp->error_code) {
10625 		rc = -EIO;
10626 		goto dbg_rd_reg_exit;
10627 	}
10628 	for (i = 0; i < num_words; i++)
10629 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
10630 
10631 dbg_rd_reg_exit:
10632 	mutex_unlock(&bp->hwrm_cmd_lock);
10633 	dma_free_coherent(&bp->pdev->dev, num_words * 4, dbg_reg_buf, mapping);
10634 	return rc;
10635 }
10636 
bnxt_dbg_hwrm_ring_info_get(struct bnxt * bp,u8 ring_type,u32 ring_id,u32 * prod,u32 * cons)10637 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
10638 				       u32 ring_id, u32 *prod, u32 *cons)
10639 {
10640 	struct hwrm_dbg_ring_info_get_output *resp = bp->hwrm_cmd_resp_addr;
10641 	struct hwrm_dbg_ring_info_get_input req = {0};
10642 	int rc;
10643 
10644 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_RING_INFO_GET, -1, -1);
10645 	req.ring_type = ring_type;
10646 	req.fw_ring_id = cpu_to_le32(ring_id);
10647 	mutex_lock(&bp->hwrm_cmd_lock);
10648 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10649 	if (!rc) {
10650 		*prod = le32_to_cpu(resp->producer_index);
10651 		*cons = le32_to_cpu(resp->consumer_index);
10652 	}
10653 	mutex_unlock(&bp->hwrm_cmd_lock);
10654 	return rc;
10655 }
10656 
bnxt_dump_tx_sw_state(struct bnxt_napi * bnapi)10657 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
10658 {
10659 	struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
10660 	int i = bnapi->index;
10661 
10662 	if (!txr)
10663 		return;
10664 
10665 	netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
10666 		    i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
10667 		    txr->tx_cons);
10668 }
10669 
bnxt_dump_rx_sw_state(struct bnxt_napi * bnapi)10670 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
10671 {
10672 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
10673 	int i = bnapi->index;
10674 
10675 	if (!rxr)
10676 		return;
10677 
10678 	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",
10679 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
10680 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
10681 		    rxr->rx_sw_agg_prod);
10682 }
10683 
bnxt_dump_cp_sw_state(struct bnxt_napi * bnapi)10684 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
10685 {
10686 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
10687 	int i = bnapi->index;
10688 
10689 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
10690 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
10691 }
10692 
bnxt_dbg_dump_states(struct bnxt * bp)10693 static void bnxt_dbg_dump_states(struct bnxt *bp)
10694 {
10695 	int i;
10696 	struct bnxt_napi *bnapi;
10697 
10698 	for (i = 0; i < bp->cp_nr_rings; i++) {
10699 		bnapi = bp->bnapi[i];
10700 		if (netif_msg_drv(bp)) {
10701 			bnxt_dump_tx_sw_state(bnapi);
10702 			bnxt_dump_rx_sw_state(bnapi);
10703 			bnxt_dump_cp_sw_state(bnapi);
10704 		}
10705 	}
10706 }
10707 
bnxt_hwrm_rx_ring_reset(struct bnxt * bp,int ring_nr)10708 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
10709 {
10710 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
10711 	struct hwrm_ring_reset_input req = {0};
10712 	struct bnxt_napi *bnapi = rxr->bnapi;
10713 	struct bnxt_cp_ring_info *cpr;
10714 	u16 cp_ring_id;
10715 
10716 	cpr = &bnapi->cp_ring;
10717 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
10718 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_RESET, cp_ring_id, -1);
10719 	req.ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
10720 	req.ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
10721 	return hwrm_send_message_silent(bp, &req, sizeof(req),
10722 					HWRM_CMD_TIMEOUT);
10723 }
10724 
bnxt_reset_task(struct bnxt * bp,bool silent)10725 static void bnxt_reset_task(struct bnxt *bp, bool silent)
10726 {
10727 	if (!silent)
10728 		bnxt_dbg_dump_states(bp);
10729 	if (netif_running(bp->dev)) {
10730 		int rc;
10731 
10732 		if (silent) {
10733 			bnxt_close_nic(bp, false, false);
10734 			bnxt_open_nic(bp, false, false);
10735 		} else {
10736 			bnxt_ulp_stop(bp);
10737 			bnxt_close_nic(bp, true, false);
10738 			rc = bnxt_open_nic(bp, true, false);
10739 			bnxt_ulp_start(bp, rc);
10740 		}
10741 	}
10742 }
10743 
bnxt_tx_timeout(struct net_device * dev,unsigned int txqueue)10744 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
10745 {
10746 	struct bnxt *bp = netdev_priv(dev);
10747 
10748 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
10749 	set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
10750 	bnxt_queue_sp_work(bp);
10751 }
10752 
bnxt_fw_health_check(struct bnxt * bp)10753 static void bnxt_fw_health_check(struct bnxt *bp)
10754 {
10755 	struct bnxt_fw_health *fw_health = bp->fw_health;
10756 	u32 val;
10757 
10758 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
10759 		return;
10760 
10761 	/* Make sure it is enabled before checking the tmr_counter. */
10762 	smp_rmb();
10763 	if (fw_health->tmr_counter) {
10764 		fw_health->tmr_counter--;
10765 		return;
10766 	}
10767 
10768 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
10769 	if (val == fw_health->last_fw_heartbeat)
10770 		goto fw_reset;
10771 
10772 	fw_health->last_fw_heartbeat = val;
10773 
10774 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
10775 	if (val != fw_health->last_fw_reset_cnt)
10776 		goto fw_reset;
10777 
10778 	fw_health->tmr_counter = fw_health->tmr_multiplier;
10779 	return;
10780 
10781 fw_reset:
10782 	set_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event);
10783 	bnxt_queue_sp_work(bp);
10784 }
10785 
bnxt_timer(struct timer_list * t)10786 static void bnxt_timer(struct timer_list *t)
10787 {
10788 	struct bnxt *bp = from_timer(bp, t, timer);
10789 	struct net_device *dev = bp->dev;
10790 
10791 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
10792 		return;
10793 
10794 	if (atomic_read(&bp->intr_sem) != 0)
10795 		goto bnxt_restart_timer;
10796 
10797 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
10798 		bnxt_fw_health_check(bp);
10799 
10800 	if (bp->link_info.link_up && bp->stats_coal_ticks) {
10801 		set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
10802 		bnxt_queue_sp_work(bp);
10803 	}
10804 
10805 	if (bnxt_tc_flower_enabled(bp)) {
10806 		set_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event);
10807 		bnxt_queue_sp_work(bp);
10808 	}
10809 
10810 #ifdef CONFIG_RFS_ACCEL
10811 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count) {
10812 		set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
10813 		bnxt_queue_sp_work(bp);
10814 	}
10815 #endif /*CONFIG_RFS_ACCEL*/
10816 
10817 	if (bp->link_info.phy_retry) {
10818 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
10819 			bp->link_info.phy_retry = false;
10820 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
10821 		} else {
10822 			set_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event);
10823 			bnxt_queue_sp_work(bp);
10824 		}
10825 	}
10826 
10827 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && !bp->chip_rev &&
10828 	    netif_carrier_ok(dev)) {
10829 		set_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event);
10830 		bnxt_queue_sp_work(bp);
10831 	}
10832 bnxt_restart_timer:
10833 	mod_timer(&bp->timer, jiffies + bp->current_interval);
10834 }
10835 
bnxt_rtnl_lock_sp(struct bnxt * bp)10836 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
10837 {
10838 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
10839 	 * set.  If the device is being closed, bnxt_close() may be holding
10840 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
10841 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
10842 	 */
10843 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
10844 	rtnl_lock();
10845 }
10846 
bnxt_rtnl_unlock_sp(struct bnxt * bp)10847 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
10848 {
10849 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
10850 	rtnl_unlock();
10851 }
10852 
10853 /* Only called from bnxt_sp_task() */
bnxt_reset(struct bnxt * bp,bool silent)10854 static void bnxt_reset(struct bnxt *bp, bool silent)
10855 {
10856 	bnxt_rtnl_lock_sp(bp);
10857 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
10858 		bnxt_reset_task(bp, silent);
10859 	bnxt_rtnl_unlock_sp(bp);
10860 }
10861 
10862 /* Only called from bnxt_sp_task() */
bnxt_rx_ring_reset(struct bnxt * bp)10863 static void bnxt_rx_ring_reset(struct bnxt *bp)
10864 {
10865 	int i;
10866 
10867 	bnxt_rtnl_lock_sp(bp);
10868 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10869 		bnxt_rtnl_unlock_sp(bp);
10870 		return;
10871 	}
10872 	/* Disable and flush TPA before resetting the RX ring */
10873 	if (bp->flags & BNXT_FLAG_TPA)
10874 		bnxt_set_tpa(bp, false);
10875 	for (i = 0; i < bp->rx_nr_rings; i++) {
10876 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
10877 		struct bnxt_cp_ring_info *cpr;
10878 		int rc;
10879 
10880 		if (!rxr->bnapi->in_reset)
10881 			continue;
10882 
10883 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
10884 		if (rc) {
10885 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
10886 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
10887 			else
10888 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
10889 					    rc);
10890 			bnxt_reset_task(bp, true);
10891 			break;
10892 		}
10893 		bnxt_free_one_rx_ring_skbs(bp, i);
10894 		rxr->rx_prod = 0;
10895 		rxr->rx_agg_prod = 0;
10896 		rxr->rx_sw_agg_prod = 0;
10897 		rxr->rx_next_cons = 0;
10898 		rxr->bnapi->in_reset = false;
10899 		bnxt_alloc_one_rx_ring(bp, i);
10900 		cpr = &rxr->bnapi->cp_ring;
10901 		cpr->sw_stats.rx.rx_resets++;
10902 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
10903 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
10904 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
10905 	}
10906 	if (bp->flags & BNXT_FLAG_TPA)
10907 		bnxt_set_tpa(bp, true);
10908 	bnxt_rtnl_unlock_sp(bp);
10909 }
10910 
bnxt_fw_reset_close(struct bnxt * bp)10911 static void bnxt_fw_reset_close(struct bnxt *bp)
10912 {
10913 	bnxt_ulp_stop(bp);
10914 	/* When firmware is fatal state, disable PCI device to prevent
10915 	 * any potential bad DMAs before freeing kernel memory.
10916 	 */
10917 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
10918 		pci_disable_device(bp->pdev);
10919 	__bnxt_close_nic(bp, true, false);
10920 	bnxt_clear_int_mode(bp);
10921 	bnxt_hwrm_func_drv_unrgtr(bp);
10922 	if (pci_is_enabled(bp->pdev))
10923 		pci_disable_device(bp->pdev);
10924 	bnxt_free_ctx_mem(bp);
10925 	kfree(bp->ctx);
10926 	bp->ctx = NULL;
10927 }
10928 
is_bnxt_fw_ok(struct bnxt * bp)10929 static bool is_bnxt_fw_ok(struct bnxt *bp)
10930 {
10931 	struct bnxt_fw_health *fw_health = bp->fw_health;
10932 	bool no_heartbeat = false, has_reset = false;
10933 	u32 val;
10934 
10935 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
10936 	if (val == fw_health->last_fw_heartbeat)
10937 		no_heartbeat = true;
10938 
10939 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
10940 	if (val != fw_health->last_fw_reset_cnt)
10941 		has_reset = true;
10942 
10943 	if (!no_heartbeat && has_reset)
10944 		return true;
10945 
10946 	return false;
10947 }
10948 
10949 /* rtnl_lock is acquired before calling this function */
bnxt_force_fw_reset(struct bnxt * bp)10950 static void bnxt_force_fw_reset(struct bnxt *bp)
10951 {
10952 	struct bnxt_fw_health *fw_health = bp->fw_health;
10953 	u32 wait_dsecs;
10954 
10955 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
10956 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
10957 		return;
10958 
10959 	set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
10960 	bnxt_fw_reset_close(bp);
10961 	wait_dsecs = fw_health->master_func_wait_dsecs;
10962 	if (fw_health->master) {
10963 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
10964 			wait_dsecs = 0;
10965 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
10966 	} else {
10967 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
10968 		wait_dsecs = fw_health->normal_func_wait_dsecs;
10969 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
10970 	}
10971 
10972 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
10973 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
10974 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
10975 }
10976 
bnxt_fw_exception(struct bnxt * bp)10977 void bnxt_fw_exception(struct bnxt *bp)
10978 {
10979 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
10980 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
10981 	bnxt_rtnl_lock_sp(bp);
10982 	bnxt_force_fw_reset(bp);
10983 	bnxt_rtnl_unlock_sp(bp);
10984 }
10985 
10986 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
10987  * < 0 on error.
10988  */
bnxt_get_registered_vfs(struct bnxt * bp)10989 static int bnxt_get_registered_vfs(struct bnxt *bp)
10990 {
10991 #ifdef CONFIG_BNXT_SRIOV
10992 	int rc;
10993 
10994 	if (!BNXT_PF(bp))
10995 		return 0;
10996 
10997 	rc = bnxt_hwrm_func_qcfg(bp);
10998 	if (rc) {
10999 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
11000 		return rc;
11001 	}
11002 	if (bp->pf.registered_vfs)
11003 		return bp->pf.registered_vfs;
11004 	if (bp->sriov_cfg)
11005 		return 1;
11006 #endif
11007 	return 0;
11008 }
11009 
bnxt_fw_reset(struct bnxt * bp)11010 void bnxt_fw_reset(struct bnxt *bp)
11011 {
11012 	bnxt_rtnl_lock_sp(bp);
11013 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
11014 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
11015 		int n = 0, tmo;
11016 
11017 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11018 		if (bp->pf.active_vfs &&
11019 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
11020 			n = bnxt_get_registered_vfs(bp);
11021 		if (n < 0) {
11022 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
11023 				   n);
11024 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11025 			dev_close(bp->dev);
11026 			goto fw_reset_exit;
11027 		} else if (n > 0) {
11028 			u16 vf_tmo_dsecs = n * 10;
11029 
11030 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
11031 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
11032 			bp->fw_reset_state =
11033 				BNXT_FW_RESET_STATE_POLL_VF;
11034 			bnxt_queue_fw_reset_work(bp, HZ / 10);
11035 			goto fw_reset_exit;
11036 		}
11037 		bnxt_fw_reset_close(bp);
11038 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
11039 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
11040 			tmo = HZ / 10;
11041 		} else {
11042 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11043 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
11044 		}
11045 		bnxt_queue_fw_reset_work(bp, tmo);
11046 	}
11047 fw_reset_exit:
11048 	bnxt_rtnl_unlock_sp(bp);
11049 }
11050 
bnxt_chk_missed_irq(struct bnxt * bp)11051 static void bnxt_chk_missed_irq(struct bnxt *bp)
11052 {
11053 	int i;
11054 
11055 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
11056 		return;
11057 
11058 	for (i = 0; i < bp->cp_nr_rings; i++) {
11059 		struct bnxt_napi *bnapi = bp->bnapi[i];
11060 		struct bnxt_cp_ring_info *cpr;
11061 		u32 fw_ring_id;
11062 		int j;
11063 
11064 		if (!bnapi)
11065 			continue;
11066 
11067 		cpr = &bnapi->cp_ring;
11068 		for (j = 0; j < 2; j++) {
11069 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
11070 			u32 val[2];
11071 
11072 			if (!cpr2 || cpr2->has_more_work ||
11073 			    !bnxt_has_work(bp, cpr2))
11074 				continue;
11075 
11076 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
11077 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
11078 				continue;
11079 			}
11080 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
11081 			bnxt_dbg_hwrm_ring_info_get(bp,
11082 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
11083 				fw_ring_id, &val[0], &val[1]);
11084 			cpr->sw_stats.cmn.missed_irqs++;
11085 		}
11086 	}
11087 }
11088 
11089 static void bnxt_cfg_ntp_filters(struct bnxt *);
11090 
bnxt_init_ethtool_link_settings(struct bnxt * bp)11091 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
11092 {
11093 	struct bnxt_link_info *link_info = &bp->link_info;
11094 
11095 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
11096 		link_info->autoneg = BNXT_AUTONEG_SPEED;
11097 		if (bp->hwrm_spec_code >= 0x10201) {
11098 			if (link_info->auto_pause_setting &
11099 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
11100 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
11101 		} else {
11102 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
11103 		}
11104 		link_info->advertising = link_info->auto_link_speeds;
11105 		link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
11106 	} else {
11107 		link_info->req_link_speed = link_info->force_link_speed;
11108 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
11109 		if (link_info->force_pam4_link_speed) {
11110 			link_info->req_link_speed =
11111 				link_info->force_pam4_link_speed;
11112 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
11113 		}
11114 		link_info->req_duplex = link_info->duplex_setting;
11115 	}
11116 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
11117 		link_info->req_flow_ctrl =
11118 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
11119 	else
11120 		link_info->req_flow_ctrl = link_info->force_pause_setting;
11121 }
11122 
bnxt_sp_task(struct work_struct * work)11123 static void bnxt_sp_task(struct work_struct *work)
11124 {
11125 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
11126 
11127 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11128 	smp_mb__after_atomic();
11129 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
11130 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11131 		return;
11132 	}
11133 
11134 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
11135 		bnxt_cfg_rx_mode(bp);
11136 
11137 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
11138 		bnxt_cfg_ntp_filters(bp);
11139 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
11140 		bnxt_hwrm_exec_fwd_req(bp);
11141 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
11142 		bnxt_hwrm_port_qstats(bp, 0);
11143 		bnxt_hwrm_port_qstats_ext(bp, 0);
11144 		bnxt_accumulate_all_stats(bp);
11145 	}
11146 
11147 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
11148 		int rc;
11149 
11150 		mutex_lock(&bp->link_lock);
11151 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
11152 				       &bp->sp_event))
11153 			bnxt_hwrm_phy_qcaps(bp);
11154 
11155 		rc = bnxt_update_link(bp, true);
11156 		if (rc)
11157 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
11158 				   rc);
11159 
11160 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
11161 				       &bp->sp_event))
11162 			bnxt_init_ethtool_link_settings(bp);
11163 		mutex_unlock(&bp->link_lock);
11164 	}
11165 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
11166 		int rc;
11167 
11168 		mutex_lock(&bp->link_lock);
11169 		rc = bnxt_update_phy_setting(bp);
11170 		mutex_unlock(&bp->link_lock);
11171 		if (rc) {
11172 			netdev_warn(bp->dev, "update phy settings retry failed\n");
11173 		} else {
11174 			bp->link_info.phy_retry = false;
11175 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
11176 		}
11177 	}
11178 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
11179 		mutex_lock(&bp->link_lock);
11180 		bnxt_get_port_module_status(bp);
11181 		mutex_unlock(&bp->link_lock);
11182 	}
11183 
11184 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
11185 		bnxt_tc_flow_stats_work(bp);
11186 
11187 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
11188 		bnxt_chk_missed_irq(bp);
11189 
11190 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
11191 	 * must be the last functions to be called before exiting.
11192 	 */
11193 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
11194 		bnxt_reset(bp, false);
11195 
11196 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
11197 		bnxt_reset(bp, true);
11198 
11199 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
11200 		bnxt_rx_ring_reset(bp);
11201 
11202 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event))
11203 		bnxt_devlink_health_report(bp, BNXT_FW_RESET_NOTIFY_SP_EVENT);
11204 
11205 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
11206 		if (!is_bnxt_fw_ok(bp))
11207 			bnxt_devlink_health_report(bp,
11208 						   BNXT_FW_EXCEPTION_SP_EVENT);
11209 	}
11210 
11211 	smp_mb__before_atomic();
11212 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11213 }
11214 
11215 /* Under rtnl_lock */
bnxt_check_rings(struct bnxt * bp,int tx,int rx,bool sh,int tcs,int tx_xdp)11216 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
11217 		     int tx_xdp)
11218 {
11219 	int max_rx, max_tx, tx_sets = 1;
11220 	int tx_rings_needed, stats;
11221 	int rx_rings = rx;
11222 	int cp, vnics, rc;
11223 
11224 	if (tcs)
11225 		tx_sets = tcs;
11226 
11227 	rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
11228 	if (rc)
11229 		return rc;
11230 
11231 	if (max_rx < rx)
11232 		return -ENOMEM;
11233 
11234 	tx_rings_needed = tx * tx_sets + tx_xdp;
11235 	if (max_tx < tx_rings_needed)
11236 		return -ENOMEM;
11237 
11238 	vnics = 1;
11239 	if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
11240 		vnics += rx_rings;
11241 
11242 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
11243 		rx_rings <<= 1;
11244 	cp = sh ? max_t(int, tx_rings_needed, rx) : tx_rings_needed + rx;
11245 	stats = cp;
11246 	if (BNXT_NEW_RM(bp)) {
11247 		cp += bnxt_get_ulp_msix_num(bp);
11248 		stats += bnxt_get_ulp_stat_ctxs(bp);
11249 	}
11250 	return bnxt_hwrm_check_rings(bp, tx_rings_needed, rx_rings, rx, cp,
11251 				     stats, vnics);
11252 }
11253 
bnxt_unmap_bars(struct bnxt * bp,struct pci_dev * pdev)11254 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
11255 {
11256 	if (bp->bar2) {
11257 		pci_iounmap(pdev, bp->bar2);
11258 		bp->bar2 = NULL;
11259 	}
11260 
11261 	if (bp->bar1) {
11262 		pci_iounmap(pdev, bp->bar1);
11263 		bp->bar1 = NULL;
11264 	}
11265 
11266 	if (bp->bar0) {
11267 		pci_iounmap(pdev, bp->bar0);
11268 		bp->bar0 = NULL;
11269 	}
11270 }
11271 
bnxt_cleanup_pci(struct bnxt * bp)11272 static void bnxt_cleanup_pci(struct bnxt *bp)
11273 {
11274 	bnxt_unmap_bars(bp, bp->pdev);
11275 	pci_release_regions(bp->pdev);
11276 	if (pci_is_enabled(bp->pdev))
11277 		pci_disable_device(bp->pdev);
11278 }
11279 
bnxt_init_dflt_coal(struct bnxt * bp)11280 static void bnxt_init_dflt_coal(struct bnxt *bp)
11281 {
11282 	struct bnxt_coal *coal;
11283 
11284 	/* Tick values in micro seconds.
11285 	 * 1 coal_buf x bufs_per_record = 1 completion record.
11286 	 */
11287 	coal = &bp->rx_coal;
11288 	coal->coal_ticks = 10;
11289 	coal->coal_bufs = 30;
11290 	coal->coal_ticks_irq = 1;
11291 	coal->coal_bufs_irq = 2;
11292 	coal->idle_thresh = 50;
11293 	coal->bufs_per_record = 2;
11294 	coal->budget = 64;		/* NAPI budget */
11295 
11296 	coal = &bp->tx_coal;
11297 	coal->coal_ticks = 28;
11298 	coal->coal_bufs = 30;
11299 	coal->coal_ticks_irq = 2;
11300 	coal->coal_bufs_irq = 2;
11301 	coal->bufs_per_record = 1;
11302 
11303 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
11304 }
11305 
bnxt_fw_reset_via_optee(struct bnxt * bp)11306 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
11307 {
11308 #ifdef CONFIG_TEE_BNXT_FW
11309 	int rc = tee_bnxt_fw_load();
11310 
11311 	if (rc)
11312 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
11313 
11314 	return rc;
11315 #else
11316 	netdev_err(bp->dev, "OP-TEE not supported\n");
11317 	return -ENODEV;
11318 #endif
11319 }
11320 
bnxt_fw_init_one_p1(struct bnxt * bp)11321 static int bnxt_fw_init_one_p1(struct bnxt *bp)
11322 {
11323 	int rc;
11324 
11325 	bp->fw_cap = 0;
11326 	rc = bnxt_hwrm_ver_get(bp);
11327 	bnxt_try_map_fw_health_reg(bp);
11328 	if (rc) {
11329 		if (bp->fw_health && bp->fw_health->status_reliable) {
11330 			u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11331 
11332 			netdev_err(bp->dev,
11333 				   "Firmware not responding, status: 0x%x\n",
11334 				   sts);
11335 			if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
11336 				netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
11337 				rc = bnxt_fw_reset_via_optee(bp);
11338 				if (!rc)
11339 					rc = bnxt_hwrm_ver_get(bp);
11340 			}
11341 		}
11342 		if (rc)
11343 			return rc;
11344 	}
11345 
11346 	if (bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL) {
11347 		rc = bnxt_alloc_kong_hwrm_resources(bp);
11348 		if (rc)
11349 			bp->fw_cap &= ~BNXT_FW_CAP_KONG_MB_CHNL;
11350 	}
11351 
11352 	if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
11353 	    bp->hwrm_max_ext_req_len > BNXT_HWRM_MAX_REQ_LEN) {
11354 		rc = bnxt_alloc_hwrm_short_cmd_req(bp);
11355 		if (rc)
11356 			return rc;
11357 	}
11358 	bnxt_nvm_cfg_ver_get(bp);
11359 
11360 	rc = bnxt_hwrm_func_reset(bp);
11361 	if (rc)
11362 		return -ENODEV;
11363 
11364 	bnxt_hwrm_fw_set_time(bp);
11365 	return 0;
11366 }
11367 
bnxt_fw_init_one_p2(struct bnxt * bp)11368 static int bnxt_fw_init_one_p2(struct bnxt *bp)
11369 {
11370 	int rc;
11371 
11372 	/* Get the MAX capabilities for this function */
11373 	rc = bnxt_hwrm_func_qcaps(bp);
11374 	if (rc) {
11375 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
11376 			   rc);
11377 		return -ENODEV;
11378 	}
11379 
11380 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
11381 	if (rc)
11382 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
11383 			    rc);
11384 
11385 	if (bnxt_alloc_fw_health(bp)) {
11386 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
11387 	} else {
11388 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
11389 		if (rc)
11390 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
11391 				    rc);
11392 	}
11393 
11394 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
11395 	if (rc)
11396 		return -ENODEV;
11397 
11398 	bnxt_hwrm_func_qcfg(bp);
11399 	bnxt_hwrm_vnic_qcaps(bp);
11400 	bnxt_hwrm_port_led_qcaps(bp);
11401 	bnxt_ethtool_init(bp);
11402 	bnxt_dcb_init(bp);
11403 	return 0;
11404 }
11405 
bnxt_set_dflt_rss_hash_type(struct bnxt * bp)11406 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
11407 {
11408 	bp->flags &= ~BNXT_FLAG_UDP_RSS_CAP;
11409 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
11410 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
11411 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
11412 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
11413 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
11414 		bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
11415 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
11416 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
11417 	}
11418 }
11419 
bnxt_set_dflt_rfs(struct bnxt * bp)11420 static void bnxt_set_dflt_rfs(struct bnxt *bp)
11421 {
11422 	struct net_device *dev = bp->dev;
11423 
11424 	dev->hw_features &= ~NETIF_F_NTUPLE;
11425 	dev->features &= ~NETIF_F_NTUPLE;
11426 	bp->flags &= ~BNXT_FLAG_RFS;
11427 	if (bnxt_rfs_supported(bp)) {
11428 		dev->hw_features |= NETIF_F_NTUPLE;
11429 		if (bnxt_rfs_capable(bp)) {
11430 			bp->flags |= BNXT_FLAG_RFS;
11431 			dev->features |= NETIF_F_NTUPLE;
11432 		}
11433 	}
11434 }
11435 
bnxt_fw_init_one_p3(struct bnxt * bp)11436 static void bnxt_fw_init_one_p3(struct bnxt *bp)
11437 {
11438 	struct pci_dev *pdev = bp->pdev;
11439 
11440 	bnxt_set_dflt_rss_hash_type(bp);
11441 	bnxt_set_dflt_rfs(bp);
11442 
11443 	bnxt_get_wol_settings(bp);
11444 	if (bp->flags & BNXT_FLAG_WOL_CAP)
11445 		device_set_wakeup_enable(&pdev->dev, bp->wol);
11446 	else
11447 		device_set_wakeup_capable(&pdev->dev, false);
11448 
11449 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
11450 	bnxt_hwrm_coal_params_qcaps(bp);
11451 }
11452 
11453 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
11454 
bnxt_fw_init_one(struct bnxt * bp)11455 static int bnxt_fw_init_one(struct bnxt *bp)
11456 {
11457 	int rc;
11458 
11459 	rc = bnxt_fw_init_one_p1(bp);
11460 	if (rc) {
11461 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
11462 		return rc;
11463 	}
11464 	rc = bnxt_fw_init_one_p2(bp);
11465 	if (rc) {
11466 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
11467 		return rc;
11468 	}
11469 	rc = bnxt_probe_phy(bp, false);
11470 	if (rc)
11471 		return rc;
11472 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
11473 	if (rc)
11474 		return rc;
11475 
11476 	/* In case fw capabilities have changed, destroy the unneeded
11477 	 * reporters and create newly capable ones.
11478 	 */
11479 	bnxt_dl_fw_reporters_destroy(bp, false);
11480 	bnxt_dl_fw_reporters_create(bp);
11481 	bnxt_fw_init_one_p3(bp);
11482 	return 0;
11483 }
11484 
bnxt_fw_reset_writel(struct bnxt * bp,int reg_idx)11485 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
11486 {
11487 	struct bnxt_fw_health *fw_health = bp->fw_health;
11488 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
11489 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
11490 	u32 reg_type, reg_off, delay_msecs;
11491 
11492 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
11493 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
11494 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
11495 	switch (reg_type) {
11496 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
11497 		pci_write_config_dword(bp->pdev, reg_off, val);
11498 		break;
11499 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
11500 		writel(reg_off & BNXT_GRC_BASE_MASK,
11501 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
11502 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
11503 		fallthrough;
11504 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
11505 		writel(val, bp->bar0 + reg_off);
11506 		break;
11507 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
11508 		writel(val, bp->bar1 + reg_off);
11509 		break;
11510 	}
11511 	if (delay_msecs) {
11512 		pci_read_config_dword(bp->pdev, 0, &val);
11513 		msleep(delay_msecs);
11514 	}
11515 }
11516 
bnxt_reset_all(struct bnxt * bp)11517 static void bnxt_reset_all(struct bnxt *bp)
11518 {
11519 	struct bnxt_fw_health *fw_health = bp->fw_health;
11520 	int i, rc;
11521 
11522 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
11523 		bnxt_fw_reset_via_optee(bp);
11524 		bp->fw_reset_timestamp = jiffies;
11525 		return;
11526 	}
11527 
11528 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
11529 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
11530 			bnxt_fw_reset_writel(bp, i);
11531 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
11532 		struct hwrm_fw_reset_input req = {0};
11533 
11534 		bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
11535 		req.resp_addr = cpu_to_le64(bp->hwrm_cmd_kong_resp_dma_addr);
11536 		req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
11537 		req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
11538 		req.flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
11539 		rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
11540 		if (rc)
11541 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
11542 	}
11543 	bp->fw_reset_timestamp = jiffies;
11544 }
11545 
bnxt_fw_reset_task(struct work_struct * work)11546 static void bnxt_fw_reset_task(struct work_struct *work)
11547 {
11548 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
11549 	int rc;
11550 
11551 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
11552 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
11553 		return;
11554 	}
11555 
11556 	switch (bp->fw_reset_state) {
11557 	case BNXT_FW_RESET_STATE_POLL_VF: {
11558 		int n = bnxt_get_registered_vfs(bp);
11559 		int tmo;
11560 
11561 		if (n < 0) {
11562 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
11563 				   n, jiffies_to_msecs(jiffies -
11564 				   bp->fw_reset_timestamp));
11565 			goto fw_reset_abort;
11566 		} else if (n > 0) {
11567 			if (time_after(jiffies, bp->fw_reset_timestamp +
11568 				       (bp->fw_reset_max_dsecs * HZ / 10))) {
11569 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11570 				bp->fw_reset_state = 0;
11571 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
11572 					   n);
11573 				return;
11574 			}
11575 			bnxt_queue_fw_reset_work(bp, HZ / 10);
11576 			return;
11577 		}
11578 		bp->fw_reset_timestamp = jiffies;
11579 		rtnl_lock();
11580 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
11581 			rtnl_unlock();
11582 			goto fw_reset_abort;
11583 		}
11584 		bnxt_fw_reset_close(bp);
11585 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
11586 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
11587 			tmo = HZ / 10;
11588 		} else {
11589 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11590 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
11591 		}
11592 		rtnl_unlock();
11593 		bnxt_queue_fw_reset_work(bp, tmo);
11594 		return;
11595 	}
11596 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
11597 		u32 val;
11598 
11599 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11600 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
11601 		    !time_after(jiffies, bp->fw_reset_timestamp +
11602 		    (bp->fw_reset_max_dsecs * HZ / 10))) {
11603 			bnxt_queue_fw_reset_work(bp, HZ / 5);
11604 			return;
11605 		}
11606 
11607 		if (!bp->fw_health->master) {
11608 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
11609 
11610 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11611 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
11612 			return;
11613 		}
11614 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
11615 	}
11616 		fallthrough;
11617 	case BNXT_FW_RESET_STATE_RESET_FW:
11618 		bnxt_reset_all(bp);
11619 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11620 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
11621 		return;
11622 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
11623 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
11624 			u32 val;
11625 
11626 			val = bnxt_fw_health_readl(bp,
11627 						   BNXT_FW_RESET_INPROG_REG);
11628 			if (val)
11629 				netdev_warn(bp->dev, "FW reset inprog %x after min wait time.\n",
11630 					    val);
11631 		}
11632 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
11633 		if (pci_enable_device(bp->pdev)) {
11634 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
11635 			goto fw_reset_abort;
11636 		}
11637 		pci_set_master(bp->pdev);
11638 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
11639 		fallthrough;
11640 	case BNXT_FW_RESET_STATE_POLL_FW:
11641 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
11642 		rc = __bnxt_hwrm_ver_get(bp, true);
11643 		if (rc) {
11644 			if (time_after(jiffies, bp->fw_reset_timestamp +
11645 				       (bp->fw_reset_max_dsecs * HZ / 10))) {
11646 				netdev_err(bp->dev, "Firmware reset aborted\n");
11647 				goto fw_reset_abort_status;
11648 			}
11649 			bnxt_queue_fw_reset_work(bp, HZ / 5);
11650 			return;
11651 		}
11652 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
11653 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
11654 		fallthrough;
11655 	case BNXT_FW_RESET_STATE_OPENING:
11656 		while (!rtnl_trylock()) {
11657 			bnxt_queue_fw_reset_work(bp, HZ / 10);
11658 			return;
11659 		}
11660 		rc = bnxt_open(bp->dev);
11661 		if (rc) {
11662 			netdev_err(bp->dev, "bnxt_open_nic() failed\n");
11663 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11664 			dev_close(bp->dev);
11665 		}
11666 
11667 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
11668 		    bp->fw_health->enabled) {
11669 			bp->fw_health->last_fw_reset_cnt =
11670 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
11671 		}
11672 		bp->fw_reset_state = 0;
11673 		/* Make sure fw_reset_state is 0 before clearing the flag */
11674 		smp_mb__before_atomic();
11675 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11676 		bnxt_ulp_start(bp, rc);
11677 		if (!rc)
11678 			bnxt_reenable_sriov(bp);
11679 		bnxt_dl_health_recovery_done(bp);
11680 		bnxt_dl_health_status_update(bp, true);
11681 		rtnl_unlock();
11682 		break;
11683 	}
11684 	return;
11685 
11686 fw_reset_abort_status:
11687 	if (bp->fw_health->status_reliable ||
11688 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
11689 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11690 
11691 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
11692 	}
11693 fw_reset_abort:
11694 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11695 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
11696 		bnxt_dl_health_status_update(bp, false);
11697 	bp->fw_reset_state = 0;
11698 	rtnl_lock();
11699 	dev_close(bp->dev);
11700 	rtnl_unlock();
11701 }
11702 
bnxt_init_board(struct pci_dev * pdev,struct net_device * dev)11703 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
11704 {
11705 	int rc;
11706 	struct bnxt *bp = netdev_priv(dev);
11707 
11708 	SET_NETDEV_DEV(dev, &pdev->dev);
11709 
11710 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
11711 	rc = pci_enable_device(pdev);
11712 	if (rc) {
11713 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
11714 		goto init_err;
11715 	}
11716 
11717 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
11718 		dev_err(&pdev->dev,
11719 			"Cannot find PCI device base address, aborting\n");
11720 		rc = -ENODEV;
11721 		goto init_err_disable;
11722 	}
11723 
11724 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
11725 	if (rc) {
11726 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
11727 		goto init_err_disable;
11728 	}
11729 
11730 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
11731 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
11732 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
11733 		rc = -EIO;
11734 		goto init_err_release;
11735 	}
11736 
11737 	pci_set_master(pdev);
11738 
11739 	bp->dev = dev;
11740 	bp->pdev = pdev;
11741 
11742 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
11743 	 * determines the BAR size.
11744 	 */
11745 	bp->bar0 = pci_ioremap_bar(pdev, 0);
11746 	if (!bp->bar0) {
11747 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
11748 		rc = -ENOMEM;
11749 		goto init_err_release;
11750 	}
11751 
11752 	bp->bar2 = pci_ioremap_bar(pdev, 4);
11753 	if (!bp->bar2) {
11754 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
11755 		rc = -ENOMEM;
11756 		goto init_err_release;
11757 	}
11758 
11759 	pci_enable_pcie_error_reporting(pdev);
11760 
11761 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
11762 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
11763 
11764 	spin_lock_init(&bp->ntp_fltr_lock);
11765 #if BITS_PER_LONG == 32
11766 	spin_lock_init(&bp->db_lock);
11767 #endif
11768 
11769 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
11770 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
11771 
11772 	bnxt_init_dflt_coal(bp);
11773 
11774 	timer_setup(&bp->timer, bnxt_timer, 0);
11775 	bp->current_interval = BNXT_TIMER_INTERVAL;
11776 
11777 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
11778 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
11779 
11780 	clear_bit(BNXT_STATE_OPEN, &bp->state);
11781 	return 0;
11782 
11783 init_err_release:
11784 	bnxt_unmap_bars(bp, pdev);
11785 	pci_release_regions(pdev);
11786 
11787 init_err_disable:
11788 	pci_disable_device(pdev);
11789 
11790 init_err:
11791 	return rc;
11792 }
11793 
11794 /* rtnl_lock held */
bnxt_change_mac_addr(struct net_device * dev,void * p)11795 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
11796 {
11797 	struct sockaddr *addr = p;
11798 	struct bnxt *bp = netdev_priv(dev);
11799 	int rc = 0;
11800 
11801 	if (!is_valid_ether_addr(addr->sa_data))
11802 		return -EADDRNOTAVAIL;
11803 
11804 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
11805 		return 0;
11806 
11807 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
11808 	if (rc)
11809 		return rc;
11810 
11811 	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
11812 	if (netif_running(dev)) {
11813 		bnxt_close_nic(bp, false, false);
11814 		rc = bnxt_open_nic(bp, false, false);
11815 	}
11816 
11817 	return rc;
11818 }
11819 
11820 /* rtnl_lock held */
bnxt_change_mtu(struct net_device * dev,int new_mtu)11821 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
11822 {
11823 	struct bnxt *bp = netdev_priv(dev);
11824 
11825 	if (netif_running(dev))
11826 		bnxt_close_nic(bp, true, false);
11827 
11828 	dev->mtu = new_mtu;
11829 	bnxt_set_ring_params(bp);
11830 
11831 	if (netif_running(dev))
11832 		return bnxt_open_nic(bp, true, false);
11833 
11834 	return 0;
11835 }
11836 
bnxt_setup_mq_tc(struct net_device * dev,u8 tc)11837 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
11838 {
11839 	struct bnxt *bp = netdev_priv(dev);
11840 	bool sh = false;
11841 	int rc;
11842 
11843 	if (tc > bp->max_tc) {
11844 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
11845 			   tc, bp->max_tc);
11846 		return -EINVAL;
11847 	}
11848 
11849 	if (netdev_get_num_tc(dev) == tc)
11850 		return 0;
11851 
11852 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11853 		sh = true;
11854 
11855 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
11856 			      sh, tc, bp->tx_nr_rings_xdp);
11857 	if (rc)
11858 		return rc;
11859 
11860 	/* Needs to close the device and do hw resource re-allocations */
11861 	if (netif_running(bp->dev))
11862 		bnxt_close_nic(bp, true, false);
11863 
11864 	if (tc) {
11865 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
11866 		netdev_set_num_tc(dev, tc);
11867 	} else {
11868 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
11869 		netdev_reset_tc(dev);
11870 	}
11871 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
11872 	bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
11873 			       bp->tx_nr_rings + bp->rx_nr_rings;
11874 
11875 	if (netif_running(bp->dev))
11876 		return bnxt_open_nic(bp, true, false);
11877 
11878 	return 0;
11879 }
11880 
bnxt_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)11881 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
11882 				  void *cb_priv)
11883 {
11884 	struct bnxt *bp = cb_priv;
11885 
11886 	if (!bnxt_tc_flower_enabled(bp) ||
11887 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
11888 		return -EOPNOTSUPP;
11889 
11890 	switch (type) {
11891 	case TC_SETUP_CLSFLOWER:
11892 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
11893 	default:
11894 		return -EOPNOTSUPP;
11895 	}
11896 }
11897 
11898 LIST_HEAD(bnxt_block_cb_list);
11899 
bnxt_setup_tc(struct net_device * dev,enum tc_setup_type type,void * type_data)11900 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
11901 			 void *type_data)
11902 {
11903 	struct bnxt *bp = netdev_priv(dev);
11904 
11905 	switch (type) {
11906 	case TC_SETUP_BLOCK:
11907 		return flow_block_cb_setup_simple(type_data,
11908 						  &bnxt_block_cb_list,
11909 						  bnxt_setup_tc_block_cb,
11910 						  bp, bp, true);
11911 	case TC_SETUP_QDISC_MQPRIO: {
11912 		struct tc_mqprio_qopt *mqprio = type_data;
11913 
11914 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
11915 
11916 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
11917 	}
11918 	default:
11919 		return -EOPNOTSUPP;
11920 	}
11921 }
11922 
11923 #ifdef CONFIG_RFS_ACCEL
bnxt_fltr_match(struct bnxt_ntuple_filter * f1,struct bnxt_ntuple_filter * f2)11924 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
11925 			    struct bnxt_ntuple_filter *f2)
11926 {
11927 	struct flow_keys *keys1 = &f1->fkeys;
11928 	struct flow_keys *keys2 = &f2->fkeys;
11929 
11930 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
11931 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
11932 		return false;
11933 
11934 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
11935 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
11936 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst)
11937 			return false;
11938 	} else {
11939 		if (memcmp(&keys1->addrs.v6addrs.src, &keys2->addrs.v6addrs.src,
11940 			   sizeof(keys1->addrs.v6addrs.src)) ||
11941 		    memcmp(&keys1->addrs.v6addrs.dst, &keys2->addrs.v6addrs.dst,
11942 			   sizeof(keys1->addrs.v6addrs.dst)))
11943 			return false;
11944 	}
11945 
11946 	if (keys1->ports.ports == keys2->ports.ports &&
11947 	    keys1->control.flags == keys2->control.flags &&
11948 	    ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
11949 	    ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
11950 		return true;
11951 
11952 	return false;
11953 }
11954 
bnxt_rx_flow_steer(struct net_device * dev,const struct sk_buff * skb,u16 rxq_index,u32 flow_id)11955 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
11956 			      u16 rxq_index, u32 flow_id)
11957 {
11958 	struct bnxt *bp = netdev_priv(dev);
11959 	struct bnxt_ntuple_filter *fltr, *new_fltr;
11960 	struct flow_keys *fkeys;
11961 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
11962 	int rc = 0, idx, bit_id, l2_idx = 0;
11963 	struct hlist_head *head;
11964 	u32 flags;
11965 
11966 	if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
11967 		struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
11968 		int off = 0, j;
11969 
11970 		netif_addr_lock_bh(dev);
11971 		for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
11972 			if (ether_addr_equal(eth->h_dest,
11973 					     vnic->uc_list + off)) {
11974 				l2_idx = j + 1;
11975 				break;
11976 			}
11977 		}
11978 		netif_addr_unlock_bh(dev);
11979 		if (!l2_idx)
11980 			return -EINVAL;
11981 	}
11982 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
11983 	if (!new_fltr)
11984 		return -ENOMEM;
11985 
11986 	fkeys = &new_fltr->fkeys;
11987 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
11988 		rc = -EPROTONOSUPPORT;
11989 		goto err_free;
11990 	}
11991 
11992 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
11993 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
11994 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
11995 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
11996 		rc = -EPROTONOSUPPORT;
11997 		goto err_free;
11998 	}
11999 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
12000 	    bp->hwrm_spec_code < 0x10601) {
12001 		rc = -EPROTONOSUPPORT;
12002 		goto err_free;
12003 	}
12004 	flags = fkeys->control.flags;
12005 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
12006 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
12007 		rc = -EPROTONOSUPPORT;
12008 		goto err_free;
12009 	}
12010 
12011 	memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
12012 	memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
12013 
12014 	idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
12015 	head = &bp->ntp_fltr_hash_tbl[idx];
12016 	rcu_read_lock();
12017 	hlist_for_each_entry_rcu(fltr, head, hash) {
12018 		if (bnxt_fltr_match(fltr, new_fltr)) {
12019 			rc = fltr->sw_id;
12020 			rcu_read_unlock();
12021 			goto err_free;
12022 		}
12023 	}
12024 	rcu_read_unlock();
12025 
12026 	spin_lock_bh(&bp->ntp_fltr_lock);
12027 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
12028 					 BNXT_NTP_FLTR_MAX_FLTR, 0);
12029 	if (bit_id < 0) {
12030 		spin_unlock_bh(&bp->ntp_fltr_lock);
12031 		rc = -ENOMEM;
12032 		goto err_free;
12033 	}
12034 
12035 	new_fltr->sw_id = (u16)bit_id;
12036 	new_fltr->flow_id = flow_id;
12037 	new_fltr->l2_fltr_idx = l2_idx;
12038 	new_fltr->rxq = rxq_index;
12039 	hlist_add_head_rcu(&new_fltr->hash, head);
12040 	bp->ntp_fltr_count++;
12041 	spin_unlock_bh(&bp->ntp_fltr_lock);
12042 
12043 	set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
12044 	bnxt_queue_sp_work(bp);
12045 
12046 	return new_fltr->sw_id;
12047 
12048 err_free:
12049 	kfree(new_fltr);
12050 	return rc;
12051 }
12052 
bnxt_cfg_ntp_filters(struct bnxt * bp)12053 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
12054 {
12055 	int i;
12056 
12057 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
12058 		struct hlist_head *head;
12059 		struct hlist_node *tmp;
12060 		struct bnxt_ntuple_filter *fltr;
12061 		int rc;
12062 
12063 		head = &bp->ntp_fltr_hash_tbl[i];
12064 		hlist_for_each_entry_safe(fltr, tmp, head, hash) {
12065 			bool del = false;
12066 
12067 			if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
12068 				if (rps_may_expire_flow(bp->dev, fltr->rxq,
12069 							fltr->flow_id,
12070 							fltr->sw_id)) {
12071 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
12072 									 fltr);
12073 					del = true;
12074 				}
12075 			} else {
12076 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
12077 								       fltr);
12078 				if (rc)
12079 					del = true;
12080 				else
12081 					set_bit(BNXT_FLTR_VALID, &fltr->state);
12082 			}
12083 
12084 			if (del) {
12085 				spin_lock_bh(&bp->ntp_fltr_lock);
12086 				hlist_del_rcu(&fltr->hash);
12087 				bp->ntp_fltr_count--;
12088 				spin_unlock_bh(&bp->ntp_fltr_lock);
12089 				synchronize_rcu();
12090 				clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
12091 				kfree(fltr);
12092 			}
12093 		}
12094 	}
12095 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
12096 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
12097 }
12098 
12099 #else
12100 
bnxt_cfg_ntp_filters(struct bnxt * bp)12101 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
12102 {
12103 }
12104 
12105 #endif /* CONFIG_RFS_ACCEL */
12106 
bnxt_udp_tunnel_set_port(struct net_device * netdev,unsigned int table,unsigned int entry,struct udp_tunnel_info * ti)12107 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
12108 				    unsigned int entry, struct udp_tunnel_info *ti)
12109 {
12110 	struct bnxt *bp = netdev_priv(netdev);
12111 	unsigned int cmd;
12112 
12113 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
12114 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
12115 	else
12116 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
12117 
12118 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
12119 }
12120 
bnxt_udp_tunnel_unset_port(struct net_device * netdev,unsigned int table,unsigned int entry,struct udp_tunnel_info * ti)12121 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
12122 				      unsigned int entry, struct udp_tunnel_info *ti)
12123 {
12124 	struct bnxt *bp = netdev_priv(netdev);
12125 	unsigned int cmd;
12126 
12127 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
12128 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
12129 	else
12130 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
12131 
12132 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
12133 }
12134 
12135 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
12136 	.set_port	= bnxt_udp_tunnel_set_port,
12137 	.unset_port	= bnxt_udp_tunnel_unset_port,
12138 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
12139 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
12140 	.tables		= {
12141 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
12142 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
12143 	},
12144 };
12145 
bnxt_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 filter_mask,int nlflags)12146 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12147 			       struct net_device *dev, u32 filter_mask,
12148 			       int nlflags)
12149 {
12150 	struct bnxt *bp = netdev_priv(dev);
12151 
12152 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
12153 				       nlflags, filter_mask, NULL);
12154 }
12155 
bnxt_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags,struct netlink_ext_ack * extack)12156 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
12157 			       u16 flags, struct netlink_ext_ack *extack)
12158 {
12159 	struct bnxt *bp = netdev_priv(dev);
12160 	struct nlattr *attr, *br_spec;
12161 	int rem, rc = 0;
12162 
12163 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
12164 		return -EOPNOTSUPP;
12165 
12166 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12167 	if (!br_spec)
12168 		return -EINVAL;
12169 
12170 	nla_for_each_nested(attr, br_spec, rem) {
12171 		u16 mode;
12172 
12173 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
12174 			continue;
12175 
12176 		if (nla_len(attr) < sizeof(mode))
12177 			return -EINVAL;
12178 
12179 		mode = nla_get_u16(attr);
12180 		if (mode == bp->br_mode)
12181 			break;
12182 
12183 		rc = bnxt_hwrm_set_br_mode(bp, mode);
12184 		if (!rc)
12185 			bp->br_mode = mode;
12186 		break;
12187 	}
12188 	return rc;
12189 }
12190 
bnxt_get_port_parent_id(struct net_device * dev,struct netdev_phys_item_id * ppid)12191 int bnxt_get_port_parent_id(struct net_device *dev,
12192 			    struct netdev_phys_item_id *ppid)
12193 {
12194 	struct bnxt *bp = netdev_priv(dev);
12195 
12196 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
12197 		return -EOPNOTSUPP;
12198 
12199 	/* The PF and it's VF-reps only support the switchdev framework */
12200 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
12201 		return -EOPNOTSUPP;
12202 
12203 	ppid->id_len = sizeof(bp->dsn);
12204 	memcpy(ppid->id, bp->dsn, ppid->id_len);
12205 
12206 	return 0;
12207 }
12208 
bnxt_get_devlink_port(struct net_device * dev)12209 static struct devlink_port *bnxt_get_devlink_port(struct net_device *dev)
12210 {
12211 	struct bnxt *bp = netdev_priv(dev);
12212 
12213 	return &bp->dl_port;
12214 }
12215 
12216 static const struct net_device_ops bnxt_netdev_ops = {
12217 	.ndo_open		= bnxt_open,
12218 	.ndo_start_xmit		= bnxt_start_xmit,
12219 	.ndo_stop		= bnxt_close,
12220 	.ndo_get_stats64	= bnxt_get_stats64,
12221 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
12222 	.ndo_do_ioctl		= bnxt_ioctl,
12223 	.ndo_validate_addr	= eth_validate_addr,
12224 	.ndo_set_mac_address	= bnxt_change_mac_addr,
12225 	.ndo_change_mtu		= bnxt_change_mtu,
12226 	.ndo_fix_features	= bnxt_fix_features,
12227 	.ndo_set_features	= bnxt_set_features,
12228 	.ndo_tx_timeout		= bnxt_tx_timeout,
12229 #ifdef CONFIG_BNXT_SRIOV
12230 	.ndo_get_vf_config	= bnxt_get_vf_config,
12231 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
12232 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
12233 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
12234 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
12235 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
12236 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
12237 #endif
12238 	.ndo_setup_tc           = bnxt_setup_tc,
12239 #ifdef CONFIG_RFS_ACCEL
12240 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
12241 #endif
12242 	.ndo_udp_tunnel_add	= udp_tunnel_nic_add_port,
12243 	.ndo_udp_tunnel_del	= udp_tunnel_nic_del_port,
12244 	.ndo_bpf		= bnxt_xdp,
12245 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
12246 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
12247 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
12248 	.ndo_get_devlink_port	= bnxt_get_devlink_port,
12249 };
12250 
bnxt_remove_one(struct pci_dev * pdev)12251 static void bnxt_remove_one(struct pci_dev *pdev)
12252 {
12253 	struct net_device *dev = pci_get_drvdata(pdev);
12254 	struct bnxt *bp = netdev_priv(dev);
12255 
12256 	if (BNXT_PF(bp))
12257 		bnxt_sriov_disable(bp);
12258 
12259 	if (BNXT_PF(bp))
12260 		devlink_port_type_clear(&bp->dl_port);
12261 	pci_disable_pcie_error_reporting(pdev);
12262 	unregister_netdev(dev);
12263 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12264 	/* Flush any pending tasks */
12265 	cancel_work_sync(&bp->sp_task);
12266 	cancel_delayed_work_sync(&bp->fw_reset_task);
12267 	bp->sp_event = 0;
12268 
12269 	bnxt_dl_fw_reporters_destroy(bp, true);
12270 	bnxt_dl_unregister(bp);
12271 	bnxt_shutdown_tc(bp);
12272 
12273 	bnxt_clear_int_mode(bp);
12274 	bnxt_hwrm_func_drv_unrgtr(bp);
12275 	bnxt_free_hwrm_resources(bp);
12276 	bnxt_free_hwrm_short_cmd_req(bp);
12277 	bnxt_ethtool_free(bp);
12278 	bnxt_dcb_free(bp);
12279 	kfree(bp->edev);
12280 	bp->edev = NULL;
12281 	kfree(bp->fw_health);
12282 	bp->fw_health = NULL;
12283 	bnxt_cleanup_pci(bp);
12284 	bnxt_free_ctx_mem(bp);
12285 	kfree(bp->ctx);
12286 	bp->ctx = NULL;
12287 	kfree(bp->rss_indir_tbl);
12288 	bp->rss_indir_tbl = NULL;
12289 	bnxt_free_port_stats(bp);
12290 	free_netdev(dev);
12291 }
12292 
bnxt_probe_phy(struct bnxt * bp,bool fw_dflt)12293 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
12294 {
12295 	int rc = 0;
12296 	struct bnxt_link_info *link_info = &bp->link_info;
12297 
12298 	rc = bnxt_hwrm_phy_qcaps(bp);
12299 	if (rc) {
12300 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
12301 			   rc);
12302 		return rc;
12303 	}
12304 	if (!fw_dflt)
12305 		return 0;
12306 
12307 	rc = bnxt_update_link(bp, false);
12308 	if (rc) {
12309 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
12310 			   rc);
12311 		return rc;
12312 	}
12313 
12314 	/* Older firmware does not have supported_auto_speeds, so assume
12315 	 * that all supported speeds can be autonegotiated.
12316 	 */
12317 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
12318 		link_info->support_auto_speeds = link_info->support_speeds;
12319 
12320 	bnxt_init_ethtool_link_settings(bp);
12321 	return 0;
12322 }
12323 
bnxt_get_max_irq(struct pci_dev * pdev)12324 static int bnxt_get_max_irq(struct pci_dev *pdev)
12325 {
12326 	u16 ctrl;
12327 
12328 	if (!pdev->msix_cap)
12329 		return 1;
12330 
12331 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
12332 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
12333 }
12334 
_bnxt_get_max_rings(struct bnxt * bp,int * max_rx,int * max_tx,int * max_cp)12335 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
12336 				int *max_cp)
12337 {
12338 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12339 	int max_ring_grps = 0, max_irq;
12340 
12341 	*max_tx = hw_resc->max_tx_rings;
12342 	*max_rx = hw_resc->max_rx_rings;
12343 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
12344 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
12345 			bnxt_get_ulp_msix_num(bp),
12346 			hw_resc->max_stat_ctxs - bnxt_get_ulp_stat_ctxs(bp));
12347 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
12348 		*max_cp = min_t(int, *max_cp, max_irq);
12349 	max_ring_grps = hw_resc->max_hw_ring_grps;
12350 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
12351 		*max_cp -= 1;
12352 		*max_rx -= 2;
12353 	}
12354 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
12355 		*max_rx >>= 1;
12356 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
12357 		bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
12358 		/* On P5 chips, max_cp output param should be available NQs */
12359 		*max_cp = max_irq;
12360 	}
12361 	*max_rx = min_t(int, *max_rx, max_ring_grps);
12362 }
12363 
bnxt_get_max_rings(struct bnxt * bp,int * max_rx,int * max_tx,bool shared)12364 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
12365 {
12366 	int rx, tx, cp;
12367 
12368 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
12369 	*max_rx = rx;
12370 	*max_tx = tx;
12371 	if (!rx || !tx || !cp)
12372 		return -ENOMEM;
12373 
12374 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
12375 }
12376 
bnxt_get_dflt_rings(struct bnxt * bp,int * max_rx,int * max_tx,bool shared)12377 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
12378 			       bool shared)
12379 {
12380 	int rc;
12381 
12382 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
12383 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
12384 		/* Not enough rings, try disabling agg rings. */
12385 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
12386 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
12387 		if (rc) {
12388 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
12389 			bp->flags |= BNXT_FLAG_AGG_RINGS;
12390 			return rc;
12391 		}
12392 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
12393 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12394 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12395 		bnxt_set_ring_params(bp);
12396 	}
12397 
12398 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
12399 		int max_cp, max_stat, max_irq;
12400 
12401 		/* Reserve minimum resources for RoCE */
12402 		max_cp = bnxt_get_max_func_cp_rings(bp);
12403 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
12404 		max_irq = bnxt_get_max_func_irqs(bp);
12405 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
12406 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
12407 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
12408 			return 0;
12409 
12410 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
12411 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
12412 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
12413 		max_cp = min_t(int, max_cp, max_irq);
12414 		max_cp = min_t(int, max_cp, max_stat);
12415 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
12416 		if (rc)
12417 			rc = 0;
12418 	}
12419 	return rc;
12420 }
12421 
12422 /* In initial default shared ring setting, each shared ring must have a
12423  * RX/TX ring pair.
12424  */
bnxt_trim_dflt_sh_rings(struct bnxt * bp)12425 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
12426 {
12427 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
12428 	bp->rx_nr_rings = bp->cp_nr_rings;
12429 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
12430 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
12431 }
12432 
bnxt_set_dflt_rings(struct bnxt * bp,bool sh)12433 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
12434 {
12435 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
12436 
12437 	if (!bnxt_can_reserve_rings(bp))
12438 		return 0;
12439 
12440 	if (sh)
12441 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
12442 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
12443 	/* Reduce default rings on multi-port cards so that total default
12444 	 * rings do not exceed CPU count.
12445 	 */
12446 	if (bp->port_count > 1) {
12447 		int max_rings =
12448 			max_t(int, num_online_cpus() / bp->port_count, 1);
12449 
12450 		dflt_rings = min_t(int, dflt_rings, max_rings);
12451 	}
12452 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
12453 	if (rc)
12454 		return rc;
12455 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
12456 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
12457 	if (sh)
12458 		bnxt_trim_dflt_sh_rings(bp);
12459 	else
12460 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
12461 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
12462 
12463 	rc = __bnxt_reserve_rings(bp);
12464 	if (rc)
12465 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
12466 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
12467 	if (sh)
12468 		bnxt_trim_dflt_sh_rings(bp);
12469 
12470 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
12471 	if (bnxt_need_reserve_rings(bp)) {
12472 		rc = __bnxt_reserve_rings(bp);
12473 		if (rc)
12474 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
12475 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
12476 	}
12477 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
12478 		bp->rx_nr_rings++;
12479 		bp->cp_nr_rings++;
12480 	}
12481 	if (rc) {
12482 		bp->tx_nr_rings = 0;
12483 		bp->rx_nr_rings = 0;
12484 	}
12485 	return rc;
12486 }
12487 
bnxt_init_dflt_ring_mode(struct bnxt * bp)12488 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
12489 {
12490 	int rc;
12491 
12492 	if (bp->tx_nr_rings)
12493 		return 0;
12494 
12495 	bnxt_ulp_irq_stop(bp);
12496 	bnxt_clear_int_mode(bp);
12497 	rc = bnxt_set_dflt_rings(bp, true);
12498 	if (rc) {
12499 		netdev_err(bp->dev, "Not enough rings available.\n");
12500 		goto init_dflt_ring_err;
12501 	}
12502 	rc = bnxt_init_int_mode(bp);
12503 	if (rc)
12504 		goto init_dflt_ring_err;
12505 
12506 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
12507 
12508 	bnxt_set_dflt_rfs(bp);
12509 
12510 init_dflt_ring_err:
12511 	bnxt_ulp_irq_restart(bp, rc);
12512 	return rc;
12513 }
12514 
bnxt_restore_pf_fw_resources(struct bnxt * bp)12515 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
12516 {
12517 	int rc;
12518 
12519 	ASSERT_RTNL();
12520 	bnxt_hwrm_func_qcaps(bp);
12521 
12522 	if (netif_running(bp->dev))
12523 		__bnxt_close_nic(bp, true, false);
12524 
12525 	bnxt_ulp_irq_stop(bp);
12526 	bnxt_clear_int_mode(bp);
12527 	rc = bnxt_init_int_mode(bp);
12528 	bnxt_ulp_irq_restart(bp, rc);
12529 
12530 	if (netif_running(bp->dev)) {
12531 		if (rc)
12532 			dev_close(bp->dev);
12533 		else
12534 			rc = bnxt_open_nic(bp, true, false);
12535 	}
12536 
12537 	return rc;
12538 }
12539 
bnxt_init_mac_addr(struct bnxt * bp)12540 static int bnxt_init_mac_addr(struct bnxt *bp)
12541 {
12542 	int rc = 0;
12543 
12544 	if (BNXT_PF(bp)) {
12545 		memcpy(bp->dev->dev_addr, bp->pf.mac_addr, ETH_ALEN);
12546 	} else {
12547 #ifdef CONFIG_BNXT_SRIOV
12548 		struct bnxt_vf_info *vf = &bp->vf;
12549 		bool strict_approval = true;
12550 
12551 		if (is_valid_ether_addr(vf->mac_addr)) {
12552 			/* overwrite netdev dev_addr with admin VF MAC */
12553 			memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
12554 			/* Older PF driver or firmware may not approve this
12555 			 * correctly.
12556 			 */
12557 			strict_approval = false;
12558 		} else {
12559 			eth_hw_addr_random(bp->dev);
12560 		}
12561 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
12562 #endif
12563 	}
12564 	return rc;
12565 }
12566 
12567 #define BNXT_VPD_LEN	512
bnxt_vpd_read_info(struct bnxt * bp)12568 static void bnxt_vpd_read_info(struct bnxt *bp)
12569 {
12570 	struct pci_dev *pdev = bp->pdev;
12571 	int i, len, pos, ro_size, size;
12572 	ssize_t vpd_size;
12573 	u8 *vpd_data;
12574 
12575 	vpd_data = kmalloc(BNXT_VPD_LEN, GFP_KERNEL);
12576 	if (!vpd_data)
12577 		return;
12578 
12579 	vpd_size = pci_read_vpd(pdev, 0, BNXT_VPD_LEN, vpd_data);
12580 	if (vpd_size <= 0) {
12581 		netdev_err(bp->dev, "Unable to read VPD\n");
12582 		goto exit;
12583 	}
12584 
12585 	i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
12586 	if (i < 0) {
12587 		netdev_err(bp->dev, "VPD READ-Only not found\n");
12588 		goto exit;
12589 	}
12590 
12591 	ro_size = pci_vpd_lrdt_size(&vpd_data[i]);
12592 	i += PCI_VPD_LRDT_TAG_SIZE;
12593 	if (i + ro_size > vpd_size)
12594 		goto exit;
12595 
12596 	pos = pci_vpd_find_info_keyword(vpd_data, i, ro_size,
12597 					PCI_VPD_RO_KEYWORD_PARTNO);
12598 	if (pos < 0)
12599 		goto read_sn;
12600 
12601 	len = pci_vpd_info_field_size(&vpd_data[pos]);
12602 	pos += PCI_VPD_INFO_FLD_HDR_SIZE;
12603 	if (len + pos > vpd_size)
12604 		goto read_sn;
12605 
12606 	size = min(len, BNXT_VPD_FLD_LEN - 1);
12607 	memcpy(bp->board_partno, &vpd_data[pos], size);
12608 
12609 read_sn:
12610 	pos = pci_vpd_find_info_keyword(vpd_data, i, ro_size,
12611 					PCI_VPD_RO_KEYWORD_SERIALNO);
12612 	if (pos < 0)
12613 		goto exit;
12614 
12615 	len = pci_vpd_info_field_size(&vpd_data[pos]);
12616 	pos += PCI_VPD_INFO_FLD_HDR_SIZE;
12617 	if (len + pos > vpd_size)
12618 		goto exit;
12619 
12620 	size = min(len, BNXT_VPD_FLD_LEN - 1);
12621 	memcpy(bp->board_serialno, &vpd_data[pos], size);
12622 exit:
12623 	kfree(vpd_data);
12624 }
12625 
bnxt_pcie_dsn_get(struct bnxt * bp,u8 dsn[])12626 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
12627 {
12628 	struct pci_dev *pdev = bp->pdev;
12629 	u64 qword;
12630 
12631 	qword = pci_get_dsn(pdev);
12632 	if (!qword) {
12633 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
12634 		return -EOPNOTSUPP;
12635 	}
12636 
12637 	put_unaligned_le64(qword, dsn);
12638 
12639 	bp->flags |= BNXT_FLAG_DSN_VALID;
12640 	return 0;
12641 }
12642 
bnxt_map_db_bar(struct bnxt * bp)12643 static int bnxt_map_db_bar(struct bnxt *bp)
12644 {
12645 	if (!bp->db_size)
12646 		return -ENODEV;
12647 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
12648 	if (!bp->bar1)
12649 		return -ENOMEM;
12650 	return 0;
12651 }
12652 
bnxt_init_one(struct pci_dev * pdev,const struct pci_device_id * ent)12653 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
12654 {
12655 	struct net_device *dev;
12656 	struct bnxt *bp;
12657 	int rc, max_irqs;
12658 
12659 	if (pci_is_bridge(pdev))
12660 		return -ENODEV;
12661 
12662 	/* Clear any pending DMA transactions from crash kernel
12663 	 * while loading driver in capture kernel.
12664 	 */
12665 	if (is_kdump_kernel()) {
12666 		pci_clear_master(pdev);
12667 		pcie_flr(pdev);
12668 	}
12669 
12670 	max_irqs = bnxt_get_max_irq(pdev);
12671 	dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
12672 	if (!dev)
12673 		return -ENOMEM;
12674 
12675 	bp = netdev_priv(dev);
12676 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
12677 	bnxt_set_max_func_irqs(bp, max_irqs);
12678 
12679 	if (bnxt_vf_pciid(ent->driver_data))
12680 		bp->flags |= BNXT_FLAG_VF;
12681 
12682 	if (pdev->msix_cap)
12683 		bp->flags |= BNXT_FLAG_MSIX_CAP;
12684 
12685 	rc = bnxt_init_board(pdev, dev);
12686 	if (rc < 0)
12687 		goto init_err_free;
12688 
12689 	dev->netdev_ops = &bnxt_netdev_ops;
12690 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
12691 	dev->ethtool_ops = &bnxt_ethtool_ops;
12692 	pci_set_drvdata(pdev, dev);
12693 
12694 	if (BNXT_PF(bp))
12695 		bnxt_vpd_read_info(bp);
12696 
12697 	rc = bnxt_alloc_hwrm_resources(bp);
12698 	if (rc)
12699 		goto init_err_pci_clean;
12700 
12701 	mutex_init(&bp->hwrm_cmd_lock);
12702 	mutex_init(&bp->link_lock);
12703 
12704 	rc = bnxt_fw_init_one_p1(bp);
12705 	if (rc)
12706 		goto init_err_pci_clean;
12707 
12708 	if (BNXT_CHIP_P5(bp)) {
12709 		bp->flags |= BNXT_FLAG_CHIP_P5;
12710 		if (BNXT_CHIP_SR2(bp))
12711 			bp->flags |= BNXT_FLAG_CHIP_SR2;
12712 	}
12713 
12714 	rc = bnxt_alloc_rss_indir_tbl(bp);
12715 	if (rc)
12716 		goto init_err_pci_clean;
12717 
12718 	rc = bnxt_fw_init_one_p2(bp);
12719 	if (rc)
12720 		goto init_err_pci_clean;
12721 
12722 	rc = bnxt_map_db_bar(bp);
12723 	if (rc) {
12724 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
12725 			rc);
12726 		goto init_err_pci_clean;
12727 	}
12728 
12729 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
12730 			   NETIF_F_TSO | NETIF_F_TSO6 |
12731 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
12732 			   NETIF_F_GSO_IPXIP4 |
12733 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
12734 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
12735 			   NETIF_F_RXCSUM | NETIF_F_GRO;
12736 
12737 	if (BNXT_SUPPORTS_TPA(bp))
12738 		dev->hw_features |= NETIF_F_LRO;
12739 
12740 	dev->hw_enc_features =
12741 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
12742 			NETIF_F_TSO | NETIF_F_TSO6 |
12743 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
12744 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
12745 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
12746 	dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
12747 
12748 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
12749 				    NETIF_F_GSO_GRE_CSUM;
12750 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
12751 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
12752 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
12753 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
12754 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
12755 	if (BNXT_SUPPORTS_TPA(bp))
12756 		dev->hw_features |= NETIF_F_GRO_HW;
12757 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
12758 	if (dev->features & NETIF_F_GRO_HW)
12759 		dev->features &= ~NETIF_F_LRO;
12760 	dev->priv_flags |= IFF_UNICAST_FLT;
12761 
12762 #ifdef CONFIG_BNXT_SRIOV
12763 	init_waitqueue_head(&bp->sriov_cfg_wait);
12764 	mutex_init(&bp->sriov_lock);
12765 #endif
12766 	if (BNXT_SUPPORTS_TPA(bp)) {
12767 		bp->gro_func = bnxt_gro_func_5730x;
12768 		if (BNXT_CHIP_P4(bp))
12769 			bp->gro_func = bnxt_gro_func_5731x;
12770 		else if (BNXT_CHIP_P5(bp))
12771 			bp->gro_func = bnxt_gro_func_5750x;
12772 	}
12773 	if (!BNXT_CHIP_P4_PLUS(bp))
12774 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
12775 
12776 	bp->ulp_probe = bnxt_ulp_probe;
12777 
12778 	rc = bnxt_init_mac_addr(bp);
12779 	if (rc) {
12780 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
12781 		rc = -EADDRNOTAVAIL;
12782 		goto init_err_pci_clean;
12783 	}
12784 
12785 	if (BNXT_PF(bp)) {
12786 		/* Read the adapter's DSN to use as the eswitch switch_id */
12787 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
12788 	}
12789 
12790 	/* MTU range: 60 - FW defined max */
12791 	dev->min_mtu = ETH_ZLEN;
12792 	dev->max_mtu = bp->max_mtu;
12793 
12794 	rc = bnxt_probe_phy(bp, true);
12795 	if (rc)
12796 		goto init_err_pci_clean;
12797 
12798 	bnxt_set_rx_skb_mode(bp, false);
12799 	bnxt_set_tpa_flags(bp);
12800 	bnxt_set_ring_params(bp);
12801 	rc = bnxt_set_dflt_rings(bp, true);
12802 	if (rc) {
12803 		netdev_err(bp->dev, "Not enough rings available.\n");
12804 		rc = -ENOMEM;
12805 		goto init_err_pci_clean;
12806 	}
12807 
12808 	bnxt_fw_init_one_p3(bp);
12809 
12810 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12811 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
12812 
12813 	rc = bnxt_init_int_mode(bp);
12814 	if (rc)
12815 		goto init_err_pci_clean;
12816 
12817 	/* No TC has been set yet and rings may have been trimmed due to
12818 	 * limited MSIX, so we re-initialize the TX rings per TC.
12819 	 */
12820 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
12821 
12822 	if (BNXT_PF(bp)) {
12823 		if (!bnxt_pf_wq) {
12824 			bnxt_pf_wq =
12825 				create_singlethread_workqueue("bnxt_pf_wq");
12826 			if (!bnxt_pf_wq) {
12827 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
12828 				rc = -ENOMEM;
12829 				goto init_err_pci_clean;
12830 			}
12831 		}
12832 		rc = bnxt_init_tc(bp);
12833 		if (rc)
12834 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
12835 				   rc);
12836 	}
12837 
12838 	bnxt_dl_register(bp);
12839 
12840 	rc = register_netdev(dev);
12841 	if (rc)
12842 		goto init_err_cleanup;
12843 
12844 	if (BNXT_PF(bp))
12845 		devlink_port_type_eth_set(&bp->dl_port, bp->dev);
12846 	bnxt_dl_fw_reporters_create(bp);
12847 
12848 	netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
12849 		    board_info[ent->driver_data].name,
12850 		    (long)pci_resource_start(pdev, 0), dev->dev_addr);
12851 	pcie_print_link_status(pdev);
12852 
12853 	pci_save_state(pdev);
12854 	return 0;
12855 
12856 init_err_cleanup:
12857 	bnxt_dl_unregister(bp);
12858 	bnxt_shutdown_tc(bp);
12859 	bnxt_clear_int_mode(bp);
12860 
12861 init_err_pci_clean:
12862 	bnxt_hwrm_func_drv_unrgtr(bp);
12863 	bnxt_free_hwrm_short_cmd_req(bp);
12864 	bnxt_free_hwrm_resources(bp);
12865 	bnxt_ethtool_free(bp);
12866 	kfree(bp->fw_health);
12867 	bp->fw_health = NULL;
12868 	bnxt_cleanup_pci(bp);
12869 	bnxt_free_ctx_mem(bp);
12870 	kfree(bp->ctx);
12871 	bp->ctx = NULL;
12872 	kfree(bp->rss_indir_tbl);
12873 	bp->rss_indir_tbl = NULL;
12874 
12875 init_err_free:
12876 	free_netdev(dev);
12877 	return rc;
12878 }
12879 
bnxt_shutdown(struct pci_dev * pdev)12880 static void bnxt_shutdown(struct pci_dev *pdev)
12881 {
12882 	struct net_device *dev = pci_get_drvdata(pdev);
12883 	struct bnxt *bp;
12884 
12885 	if (!dev)
12886 		return;
12887 
12888 	rtnl_lock();
12889 	bp = netdev_priv(dev);
12890 	if (!bp)
12891 		goto shutdown_exit;
12892 
12893 	if (netif_running(dev))
12894 		dev_close(dev);
12895 
12896 	bnxt_ulp_shutdown(bp);
12897 	bnxt_clear_int_mode(bp);
12898 	pci_disable_device(pdev);
12899 
12900 	if (system_state == SYSTEM_POWER_OFF) {
12901 		pci_wake_from_d3(pdev, bp->wol);
12902 		pci_set_power_state(pdev, PCI_D3hot);
12903 	}
12904 
12905 shutdown_exit:
12906 	rtnl_unlock();
12907 }
12908 
12909 #ifdef CONFIG_PM_SLEEP
bnxt_suspend(struct device * device)12910 static int bnxt_suspend(struct device *device)
12911 {
12912 	struct net_device *dev = dev_get_drvdata(device);
12913 	struct bnxt *bp = netdev_priv(dev);
12914 	int rc = 0;
12915 
12916 	rtnl_lock();
12917 	bnxt_ulp_stop(bp);
12918 	if (netif_running(dev)) {
12919 		netif_device_detach(dev);
12920 		rc = bnxt_close(dev);
12921 	}
12922 	bnxt_hwrm_func_drv_unrgtr(bp);
12923 	pci_disable_device(bp->pdev);
12924 	bnxt_free_ctx_mem(bp);
12925 	kfree(bp->ctx);
12926 	bp->ctx = NULL;
12927 	rtnl_unlock();
12928 	return rc;
12929 }
12930 
bnxt_resume(struct device * device)12931 static int bnxt_resume(struct device *device)
12932 {
12933 	struct net_device *dev = dev_get_drvdata(device);
12934 	struct bnxt *bp = netdev_priv(dev);
12935 	int rc = 0;
12936 
12937 	rtnl_lock();
12938 	rc = pci_enable_device(bp->pdev);
12939 	if (rc) {
12940 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
12941 			   rc);
12942 		goto resume_exit;
12943 	}
12944 	pci_set_master(bp->pdev);
12945 	if (bnxt_hwrm_ver_get(bp)) {
12946 		rc = -ENODEV;
12947 		goto resume_exit;
12948 	}
12949 	rc = bnxt_hwrm_func_reset(bp);
12950 	if (rc) {
12951 		rc = -EBUSY;
12952 		goto resume_exit;
12953 	}
12954 
12955 	rc = bnxt_hwrm_func_qcaps(bp);
12956 	if (rc)
12957 		goto resume_exit;
12958 
12959 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
12960 		rc = -ENODEV;
12961 		goto resume_exit;
12962 	}
12963 
12964 	bnxt_get_wol_settings(bp);
12965 	if (netif_running(dev)) {
12966 		rc = bnxt_open(dev);
12967 		if (!rc)
12968 			netif_device_attach(dev);
12969 	}
12970 
12971 resume_exit:
12972 	bnxt_ulp_start(bp, rc);
12973 	if (!rc)
12974 		bnxt_reenable_sriov(bp);
12975 	rtnl_unlock();
12976 	return rc;
12977 }
12978 
12979 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
12980 #define BNXT_PM_OPS (&bnxt_pm_ops)
12981 
12982 #else
12983 
12984 #define BNXT_PM_OPS NULL
12985 
12986 #endif /* CONFIG_PM_SLEEP */
12987 
12988 /**
12989  * bnxt_io_error_detected - called when PCI error is detected
12990  * @pdev: Pointer to PCI device
12991  * @state: The current pci connection state
12992  *
12993  * This function is called after a PCI bus error affecting
12994  * this device has been detected.
12995  */
bnxt_io_error_detected(struct pci_dev * pdev,pci_channel_state_t state)12996 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
12997 					       pci_channel_state_t state)
12998 {
12999 	struct net_device *netdev = pci_get_drvdata(pdev);
13000 	struct bnxt *bp = netdev_priv(netdev);
13001 
13002 	netdev_info(netdev, "PCI I/O error detected\n");
13003 
13004 	rtnl_lock();
13005 	netif_device_detach(netdev);
13006 
13007 	bnxt_ulp_stop(bp);
13008 
13009 	if (state == pci_channel_io_perm_failure) {
13010 		rtnl_unlock();
13011 		return PCI_ERS_RESULT_DISCONNECT;
13012 	}
13013 
13014 	if (state == pci_channel_io_frozen)
13015 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
13016 
13017 	if (netif_running(netdev))
13018 		bnxt_close(netdev);
13019 
13020 	if (pci_is_enabled(pdev))
13021 		pci_disable_device(pdev);
13022 	bnxt_free_ctx_mem(bp);
13023 	kfree(bp->ctx);
13024 	bp->ctx = NULL;
13025 	rtnl_unlock();
13026 
13027 	/* Request a slot slot reset. */
13028 	return PCI_ERS_RESULT_NEED_RESET;
13029 }
13030 
13031 /**
13032  * bnxt_io_slot_reset - called after the pci bus has been reset.
13033  * @pdev: Pointer to PCI device
13034  *
13035  * Restart the card from scratch, as if from a cold-boot.
13036  * At this point, the card has exprienced a hard reset,
13037  * followed by fixups by BIOS, and has its config space
13038  * set up identically to what it was at cold boot.
13039  */
bnxt_io_slot_reset(struct pci_dev * pdev)13040 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
13041 {
13042 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
13043 	struct net_device *netdev = pci_get_drvdata(pdev);
13044 	struct bnxt *bp = netdev_priv(netdev);
13045 	int err = 0, off;
13046 
13047 	netdev_info(bp->dev, "PCI Slot Reset\n");
13048 
13049 	rtnl_lock();
13050 
13051 	if (pci_enable_device(pdev)) {
13052 		dev_err(&pdev->dev,
13053 			"Cannot re-enable PCI device after reset.\n");
13054 	} else {
13055 		pci_set_master(pdev);
13056 		/* Upon fatal error, our device internal logic that latches to
13057 		 * BAR value is getting reset and will restore only upon
13058 		 * rewritting the BARs.
13059 		 *
13060 		 * As pci_restore_state() does not re-write the BARs if the
13061 		 * value is same as saved value earlier, driver needs to
13062 		 * write the BARs to 0 to force restore, in case of fatal error.
13063 		 */
13064 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
13065 				       &bp->state)) {
13066 			for (off = PCI_BASE_ADDRESS_0;
13067 			     off <= PCI_BASE_ADDRESS_5; off += 4)
13068 				pci_write_config_dword(bp->pdev, off, 0);
13069 		}
13070 		pci_restore_state(pdev);
13071 		pci_save_state(pdev);
13072 
13073 		err = bnxt_hwrm_func_reset(bp);
13074 		if (!err)
13075 			result = PCI_ERS_RESULT_RECOVERED;
13076 	}
13077 
13078 	rtnl_unlock();
13079 
13080 	return result;
13081 }
13082 
13083 /**
13084  * bnxt_io_resume - called when traffic can start flowing again.
13085  * @pdev: Pointer to PCI device
13086  *
13087  * This callback is called when the error recovery driver tells
13088  * us that its OK to resume normal operation.
13089  */
bnxt_io_resume(struct pci_dev * pdev)13090 static void bnxt_io_resume(struct pci_dev *pdev)
13091 {
13092 	struct net_device *netdev = pci_get_drvdata(pdev);
13093 	struct bnxt *bp = netdev_priv(netdev);
13094 	int err;
13095 
13096 	netdev_info(bp->dev, "PCI Slot Resume\n");
13097 	rtnl_lock();
13098 
13099 	err = bnxt_hwrm_func_qcaps(bp);
13100 	if (!err && netif_running(netdev))
13101 		err = bnxt_open(netdev);
13102 
13103 	bnxt_ulp_start(bp, err);
13104 	if (!err) {
13105 		bnxt_reenable_sriov(bp);
13106 		netif_device_attach(netdev);
13107 	}
13108 
13109 	rtnl_unlock();
13110 }
13111 
13112 static const struct pci_error_handlers bnxt_err_handler = {
13113 	.error_detected	= bnxt_io_error_detected,
13114 	.slot_reset	= bnxt_io_slot_reset,
13115 	.resume		= bnxt_io_resume
13116 };
13117 
13118 static struct pci_driver bnxt_pci_driver = {
13119 	.name		= DRV_MODULE_NAME,
13120 	.id_table	= bnxt_pci_tbl,
13121 	.probe		= bnxt_init_one,
13122 	.remove		= bnxt_remove_one,
13123 	.shutdown	= bnxt_shutdown,
13124 	.driver.pm	= BNXT_PM_OPS,
13125 	.err_handler	= &bnxt_err_handler,
13126 #if defined(CONFIG_BNXT_SRIOV)
13127 	.sriov_configure = bnxt_sriov_configure,
13128 #endif
13129 };
13130 
bnxt_init(void)13131 static int __init bnxt_init(void)
13132 {
13133 	int err;
13134 
13135 	bnxt_debug_init();
13136 	err = pci_register_driver(&bnxt_pci_driver);
13137 	if (err) {
13138 		bnxt_debug_exit();
13139 		return err;
13140 	}
13141 
13142 	return 0;
13143 }
13144 
bnxt_exit(void)13145 static void __exit bnxt_exit(void)
13146 {
13147 	pci_unregister_driver(&bnxt_pci_driver);
13148 	if (bnxt_pf_wq)
13149 		destroy_workqueue(bnxt_pf_wq);
13150 	bnxt_debug_exit();
13151 }
13152 
13153 module_init(bnxt_init);
13154 module_exit(bnxt_exit);
13155