1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/aer.h>
52 #include <linux/bitmap.h>
53 #include <linux/cpu_rmap.h>
54 #include <linux/cpumask.h>
55 #include <net/pkt_cls.h>
56 #include <linux/hwmon.h>
57 #include <linux/hwmon-sysfs.h>
58 #include <net/page_pool.h>
59 #include <linux/align.h>
60 
61 #include "bnxt_hsi.h"
62 #include "bnxt.h"
63 #include "bnxt_hwrm.h"
64 #include "bnxt_ulp.h"
65 #include "bnxt_sriov.h"
66 #include "bnxt_ethtool.h"
67 #include "bnxt_dcb.h"
68 #include "bnxt_xdp.h"
69 #include "bnxt_ptp.h"
70 #include "bnxt_vfr.h"
71 #include "bnxt_tc.h"
72 #include "bnxt_devlink.h"
73 #include "bnxt_debugfs.h"
74 
75 #define BNXT_TX_TIMEOUT		(5 * HZ)
76 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
77 				 NETIF_MSG_TX_ERR)
78 
79 MODULE_LICENSE("GPL");
80 MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
81 
82 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
83 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
84 #define BNXT_RX_COPY_THRESH 256
85 
86 #define BNXT_TX_PUSH_THRESH 164
87 
88 /* indexed by enum board_idx */
89 static const struct {
90 	char *name;
91 } board_info[] = {
92 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
93 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
94 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
95 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
96 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
97 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
98 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
99 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
100 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
101 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
102 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
103 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
104 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
105 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
106 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
108 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
109 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
110 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
111 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
112 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
113 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
114 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
115 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
116 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
117 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
118 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
119 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
120 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
121 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
122 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
123 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
124 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
125 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
126 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
127 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
128 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
129 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
130 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
131 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
132 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
133 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
134 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
135 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
136 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
137 };
138 
139 static const struct pci_device_id bnxt_pci_tbl[] = {
140 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
141 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
142 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
143 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
144 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
145 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
146 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
147 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
148 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
149 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
150 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
151 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
152 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
153 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
155 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
161 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
163 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
164 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
165 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
166 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
167 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
168 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
169 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
170 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
171 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
172 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
173 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
174 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
175 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
176 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
177 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
178 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57508_NPAR },
179 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
180 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57502_NPAR },
181 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57508_NPAR },
182 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
183 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57502_NPAR },
184 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
185 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
186 #ifdef CONFIG_BNXT_SRIOV
187 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
188 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
189 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
190 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
191 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
192 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
193 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
194 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
195 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
196 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
197 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
198 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
199 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
200 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
201 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
202 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
204 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
205 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
206 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
207 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
208 #endif
209 	{ 0 }
210 };
211 
212 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
213 
214 static const u16 bnxt_vf_req_snif[] = {
215 	HWRM_FUNC_CFG,
216 	HWRM_FUNC_VF_CFG,
217 	HWRM_PORT_PHY_QCFG,
218 	HWRM_CFA_L2_FILTER_ALLOC,
219 };
220 
221 static const u16 bnxt_async_events_arr[] = {
222 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
223 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
224 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
225 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
226 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
227 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
228 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
229 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
230 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
231 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
232 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
233 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
234 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
235 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
236 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
237 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
238 };
239 
240 static struct workqueue_struct *bnxt_pf_wq;
241 
bnxt_vf_pciid(enum board_idx idx)242 static bool bnxt_vf_pciid(enum board_idx idx)
243 {
244 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
245 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
246 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
247 		idx == NETXTREME_E_P5_VF_HV);
248 }
249 
250 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
251 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
252 #define DB_CP_IRQ_DIS_FLAGS	(DB_KEY_CP | DB_IRQ_DIS)
253 
254 #define BNXT_CP_DB_IRQ_DIS(db)						\
255 		writel(DB_CP_IRQ_DIS_FLAGS, db)
256 
257 #define BNXT_DB_CQ(db, idx)						\
258 	writel(DB_CP_FLAGS | RING_CMP(idx), (db)->doorbell)
259 
260 #define BNXT_DB_NQ_P5(db, idx)						\
261 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | RING_CMP(idx),	\
262 		    (db)->doorbell)
263 
264 #define BNXT_DB_CQ_ARM(db, idx)						\
265 	writel(DB_CP_REARM_FLAGS | RING_CMP(idx), (db)->doorbell)
266 
267 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
268 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM | RING_CMP(idx),\
269 		    (db)->doorbell)
270 
bnxt_db_nq(struct bnxt * bp,struct bnxt_db_info * db,u32 idx)271 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
272 {
273 	if (bp->flags & BNXT_FLAG_CHIP_P5)
274 		BNXT_DB_NQ_P5(db, idx);
275 	else
276 		BNXT_DB_CQ(db, idx);
277 }
278 
bnxt_db_nq_arm(struct bnxt * bp,struct bnxt_db_info * db,u32 idx)279 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
280 {
281 	if (bp->flags & BNXT_FLAG_CHIP_P5)
282 		BNXT_DB_NQ_ARM_P5(db, idx);
283 	else
284 		BNXT_DB_CQ_ARM(db, idx);
285 }
286 
bnxt_db_cq(struct bnxt * bp,struct bnxt_db_info * db,u32 idx)287 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
288 {
289 	if (bp->flags & BNXT_FLAG_CHIP_P5)
290 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
291 			    RING_CMP(idx), db->doorbell);
292 	else
293 		BNXT_DB_CQ(db, idx);
294 }
295 
296 const u16 bnxt_lhint_arr[] = {
297 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
298 	TX_BD_FLAGS_LHINT_512_TO_1023,
299 	TX_BD_FLAGS_LHINT_1024_TO_2047,
300 	TX_BD_FLAGS_LHINT_1024_TO_2047,
301 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
302 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
303 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
304 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
305 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
306 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
307 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
308 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
309 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
310 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
311 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
312 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
313 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
314 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
315 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
316 };
317 
bnxt_xmit_get_cfa_action(struct sk_buff * skb)318 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
319 {
320 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
321 
322 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
323 		return 0;
324 
325 	return md_dst->u.port_info.port_id;
326 }
327 
bnxt_txr_db_kick(struct bnxt * bp,struct bnxt_tx_ring_info * txr,u16 prod)328 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
329 			     u16 prod)
330 {
331 	bnxt_db_write(bp, &txr->tx_db, prod);
332 	txr->kick_pending = 0;
333 }
334 
bnxt_txr_netif_try_stop_queue(struct bnxt * bp,struct bnxt_tx_ring_info * txr,struct netdev_queue * txq)335 static bool bnxt_txr_netif_try_stop_queue(struct bnxt *bp,
336 					  struct bnxt_tx_ring_info *txr,
337 					  struct netdev_queue *txq)
338 {
339 	netif_tx_stop_queue(txq);
340 
341 	/* netif_tx_stop_queue() must be done before checking
342 	 * tx index in bnxt_tx_avail() below, because in
343 	 * bnxt_tx_int(), we update tx index before checking for
344 	 * netif_tx_queue_stopped().
345 	 */
346 	smp_mb();
347 	if (bnxt_tx_avail(bp, txr) >= bp->tx_wake_thresh) {
348 		netif_tx_wake_queue(txq);
349 		return false;
350 	}
351 
352 	return true;
353 }
354 
bnxt_start_xmit(struct sk_buff * skb,struct net_device * dev)355 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
356 {
357 	struct bnxt *bp = netdev_priv(dev);
358 	struct tx_bd *txbd;
359 	struct tx_bd_ext *txbd1;
360 	struct netdev_queue *txq;
361 	int i;
362 	dma_addr_t mapping;
363 	unsigned int length, pad = 0;
364 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
365 	u16 prod, last_frag;
366 	struct pci_dev *pdev = bp->pdev;
367 	struct bnxt_tx_ring_info *txr;
368 	struct bnxt_sw_tx_bd *tx_buf;
369 	__le32 lflags = 0;
370 
371 	i = skb_get_queue_mapping(skb);
372 	if (unlikely(i >= bp->tx_nr_rings)) {
373 		dev_kfree_skb_any(skb);
374 		dev_core_stats_tx_dropped_inc(dev);
375 		return NETDEV_TX_OK;
376 	}
377 
378 	txq = netdev_get_tx_queue(dev, i);
379 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
380 	prod = txr->tx_prod;
381 
382 	free_size = bnxt_tx_avail(bp, txr);
383 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
384 		/* We must have raced with NAPI cleanup */
385 		if (net_ratelimit() && txr->kick_pending)
386 			netif_warn(bp, tx_err, dev,
387 				   "bnxt: ring busy w/ flush pending!\n");
388 		if (bnxt_txr_netif_try_stop_queue(bp, txr, txq))
389 			return NETDEV_TX_BUSY;
390 	}
391 
392 	length = skb->len;
393 	len = skb_headlen(skb);
394 	last_frag = skb_shinfo(skb)->nr_frags;
395 
396 	txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
397 
398 	txbd->tx_bd_opaque = prod;
399 
400 	tx_buf = &txr->tx_buf_ring[prod];
401 	tx_buf->skb = skb;
402 	tx_buf->nr_frags = last_frag;
403 
404 	vlan_tag_flags = 0;
405 	cfa_action = bnxt_xmit_get_cfa_action(skb);
406 	if (skb_vlan_tag_present(skb)) {
407 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
408 				 skb_vlan_tag_get(skb);
409 		/* Currently supports 8021Q, 8021AD vlan offloads
410 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
411 		 */
412 		if (skb->vlan_proto == htons(ETH_P_8021Q))
413 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
414 	}
415 
416 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
417 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
418 
419 		if (ptp && ptp->tx_tstamp_en && !skb_is_gso(skb) &&
420 		    atomic_dec_if_positive(&ptp->tx_avail) >= 0) {
421 			if (!bnxt_ptp_parse(skb, &ptp->tx_seqid,
422 					    &ptp->tx_hdr_off)) {
423 				if (vlan_tag_flags)
424 					ptp->tx_hdr_off += VLAN_HLEN;
425 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
426 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
427 			} else {
428 				atomic_inc(&bp->ptp_cfg->tx_avail);
429 			}
430 		}
431 	}
432 
433 	if (unlikely(skb->no_fcs))
434 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
435 
436 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
437 	    !lflags) {
438 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
439 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
440 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
441 		void __iomem *db = txr->tx_db.doorbell;
442 		void *pdata = tx_push_buf->data;
443 		u64 *end;
444 		int j, push_len;
445 
446 		/* Set COAL_NOW to be ready quickly for the next push */
447 		tx_push->tx_bd_len_flags_type =
448 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
449 					TX_BD_TYPE_LONG_TX_BD |
450 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
451 					TX_BD_FLAGS_COAL_NOW |
452 					TX_BD_FLAGS_PACKET_END |
453 					(2 << TX_BD_FLAGS_BD_CNT_SHIFT));
454 
455 		if (skb->ip_summed == CHECKSUM_PARTIAL)
456 			tx_push1->tx_bd_hsize_lflags =
457 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
458 		else
459 			tx_push1->tx_bd_hsize_lflags = 0;
460 
461 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
462 		tx_push1->tx_bd_cfa_action =
463 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
464 
465 		end = pdata + length;
466 		end = PTR_ALIGN(end, 8) - 1;
467 		*end = 0;
468 
469 		skb_copy_from_linear_data(skb, pdata, len);
470 		pdata += len;
471 		for (j = 0; j < last_frag; j++) {
472 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
473 			void *fptr;
474 
475 			fptr = skb_frag_address_safe(frag);
476 			if (!fptr)
477 				goto normal_tx;
478 
479 			memcpy(pdata, fptr, skb_frag_size(frag));
480 			pdata += skb_frag_size(frag);
481 		}
482 
483 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
484 		txbd->tx_bd_haddr = txr->data_mapping;
485 		prod = NEXT_TX(prod);
486 		txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
487 		memcpy(txbd, tx_push1, sizeof(*txbd));
488 		prod = NEXT_TX(prod);
489 		tx_push->doorbell =
490 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
491 		txr->tx_prod = prod;
492 
493 		tx_buf->is_push = 1;
494 		netdev_tx_sent_queue(txq, skb->len);
495 		wmb();	/* Sync is_push and byte queue before pushing data */
496 
497 		push_len = (length + sizeof(*tx_push) + 7) / 8;
498 		if (push_len > 16) {
499 			__iowrite64_copy(db, tx_push_buf, 16);
500 			__iowrite32_copy(db + 4, tx_push_buf + 1,
501 					 (push_len - 16) << 1);
502 		} else {
503 			__iowrite64_copy(db, tx_push_buf, push_len);
504 		}
505 
506 		goto tx_done;
507 	}
508 
509 normal_tx:
510 	if (length < BNXT_MIN_PKT_SIZE) {
511 		pad = BNXT_MIN_PKT_SIZE - length;
512 		if (skb_pad(skb, pad))
513 			/* SKB already freed. */
514 			goto tx_kick_pending;
515 		length = BNXT_MIN_PKT_SIZE;
516 	}
517 
518 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
519 
520 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
521 		goto tx_free;
522 
523 	dma_unmap_addr_set(tx_buf, mapping, mapping);
524 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
525 		((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
526 
527 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
528 
529 	prod = NEXT_TX(prod);
530 	txbd1 = (struct tx_bd_ext *)
531 		&txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
532 
533 	txbd1->tx_bd_hsize_lflags = lflags;
534 	if (skb_is_gso(skb)) {
535 		u32 hdr_len;
536 
537 		if (skb->encapsulation)
538 			hdr_len = skb_inner_tcp_all_headers(skb);
539 		else
540 			hdr_len = skb_tcp_all_headers(skb);
541 
542 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
543 					TX_BD_FLAGS_T_IPID |
544 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
545 		length = skb_shinfo(skb)->gso_size;
546 		txbd1->tx_bd_mss = cpu_to_le32(length);
547 		length += hdr_len;
548 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
549 		txbd1->tx_bd_hsize_lflags |=
550 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
551 		txbd1->tx_bd_mss = 0;
552 	}
553 
554 	length >>= 9;
555 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
556 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
557 				     skb->len);
558 		i = 0;
559 		goto tx_dma_error;
560 	}
561 	flags |= bnxt_lhint_arr[length];
562 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
563 
564 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
565 	txbd1->tx_bd_cfa_action =
566 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
567 	for (i = 0; i < last_frag; i++) {
568 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
569 
570 		prod = NEXT_TX(prod);
571 		txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
572 
573 		len = skb_frag_size(frag);
574 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
575 					   DMA_TO_DEVICE);
576 
577 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
578 			goto tx_dma_error;
579 
580 		tx_buf = &txr->tx_buf_ring[prod];
581 		dma_unmap_addr_set(tx_buf, mapping, mapping);
582 
583 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
584 
585 		flags = len << TX_BD_LEN_SHIFT;
586 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
587 	}
588 
589 	flags &= ~TX_BD_LEN;
590 	txbd->tx_bd_len_flags_type =
591 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
592 			    TX_BD_FLAGS_PACKET_END);
593 
594 	netdev_tx_sent_queue(txq, skb->len);
595 
596 	skb_tx_timestamp(skb);
597 
598 	/* Sync BD data before updating doorbell */
599 	wmb();
600 
601 	prod = NEXT_TX(prod);
602 	txr->tx_prod = prod;
603 
604 	if (!netdev_xmit_more() || netif_xmit_stopped(txq))
605 		bnxt_txr_db_kick(bp, txr, prod);
606 	else
607 		txr->kick_pending = 1;
608 
609 tx_done:
610 
611 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
612 		if (netdev_xmit_more() && !tx_buf->is_push)
613 			bnxt_txr_db_kick(bp, txr, prod);
614 
615 		bnxt_txr_netif_try_stop_queue(bp, txr, txq);
616 	}
617 	return NETDEV_TX_OK;
618 
619 tx_dma_error:
620 	if (BNXT_TX_PTP_IS_SET(lflags))
621 		atomic_inc(&bp->ptp_cfg->tx_avail);
622 
623 	last_frag = i;
624 
625 	/* start back at beginning and unmap skb */
626 	prod = txr->tx_prod;
627 	tx_buf = &txr->tx_buf_ring[prod];
628 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
629 			 skb_headlen(skb), DMA_TO_DEVICE);
630 	prod = NEXT_TX(prod);
631 
632 	/* unmap remaining mapped pages */
633 	for (i = 0; i < last_frag; i++) {
634 		prod = NEXT_TX(prod);
635 		tx_buf = &txr->tx_buf_ring[prod];
636 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
637 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
638 			       DMA_TO_DEVICE);
639 	}
640 
641 tx_free:
642 	dev_kfree_skb_any(skb);
643 tx_kick_pending:
644 	if (txr->kick_pending)
645 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
646 	txr->tx_buf_ring[txr->tx_prod].skb = NULL;
647 	dev_core_stats_tx_dropped_inc(dev);
648 	return NETDEV_TX_OK;
649 }
650 
bnxt_tx_int(struct bnxt * bp,struct bnxt_napi * bnapi,int nr_pkts)651 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
652 {
653 	struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
654 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
655 	u16 cons = txr->tx_cons;
656 	struct pci_dev *pdev = bp->pdev;
657 	int i;
658 	unsigned int tx_bytes = 0;
659 
660 	for (i = 0; i < nr_pkts; i++) {
661 		struct bnxt_sw_tx_bd *tx_buf;
662 		struct sk_buff *skb;
663 		int j, last;
664 
665 		tx_buf = &txr->tx_buf_ring[cons];
666 		cons = NEXT_TX(cons);
667 		skb = tx_buf->skb;
668 		tx_buf->skb = NULL;
669 
670 		tx_bytes += skb->len;
671 
672 		if (tx_buf->is_push) {
673 			tx_buf->is_push = 0;
674 			goto next_tx_int;
675 		}
676 
677 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
678 				 skb_headlen(skb), DMA_TO_DEVICE);
679 		last = tx_buf->nr_frags;
680 
681 		for (j = 0; j < last; j++) {
682 			cons = NEXT_TX(cons);
683 			tx_buf = &txr->tx_buf_ring[cons];
684 			dma_unmap_page(
685 				&pdev->dev,
686 				dma_unmap_addr(tx_buf, mapping),
687 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
688 				DMA_TO_DEVICE);
689 		}
690 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
691 			if (bp->flags & BNXT_FLAG_CHIP_P5) {
692 				/* PTP worker takes ownership of the skb */
693 				if (!bnxt_get_tx_ts_p5(bp, skb))
694 					skb = NULL;
695 				else
696 					atomic_inc(&bp->ptp_cfg->tx_avail);
697 			}
698 		}
699 
700 next_tx_int:
701 		cons = NEXT_TX(cons);
702 
703 		dev_kfree_skb_any(skb);
704 	}
705 
706 	netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
707 	txr->tx_cons = cons;
708 
709 	/* Need to make the tx_cons update visible to bnxt_start_xmit()
710 	 * before checking for netif_tx_queue_stopped().  Without the
711 	 * memory barrier, there is a small possibility that bnxt_start_xmit()
712 	 * will miss it and cause the queue to be stopped forever.
713 	 */
714 	smp_mb();
715 
716 	if (unlikely(netif_tx_queue_stopped(txq)) &&
717 	    bnxt_tx_avail(bp, txr) >= bp->tx_wake_thresh &&
718 	    READ_ONCE(txr->dev_state) != BNXT_DEV_STATE_CLOSING)
719 		netif_tx_wake_queue(txq);
720 }
721 
__bnxt_alloc_rx_page(struct bnxt * bp,dma_addr_t * mapping,struct bnxt_rx_ring_info * rxr,gfp_t gfp)722 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
723 					 struct bnxt_rx_ring_info *rxr,
724 					 gfp_t gfp)
725 {
726 	struct device *dev = &bp->pdev->dev;
727 	struct page *page;
728 
729 	page = page_pool_dev_alloc_pages(rxr->page_pool);
730 	if (!page)
731 		return NULL;
732 
733 	*mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
734 				      DMA_ATTR_WEAK_ORDERING);
735 	if (dma_mapping_error(dev, *mapping)) {
736 		page_pool_recycle_direct(rxr->page_pool, page);
737 		return NULL;
738 	}
739 	return page;
740 }
741 
__bnxt_alloc_rx_frag(struct bnxt * bp,dma_addr_t * mapping,gfp_t gfp)742 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
743 				       gfp_t gfp)
744 {
745 	u8 *data;
746 	struct pci_dev *pdev = bp->pdev;
747 
748 	if (gfp == GFP_ATOMIC)
749 		data = napi_alloc_frag(bp->rx_buf_size);
750 	else
751 		data = netdev_alloc_frag(bp->rx_buf_size);
752 	if (!data)
753 		return NULL;
754 
755 	*mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
756 					bp->rx_buf_use_size, bp->rx_dir,
757 					DMA_ATTR_WEAK_ORDERING);
758 
759 	if (dma_mapping_error(&pdev->dev, *mapping)) {
760 		skb_free_frag(data);
761 		data = NULL;
762 	}
763 	return data;
764 }
765 
bnxt_alloc_rx_data(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,u16 prod,gfp_t gfp)766 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
767 		       u16 prod, gfp_t gfp)
768 {
769 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
770 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
771 	dma_addr_t mapping;
772 
773 	if (BNXT_RX_PAGE_MODE(bp)) {
774 		struct page *page =
775 			__bnxt_alloc_rx_page(bp, &mapping, rxr, gfp);
776 
777 		if (!page)
778 			return -ENOMEM;
779 
780 		mapping += bp->rx_dma_offset;
781 		rx_buf->data = page;
782 		rx_buf->data_ptr = page_address(page) + bp->rx_offset;
783 	} else {
784 		u8 *data = __bnxt_alloc_rx_frag(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 (BNXT_RX_PAGE_MODE(bp)) {
842 		page = __bnxt_alloc_rx_page(bp, &mapping, rxr, gfp);
843 
844 		if (!page)
845 			return -ENOMEM;
846 
847 	} else {
848 		if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
849 			page = rxr->rx_page;
850 			if (!page) {
851 				page = alloc_page(gfp);
852 				if (!page)
853 					return -ENOMEM;
854 				rxr->rx_page = page;
855 				rxr->rx_page_offset = 0;
856 			}
857 			offset = rxr->rx_page_offset;
858 			rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
859 			if (rxr->rx_page_offset == PAGE_SIZE)
860 				rxr->rx_page = NULL;
861 			else
862 				get_page(page);
863 		} else {
864 			page = alloc_page(gfp);
865 			if (!page)
866 				return -ENOMEM;
867 		}
868 
869 		mapping = dma_map_page_attrs(&pdev->dev, page, offset,
870 					     BNXT_RX_PAGE_SIZE, DMA_FROM_DEVICE,
871 					     DMA_ATTR_WEAK_ORDERING);
872 		if (dma_mapping_error(&pdev->dev, mapping)) {
873 			__free_page(page);
874 			return -EIO;
875 		}
876 	}
877 
878 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
879 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
880 
881 	__set_bit(sw_prod, rxr->rx_agg_bmap);
882 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
883 	rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
884 
885 	rx_agg_buf->page = page;
886 	rx_agg_buf->offset = offset;
887 	rx_agg_buf->mapping = mapping;
888 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
889 	rxbd->rx_bd_opaque = sw_prod;
890 	return 0;
891 }
892 
bnxt_get_agg(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,u16 cp_cons,u16 curr)893 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
894 				       struct bnxt_cp_ring_info *cpr,
895 				       u16 cp_cons, u16 curr)
896 {
897 	struct rx_agg_cmp *agg;
898 
899 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
900 	agg = (struct rx_agg_cmp *)
901 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
902 	return agg;
903 }
904 
bnxt_get_tpa_agg_p5(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,u16 agg_id,u16 curr)905 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
906 					      struct bnxt_rx_ring_info *rxr,
907 					      u16 agg_id, u16 curr)
908 {
909 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
910 
911 	return &tpa_info->agg_arr[curr];
912 }
913 
bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info * cpr,u16 idx,u16 start,u32 agg_bufs,bool tpa)914 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
915 				   u16 start, u32 agg_bufs, bool tpa)
916 {
917 	struct bnxt_napi *bnapi = cpr->bnapi;
918 	struct bnxt *bp = bnapi->bp;
919 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
920 	u16 prod = rxr->rx_agg_prod;
921 	u16 sw_prod = rxr->rx_sw_agg_prod;
922 	bool p5_tpa = false;
923 	u32 i;
924 
925 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
926 		p5_tpa = true;
927 
928 	for (i = 0; i < agg_bufs; i++) {
929 		u16 cons;
930 		struct rx_agg_cmp *agg;
931 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
932 		struct rx_bd *prod_bd;
933 		struct page *page;
934 
935 		if (p5_tpa)
936 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
937 		else
938 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
939 		cons = agg->rx_agg_cmp_opaque;
940 		__clear_bit(cons, rxr->rx_agg_bmap);
941 
942 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
943 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
944 
945 		__set_bit(sw_prod, rxr->rx_agg_bmap);
946 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
947 		cons_rx_buf = &rxr->rx_agg_ring[cons];
948 
949 		/* It is possible for sw_prod to be equal to cons, so
950 		 * set cons_rx_buf->page to NULL first.
951 		 */
952 		page = cons_rx_buf->page;
953 		cons_rx_buf->page = NULL;
954 		prod_rx_buf->page = page;
955 		prod_rx_buf->offset = cons_rx_buf->offset;
956 
957 		prod_rx_buf->mapping = cons_rx_buf->mapping;
958 
959 		prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
960 
961 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
962 		prod_bd->rx_bd_opaque = sw_prod;
963 
964 		prod = NEXT_RX_AGG(prod);
965 		sw_prod = NEXT_RX_AGG(sw_prod);
966 	}
967 	rxr->rx_agg_prod = prod;
968 	rxr->rx_sw_agg_prod = sw_prod;
969 }
970 
bnxt_rx_multi_page_skb(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,u16 cons,void * data,u8 * data_ptr,dma_addr_t dma_addr,unsigned int offset_and_len)971 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
972 					      struct bnxt_rx_ring_info *rxr,
973 					      u16 cons, void *data, u8 *data_ptr,
974 					      dma_addr_t dma_addr,
975 					      unsigned int offset_and_len)
976 {
977 	unsigned int len = offset_and_len & 0xffff;
978 	struct page *page = data;
979 	u16 prod = rxr->rx_prod;
980 	struct sk_buff *skb;
981 	int err;
982 
983 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
984 	if (unlikely(err)) {
985 		bnxt_reuse_rx_data(rxr, cons, data);
986 		return NULL;
987 	}
988 	dma_addr -= bp->rx_dma_offset;
989 	dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
990 			     DMA_ATTR_WEAK_ORDERING);
991 	skb = build_skb(page_address(page), PAGE_SIZE);
992 	if (!skb) {
993 		page_pool_recycle_direct(rxr->page_pool, page);
994 		return NULL;
995 	}
996 	skb_mark_for_recycle(skb);
997 	skb_reserve(skb, bp->rx_dma_offset);
998 	__skb_put(skb, len);
999 
1000 	return skb;
1001 }
1002 
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)1003 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1004 					struct bnxt_rx_ring_info *rxr,
1005 					u16 cons, void *data, u8 *data_ptr,
1006 					dma_addr_t dma_addr,
1007 					unsigned int offset_and_len)
1008 {
1009 	unsigned int payload = offset_and_len >> 16;
1010 	unsigned int len = offset_and_len & 0xffff;
1011 	skb_frag_t *frag;
1012 	struct page *page = data;
1013 	u16 prod = rxr->rx_prod;
1014 	struct sk_buff *skb;
1015 	int off, err;
1016 
1017 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1018 	if (unlikely(err)) {
1019 		bnxt_reuse_rx_data(rxr, cons, data);
1020 		return NULL;
1021 	}
1022 	dma_addr -= bp->rx_dma_offset;
1023 	dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
1024 			     DMA_ATTR_WEAK_ORDERING);
1025 
1026 	if (unlikely(!payload))
1027 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1028 
1029 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1030 	if (!skb) {
1031 		page_pool_recycle_direct(rxr->page_pool, page);
1032 		return NULL;
1033 	}
1034 
1035 	skb_mark_for_recycle(skb);
1036 	off = (void *)data_ptr - page_address(page);
1037 	skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
1038 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1039 	       payload + NET_IP_ALIGN);
1040 
1041 	frag = &skb_shinfo(skb)->frags[0];
1042 	skb_frag_size_sub(frag, payload);
1043 	skb_frag_off_add(frag, payload);
1044 	skb->data_len -= payload;
1045 	skb->tail += payload;
1046 
1047 	return skb;
1048 }
1049 
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)1050 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1051 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1052 				   void *data, u8 *data_ptr,
1053 				   dma_addr_t dma_addr,
1054 				   unsigned int offset_and_len)
1055 {
1056 	u16 prod = rxr->rx_prod;
1057 	struct sk_buff *skb;
1058 	int err;
1059 
1060 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1061 	if (unlikely(err)) {
1062 		bnxt_reuse_rx_data(rxr, cons, data);
1063 		return NULL;
1064 	}
1065 
1066 	skb = build_skb(data, bp->rx_buf_size);
1067 	dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1068 			       bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
1069 	if (!skb) {
1070 		skb_free_frag(data);
1071 		return NULL;
1072 	}
1073 
1074 	skb_reserve(skb, bp->rx_offset);
1075 	skb_put(skb, offset_and_len & 0xffff);
1076 	return skb;
1077 }
1078 
__bnxt_rx_agg_pages(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,struct skb_shared_info * shinfo,u16 idx,u32 agg_bufs,bool tpa,struct xdp_buff * xdp)1079 static u32 __bnxt_rx_agg_pages(struct bnxt *bp,
1080 			       struct bnxt_cp_ring_info *cpr,
1081 			       struct skb_shared_info *shinfo,
1082 			       u16 idx, u32 agg_bufs, bool tpa,
1083 			       struct xdp_buff *xdp)
1084 {
1085 	struct bnxt_napi *bnapi = cpr->bnapi;
1086 	struct pci_dev *pdev = bp->pdev;
1087 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1088 	u16 prod = rxr->rx_agg_prod;
1089 	u32 i, total_frag_len = 0;
1090 	bool p5_tpa = false;
1091 
1092 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
1093 		p5_tpa = true;
1094 
1095 	for (i = 0; i < agg_bufs; i++) {
1096 		skb_frag_t *frag = &shinfo->frags[i];
1097 		u16 cons, frag_len;
1098 		struct rx_agg_cmp *agg;
1099 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1100 		struct page *page;
1101 		dma_addr_t mapping;
1102 
1103 		if (p5_tpa)
1104 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1105 		else
1106 			agg = bnxt_get_agg(bp, cpr, idx, i);
1107 		cons = agg->rx_agg_cmp_opaque;
1108 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1109 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1110 
1111 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1112 		skb_frag_off_set(frag, cons_rx_buf->offset);
1113 		skb_frag_size_set(frag, frag_len);
1114 		__skb_frag_set_page(frag, cons_rx_buf->page);
1115 		shinfo->nr_frags = i + 1;
1116 		__clear_bit(cons, rxr->rx_agg_bmap);
1117 
1118 		/* It is possible for bnxt_alloc_rx_page() to allocate
1119 		 * a sw_prod index that equals the cons index, so we
1120 		 * need to clear the cons entry now.
1121 		 */
1122 		mapping = cons_rx_buf->mapping;
1123 		page = cons_rx_buf->page;
1124 		cons_rx_buf->page = NULL;
1125 
1126 		if (xdp && page_is_pfmemalloc(page))
1127 			xdp_buff_set_frag_pfmemalloc(xdp);
1128 
1129 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1130 			unsigned int nr_frags;
1131 
1132 			nr_frags = --shinfo->nr_frags;
1133 			__skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
1134 			cons_rx_buf->page = page;
1135 
1136 			/* Update prod since possibly some pages have been
1137 			 * allocated already.
1138 			 */
1139 			rxr->rx_agg_prod = prod;
1140 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1141 			return 0;
1142 		}
1143 
1144 		dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1145 				     bp->rx_dir,
1146 				     DMA_ATTR_WEAK_ORDERING);
1147 
1148 		total_frag_len += frag_len;
1149 		prod = NEXT_RX_AGG(prod);
1150 	}
1151 	rxr->rx_agg_prod = prod;
1152 	return total_frag_len;
1153 }
1154 
bnxt_rx_agg_pages_skb(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,struct sk_buff * skb,u16 idx,u32 agg_bufs,bool tpa)1155 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp,
1156 					     struct bnxt_cp_ring_info *cpr,
1157 					     struct sk_buff *skb, u16 idx,
1158 					     u32 agg_bufs, bool tpa)
1159 {
1160 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1161 	u32 total_frag_len = 0;
1162 
1163 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx,
1164 					     agg_bufs, tpa, NULL);
1165 	if (!total_frag_len) {
1166 		dev_kfree_skb(skb);
1167 		return NULL;
1168 	}
1169 
1170 	skb->data_len += total_frag_len;
1171 	skb->len += total_frag_len;
1172 	skb->truesize += PAGE_SIZE * agg_bufs;
1173 	return skb;
1174 }
1175 
bnxt_rx_agg_pages_xdp(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,struct xdp_buff * xdp,u16 idx,u32 agg_bufs,bool tpa)1176 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp,
1177 				 struct bnxt_cp_ring_info *cpr,
1178 				 struct xdp_buff *xdp, u16 idx,
1179 				 u32 agg_bufs, bool tpa)
1180 {
1181 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1182 	u32 total_frag_len = 0;
1183 
1184 	if (!xdp_buff_has_frags(xdp))
1185 		shinfo->nr_frags = 0;
1186 
1187 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo,
1188 					     idx, agg_bufs, tpa, xdp);
1189 	if (total_frag_len) {
1190 		xdp_buff_set_frags_flag(xdp);
1191 		shinfo->nr_frags = agg_bufs;
1192 		shinfo->xdp_frags_size = total_frag_len;
1193 	}
1194 	return total_frag_len;
1195 }
1196 
bnxt_agg_bufs_valid(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,u8 agg_bufs,u32 * raw_cons)1197 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1198 			       u8 agg_bufs, u32 *raw_cons)
1199 {
1200 	u16 last;
1201 	struct rx_agg_cmp *agg;
1202 
1203 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1204 	last = RING_CMP(*raw_cons);
1205 	agg = (struct rx_agg_cmp *)
1206 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1207 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1208 }
1209 
bnxt_copy_skb(struct bnxt_napi * bnapi,u8 * data,unsigned int len,dma_addr_t mapping)1210 static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1211 					    unsigned int len,
1212 					    dma_addr_t mapping)
1213 {
1214 	struct bnxt *bp = bnapi->bp;
1215 	struct pci_dev *pdev = bp->pdev;
1216 	struct sk_buff *skb;
1217 
1218 	skb = napi_alloc_skb(&bnapi->napi, len);
1219 	if (!skb)
1220 		return NULL;
1221 
1222 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1223 				bp->rx_dir);
1224 
1225 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1226 	       len + NET_IP_ALIGN);
1227 
1228 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1229 				   bp->rx_dir);
1230 
1231 	skb_put(skb, len);
1232 	return skb;
1233 }
1234 
bnxt_discard_rx(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,u32 * raw_cons,void * cmp)1235 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1236 			   u32 *raw_cons, void *cmp)
1237 {
1238 	struct rx_cmp *rxcmp = cmp;
1239 	u32 tmp_raw_cons = *raw_cons;
1240 	u8 cmp_type, agg_bufs = 0;
1241 
1242 	cmp_type = RX_CMP_TYPE(rxcmp);
1243 
1244 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1245 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1246 			    RX_CMP_AGG_BUFS) >>
1247 			   RX_CMP_AGG_BUFS_SHIFT;
1248 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1249 		struct rx_tpa_end_cmp *tpa_end = cmp;
1250 
1251 		if (bp->flags & BNXT_FLAG_CHIP_P5)
1252 			return 0;
1253 
1254 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1255 	}
1256 
1257 	if (agg_bufs) {
1258 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1259 			return -EBUSY;
1260 	}
1261 	*raw_cons = tmp_raw_cons;
1262 	return 0;
1263 }
1264 
bnxt_queue_fw_reset_work(struct bnxt * bp,unsigned long delay)1265 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
1266 {
1267 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
1268 		return;
1269 
1270 	if (BNXT_PF(bp))
1271 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
1272 	else
1273 		schedule_delayed_work(&bp->fw_reset_task, delay);
1274 }
1275 
bnxt_queue_sp_work(struct bnxt * bp)1276 static void bnxt_queue_sp_work(struct bnxt *bp)
1277 {
1278 	if (BNXT_PF(bp))
1279 		queue_work(bnxt_pf_wq, &bp->sp_task);
1280 	else
1281 		schedule_work(&bp->sp_task);
1282 }
1283 
bnxt_sched_reset(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)1284 static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1285 {
1286 	if (!rxr->bnapi->in_reset) {
1287 		rxr->bnapi->in_reset = true;
1288 		if (bp->flags & BNXT_FLAG_CHIP_P5)
1289 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1290 		else
1291 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
1292 		bnxt_queue_sp_work(bp);
1293 	}
1294 	rxr->rx_next_cons = 0xffff;
1295 }
1296 
bnxt_alloc_agg_idx(struct bnxt_rx_ring_info * rxr,u16 agg_id)1297 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1298 {
1299 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1300 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1301 
1302 	if (test_bit(idx, map->agg_idx_bmap))
1303 		idx = find_first_zero_bit(map->agg_idx_bmap,
1304 					  BNXT_AGG_IDX_BMAP_SIZE);
1305 	__set_bit(idx, map->agg_idx_bmap);
1306 	map->agg_id_tbl[agg_id] = idx;
1307 	return idx;
1308 }
1309 
bnxt_free_agg_idx(struct bnxt_rx_ring_info * rxr,u16 idx)1310 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1311 {
1312 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1313 
1314 	__clear_bit(idx, map->agg_idx_bmap);
1315 }
1316 
bnxt_lookup_agg_idx(struct bnxt_rx_ring_info * rxr,u16 agg_id)1317 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1318 {
1319 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1320 
1321 	return map->agg_id_tbl[agg_id];
1322 }
1323 
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)1324 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1325 			   struct rx_tpa_start_cmp *tpa_start,
1326 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1327 {
1328 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1329 	struct bnxt_tpa_info *tpa_info;
1330 	u16 cons, prod, agg_id;
1331 	struct rx_bd *prod_bd;
1332 	dma_addr_t mapping;
1333 
1334 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
1335 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1336 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1337 	} else {
1338 		agg_id = TPA_START_AGG_ID(tpa_start);
1339 	}
1340 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1341 	prod = rxr->rx_prod;
1342 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1343 	prod_rx_buf = &rxr->rx_buf_ring[prod];
1344 	tpa_info = &rxr->rx_tpa[agg_id];
1345 
1346 	if (unlikely(cons != rxr->rx_next_cons ||
1347 		     TPA_START_ERROR(tpa_start))) {
1348 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1349 			    cons, rxr->rx_next_cons,
1350 			    TPA_START_ERROR_CODE(tpa_start1));
1351 		bnxt_sched_reset(bp, rxr);
1352 		return;
1353 	}
1354 	/* Store cfa_code in tpa_info to use in tpa_end
1355 	 * completion processing.
1356 	 */
1357 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1358 	prod_rx_buf->data = tpa_info->data;
1359 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1360 
1361 	mapping = tpa_info->mapping;
1362 	prod_rx_buf->mapping = mapping;
1363 
1364 	prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1365 
1366 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1367 
1368 	tpa_info->data = cons_rx_buf->data;
1369 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1370 	cons_rx_buf->data = NULL;
1371 	tpa_info->mapping = cons_rx_buf->mapping;
1372 
1373 	tpa_info->len =
1374 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1375 				RX_TPA_START_CMP_LEN_SHIFT;
1376 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1377 		u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1378 
1379 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1380 		tpa_info->gso_type = SKB_GSO_TCPV4;
1381 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1382 		if (hash_type == 3 || TPA_START_IS_IPV6(tpa_start1))
1383 			tpa_info->gso_type = SKB_GSO_TCPV6;
1384 		tpa_info->rss_hash =
1385 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1386 	} else {
1387 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1388 		tpa_info->gso_type = 0;
1389 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1390 	}
1391 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1392 	tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1393 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1394 	tpa_info->agg_count = 0;
1395 
1396 	rxr->rx_prod = NEXT_RX(prod);
1397 	cons = NEXT_RX(cons);
1398 	rxr->rx_next_cons = NEXT_RX(cons);
1399 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1400 
1401 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1402 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1403 	cons_rx_buf->data = NULL;
1404 }
1405 
bnxt_abort_tpa(struct bnxt_cp_ring_info * cpr,u16 idx,u32 agg_bufs)1406 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1407 {
1408 	if (agg_bufs)
1409 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1410 }
1411 
1412 #ifdef CONFIG_INET
bnxt_gro_tunnel(struct sk_buff * skb,__be16 ip_proto)1413 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1414 {
1415 	struct udphdr *uh = NULL;
1416 
1417 	if (ip_proto == htons(ETH_P_IP)) {
1418 		struct iphdr *iph = (struct iphdr *)skb->data;
1419 
1420 		if (iph->protocol == IPPROTO_UDP)
1421 			uh = (struct udphdr *)(iph + 1);
1422 	} else {
1423 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1424 
1425 		if (iph->nexthdr == IPPROTO_UDP)
1426 			uh = (struct udphdr *)(iph + 1);
1427 	}
1428 	if (uh) {
1429 		if (uh->check)
1430 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1431 		else
1432 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1433 	}
1434 }
1435 #endif
1436 
bnxt_gro_func_5731x(struct bnxt_tpa_info * tpa_info,int payload_off,int tcp_ts,struct sk_buff * skb)1437 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1438 					   int payload_off, int tcp_ts,
1439 					   struct sk_buff *skb)
1440 {
1441 #ifdef CONFIG_INET
1442 	struct tcphdr *th;
1443 	int len, nw_off;
1444 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1445 	u32 hdr_info = tpa_info->hdr_info;
1446 	bool loopback = false;
1447 
1448 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1449 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1450 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1451 
1452 	/* If the packet is an internal loopback packet, the offsets will
1453 	 * have an extra 4 bytes.
1454 	 */
1455 	if (inner_mac_off == 4) {
1456 		loopback = true;
1457 	} else if (inner_mac_off > 4) {
1458 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1459 					    ETH_HLEN - 2));
1460 
1461 		/* We only support inner iPv4/ipv6.  If we don't see the
1462 		 * correct protocol ID, it must be a loopback packet where
1463 		 * the offsets are off by 4.
1464 		 */
1465 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1466 			loopback = true;
1467 	}
1468 	if (loopback) {
1469 		/* internal loopback packet, subtract all offsets by 4 */
1470 		inner_ip_off -= 4;
1471 		inner_mac_off -= 4;
1472 		outer_ip_off -= 4;
1473 	}
1474 
1475 	nw_off = inner_ip_off - ETH_HLEN;
1476 	skb_set_network_header(skb, nw_off);
1477 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1478 		struct ipv6hdr *iph = ipv6_hdr(skb);
1479 
1480 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1481 		len = skb->len - skb_transport_offset(skb);
1482 		th = tcp_hdr(skb);
1483 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1484 	} else {
1485 		struct iphdr *iph = ip_hdr(skb);
1486 
1487 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1488 		len = skb->len - skb_transport_offset(skb);
1489 		th = tcp_hdr(skb);
1490 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1491 	}
1492 
1493 	if (inner_mac_off) { /* tunnel */
1494 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1495 					    ETH_HLEN - 2));
1496 
1497 		bnxt_gro_tunnel(skb, proto);
1498 	}
1499 #endif
1500 	return skb;
1501 }
1502 
bnxt_gro_func_5750x(struct bnxt_tpa_info * tpa_info,int payload_off,int tcp_ts,struct sk_buff * skb)1503 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1504 					   int payload_off, int tcp_ts,
1505 					   struct sk_buff *skb)
1506 {
1507 #ifdef CONFIG_INET
1508 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1509 	u32 hdr_info = tpa_info->hdr_info;
1510 	int iphdr_len, nw_off;
1511 
1512 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1513 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1514 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1515 
1516 	nw_off = inner_ip_off - ETH_HLEN;
1517 	skb_set_network_header(skb, nw_off);
1518 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1519 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1520 	skb_set_transport_header(skb, nw_off + iphdr_len);
1521 
1522 	if (inner_mac_off) { /* tunnel */
1523 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1524 					    ETH_HLEN - 2));
1525 
1526 		bnxt_gro_tunnel(skb, proto);
1527 	}
1528 #endif
1529 	return skb;
1530 }
1531 
1532 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1533 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1534 
bnxt_gro_func_5730x(struct bnxt_tpa_info * tpa_info,int payload_off,int tcp_ts,struct sk_buff * skb)1535 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1536 					   int payload_off, int tcp_ts,
1537 					   struct sk_buff *skb)
1538 {
1539 #ifdef CONFIG_INET
1540 	struct tcphdr *th;
1541 	int len, nw_off, tcp_opt_len = 0;
1542 
1543 	if (tcp_ts)
1544 		tcp_opt_len = 12;
1545 
1546 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1547 		struct iphdr *iph;
1548 
1549 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1550 			 ETH_HLEN;
1551 		skb_set_network_header(skb, nw_off);
1552 		iph = ip_hdr(skb);
1553 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1554 		len = skb->len - skb_transport_offset(skb);
1555 		th = tcp_hdr(skb);
1556 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1557 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1558 		struct ipv6hdr *iph;
1559 
1560 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1561 			 ETH_HLEN;
1562 		skb_set_network_header(skb, nw_off);
1563 		iph = ipv6_hdr(skb);
1564 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1565 		len = skb->len - skb_transport_offset(skb);
1566 		th = tcp_hdr(skb);
1567 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1568 	} else {
1569 		dev_kfree_skb_any(skb);
1570 		return NULL;
1571 	}
1572 
1573 	if (nw_off) /* tunnel */
1574 		bnxt_gro_tunnel(skb, skb->protocol);
1575 #endif
1576 	return skb;
1577 }
1578 
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)1579 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1580 					   struct bnxt_tpa_info *tpa_info,
1581 					   struct rx_tpa_end_cmp *tpa_end,
1582 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1583 					   struct sk_buff *skb)
1584 {
1585 #ifdef CONFIG_INET
1586 	int payload_off;
1587 	u16 segs;
1588 
1589 	segs = TPA_END_TPA_SEGS(tpa_end);
1590 	if (segs == 1)
1591 		return skb;
1592 
1593 	NAPI_GRO_CB(skb)->count = segs;
1594 	skb_shinfo(skb)->gso_size =
1595 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1596 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1597 	if (bp->flags & BNXT_FLAG_CHIP_P5)
1598 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1599 	else
1600 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1601 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1602 	if (likely(skb))
1603 		tcp_gro_complete(skb);
1604 #endif
1605 	return skb;
1606 }
1607 
1608 /* Given the cfa_code of a received packet determine which
1609  * netdev (vf-rep or PF) the packet is destined to.
1610  */
bnxt_get_pkt_dev(struct bnxt * bp,u16 cfa_code)1611 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1612 {
1613 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1614 
1615 	/* if vf-rep dev is NULL, the must belongs to the PF */
1616 	return dev ? dev : bp->dev;
1617 }
1618 
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)1619 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1620 					   struct bnxt_cp_ring_info *cpr,
1621 					   u32 *raw_cons,
1622 					   struct rx_tpa_end_cmp *tpa_end,
1623 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1624 					   u8 *event)
1625 {
1626 	struct bnxt_napi *bnapi = cpr->bnapi;
1627 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1628 	u8 *data_ptr, agg_bufs;
1629 	unsigned int len;
1630 	struct bnxt_tpa_info *tpa_info;
1631 	dma_addr_t mapping;
1632 	struct sk_buff *skb;
1633 	u16 idx = 0, agg_id;
1634 	void *data;
1635 	bool gro;
1636 
1637 	if (unlikely(bnapi->in_reset)) {
1638 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1639 
1640 		if (rc < 0)
1641 			return ERR_PTR(-EBUSY);
1642 		return NULL;
1643 	}
1644 
1645 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
1646 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1647 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1648 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1649 		tpa_info = &rxr->rx_tpa[agg_id];
1650 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1651 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1652 				    agg_bufs, tpa_info->agg_count);
1653 			agg_bufs = tpa_info->agg_count;
1654 		}
1655 		tpa_info->agg_count = 0;
1656 		*event |= BNXT_AGG_EVENT;
1657 		bnxt_free_agg_idx(rxr, agg_id);
1658 		idx = agg_id;
1659 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1660 	} else {
1661 		agg_id = TPA_END_AGG_ID(tpa_end);
1662 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1663 		tpa_info = &rxr->rx_tpa[agg_id];
1664 		idx = RING_CMP(*raw_cons);
1665 		if (agg_bufs) {
1666 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1667 				return ERR_PTR(-EBUSY);
1668 
1669 			*event |= BNXT_AGG_EVENT;
1670 			idx = NEXT_CMP(idx);
1671 		}
1672 		gro = !!TPA_END_GRO(tpa_end);
1673 	}
1674 	data = tpa_info->data;
1675 	data_ptr = tpa_info->data_ptr;
1676 	prefetch(data_ptr);
1677 	len = tpa_info->len;
1678 	mapping = tpa_info->mapping;
1679 
1680 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1681 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1682 		if (agg_bufs > MAX_SKB_FRAGS)
1683 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1684 				    agg_bufs, (int)MAX_SKB_FRAGS);
1685 		return NULL;
1686 	}
1687 
1688 	if (len <= bp->rx_copy_thresh) {
1689 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1690 		if (!skb) {
1691 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1692 			cpr->sw_stats.rx.rx_oom_discards += 1;
1693 			return NULL;
1694 		}
1695 	} else {
1696 		u8 *new_data;
1697 		dma_addr_t new_mapping;
1698 
1699 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, GFP_ATOMIC);
1700 		if (!new_data) {
1701 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1702 			cpr->sw_stats.rx.rx_oom_discards += 1;
1703 			return NULL;
1704 		}
1705 
1706 		tpa_info->data = new_data;
1707 		tpa_info->data_ptr = new_data + bp->rx_offset;
1708 		tpa_info->mapping = new_mapping;
1709 
1710 		skb = build_skb(data, bp->rx_buf_size);
1711 		dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1712 				       bp->rx_buf_use_size, bp->rx_dir,
1713 				       DMA_ATTR_WEAK_ORDERING);
1714 
1715 		if (!skb) {
1716 			skb_free_frag(data);
1717 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1718 			cpr->sw_stats.rx.rx_oom_discards += 1;
1719 			return NULL;
1720 		}
1721 		skb_reserve(skb, bp->rx_offset);
1722 		skb_put(skb, len);
1723 	}
1724 
1725 	if (agg_bufs) {
1726 		skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true);
1727 		if (!skb) {
1728 			/* Page reuse already handled by bnxt_rx_pages(). */
1729 			cpr->sw_stats.rx.rx_oom_discards += 1;
1730 			return NULL;
1731 		}
1732 	}
1733 
1734 	skb->protocol =
1735 		eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
1736 
1737 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1738 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1739 
1740 	if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1741 	    (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1742 		__be16 vlan_proto = htons(tpa_info->metadata >>
1743 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1744 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1745 
1746 		if (eth_type_vlan(vlan_proto)) {
1747 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1748 		} else {
1749 			dev_kfree_skb(skb);
1750 			return NULL;
1751 		}
1752 	}
1753 
1754 	skb_checksum_none_assert(skb);
1755 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1756 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1757 		skb->csum_level =
1758 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1759 	}
1760 
1761 	if (gro)
1762 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1763 
1764 	return skb;
1765 }
1766 
bnxt_tpa_agg(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,struct rx_agg_cmp * rx_agg)1767 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1768 			 struct rx_agg_cmp *rx_agg)
1769 {
1770 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1771 	struct bnxt_tpa_info *tpa_info;
1772 
1773 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1774 	tpa_info = &rxr->rx_tpa[agg_id];
1775 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1776 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1777 }
1778 
bnxt_deliver_skb(struct bnxt * bp,struct bnxt_napi * bnapi,struct sk_buff * skb)1779 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1780 			     struct sk_buff *skb)
1781 {
1782 	if (skb->dev != bp->dev) {
1783 		/* this packet belongs to a vf-rep */
1784 		bnxt_vf_rep_rx(bp, skb);
1785 		return;
1786 	}
1787 	skb_record_rx_queue(skb, bnapi->index);
1788 	napi_gro_receive(&bnapi->napi, skb);
1789 }
1790 
1791 /* returns the following:
1792  * 1       - 1 packet successfully received
1793  * 0       - successful TPA_START, packet not completed yet
1794  * -EBUSY  - completion ring does not have all the agg buffers yet
1795  * -ENOMEM - packet aborted due to out of memory
1796  * -EIO    - packet aborted due to hw error indicated in BD
1797  */
bnxt_rx_pkt(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,u32 * raw_cons,u8 * event)1798 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1799 		       u32 *raw_cons, u8 *event)
1800 {
1801 	struct bnxt_napi *bnapi = cpr->bnapi;
1802 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1803 	struct net_device *dev = bp->dev;
1804 	struct rx_cmp *rxcmp;
1805 	struct rx_cmp_ext *rxcmp1;
1806 	u32 tmp_raw_cons = *raw_cons;
1807 	u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1808 	struct bnxt_sw_rx_bd *rx_buf;
1809 	unsigned int len;
1810 	u8 *data_ptr, agg_bufs, cmp_type;
1811 	bool xdp_active = false;
1812 	dma_addr_t dma_addr;
1813 	struct sk_buff *skb;
1814 	struct xdp_buff xdp;
1815 	u32 flags, misc;
1816 	void *data;
1817 	int rc = 0;
1818 
1819 	rxcmp = (struct rx_cmp *)
1820 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1821 
1822 	cmp_type = RX_CMP_TYPE(rxcmp);
1823 
1824 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
1825 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
1826 		goto next_rx_no_prod_no_len;
1827 	}
1828 
1829 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1830 	cp_cons = RING_CMP(tmp_raw_cons);
1831 	rxcmp1 = (struct rx_cmp_ext *)
1832 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1833 
1834 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1835 		return -EBUSY;
1836 
1837 	/* The valid test of the entry must be done first before
1838 	 * reading any further.
1839 	 */
1840 	dma_rmb();
1841 	prod = rxr->rx_prod;
1842 
1843 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1844 		bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1845 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
1846 
1847 		*event |= BNXT_RX_EVENT;
1848 		goto next_rx_no_prod_no_len;
1849 
1850 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1851 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
1852 				   (struct rx_tpa_end_cmp *)rxcmp,
1853 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
1854 
1855 		if (IS_ERR(skb))
1856 			return -EBUSY;
1857 
1858 		rc = -ENOMEM;
1859 		if (likely(skb)) {
1860 			bnxt_deliver_skb(bp, bnapi, skb);
1861 			rc = 1;
1862 		}
1863 		*event |= BNXT_RX_EVENT;
1864 		goto next_rx_no_prod_no_len;
1865 	}
1866 
1867 	cons = rxcmp->rx_cmp_opaque;
1868 	if (unlikely(cons != rxr->rx_next_cons)) {
1869 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
1870 
1871 		/* 0xffff is forced error, don't print it */
1872 		if (rxr->rx_next_cons != 0xffff)
1873 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
1874 				    cons, rxr->rx_next_cons);
1875 		bnxt_sched_reset(bp, rxr);
1876 		if (rc1)
1877 			return rc1;
1878 		goto next_rx_no_prod_no_len;
1879 	}
1880 	rx_buf = &rxr->rx_buf_ring[cons];
1881 	data = rx_buf->data;
1882 	data_ptr = rx_buf->data_ptr;
1883 	prefetch(data_ptr);
1884 
1885 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1886 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
1887 
1888 	if (agg_bufs) {
1889 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1890 			return -EBUSY;
1891 
1892 		cp_cons = NEXT_CMP(cp_cons);
1893 		*event |= BNXT_AGG_EVENT;
1894 	}
1895 	*event |= BNXT_RX_EVENT;
1896 
1897 	rx_buf->data = NULL;
1898 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1899 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
1900 
1901 		bnxt_reuse_rx_data(rxr, cons, data);
1902 		if (agg_bufs)
1903 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
1904 					       false);
1905 
1906 		rc = -EIO;
1907 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
1908 			bnapi->cp_ring.sw_stats.rx.rx_buf_errors++;
1909 			if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
1910 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
1911 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
1912 						 rx_err);
1913 				bnxt_sched_reset(bp, rxr);
1914 			}
1915 		}
1916 		goto next_rx_no_len;
1917 	}
1918 
1919 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
1920 	len = flags >> RX_CMP_LEN_SHIFT;
1921 	dma_addr = rx_buf->mapping;
1922 
1923 	if (bnxt_xdp_attached(bp, rxr)) {
1924 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
1925 		if (agg_bufs) {
1926 			u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
1927 							     cp_cons, agg_bufs,
1928 							     false);
1929 			if (!frag_len) {
1930 				cpr->sw_stats.rx.rx_oom_discards += 1;
1931 				rc = -ENOMEM;
1932 				goto next_rx;
1933 			}
1934 		}
1935 		xdp_active = true;
1936 	}
1937 
1938 	if (xdp_active) {
1939 		if (bnxt_rx_xdp(bp, rxr, cons, xdp, data, &data_ptr, &len, event)) {
1940 			rc = 1;
1941 			goto next_rx;
1942 		}
1943 	}
1944 
1945 	if (len <= bp->rx_copy_thresh) {
1946 		skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
1947 		bnxt_reuse_rx_data(rxr, cons, data);
1948 		if (!skb) {
1949 			if (agg_bufs) {
1950 				if (!xdp_active)
1951 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
1952 							       agg_bufs, false);
1953 				else
1954 					bnxt_xdp_buff_frags_free(rxr, &xdp);
1955 			}
1956 			cpr->sw_stats.rx.rx_oom_discards += 1;
1957 			rc = -ENOMEM;
1958 			goto next_rx;
1959 		}
1960 	} else {
1961 		u32 payload;
1962 
1963 		if (rx_buf->data_ptr == data_ptr)
1964 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
1965 		else
1966 			payload = 0;
1967 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
1968 				      payload | len);
1969 		if (!skb) {
1970 			cpr->sw_stats.rx.rx_oom_discards += 1;
1971 			rc = -ENOMEM;
1972 			goto next_rx;
1973 		}
1974 	}
1975 
1976 	if (agg_bufs) {
1977 		if (!xdp_active) {
1978 			skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
1979 			if (!skb) {
1980 				cpr->sw_stats.rx.rx_oom_discards += 1;
1981 				rc = -ENOMEM;
1982 				goto next_rx;
1983 			}
1984 		} else {
1985 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1);
1986 			if (!skb) {
1987 				/* we should be able to free the old skb here */
1988 				bnxt_xdp_buff_frags_free(rxr, &xdp);
1989 				cpr->sw_stats.rx.rx_oom_discards += 1;
1990 				rc = -ENOMEM;
1991 				goto next_rx;
1992 			}
1993 		}
1994 	}
1995 
1996 	if (RX_CMP_HASH_VALID(rxcmp)) {
1997 		u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1998 		enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1999 
2000 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
2001 		if (hash_type != 1 && hash_type != 3)
2002 			type = PKT_HASH_TYPE_L3;
2003 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2004 	}
2005 
2006 	cfa_code = RX_CMP_CFA_CODE(rxcmp1);
2007 	skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
2008 
2009 	if ((rxcmp1->rx_cmp_flags2 &
2010 	     cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
2011 	    (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
2012 		u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
2013 		u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2014 		__be16 vlan_proto = htons(meta_data >>
2015 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
2016 
2017 		if (eth_type_vlan(vlan_proto)) {
2018 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2019 		} else {
2020 			dev_kfree_skb(skb);
2021 			goto next_rx;
2022 		}
2023 	}
2024 
2025 	skb_checksum_none_assert(skb);
2026 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2027 		if (dev->features & NETIF_F_RXCSUM) {
2028 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2029 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2030 		}
2031 	} else {
2032 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2033 			if (dev->features & NETIF_F_RXCSUM)
2034 				bnapi->cp_ring.sw_stats.rx.rx_l4_csum_errors++;
2035 		}
2036 	}
2037 
2038 	if (unlikely((flags & RX_CMP_FLAGS_ITYPES_MASK) ==
2039 		     RX_CMP_FLAGS_ITYPE_PTP_W_TS) || bp->ptp_all_rx_tstamp) {
2040 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
2041 			u32 cmpl_ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
2042 			u64 ns, ts;
2043 
2044 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2045 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2046 
2047 				spin_lock_bh(&ptp->ptp_lock);
2048 				ns = timecounter_cyc2time(&ptp->tc, ts);
2049 				spin_unlock_bh(&ptp->ptp_lock);
2050 				memset(skb_hwtstamps(skb), 0,
2051 				       sizeof(*skb_hwtstamps(skb)));
2052 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2053 			}
2054 		}
2055 	}
2056 	bnxt_deliver_skb(bp, bnapi, skb);
2057 	rc = 1;
2058 
2059 next_rx:
2060 	cpr->rx_packets += 1;
2061 	cpr->rx_bytes += len;
2062 
2063 next_rx_no_len:
2064 	rxr->rx_prod = NEXT_RX(prod);
2065 	rxr->rx_next_cons = NEXT_RX(cons);
2066 
2067 next_rx_no_prod_no_len:
2068 	*raw_cons = tmp_raw_cons;
2069 
2070 	return rc;
2071 }
2072 
2073 /* In netpoll mode, if we are using a combined completion ring, we need to
2074  * discard the rx packets and recycle the buffers.
2075  */
bnxt_force_rx_discard(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,u32 * raw_cons,u8 * event)2076 static int bnxt_force_rx_discard(struct bnxt *bp,
2077 				 struct bnxt_cp_ring_info *cpr,
2078 				 u32 *raw_cons, u8 *event)
2079 {
2080 	u32 tmp_raw_cons = *raw_cons;
2081 	struct rx_cmp_ext *rxcmp1;
2082 	struct rx_cmp *rxcmp;
2083 	u16 cp_cons;
2084 	u8 cmp_type;
2085 	int rc;
2086 
2087 	cp_cons = RING_CMP(tmp_raw_cons);
2088 	rxcmp = (struct rx_cmp *)
2089 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2090 
2091 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2092 	cp_cons = RING_CMP(tmp_raw_cons);
2093 	rxcmp1 = (struct rx_cmp_ext *)
2094 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2095 
2096 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2097 		return -EBUSY;
2098 
2099 	/* The valid test of the entry must be done first before
2100 	 * reading any further.
2101 	 */
2102 	dma_rmb();
2103 	cmp_type = RX_CMP_TYPE(rxcmp);
2104 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
2105 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2106 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2107 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2108 		struct rx_tpa_end_cmp_ext *tpa_end1;
2109 
2110 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2111 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2112 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2113 	}
2114 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2115 	if (rc && rc != -EBUSY)
2116 		cpr->sw_stats.rx.rx_netpoll_discards += 1;
2117 	return rc;
2118 }
2119 
bnxt_fw_health_readl(struct bnxt * bp,int reg_idx)2120 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2121 {
2122 	struct bnxt_fw_health *fw_health = bp->fw_health;
2123 	u32 reg = fw_health->regs[reg_idx];
2124 	u32 reg_type, reg_off, val = 0;
2125 
2126 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2127 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2128 	switch (reg_type) {
2129 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2130 		pci_read_config_dword(bp->pdev, reg_off, &val);
2131 		break;
2132 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2133 		reg_off = fw_health->mapped_regs[reg_idx];
2134 		fallthrough;
2135 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2136 		val = readl(bp->bar0 + reg_off);
2137 		break;
2138 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2139 		val = readl(bp->bar1 + reg_off);
2140 		break;
2141 	}
2142 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2143 		val &= fw_health->fw_reset_inprog_reg_mask;
2144 	return val;
2145 }
2146 
bnxt_agg_ring_id_to_grp_idx(struct bnxt * bp,u16 ring_id)2147 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2148 {
2149 	int i;
2150 
2151 	for (i = 0; i < bp->rx_nr_rings; i++) {
2152 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2153 		struct bnxt_ring_grp_info *grp_info;
2154 
2155 		grp_info = &bp->grp_info[grp_idx];
2156 		if (grp_info->agg_fw_ring_id == ring_id)
2157 			return grp_idx;
2158 	}
2159 	return INVALID_HW_RING_ID;
2160 }
2161 
bnxt_event_error_report(struct bnxt * bp,u32 data1,u32 data2)2162 static void bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2163 {
2164 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2165 
2166 	switch (err_type) {
2167 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2168 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2169 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2170 		break;
2171 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2172 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2173 		break;
2174 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2175 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2176 		break;
2177 	default:
2178 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2179 			   err_type);
2180 		break;
2181 	}
2182 }
2183 
2184 #define BNXT_GET_EVENT_PORT(data)	\
2185 	((data) &			\
2186 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2187 
2188 #define BNXT_EVENT_RING_TYPE(data2)	\
2189 	((data2) &			\
2190 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2191 
2192 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2193 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2194 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2195 
2196 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2197 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2198 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2199 
2200 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2201 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2202 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2203 
2204 #define BNXT_PHC_BITS	48
2205 
bnxt_async_event_process(struct bnxt * bp,struct hwrm_async_event_cmpl * cmpl)2206 static int bnxt_async_event_process(struct bnxt *bp,
2207 				    struct hwrm_async_event_cmpl *cmpl)
2208 {
2209 	u16 event_id = le16_to_cpu(cmpl->event_id);
2210 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2211 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2212 
2213 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2214 		   event_id, data1, data2);
2215 
2216 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2217 	switch (event_id) {
2218 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2219 		struct bnxt_link_info *link_info = &bp->link_info;
2220 
2221 		if (BNXT_VF(bp))
2222 			goto async_event_process_exit;
2223 
2224 		/* print unsupported speed warning in forced speed mode only */
2225 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2226 		    (data1 & 0x20000)) {
2227 			u16 fw_speed = link_info->force_link_speed;
2228 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2229 
2230 			if (speed != SPEED_UNKNOWN)
2231 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2232 					    speed);
2233 		}
2234 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2235 	}
2236 		fallthrough;
2237 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2238 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2239 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2240 		fallthrough;
2241 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2242 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2243 		break;
2244 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2245 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2246 		break;
2247 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2248 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2249 
2250 		if (BNXT_VF(bp))
2251 			break;
2252 
2253 		if (bp->pf.port_id != port_id)
2254 			break;
2255 
2256 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2257 		break;
2258 	}
2259 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2260 		if (BNXT_PF(bp))
2261 			goto async_event_process_exit;
2262 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2263 		break;
2264 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2265 		char *type_str = "Solicited";
2266 
2267 		if (!bp->fw_health)
2268 			goto async_event_process_exit;
2269 
2270 		bp->fw_reset_timestamp = jiffies;
2271 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2272 		if (!bp->fw_reset_min_dsecs)
2273 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2274 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2275 		if (!bp->fw_reset_max_dsecs)
2276 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2277 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2278 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2279 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2280 			type_str = "Fatal";
2281 			bp->fw_health->fatalities++;
2282 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2283 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2284 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2285 			type_str = "Non-fatal";
2286 			bp->fw_health->survivals++;
2287 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2288 		}
2289 		netif_warn(bp, hw, bp->dev,
2290 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2291 			   type_str, data1, data2,
2292 			   bp->fw_reset_min_dsecs * 100,
2293 			   bp->fw_reset_max_dsecs * 100);
2294 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2295 		break;
2296 	}
2297 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2298 		struct bnxt_fw_health *fw_health = bp->fw_health;
2299 		char *status_desc = "healthy";
2300 		u32 status;
2301 
2302 		if (!fw_health)
2303 			goto async_event_process_exit;
2304 
2305 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2306 			fw_health->enabled = false;
2307 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2308 			break;
2309 		}
2310 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2311 		fw_health->tmr_multiplier =
2312 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2313 				     bp->current_interval * 10);
2314 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2315 		if (!fw_health->enabled)
2316 			fw_health->last_fw_heartbeat =
2317 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2318 		fw_health->last_fw_reset_cnt =
2319 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2320 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2321 		if (status != BNXT_FW_STATUS_HEALTHY)
2322 			status_desc = "unhealthy";
2323 		netif_info(bp, drv, bp->dev,
2324 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2325 			   fw_health->primary ? "primary" : "backup", status,
2326 			   status_desc, fw_health->last_fw_reset_cnt);
2327 		if (!fw_health->enabled) {
2328 			/* Make sure tmr_counter is set and visible to
2329 			 * bnxt_health_check() before setting enabled to true.
2330 			 */
2331 			smp_wmb();
2332 			fw_health->enabled = true;
2333 		}
2334 		goto async_event_process_exit;
2335 	}
2336 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2337 		netif_notice(bp, hw, bp->dev,
2338 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2339 			     data1, data2);
2340 		goto async_event_process_exit;
2341 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2342 		struct bnxt_rx_ring_info *rxr;
2343 		u16 grp_idx;
2344 
2345 		if (bp->flags & BNXT_FLAG_CHIP_P5)
2346 			goto async_event_process_exit;
2347 
2348 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2349 			    BNXT_EVENT_RING_TYPE(data2), data1);
2350 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2351 			goto async_event_process_exit;
2352 
2353 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2354 		if (grp_idx == INVALID_HW_RING_ID) {
2355 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2356 				    data1);
2357 			goto async_event_process_exit;
2358 		}
2359 		rxr = bp->bnapi[grp_idx]->rx_ring;
2360 		bnxt_sched_reset(bp, rxr);
2361 		goto async_event_process_exit;
2362 	}
2363 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2364 		struct bnxt_fw_health *fw_health = bp->fw_health;
2365 
2366 		netif_notice(bp, hw, bp->dev,
2367 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2368 			     data1, data2);
2369 		if (fw_health) {
2370 			fw_health->echo_req_data1 = data1;
2371 			fw_health->echo_req_data2 = data2;
2372 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2373 			break;
2374 		}
2375 		goto async_event_process_exit;
2376 	}
2377 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2378 		bnxt_ptp_pps_event(bp, data1, data2);
2379 		goto async_event_process_exit;
2380 	}
2381 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2382 		bnxt_event_error_report(bp, data1, data2);
2383 		goto async_event_process_exit;
2384 	}
2385 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2386 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2387 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2388 			if (bp->fw_cap & BNXT_FW_CAP_PTP_RTC) {
2389 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2390 				u64 ns;
2391 
2392 				spin_lock_bh(&ptp->ptp_lock);
2393 				bnxt_ptp_update_current_time(bp);
2394 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2395 				       BNXT_PHC_BITS) | ptp->current_time);
2396 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2397 				spin_unlock_bh(&ptp->ptp_lock);
2398 			}
2399 			break;
2400 		}
2401 		goto async_event_process_exit;
2402 	}
2403 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2404 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2405 
2406 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2407 		goto async_event_process_exit;
2408 	}
2409 	default:
2410 		goto async_event_process_exit;
2411 	}
2412 	bnxt_queue_sp_work(bp);
2413 async_event_process_exit:
2414 	bnxt_ulp_async_events(bp, cmpl);
2415 	return 0;
2416 }
2417 
bnxt_hwrm_handler(struct bnxt * bp,struct tx_cmp * txcmp)2418 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2419 {
2420 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2421 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2422 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2423 				(struct hwrm_fwd_req_cmpl *)txcmp;
2424 
2425 	switch (cmpl_type) {
2426 	case CMPL_BASE_TYPE_HWRM_DONE:
2427 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2428 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2429 		break;
2430 
2431 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2432 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2433 
2434 		if ((vf_id < bp->pf.first_vf_id) ||
2435 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2436 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2437 				   vf_id);
2438 			return -EINVAL;
2439 		}
2440 
2441 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2442 		set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
2443 		bnxt_queue_sp_work(bp);
2444 		break;
2445 
2446 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2447 		bnxt_async_event_process(bp,
2448 					 (struct hwrm_async_event_cmpl *)txcmp);
2449 		break;
2450 
2451 	default:
2452 		break;
2453 	}
2454 
2455 	return 0;
2456 }
2457 
bnxt_msix(int irq,void * dev_instance)2458 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2459 {
2460 	struct bnxt_napi *bnapi = dev_instance;
2461 	struct bnxt *bp = bnapi->bp;
2462 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2463 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2464 
2465 	cpr->event_ctr++;
2466 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2467 	napi_schedule(&bnapi->napi);
2468 	return IRQ_HANDLED;
2469 }
2470 
bnxt_has_work(struct bnxt * bp,struct bnxt_cp_ring_info * cpr)2471 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2472 {
2473 	u32 raw_cons = cpr->cp_raw_cons;
2474 	u16 cons = RING_CMP(raw_cons);
2475 	struct tx_cmp *txcmp;
2476 
2477 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2478 
2479 	return TX_CMP_VALID(txcmp, raw_cons);
2480 }
2481 
bnxt_inta(int irq,void * dev_instance)2482 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
2483 {
2484 	struct bnxt_napi *bnapi = dev_instance;
2485 	struct bnxt *bp = bnapi->bp;
2486 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2487 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2488 	u32 int_status;
2489 
2490 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2491 
2492 	if (!bnxt_has_work(bp, cpr)) {
2493 		int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
2494 		/* return if erroneous interrupt */
2495 		if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
2496 			return IRQ_NONE;
2497 	}
2498 
2499 	/* disable ring IRQ */
2500 	BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
2501 
2502 	/* Return here if interrupt is shared and is disabled. */
2503 	if (unlikely(atomic_read(&bp->intr_sem) != 0))
2504 		return IRQ_HANDLED;
2505 
2506 	napi_schedule(&bnapi->napi);
2507 	return IRQ_HANDLED;
2508 }
2509 
__bnxt_poll_work(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,int budget)2510 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2511 			    int budget)
2512 {
2513 	struct bnxt_napi *bnapi = cpr->bnapi;
2514 	u32 raw_cons = cpr->cp_raw_cons;
2515 	u32 cons;
2516 	int tx_pkts = 0;
2517 	int rx_pkts = 0;
2518 	u8 event = 0;
2519 	struct tx_cmp *txcmp;
2520 
2521 	cpr->has_more_work = 0;
2522 	cpr->had_work_done = 1;
2523 	while (1) {
2524 		int rc;
2525 
2526 		cons = RING_CMP(raw_cons);
2527 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2528 
2529 		if (!TX_CMP_VALID(txcmp, raw_cons))
2530 			break;
2531 
2532 		/* The valid test of the entry must be done first before
2533 		 * reading any further.
2534 		 */
2535 		dma_rmb();
2536 		if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
2537 			tx_pkts++;
2538 			/* return full budget so NAPI will complete. */
2539 			if (unlikely(tx_pkts >= bp->tx_wake_thresh)) {
2540 				rx_pkts = budget;
2541 				raw_cons = NEXT_RAW_CMP(raw_cons);
2542 				if (budget)
2543 					cpr->has_more_work = 1;
2544 				break;
2545 			}
2546 		} else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2547 			if (likely(budget))
2548 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2549 			else
2550 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2551 							   &event);
2552 			if (likely(rc >= 0))
2553 				rx_pkts += rc;
2554 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2555 			 * the NAPI budget.  Otherwise, we may potentially loop
2556 			 * here forever if we consistently cannot allocate
2557 			 * buffers.
2558 			 */
2559 			else if (rc == -ENOMEM && budget)
2560 				rx_pkts++;
2561 			else if (rc == -EBUSY)	/* partial completion */
2562 				break;
2563 		} else if (unlikely((TX_CMP_TYPE(txcmp) ==
2564 				     CMPL_BASE_TYPE_HWRM_DONE) ||
2565 				    (TX_CMP_TYPE(txcmp) ==
2566 				     CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
2567 				    (TX_CMP_TYPE(txcmp) ==
2568 				     CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
2569 			bnxt_hwrm_handler(bp, txcmp);
2570 		}
2571 		raw_cons = NEXT_RAW_CMP(raw_cons);
2572 
2573 		if (rx_pkts && rx_pkts == budget) {
2574 			cpr->has_more_work = 1;
2575 			break;
2576 		}
2577 	}
2578 
2579 	if (event & BNXT_REDIRECT_EVENT)
2580 		xdp_do_flush();
2581 
2582 	if (event & BNXT_TX_EVENT) {
2583 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
2584 		u16 prod = txr->tx_prod;
2585 
2586 		/* Sync BD data before updating doorbell */
2587 		wmb();
2588 
2589 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
2590 	}
2591 
2592 	cpr->cp_raw_cons = raw_cons;
2593 	bnapi->tx_pkts += tx_pkts;
2594 	bnapi->events |= event;
2595 	return rx_pkts;
2596 }
2597 
__bnxt_poll_work_done(struct bnxt * bp,struct bnxt_napi * bnapi)2598 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi)
2599 {
2600 	if (bnapi->tx_pkts) {
2601 		bnapi->tx_int(bp, bnapi, bnapi->tx_pkts);
2602 		bnapi->tx_pkts = 0;
2603 	}
2604 
2605 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
2606 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2607 
2608 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2609 	}
2610 	if (bnapi->events & BNXT_AGG_EVENT) {
2611 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2612 
2613 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2614 	}
2615 	bnapi->events = 0;
2616 }
2617 
bnxt_poll_work(struct bnxt * bp,struct bnxt_cp_ring_info * cpr,int budget)2618 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2619 			  int budget)
2620 {
2621 	struct bnxt_napi *bnapi = cpr->bnapi;
2622 	int rx_pkts;
2623 
2624 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2625 
2626 	/* ACK completion ring before freeing tx ring and producing new
2627 	 * buffers in rx/agg rings to prevent overflowing the completion
2628 	 * ring.
2629 	 */
2630 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
2631 
2632 	__bnxt_poll_work_done(bp, bnapi);
2633 	return rx_pkts;
2634 }
2635 
bnxt_poll_nitroa0(struct napi_struct * napi,int budget)2636 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
2637 {
2638 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2639 	struct bnxt *bp = bnapi->bp;
2640 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2641 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2642 	struct tx_cmp *txcmp;
2643 	struct rx_cmp_ext *rxcmp1;
2644 	u32 cp_cons, tmp_raw_cons;
2645 	u32 raw_cons = cpr->cp_raw_cons;
2646 	u32 rx_pkts = 0;
2647 	u8 event = 0;
2648 
2649 	while (1) {
2650 		int rc;
2651 
2652 		cp_cons = RING_CMP(raw_cons);
2653 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2654 
2655 		if (!TX_CMP_VALID(txcmp, raw_cons))
2656 			break;
2657 
2658 		/* The valid test of the entry must be done first before
2659 		 * reading any further.
2660 		 */
2661 		dma_rmb();
2662 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2663 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
2664 			cp_cons = RING_CMP(tmp_raw_cons);
2665 			rxcmp1 = (struct rx_cmp_ext *)
2666 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2667 
2668 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2669 				break;
2670 
2671 			/* force an error to recycle the buffer */
2672 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2673 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2674 
2675 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2676 			if (likely(rc == -EIO) && budget)
2677 				rx_pkts++;
2678 			else if (rc == -EBUSY)	/* partial completion */
2679 				break;
2680 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
2681 				    CMPL_BASE_TYPE_HWRM_DONE)) {
2682 			bnxt_hwrm_handler(bp, txcmp);
2683 		} else {
2684 			netdev_err(bp->dev,
2685 				   "Invalid completion received on special ring\n");
2686 		}
2687 		raw_cons = NEXT_RAW_CMP(raw_cons);
2688 
2689 		if (rx_pkts == budget)
2690 			break;
2691 	}
2692 
2693 	cpr->cp_raw_cons = raw_cons;
2694 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
2695 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2696 
2697 	if (event & BNXT_AGG_EVENT)
2698 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2699 
2700 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
2701 		napi_complete_done(napi, rx_pkts);
2702 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2703 	}
2704 	return rx_pkts;
2705 }
2706 
bnxt_poll(struct napi_struct * napi,int budget)2707 static int bnxt_poll(struct napi_struct *napi, int budget)
2708 {
2709 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2710 	struct bnxt *bp = bnapi->bp;
2711 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2712 	int work_done = 0;
2713 
2714 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
2715 		napi_complete(napi);
2716 		return 0;
2717 	}
2718 	while (1) {
2719 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
2720 
2721 		if (work_done >= budget) {
2722 			if (!budget)
2723 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2724 			break;
2725 		}
2726 
2727 		if (!bnxt_has_work(bp, cpr)) {
2728 			if (napi_complete_done(napi, work_done))
2729 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2730 			break;
2731 		}
2732 	}
2733 	if (bp->flags & BNXT_FLAG_DIM) {
2734 		struct dim_sample dim_sample = {};
2735 
2736 		dim_update_sample(cpr->event_ctr,
2737 				  cpr->rx_packets,
2738 				  cpr->rx_bytes,
2739 				  &dim_sample);
2740 		net_dim(&cpr->dim, dim_sample);
2741 	}
2742 	return work_done;
2743 }
2744 
__bnxt_poll_cqs(struct bnxt * bp,struct bnxt_napi * bnapi,int budget)2745 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
2746 {
2747 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2748 	int i, work_done = 0;
2749 
2750 	for (i = 0; i < 2; i++) {
2751 		struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2752 
2753 		if (cpr2) {
2754 			work_done += __bnxt_poll_work(bp, cpr2,
2755 						      budget - work_done);
2756 			cpr->has_more_work |= cpr2->has_more_work;
2757 		}
2758 	}
2759 	return work_done;
2760 }
2761 
__bnxt_poll_cqs_done(struct bnxt * bp,struct bnxt_napi * bnapi,u64 dbr_type)2762 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2763 				 u64 dbr_type)
2764 {
2765 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2766 	int i;
2767 
2768 	for (i = 0; i < 2; i++) {
2769 		struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2770 		struct bnxt_db_info *db;
2771 
2772 		if (cpr2 && cpr2->had_work_done) {
2773 			db = &cpr2->cp_db;
2774 			bnxt_writeq(bp, db->db_key64 | dbr_type |
2775 				    RING_CMP(cpr2->cp_raw_cons), db->doorbell);
2776 			cpr2->had_work_done = 0;
2777 		}
2778 	}
2779 	__bnxt_poll_work_done(bp, bnapi);
2780 }
2781 
bnxt_poll_p5(struct napi_struct * napi,int budget)2782 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
2783 {
2784 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2785 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2786 	struct bnxt_cp_ring_info *cpr_rx;
2787 	u32 raw_cons = cpr->cp_raw_cons;
2788 	struct bnxt *bp = bnapi->bp;
2789 	struct nqe_cn *nqcmp;
2790 	int work_done = 0;
2791 	u32 cons;
2792 
2793 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
2794 		napi_complete(napi);
2795 		return 0;
2796 	}
2797 	if (cpr->has_more_work) {
2798 		cpr->has_more_work = 0;
2799 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
2800 	}
2801 	while (1) {
2802 		cons = RING_CMP(raw_cons);
2803 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2804 
2805 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
2806 			if (cpr->has_more_work)
2807 				break;
2808 
2809 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL);
2810 			cpr->cp_raw_cons = raw_cons;
2811 			if (napi_complete_done(napi, work_done))
2812 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
2813 						  cpr->cp_raw_cons);
2814 			goto poll_done;
2815 		}
2816 
2817 		/* The valid test of the entry must be done first before
2818 		 * reading any further.
2819 		 */
2820 		dma_rmb();
2821 
2822 		if (nqcmp->type == cpu_to_le16(NQ_CN_TYPE_CQ_NOTIFICATION)) {
2823 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
2824 			struct bnxt_cp_ring_info *cpr2;
2825 
2826 			/* No more budget for RX work */
2827 			if (budget && work_done >= budget && idx == BNXT_RX_HDL)
2828 				break;
2829 
2830 			cpr2 = cpr->cp_ring_arr[idx];
2831 			work_done += __bnxt_poll_work(bp, cpr2,
2832 						      budget - work_done);
2833 			cpr->has_more_work |= cpr2->has_more_work;
2834 		} else {
2835 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
2836 		}
2837 		raw_cons = NEXT_RAW_CMP(raw_cons);
2838 	}
2839 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ);
2840 	if (raw_cons != cpr->cp_raw_cons) {
2841 		cpr->cp_raw_cons = raw_cons;
2842 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
2843 	}
2844 poll_done:
2845 	cpr_rx = cpr->cp_ring_arr[BNXT_RX_HDL];
2846 	if (cpr_rx && (bp->flags & BNXT_FLAG_DIM)) {
2847 		struct dim_sample dim_sample = {};
2848 
2849 		dim_update_sample(cpr->event_ctr,
2850 				  cpr_rx->rx_packets,
2851 				  cpr_rx->rx_bytes,
2852 				  &dim_sample);
2853 		net_dim(&cpr->dim, dim_sample);
2854 	}
2855 	return work_done;
2856 }
2857 
bnxt_free_tx_skbs(struct bnxt * bp)2858 static void bnxt_free_tx_skbs(struct bnxt *bp)
2859 {
2860 	int i, max_idx;
2861 	struct pci_dev *pdev = bp->pdev;
2862 
2863 	if (!bp->tx_ring)
2864 		return;
2865 
2866 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
2867 	for (i = 0; i < bp->tx_nr_rings; i++) {
2868 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2869 		int j;
2870 
2871 		if (!txr->tx_buf_ring)
2872 			continue;
2873 
2874 		for (j = 0; j < max_idx;) {
2875 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
2876 			struct sk_buff *skb;
2877 			int k, last;
2878 
2879 			if (i < bp->tx_nr_rings_xdp &&
2880 			    tx_buf->action == XDP_REDIRECT) {
2881 				dma_unmap_single(&pdev->dev,
2882 					dma_unmap_addr(tx_buf, mapping),
2883 					dma_unmap_len(tx_buf, len),
2884 					DMA_TO_DEVICE);
2885 				xdp_return_frame(tx_buf->xdpf);
2886 				tx_buf->action = 0;
2887 				tx_buf->xdpf = NULL;
2888 				j++;
2889 				continue;
2890 			}
2891 
2892 			skb = tx_buf->skb;
2893 			if (!skb) {
2894 				j++;
2895 				continue;
2896 			}
2897 
2898 			tx_buf->skb = NULL;
2899 
2900 			if (tx_buf->is_push) {
2901 				dev_kfree_skb(skb);
2902 				j += 2;
2903 				continue;
2904 			}
2905 
2906 			dma_unmap_single(&pdev->dev,
2907 					 dma_unmap_addr(tx_buf, mapping),
2908 					 skb_headlen(skb),
2909 					 DMA_TO_DEVICE);
2910 
2911 			last = tx_buf->nr_frags;
2912 			j += 2;
2913 			for (k = 0; k < last; k++, j++) {
2914 				int ring_idx = j & bp->tx_ring_mask;
2915 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2916 
2917 				tx_buf = &txr->tx_buf_ring[ring_idx];
2918 				dma_unmap_page(
2919 					&pdev->dev,
2920 					dma_unmap_addr(tx_buf, mapping),
2921 					skb_frag_size(frag), DMA_TO_DEVICE);
2922 			}
2923 			dev_kfree_skb(skb);
2924 		}
2925 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
2926 	}
2927 }
2928 
bnxt_free_one_rx_ring_skbs(struct bnxt * bp,int ring_nr)2929 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr)
2930 {
2931 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
2932 	struct pci_dev *pdev = bp->pdev;
2933 	struct bnxt_tpa_idx_map *map;
2934 	int i, max_idx, max_agg_idx;
2935 
2936 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2937 	max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2938 	if (!rxr->rx_tpa)
2939 		goto skip_rx_tpa_free;
2940 
2941 	for (i = 0; i < bp->max_tpa; i++) {
2942 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
2943 		u8 *data = tpa_info->data;
2944 
2945 		if (!data)
2946 			continue;
2947 
2948 		dma_unmap_single_attrs(&pdev->dev, tpa_info->mapping,
2949 				       bp->rx_buf_use_size, bp->rx_dir,
2950 				       DMA_ATTR_WEAK_ORDERING);
2951 
2952 		tpa_info->data = NULL;
2953 
2954 		skb_free_frag(data);
2955 	}
2956 
2957 skip_rx_tpa_free:
2958 	if (!rxr->rx_buf_ring)
2959 		goto skip_rx_buf_free;
2960 
2961 	for (i = 0; i < max_idx; i++) {
2962 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
2963 		dma_addr_t mapping = rx_buf->mapping;
2964 		void *data = rx_buf->data;
2965 
2966 		if (!data)
2967 			continue;
2968 
2969 		rx_buf->data = NULL;
2970 		if (BNXT_RX_PAGE_MODE(bp)) {
2971 			mapping -= bp->rx_dma_offset;
2972 			dma_unmap_page_attrs(&pdev->dev, mapping, PAGE_SIZE,
2973 					     bp->rx_dir,
2974 					     DMA_ATTR_WEAK_ORDERING);
2975 			page_pool_recycle_direct(rxr->page_pool, data);
2976 		} else {
2977 			dma_unmap_single_attrs(&pdev->dev, mapping,
2978 					       bp->rx_buf_use_size, bp->rx_dir,
2979 					       DMA_ATTR_WEAK_ORDERING);
2980 			skb_free_frag(data);
2981 		}
2982 	}
2983 
2984 skip_rx_buf_free:
2985 	if (!rxr->rx_agg_ring)
2986 		goto skip_rx_agg_free;
2987 
2988 	for (i = 0; i < max_agg_idx; i++) {
2989 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
2990 		struct page *page = rx_agg_buf->page;
2991 
2992 		if (!page)
2993 			continue;
2994 
2995 		if (BNXT_RX_PAGE_MODE(bp)) {
2996 			dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2997 					     BNXT_RX_PAGE_SIZE, bp->rx_dir,
2998 					     DMA_ATTR_WEAK_ORDERING);
2999 			rx_agg_buf->page = NULL;
3000 			__clear_bit(i, rxr->rx_agg_bmap);
3001 
3002 			page_pool_recycle_direct(rxr->page_pool, page);
3003 		} else {
3004 			dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
3005 					     BNXT_RX_PAGE_SIZE, DMA_FROM_DEVICE,
3006 					     DMA_ATTR_WEAK_ORDERING);
3007 			rx_agg_buf->page = NULL;
3008 			__clear_bit(i, rxr->rx_agg_bmap);
3009 
3010 			__free_page(page);
3011 		}
3012 	}
3013 
3014 skip_rx_agg_free:
3015 	if (rxr->rx_page) {
3016 		__free_page(rxr->rx_page);
3017 		rxr->rx_page = NULL;
3018 	}
3019 	map = rxr->rx_tpa_idx_map;
3020 	if (map)
3021 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3022 }
3023 
bnxt_free_rx_skbs(struct bnxt * bp)3024 static void bnxt_free_rx_skbs(struct bnxt *bp)
3025 {
3026 	int i;
3027 
3028 	if (!bp->rx_ring)
3029 		return;
3030 
3031 	for (i = 0; i < bp->rx_nr_rings; i++)
3032 		bnxt_free_one_rx_ring_skbs(bp, i);
3033 }
3034 
bnxt_free_skbs(struct bnxt * bp)3035 static void bnxt_free_skbs(struct bnxt *bp)
3036 {
3037 	bnxt_free_tx_skbs(bp);
3038 	bnxt_free_rx_skbs(bp);
3039 }
3040 
bnxt_init_ctx_mem(struct bnxt_mem_init * mem_init,void * p,int len)3041 static void bnxt_init_ctx_mem(struct bnxt_mem_init *mem_init, void *p, int len)
3042 {
3043 	u8 init_val = mem_init->init_val;
3044 	u16 offset = mem_init->offset;
3045 	u8 *p2 = p;
3046 	int i;
3047 
3048 	if (!init_val)
3049 		return;
3050 	if (offset == BNXT_MEM_INVALID_OFFSET) {
3051 		memset(p, init_val, len);
3052 		return;
3053 	}
3054 	for (i = 0; i < len; i += mem_init->size)
3055 		*(p2 + i + offset) = init_val;
3056 }
3057 
bnxt_free_ring(struct bnxt * bp,struct bnxt_ring_mem_info * rmem)3058 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3059 {
3060 	struct pci_dev *pdev = bp->pdev;
3061 	int i;
3062 
3063 	if (!rmem->pg_arr)
3064 		goto skip_pages;
3065 
3066 	for (i = 0; i < rmem->nr_pages; i++) {
3067 		if (!rmem->pg_arr[i])
3068 			continue;
3069 
3070 		dma_free_coherent(&pdev->dev, rmem->page_size,
3071 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3072 
3073 		rmem->pg_arr[i] = NULL;
3074 	}
3075 skip_pages:
3076 	if (rmem->pg_tbl) {
3077 		size_t pg_tbl_size = rmem->nr_pages * 8;
3078 
3079 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3080 			pg_tbl_size = rmem->page_size;
3081 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3082 				  rmem->pg_tbl, rmem->pg_tbl_map);
3083 		rmem->pg_tbl = NULL;
3084 	}
3085 	if (rmem->vmem_size && *rmem->vmem) {
3086 		vfree(*rmem->vmem);
3087 		*rmem->vmem = NULL;
3088 	}
3089 }
3090 
bnxt_alloc_ring(struct bnxt * bp,struct bnxt_ring_mem_info * rmem)3091 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3092 {
3093 	struct pci_dev *pdev = bp->pdev;
3094 	u64 valid_bit = 0;
3095 	int i;
3096 
3097 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3098 		valid_bit = PTU_PTE_VALID;
3099 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3100 		size_t pg_tbl_size = rmem->nr_pages * 8;
3101 
3102 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3103 			pg_tbl_size = rmem->page_size;
3104 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3105 						  &rmem->pg_tbl_map,
3106 						  GFP_KERNEL);
3107 		if (!rmem->pg_tbl)
3108 			return -ENOMEM;
3109 	}
3110 
3111 	for (i = 0; i < rmem->nr_pages; i++) {
3112 		u64 extra_bits = valid_bit;
3113 
3114 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3115 						     rmem->page_size,
3116 						     &rmem->dma_arr[i],
3117 						     GFP_KERNEL);
3118 		if (!rmem->pg_arr[i])
3119 			return -ENOMEM;
3120 
3121 		if (rmem->mem_init)
3122 			bnxt_init_ctx_mem(rmem->mem_init, rmem->pg_arr[i],
3123 					  rmem->page_size);
3124 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3125 			if (i == rmem->nr_pages - 2 &&
3126 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3127 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3128 			else if (i == rmem->nr_pages - 1 &&
3129 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3130 				extra_bits |= PTU_PTE_LAST;
3131 			rmem->pg_tbl[i] =
3132 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3133 		}
3134 	}
3135 
3136 	if (rmem->vmem_size) {
3137 		*rmem->vmem = vzalloc(rmem->vmem_size);
3138 		if (!(*rmem->vmem))
3139 			return -ENOMEM;
3140 	}
3141 	return 0;
3142 }
3143 
bnxt_free_tpa_info(struct bnxt * bp)3144 static void bnxt_free_tpa_info(struct bnxt *bp)
3145 {
3146 	int i;
3147 
3148 	for (i = 0; i < bp->rx_nr_rings; i++) {
3149 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3150 
3151 		kfree(rxr->rx_tpa_idx_map);
3152 		rxr->rx_tpa_idx_map = NULL;
3153 		if (rxr->rx_tpa) {
3154 			kfree(rxr->rx_tpa[0].agg_arr);
3155 			rxr->rx_tpa[0].agg_arr = NULL;
3156 		}
3157 		kfree(rxr->rx_tpa);
3158 		rxr->rx_tpa = NULL;
3159 	}
3160 }
3161 
bnxt_alloc_tpa_info(struct bnxt * bp)3162 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3163 {
3164 	int i, j, total_aggs = 0;
3165 
3166 	bp->max_tpa = MAX_TPA;
3167 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
3168 		if (!bp->max_tpa_v2)
3169 			return 0;
3170 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3171 		total_aggs = bp->max_tpa * MAX_SKB_FRAGS;
3172 	}
3173 
3174 	for (i = 0; i < bp->rx_nr_rings; i++) {
3175 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3176 		struct rx_agg_cmp *agg;
3177 
3178 		rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3179 				      GFP_KERNEL);
3180 		if (!rxr->rx_tpa)
3181 			return -ENOMEM;
3182 
3183 		if (!(bp->flags & BNXT_FLAG_CHIP_P5))
3184 			continue;
3185 		agg = kcalloc(total_aggs, sizeof(*agg), GFP_KERNEL);
3186 		rxr->rx_tpa[0].agg_arr = agg;
3187 		if (!agg)
3188 			return -ENOMEM;
3189 		for (j = 1; j < bp->max_tpa; j++)
3190 			rxr->rx_tpa[j].agg_arr = agg + j * MAX_SKB_FRAGS;
3191 		rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3192 					      GFP_KERNEL);
3193 		if (!rxr->rx_tpa_idx_map)
3194 			return -ENOMEM;
3195 	}
3196 	return 0;
3197 }
3198 
bnxt_free_rx_rings(struct bnxt * bp)3199 static void bnxt_free_rx_rings(struct bnxt *bp)
3200 {
3201 	int i;
3202 
3203 	if (!bp->rx_ring)
3204 		return;
3205 
3206 	bnxt_free_tpa_info(bp);
3207 	for (i = 0; i < bp->rx_nr_rings; i++) {
3208 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3209 		struct bnxt_ring_struct *ring;
3210 
3211 		if (rxr->xdp_prog)
3212 			bpf_prog_put(rxr->xdp_prog);
3213 
3214 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3215 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3216 
3217 		page_pool_destroy(rxr->page_pool);
3218 		rxr->page_pool = NULL;
3219 
3220 		kfree(rxr->rx_agg_bmap);
3221 		rxr->rx_agg_bmap = NULL;
3222 
3223 		ring = &rxr->rx_ring_struct;
3224 		bnxt_free_ring(bp, &ring->ring_mem);
3225 
3226 		ring = &rxr->rx_agg_ring_struct;
3227 		bnxt_free_ring(bp, &ring->ring_mem);
3228 	}
3229 }
3230 
bnxt_alloc_rx_page_pool(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)3231 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3232 				   struct bnxt_rx_ring_info *rxr)
3233 {
3234 	struct page_pool_params pp = { 0 };
3235 
3236 	pp.pool_size = bp->rx_ring_size;
3237 	pp.nid = dev_to_node(&bp->pdev->dev);
3238 	pp.dev = &bp->pdev->dev;
3239 	pp.dma_dir = DMA_BIDIRECTIONAL;
3240 
3241 	rxr->page_pool = page_pool_create(&pp);
3242 	if (IS_ERR(rxr->page_pool)) {
3243 		int err = PTR_ERR(rxr->page_pool);
3244 
3245 		rxr->page_pool = NULL;
3246 		return err;
3247 	}
3248 	return 0;
3249 }
3250 
bnxt_alloc_rx_rings(struct bnxt * bp)3251 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3252 {
3253 	int i, rc = 0, agg_rings = 0;
3254 
3255 	if (!bp->rx_ring)
3256 		return -ENOMEM;
3257 
3258 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3259 		agg_rings = 1;
3260 
3261 	for (i = 0; i < bp->rx_nr_rings; i++) {
3262 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3263 		struct bnxt_ring_struct *ring;
3264 
3265 		ring = &rxr->rx_ring_struct;
3266 
3267 		rc = bnxt_alloc_rx_page_pool(bp, rxr);
3268 		if (rc)
3269 			return rc;
3270 
3271 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3272 		if (rc < 0)
3273 			return rc;
3274 
3275 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3276 						MEM_TYPE_PAGE_POOL,
3277 						rxr->page_pool);
3278 		if (rc) {
3279 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3280 			return rc;
3281 		}
3282 
3283 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3284 		if (rc)
3285 			return rc;
3286 
3287 		ring->grp_idx = i;
3288 		if (agg_rings) {
3289 			u16 mem_size;
3290 
3291 			ring = &rxr->rx_agg_ring_struct;
3292 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3293 			if (rc)
3294 				return rc;
3295 
3296 			ring->grp_idx = i;
3297 			rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3298 			mem_size = rxr->rx_agg_bmap_size / 8;
3299 			rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3300 			if (!rxr->rx_agg_bmap)
3301 				return -ENOMEM;
3302 		}
3303 	}
3304 	if (bp->flags & BNXT_FLAG_TPA)
3305 		rc = bnxt_alloc_tpa_info(bp);
3306 	return rc;
3307 }
3308 
bnxt_free_tx_rings(struct bnxt * bp)3309 static void bnxt_free_tx_rings(struct bnxt *bp)
3310 {
3311 	int i;
3312 	struct pci_dev *pdev = bp->pdev;
3313 
3314 	if (!bp->tx_ring)
3315 		return;
3316 
3317 	for (i = 0; i < bp->tx_nr_rings; i++) {
3318 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3319 		struct bnxt_ring_struct *ring;
3320 
3321 		if (txr->tx_push) {
3322 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3323 					  txr->tx_push, txr->tx_push_mapping);
3324 			txr->tx_push = NULL;
3325 		}
3326 
3327 		ring = &txr->tx_ring_struct;
3328 
3329 		bnxt_free_ring(bp, &ring->ring_mem);
3330 	}
3331 }
3332 
bnxt_alloc_tx_rings(struct bnxt * bp)3333 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3334 {
3335 	int i, j, rc;
3336 	struct pci_dev *pdev = bp->pdev;
3337 
3338 	bp->tx_push_size = 0;
3339 	if (bp->tx_push_thresh) {
3340 		int push_size;
3341 
3342 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3343 					bp->tx_push_thresh);
3344 
3345 		if (push_size > 256) {
3346 			push_size = 0;
3347 			bp->tx_push_thresh = 0;
3348 		}
3349 
3350 		bp->tx_push_size = push_size;
3351 	}
3352 
3353 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3354 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3355 		struct bnxt_ring_struct *ring;
3356 		u8 qidx;
3357 
3358 		ring = &txr->tx_ring_struct;
3359 
3360 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3361 		if (rc)
3362 			return rc;
3363 
3364 		ring->grp_idx = txr->bnapi->index;
3365 		if (bp->tx_push_size) {
3366 			dma_addr_t mapping;
3367 
3368 			/* One pre-allocated DMA buffer to backup
3369 			 * TX push operation
3370 			 */
3371 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3372 						bp->tx_push_size,
3373 						&txr->tx_push_mapping,
3374 						GFP_KERNEL);
3375 
3376 			if (!txr->tx_push)
3377 				return -ENOMEM;
3378 
3379 			mapping = txr->tx_push_mapping +
3380 				sizeof(struct tx_push_bd);
3381 			txr->data_mapping = cpu_to_le64(mapping);
3382 		}
3383 		qidx = bp->tc_to_qidx[j];
3384 		ring->queue_id = bp->q_info[qidx].queue_id;
3385 		spin_lock_init(&txr->xdp_tx_lock);
3386 		if (i < bp->tx_nr_rings_xdp)
3387 			continue;
3388 		if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
3389 			j++;
3390 	}
3391 	return 0;
3392 }
3393 
bnxt_free_cp_arrays(struct bnxt_cp_ring_info * cpr)3394 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3395 {
3396 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3397 
3398 	kfree(cpr->cp_desc_ring);
3399 	cpr->cp_desc_ring = NULL;
3400 	ring->ring_mem.pg_arr = NULL;
3401 	kfree(cpr->cp_desc_mapping);
3402 	cpr->cp_desc_mapping = NULL;
3403 	ring->ring_mem.dma_arr = NULL;
3404 }
3405 
bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info * cpr,int n)3406 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3407 {
3408 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3409 	if (!cpr->cp_desc_ring)
3410 		return -ENOMEM;
3411 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3412 				       GFP_KERNEL);
3413 	if (!cpr->cp_desc_mapping)
3414 		return -ENOMEM;
3415 	return 0;
3416 }
3417 
bnxt_free_all_cp_arrays(struct bnxt * bp)3418 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3419 {
3420 	int i;
3421 
3422 	if (!bp->bnapi)
3423 		return;
3424 	for (i = 0; i < bp->cp_nr_rings; i++) {
3425 		struct bnxt_napi *bnapi = bp->bnapi[i];
3426 
3427 		if (!bnapi)
3428 			continue;
3429 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3430 	}
3431 }
3432 
bnxt_alloc_all_cp_arrays(struct bnxt * bp)3433 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3434 {
3435 	int i, n = bp->cp_nr_pages;
3436 
3437 	for (i = 0; i < bp->cp_nr_rings; i++) {
3438 		struct bnxt_napi *bnapi = bp->bnapi[i];
3439 		int rc;
3440 
3441 		if (!bnapi)
3442 			continue;
3443 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3444 		if (rc)
3445 			return rc;
3446 	}
3447 	return 0;
3448 }
3449 
bnxt_free_cp_rings(struct bnxt * bp)3450 static void bnxt_free_cp_rings(struct bnxt *bp)
3451 {
3452 	int i;
3453 
3454 	if (!bp->bnapi)
3455 		return;
3456 
3457 	for (i = 0; i < bp->cp_nr_rings; i++) {
3458 		struct bnxt_napi *bnapi = bp->bnapi[i];
3459 		struct bnxt_cp_ring_info *cpr;
3460 		struct bnxt_ring_struct *ring;
3461 		int j;
3462 
3463 		if (!bnapi)
3464 			continue;
3465 
3466 		cpr = &bnapi->cp_ring;
3467 		ring = &cpr->cp_ring_struct;
3468 
3469 		bnxt_free_ring(bp, &ring->ring_mem);
3470 
3471 		for (j = 0; j < 2; j++) {
3472 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3473 
3474 			if (cpr2) {
3475 				ring = &cpr2->cp_ring_struct;
3476 				bnxt_free_ring(bp, &ring->ring_mem);
3477 				bnxt_free_cp_arrays(cpr2);
3478 				kfree(cpr2);
3479 				cpr->cp_ring_arr[j] = NULL;
3480 			}
3481 		}
3482 	}
3483 }
3484 
bnxt_alloc_cp_sub_ring(struct bnxt * bp)3485 static struct bnxt_cp_ring_info *bnxt_alloc_cp_sub_ring(struct bnxt *bp)
3486 {
3487 	struct bnxt_ring_mem_info *rmem;
3488 	struct bnxt_ring_struct *ring;
3489 	struct bnxt_cp_ring_info *cpr;
3490 	int rc;
3491 
3492 	cpr = kzalloc(sizeof(*cpr), GFP_KERNEL);
3493 	if (!cpr)
3494 		return NULL;
3495 
3496 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
3497 	if (rc) {
3498 		bnxt_free_cp_arrays(cpr);
3499 		kfree(cpr);
3500 		return NULL;
3501 	}
3502 	ring = &cpr->cp_ring_struct;
3503 	rmem = &ring->ring_mem;
3504 	rmem->nr_pages = bp->cp_nr_pages;
3505 	rmem->page_size = HW_CMPD_RING_SIZE;
3506 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
3507 	rmem->dma_arr = cpr->cp_desc_mapping;
3508 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
3509 	rc = bnxt_alloc_ring(bp, rmem);
3510 	if (rc) {
3511 		bnxt_free_ring(bp, rmem);
3512 		bnxt_free_cp_arrays(cpr);
3513 		kfree(cpr);
3514 		cpr = NULL;
3515 	}
3516 	return cpr;
3517 }
3518 
bnxt_alloc_cp_rings(struct bnxt * bp)3519 static int bnxt_alloc_cp_rings(struct bnxt *bp)
3520 {
3521 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
3522 	int i, rc, ulp_base_vec, ulp_msix;
3523 
3524 	ulp_msix = bnxt_get_ulp_msix_num(bp);
3525 	ulp_base_vec = bnxt_get_ulp_msix_base(bp);
3526 	for (i = 0; i < bp->cp_nr_rings; i++) {
3527 		struct bnxt_napi *bnapi = bp->bnapi[i];
3528 		struct bnxt_cp_ring_info *cpr;
3529 		struct bnxt_ring_struct *ring;
3530 
3531 		if (!bnapi)
3532 			continue;
3533 
3534 		cpr = &bnapi->cp_ring;
3535 		cpr->bnapi = bnapi;
3536 		ring = &cpr->cp_ring_struct;
3537 
3538 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3539 		if (rc)
3540 			return rc;
3541 
3542 		if (ulp_msix && i >= ulp_base_vec)
3543 			ring->map_idx = i + ulp_msix;
3544 		else
3545 			ring->map_idx = i;
3546 
3547 		if (!(bp->flags & BNXT_FLAG_CHIP_P5))
3548 			continue;
3549 
3550 		if (i < bp->rx_nr_rings) {
3551 			struct bnxt_cp_ring_info *cpr2 =
3552 				bnxt_alloc_cp_sub_ring(bp);
3553 
3554 			cpr->cp_ring_arr[BNXT_RX_HDL] = cpr2;
3555 			if (!cpr2)
3556 				return -ENOMEM;
3557 			cpr2->bnapi = bnapi;
3558 		}
3559 		if ((sh && i < bp->tx_nr_rings) ||
3560 		    (!sh && i >= bp->rx_nr_rings)) {
3561 			struct bnxt_cp_ring_info *cpr2 =
3562 				bnxt_alloc_cp_sub_ring(bp);
3563 
3564 			cpr->cp_ring_arr[BNXT_TX_HDL] = cpr2;
3565 			if (!cpr2)
3566 				return -ENOMEM;
3567 			cpr2->bnapi = bnapi;
3568 		}
3569 	}
3570 	return 0;
3571 }
3572 
bnxt_init_ring_struct(struct bnxt * bp)3573 static void bnxt_init_ring_struct(struct bnxt *bp)
3574 {
3575 	int i;
3576 
3577 	for (i = 0; i < bp->cp_nr_rings; i++) {
3578 		struct bnxt_napi *bnapi = bp->bnapi[i];
3579 		struct bnxt_ring_mem_info *rmem;
3580 		struct bnxt_cp_ring_info *cpr;
3581 		struct bnxt_rx_ring_info *rxr;
3582 		struct bnxt_tx_ring_info *txr;
3583 		struct bnxt_ring_struct *ring;
3584 
3585 		if (!bnapi)
3586 			continue;
3587 
3588 		cpr = &bnapi->cp_ring;
3589 		ring = &cpr->cp_ring_struct;
3590 		rmem = &ring->ring_mem;
3591 		rmem->nr_pages = bp->cp_nr_pages;
3592 		rmem->page_size = HW_CMPD_RING_SIZE;
3593 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
3594 		rmem->dma_arr = cpr->cp_desc_mapping;
3595 		rmem->vmem_size = 0;
3596 
3597 		rxr = bnapi->rx_ring;
3598 		if (!rxr)
3599 			goto skip_rx;
3600 
3601 		ring = &rxr->rx_ring_struct;
3602 		rmem = &ring->ring_mem;
3603 		rmem->nr_pages = bp->rx_nr_pages;
3604 		rmem->page_size = HW_RXBD_RING_SIZE;
3605 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
3606 		rmem->dma_arr = rxr->rx_desc_mapping;
3607 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
3608 		rmem->vmem = (void **)&rxr->rx_buf_ring;
3609 
3610 		ring = &rxr->rx_agg_ring_struct;
3611 		rmem = &ring->ring_mem;
3612 		rmem->nr_pages = bp->rx_agg_nr_pages;
3613 		rmem->page_size = HW_RXBD_RING_SIZE;
3614 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
3615 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
3616 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
3617 		rmem->vmem = (void **)&rxr->rx_agg_ring;
3618 
3619 skip_rx:
3620 		txr = bnapi->tx_ring;
3621 		if (!txr)
3622 			continue;
3623 
3624 		ring = &txr->tx_ring_struct;
3625 		rmem = &ring->ring_mem;
3626 		rmem->nr_pages = bp->tx_nr_pages;
3627 		rmem->page_size = HW_RXBD_RING_SIZE;
3628 		rmem->pg_arr = (void **)txr->tx_desc_ring;
3629 		rmem->dma_arr = txr->tx_desc_mapping;
3630 		rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
3631 		rmem->vmem = (void **)&txr->tx_buf_ring;
3632 	}
3633 }
3634 
bnxt_init_rxbd_pages(struct bnxt_ring_struct * ring,u32 type)3635 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
3636 {
3637 	int i;
3638 	u32 prod;
3639 	struct rx_bd **rx_buf_ring;
3640 
3641 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
3642 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
3643 		int j;
3644 		struct rx_bd *rxbd;
3645 
3646 		rxbd = rx_buf_ring[i];
3647 		if (!rxbd)
3648 			continue;
3649 
3650 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
3651 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
3652 			rxbd->rx_bd_opaque = prod;
3653 		}
3654 	}
3655 }
3656 
bnxt_alloc_one_rx_ring(struct bnxt * bp,int ring_nr)3657 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
3658 {
3659 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
3660 	struct net_device *dev = bp->dev;
3661 	u32 prod;
3662 	int i;
3663 
3664 	prod = rxr->rx_prod;
3665 	for (i = 0; i < bp->rx_ring_size; i++) {
3666 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
3667 			netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
3668 				    ring_nr, i, bp->rx_ring_size);
3669 			break;
3670 		}
3671 		prod = NEXT_RX(prod);
3672 	}
3673 	rxr->rx_prod = prod;
3674 
3675 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
3676 		return 0;
3677 
3678 	prod = rxr->rx_agg_prod;
3679 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
3680 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
3681 			netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
3682 				    ring_nr, i, bp->rx_ring_size);
3683 			break;
3684 		}
3685 		prod = NEXT_RX_AGG(prod);
3686 	}
3687 	rxr->rx_agg_prod = prod;
3688 
3689 	if (rxr->rx_tpa) {
3690 		dma_addr_t mapping;
3691 		u8 *data;
3692 
3693 		for (i = 0; i < bp->max_tpa; i++) {
3694 			data = __bnxt_alloc_rx_frag(bp, &mapping, GFP_KERNEL);
3695 			if (!data)
3696 				return -ENOMEM;
3697 
3698 			rxr->rx_tpa[i].data = data;
3699 			rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
3700 			rxr->rx_tpa[i].mapping = mapping;
3701 		}
3702 	}
3703 	return 0;
3704 }
3705 
bnxt_init_one_rx_ring(struct bnxt * bp,int ring_nr)3706 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
3707 {
3708 	struct bnxt_rx_ring_info *rxr;
3709 	struct bnxt_ring_struct *ring;
3710 	u32 type;
3711 
3712 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
3713 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
3714 
3715 	if (NET_IP_ALIGN == 2)
3716 		type |= RX_BD_FLAGS_SOP;
3717 
3718 	rxr = &bp->rx_ring[ring_nr];
3719 	ring = &rxr->rx_ring_struct;
3720 	bnxt_init_rxbd_pages(ring, type);
3721 
3722 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
3723 		bpf_prog_add(bp->xdp_prog, 1);
3724 		rxr->xdp_prog = bp->xdp_prog;
3725 	}
3726 	ring->fw_ring_id = INVALID_HW_RING_ID;
3727 
3728 	ring = &rxr->rx_agg_ring_struct;
3729 	ring->fw_ring_id = INVALID_HW_RING_ID;
3730 
3731 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
3732 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
3733 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
3734 
3735 		bnxt_init_rxbd_pages(ring, type);
3736 	}
3737 
3738 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
3739 }
3740 
bnxt_init_cp_rings(struct bnxt * bp)3741 static void bnxt_init_cp_rings(struct bnxt *bp)
3742 {
3743 	int i, j;
3744 
3745 	for (i = 0; i < bp->cp_nr_rings; i++) {
3746 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
3747 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3748 
3749 		ring->fw_ring_id = INVALID_HW_RING_ID;
3750 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3751 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3752 		for (j = 0; j < 2; j++) {
3753 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3754 
3755 			if (!cpr2)
3756 				continue;
3757 
3758 			ring = &cpr2->cp_ring_struct;
3759 			ring->fw_ring_id = INVALID_HW_RING_ID;
3760 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3761 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3762 		}
3763 	}
3764 }
3765 
bnxt_init_rx_rings(struct bnxt * bp)3766 static int bnxt_init_rx_rings(struct bnxt *bp)
3767 {
3768 	int i, rc = 0;
3769 
3770 	if (BNXT_RX_PAGE_MODE(bp)) {
3771 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
3772 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
3773 	} else {
3774 		bp->rx_offset = BNXT_RX_OFFSET;
3775 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
3776 	}
3777 
3778 	for (i = 0; i < bp->rx_nr_rings; i++) {
3779 		rc = bnxt_init_one_rx_ring(bp, i);
3780 		if (rc)
3781 			break;
3782 	}
3783 
3784 	return rc;
3785 }
3786 
bnxt_init_tx_rings(struct bnxt * bp)3787 static int bnxt_init_tx_rings(struct bnxt *bp)
3788 {
3789 	u16 i;
3790 
3791 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
3792 				   BNXT_MIN_TX_DESC_CNT);
3793 
3794 	for (i = 0; i < bp->tx_nr_rings; i++) {
3795 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3796 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
3797 
3798 		ring->fw_ring_id = INVALID_HW_RING_ID;
3799 	}
3800 
3801 	return 0;
3802 }
3803 
bnxt_free_ring_grps(struct bnxt * bp)3804 static void bnxt_free_ring_grps(struct bnxt *bp)
3805 {
3806 	kfree(bp->grp_info);
3807 	bp->grp_info = NULL;
3808 }
3809 
bnxt_init_ring_grps(struct bnxt * bp,bool irq_re_init)3810 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
3811 {
3812 	int i;
3813 
3814 	if (irq_re_init) {
3815 		bp->grp_info = kcalloc(bp->cp_nr_rings,
3816 				       sizeof(struct bnxt_ring_grp_info),
3817 				       GFP_KERNEL);
3818 		if (!bp->grp_info)
3819 			return -ENOMEM;
3820 	}
3821 	for (i = 0; i < bp->cp_nr_rings; i++) {
3822 		if (irq_re_init)
3823 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
3824 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3825 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
3826 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
3827 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
3828 	}
3829 	return 0;
3830 }
3831 
bnxt_free_vnics(struct bnxt * bp)3832 static void bnxt_free_vnics(struct bnxt *bp)
3833 {
3834 	kfree(bp->vnic_info);
3835 	bp->vnic_info = NULL;
3836 	bp->nr_vnics = 0;
3837 }
3838 
bnxt_alloc_vnics(struct bnxt * bp)3839 static int bnxt_alloc_vnics(struct bnxt *bp)
3840 {
3841 	int num_vnics = 1;
3842 
3843 #ifdef CONFIG_RFS_ACCEL
3844 	if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
3845 		num_vnics += bp->rx_nr_rings;
3846 #endif
3847 
3848 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3849 		num_vnics++;
3850 
3851 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
3852 				GFP_KERNEL);
3853 	if (!bp->vnic_info)
3854 		return -ENOMEM;
3855 
3856 	bp->nr_vnics = num_vnics;
3857 	return 0;
3858 }
3859 
bnxt_init_vnics(struct bnxt * bp)3860 static void bnxt_init_vnics(struct bnxt *bp)
3861 {
3862 	int i;
3863 
3864 	for (i = 0; i < bp->nr_vnics; i++) {
3865 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3866 		int j;
3867 
3868 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
3869 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
3870 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
3871 
3872 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
3873 
3874 		if (bp->vnic_info[i].rss_hash_key) {
3875 			if (i == 0)
3876 				get_random_bytes(vnic->rss_hash_key,
3877 					      HW_HASH_KEY_SIZE);
3878 			else
3879 				memcpy(vnic->rss_hash_key,
3880 				       bp->vnic_info[0].rss_hash_key,
3881 				       HW_HASH_KEY_SIZE);
3882 		}
3883 	}
3884 }
3885 
bnxt_calc_nr_ring_pages(u32 ring_size,int desc_per_pg)3886 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
3887 {
3888 	int pages;
3889 
3890 	pages = ring_size / desc_per_pg;
3891 
3892 	if (!pages)
3893 		return 1;
3894 
3895 	pages++;
3896 
3897 	while (pages & (pages - 1))
3898 		pages++;
3899 
3900 	return pages;
3901 }
3902 
bnxt_set_tpa_flags(struct bnxt * bp)3903 void bnxt_set_tpa_flags(struct bnxt *bp)
3904 {
3905 	bp->flags &= ~BNXT_FLAG_TPA;
3906 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
3907 		return;
3908 	if (bp->dev->features & NETIF_F_LRO)
3909 		bp->flags |= BNXT_FLAG_LRO;
3910 	else if (bp->dev->features & NETIF_F_GRO_HW)
3911 		bp->flags |= BNXT_FLAG_GRO;
3912 }
3913 
3914 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
3915  * be set on entry.
3916  */
bnxt_set_ring_params(struct bnxt * bp)3917 void bnxt_set_ring_params(struct bnxt *bp)
3918 {
3919 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
3920 	u32 agg_factor = 0, agg_ring_size = 0;
3921 
3922 	/* 8 for CRC and VLAN */
3923 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
3924 
3925 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
3926 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3927 
3928 	bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
3929 	ring_size = bp->rx_ring_size;
3930 	bp->rx_agg_ring_size = 0;
3931 	bp->rx_agg_nr_pages = 0;
3932 
3933 	if (bp->flags & BNXT_FLAG_TPA)
3934 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
3935 
3936 	bp->flags &= ~BNXT_FLAG_JUMBO;
3937 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
3938 		u32 jumbo_factor;
3939 
3940 		bp->flags |= BNXT_FLAG_JUMBO;
3941 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
3942 		if (jumbo_factor > agg_factor)
3943 			agg_factor = jumbo_factor;
3944 	}
3945 	if (agg_factor) {
3946 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
3947 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
3948 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
3949 				    bp->rx_ring_size, ring_size);
3950 			bp->rx_ring_size = ring_size;
3951 		}
3952 		agg_ring_size = ring_size * agg_factor;
3953 
3954 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
3955 							RX_DESC_CNT);
3956 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
3957 			u32 tmp = agg_ring_size;
3958 
3959 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
3960 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
3961 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
3962 				    tmp, agg_ring_size);
3963 		}
3964 		bp->rx_agg_ring_size = agg_ring_size;
3965 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
3966 
3967 		if (BNXT_RX_PAGE_MODE(bp)) {
3968 			rx_space = PAGE_SIZE;
3969 			rx_size = PAGE_SIZE -
3970 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
3971 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3972 		} else {
3973 			rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
3974 			rx_space = rx_size + NET_SKB_PAD +
3975 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3976 		}
3977 	}
3978 
3979 	bp->rx_buf_use_size = rx_size;
3980 	bp->rx_buf_size = rx_space;
3981 
3982 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
3983 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
3984 
3985 	ring_size = bp->tx_ring_size;
3986 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
3987 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
3988 
3989 	max_rx_cmpl = bp->rx_ring_size;
3990 	/* MAX TPA needs to be added because TPA_START completions are
3991 	 * immediately recycled, so the TPA completions are not bound by
3992 	 * the RX ring size.
3993 	 */
3994 	if (bp->flags & BNXT_FLAG_TPA)
3995 		max_rx_cmpl += bp->max_tpa;
3996 	/* RX and TPA completions are 32-byte, all others are 16-byte */
3997 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
3998 	bp->cp_ring_size = ring_size;
3999 
4000 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4001 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4002 		bp->cp_nr_pages = MAX_CP_PAGES;
4003 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4004 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4005 			    ring_size, bp->cp_ring_size);
4006 	}
4007 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4008 	bp->cp_ring_mask = bp->cp_bit - 1;
4009 }
4010 
4011 /* Changing allocation mode of RX rings.
4012  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4013  */
bnxt_set_rx_skb_mode(struct bnxt * bp,bool page_mode)4014 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4015 {
4016 	if (page_mode) {
4017 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
4018 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4019 
4020 		if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4021 			bp->flags |= BNXT_FLAG_JUMBO;
4022 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4023 			bp->dev->max_mtu =
4024 				min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4025 		} else {
4026 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4027 			bp->rx_skb_func = bnxt_rx_page_skb;
4028 			bp->dev->max_mtu =
4029 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4030 		}
4031 		bp->rx_dir = DMA_BIDIRECTIONAL;
4032 		/* Disable LRO or GRO_HW */
4033 		netdev_update_features(bp->dev);
4034 	} else {
4035 		bp->dev->max_mtu = bp->max_mtu;
4036 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4037 		bp->rx_dir = DMA_FROM_DEVICE;
4038 		bp->rx_skb_func = bnxt_rx_skb;
4039 	}
4040 	return 0;
4041 }
4042 
bnxt_free_vnic_attributes(struct bnxt * bp)4043 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4044 {
4045 	int i;
4046 	struct bnxt_vnic_info *vnic;
4047 	struct pci_dev *pdev = bp->pdev;
4048 
4049 	if (!bp->vnic_info)
4050 		return;
4051 
4052 	for (i = 0; i < bp->nr_vnics; i++) {
4053 		vnic = &bp->vnic_info[i];
4054 
4055 		kfree(vnic->fw_grp_ids);
4056 		vnic->fw_grp_ids = NULL;
4057 
4058 		kfree(vnic->uc_list);
4059 		vnic->uc_list = NULL;
4060 
4061 		if (vnic->mc_list) {
4062 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4063 					  vnic->mc_list, vnic->mc_list_mapping);
4064 			vnic->mc_list = NULL;
4065 		}
4066 
4067 		if (vnic->rss_table) {
4068 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4069 					  vnic->rss_table,
4070 					  vnic->rss_table_dma_addr);
4071 			vnic->rss_table = NULL;
4072 		}
4073 
4074 		vnic->rss_hash_key = NULL;
4075 		vnic->flags = 0;
4076 	}
4077 }
4078 
bnxt_alloc_vnic_attributes(struct bnxt * bp)4079 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4080 {
4081 	int i, rc = 0, size;
4082 	struct bnxt_vnic_info *vnic;
4083 	struct pci_dev *pdev = bp->pdev;
4084 	int max_rings;
4085 
4086 	for (i = 0; i < bp->nr_vnics; i++) {
4087 		vnic = &bp->vnic_info[i];
4088 
4089 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4090 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4091 
4092 			if (mem_size > 0) {
4093 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4094 				if (!vnic->uc_list) {
4095 					rc = -ENOMEM;
4096 					goto out;
4097 				}
4098 			}
4099 		}
4100 
4101 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4102 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4103 			vnic->mc_list =
4104 				dma_alloc_coherent(&pdev->dev,
4105 						   vnic->mc_list_size,
4106 						   &vnic->mc_list_mapping,
4107 						   GFP_KERNEL);
4108 			if (!vnic->mc_list) {
4109 				rc = -ENOMEM;
4110 				goto out;
4111 			}
4112 		}
4113 
4114 		if (bp->flags & BNXT_FLAG_CHIP_P5)
4115 			goto vnic_skip_grps;
4116 
4117 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4118 			max_rings = bp->rx_nr_rings;
4119 		else
4120 			max_rings = 1;
4121 
4122 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4123 		if (!vnic->fw_grp_ids) {
4124 			rc = -ENOMEM;
4125 			goto out;
4126 		}
4127 vnic_skip_grps:
4128 		if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
4129 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4130 			continue;
4131 
4132 		/* Allocate rss table and hash key */
4133 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4134 		if (bp->flags & BNXT_FLAG_CHIP_P5)
4135 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4136 
4137 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4138 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4139 						     vnic->rss_table_size,
4140 						     &vnic->rss_table_dma_addr,
4141 						     GFP_KERNEL);
4142 		if (!vnic->rss_table) {
4143 			rc = -ENOMEM;
4144 			goto out;
4145 		}
4146 
4147 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4148 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4149 	}
4150 	return 0;
4151 
4152 out:
4153 	return rc;
4154 }
4155 
bnxt_free_hwrm_resources(struct bnxt * bp)4156 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4157 {
4158 	struct bnxt_hwrm_wait_token *token;
4159 
4160 	dma_pool_destroy(bp->hwrm_dma_pool);
4161 	bp->hwrm_dma_pool = NULL;
4162 
4163 	rcu_read_lock();
4164 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4165 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4166 	rcu_read_unlock();
4167 }
4168 
bnxt_alloc_hwrm_resources(struct bnxt * bp)4169 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4170 {
4171 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4172 					    BNXT_HWRM_DMA_SIZE,
4173 					    BNXT_HWRM_DMA_ALIGN, 0);
4174 	if (!bp->hwrm_dma_pool)
4175 		return -ENOMEM;
4176 
4177 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4178 
4179 	return 0;
4180 }
4181 
bnxt_free_stats_mem(struct bnxt * bp,struct bnxt_stats_mem * stats)4182 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4183 {
4184 	kfree(stats->hw_masks);
4185 	stats->hw_masks = NULL;
4186 	kfree(stats->sw_stats);
4187 	stats->sw_stats = NULL;
4188 	if (stats->hw_stats) {
4189 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4190 				  stats->hw_stats_map);
4191 		stats->hw_stats = NULL;
4192 	}
4193 }
4194 
bnxt_alloc_stats_mem(struct bnxt * bp,struct bnxt_stats_mem * stats,bool alloc_masks)4195 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4196 				bool alloc_masks)
4197 {
4198 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4199 					     &stats->hw_stats_map, GFP_KERNEL);
4200 	if (!stats->hw_stats)
4201 		return -ENOMEM;
4202 
4203 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4204 	if (!stats->sw_stats)
4205 		goto stats_mem_err;
4206 
4207 	if (alloc_masks) {
4208 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4209 		if (!stats->hw_masks)
4210 			goto stats_mem_err;
4211 	}
4212 	return 0;
4213 
4214 stats_mem_err:
4215 	bnxt_free_stats_mem(bp, stats);
4216 	return -ENOMEM;
4217 }
4218 
bnxt_fill_masks(u64 * mask_arr,u64 mask,int count)4219 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4220 {
4221 	int i;
4222 
4223 	for (i = 0; i < count; i++)
4224 		mask_arr[i] = mask;
4225 }
4226 
bnxt_copy_hw_masks(u64 * mask_arr,__le64 * hw_mask_arr,int count)4227 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4228 {
4229 	int i;
4230 
4231 	for (i = 0; i < count; i++)
4232 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4233 }
4234 
bnxt_hwrm_func_qstat_ext(struct bnxt * bp,struct bnxt_stats_mem * stats)4235 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4236 				    struct bnxt_stats_mem *stats)
4237 {
4238 	struct hwrm_func_qstats_ext_output *resp;
4239 	struct hwrm_func_qstats_ext_input *req;
4240 	__le64 *hw_masks;
4241 	int rc;
4242 
4243 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4244 	    !(bp->flags & BNXT_FLAG_CHIP_P5))
4245 		return -EOPNOTSUPP;
4246 
4247 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4248 	if (rc)
4249 		return rc;
4250 
4251 	req->fid = cpu_to_le16(0xffff);
4252 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4253 
4254 	resp = hwrm_req_hold(bp, req);
4255 	rc = hwrm_req_send(bp, req);
4256 	if (!rc) {
4257 		hw_masks = &resp->rx_ucast_pkts;
4258 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4259 	}
4260 	hwrm_req_drop(bp, req);
4261 	return rc;
4262 }
4263 
4264 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4265 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4266 
bnxt_init_stats(struct bnxt * bp)4267 static void bnxt_init_stats(struct bnxt *bp)
4268 {
4269 	struct bnxt_napi *bnapi = bp->bnapi[0];
4270 	struct bnxt_cp_ring_info *cpr;
4271 	struct bnxt_stats_mem *stats;
4272 	__le64 *rx_stats, *tx_stats;
4273 	int rc, rx_count, tx_count;
4274 	u64 *rx_masks, *tx_masks;
4275 	u64 mask;
4276 	u8 flags;
4277 
4278 	cpr = &bnapi->cp_ring;
4279 	stats = &cpr->stats;
4280 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4281 	if (rc) {
4282 		if (bp->flags & BNXT_FLAG_CHIP_P5)
4283 			mask = (1ULL << 48) - 1;
4284 		else
4285 			mask = -1ULL;
4286 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
4287 	}
4288 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
4289 		stats = &bp->port_stats;
4290 		rx_stats = stats->hw_stats;
4291 		rx_masks = stats->hw_masks;
4292 		rx_count = sizeof(struct rx_port_stats) / 8;
4293 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4294 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4295 		tx_count = sizeof(struct tx_port_stats) / 8;
4296 
4297 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
4298 		rc = bnxt_hwrm_port_qstats(bp, flags);
4299 		if (rc) {
4300 			mask = (1ULL << 40) - 1;
4301 
4302 			bnxt_fill_masks(rx_masks, mask, rx_count);
4303 			bnxt_fill_masks(tx_masks, mask, tx_count);
4304 		} else {
4305 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4306 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
4307 			bnxt_hwrm_port_qstats(bp, 0);
4308 		}
4309 	}
4310 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
4311 		stats = &bp->rx_port_stats_ext;
4312 		rx_stats = stats->hw_stats;
4313 		rx_masks = stats->hw_masks;
4314 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
4315 		stats = &bp->tx_port_stats_ext;
4316 		tx_stats = stats->hw_stats;
4317 		tx_masks = stats->hw_masks;
4318 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
4319 
4320 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4321 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
4322 		if (rc) {
4323 			mask = (1ULL << 40) - 1;
4324 
4325 			bnxt_fill_masks(rx_masks, mask, rx_count);
4326 			if (tx_stats)
4327 				bnxt_fill_masks(tx_masks, mask, tx_count);
4328 		} else {
4329 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4330 			if (tx_stats)
4331 				bnxt_copy_hw_masks(tx_masks, tx_stats,
4332 						   tx_count);
4333 			bnxt_hwrm_port_qstats_ext(bp, 0);
4334 		}
4335 	}
4336 }
4337 
bnxt_free_port_stats(struct bnxt * bp)4338 static void bnxt_free_port_stats(struct bnxt *bp)
4339 {
4340 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
4341 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
4342 
4343 	bnxt_free_stats_mem(bp, &bp->port_stats);
4344 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
4345 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
4346 }
4347 
bnxt_free_ring_stats(struct bnxt * bp)4348 static void bnxt_free_ring_stats(struct bnxt *bp)
4349 {
4350 	int i;
4351 
4352 	if (!bp->bnapi)
4353 		return;
4354 
4355 	for (i = 0; i < bp->cp_nr_rings; i++) {
4356 		struct bnxt_napi *bnapi = bp->bnapi[i];
4357 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4358 
4359 		bnxt_free_stats_mem(bp, &cpr->stats);
4360 	}
4361 }
4362 
bnxt_alloc_stats(struct bnxt * bp)4363 static int bnxt_alloc_stats(struct bnxt *bp)
4364 {
4365 	u32 size, i;
4366 	int rc;
4367 
4368 	size = bp->hw_ring_stats_size;
4369 
4370 	for (i = 0; i < bp->cp_nr_rings; i++) {
4371 		struct bnxt_napi *bnapi = bp->bnapi[i];
4372 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4373 
4374 		cpr->stats.len = size;
4375 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
4376 		if (rc)
4377 			return rc;
4378 
4379 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4380 	}
4381 
4382 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
4383 		return 0;
4384 
4385 	if (bp->port_stats.hw_stats)
4386 		goto alloc_ext_stats;
4387 
4388 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
4389 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
4390 	if (rc)
4391 		return rc;
4392 
4393 	bp->flags |= BNXT_FLAG_PORT_STATS;
4394 
4395 alloc_ext_stats:
4396 	/* Display extended statistics only if FW supports it */
4397 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
4398 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
4399 			return 0;
4400 
4401 	if (bp->rx_port_stats_ext.hw_stats)
4402 		goto alloc_tx_ext_stats;
4403 
4404 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
4405 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
4406 	/* Extended stats are optional */
4407 	if (rc)
4408 		return 0;
4409 
4410 alloc_tx_ext_stats:
4411 	if (bp->tx_port_stats_ext.hw_stats)
4412 		return 0;
4413 
4414 	if (bp->hwrm_spec_code >= 0x10902 ||
4415 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
4416 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
4417 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
4418 		/* Extended stats are optional */
4419 		if (rc)
4420 			return 0;
4421 	}
4422 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
4423 	return 0;
4424 }
4425 
bnxt_clear_ring_indices(struct bnxt * bp)4426 static void bnxt_clear_ring_indices(struct bnxt *bp)
4427 {
4428 	int i;
4429 
4430 	if (!bp->bnapi)
4431 		return;
4432 
4433 	for (i = 0; i < bp->cp_nr_rings; i++) {
4434 		struct bnxt_napi *bnapi = bp->bnapi[i];
4435 		struct bnxt_cp_ring_info *cpr;
4436 		struct bnxt_rx_ring_info *rxr;
4437 		struct bnxt_tx_ring_info *txr;
4438 
4439 		if (!bnapi)
4440 			continue;
4441 
4442 		cpr = &bnapi->cp_ring;
4443 		cpr->cp_raw_cons = 0;
4444 
4445 		txr = bnapi->tx_ring;
4446 		if (txr) {
4447 			txr->tx_prod = 0;
4448 			txr->tx_cons = 0;
4449 		}
4450 
4451 		rxr = bnapi->rx_ring;
4452 		if (rxr) {
4453 			rxr->rx_prod = 0;
4454 			rxr->rx_agg_prod = 0;
4455 			rxr->rx_sw_agg_prod = 0;
4456 			rxr->rx_next_cons = 0;
4457 		}
4458 	}
4459 }
4460 
bnxt_free_ntp_fltrs(struct bnxt * bp,bool irq_reinit)4461 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
4462 {
4463 #ifdef CONFIG_RFS_ACCEL
4464 	int i;
4465 
4466 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
4467 	 * safe to delete the hash table.
4468 	 */
4469 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
4470 		struct hlist_head *head;
4471 		struct hlist_node *tmp;
4472 		struct bnxt_ntuple_filter *fltr;
4473 
4474 		head = &bp->ntp_fltr_hash_tbl[i];
4475 		hlist_for_each_entry_safe(fltr, tmp, head, hash) {
4476 			hlist_del(&fltr->hash);
4477 			kfree(fltr);
4478 		}
4479 	}
4480 	if (irq_reinit) {
4481 		bitmap_free(bp->ntp_fltr_bmap);
4482 		bp->ntp_fltr_bmap = NULL;
4483 	}
4484 	bp->ntp_fltr_count = 0;
4485 #endif
4486 }
4487 
bnxt_alloc_ntp_fltrs(struct bnxt * bp)4488 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
4489 {
4490 #ifdef CONFIG_RFS_ACCEL
4491 	int i, rc = 0;
4492 
4493 	if (!(bp->flags & BNXT_FLAG_RFS))
4494 		return 0;
4495 
4496 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
4497 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
4498 
4499 	bp->ntp_fltr_count = 0;
4500 	bp->ntp_fltr_bmap = bitmap_zalloc(BNXT_NTP_FLTR_MAX_FLTR, GFP_KERNEL);
4501 
4502 	if (!bp->ntp_fltr_bmap)
4503 		rc = -ENOMEM;
4504 
4505 	return rc;
4506 #else
4507 	return 0;
4508 #endif
4509 }
4510 
bnxt_free_mem(struct bnxt * bp,bool irq_re_init)4511 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
4512 {
4513 	bnxt_free_vnic_attributes(bp);
4514 	bnxt_free_tx_rings(bp);
4515 	bnxt_free_rx_rings(bp);
4516 	bnxt_free_cp_rings(bp);
4517 	bnxt_free_all_cp_arrays(bp);
4518 	bnxt_free_ntp_fltrs(bp, irq_re_init);
4519 	if (irq_re_init) {
4520 		bnxt_free_ring_stats(bp);
4521 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
4522 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
4523 			bnxt_free_port_stats(bp);
4524 		bnxt_free_ring_grps(bp);
4525 		bnxt_free_vnics(bp);
4526 		kfree(bp->tx_ring_map);
4527 		bp->tx_ring_map = NULL;
4528 		kfree(bp->tx_ring);
4529 		bp->tx_ring = NULL;
4530 		kfree(bp->rx_ring);
4531 		bp->rx_ring = NULL;
4532 		kfree(bp->bnapi);
4533 		bp->bnapi = NULL;
4534 	} else {
4535 		bnxt_clear_ring_indices(bp);
4536 	}
4537 }
4538 
bnxt_alloc_mem(struct bnxt * bp,bool irq_re_init)4539 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
4540 {
4541 	int i, j, rc, size, arr_size;
4542 	void *bnapi;
4543 
4544 	if (irq_re_init) {
4545 		/* Allocate bnapi mem pointer array and mem block for
4546 		 * all queues
4547 		 */
4548 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
4549 				bp->cp_nr_rings);
4550 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
4551 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
4552 		if (!bnapi)
4553 			return -ENOMEM;
4554 
4555 		bp->bnapi = bnapi;
4556 		bnapi += arr_size;
4557 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
4558 			bp->bnapi[i] = bnapi;
4559 			bp->bnapi[i]->index = i;
4560 			bp->bnapi[i]->bp = bp;
4561 			if (bp->flags & BNXT_FLAG_CHIP_P5) {
4562 				struct bnxt_cp_ring_info *cpr =
4563 					&bp->bnapi[i]->cp_ring;
4564 
4565 				cpr->cp_ring_struct.ring_mem.flags =
4566 					BNXT_RMEM_RING_PTE_FLAG;
4567 			}
4568 		}
4569 
4570 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
4571 				      sizeof(struct bnxt_rx_ring_info),
4572 				      GFP_KERNEL);
4573 		if (!bp->rx_ring)
4574 			return -ENOMEM;
4575 
4576 		for (i = 0; i < bp->rx_nr_rings; i++) {
4577 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4578 
4579 			if (bp->flags & BNXT_FLAG_CHIP_P5) {
4580 				rxr->rx_ring_struct.ring_mem.flags =
4581 					BNXT_RMEM_RING_PTE_FLAG;
4582 				rxr->rx_agg_ring_struct.ring_mem.flags =
4583 					BNXT_RMEM_RING_PTE_FLAG;
4584 			}
4585 			rxr->bnapi = bp->bnapi[i];
4586 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
4587 		}
4588 
4589 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
4590 				      sizeof(struct bnxt_tx_ring_info),
4591 				      GFP_KERNEL);
4592 		if (!bp->tx_ring)
4593 			return -ENOMEM;
4594 
4595 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
4596 					  GFP_KERNEL);
4597 
4598 		if (!bp->tx_ring_map)
4599 			return -ENOMEM;
4600 
4601 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4602 			j = 0;
4603 		else
4604 			j = bp->rx_nr_rings;
4605 
4606 		for (i = 0; i < bp->tx_nr_rings; i++, j++) {
4607 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4608 
4609 			if (bp->flags & BNXT_FLAG_CHIP_P5)
4610 				txr->tx_ring_struct.ring_mem.flags =
4611 					BNXT_RMEM_RING_PTE_FLAG;
4612 			txr->bnapi = bp->bnapi[j];
4613 			bp->bnapi[j]->tx_ring = txr;
4614 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
4615 			if (i >= bp->tx_nr_rings_xdp) {
4616 				txr->txq_index = i - bp->tx_nr_rings_xdp;
4617 				bp->bnapi[j]->tx_int = bnxt_tx_int;
4618 			} else {
4619 				bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
4620 				bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
4621 			}
4622 		}
4623 
4624 		rc = bnxt_alloc_stats(bp);
4625 		if (rc)
4626 			goto alloc_mem_err;
4627 		bnxt_init_stats(bp);
4628 
4629 		rc = bnxt_alloc_ntp_fltrs(bp);
4630 		if (rc)
4631 			goto alloc_mem_err;
4632 
4633 		rc = bnxt_alloc_vnics(bp);
4634 		if (rc)
4635 			goto alloc_mem_err;
4636 	}
4637 
4638 	rc = bnxt_alloc_all_cp_arrays(bp);
4639 	if (rc)
4640 		goto alloc_mem_err;
4641 
4642 	bnxt_init_ring_struct(bp);
4643 
4644 	rc = bnxt_alloc_rx_rings(bp);
4645 	if (rc)
4646 		goto alloc_mem_err;
4647 
4648 	rc = bnxt_alloc_tx_rings(bp);
4649 	if (rc)
4650 		goto alloc_mem_err;
4651 
4652 	rc = bnxt_alloc_cp_rings(bp);
4653 	if (rc)
4654 		goto alloc_mem_err;
4655 
4656 	bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
4657 				  BNXT_VNIC_UCAST_FLAG;
4658 	rc = bnxt_alloc_vnic_attributes(bp);
4659 	if (rc)
4660 		goto alloc_mem_err;
4661 	return 0;
4662 
4663 alloc_mem_err:
4664 	bnxt_free_mem(bp, true);
4665 	return rc;
4666 }
4667 
bnxt_disable_int(struct bnxt * bp)4668 static void bnxt_disable_int(struct bnxt *bp)
4669 {
4670 	int i;
4671 
4672 	if (!bp->bnapi)
4673 		return;
4674 
4675 	for (i = 0; i < bp->cp_nr_rings; i++) {
4676 		struct bnxt_napi *bnapi = bp->bnapi[i];
4677 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4678 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4679 
4680 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
4681 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
4682 	}
4683 }
4684 
bnxt_cp_num_to_irq_num(struct bnxt * bp,int n)4685 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
4686 {
4687 	struct bnxt_napi *bnapi = bp->bnapi[n];
4688 	struct bnxt_cp_ring_info *cpr;
4689 
4690 	cpr = &bnapi->cp_ring;
4691 	return cpr->cp_ring_struct.map_idx;
4692 }
4693 
bnxt_disable_int_sync(struct bnxt * bp)4694 static void bnxt_disable_int_sync(struct bnxt *bp)
4695 {
4696 	int i;
4697 
4698 	if (!bp->irq_tbl)
4699 		return;
4700 
4701 	atomic_inc(&bp->intr_sem);
4702 
4703 	bnxt_disable_int(bp);
4704 	for (i = 0; i < bp->cp_nr_rings; i++) {
4705 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
4706 
4707 		synchronize_irq(bp->irq_tbl[map_idx].vector);
4708 	}
4709 }
4710 
bnxt_enable_int(struct bnxt * bp)4711 static void bnxt_enable_int(struct bnxt *bp)
4712 {
4713 	int i;
4714 
4715 	atomic_set(&bp->intr_sem, 0);
4716 	for (i = 0; i < bp->cp_nr_rings; i++) {
4717 		struct bnxt_napi *bnapi = bp->bnapi[i];
4718 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4719 
4720 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
4721 	}
4722 }
4723 
bnxt_hwrm_func_drv_rgtr(struct bnxt * bp,unsigned long * bmap,int bmap_size,bool async_only)4724 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
4725 			    bool async_only)
4726 {
4727 	DECLARE_BITMAP(async_events_bmap, 256);
4728 	u32 *events = (u32 *)async_events_bmap;
4729 	struct hwrm_func_drv_rgtr_output *resp;
4730 	struct hwrm_func_drv_rgtr_input *req;
4731 	u32 flags;
4732 	int rc, i;
4733 
4734 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
4735 	if (rc)
4736 		return rc;
4737 
4738 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
4739 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
4740 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4741 
4742 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
4743 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
4744 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
4745 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
4746 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
4747 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
4748 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
4749 	req->flags = cpu_to_le32(flags);
4750 	req->ver_maj_8b = DRV_VER_MAJ;
4751 	req->ver_min_8b = DRV_VER_MIN;
4752 	req->ver_upd_8b = DRV_VER_UPD;
4753 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
4754 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
4755 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
4756 
4757 	if (BNXT_PF(bp)) {
4758 		u32 data[8];
4759 		int i;
4760 
4761 		memset(data, 0, sizeof(data));
4762 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
4763 			u16 cmd = bnxt_vf_req_snif[i];
4764 			unsigned int bit, idx;
4765 
4766 			idx = cmd / 32;
4767 			bit = cmd % 32;
4768 			data[idx] |= 1 << bit;
4769 		}
4770 
4771 		for (i = 0; i < 8; i++)
4772 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
4773 
4774 		req->enables |=
4775 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
4776 	}
4777 
4778 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
4779 		req->flags |= cpu_to_le32(
4780 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
4781 
4782 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
4783 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
4784 		u16 event_id = bnxt_async_events_arr[i];
4785 
4786 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
4787 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
4788 			continue;
4789 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
4790 	}
4791 	if (bmap && bmap_size) {
4792 		for (i = 0; i < bmap_size; i++) {
4793 			if (test_bit(i, bmap))
4794 				__set_bit(i, async_events_bmap);
4795 		}
4796 	}
4797 	for (i = 0; i < 8; i++)
4798 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
4799 
4800 	if (async_only)
4801 		req->enables =
4802 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4803 
4804 	resp = hwrm_req_hold(bp, req);
4805 	rc = hwrm_req_send(bp, req);
4806 	if (!rc) {
4807 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
4808 		if (resp->flags &
4809 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
4810 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
4811 	}
4812 	hwrm_req_drop(bp, req);
4813 	return rc;
4814 }
4815 
bnxt_hwrm_func_drv_unrgtr(struct bnxt * bp)4816 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
4817 {
4818 	struct hwrm_func_drv_unrgtr_input *req;
4819 	int rc;
4820 
4821 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
4822 		return 0;
4823 
4824 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
4825 	if (rc)
4826 		return rc;
4827 	return hwrm_req_send(bp, req);
4828 }
4829 
bnxt_hwrm_tunnel_dst_port_free(struct bnxt * bp,u8 tunnel_type)4830 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
4831 {
4832 	struct hwrm_tunnel_dst_port_free_input *req;
4833 	int rc;
4834 
4835 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
4836 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
4837 		return 0;
4838 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
4839 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
4840 		return 0;
4841 
4842 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
4843 	if (rc)
4844 		return rc;
4845 
4846 	req->tunnel_type = tunnel_type;
4847 
4848 	switch (tunnel_type) {
4849 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
4850 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
4851 		bp->vxlan_port = 0;
4852 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
4853 		break;
4854 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
4855 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
4856 		bp->nge_port = 0;
4857 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
4858 		break;
4859 	default:
4860 		break;
4861 	}
4862 
4863 	rc = hwrm_req_send(bp, req);
4864 	if (rc)
4865 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
4866 			   rc);
4867 	return rc;
4868 }
4869 
bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt * bp,__be16 port,u8 tunnel_type)4870 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
4871 					   u8 tunnel_type)
4872 {
4873 	struct hwrm_tunnel_dst_port_alloc_output *resp;
4874 	struct hwrm_tunnel_dst_port_alloc_input *req;
4875 	int rc;
4876 
4877 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
4878 	if (rc)
4879 		return rc;
4880 
4881 	req->tunnel_type = tunnel_type;
4882 	req->tunnel_dst_port_val = port;
4883 
4884 	resp = hwrm_req_hold(bp, req);
4885 	rc = hwrm_req_send(bp, req);
4886 	if (rc) {
4887 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
4888 			   rc);
4889 		goto err_out;
4890 	}
4891 
4892 	switch (tunnel_type) {
4893 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
4894 		bp->vxlan_port = port;
4895 		bp->vxlan_fw_dst_port_id =
4896 			le16_to_cpu(resp->tunnel_dst_port_id);
4897 		break;
4898 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
4899 		bp->nge_port = port;
4900 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
4901 		break;
4902 	default:
4903 		break;
4904 	}
4905 
4906 err_out:
4907 	hwrm_req_drop(bp, req);
4908 	return rc;
4909 }
4910 
bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt * bp,u16 vnic_id)4911 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
4912 {
4913 	struct hwrm_cfa_l2_set_rx_mask_input *req;
4914 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4915 	int rc;
4916 
4917 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
4918 	if (rc)
4919 		return rc;
4920 
4921 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4922 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
4923 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
4924 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
4925 	}
4926 	req->mask = cpu_to_le32(vnic->rx_mask);
4927 	return hwrm_req_send_silent(bp, req);
4928 }
4929 
4930 #ifdef CONFIG_RFS_ACCEL
bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt * bp,struct bnxt_ntuple_filter * fltr)4931 static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
4932 					    struct bnxt_ntuple_filter *fltr)
4933 {
4934 	struct hwrm_cfa_ntuple_filter_free_input *req;
4935 	int rc;
4936 
4937 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
4938 	if (rc)
4939 		return rc;
4940 
4941 	req->ntuple_filter_id = fltr->filter_id;
4942 	return hwrm_req_send(bp, req);
4943 }
4944 
4945 #define BNXT_NTP_FLTR_FLAGS					\
4946 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
4947 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
4948 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR |	\
4949 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
4950 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
4951 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
4952 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
4953 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
4954 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
4955 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
4956 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
4957 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
4958 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
4959 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
4960 
4961 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
4962 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
4963 
bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt * bp,struct bnxt_ntuple_filter * fltr)4964 static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
4965 					     struct bnxt_ntuple_filter *fltr)
4966 {
4967 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
4968 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
4969 	struct flow_keys *keys = &fltr->fkeys;
4970 	struct bnxt_vnic_info *vnic;
4971 	u32 flags = 0;
4972 	int rc;
4973 
4974 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
4975 	if (rc)
4976 		return rc;
4977 
4978 	req->l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
4979 
4980 	if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
4981 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
4982 		req->dst_id = cpu_to_le16(fltr->rxq);
4983 	} else {
4984 		vnic = &bp->vnic_info[fltr->rxq + 1];
4985 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
4986 	}
4987 	req->flags = cpu_to_le32(flags);
4988 	req->enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
4989 
4990 	req->ethertype = htons(ETH_P_IP);
4991 	memcpy(req->src_macaddr, fltr->src_mac_addr, ETH_ALEN);
4992 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
4993 	req->ip_protocol = keys->basic.ip_proto;
4994 
4995 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
4996 		int i;
4997 
4998 		req->ethertype = htons(ETH_P_IPV6);
4999 		req->ip_addr_type =
5000 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
5001 		*(struct in6_addr *)&req->src_ipaddr[0] =
5002 			keys->addrs.v6addrs.src;
5003 		*(struct in6_addr *)&req->dst_ipaddr[0] =
5004 			keys->addrs.v6addrs.dst;
5005 		for (i = 0; i < 4; i++) {
5006 			req->src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
5007 			req->dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
5008 		}
5009 	} else {
5010 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
5011 		req->src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
5012 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
5013 		req->dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
5014 	}
5015 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
5016 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
5017 		req->tunnel_type =
5018 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
5019 	}
5020 
5021 	req->src_port = keys->ports.src;
5022 	req->src_port_mask = cpu_to_be16(0xffff);
5023 	req->dst_port = keys->ports.dst;
5024 	req->dst_port_mask = cpu_to_be16(0xffff);
5025 
5026 	resp = hwrm_req_hold(bp, req);
5027 	rc = hwrm_req_send(bp, req);
5028 	if (!rc)
5029 		fltr->filter_id = resp->ntuple_filter_id;
5030 	hwrm_req_drop(bp, req);
5031 	return rc;
5032 }
5033 #endif
5034 
bnxt_hwrm_set_vnic_filter(struct bnxt * bp,u16 vnic_id,u16 idx,const u8 * mac_addr)5035 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
5036 				     const u8 *mac_addr)
5037 {
5038 	struct hwrm_cfa_l2_filter_alloc_output *resp;
5039 	struct hwrm_cfa_l2_filter_alloc_input *req;
5040 	int rc;
5041 
5042 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
5043 	if (rc)
5044 		return rc;
5045 
5046 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
5047 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
5048 		req->flags |=
5049 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
5050 	req->dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
5051 	req->enables =
5052 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
5053 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
5054 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
5055 	memcpy(req->l2_addr, mac_addr, ETH_ALEN);
5056 	req->l2_addr_mask[0] = 0xff;
5057 	req->l2_addr_mask[1] = 0xff;
5058 	req->l2_addr_mask[2] = 0xff;
5059 	req->l2_addr_mask[3] = 0xff;
5060 	req->l2_addr_mask[4] = 0xff;
5061 	req->l2_addr_mask[5] = 0xff;
5062 
5063 	resp = hwrm_req_hold(bp, req);
5064 	rc = hwrm_req_send(bp, req);
5065 	if (!rc)
5066 		bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
5067 							resp->l2_filter_id;
5068 	hwrm_req_drop(bp, req);
5069 	return rc;
5070 }
5071 
bnxt_hwrm_clear_vnic_filter(struct bnxt * bp)5072 static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
5073 {
5074 	struct hwrm_cfa_l2_filter_free_input *req;
5075 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
5076 	int rc;
5077 
5078 	/* Any associated ntuple filters will also be cleared by firmware. */
5079 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
5080 	if (rc)
5081 		return rc;
5082 	hwrm_req_hold(bp, req);
5083 	for (i = 0; i < num_of_vnics; i++) {
5084 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
5085 
5086 		for (j = 0; j < vnic->uc_filter_count; j++) {
5087 			req->l2_filter_id = vnic->fw_l2_filter_id[j];
5088 
5089 			rc = hwrm_req_send(bp, req);
5090 		}
5091 		vnic->uc_filter_count = 0;
5092 	}
5093 	hwrm_req_drop(bp, req);
5094 	return rc;
5095 }
5096 
bnxt_hwrm_vnic_set_tpa(struct bnxt * bp,u16 vnic_id,u32 tpa_flags)5097 static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
5098 {
5099 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5100 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
5101 	struct hwrm_vnic_tpa_cfg_input *req;
5102 	int rc;
5103 
5104 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
5105 		return 0;
5106 
5107 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
5108 	if (rc)
5109 		return rc;
5110 
5111 	if (tpa_flags) {
5112 		u16 mss = bp->dev->mtu - 40;
5113 		u32 nsegs, n, segs = 0, flags;
5114 
5115 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
5116 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
5117 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
5118 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
5119 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
5120 		if (tpa_flags & BNXT_FLAG_GRO)
5121 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
5122 
5123 		req->flags = cpu_to_le32(flags);
5124 
5125 		req->enables =
5126 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
5127 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
5128 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
5129 
5130 		/* Number of segs are log2 units, and first packet is not
5131 		 * included as part of this units.
5132 		 */
5133 		if (mss <= BNXT_RX_PAGE_SIZE) {
5134 			n = BNXT_RX_PAGE_SIZE / mss;
5135 			nsegs = (MAX_SKB_FRAGS - 1) * n;
5136 		} else {
5137 			n = mss / BNXT_RX_PAGE_SIZE;
5138 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
5139 				n++;
5140 			nsegs = (MAX_SKB_FRAGS - n) / n;
5141 		}
5142 
5143 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5144 			segs = MAX_TPA_SEGS_P5;
5145 			max_aggs = bp->max_tpa;
5146 		} else {
5147 			segs = ilog2(nsegs);
5148 		}
5149 		req->max_agg_segs = cpu_to_le16(segs);
5150 		req->max_aggs = cpu_to_le16(max_aggs);
5151 
5152 		req->min_agg_len = cpu_to_le32(512);
5153 	}
5154 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5155 
5156 	return hwrm_req_send(bp, req);
5157 }
5158 
bnxt_cp_ring_from_grp(struct bnxt * bp,struct bnxt_ring_struct * ring)5159 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
5160 {
5161 	struct bnxt_ring_grp_info *grp_info;
5162 
5163 	grp_info = &bp->grp_info[ring->grp_idx];
5164 	return grp_info->cp_fw_ring_id;
5165 }
5166 
bnxt_cp_ring_for_rx(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)5167 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
5168 {
5169 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5170 		struct bnxt_napi *bnapi = rxr->bnapi;
5171 		struct bnxt_cp_ring_info *cpr;
5172 
5173 		cpr = bnapi->cp_ring.cp_ring_arr[BNXT_RX_HDL];
5174 		return cpr->cp_ring_struct.fw_ring_id;
5175 	} else {
5176 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
5177 	}
5178 }
5179 
bnxt_cp_ring_for_tx(struct bnxt * bp,struct bnxt_tx_ring_info * txr)5180 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
5181 {
5182 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5183 		struct bnxt_napi *bnapi = txr->bnapi;
5184 		struct bnxt_cp_ring_info *cpr;
5185 
5186 		cpr = bnapi->cp_ring.cp_ring_arr[BNXT_TX_HDL];
5187 		return cpr->cp_ring_struct.fw_ring_id;
5188 	} else {
5189 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
5190 	}
5191 }
5192 
bnxt_alloc_rss_indir_tbl(struct bnxt * bp)5193 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
5194 {
5195 	int entries;
5196 
5197 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5198 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
5199 	else
5200 		entries = HW_HASH_INDEX_SIZE;
5201 
5202 	bp->rss_indir_tbl_entries = entries;
5203 	bp->rss_indir_tbl = kmalloc_array(entries, sizeof(*bp->rss_indir_tbl),
5204 					  GFP_KERNEL);
5205 	if (!bp->rss_indir_tbl)
5206 		return -ENOMEM;
5207 	return 0;
5208 }
5209 
bnxt_set_dflt_rss_indir_tbl(struct bnxt * bp)5210 static void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp)
5211 {
5212 	u16 max_rings, max_entries, pad, i;
5213 
5214 	if (!bp->rx_nr_rings)
5215 		return;
5216 
5217 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5218 		max_rings = bp->rx_nr_rings - 1;
5219 	else
5220 		max_rings = bp->rx_nr_rings;
5221 
5222 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
5223 
5224 	for (i = 0; i < max_entries; i++)
5225 		bp->rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
5226 
5227 	pad = bp->rss_indir_tbl_entries - max_entries;
5228 	if (pad)
5229 		memset(&bp->rss_indir_tbl[i], 0, pad * sizeof(u16));
5230 }
5231 
bnxt_get_max_rss_ring(struct bnxt * bp)5232 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
5233 {
5234 	u16 i, tbl_size, max_ring = 0;
5235 
5236 	if (!bp->rss_indir_tbl)
5237 		return 0;
5238 
5239 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
5240 	for (i = 0; i < tbl_size; i++)
5241 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
5242 	return max_ring;
5243 }
5244 
bnxt_get_nr_rss_ctxs(struct bnxt * bp,int rx_rings)5245 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
5246 {
5247 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5248 		return DIV_ROUND_UP(rx_rings, BNXT_RSS_TABLE_ENTRIES_P5);
5249 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5250 		return 2;
5251 	return 1;
5252 }
5253 
__bnxt_fill_hw_rss_tbl(struct bnxt * bp,struct bnxt_vnic_info * vnic)5254 static void __bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
5255 {
5256 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
5257 	u16 i, j;
5258 
5259 	/* Fill the RSS indirection table with ring group ids */
5260 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
5261 		if (!no_rss)
5262 			j = bp->rss_indir_tbl[i];
5263 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
5264 	}
5265 }
5266 
__bnxt_fill_hw_rss_tbl_p5(struct bnxt * bp,struct bnxt_vnic_info * vnic)5267 static void __bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
5268 				      struct bnxt_vnic_info *vnic)
5269 {
5270 	__le16 *ring_tbl = vnic->rss_table;
5271 	struct bnxt_rx_ring_info *rxr;
5272 	u16 tbl_size, i;
5273 
5274 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
5275 
5276 	for (i = 0; i < tbl_size; i++) {
5277 		u16 ring_id, j;
5278 
5279 		j = bp->rss_indir_tbl[i];
5280 		rxr = &bp->rx_ring[j];
5281 
5282 		ring_id = rxr->rx_ring_struct.fw_ring_id;
5283 		*ring_tbl++ = cpu_to_le16(ring_id);
5284 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5285 		*ring_tbl++ = cpu_to_le16(ring_id);
5286 	}
5287 }
5288 
bnxt_fill_hw_rss_tbl(struct bnxt * bp,struct bnxt_vnic_info * vnic)5289 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
5290 {
5291 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5292 		__bnxt_fill_hw_rss_tbl_p5(bp, vnic);
5293 	else
5294 		__bnxt_fill_hw_rss_tbl(bp, vnic);
5295 }
5296 
bnxt_hwrm_vnic_set_rss(struct bnxt * bp,u16 vnic_id,bool set_rss)5297 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
5298 {
5299 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5300 	struct hwrm_vnic_rss_cfg_input *req;
5301 	int rc;
5302 
5303 	if ((bp->flags & BNXT_FLAG_CHIP_P5) ||
5304 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
5305 		return 0;
5306 
5307 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
5308 	if (rc)
5309 		return rc;
5310 
5311 	if (set_rss) {
5312 		bnxt_fill_hw_rss_tbl(bp, vnic);
5313 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
5314 		req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
5315 		req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
5316 		req->hash_key_tbl_addr =
5317 			cpu_to_le64(vnic->rss_hash_key_dma_addr);
5318 	}
5319 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5320 	return hwrm_req_send(bp, req);
5321 }
5322 
bnxt_hwrm_vnic_set_rss_p5(struct bnxt * bp,u16 vnic_id,bool set_rss)5323 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, u16 vnic_id, bool set_rss)
5324 {
5325 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5326 	struct hwrm_vnic_rss_cfg_input *req;
5327 	dma_addr_t ring_tbl_map;
5328 	u32 i, nr_ctxs;
5329 	int rc;
5330 
5331 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
5332 	if (rc)
5333 		return rc;
5334 
5335 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5336 	if (!set_rss)
5337 		return hwrm_req_send(bp, req);
5338 
5339 	bnxt_fill_hw_rss_tbl(bp, vnic);
5340 	req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
5341 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
5342 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
5343 	ring_tbl_map = vnic->rss_table_dma_addr;
5344 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
5345 
5346 	hwrm_req_hold(bp, req);
5347 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
5348 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
5349 		req->ring_table_pair_index = i;
5350 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
5351 		rc = hwrm_req_send(bp, req);
5352 		if (rc)
5353 			goto exit;
5354 	}
5355 
5356 exit:
5357 	hwrm_req_drop(bp, req);
5358 	return rc;
5359 }
5360 
bnxt_hwrm_vnic_set_hds(struct bnxt * bp,u16 vnic_id)5361 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
5362 {
5363 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5364 	struct hwrm_vnic_plcmodes_cfg_input *req;
5365 	int rc;
5366 
5367 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
5368 	if (rc)
5369 		return rc;
5370 
5371 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
5372 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
5373 
5374 	if (BNXT_RX_PAGE_MODE(bp)) {
5375 		req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
5376 	} else {
5377 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
5378 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
5379 		req->enables |=
5380 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
5381 		req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
5382 		req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
5383 	}
5384 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5385 	return hwrm_req_send(bp, req);
5386 }
5387 
bnxt_hwrm_vnic_ctx_free_one(struct bnxt * bp,u16 vnic_id,u16 ctx_idx)5388 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
5389 					u16 ctx_idx)
5390 {
5391 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
5392 
5393 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
5394 		return;
5395 
5396 	req->rss_cos_lb_ctx_id =
5397 		cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
5398 
5399 	hwrm_req_send(bp, req);
5400 	bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
5401 }
5402 
bnxt_hwrm_vnic_ctx_free(struct bnxt * bp)5403 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
5404 {
5405 	int i, j;
5406 
5407 	for (i = 0; i < bp->nr_vnics; i++) {
5408 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
5409 
5410 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
5411 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
5412 				bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
5413 		}
5414 	}
5415 	bp->rsscos_nr_ctxs = 0;
5416 }
5417 
bnxt_hwrm_vnic_ctx_alloc(struct bnxt * bp,u16 vnic_id,u16 ctx_idx)5418 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
5419 {
5420 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
5421 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
5422 	int rc;
5423 
5424 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
5425 	if (rc)
5426 		return rc;
5427 
5428 	resp = hwrm_req_hold(bp, req);
5429 	rc = hwrm_req_send(bp, req);
5430 	if (!rc)
5431 		bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
5432 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
5433 	hwrm_req_drop(bp, req);
5434 
5435 	return rc;
5436 }
5437 
bnxt_get_roce_vnic_mode(struct bnxt * bp)5438 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
5439 {
5440 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
5441 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
5442 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
5443 }
5444 
bnxt_hwrm_vnic_cfg(struct bnxt * bp,u16 vnic_id)5445 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
5446 {
5447 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5448 	struct hwrm_vnic_cfg_input *req;
5449 	unsigned int ring = 0, grp_idx;
5450 	u16 def_vlan = 0;
5451 	int rc;
5452 
5453 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
5454 	if (rc)
5455 		return rc;
5456 
5457 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5458 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
5459 
5460 		req->default_rx_ring_id =
5461 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
5462 		req->default_cmpl_ring_id =
5463 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
5464 		req->enables =
5465 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
5466 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
5467 		goto vnic_mru;
5468 	}
5469 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
5470 	/* Only RSS support for now TBD: COS & LB */
5471 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
5472 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5473 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5474 					   VNIC_CFG_REQ_ENABLES_MRU);
5475 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
5476 		req->rss_rule =
5477 			cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
5478 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5479 					   VNIC_CFG_REQ_ENABLES_MRU);
5480 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
5481 	} else {
5482 		req->rss_rule = cpu_to_le16(0xffff);
5483 	}
5484 
5485 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
5486 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
5487 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
5488 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
5489 	} else {
5490 		req->cos_rule = cpu_to_le16(0xffff);
5491 	}
5492 
5493 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
5494 		ring = 0;
5495 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
5496 		ring = vnic_id - 1;
5497 	else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
5498 		ring = bp->rx_nr_rings - 1;
5499 
5500 	grp_idx = bp->rx_ring[ring].bnapi->index;
5501 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
5502 	req->lb_rule = cpu_to_le16(0xffff);
5503 vnic_mru:
5504 	req->mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + VLAN_HLEN);
5505 
5506 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5507 #ifdef CONFIG_BNXT_SRIOV
5508 	if (BNXT_VF(bp))
5509 		def_vlan = bp->vf.vlan;
5510 #endif
5511 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
5512 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
5513 	if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
5514 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
5515 
5516 	return hwrm_req_send(bp, req);
5517 }
5518 
bnxt_hwrm_vnic_free_one(struct bnxt * bp,u16 vnic_id)5519 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
5520 {
5521 	if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
5522 		struct hwrm_vnic_free_input *req;
5523 
5524 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
5525 			return;
5526 
5527 		req->vnic_id =
5528 			cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
5529 
5530 		hwrm_req_send(bp, req);
5531 		bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
5532 	}
5533 }
5534 
bnxt_hwrm_vnic_free(struct bnxt * bp)5535 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
5536 {
5537 	u16 i;
5538 
5539 	for (i = 0; i < bp->nr_vnics; i++)
5540 		bnxt_hwrm_vnic_free_one(bp, i);
5541 }
5542 
bnxt_hwrm_vnic_alloc(struct bnxt * bp,u16 vnic_id,unsigned int start_rx_ring_idx,unsigned int nr_rings)5543 static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
5544 				unsigned int start_rx_ring_idx,
5545 				unsigned int nr_rings)
5546 {
5547 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
5548 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5549 	struct hwrm_vnic_alloc_output *resp;
5550 	struct hwrm_vnic_alloc_input *req;
5551 	int rc;
5552 
5553 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
5554 	if (rc)
5555 		return rc;
5556 
5557 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5558 		goto vnic_no_ring_grps;
5559 
5560 	/* map ring groups to this vnic */
5561 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
5562 		grp_idx = bp->rx_ring[i].bnapi->index;
5563 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
5564 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
5565 				   j, nr_rings);
5566 			break;
5567 		}
5568 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
5569 	}
5570 
5571 vnic_no_ring_grps:
5572 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
5573 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
5574 	if (vnic_id == 0)
5575 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
5576 
5577 	resp = hwrm_req_hold(bp, req);
5578 	rc = hwrm_req_send(bp, req);
5579 	if (!rc)
5580 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
5581 	hwrm_req_drop(bp, req);
5582 	return rc;
5583 }
5584 
bnxt_hwrm_vnic_qcaps(struct bnxt * bp)5585 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
5586 {
5587 	struct hwrm_vnic_qcaps_output *resp;
5588 	struct hwrm_vnic_qcaps_input *req;
5589 	int rc;
5590 
5591 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
5592 	bp->flags &= ~(BNXT_FLAG_NEW_RSS_CAP | BNXT_FLAG_ROCE_MIRROR_CAP);
5593 	if (bp->hwrm_spec_code < 0x10600)
5594 		return 0;
5595 
5596 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
5597 	if (rc)
5598 		return rc;
5599 
5600 	resp = hwrm_req_hold(bp, req);
5601 	rc = hwrm_req_send(bp, req);
5602 	if (!rc) {
5603 		u32 flags = le32_to_cpu(resp->flags);
5604 
5605 		if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
5606 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
5607 			bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
5608 		if (flags &
5609 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
5610 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
5611 
5612 		/* Older P5 fw before EXT_HW_STATS support did not set
5613 		 * VLAN_STRIP_CAP properly.
5614 		 */
5615 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
5616 		    (BNXT_CHIP_P5_THOR(bp) &&
5617 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
5618 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
5619 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
5620 		if (bp->max_tpa_v2) {
5621 			if (BNXT_CHIP_P5_THOR(bp))
5622 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
5623 			else
5624 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5_SR2;
5625 		}
5626 	}
5627 	hwrm_req_drop(bp, req);
5628 	return rc;
5629 }
5630 
bnxt_hwrm_ring_grp_alloc(struct bnxt * bp)5631 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
5632 {
5633 	struct hwrm_ring_grp_alloc_output *resp;
5634 	struct hwrm_ring_grp_alloc_input *req;
5635 	int rc;
5636 	u16 i;
5637 
5638 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5639 		return 0;
5640 
5641 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
5642 	if (rc)
5643 		return rc;
5644 
5645 	resp = hwrm_req_hold(bp, req);
5646 	for (i = 0; i < bp->rx_nr_rings; i++) {
5647 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
5648 
5649 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
5650 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
5651 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
5652 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
5653 
5654 		rc = hwrm_req_send(bp, req);
5655 
5656 		if (rc)
5657 			break;
5658 
5659 		bp->grp_info[grp_idx].fw_grp_id =
5660 			le32_to_cpu(resp->ring_group_id);
5661 	}
5662 	hwrm_req_drop(bp, req);
5663 	return rc;
5664 }
5665 
bnxt_hwrm_ring_grp_free(struct bnxt * bp)5666 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
5667 {
5668 	struct hwrm_ring_grp_free_input *req;
5669 	u16 i;
5670 
5671 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5))
5672 		return;
5673 
5674 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
5675 		return;
5676 
5677 	hwrm_req_hold(bp, req);
5678 	for (i = 0; i < bp->cp_nr_rings; i++) {
5679 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
5680 			continue;
5681 		req->ring_group_id =
5682 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
5683 
5684 		hwrm_req_send(bp, req);
5685 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
5686 	}
5687 	hwrm_req_drop(bp, req);
5688 }
5689 
hwrm_ring_alloc_send_msg(struct bnxt * bp,struct bnxt_ring_struct * ring,u32 ring_type,u32 map_index)5690 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
5691 				    struct bnxt_ring_struct *ring,
5692 				    u32 ring_type, u32 map_index)
5693 {
5694 	struct hwrm_ring_alloc_output *resp;
5695 	struct hwrm_ring_alloc_input *req;
5696 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
5697 	struct bnxt_ring_grp_info *grp_info;
5698 	int rc, err = 0;
5699 	u16 ring_id;
5700 
5701 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
5702 	if (rc)
5703 		goto exit;
5704 
5705 	req->enables = 0;
5706 	if (rmem->nr_pages > 1) {
5707 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
5708 		/* Page size is in log2 units */
5709 		req->page_size = BNXT_PAGE_SHIFT;
5710 		req->page_tbl_depth = 1;
5711 	} else {
5712 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
5713 	}
5714 	req->fbo = 0;
5715 	/* Association of ring index with doorbell index and MSIX number */
5716 	req->logical_id = cpu_to_le16(map_index);
5717 
5718 	switch (ring_type) {
5719 	case HWRM_RING_ALLOC_TX: {
5720 		struct bnxt_tx_ring_info *txr;
5721 
5722 		txr = container_of(ring, struct bnxt_tx_ring_info,
5723 				   tx_ring_struct);
5724 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
5725 		/* Association of transmit ring with completion ring */
5726 		grp_info = &bp->grp_info[ring->grp_idx];
5727 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
5728 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
5729 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5730 		req->queue_id = cpu_to_le16(ring->queue_id);
5731 		break;
5732 	}
5733 	case HWRM_RING_ALLOC_RX:
5734 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5735 		req->length = cpu_to_le32(bp->rx_ring_mask + 1);
5736 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5737 			u16 flags = 0;
5738 
5739 			/* Association of rx ring with stats context */
5740 			grp_info = &bp->grp_info[ring->grp_idx];
5741 			req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
5742 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5743 			req->enables |= cpu_to_le32(
5744 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5745 			if (NET_IP_ALIGN == 2)
5746 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
5747 			req->flags = cpu_to_le16(flags);
5748 		}
5749 		break;
5750 	case HWRM_RING_ALLOC_AGG:
5751 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5752 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
5753 			/* Association of agg ring with rx ring */
5754 			grp_info = &bp->grp_info[ring->grp_idx];
5755 			req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
5756 			req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
5757 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5758 			req->enables |= cpu_to_le32(
5759 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
5760 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5761 		} else {
5762 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5763 		}
5764 		req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
5765 		break;
5766 	case HWRM_RING_ALLOC_CMPL:
5767 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
5768 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
5769 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5770 			/* Association of cp ring with nq */
5771 			grp_info = &bp->grp_info[map_index];
5772 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
5773 			req->cq_handle = cpu_to_le64(ring->handle);
5774 			req->enables |= cpu_to_le32(
5775 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
5776 		} else if (bp->flags & BNXT_FLAG_USING_MSIX) {
5777 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5778 		}
5779 		break;
5780 	case HWRM_RING_ALLOC_NQ:
5781 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
5782 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
5783 		if (bp->flags & BNXT_FLAG_USING_MSIX)
5784 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5785 		break;
5786 	default:
5787 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
5788 			   ring_type);
5789 		return -1;
5790 	}
5791 
5792 	resp = hwrm_req_hold(bp, req);
5793 	rc = hwrm_req_send(bp, req);
5794 	err = le16_to_cpu(resp->error_code);
5795 	ring_id = le16_to_cpu(resp->ring_id);
5796 	hwrm_req_drop(bp, req);
5797 
5798 exit:
5799 	if (rc || err) {
5800 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
5801 			   ring_type, rc, err);
5802 		return -EIO;
5803 	}
5804 	ring->fw_ring_id = ring_id;
5805 	return rc;
5806 }
5807 
bnxt_hwrm_set_async_event_cr(struct bnxt * bp,int idx)5808 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
5809 {
5810 	int rc;
5811 
5812 	if (BNXT_PF(bp)) {
5813 		struct hwrm_func_cfg_input *req;
5814 
5815 		rc = hwrm_req_init(bp, req, HWRM_FUNC_CFG);
5816 		if (rc)
5817 			return rc;
5818 
5819 		req->fid = cpu_to_le16(0xffff);
5820 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5821 		req->async_event_cr = cpu_to_le16(idx);
5822 		return hwrm_req_send(bp, req);
5823 	} else {
5824 		struct hwrm_func_vf_cfg_input *req;
5825 
5826 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
5827 		if (rc)
5828 			return rc;
5829 
5830 		req->enables =
5831 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5832 		req->async_event_cr = cpu_to_le16(idx);
5833 		return hwrm_req_send(bp, req);
5834 	}
5835 }
5836 
bnxt_set_db(struct bnxt * bp,struct bnxt_db_info * db,u32 ring_type,u32 map_idx,u32 xid)5837 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
5838 			u32 map_idx, u32 xid)
5839 {
5840 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
5841 		if (BNXT_PF(bp))
5842 			db->doorbell = bp->bar1 + DB_PF_OFFSET_P5;
5843 		else
5844 			db->doorbell = bp->bar1 + DB_VF_OFFSET_P5;
5845 		switch (ring_type) {
5846 		case HWRM_RING_ALLOC_TX:
5847 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
5848 			break;
5849 		case HWRM_RING_ALLOC_RX:
5850 		case HWRM_RING_ALLOC_AGG:
5851 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
5852 			break;
5853 		case HWRM_RING_ALLOC_CMPL:
5854 			db->db_key64 = DBR_PATH_L2;
5855 			break;
5856 		case HWRM_RING_ALLOC_NQ:
5857 			db->db_key64 = DBR_PATH_L2;
5858 			break;
5859 		}
5860 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
5861 	} else {
5862 		db->doorbell = bp->bar1 + map_idx * 0x80;
5863 		switch (ring_type) {
5864 		case HWRM_RING_ALLOC_TX:
5865 			db->db_key32 = DB_KEY_TX;
5866 			break;
5867 		case HWRM_RING_ALLOC_RX:
5868 		case HWRM_RING_ALLOC_AGG:
5869 			db->db_key32 = DB_KEY_RX;
5870 			break;
5871 		case HWRM_RING_ALLOC_CMPL:
5872 			db->db_key32 = DB_KEY_CP;
5873 			break;
5874 		}
5875 	}
5876 }
5877 
bnxt_hwrm_ring_alloc(struct bnxt * bp)5878 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
5879 {
5880 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
5881 	int i, rc = 0;
5882 	u32 type;
5883 
5884 	if (bp->flags & BNXT_FLAG_CHIP_P5)
5885 		type = HWRM_RING_ALLOC_NQ;
5886 	else
5887 		type = HWRM_RING_ALLOC_CMPL;
5888 	for (i = 0; i < bp->cp_nr_rings; i++) {
5889 		struct bnxt_napi *bnapi = bp->bnapi[i];
5890 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5891 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5892 		u32 map_idx = ring->map_idx;
5893 		unsigned int vector;
5894 
5895 		vector = bp->irq_tbl[map_idx].vector;
5896 		disable_irq_nosync(vector);
5897 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5898 		if (rc) {
5899 			enable_irq(vector);
5900 			goto err_out;
5901 		}
5902 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
5903 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5904 		enable_irq(vector);
5905 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
5906 
5907 		if (!i) {
5908 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
5909 			if (rc)
5910 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
5911 		}
5912 	}
5913 
5914 	type = HWRM_RING_ALLOC_TX;
5915 	for (i = 0; i < bp->tx_nr_rings; i++) {
5916 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5917 		struct bnxt_ring_struct *ring;
5918 		u32 map_idx;
5919 
5920 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5921 			struct bnxt_napi *bnapi = txr->bnapi;
5922 			struct bnxt_cp_ring_info *cpr, *cpr2;
5923 			u32 type2 = HWRM_RING_ALLOC_CMPL;
5924 
5925 			cpr = &bnapi->cp_ring;
5926 			cpr2 = cpr->cp_ring_arr[BNXT_TX_HDL];
5927 			ring = &cpr2->cp_ring_struct;
5928 			ring->handle = BNXT_TX_HDL;
5929 			map_idx = bnapi->index;
5930 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5931 			if (rc)
5932 				goto err_out;
5933 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5934 				    ring->fw_ring_id);
5935 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5936 		}
5937 		ring = &txr->tx_ring_struct;
5938 		map_idx = i;
5939 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5940 		if (rc)
5941 			goto err_out;
5942 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
5943 	}
5944 
5945 	type = HWRM_RING_ALLOC_RX;
5946 	for (i = 0; i < bp->rx_nr_rings; i++) {
5947 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5948 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5949 		struct bnxt_napi *bnapi = rxr->bnapi;
5950 		u32 map_idx = bnapi->index;
5951 
5952 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5953 		if (rc)
5954 			goto err_out;
5955 		bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
5956 		/* If we have agg rings, post agg buffers first. */
5957 		if (!agg_rings)
5958 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
5959 		bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
5960 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
5961 			struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5962 			u32 type2 = HWRM_RING_ALLOC_CMPL;
5963 			struct bnxt_cp_ring_info *cpr2;
5964 
5965 			cpr2 = cpr->cp_ring_arr[BNXT_RX_HDL];
5966 			ring = &cpr2->cp_ring_struct;
5967 			ring->handle = BNXT_RX_HDL;
5968 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5969 			if (rc)
5970 				goto err_out;
5971 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5972 				    ring->fw_ring_id);
5973 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5974 		}
5975 	}
5976 
5977 	if (agg_rings) {
5978 		type = HWRM_RING_ALLOC_AGG;
5979 		for (i = 0; i < bp->rx_nr_rings; i++) {
5980 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5981 			struct bnxt_ring_struct *ring =
5982 						&rxr->rx_agg_ring_struct;
5983 			u32 grp_idx = ring->grp_idx;
5984 			u32 map_idx = grp_idx + bp->rx_nr_rings;
5985 
5986 			rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5987 			if (rc)
5988 				goto err_out;
5989 
5990 			bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
5991 				    ring->fw_ring_id);
5992 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
5993 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
5994 			bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
5995 		}
5996 	}
5997 err_out:
5998 	return rc;
5999 }
6000 
hwrm_ring_free_send_msg(struct bnxt * bp,struct bnxt_ring_struct * ring,u32 ring_type,int cmpl_ring_id)6001 static int hwrm_ring_free_send_msg(struct bnxt *bp,
6002 				   struct bnxt_ring_struct *ring,
6003 				   u32 ring_type, int cmpl_ring_id)
6004 {
6005 	struct hwrm_ring_free_output *resp;
6006 	struct hwrm_ring_free_input *req;
6007 	u16 error_code = 0;
6008 	int rc;
6009 
6010 	if (BNXT_NO_FW_ACCESS(bp))
6011 		return 0;
6012 
6013 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
6014 	if (rc)
6015 		goto exit;
6016 
6017 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
6018 	req->ring_type = ring_type;
6019 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
6020 
6021 	resp = hwrm_req_hold(bp, req);
6022 	rc = hwrm_req_send(bp, req);
6023 	error_code = le16_to_cpu(resp->error_code);
6024 	hwrm_req_drop(bp, req);
6025 exit:
6026 	if (rc || error_code) {
6027 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
6028 			   ring_type, rc, error_code);
6029 		return -EIO;
6030 	}
6031 	return 0;
6032 }
6033 
bnxt_hwrm_ring_free(struct bnxt * bp,bool close_path)6034 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
6035 {
6036 	u32 type;
6037 	int i;
6038 
6039 	if (!bp->bnapi)
6040 		return;
6041 
6042 	for (i = 0; i < bp->tx_nr_rings; i++) {
6043 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
6044 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
6045 
6046 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6047 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
6048 
6049 			hwrm_ring_free_send_msg(bp, ring,
6050 						RING_FREE_REQ_RING_TYPE_TX,
6051 						close_path ? cmpl_ring_id :
6052 						INVALID_HW_RING_ID);
6053 			ring->fw_ring_id = INVALID_HW_RING_ID;
6054 		}
6055 	}
6056 
6057 	for (i = 0; i < bp->rx_nr_rings; i++) {
6058 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6059 		struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
6060 		u32 grp_idx = rxr->bnapi->index;
6061 
6062 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6063 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6064 
6065 			hwrm_ring_free_send_msg(bp, ring,
6066 						RING_FREE_REQ_RING_TYPE_RX,
6067 						close_path ? cmpl_ring_id :
6068 						INVALID_HW_RING_ID);
6069 			ring->fw_ring_id = INVALID_HW_RING_ID;
6070 			bp->grp_info[grp_idx].rx_fw_ring_id =
6071 				INVALID_HW_RING_ID;
6072 		}
6073 	}
6074 
6075 	if (bp->flags & BNXT_FLAG_CHIP_P5)
6076 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
6077 	else
6078 		type = RING_FREE_REQ_RING_TYPE_RX;
6079 	for (i = 0; i < bp->rx_nr_rings; i++) {
6080 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6081 		struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
6082 		u32 grp_idx = rxr->bnapi->index;
6083 
6084 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6085 			u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6086 
6087 			hwrm_ring_free_send_msg(bp, ring, type,
6088 						close_path ? cmpl_ring_id :
6089 						INVALID_HW_RING_ID);
6090 			ring->fw_ring_id = INVALID_HW_RING_ID;
6091 			bp->grp_info[grp_idx].agg_fw_ring_id =
6092 				INVALID_HW_RING_ID;
6093 		}
6094 	}
6095 
6096 	/* The completion rings are about to be freed.  After that the
6097 	 * IRQ doorbell will not work anymore.  So we need to disable
6098 	 * IRQ here.
6099 	 */
6100 	bnxt_disable_int_sync(bp);
6101 
6102 	if (bp->flags & BNXT_FLAG_CHIP_P5)
6103 		type = RING_FREE_REQ_RING_TYPE_NQ;
6104 	else
6105 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
6106 	for (i = 0; i < bp->cp_nr_rings; i++) {
6107 		struct bnxt_napi *bnapi = bp->bnapi[i];
6108 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6109 		struct bnxt_ring_struct *ring;
6110 		int j;
6111 
6112 		for (j = 0; j < 2; j++) {
6113 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
6114 
6115 			if (cpr2) {
6116 				ring = &cpr2->cp_ring_struct;
6117 				if (ring->fw_ring_id == INVALID_HW_RING_ID)
6118 					continue;
6119 				hwrm_ring_free_send_msg(bp, ring,
6120 					RING_FREE_REQ_RING_TYPE_L2_CMPL,
6121 					INVALID_HW_RING_ID);
6122 				ring->fw_ring_id = INVALID_HW_RING_ID;
6123 			}
6124 		}
6125 		ring = &cpr->cp_ring_struct;
6126 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6127 			hwrm_ring_free_send_msg(bp, ring, type,
6128 						INVALID_HW_RING_ID);
6129 			ring->fw_ring_id = INVALID_HW_RING_ID;
6130 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
6131 		}
6132 	}
6133 }
6134 
6135 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
6136 			   bool shared);
6137 
bnxt_hwrm_get_rings(struct bnxt * bp)6138 static int bnxt_hwrm_get_rings(struct bnxt *bp)
6139 {
6140 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6141 	struct hwrm_func_qcfg_output *resp;
6142 	struct hwrm_func_qcfg_input *req;
6143 	int rc;
6144 
6145 	if (bp->hwrm_spec_code < 0x10601)
6146 		return 0;
6147 
6148 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
6149 	if (rc)
6150 		return rc;
6151 
6152 	req->fid = cpu_to_le16(0xffff);
6153 	resp = hwrm_req_hold(bp, req);
6154 	rc = hwrm_req_send(bp, req);
6155 	if (rc) {
6156 		hwrm_req_drop(bp, req);
6157 		return rc;
6158 	}
6159 
6160 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
6161 	if (BNXT_NEW_RM(bp)) {
6162 		u16 cp, stats;
6163 
6164 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
6165 		hw_resc->resv_hw_ring_grps =
6166 			le32_to_cpu(resp->alloc_hw_ring_grps);
6167 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
6168 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
6169 		stats = le16_to_cpu(resp->alloc_stat_ctx);
6170 		hw_resc->resv_irqs = cp;
6171 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
6172 			int rx = hw_resc->resv_rx_rings;
6173 			int tx = hw_resc->resv_tx_rings;
6174 
6175 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
6176 				rx >>= 1;
6177 			if (cp < (rx + tx)) {
6178 				bnxt_trim_rings(bp, &rx, &tx, cp, false);
6179 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
6180 					rx <<= 1;
6181 				hw_resc->resv_rx_rings = rx;
6182 				hw_resc->resv_tx_rings = tx;
6183 			}
6184 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
6185 			hw_resc->resv_hw_ring_grps = rx;
6186 		}
6187 		hw_resc->resv_cp_rings = cp;
6188 		hw_resc->resv_stat_ctxs = stats;
6189 	}
6190 	hwrm_req_drop(bp, req);
6191 	return 0;
6192 }
6193 
__bnxt_hwrm_get_tx_rings(struct bnxt * bp,u16 fid,int * tx_rings)6194 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
6195 {
6196 	struct hwrm_func_qcfg_output *resp;
6197 	struct hwrm_func_qcfg_input *req;
6198 	int rc;
6199 
6200 	if (bp->hwrm_spec_code < 0x10601)
6201 		return 0;
6202 
6203 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
6204 	if (rc)
6205 		return rc;
6206 
6207 	req->fid = cpu_to_le16(fid);
6208 	resp = hwrm_req_hold(bp, req);
6209 	rc = hwrm_req_send(bp, req);
6210 	if (!rc)
6211 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
6212 
6213 	hwrm_req_drop(bp, req);
6214 	return rc;
6215 }
6216 
6217 static bool bnxt_rfs_supported(struct bnxt *bp);
6218 
6219 static struct hwrm_func_cfg_input *
__bnxt_hwrm_reserve_pf_rings(struct bnxt * bp,int tx_rings,int rx_rings,int ring_grps,int cp_rings,int stats,int vnics)6220 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6221 			     int ring_grps, int cp_rings, int stats, int vnics)
6222 {
6223 	struct hwrm_func_cfg_input *req;
6224 	u32 enables = 0;
6225 
6226 	if (hwrm_req_init(bp, req, HWRM_FUNC_CFG))
6227 		return NULL;
6228 
6229 	req->fid = cpu_to_le16(0xffff);
6230 	enables |= tx_rings ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
6231 	req->num_tx_rings = cpu_to_le16(tx_rings);
6232 	if (BNXT_NEW_RM(bp)) {
6233 		enables |= rx_rings ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
6234 		enables |= stats ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
6235 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
6236 			enables |= cp_rings ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
6237 			enables |= tx_rings + ring_grps ?
6238 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6239 			enables |= rx_rings ?
6240 				FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6241 		} else {
6242 			enables |= cp_rings ?
6243 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6244 			enables |= ring_grps ?
6245 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS |
6246 				   FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6247 		}
6248 		enables |= vnics ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
6249 
6250 		req->num_rx_rings = cpu_to_le16(rx_rings);
6251 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
6252 			req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
6253 			req->num_msix = cpu_to_le16(cp_rings);
6254 			req->num_rsscos_ctxs =
6255 				cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
6256 		} else {
6257 			req->num_cmpl_rings = cpu_to_le16(cp_rings);
6258 			req->num_hw_ring_grps = cpu_to_le16(ring_grps);
6259 			req->num_rsscos_ctxs = cpu_to_le16(1);
6260 			if (!(bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
6261 			    bnxt_rfs_supported(bp))
6262 				req->num_rsscos_ctxs =
6263 					cpu_to_le16(ring_grps + 1);
6264 		}
6265 		req->num_stat_ctxs = cpu_to_le16(stats);
6266 		req->num_vnics = cpu_to_le16(vnics);
6267 	}
6268 	req->enables = cpu_to_le32(enables);
6269 	return req;
6270 }
6271 
6272 static struct hwrm_func_vf_cfg_input *
__bnxt_hwrm_reserve_vf_rings(struct bnxt * bp,int tx_rings,int rx_rings,int ring_grps,int cp_rings,int stats,int vnics)6273 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6274 			     int ring_grps, int cp_rings, int stats, int vnics)
6275 {
6276 	struct hwrm_func_vf_cfg_input *req;
6277 	u32 enables = 0;
6278 
6279 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
6280 		return NULL;
6281 
6282 	enables |= tx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
6283 	enables |= rx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
6284 			      FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6285 	enables |= stats ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
6286 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
6287 		enables |= tx_rings + ring_grps ?
6288 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6289 	} else {
6290 		enables |= cp_rings ?
6291 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6292 		enables |= ring_grps ?
6293 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
6294 	}
6295 	enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
6296 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
6297 
6298 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
6299 	req->num_tx_rings = cpu_to_le16(tx_rings);
6300 	req->num_rx_rings = cpu_to_le16(rx_rings);
6301 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
6302 		req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
6303 		req->num_rsscos_ctxs = cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
6304 	} else {
6305 		req->num_cmpl_rings = cpu_to_le16(cp_rings);
6306 		req->num_hw_ring_grps = cpu_to_le16(ring_grps);
6307 		req->num_rsscos_ctxs = cpu_to_le16(BNXT_VF_MAX_RSS_CTX);
6308 	}
6309 	req->num_stat_ctxs = cpu_to_le16(stats);
6310 	req->num_vnics = cpu_to_le16(vnics);
6311 
6312 	req->enables = cpu_to_le32(enables);
6313 	return req;
6314 }
6315 
6316 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)6317 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6318 			   int ring_grps, int cp_rings, int stats, int vnics)
6319 {
6320 	struct hwrm_func_cfg_input *req;
6321 	int rc;
6322 
6323 	req = __bnxt_hwrm_reserve_pf_rings(bp, tx_rings, rx_rings, ring_grps,
6324 					   cp_rings, stats, vnics);
6325 	if (!req)
6326 		return -ENOMEM;
6327 
6328 	if (!req->enables) {
6329 		hwrm_req_drop(bp, req);
6330 		return 0;
6331 	}
6332 
6333 	rc = hwrm_req_send(bp, req);
6334 	if (rc)
6335 		return rc;
6336 
6337 	if (bp->hwrm_spec_code < 0x10601)
6338 		bp->hw_resc.resv_tx_rings = tx_rings;
6339 
6340 	return bnxt_hwrm_get_rings(bp);
6341 }
6342 
6343 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)6344 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6345 			   int ring_grps, int cp_rings, int stats, int vnics)
6346 {
6347 	struct hwrm_func_vf_cfg_input *req;
6348 	int rc;
6349 
6350 	if (!BNXT_NEW_RM(bp)) {
6351 		bp->hw_resc.resv_tx_rings = tx_rings;
6352 		return 0;
6353 	}
6354 
6355 	req = __bnxt_hwrm_reserve_vf_rings(bp, tx_rings, rx_rings, ring_grps,
6356 					   cp_rings, stats, vnics);
6357 	if (!req)
6358 		return -ENOMEM;
6359 
6360 	rc = hwrm_req_send(bp, req);
6361 	if (rc)
6362 		return rc;
6363 
6364 	return bnxt_hwrm_get_rings(bp);
6365 }
6366 
bnxt_hwrm_reserve_rings(struct bnxt * bp,int tx,int rx,int grp,int cp,int stat,int vnic)6367 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, int tx, int rx, int grp,
6368 				   int cp, int stat, int vnic)
6369 {
6370 	if (BNXT_PF(bp))
6371 		return bnxt_hwrm_reserve_pf_rings(bp, tx, rx, grp, cp, stat,
6372 						  vnic);
6373 	else
6374 		return bnxt_hwrm_reserve_vf_rings(bp, tx, rx, grp, cp, stat,
6375 						  vnic);
6376 }
6377 
bnxt_nq_rings_in_use(struct bnxt * bp)6378 int bnxt_nq_rings_in_use(struct bnxt *bp)
6379 {
6380 	int cp = bp->cp_nr_rings;
6381 	int ulp_msix, ulp_base;
6382 
6383 	ulp_msix = bnxt_get_ulp_msix_num(bp);
6384 	if (ulp_msix) {
6385 		ulp_base = bnxt_get_ulp_msix_base(bp);
6386 		cp += ulp_msix;
6387 		if ((ulp_base + ulp_msix) > cp)
6388 			cp = ulp_base + ulp_msix;
6389 	}
6390 	return cp;
6391 }
6392 
bnxt_cp_rings_in_use(struct bnxt * bp)6393 static int bnxt_cp_rings_in_use(struct bnxt *bp)
6394 {
6395 	int cp;
6396 
6397 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6398 		return bnxt_nq_rings_in_use(bp);
6399 
6400 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
6401 	return cp;
6402 }
6403 
bnxt_get_func_stat_ctxs(struct bnxt * bp)6404 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
6405 {
6406 	int ulp_stat = bnxt_get_ulp_stat_ctxs(bp);
6407 	int cp = bp->cp_nr_rings;
6408 
6409 	if (!ulp_stat)
6410 		return cp;
6411 
6412 	if (bnxt_nq_rings_in_use(bp) > cp + bnxt_get_ulp_msix_num(bp))
6413 		return bnxt_get_ulp_msix_base(bp) + ulp_stat;
6414 
6415 	return cp + ulp_stat;
6416 }
6417 
6418 /* Check if a default RSS map needs to be setup.  This function is only
6419  * used on older firmware that does not require reserving RX rings.
6420  */
bnxt_check_rss_tbl_no_rmgr(struct bnxt * bp)6421 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
6422 {
6423 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6424 
6425 	/* The RSS map is valid for RX rings set to resv_rx_rings */
6426 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
6427 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
6428 		if (!netif_is_rxfh_configured(bp->dev))
6429 			bnxt_set_dflt_rss_indir_tbl(bp);
6430 	}
6431 }
6432 
bnxt_need_reserve_rings(struct bnxt * bp)6433 static bool bnxt_need_reserve_rings(struct bnxt *bp)
6434 {
6435 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6436 	int cp = bnxt_cp_rings_in_use(bp);
6437 	int nq = bnxt_nq_rings_in_use(bp);
6438 	int rx = bp->rx_nr_rings, stat;
6439 	int vnic = 1, grp = rx;
6440 
6441 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
6442 	    bp->hwrm_spec_code >= 0x10601)
6443 		return true;
6444 
6445 	/* Old firmware does not need RX ring reservations but we still
6446 	 * need to setup a default RSS map when needed.  With new firmware
6447 	 * we go through RX ring reservations first and then set up the
6448 	 * RSS map for the successfully reserved RX rings when needed.
6449 	 */
6450 	if (!BNXT_NEW_RM(bp)) {
6451 		bnxt_check_rss_tbl_no_rmgr(bp);
6452 		return false;
6453 	}
6454 	if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
6455 		vnic = rx + 1;
6456 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
6457 		rx <<= 1;
6458 	stat = bnxt_get_func_stat_ctxs(bp);
6459 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
6460 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
6461 	    (hw_resc->resv_hw_ring_grps != grp &&
6462 	     !(bp->flags & BNXT_FLAG_CHIP_P5)))
6463 		return true;
6464 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && BNXT_PF(bp) &&
6465 	    hw_resc->resv_irqs != nq)
6466 		return true;
6467 	return false;
6468 }
6469 
__bnxt_reserve_rings(struct bnxt * bp)6470 static int __bnxt_reserve_rings(struct bnxt *bp)
6471 {
6472 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6473 	int cp = bnxt_nq_rings_in_use(bp);
6474 	int tx = bp->tx_nr_rings;
6475 	int rx = bp->rx_nr_rings;
6476 	int grp, rx_rings, rc;
6477 	int vnic = 1, stat;
6478 	bool sh = false;
6479 
6480 	if (!bnxt_need_reserve_rings(bp))
6481 		return 0;
6482 
6483 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6484 		sh = true;
6485 	if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
6486 		vnic = rx + 1;
6487 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
6488 		rx <<= 1;
6489 	grp = bp->rx_nr_rings;
6490 	stat = bnxt_get_func_stat_ctxs(bp);
6491 
6492 	rc = bnxt_hwrm_reserve_rings(bp, tx, rx, grp, cp, stat, vnic);
6493 	if (rc)
6494 		return rc;
6495 
6496 	tx = hw_resc->resv_tx_rings;
6497 	if (BNXT_NEW_RM(bp)) {
6498 		rx = hw_resc->resv_rx_rings;
6499 		cp = hw_resc->resv_irqs;
6500 		grp = hw_resc->resv_hw_ring_grps;
6501 		vnic = hw_resc->resv_vnics;
6502 		stat = hw_resc->resv_stat_ctxs;
6503 	}
6504 
6505 	rx_rings = rx;
6506 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
6507 		if (rx >= 2) {
6508 			rx_rings = rx >> 1;
6509 		} else {
6510 			if (netif_running(bp->dev))
6511 				return -ENOMEM;
6512 
6513 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
6514 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
6515 			bp->dev->hw_features &= ~NETIF_F_LRO;
6516 			bp->dev->features &= ~NETIF_F_LRO;
6517 			bnxt_set_ring_params(bp);
6518 		}
6519 	}
6520 	rx_rings = min_t(int, rx_rings, grp);
6521 	cp = min_t(int, cp, bp->cp_nr_rings);
6522 	if (stat > bnxt_get_ulp_stat_ctxs(bp))
6523 		stat -= bnxt_get_ulp_stat_ctxs(bp);
6524 	cp = min_t(int, cp, stat);
6525 	rc = bnxt_trim_rings(bp, &rx_rings, &tx, cp, sh);
6526 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
6527 		rx = rx_rings << 1;
6528 	cp = sh ? max_t(int, tx, rx_rings) : tx + rx_rings;
6529 	bp->tx_nr_rings = tx;
6530 
6531 	/* If we cannot reserve all the RX rings, reset the RSS map only
6532 	 * if absolutely necessary
6533 	 */
6534 	if (rx_rings != bp->rx_nr_rings) {
6535 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
6536 			    rx_rings, bp->rx_nr_rings);
6537 		if (netif_is_rxfh_configured(bp->dev) &&
6538 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
6539 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
6540 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
6541 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
6542 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
6543 		}
6544 	}
6545 	bp->rx_nr_rings = rx_rings;
6546 	bp->cp_nr_rings = cp;
6547 
6548 	if (!tx || !rx || !cp || !grp || !vnic || !stat)
6549 		return -ENOMEM;
6550 
6551 	if (!netif_is_rxfh_configured(bp->dev))
6552 		bnxt_set_dflt_rss_indir_tbl(bp);
6553 
6554 	return rc;
6555 }
6556 
bnxt_hwrm_check_vf_rings(struct bnxt * bp,int tx_rings,int rx_rings,int ring_grps,int cp_rings,int stats,int vnics)6557 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6558 				    int ring_grps, int cp_rings, int stats,
6559 				    int vnics)
6560 {
6561 	struct hwrm_func_vf_cfg_input *req;
6562 	u32 flags;
6563 
6564 	if (!BNXT_NEW_RM(bp))
6565 		return 0;
6566 
6567 	req = __bnxt_hwrm_reserve_vf_rings(bp, tx_rings, rx_rings, ring_grps,
6568 					   cp_rings, stats, vnics);
6569 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
6570 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6571 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6572 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6573 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
6574 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
6575 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6576 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6577 
6578 	req->flags = cpu_to_le32(flags);
6579 	return hwrm_req_send_silent(bp, req);
6580 }
6581 
bnxt_hwrm_check_pf_rings(struct bnxt * bp,int tx_rings,int rx_rings,int ring_grps,int cp_rings,int stats,int vnics)6582 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6583 				    int ring_grps, int cp_rings, int stats,
6584 				    int vnics)
6585 {
6586 	struct hwrm_func_cfg_input *req;
6587 	u32 flags;
6588 
6589 	req = __bnxt_hwrm_reserve_pf_rings(bp, tx_rings, rx_rings, ring_grps,
6590 					   cp_rings, stats, vnics);
6591 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
6592 	if (BNXT_NEW_RM(bp)) {
6593 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6594 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6595 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6596 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
6597 		if (bp->flags & BNXT_FLAG_CHIP_P5)
6598 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
6599 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
6600 		else
6601 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6602 	}
6603 
6604 	req->flags = cpu_to_le32(flags);
6605 	return hwrm_req_send_silent(bp, req);
6606 }
6607 
bnxt_hwrm_check_rings(struct bnxt * bp,int tx_rings,int rx_rings,int ring_grps,int cp_rings,int stats,int vnics)6608 static int bnxt_hwrm_check_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6609 				 int ring_grps, int cp_rings, int stats,
6610 				 int vnics)
6611 {
6612 	if (bp->hwrm_spec_code < 0x10801)
6613 		return 0;
6614 
6615 	if (BNXT_PF(bp))
6616 		return bnxt_hwrm_check_pf_rings(bp, tx_rings, rx_rings,
6617 						ring_grps, cp_rings, stats,
6618 						vnics);
6619 
6620 	return bnxt_hwrm_check_vf_rings(bp, tx_rings, rx_rings, ring_grps,
6621 					cp_rings, stats, vnics);
6622 }
6623 
bnxt_hwrm_coal_params_qcaps(struct bnxt * bp)6624 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
6625 {
6626 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6627 	struct hwrm_ring_aggint_qcaps_output *resp;
6628 	struct hwrm_ring_aggint_qcaps_input *req;
6629 	int rc;
6630 
6631 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
6632 	coal_cap->num_cmpl_dma_aggr_max = 63;
6633 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
6634 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
6635 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
6636 	coal_cap->int_lat_tmr_min_max = 65535;
6637 	coal_cap->int_lat_tmr_max_max = 65535;
6638 	coal_cap->num_cmpl_aggr_int_max = 65535;
6639 	coal_cap->timer_units = 80;
6640 
6641 	if (bp->hwrm_spec_code < 0x10902)
6642 		return;
6643 
6644 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
6645 		return;
6646 
6647 	resp = hwrm_req_hold(bp, req);
6648 	rc = hwrm_req_send_silent(bp, req);
6649 	if (!rc) {
6650 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
6651 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
6652 		coal_cap->num_cmpl_dma_aggr_max =
6653 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
6654 		coal_cap->num_cmpl_dma_aggr_during_int_max =
6655 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
6656 		coal_cap->cmpl_aggr_dma_tmr_max =
6657 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
6658 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
6659 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
6660 		coal_cap->int_lat_tmr_min_max =
6661 			le16_to_cpu(resp->int_lat_tmr_min_max);
6662 		coal_cap->int_lat_tmr_max_max =
6663 			le16_to_cpu(resp->int_lat_tmr_max_max);
6664 		coal_cap->num_cmpl_aggr_int_max =
6665 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
6666 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
6667 	}
6668 	hwrm_req_drop(bp, req);
6669 }
6670 
bnxt_usec_to_coal_tmr(struct bnxt * bp,u16 usec)6671 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
6672 {
6673 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6674 
6675 	return usec * 1000 / coal_cap->timer_units;
6676 }
6677 
bnxt_hwrm_set_coal_params(struct bnxt * bp,struct bnxt_coal * hw_coal,struct hwrm_ring_cmpl_ring_cfg_aggint_params_input * req)6678 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
6679 	struct bnxt_coal *hw_coal,
6680 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
6681 {
6682 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6683 	u16 val, tmr, max, flags = hw_coal->flags;
6684 	u32 cmpl_params = coal_cap->cmpl_params;
6685 
6686 	max = hw_coal->bufs_per_record * 128;
6687 	if (hw_coal->budget)
6688 		max = hw_coal->bufs_per_record * hw_coal->budget;
6689 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
6690 
6691 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
6692 	req->num_cmpl_aggr_int = cpu_to_le16(val);
6693 
6694 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
6695 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
6696 
6697 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
6698 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
6699 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
6700 
6701 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
6702 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
6703 	req->int_lat_tmr_max = cpu_to_le16(tmr);
6704 
6705 	/* min timer set to 1/2 of interrupt timer */
6706 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
6707 		val = tmr / 2;
6708 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
6709 		req->int_lat_tmr_min = cpu_to_le16(val);
6710 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6711 	}
6712 
6713 	/* buf timer set to 1/4 of interrupt timer */
6714 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
6715 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
6716 
6717 	if (cmpl_params &
6718 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
6719 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
6720 		val = clamp_t(u16, tmr, 1,
6721 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
6722 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
6723 		req->enables |=
6724 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
6725 	}
6726 
6727 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
6728 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
6729 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
6730 	req->flags = cpu_to_le16(flags);
6731 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
6732 }
6733 
__bnxt_hwrm_set_coal_nq(struct bnxt * bp,struct bnxt_napi * bnapi,struct bnxt_coal * hw_coal)6734 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
6735 				   struct bnxt_coal *hw_coal)
6736 {
6737 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
6738 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6739 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6740 	u32 nq_params = coal_cap->nq_params;
6741 	u16 tmr;
6742 	int rc;
6743 
6744 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
6745 		return 0;
6746 
6747 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
6748 	if (rc)
6749 		return rc;
6750 
6751 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
6752 	req->flags =
6753 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
6754 
6755 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
6756 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
6757 	req->int_lat_tmr_min = cpu_to_le16(tmr);
6758 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6759 	return hwrm_req_send(bp, req);
6760 }
6761 
bnxt_hwrm_set_ring_coal(struct bnxt * bp,struct bnxt_napi * bnapi)6762 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
6763 {
6764 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
6765 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6766 	struct bnxt_coal coal;
6767 	int rc;
6768 
6769 	/* Tick values in micro seconds.
6770 	 * 1 coal_buf x bufs_per_record = 1 completion record.
6771 	 */
6772 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
6773 
6774 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
6775 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
6776 
6777 	if (!bnapi->rx_ring)
6778 		return -ENODEV;
6779 
6780 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
6781 	if (rc)
6782 		return rc;
6783 
6784 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
6785 
6786 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
6787 
6788 	return hwrm_req_send(bp, req_rx);
6789 }
6790 
bnxt_hwrm_set_coal(struct bnxt * bp)6791 int bnxt_hwrm_set_coal(struct bnxt *bp)
6792 {
6793 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx,
6794 							   *req;
6795 	int i, rc;
6796 
6797 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
6798 	if (rc)
6799 		return rc;
6800 
6801 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
6802 	if (rc) {
6803 		hwrm_req_drop(bp, req_rx);
6804 		return rc;
6805 	}
6806 
6807 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
6808 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
6809 
6810 	hwrm_req_hold(bp, req_rx);
6811 	hwrm_req_hold(bp, req_tx);
6812 	for (i = 0; i < bp->cp_nr_rings; i++) {
6813 		struct bnxt_napi *bnapi = bp->bnapi[i];
6814 		struct bnxt_coal *hw_coal;
6815 		u16 ring_id;
6816 
6817 		req = req_rx;
6818 		if (!bnapi->rx_ring) {
6819 			ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6820 			req = req_tx;
6821 		} else {
6822 			ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
6823 		}
6824 		req->ring_id = cpu_to_le16(ring_id);
6825 
6826 		rc = hwrm_req_send(bp, req);
6827 		if (rc)
6828 			break;
6829 
6830 		if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6831 			continue;
6832 
6833 		if (bnapi->rx_ring && bnapi->tx_ring) {
6834 			req = req_tx;
6835 			ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6836 			req->ring_id = cpu_to_le16(ring_id);
6837 			rc = hwrm_req_send(bp, req);
6838 			if (rc)
6839 				break;
6840 		}
6841 		if (bnapi->rx_ring)
6842 			hw_coal = &bp->rx_coal;
6843 		else
6844 			hw_coal = &bp->tx_coal;
6845 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
6846 	}
6847 	hwrm_req_drop(bp, req_rx);
6848 	hwrm_req_drop(bp, req_tx);
6849 	return rc;
6850 }
6851 
bnxt_hwrm_stat_ctx_free(struct bnxt * bp)6852 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
6853 {
6854 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
6855 	struct hwrm_stat_ctx_free_input *req;
6856 	int i;
6857 
6858 	if (!bp->bnapi)
6859 		return;
6860 
6861 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6862 		return;
6863 
6864 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
6865 		return;
6866 	if (BNXT_FW_MAJ(bp) <= 20) {
6867 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
6868 			hwrm_req_drop(bp, req);
6869 			return;
6870 		}
6871 		hwrm_req_hold(bp, req0);
6872 	}
6873 	hwrm_req_hold(bp, req);
6874 	for (i = 0; i < bp->cp_nr_rings; i++) {
6875 		struct bnxt_napi *bnapi = bp->bnapi[i];
6876 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6877 
6878 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
6879 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
6880 			if (req0) {
6881 				req0->stat_ctx_id = req->stat_ctx_id;
6882 				hwrm_req_send(bp, req0);
6883 			}
6884 			hwrm_req_send(bp, req);
6885 
6886 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
6887 		}
6888 	}
6889 	hwrm_req_drop(bp, req);
6890 	if (req0)
6891 		hwrm_req_drop(bp, req0);
6892 }
6893 
bnxt_hwrm_stat_ctx_alloc(struct bnxt * bp)6894 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
6895 {
6896 	struct hwrm_stat_ctx_alloc_output *resp;
6897 	struct hwrm_stat_ctx_alloc_input *req;
6898 	int rc, i;
6899 
6900 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6901 		return 0;
6902 
6903 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
6904 	if (rc)
6905 		return rc;
6906 
6907 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
6908 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
6909 
6910 	resp = hwrm_req_hold(bp, req);
6911 	for (i = 0; i < bp->cp_nr_rings; i++) {
6912 		struct bnxt_napi *bnapi = bp->bnapi[i];
6913 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6914 
6915 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
6916 
6917 		rc = hwrm_req_send(bp, req);
6918 		if (rc)
6919 			break;
6920 
6921 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
6922 
6923 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
6924 	}
6925 	hwrm_req_drop(bp, req);
6926 	return rc;
6927 }
6928 
bnxt_hwrm_func_qcfg(struct bnxt * bp)6929 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
6930 {
6931 	struct hwrm_func_qcfg_output *resp;
6932 	struct hwrm_func_qcfg_input *req;
6933 	u32 min_db_offset = 0;
6934 	u16 flags;
6935 	int rc;
6936 
6937 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
6938 	if (rc)
6939 		return rc;
6940 
6941 	req->fid = cpu_to_le16(0xffff);
6942 	resp = hwrm_req_hold(bp, req);
6943 	rc = hwrm_req_send(bp, req);
6944 	if (rc)
6945 		goto func_qcfg_exit;
6946 
6947 #ifdef CONFIG_BNXT_SRIOV
6948 	if (BNXT_VF(bp)) {
6949 		struct bnxt_vf_info *vf = &bp->vf;
6950 
6951 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
6952 	} else {
6953 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
6954 	}
6955 #endif
6956 	flags = le16_to_cpu(resp->flags);
6957 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
6958 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
6959 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
6960 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
6961 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
6962 	}
6963 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
6964 		bp->flags |= BNXT_FLAG_MULTI_HOST;
6965 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
6966 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
6967 
6968 	switch (resp->port_partition_type) {
6969 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
6970 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
6971 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
6972 		bp->port_partition_type = resp->port_partition_type;
6973 		break;
6974 	}
6975 	if (bp->hwrm_spec_code < 0x10707 ||
6976 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
6977 		bp->br_mode = BRIDGE_MODE_VEB;
6978 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
6979 		bp->br_mode = BRIDGE_MODE_VEPA;
6980 	else
6981 		bp->br_mode = BRIDGE_MODE_UNDEF;
6982 
6983 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
6984 	if (!bp->max_mtu)
6985 		bp->max_mtu = BNXT_MAX_MTU;
6986 
6987 	if (bp->db_size)
6988 		goto func_qcfg_exit;
6989 
6990 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
6991 		if (BNXT_PF(bp))
6992 			min_db_offset = DB_PF_OFFSET_P5;
6993 		else
6994 			min_db_offset = DB_VF_OFFSET_P5;
6995 	}
6996 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
6997 				 1024);
6998 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
6999 	    bp->db_size <= min_db_offset)
7000 		bp->db_size = pci_resource_len(bp->pdev, 2);
7001 
7002 func_qcfg_exit:
7003 	hwrm_req_drop(bp, req);
7004 	return rc;
7005 }
7006 
bnxt_init_ctx_initializer(struct bnxt_ctx_mem_info * ctx,struct hwrm_func_backing_store_qcaps_output * resp)7007 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_info *ctx,
7008 			struct hwrm_func_backing_store_qcaps_output *resp)
7009 {
7010 	struct bnxt_mem_init *mem_init;
7011 	u16 init_mask;
7012 	u8 init_val;
7013 	u8 *offset;
7014 	int i;
7015 
7016 	init_val = resp->ctx_kind_initializer;
7017 	init_mask = le16_to_cpu(resp->ctx_init_mask);
7018 	offset = &resp->qp_init_offset;
7019 	mem_init = &ctx->mem_init[BNXT_CTX_MEM_INIT_QP];
7020 	for (i = 0; i < BNXT_CTX_MEM_INIT_MAX; i++, mem_init++, offset++) {
7021 		mem_init->init_val = init_val;
7022 		mem_init->offset = BNXT_MEM_INVALID_OFFSET;
7023 		if (!init_mask)
7024 			continue;
7025 		if (i == BNXT_CTX_MEM_INIT_STAT)
7026 			offset = &resp->stat_init_offset;
7027 		if (init_mask & (1 << i))
7028 			mem_init->offset = *offset * 4;
7029 		else
7030 			mem_init->init_val = 0;
7031 	}
7032 	ctx->mem_init[BNXT_CTX_MEM_INIT_QP].size = ctx->qp_entry_size;
7033 	ctx->mem_init[BNXT_CTX_MEM_INIT_SRQ].size = ctx->srq_entry_size;
7034 	ctx->mem_init[BNXT_CTX_MEM_INIT_CQ].size = ctx->cq_entry_size;
7035 	ctx->mem_init[BNXT_CTX_MEM_INIT_VNIC].size = ctx->vnic_entry_size;
7036 	ctx->mem_init[BNXT_CTX_MEM_INIT_STAT].size = ctx->stat_entry_size;
7037 	ctx->mem_init[BNXT_CTX_MEM_INIT_MRAV].size = ctx->mrav_entry_size;
7038 }
7039 
bnxt_hwrm_func_backing_store_qcaps(struct bnxt * bp)7040 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
7041 {
7042 	struct hwrm_func_backing_store_qcaps_output *resp;
7043 	struct hwrm_func_backing_store_qcaps_input *req;
7044 	int rc;
7045 
7046 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
7047 		return 0;
7048 
7049 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
7050 	if (rc)
7051 		return rc;
7052 
7053 	resp = hwrm_req_hold(bp, req);
7054 	rc = hwrm_req_send_silent(bp, req);
7055 	if (!rc) {
7056 		struct bnxt_ctx_pg_info *ctx_pg;
7057 		struct bnxt_ctx_mem_info *ctx;
7058 		int i, tqm_rings;
7059 
7060 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
7061 		if (!ctx) {
7062 			rc = -ENOMEM;
7063 			goto ctx_err;
7064 		}
7065 		ctx->qp_max_entries = le32_to_cpu(resp->qp_max_entries);
7066 		ctx->qp_min_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
7067 		ctx->qp_max_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
7068 		ctx->qp_entry_size = le16_to_cpu(resp->qp_entry_size);
7069 		ctx->srq_max_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
7070 		ctx->srq_max_entries = le32_to_cpu(resp->srq_max_entries);
7071 		ctx->srq_entry_size = le16_to_cpu(resp->srq_entry_size);
7072 		ctx->cq_max_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
7073 		ctx->cq_max_entries = le32_to_cpu(resp->cq_max_entries);
7074 		ctx->cq_entry_size = le16_to_cpu(resp->cq_entry_size);
7075 		ctx->vnic_max_vnic_entries =
7076 			le16_to_cpu(resp->vnic_max_vnic_entries);
7077 		ctx->vnic_max_ring_table_entries =
7078 			le16_to_cpu(resp->vnic_max_ring_table_entries);
7079 		ctx->vnic_entry_size = le16_to_cpu(resp->vnic_entry_size);
7080 		ctx->stat_max_entries = le32_to_cpu(resp->stat_max_entries);
7081 		ctx->stat_entry_size = le16_to_cpu(resp->stat_entry_size);
7082 		ctx->tqm_entry_size = le16_to_cpu(resp->tqm_entry_size);
7083 		ctx->tqm_min_entries_per_ring =
7084 			le32_to_cpu(resp->tqm_min_entries_per_ring);
7085 		ctx->tqm_max_entries_per_ring =
7086 			le32_to_cpu(resp->tqm_max_entries_per_ring);
7087 		ctx->tqm_entries_multiple = resp->tqm_entries_multiple;
7088 		if (!ctx->tqm_entries_multiple)
7089 			ctx->tqm_entries_multiple = 1;
7090 		ctx->mrav_max_entries = le32_to_cpu(resp->mrav_max_entries);
7091 		ctx->mrav_entry_size = le16_to_cpu(resp->mrav_entry_size);
7092 		ctx->mrav_num_entries_units =
7093 			le16_to_cpu(resp->mrav_num_entries_units);
7094 		ctx->tim_entry_size = le16_to_cpu(resp->tim_entry_size);
7095 		ctx->tim_max_entries = le32_to_cpu(resp->tim_max_entries);
7096 
7097 		bnxt_init_ctx_initializer(ctx, resp);
7098 
7099 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
7100 		if (!ctx->tqm_fp_rings_count)
7101 			ctx->tqm_fp_rings_count = bp->max_q;
7102 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
7103 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
7104 
7105 		tqm_rings = ctx->tqm_fp_rings_count + BNXT_MAX_TQM_SP_RINGS;
7106 		ctx_pg = kcalloc(tqm_rings, sizeof(*ctx_pg), GFP_KERNEL);
7107 		if (!ctx_pg) {
7108 			kfree(ctx);
7109 			rc = -ENOMEM;
7110 			goto ctx_err;
7111 		}
7112 		for (i = 0; i < tqm_rings; i++, ctx_pg++)
7113 			ctx->tqm_mem[i] = ctx_pg;
7114 		bp->ctx = ctx;
7115 	} else {
7116 		rc = 0;
7117 	}
7118 ctx_err:
7119 	hwrm_req_drop(bp, req);
7120 	return rc;
7121 }
7122 
bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info * rmem,u8 * pg_attr,__le64 * pg_dir)7123 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
7124 				  __le64 *pg_dir)
7125 {
7126 	if (!rmem->nr_pages)
7127 		return;
7128 
7129 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
7130 	if (rmem->depth >= 1) {
7131 		if (rmem->depth == 2)
7132 			*pg_attr |= 2;
7133 		else
7134 			*pg_attr |= 1;
7135 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
7136 	} else {
7137 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
7138 	}
7139 }
7140 
7141 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
7142 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
7143 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
7144 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
7145 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
7146 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
7147 
bnxt_hwrm_func_backing_store_cfg(struct bnxt * bp,u32 enables)7148 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
7149 {
7150 	struct hwrm_func_backing_store_cfg_input *req;
7151 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
7152 	struct bnxt_ctx_pg_info *ctx_pg;
7153 	void **__req = (void **)&req;
7154 	u32 req_len = sizeof(*req);
7155 	__le32 *num_entries;
7156 	__le64 *pg_dir;
7157 	u32 flags = 0;
7158 	u8 *pg_attr;
7159 	u32 ena;
7160 	int rc;
7161 	int i;
7162 
7163 	if (!ctx)
7164 		return 0;
7165 
7166 	if (req_len > bp->hwrm_max_ext_req_len)
7167 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
7168 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
7169 	if (rc)
7170 		return rc;
7171 
7172 	req->enables = cpu_to_le32(enables);
7173 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
7174 		ctx_pg = &ctx->qp_mem;
7175 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
7176 		req->qp_num_qp1_entries = cpu_to_le16(ctx->qp_min_qp1_entries);
7177 		req->qp_num_l2_entries = cpu_to_le16(ctx->qp_max_l2_entries);
7178 		req->qp_entry_size = cpu_to_le16(ctx->qp_entry_size);
7179 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7180 				      &req->qpc_pg_size_qpc_lvl,
7181 				      &req->qpc_page_dir);
7182 	}
7183 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
7184 		ctx_pg = &ctx->srq_mem;
7185 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
7186 		req->srq_num_l2_entries = cpu_to_le16(ctx->srq_max_l2_entries);
7187 		req->srq_entry_size = cpu_to_le16(ctx->srq_entry_size);
7188 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7189 				      &req->srq_pg_size_srq_lvl,
7190 				      &req->srq_page_dir);
7191 	}
7192 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
7193 		ctx_pg = &ctx->cq_mem;
7194 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
7195 		req->cq_num_l2_entries = cpu_to_le16(ctx->cq_max_l2_entries);
7196 		req->cq_entry_size = cpu_to_le16(ctx->cq_entry_size);
7197 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7198 				      &req->cq_pg_size_cq_lvl,
7199 				      &req->cq_page_dir);
7200 	}
7201 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
7202 		ctx_pg = &ctx->vnic_mem;
7203 		req->vnic_num_vnic_entries =
7204 			cpu_to_le16(ctx->vnic_max_vnic_entries);
7205 		req->vnic_num_ring_table_entries =
7206 			cpu_to_le16(ctx->vnic_max_ring_table_entries);
7207 		req->vnic_entry_size = cpu_to_le16(ctx->vnic_entry_size);
7208 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7209 				      &req->vnic_pg_size_vnic_lvl,
7210 				      &req->vnic_page_dir);
7211 	}
7212 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
7213 		ctx_pg = &ctx->stat_mem;
7214 		req->stat_num_entries = cpu_to_le32(ctx->stat_max_entries);
7215 		req->stat_entry_size = cpu_to_le16(ctx->stat_entry_size);
7216 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7217 				      &req->stat_pg_size_stat_lvl,
7218 				      &req->stat_page_dir);
7219 	}
7220 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
7221 		ctx_pg = &ctx->mrav_mem;
7222 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
7223 		if (ctx->mrav_num_entries_units)
7224 			flags |=
7225 			FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
7226 		req->mrav_entry_size = cpu_to_le16(ctx->mrav_entry_size);
7227 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7228 				      &req->mrav_pg_size_mrav_lvl,
7229 				      &req->mrav_page_dir);
7230 	}
7231 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
7232 		ctx_pg = &ctx->tim_mem;
7233 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
7234 		req->tim_entry_size = cpu_to_le16(ctx->tim_entry_size);
7235 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7236 				      &req->tim_pg_size_tim_lvl,
7237 				      &req->tim_page_dir);
7238 	}
7239 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
7240 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
7241 	     pg_dir = &req->tqm_sp_page_dir,
7242 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP;
7243 	     i < BNXT_MAX_TQM_RINGS;
7244 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
7245 		if (!(enables & ena))
7246 			continue;
7247 
7248 		req->tqm_entry_size = cpu_to_le16(ctx->tqm_entry_size);
7249 		ctx_pg = ctx->tqm_mem[i];
7250 		*num_entries = cpu_to_le32(ctx_pg->entries);
7251 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
7252 	}
7253 	req->flags = cpu_to_le32(flags);
7254 	return hwrm_req_send(bp, req);
7255 }
7256 
bnxt_alloc_ctx_mem_blk(struct bnxt * bp,struct bnxt_ctx_pg_info * ctx_pg)7257 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
7258 				  struct bnxt_ctx_pg_info *ctx_pg)
7259 {
7260 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7261 
7262 	rmem->page_size = BNXT_PAGE_SIZE;
7263 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
7264 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
7265 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
7266 	if (rmem->depth >= 1)
7267 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
7268 	return bnxt_alloc_ring(bp, rmem);
7269 }
7270 
bnxt_alloc_ctx_pg_tbls(struct bnxt * bp,struct bnxt_ctx_pg_info * ctx_pg,u32 mem_size,u8 depth,struct bnxt_mem_init * mem_init)7271 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
7272 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
7273 				  u8 depth, struct bnxt_mem_init *mem_init)
7274 {
7275 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7276 	int rc;
7277 
7278 	if (!mem_size)
7279 		return -EINVAL;
7280 
7281 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
7282 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
7283 		ctx_pg->nr_pages = 0;
7284 		return -EINVAL;
7285 	}
7286 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
7287 		int nr_tbls, i;
7288 
7289 		rmem->depth = 2;
7290 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
7291 					     GFP_KERNEL);
7292 		if (!ctx_pg->ctx_pg_tbl)
7293 			return -ENOMEM;
7294 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
7295 		rmem->nr_pages = nr_tbls;
7296 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
7297 		if (rc)
7298 			return rc;
7299 		for (i = 0; i < nr_tbls; i++) {
7300 			struct bnxt_ctx_pg_info *pg_tbl;
7301 
7302 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
7303 			if (!pg_tbl)
7304 				return -ENOMEM;
7305 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
7306 			rmem = &pg_tbl->ring_mem;
7307 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
7308 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
7309 			rmem->depth = 1;
7310 			rmem->nr_pages = MAX_CTX_PAGES;
7311 			rmem->mem_init = mem_init;
7312 			if (i == (nr_tbls - 1)) {
7313 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
7314 
7315 				if (rem)
7316 					rmem->nr_pages = rem;
7317 			}
7318 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
7319 			if (rc)
7320 				break;
7321 		}
7322 	} else {
7323 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
7324 		if (rmem->nr_pages > 1 || depth)
7325 			rmem->depth = 1;
7326 		rmem->mem_init = mem_init;
7327 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
7328 	}
7329 	return rc;
7330 }
7331 
bnxt_free_ctx_pg_tbls(struct bnxt * bp,struct bnxt_ctx_pg_info * ctx_pg)7332 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
7333 				  struct bnxt_ctx_pg_info *ctx_pg)
7334 {
7335 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7336 
7337 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
7338 	    ctx_pg->ctx_pg_tbl) {
7339 		int i, nr_tbls = rmem->nr_pages;
7340 
7341 		for (i = 0; i < nr_tbls; i++) {
7342 			struct bnxt_ctx_pg_info *pg_tbl;
7343 			struct bnxt_ring_mem_info *rmem2;
7344 
7345 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
7346 			if (!pg_tbl)
7347 				continue;
7348 			rmem2 = &pg_tbl->ring_mem;
7349 			bnxt_free_ring(bp, rmem2);
7350 			ctx_pg->ctx_pg_arr[i] = NULL;
7351 			kfree(pg_tbl);
7352 			ctx_pg->ctx_pg_tbl[i] = NULL;
7353 		}
7354 		kfree(ctx_pg->ctx_pg_tbl);
7355 		ctx_pg->ctx_pg_tbl = NULL;
7356 	}
7357 	bnxt_free_ring(bp, rmem);
7358 	ctx_pg->nr_pages = 0;
7359 }
7360 
bnxt_free_ctx_mem(struct bnxt * bp)7361 void bnxt_free_ctx_mem(struct bnxt *bp)
7362 {
7363 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
7364 	int i;
7365 
7366 	if (!ctx)
7367 		return;
7368 
7369 	if (ctx->tqm_mem[0]) {
7370 		for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
7371 			bnxt_free_ctx_pg_tbls(bp, ctx->tqm_mem[i]);
7372 		kfree(ctx->tqm_mem[0]);
7373 		ctx->tqm_mem[0] = NULL;
7374 	}
7375 
7376 	bnxt_free_ctx_pg_tbls(bp, &ctx->tim_mem);
7377 	bnxt_free_ctx_pg_tbls(bp, &ctx->mrav_mem);
7378 	bnxt_free_ctx_pg_tbls(bp, &ctx->stat_mem);
7379 	bnxt_free_ctx_pg_tbls(bp, &ctx->vnic_mem);
7380 	bnxt_free_ctx_pg_tbls(bp, &ctx->cq_mem);
7381 	bnxt_free_ctx_pg_tbls(bp, &ctx->srq_mem);
7382 	bnxt_free_ctx_pg_tbls(bp, &ctx->qp_mem);
7383 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
7384 }
7385 
bnxt_alloc_ctx_mem(struct bnxt * bp)7386 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
7387 {
7388 	struct bnxt_ctx_pg_info *ctx_pg;
7389 	struct bnxt_ctx_mem_info *ctx;
7390 	struct bnxt_mem_init *init;
7391 	u32 mem_size, ena, entries;
7392 	u32 entries_sp, min;
7393 	u32 num_mr, num_ah;
7394 	u32 extra_srqs = 0;
7395 	u32 extra_qps = 0;
7396 	u8 pg_lvl = 1;
7397 	int i, rc;
7398 
7399 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
7400 	if (rc) {
7401 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
7402 			   rc);
7403 		return rc;
7404 	}
7405 	ctx = bp->ctx;
7406 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
7407 		return 0;
7408 
7409 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
7410 		pg_lvl = 2;
7411 		extra_qps = 65536;
7412 		extra_srqs = 8192;
7413 	}
7414 
7415 	ctx_pg = &ctx->qp_mem;
7416 	ctx_pg->entries = ctx->qp_min_qp1_entries + ctx->qp_max_l2_entries +
7417 			  extra_qps;
7418 	if (ctx->qp_entry_size) {
7419 		mem_size = ctx->qp_entry_size * ctx_pg->entries;
7420 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_QP];
7421 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init);
7422 		if (rc)
7423 			return rc;
7424 	}
7425 
7426 	ctx_pg = &ctx->srq_mem;
7427 	ctx_pg->entries = ctx->srq_max_l2_entries + extra_srqs;
7428 	if (ctx->srq_entry_size) {
7429 		mem_size = ctx->srq_entry_size * ctx_pg->entries;
7430 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_SRQ];
7431 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init);
7432 		if (rc)
7433 			return rc;
7434 	}
7435 
7436 	ctx_pg = &ctx->cq_mem;
7437 	ctx_pg->entries = ctx->cq_max_l2_entries + extra_qps * 2;
7438 	if (ctx->cq_entry_size) {
7439 		mem_size = ctx->cq_entry_size * ctx_pg->entries;
7440 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_CQ];
7441 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init);
7442 		if (rc)
7443 			return rc;
7444 	}
7445 
7446 	ctx_pg = &ctx->vnic_mem;
7447 	ctx_pg->entries = ctx->vnic_max_vnic_entries +
7448 			  ctx->vnic_max_ring_table_entries;
7449 	if (ctx->vnic_entry_size) {
7450 		mem_size = ctx->vnic_entry_size * ctx_pg->entries;
7451 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_VNIC];
7452 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, init);
7453 		if (rc)
7454 			return rc;
7455 	}
7456 
7457 	ctx_pg = &ctx->stat_mem;
7458 	ctx_pg->entries = ctx->stat_max_entries;
7459 	if (ctx->stat_entry_size) {
7460 		mem_size = ctx->stat_entry_size * ctx_pg->entries;
7461 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_STAT];
7462 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, init);
7463 		if (rc)
7464 			return rc;
7465 	}
7466 
7467 	ena = 0;
7468 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
7469 		goto skip_rdma;
7470 
7471 	ctx_pg = &ctx->mrav_mem;
7472 	/* 128K extra is needed to accommodate static AH context
7473 	 * allocation by f/w.
7474 	 */
7475 	num_mr = 1024 * 256;
7476 	num_ah = 1024 * 128;
7477 	ctx_pg->entries = num_mr + num_ah;
7478 	if (ctx->mrav_entry_size) {
7479 		mem_size = ctx->mrav_entry_size * ctx_pg->entries;
7480 		init = &ctx->mem_init[BNXT_CTX_MEM_INIT_MRAV];
7481 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 2, init);
7482 		if (rc)
7483 			return rc;
7484 	}
7485 	ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
7486 	if (ctx->mrav_num_entries_units)
7487 		ctx_pg->entries =
7488 			((num_mr / ctx->mrav_num_entries_units) << 16) |
7489 			 (num_ah / ctx->mrav_num_entries_units);
7490 
7491 	ctx_pg = &ctx->tim_mem;
7492 	ctx_pg->entries = ctx->qp_mem.entries;
7493 	if (ctx->tim_entry_size) {
7494 		mem_size = ctx->tim_entry_size * ctx_pg->entries;
7495 		rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, NULL);
7496 		if (rc)
7497 			return rc;
7498 	}
7499 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
7500 
7501 skip_rdma:
7502 	min = ctx->tqm_min_entries_per_ring;
7503 	entries_sp = ctx->vnic_max_vnic_entries + ctx->qp_max_l2_entries +
7504 		     2 * (extra_qps + ctx->qp_min_qp1_entries) + min;
7505 	entries_sp = roundup(entries_sp, ctx->tqm_entries_multiple);
7506 	entries = ctx->qp_max_l2_entries + 2 * (extra_qps + ctx->qp_min_qp1_entries);
7507 	entries = roundup(entries, ctx->tqm_entries_multiple);
7508 	entries = clamp_t(u32, entries, min, ctx->tqm_max_entries_per_ring);
7509 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++) {
7510 		ctx_pg = ctx->tqm_mem[i];
7511 		ctx_pg->entries = i ? entries : entries_sp;
7512 		if (ctx->tqm_entry_size) {
7513 			mem_size = ctx->tqm_entry_size * ctx_pg->entries;
7514 			rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1,
7515 						    NULL);
7516 			if (rc)
7517 				return rc;
7518 		}
7519 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
7520 	}
7521 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
7522 	rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
7523 	if (rc) {
7524 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
7525 			   rc);
7526 		return rc;
7527 	}
7528 	ctx->flags |= BNXT_CTX_FLAG_INITED;
7529 	return 0;
7530 }
7531 
bnxt_hwrm_func_resc_qcaps(struct bnxt * bp,bool all)7532 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
7533 {
7534 	struct hwrm_func_resource_qcaps_output *resp;
7535 	struct hwrm_func_resource_qcaps_input *req;
7536 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7537 	int rc;
7538 
7539 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
7540 	if (rc)
7541 		return rc;
7542 
7543 	req->fid = cpu_to_le16(0xffff);
7544 	resp = hwrm_req_hold(bp, req);
7545 	rc = hwrm_req_send_silent(bp, req);
7546 	if (rc)
7547 		goto hwrm_func_resc_qcaps_exit;
7548 
7549 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
7550 	if (!all)
7551 		goto hwrm_func_resc_qcaps_exit;
7552 
7553 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
7554 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
7555 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
7556 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
7557 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
7558 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
7559 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
7560 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
7561 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
7562 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
7563 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
7564 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
7565 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
7566 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
7567 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
7568 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
7569 
7570 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
7571 		u16 max_msix = le16_to_cpu(resp->max_msix);
7572 
7573 		hw_resc->max_nqs = max_msix;
7574 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
7575 	}
7576 
7577 	if (BNXT_PF(bp)) {
7578 		struct bnxt_pf_info *pf = &bp->pf;
7579 
7580 		pf->vf_resv_strategy =
7581 			le16_to_cpu(resp->vf_reservation_strategy);
7582 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
7583 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
7584 	}
7585 hwrm_func_resc_qcaps_exit:
7586 	hwrm_req_drop(bp, req);
7587 	return rc;
7588 }
7589 
__bnxt_hwrm_ptp_qcfg(struct bnxt * bp)7590 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
7591 {
7592 	struct hwrm_port_mac_ptp_qcfg_output *resp;
7593 	struct hwrm_port_mac_ptp_qcfg_input *req;
7594 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
7595 	bool phc_cfg;
7596 	u8 flags;
7597 	int rc;
7598 
7599 	if (bp->hwrm_spec_code < 0x10801) {
7600 		rc = -ENODEV;
7601 		goto no_ptp;
7602 	}
7603 
7604 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
7605 	if (rc)
7606 		goto no_ptp;
7607 
7608 	req->port_id = cpu_to_le16(bp->pf.port_id);
7609 	resp = hwrm_req_hold(bp, req);
7610 	rc = hwrm_req_send(bp, req);
7611 	if (rc)
7612 		goto exit;
7613 
7614 	flags = resp->flags;
7615 	if (!(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
7616 		rc = -ENODEV;
7617 		goto exit;
7618 	}
7619 	if (!ptp) {
7620 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
7621 		if (!ptp) {
7622 			rc = -ENOMEM;
7623 			goto exit;
7624 		}
7625 		ptp->bp = bp;
7626 		bp->ptp_cfg = ptp;
7627 	}
7628 	if (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK) {
7629 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
7630 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
7631 	} else if (bp->flags & BNXT_FLAG_CHIP_P5) {
7632 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
7633 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
7634 	} else {
7635 		rc = -ENODEV;
7636 		goto exit;
7637 	}
7638 	phc_cfg = (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
7639 	rc = bnxt_ptp_init(bp, phc_cfg);
7640 	if (rc)
7641 		netdev_warn(bp->dev, "PTP initialization failed.\n");
7642 exit:
7643 	hwrm_req_drop(bp, req);
7644 	if (!rc)
7645 		return 0;
7646 
7647 no_ptp:
7648 	bnxt_ptp_clear(bp);
7649 	kfree(ptp);
7650 	bp->ptp_cfg = NULL;
7651 	return rc;
7652 }
7653 
__bnxt_hwrm_func_qcaps(struct bnxt * bp)7654 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
7655 {
7656 	struct hwrm_func_qcaps_output *resp;
7657 	struct hwrm_func_qcaps_input *req;
7658 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7659 	u32 flags, flags_ext, flags_ext2;
7660 	int rc;
7661 
7662 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
7663 	if (rc)
7664 		return rc;
7665 
7666 	req->fid = cpu_to_le16(0xffff);
7667 	resp = hwrm_req_hold(bp, req);
7668 	rc = hwrm_req_send(bp, req);
7669 	if (rc)
7670 		goto hwrm_func_qcaps_exit;
7671 
7672 	flags = le32_to_cpu(resp->flags);
7673 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
7674 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
7675 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
7676 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
7677 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
7678 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
7679 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
7680 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
7681 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
7682 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
7683 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
7684 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
7685 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
7686 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
7687 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
7688 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
7689 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
7690 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
7691 
7692 	flags_ext = le32_to_cpu(resp->flags_ext);
7693 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
7694 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
7695 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
7696 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
7697 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
7698 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
7699 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
7700 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
7701 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
7702 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
7703 
7704 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
7705 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
7706 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
7707 
7708 	bp->tx_push_thresh = 0;
7709 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
7710 	    BNXT_FW_MAJ(bp) > 217)
7711 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
7712 
7713 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
7714 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
7715 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
7716 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
7717 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
7718 	if (!hw_resc->max_hw_ring_grps)
7719 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
7720 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
7721 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
7722 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
7723 
7724 	if (BNXT_PF(bp)) {
7725 		struct bnxt_pf_info *pf = &bp->pf;
7726 
7727 		pf->fw_fid = le16_to_cpu(resp->fid);
7728 		pf->port_id = le16_to_cpu(resp->port_id);
7729 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
7730 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
7731 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
7732 		pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
7733 		pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
7734 		pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
7735 		pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
7736 		pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
7737 		pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
7738 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
7739 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
7740 			bp->flags |= BNXT_FLAG_WOL_CAP;
7741 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
7742 			__bnxt_hwrm_ptp_qcfg(bp);
7743 		} else {
7744 			bnxt_ptp_clear(bp);
7745 			kfree(bp->ptp_cfg);
7746 			bp->ptp_cfg = NULL;
7747 		}
7748 	} else {
7749 #ifdef CONFIG_BNXT_SRIOV
7750 		struct bnxt_vf_info *vf = &bp->vf;
7751 
7752 		vf->fw_fid = le16_to_cpu(resp->fid);
7753 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
7754 #endif
7755 	}
7756 
7757 hwrm_func_qcaps_exit:
7758 	hwrm_req_drop(bp, req);
7759 	return rc;
7760 }
7761 
bnxt_hwrm_dbg_qcaps(struct bnxt * bp)7762 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
7763 {
7764 	struct hwrm_dbg_qcaps_output *resp;
7765 	struct hwrm_dbg_qcaps_input *req;
7766 	int rc;
7767 
7768 	bp->fw_dbg_cap = 0;
7769 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
7770 		return;
7771 
7772 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
7773 	if (rc)
7774 		return;
7775 
7776 	req->fid = cpu_to_le16(0xffff);
7777 	resp = hwrm_req_hold(bp, req);
7778 	rc = hwrm_req_send(bp, req);
7779 	if (rc)
7780 		goto hwrm_dbg_qcaps_exit;
7781 
7782 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
7783 
7784 hwrm_dbg_qcaps_exit:
7785 	hwrm_req_drop(bp, req);
7786 }
7787 
7788 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
7789 
bnxt_hwrm_func_qcaps(struct bnxt * bp)7790 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
7791 {
7792 	int rc;
7793 
7794 	rc = __bnxt_hwrm_func_qcaps(bp);
7795 	if (rc)
7796 		return rc;
7797 
7798 	bnxt_hwrm_dbg_qcaps(bp);
7799 
7800 	rc = bnxt_hwrm_queue_qportcfg(bp);
7801 	if (rc) {
7802 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
7803 		return rc;
7804 	}
7805 	if (bp->hwrm_spec_code >= 0x10803) {
7806 		rc = bnxt_alloc_ctx_mem(bp);
7807 		if (rc)
7808 			return rc;
7809 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
7810 		if (!rc)
7811 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
7812 	}
7813 	return 0;
7814 }
7815 
bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt * bp)7816 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
7817 {
7818 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
7819 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
7820 	u32 flags;
7821 	int rc;
7822 
7823 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
7824 		return 0;
7825 
7826 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
7827 	if (rc)
7828 		return rc;
7829 
7830 	resp = hwrm_req_hold(bp, req);
7831 	rc = hwrm_req_send(bp, req);
7832 	if (rc)
7833 		goto hwrm_cfa_adv_qcaps_exit;
7834 
7835 	flags = le32_to_cpu(resp->flags);
7836 	if (flags &
7837 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
7838 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
7839 
7840 hwrm_cfa_adv_qcaps_exit:
7841 	hwrm_req_drop(bp, req);
7842 	return rc;
7843 }
7844 
__bnxt_alloc_fw_health(struct bnxt * bp)7845 static int __bnxt_alloc_fw_health(struct bnxt *bp)
7846 {
7847 	if (bp->fw_health)
7848 		return 0;
7849 
7850 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
7851 	if (!bp->fw_health)
7852 		return -ENOMEM;
7853 
7854 	mutex_init(&bp->fw_health->lock);
7855 	return 0;
7856 }
7857 
bnxt_alloc_fw_health(struct bnxt * bp)7858 static int bnxt_alloc_fw_health(struct bnxt *bp)
7859 {
7860 	int rc;
7861 
7862 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
7863 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
7864 		return 0;
7865 
7866 	rc = __bnxt_alloc_fw_health(bp);
7867 	if (rc) {
7868 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
7869 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
7870 		return rc;
7871 	}
7872 
7873 	return 0;
7874 }
7875 
__bnxt_map_fw_health_reg(struct bnxt * bp,u32 reg)7876 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
7877 {
7878 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
7879 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
7880 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
7881 }
7882 
bnxt_inv_fw_health_reg(struct bnxt * bp)7883 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
7884 {
7885 	struct bnxt_fw_health *fw_health = bp->fw_health;
7886 	u32 reg_type;
7887 
7888 	if (!fw_health)
7889 		return;
7890 
7891 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
7892 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
7893 		fw_health->status_reliable = false;
7894 
7895 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
7896 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
7897 		fw_health->resets_reliable = false;
7898 }
7899 
bnxt_try_map_fw_health_reg(struct bnxt * bp)7900 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
7901 {
7902 	void __iomem *hs;
7903 	u32 status_loc;
7904 	u32 reg_type;
7905 	u32 sig;
7906 
7907 	if (bp->fw_health)
7908 		bp->fw_health->status_reliable = false;
7909 
7910 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
7911 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
7912 
7913 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
7914 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
7915 		if (!bp->chip_num) {
7916 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
7917 			bp->chip_num = readl(bp->bar0 +
7918 					     BNXT_FW_HEALTH_WIN_BASE +
7919 					     BNXT_GRC_REG_CHIP_NUM);
7920 		}
7921 		if (!BNXT_CHIP_P5(bp))
7922 			return;
7923 
7924 		status_loc = BNXT_GRC_REG_STATUS_P5 |
7925 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
7926 	} else {
7927 		status_loc = readl(hs + offsetof(struct hcomm_status,
7928 						 fw_status_loc));
7929 	}
7930 
7931 	if (__bnxt_alloc_fw_health(bp)) {
7932 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
7933 		return;
7934 	}
7935 
7936 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
7937 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
7938 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
7939 		__bnxt_map_fw_health_reg(bp, status_loc);
7940 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
7941 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
7942 	}
7943 
7944 	bp->fw_health->status_reliable = true;
7945 }
7946 
bnxt_map_fw_health_regs(struct bnxt * bp)7947 static int bnxt_map_fw_health_regs(struct bnxt *bp)
7948 {
7949 	struct bnxt_fw_health *fw_health = bp->fw_health;
7950 	u32 reg_base = 0xffffffff;
7951 	int i;
7952 
7953 	bp->fw_health->status_reliable = false;
7954 	bp->fw_health->resets_reliable = false;
7955 	/* Only pre-map the monitoring GRC registers using window 3 */
7956 	for (i = 0; i < 4; i++) {
7957 		u32 reg = fw_health->regs[i];
7958 
7959 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
7960 			continue;
7961 		if (reg_base == 0xffffffff)
7962 			reg_base = reg & BNXT_GRC_BASE_MASK;
7963 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
7964 			return -ERANGE;
7965 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
7966 	}
7967 	bp->fw_health->status_reliable = true;
7968 	bp->fw_health->resets_reliable = true;
7969 	if (reg_base == 0xffffffff)
7970 		return 0;
7971 
7972 	__bnxt_map_fw_health_reg(bp, reg_base);
7973 	return 0;
7974 }
7975 
bnxt_remap_fw_health_regs(struct bnxt * bp)7976 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
7977 {
7978 	if (!bp->fw_health)
7979 		return;
7980 
7981 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
7982 		bp->fw_health->status_reliable = true;
7983 		bp->fw_health->resets_reliable = true;
7984 	} else {
7985 		bnxt_try_map_fw_health_reg(bp);
7986 	}
7987 }
7988 
bnxt_hwrm_error_recovery_qcfg(struct bnxt * bp)7989 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
7990 {
7991 	struct bnxt_fw_health *fw_health = bp->fw_health;
7992 	struct hwrm_error_recovery_qcfg_output *resp;
7993 	struct hwrm_error_recovery_qcfg_input *req;
7994 	int rc, i;
7995 
7996 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
7997 		return 0;
7998 
7999 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
8000 	if (rc)
8001 		return rc;
8002 
8003 	resp = hwrm_req_hold(bp, req);
8004 	rc = hwrm_req_send(bp, req);
8005 	if (rc)
8006 		goto err_recovery_out;
8007 	fw_health->flags = le32_to_cpu(resp->flags);
8008 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
8009 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
8010 		rc = -EINVAL;
8011 		goto err_recovery_out;
8012 	}
8013 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
8014 	fw_health->master_func_wait_dsecs =
8015 		le32_to_cpu(resp->master_func_wait_period);
8016 	fw_health->normal_func_wait_dsecs =
8017 		le32_to_cpu(resp->normal_func_wait_period);
8018 	fw_health->post_reset_wait_dsecs =
8019 		le32_to_cpu(resp->master_func_wait_period_after_reset);
8020 	fw_health->post_reset_max_wait_dsecs =
8021 		le32_to_cpu(resp->max_bailout_time_after_reset);
8022 	fw_health->regs[BNXT_FW_HEALTH_REG] =
8023 		le32_to_cpu(resp->fw_health_status_reg);
8024 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
8025 		le32_to_cpu(resp->fw_heartbeat_reg);
8026 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
8027 		le32_to_cpu(resp->fw_reset_cnt_reg);
8028 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
8029 		le32_to_cpu(resp->reset_inprogress_reg);
8030 	fw_health->fw_reset_inprog_reg_mask =
8031 		le32_to_cpu(resp->reset_inprogress_reg_mask);
8032 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
8033 	if (fw_health->fw_reset_seq_cnt >= 16) {
8034 		rc = -EINVAL;
8035 		goto err_recovery_out;
8036 	}
8037 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
8038 		fw_health->fw_reset_seq_regs[i] =
8039 			le32_to_cpu(resp->reset_reg[i]);
8040 		fw_health->fw_reset_seq_vals[i] =
8041 			le32_to_cpu(resp->reset_reg_val[i]);
8042 		fw_health->fw_reset_seq_delay_msec[i] =
8043 			resp->delay_after_reset[i];
8044 	}
8045 err_recovery_out:
8046 	hwrm_req_drop(bp, req);
8047 	if (!rc)
8048 		rc = bnxt_map_fw_health_regs(bp);
8049 	if (rc)
8050 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
8051 	return rc;
8052 }
8053 
bnxt_hwrm_func_reset(struct bnxt * bp)8054 static int bnxt_hwrm_func_reset(struct bnxt *bp)
8055 {
8056 	struct hwrm_func_reset_input *req;
8057 	int rc;
8058 
8059 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
8060 	if (rc)
8061 		return rc;
8062 
8063 	req->enables = 0;
8064 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
8065 	return hwrm_req_send(bp, req);
8066 }
8067 
bnxt_nvm_cfg_ver_get(struct bnxt * bp)8068 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
8069 {
8070 	struct hwrm_nvm_get_dev_info_output nvm_info;
8071 
8072 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
8073 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
8074 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
8075 			 nvm_info.nvm_cfg_ver_upd);
8076 }
8077 
bnxt_hwrm_queue_qportcfg(struct bnxt * bp)8078 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
8079 {
8080 	struct hwrm_queue_qportcfg_output *resp;
8081 	struct hwrm_queue_qportcfg_input *req;
8082 	u8 i, j, *qptr;
8083 	bool no_rdma;
8084 	int rc = 0;
8085 
8086 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
8087 	if (rc)
8088 		return rc;
8089 
8090 	resp = hwrm_req_hold(bp, req);
8091 	rc = hwrm_req_send(bp, req);
8092 	if (rc)
8093 		goto qportcfg_exit;
8094 
8095 	if (!resp->max_configurable_queues) {
8096 		rc = -EINVAL;
8097 		goto qportcfg_exit;
8098 	}
8099 	bp->max_tc = resp->max_configurable_queues;
8100 	bp->max_lltc = resp->max_configurable_lossless_queues;
8101 	if (bp->max_tc > BNXT_MAX_QUEUE)
8102 		bp->max_tc = BNXT_MAX_QUEUE;
8103 
8104 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
8105 	qptr = &resp->queue_id0;
8106 	for (i = 0, j = 0; i < bp->max_tc; i++) {
8107 		bp->q_info[j].queue_id = *qptr;
8108 		bp->q_ids[i] = *qptr++;
8109 		bp->q_info[j].queue_profile = *qptr++;
8110 		bp->tc_to_qidx[j] = j;
8111 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
8112 		    (no_rdma && BNXT_PF(bp)))
8113 			j++;
8114 	}
8115 	bp->max_q = bp->max_tc;
8116 	bp->max_tc = max_t(u8, j, 1);
8117 
8118 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
8119 		bp->max_tc = 1;
8120 
8121 	if (bp->max_lltc > bp->max_tc)
8122 		bp->max_lltc = bp->max_tc;
8123 
8124 qportcfg_exit:
8125 	hwrm_req_drop(bp, req);
8126 	return rc;
8127 }
8128 
bnxt_hwrm_poll(struct bnxt * bp)8129 static int bnxt_hwrm_poll(struct bnxt *bp)
8130 {
8131 	struct hwrm_ver_get_input *req;
8132 	int rc;
8133 
8134 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
8135 	if (rc)
8136 		return rc;
8137 
8138 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
8139 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
8140 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
8141 
8142 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
8143 	rc = hwrm_req_send(bp, req);
8144 	return rc;
8145 }
8146 
bnxt_hwrm_ver_get(struct bnxt * bp)8147 static int bnxt_hwrm_ver_get(struct bnxt *bp)
8148 {
8149 	struct hwrm_ver_get_output *resp;
8150 	struct hwrm_ver_get_input *req;
8151 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
8152 	u32 dev_caps_cfg, hwrm_ver;
8153 	int rc, len;
8154 
8155 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
8156 	if (rc)
8157 		return rc;
8158 
8159 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
8160 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
8161 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
8162 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
8163 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
8164 
8165 	resp = hwrm_req_hold(bp, req);
8166 	rc = hwrm_req_send(bp, req);
8167 	if (rc)
8168 		goto hwrm_ver_get_exit;
8169 
8170 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
8171 
8172 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
8173 			     resp->hwrm_intf_min_8b << 8 |
8174 			     resp->hwrm_intf_upd_8b;
8175 	if (resp->hwrm_intf_maj_8b < 1) {
8176 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
8177 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
8178 			    resp->hwrm_intf_upd_8b);
8179 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
8180 	}
8181 
8182 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
8183 			HWRM_VERSION_UPDATE;
8184 
8185 	if (bp->hwrm_spec_code > hwrm_ver)
8186 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
8187 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
8188 			 HWRM_VERSION_UPDATE);
8189 	else
8190 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
8191 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
8192 			 resp->hwrm_intf_upd_8b);
8193 
8194 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
8195 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
8196 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
8197 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
8198 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
8199 		len = FW_VER_STR_LEN;
8200 	} else {
8201 		fw_maj = resp->hwrm_fw_maj_8b;
8202 		fw_min = resp->hwrm_fw_min_8b;
8203 		fw_bld = resp->hwrm_fw_bld_8b;
8204 		fw_rsv = resp->hwrm_fw_rsvd_8b;
8205 		len = BC_HWRM_STR_LEN;
8206 	}
8207 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
8208 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
8209 		 fw_rsv);
8210 
8211 	if (strlen(resp->active_pkg_name)) {
8212 		int fw_ver_len = strlen(bp->fw_ver_str);
8213 
8214 		snprintf(bp->fw_ver_str + fw_ver_len,
8215 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
8216 			 resp->active_pkg_name);
8217 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
8218 	}
8219 
8220 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
8221 	if (!bp->hwrm_cmd_timeout)
8222 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
8223 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
8224 	if (!bp->hwrm_cmd_max_timeout)
8225 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
8226 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
8227 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
8228 			    bp->hwrm_cmd_max_timeout / 1000);
8229 
8230 	if (resp->hwrm_intf_maj_8b >= 1) {
8231 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
8232 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
8233 	}
8234 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
8235 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
8236 
8237 	bp->chip_num = le16_to_cpu(resp->chip_num);
8238 	bp->chip_rev = resp->chip_rev;
8239 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
8240 	    !resp->chip_metal)
8241 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
8242 
8243 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
8244 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
8245 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
8246 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
8247 
8248 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
8249 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
8250 
8251 	if (dev_caps_cfg &
8252 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
8253 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
8254 
8255 	if (dev_caps_cfg &
8256 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
8257 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
8258 
8259 	if (dev_caps_cfg &
8260 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
8261 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
8262 
8263 hwrm_ver_get_exit:
8264 	hwrm_req_drop(bp, req);
8265 	return rc;
8266 }
8267 
bnxt_hwrm_fw_set_time(struct bnxt * bp)8268 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
8269 {
8270 	struct hwrm_fw_set_time_input *req;
8271 	struct tm tm;
8272 	time64_t now = ktime_get_real_seconds();
8273 	int rc;
8274 
8275 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
8276 	    bp->hwrm_spec_code < 0x10400)
8277 		return -EOPNOTSUPP;
8278 
8279 	time64_to_tm(now, 0, &tm);
8280 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
8281 	if (rc)
8282 		return rc;
8283 
8284 	req->year = cpu_to_le16(1900 + tm.tm_year);
8285 	req->month = 1 + tm.tm_mon;
8286 	req->day = tm.tm_mday;
8287 	req->hour = tm.tm_hour;
8288 	req->minute = tm.tm_min;
8289 	req->second = tm.tm_sec;
8290 	return hwrm_req_send(bp, req);
8291 }
8292 
bnxt_add_one_ctr(u64 hw,u64 * sw,u64 mask)8293 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
8294 {
8295 	u64 sw_tmp;
8296 
8297 	hw &= mask;
8298 	sw_tmp = (*sw & ~mask) | hw;
8299 	if (hw < (*sw & mask))
8300 		sw_tmp += mask + 1;
8301 	WRITE_ONCE(*sw, sw_tmp);
8302 }
8303 
__bnxt_accumulate_stats(__le64 * hw_stats,u64 * sw_stats,u64 * masks,int count,bool ignore_zero)8304 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
8305 				    int count, bool ignore_zero)
8306 {
8307 	int i;
8308 
8309 	for (i = 0; i < count; i++) {
8310 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
8311 
8312 		if (ignore_zero && !hw)
8313 			continue;
8314 
8315 		if (masks[i] == -1ULL)
8316 			sw_stats[i] = hw;
8317 		else
8318 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
8319 	}
8320 }
8321 
bnxt_accumulate_stats(struct bnxt_stats_mem * stats)8322 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
8323 {
8324 	if (!stats->hw_stats)
8325 		return;
8326 
8327 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
8328 				stats->hw_masks, stats->len / 8, false);
8329 }
8330 
bnxt_accumulate_all_stats(struct bnxt * bp)8331 static void bnxt_accumulate_all_stats(struct bnxt *bp)
8332 {
8333 	struct bnxt_stats_mem *ring0_stats;
8334 	bool ignore_zero = false;
8335 	int i;
8336 
8337 	/* Chip bug.  Counter intermittently becomes 0. */
8338 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8339 		ignore_zero = true;
8340 
8341 	for (i = 0; i < bp->cp_nr_rings; i++) {
8342 		struct bnxt_napi *bnapi = bp->bnapi[i];
8343 		struct bnxt_cp_ring_info *cpr;
8344 		struct bnxt_stats_mem *stats;
8345 
8346 		cpr = &bnapi->cp_ring;
8347 		stats = &cpr->stats;
8348 		if (!i)
8349 			ring0_stats = stats;
8350 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
8351 					ring0_stats->hw_masks,
8352 					ring0_stats->len / 8, ignore_zero);
8353 	}
8354 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
8355 		struct bnxt_stats_mem *stats = &bp->port_stats;
8356 		__le64 *hw_stats = stats->hw_stats;
8357 		u64 *sw_stats = stats->sw_stats;
8358 		u64 *masks = stats->hw_masks;
8359 		int cnt;
8360 
8361 		cnt = sizeof(struct rx_port_stats) / 8;
8362 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
8363 
8364 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
8365 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
8366 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
8367 		cnt = sizeof(struct tx_port_stats) / 8;
8368 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
8369 	}
8370 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
8371 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
8372 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
8373 	}
8374 }
8375 
bnxt_hwrm_port_qstats(struct bnxt * bp,u8 flags)8376 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
8377 {
8378 	struct hwrm_port_qstats_input *req;
8379 	struct bnxt_pf_info *pf = &bp->pf;
8380 	int rc;
8381 
8382 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
8383 		return 0;
8384 
8385 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
8386 		return -EOPNOTSUPP;
8387 
8388 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
8389 	if (rc)
8390 		return rc;
8391 
8392 	req->flags = flags;
8393 	req->port_id = cpu_to_le16(pf->port_id);
8394 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
8395 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
8396 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
8397 	return hwrm_req_send(bp, req);
8398 }
8399 
bnxt_hwrm_port_qstats_ext(struct bnxt * bp,u8 flags)8400 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
8401 {
8402 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
8403 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
8404 	struct hwrm_port_qstats_ext_output *resp_qs;
8405 	struct hwrm_port_qstats_ext_input *req_qs;
8406 	struct bnxt_pf_info *pf = &bp->pf;
8407 	u32 tx_stat_size;
8408 	int rc;
8409 
8410 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
8411 		return 0;
8412 
8413 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
8414 		return -EOPNOTSUPP;
8415 
8416 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
8417 	if (rc)
8418 		return rc;
8419 
8420 	req_qs->flags = flags;
8421 	req_qs->port_id = cpu_to_le16(pf->port_id);
8422 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
8423 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
8424 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
8425 		       sizeof(struct tx_port_stats_ext) : 0;
8426 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
8427 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
8428 	resp_qs = hwrm_req_hold(bp, req_qs);
8429 	rc = hwrm_req_send(bp, req_qs);
8430 	if (!rc) {
8431 		bp->fw_rx_stats_ext_size =
8432 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
8433 		if (BNXT_FW_MAJ(bp) < 220 &&
8434 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
8435 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
8436 
8437 		bp->fw_tx_stats_ext_size = tx_stat_size ?
8438 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
8439 	} else {
8440 		bp->fw_rx_stats_ext_size = 0;
8441 		bp->fw_tx_stats_ext_size = 0;
8442 	}
8443 	hwrm_req_drop(bp, req_qs);
8444 
8445 	if (flags)
8446 		return rc;
8447 
8448 	if (bp->fw_tx_stats_ext_size <=
8449 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
8450 		bp->pri2cos_valid = 0;
8451 		return rc;
8452 	}
8453 
8454 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
8455 	if (rc)
8456 		return rc;
8457 
8458 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
8459 
8460 	resp_qc = hwrm_req_hold(bp, req_qc);
8461 	rc = hwrm_req_send(bp, req_qc);
8462 	if (!rc) {
8463 		u8 *pri2cos;
8464 		int i, j;
8465 
8466 		pri2cos = &resp_qc->pri0_cos_queue_id;
8467 		for (i = 0; i < 8; i++) {
8468 			u8 queue_id = pri2cos[i];
8469 			u8 queue_idx;
8470 
8471 			/* Per port queue IDs start from 0, 10, 20, etc */
8472 			queue_idx = queue_id % 10;
8473 			if (queue_idx > BNXT_MAX_QUEUE) {
8474 				bp->pri2cos_valid = false;
8475 				hwrm_req_drop(bp, req_qc);
8476 				return rc;
8477 			}
8478 			for (j = 0; j < bp->max_q; j++) {
8479 				if (bp->q_ids[j] == queue_id)
8480 					bp->pri2cos_idx[i] = queue_idx;
8481 			}
8482 		}
8483 		bp->pri2cos_valid = true;
8484 	}
8485 	hwrm_req_drop(bp, req_qc);
8486 
8487 	return rc;
8488 }
8489 
bnxt_hwrm_free_tunnel_ports(struct bnxt * bp)8490 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
8491 {
8492 	bnxt_hwrm_tunnel_dst_port_free(bp,
8493 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
8494 	bnxt_hwrm_tunnel_dst_port_free(bp,
8495 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
8496 }
8497 
bnxt_set_tpa(struct bnxt * bp,bool set_tpa)8498 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
8499 {
8500 	int rc, i;
8501 	u32 tpa_flags = 0;
8502 
8503 	if (set_tpa)
8504 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
8505 	else if (BNXT_NO_FW_ACCESS(bp))
8506 		return 0;
8507 	for (i = 0; i < bp->nr_vnics; i++) {
8508 		rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
8509 		if (rc) {
8510 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
8511 				   i, rc);
8512 			return rc;
8513 		}
8514 	}
8515 	return 0;
8516 }
8517 
bnxt_hwrm_clear_vnic_rss(struct bnxt * bp)8518 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
8519 {
8520 	int i;
8521 
8522 	for (i = 0; i < bp->nr_vnics; i++)
8523 		bnxt_hwrm_vnic_set_rss(bp, i, false);
8524 }
8525 
bnxt_clear_vnic(struct bnxt * bp)8526 static void bnxt_clear_vnic(struct bnxt *bp)
8527 {
8528 	if (!bp->vnic_info)
8529 		return;
8530 
8531 	bnxt_hwrm_clear_vnic_filter(bp);
8532 	if (!(bp->flags & BNXT_FLAG_CHIP_P5)) {
8533 		/* clear all RSS setting before free vnic ctx */
8534 		bnxt_hwrm_clear_vnic_rss(bp);
8535 		bnxt_hwrm_vnic_ctx_free(bp);
8536 	}
8537 	/* before free the vnic, undo the vnic tpa settings */
8538 	if (bp->flags & BNXT_FLAG_TPA)
8539 		bnxt_set_tpa(bp, false);
8540 	bnxt_hwrm_vnic_free(bp);
8541 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8542 		bnxt_hwrm_vnic_ctx_free(bp);
8543 }
8544 
bnxt_hwrm_resource_free(struct bnxt * bp,bool close_path,bool irq_re_init)8545 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
8546 				    bool irq_re_init)
8547 {
8548 	bnxt_clear_vnic(bp);
8549 	bnxt_hwrm_ring_free(bp, close_path);
8550 	bnxt_hwrm_ring_grp_free(bp);
8551 	if (irq_re_init) {
8552 		bnxt_hwrm_stat_ctx_free(bp);
8553 		bnxt_hwrm_free_tunnel_ports(bp);
8554 	}
8555 }
8556 
bnxt_hwrm_set_br_mode(struct bnxt * bp,u16 br_mode)8557 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
8558 {
8559 	struct hwrm_func_cfg_input *req;
8560 	u8 evb_mode;
8561 	int rc;
8562 
8563 	if (br_mode == BRIDGE_MODE_VEB)
8564 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
8565 	else if (br_mode == BRIDGE_MODE_VEPA)
8566 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
8567 	else
8568 		return -EINVAL;
8569 
8570 	rc = hwrm_req_init(bp, req, HWRM_FUNC_CFG);
8571 	if (rc)
8572 		return rc;
8573 
8574 	req->fid = cpu_to_le16(0xffff);
8575 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
8576 	req->evb_mode = evb_mode;
8577 	return hwrm_req_send(bp, req);
8578 }
8579 
bnxt_hwrm_set_cache_line_size(struct bnxt * bp,int size)8580 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
8581 {
8582 	struct hwrm_func_cfg_input *req;
8583 	int rc;
8584 
8585 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
8586 		return 0;
8587 
8588 	rc = hwrm_req_init(bp, req, HWRM_FUNC_CFG);
8589 	if (rc)
8590 		return rc;
8591 
8592 	req->fid = cpu_to_le16(0xffff);
8593 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
8594 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
8595 	if (size == 128)
8596 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
8597 
8598 	return hwrm_req_send(bp, req);
8599 }
8600 
__bnxt_setup_vnic(struct bnxt * bp,u16 vnic_id)8601 static int __bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
8602 {
8603 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
8604 	int rc;
8605 
8606 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
8607 		goto skip_rss_ctx;
8608 
8609 	/* allocate context for vnic */
8610 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
8611 	if (rc) {
8612 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
8613 			   vnic_id, rc);
8614 		goto vnic_setup_err;
8615 	}
8616 	bp->rsscos_nr_ctxs++;
8617 
8618 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8619 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
8620 		if (rc) {
8621 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
8622 				   vnic_id, rc);
8623 			goto vnic_setup_err;
8624 		}
8625 		bp->rsscos_nr_ctxs++;
8626 	}
8627 
8628 skip_rss_ctx:
8629 	/* configure default vnic, ring grp */
8630 	rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
8631 	if (rc) {
8632 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
8633 			   vnic_id, rc);
8634 		goto vnic_setup_err;
8635 	}
8636 
8637 	/* Enable RSS hashing on vnic */
8638 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
8639 	if (rc) {
8640 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
8641 			   vnic_id, rc);
8642 		goto vnic_setup_err;
8643 	}
8644 
8645 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8646 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
8647 		if (rc) {
8648 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
8649 				   vnic_id, rc);
8650 		}
8651 	}
8652 
8653 vnic_setup_err:
8654 	return rc;
8655 }
8656 
__bnxt_setup_vnic_p5(struct bnxt * bp,u16 vnic_id)8657 static int __bnxt_setup_vnic_p5(struct bnxt *bp, u16 vnic_id)
8658 {
8659 	int rc, i, nr_ctxs;
8660 
8661 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
8662 	for (i = 0; i < nr_ctxs; i++) {
8663 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, i);
8664 		if (rc) {
8665 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
8666 				   vnic_id, i, rc);
8667 			break;
8668 		}
8669 		bp->rsscos_nr_ctxs++;
8670 	}
8671 	if (i < nr_ctxs)
8672 		return -ENOMEM;
8673 
8674 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic_id, true);
8675 	if (rc) {
8676 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
8677 			   vnic_id, rc);
8678 		return rc;
8679 	}
8680 	rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
8681 	if (rc) {
8682 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
8683 			   vnic_id, rc);
8684 		return rc;
8685 	}
8686 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8687 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
8688 		if (rc) {
8689 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
8690 				   vnic_id, rc);
8691 		}
8692 	}
8693 	return rc;
8694 }
8695 
bnxt_setup_vnic(struct bnxt * bp,u16 vnic_id)8696 static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
8697 {
8698 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8699 		return __bnxt_setup_vnic_p5(bp, vnic_id);
8700 	else
8701 		return __bnxt_setup_vnic(bp, vnic_id);
8702 }
8703 
bnxt_alloc_rfs_vnics(struct bnxt * bp)8704 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
8705 {
8706 #ifdef CONFIG_RFS_ACCEL
8707 	int i, rc = 0;
8708 
8709 	if (bp->flags & BNXT_FLAG_CHIP_P5)
8710 		return 0;
8711 
8712 	for (i = 0; i < bp->rx_nr_rings; i++) {
8713 		struct bnxt_vnic_info *vnic;
8714 		u16 vnic_id = i + 1;
8715 		u16 ring_id = i;
8716 
8717 		if (vnic_id >= bp->nr_vnics)
8718 			break;
8719 
8720 		vnic = &bp->vnic_info[vnic_id];
8721 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
8722 		if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
8723 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
8724 		rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
8725 		if (rc) {
8726 			netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
8727 				   vnic_id, rc);
8728 			break;
8729 		}
8730 		rc = bnxt_setup_vnic(bp, vnic_id);
8731 		if (rc)
8732 			break;
8733 	}
8734 	return rc;
8735 #else
8736 	return 0;
8737 #endif
8738 }
8739 
8740 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
bnxt_promisc_ok(struct bnxt * bp)8741 static bool bnxt_promisc_ok(struct bnxt *bp)
8742 {
8743 #ifdef CONFIG_BNXT_SRIOV
8744 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
8745 		return false;
8746 #endif
8747 	return true;
8748 }
8749 
bnxt_setup_nitroa0_vnic(struct bnxt * bp)8750 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
8751 {
8752 	unsigned int rc = 0;
8753 
8754 	rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
8755 	if (rc) {
8756 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
8757 			   rc);
8758 		return rc;
8759 	}
8760 
8761 	rc = bnxt_hwrm_vnic_cfg(bp, 1);
8762 	if (rc) {
8763 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
8764 			   rc);
8765 		return rc;
8766 	}
8767 	return rc;
8768 }
8769 
8770 static int bnxt_cfg_rx_mode(struct bnxt *);
8771 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
8772 
bnxt_init_chip(struct bnxt * bp,bool irq_re_init)8773 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
8774 {
8775 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8776 	int rc = 0;
8777 	unsigned int rx_nr_rings = bp->rx_nr_rings;
8778 
8779 	if (irq_re_init) {
8780 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
8781 		if (rc) {
8782 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
8783 				   rc);
8784 			goto err_out;
8785 		}
8786 	}
8787 
8788 	rc = bnxt_hwrm_ring_alloc(bp);
8789 	if (rc) {
8790 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
8791 		goto err_out;
8792 	}
8793 
8794 	rc = bnxt_hwrm_ring_grp_alloc(bp);
8795 	if (rc) {
8796 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
8797 		goto err_out;
8798 	}
8799 
8800 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8801 		rx_nr_rings--;
8802 
8803 	/* default vnic 0 */
8804 	rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
8805 	if (rc) {
8806 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
8807 		goto err_out;
8808 	}
8809 
8810 	rc = bnxt_setup_vnic(bp, 0);
8811 	if (rc)
8812 		goto err_out;
8813 
8814 	if (bp->flags & BNXT_FLAG_RFS) {
8815 		rc = bnxt_alloc_rfs_vnics(bp);
8816 		if (rc)
8817 			goto err_out;
8818 	}
8819 
8820 	if (bp->flags & BNXT_FLAG_TPA) {
8821 		rc = bnxt_set_tpa(bp, true);
8822 		if (rc)
8823 			goto err_out;
8824 	}
8825 
8826 	if (BNXT_VF(bp))
8827 		bnxt_update_vf_mac(bp);
8828 
8829 	/* Filter for default vnic 0 */
8830 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
8831 	if (rc) {
8832 		if (BNXT_VF(bp) && rc == -ENODEV)
8833 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
8834 		else
8835 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
8836 		goto err_out;
8837 	}
8838 	vnic->uc_filter_count = 1;
8839 
8840 	vnic->rx_mask = 0;
8841 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
8842 		goto skip_rx_mask;
8843 
8844 	if (bp->dev->flags & IFF_BROADCAST)
8845 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
8846 
8847 	if (bp->dev->flags & IFF_PROMISC)
8848 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
8849 
8850 	if (bp->dev->flags & IFF_ALLMULTI) {
8851 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
8852 		vnic->mc_list_count = 0;
8853 	} else if (bp->dev->flags & IFF_MULTICAST) {
8854 		u32 mask = 0;
8855 
8856 		bnxt_mc_list_updated(bp, &mask);
8857 		vnic->rx_mask |= mask;
8858 	}
8859 
8860 	rc = bnxt_cfg_rx_mode(bp);
8861 	if (rc)
8862 		goto err_out;
8863 
8864 skip_rx_mask:
8865 	rc = bnxt_hwrm_set_coal(bp);
8866 	if (rc)
8867 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
8868 				rc);
8869 
8870 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8871 		rc = bnxt_setup_nitroa0_vnic(bp);
8872 		if (rc)
8873 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
8874 				   rc);
8875 	}
8876 
8877 	if (BNXT_VF(bp)) {
8878 		bnxt_hwrm_func_qcfg(bp);
8879 		netdev_update_features(bp->dev);
8880 	}
8881 
8882 	return 0;
8883 
8884 err_out:
8885 	bnxt_hwrm_resource_free(bp, 0, true);
8886 
8887 	return rc;
8888 }
8889 
bnxt_shutdown_nic(struct bnxt * bp,bool irq_re_init)8890 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
8891 {
8892 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
8893 	return 0;
8894 }
8895 
bnxt_init_nic(struct bnxt * bp,bool irq_re_init)8896 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
8897 {
8898 	bnxt_init_cp_rings(bp);
8899 	bnxt_init_rx_rings(bp);
8900 	bnxt_init_tx_rings(bp);
8901 	bnxt_init_ring_grps(bp, irq_re_init);
8902 	bnxt_init_vnics(bp);
8903 
8904 	return bnxt_init_chip(bp, irq_re_init);
8905 }
8906 
bnxt_set_real_num_queues(struct bnxt * bp)8907 static int bnxt_set_real_num_queues(struct bnxt *bp)
8908 {
8909 	int rc;
8910 	struct net_device *dev = bp->dev;
8911 
8912 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
8913 					  bp->tx_nr_rings_xdp);
8914 	if (rc)
8915 		return rc;
8916 
8917 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
8918 	if (rc)
8919 		return rc;
8920 
8921 #ifdef CONFIG_RFS_ACCEL
8922 	if (bp->flags & BNXT_FLAG_RFS)
8923 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
8924 #endif
8925 
8926 	return rc;
8927 }
8928 
bnxt_trim_rings(struct bnxt * bp,int * rx,int * tx,int max,bool shared)8929 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
8930 			   bool shared)
8931 {
8932 	int _rx = *rx, _tx = *tx;
8933 
8934 	if (shared) {
8935 		*rx = min_t(int, _rx, max);
8936 		*tx = min_t(int, _tx, max);
8937 	} else {
8938 		if (max < 2)
8939 			return -ENOMEM;
8940 
8941 		while (_rx + _tx > max) {
8942 			if (_rx > _tx && _rx > 1)
8943 				_rx--;
8944 			else if (_tx > 1)
8945 				_tx--;
8946 		}
8947 		*rx = _rx;
8948 		*tx = _tx;
8949 	}
8950 	return 0;
8951 }
8952 
bnxt_setup_msix(struct bnxt * bp)8953 static void bnxt_setup_msix(struct bnxt *bp)
8954 {
8955 	const int len = sizeof(bp->irq_tbl[0].name);
8956 	struct net_device *dev = bp->dev;
8957 	int tcs, i;
8958 
8959 	tcs = netdev_get_num_tc(dev);
8960 	if (tcs) {
8961 		int i, off, count;
8962 
8963 		for (i = 0; i < tcs; i++) {
8964 			count = bp->tx_nr_rings_per_tc;
8965 			off = i * count;
8966 			netdev_set_tc_queue(dev, i, count, off);
8967 		}
8968 	}
8969 
8970 	for (i = 0; i < bp->cp_nr_rings; i++) {
8971 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
8972 		char *attr;
8973 
8974 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8975 			attr = "TxRx";
8976 		else if (i < bp->rx_nr_rings)
8977 			attr = "rx";
8978 		else
8979 			attr = "tx";
8980 
8981 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
8982 			 attr, i);
8983 		bp->irq_tbl[map_idx].handler = bnxt_msix;
8984 	}
8985 }
8986 
bnxt_setup_inta(struct bnxt * bp)8987 static void bnxt_setup_inta(struct bnxt *bp)
8988 {
8989 	const int len = sizeof(bp->irq_tbl[0].name);
8990 
8991 	if (netdev_get_num_tc(bp->dev))
8992 		netdev_reset_tc(bp->dev);
8993 
8994 	snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
8995 		 0);
8996 	bp->irq_tbl[0].handler = bnxt_inta;
8997 }
8998 
8999 static int bnxt_init_int_mode(struct bnxt *bp);
9000 
bnxt_setup_int_mode(struct bnxt * bp)9001 static int bnxt_setup_int_mode(struct bnxt *bp)
9002 {
9003 	int rc;
9004 
9005 	if (!bp->irq_tbl) {
9006 		rc = bnxt_init_int_mode(bp);
9007 		if (rc || !bp->irq_tbl)
9008 			return rc ?: -ENODEV;
9009 	}
9010 
9011 	if (bp->flags & BNXT_FLAG_USING_MSIX)
9012 		bnxt_setup_msix(bp);
9013 	else
9014 		bnxt_setup_inta(bp);
9015 
9016 	rc = bnxt_set_real_num_queues(bp);
9017 	return rc;
9018 }
9019 
9020 #ifdef CONFIG_RFS_ACCEL
bnxt_get_max_func_rss_ctxs(struct bnxt * bp)9021 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
9022 {
9023 	return bp->hw_resc.max_rsscos_ctxs;
9024 }
9025 
bnxt_get_max_func_vnics(struct bnxt * bp)9026 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
9027 {
9028 	return bp->hw_resc.max_vnics;
9029 }
9030 #endif
9031 
bnxt_get_max_func_stat_ctxs(struct bnxt * bp)9032 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
9033 {
9034 	return bp->hw_resc.max_stat_ctxs;
9035 }
9036 
bnxt_get_max_func_cp_rings(struct bnxt * bp)9037 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
9038 {
9039 	return bp->hw_resc.max_cp_rings;
9040 }
9041 
bnxt_get_max_func_cp_rings_for_en(struct bnxt * bp)9042 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
9043 {
9044 	unsigned int cp = bp->hw_resc.max_cp_rings;
9045 
9046 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
9047 		cp -= bnxt_get_ulp_msix_num(bp);
9048 
9049 	return cp;
9050 }
9051 
bnxt_get_max_func_irqs(struct bnxt * bp)9052 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
9053 {
9054 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9055 
9056 	if (bp->flags & BNXT_FLAG_CHIP_P5)
9057 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
9058 
9059 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
9060 }
9061 
bnxt_set_max_func_irqs(struct bnxt * bp,unsigned int max_irqs)9062 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
9063 {
9064 	bp->hw_resc.max_irqs = max_irqs;
9065 }
9066 
bnxt_get_avail_cp_rings_for_en(struct bnxt * bp)9067 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
9068 {
9069 	unsigned int cp;
9070 
9071 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
9072 	if (bp->flags & BNXT_FLAG_CHIP_P5)
9073 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
9074 	else
9075 		return cp - bp->cp_nr_rings;
9076 }
9077 
bnxt_get_avail_stat_ctxs_for_en(struct bnxt * bp)9078 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
9079 {
9080 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
9081 }
9082 
bnxt_get_avail_msix(struct bnxt * bp,int num)9083 int bnxt_get_avail_msix(struct bnxt *bp, int num)
9084 {
9085 	int max_cp = bnxt_get_max_func_cp_rings(bp);
9086 	int max_irq = bnxt_get_max_func_irqs(bp);
9087 	int total_req = bp->cp_nr_rings + num;
9088 	int max_idx, avail_msix;
9089 
9090 	max_idx = bp->total_irqs;
9091 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
9092 		max_idx = min_t(int, bp->total_irqs, max_cp);
9093 	avail_msix = max_idx - bp->cp_nr_rings;
9094 	if (!BNXT_NEW_RM(bp) || avail_msix >= num)
9095 		return avail_msix;
9096 
9097 	if (max_irq < total_req) {
9098 		num = max_irq - bp->cp_nr_rings;
9099 		if (num <= 0)
9100 			return 0;
9101 	}
9102 	return num;
9103 }
9104 
bnxt_get_num_msix(struct bnxt * bp)9105 static int bnxt_get_num_msix(struct bnxt *bp)
9106 {
9107 	if (!BNXT_NEW_RM(bp))
9108 		return bnxt_get_max_func_irqs(bp);
9109 
9110 	return bnxt_nq_rings_in_use(bp);
9111 }
9112 
bnxt_init_msix(struct bnxt * bp)9113 static int bnxt_init_msix(struct bnxt *bp)
9114 {
9115 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix;
9116 	struct msix_entry *msix_ent;
9117 
9118 	total_vecs = bnxt_get_num_msix(bp);
9119 	max = bnxt_get_max_func_irqs(bp);
9120 	if (total_vecs > max)
9121 		total_vecs = max;
9122 
9123 	if (!total_vecs)
9124 		return 0;
9125 
9126 	msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
9127 	if (!msix_ent)
9128 		return -ENOMEM;
9129 
9130 	for (i = 0; i < total_vecs; i++) {
9131 		msix_ent[i].entry = i;
9132 		msix_ent[i].vector = 0;
9133 	}
9134 
9135 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
9136 		min = 2;
9137 
9138 	total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
9139 	ulp_msix = bnxt_get_ulp_msix_num(bp);
9140 	if (total_vecs < 0 || total_vecs < ulp_msix) {
9141 		rc = -ENODEV;
9142 		goto msix_setup_exit;
9143 	}
9144 
9145 	bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
9146 	if (bp->irq_tbl) {
9147 		for (i = 0; i < total_vecs; i++)
9148 			bp->irq_tbl[i].vector = msix_ent[i].vector;
9149 
9150 		bp->total_irqs = total_vecs;
9151 		/* Trim rings based upon num of vectors allocated */
9152 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
9153 				     total_vecs - ulp_msix, min == 1);
9154 		if (rc)
9155 			goto msix_setup_exit;
9156 
9157 		bp->cp_nr_rings = (min == 1) ?
9158 				  max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
9159 				  bp->tx_nr_rings + bp->rx_nr_rings;
9160 
9161 	} else {
9162 		rc = -ENOMEM;
9163 		goto msix_setup_exit;
9164 	}
9165 	bp->flags |= BNXT_FLAG_USING_MSIX;
9166 	kfree(msix_ent);
9167 	return 0;
9168 
9169 msix_setup_exit:
9170 	netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
9171 	kfree(bp->irq_tbl);
9172 	bp->irq_tbl = NULL;
9173 	pci_disable_msix(bp->pdev);
9174 	kfree(msix_ent);
9175 	return rc;
9176 }
9177 
bnxt_init_inta(struct bnxt * bp)9178 static int bnxt_init_inta(struct bnxt *bp)
9179 {
9180 	bp->irq_tbl = kzalloc(sizeof(struct bnxt_irq), GFP_KERNEL);
9181 	if (!bp->irq_tbl)
9182 		return -ENOMEM;
9183 
9184 	bp->total_irqs = 1;
9185 	bp->rx_nr_rings = 1;
9186 	bp->tx_nr_rings = 1;
9187 	bp->cp_nr_rings = 1;
9188 	bp->flags |= BNXT_FLAG_SHARED_RINGS;
9189 	bp->irq_tbl[0].vector = bp->pdev->irq;
9190 	return 0;
9191 }
9192 
bnxt_init_int_mode(struct bnxt * bp)9193 static int bnxt_init_int_mode(struct bnxt *bp)
9194 {
9195 	int rc = -ENODEV;
9196 
9197 	if (bp->flags & BNXT_FLAG_MSIX_CAP)
9198 		rc = bnxt_init_msix(bp);
9199 
9200 	if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
9201 		/* fallback to INTA */
9202 		rc = bnxt_init_inta(bp);
9203 	}
9204 	return rc;
9205 }
9206 
bnxt_clear_int_mode(struct bnxt * bp)9207 static void bnxt_clear_int_mode(struct bnxt *bp)
9208 {
9209 	if (bp->flags & BNXT_FLAG_USING_MSIX)
9210 		pci_disable_msix(bp->pdev);
9211 
9212 	kfree(bp->irq_tbl);
9213 	bp->irq_tbl = NULL;
9214 	bp->flags &= ~BNXT_FLAG_USING_MSIX;
9215 }
9216 
bnxt_reserve_rings(struct bnxt * bp,bool irq_re_init)9217 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
9218 {
9219 	int tcs = netdev_get_num_tc(bp->dev);
9220 	bool irq_cleared = false;
9221 	int rc;
9222 
9223 	if (!bnxt_need_reserve_rings(bp))
9224 		return 0;
9225 
9226 	if (irq_re_init && BNXT_NEW_RM(bp) &&
9227 	    bnxt_get_num_msix(bp) != bp->total_irqs) {
9228 		bnxt_ulp_irq_stop(bp);
9229 		bnxt_clear_int_mode(bp);
9230 		irq_cleared = true;
9231 	}
9232 	rc = __bnxt_reserve_rings(bp);
9233 	if (irq_cleared) {
9234 		if (!rc)
9235 			rc = bnxt_init_int_mode(bp);
9236 		bnxt_ulp_irq_restart(bp, rc);
9237 	}
9238 	if (rc) {
9239 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
9240 		return rc;
9241 	}
9242 	if (tcs && (bp->tx_nr_rings_per_tc * tcs != bp->tx_nr_rings)) {
9243 		netdev_err(bp->dev, "tx ring reservation failure\n");
9244 		netdev_reset_tc(bp->dev);
9245 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
9246 		return -ENOMEM;
9247 	}
9248 	return 0;
9249 }
9250 
bnxt_free_irq(struct bnxt * bp)9251 static void bnxt_free_irq(struct bnxt *bp)
9252 {
9253 	struct bnxt_irq *irq;
9254 	int i;
9255 
9256 #ifdef CONFIG_RFS_ACCEL
9257 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
9258 	bp->dev->rx_cpu_rmap = NULL;
9259 #endif
9260 	if (!bp->irq_tbl || !bp->bnapi)
9261 		return;
9262 
9263 	for (i = 0; i < bp->cp_nr_rings; i++) {
9264 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
9265 
9266 		irq = &bp->irq_tbl[map_idx];
9267 		if (irq->requested) {
9268 			if (irq->have_cpumask) {
9269 				irq_set_affinity_hint(irq->vector, NULL);
9270 				free_cpumask_var(irq->cpu_mask);
9271 				irq->have_cpumask = 0;
9272 			}
9273 			free_irq(irq->vector, bp->bnapi[i]);
9274 		}
9275 
9276 		irq->requested = 0;
9277 	}
9278 }
9279 
bnxt_request_irq(struct bnxt * bp)9280 static int bnxt_request_irq(struct bnxt *bp)
9281 {
9282 	int i, j, rc = 0;
9283 	unsigned long flags = 0;
9284 #ifdef CONFIG_RFS_ACCEL
9285 	struct cpu_rmap *rmap;
9286 #endif
9287 
9288 	rc = bnxt_setup_int_mode(bp);
9289 	if (rc) {
9290 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
9291 			   rc);
9292 		return rc;
9293 	}
9294 #ifdef CONFIG_RFS_ACCEL
9295 	rmap = bp->dev->rx_cpu_rmap;
9296 #endif
9297 	if (!(bp->flags & BNXT_FLAG_USING_MSIX))
9298 		flags = IRQF_SHARED;
9299 
9300 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
9301 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
9302 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
9303 
9304 #ifdef CONFIG_RFS_ACCEL
9305 		if (rmap && bp->bnapi[i]->rx_ring) {
9306 			rc = irq_cpu_rmap_add(rmap, irq->vector);
9307 			if (rc)
9308 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
9309 					    j);
9310 			j++;
9311 		}
9312 #endif
9313 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
9314 				 bp->bnapi[i]);
9315 		if (rc)
9316 			break;
9317 
9318 		irq->requested = 1;
9319 
9320 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
9321 			int numa_node = dev_to_node(&bp->pdev->dev);
9322 
9323 			irq->have_cpumask = 1;
9324 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
9325 					irq->cpu_mask);
9326 			rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
9327 			if (rc) {
9328 				netdev_warn(bp->dev,
9329 					    "Set affinity failed, IRQ = %d\n",
9330 					    irq->vector);
9331 				break;
9332 			}
9333 		}
9334 	}
9335 	return rc;
9336 }
9337 
bnxt_del_napi(struct bnxt * bp)9338 static void bnxt_del_napi(struct bnxt *bp)
9339 {
9340 	int i;
9341 
9342 	if (!bp->bnapi)
9343 		return;
9344 
9345 	for (i = 0; i < bp->cp_nr_rings; i++) {
9346 		struct bnxt_napi *bnapi = bp->bnapi[i];
9347 
9348 		__netif_napi_del(&bnapi->napi);
9349 	}
9350 	/* We called __netif_napi_del(), we need
9351 	 * to respect an RCU grace period before freeing napi structures.
9352 	 */
9353 	synchronize_net();
9354 }
9355 
bnxt_init_napi(struct bnxt * bp)9356 static void bnxt_init_napi(struct bnxt *bp)
9357 {
9358 	int i;
9359 	unsigned int cp_nr_rings = bp->cp_nr_rings;
9360 	struct bnxt_napi *bnapi;
9361 
9362 	if (bp->flags & BNXT_FLAG_USING_MSIX) {
9363 		int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
9364 
9365 		if (bp->flags & BNXT_FLAG_CHIP_P5)
9366 			poll_fn = bnxt_poll_p5;
9367 		else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
9368 			cp_nr_rings--;
9369 		for (i = 0; i < cp_nr_rings; i++) {
9370 			bnapi = bp->bnapi[i];
9371 			netif_napi_add(bp->dev, &bnapi->napi, poll_fn);
9372 		}
9373 		if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
9374 			bnapi = bp->bnapi[cp_nr_rings];
9375 			netif_napi_add(bp->dev, &bnapi->napi,
9376 				       bnxt_poll_nitroa0);
9377 		}
9378 	} else {
9379 		bnapi = bp->bnapi[0];
9380 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll);
9381 	}
9382 }
9383 
bnxt_disable_napi(struct bnxt * bp)9384 static void bnxt_disable_napi(struct bnxt *bp)
9385 {
9386 	int i;
9387 
9388 	if (!bp->bnapi ||
9389 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
9390 		return;
9391 
9392 	for (i = 0; i < bp->cp_nr_rings; i++) {
9393 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
9394 
9395 		napi_disable(&bp->bnapi[i]->napi);
9396 		if (bp->bnapi[i]->rx_ring)
9397 			cancel_work_sync(&cpr->dim.work);
9398 	}
9399 }
9400 
bnxt_enable_napi(struct bnxt * bp)9401 static void bnxt_enable_napi(struct bnxt *bp)
9402 {
9403 	int i;
9404 
9405 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
9406 	for (i = 0; i < bp->cp_nr_rings; i++) {
9407 		struct bnxt_napi *bnapi = bp->bnapi[i];
9408 		struct bnxt_cp_ring_info *cpr;
9409 
9410 		cpr = &bnapi->cp_ring;
9411 		if (bnapi->in_reset)
9412 			cpr->sw_stats.rx.rx_resets++;
9413 		bnapi->in_reset = false;
9414 
9415 		if (bnapi->rx_ring) {
9416 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
9417 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
9418 		}
9419 		napi_enable(&bnapi->napi);
9420 	}
9421 }
9422 
bnxt_tx_disable(struct bnxt * bp)9423 void bnxt_tx_disable(struct bnxt *bp)
9424 {
9425 	int i;
9426 	struct bnxt_tx_ring_info *txr;
9427 
9428 	if (bp->tx_ring) {
9429 		for (i = 0; i < bp->tx_nr_rings; i++) {
9430 			txr = &bp->tx_ring[i];
9431 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
9432 		}
9433 	}
9434 	/* Make sure napi polls see @dev_state change */
9435 	synchronize_net();
9436 	/* Drop carrier first to prevent TX timeout */
9437 	netif_carrier_off(bp->dev);
9438 	/* Stop all TX queues */
9439 	netif_tx_disable(bp->dev);
9440 }
9441 
bnxt_tx_enable(struct bnxt * bp)9442 void bnxt_tx_enable(struct bnxt *bp)
9443 {
9444 	int i;
9445 	struct bnxt_tx_ring_info *txr;
9446 
9447 	for (i = 0; i < bp->tx_nr_rings; i++) {
9448 		txr = &bp->tx_ring[i];
9449 		WRITE_ONCE(txr->dev_state, 0);
9450 	}
9451 	/* Make sure napi polls see @dev_state change */
9452 	synchronize_net();
9453 	netif_tx_wake_all_queues(bp->dev);
9454 	if (BNXT_LINK_IS_UP(bp))
9455 		netif_carrier_on(bp->dev);
9456 }
9457 
bnxt_report_fec(struct bnxt_link_info * link_info)9458 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
9459 {
9460 	u8 active_fec = link_info->active_fec_sig_mode &
9461 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
9462 
9463 	switch (active_fec) {
9464 	default:
9465 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
9466 		return "None";
9467 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
9468 		return "Clause 74 BaseR";
9469 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
9470 		return "Clause 91 RS(528,514)";
9471 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
9472 		return "Clause 91 RS544_1XN";
9473 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
9474 		return "Clause 91 RS(544,514)";
9475 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
9476 		return "Clause 91 RS272_1XN";
9477 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
9478 		return "Clause 91 RS(272,257)";
9479 	}
9480 }
9481 
bnxt_report_link(struct bnxt * bp)9482 void bnxt_report_link(struct bnxt *bp)
9483 {
9484 	if (BNXT_LINK_IS_UP(bp)) {
9485 		const char *signal = "";
9486 		const char *flow_ctrl;
9487 		const char *duplex;
9488 		u32 speed;
9489 		u16 fec;
9490 
9491 		netif_carrier_on(bp->dev);
9492 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
9493 		if (speed == SPEED_UNKNOWN) {
9494 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
9495 			return;
9496 		}
9497 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
9498 			duplex = "full";
9499 		else
9500 			duplex = "half";
9501 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
9502 			flow_ctrl = "ON - receive & transmit";
9503 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
9504 			flow_ctrl = "ON - transmit";
9505 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
9506 			flow_ctrl = "ON - receive";
9507 		else
9508 			flow_ctrl = "none";
9509 		if (bp->link_info.phy_qcfg_resp.option_flags &
9510 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
9511 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
9512 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
9513 			switch (sig_mode) {
9514 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
9515 				signal = "(NRZ) ";
9516 				break;
9517 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
9518 				signal = "(PAM4) ";
9519 				break;
9520 			default:
9521 				break;
9522 			}
9523 		}
9524 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
9525 			    speed, signal, duplex, flow_ctrl);
9526 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
9527 			netdev_info(bp->dev, "EEE is %s\n",
9528 				    bp->eee.eee_active ? "active" :
9529 							 "not active");
9530 		fec = bp->link_info.fec_cfg;
9531 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
9532 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
9533 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
9534 				    bnxt_report_fec(&bp->link_info));
9535 	} else {
9536 		netif_carrier_off(bp->dev);
9537 		netdev_err(bp->dev, "NIC Link is Down\n");
9538 	}
9539 }
9540 
bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output * resp)9541 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
9542 {
9543 	if (!resp->supported_speeds_auto_mode &&
9544 	    !resp->supported_speeds_force_mode &&
9545 	    !resp->supported_pam4_speeds_auto_mode &&
9546 	    !resp->supported_pam4_speeds_force_mode)
9547 		return true;
9548 	return false;
9549 }
9550 
bnxt_hwrm_phy_qcaps(struct bnxt * bp)9551 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
9552 {
9553 	struct bnxt_link_info *link_info = &bp->link_info;
9554 	struct hwrm_port_phy_qcaps_output *resp;
9555 	struct hwrm_port_phy_qcaps_input *req;
9556 	int rc = 0;
9557 
9558 	if (bp->hwrm_spec_code < 0x10201)
9559 		return 0;
9560 
9561 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
9562 	if (rc)
9563 		return rc;
9564 
9565 	resp = hwrm_req_hold(bp, req);
9566 	rc = hwrm_req_send(bp, req);
9567 	if (rc)
9568 		goto hwrm_phy_qcaps_exit;
9569 
9570 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
9571 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
9572 		struct ethtool_eee *eee = &bp->eee;
9573 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
9574 
9575 		eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9576 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
9577 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
9578 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
9579 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
9580 	}
9581 
9582 	if (bp->hwrm_spec_code >= 0x10a01) {
9583 		if (bnxt_phy_qcaps_no_speed(resp)) {
9584 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
9585 			netdev_warn(bp->dev, "Ethernet link disabled\n");
9586 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
9587 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
9588 			netdev_info(bp->dev, "Ethernet link enabled\n");
9589 			/* Phy re-enabled, reprobe the speeds */
9590 			link_info->support_auto_speeds = 0;
9591 			link_info->support_pam4_auto_speeds = 0;
9592 		}
9593 	}
9594 	if (resp->supported_speeds_auto_mode)
9595 		link_info->support_auto_speeds =
9596 			le16_to_cpu(resp->supported_speeds_auto_mode);
9597 	if (resp->supported_pam4_speeds_auto_mode)
9598 		link_info->support_pam4_auto_speeds =
9599 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
9600 
9601 	bp->port_count = resp->port_cnt;
9602 
9603 hwrm_phy_qcaps_exit:
9604 	hwrm_req_drop(bp, req);
9605 	return rc;
9606 }
9607 
bnxt_support_dropped(u16 advertising,u16 supported)9608 static bool bnxt_support_dropped(u16 advertising, u16 supported)
9609 {
9610 	u16 diff = advertising ^ supported;
9611 
9612 	return ((supported | diff) != supported);
9613 }
9614 
bnxt_update_link(struct bnxt * bp,bool chng_link_state)9615 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
9616 {
9617 	struct bnxt_link_info *link_info = &bp->link_info;
9618 	struct hwrm_port_phy_qcfg_output *resp;
9619 	struct hwrm_port_phy_qcfg_input *req;
9620 	u8 link_state = link_info->link_state;
9621 	bool support_changed = false;
9622 	int rc;
9623 
9624 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
9625 	if (rc)
9626 		return rc;
9627 
9628 	resp = hwrm_req_hold(bp, req);
9629 	rc = hwrm_req_send(bp, req);
9630 	if (rc) {
9631 		hwrm_req_drop(bp, req);
9632 		if (BNXT_VF(bp) && rc == -ENODEV) {
9633 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
9634 			rc = 0;
9635 		}
9636 		return rc;
9637 	}
9638 
9639 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
9640 	link_info->phy_link_status = resp->link;
9641 	link_info->duplex = resp->duplex_cfg;
9642 	if (bp->hwrm_spec_code >= 0x10800)
9643 		link_info->duplex = resp->duplex_state;
9644 	link_info->pause = resp->pause;
9645 	link_info->auto_mode = resp->auto_mode;
9646 	link_info->auto_pause_setting = resp->auto_pause;
9647 	link_info->lp_pause = resp->link_partner_adv_pause;
9648 	link_info->force_pause_setting = resp->force_pause;
9649 	link_info->duplex_setting = resp->duplex_cfg;
9650 	if (link_info->phy_link_status == BNXT_LINK_LINK)
9651 		link_info->link_speed = le16_to_cpu(resp->link_speed);
9652 	else
9653 		link_info->link_speed = 0;
9654 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
9655 	link_info->force_pam4_link_speed =
9656 		le16_to_cpu(resp->force_pam4_link_speed);
9657 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
9658 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
9659 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
9660 	link_info->auto_pam4_link_speeds =
9661 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
9662 	link_info->lp_auto_link_speeds =
9663 		le16_to_cpu(resp->link_partner_adv_speeds);
9664 	link_info->lp_auto_pam4_link_speeds =
9665 		resp->link_partner_pam4_adv_speeds;
9666 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
9667 	link_info->phy_ver[0] = resp->phy_maj;
9668 	link_info->phy_ver[1] = resp->phy_min;
9669 	link_info->phy_ver[2] = resp->phy_bld;
9670 	link_info->media_type = resp->media_type;
9671 	link_info->phy_type = resp->phy_type;
9672 	link_info->transceiver = resp->xcvr_pkg_type;
9673 	link_info->phy_addr = resp->eee_config_phy_addr &
9674 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
9675 	link_info->module_status = resp->module_status;
9676 
9677 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
9678 		struct ethtool_eee *eee = &bp->eee;
9679 		u16 fw_speeds;
9680 
9681 		eee->eee_active = 0;
9682 		if (resp->eee_config_phy_addr &
9683 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
9684 			eee->eee_active = 1;
9685 			fw_speeds = le16_to_cpu(
9686 				resp->link_partner_adv_eee_link_speed_mask);
9687 			eee->lp_advertised =
9688 				_bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9689 		}
9690 
9691 		/* Pull initial EEE config */
9692 		if (!chng_link_state) {
9693 			if (resp->eee_config_phy_addr &
9694 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
9695 				eee->eee_enabled = 1;
9696 
9697 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
9698 			eee->advertised =
9699 				_bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9700 
9701 			if (resp->eee_config_phy_addr &
9702 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
9703 				__le32 tmr;
9704 
9705 				eee->tx_lpi_enabled = 1;
9706 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
9707 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
9708 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
9709 			}
9710 		}
9711 	}
9712 
9713 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
9714 	if (bp->hwrm_spec_code >= 0x10504) {
9715 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
9716 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
9717 	}
9718 	/* TODO: need to add more logic to report VF link */
9719 	if (chng_link_state) {
9720 		if (link_info->phy_link_status == BNXT_LINK_LINK)
9721 			link_info->link_state = BNXT_LINK_STATE_UP;
9722 		else
9723 			link_info->link_state = BNXT_LINK_STATE_DOWN;
9724 		if (link_state != link_info->link_state)
9725 			bnxt_report_link(bp);
9726 	} else {
9727 		/* always link down if not require to update link state */
9728 		link_info->link_state = BNXT_LINK_STATE_DOWN;
9729 	}
9730 	hwrm_req_drop(bp, req);
9731 
9732 	if (!BNXT_PHY_CFG_ABLE(bp))
9733 		return 0;
9734 
9735 	/* Check if any advertised speeds are no longer supported. The caller
9736 	 * holds the link_lock mutex, so we can modify link_info settings.
9737 	 */
9738 	if (bnxt_support_dropped(link_info->advertising,
9739 				 link_info->support_auto_speeds)) {
9740 		link_info->advertising = link_info->support_auto_speeds;
9741 		support_changed = true;
9742 	}
9743 	if (bnxt_support_dropped(link_info->advertising_pam4,
9744 				 link_info->support_pam4_auto_speeds)) {
9745 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
9746 		support_changed = true;
9747 	}
9748 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
9749 		bnxt_hwrm_set_link_setting(bp, true, false);
9750 	return 0;
9751 }
9752 
bnxt_get_port_module_status(struct bnxt * bp)9753 static void bnxt_get_port_module_status(struct bnxt *bp)
9754 {
9755 	struct bnxt_link_info *link_info = &bp->link_info;
9756 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
9757 	u8 module_status;
9758 
9759 	if (bnxt_update_link(bp, true))
9760 		return;
9761 
9762 	module_status = link_info->module_status;
9763 	switch (module_status) {
9764 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
9765 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
9766 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
9767 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
9768 			    bp->pf.port_id);
9769 		if (bp->hwrm_spec_code >= 0x10201) {
9770 			netdev_warn(bp->dev, "Module part number %s\n",
9771 				    resp->phy_vendor_partnumber);
9772 		}
9773 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
9774 			netdev_warn(bp->dev, "TX is disabled\n");
9775 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
9776 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
9777 	}
9778 }
9779 
9780 static void
bnxt_hwrm_set_pause_common(struct bnxt * bp,struct hwrm_port_phy_cfg_input * req)9781 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
9782 {
9783 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
9784 		if (bp->hwrm_spec_code >= 0x10201)
9785 			req->auto_pause =
9786 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
9787 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
9788 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
9789 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
9790 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
9791 		req->enables |=
9792 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
9793 	} else {
9794 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
9795 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
9796 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
9797 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
9798 		req->enables |=
9799 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
9800 		if (bp->hwrm_spec_code >= 0x10201) {
9801 			req->auto_pause = req->force_pause;
9802 			req->enables |= cpu_to_le32(
9803 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
9804 		}
9805 	}
9806 }
9807 
bnxt_hwrm_set_link_common(struct bnxt * bp,struct hwrm_port_phy_cfg_input * req)9808 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
9809 {
9810 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
9811 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
9812 		if (bp->link_info.advertising) {
9813 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
9814 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
9815 		}
9816 		if (bp->link_info.advertising_pam4) {
9817 			req->enables |=
9818 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
9819 			req->auto_link_pam4_speed_mask =
9820 				cpu_to_le16(bp->link_info.advertising_pam4);
9821 		}
9822 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
9823 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
9824 	} else {
9825 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
9826 		if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
9827 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
9828 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
9829 		} else {
9830 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
9831 		}
9832 	}
9833 
9834 	/* tell chimp that the setting takes effect immediately */
9835 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
9836 }
9837 
bnxt_hwrm_set_pause(struct bnxt * bp)9838 int bnxt_hwrm_set_pause(struct bnxt *bp)
9839 {
9840 	struct hwrm_port_phy_cfg_input *req;
9841 	int rc;
9842 
9843 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
9844 	if (rc)
9845 		return rc;
9846 
9847 	bnxt_hwrm_set_pause_common(bp, req);
9848 
9849 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
9850 	    bp->link_info.force_link_chng)
9851 		bnxt_hwrm_set_link_common(bp, req);
9852 
9853 	rc = hwrm_req_send(bp, req);
9854 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
9855 		/* since changing of pause setting doesn't trigger any link
9856 		 * change event, the driver needs to update the current pause
9857 		 * result upon successfully return of the phy_cfg command
9858 		 */
9859 		bp->link_info.pause =
9860 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
9861 		bp->link_info.auto_pause_setting = 0;
9862 		if (!bp->link_info.force_link_chng)
9863 			bnxt_report_link(bp);
9864 	}
9865 	bp->link_info.force_link_chng = false;
9866 	return rc;
9867 }
9868 
bnxt_hwrm_set_eee(struct bnxt * bp,struct hwrm_port_phy_cfg_input * req)9869 static void bnxt_hwrm_set_eee(struct bnxt *bp,
9870 			      struct hwrm_port_phy_cfg_input *req)
9871 {
9872 	struct ethtool_eee *eee = &bp->eee;
9873 
9874 	if (eee->eee_enabled) {
9875 		u16 eee_speeds;
9876 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
9877 
9878 		if (eee->tx_lpi_enabled)
9879 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
9880 		else
9881 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
9882 
9883 		req->flags |= cpu_to_le32(flags);
9884 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
9885 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
9886 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
9887 	} else {
9888 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
9889 	}
9890 }
9891 
bnxt_hwrm_set_link_setting(struct bnxt * bp,bool set_pause,bool set_eee)9892 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
9893 {
9894 	struct hwrm_port_phy_cfg_input *req;
9895 	int rc;
9896 
9897 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
9898 	if (rc)
9899 		return rc;
9900 
9901 	if (set_pause)
9902 		bnxt_hwrm_set_pause_common(bp, req);
9903 
9904 	bnxt_hwrm_set_link_common(bp, req);
9905 
9906 	if (set_eee)
9907 		bnxt_hwrm_set_eee(bp, req);
9908 	return hwrm_req_send(bp, req);
9909 }
9910 
bnxt_hwrm_shutdown_link(struct bnxt * bp)9911 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
9912 {
9913 	struct hwrm_port_phy_cfg_input *req;
9914 	int rc;
9915 
9916 	if (!BNXT_SINGLE_PF(bp))
9917 		return 0;
9918 
9919 	if (pci_num_vf(bp->pdev) &&
9920 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
9921 		return 0;
9922 
9923 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
9924 	if (rc)
9925 		return rc;
9926 
9927 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
9928 	rc = hwrm_req_send(bp, req);
9929 	if (!rc) {
9930 		mutex_lock(&bp->link_lock);
9931 		/* Device is not obliged link down in certain scenarios, even
9932 		 * when forced. Setting the state unknown is consistent with
9933 		 * driver startup and will force link state to be reported
9934 		 * during subsequent open based on PORT_PHY_QCFG.
9935 		 */
9936 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
9937 		mutex_unlock(&bp->link_lock);
9938 	}
9939 	return rc;
9940 }
9941 
bnxt_fw_reset_via_optee(struct bnxt * bp)9942 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
9943 {
9944 #ifdef CONFIG_TEE_BNXT_FW
9945 	int rc = tee_bnxt_fw_load();
9946 
9947 	if (rc)
9948 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
9949 
9950 	return rc;
9951 #else
9952 	netdev_err(bp->dev, "OP-TEE not supported\n");
9953 	return -ENODEV;
9954 #endif
9955 }
9956 
bnxt_try_recover_fw(struct bnxt * bp)9957 static int bnxt_try_recover_fw(struct bnxt *bp)
9958 {
9959 	if (bp->fw_health && bp->fw_health->status_reliable) {
9960 		int retry = 0, rc;
9961 		u32 sts;
9962 
9963 		do {
9964 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
9965 			rc = bnxt_hwrm_poll(bp);
9966 			if (!BNXT_FW_IS_BOOTING(sts) &&
9967 			    !BNXT_FW_IS_RECOVERING(sts))
9968 				break;
9969 			retry++;
9970 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
9971 
9972 		if (!BNXT_FW_IS_HEALTHY(sts)) {
9973 			netdev_err(bp->dev,
9974 				   "Firmware not responding, status: 0x%x\n",
9975 				   sts);
9976 			rc = -ENODEV;
9977 		}
9978 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
9979 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
9980 			return bnxt_fw_reset_via_optee(bp);
9981 		}
9982 		return rc;
9983 	}
9984 
9985 	return -ENODEV;
9986 }
9987 
bnxt_clear_reservations(struct bnxt * bp,bool fw_reset)9988 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
9989 {
9990 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9991 
9992 	if (!BNXT_NEW_RM(bp))
9993 		return; /* no resource reservations required */
9994 
9995 	hw_resc->resv_cp_rings = 0;
9996 	hw_resc->resv_stat_ctxs = 0;
9997 	hw_resc->resv_irqs = 0;
9998 	hw_resc->resv_tx_rings = 0;
9999 	hw_resc->resv_rx_rings = 0;
10000 	hw_resc->resv_hw_ring_grps = 0;
10001 	hw_resc->resv_vnics = 0;
10002 	if (!fw_reset) {
10003 		bp->tx_nr_rings = 0;
10004 		bp->rx_nr_rings = 0;
10005 	}
10006 }
10007 
bnxt_cancel_reservations(struct bnxt * bp,bool fw_reset)10008 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
10009 {
10010 	int rc;
10011 
10012 	if (!BNXT_NEW_RM(bp))
10013 		return 0; /* no resource reservations required */
10014 
10015 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
10016 	if (rc)
10017 		netdev_err(bp->dev, "resc_qcaps failed\n");
10018 
10019 	bnxt_clear_reservations(bp, fw_reset);
10020 
10021 	return rc;
10022 }
10023 
bnxt_hwrm_if_change(struct bnxt * bp,bool up)10024 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
10025 {
10026 	struct hwrm_func_drv_if_change_output *resp;
10027 	struct hwrm_func_drv_if_change_input *req;
10028 	bool fw_reset = !bp->irq_tbl;
10029 	bool resc_reinit = false;
10030 	int rc, retry = 0;
10031 	u32 flags = 0;
10032 
10033 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
10034 		return 0;
10035 
10036 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
10037 	if (rc)
10038 		return rc;
10039 
10040 	if (up)
10041 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
10042 	resp = hwrm_req_hold(bp, req);
10043 
10044 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10045 	while (retry < BNXT_FW_IF_RETRY) {
10046 		rc = hwrm_req_send(bp, req);
10047 		if (rc != -EAGAIN)
10048 			break;
10049 
10050 		msleep(50);
10051 		retry++;
10052 	}
10053 
10054 	if (rc == -EAGAIN) {
10055 		hwrm_req_drop(bp, req);
10056 		return rc;
10057 	} else if (!rc) {
10058 		flags = le32_to_cpu(resp->flags);
10059 	} else if (up) {
10060 		rc = bnxt_try_recover_fw(bp);
10061 		fw_reset = true;
10062 	}
10063 	hwrm_req_drop(bp, req);
10064 	if (rc)
10065 		return rc;
10066 
10067 	if (!up) {
10068 		bnxt_inv_fw_health_reg(bp);
10069 		return 0;
10070 	}
10071 
10072 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
10073 		resc_reinit = true;
10074 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
10075 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
10076 		fw_reset = true;
10077 	else
10078 		bnxt_remap_fw_health_regs(bp);
10079 
10080 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
10081 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
10082 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10083 		return -ENODEV;
10084 	}
10085 	if (resc_reinit || fw_reset) {
10086 		if (fw_reset) {
10087 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
10088 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
10089 				bnxt_ulp_stop(bp);
10090 			bnxt_free_ctx_mem(bp);
10091 			kfree(bp->ctx);
10092 			bp->ctx = NULL;
10093 			bnxt_dcb_free(bp);
10094 			rc = bnxt_fw_init_one(bp);
10095 			if (rc) {
10096 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
10097 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10098 				return rc;
10099 			}
10100 			bnxt_clear_int_mode(bp);
10101 			rc = bnxt_init_int_mode(bp);
10102 			if (rc) {
10103 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
10104 				netdev_err(bp->dev, "init int mode failed\n");
10105 				return rc;
10106 			}
10107 		}
10108 		rc = bnxt_cancel_reservations(bp, fw_reset);
10109 	}
10110 	return rc;
10111 }
10112 
bnxt_hwrm_port_led_qcaps(struct bnxt * bp)10113 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
10114 {
10115 	struct hwrm_port_led_qcaps_output *resp;
10116 	struct hwrm_port_led_qcaps_input *req;
10117 	struct bnxt_pf_info *pf = &bp->pf;
10118 	int rc;
10119 
10120 	bp->num_leds = 0;
10121 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
10122 		return 0;
10123 
10124 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
10125 	if (rc)
10126 		return rc;
10127 
10128 	req->port_id = cpu_to_le16(pf->port_id);
10129 	resp = hwrm_req_hold(bp, req);
10130 	rc = hwrm_req_send(bp, req);
10131 	if (rc) {
10132 		hwrm_req_drop(bp, req);
10133 		return rc;
10134 	}
10135 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
10136 		int i;
10137 
10138 		bp->num_leds = resp->num_leds;
10139 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
10140 						 bp->num_leds);
10141 		for (i = 0; i < bp->num_leds; i++) {
10142 			struct bnxt_led_info *led = &bp->leds[i];
10143 			__le16 caps = led->led_state_caps;
10144 
10145 			if (!led->led_group_id ||
10146 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
10147 				bp->num_leds = 0;
10148 				break;
10149 			}
10150 		}
10151 	}
10152 	hwrm_req_drop(bp, req);
10153 	return 0;
10154 }
10155 
bnxt_hwrm_alloc_wol_fltr(struct bnxt * bp)10156 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
10157 {
10158 	struct hwrm_wol_filter_alloc_output *resp;
10159 	struct hwrm_wol_filter_alloc_input *req;
10160 	int rc;
10161 
10162 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
10163 	if (rc)
10164 		return rc;
10165 
10166 	req->port_id = cpu_to_le16(bp->pf.port_id);
10167 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
10168 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
10169 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
10170 
10171 	resp = hwrm_req_hold(bp, req);
10172 	rc = hwrm_req_send(bp, req);
10173 	if (!rc)
10174 		bp->wol_filter_id = resp->wol_filter_id;
10175 	hwrm_req_drop(bp, req);
10176 	return rc;
10177 }
10178 
bnxt_hwrm_free_wol_fltr(struct bnxt * bp)10179 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
10180 {
10181 	struct hwrm_wol_filter_free_input *req;
10182 	int rc;
10183 
10184 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
10185 	if (rc)
10186 		return rc;
10187 
10188 	req->port_id = cpu_to_le16(bp->pf.port_id);
10189 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
10190 	req->wol_filter_id = bp->wol_filter_id;
10191 
10192 	return hwrm_req_send(bp, req);
10193 }
10194 
bnxt_hwrm_get_wol_fltrs(struct bnxt * bp,u16 handle)10195 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
10196 {
10197 	struct hwrm_wol_filter_qcfg_output *resp;
10198 	struct hwrm_wol_filter_qcfg_input *req;
10199 	u16 next_handle = 0;
10200 	int rc;
10201 
10202 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
10203 	if (rc)
10204 		return rc;
10205 
10206 	req->port_id = cpu_to_le16(bp->pf.port_id);
10207 	req->handle = cpu_to_le16(handle);
10208 	resp = hwrm_req_hold(bp, req);
10209 	rc = hwrm_req_send(bp, req);
10210 	if (!rc) {
10211 		next_handle = le16_to_cpu(resp->next_handle);
10212 		if (next_handle != 0) {
10213 			if (resp->wol_type ==
10214 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
10215 				bp->wol = 1;
10216 				bp->wol_filter_id = resp->wol_filter_id;
10217 			}
10218 		}
10219 	}
10220 	hwrm_req_drop(bp, req);
10221 	return next_handle;
10222 }
10223 
bnxt_get_wol_settings(struct bnxt * bp)10224 static void bnxt_get_wol_settings(struct bnxt *bp)
10225 {
10226 	u16 handle = 0;
10227 
10228 	bp->wol = 0;
10229 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
10230 		return;
10231 
10232 	do {
10233 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
10234 	} while (handle && handle != 0xffff);
10235 }
10236 
10237 #ifdef CONFIG_BNXT_HWMON
bnxt_show_temp(struct device * dev,struct device_attribute * devattr,char * buf)10238 static ssize_t bnxt_show_temp(struct device *dev,
10239 			      struct device_attribute *devattr, char *buf)
10240 {
10241 	struct hwrm_temp_monitor_query_output *resp;
10242 	struct hwrm_temp_monitor_query_input *req;
10243 	struct bnxt *bp = dev_get_drvdata(dev);
10244 	u32 len = 0;
10245 	int rc;
10246 
10247 	rc = hwrm_req_init(bp, req, HWRM_TEMP_MONITOR_QUERY);
10248 	if (rc)
10249 		return rc;
10250 	resp = hwrm_req_hold(bp, req);
10251 	rc = hwrm_req_send(bp, req);
10252 	if (!rc)
10253 		len = sprintf(buf, "%u\n", resp->temp * 1000); /* display millidegree */
10254 	hwrm_req_drop(bp, req);
10255 	if (rc)
10256 		return rc;
10257 	return len;
10258 }
10259 static SENSOR_DEVICE_ATTR(temp1_input, 0444, bnxt_show_temp, NULL, 0);
10260 
10261 static struct attribute *bnxt_attrs[] = {
10262 	&sensor_dev_attr_temp1_input.dev_attr.attr,
10263 	NULL
10264 };
10265 ATTRIBUTE_GROUPS(bnxt);
10266 
bnxt_hwmon_close(struct bnxt * bp)10267 static void bnxt_hwmon_close(struct bnxt *bp)
10268 {
10269 	if (bp->hwmon_dev) {
10270 		hwmon_device_unregister(bp->hwmon_dev);
10271 		bp->hwmon_dev = NULL;
10272 	}
10273 }
10274 
bnxt_hwmon_open(struct bnxt * bp)10275 static void bnxt_hwmon_open(struct bnxt *bp)
10276 {
10277 	struct hwrm_temp_monitor_query_input *req;
10278 	struct pci_dev *pdev = bp->pdev;
10279 	int rc;
10280 
10281 	rc = hwrm_req_init(bp, req, HWRM_TEMP_MONITOR_QUERY);
10282 	if (!rc)
10283 		rc = hwrm_req_send_silent(bp, req);
10284 	if (rc == -EACCES || rc == -EOPNOTSUPP) {
10285 		bnxt_hwmon_close(bp);
10286 		return;
10287 	}
10288 
10289 	if (bp->hwmon_dev)
10290 		return;
10291 
10292 	bp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev,
10293 							  DRV_MODULE_NAME, bp,
10294 							  bnxt_groups);
10295 	if (IS_ERR(bp->hwmon_dev)) {
10296 		bp->hwmon_dev = NULL;
10297 		dev_warn(&pdev->dev, "Cannot register hwmon device\n");
10298 	}
10299 }
10300 #else
bnxt_hwmon_close(struct bnxt * bp)10301 static void bnxt_hwmon_close(struct bnxt *bp)
10302 {
10303 }
10304 
bnxt_hwmon_open(struct bnxt * bp)10305 static void bnxt_hwmon_open(struct bnxt *bp)
10306 {
10307 }
10308 #endif
10309 
bnxt_eee_config_ok(struct bnxt * bp)10310 static bool bnxt_eee_config_ok(struct bnxt *bp)
10311 {
10312 	struct ethtool_eee *eee = &bp->eee;
10313 	struct bnxt_link_info *link_info = &bp->link_info;
10314 
10315 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
10316 		return true;
10317 
10318 	if (eee->eee_enabled) {
10319 		u32 advertising =
10320 			_bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
10321 
10322 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
10323 			eee->eee_enabled = 0;
10324 			return false;
10325 		}
10326 		if (eee->advertised & ~advertising) {
10327 			eee->advertised = advertising & eee->supported;
10328 			return false;
10329 		}
10330 	}
10331 	return true;
10332 }
10333 
bnxt_update_phy_setting(struct bnxt * bp)10334 static int bnxt_update_phy_setting(struct bnxt *bp)
10335 {
10336 	int rc;
10337 	bool update_link = false;
10338 	bool update_pause = false;
10339 	bool update_eee = false;
10340 	struct bnxt_link_info *link_info = &bp->link_info;
10341 
10342 	rc = bnxt_update_link(bp, true);
10343 	if (rc) {
10344 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
10345 			   rc);
10346 		return rc;
10347 	}
10348 	if (!BNXT_SINGLE_PF(bp))
10349 		return 0;
10350 
10351 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
10352 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
10353 	    link_info->req_flow_ctrl)
10354 		update_pause = true;
10355 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
10356 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
10357 		update_pause = true;
10358 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
10359 		if (BNXT_AUTO_MODE(link_info->auto_mode))
10360 			update_link = true;
10361 		if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
10362 		    link_info->req_link_speed != link_info->force_link_speed)
10363 			update_link = true;
10364 		else if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
10365 			 link_info->req_link_speed != link_info->force_pam4_link_speed)
10366 			update_link = true;
10367 		if (link_info->req_duplex != link_info->duplex_setting)
10368 			update_link = true;
10369 	} else {
10370 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
10371 			update_link = true;
10372 		if (link_info->advertising != link_info->auto_link_speeds ||
10373 		    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
10374 			update_link = true;
10375 	}
10376 
10377 	/* The last close may have shutdown the link, so need to call
10378 	 * PHY_CFG to bring it back up.
10379 	 */
10380 	if (!BNXT_LINK_IS_UP(bp))
10381 		update_link = true;
10382 
10383 	if (!bnxt_eee_config_ok(bp))
10384 		update_eee = true;
10385 
10386 	if (update_link)
10387 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
10388 	else if (update_pause)
10389 		rc = bnxt_hwrm_set_pause(bp);
10390 	if (rc) {
10391 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
10392 			   rc);
10393 		return rc;
10394 	}
10395 
10396 	return rc;
10397 }
10398 
10399 /* Common routine to pre-map certain register block to different GRC window.
10400  * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
10401  * in PF and 3 windows in VF that can be customized to map in different
10402  * register blocks.
10403  */
bnxt_preset_reg_win(struct bnxt * bp)10404 static void bnxt_preset_reg_win(struct bnxt *bp)
10405 {
10406 	if (BNXT_PF(bp)) {
10407 		/* CAG registers map to GRC window #4 */
10408 		writel(BNXT_CAG_REG_BASE,
10409 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
10410 	}
10411 }
10412 
10413 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
10414 
bnxt_reinit_after_abort(struct bnxt * bp)10415 static int bnxt_reinit_after_abort(struct bnxt *bp)
10416 {
10417 	int rc;
10418 
10419 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
10420 		return -EBUSY;
10421 
10422 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
10423 		return -ENODEV;
10424 
10425 	rc = bnxt_fw_init_one(bp);
10426 	if (!rc) {
10427 		bnxt_clear_int_mode(bp);
10428 		rc = bnxt_init_int_mode(bp);
10429 		if (!rc) {
10430 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10431 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
10432 		}
10433 	}
10434 	return rc;
10435 }
10436 
__bnxt_open_nic(struct bnxt * bp,bool irq_re_init,bool link_re_init)10437 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10438 {
10439 	int rc = 0;
10440 
10441 	bnxt_preset_reg_win(bp);
10442 	netif_carrier_off(bp->dev);
10443 	if (irq_re_init) {
10444 		/* Reserve rings now if none were reserved at driver probe. */
10445 		rc = bnxt_init_dflt_ring_mode(bp);
10446 		if (rc) {
10447 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
10448 			return rc;
10449 		}
10450 	}
10451 	rc = bnxt_reserve_rings(bp, irq_re_init);
10452 	if (rc)
10453 		return rc;
10454 	if ((bp->flags & BNXT_FLAG_RFS) &&
10455 	    !(bp->flags & BNXT_FLAG_USING_MSIX)) {
10456 		/* disable RFS if falling back to INTA */
10457 		bp->dev->hw_features &= ~NETIF_F_NTUPLE;
10458 		bp->flags &= ~BNXT_FLAG_RFS;
10459 	}
10460 
10461 	rc = bnxt_alloc_mem(bp, irq_re_init);
10462 	if (rc) {
10463 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
10464 		goto open_err_free_mem;
10465 	}
10466 
10467 	if (irq_re_init) {
10468 		bnxt_init_napi(bp);
10469 		rc = bnxt_request_irq(bp);
10470 		if (rc) {
10471 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
10472 			goto open_err_irq;
10473 		}
10474 	}
10475 
10476 	rc = bnxt_init_nic(bp, irq_re_init);
10477 	if (rc) {
10478 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
10479 		goto open_err_irq;
10480 	}
10481 
10482 	bnxt_enable_napi(bp);
10483 	bnxt_debug_dev_init(bp);
10484 
10485 	if (link_re_init) {
10486 		mutex_lock(&bp->link_lock);
10487 		rc = bnxt_update_phy_setting(bp);
10488 		mutex_unlock(&bp->link_lock);
10489 		if (rc) {
10490 			netdev_warn(bp->dev, "failed to update phy settings\n");
10491 			if (BNXT_SINGLE_PF(bp)) {
10492 				bp->link_info.phy_retry = true;
10493 				bp->link_info.phy_retry_expires =
10494 					jiffies + 5 * HZ;
10495 			}
10496 		}
10497 	}
10498 
10499 	if (irq_re_init)
10500 		udp_tunnel_nic_reset_ntf(bp->dev);
10501 
10502 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
10503 		if (!static_key_enabled(&bnxt_xdp_locking_key))
10504 			static_branch_enable(&bnxt_xdp_locking_key);
10505 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
10506 		static_branch_disable(&bnxt_xdp_locking_key);
10507 	}
10508 	set_bit(BNXT_STATE_OPEN, &bp->state);
10509 	bnxt_enable_int(bp);
10510 	/* Enable TX queues */
10511 	bnxt_tx_enable(bp);
10512 	mod_timer(&bp->timer, jiffies + bp->current_interval);
10513 	/* Poll link status and check for SFP+ module status */
10514 	mutex_lock(&bp->link_lock);
10515 	bnxt_get_port_module_status(bp);
10516 	mutex_unlock(&bp->link_lock);
10517 
10518 	/* VF-reps may need to be re-opened after the PF is re-opened */
10519 	if (BNXT_PF(bp))
10520 		bnxt_vf_reps_open(bp);
10521 	bnxt_ptp_init_rtc(bp, true);
10522 	bnxt_ptp_cfg_tstamp_filters(bp);
10523 	return 0;
10524 
10525 open_err_irq:
10526 	bnxt_del_napi(bp);
10527 
10528 open_err_free_mem:
10529 	bnxt_free_skbs(bp);
10530 	bnxt_free_irq(bp);
10531 	bnxt_free_mem(bp, true);
10532 	return rc;
10533 }
10534 
10535 /* rtnl_lock held */
bnxt_open_nic(struct bnxt * bp,bool irq_re_init,bool link_re_init)10536 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10537 {
10538 	int rc = 0;
10539 
10540 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
10541 		rc = -EIO;
10542 	if (!rc)
10543 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
10544 	if (rc) {
10545 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
10546 		dev_close(bp->dev);
10547 	}
10548 	return rc;
10549 }
10550 
10551 /* rtnl_lock held, open the NIC half way by allocating all resources, but
10552  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
10553  * self tests.
10554  */
bnxt_half_open_nic(struct bnxt * bp)10555 int bnxt_half_open_nic(struct bnxt *bp)
10556 {
10557 	int rc = 0;
10558 
10559 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
10560 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
10561 		rc = -ENODEV;
10562 		goto half_open_err;
10563 	}
10564 
10565 	rc = bnxt_alloc_mem(bp, true);
10566 	if (rc) {
10567 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
10568 		goto half_open_err;
10569 	}
10570 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
10571 	rc = bnxt_init_nic(bp, true);
10572 	if (rc) {
10573 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
10574 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
10575 		goto half_open_err;
10576 	}
10577 	return 0;
10578 
10579 half_open_err:
10580 	bnxt_free_skbs(bp);
10581 	bnxt_free_mem(bp, true);
10582 	dev_close(bp->dev);
10583 	return rc;
10584 }
10585 
10586 /* rtnl_lock held, this call can only be made after a previous successful
10587  * call to bnxt_half_open_nic().
10588  */
bnxt_half_close_nic(struct bnxt * bp)10589 void bnxt_half_close_nic(struct bnxt *bp)
10590 {
10591 	bnxt_hwrm_resource_free(bp, false, true);
10592 	bnxt_free_skbs(bp);
10593 	bnxt_free_mem(bp, true);
10594 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
10595 }
10596 
bnxt_reenable_sriov(struct bnxt * bp)10597 void bnxt_reenable_sriov(struct bnxt *bp)
10598 {
10599 	if (BNXT_PF(bp)) {
10600 		struct bnxt_pf_info *pf = &bp->pf;
10601 		int n = pf->active_vfs;
10602 
10603 		if (n)
10604 			bnxt_cfg_hw_sriov(bp, &n, true);
10605 	}
10606 }
10607 
bnxt_open(struct net_device * dev)10608 static int bnxt_open(struct net_device *dev)
10609 {
10610 	struct bnxt *bp = netdev_priv(dev);
10611 	int rc;
10612 
10613 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
10614 		rc = bnxt_reinit_after_abort(bp);
10615 		if (rc) {
10616 			if (rc == -EBUSY)
10617 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
10618 			else
10619 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
10620 			return -ENODEV;
10621 		}
10622 	}
10623 
10624 	rc = bnxt_hwrm_if_change(bp, true);
10625 	if (rc)
10626 		return rc;
10627 
10628 	rc = __bnxt_open_nic(bp, true, true);
10629 	if (rc) {
10630 		bnxt_hwrm_if_change(bp, false);
10631 	} else {
10632 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
10633 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
10634 				bnxt_ulp_start(bp, 0);
10635 				bnxt_reenable_sriov(bp);
10636 			}
10637 		}
10638 		bnxt_hwmon_open(bp);
10639 	}
10640 
10641 	return rc;
10642 }
10643 
bnxt_drv_busy(struct bnxt * bp)10644 static bool bnxt_drv_busy(struct bnxt *bp)
10645 {
10646 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
10647 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
10648 }
10649 
10650 static void bnxt_get_ring_stats(struct bnxt *bp,
10651 				struct rtnl_link_stats64 *stats);
10652 
__bnxt_close_nic(struct bnxt * bp,bool irq_re_init,bool link_re_init)10653 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
10654 			     bool link_re_init)
10655 {
10656 	/* Close the VF-reps before closing PF */
10657 	if (BNXT_PF(bp))
10658 		bnxt_vf_reps_close(bp);
10659 
10660 	/* Change device state to avoid TX queue wake up's */
10661 	bnxt_tx_disable(bp);
10662 
10663 	clear_bit(BNXT_STATE_OPEN, &bp->state);
10664 	smp_mb__after_atomic();
10665 	while (bnxt_drv_busy(bp))
10666 		msleep(20);
10667 
10668 	/* Flush rings and disable interrupts */
10669 	bnxt_shutdown_nic(bp, irq_re_init);
10670 
10671 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
10672 
10673 	bnxt_debug_dev_exit(bp);
10674 	bnxt_disable_napi(bp);
10675 	del_timer_sync(&bp->timer);
10676 	bnxt_free_skbs(bp);
10677 
10678 	/* Save ring stats before shutdown */
10679 	if (bp->bnapi && irq_re_init)
10680 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
10681 	if (irq_re_init) {
10682 		bnxt_free_irq(bp);
10683 		bnxt_del_napi(bp);
10684 	}
10685 	bnxt_free_mem(bp, irq_re_init);
10686 }
10687 
bnxt_close_nic(struct bnxt * bp,bool irq_re_init,bool link_re_init)10688 int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10689 {
10690 	int rc = 0;
10691 
10692 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
10693 		/* If we get here, it means firmware reset is in progress
10694 		 * while we are trying to close.  We can safely proceed with
10695 		 * the close because we are holding rtnl_lock().  Some firmware
10696 		 * messages may fail as we proceed to close.  We set the
10697 		 * ABORT_ERR flag here so that the FW reset thread will later
10698 		 * abort when it gets the rtnl_lock() and sees the flag.
10699 		 */
10700 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
10701 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10702 	}
10703 
10704 #ifdef CONFIG_BNXT_SRIOV
10705 	if (bp->sriov_cfg) {
10706 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
10707 						      !bp->sriov_cfg,
10708 						      BNXT_SRIOV_CFG_WAIT_TMO);
10709 		if (rc)
10710 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
10711 	}
10712 #endif
10713 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
10714 	return rc;
10715 }
10716 
bnxt_close(struct net_device * dev)10717 static int bnxt_close(struct net_device *dev)
10718 {
10719 	struct bnxt *bp = netdev_priv(dev);
10720 
10721 	bnxt_hwmon_close(bp);
10722 	bnxt_close_nic(bp, true, true);
10723 	bnxt_hwrm_shutdown_link(bp);
10724 	bnxt_hwrm_if_change(bp, false);
10725 	return 0;
10726 }
10727 
bnxt_hwrm_port_phy_read(struct bnxt * bp,u16 phy_addr,u16 reg,u16 * val)10728 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
10729 				   u16 *val)
10730 {
10731 	struct hwrm_port_phy_mdio_read_output *resp;
10732 	struct hwrm_port_phy_mdio_read_input *req;
10733 	int rc;
10734 
10735 	if (bp->hwrm_spec_code < 0x10a00)
10736 		return -EOPNOTSUPP;
10737 
10738 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
10739 	if (rc)
10740 		return rc;
10741 
10742 	req->port_id = cpu_to_le16(bp->pf.port_id);
10743 	req->phy_addr = phy_addr;
10744 	req->reg_addr = cpu_to_le16(reg & 0x1f);
10745 	if (mdio_phy_id_is_c45(phy_addr)) {
10746 		req->cl45_mdio = 1;
10747 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
10748 		req->dev_addr = mdio_phy_id_devad(phy_addr);
10749 		req->reg_addr = cpu_to_le16(reg);
10750 	}
10751 
10752 	resp = hwrm_req_hold(bp, req);
10753 	rc = hwrm_req_send(bp, req);
10754 	if (!rc)
10755 		*val = le16_to_cpu(resp->reg_data);
10756 	hwrm_req_drop(bp, req);
10757 	return rc;
10758 }
10759 
bnxt_hwrm_port_phy_write(struct bnxt * bp,u16 phy_addr,u16 reg,u16 val)10760 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
10761 				    u16 val)
10762 {
10763 	struct hwrm_port_phy_mdio_write_input *req;
10764 	int rc;
10765 
10766 	if (bp->hwrm_spec_code < 0x10a00)
10767 		return -EOPNOTSUPP;
10768 
10769 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
10770 	if (rc)
10771 		return rc;
10772 
10773 	req->port_id = cpu_to_le16(bp->pf.port_id);
10774 	req->phy_addr = phy_addr;
10775 	req->reg_addr = cpu_to_le16(reg & 0x1f);
10776 	if (mdio_phy_id_is_c45(phy_addr)) {
10777 		req->cl45_mdio = 1;
10778 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
10779 		req->dev_addr = mdio_phy_id_devad(phy_addr);
10780 		req->reg_addr = cpu_to_le16(reg);
10781 	}
10782 	req->reg_data = cpu_to_le16(val);
10783 
10784 	return hwrm_req_send(bp, req);
10785 }
10786 
10787 /* rtnl_lock held */
bnxt_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)10788 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10789 {
10790 	struct mii_ioctl_data *mdio = if_mii(ifr);
10791 	struct bnxt *bp = netdev_priv(dev);
10792 	int rc;
10793 
10794 	switch (cmd) {
10795 	case SIOCGMIIPHY:
10796 		mdio->phy_id = bp->link_info.phy_addr;
10797 
10798 		fallthrough;
10799 	case SIOCGMIIREG: {
10800 		u16 mii_regval = 0;
10801 
10802 		if (!netif_running(dev))
10803 			return -EAGAIN;
10804 
10805 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
10806 					     &mii_regval);
10807 		mdio->val_out = mii_regval;
10808 		return rc;
10809 	}
10810 
10811 	case SIOCSMIIREG:
10812 		if (!netif_running(dev))
10813 			return -EAGAIN;
10814 
10815 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
10816 						mdio->val_in);
10817 
10818 	case SIOCSHWTSTAMP:
10819 		return bnxt_hwtstamp_set(dev, ifr);
10820 
10821 	case SIOCGHWTSTAMP:
10822 		return bnxt_hwtstamp_get(dev, ifr);
10823 
10824 	default:
10825 		/* do nothing */
10826 		break;
10827 	}
10828 	return -EOPNOTSUPP;
10829 }
10830 
bnxt_get_ring_stats(struct bnxt * bp,struct rtnl_link_stats64 * stats)10831 static void bnxt_get_ring_stats(struct bnxt *bp,
10832 				struct rtnl_link_stats64 *stats)
10833 {
10834 	int i;
10835 
10836 	for (i = 0; i < bp->cp_nr_rings; i++) {
10837 		struct bnxt_napi *bnapi = bp->bnapi[i];
10838 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
10839 		u64 *sw = cpr->stats.sw_stats;
10840 
10841 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
10842 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
10843 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
10844 
10845 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
10846 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
10847 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
10848 
10849 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
10850 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
10851 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
10852 
10853 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
10854 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
10855 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
10856 
10857 		stats->rx_missed_errors +=
10858 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
10859 
10860 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
10861 
10862 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
10863 
10864 		stats->rx_dropped +=
10865 			cpr->sw_stats.rx.rx_netpoll_discards +
10866 			cpr->sw_stats.rx.rx_oom_discards;
10867 	}
10868 }
10869 
bnxt_add_prev_stats(struct bnxt * bp,struct rtnl_link_stats64 * stats)10870 static void bnxt_add_prev_stats(struct bnxt *bp,
10871 				struct rtnl_link_stats64 *stats)
10872 {
10873 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
10874 
10875 	stats->rx_packets += prev_stats->rx_packets;
10876 	stats->tx_packets += prev_stats->tx_packets;
10877 	stats->rx_bytes += prev_stats->rx_bytes;
10878 	stats->tx_bytes += prev_stats->tx_bytes;
10879 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
10880 	stats->multicast += prev_stats->multicast;
10881 	stats->rx_dropped += prev_stats->rx_dropped;
10882 	stats->tx_dropped += prev_stats->tx_dropped;
10883 }
10884 
10885 static void
bnxt_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)10886 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
10887 {
10888 	struct bnxt *bp = netdev_priv(dev);
10889 
10890 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
10891 	/* Make sure bnxt_close_nic() sees that we are reading stats before
10892 	 * we check the BNXT_STATE_OPEN flag.
10893 	 */
10894 	smp_mb__after_atomic();
10895 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10896 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
10897 		*stats = bp->net_stats_prev;
10898 		return;
10899 	}
10900 
10901 	bnxt_get_ring_stats(bp, stats);
10902 	bnxt_add_prev_stats(bp, stats);
10903 
10904 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10905 		u64 *rx = bp->port_stats.sw_stats;
10906 		u64 *tx = bp->port_stats.sw_stats +
10907 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10908 
10909 		stats->rx_crc_errors =
10910 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
10911 		stats->rx_frame_errors =
10912 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
10913 		stats->rx_length_errors =
10914 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
10915 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
10916 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
10917 		stats->rx_errors =
10918 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
10919 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
10920 		stats->collisions =
10921 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
10922 		stats->tx_fifo_errors =
10923 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
10924 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
10925 	}
10926 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
10927 }
10928 
bnxt_mc_list_updated(struct bnxt * bp,u32 * rx_mask)10929 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
10930 {
10931 	struct net_device *dev = bp->dev;
10932 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10933 	struct netdev_hw_addr *ha;
10934 	u8 *haddr;
10935 	int mc_count = 0;
10936 	bool update = false;
10937 	int off = 0;
10938 
10939 	netdev_for_each_mc_addr(ha, dev) {
10940 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
10941 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10942 			vnic->mc_list_count = 0;
10943 			return false;
10944 		}
10945 		haddr = ha->addr;
10946 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
10947 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
10948 			update = true;
10949 		}
10950 		off += ETH_ALEN;
10951 		mc_count++;
10952 	}
10953 	if (mc_count)
10954 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
10955 
10956 	if (mc_count != vnic->mc_list_count) {
10957 		vnic->mc_list_count = mc_count;
10958 		update = true;
10959 	}
10960 	return update;
10961 }
10962 
bnxt_uc_list_updated(struct bnxt * bp)10963 static bool bnxt_uc_list_updated(struct bnxt *bp)
10964 {
10965 	struct net_device *dev = bp->dev;
10966 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10967 	struct netdev_hw_addr *ha;
10968 	int off = 0;
10969 
10970 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
10971 		return true;
10972 
10973 	netdev_for_each_uc_addr(ha, dev) {
10974 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
10975 			return true;
10976 
10977 		off += ETH_ALEN;
10978 	}
10979 	return false;
10980 }
10981 
bnxt_set_rx_mode(struct net_device * dev)10982 static void bnxt_set_rx_mode(struct net_device *dev)
10983 {
10984 	struct bnxt *bp = netdev_priv(dev);
10985 	struct bnxt_vnic_info *vnic;
10986 	bool mc_update = false;
10987 	bool uc_update;
10988 	u32 mask;
10989 
10990 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
10991 		return;
10992 
10993 	vnic = &bp->vnic_info[0];
10994 	mask = vnic->rx_mask;
10995 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
10996 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
10997 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
10998 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
10999 
11000 	if (dev->flags & IFF_PROMISC)
11001 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11002 
11003 	uc_update = bnxt_uc_list_updated(bp);
11004 
11005 	if (dev->flags & IFF_BROADCAST)
11006 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11007 	if (dev->flags & IFF_ALLMULTI) {
11008 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11009 		vnic->mc_list_count = 0;
11010 	} else if (dev->flags & IFF_MULTICAST) {
11011 		mc_update = bnxt_mc_list_updated(bp, &mask);
11012 	}
11013 
11014 	if (mask != vnic->rx_mask || uc_update || mc_update) {
11015 		vnic->rx_mask = mask;
11016 
11017 		set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
11018 		bnxt_queue_sp_work(bp);
11019 	}
11020 }
11021 
bnxt_cfg_rx_mode(struct bnxt * bp)11022 static int bnxt_cfg_rx_mode(struct bnxt *bp)
11023 {
11024 	struct net_device *dev = bp->dev;
11025 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
11026 	struct hwrm_cfa_l2_filter_free_input *req;
11027 	struct netdev_hw_addr *ha;
11028 	int i, off = 0, rc;
11029 	bool uc_update;
11030 
11031 	netif_addr_lock_bh(dev);
11032 	uc_update = bnxt_uc_list_updated(bp);
11033 	netif_addr_unlock_bh(dev);
11034 
11035 	if (!uc_update)
11036 		goto skip_uc;
11037 
11038 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
11039 	if (rc)
11040 		return rc;
11041 	hwrm_req_hold(bp, req);
11042 	for (i = 1; i < vnic->uc_filter_count; i++) {
11043 		req->l2_filter_id = vnic->fw_l2_filter_id[i];
11044 
11045 		rc = hwrm_req_send(bp, req);
11046 	}
11047 	hwrm_req_drop(bp, req);
11048 
11049 	vnic->uc_filter_count = 1;
11050 
11051 	netif_addr_lock_bh(dev);
11052 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
11053 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11054 	} else {
11055 		netdev_for_each_uc_addr(ha, dev) {
11056 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
11057 			off += ETH_ALEN;
11058 			vnic->uc_filter_count++;
11059 		}
11060 	}
11061 	netif_addr_unlock_bh(dev);
11062 
11063 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
11064 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
11065 		if (rc) {
11066 			if (BNXT_VF(bp) && rc == -ENODEV) {
11067 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
11068 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
11069 				else
11070 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
11071 				rc = 0;
11072 			} else {
11073 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
11074 			}
11075 			vnic->uc_filter_count = i;
11076 			return rc;
11077 		}
11078 	}
11079 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
11080 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
11081 
11082 skip_uc:
11083 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
11084 	    !bnxt_promisc_ok(bp))
11085 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11086 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
11087 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
11088 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
11089 			    rc);
11090 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
11091 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11092 		vnic->mc_list_count = 0;
11093 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
11094 	}
11095 	if (rc)
11096 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
11097 			   rc);
11098 
11099 	return rc;
11100 }
11101 
bnxt_can_reserve_rings(struct bnxt * bp)11102 static bool bnxt_can_reserve_rings(struct bnxt *bp)
11103 {
11104 #ifdef CONFIG_BNXT_SRIOV
11105 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
11106 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11107 
11108 		/* No minimum rings were provisioned by the PF.  Don't
11109 		 * reserve rings by default when device is down.
11110 		 */
11111 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
11112 			return true;
11113 
11114 		if (!netif_running(bp->dev))
11115 			return false;
11116 	}
11117 #endif
11118 	return true;
11119 }
11120 
11121 /* If the chip and firmware supports RFS */
bnxt_rfs_supported(struct bnxt * bp)11122 static bool bnxt_rfs_supported(struct bnxt *bp)
11123 {
11124 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
11125 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
11126 			return true;
11127 		return false;
11128 	}
11129 	/* 212 firmware is broken for aRFS */
11130 	if (BNXT_FW_MAJ(bp) == 212)
11131 		return false;
11132 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
11133 		return true;
11134 	if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
11135 		return true;
11136 	return false;
11137 }
11138 
11139 /* If runtime conditions support RFS */
bnxt_rfs_capable(struct bnxt * bp)11140 static bool bnxt_rfs_capable(struct bnxt *bp)
11141 {
11142 #ifdef CONFIG_RFS_ACCEL
11143 	int vnics, max_vnics, max_rss_ctxs;
11144 
11145 	if (bp->flags & BNXT_FLAG_CHIP_P5)
11146 		return bnxt_rfs_supported(bp);
11147 	if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
11148 		return false;
11149 
11150 	vnics = 1 + bp->rx_nr_rings;
11151 	max_vnics = bnxt_get_max_func_vnics(bp);
11152 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
11153 
11154 	/* RSS contexts not a limiting factor */
11155 	if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
11156 		max_rss_ctxs = max_vnics;
11157 	if (vnics > max_vnics || vnics > max_rss_ctxs) {
11158 		if (bp->rx_nr_rings > 1)
11159 			netdev_warn(bp->dev,
11160 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
11161 				    min(max_rss_ctxs - 1, max_vnics - 1));
11162 		return false;
11163 	}
11164 
11165 	if (!BNXT_NEW_RM(bp))
11166 		return true;
11167 
11168 	if (vnics == bp->hw_resc.resv_vnics)
11169 		return true;
11170 
11171 	bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, vnics);
11172 	if (vnics <= bp->hw_resc.resv_vnics)
11173 		return true;
11174 
11175 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
11176 	bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, 1);
11177 	return false;
11178 #else
11179 	return false;
11180 #endif
11181 }
11182 
bnxt_fix_features(struct net_device * dev,netdev_features_t features)11183 static netdev_features_t bnxt_fix_features(struct net_device *dev,
11184 					   netdev_features_t features)
11185 {
11186 	struct bnxt *bp = netdev_priv(dev);
11187 	netdev_features_t vlan_features;
11188 
11189 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
11190 		features &= ~NETIF_F_NTUPLE;
11191 
11192 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
11193 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
11194 
11195 	if (!(features & NETIF_F_GRO))
11196 		features &= ~NETIF_F_GRO_HW;
11197 
11198 	if (features & NETIF_F_GRO_HW)
11199 		features &= ~NETIF_F_LRO;
11200 
11201 	/* Both CTAG and STAG VLAN accelaration on the RX side have to be
11202 	 * turned on or off together.
11203 	 */
11204 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
11205 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
11206 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
11207 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
11208 		else if (vlan_features)
11209 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
11210 	}
11211 #ifdef CONFIG_BNXT_SRIOV
11212 	if (BNXT_VF(bp) && bp->vf.vlan)
11213 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
11214 #endif
11215 	return features;
11216 }
11217 
bnxt_set_features(struct net_device * dev,netdev_features_t features)11218 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
11219 {
11220 	struct bnxt *bp = netdev_priv(dev);
11221 	u32 flags = bp->flags;
11222 	u32 changes;
11223 	int rc = 0;
11224 	bool re_init = false;
11225 	bool update_tpa = false;
11226 
11227 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
11228 	if (features & NETIF_F_GRO_HW)
11229 		flags |= BNXT_FLAG_GRO;
11230 	else if (features & NETIF_F_LRO)
11231 		flags |= BNXT_FLAG_LRO;
11232 
11233 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
11234 		flags &= ~BNXT_FLAG_TPA;
11235 
11236 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
11237 		flags |= BNXT_FLAG_STRIP_VLAN;
11238 
11239 	if (features & NETIF_F_NTUPLE)
11240 		flags |= BNXT_FLAG_RFS;
11241 
11242 	changes = flags ^ bp->flags;
11243 	if (changes & BNXT_FLAG_TPA) {
11244 		update_tpa = true;
11245 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
11246 		    (flags & BNXT_FLAG_TPA) == 0 ||
11247 		    (bp->flags & BNXT_FLAG_CHIP_P5))
11248 			re_init = true;
11249 	}
11250 
11251 	if (changes & ~BNXT_FLAG_TPA)
11252 		re_init = true;
11253 
11254 	if (flags != bp->flags) {
11255 		u32 old_flags = bp->flags;
11256 
11257 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
11258 			bp->flags = flags;
11259 			if (update_tpa)
11260 				bnxt_set_ring_params(bp);
11261 			return rc;
11262 		}
11263 
11264 		if (re_init) {
11265 			bnxt_close_nic(bp, false, false);
11266 			bp->flags = flags;
11267 			if (update_tpa)
11268 				bnxt_set_ring_params(bp);
11269 
11270 			return bnxt_open_nic(bp, false, false);
11271 		}
11272 		if (update_tpa) {
11273 			bp->flags = flags;
11274 			rc = bnxt_set_tpa(bp,
11275 					  (flags & BNXT_FLAG_TPA) ?
11276 					  true : false);
11277 			if (rc)
11278 				bp->flags = old_flags;
11279 		}
11280 	}
11281 	return rc;
11282 }
11283 
bnxt_exthdr_check(struct bnxt * bp,struct sk_buff * skb,int nw_off,u8 ** nextp)11284 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
11285 			      u8 **nextp)
11286 {
11287 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
11288 	int hdr_count = 0;
11289 	u8 *nexthdr;
11290 	int start;
11291 
11292 	/* Check that there are at most 2 IPv6 extension headers, no
11293 	 * fragment header, and each is <= 64 bytes.
11294 	 */
11295 	start = nw_off + sizeof(*ip6h);
11296 	nexthdr = &ip6h->nexthdr;
11297 	while (ipv6_ext_hdr(*nexthdr)) {
11298 		struct ipv6_opt_hdr *hp;
11299 		int hdrlen;
11300 
11301 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
11302 		    *nexthdr == NEXTHDR_FRAGMENT)
11303 			return false;
11304 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
11305 					  skb_headlen(skb), NULL);
11306 		if (!hp)
11307 			return false;
11308 		if (*nexthdr == NEXTHDR_AUTH)
11309 			hdrlen = ipv6_authlen(hp);
11310 		else
11311 			hdrlen = ipv6_optlen(hp);
11312 
11313 		if (hdrlen > 64)
11314 			return false;
11315 		nexthdr = &hp->nexthdr;
11316 		start += hdrlen;
11317 		hdr_count++;
11318 	}
11319 	if (nextp) {
11320 		/* Caller will check inner protocol */
11321 		if (skb->encapsulation) {
11322 			*nextp = nexthdr;
11323 			return true;
11324 		}
11325 		*nextp = NULL;
11326 	}
11327 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
11328 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
11329 }
11330 
11331 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
bnxt_udp_tunl_check(struct bnxt * bp,struct sk_buff * skb)11332 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
11333 {
11334 	struct udphdr *uh = udp_hdr(skb);
11335 	__be16 udp_port = uh->dest;
11336 
11337 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port)
11338 		return false;
11339 	if (skb->inner_protocol_type == ENCAP_TYPE_ETHER) {
11340 		struct ethhdr *eh = inner_eth_hdr(skb);
11341 
11342 		switch (eh->h_proto) {
11343 		case htons(ETH_P_IP):
11344 			return true;
11345 		case htons(ETH_P_IPV6):
11346 			return bnxt_exthdr_check(bp, skb,
11347 						 skb_inner_network_offset(skb),
11348 						 NULL);
11349 		}
11350 	}
11351 	return false;
11352 }
11353 
bnxt_tunl_check(struct bnxt * bp,struct sk_buff * skb,u8 l4_proto)11354 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
11355 {
11356 	switch (l4_proto) {
11357 	case IPPROTO_UDP:
11358 		return bnxt_udp_tunl_check(bp, skb);
11359 	case IPPROTO_IPIP:
11360 		return true;
11361 	case IPPROTO_GRE: {
11362 		switch (skb->inner_protocol) {
11363 		default:
11364 			return false;
11365 		case htons(ETH_P_IP):
11366 			return true;
11367 		case htons(ETH_P_IPV6):
11368 			fallthrough;
11369 		}
11370 	}
11371 	case IPPROTO_IPV6:
11372 		/* Check ext headers of inner ipv6 */
11373 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
11374 					 NULL);
11375 	}
11376 	return false;
11377 }
11378 
bnxt_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)11379 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
11380 					     struct net_device *dev,
11381 					     netdev_features_t features)
11382 {
11383 	struct bnxt *bp = netdev_priv(dev);
11384 	u8 *l4_proto;
11385 
11386 	features = vlan_features_check(skb, features);
11387 	switch (vlan_get_protocol(skb)) {
11388 	case htons(ETH_P_IP):
11389 		if (!skb->encapsulation)
11390 			return features;
11391 		l4_proto = &ip_hdr(skb)->protocol;
11392 		if (bnxt_tunl_check(bp, skb, *l4_proto))
11393 			return features;
11394 		break;
11395 	case htons(ETH_P_IPV6):
11396 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
11397 				       &l4_proto))
11398 			break;
11399 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
11400 			return features;
11401 		break;
11402 	}
11403 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
11404 }
11405 
bnxt_dbg_hwrm_rd_reg(struct bnxt * bp,u32 reg_off,u16 num_words,u32 * reg_buf)11406 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
11407 			 u32 *reg_buf)
11408 {
11409 	struct hwrm_dbg_read_direct_output *resp;
11410 	struct hwrm_dbg_read_direct_input *req;
11411 	__le32 *dbg_reg_buf;
11412 	dma_addr_t mapping;
11413 	int rc, i;
11414 
11415 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
11416 	if (rc)
11417 		return rc;
11418 
11419 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
11420 					 &mapping);
11421 	if (!dbg_reg_buf) {
11422 		rc = -ENOMEM;
11423 		goto dbg_rd_reg_exit;
11424 	}
11425 
11426 	req->host_dest_addr = cpu_to_le64(mapping);
11427 
11428 	resp = hwrm_req_hold(bp, req);
11429 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
11430 	req->read_len32 = cpu_to_le32(num_words);
11431 
11432 	rc = hwrm_req_send(bp, req);
11433 	if (rc || resp->error_code) {
11434 		rc = -EIO;
11435 		goto dbg_rd_reg_exit;
11436 	}
11437 	for (i = 0; i < num_words; i++)
11438 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
11439 
11440 dbg_rd_reg_exit:
11441 	hwrm_req_drop(bp, req);
11442 	return rc;
11443 }
11444 
bnxt_dbg_hwrm_ring_info_get(struct bnxt * bp,u8 ring_type,u32 ring_id,u32 * prod,u32 * cons)11445 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
11446 				       u32 ring_id, u32 *prod, u32 *cons)
11447 {
11448 	struct hwrm_dbg_ring_info_get_output *resp;
11449 	struct hwrm_dbg_ring_info_get_input *req;
11450 	int rc;
11451 
11452 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
11453 	if (rc)
11454 		return rc;
11455 
11456 	req->ring_type = ring_type;
11457 	req->fw_ring_id = cpu_to_le32(ring_id);
11458 	resp = hwrm_req_hold(bp, req);
11459 	rc = hwrm_req_send(bp, req);
11460 	if (!rc) {
11461 		*prod = le32_to_cpu(resp->producer_index);
11462 		*cons = le32_to_cpu(resp->consumer_index);
11463 	}
11464 	hwrm_req_drop(bp, req);
11465 	return rc;
11466 }
11467 
bnxt_dump_tx_sw_state(struct bnxt_napi * bnapi)11468 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
11469 {
11470 	struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
11471 	int i = bnapi->index;
11472 
11473 	if (!txr)
11474 		return;
11475 
11476 	netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
11477 		    i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
11478 		    txr->tx_cons);
11479 }
11480 
bnxt_dump_rx_sw_state(struct bnxt_napi * bnapi)11481 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
11482 {
11483 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
11484 	int i = bnapi->index;
11485 
11486 	if (!rxr)
11487 		return;
11488 
11489 	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",
11490 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
11491 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
11492 		    rxr->rx_sw_agg_prod);
11493 }
11494 
bnxt_dump_cp_sw_state(struct bnxt_napi * bnapi)11495 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
11496 {
11497 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
11498 	int i = bnapi->index;
11499 
11500 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
11501 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
11502 }
11503 
bnxt_dbg_dump_states(struct bnxt * bp)11504 static void bnxt_dbg_dump_states(struct bnxt *bp)
11505 {
11506 	int i;
11507 	struct bnxt_napi *bnapi;
11508 
11509 	for (i = 0; i < bp->cp_nr_rings; i++) {
11510 		bnapi = bp->bnapi[i];
11511 		if (netif_msg_drv(bp)) {
11512 			bnxt_dump_tx_sw_state(bnapi);
11513 			bnxt_dump_rx_sw_state(bnapi);
11514 			bnxt_dump_cp_sw_state(bnapi);
11515 		}
11516 	}
11517 }
11518 
bnxt_hwrm_rx_ring_reset(struct bnxt * bp,int ring_nr)11519 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
11520 {
11521 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
11522 	struct hwrm_ring_reset_input *req;
11523 	struct bnxt_napi *bnapi = rxr->bnapi;
11524 	struct bnxt_cp_ring_info *cpr;
11525 	u16 cp_ring_id;
11526 	int rc;
11527 
11528 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
11529 	if (rc)
11530 		return rc;
11531 
11532 	cpr = &bnapi->cp_ring;
11533 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
11534 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
11535 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
11536 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
11537 	return hwrm_req_send_silent(bp, req);
11538 }
11539 
bnxt_reset_task(struct bnxt * bp,bool silent)11540 static void bnxt_reset_task(struct bnxt *bp, bool silent)
11541 {
11542 	if (!silent)
11543 		bnxt_dbg_dump_states(bp);
11544 	if (netif_running(bp->dev)) {
11545 		int rc;
11546 
11547 		if (silent) {
11548 			bnxt_close_nic(bp, false, false);
11549 			bnxt_open_nic(bp, false, false);
11550 		} else {
11551 			bnxt_ulp_stop(bp);
11552 			bnxt_close_nic(bp, true, false);
11553 			rc = bnxt_open_nic(bp, true, false);
11554 			bnxt_ulp_start(bp, rc);
11555 		}
11556 	}
11557 }
11558 
bnxt_tx_timeout(struct net_device * dev,unsigned int txqueue)11559 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
11560 {
11561 	struct bnxt *bp = netdev_priv(dev);
11562 
11563 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
11564 	set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
11565 	bnxt_queue_sp_work(bp);
11566 }
11567 
bnxt_fw_health_check(struct bnxt * bp)11568 static void bnxt_fw_health_check(struct bnxt *bp)
11569 {
11570 	struct bnxt_fw_health *fw_health = bp->fw_health;
11571 	u32 val;
11572 
11573 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11574 		return;
11575 
11576 	/* Make sure it is enabled before checking the tmr_counter. */
11577 	smp_rmb();
11578 	if (fw_health->tmr_counter) {
11579 		fw_health->tmr_counter--;
11580 		return;
11581 	}
11582 
11583 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
11584 	if (val == fw_health->last_fw_heartbeat) {
11585 		fw_health->arrests++;
11586 		goto fw_reset;
11587 	}
11588 
11589 	fw_health->last_fw_heartbeat = val;
11590 
11591 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
11592 	if (val != fw_health->last_fw_reset_cnt) {
11593 		fw_health->discoveries++;
11594 		goto fw_reset;
11595 	}
11596 
11597 	fw_health->tmr_counter = fw_health->tmr_multiplier;
11598 	return;
11599 
11600 fw_reset:
11601 	set_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event);
11602 	bnxt_queue_sp_work(bp);
11603 }
11604 
bnxt_timer(struct timer_list * t)11605 static void bnxt_timer(struct timer_list *t)
11606 {
11607 	struct bnxt *bp = from_timer(bp, t, timer);
11608 	struct net_device *dev = bp->dev;
11609 
11610 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
11611 		return;
11612 
11613 	if (atomic_read(&bp->intr_sem) != 0)
11614 		goto bnxt_restart_timer;
11615 
11616 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
11617 		bnxt_fw_health_check(bp);
11618 
11619 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks) {
11620 		set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
11621 		bnxt_queue_sp_work(bp);
11622 	}
11623 
11624 	if (bnxt_tc_flower_enabled(bp)) {
11625 		set_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event);
11626 		bnxt_queue_sp_work(bp);
11627 	}
11628 
11629 #ifdef CONFIG_RFS_ACCEL
11630 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count) {
11631 		set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
11632 		bnxt_queue_sp_work(bp);
11633 	}
11634 #endif /*CONFIG_RFS_ACCEL*/
11635 
11636 	if (bp->link_info.phy_retry) {
11637 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
11638 			bp->link_info.phy_retry = false;
11639 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
11640 		} else {
11641 			set_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event);
11642 			bnxt_queue_sp_work(bp);
11643 		}
11644 	}
11645 
11646 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) {
11647 		set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
11648 		bnxt_queue_sp_work(bp);
11649 	}
11650 
11651 	if ((bp->flags & BNXT_FLAG_CHIP_P5) && !bp->chip_rev &&
11652 	    netif_carrier_ok(dev)) {
11653 		set_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event);
11654 		bnxt_queue_sp_work(bp);
11655 	}
11656 bnxt_restart_timer:
11657 	mod_timer(&bp->timer, jiffies + bp->current_interval);
11658 }
11659 
bnxt_rtnl_lock_sp(struct bnxt * bp)11660 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
11661 {
11662 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
11663 	 * set.  If the device is being closed, bnxt_close() may be holding
11664 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
11665 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
11666 	 */
11667 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11668 	rtnl_lock();
11669 }
11670 
bnxt_rtnl_unlock_sp(struct bnxt * bp)11671 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
11672 {
11673 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11674 	rtnl_unlock();
11675 }
11676 
11677 /* Only called from bnxt_sp_task() */
bnxt_reset(struct bnxt * bp,bool silent)11678 static void bnxt_reset(struct bnxt *bp, bool silent)
11679 {
11680 	bnxt_rtnl_lock_sp(bp);
11681 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
11682 		bnxt_reset_task(bp, silent);
11683 	bnxt_rtnl_unlock_sp(bp);
11684 }
11685 
11686 /* Only called from bnxt_sp_task() */
bnxt_rx_ring_reset(struct bnxt * bp)11687 static void bnxt_rx_ring_reset(struct bnxt *bp)
11688 {
11689 	int i;
11690 
11691 	bnxt_rtnl_lock_sp(bp);
11692 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
11693 		bnxt_rtnl_unlock_sp(bp);
11694 		return;
11695 	}
11696 	/* Disable and flush TPA before resetting the RX ring */
11697 	if (bp->flags & BNXT_FLAG_TPA)
11698 		bnxt_set_tpa(bp, false);
11699 	for (i = 0; i < bp->rx_nr_rings; i++) {
11700 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
11701 		struct bnxt_cp_ring_info *cpr;
11702 		int rc;
11703 
11704 		if (!rxr->bnapi->in_reset)
11705 			continue;
11706 
11707 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
11708 		if (rc) {
11709 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
11710 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
11711 			else
11712 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
11713 					    rc);
11714 			bnxt_reset_task(bp, true);
11715 			break;
11716 		}
11717 		bnxt_free_one_rx_ring_skbs(bp, i);
11718 		rxr->rx_prod = 0;
11719 		rxr->rx_agg_prod = 0;
11720 		rxr->rx_sw_agg_prod = 0;
11721 		rxr->rx_next_cons = 0;
11722 		rxr->bnapi->in_reset = false;
11723 		bnxt_alloc_one_rx_ring(bp, i);
11724 		cpr = &rxr->bnapi->cp_ring;
11725 		cpr->sw_stats.rx.rx_resets++;
11726 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
11727 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
11728 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
11729 	}
11730 	if (bp->flags & BNXT_FLAG_TPA)
11731 		bnxt_set_tpa(bp, true);
11732 	bnxt_rtnl_unlock_sp(bp);
11733 }
11734 
bnxt_fw_reset_close(struct bnxt * bp)11735 static void bnxt_fw_reset_close(struct bnxt *bp)
11736 {
11737 	bnxt_ulp_stop(bp);
11738 	/* When firmware is in fatal state, quiesce device and disable
11739 	 * bus master to prevent any potential bad DMAs before freeing
11740 	 * kernel memory.
11741 	 */
11742 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
11743 		u16 val = 0;
11744 
11745 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
11746 		if (val == 0xffff)
11747 			bp->fw_reset_min_dsecs = 0;
11748 		bnxt_tx_disable(bp);
11749 		bnxt_disable_napi(bp);
11750 		bnxt_disable_int_sync(bp);
11751 		bnxt_free_irq(bp);
11752 		bnxt_clear_int_mode(bp);
11753 		pci_disable_device(bp->pdev);
11754 	}
11755 	__bnxt_close_nic(bp, true, false);
11756 	bnxt_vf_reps_free(bp);
11757 	bnxt_clear_int_mode(bp);
11758 	bnxt_hwrm_func_drv_unrgtr(bp);
11759 	if (pci_is_enabled(bp->pdev))
11760 		pci_disable_device(bp->pdev);
11761 	bnxt_free_ctx_mem(bp);
11762 	kfree(bp->ctx);
11763 	bp->ctx = NULL;
11764 }
11765 
is_bnxt_fw_ok(struct bnxt * bp)11766 static bool is_bnxt_fw_ok(struct bnxt *bp)
11767 {
11768 	struct bnxt_fw_health *fw_health = bp->fw_health;
11769 	bool no_heartbeat = false, has_reset = false;
11770 	u32 val;
11771 
11772 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
11773 	if (val == fw_health->last_fw_heartbeat)
11774 		no_heartbeat = true;
11775 
11776 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
11777 	if (val != fw_health->last_fw_reset_cnt)
11778 		has_reset = true;
11779 
11780 	if (!no_heartbeat && has_reset)
11781 		return true;
11782 
11783 	return false;
11784 }
11785 
11786 /* rtnl_lock is acquired before calling this function */
bnxt_force_fw_reset(struct bnxt * bp)11787 static void bnxt_force_fw_reset(struct bnxt *bp)
11788 {
11789 	struct bnxt_fw_health *fw_health = bp->fw_health;
11790 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
11791 	u32 wait_dsecs;
11792 
11793 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
11794 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11795 		return;
11796 
11797 	if (ptp) {
11798 		spin_lock_bh(&ptp->ptp_lock);
11799 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11800 		spin_unlock_bh(&ptp->ptp_lock);
11801 	} else {
11802 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11803 	}
11804 	bnxt_fw_reset_close(bp);
11805 	wait_dsecs = fw_health->master_func_wait_dsecs;
11806 	if (fw_health->primary) {
11807 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
11808 			wait_dsecs = 0;
11809 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
11810 	} else {
11811 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
11812 		wait_dsecs = fw_health->normal_func_wait_dsecs;
11813 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11814 	}
11815 
11816 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
11817 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
11818 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
11819 }
11820 
bnxt_fw_exception(struct bnxt * bp)11821 void bnxt_fw_exception(struct bnxt *bp)
11822 {
11823 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
11824 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
11825 	bnxt_rtnl_lock_sp(bp);
11826 	bnxt_force_fw_reset(bp);
11827 	bnxt_rtnl_unlock_sp(bp);
11828 }
11829 
11830 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
11831  * < 0 on error.
11832  */
bnxt_get_registered_vfs(struct bnxt * bp)11833 static int bnxt_get_registered_vfs(struct bnxt *bp)
11834 {
11835 #ifdef CONFIG_BNXT_SRIOV
11836 	int rc;
11837 
11838 	if (!BNXT_PF(bp))
11839 		return 0;
11840 
11841 	rc = bnxt_hwrm_func_qcfg(bp);
11842 	if (rc) {
11843 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
11844 		return rc;
11845 	}
11846 	if (bp->pf.registered_vfs)
11847 		return bp->pf.registered_vfs;
11848 	if (bp->sriov_cfg)
11849 		return 1;
11850 #endif
11851 	return 0;
11852 }
11853 
bnxt_fw_reset(struct bnxt * bp)11854 void bnxt_fw_reset(struct bnxt *bp)
11855 {
11856 	bnxt_rtnl_lock_sp(bp);
11857 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
11858 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
11859 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
11860 		int n = 0, tmo;
11861 
11862 		if (ptp) {
11863 			spin_lock_bh(&ptp->ptp_lock);
11864 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11865 			spin_unlock_bh(&ptp->ptp_lock);
11866 		} else {
11867 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11868 		}
11869 		if (bp->pf.active_vfs &&
11870 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
11871 			n = bnxt_get_registered_vfs(bp);
11872 		if (n < 0) {
11873 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
11874 				   n);
11875 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11876 			dev_close(bp->dev);
11877 			goto fw_reset_exit;
11878 		} else if (n > 0) {
11879 			u16 vf_tmo_dsecs = n * 10;
11880 
11881 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
11882 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
11883 			bp->fw_reset_state =
11884 				BNXT_FW_RESET_STATE_POLL_VF;
11885 			bnxt_queue_fw_reset_work(bp, HZ / 10);
11886 			goto fw_reset_exit;
11887 		}
11888 		bnxt_fw_reset_close(bp);
11889 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
11890 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
11891 			tmo = HZ / 10;
11892 		} else {
11893 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11894 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
11895 		}
11896 		bnxt_queue_fw_reset_work(bp, tmo);
11897 	}
11898 fw_reset_exit:
11899 	bnxt_rtnl_unlock_sp(bp);
11900 }
11901 
bnxt_chk_missed_irq(struct bnxt * bp)11902 static void bnxt_chk_missed_irq(struct bnxt *bp)
11903 {
11904 	int i;
11905 
11906 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
11907 		return;
11908 
11909 	for (i = 0; i < bp->cp_nr_rings; i++) {
11910 		struct bnxt_napi *bnapi = bp->bnapi[i];
11911 		struct bnxt_cp_ring_info *cpr;
11912 		u32 fw_ring_id;
11913 		int j;
11914 
11915 		if (!bnapi)
11916 			continue;
11917 
11918 		cpr = &bnapi->cp_ring;
11919 		for (j = 0; j < 2; j++) {
11920 			struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
11921 			u32 val[2];
11922 
11923 			if (!cpr2 || cpr2->has_more_work ||
11924 			    !bnxt_has_work(bp, cpr2))
11925 				continue;
11926 
11927 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
11928 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
11929 				continue;
11930 			}
11931 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
11932 			bnxt_dbg_hwrm_ring_info_get(bp,
11933 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
11934 				fw_ring_id, &val[0], &val[1]);
11935 			cpr->sw_stats.cmn.missed_irqs++;
11936 		}
11937 	}
11938 }
11939 
11940 static void bnxt_cfg_ntp_filters(struct bnxt *);
11941 
bnxt_init_ethtool_link_settings(struct bnxt * bp)11942 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
11943 {
11944 	struct bnxt_link_info *link_info = &bp->link_info;
11945 
11946 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
11947 		link_info->autoneg = BNXT_AUTONEG_SPEED;
11948 		if (bp->hwrm_spec_code >= 0x10201) {
11949 			if (link_info->auto_pause_setting &
11950 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
11951 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
11952 		} else {
11953 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
11954 		}
11955 		link_info->advertising = link_info->auto_link_speeds;
11956 		link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
11957 	} else {
11958 		link_info->req_link_speed = link_info->force_link_speed;
11959 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
11960 		if (link_info->force_pam4_link_speed) {
11961 			link_info->req_link_speed =
11962 				link_info->force_pam4_link_speed;
11963 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
11964 		}
11965 		link_info->req_duplex = link_info->duplex_setting;
11966 	}
11967 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
11968 		link_info->req_flow_ctrl =
11969 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
11970 	else
11971 		link_info->req_flow_ctrl = link_info->force_pause_setting;
11972 }
11973 
bnxt_fw_echo_reply(struct bnxt * bp)11974 static void bnxt_fw_echo_reply(struct bnxt *bp)
11975 {
11976 	struct bnxt_fw_health *fw_health = bp->fw_health;
11977 	struct hwrm_func_echo_response_input *req;
11978 	int rc;
11979 
11980 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
11981 	if (rc)
11982 		return;
11983 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
11984 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
11985 	hwrm_req_send(bp, req);
11986 }
11987 
bnxt_sp_task(struct work_struct * work)11988 static void bnxt_sp_task(struct work_struct *work)
11989 {
11990 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
11991 
11992 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11993 	smp_mb__after_atomic();
11994 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
11995 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11996 		return;
11997 	}
11998 
11999 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
12000 		bnxt_cfg_rx_mode(bp);
12001 
12002 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
12003 		bnxt_cfg_ntp_filters(bp);
12004 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
12005 		bnxt_hwrm_exec_fwd_req(bp);
12006 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
12007 		bnxt_hwrm_port_qstats(bp, 0);
12008 		bnxt_hwrm_port_qstats_ext(bp, 0);
12009 		bnxt_accumulate_all_stats(bp);
12010 	}
12011 
12012 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
12013 		int rc;
12014 
12015 		mutex_lock(&bp->link_lock);
12016 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
12017 				       &bp->sp_event))
12018 			bnxt_hwrm_phy_qcaps(bp);
12019 
12020 		rc = bnxt_update_link(bp, true);
12021 		if (rc)
12022 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
12023 				   rc);
12024 
12025 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
12026 				       &bp->sp_event))
12027 			bnxt_init_ethtool_link_settings(bp);
12028 		mutex_unlock(&bp->link_lock);
12029 	}
12030 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
12031 		int rc;
12032 
12033 		mutex_lock(&bp->link_lock);
12034 		rc = bnxt_update_phy_setting(bp);
12035 		mutex_unlock(&bp->link_lock);
12036 		if (rc) {
12037 			netdev_warn(bp->dev, "update phy settings retry failed\n");
12038 		} else {
12039 			bp->link_info.phy_retry = false;
12040 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
12041 		}
12042 	}
12043 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
12044 		mutex_lock(&bp->link_lock);
12045 		bnxt_get_port_module_status(bp);
12046 		mutex_unlock(&bp->link_lock);
12047 	}
12048 
12049 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
12050 		bnxt_tc_flow_stats_work(bp);
12051 
12052 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
12053 		bnxt_chk_missed_irq(bp);
12054 
12055 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
12056 		bnxt_fw_echo_reply(bp);
12057 
12058 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
12059 	 * must be the last functions to be called before exiting.
12060 	 */
12061 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
12062 		bnxt_reset(bp, false);
12063 
12064 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
12065 		bnxt_reset(bp, true);
12066 
12067 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
12068 		bnxt_rx_ring_reset(bp);
12069 
12070 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
12071 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
12072 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
12073 			bnxt_devlink_health_fw_report(bp);
12074 		else
12075 			bnxt_fw_reset(bp);
12076 	}
12077 
12078 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
12079 		if (!is_bnxt_fw_ok(bp))
12080 			bnxt_devlink_health_fw_report(bp);
12081 	}
12082 
12083 	smp_mb__before_atomic();
12084 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
12085 }
12086 
12087 /* Under rtnl_lock */
bnxt_check_rings(struct bnxt * bp,int tx,int rx,bool sh,int tcs,int tx_xdp)12088 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
12089 		     int tx_xdp)
12090 {
12091 	int max_rx, max_tx, tx_sets = 1;
12092 	int tx_rings_needed, stats;
12093 	int rx_rings = rx;
12094 	int cp, vnics, rc;
12095 
12096 	if (tcs)
12097 		tx_sets = tcs;
12098 
12099 	rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
12100 	if (rc)
12101 		return rc;
12102 
12103 	if (max_rx < rx)
12104 		return -ENOMEM;
12105 
12106 	tx_rings_needed = tx * tx_sets + tx_xdp;
12107 	if (max_tx < tx_rings_needed)
12108 		return -ENOMEM;
12109 
12110 	vnics = 1;
12111 	if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
12112 		vnics += rx_rings;
12113 
12114 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
12115 		rx_rings <<= 1;
12116 	cp = sh ? max_t(int, tx_rings_needed, rx) : tx_rings_needed + rx;
12117 	stats = cp;
12118 	if (BNXT_NEW_RM(bp)) {
12119 		cp += bnxt_get_ulp_msix_num(bp);
12120 		stats += bnxt_get_ulp_stat_ctxs(bp);
12121 	}
12122 	return bnxt_hwrm_check_rings(bp, tx_rings_needed, rx_rings, rx, cp,
12123 				     stats, vnics);
12124 }
12125 
bnxt_unmap_bars(struct bnxt * bp,struct pci_dev * pdev)12126 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
12127 {
12128 	if (bp->bar2) {
12129 		pci_iounmap(pdev, bp->bar2);
12130 		bp->bar2 = NULL;
12131 	}
12132 
12133 	if (bp->bar1) {
12134 		pci_iounmap(pdev, bp->bar1);
12135 		bp->bar1 = NULL;
12136 	}
12137 
12138 	if (bp->bar0) {
12139 		pci_iounmap(pdev, bp->bar0);
12140 		bp->bar0 = NULL;
12141 	}
12142 }
12143 
bnxt_cleanup_pci(struct bnxt * bp)12144 static void bnxt_cleanup_pci(struct bnxt *bp)
12145 {
12146 	bnxt_unmap_bars(bp, bp->pdev);
12147 	pci_release_regions(bp->pdev);
12148 	if (pci_is_enabled(bp->pdev))
12149 		pci_disable_device(bp->pdev);
12150 }
12151 
bnxt_init_dflt_coal(struct bnxt * bp)12152 static void bnxt_init_dflt_coal(struct bnxt *bp)
12153 {
12154 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
12155 	struct bnxt_coal *coal;
12156 	u16 flags = 0;
12157 
12158 	if (coal_cap->cmpl_params &
12159 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
12160 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
12161 
12162 	/* Tick values in micro seconds.
12163 	 * 1 coal_buf x bufs_per_record = 1 completion record.
12164 	 */
12165 	coal = &bp->rx_coal;
12166 	coal->coal_ticks = 10;
12167 	coal->coal_bufs = 30;
12168 	coal->coal_ticks_irq = 1;
12169 	coal->coal_bufs_irq = 2;
12170 	coal->idle_thresh = 50;
12171 	coal->bufs_per_record = 2;
12172 	coal->budget = 64;		/* NAPI budget */
12173 	coal->flags = flags;
12174 
12175 	coal = &bp->tx_coal;
12176 	coal->coal_ticks = 28;
12177 	coal->coal_bufs = 30;
12178 	coal->coal_ticks_irq = 2;
12179 	coal->coal_bufs_irq = 2;
12180 	coal->bufs_per_record = 1;
12181 	coal->flags = flags;
12182 
12183 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
12184 }
12185 
bnxt_fw_init_one_p1(struct bnxt * bp)12186 static int bnxt_fw_init_one_p1(struct bnxt *bp)
12187 {
12188 	int rc;
12189 
12190 	bp->fw_cap = 0;
12191 	rc = bnxt_hwrm_ver_get(bp);
12192 	bnxt_try_map_fw_health_reg(bp);
12193 	if (rc) {
12194 		rc = bnxt_try_recover_fw(bp);
12195 		if (rc)
12196 			return rc;
12197 		rc = bnxt_hwrm_ver_get(bp);
12198 		if (rc)
12199 			return rc;
12200 	}
12201 
12202 	bnxt_nvm_cfg_ver_get(bp);
12203 
12204 	rc = bnxt_hwrm_func_reset(bp);
12205 	if (rc)
12206 		return -ENODEV;
12207 
12208 	bnxt_hwrm_fw_set_time(bp);
12209 	return 0;
12210 }
12211 
bnxt_fw_init_one_p2(struct bnxt * bp)12212 static int bnxt_fw_init_one_p2(struct bnxt *bp)
12213 {
12214 	int rc;
12215 
12216 	/* Get the MAX capabilities for this function */
12217 	rc = bnxt_hwrm_func_qcaps(bp);
12218 	if (rc) {
12219 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
12220 			   rc);
12221 		return -ENODEV;
12222 	}
12223 
12224 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
12225 	if (rc)
12226 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
12227 			    rc);
12228 
12229 	if (bnxt_alloc_fw_health(bp)) {
12230 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
12231 	} else {
12232 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
12233 		if (rc)
12234 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
12235 				    rc);
12236 	}
12237 
12238 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
12239 	if (rc)
12240 		return -ENODEV;
12241 
12242 	bnxt_hwrm_func_qcfg(bp);
12243 	bnxt_hwrm_vnic_qcaps(bp);
12244 	bnxt_hwrm_port_led_qcaps(bp);
12245 	bnxt_ethtool_init(bp);
12246 	bnxt_dcb_init(bp);
12247 	return 0;
12248 }
12249 
bnxt_set_dflt_rss_hash_type(struct bnxt * bp)12250 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
12251 {
12252 	bp->flags &= ~BNXT_FLAG_UDP_RSS_CAP;
12253 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
12254 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
12255 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
12256 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
12257 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
12258 		bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
12259 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
12260 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
12261 	}
12262 }
12263 
bnxt_set_dflt_rfs(struct bnxt * bp)12264 static void bnxt_set_dflt_rfs(struct bnxt *bp)
12265 {
12266 	struct net_device *dev = bp->dev;
12267 
12268 	dev->hw_features &= ~NETIF_F_NTUPLE;
12269 	dev->features &= ~NETIF_F_NTUPLE;
12270 	bp->flags &= ~BNXT_FLAG_RFS;
12271 	if (bnxt_rfs_supported(bp)) {
12272 		dev->hw_features |= NETIF_F_NTUPLE;
12273 		if (bnxt_rfs_capable(bp)) {
12274 			bp->flags |= BNXT_FLAG_RFS;
12275 			dev->features |= NETIF_F_NTUPLE;
12276 		}
12277 	}
12278 }
12279 
bnxt_fw_init_one_p3(struct bnxt * bp)12280 static void bnxt_fw_init_one_p3(struct bnxt *bp)
12281 {
12282 	struct pci_dev *pdev = bp->pdev;
12283 
12284 	bnxt_set_dflt_rss_hash_type(bp);
12285 	bnxt_set_dflt_rfs(bp);
12286 
12287 	bnxt_get_wol_settings(bp);
12288 	if (bp->flags & BNXT_FLAG_WOL_CAP)
12289 		device_set_wakeup_enable(&pdev->dev, bp->wol);
12290 	else
12291 		device_set_wakeup_capable(&pdev->dev, false);
12292 
12293 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
12294 	bnxt_hwrm_coal_params_qcaps(bp);
12295 }
12296 
12297 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
12298 
bnxt_fw_init_one(struct bnxt * bp)12299 int bnxt_fw_init_one(struct bnxt *bp)
12300 {
12301 	int rc;
12302 
12303 	rc = bnxt_fw_init_one_p1(bp);
12304 	if (rc) {
12305 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
12306 		return rc;
12307 	}
12308 	rc = bnxt_fw_init_one_p2(bp);
12309 	if (rc) {
12310 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
12311 		return rc;
12312 	}
12313 	rc = bnxt_probe_phy(bp, false);
12314 	if (rc)
12315 		return rc;
12316 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
12317 	if (rc)
12318 		return rc;
12319 
12320 	bnxt_fw_init_one_p3(bp);
12321 	return 0;
12322 }
12323 
bnxt_fw_reset_writel(struct bnxt * bp,int reg_idx)12324 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
12325 {
12326 	struct bnxt_fw_health *fw_health = bp->fw_health;
12327 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
12328 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
12329 	u32 reg_type, reg_off, delay_msecs;
12330 
12331 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
12332 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
12333 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
12334 	switch (reg_type) {
12335 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
12336 		pci_write_config_dword(bp->pdev, reg_off, val);
12337 		break;
12338 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
12339 		writel(reg_off & BNXT_GRC_BASE_MASK,
12340 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
12341 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
12342 		fallthrough;
12343 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
12344 		writel(val, bp->bar0 + reg_off);
12345 		break;
12346 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
12347 		writel(val, bp->bar1 + reg_off);
12348 		break;
12349 	}
12350 	if (delay_msecs) {
12351 		pci_read_config_dword(bp->pdev, 0, &val);
12352 		msleep(delay_msecs);
12353 	}
12354 }
12355 
bnxt_hwrm_reset_permitted(struct bnxt * bp)12356 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
12357 {
12358 	struct hwrm_func_qcfg_output *resp;
12359 	struct hwrm_func_qcfg_input *req;
12360 	bool result = true; /* firmware will enforce if unknown */
12361 
12362 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
12363 		return result;
12364 
12365 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
12366 		return result;
12367 
12368 	req->fid = cpu_to_le16(0xffff);
12369 	resp = hwrm_req_hold(bp, req);
12370 	if (!hwrm_req_send(bp, req))
12371 		result = !!(le16_to_cpu(resp->flags) &
12372 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
12373 	hwrm_req_drop(bp, req);
12374 	return result;
12375 }
12376 
bnxt_reset_all(struct bnxt * bp)12377 static void bnxt_reset_all(struct bnxt *bp)
12378 {
12379 	struct bnxt_fw_health *fw_health = bp->fw_health;
12380 	int i, rc;
12381 
12382 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
12383 		bnxt_fw_reset_via_optee(bp);
12384 		bp->fw_reset_timestamp = jiffies;
12385 		return;
12386 	}
12387 
12388 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
12389 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
12390 			bnxt_fw_reset_writel(bp, i);
12391 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
12392 		struct hwrm_fw_reset_input *req;
12393 
12394 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
12395 		if (!rc) {
12396 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
12397 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
12398 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
12399 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
12400 			rc = hwrm_req_send(bp, req);
12401 		}
12402 		if (rc != -ENODEV)
12403 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
12404 	}
12405 	bp->fw_reset_timestamp = jiffies;
12406 }
12407 
bnxt_fw_reset_timeout(struct bnxt * bp)12408 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
12409 {
12410 	return time_after(jiffies, bp->fw_reset_timestamp +
12411 			  (bp->fw_reset_max_dsecs * HZ / 10));
12412 }
12413 
bnxt_fw_reset_abort(struct bnxt * bp,int rc)12414 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
12415 {
12416 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12417 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF) {
12418 		bnxt_ulp_start(bp, rc);
12419 		bnxt_dl_health_fw_status_update(bp, false);
12420 	}
12421 	bp->fw_reset_state = 0;
12422 	dev_close(bp->dev);
12423 }
12424 
bnxt_fw_reset_task(struct work_struct * work)12425 static void bnxt_fw_reset_task(struct work_struct *work)
12426 {
12427 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
12428 	int rc = 0;
12429 
12430 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12431 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
12432 		return;
12433 	}
12434 
12435 	switch (bp->fw_reset_state) {
12436 	case BNXT_FW_RESET_STATE_POLL_VF: {
12437 		int n = bnxt_get_registered_vfs(bp);
12438 		int tmo;
12439 
12440 		if (n < 0) {
12441 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
12442 				   n, jiffies_to_msecs(jiffies -
12443 				   bp->fw_reset_timestamp));
12444 			goto fw_reset_abort;
12445 		} else if (n > 0) {
12446 			if (bnxt_fw_reset_timeout(bp)) {
12447 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12448 				bp->fw_reset_state = 0;
12449 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
12450 					   n);
12451 				return;
12452 			}
12453 			bnxt_queue_fw_reset_work(bp, HZ / 10);
12454 			return;
12455 		}
12456 		bp->fw_reset_timestamp = jiffies;
12457 		rtnl_lock();
12458 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12459 			bnxt_fw_reset_abort(bp, rc);
12460 			rtnl_unlock();
12461 			return;
12462 		}
12463 		bnxt_fw_reset_close(bp);
12464 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
12465 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
12466 			tmo = HZ / 10;
12467 		} else {
12468 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
12469 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
12470 		}
12471 		rtnl_unlock();
12472 		bnxt_queue_fw_reset_work(bp, tmo);
12473 		return;
12474 	}
12475 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
12476 		u32 val;
12477 
12478 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12479 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
12480 		    !bnxt_fw_reset_timeout(bp)) {
12481 			bnxt_queue_fw_reset_work(bp, HZ / 5);
12482 			return;
12483 		}
12484 
12485 		if (!bp->fw_health->primary) {
12486 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
12487 
12488 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
12489 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
12490 			return;
12491 		}
12492 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
12493 	}
12494 		fallthrough;
12495 	case BNXT_FW_RESET_STATE_RESET_FW:
12496 		bnxt_reset_all(bp);
12497 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
12498 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
12499 		return;
12500 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
12501 		bnxt_inv_fw_health_reg(bp);
12502 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
12503 		    !bp->fw_reset_min_dsecs) {
12504 			u16 val;
12505 
12506 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
12507 			if (val == 0xffff) {
12508 				if (bnxt_fw_reset_timeout(bp)) {
12509 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
12510 					rc = -ETIMEDOUT;
12511 					goto fw_reset_abort;
12512 				}
12513 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
12514 				return;
12515 			}
12516 		}
12517 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
12518 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
12519 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
12520 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
12521 			bnxt_dl_remote_reload(bp);
12522 		if (pci_enable_device(bp->pdev)) {
12523 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
12524 			rc = -ENODEV;
12525 			goto fw_reset_abort;
12526 		}
12527 		pci_set_master(bp->pdev);
12528 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
12529 		fallthrough;
12530 	case BNXT_FW_RESET_STATE_POLL_FW:
12531 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
12532 		rc = bnxt_hwrm_poll(bp);
12533 		if (rc) {
12534 			if (bnxt_fw_reset_timeout(bp)) {
12535 				netdev_err(bp->dev, "Firmware reset aborted\n");
12536 				goto fw_reset_abort_status;
12537 			}
12538 			bnxt_queue_fw_reset_work(bp, HZ / 5);
12539 			return;
12540 		}
12541 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
12542 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
12543 		fallthrough;
12544 	case BNXT_FW_RESET_STATE_OPENING:
12545 		while (!rtnl_trylock()) {
12546 			bnxt_queue_fw_reset_work(bp, HZ / 10);
12547 			return;
12548 		}
12549 		rc = bnxt_open(bp->dev);
12550 		if (rc) {
12551 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
12552 			bnxt_fw_reset_abort(bp, rc);
12553 			rtnl_unlock();
12554 			return;
12555 		}
12556 
12557 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
12558 		    bp->fw_health->enabled) {
12559 			bp->fw_health->last_fw_reset_cnt =
12560 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
12561 		}
12562 		bp->fw_reset_state = 0;
12563 		/* Make sure fw_reset_state is 0 before clearing the flag */
12564 		smp_mb__before_atomic();
12565 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12566 		bnxt_ulp_start(bp, 0);
12567 		bnxt_reenable_sriov(bp);
12568 		bnxt_vf_reps_alloc(bp);
12569 		bnxt_vf_reps_open(bp);
12570 		bnxt_ptp_reapply_pps(bp);
12571 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
12572 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
12573 			bnxt_dl_health_fw_recovery_done(bp);
12574 			bnxt_dl_health_fw_status_update(bp, true);
12575 		}
12576 		rtnl_unlock();
12577 		break;
12578 	}
12579 	return;
12580 
12581 fw_reset_abort_status:
12582 	if (bp->fw_health->status_reliable ||
12583 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
12584 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12585 
12586 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
12587 	}
12588 fw_reset_abort:
12589 	rtnl_lock();
12590 	bnxt_fw_reset_abort(bp, rc);
12591 	rtnl_unlock();
12592 }
12593 
bnxt_init_board(struct pci_dev * pdev,struct net_device * dev)12594 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
12595 {
12596 	int rc;
12597 	struct bnxt *bp = netdev_priv(dev);
12598 
12599 	SET_NETDEV_DEV(dev, &pdev->dev);
12600 
12601 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
12602 	rc = pci_enable_device(pdev);
12603 	if (rc) {
12604 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
12605 		goto init_err;
12606 	}
12607 
12608 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
12609 		dev_err(&pdev->dev,
12610 			"Cannot find PCI device base address, aborting\n");
12611 		rc = -ENODEV;
12612 		goto init_err_disable;
12613 	}
12614 
12615 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
12616 	if (rc) {
12617 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
12618 		goto init_err_disable;
12619 	}
12620 
12621 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
12622 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
12623 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
12624 		rc = -EIO;
12625 		goto init_err_release;
12626 	}
12627 
12628 	pci_set_master(pdev);
12629 
12630 	bp->dev = dev;
12631 	bp->pdev = pdev;
12632 
12633 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
12634 	 * determines the BAR size.
12635 	 */
12636 	bp->bar0 = pci_ioremap_bar(pdev, 0);
12637 	if (!bp->bar0) {
12638 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
12639 		rc = -ENOMEM;
12640 		goto init_err_release;
12641 	}
12642 
12643 	bp->bar2 = pci_ioremap_bar(pdev, 4);
12644 	if (!bp->bar2) {
12645 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
12646 		rc = -ENOMEM;
12647 		goto init_err_release;
12648 	}
12649 
12650 	pci_enable_pcie_error_reporting(pdev);
12651 
12652 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
12653 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
12654 
12655 	spin_lock_init(&bp->ntp_fltr_lock);
12656 #if BITS_PER_LONG == 32
12657 	spin_lock_init(&bp->db_lock);
12658 #endif
12659 
12660 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
12661 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
12662 
12663 	timer_setup(&bp->timer, bnxt_timer, 0);
12664 	bp->current_interval = BNXT_TIMER_INTERVAL;
12665 
12666 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
12667 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
12668 
12669 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12670 	return 0;
12671 
12672 init_err_release:
12673 	bnxt_unmap_bars(bp, pdev);
12674 	pci_release_regions(pdev);
12675 
12676 init_err_disable:
12677 	pci_disable_device(pdev);
12678 
12679 init_err:
12680 	return rc;
12681 }
12682 
12683 /* rtnl_lock held */
bnxt_change_mac_addr(struct net_device * dev,void * p)12684 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
12685 {
12686 	struct sockaddr *addr = p;
12687 	struct bnxt *bp = netdev_priv(dev);
12688 	int rc = 0;
12689 
12690 	if (!is_valid_ether_addr(addr->sa_data))
12691 		return -EADDRNOTAVAIL;
12692 
12693 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
12694 		return 0;
12695 
12696 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
12697 	if (rc)
12698 		return rc;
12699 
12700 	eth_hw_addr_set(dev, addr->sa_data);
12701 	if (netif_running(dev)) {
12702 		bnxt_close_nic(bp, false, false);
12703 		rc = bnxt_open_nic(bp, false, false);
12704 	}
12705 
12706 	return rc;
12707 }
12708 
12709 /* rtnl_lock held */
bnxt_change_mtu(struct net_device * dev,int new_mtu)12710 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
12711 {
12712 	struct bnxt *bp = netdev_priv(dev);
12713 
12714 	if (netif_running(dev))
12715 		bnxt_close_nic(bp, true, false);
12716 
12717 	dev->mtu = new_mtu;
12718 	bnxt_set_ring_params(bp);
12719 
12720 	if (netif_running(dev))
12721 		return bnxt_open_nic(bp, true, false);
12722 
12723 	return 0;
12724 }
12725 
bnxt_setup_mq_tc(struct net_device * dev,u8 tc)12726 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
12727 {
12728 	struct bnxt *bp = netdev_priv(dev);
12729 	bool sh = false;
12730 	int rc;
12731 
12732 	if (tc > bp->max_tc) {
12733 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
12734 			   tc, bp->max_tc);
12735 		return -EINVAL;
12736 	}
12737 
12738 	if (netdev_get_num_tc(dev) == tc)
12739 		return 0;
12740 
12741 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
12742 		sh = true;
12743 
12744 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
12745 			      sh, tc, bp->tx_nr_rings_xdp);
12746 	if (rc)
12747 		return rc;
12748 
12749 	/* Needs to close the device and do hw resource re-allocations */
12750 	if (netif_running(bp->dev))
12751 		bnxt_close_nic(bp, true, false);
12752 
12753 	if (tc) {
12754 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
12755 		netdev_set_num_tc(dev, tc);
12756 	} else {
12757 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
12758 		netdev_reset_tc(dev);
12759 	}
12760 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
12761 	bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
12762 			       bp->tx_nr_rings + bp->rx_nr_rings;
12763 
12764 	if (netif_running(bp->dev))
12765 		return bnxt_open_nic(bp, true, false);
12766 
12767 	return 0;
12768 }
12769 
bnxt_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)12770 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
12771 				  void *cb_priv)
12772 {
12773 	struct bnxt *bp = cb_priv;
12774 
12775 	if (!bnxt_tc_flower_enabled(bp) ||
12776 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
12777 		return -EOPNOTSUPP;
12778 
12779 	switch (type) {
12780 	case TC_SETUP_CLSFLOWER:
12781 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
12782 	default:
12783 		return -EOPNOTSUPP;
12784 	}
12785 }
12786 
12787 LIST_HEAD(bnxt_block_cb_list);
12788 
bnxt_setup_tc(struct net_device * dev,enum tc_setup_type type,void * type_data)12789 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
12790 			 void *type_data)
12791 {
12792 	struct bnxt *bp = netdev_priv(dev);
12793 
12794 	switch (type) {
12795 	case TC_SETUP_BLOCK:
12796 		return flow_block_cb_setup_simple(type_data,
12797 						  &bnxt_block_cb_list,
12798 						  bnxt_setup_tc_block_cb,
12799 						  bp, bp, true);
12800 	case TC_SETUP_QDISC_MQPRIO: {
12801 		struct tc_mqprio_qopt *mqprio = type_data;
12802 
12803 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
12804 
12805 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
12806 	}
12807 	default:
12808 		return -EOPNOTSUPP;
12809 	}
12810 }
12811 
12812 #ifdef CONFIG_RFS_ACCEL
bnxt_fltr_match(struct bnxt_ntuple_filter * f1,struct bnxt_ntuple_filter * f2)12813 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
12814 			    struct bnxt_ntuple_filter *f2)
12815 {
12816 	struct flow_keys *keys1 = &f1->fkeys;
12817 	struct flow_keys *keys2 = &f2->fkeys;
12818 
12819 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
12820 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
12821 		return false;
12822 
12823 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
12824 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
12825 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst)
12826 			return false;
12827 	} else {
12828 		if (memcmp(&keys1->addrs.v6addrs.src, &keys2->addrs.v6addrs.src,
12829 			   sizeof(keys1->addrs.v6addrs.src)) ||
12830 		    memcmp(&keys1->addrs.v6addrs.dst, &keys2->addrs.v6addrs.dst,
12831 			   sizeof(keys1->addrs.v6addrs.dst)))
12832 			return false;
12833 	}
12834 
12835 	if (keys1->ports.ports == keys2->ports.ports &&
12836 	    keys1->control.flags == keys2->control.flags &&
12837 	    ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
12838 	    ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
12839 		return true;
12840 
12841 	return false;
12842 }
12843 
bnxt_rx_flow_steer(struct net_device * dev,const struct sk_buff * skb,u16 rxq_index,u32 flow_id)12844 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
12845 			      u16 rxq_index, u32 flow_id)
12846 {
12847 	struct bnxt *bp = netdev_priv(dev);
12848 	struct bnxt_ntuple_filter *fltr, *new_fltr;
12849 	struct flow_keys *fkeys;
12850 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
12851 	int rc = 0, idx, bit_id, l2_idx = 0;
12852 	struct hlist_head *head;
12853 	u32 flags;
12854 
12855 	if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
12856 		struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
12857 		int off = 0, j;
12858 
12859 		netif_addr_lock_bh(dev);
12860 		for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
12861 			if (ether_addr_equal(eth->h_dest,
12862 					     vnic->uc_list + off)) {
12863 				l2_idx = j + 1;
12864 				break;
12865 			}
12866 		}
12867 		netif_addr_unlock_bh(dev);
12868 		if (!l2_idx)
12869 			return -EINVAL;
12870 	}
12871 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
12872 	if (!new_fltr)
12873 		return -ENOMEM;
12874 
12875 	fkeys = &new_fltr->fkeys;
12876 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
12877 		rc = -EPROTONOSUPPORT;
12878 		goto err_free;
12879 	}
12880 
12881 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
12882 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
12883 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
12884 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
12885 		rc = -EPROTONOSUPPORT;
12886 		goto err_free;
12887 	}
12888 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
12889 	    bp->hwrm_spec_code < 0x10601) {
12890 		rc = -EPROTONOSUPPORT;
12891 		goto err_free;
12892 	}
12893 	flags = fkeys->control.flags;
12894 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
12895 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
12896 		rc = -EPROTONOSUPPORT;
12897 		goto err_free;
12898 	}
12899 
12900 	memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
12901 	memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
12902 
12903 	idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
12904 	head = &bp->ntp_fltr_hash_tbl[idx];
12905 	rcu_read_lock();
12906 	hlist_for_each_entry_rcu(fltr, head, hash) {
12907 		if (bnxt_fltr_match(fltr, new_fltr)) {
12908 			rc = fltr->sw_id;
12909 			rcu_read_unlock();
12910 			goto err_free;
12911 		}
12912 	}
12913 	rcu_read_unlock();
12914 
12915 	spin_lock_bh(&bp->ntp_fltr_lock);
12916 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
12917 					 BNXT_NTP_FLTR_MAX_FLTR, 0);
12918 	if (bit_id < 0) {
12919 		spin_unlock_bh(&bp->ntp_fltr_lock);
12920 		rc = -ENOMEM;
12921 		goto err_free;
12922 	}
12923 
12924 	new_fltr->sw_id = (u16)bit_id;
12925 	new_fltr->flow_id = flow_id;
12926 	new_fltr->l2_fltr_idx = l2_idx;
12927 	new_fltr->rxq = rxq_index;
12928 	hlist_add_head_rcu(&new_fltr->hash, head);
12929 	bp->ntp_fltr_count++;
12930 	spin_unlock_bh(&bp->ntp_fltr_lock);
12931 
12932 	set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
12933 	bnxt_queue_sp_work(bp);
12934 
12935 	return new_fltr->sw_id;
12936 
12937 err_free:
12938 	kfree(new_fltr);
12939 	return rc;
12940 }
12941 
bnxt_cfg_ntp_filters(struct bnxt * bp)12942 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
12943 {
12944 	int i;
12945 
12946 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
12947 		struct hlist_head *head;
12948 		struct hlist_node *tmp;
12949 		struct bnxt_ntuple_filter *fltr;
12950 		int rc;
12951 
12952 		head = &bp->ntp_fltr_hash_tbl[i];
12953 		hlist_for_each_entry_safe(fltr, tmp, head, hash) {
12954 			bool del = false;
12955 
12956 			if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
12957 				if (rps_may_expire_flow(bp->dev, fltr->rxq,
12958 							fltr->flow_id,
12959 							fltr->sw_id)) {
12960 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
12961 									 fltr);
12962 					del = true;
12963 				}
12964 			} else {
12965 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
12966 								       fltr);
12967 				if (rc)
12968 					del = true;
12969 				else
12970 					set_bit(BNXT_FLTR_VALID, &fltr->state);
12971 			}
12972 
12973 			if (del) {
12974 				spin_lock_bh(&bp->ntp_fltr_lock);
12975 				hlist_del_rcu(&fltr->hash);
12976 				bp->ntp_fltr_count--;
12977 				spin_unlock_bh(&bp->ntp_fltr_lock);
12978 				synchronize_rcu();
12979 				clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
12980 				kfree(fltr);
12981 			}
12982 		}
12983 	}
12984 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
12985 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
12986 }
12987 
12988 #else
12989 
bnxt_cfg_ntp_filters(struct bnxt * bp)12990 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
12991 {
12992 }
12993 
12994 #endif /* CONFIG_RFS_ACCEL */
12995 
bnxt_udp_tunnel_sync(struct net_device * netdev,unsigned int table)12996 static int bnxt_udp_tunnel_sync(struct net_device *netdev, unsigned int table)
12997 {
12998 	struct bnxt *bp = netdev_priv(netdev);
12999 	struct udp_tunnel_info ti;
13000 	unsigned int cmd;
13001 
13002 	udp_tunnel_nic_get_port(netdev, table, 0, &ti);
13003 	if (ti.type == UDP_TUNNEL_TYPE_VXLAN)
13004 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
13005 	else
13006 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
13007 
13008 	if (ti.port)
13009 		return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti.port, cmd);
13010 
13011 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
13012 }
13013 
13014 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
13015 	.sync_table	= bnxt_udp_tunnel_sync,
13016 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
13017 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
13018 	.tables		= {
13019 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
13020 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
13021 	},
13022 };
13023 
bnxt_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 filter_mask,int nlflags)13024 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13025 			       struct net_device *dev, u32 filter_mask,
13026 			       int nlflags)
13027 {
13028 	struct bnxt *bp = netdev_priv(dev);
13029 
13030 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
13031 				       nlflags, filter_mask, NULL);
13032 }
13033 
bnxt_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags,struct netlink_ext_ack * extack)13034 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
13035 			       u16 flags, struct netlink_ext_ack *extack)
13036 {
13037 	struct bnxt *bp = netdev_priv(dev);
13038 	struct nlattr *attr, *br_spec;
13039 	int rem, rc = 0;
13040 
13041 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
13042 		return -EOPNOTSUPP;
13043 
13044 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13045 	if (!br_spec)
13046 		return -EINVAL;
13047 
13048 	nla_for_each_nested(attr, br_spec, rem) {
13049 		u16 mode;
13050 
13051 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13052 			continue;
13053 
13054 		if (nla_len(attr) < sizeof(mode))
13055 			return -EINVAL;
13056 
13057 		mode = nla_get_u16(attr);
13058 		if (mode == bp->br_mode)
13059 			break;
13060 
13061 		rc = bnxt_hwrm_set_br_mode(bp, mode);
13062 		if (!rc)
13063 			bp->br_mode = mode;
13064 		break;
13065 	}
13066 	return rc;
13067 }
13068 
bnxt_get_port_parent_id(struct net_device * dev,struct netdev_phys_item_id * ppid)13069 int bnxt_get_port_parent_id(struct net_device *dev,
13070 			    struct netdev_phys_item_id *ppid)
13071 {
13072 	struct bnxt *bp = netdev_priv(dev);
13073 
13074 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
13075 		return -EOPNOTSUPP;
13076 
13077 	/* The PF and it's VF-reps only support the switchdev framework */
13078 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
13079 		return -EOPNOTSUPP;
13080 
13081 	ppid->id_len = sizeof(bp->dsn);
13082 	memcpy(ppid->id, bp->dsn, ppid->id_len);
13083 
13084 	return 0;
13085 }
13086 
bnxt_get_devlink_port(struct net_device * dev)13087 static struct devlink_port *bnxt_get_devlink_port(struct net_device *dev)
13088 {
13089 	struct bnxt *bp = netdev_priv(dev);
13090 
13091 	return &bp->dl_port;
13092 }
13093 
13094 static const struct net_device_ops bnxt_netdev_ops = {
13095 	.ndo_open		= bnxt_open,
13096 	.ndo_start_xmit		= bnxt_start_xmit,
13097 	.ndo_stop		= bnxt_close,
13098 	.ndo_get_stats64	= bnxt_get_stats64,
13099 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
13100 	.ndo_eth_ioctl		= bnxt_ioctl,
13101 	.ndo_validate_addr	= eth_validate_addr,
13102 	.ndo_set_mac_address	= bnxt_change_mac_addr,
13103 	.ndo_change_mtu		= bnxt_change_mtu,
13104 	.ndo_fix_features	= bnxt_fix_features,
13105 	.ndo_set_features	= bnxt_set_features,
13106 	.ndo_features_check	= bnxt_features_check,
13107 	.ndo_tx_timeout		= bnxt_tx_timeout,
13108 #ifdef CONFIG_BNXT_SRIOV
13109 	.ndo_get_vf_config	= bnxt_get_vf_config,
13110 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
13111 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
13112 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
13113 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
13114 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
13115 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
13116 #endif
13117 	.ndo_setup_tc           = bnxt_setup_tc,
13118 #ifdef CONFIG_RFS_ACCEL
13119 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
13120 #endif
13121 	.ndo_bpf		= bnxt_xdp,
13122 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
13123 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
13124 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
13125 	.ndo_get_devlink_port	= bnxt_get_devlink_port,
13126 };
13127 
bnxt_remove_one(struct pci_dev * pdev)13128 static void bnxt_remove_one(struct pci_dev *pdev)
13129 {
13130 	struct net_device *dev = pci_get_drvdata(pdev);
13131 	struct bnxt *bp = netdev_priv(dev);
13132 
13133 	if (BNXT_PF(bp))
13134 		bnxt_sriov_disable(bp);
13135 
13136 	if (BNXT_PF(bp))
13137 		devlink_port_type_clear(&bp->dl_port);
13138 
13139 	bnxt_ptp_clear(bp);
13140 	pci_disable_pcie_error_reporting(pdev);
13141 	unregister_netdev(dev);
13142 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13143 	/* Flush any pending tasks */
13144 	cancel_work_sync(&bp->sp_task);
13145 	cancel_delayed_work_sync(&bp->fw_reset_task);
13146 	bp->sp_event = 0;
13147 
13148 	bnxt_dl_fw_reporters_destroy(bp);
13149 	bnxt_dl_unregister(bp);
13150 	bnxt_shutdown_tc(bp);
13151 
13152 	bnxt_clear_int_mode(bp);
13153 	bnxt_hwrm_func_drv_unrgtr(bp);
13154 	bnxt_free_hwrm_resources(bp);
13155 	bnxt_ethtool_free(bp);
13156 	bnxt_dcb_free(bp);
13157 	kfree(bp->edev);
13158 	bp->edev = NULL;
13159 	kfree(bp->ptp_cfg);
13160 	bp->ptp_cfg = NULL;
13161 	kfree(bp->fw_health);
13162 	bp->fw_health = NULL;
13163 	bnxt_cleanup_pci(bp);
13164 	bnxt_free_ctx_mem(bp);
13165 	kfree(bp->ctx);
13166 	bp->ctx = NULL;
13167 	kfree(bp->rss_indir_tbl);
13168 	bp->rss_indir_tbl = NULL;
13169 	bnxt_free_port_stats(bp);
13170 	free_netdev(dev);
13171 }
13172 
bnxt_probe_phy(struct bnxt * bp,bool fw_dflt)13173 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
13174 {
13175 	int rc = 0;
13176 	struct bnxt_link_info *link_info = &bp->link_info;
13177 
13178 	bp->phy_flags = 0;
13179 	rc = bnxt_hwrm_phy_qcaps(bp);
13180 	if (rc) {
13181 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
13182 			   rc);
13183 		return rc;
13184 	}
13185 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
13186 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
13187 	else
13188 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
13189 	if (!fw_dflt)
13190 		return 0;
13191 
13192 	mutex_lock(&bp->link_lock);
13193 	rc = bnxt_update_link(bp, false);
13194 	if (rc) {
13195 		mutex_unlock(&bp->link_lock);
13196 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
13197 			   rc);
13198 		return rc;
13199 	}
13200 
13201 	/* Older firmware does not have supported_auto_speeds, so assume
13202 	 * that all supported speeds can be autonegotiated.
13203 	 */
13204 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
13205 		link_info->support_auto_speeds = link_info->support_speeds;
13206 
13207 	bnxt_init_ethtool_link_settings(bp);
13208 	mutex_unlock(&bp->link_lock);
13209 	return 0;
13210 }
13211 
bnxt_get_max_irq(struct pci_dev * pdev)13212 static int bnxt_get_max_irq(struct pci_dev *pdev)
13213 {
13214 	u16 ctrl;
13215 
13216 	if (!pdev->msix_cap)
13217 		return 1;
13218 
13219 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
13220 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
13221 }
13222 
_bnxt_get_max_rings(struct bnxt * bp,int * max_rx,int * max_tx,int * max_cp)13223 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
13224 				int *max_cp)
13225 {
13226 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13227 	int max_ring_grps = 0, max_irq;
13228 
13229 	*max_tx = hw_resc->max_tx_rings;
13230 	*max_rx = hw_resc->max_rx_rings;
13231 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
13232 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
13233 			bnxt_get_ulp_msix_num(bp),
13234 			hw_resc->max_stat_ctxs - bnxt_get_ulp_stat_ctxs(bp));
13235 	if (!(bp->flags & BNXT_FLAG_CHIP_P5))
13236 		*max_cp = min_t(int, *max_cp, max_irq);
13237 	max_ring_grps = hw_resc->max_hw_ring_grps;
13238 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
13239 		*max_cp -= 1;
13240 		*max_rx -= 2;
13241 	}
13242 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
13243 		*max_rx >>= 1;
13244 	if (bp->flags & BNXT_FLAG_CHIP_P5) {
13245 		bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
13246 		/* On P5 chips, max_cp output param should be available NQs */
13247 		*max_cp = max_irq;
13248 	}
13249 	*max_rx = min_t(int, *max_rx, max_ring_grps);
13250 }
13251 
bnxt_get_max_rings(struct bnxt * bp,int * max_rx,int * max_tx,bool shared)13252 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
13253 {
13254 	int rx, tx, cp;
13255 
13256 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
13257 	*max_rx = rx;
13258 	*max_tx = tx;
13259 	if (!rx || !tx || !cp)
13260 		return -ENOMEM;
13261 
13262 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
13263 }
13264 
bnxt_get_dflt_rings(struct bnxt * bp,int * max_rx,int * max_tx,bool shared)13265 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
13266 			       bool shared)
13267 {
13268 	int rc;
13269 
13270 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
13271 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
13272 		/* Not enough rings, try disabling agg rings. */
13273 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
13274 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
13275 		if (rc) {
13276 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
13277 			bp->flags |= BNXT_FLAG_AGG_RINGS;
13278 			return rc;
13279 		}
13280 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
13281 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13282 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13283 		bnxt_set_ring_params(bp);
13284 	}
13285 
13286 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
13287 		int max_cp, max_stat, max_irq;
13288 
13289 		/* Reserve minimum resources for RoCE */
13290 		max_cp = bnxt_get_max_func_cp_rings(bp);
13291 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
13292 		max_irq = bnxt_get_max_func_irqs(bp);
13293 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
13294 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
13295 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
13296 			return 0;
13297 
13298 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
13299 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
13300 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
13301 		max_cp = min_t(int, max_cp, max_irq);
13302 		max_cp = min_t(int, max_cp, max_stat);
13303 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
13304 		if (rc)
13305 			rc = 0;
13306 	}
13307 	return rc;
13308 }
13309 
13310 /* In initial default shared ring setting, each shared ring must have a
13311  * RX/TX ring pair.
13312  */
bnxt_trim_dflt_sh_rings(struct bnxt * bp)13313 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
13314 {
13315 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
13316 	bp->rx_nr_rings = bp->cp_nr_rings;
13317 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
13318 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
13319 }
13320 
bnxt_set_dflt_rings(struct bnxt * bp,bool sh)13321 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
13322 {
13323 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
13324 
13325 	if (!bnxt_can_reserve_rings(bp))
13326 		return 0;
13327 
13328 	if (sh)
13329 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
13330 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
13331 	/* Reduce default rings on multi-port cards so that total default
13332 	 * rings do not exceed CPU count.
13333 	 */
13334 	if (bp->port_count > 1) {
13335 		int max_rings =
13336 			max_t(int, num_online_cpus() / bp->port_count, 1);
13337 
13338 		dflt_rings = min_t(int, dflt_rings, max_rings);
13339 	}
13340 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
13341 	if (rc)
13342 		return rc;
13343 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
13344 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
13345 	if (sh)
13346 		bnxt_trim_dflt_sh_rings(bp);
13347 	else
13348 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
13349 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
13350 
13351 	rc = __bnxt_reserve_rings(bp);
13352 	if (rc && rc != -ENODEV)
13353 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
13354 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13355 	if (sh)
13356 		bnxt_trim_dflt_sh_rings(bp);
13357 
13358 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
13359 	if (bnxt_need_reserve_rings(bp)) {
13360 		rc = __bnxt_reserve_rings(bp);
13361 		if (rc && rc != -ENODEV)
13362 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
13363 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13364 	}
13365 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
13366 		bp->rx_nr_rings++;
13367 		bp->cp_nr_rings++;
13368 	}
13369 	if (rc) {
13370 		bp->tx_nr_rings = 0;
13371 		bp->rx_nr_rings = 0;
13372 	}
13373 	return rc;
13374 }
13375 
bnxt_init_dflt_ring_mode(struct bnxt * bp)13376 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
13377 {
13378 	int rc;
13379 
13380 	if (bp->tx_nr_rings)
13381 		return 0;
13382 
13383 	bnxt_ulp_irq_stop(bp);
13384 	bnxt_clear_int_mode(bp);
13385 	rc = bnxt_set_dflt_rings(bp, true);
13386 	if (rc) {
13387 		if (BNXT_VF(bp) && rc == -ENODEV)
13388 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
13389 		else
13390 			netdev_err(bp->dev, "Not enough rings available.\n");
13391 		goto init_dflt_ring_err;
13392 	}
13393 	rc = bnxt_init_int_mode(bp);
13394 	if (rc)
13395 		goto init_dflt_ring_err;
13396 
13397 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13398 
13399 	bnxt_set_dflt_rfs(bp);
13400 
13401 init_dflt_ring_err:
13402 	bnxt_ulp_irq_restart(bp, rc);
13403 	return rc;
13404 }
13405 
bnxt_restore_pf_fw_resources(struct bnxt * bp)13406 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
13407 {
13408 	int rc;
13409 
13410 	ASSERT_RTNL();
13411 	bnxt_hwrm_func_qcaps(bp);
13412 
13413 	if (netif_running(bp->dev))
13414 		__bnxt_close_nic(bp, true, false);
13415 
13416 	bnxt_ulp_irq_stop(bp);
13417 	bnxt_clear_int_mode(bp);
13418 	rc = bnxt_init_int_mode(bp);
13419 	bnxt_ulp_irq_restart(bp, rc);
13420 
13421 	if (netif_running(bp->dev)) {
13422 		if (rc)
13423 			dev_close(bp->dev);
13424 		else
13425 			rc = bnxt_open_nic(bp, true, false);
13426 	}
13427 
13428 	return rc;
13429 }
13430 
bnxt_init_mac_addr(struct bnxt * bp)13431 static int bnxt_init_mac_addr(struct bnxt *bp)
13432 {
13433 	int rc = 0;
13434 
13435 	if (BNXT_PF(bp)) {
13436 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
13437 	} else {
13438 #ifdef CONFIG_BNXT_SRIOV
13439 		struct bnxt_vf_info *vf = &bp->vf;
13440 		bool strict_approval = true;
13441 
13442 		if (is_valid_ether_addr(vf->mac_addr)) {
13443 			/* overwrite netdev dev_addr with admin VF MAC */
13444 			eth_hw_addr_set(bp->dev, vf->mac_addr);
13445 			/* Older PF driver or firmware may not approve this
13446 			 * correctly.
13447 			 */
13448 			strict_approval = false;
13449 		} else {
13450 			eth_hw_addr_random(bp->dev);
13451 		}
13452 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
13453 #endif
13454 	}
13455 	return rc;
13456 }
13457 
bnxt_vpd_read_info(struct bnxt * bp)13458 static void bnxt_vpd_read_info(struct bnxt *bp)
13459 {
13460 	struct pci_dev *pdev = bp->pdev;
13461 	unsigned int vpd_size, kw_len;
13462 	int pos, size;
13463 	u8 *vpd_data;
13464 
13465 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
13466 	if (IS_ERR(vpd_data)) {
13467 		pci_warn(pdev, "Unable to read VPD\n");
13468 		return;
13469 	}
13470 
13471 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
13472 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
13473 	if (pos < 0)
13474 		goto read_sn;
13475 
13476 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
13477 	memcpy(bp->board_partno, &vpd_data[pos], size);
13478 
13479 read_sn:
13480 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
13481 					   PCI_VPD_RO_KEYWORD_SERIALNO,
13482 					   &kw_len);
13483 	if (pos < 0)
13484 		goto exit;
13485 
13486 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
13487 	memcpy(bp->board_serialno, &vpd_data[pos], size);
13488 exit:
13489 	kfree(vpd_data);
13490 }
13491 
bnxt_pcie_dsn_get(struct bnxt * bp,u8 dsn[])13492 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
13493 {
13494 	struct pci_dev *pdev = bp->pdev;
13495 	u64 qword;
13496 
13497 	qword = pci_get_dsn(pdev);
13498 	if (!qword) {
13499 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
13500 		return -EOPNOTSUPP;
13501 	}
13502 
13503 	put_unaligned_le64(qword, dsn);
13504 
13505 	bp->flags |= BNXT_FLAG_DSN_VALID;
13506 	return 0;
13507 }
13508 
bnxt_map_db_bar(struct bnxt * bp)13509 static int bnxt_map_db_bar(struct bnxt *bp)
13510 {
13511 	if (!bp->db_size)
13512 		return -ENODEV;
13513 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
13514 	if (!bp->bar1)
13515 		return -ENOMEM;
13516 	return 0;
13517 }
13518 
bnxt_print_device_info(struct bnxt * bp)13519 void bnxt_print_device_info(struct bnxt *bp)
13520 {
13521 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
13522 		    board_info[bp->board_idx].name,
13523 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
13524 
13525 	pcie_print_link_status(bp->pdev);
13526 }
13527 
bnxt_init_one(struct pci_dev * pdev,const struct pci_device_id * ent)13528 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
13529 {
13530 	struct net_device *dev;
13531 	struct bnxt *bp;
13532 	int rc, max_irqs;
13533 
13534 	if (pci_is_bridge(pdev))
13535 		return -ENODEV;
13536 
13537 	/* Clear any pending DMA transactions from crash kernel
13538 	 * while loading driver in capture kernel.
13539 	 */
13540 	if (is_kdump_kernel()) {
13541 		pci_clear_master(pdev);
13542 		pcie_flr(pdev);
13543 	}
13544 
13545 	max_irqs = bnxt_get_max_irq(pdev);
13546 	dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
13547 	if (!dev)
13548 		return -ENOMEM;
13549 
13550 	bp = netdev_priv(dev);
13551 	bp->board_idx = ent->driver_data;
13552 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
13553 	bnxt_set_max_func_irqs(bp, max_irqs);
13554 
13555 	if (bnxt_vf_pciid(bp->board_idx))
13556 		bp->flags |= BNXT_FLAG_VF;
13557 
13558 	if (pdev->msix_cap)
13559 		bp->flags |= BNXT_FLAG_MSIX_CAP;
13560 
13561 	rc = bnxt_init_board(pdev, dev);
13562 	if (rc < 0)
13563 		goto init_err_free;
13564 
13565 	dev->netdev_ops = &bnxt_netdev_ops;
13566 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
13567 	dev->ethtool_ops = &bnxt_ethtool_ops;
13568 	pci_set_drvdata(pdev, dev);
13569 
13570 	rc = bnxt_alloc_hwrm_resources(bp);
13571 	if (rc)
13572 		goto init_err_pci_clean;
13573 
13574 	mutex_init(&bp->hwrm_cmd_lock);
13575 	mutex_init(&bp->link_lock);
13576 
13577 	rc = bnxt_fw_init_one_p1(bp);
13578 	if (rc)
13579 		goto init_err_pci_clean;
13580 
13581 	if (BNXT_PF(bp))
13582 		bnxt_vpd_read_info(bp);
13583 
13584 	if (BNXT_CHIP_P5(bp)) {
13585 		bp->flags |= BNXT_FLAG_CHIP_P5;
13586 		if (BNXT_CHIP_SR2(bp))
13587 			bp->flags |= BNXT_FLAG_CHIP_SR2;
13588 	}
13589 
13590 	rc = bnxt_alloc_rss_indir_tbl(bp);
13591 	if (rc)
13592 		goto init_err_pci_clean;
13593 
13594 	rc = bnxt_fw_init_one_p2(bp);
13595 	if (rc)
13596 		goto init_err_pci_clean;
13597 
13598 	rc = bnxt_map_db_bar(bp);
13599 	if (rc) {
13600 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
13601 			rc);
13602 		goto init_err_pci_clean;
13603 	}
13604 
13605 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
13606 			   NETIF_F_TSO | NETIF_F_TSO6 |
13607 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
13608 			   NETIF_F_GSO_IPXIP4 |
13609 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
13610 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
13611 			   NETIF_F_RXCSUM | NETIF_F_GRO;
13612 
13613 	if (BNXT_SUPPORTS_TPA(bp))
13614 		dev->hw_features |= NETIF_F_LRO;
13615 
13616 	dev->hw_enc_features =
13617 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
13618 			NETIF_F_TSO | NETIF_F_TSO6 |
13619 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
13620 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
13621 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
13622 	dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
13623 
13624 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
13625 				    NETIF_F_GSO_GRE_CSUM;
13626 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
13627 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
13628 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13629 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
13630 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
13631 	if (BNXT_SUPPORTS_TPA(bp))
13632 		dev->hw_features |= NETIF_F_GRO_HW;
13633 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
13634 	if (dev->features & NETIF_F_GRO_HW)
13635 		dev->features &= ~NETIF_F_LRO;
13636 	dev->priv_flags |= IFF_UNICAST_FLT;
13637 
13638 #ifdef CONFIG_BNXT_SRIOV
13639 	init_waitqueue_head(&bp->sriov_cfg_wait);
13640 #endif
13641 	if (BNXT_SUPPORTS_TPA(bp)) {
13642 		bp->gro_func = bnxt_gro_func_5730x;
13643 		if (BNXT_CHIP_P4(bp))
13644 			bp->gro_func = bnxt_gro_func_5731x;
13645 		else if (BNXT_CHIP_P5(bp))
13646 			bp->gro_func = bnxt_gro_func_5750x;
13647 	}
13648 	if (!BNXT_CHIP_P4_PLUS(bp))
13649 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
13650 
13651 	rc = bnxt_init_mac_addr(bp);
13652 	if (rc) {
13653 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
13654 		rc = -EADDRNOTAVAIL;
13655 		goto init_err_pci_clean;
13656 	}
13657 
13658 	if (BNXT_PF(bp)) {
13659 		/* Read the adapter's DSN to use as the eswitch switch_id */
13660 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
13661 	}
13662 
13663 	/* MTU range: 60 - FW defined max */
13664 	dev->min_mtu = ETH_ZLEN;
13665 	dev->max_mtu = bp->max_mtu;
13666 
13667 	rc = bnxt_probe_phy(bp, true);
13668 	if (rc)
13669 		goto init_err_pci_clean;
13670 
13671 	bnxt_set_rx_skb_mode(bp, false);
13672 	bnxt_set_tpa_flags(bp);
13673 	bnxt_set_ring_params(bp);
13674 	rc = bnxt_set_dflt_rings(bp, true);
13675 	if (rc) {
13676 		if (BNXT_VF(bp) && rc == -ENODEV) {
13677 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
13678 		} else {
13679 			netdev_err(bp->dev, "Not enough rings available.\n");
13680 			rc = -ENOMEM;
13681 		}
13682 		goto init_err_pci_clean;
13683 	}
13684 
13685 	bnxt_fw_init_one_p3(bp);
13686 
13687 	bnxt_init_dflt_coal(bp);
13688 
13689 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13690 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
13691 
13692 	rc = bnxt_init_int_mode(bp);
13693 	if (rc)
13694 		goto init_err_pci_clean;
13695 
13696 	/* No TC has been set yet and rings may have been trimmed due to
13697 	 * limited MSIX, so we re-initialize the TX rings per TC.
13698 	 */
13699 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13700 
13701 	if (BNXT_PF(bp)) {
13702 		if (!bnxt_pf_wq) {
13703 			bnxt_pf_wq =
13704 				create_singlethread_workqueue("bnxt_pf_wq");
13705 			if (!bnxt_pf_wq) {
13706 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
13707 				rc = -ENOMEM;
13708 				goto init_err_pci_clean;
13709 			}
13710 		}
13711 		rc = bnxt_init_tc(bp);
13712 		if (rc)
13713 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
13714 				   rc);
13715 	}
13716 
13717 	bnxt_inv_fw_health_reg(bp);
13718 	rc = bnxt_dl_register(bp);
13719 	if (rc)
13720 		goto init_err_dl;
13721 
13722 	rc = register_netdev(dev);
13723 	if (rc)
13724 		goto init_err_cleanup;
13725 
13726 	if (BNXT_PF(bp))
13727 		devlink_port_type_eth_set(&bp->dl_port, bp->dev);
13728 	bnxt_dl_fw_reporters_create(bp);
13729 
13730 	bnxt_print_device_info(bp);
13731 
13732 	pci_save_state(pdev);
13733 	return 0;
13734 
13735 init_err_cleanup:
13736 	bnxt_dl_unregister(bp);
13737 init_err_dl:
13738 	bnxt_shutdown_tc(bp);
13739 	bnxt_clear_int_mode(bp);
13740 
13741 init_err_pci_clean:
13742 	bnxt_hwrm_func_drv_unrgtr(bp);
13743 	bnxt_free_hwrm_resources(bp);
13744 	bnxt_ethtool_free(bp);
13745 	bnxt_ptp_clear(bp);
13746 	kfree(bp->ptp_cfg);
13747 	bp->ptp_cfg = NULL;
13748 	kfree(bp->fw_health);
13749 	bp->fw_health = NULL;
13750 	bnxt_cleanup_pci(bp);
13751 	bnxt_free_ctx_mem(bp);
13752 	kfree(bp->ctx);
13753 	bp->ctx = NULL;
13754 	kfree(bp->rss_indir_tbl);
13755 	bp->rss_indir_tbl = NULL;
13756 
13757 init_err_free:
13758 	free_netdev(dev);
13759 	return rc;
13760 }
13761 
bnxt_shutdown(struct pci_dev * pdev)13762 static void bnxt_shutdown(struct pci_dev *pdev)
13763 {
13764 	struct net_device *dev = pci_get_drvdata(pdev);
13765 	struct bnxt *bp;
13766 
13767 	if (!dev)
13768 		return;
13769 
13770 	rtnl_lock();
13771 	bp = netdev_priv(dev);
13772 	if (!bp)
13773 		goto shutdown_exit;
13774 
13775 	if (netif_running(dev))
13776 		dev_close(dev);
13777 
13778 	bnxt_ulp_shutdown(bp);
13779 	bnxt_clear_int_mode(bp);
13780 	pci_disable_device(pdev);
13781 
13782 	if (system_state == SYSTEM_POWER_OFF) {
13783 		pci_wake_from_d3(pdev, bp->wol);
13784 		pci_set_power_state(pdev, PCI_D3hot);
13785 	}
13786 
13787 shutdown_exit:
13788 	rtnl_unlock();
13789 }
13790 
13791 #ifdef CONFIG_PM_SLEEP
bnxt_suspend(struct device * device)13792 static int bnxt_suspend(struct device *device)
13793 {
13794 	struct net_device *dev = dev_get_drvdata(device);
13795 	struct bnxt *bp = netdev_priv(dev);
13796 	int rc = 0;
13797 
13798 	rtnl_lock();
13799 	bnxt_ulp_stop(bp);
13800 	if (netif_running(dev)) {
13801 		netif_device_detach(dev);
13802 		rc = bnxt_close(dev);
13803 	}
13804 	bnxt_hwrm_func_drv_unrgtr(bp);
13805 	pci_disable_device(bp->pdev);
13806 	bnxt_free_ctx_mem(bp);
13807 	kfree(bp->ctx);
13808 	bp->ctx = NULL;
13809 	rtnl_unlock();
13810 	return rc;
13811 }
13812 
bnxt_resume(struct device * device)13813 static int bnxt_resume(struct device *device)
13814 {
13815 	struct net_device *dev = dev_get_drvdata(device);
13816 	struct bnxt *bp = netdev_priv(dev);
13817 	int rc = 0;
13818 
13819 	rtnl_lock();
13820 	rc = pci_enable_device(bp->pdev);
13821 	if (rc) {
13822 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
13823 			   rc);
13824 		goto resume_exit;
13825 	}
13826 	pci_set_master(bp->pdev);
13827 	if (bnxt_hwrm_ver_get(bp)) {
13828 		rc = -ENODEV;
13829 		goto resume_exit;
13830 	}
13831 	rc = bnxt_hwrm_func_reset(bp);
13832 	if (rc) {
13833 		rc = -EBUSY;
13834 		goto resume_exit;
13835 	}
13836 
13837 	rc = bnxt_hwrm_func_qcaps(bp);
13838 	if (rc)
13839 		goto resume_exit;
13840 
13841 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
13842 		rc = -ENODEV;
13843 		goto resume_exit;
13844 	}
13845 
13846 	bnxt_get_wol_settings(bp);
13847 	if (netif_running(dev)) {
13848 		rc = bnxt_open(dev);
13849 		if (!rc)
13850 			netif_device_attach(dev);
13851 	}
13852 
13853 resume_exit:
13854 	bnxt_ulp_start(bp, rc);
13855 	if (!rc)
13856 		bnxt_reenable_sriov(bp);
13857 	rtnl_unlock();
13858 	return rc;
13859 }
13860 
13861 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
13862 #define BNXT_PM_OPS (&bnxt_pm_ops)
13863 
13864 #else
13865 
13866 #define BNXT_PM_OPS NULL
13867 
13868 #endif /* CONFIG_PM_SLEEP */
13869 
13870 /**
13871  * bnxt_io_error_detected - called when PCI error is detected
13872  * @pdev: Pointer to PCI device
13873  * @state: The current pci connection state
13874  *
13875  * This function is called after a PCI bus error affecting
13876  * this device has been detected.
13877  */
bnxt_io_error_detected(struct pci_dev * pdev,pci_channel_state_t state)13878 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
13879 					       pci_channel_state_t state)
13880 {
13881 	struct net_device *netdev = pci_get_drvdata(pdev);
13882 	struct bnxt *bp = netdev_priv(netdev);
13883 
13884 	netdev_info(netdev, "PCI I/O error detected\n");
13885 
13886 	rtnl_lock();
13887 	netif_device_detach(netdev);
13888 
13889 	bnxt_ulp_stop(bp);
13890 
13891 	if (state == pci_channel_io_perm_failure) {
13892 		rtnl_unlock();
13893 		return PCI_ERS_RESULT_DISCONNECT;
13894 	}
13895 
13896 	if (state == pci_channel_io_frozen)
13897 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
13898 
13899 	if (netif_running(netdev))
13900 		bnxt_close(netdev);
13901 
13902 	if (pci_is_enabled(pdev))
13903 		pci_disable_device(pdev);
13904 	bnxt_free_ctx_mem(bp);
13905 	kfree(bp->ctx);
13906 	bp->ctx = NULL;
13907 	rtnl_unlock();
13908 
13909 	/* Request a slot slot reset. */
13910 	return PCI_ERS_RESULT_NEED_RESET;
13911 }
13912 
13913 /**
13914  * bnxt_io_slot_reset - called after the pci bus has been reset.
13915  * @pdev: Pointer to PCI device
13916  *
13917  * Restart the card from scratch, as if from a cold-boot.
13918  * At this point, the card has exprienced a hard reset,
13919  * followed by fixups by BIOS, and has its config space
13920  * set up identically to what it was at cold boot.
13921  */
bnxt_io_slot_reset(struct pci_dev * pdev)13922 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
13923 {
13924 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
13925 	struct net_device *netdev = pci_get_drvdata(pdev);
13926 	struct bnxt *bp = netdev_priv(netdev);
13927 	int retry = 0;
13928 	int err = 0;
13929 	int off;
13930 
13931 	netdev_info(bp->dev, "PCI Slot Reset\n");
13932 
13933 	rtnl_lock();
13934 
13935 	if (pci_enable_device(pdev)) {
13936 		dev_err(&pdev->dev,
13937 			"Cannot re-enable PCI device after reset.\n");
13938 	} else {
13939 		pci_set_master(pdev);
13940 		/* Upon fatal error, our device internal logic that latches to
13941 		 * BAR value is getting reset and will restore only upon
13942 		 * rewritting the BARs.
13943 		 *
13944 		 * As pci_restore_state() does not re-write the BARs if the
13945 		 * value is same as saved value earlier, driver needs to
13946 		 * write the BARs to 0 to force restore, in case of fatal error.
13947 		 */
13948 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
13949 				       &bp->state)) {
13950 			for (off = PCI_BASE_ADDRESS_0;
13951 			     off <= PCI_BASE_ADDRESS_5; off += 4)
13952 				pci_write_config_dword(bp->pdev, off, 0);
13953 		}
13954 		pci_restore_state(pdev);
13955 		pci_save_state(pdev);
13956 
13957 		bnxt_inv_fw_health_reg(bp);
13958 		bnxt_try_map_fw_health_reg(bp);
13959 
13960 		/* In some PCIe AER scenarios, firmware may take up to
13961 		 * 10 seconds to become ready in the worst case.
13962 		 */
13963 		do {
13964 			err = bnxt_try_recover_fw(bp);
13965 			if (!err)
13966 				break;
13967 			retry++;
13968 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
13969 
13970 		if (err) {
13971 			dev_err(&pdev->dev, "Firmware not ready\n");
13972 			goto reset_exit;
13973 		}
13974 
13975 		err = bnxt_hwrm_func_reset(bp);
13976 		if (!err)
13977 			result = PCI_ERS_RESULT_RECOVERED;
13978 
13979 		bnxt_ulp_irq_stop(bp);
13980 		bnxt_clear_int_mode(bp);
13981 		err = bnxt_init_int_mode(bp);
13982 		bnxt_ulp_irq_restart(bp, err);
13983 	}
13984 
13985 reset_exit:
13986 	bnxt_clear_reservations(bp, true);
13987 	rtnl_unlock();
13988 
13989 	return result;
13990 }
13991 
13992 /**
13993  * bnxt_io_resume - called when traffic can start flowing again.
13994  * @pdev: Pointer to PCI device
13995  *
13996  * This callback is called when the error recovery driver tells
13997  * us that its OK to resume normal operation.
13998  */
bnxt_io_resume(struct pci_dev * pdev)13999 static void bnxt_io_resume(struct pci_dev *pdev)
14000 {
14001 	struct net_device *netdev = pci_get_drvdata(pdev);
14002 	struct bnxt *bp = netdev_priv(netdev);
14003 	int err;
14004 
14005 	netdev_info(bp->dev, "PCI Slot Resume\n");
14006 	rtnl_lock();
14007 
14008 	err = bnxt_hwrm_func_qcaps(bp);
14009 	if (!err && netif_running(netdev))
14010 		err = bnxt_open(netdev);
14011 
14012 	bnxt_ulp_start(bp, err);
14013 	if (!err) {
14014 		bnxt_reenable_sriov(bp);
14015 		netif_device_attach(netdev);
14016 	}
14017 
14018 	rtnl_unlock();
14019 }
14020 
14021 static const struct pci_error_handlers bnxt_err_handler = {
14022 	.error_detected	= bnxt_io_error_detected,
14023 	.slot_reset	= bnxt_io_slot_reset,
14024 	.resume		= bnxt_io_resume
14025 };
14026 
14027 static struct pci_driver bnxt_pci_driver = {
14028 	.name		= DRV_MODULE_NAME,
14029 	.id_table	= bnxt_pci_tbl,
14030 	.probe		= bnxt_init_one,
14031 	.remove		= bnxt_remove_one,
14032 	.shutdown	= bnxt_shutdown,
14033 	.driver.pm	= BNXT_PM_OPS,
14034 	.err_handler	= &bnxt_err_handler,
14035 #if defined(CONFIG_BNXT_SRIOV)
14036 	.sriov_configure = bnxt_sriov_configure,
14037 #endif
14038 };
14039 
bnxt_init(void)14040 static int __init bnxt_init(void)
14041 {
14042 	int err;
14043 
14044 	bnxt_debug_init();
14045 	err = pci_register_driver(&bnxt_pci_driver);
14046 	if (err) {
14047 		bnxt_debug_exit();
14048 		return err;
14049 	}
14050 
14051 	return 0;
14052 }
14053 
bnxt_exit(void)14054 static void __exit bnxt_exit(void)
14055 {
14056 	pci_unregister_driver(&bnxt_pci_driver);
14057 	if (bnxt_pf_wq)
14058 		destroy_workqueue(bnxt_pf_wq);
14059 	bnxt_debug_exit();
14060 }
14061 
14062 module_init(bnxt_init);
14063 module_exit(bnxt_exit);
14064