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