1 /* Broadcom NetXtreme-C/E network driver.
3 * Copyright (c) 2014-2016 Broadcom Corporation
4 * Copyright (c) 2016-2019 Broadcom Limited
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.
11 #include <linux/module.h>
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>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
43 #include <net/checksum.h>
44 #include <net/ip6_checksum.h>
45 #include <net/udp_tunnel.h>
46 #include <linux/workqueue.h>
47 #include <linux/prefetch.h>
48 #include <linux/cache.h>
49 #include <linux/log2.h>
50 #include <linux/aer.h>
51 #include <linux/bitmap.h>
52 #include <linux/ptp_clock_kernel.h>
53 #include <linux/timecounter.h>
54 #include <linux/cpu_rmap.h>
55 #include <linux/cpumask.h>
56 #include <net/pkt_cls.h>
57 #include <linux/hwmon.h>
58 #include <linux/hwmon-sysfs.h>
59 #include <net/page_pool.h>
64 #include "bnxt_sriov.h"
65 #include "bnxt_ethtool.h"
71 #include "bnxt_devlink.h"
72 #include "bnxt_debugfs.h"
74 #define BNXT_TX_TIMEOUT (5 * HZ)
75 #define BNXT_DEF_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_HW)
77 MODULE_LICENSE("GPL");
78 MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
80 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
81 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
82 #define BNXT_RX_COPY_THRESH 256
84 #define BNXT_TX_PUSH_THRESH 164
131 NETXTREME_E_P5_VF_HV,
134 /* indexed by enum above */
135 static const struct {
138 [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
139 [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
140 [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
141 [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
142 [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
143 [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
144 [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
145 [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
146 [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
147 [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
148 [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
149 [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
150 [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
151 [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
152 [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
153 [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
154 [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
155 [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
156 [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
157 [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
158 [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
159 [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
160 [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
161 [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
162 [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
163 [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
164 [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
165 [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
166 [BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
167 [BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
168 [BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
169 [BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
170 [BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
171 [BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
172 [BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
173 [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
174 [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
175 [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
176 [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
177 [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
178 [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
179 [NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
180 [NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
181 [NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
182 [NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
185 static const struct pci_device_id bnxt_pci_tbl[] = {
186 { PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
187 { PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
188 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
189 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
190 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
191 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
192 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
193 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
194 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
195 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
196 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
197 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
198 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
199 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
200 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
201 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
202 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
203 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
204 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
205 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
206 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
207 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
208 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
209 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
210 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
211 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
212 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
213 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
214 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
215 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
216 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
217 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
218 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
219 { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
220 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
221 { PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
222 { PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
223 { PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
224 { PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57508_NPAR },
225 { PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
226 { PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57502_NPAR },
227 { PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57508_NPAR },
228 { PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
229 { PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57502_NPAR },
230 { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
231 { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
232 #ifdef CONFIG_BNXT_SRIOV
233 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
234 { PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
235 { PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
236 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
237 { PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
238 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
239 { PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
240 { PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
241 { PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
242 { PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
243 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
244 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
245 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
246 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
247 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
248 { PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
249 { PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
250 { PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
251 { PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
252 { PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
253 { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
258 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
260 static const u16 bnxt_vf_req_snif[] = {
264 HWRM_CFA_L2_FILTER_ALLOC,
267 static const u16 bnxt_async_events_arr[] = {
268 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
269 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
270 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
271 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
272 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
273 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
274 ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
275 ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
276 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
277 ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
278 ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
279 ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
282 static struct workqueue_struct *bnxt_pf_wq;
284 static bool bnxt_vf_pciid(enum board_idx idx)
286 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
287 idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
288 idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
289 idx == NETXTREME_E_P5_VF_HV);
292 #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
293 #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
294 #define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
296 #define BNXT_CP_DB_IRQ_DIS(db) \
297 writel(DB_CP_IRQ_DIS_FLAGS, db)
299 #define BNXT_DB_CQ(db, idx) \
300 writel(DB_CP_FLAGS | RING_CMP(idx), (db)->doorbell)
302 #define BNXT_DB_NQ_P5(db, idx) \
303 writeq((db)->db_key64 | DBR_TYPE_NQ | RING_CMP(idx), (db)->doorbell)
305 #define BNXT_DB_CQ_ARM(db, idx) \
306 writel(DB_CP_REARM_FLAGS | RING_CMP(idx), (db)->doorbell)
308 #define BNXT_DB_NQ_ARM_P5(db, idx) \
309 writeq((db)->db_key64 | DBR_TYPE_NQ_ARM | RING_CMP(idx), (db)->doorbell)
311 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
313 if (bp->flags & BNXT_FLAG_CHIP_P5)
314 BNXT_DB_NQ_P5(db, idx);
319 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
321 if (bp->flags & BNXT_FLAG_CHIP_P5)
322 BNXT_DB_NQ_ARM_P5(db, idx);
324 BNXT_DB_CQ_ARM(db, idx);
327 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
329 if (bp->flags & BNXT_FLAG_CHIP_P5)
330 writeq(db->db_key64 | DBR_TYPE_CQ_ARMALL | RING_CMP(idx),
336 const u16 bnxt_lhint_arr[] = {
337 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
338 TX_BD_FLAGS_LHINT_512_TO_1023,
339 TX_BD_FLAGS_LHINT_1024_TO_2047,
340 TX_BD_FLAGS_LHINT_1024_TO_2047,
341 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
342 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
343 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
344 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
345 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
346 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
347 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
348 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
349 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
350 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
351 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
352 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
353 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
354 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
355 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
358 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
360 struct metadata_dst *md_dst = skb_metadata_dst(skb);
362 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
365 return md_dst->u.port_info.port_id;
368 static bool bnxt_txr_netif_try_stop_queue(struct bnxt *bp,
369 struct bnxt_tx_ring_info *txr,
370 struct netdev_queue *txq)
372 netif_tx_stop_queue(txq);
374 /* netif_tx_stop_queue() must be done before checking
375 * tx index in bnxt_tx_avail() below, because in
376 * bnxt_tx_int(), we update tx index before checking for
377 * netif_tx_queue_stopped().
380 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh) {
381 netif_tx_wake_queue(txq);
388 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
390 struct bnxt *bp = netdev_priv(dev);
392 struct tx_bd_ext *txbd1;
393 struct netdev_queue *txq;
396 unsigned int length, pad = 0;
397 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
399 struct pci_dev *pdev = bp->pdev;
400 struct bnxt_tx_ring_info *txr;
401 struct bnxt_sw_tx_bd *tx_buf;
404 i = skb_get_queue_mapping(skb);
405 if (unlikely(i >= bp->tx_nr_rings)) {
406 dev_kfree_skb_any(skb);
410 txq = netdev_get_tx_queue(dev, i);
411 txr = &bp->tx_ring[bp->tx_ring_map[i]];
414 free_size = bnxt_tx_avail(bp, txr);
415 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
416 if (bnxt_txr_netif_try_stop_queue(bp, txr, txq))
417 return NETDEV_TX_BUSY;
421 len = skb_headlen(skb);
422 last_frag = skb_shinfo(skb)->nr_frags;
424 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
426 txbd->tx_bd_opaque = prod;
428 tx_buf = &txr->tx_buf_ring[prod];
430 tx_buf->nr_frags = last_frag;
433 cfa_action = bnxt_xmit_get_cfa_action(skb);
434 if (skb_vlan_tag_present(skb)) {
435 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
436 skb_vlan_tag_get(skb);
437 /* Currently supports 8021Q, 8021AD vlan offloads
438 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
440 if (skb->vlan_proto == htons(ETH_P_8021Q))
441 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
444 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
445 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
447 if (ptp && ptp->tx_tstamp_en && !skb_is_gso(skb) &&
448 atomic_dec_if_positive(&ptp->tx_avail) >= 0) {
449 if (!bnxt_ptp_parse(skb, &ptp->tx_seqid,
452 ptp->tx_hdr_off += VLAN_HLEN;
453 lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
454 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
456 atomic_inc(&bp->ptp_cfg->tx_avail);
461 if (unlikely(skb->no_fcs))
462 lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
464 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
466 struct tx_push_buffer *tx_push_buf = txr->tx_push;
467 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
468 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
469 void __iomem *db = txr->tx_db.doorbell;
470 void *pdata = tx_push_buf->data;
474 /* Set COAL_NOW to be ready quickly for the next push */
475 tx_push->tx_bd_len_flags_type =
476 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
477 TX_BD_TYPE_LONG_TX_BD |
478 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
479 TX_BD_FLAGS_COAL_NOW |
480 TX_BD_FLAGS_PACKET_END |
481 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
483 if (skb->ip_summed == CHECKSUM_PARTIAL)
484 tx_push1->tx_bd_hsize_lflags =
485 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
487 tx_push1->tx_bd_hsize_lflags = 0;
489 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
490 tx_push1->tx_bd_cfa_action =
491 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
493 end = pdata + length;
494 end = PTR_ALIGN(end, 8) - 1;
497 skb_copy_from_linear_data(skb, pdata, len);
499 for (j = 0; j < last_frag; j++) {
500 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
503 fptr = skb_frag_address_safe(frag);
507 memcpy(pdata, fptr, skb_frag_size(frag));
508 pdata += skb_frag_size(frag);
511 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
512 txbd->tx_bd_haddr = txr->data_mapping;
513 prod = NEXT_TX(prod);
514 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
515 memcpy(txbd, tx_push1, sizeof(*txbd));
516 prod = NEXT_TX(prod);
518 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
522 netdev_tx_sent_queue(txq, skb->len);
523 wmb(); /* Sync is_push and byte queue before pushing data */
525 push_len = (length + sizeof(*tx_push) + 7) / 8;
527 __iowrite64_copy(db, tx_push_buf, 16);
528 __iowrite32_copy(db + 4, tx_push_buf + 1,
529 (push_len - 16) << 1);
531 __iowrite64_copy(db, tx_push_buf, push_len);
538 if (length < BNXT_MIN_PKT_SIZE) {
539 pad = BNXT_MIN_PKT_SIZE - length;
540 if (skb_pad(skb, pad)) {
541 /* SKB already freed. */
545 length = BNXT_MIN_PKT_SIZE;
548 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
550 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
551 dev_kfree_skb_any(skb);
556 dma_unmap_addr_set(tx_buf, mapping, mapping);
557 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
558 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
560 txbd->tx_bd_haddr = cpu_to_le64(mapping);
562 prod = NEXT_TX(prod);
563 txbd1 = (struct tx_bd_ext *)
564 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
566 txbd1->tx_bd_hsize_lflags = lflags;
567 if (skb_is_gso(skb)) {
570 if (skb->encapsulation)
571 hdr_len = skb_inner_network_offset(skb) +
572 skb_inner_network_header_len(skb) +
573 inner_tcp_hdrlen(skb);
575 hdr_len = skb_transport_offset(skb) +
578 txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
580 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
581 length = skb_shinfo(skb)->gso_size;
582 txbd1->tx_bd_mss = cpu_to_le32(length);
584 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
585 txbd1->tx_bd_hsize_lflags |=
586 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
587 txbd1->tx_bd_mss = 0;
591 if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
592 dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
597 flags |= bnxt_lhint_arr[length];
598 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
600 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
601 txbd1->tx_bd_cfa_action =
602 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
603 for (i = 0; i < last_frag; i++) {
604 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
606 prod = NEXT_TX(prod);
607 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
609 len = skb_frag_size(frag);
610 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
613 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
616 tx_buf = &txr->tx_buf_ring[prod];
617 dma_unmap_addr_set(tx_buf, mapping, mapping);
619 txbd->tx_bd_haddr = cpu_to_le64(mapping);
621 flags = len << TX_BD_LEN_SHIFT;
622 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
626 txbd->tx_bd_len_flags_type =
627 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
628 TX_BD_FLAGS_PACKET_END);
630 netdev_tx_sent_queue(txq, skb->len);
632 skb_tx_timestamp(skb);
634 /* Sync BD data before updating doorbell */
637 prod = NEXT_TX(prod);
640 if (!netdev_xmit_more() || netif_xmit_stopped(txq))
641 bnxt_db_write(bp, &txr->tx_db, prod);
645 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
646 if (netdev_xmit_more() && !tx_buf->is_push)
647 bnxt_db_write(bp, &txr->tx_db, prod);
649 bnxt_txr_netif_try_stop_queue(bp, txr, txq);
654 if (BNXT_TX_PTP_IS_SET(lflags))
655 atomic_inc(&bp->ptp_cfg->tx_avail);
659 /* start back at beginning and unmap skb */
661 tx_buf = &txr->tx_buf_ring[prod];
663 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
664 skb_headlen(skb), PCI_DMA_TODEVICE);
665 prod = NEXT_TX(prod);
667 /* unmap remaining mapped pages */
668 for (i = 0; i < last_frag; i++) {
669 prod = NEXT_TX(prod);
670 tx_buf = &txr->tx_buf_ring[prod];
671 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
672 skb_frag_size(&skb_shinfo(skb)->frags[i]),
676 dev_kfree_skb_any(skb);
680 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
682 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
683 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
684 u16 cons = txr->tx_cons;
685 struct pci_dev *pdev = bp->pdev;
687 unsigned int tx_bytes = 0;
689 for (i = 0; i < nr_pkts; i++) {
690 struct bnxt_sw_tx_bd *tx_buf;
691 bool compl_deferred = false;
695 tx_buf = &txr->tx_buf_ring[cons];
696 cons = NEXT_TX(cons);
700 if (tx_buf->is_push) {
705 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
706 skb_headlen(skb), PCI_DMA_TODEVICE);
707 last = tx_buf->nr_frags;
709 for (j = 0; j < last; j++) {
710 cons = NEXT_TX(cons);
711 tx_buf = &txr->tx_buf_ring[cons];
714 dma_unmap_addr(tx_buf, mapping),
715 skb_frag_size(&skb_shinfo(skb)->frags[j]),
718 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
719 if (bp->flags & BNXT_FLAG_CHIP_P5) {
720 if (!bnxt_get_tx_ts_p5(bp, skb))
721 compl_deferred = true;
723 atomic_inc(&bp->ptp_cfg->tx_avail);
728 cons = NEXT_TX(cons);
730 tx_bytes += skb->len;
732 dev_kfree_skb_any(skb);
735 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
738 /* Need to make the tx_cons update visible to bnxt_start_xmit()
739 * before checking for netif_tx_queue_stopped(). Without the
740 * memory barrier, there is a small possibility that bnxt_start_xmit()
741 * will miss it and cause the queue to be stopped forever.
745 if (unlikely(netif_tx_queue_stopped(txq)) &&
746 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
747 READ_ONCE(txr->dev_state) != BNXT_DEV_STATE_CLOSING)
748 netif_tx_wake_queue(txq);
751 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
752 struct bnxt_rx_ring_info *rxr,
755 struct device *dev = &bp->pdev->dev;
758 page = page_pool_dev_alloc_pages(rxr->page_pool);
762 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
763 DMA_ATTR_WEAK_ORDERING);
764 if (dma_mapping_error(dev, *mapping)) {
765 page_pool_recycle_direct(rxr->page_pool, page);
768 *mapping += bp->rx_dma_offset;
772 static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
776 struct pci_dev *pdev = bp->pdev;
778 data = kmalloc(bp->rx_buf_size, gfp);
782 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
783 bp->rx_buf_use_size, bp->rx_dir,
784 DMA_ATTR_WEAK_ORDERING);
786 if (dma_mapping_error(&pdev->dev, *mapping)) {
793 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
796 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
797 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
800 if (BNXT_RX_PAGE_MODE(bp)) {
802 __bnxt_alloc_rx_page(bp, &mapping, rxr, gfp);
808 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
810 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
816 rx_buf->data_ptr = data + bp->rx_offset;
818 rx_buf->mapping = mapping;
820 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
824 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
826 u16 prod = rxr->rx_prod;
827 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
828 struct rx_bd *cons_bd, *prod_bd;
830 prod_rx_buf = &rxr->rx_buf_ring[prod];
831 cons_rx_buf = &rxr->rx_buf_ring[cons];
833 prod_rx_buf->data = data;
834 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
836 prod_rx_buf->mapping = cons_rx_buf->mapping;
838 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
839 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
841 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
844 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
846 u16 next, max = rxr->rx_agg_bmap_size;
848 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
850 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
854 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
855 struct bnxt_rx_ring_info *rxr,
859 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
860 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
861 struct pci_dev *pdev = bp->pdev;
864 u16 sw_prod = rxr->rx_sw_agg_prod;
865 unsigned int offset = 0;
867 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
870 page = alloc_page(gfp);
874 rxr->rx_page_offset = 0;
876 offset = rxr->rx_page_offset;
877 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
878 if (rxr->rx_page_offset == PAGE_SIZE)
883 page = alloc_page(gfp);
888 mapping = dma_map_page_attrs(&pdev->dev, page, offset,
889 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
890 DMA_ATTR_WEAK_ORDERING);
891 if (dma_mapping_error(&pdev->dev, mapping)) {
896 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
897 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
899 __set_bit(sw_prod, rxr->rx_agg_bmap);
900 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
901 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
903 rx_agg_buf->page = page;
904 rx_agg_buf->offset = offset;
905 rx_agg_buf->mapping = mapping;
906 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
907 rxbd->rx_bd_opaque = sw_prod;
911 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
912 struct bnxt_cp_ring_info *cpr,
913 u16 cp_cons, u16 curr)
915 struct rx_agg_cmp *agg;
917 cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
918 agg = (struct rx_agg_cmp *)
919 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
923 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
924 struct bnxt_rx_ring_info *rxr,
925 u16 agg_id, u16 curr)
927 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
929 return &tpa_info->agg_arr[curr];
932 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
933 u16 start, u32 agg_bufs, bool tpa)
935 struct bnxt_napi *bnapi = cpr->bnapi;
936 struct bnxt *bp = bnapi->bp;
937 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
938 u16 prod = rxr->rx_agg_prod;
939 u16 sw_prod = rxr->rx_sw_agg_prod;
943 if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
946 for (i = 0; i < agg_bufs; i++) {
948 struct rx_agg_cmp *agg;
949 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
950 struct rx_bd *prod_bd;
954 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
956 agg = bnxt_get_agg(bp, cpr, idx, start + i);
957 cons = agg->rx_agg_cmp_opaque;
958 __clear_bit(cons, rxr->rx_agg_bmap);
960 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
961 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
963 __set_bit(sw_prod, rxr->rx_agg_bmap);
964 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
965 cons_rx_buf = &rxr->rx_agg_ring[cons];
967 /* It is possible for sw_prod to be equal to cons, so
968 * set cons_rx_buf->page to NULL first.
970 page = cons_rx_buf->page;
971 cons_rx_buf->page = NULL;
972 prod_rx_buf->page = page;
973 prod_rx_buf->offset = cons_rx_buf->offset;
975 prod_rx_buf->mapping = cons_rx_buf->mapping;
977 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
979 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
980 prod_bd->rx_bd_opaque = sw_prod;
982 prod = NEXT_RX_AGG(prod);
983 sw_prod = NEXT_RX_AGG(sw_prod);
985 rxr->rx_agg_prod = prod;
986 rxr->rx_sw_agg_prod = sw_prod;
989 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
990 struct bnxt_rx_ring_info *rxr,
991 u16 cons, void *data, u8 *data_ptr,
993 unsigned int offset_and_len)
995 unsigned int payload = offset_and_len >> 16;
996 unsigned int len = offset_and_len & 0xffff;
998 struct page *page = data;
999 u16 prod = rxr->rx_prod;
1000 struct sk_buff *skb;
1003 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1004 if (unlikely(err)) {
1005 bnxt_reuse_rx_data(rxr, cons, data);
1008 dma_addr -= bp->rx_dma_offset;
1009 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
1010 DMA_ATTR_WEAK_ORDERING);
1011 page_pool_release_page(rxr->page_pool, page);
1013 if (unlikely(!payload))
1014 payload = eth_get_headlen(bp->dev, data_ptr, len);
1016 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1022 off = (void *)data_ptr - page_address(page);
1023 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
1024 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1025 payload + NET_IP_ALIGN);
1027 frag = &skb_shinfo(skb)->frags[0];
1028 skb_frag_size_sub(frag, payload);
1029 skb_frag_off_add(frag, payload);
1030 skb->data_len -= payload;
1031 skb->tail += payload;
1036 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1037 struct bnxt_rx_ring_info *rxr, u16 cons,
1038 void *data, u8 *data_ptr,
1039 dma_addr_t dma_addr,
1040 unsigned int offset_and_len)
1042 u16 prod = rxr->rx_prod;
1043 struct sk_buff *skb;
1046 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1047 if (unlikely(err)) {
1048 bnxt_reuse_rx_data(rxr, cons, data);
1052 skb = build_skb(data, 0);
1053 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1054 bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
1060 skb_reserve(skb, bp->rx_offset);
1061 skb_put(skb, offset_and_len & 0xffff);
1065 static struct sk_buff *bnxt_rx_pages(struct bnxt *bp,
1066 struct bnxt_cp_ring_info *cpr,
1067 struct sk_buff *skb, u16 idx,
1068 u32 agg_bufs, bool tpa)
1070 struct bnxt_napi *bnapi = cpr->bnapi;
1071 struct pci_dev *pdev = bp->pdev;
1072 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1073 u16 prod = rxr->rx_agg_prod;
1074 bool p5_tpa = false;
1077 if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
1080 for (i = 0; i < agg_bufs; i++) {
1082 struct rx_agg_cmp *agg;
1083 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1088 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1090 agg = bnxt_get_agg(bp, cpr, idx, i);
1091 cons = agg->rx_agg_cmp_opaque;
1092 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1093 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1095 cons_rx_buf = &rxr->rx_agg_ring[cons];
1096 skb_fill_page_desc(skb, i, cons_rx_buf->page,
1097 cons_rx_buf->offset, frag_len);
1098 __clear_bit(cons, rxr->rx_agg_bmap);
1100 /* It is possible for bnxt_alloc_rx_page() to allocate
1101 * a sw_prod index that equals the cons index, so we
1102 * need to clear the cons entry now.
1104 mapping = cons_rx_buf->mapping;
1105 page = cons_rx_buf->page;
1106 cons_rx_buf->page = NULL;
1108 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1109 struct skb_shared_info *shinfo;
1110 unsigned int nr_frags;
1112 shinfo = skb_shinfo(skb);
1113 nr_frags = --shinfo->nr_frags;
1114 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
1118 cons_rx_buf->page = page;
1120 /* Update prod since possibly some pages have been
1121 * allocated already.
1123 rxr->rx_agg_prod = prod;
1124 bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1128 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1130 DMA_ATTR_WEAK_ORDERING);
1132 skb->data_len += frag_len;
1133 skb->len += frag_len;
1134 skb->truesize += PAGE_SIZE;
1136 prod = NEXT_RX_AGG(prod);
1138 rxr->rx_agg_prod = prod;
1142 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1143 u8 agg_bufs, u32 *raw_cons)
1146 struct rx_agg_cmp *agg;
1148 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1149 last = RING_CMP(*raw_cons);
1150 agg = (struct rx_agg_cmp *)
1151 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1152 return RX_AGG_CMP_VALID(agg, *raw_cons);
1155 static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1159 struct bnxt *bp = bnapi->bp;
1160 struct pci_dev *pdev = bp->pdev;
1161 struct sk_buff *skb;
1163 skb = napi_alloc_skb(&bnapi->napi, len);
1167 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1170 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1171 len + NET_IP_ALIGN);
1173 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1180 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1181 u32 *raw_cons, void *cmp)
1183 struct rx_cmp *rxcmp = cmp;
1184 u32 tmp_raw_cons = *raw_cons;
1185 u8 cmp_type, agg_bufs = 0;
1187 cmp_type = RX_CMP_TYPE(rxcmp);
1189 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1190 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1192 RX_CMP_AGG_BUFS_SHIFT;
1193 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1194 struct rx_tpa_end_cmp *tpa_end = cmp;
1196 if (bp->flags & BNXT_FLAG_CHIP_P5)
1199 agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1203 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1206 *raw_cons = tmp_raw_cons;
1210 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
1212 if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
1216 queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
1218 schedule_delayed_work(&bp->fw_reset_task, delay);
1221 static void bnxt_queue_sp_work(struct bnxt *bp)
1224 queue_work(bnxt_pf_wq, &bp->sp_task);
1226 schedule_work(&bp->sp_task);
1229 static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1231 if (!rxr->bnapi->in_reset) {
1232 rxr->bnapi->in_reset = true;
1233 if (bp->flags & BNXT_FLAG_CHIP_P5)
1234 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1236 set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
1237 bnxt_queue_sp_work(bp);
1239 rxr->rx_next_cons = 0xffff;
1242 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1244 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1245 u16 idx = agg_id & MAX_TPA_P5_MASK;
1247 if (test_bit(idx, map->agg_idx_bmap))
1248 idx = find_first_zero_bit(map->agg_idx_bmap,
1249 BNXT_AGG_IDX_BMAP_SIZE);
1250 __set_bit(idx, map->agg_idx_bmap);
1251 map->agg_id_tbl[agg_id] = idx;
1255 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1257 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1259 __clear_bit(idx, map->agg_idx_bmap);
1262 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1264 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1266 return map->agg_id_tbl[agg_id];
1269 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1270 struct rx_tpa_start_cmp *tpa_start,
1271 struct rx_tpa_start_cmp_ext *tpa_start1)
1273 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1274 struct bnxt_tpa_info *tpa_info;
1275 u16 cons, prod, agg_id;
1276 struct rx_bd *prod_bd;
1279 if (bp->flags & BNXT_FLAG_CHIP_P5) {
1280 agg_id = TPA_START_AGG_ID_P5(tpa_start);
1281 agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1283 agg_id = TPA_START_AGG_ID(tpa_start);
1285 cons = tpa_start->rx_tpa_start_cmp_opaque;
1286 prod = rxr->rx_prod;
1287 cons_rx_buf = &rxr->rx_buf_ring[cons];
1288 prod_rx_buf = &rxr->rx_buf_ring[prod];
1289 tpa_info = &rxr->rx_tpa[agg_id];
1291 if (unlikely(cons != rxr->rx_next_cons ||
1292 TPA_START_ERROR(tpa_start))) {
1293 netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1294 cons, rxr->rx_next_cons,
1295 TPA_START_ERROR_CODE(tpa_start1));
1296 bnxt_sched_reset(bp, rxr);
1299 /* Store cfa_code in tpa_info to use in tpa_end
1300 * completion processing.
1302 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1303 prod_rx_buf->data = tpa_info->data;
1304 prod_rx_buf->data_ptr = tpa_info->data_ptr;
1306 mapping = tpa_info->mapping;
1307 prod_rx_buf->mapping = mapping;
1309 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1311 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1313 tpa_info->data = cons_rx_buf->data;
1314 tpa_info->data_ptr = cons_rx_buf->data_ptr;
1315 cons_rx_buf->data = NULL;
1316 tpa_info->mapping = cons_rx_buf->mapping;
1319 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1320 RX_TPA_START_CMP_LEN_SHIFT;
1321 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1322 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1324 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1325 tpa_info->gso_type = SKB_GSO_TCPV4;
1326 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1327 if (hash_type == 3 || TPA_START_IS_IPV6(tpa_start1))
1328 tpa_info->gso_type = SKB_GSO_TCPV6;
1329 tpa_info->rss_hash =
1330 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1332 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1333 tpa_info->gso_type = 0;
1334 netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1336 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1337 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1338 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1339 tpa_info->agg_count = 0;
1341 rxr->rx_prod = NEXT_RX(prod);
1342 cons = NEXT_RX(cons);
1343 rxr->rx_next_cons = NEXT_RX(cons);
1344 cons_rx_buf = &rxr->rx_buf_ring[cons];
1346 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1347 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1348 cons_rx_buf->data = NULL;
1351 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1354 bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1358 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1360 struct udphdr *uh = NULL;
1362 if (ip_proto == htons(ETH_P_IP)) {
1363 struct iphdr *iph = (struct iphdr *)skb->data;
1365 if (iph->protocol == IPPROTO_UDP)
1366 uh = (struct udphdr *)(iph + 1);
1368 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1370 if (iph->nexthdr == IPPROTO_UDP)
1371 uh = (struct udphdr *)(iph + 1);
1375 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1377 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1382 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1383 int payload_off, int tcp_ts,
1384 struct sk_buff *skb)
1389 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1390 u32 hdr_info = tpa_info->hdr_info;
1391 bool loopback = false;
1393 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1394 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1395 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1397 /* If the packet is an internal loopback packet, the offsets will
1398 * have an extra 4 bytes.
1400 if (inner_mac_off == 4) {
1402 } else if (inner_mac_off > 4) {
1403 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1406 /* We only support inner iPv4/ipv6. If we don't see the
1407 * correct protocol ID, it must be a loopback packet where
1408 * the offsets are off by 4.
1410 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1414 /* internal loopback packet, subtract all offsets by 4 */
1420 nw_off = inner_ip_off - ETH_HLEN;
1421 skb_set_network_header(skb, nw_off);
1422 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1423 struct ipv6hdr *iph = ipv6_hdr(skb);
1425 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1426 len = skb->len - skb_transport_offset(skb);
1428 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1430 struct iphdr *iph = ip_hdr(skb);
1432 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1433 len = skb->len - skb_transport_offset(skb);
1435 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1438 if (inner_mac_off) { /* tunnel */
1439 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1442 bnxt_gro_tunnel(skb, proto);
1448 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1449 int payload_off, int tcp_ts,
1450 struct sk_buff *skb)
1453 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1454 u32 hdr_info = tpa_info->hdr_info;
1455 int iphdr_len, nw_off;
1457 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1458 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1459 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1461 nw_off = inner_ip_off - ETH_HLEN;
1462 skb_set_network_header(skb, nw_off);
1463 iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1464 sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1465 skb_set_transport_header(skb, nw_off + iphdr_len);
1467 if (inner_mac_off) { /* tunnel */
1468 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1471 bnxt_gro_tunnel(skb, proto);
1477 #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1478 #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1480 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1481 int payload_off, int tcp_ts,
1482 struct sk_buff *skb)
1486 int len, nw_off, tcp_opt_len = 0;
1491 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1494 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1496 skb_set_network_header(skb, nw_off);
1498 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1499 len = skb->len - skb_transport_offset(skb);
1501 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1502 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1503 struct ipv6hdr *iph;
1505 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1507 skb_set_network_header(skb, nw_off);
1508 iph = ipv6_hdr(skb);
1509 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1510 len = skb->len - skb_transport_offset(skb);
1512 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1514 dev_kfree_skb_any(skb);
1518 if (nw_off) /* tunnel */
1519 bnxt_gro_tunnel(skb, skb->protocol);
1524 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1525 struct bnxt_tpa_info *tpa_info,
1526 struct rx_tpa_end_cmp *tpa_end,
1527 struct rx_tpa_end_cmp_ext *tpa_end1,
1528 struct sk_buff *skb)
1534 segs = TPA_END_TPA_SEGS(tpa_end);
1538 NAPI_GRO_CB(skb)->count = segs;
1539 skb_shinfo(skb)->gso_size =
1540 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1541 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1542 if (bp->flags & BNXT_FLAG_CHIP_P5)
1543 payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1545 payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1546 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1548 tcp_gro_complete(skb);
1553 /* Given the cfa_code of a received packet determine which
1554 * netdev (vf-rep or PF) the packet is destined to.
1556 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1558 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1560 /* if vf-rep dev is NULL, the must belongs to the PF */
1561 return dev ? dev : bp->dev;
1564 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1565 struct bnxt_cp_ring_info *cpr,
1567 struct rx_tpa_end_cmp *tpa_end,
1568 struct rx_tpa_end_cmp_ext *tpa_end1,
1571 struct bnxt_napi *bnapi = cpr->bnapi;
1572 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1573 u8 *data_ptr, agg_bufs;
1575 struct bnxt_tpa_info *tpa_info;
1577 struct sk_buff *skb;
1578 u16 idx = 0, agg_id;
1582 if (unlikely(bnapi->in_reset)) {
1583 int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1586 return ERR_PTR(-EBUSY);
1590 if (bp->flags & BNXT_FLAG_CHIP_P5) {
1591 agg_id = TPA_END_AGG_ID_P5(tpa_end);
1592 agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1593 agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1594 tpa_info = &rxr->rx_tpa[agg_id];
1595 if (unlikely(agg_bufs != tpa_info->agg_count)) {
1596 netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1597 agg_bufs, tpa_info->agg_count);
1598 agg_bufs = tpa_info->agg_count;
1600 tpa_info->agg_count = 0;
1601 *event |= BNXT_AGG_EVENT;
1602 bnxt_free_agg_idx(rxr, agg_id);
1604 gro = !!(bp->flags & BNXT_FLAG_GRO);
1606 agg_id = TPA_END_AGG_ID(tpa_end);
1607 agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1608 tpa_info = &rxr->rx_tpa[agg_id];
1609 idx = RING_CMP(*raw_cons);
1611 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1612 return ERR_PTR(-EBUSY);
1614 *event |= BNXT_AGG_EVENT;
1615 idx = NEXT_CMP(idx);
1617 gro = !!TPA_END_GRO(tpa_end);
1619 data = tpa_info->data;
1620 data_ptr = tpa_info->data_ptr;
1622 len = tpa_info->len;
1623 mapping = tpa_info->mapping;
1625 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1626 bnxt_abort_tpa(cpr, idx, agg_bufs);
1627 if (agg_bufs > MAX_SKB_FRAGS)
1628 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1629 agg_bufs, (int)MAX_SKB_FRAGS);
1633 if (len <= bp->rx_copy_thresh) {
1634 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1636 bnxt_abort_tpa(cpr, idx, agg_bufs);
1641 dma_addr_t new_mapping;
1643 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1645 bnxt_abort_tpa(cpr, idx, agg_bufs);
1649 tpa_info->data = new_data;
1650 tpa_info->data_ptr = new_data + bp->rx_offset;
1651 tpa_info->mapping = new_mapping;
1653 skb = build_skb(data, 0);
1654 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1655 bp->rx_buf_use_size, bp->rx_dir,
1656 DMA_ATTR_WEAK_ORDERING);
1660 bnxt_abort_tpa(cpr, idx, agg_bufs);
1663 skb_reserve(skb, bp->rx_offset);
1668 skb = bnxt_rx_pages(bp, cpr, skb, idx, agg_bufs, true);
1670 /* Page reuse already handled by bnxt_rx_pages(). */
1676 eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
1678 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1679 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1681 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1682 (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1683 __be16 vlan_proto = htons(tpa_info->metadata >>
1684 RX_CMP_FLAGS2_METADATA_TPID_SFT);
1685 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1687 if (eth_type_vlan(vlan_proto)) {
1688 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1695 skb_checksum_none_assert(skb);
1696 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1697 skb->ip_summed = CHECKSUM_UNNECESSARY;
1699 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1703 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1708 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1709 struct rx_agg_cmp *rx_agg)
1711 u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1712 struct bnxt_tpa_info *tpa_info;
1714 agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1715 tpa_info = &rxr->rx_tpa[agg_id];
1716 BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1717 tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1720 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1721 struct sk_buff *skb)
1723 if (skb->dev != bp->dev) {
1724 /* this packet belongs to a vf-rep */
1725 bnxt_vf_rep_rx(bp, skb);
1728 skb_record_rx_queue(skb, bnapi->index);
1729 napi_gro_receive(&bnapi->napi, skb);
1732 /* returns the following:
1733 * 1 - 1 packet successfully received
1734 * 0 - successful TPA_START, packet not completed yet
1735 * -EBUSY - completion ring does not have all the agg buffers yet
1736 * -ENOMEM - packet aborted due to out of memory
1737 * -EIO - packet aborted due to hw error indicated in BD
1739 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1740 u32 *raw_cons, u8 *event)
1742 struct bnxt_napi *bnapi = cpr->bnapi;
1743 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1744 struct net_device *dev = bp->dev;
1745 struct rx_cmp *rxcmp;
1746 struct rx_cmp_ext *rxcmp1;
1747 u32 tmp_raw_cons = *raw_cons;
1748 u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1749 struct bnxt_sw_rx_bd *rx_buf;
1751 u8 *data_ptr, agg_bufs, cmp_type;
1752 dma_addr_t dma_addr;
1753 struct sk_buff *skb;
1758 rxcmp = (struct rx_cmp *)
1759 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1761 cmp_type = RX_CMP_TYPE(rxcmp);
1763 if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
1764 bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
1765 goto next_rx_no_prod_no_len;
1768 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1769 cp_cons = RING_CMP(tmp_raw_cons);
1770 rxcmp1 = (struct rx_cmp_ext *)
1771 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1773 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1776 prod = rxr->rx_prod;
1778 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1779 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1780 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1782 *event |= BNXT_RX_EVENT;
1783 goto next_rx_no_prod_no_len;
1785 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1786 skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
1787 (struct rx_tpa_end_cmp *)rxcmp,
1788 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
1795 bnxt_deliver_skb(bp, bnapi, skb);
1798 *event |= BNXT_RX_EVENT;
1799 goto next_rx_no_prod_no_len;
1802 cons = rxcmp->rx_cmp_opaque;
1803 if (unlikely(cons != rxr->rx_next_cons)) {
1804 int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
1806 /* 0xffff is forced error, don't print it */
1807 if (rxr->rx_next_cons != 0xffff)
1808 netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
1809 cons, rxr->rx_next_cons);
1810 bnxt_sched_reset(bp, rxr);
1813 goto next_rx_no_prod_no_len;
1815 rx_buf = &rxr->rx_buf_ring[cons];
1816 data = rx_buf->data;
1817 data_ptr = rx_buf->data_ptr;
1820 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1821 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
1824 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1827 cp_cons = NEXT_CMP(cp_cons);
1828 *event |= BNXT_AGG_EVENT;
1830 *event |= BNXT_RX_EVENT;
1832 rx_buf->data = NULL;
1833 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1834 u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
1836 bnxt_reuse_rx_data(rxr, cons, data);
1838 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
1842 if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
1843 bnapi->cp_ring.sw_stats.rx.rx_buf_errors++;
1844 if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
1845 !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
1846 netdev_warn_once(bp->dev, "RX buffer error %x\n",
1848 bnxt_sched_reset(bp, rxr);
1851 goto next_rx_no_len;
1854 flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
1855 len = flags >> RX_CMP_LEN_SHIFT;
1856 dma_addr = rx_buf->mapping;
1858 if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1863 if (len <= bp->rx_copy_thresh) {
1864 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
1865 bnxt_reuse_rx_data(rxr, cons, data);
1868 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
1876 if (rx_buf->data_ptr == data_ptr)
1877 payload = misc & RX_CMP_PAYLOAD_OFFSET;
1880 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
1889 skb = bnxt_rx_pages(bp, cpr, skb, cp_cons, agg_bufs, false);
1896 if (RX_CMP_HASH_VALID(rxcmp)) {
1897 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1898 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1900 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1901 if (hash_type != 1 && hash_type != 3)
1902 type = PKT_HASH_TYPE_L3;
1903 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1906 cfa_code = RX_CMP_CFA_CODE(rxcmp1);
1907 skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
1909 if ((rxcmp1->rx_cmp_flags2 &
1910 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1911 (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1912 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1913 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1914 __be16 vlan_proto = htons(meta_data >>
1915 RX_CMP_FLAGS2_METADATA_TPID_SFT);
1917 if (eth_type_vlan(vlan_proto)) {
1918 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1925 skb_checksum_none_assert(skb);
1926 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1927 if (dev->features & NETIF_F_RXCSUM) {
1928 skb->ip_summed = CHECKSUM_UNNECESSARY;
1929 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1932 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1933 if (dev->features & NETIF_F_RXCSUM)
1934 bnapi->cp_ring.sw_stats.rx.rx_l4_csum_errors++;
1938 if (unlikely((flags & RX_CMP_FLAGS_ITYPES_MASK) ==
1939 RX_CMP_FLAGS_ITYPE_PTP_W_TS)) {
1940 if (bp->flags & BNXT_FLAG_CHIP_P5) {
1941 u32 cmpl_ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1944 if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
1945 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
1947 spin_lock_bh(&ptp->ptp_lock);
1948 ns = timecounter_cyc2time(&ptp->tc, ts);
1949 spin_unlock_bh(&ptp->ptp_lock);
1950 memset(skb_hwtstamps(skb), 0,
1951 sizeof(*skb_hwtstamps(skb)));
1952 skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
1956 bnxt_deliver_skb(bp, bnapi, skb);
1960 cpr->rx_packets += 1;
1961 cpr->rx_bytes += len;
1964 rxr->rx_prod = NEXT_RX(prod);
1965 rxr->rx_next_cons = NEXT_RX(cons);
1967 next_rx_no_prod_no_len:
1968 *raw_cons = tmp_raw_cons;
1973 /* In netpoll mode, if we are using a combined completion ring, we need to
1974 * discard the rx packets and recycle the buffers.
1976 static int bnxt_force_rx_discard(struct bnxt *bp,
1977 struct bnxt_cp_ring_info *cpr,
1978 u32 *raw_cons, u8 *event)
1980 u32 tmp_raw_cons = *raw_cons;
1981 struct rx_cmp_ext *rxcmp1;
1982 struct rx_cmp *rxcmp;
1986 cp_cons = RING_CMP(tmp_raw_cons);
1987 rxcmp = (struct rx_cmp *)
1988 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1990 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1991 cp_cons = RING_CMP(tmp_raw_cons);
1992 rxcmp1 = (struct rx_cmp_ext *)
1993 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1995 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1998 cmp_type = RX_CMP_TYPE(rxcmp);
1999 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
2000 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2001 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2002 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2003 struct rx_tpa_end_cmp_ext *tpa_end1;
2005 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2006 tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2007 cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2009 return bnxt_rx_pkt(bp, cpr, raw_cons, event);
2012 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2014 struct bnxt_fw_health *fw_health = bp->fw_health;
2015 u32 reg = fw_health->regs[reg_idx];
2016 u32 reg_type, reg_off, val = 0;
2018 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2019 reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2021 case BNXT_FW_HEALTH_REG_TYPE_CFG:
2022 pci_read_config_dword(bp->pdev, reg_off, &val);
2024 case BNXT_FW_HEALTH_REG_TYPE_GRC:
2025 reg_off = fw_health->mapped_regs[reg_idx];
2027 case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2028 val = readl(bp->bar0 + reg_off);
2030 case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2031 val = readl(bp->bar1 + reg_off);
2034 if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2035 val &= fw_health->fw_reset_inprog_reg_mask;
2039 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2043 for (i = 0; i < bp->rx_nr_rings; i++) {
2044 u16 grp_idx = bp->rx_ring[i].bnapi->index;
2045 struct bnxt_ring_grp_info *grp_info;
2047 grp_info = &bp->grp_info[grp_idx];
2048 if (grp_info->agg_fw_ring_id == ring_id)
2051 return INVALID_HW_RING_ID;
2054 #define BNXT_GET_EVENT_PORT(data) \
2056 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2058 #define BNXT_EVENT_RING_TYPE(data2) \
2060 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2062 #define BNXT_EVENT_RING_TYPE_RX(data2) \
2063 (BNXT_EVENT_RING_TYPE(data2) == \
2064 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2066 static int bnxt_async_event_process(struct bnxt *bp,
2067 struct hwrm_async_event_cmpl *cmpl)
2069 u16 event_id = le16_to_cpu(cmpl->event_id);
2070 u32 data1 = le32_to_cpu(cmpl->event_data1);
2071 u32 data2 = le32_to_cpu(cmpl->event_data2);
2073 /* TODO CHIMP_FW: Define event id's for link change, error etc */
2075 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2076 struct bnxt_link_info *link_info = &bp->link_info;
2079 goto async_event_process_exit;
2081 /* print unsupported speed warning in forced speed mode only */
2082 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2083 (data1 & 0x20000)) {
2084 u16 fw_speed = link_info->force_link_speed;
2085 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2087 if (speed != SPEED_UNKNOWN)
2088 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2091 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2094 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2095 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2096 set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2098 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2099 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2101 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2102 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2104 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2105 u16 port_id = BNXT_GET_EVENT_PORT(data1);
2110 if (bp->pf.port_id != port_id)
2113 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2116 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2118 goto async_event_process_exit;
2119 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2121 case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2122 char *fatal_str = "non-fatal";
2125 goto async_event_process_exit;
2127 bp->fw_reset_timestamp = jiffies;
2128 bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2129 if (!bp->fw_reset_min_dsecs)
2130 bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2131 bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2132 if (!bp->fw_reset_max_dsecs)
2133 bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2134 if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2135 fatal_str = "fatal";
2136 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2138 netif_warn(bp, hw, bp->dev,
2139 "Firmware %s reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2140 fatal_str, data1, data2,
2141 bp->fw_reset_min_dsecs * 100,
2142 bp->fw_reset_max_dsecs * 100);
2143 set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2146 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2147 struct bnxt_fw_health *fw_health = bp->fw_health;
2150 goto async_event_process_exit;
2152 fw_health->enabled = EVENT_DATA1_RECOVERY_ENABLED(data1);
2153 fw_health->master = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2154 if (!fw_health->enabled) {
2155 netif_info(bp, drv, bp->dev,
2156 "Error recovery info: error recovery[0]\n");
2159 fw_health->tmr_multiplier =
2160 DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2161 bp->current_interval * 10);
2162 fw_health->tmr_counter = fw_health->tmr_multiplier;
2163 fw_health->last_fw_heartbeat =
2164 bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2165 fw_health->last_fw_reset_cnt =
2166 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2167 netif_info(bp, drv, bp->dev,
2168 "Error recovery info: error recovery[1], master[%d], reset count[%u], health status: 0x%x\n",
2169 fw_health->master, fw_health->last_fw_reset_cnt,
2170 bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG));
2171 goto async_event_process_exit;
2173 case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2174 netif_notice(bp, hw, bp->dev,
2175 "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2177 goto async_event_process_exit;
2178 case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2179 struct bnxt_rx_ring_info *rxr;
2182 if (bp->flags & BNXT_FLAG_CHIP_P5)
2183 goto async_event_process_exit;
2185 netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2186 BNXT_EVENT_RING_TYPE(data2), data1);
2187 if (!BNXT_EVENT_RING_TYPE_RX(data2))
2188 goto async_event_process_exit;
2190 grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2191 if (grp_idx == INVALID_HW_RING_ID) {
2192 netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2194 goto async_event_process_exit;
2196 rxr = bp->bnapi[grp_idx]->rx_ring;
2197 bnxt_sched_reset(bp, rxr);
2198 goto async_event_process_exit;
2200 case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2201 struct bnxt_fw_health *fw_health = bp->fw_health;
2203 netif_notice(bp, hw, bp->dev,
2204 "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2207 fw_health->echo_req_data1 = data1;
2208 fw_health->echo_req_data2 = data2;
2209 set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2212 goto async_event_process_exit;
2215 goto async_event_process_exit;
2217 bnxt_queue_sp_work(bp);
2218 async_event_process_exit:
2219 bnxt_ulp_async_events(bp, cmpl);
2223 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2225 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2226 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2227 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2228 (struct hwrm_fwd_req_cmpl *)txcmp;
2230 switch (cmpl_type) {
2231 case CMPL_BASE_TYPE_HWRM_DONE:
2232 seq_id = le16_to_cpu(h_cmpl->sequence_id);
2233 if (seq_id == bp->hwrm_intr_seq_id)
2234 bp->hwrm_intr_seq_id = (u16)~bp->hwrm_intr_seq_id;
2236 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
2239 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2240 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2242 if ((vf_id < bp->pf.first_vf_id) ||
2243 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2244 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2249 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2250 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
2251 bnxt_queue_sp_work(bp);
2254 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2255 bnxt_async_event_process(bp,
2256 (struct hwrm_async_event_cmpl *)txcmp);
2266 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2268 struct bnxt_napi *bnapi = dev_instance;
2269 struct bnxt *bp = bnapi->bp;
2270 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2271 u32 cons = RING_CMP(cpr->cp_raw_cons);
2274 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2275 napi_schedule(&bnapi->napi);
2279 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2281 u32 raw_cons = cpr->cp_raw_cons;
2282 u16 cons = RING_CMP(raw_cons);
2283 struct tx_cmp *txcmp;
2285 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2287 return TX_CMP_VALID(txcmp, raw_cons);
2290 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
2292 struct bnxt_napi *bnapi = dev_instance;
2293 struct bnxt *bp = bnapi->bp;
2294 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2295 u32 cons = RING_CMP(cpr->cp_raw_cons);
2298 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2300 if (!bnxt_has_work(bp, cpr)) {
2301 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
2302 /* return if erroneous interrupt */
2303 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
2307 /* disable ring IRQ */
2308 BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
2310 /* Return here if interrupt is shared and is disabled. */
2311 if (unlikely(atomic_read(&bp->intr_sem) != 0))
2314 napi_schedule(&bnapi->napi);
2318 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2321 struct bnxt_napi *bnapi = cpr->bnapi;
2322 u32 raw_cons = cpr->cp_raw_cons;
2327 struct tx_cmp *txcmp;
2329 cpr->has_more_work = 0;
2330 cpr->had_work_done = 1;
2334 cons = RING_CMP(raw_cons);
2335 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2337 if (!TX_CMP_VALID(txcmp, raw_cons))
2340 /* The valid test of the entry must be done first before
2341 * reading any further.
2344 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
2346 /* return full budget so NAPI will complete. */
2347 if (unlikely(tx_pkts > bp->tx_wake_thresh)) {
2349 raw_cons = NEXT_RAW_CMP(raw_cons);
2351 cpr->has_more_work = 1;
2354 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2356 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2358 rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2360 if (likely(rc >= 0))
2362 /* Increment rx_pkts when rc is -ENOMEM to count towards
2363 * the NAPI budget. Otherwise, we may potentially loop
2364 * here forever if we consistently cannot allocate
2367 else if (rc == -ENOMEM && budget)
2369 else if (rc == -EBUSY) /* partial completion */
2371 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
2372 CMPL_BASE_TYPE_HWRM_DONE) ||
2373 (TX_CMP_TYPE(txcmp) ==
2374 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
2375 (TX_CMP_TYPE(txcmp) ==
2376 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
2377 bnxt_hwrm_handler(bp, txcmp);
2379 raw_cons = NEXT_RAW_CMP(raw_cons);
2381 if (rx_pkts && rx_pkts == budget) {
2382 cpr->has_more_work = 1;
2387 if (event & BNXT_REDIRECT_EVENT)
2390 if (event & BNXT_TX_EVENT) {
2391 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
2392 u16 prod = txr->tx_prod;
2394 /* Sync BD data before updating doorbell */
2397 bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
2400 cpr->cp_raw_cons = raw_cons;
2401 bnapi->tx_pkts += tx_pkts;
2402 bnapi->events |= event;
2406 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi)
2408 if (bnapi->tx_pkts) {
2409 bnapi->tx_int(bp, bnapi, bnapi->tx_pkts);
2413 if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
2414 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2416 if (bnapi->events & BNXT_AGG_EVENT)
2417 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2418 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2423 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2426 struct bnxt_napi *bnapi = cpr->bnapi;
2429 rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2431 /* ACK completion ring before freeing tx ring and producing new
2432 * buffers in rx/agg rings to prevent overflowing the completion
2435 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
2437 __bnxt_poll_work_done(bp, bnapi);
2441 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
2443 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2444 struct bnxt *bp = bnapi->bp;
2445 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2446 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2447 struct tx_cmp *txcmp;
2448 struct rx_cmp_ext *rxcmp1;
2449 u32 cp_cons, tmp_raw_cons;
2450 u32 raw_cons = cpr->cp_raw_cons;
2457 cp_cons = RING_CMP(raw_cons);
2458 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2460 if (!TX_CMP_VALID(txcmp, raw_cons))
2463 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2464 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
2465 cp_cons = RING_CMP(tmp_raw_cons);
2466 rxcmp1 = (struct rx_cmp_ext *)
2467 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2469 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2472 /* force an error to recycle the buffer */
2473 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2474 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2476 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2477 if (likely(rc == -EIO) && budget)
2479 else if (rc == -EBUSY) /* partial completion */
2481 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
2482 CMPL_BASE_TYPE_HWRM_DONE)) {
2483 bnxt_hwrm_handler(bp, txcmp);
2486 "Invalid completion received on special ring\n");
2488 raw_cons = NEXT_RAW_CMP(raw_cons);
2490 if (rx_pkts == budget)
2494 cpr->cp_raw_cons = raw_cons;
2495 BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
2496 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2498 if (event & BNXT_AGG_EVENT)
2499 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2501 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
2502 napi_complete_done(napi, rx_pkts);
2503 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2508 static int bnxt_poll(struct napi_struct *napi, int budget)
2510 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2511 struct bnxt *bp = bnapi->bp;
2512 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2515 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
2516 napi_complete(napi);
2520 work_done += bnxt_poll_work(bp, cpr, budget - work_done);
2522 if (work_done >= budget) {
2524 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2528 if (!bnxt_has_work(bp, cpr)) {
2529 if (napi_complete_done(napi, work_done))
2530 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2534 if (bp->flags & BNXT_FLAG_DIM) {
2535 struct dim_sample dim_sample = {};
2537 dim_update_sample(cpr->event_ctr,
2541 net_dim(&cpr->dim, dim_sample);
2546 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
2548 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2549 int i, work_done = 0;
2551 for (i = 0; i < 2; i++) {
2552 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2555 work_done += __bnxt_poll_work(bp, cpr2,
2556 budget - work_done);
2557 cpr->has_more_work |= cpr2->has_more_work;
2563 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2566 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2569 for (i = 0; i < 2; i++) {
2570 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2571 struct bnxt_db_info *db;
2573 if (cpr2 && cpr2->had_work_done) {
2575 writeq(db->db_key64 | dbr_type |
2576 RING_CMP(cpr2->cp_raw_cons), db->doorbell);
2577 cpr2->had_work_done = 0;
2580 __bnxt_poll_work_done(bp, bnapi);
2583 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
2585 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2586 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2587 u32 raw_cons = cpr->cp_raw_cons;
2588 struct bnxt *bp = bnapi->bp;
2589 struct nqe_cn *nqcmp;
2593 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
2594 napi_complete(napi);
2597 if (cpr->has_more_work) {
2598 cpr->has_more_work = 0;
2599 work_done = __bnxt_poll_cqs(bp, bnapi, budget);
2602 cons = RING_CMP(raw_cons);
2603 nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2605 if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
2606 if (cpr->has_more_work)
2609 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL);
2610 cpr->cp_raw_cons = raw_cons;
2611 if (napi_complete_done(napi, work_done))
2612 BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
2617 /* The valid test of the entry must be done first before
2618 * reading any further.
2622 if (nqcmp->type == cpu_to_le16(NQ_CN_TYPE_CQ_NOTIFICATION)) {
2623 u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
2624 struct bnxt_cp_ring_info *cpr2;
2626 cpr2 = cpr->cp_ring_arr[idx];
2627 work_done += __bnxt_poll_work(bp, cpr2,
2628 budget - work_done);
2629 cpr->has_more_work |= cpr2->has_more_work;
2631 bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
2633 raw_cons = NEXT_RAW_CMP(raw_cons);
2635 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ);
2636 if (raw_cons != cpr->cp_raw_cons) {
2637 cpr->cp_raw_cons = raw_cons;
2638 BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
2643 static void bnxt_free_tx_skbs(struct bnxt *bp)
2646 struct pci_dev *pdev = bp->pdev;
2651 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
2652 for (i = 0; i < bp->tx_nr_rings; i++) {
2653 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2656 for (j = 0; j < max_idx;) {
2657 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
2658 struct sk_buff *skb;
2661 if (i < bp->tx_nr_rings_xdp &&
2662 tx_buf->action == XDP_REDIRECT) {
2663 dma_unmap_single(&pdev->dev,
2664 dma_unmap_addr(tx_buf, mapping),
2665 dma_unmap_len(tx_buf, len),
2667 xdp_return_frame(tx_buf->xdpf);
2669 tx_buf->xdpf = NULL;
2682 if (tx_buf->is_push) {
2688 dma_unmap_single(&pdev->dev,
2689 dma_unmap_addr(tx_buf, mapping),
2693 last = tx_buf->nr_frags;
2695 for (k = 0; k < last; k++, j++) {
2696 int ring_idx = j & bp->tx_ring_mask;
2697 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2699 tx_buf = &txr->tx_buf_ring[ring_idx];
2702 dma_unmap_addr(tx_buf, mapping),
2703 skb_frag_size(frag), PCI_DMA_TODEVICE);
2707 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
2711 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr)
2713 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
2714 struct pci_dev *pdev = bp->pdev;
2715 struct bnxt_tpa_idx_map *map;
2716 int i, max_idx, max_agg_idx;
2718 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2719 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2721 goto skip_rx_tpa_free;
2723 for (i = 0; i < bp->max_tpa; i++) {
2724 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
2725 u8 *data = tpa_info->data;
2730 dma_unmap_single_attrs(&pdev->dev, tpa_info->mapping,
2731 bp->rx_buf_use_size, bp->rx_dir,
2732 DMA_ATTR_WEAK_ORDERING);
2734 tpa_info->data = NULL;
2740 for (i = 0; i < max_idx; i++) {
2741 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
2742 dma_addr_t mapping = rx_buf->mapping;
2743 void *data = rx_buf->data;
2748 rx_buf->data = NULL;
2749 if (BNXT_RX_PAGE_MODE(bp)) {
2750 mapping -= bp->rx_dma_offset;
2751 dma_unmap_page_attrs(&pdev->dev, mapping, PAGE_SIZE,
2753 DMA_ATTR_WEAK_ORDERING);
2754 page_pool_recycle_direct(rxr->page_pool, data);
2756 dma_unmap_single_attrs(&pdev->dev, mapping,
2757 bp->rx_buf_use_size, bp->rx_dir,
2758 DMA_ATTR_WEAK_ORDERING);
2762 for (i = 0; i < max_agg_idx; i++) {
2763 struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
2764 struct page *page = rx_agg_buf->page;
2769 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2770 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
2771 DMA_ATTR_WEAK_ORDERING);
2773 rx_agg_buf->page = NULL;
2774 __clear_bit(i, rxr->rx_agg_bmap);
2779 __free_page(rxr->rx_page);
2780 rxr->rx_page = NULL;
2782 map = rxr->rx_tpa_idx_map;
2784 memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
2787 static void bnxt_free_rx_skbs(struct bnxt *bp)
2794 for (i = 0; i < bp->rx_nr_rings; i++)
2795 bnxt_free_one_rx_ring_skbs(bp, i);
2798 static void bnxt_free_skbs(struct bnxt *bp)
2800 bnxt_free_tx_skbs(bp);
2801 bnxt_free_rx_skbs(bp);
2804 static void bnxt_init_ctx_mem(struct bnxt_mem_init *mem_init, void *p, int len)
2806 u8 init_val = mem_init->init_val;
2807 u16 offset = mem_init->offset;
2813 if (offset == BNXT_MEM_INVALID_OFFSET) {
2814 memset(p, init_val, len);
2817 for (i = 0; i < len; i += mem_init->size)
2818 *(p2 + i + offset) = init_val;
2821 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
2823 struct pci_dev *pdev = bp->pdev;
2826 for (i = 0; i < rmem->nr_pages; i++) {
2827 if (!rmem->pg_arr[i])
2830 dma_free_coherent(&pdev->dev, rmem->page_size,
2831 rmem->pg_arr[i], rmem->dma_arr[i]);
2833 rmem->pg_arr[i] = NULL;
2836 size_t pg_tbl_size = rmem->nr_pages * 8;
2838 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
2839 pg_tbl_size = rmem->page_size;
2840 dma_free_coherent(&pdev->dev, pg_tbl_size,
2841 rmem->pg_tbl, rmem->pg_tbl_map);
2842 rmem->pg_tbl = NULL;
2844 if (rmem->vmem_size && *rmem->vmem) {
2850 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
2852 struct pci_dev *pdev = bp->pdev;
2856 if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
2857 valid_bit = PTU_PTE_VALID;
2858 if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
2859 size_t pg_tbl_size = rmem->nr_pages * 8;
2861 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
2862 pg_tbl_size = rmem->page_size;
2863 rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
2870 for (i = 0; i < rmem->nr_pages; i++) {
2871 u64 extra_bits = valid_bit;
2873 rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2877 if (!rmem->pg_arr[i])
2881 bnxt_init_ctx_mem(rmem->mem_init, rmem->pg_arr[i],
2883 if (rmem->nr_pages > 1 || rmem->depth > 0) {
2884 if (i == rmem->nr_pages - 2 &&
2885 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2886 extra_bits |= PTU_PTE_NEXT_TO_LAST;
2887 else if (i == rmem->nr_pages - 1 &&
2888 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2889 extra_bits |= PTU_PTE_LAST;
2891 cpu_to_le64(rmem->dma_arr[i] | extra_bits);
2895 if (rmem->vmem_size) {
2896 *rmem->vmem = vzalloc(rmem->vmem_size);
2903 static void bnxt_free_tpa_info(struct bnxt *bp)
2907 for (i = 0; i < bp->rx_nr_rings; i++) {
2908 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2910 kfree(rxr->rx_tpa_idx_map);
2911 rxr->rx_tpa_idx_map = NULL;
2913 kfree(rxr->rx_tpa[0].agg_arr);
2914 rxr->rx_tpa[0].agg_arr = NULL;
2921 static int bnxt_alloc_tpa_info(struct bnxt *bp)
2923 int i, j, total_aggs = 0;
2925 bp->max_tpa = MAX_TPA;
2926 if (bp->flags & BNXT_FLAG_CHIP_P5) {
2927 if (!bp->max_tpa_v2)
2929 bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
2930 total_aggs = bp->max_tpa * MAX_SKB_FRAGS;
2933 for (i = 0; i < bp->rx_nr_rings; i++) {
2934 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2935 struct rx_agg_cmp *agg;
2937 rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
2942 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
2944 agg = kcalloc(total_aggs, sizeof(*agg), GFP_KERNEL);
2945 rxr->rx_tpa[0].agg_arr = agg;
2948 for (j = 1; j < bp->max_tpa; j++)
2949 rxr->rx_tpa[j].agg_arr = agg + j * MAX_SKB_FRAGS;
2950 rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
2952 if (!rxr->rx_tpa_idx_map)
2958 static void bnxt_free_rx_rings(struct bnxt *bp)
2965 bnxt_free_tpa_info(bp);
2966 for (i = 0; i < bp->rx_nr_rings; i++) {
2967 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2968 struct bnxt_ring_struct *ring;
2971 bpf_prog_put(rxr->xdp_prog);
2973 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
2974 xdp_rxq_info_unreg(&rxr->xdp_rxq);
2976 page_pool_destroy(rxr->page_pool);
2977 rxr->page_pool = NULL;
2979 kfree(rxr->rx_agg_bmap);
2980 rxr->rx_agg_bmap = NULL;
2982 ring = &rxr->rx_ring_struct;
2983 bnxt_free_ring(bp, &ring->ring_mem);
2985 ring = &rxr->rx_agg_ring_struct;
2986 bnxt_free_ring(bp, &ring->ring_mem);
2990 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
2991 struct bnxt_rx_ring_info *rxr)
2993 struct page_pool_params pp = { 0 };
2995 pp.pool_size = bp->rx_ring_size;
2996 pp.nid = dev_to_node(&bp->pdev->dev);
2997 pp.dev = &bp->pdev->dev;
2998 pp.dma_dir = DMA_BIDIRECTIONAL;
3000 rxr->page_pool = page_pool_create(&pp);
3001 if (IS_ERR(rxr->page_pool)) {
3002 int err = PTR_ERR(rxr->page_pool);
3004 rxr->page_pool = NULL;
3010 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3012 int i, rc = 0, agg_rings = 0;
3017 if (bp->flags & BNXT_FLAG_AGG_RINGS)
3020 for (i = 0; i < bp->rx_nr_rings; i++) {
3021 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3022 struct bnxt_ring_struct *ring;
3024 ring = &rxr->rx_ring_struct;
3026 rc = bnxt_alloc_rx_page_pool(bp, rxr);
3030 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3034 rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3038 xdp_rxq_info_unreg(&rxr->xdp_rxq);
3042 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3050 ring = &rxr->rx_agg_ring_struct;
3051 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3056 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3057 mem_size = rxr->rx_agg_bmap_size / 8;
3058 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3059 if (!rxr->rx_agg_bmap)
3063 if (bp->flags & BNXT_FLAG_TPA)
3064 rc = bnxt_alloc_tpa_info(bp);
3068 static void bnxt_free_tx_rings(struct bnxt *bp)
3071 struct pci_dev *pdev = bp->pdev;
3076 for (i = 0; i < bp->tx_nr_rings; i++) {
3077 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3078 struct bnxt_ring_struct *ring;
3081 dma_free_coherent(&pdev->dev, bp->tx_push_size,
3082 txr->tx_push, txr->tx_push_mapping);
3083 txr->tx_push = NULL;
3086 ring = &txr->tx_ring_struct;
3088 bnxt_free_ring(bp, &ring->ring_mem);
3092 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3095 struct pci_dev *pdev = bp->pdev;
3097 bp->tx_push_size = 0;
3098 if (bp->tx_push_thresh) {
3101 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3102 bp->tx_push_thresh);
3104 if (push_size > 256) {
3106 bp->tx_push_thresh = 0;
3109 bp->tx_push_size = push_size;
3112 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3113 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3114 struct bnxt_ring_struct *ring;
3117 ring = &txr->tx_ring_struct;
3119 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3123 ring->grp_idx = txr->bnapi->index;
3124 if (bp->tx_push_size) {
3127 /* One pre-allocated DMA buffer to backup
3130 txr->tx_push = dma_alloc_coherent(&pdev->dev,
3132 &txr->tx_push_mapping,
3138 mapping = txr->tx_push_mapping +
3139 sizeof(struct tx_push_bd);
3140 txr->data_mapping = cpu_to_le64(mapping);
3142 qidx = bp->tc_to_qidx[j];
3143 ring->queue_id = bp->q_info[qidx].queue_id;
3144 if (i < bp->tx_nr_rings_xdp)
3146 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
3152 static void bnxt_free_cp_rings(struct bnxt *bp)
3159 for (i = 0; i < bp->cp_nr_rings; i++) {
3160 struct bnxt_napi *bnapi = bp->bnapi[i];
3161 struct bnxt_cp_ring_info *cpr;
3162 struct bnxt_ring_struct *ring;
3168 cpr = &bnapi->cp_ring;
3169 ring = &cpr->cp_ring_struct;
3171 bnxt_free_ring(bp, &ring->ring_mem);
3173 for (j = 0; j < 2; j++) {
3174 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3177 ring = &cpr2->cp_ring_struct;
3178 bnxt_free_ring(bp, &ring->ring_mem);
3180 cpr->cp_ring_arr[j] = NULL;
3186 static struct bnxt_cp_ring_info *bnxt_alloc_cp_sub_ring(struct bnxt *bp)
3188 struct bnxt_ring_mem_info *rmem;
3189 struct bnxt_ring_struct *ring;
3190 struct bnxt_cp_ring_info *cpr;
3193 cpr = kzalloc(sizeof(*cpr), GFP_KERNEL);
3197 ring = &cpr->cp_ring_struct;
3198 rmem = &ring->ring_mem;
3199 rmem->nr_pages = bp->cp_nr_pages;
3200 rmem->page_size = HW_CMPD_RING_SIZE;
3201 rmem->pg_arr = (void **)cpr->cp_desc_ring;
3202 rmem->dma_arr = cpr->cp_desc_mapping;
3203 rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
3204 rc = bnxt_alloc_ring(bp, rmem);
3206 bnxt_free_ring(bp, rmem);
3213 static int bnxt_alloc_cp_rings(struct bnxt *bp)
3215 bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
3216 int i, rc, ulp_base_vec, ulp_msix;
3218 ulp_msix = bnxt_get_ulp_msix_num(bp);
3219 ulp_base_vec = bnxt_get_ulp_msix_base(bp);
3220 for (i = 0; i < bp->cp_nr_rings; i++) {
3221 struct bnxt_napi *bnapi = bp->bnapi[i];
3222 struct bnxt_cp_ring_info *cpr;
3223 struct bnxt_ring_struct *ring;
3228 cpr = &bnapi->cp_ring;
3230 ring = &cpr->cp_ring_struct;
3232 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3236 if (ulp_msix && i >= ulp_base_vec)
3237 ring->map_idx = i + ulp_msix;
3241 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
3244 if (i < bp->rx_nr_rings) {
3245 struct bnxt_cp_ring_info *cpr2 =
3246 bnxt_alloc_cp_sub_ring(bp);
3248 cpr->cp_ring_arr[BNXT_RX_HDL] = cpr2;
3251 cpr2->bnapi = bnapi;
3253 if ((sh && i < bp->tx_nr_rings) ||
3254 (!sh && i >= bp->rx_nr_rings)) {
3255 struct bnxt_cp_ring_info *cpr2 =
3256 bnxt_alloc_cp_sub_ring(bp);
3258 cpr->cp_ring_arr[BNXT_TX_HDL] = cpr2;
3261 cpr2->bnapi = bnapi;
3267 static void bnxt_init_ring_struct(struct bnxt *bp)
3271 for (i = 0; i < bp->cp_nr_rings; i++) {
3272 struct bnxt_napi *bnapi = bp->bnapi[i];
3273 struct bnxt_ring_mem_info *rmem;
3274 struct bnxt_cp_ring_info *cpr;
3275 struct bnxt_rx_ring_info *rxr;
3276 struct bnxt_tx_ring_info *txr;
3277 struct bnxt_ring_struct *ring;
3282 cpr = &bnapi->cp_ring;
3283 ring = &cpr->cp_ring_struct;
3284 rmem = &ring->ring_mem;
3285 rmem->nr_pages = bp->cp_nr_pages;
3286 rmem->page_size = HW_CMPD_RING_SIZE;
3287 rmem->pg_arr = (void **)cpr->cp_desc_ring;
3288 rmem->dma_arr = cpr->cp_desc_mapping;
3289 rmem->vmem_size = 0;
3291 rxr = bnapi->rx_ring;
3295 ring = &rxr->rx_ring_struct;
3296 rmem = &ring->ring_mem;
3297 rmem->nr_pages = bp->rx_nr_pages;
3298 rmem->page_size = HW_RXBD_RING_SIZE;
3299 rmem->pg_arr = (void **)rxr->rx_desc_ring;
3300 rmem->dma_arr = rxr->rx_desc_mapping;
3301 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
3302 rmem->vmem = (void **)&rxr->rx_buf_ring;
3304 ring = &rxr->rx_agg_ring_struct;
3305 rmem = &ring->ring_mem;
3306 rmem->nr_pages = bp->rx_agg_nr_pages;
3307 rmem->page_size = HW_RXBD_RING_SIZE;
3308 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
3309 rmem->dma_arr = rxr->rx_agg_desc_mapping;
3310 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
3311 rmem->vmem = (void **)&rxr->rx_agg_ring;
3314 txr = bnapi->tx_ring;
3318 ring = &txr->tx_ring_struct;
3319 rmem = &ring->ring_mem;
3320 rmem->nr_pages = bp->tx_nr_pages;
3321 rmem->page_size = HW_RXBD_RING_SIZE;
3322 rmem->pg_arr = (void **)txr->tx_desc_ring;
3323 rmem->dma_arr = txr->tx_desc_mapping;
3324 rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
3325 rmem->vmem = (void **)&txr->tx_buf_ring;
3329 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
3333 struct rx_bd **rx_buf_ring;
3335 rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
3336 for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
3340 rxbd = rx_buf_ring[i];
3344 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
3345 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
3346 rxbd->rx_bd_opaque = prod;
3351 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
3353 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
3354 struct net_device *dev = bp->dev;
3358 prod = rxr->rx_prod;
3359 for (i = 0; i < bp->rx_ring_size; i++) {
3360 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
3361 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
3362 ring_nr, i, bp->rx_ring_size);
3365 prod = NEXT_RX(prod);
3367 rxr->rx_prod = prod;
3369 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
3372 prod = rxr->rx_agg_prod;
3373 for (i = 0; i < bp->rx_agg_ring_size; i++) {
3374 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
3375 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
3376 ring_nr, i, bp->rx_ring_size);
3379 prod = NEXT_RX_AGG(prod);
3381 rxr->rx_agg_prod = prod;
3387 for (i = 0; i < bp->max_tpa; i++) {
3388 data = __bnxt_alloc_rx_data(bp, &mapping, GFP_KERNEL);
3392 rxr->rx_tpa[i].data = data;
3393 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
3394 rxr->rx_tpa[i].mapping = mapping;
3400 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
3402 struct bnxt_rx_ring_info *rxr;
3403 struct bnxt_ring_struct *ring;
3406 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
3407 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
3409 if (NET_IP_ALIGN == 2)
3410 type |= RX_BD_FLAGS_SOP;
3412 rxr = &bp->rx_ring[ring_nr];
3413 ring = &rxr->rx_ring_struct;
3414 bnxt_init_rxbd_pages(ring, type);
3416 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
3417 bpf_prog_add(bp->xdp_prog, 1);
3418 rxr->xdp_prog = bp->xdp_prog;
3420 ring->fw_ring_id = INVALID_HW_RING_ID;
3422 ring = &rxr->rx_agg_ring_struct;
3423 ring->fw_ring_id = INVALID_HW_RING_ID;
3425 if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
3426 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
3427 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
3429 bnxt_init_rxbd_pages(ring, type);
3432 return bnxt_alloc_one_rx_ring(bp, ring_nr);
3435 static void bnxt_init_cp_rings(struct bnxt *bp)
3439 for (i = 0; i < bp->cp_nr_rings; i++) {
3440 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
3441 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3443 ring->fw_ring_id = INVALID_HW_RING_ID;
3444 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3445 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3446 for (j = 0; j < 2; j++) {
3447 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3452 ring = &cpr2->cp_ring_struct;
3453 ring->fw_ring_id = INVALID_HW_RING_ID;
3454 cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3455 cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3460 static int bnxt_init_rx_rings(struct bnxt *bp)
3464 if (BNXT_RX_PAGE_MODE(bp)) {
3465 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
3466 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
3468 bp->rx_offset = BNXT_RX_OFFSET;
3469 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
3472 for (i = 0; i < bp->rx_nr_rings; i++) {
3473 rc = bnxt_init_one_rx_ring(bp, i);
3481 static int bnxt_init_tx_rings(struct bnxt *bp)
3485 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
3488 for (i = 0; i < bp->tx_nr_rings; i++) {
3489 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3490 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
3492 ring->fw_ring_id = INVALID_HW_RING_ID;
3498 static void bnxt_free_ring_grps(struct bnxt *bp)
3500 kfree(bp->grp_info);
3501 bp->grp_info = NULL;
3504 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
3509 bp->grp_info = kcalloc(bp->cp_nr_rings,
3510 sizeof(struct bnxt_ring_grp_info),
3515 for (i = 0; i < bp->cp_nr_rings; i++) {
3517 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
3518 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3519 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
3520 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
3521 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
3526 static void bnxt_free_vnics(struct bnxt *bp)
3528 kfree(bp->vnic_info);
3529 bp->vnic_info = NULL;
3533 static int bnxt_alloc_vnics(struct bnxt *bp)
3537 #ifdef CONFIG_RFS_ACCEL
3538 if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
3539 num_vnics += bp->rx_nr_rings;
3542 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3545 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
3550 bp->nr_vnics = num_vnics;
3554 static void bnxt_init_vnics(struct bnxt *bp)
3558 for (i = 0; i < bp->nr_vnics; i++) {
3559 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3562 vnic->fw_vnic_id = INVALID_HW_RING_ID;
3563 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
3564 vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
3566 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
3568 if (bp->vnic_info[i].rss_hash_key) {
3570 prandom_bytes(vnic->rss_hash_key,
3573 memcpy(vnic->rss_hash_key,
3574 bp->vnic_info[0].rss_hash_key,
3580 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
3584 pages = ring_size / desc_per_pg;
3591 while (pages & (pages - 1))
3597 void bnxt_set_tpa_flags(struct bnxt *bp)
3599 bp->flags &= ~BNXT_FLAG_TPA;
3600 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
3602 if (bp->dev->features & NETIF_F_LRO)
3603 bp->flags |= BNXT_FLAG_LRO;
3604 else if (bp->dev->features & NETIF_F_GRO_HW)
3605 bp->flags |= BNXT_FLAG_GRO;
3608 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
3611 void bnxt_set_ring_params(struct bnxt *bp)
3613 u32 ring_size, rx_size, rx_space, max_rx_cmpl;
3614 u32 agg_factor = 0, agg_ring_size = 0;
3616 /* 8 for CRC and VLAN */
3617 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
3619 rx_space = rx_size + NET_SKB_PAD +
3620 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3622 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
3623 ring_size = bp->rx_ring_size;
3624 bp->rx_agg_ring_size = 0;
3625 bp->rx_agg_nr_pages = 0;
3627 if (bp->flags & BNXT_FLAG_TPA)
3628 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
3630 bp->flags &= ~BNXT_FLAG_JUMBO;
3631 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
3634 bp->flags |= BNXT_FLAG_JUMBO;
3635 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
3636 if (jumbo_factor > agg_factor)
3637 agg_factor = jumbo_factor;
3639 agg_ring_size = ring_size * agg_factor;
3641 if (agg_ring_size) {
3642 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
3644 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
3645 u32 tmp = agg_ring_size;
3647 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
3648 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
3649 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
3650 tmp, agg_ring_size);
3652 bp->rx_agg_ring_size = agg_ring_size;
3653 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
3654 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
3655 rx_space = rx_size + NET_SKB_PAD +
3656 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3659 bp->rx_buf_use_size = rx_size;
3660 bp->rx_buf_size = rx_space;
3662 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
3663 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
3665 ring_size = bp->tx_ring_size;
3666 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
3667 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
3669 max_rx_cmpl = bp->rx_ring_size;
3670 /* MAX TPA needs to be added because TPA_START completions are
3671 * immediately recycled, so the TPA completions are not bound by
3674 if (bp->flags & BNXT_FLAG_TPA)
3675 max_rx_cmpl += bp->max_tpa;
3676 /* RX and TPA completions are 32-byte, all others are 16-byte */
3677 ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
3678 bp->cp_ring_size = ring_size;
3680 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
3681 if (bp->cp_nr_pages > MAX_CP_PAGES) {
3682 bp->cp_nr_pages = MAX_CP_PAGES;
3683 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
3684 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
3685 ring_size, bp->cp_ring_size);
3687 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
3688 bp->cp_ring_mask = bp->cp_bit - 1;
3691 /* Changing allocation mode of RX rings.
3692 * TODO: Update when extending xdp_rxq_info to support allocation modes.
3694 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
3697 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
3700 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
3701 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
3702 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
3703 bp->rx_dir = DMA_BIDIRECTIONAL;
3704 bp->rx_skb_func = bnxt_rx_page_skb;
3705 /* Disable LRO or GRO_HW */
3706 netdev_update_features(bp->dev);
3708 bp->dev->max_mtu = bp->max_mtu;
3709 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
3710 bp->rx_dir = DMA_FROM_DEVICE;
3711 bp->rx_skb_func = bnxt_rx_skb;
3716 static void bnxt_free_vnic_attributes(struct bnxt *bp)
3719 struct bnxt_vnic_info *vnic;
3720 struct pci_dev *pdev = bp->pdev;
3725 for (i = 0; i < bp->nr_vnics; i++) {
3726 vnic = &bp->vnic_info[i];
3728 kfree(vnic->fw_grp_ids);
3729 vnic->fw_grp_ids = NULL;
3731 kfree(vnic->uc_list);
3732 vnic->uc_list = NULL;
3734 if (vnic->mc_list) {
3735 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
3736 vnic->mc_list, vnic->mc_list_mapping);
3737 vnic->mc_list = NULL;
3740 if (vnic->rss_table) {
3741 dma_free_coherent(&pdev->dev, vnic->rss_table_size,
3743 vnic->rss_table_dma_addr);
3744 vnic->rss_table = NULL;
3747 vnic->rss_hash_key = NULL;
3752 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
3754 int i, rc = 0, size;
3755 struct bnxt_vnic_info *vnic;
3756 struct pci_dev *pdev = bp->pdev;
3759 for (i = 0; i < bp->nr_vnics; i++) {
3760 vnic = &bp->vnic_info[i];
3762 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
3763 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
3766 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
3767 if (!vnic->uc_list) {
3774 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
3775 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
3777 dma_alloc_coherent(&pdev->dev,
3779 &vnic->mc_list_mapping,
3781 if (!vnic->mc_list) {
3787 if (bp->flags & BNXT_FLAG_CHIP_P5)
3788 goto vnic_skip_grps;
3790 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
3791 max_rings = bp->rx_nr_rings;
3795 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
3796 if (!vnic->fw_grp_ids) {
3801 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
3802 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
3805 /* Allocate rss table and hash key */
3806 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
3807 if (bp->flags & BNXT_FLAG_CHIP_P5)
3808 size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
3810 vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
3811 vnic->rss_table = dma_alloc_coherent(&pdev->dev,
3812 vnic->rss_table_size,
3813 &vnic->rss_table_dma_addr,
3815 if (!vnic->rss_table) {
3820 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
3821 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
3829 static void bnxt_free_hwrm_resources(struct bnxt *bp)
3831 struct pci_dev *pdev = bp->pdev;
3833 if (bp->hwrm_cmd_resp_addr) {
3834 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
3835 bp->hwrm_cmd_resp_dma_addr);
3836 bp->hwrm_cmd_resp_addr = NULL;
3839 if (bp->hwrm_cmd_kong_resp_addr) {
3840 dma_free_coherent(&pdev->dev, PAGE_SIZE,
3841 bp->hwrm_cmd_kong_resp_addr,
3842 bp->hwrm_cmd_kong_resp_dma_addr);
3843 bp->hwrm_cmd_kong_resp_addr = NULL;
3847 static int bnxt_alloc_kong_hwrm_resources(struct bnxt *bp)
3849 struct pci_dev *pdev = bp->pdev;
3851 if (bp->hwrm_cmd_kong_resp_addr)
3854 bp->hwrm_cmd_kong_resp_addr =
3855 dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3856 &bp->hwrm_cmd_kong_resp_dma_addr,
3858 if (!bp->hwrm_cmd_kong_resp_addr)
3864 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
3866 struct pci_dev *pdev = bp->pdev;
3868 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3869 &bp->hwrm_cmd_resp_dma_addr,
3871 if (!bp->hwrm_cmd_resp_addr)
3877 static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
3879 if (bp->hwrm_short_cmd_req_addr) {
3880 struct pci_dev *pdev = bp->pdev;
3882 dma_free_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
3883 bp->hwrm_short_cmd_req_addr,
3884 bp->hwrm_short_cmd_req_dma_addr);
3885 bp->hwrm_short_cmd_req_addr = NULL;
3889 static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
3891 struct pci_dev *pdev = bp->pdev;
3893 if (bp->hwrm_short_cmd_req_addr)
3896 bp->hwrm_short_cmd_req_addr =
3897 dma_alloc_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
3898 &bp->hwrm_short_cmd_req_dma_addr,
3900 if (!bp->hwrm_short_cmd_req_addr)
3906 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
3908 kfree(stats->hw_masks);
3909 stats->hw_masks = NULL;
3910 kfree(stats->sw_stats);
3911 stats->sw_stats = NULL;
3912 if (stats->hw_stats) {
3913 dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
3914 stats->hw_stats_map);
3915 stats->hw_stats = NULL;
3919 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
3922 stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
3923 &stats->hw_stats_map, GFP_KERNEL);
3924 if (!stats->hw_stats)
3927 stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
3928 if (!stats->sw_stats)
3932 stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
3933 if (!stats->hw_masks)
3939 bnxt_free_stats_mem(bp, stats);
3943 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
3947 for (i = 0; i < count; i++)
3951 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
3955 for (i = 0; i < count; i++)
3956 mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
3959 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
3960 struct bnxt_stats_mem *stats)
3962 struct hwrm_func_qstats_ext_output *resp = bp->hwrm_cmd_resp_addr;
3963 struct hwrm_func_qstats_ext_input req = {0};
3967 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
3968 !(bp->flags & BNXT_FLAG_CHIP_P5))
3971 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QSTATS_EXT, -1, -1);
3972 req.fid = cpu_to_le16(0xffff);
3973 req.flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
3974 mutex_lock(&bp->hwrm_cmd_lock);
3975 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3979 hw_masks = &resp->rx_ucast_pkts;
3980 bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
3983 mutex_unlock(&bp->hwrm_cmd_lock);
3987 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
3988 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
3990 static void bnxt_init_stats(struct bnxt *bp)
3992 struct bnxt_napi *bnapi = bp->bnapi[0];
3993 struct bnxt_cp_ring_info *cpr;
3994 struct bnxt_stats_mem *stats;
3995 __le64 *rx_stats, *tx_stats;
3996 int rc, rx_count, tx_count;
3997 u64 *rx_masks, *tx_masks;
4001 cpr = &bnapi->cp_ring;
4002 stats = &cpr->stats;
4003 rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4005 if (bp->flags & BNXT_FLAG_CHIP_P5)
4006 mask = (1ULL << 48) - 1;
4009 bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
4011 if (bp->flags & BNXT_FLAG_PORT_STATS) {
4012 stats = &bp->port_stats;
4013 rx_stats = stats->hw_stats;
4014 rx_masks = stats->hw_masks;
4015 rx_count = sizeof(struct rx_port_stats) / 8;
4016 tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4017 tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4018 tx_count = sizeof(struct tx_port_stats) / 8;
4020 flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
4021 rc = bnxt_hwrm_port_qstats(bp, flags);
4023 mask = (1ULL << 40) - 1;
4025 bnxt_fill_masks(rx_masks, mask, rx_count);
4026 bnxt_fill_masks(tx_masks, mask, tx_count);
4028 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4029 bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
4030 bnxt_hwrm_port_qstats(bp, 0);
4033 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
4034 stats = &bp->rx_port_stats_ext;
4035 rx_stats = stats->hw_stats;
4036 rx_masks = stats->hw_masks;
4037 rx_count = sizeof(struct rx_port_stats_ext) / 8;
4038 stats = &bp->tx_port_stats_ext;
4039 tx_stats = stats->hw_stats;
4040 tx_masks = stats->hw_masks;
4041 tx_count = sizeof(struct tx_port_stats_ext) / 8;
4043 flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4044 rc = bnxt_hwrm_port_qstats_ext(bp, flags);
4046 mask = (1ULL << 40) - 1;
4048 bnxt_fill_masks(rx_masks, mask, rx_count);
4050 bnxt_fill_masks(tx_masks, mask, tx_count);
4052 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4054 bnxt_copy_hw_masks(tx_masks, tx_stats,
4056 bnxt_hwrm_port_qstats_ext(bp, 0);
4061 static void bnxt_free_port_stats(struct bnxt *bp)
4063 bp->flags &= ~BNXT_FLAG_PORT_STATS;
4064 bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
4066 bnxt_free_stats_mem(bp, &bp->port_stats);
4067 bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
4068 bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
4071 static void bnxt_free_ring_stats(struct bnxt *bp)
4078 for (i = 0; i < bp->cp_nr_rings; i++) {
4079 struct bnxt_napi *bnapi = bp->bnapi[i];
4080 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4082 bnxt_free_stats_mem(bp, &cpr->stats);
4086 static int bnxt_alloc_stats(struct bnxt *bp)
4091 size = bp->hw_ring_stats_size;
4093 for (i = 0; i < bp->cp_nr_rings; i++) {
4094 struct bnxt_napi *bnapi = bp->bnapi[i];
4095 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4097 cpr->stats.len = size;
4098 rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
4102 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4105 if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
4108 if (bp->port_stats.hw_stats)
4109 goto alloc_ext_stats;
4111 bp->port_stats.len = BNXT_PORT_STATS_SIZE;
4112 rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
4116 bp->flags |= BNXT_FLAG_PORT_STATS;
4119 /* Display extended statistics only if FW supports it */
4120 if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
4121 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
4124 if (bp->rx_port_stats_ext.hw_stats)
4125 goto alloc_tx_ext_stats;
4127 bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
4128 rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
4129 /* Extended stats are optional */
4134 if (bp->tx_port_stats_ext.hw_stats)
4137 if (bp->hwrm_spec_code >= 0x10902 ||
4138 (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
4139 bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
4140 rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
4141 /* Extended stats are optional */
4145 bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
4149 static void bnxt_clear_ring_indices(struct bnxt *bp)
4156 for (i = 0; i < bp->cp_nr_rings; i++) {
4157 struct bnxt_napi *bnapi = bp->bnapi[i];
4158 struct bnxt_cp_ring_info *cpr;
4159 struct bnxt_rx_ring_info *rxr;
4160 struct bnxt_tx_ring_info *txr;
4165 cpr = &bnapi->cp_ring;
4166 cpr->cp_raw_cons = 0;
4168 txr = bnapi->tx_ring;
4174 rxr = bnapi->rx_ring;
4177 rxr->rx_agg_prod = 0;
4178 rxr->rx_sw_agg_prod = 0;
4179 rxr->rx_next_cons = 0;
4184 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
4186 #ifdef CONFIG_RFS_ACCEL
4189 /* Under rtnl_lock and all our NAPIs have been disabled. It's
4190 * safe to delete the hash table.
4192 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
4193 struct hlist_head *head;
4194 struct hlist_node *tmp;
4195 struct bnxt_ntuple_filter *fltr;
4197 head = &bp->ntp_fltr_hash_tbl[i];
4198 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
4199 hlist_del(&fltr->hash);
4204 kfree(bp->ntp_fltr_bmap);
4205 bp->ntp_fltr_bmap = NULL;
4207 bp->ntp_fltr_count = 0;
4211 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
4213 #ifdef CONFIG_RFS_ACCEL
4216 if (!(bp->flags & BNXT_FLAG_RFS))
4219 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
4220 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
4222 bp->ntp_fltr_count = 0;
4223 bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
4227 if (!bp->ntp_fltr_bmap)
4236 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
4238 bnxt_free_vnic_attributes(bp);
4239 bnxt_free_tx_rings(bp);
4240 bnxt_free_rx_rings(bp);
4241 bnxt_free_cp_rings(bp);
4242 bnxt_free_ntp_fltrs(bp, irq_re_init);
4244 bnxt_free_ring_stats(bp);
4245 if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
4246 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
4247 bnxt_free_port_stats(bp);
4248 bnxt_free_ring_grps(bp);
4249 bnxt_free_vnics(bp);
4250 kfree(bp->tx_ring_map);
4251 bp->tx_ring_map = NULL;
4259 bnxt_clear_ring_indices(bp);
4263 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
4265 int i, j, rc, size, arr_size;
4269 /* Allocate bnapi mem pointer array and mem block for
4272 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
4274 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
4275 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
4281 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
4282 bp->bnapi[i] = bnapi;
4283 bp->bnapi[i]->index = i;
4284 bp->bnapi[i]->bp = bp;
4285 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4286 struct bnxt_cp_ring_info *cpr =
4287 &bp->bnapi[i]->cp_ring;
4289 cpr->cp_ring_struct.ring_mem.flags =
4290 BNXT_RMEM_RING_PTE_FLAG;
4294 bp->rx_ring = kcalloc(bp->rx_nr_rings,
4295 sizeof(struct bnxt_rx_ring_info),
4300 for (i = 0; i < bp->rx_nr_rings; i++) {
4301 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4303 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4304 rxr->rx_ring_struct.ring_mem.flags =
4305 BNXT_RMEM_RING_PTE_FLAG;
4306 rxr->rx_agg_ring_struct.ring_mem.flags =
4307 BNXT_RMEM_RING_PTE_FLAG;
4309 rxr->bnapi = bp->bnapi[i];
4310 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
4313 bp->tx_ring = kcalloc(bp->tx_nr_rings,
4314 sizeof(struct bnxt_tx_ring_info),
4319 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
4322 if (!bp->tx_ring_map)
4325 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4328 j = bp->rx_nr_rings;
4330 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
4331 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4333 if (bp->flags & BNXT_FLAG_CHIP_P5)
4334 txr->tx_ring_struct.ring_mem.flags =
4335 BNXT_RMEM_RING_PTE_FLAG;
4336 txr->bnapi = bp->bnapi[j];
4337 bp->bnapi[j]->tx_ring = txr;
4338 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
4339 if (i >= bp->tx_nr_rings_xdp) {
4340 txr->txq_index = i - bp->tx_nr_rings_xdp;
4341 bp->bnapi[j]->tx_int = bnxt_tx_int;
4343 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
4344 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
4348 rc = bnxt_alloc_stats(bp);
4351 bnxt_init_stats(bp);
4353 rc = bnxt_alloc_ntp_fltrs(bp);
4357 rc = bnxt_alloc_vnics(bp);
4362 bnxt_init_ring_struct(bp);
4364 rc = bnxt_alloc_rx_rings(bp);
4368 rc = bnxt_alloc_tx_rings(bp);
4372 rc = bnxt_alloc_cp_rings(bp);
4376 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
4377 BNXT_VNIC_UCAST_FLAG;
4378 rc = bnxt_alloc_vnic_attributes(bp);
4384 bnxt_free_mem(bp, true);
4388 static void bnxt_disable_int(struct bnxt *bp)
4395 for (i = 0; i < bp->cp_nr_rings; i++) {
4396 struct bnxt_napi *bnapi = bp->bnapi[i];
4397 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4398 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4400 if (ring->fw_ring_id != INVALID_HW_RING_ID)
4401 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
4405 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
4407 struct bnxt_napi *bnapi = bp->bnapi[n];
4408 struct bnxt_cp_ring_info *cpr;
4410 cpr = &bnapi->cp_ring;
4411 return cpr->cp_ring_struct.map_idx;
4414 static void bnxt_disable_int_sync(struct bnxt *bp)
4421 atomic_inc(&bp->intr_sem);
4423 bnxt_disable_int(bp);
4424 for (i = 0; i < bp->cp_nr_rings; i++) {
4425 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
4427 synchronize_irq(bp->irq_tbl[map_idx].vector);
4431 static void bnxt_enable_int(struct bnxt *bp)
4435 atomic_set(&bp->intr_sem, 0);
4436 for (i = 0; i < bp->cp_nr_rings; i++) {
4437 struct bnxt_napi *bnapi = bp->bnapi[i];
4438 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4440 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
4444 void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
4445 u16 cmpl_ring, u16 target_id)
4447 struct input *req = request;
4449 req->req_type = cpu_to_le16(req_type);
4450 req->cmpl_ring = cpu_to_le16(cmpl_ring);
4451 req->target_id = cpu_to_le16(target_id);
4452 if (bnxt_kong_hwrm_message(bp, req))
4453 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_kong_resp_dma_addr);
4455 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
4458 static int bnxt_hwrm_to_stderr(u32 hwrm_err)
4461 case HWRM_ERR_CODE_SUCCESS:
4463 case HWRM_ERR_CODE_RESOURCE_LOCKED:
4465 case HWRM_ERR_CODE_RESOURCE_ACCESS_DENIED:
4467 case HWRM_ERR_CODE_RESOURCE_ALLOC_ERROR:
4469 case HWRM_ERR_CODE_INVALID_PARAMS:
4470 case HWRM_ERR_CODE_INVALID_FLAGS:
4471 case HWRM_ERR_CODE_INVALID_ENABLES:
4472 case HWRM_ERR_CODE_UNSUPPORTED_TLV:
4473 case HWRM_ERR_CODE_UNSUPPORTED_OPTION_ERR:
4475 case HWRM_ERR_CODE_NO_BUFFER:
4477 case HWRM_ERR_CODE_HOT_RESET_PROGRESS:
4478 case HWRM_ERR_CODE_BUSY:
4480 case HWRM_ERR_CODE_CMD_NOT_SUPPORTED:
4487 static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
4488 int timeout, bool silent)
4490 int i, intr_process, rc, tmo_count;
4491 struct input *req = msg;
4494 u16 cp_ring_id, len = 0;
4495 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
4496 u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
4497 struct hwrm_short_input short_input = {0};
4498 u32 doorbell_offset = BNXT_GRCPF_REG_CHIMP_COMM_TRIGGER;
4499 u32 bar_offset = BNXT_GRCPF_REG_CHIMP_COMM;
4500 u16 dst = BNXT_HWRM_CHNL_CHIMP;
4502 if (BNXT_NO_FW_ACCESS(bp) &&
4503 le16_to_cpu(req->req_type) != HWRM_FUNC_RESET)
4506 if (msg_len > BNXT_HWRM_MAX_REQ_LEN) {
4507 if (msg_len > bp->hwrm_max_ext_req_len ||
4508 !bp->hwrm_short_cmd_req_addr)
4512 if (bnxt_hwrm_kong_chnl(bp, req)) {
4513 dst = BNXT_HWRM_CHNL_KONG;
4514 bar_offset = BNXT_GRCPF_REG_KONG_COMM;
4515 doorbell_offset = BNXT_GRCPF_REG_KONG_COMM_TRIGGER;
4516 resp = bp->hwrm_cmd_kong_resp_addr;
4519 memset(resp, 0, PAGE_SIZE);
4520 cp_ring_id = le16_to_cpu(req->cmpl_ring);
4521 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
4523 req->seq_id = cpu_to_le16(bnxt_get_hwrm_seq_id(bp, dst));
4524 /* currently supports only one outstanding message */
4526 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
4528 if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
4529 msg_len > BNXT_HWRM_MAX_REQ_LEN) {
4530 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
4533 /* Set boundary for maximum extended request length for short
4534 * cmd format. If passed up from device use the max supported
4535 * internal req length.
4537 max_msg_len = bp->hwrm_max_ext_req_len;
4539 memcpy(short_cmd_req, req, msg_len);
4540 if (msg_len < max_msg_len)
4541 memset(short_cmd_req + msg_len, 0,
4542 max_msg_len - msg_len);
4544 short_input.req_type = req->req_type;
4545 short_input.signature =
4546 cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
4547 short_input.size = cpu_to_le16(msg_len);
4548 short_input.req_addr =
4549 cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
4551 data = (u32 *)&short_input;
4552 msg_len = sizeof(short_input);
4554 /* Sync memory write before updating doorbell */
4557 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
4560 /* Write request msg to hwrm channel */
4561 __iowrite32_copy(bp->bar0 + bar_offset, data, msg_len / 4);
4563 for (i = msg_len; i < max_req_len; i += 4)
4564 writel(0, bp->bar0 + bar_offset + i);
4566 /* Ring channel doorbell */
4567 writel(1, bp->bar0 + doorbell_offset);
4569 if (!pci_is_enabled(bp->pdev))
4573 timeout = DFLT_HWRM_CMD_TIMEOUT;
4574 /* Limit timeout to an upper limit */
4575 timeout = min(timeout, HWRM_CMD_MAX_TIMEOUT);
4576 /* convert timeout to usec */
4580 /* Short timeout for the first few iterations:
4581 * number of loops = number of loops for short timeout +
4582 * number of loops for standard timeout.
4584 tmo_count = HWRM_SHORT_TIMEOUT_COUNTER;
4585 timeout = timeout - HWRM_SHORT_MIN_TIMEOUT * HWRM_SHORT_TIMEOUT_COUNTER;
4586 tmo_count += DIV_ROUND_UP(timeout, HWRM_MIN_TIMEOUT);
4589 u16 seq_id = bp->hwrm_intr_seq_id;
4591 /* Wait until hwrm response cmpl interrupt is processed */
4592 while (bp->hwrm_intr_seq_id != (u16)~seq_id &&
4594 /* Abort the wait for completion if the FW health
4597 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
4599 /* on first few passes, just barely sleep */
4600 if (i < HWRM_SHORT_TIMEOUT_COUNTER) {
4601 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
4602 HWRM_SHORT_MAX_TIMEOUT);
4604 if (HWRM_WAIT_MUST_ABORT(bp, req))
4606 usleep_range(HWRM_MIN_TIMEOUT,
4611 if (bp->hwrm_intr_seq_id != (u16)~seq_id) {
4613 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
4614 le16_to_cpu(req->req_type));
4617 len = le16_to_cpu(resp->resp_len);
4618 valid = ((u8 *)resp) + len - 1;
4622 /* Check if response len is updated */
4623 for (i = 0; i < tmo_count; i++) {
4624 /* Abort the wait for completion if the FW health
4627 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
4629 len = le16_to_cpu(resp->resp_len);
4632 /* on first few passes, just barely sleep */
4633 if (i < HWRM_SHORT_TIMEOUT_COUNTER) {
4634 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
4635 HWRM_SHORT_MAX_TIMEOUT);
4637 if (HWRM_WAIT_MUST_ABORT(bp, req))
4639 usleep_range(HWRM_MIN_TIMEOUT,
4644 if (i >= tmo_count) {
4647 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
4648 HWRM_TOTAL_TIMEOUT(i),
4649 le16_to_cpu(req->req_type),
4650 le16_to_cpu(req->seq_id), len);
4654 /* Last byte of resp contains valid bit */
4655 valid = ((u8 *)resp) + len - 1;
4656 for (j = 0; j < HWRM_VALID_BIT_DELAY_USEC; j++) {
4657 /* make sure we read from updated DMA memory */
4664 if (j >= HWRM_VALID_BIT_DELAY_USEC) {
4666 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
4667 HWRM_TOTAL_TIMEOUT(i),
4668 le16_to_cpu(req->req_type),
4669 le16_to_cpu(req->seq_id), len,
4675 /* Zero valid bit for compatibility. Valid bit in an older spec
4676 * may become a new field in a newer spec. We must make sure that
4677 * a new field not implemented by old spec will read zero.
4680 rc = le16_to_cpu(resp->error_code);
4682 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
4683 le16_to_cpu(resp->req_type),
4684 le16_to_cpu(resp->seq_id), rc);
4685 return bnxt_hwrm_to_stderr(rc);
4688 int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
4690 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
4693 int _hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
4696 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
4699 int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
4703 mutex_lock(&bp->hwrm_cmd_lock);
4704 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
4705 mutex_unlock(&bp->hwrm_cmd_lock);
4709 int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
4714 mutex_lock(&bp->hwrm_cmd_lock);
4715 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
4716 mutex_unlock(&bp->hwrm_cmd_lock);
4720 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
4723 struct hwrm_func_drv_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
4724 struct hwrm_func_drv_rgtr_input req = {0};
4725 DECLARE_BITMAP(async_events_bmap, 256);
4726 u32 *events = (u32 *)async_events_bmap;
4730 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
4733 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
4734 FUNC_DRV_RGTR_REQ_ENABLES_VER |
4735 FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4737 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
4738 flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
4739 if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
4740 flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
4741 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
4742 flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
4743 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
4744 req.flags = cpu_to_le32(flags);
4745 req.ver_maj_8b = DRV_VER_MAJ;
4746 req.ver_min_8b = DRV_VER_MIN;
4747 req.ver_upd_8b = DRV_VER_UPD;
4748 req.ver_maj = cpu_to_le16(DRV_VER_MAJ);
4749 req.ver_min = cpu_to_le16(DRV_VER_MIN);
4750 req.ver_upd = cpu_to_le16(DRV_VER_UPD);
4756 memset(data, 0, sizeof(data));
4757 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
4758 u16 cmd = bnxt_vf_req_snif[i];
4759 unsigned int bit, idx;
4763 data[idx] |= 1 << bit;
4766 for (i = 0; i < 8; i++)
4767 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
4770 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
4773 if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
4774 req.flags |= cpu_to_le32(
4775 FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
4777 memset(async_events_bmap, 0, sizeof(async_events_bmap));
4778 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
4779 u16 event_id = bnxt_async_events_arr[i];
4781 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
4782 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
4784 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
4786 if (bmap && bmap_size) {
4787 for (i = 0; i < bmap_size; i++) {
4788 if (test_bit(i, bmap))
4789 __set_bit(i, async_events_bmap);
4792 for (i = 0; i < 8; i++)
4793 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
4797 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4799 mutex_lock(&bp->hwrm_cmd_lock);
4800 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4802 set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
4804 cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
4805 bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
4807 mutex_unlock(&bp->hwrm_cmd_lock);
4811 static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
4813 struct hwrm_func_drv_unrgtr_input req = {0};
4815 if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
4818 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
4819 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4822 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
4825 struct hwrm_tunnel_dst_port_free_input req = {0};
4827 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
4828 req.tunnel_type = tunnel_type;
4830 switch (tunnel_type) {
4831 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
4832 req.tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
4833 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
4835 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
4836 req.tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
4837 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
4843 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4845 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
4850 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
4854 struct hwrm_tunnel_dst_port_alloc_input req = {0};
4855 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4857 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
4859 req.tunnel_type = tunnel_type;
4860 req.tunnel_dst_port_val = port;
4862 mutex_lock(&bp->hwrm_cmd_lock);
4863 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4865 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
4870 switch (tunnel_type) {
4871 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
4872 bp->vxlan_fw_dst_port_id =
4873 le16_to_cpu(resp->tunnel_dst_port_id);
4875 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
4876 bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
4883 mutex_unlock(&bp->hwrm_cmd_lock);
4887 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
4889 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
4890 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4892 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
4893 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4895 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
4896 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
4897 req.mask = cpu_to_le32(vnic->rx_mask);
4898 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4901 #ifdef CONFIG_RFS_ACCEL
4902 static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
4903 struct bnxt_ntuple_filter *fltr)
4905 struct hwrm_cfa_ntuple_filter_free_input req = {0};
4907 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
4908 req.ntuple_filter_id = fltr->filter_id;
4909 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4912 #define BNXT_NTP_FLTR_FLAGS \
4913 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
4914 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
4915 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
4916 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
4917 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
4918 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
4919 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
4920 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
4921 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
4922 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
4923 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
4924 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
4925 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
4926 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
4928 #define BNXT_NTP_TUNNEL_FLTR_FLAG \
4929 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
4931 static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
4932 struct bnxt_ntuple_filter *fltr)
4934 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
4935 struct hwrm_cfa_ntuple_filter_alloc_output *resp;
4936 struct flow_keys *keys = &fltr->fkeys;
4937 struct bnxt_vnic_info *vnic;
4941 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
4942 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
4944 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
4945 flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
4946 req.dst_id = cpu_to_le16(fltr->rxq);
4948 vnic = &bp->vnic_info[fltr->rxq + 1];
4949 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
4951 req.flags = cpu_to_le32(flags);
4952 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
4954 req.ethertype = htons(ETH_P_IP);
4955 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
4956 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
4957 req.ip_protocol = keys->basic.ip_proto;
4959 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
4962 req.ethertype = htons(ETH_P_IPV6);
4964 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
4965 *(struct in6_addr *)&req.src_ipaddr[0] =
4966 keys->addrs.v6addrs.src;
4967 *(struct in6_addr *)&req.dst_ipaddr[0] =
4968 keys->addrs.v6addrs.dst;
4969 for (i = 0; i < 4; i++) {
4970 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4971 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4974 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
4975 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4976 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
4977 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4979 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
4980 req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
4982 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
4985 req.src_port = keys->ports.src;
4986 req.src_port_mask = cpu_to_be16(0xffff);
4987 req.dst_port = keys->ports.dst;
4988 req.dst_port_mask = cpu_to_be16(0xffff);
4990 mutex_lock(&bp->hwrm_cmd_lock);
4991 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4993 resp = bnxt_get_hwrm_resp_addr(bp, &req);
4994 fltr->filter_id = resp->ntuple_filter_id;
4996 mutex_unlock(&bp->hwrm_cmd_lock);
5001 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
5005 struct hwrm_cfa_l2_filter_alloc_input req = {0};
5006 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5008 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
5009 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
5010 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
5012 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
5013 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
5015 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
5016 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
5017 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
5018 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
5019 req.l2_addr_mask[0] = 0xff;
5020 req.l2_addr_mask[1] = 0xff;
5021 req.l2_addr_mask[2] = 0xff;
5022 req.l2_addr_mask[3] = 0xff;
5023 req.l2_addr_mask[4] = 0xff;
5024 req.l2_addr_mask[5] = 0xff;
5026 mutex_lock(&bp->hwrm_cmd_lock);
5027 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5029 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
5031 mutex_unlock(&bp->hwrm_cmd_lock);
5035 static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
5037 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
5040 /* Any associated ntuple filters will also be cleared by firmware. */
5041 mutex_lock(&bp->hwrm_cmd_lock);
5042 for (i = 0; i < num_of_vnics; i++) {
5043 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
5045 for (j = 0; j < vnic->uc_filter_count; j++) {
5046 struct hwrm_cfa_l2_filter_free_input req = {0};
5048 bnxt_hwrm_cmd_hdr_init(bp, &req,
5049 HWRM_CFA_L2_FILTER_FREE, -1, -1);
5051 req.l2_filter_id = vnic->fw_l2_filter_id[j];
5053 rc = _hwrm_send_message(bp, &req, sizeof(req),
5056 vnic->uc_filter_count = 0;
5058 mutex_unlock(&bp->hwrm_cmd_lock);
5063 static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
5065 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5066 u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
5067 struct hwrm_vnic_tpa_cfg_input req = {0};
5069 if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
5072 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
5075 u16 mss = bp->dev->mtu - 40;
5076 u32 nsegs, n, segs = 0, flags;
5078 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
5079 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
5080 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
5081 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
5082 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
5083 if (tpa_flags & BNXT_FLAG_GRO)
5084 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
5086 req.flags = cpu_to_le32(flags);
5089 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
5090 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
5091 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
5093 /* Number of segs are log2 units, and first packet is not
5094 * included as part of this units.
5096 if (mss <= BNXT_RX_PAGE_SIZE) {
5097 n = BNXT_RX_PAGE_SIZE / mss;
5098 nsegs = (MAX_SKB_FRAGS - 1) * n;
5100 n = mss / BNXT_RX_PAGE_SIZE;
5101 if (mss & (BNXT_RX_PAGE_SIZE - 1))
5103 nsegs = (MAX_SKB_FRAGS - n) / n;
5106 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5107 segs = MAX_TPA_SEGS_P5;
5108 max_aggs = bp->max_tpa;
5110 segs = ilog2(nsegs);
5112 req.max_agg_segs = cpu_to_le16(segs);
5113 req.max_aggs = cpu_to_le16(max_aggs);
5115 req.min_agg_len = cpu_to_le32(512);
5117 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5119 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5122 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
5124 struct bnxt_ring_grp_info *grp_info;
5126 grp_info = &bp->grp_info[ring->grp_idx];
5127 return grp_info->cp_fw_ring_id;
5130 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
5132 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5133 struct bnxt_napi *bnapi = rxr->bnapi;
5134 struct bnxt_cp_ring_info *cpr;
5136 cpr = bnapi->cp_ring.cp_ring_arr[BNXT_RX_HDL];
5137 return cpr->cp_ring_struct.fw_ring_id;
5139 return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
5143 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
5145 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5146 struct bnxt_napi *bnapi = txr->bnapi;
5147 struct bnxt_cp_ring_info *cpr;
5149 cpr = bnapi->cp_ring.cp_ring_arr[BNXT_TX_HDL];
5150 return cpr->cp_ring_struct.fw_ring_id;
5152 return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
5156 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
5160 if (bp->flags & BNXT_FLAG_CHIP_P5)
5161 entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
5163 entries = HW_HASH_INDEX_SIZE;
5165 bp->rss_indir_tbl_entries = entries;
5166 bp->rss_indir_tbl = kmalloc_array(entries, sizeof(*bp->rss_indir_tbl),
5168 if (!bp->rss_indir_tbl)
5173 static void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp)
5175 u16 max_rings, max_entries, pad, i;
5177 if (!bp->rx_nr_rings)
5180 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5181 max_rings = bp->rx_nr_rings - 1;
5183 max_rings = bp->rx_nr_rings;
5185 max_entries = bnxt_get_rxfh_indir_size(bp->dev);
5187 for (i = 0; i < max_entries; i++)
5188 bp->rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
5190 pad = bp->rss_indir_tbl_entries - max_entries;
5192 memset(&bp->rss_indir_tbl[i], 0, pad * sizeof(u16));
5195 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
5197 u16 i, tbl_size, max_ring = 0;
5199 if (!bp->rss_indir_tbl)
5202 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
5203 for (i = 0; i < tbl_size; i++)
5204 max_ring = max(max_ring, bp->rss_indir_tbl[i]);
5208 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
5210 if (bp->flags & BNXT_FLAG_CHIP_P5)
5211 return DIV_ROUND_UP(rx_rings, BNXT_RSS_TABLE_ENTRIES_P5);
5212 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5217 static void __bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
5219 bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
5222 /* Fill the RSS indirection table with ring group ids */
5223 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
5225 j = bp->rss_indir_tbl[i];
5226 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
5230 static void __bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
5231 struct bnxt_vnic_info *vnic)
5233 __le16 *ring_tbl = vnic->rss_table;
5234 struct bnxt_rx_ring_info *rxr;
5237 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
5239 for (i = 0; i < tbl_size; i++) {
5242 j = bp->rss_indir_tbl[i];
5243 rxr = &bp->rx_ring[j];
5245 ring_id = rxr->rx_ring_struct.fw_ring_id;
5246 *ring_tbl++ = cpu_to_le16(ring_id);
5247 ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5248 *ring_tbl++ = cpu_to_le16(ring_id);
5252 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
5254 if (bp->flags & BNXT_FLAG_CHIP_P5)
5255 __bnxt_fill_hw_rss_tbl_p5(bp, vnic);
5257 __bnxt_fill_hw_rss_tbl(bp, vnic);
5260 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
5262 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5263 struct hwrm_vnic_rss_cfg_input req = {0};
5265 if ((bp->flags & BNXT_FLAG_CHIP_P5) ||
5266 vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
5269 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
5271 bnxt_fill_hw_rss_tbl(bp, vnic);
5272 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
5273 req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
5274 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
5275 req.hash_key_tbl_addr =
5276 cpu_to_le64(vnic->rss_hash_key_dma_addr);
5278 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5279 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5282 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, u16 vnic_id, bool set_rss)
5284 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5285 struct hwrm_vnic_rss_cfg_input req = {0};
5286 dma_addr_t ring_tbl_map;
5289 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
5290 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5292 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5295 bnxt_fill_hw_rss_tbl(bp, vnic);
5296 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
5297 req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
5298 req.hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
5299 ring_tbl_map = vnic->rss_table_dma_addr;
5300 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
5301 for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
5304 req.ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
5305 req.ring_table_pair_index = i;
5306 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
5307 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5314 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
5316 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5317 struct hwrm_vnic_plcmodes_cfg_input req = {0};
5319 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
5320 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
5321 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
5322 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
5324 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
5325 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
5326 /* thresholds not implemented in firmware yet */
5327 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
5328 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
5329 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5330 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5333 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
5336 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
5338 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
5339 req.rss_cos_lb_ctx_id =
5340 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
5342 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5343 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
5346 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
5350 for (i = 0; i < bp->nr_vnics; i++) {
5351 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
5353 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
5354 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
5355 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
5358 bp->rsscos_nr_ctxs = 0;
5361 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
5364 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
5365 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
5366 bp->hwrm_cmd_resp_addr;
5368 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
5371 mutex_lock(&bp->hwrm_cmd_lock);
5372 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5374 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
5375 le16_to_cpu(resp->rss_cos_lb_ctx_id);
5376 mutex_unlock(&bp->hwrm_cmd_lock);
5381 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
5383 if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
5384 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
5385 return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
5388 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
5390 unsigned int ring = 0, grp_idx;
5391 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5392 struct hwrm_vnic_cfg_input req = {0};
5395 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
5397 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5398 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
5400 req.default_rx_ring_id =
5401 cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
5402 req.default_cmpl_ring_id =
5403 cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
5405 cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
5406 VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
5409 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
5410 /* Only RSS support for now TBD: COS & LB */
5411 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
5412 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5413 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5414 VNIC_CFG_REQ_ENABLES_MRU);
5415 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
5417 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
5418 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5419 VNIC_CFG_REQ_ENABLES_MRU);
5420 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
5422 req.rss_rule = cpu_to_le16(0xffff);
5425 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
5426 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
5427 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
5428 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
5430 req.cos_rule = cpu_to_le16(0xffff);
5433 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
5435 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
5437 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
5438 ring = bp->rx_nr_rings - 1;
5440 grp_idx = bp->rx_ring[ring].bnapi->index;
5441 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
5442 req.lb_rule = cpu_to_le16(0xffff);
5444 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + VLAN_HLEN);
5446 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5447 #ifdef CONFIG_BNXT_SRIOV
5449 def_vlan = bp->vf.vlan;
5451 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
5452 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
5453 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
5454 req.flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
5456 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5459 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
5461 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
5462 struct hwrm_vnic_free_input req = {0};
5464 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
5466 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
5468 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5469 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
5473 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
5477 for (i = 0; i < bp->nr_vnics; i++)
5478 bnxt_hwrm_vnic_free_one(bp, i);
5481 static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
5482 unsigned int start_rx_ring_idx,
5483 unsigned int nr_rings)
5486 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
5487 struct hwrm_vnic_alloc_input req = {0};
5488 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5489 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5491 if (bp->flags & BNXT_FLAG_CHIP_P5)
5492 goto vnic_no_ring_grps;
5494 /* map ring groups to this vnic */
5495 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
5496 grp_idx = bp->rx_ring[i].bnapi->index;
5497 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
5498 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
5502 vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
5506 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
5507 vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
5509 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
5511 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
5513 mutex_lock(&bp->hwrm_cmd_lock);
5514 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5516 vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
5517 mutex_unlock(&bp->hwrm_cmd_lock);
5521 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
5523 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5524 struct hwrm_vnic_qcaps_input req = {0};
5527 bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
5528 bp->flags &= ~(BNXT_FLAG_NEW_RSS_CAP | BNXT_FLAG_ROCE_MIRROR_CAP);
5529 if (bp->hwrm_spec_code < 0x10600)
5532 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
5533 mutex_lock(&bp->hwrm_cmd_lock);
5534 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5536 u32 flags = le32_to_cpu(resp->flags);
5538 if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
5539 (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
5540 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
5542 VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
5543 bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
5545 /* Older P5 fw before EXT_HW_STATS support did not set
5546 * VLAN_STRIP_CAP properly.
5548 if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
5549 (BNXT_CHIP_P5_THOR(bp) &&
5550 !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
5551 bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
5552 bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
5553 if (bp->max_tpa_v2) {
5554 if (BNXT_CHIP_P5_THOR(bp))
5555 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
5557 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5_SR2;
5560 mutex_unlock(&bp->hwrm_cmd_lock);
5564 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
5569 if (bp->flags & BNXT_FLAG_CHIP_P5)
5572 mutex_lock(&bp->hwrm_cmd_lock);
5573 for (i = 0; i < bp->rx_nr_rings; i++) {
5574 struct hwrm_ring_grp_alloc_input req = {0};
5575 struct hwrm_ring_grp_alloc_output *resp =
5576 bp->hwrm_cmd_resp_addr;
5577 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
5579 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
5581 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
5582 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
5583 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
5584 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
5586 rc = _hwrm_send_message(bp, &req, sizeof(req),
5591 bp->grp_info[grp_idx].fw_grp_id =
5592 le32_to_cpu(resp->ring_group_id);
5594 mutex_unlock(&bp->hwrm_cmd_lock);
5598 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
5601 struct hwrm_ring_grp_free_input req = {0};
5603 if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5))
5606 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
5608 mutex_lock(&bp->hwrm_cmd_lock);
5609 for (i = 0; i < bp->cp_nr_rings; i++) {
5610 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
5613 cpu_to_le32(bp->grp_info[i].fw_grp_id);
5615 _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5616 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
5618 mutex_unlock(&bp->hwrm_cmd_lock);
5621 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
5622 struct bnxt_ring_struct *ring,
5623 u32 ring_type, u32 map_index)
5625 int rc = 0, err = 0;
5626 struct hwrm_ring_alloc_input req = {0};
5627 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5628 struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
5629 struct bnxt_ring_grp_info *grp_info;
5632 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
5635 if (rmem->nr_pages > 1) {
5636 req.page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
5637 /* Page size is in log2 units */
5638 req.page_size = BNXT_PAGE_SHIFT;
5639 req.page_tbl_depth = 1;
5641 req.page_tbl_addr = cpu_to_le64(rmem->dma_arr[0]);
5644 /* Association of ring index with doorbell index and MSIX number */
5645 req.logical_id = cpu_to_le16(map_index);
5647 switch (ring_type) {
5648 case HWRM_RING_ALLOC_TX: {
5649 struct bnxt_tx_ring_info *txr;
5651 txr = container_of(ring, struct bnxt_tx_ring_info,
5653 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
5654 /* Association of transmit ring with completion ring */
5655 grp_info = &bp->grp_info[ring->grp_idx];
5656 req.cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
5657 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
5658 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5659 req.queue_id = cpu_to_le16(ring->queue_id);
5662 case HWRM_RING_ALLOC_RX:
5663 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5664 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
5665 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5668 /* Association of rx ring with stats context */
5669 grp_info = &bp->grp_info[ring->grp_idx];
5670 req.rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
5671 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5672 req.enables |= cpu_to_le32(
5673 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5674 if (NET_IP_ALIGN == 2)
5675 flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
5676 req.flags = cpu_to_le16(flags);
5679 case HWRM_RING_ALLOC_AGG:
5680 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5681 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
5682 /* Association of agg ring with rx ring */
5683 grp_info = &bp->grp_info[ring->grp_idx];
5684 req.rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
5685 req.rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
5686 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5687 req.enables |= cpu_to_le32(
5688 RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
5689 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5691 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5693 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
5695 case HWRM_RING_ALLOC_CMPL:
5696 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
5697 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
5698 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5699 /* Association of cp ring with nq */
5700 grp_info = &bp->grp_info[map_index];
5701 req.nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
5702 req.cq_handle = cpu_to_le64(ring->handle);
5703 req.enables |= cpu_to_le32(
5704 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
5705 } else if (bp->flags & BNXT_FLAG_USING_MSIX) {
5706 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5709 case HWRM_RING_ALLOC_NQ:
5710 req.ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
5711 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
5712 if (bp->flags & BNXT_FLAG_USING_MSIX)
5713 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5716 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
5721 mutex_lock(&bp->hwrm_cmd_lock);
5722 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5723 err = le16_to_cpu(resp->error_code);
5724 ring_id = le16_to_cpu(resp->ring_id);
5725 mutex_unlock(&bp->hwrm_cmd_lock);
5728 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
5729 ring_type, rc, err);
5732 ring->fw_ring_id = ring_id;
5736 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
5741 struct hwrm_func_cfg_input req = {0};
5743 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
5744 req.fid = cpu_to_le16(0xffff);
5745 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5746 req.async_event_cr = cpu_to_le16(idx);
5747 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5749 struct hwrm_func_vf_cfg_input req = {0};
5751 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
5753 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5754 req.async_event_cr = cpu_to_le16(idx);
5755 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5760 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
5761 u32 map_idx, u32 xid)
5763 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5765 db->doorbell = bp->bar1 + DB_PF_OFFSET_P5;
5767 db->doorbell = bp->bar1 + DB_VF_OFFSET_P5;
5768 switch (ring_type) {
5769 case HWRM_RING_ALLOC_TX:
5770 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
5772 case HWRM_RING_ALLOC_RX:
5773 case HWRM_RING_ALLOC_AGG:
5774 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
5776 case HWRM_RING_ALLOC_CMPL:
5777 db->db_key64 = DBR_PATH_L2;
5779 case HWRM_RING_ALLOC_NQ:
5780 db->db_key64 = DBR_PATH_L2;
5783 db->db_key64 |= (u64)xid << DBR_XID_SFT;
5785 db->doorbell = bp->bar1 + map_idx * 0x80;
5786 switch (ring_type) {
5787 case HWRM_RING_ALLOC_TX:
5788 db->db_key32 = DB_KEY_TX;
5790 case HWRM_RING_ALLOC_RX:
5791 case HWRM_RING_ALLOC_AGG:
5792 db->db_key32 = DB_KEY_RX;
5794 case HWRM_RING_ALLOC_CMPL:
5795 db->db_key32 = DB_KEY_CP;
5801 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
5803 bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
5807 if (bp->flags & BNXT_FLAG_CHIP_P5)
5808 type = HWRM_RING_ALLOC_NQ;
5810 type = HWRM_RING_ALLOC_CMPL;
5811 for (i = 0; i < bp->cp_nr_rings; i++) {
5812 struct bnxt_napi *bnapi = bp->bnapi[i];
5813 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5814 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5815 u32 map_idx = ring->map_idx;
5816 unsigned int vector;
5818 vector = bp->irq_tbl[map_idx].vector;
5819 disable_irq_nosync(vector);
5820 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5825 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
5826 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5828 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
5831 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
5833 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
5837 type = HWRM_RING_ALLOC_TX;
5838 for (i = 0; i < bp->tx_nr_rings; i++) {
5839 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5840 struct bnxt_ring_struct *ring;
5843 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5844 struct bnxt_napi *bnapi = txr->bnapi;
5845 struct bnxt_cp_ring_info *cpr, *cpr2;
5846 u32 type2 = HWRM_RING_ALLOC_CMPL;
5848 cpr = &bnapi->cp_ring;
5849 cpr2 = cpr->cp_ring_arr[BNXT_TX_HDL];
5850 ring = &cpr2->cp_ring_struct;
5851 ring->handle = BNXT_TX_HDL;
5852 map_idx = bnapi->index;
5853 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5856 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5858 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5860 ring = &txr->tx_ring_struct;
5862 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5865 bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
5868 type = HWRM_RING_ALLOC_RX;
5869 for (i = 0; i < bp->rx_nr_rings; i++) {
5870 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5871 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5872 struct bnxt_napi *bnapi = rxr->bnapi;
5873 u32 map_idx = bnapi->index;
5875 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5878 bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
5879 /* If we have agg rings, post agg buffers first. */
5881 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
5882 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
5883 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5884 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5885 u32 type2 = HWRM_RING_ALLOC_CMPL;
5886 struct bnxt_cp_ring_info *cpr2;
5888 cpr2 = cpr->cp_ring_arr[BNXT_RX_HDL];
5889 ring = &cpr2->cp_ring_struct;
5890 ring->handle = BNXT_RX_HDL;
5891 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5894 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5896 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5901 type = HWRM_RING_ALLOC_AGG;
5902 for (i = 0; i < bp->rx_nr_rings; i++) {
5903 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5904 struct bnxt_ring_struct *ring =
5905 &rxr->rx_agg_ring_struct;
5906 u32 grp_idx = ring->grp_idx;
5907 u32 map_idx = grp_idx + bp->rx_nr_rings;
5909 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5913 bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
5915 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
5916 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
5917 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
5924 static int hwrm_ring_free_send_msg(struct bnxt *bp,
5925 struct bnxt_ring_struct *ring,
5926 u32 ring_type, int cmpl_ring_id)
5929 struct hwrm_ring_free_input req = {0};
5930 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
5933 if (BNXT_NO_FW_ACCESS(bp))
5936 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
5937 req.ring_type = ring_type;
5938 req.ring_id = cpu_to_le16(ring->fw_ring_id);
5940 mutex_lock(&bp->hwrm_cmd_lock);
5941 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5942 error_code = le16_to_cpu(resp->error_code);
5943 mutex_unlock(&bp->hwrm_cmd_lock);
5945 if (rc || error_code) {
5946 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
5947 ring_type, rc, error_code);
5953 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
5961 for (i = 0; i < bp->tx_nr_rings; i++) {
5962 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5963 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
5965 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5966 u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
5968 hwrm_ring_free_send_msg(bp, ring,
5969 RING_FREE_REQ_RING_TYPE_TX,
5970 close_path ? cmpl_ring_id :
5971 INVALID_HW_RING_ID);
5972 ring->fw_ring_id = INVALID_HW_RING_ID;
5976 for (i = 0; i < bp->rx_nr_rings; i++) {
5977 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5978 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5979 u32 grp_idx = rxr->bnapi->index;
5981 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5982 u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5984 hwrm_ring_free_send_msg(bp, ring,
5985 RING_FREE_REQ_RING_TYPE_RX,
5986 close_path ? cmpl_ring_id :
5987 INVALID_HW_RING_ID);
5988 ring->fw_ring_id = INVALID_HW_RING_ID;
5989 bp->grp_info[grp_idx].rx_fw_ring_id =
5994 if (bp->flags & BNXT_FLAG_CHIP_P5)
5995 type = RING_FREE_REQ_RING_TYPE_RX_AGG;
5997 type = RING_FREE_REQ_RING_TYPE_RX;
5998 for (i = 0; i < bp->rx_nr_rings; i++) {
5999 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6000 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
6001 u32 grp_idx = rxr->bnapi->index;
6003 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6004 u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6006 hwrm_ring_free_send_msg(bp, ring, type,
6007 close_path ? cmpl_ring_id :
6008 INVALID_HW_RING_ID);
6009 ring->fw_ring_id = INVALID_HW_RING_ID;
6010 bp->grp_info[grp_idx].agg_fw_ring_id =
6015 /* The completion rings are about to be freed. After that the
6016 * IRQ doorbell will not work anymore. So we need to disable
6019 bnxt_disable_int_sync(bp);
6021 if (bp->flags & BNXT_FLAG_CHIP_P5)
6022 type = RING_FREE_REQ_RING_TYPE_NQ;
6024 type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
6025 for (i = 0; i < bp->cp_nr_rings; i++) {
6026 struct bnxt_napi *bnapi = bp->bnapi[i];
6027 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6028 struct bnxt_ring_struct *ring;
6031 for (j = 0; j < 2; j++) {
6032 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
6035 ring = &cpr2->cp_ring_struct;
6036 if (ring->fw_ring_id == INVALID_HW_RING_ID)
6038 hwrm_ring_free_send_msg(bp, ring,
6039 RING_FREE_REQ_RING_TYPE_L2_CMPL,
6040 INVALID_HW_RING_ID);
6041 ring->fw_ring_id = INVALID_HW_RING_ID;
6044 ring = &cpr->cp_ring_struct;
6045 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6046 hwrm_ring_free_send_msg(bp, ring, type,
6047 INVALID_HW_RING_ID);
6048 ring->fw_ring_id = INVALID_HW_RING_ID;
6049 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
6054 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
6057 static int bnxt_hwrm_get_rings(struct bnxt *bp)
6059 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6060 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6061 struct hwrm_func_qcfg_input req = {0};
6064 if (bp->hwrm_spec_code < 0x10601)
6067 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
6068 req.fid = cpu_to_le16(0xffff);
6069 mutex_lock(&bp->hwrm_cmd_lock);
6070 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6072 mutex_unlock(&bp->hwrm_cmd_lock);
6076 hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
6077 if (BNXT_NEW_RM(bp)) {
6080 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
6081 hw_resc->resv_hw_ring_grps =
6082 le32_to_cpu(resp->alloc_hw_ring_grps);
6083 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
6084 cp = le16_to_cpu(resp->alloc_cmpl_rings);
6085 stats = le16_to_cpu(resp->alloc_stat_ctx);
6086 hw_resc->resv_irqs = cp;
6087 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6088 int rx = hw_resc->resv_rx_rings;
6089 int tx = hw_resc->resv_tx_rings;
6091 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6093 if (cp < (rx + tx)) {
6094 bnxt_trim_rings(bp, &rx, &tx, cp, false);
6095 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6097 hw_resc->resv_rx_rings = rx;
6098 hw_resc->resv_tx_rings = tx;
6100 hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
6101 hw_resc->resv_hw_ring_grps = rx;
6103 hw_resc->resv_cp_rings = cp;
6104 hw_resc->resv_stat_ctxs = stats;
6106 mutex_unlock(&bp->hwrm_cmd_lock);
6110 /* Caller must hold bp->hwrm_cmd_lock */
6111 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
6113 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6114 struct hwrm_func_qcfg_input req = {0};
6117 if (bp->hwrm_spec_code < 0x10601)
6120 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
6121 req.fid = cpu_to_le16(fid);
6122 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6124 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
6129 static bool bnxt_rfs_supported(struct bnxt *bp);
6132 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct hwrm_func_cfg_input *req,
6133 int tx_rings, int rx_rings, int ring_grps,
6134 int cp_rings, int stats, int vnics)
6138 bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_CFG, -1, -1);
6139 req->fid = cpu_to_le16(0xffff);
6140 enables |= tx_rings ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
6141 req->num_tx_rings = cpu_to_le16(tx_rings);
6142 if (BNXT_NEW_RM(bp)) {
6143 enables |= rx_rings ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
6144 enables |= stats ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
6145 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6146 enables |= cp_rings ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
6147 enables |= tx_rings + ring_grps ?
6148 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6149 enables |= rx_rings ?
6150 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6152 enables |= cp_rings ?
6153 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6154 enables |= ring_grps ?
6155 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS |
6156 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6158 enables |= vnics ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
6160 req->num_rx_rings = cpu_to_le16(rx_rings);
6161 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6162 req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
6163 req->num_msix = cpu_to_le16(cp_rings);
6164 req->num_rsscos_ctxs =
6165 cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
6167 req->num_cmpl_rings = cpu_to_le16(cp_rings);
6168 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
6169 req->num_rsscos_ctxs = cpu_to_le16(1);
6170 if (!(bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
6171 bnxt_rfs_supported(bp))
6172 req->num_rsscos_ctxs =
6173 cpu_to_le16(ring_grps + 1);
6175 req->num_stat_ctxs = cpu_to_le16(stats);
6176 req->num_vnics = cpu_to_le16(vnics);
6178 req->enables = cpu_to_le32(enables);
6182 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp,
6183 struct hwrm_func_vf_cfg_input *req, int tx_rings,
6184 int rx_rings, int ring_grps, int cp_rings,
6185 int stats, int vnics)
6189 bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_VF_CFG, -1, -1);
6190 enables |= tx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
6191 enables |= rx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
6192 FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6193 enables |= stats ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
6194 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6195 enables |= tx_rings + ring_grps ?
6196 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6198 enables |= cp_rings ?
6199 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6200 enables |= ring_grps ?
6201 FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
6203 enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
6204 enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
6206 req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
6207 req->num_tx_rings = cpu_to_le16(tx_rings);
6208 req->num_rx_rings = cpu_to_le16(rx_rings);
6209 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6210 req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
6211 req->num_rsscos_ctxs = cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
6213 req->num_cmpl_rings = cpu_to_le16(cp_rings);
6214 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
6215 req->num_rsscos_ctxs = cpu_to_le16(BNXT_VF_MAX_RSS_CTX);
6217 req->num_stat_ctxs = cpu_to_le16(stats);
6218 req->num_vnics = cpu_to_le16(vnics);
6220 req->enables = cpu_to_le32(enables);
6224 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6225 int ring_grps, int cp_rings, int stats, int vnics)
6227 struct hwrm_func_cfg_input req = {0};
6230 __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6231 cp_rings, stats, vnics);
6235 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6239 if (bp->hwrm_spec_code < 0x10601)
6240 bp->hw_resc.resv_tx_rings = tx_rings;
6242 return bnxt_hwrm_get_rings(bp);
6246 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6247 int ring_grps, int cp_rings, int stats, int vnics)
6249 struct hwrm_func_vf_cfg_input req = {0};
6252 if (!BNXT_NEW_RM(bp)) {
6253 bp->hw_resc.resv_tx_rings = tx_rings;
6257 __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6258 cp_rings, stats, vnics);
6259 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6263 return bnxt_hwrm_get_rings(bp);
6266 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, int tx, int rx, int grp,
6267 int cp, int stat, int vnic)
6270 return bnxt_hwrm_reserve_pf_rings(bp, tx, rx, grp, cp, stat,
6273 return bnxt_hwrm_reserve_vf_rings(bp, tx, rx, grp, cp, stat,
6277 int bnxt_nq_rings_in_use(struct bnxt *bp)
6279 int cp = bp->cp_nr_rings;
6280 int ulp_msix, ulp_base;
6282 ulp_msix = bnxt_get_ulp_msix_num(bp);
6284 ulp_base = bnxt_get_ulp_msix_base(bp);
6286 if ((ulp_base + ulp_msix) > cp)
6287 cp = ulp_base + ulp_msix;
6292 static int bnxt_cp_rings_in_use(struct bnxt *bp)
6296 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6297 return bnxt_nq_rings_in_use(bp);
6299 cp = bp->tx_nr_rings + bp->rx_nr_rings;
6303 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
6305 int ulp_stat = bnxt_get_ulp_stat_ctxs(bp);
6306 int cp = bp->cp_nr_rings;
6311 if (bnxt_nq_rings_in_use(bp) > cp + bnxt_get_ulp_msix_num(bp))
6312 return bnxt_get_ulp_msix_base(bp) + ulp_stat;
6314 return cp + ulp_stat;
6317 /* Check if a default RSS map needs to be setup. This function is only
6318 * used on older firmware that does not require reserving RX rings.
6320 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
6322 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6324 /* The RSS map is valid for RX rings set to resv_rx_rings */
6325 if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
6326 hw_resc->resv_rx_rings = bp->rx_nr_rings;
6327 if (!netif_is_rxfh_configured(bp->dev))
6328 bnxt_set_dflt_rss_indir_tbl(bp);
6332 static bool bnxt_need_reserve_rings(struct bnxt *bp)
6334 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6335 int cp = bnxt_cp_rings_in_use(bp);
6336 int nq = bnxt_nq_rings_in_use(bp);
6337 int rx = bp->rx_nr_rings, stat;
6338 int vnic = 1, grp = rx;
6340 if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
6341 bp->hwrm_spec_code >= 0x10601)
6344 /* Old firmware does not need RX ring reservations but we still
6345 * need to setup a default RSS map when needed. With new firmware
6346 * we go through RX ring reservations first and then set up the
6347 * RSS map for the successfully reserved RX rings when needed.
6349 if (!BNXT_NEW_RM(bp)) {
6350 bnxt_check_rss_tbl_no_rmgr(bp);
6353 if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
6355 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6357 stat = bnxt_get_func_stat_ctxs(bp);
6358 if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
6359 hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
6360 (hw_resc->resv_hw_ring_grps != grp &&
6361 !(bp->flags & BNXT_FLAG_CHIP_P5)))
6363 if ((bp->flags & BNXT_FLAG_CHIP_P5) && BNXT_PF(bp) &&
6364 hw_resc->resv_irqs != nq)
6369 static int __bnxt_reserve_rings(struct bnxt *bp)
6371 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6372 int cp = bnxt_nq_rings_in_use(bp);
6373 int tx = bp->tx_nr_rings;
6374 int rx = bp->rx_nr_rings;
6375 int grp, rx_rings, rc;
6379 if (!bnxt_need_reserve_rings(bp))
6382 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6384 if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
6386 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6388 grp = bp->rx_nr_rings;
6389 stat = bnxt_get_func_stat_ctxs(bp);
6391 rc = bnxt_hwrm_reserve_rings(bp, tx, rx, grp, cp, stat, vnic);
6395 tx = hw_resc->resv_tx_rings;
6396 if (BNXT_NEW_RM(bp)) {
6397 rx = hw_resc->resv_rx_rings;
6398 cp = hw_resc->resv_irqs;
6399 grp = hw_resc->resv_hw_ring_grps;
6400 vnic = hw_resc->resv_vnics;
6401 stat = hw_resc->resv_stat_ctxs;
6405 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
6409 if (netif_running(bp->dev))
6412 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
6413 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
6414 bp->dev->hw_features &= ~NETIF_F_LRO;
6415 bp->dev->features &= ~NETIF_F_LRO;
6416 bnxt_set_ring_params(bp);
6419 rx_rings = min_t(int, rx_rings, grp);
6420 cp = min_t(int, cp, bp->cp_nr_rings);
6421 if (stat > bnxt_get_ulp_stat_ctxs(bp))
6422 stat -= bnxt_get_ulp_stat_ctxs(bp);
6423 cp = min_t(int, cp, stat);
6424 rc = bnxt_trim_rings(bp, &rx_rings, &tx, cp, sh);
6425 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6427 cp = sh ? max_t(int, tx, rx_rings) : tx + rx_rings;
6428 bp->tx_nr_rings = tx;
6430 /* If we cannot reserve all the RX rings, reset the RSS map only
6431 * if absolutely necessary
6433 if (rx_rings != bp->rx_nr_rings) {
6434 netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
6435 rx_rings, bp->rx_nr_rings);
6436 if ((bp->dev->priv_flags & IFF_RXFH_CONFIGURED) &&
6437 (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
6438 bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
6439 bnxt_get_max_rss_ring(bp) >= rx_rings)) {
6440 netdev_warn(bp->dev, "RSS table entries reverting to default\n");
6441 bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
6444 bp->rx_nr_rings = rx_rings;
6445 bp->cp_nr_rings = cp;
6447 if (!tx || !rx || !cp || !grp || !vnic || !stat)
6450 if (!netif_is_rxfh_configured(bp->dev))
6451 bnxt_set_dflt_rss_indir_tbl(bp);
6456 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6457 int ring_grps, int cp_rings, int stats,
6460 struct hwrm_func_vf_cfg_input req = {0};
6463 if (!BNXT_NEW_RM(bp))
6466 __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6467 cp_rings, stats, vnics);
6468 flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
6469 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6470 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6471 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6472 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
6473 FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
6474 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6475 flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6477 req.flags = cpu_to_le32(flags);
6478 return hwrm_send_message_silent(bp, &req, sizeof(req),
6482 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6483 int ring_grps, int cp_rings, int stats,
6486 struct hwrm_func_cfg_input req = {0};
6489 __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6490 cp_rings, stats, vnics);
6491 flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
6492 if (BNXT_NEW_RM(bp)) {
6493 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6494 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6495 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6496 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
6497 if (bp->flags & BNXT_FLAG_CHIP_P5)
6498 flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
6499 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
6501 flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6504 req.flags = cpu_to_le32(flags);
6505 return hwrm_send_message_silent(bp, &req, sizeof(req),
6509 static int bnxt_hwrm_check_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6510 int ring_grps, int cp_rings, int stats,
6513 if (bp->hwrm_spec_code < 0x10801)
6517 return bnxt_hwrm_check_pf_rings(bp, tx_rings, rx_rings,
6518 ring_grps, cp_rings, stats,
6521 return bnxt_hwrm_check_vf_rings(bp, tx_rings, rx_rings, ring_grps,
6522 cp_rings, stats, vnics);
6525 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
6527 struct hwrm_ring_aggint_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6528 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6529 struct hwrm_ring_aggint_qcaps_input req = {0};
6532 coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
6533 coal_cap->num_cmpl_dma_aggr_max = 63;
6534 coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
6535 coal_cap->cmpl_aggr_dma_tmr_max = 65535;
6536 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
6537 coal_cap->int_lat_tmr_min_max = 65535;
6538 coal_cap->int_lat_tmr_max_max = 65535;
6539 coal_cap->num_cmpl_aggr_int_max = 65535;
6540 coal_cap->timer_units = 80;
6542 if (bp->hwrm_spec_code < 0x10902)
6545 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_AGGINT_QCAPS, -1, -1);
6546 mutex_lock(&bp->hwrm_cmd_lock);
6547 rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6549 coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
6550 coal_cap->nq_params = le32_to_cpu(resp->nq_params);
6551 coal_cap->num_cmpl_dma_aggr_max =
6552 le16_to_cpu(resp->num_cmpl_dma_aggr_max);
6553 coal_cap->num_cmpl_dma_aggr_during_int_max =
6554 le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
6555 coal_cap->cmpl_aggr_dma_tmr_max =
6556 le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
6557 coal_cap->cmpl_aggr_dma_tmr_during_int_max =
6558 le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
6559 coal_cap->int_lat_tmr_min_max =
6560 le16_to_cpu(resp->int_lat_tmr_min_max);
6561 coal_cap->int_lat_tmr_max_max =
6562 le16_to_cpu(resp->int_lat_tmr_max_max);
6563 coal_cap->num_cmpl_aggr_int_max =
6564 le16_to_cpu(resp->num_cmpl_aggr_int_max);
6565 coal_cap->timer_units = le16_to_cpu(resp->timer_units);
6567 mutex_unlock(&bp->hwrm_cmd_lock);
6570 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
6572 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6574 return usec * 1000 / coal_cap->timer_units;
6577 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
6578 struct bnxt_coal *hw_coal,
6579 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
6581 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6582 u32 cmpl_params = coal_cap->cmpl_params;
6583 u16 val, tmr, max, flags = 0;
6585 max = hw_coal->bufs_per_record * 128;
6586 if (hw_coal->budget)
6587 max = hw_coal->bufs_per_record * hw_coal->budget;
6588 max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
6590 val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
6591 req->num_cmpl_aggr_int = cpu_to_le16(val);
6593 val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
6594 req->num_cmpl_dma_aggr = cpu_to_le16(val);
6596 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
6597 coal_cap->num_cmpl_dma_aggr_during_int_max);
6598 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
6600 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
6601 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
6602 req->int_lat_tmr_max = cpu_to_le16(tmr);
6604 /* min timer set to 1/2 of interrupt timer */
6605 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
6607 val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
6608 req->int_lat_tmr_min = cpu_to_le16(val);
6609 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6612 /* buf timer set to 1/4 of interrupt timer */
6613 val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
6614 req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
6617 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
6618 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
6619 val = clamp_t(u16, tmr, 1,
6620 coal_cap->cmpl_aggr_dma_tmr_during_int_max);
6621 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
6623 cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
6626 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
6627 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
6628 if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
6629 hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
6630 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
6631 req->flags = cpu_to_le16(flags);
6632 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
6635 /* Caller holds bp->hwrm_cmd_lock */
6636 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
6637 struct bnxt_coal *hw_coal)
6639 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req = {0};
6640 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6641 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6642 u32 nq_params = coal_cap->nq_params;
6645 if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
6648 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS,
6650 req.ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
6652 cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
6654 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
6655 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
6656 req.int_lat_tmr_min = cpu_to_le16(tmr);
6657 req.enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6658 return _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6661 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
6663 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0};
6664 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6665 struct bnxt_coal coal;
6667 /* Tick values in micro seconds.
6668 * 1 coal_buf x bufs_per_record = 1 completion record.
6670 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
6672 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
6673 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
6675 if (!bnapi->rx_ring)
6678 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
6679 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6681 bnxt_hwrm_set_coal_params(bp, &coal, &req_rx);
6683 req_rx.ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
6685 return hwrm_send_message(bp, &req_rx, sizeof(req_rx),
6689 int bnxt_hwrm_set_coal(struct bnxt *bp)
6692 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
6695 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
6696 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6697 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
6698 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6700 bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, &req_rx);
6701 bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, &req_tx);
6703 mutex_lock(&bp->hwrm_cmd_lock);
6704 for (i = 0; i < bp->cp_nr_rings; i++) {
6705 struct bnxt_napi *bnapi = bp->bnapi[i];
6706 struct bnxt_coal *hw_coal;
6710 if (!bnapi->rx_ring) {
6711 ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6714 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
6716 req->ring_id = cpu_to_le16(ring_id);
6718 rc = _hwrm_send_message(bp, req, sizeof(*req),
6723 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6726 if (bnapi->rx_ring && bnapi->tx_ring) {
6728 ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6729 req->ring_id = cpu_to_le16(ring_id);
6730 rc = _hwrm_send_message(bp, req, sizeof(*req),
6736 hw_coal = &bp->rx_coal;
6738 hw_coal = &bp->tx_coal;
6739 __bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
6741 mutex_unlock(&bp->hwrm_cmd_lock);
6745 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
6747 struct hwrm_stat_ctx_clr_stats_input req0 = {0};
6748 struct hwrm_stat_ctx_free_input req = {0};
6754 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6757 bnxt_hwrm_cmd_hdr_init(bp, &req0, HWRM_STAT_CTX_CLR_STATS, -1, -1);
6758 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
6760 mutex_lock(&bp->hwrm_cmd_lock);
6761 for (i = 0; i < bp->cp_nr_rings; i++) {
6762 struct bnxt_napi *bnapi = bp->bnapi[i];
6763 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6765 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
6766 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
6767 if (BNXT_FW_MAJ(bp) <= 20) {
6768 req0.stat_ctx_id = req.stat_ctx_id;
6769 _hwrm_send_message(bp, &req0, sizeof(req0),
6772 _hwrm_send_message(bp, &req, sizeof(req),
6775 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
6778 mutex_unlock(&bp->hwrm_cmd_lock);
6781 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
6784 struct hwrm_stat_ctx_alloc_input req = {0};
6785 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
6787 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6790 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
6792 req.stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
6793 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
6795 mutex_lock(&bp->hwrm_cmd_lock);
6796 for (i = 0; i < bp->cp_nr_rings; i++) {
6797 struct bnxt_napi *bnapi = bp->bnapi[i];
6798 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6800 req.stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
6802 rc = _hwrm_send_message(bp, &req, sizeof(req),
6807 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
6809 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
6811 mutex_unlock(&bp->hwrm_cmd_lock);
6815 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
6817 struct hwrm_func_qcfg_input req = {0};
6818 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6819 u32 min_db_offset = 0;
6823 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
6824 req.fid = cpu_to_le16(0xffff);
6825 mutex_lock(&bp->hwrm_cmd_lock);
6826 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6828 goto func_qcfg_exit;
6830 #ifdef CONFIG_BNXT_SRIOV
6832 struct bnxt_vf_info *vf = &bp->vf;
6834 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
6836 bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
6839 flags = le16_to_cpu(resp->flags);
6840 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
6841 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
6842 bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
6843 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
6844 bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
6846 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
6847 bp->flags |= BNXT_FLAG_MULTI_HOST;
6848 if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
6849 bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
6851 switch (resp->port_partition_type) {
6852 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
6853 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
6854 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
6855 bp->port_partition_type = resp->port_partition_type;
6858 if (bp->hwrm_spec_code < 0x10707 ||
6859 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
6860 bp->br_mode = BRIDGE_MODE_VEB;
6861 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
6862 bp->br_mode = BRIDGE_MODE_VEPA;
6864 bp->br_mode = BRIDGE_MODE_UNDEF;
6866 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
6868 bp->max_mtu = BNXT_MAX_MTU;
6871 goto func_qcfg_exit;
6873 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6875 min_db_offset = DB_PF_OFFSET_P5;
6877 min_db_offset = DB_VF_OFFSET_P5;
6879 bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
6881 if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
6882 bp->db_size <= min_db_offset)
6883 bp->db_size = pci_resource_len(bp->pdev, 2);
6886 mutex_unlock(&bp->hwrm_cmd_lock);
6890 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_info *ctx,
6891 struct hwrm_func_backing_store_qcaps_output *resp)
6893 struct bnxt_mem_init *mem_init;
6899 init_val = resp->ctx_kind_initializer;
6900 init_mask = le16_to_cpu(resp->ctx_init_mask);
6901 offset = &resp->qp_init_offset;
6902 mem_init = &ctx->mem_init[BNXT_CTX_MEM_INIT_QP];
6903 for (i = 0; i < BNXT_CTX_MEM_INIT_MAX; i++, mem_init++, offset++) {
6904 mem_init->init_val = init_val;
6905 mem_init->offset = BNXT_MEM_INVALID_OFFSET;
6908 if (i == BNXT_CTX_MEM_INIT_STAT)
6909 offset = &resp->stat_init_offset;
6910 if (init_mask & (1 << i))
6911 mem_init->offset = *offset * 4;
6913 mem_init->init_val = 0;
6915 ctx->mem_init[BNXT_CTX_MEM_INIT_QP].size = ctx->qp_entry_size;
6916 ctx->mem_init[BNXT_CTX_MEM_INIT_SRQ].size = ctx->srq_entry_size;
6917 ctx->mem_init[BNXT_CTX_MEM_INIT_CQ].size = ctx->cq_entry_size;
6918 ctx->mem_init[BNXT_CTX_MEM_INIT_VNIC].size = ctx->vnic_entry_size;
6919 ctx->mem_init[BNXT_CTX_MEM_INIT_STAT].size = ctx->stat_entry_size;
6920 ctx->mem_init[BNXT_CTX_MEM_INIT_MRAV].size = ctx->mrav_entry_size;
6923 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
6925 struct hwrm_func_backing_store_qcaps_input req = {0};
6926 struct hwrm_func_backing_store_qcaps_output *resp =
6927 bp->hwrm_cmd_resp_addr;
6930 if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
6933 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_QCAPS, -1, -1);
6934 mutex_lock(&bp->hwrm_cmd_lock);
6935 rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6937 struct bnxt_ctx_pg_info *ctx_pg;
6938 struct bnxt_ctx_mem_info *ctx;
6941 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
6946 ctx->qp_max_entries = le32_to_cpu(resp->qp_max_entries);
6947 ctx->qp_min_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
6948 ctx->qp_max_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
6949 ctx->qp_entry_size = le16_to_cpu(resp->qp_entry_size);
6950 ctx->srq_max_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
6951 ctx->srq_max_entries = le32_to_cpu(resp->srq_max_entries);
6952 ctx->srq_entry_size = le16_to_cpu(resp->srq_entry_size);
6953 ctx->cq_max_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
6954 ctx->cq_max_entries = le32_to_cpu(resp->cq_max_entries);
6955 ctx->cq_entry_size = le16_to_cpu(resp->cq_entry_size);
6956 ctx->vnic_max_vnic_entries =
6957 le16_to_cpu(resp->vnic_max_vnic_entries);
6958 ctx->vnic_max_ring_table_entries =
6959 le16_to_cpu(resp->vnic_max_ring_table_entries);
6960 ctx->vnic_entry_size = le16_to_cpu(resp->vnic_entry_size);
6961 ctx->stat_max_entries = le32_to_cpu(resp->stat_max_entries);
6962 ctx->stat_entry_size = le16_to_cpu(resp->stat_entry_size);
6963 ctx->tqm_entry_size = le16_to_cpu(resp->tqm_entry_size);
6964 ctx->tqm_min_entries_per_ring =
6965 le32_to_cpu(resp->tqm_min_entries_per_ring);
6966 ctx->tqm_max_entries_per_ring =
6967 le32_to_cpu(resp->tqm_max_entries_per_ring);
6968 ctx->tqm_entries_multiple = resp->tqm_entries_multiple;
6969 if (!ctx->tqm_entries_multiple)
6970 ctx->tqm_entries_multiple = 1;
6971 ctx->mrav_max_entries = le32_to_cpu(resp->mrav_max_entries);
6972 ctx->mrav_entry_size = le16_to_cpu(resp->mrav_entry_size);
6973 ctx->mrav_num_entries_units =
6974 le16_to_cpu(resp->mrav_num_entries_units);
6975 ctx->tim_entry_size = le16_to_cpu(resp->tim_entry_size);
6976 ctx->tim_max_entries = le32_to_cpu(resp->tim_max_entries);
6978 bnxt_init_ctx_initializer(ctx, resp);
6980 ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
6981 if (!ctx->tqm_fp_rings_count)
6982 ctx->tqm_fp_rings_count = bp->max_q;
6983 else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
6984 ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
6986 tqm_rings = ctx->tqm_fp_rings_count + BNXT_MAX_TQM_SP_RINGS;
6987 ctx_pg = kcalloc(tqm_rings, sizeof(*ctx_pg), GFP_KERNEL);
6993 for (i = 0; i < tqm_rings; i++, ctx_pg++)
6994 ctx->tqm_mem[i] = ctx_pg;
7000 mutex_unlock(&bp->hwrm_cmd_lock);
7004 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
7007 if (!rmem->nr_pages)
7010 BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
7011 if (rmem->depth >= 1) {
7012 if (rmem->depth == 2)
7016 *pg_dir = cpu_to_le64(rmem->pg_tbl_map);
7018 *pg_dir = cpu_to_le64(rmem->dma_arr[0]);
7022 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES \
7023 (FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP | \
7024 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ | \
7025 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ | \
7026 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC | \
7027 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
7029 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
7031 struct hwrm_func_backing_store_cfg_input req = {0};
7032 struct bnxt_ctx_mem_info *ctx = bp->ctx;
7033 struct bnxt_ctx_pg_info *ctx_pg;
7034 u32 req_len = sizeof(req);
7035 __le32 *num_entries;
7045 if (req_len > bp->hwrm_max_ext_req_len)
7046 req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
7047 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_CFG, -1, -1);
7048 req.enables = cpu_to_le32(enables);
7050 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
7051 ctx_pg = &ctx->qp_mem;
7052 req.qp_num_entries = cpu_to_le32(ctx_pg->entries);
7053 req.qp_num_qp1_entries = cpu_to_le16(ctx->qp_min_qp1_entries);
7054 req.qp_num_l2_entries = cpu_to_le16(ctx->qp_max_l2_entries);
7055 req.qp_entry_size = cpu_to_le16(ctx->qp_entry_size);
7056 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7057 &req.qpc_pg_size_qpc_lvl,
7060 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
7061 ctx_pg = &ctx->srq_mem;
7062 req.srq_num_entries = cpu_to_le32(ctx_pg->entries);
7063 req.srq_num_l2_entries = cpu_to_le16(ctx->srq_max_l2_entries);
7064 req.srq_entry_size = cpu_to_le16(ctx->srq_entry_size);
7065 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7066 &req.srq_pg_size_srq_lvl,
7069 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
7070 ctx_pg = &ctx->cq_mem;
7071 req.cq_num_entries = cpu_to_le32(ctx_pg->entries);
7072 req.cq_num_l2_entries = cpu_to_le16(ctx->cq_max_l2_entries);
7073 req.cq_entry_size = cpu_to_le16(ctx->cq_entry_size);
7074 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, &req.cq_pg_size_cq_lvl,
7077 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
7078 ctx_pg = &ctx->vnic_mem;
7079 req.vnic_num_vnic_entries =
7080 cpu_to_le16(ctx->vnic_max_vnic_entries);
7081 req.vnic_num_ring_table_entries =
7082 cpu_to_le16(ctx->vnic_max_ring_table_entries);
7083 req.vnic_entry_size = cpu_to_le16(ctx->vnic_entry_size);
7084 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7085 &req.vnic_pg_size_vnic_lvl,
7086 &req.vnic_page_dir);
7088 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
7089 ctx_pg = &ctx->stat_mem;
7090 req.stat_num_entries = cpu_to_le32(ctx->stat_max_entries);
7091 req.stat_entry_size = cpu_to_le16(ctx->stat_entry_size);
7092 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7093 &req.stat_pg_size_stat_lvl,
7094 &req.stat_page_dir);
7096 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
7097 ctx_pg = &ctx->mrav_mem;
7098 req.mrav_num_entries = cpu_to_le32(ctx_pg->entries);
7099 if (ctx->mrav_num_entries_units)
7101 FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
7102 req.mrav_entry_size = cpu_to_le16(ctx->mrav_entry_size);
7103 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7104 &req.mrav_pg_size_mrav_lvl,
7105 &req.mrav_page_dir);
7107 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
7108 ctx_pg = &ctx->tim_mem;
7109 req.tim_num_entries = cpu_to_le32(ctx_pg->entries);
7110 req.tim_entry_size = cpu_to_le16(ctx->tim_entry_size);
7111 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7112 &req.tim_pg_size_tim_lvl,
7115 for (i = 0, num_entries = &req.tqm_sp_num_entries,
7116 pg_attr = &req.tqm_sp_pg_size_tqm_sp_lvl,
7117 pg_dir = &req.tqm_sp_page_dir,
7118 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP;
7119 i < BNXT_MAX_TQM_RINGS;
7120 i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
7121 if (!(enables & ena))
7124 req.tqm_entry_size = cpu_to_le16(ctx->tqm_entry_size);
7125 ctx_pg = ctx->tqm_mem[i];
7126 *num_entries = cpu_to_le32(ctx_pg->entries);
7127 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
7129 req.flags = cpu_to_le32(flags);
7130 return hwrm_send_message(bp, &req, req_len, HWRM_CMD_TIMEOUT);
7133 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
7134 struct bnxt_ctx_pg_info *ctx_pg)
7136 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7138 rmem->page_size = BNXT_PAGE_SIZE;
7139 rmem->pg_arr = ctx_pg->ctx_pg_arr;
7140 rmem->dma_arr = ctx_pg->ctx_dma_arr;
7141 rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
7142 if (rmem->depth >= 1)
7143 rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
7144 return bnxt_alloc_ring(bp, rmem);
7147 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
7148 struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
7149 u8 depth, struct bnxt_mem_init *mem_init)
7151 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7157 ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
7158 if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
7159 ctx_pg->nr_pages = 0;
7162 if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
7166 ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
7168 if (!ctx_pg->ctx_pg_tbl)
7170 nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
7171 rmem->nr_pages = nr_tbls;
7172 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
7175 for (i = 0; i < nr_tbls; i++) {
7176 struct bnxt_ctx_pg_info *pg_tbl;
7178 pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
7181 ctx_pg->ctx_pg_tbl[i] = pg_tbl;
7182 rmem = &pg_tbl->ring_mem;
7183 rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
7184 rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
7186 rmem->nr_pages = MAX_CTX_PAGES;
7187 rmem->mem_init = mem_init;
7188 if (i == (nr_tbls - 1)) {
7189 int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
7192 rmem->nr_pages = rem;
7194 rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
7199 rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
7200 if (rmem->nr_pages > 1 || depth)
7202 rmem->mem_init = mem_init;
7203 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
7208 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
7209 struct bnxt_ctx_pg_info *ctx_pg)
7211 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7213 if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
7214 ctx_pg->ctx_pg_tbl) {
7215 int i, nr_tbls = rmem->nr_pages;
7217 for (i = 0; i < nr_tbls; i++) {
7218 struct bnxt_ctx_pg_info *pg_tbl;
7219 struct bnxt_ring_mem_info *rmem2;
7221 pg_tbl = ctx_pg->ctx_pg_tbl[i];
7224 rmem2 = &pg_tbl->ring_mem;
7225 bnxt_free_ring(bp, rmem2);
7226 ctx_pg->ctx_pg_arr[i] = NULL;
7228 ctx_pg->ctx_pg_tbl[i] = NULL;
7230 kfree(ctx_pg->ctx_pg_tbl);
7231 ctx_pg->ctx_pg_tbl = NULL;
7233 bnxt_free_ring(bp, rmem);
7234 ctx_pg->nr_pages = 0;
7237 static void bnxt_free_ctx_mem(struct bnxt *bp)
7239 struct bnxt_ctx_mem_info *ctx = bp->ctx;
7245 if (ctx->tqm_mem[0]) {
7246 for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
7247 bnxt_free_ctx_pg_tbls(bp, ctx->tqm_mem[i]);
7248 kfree(ctx->tqm_mem[0]);
7249 ctx->tqm_mem[0] = NULL;
7252 bnxt_free_ctx_pg_tbls(bp, &ctx->tim_mem);
7253 bnxt_free_ctx_pg_tbls(bp, &ctx->mrav_mem);
7254 bnxt_free_ctx_pg_tbls(bp, &ctx->stat_mem);
7255 bnxt_free_ctx_pg_tbls(bp, &ctx->vnic_mem);
7256 bnxt_free_ctx_pg_tbls(bp, &ctx->cq_mem);
7257 bnxt_free_ctx_pg_tbls(bp, &ctx->srq_mem);
7258 bnxt_free_ctx_pg_tbls(bp, &ctx->qp_mem);
7259 ctx->flags &= ~BNXT_CTX_FLAG_INITED;
7262 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
7264 struct bnxt_ctx_pg_info *ctx_pg;
7265 struct bnxt_ctx_mem_info *ctx;
7266 struct bnxt_mem_init *init;
7267 u32 mem_size, ena, entries;
7268 u32 entries_sp, min;
7275 rc = bnxt_hwrm_func_backing_store_qcaps(bp);
7277 netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
7282 if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
7285 if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
7291 ctx_pg = &ctx->qp_mem;
7292 ctx_pg->entries = ctx->qp_min_qp1_entries + ctx->qp_max_l2_entries +
7294 if (ctx->qp_entry_size) {
7295 mem_size = ctx->qp_entry_size * ctx_pg->entries;
7296 init = &ctx->mem_init[BNXT_CTX_MEM_INIT_QP];
7297 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init);
7302 ctx_pg = &ctx->srq_mem;
7303 ctx_pg->entries = ctx->srq_max_l2_entries + extra_srqs;
7304 if (ctx->srq_entry_size) {
7305 mem_size = ctx->srq_entry_size * ctx_pg->entries;
7306 init = &ctx->mem_init[BNXT_CTX_MEM_INIT_SRQ];
7307 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init);
7312 ctx_pg = &ctx->cq_mem;
7313 ctx_pg->entries = ctx->cq_max_l2_entries + extra_qps * 2;
7314 if (ctx->cq_entry_size) {
7315 mem_size = ctx->cq_entry_size * ctx_pg->entries;
7316 init = &ctx->mem_init[BNXT_CTX_MEM_INIT_CQ];
7317 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init);
7322 ctx_pg = &ctx->vnic_mem;
7323 ctx_pg->entries = ctx->vnic_max_vnic_entries +
7324 ctx->vnic_max_ring_table_entries;
7325 if (ctx->vnic_entry_size) {
7326 mem_size = ctx->vnic_entry_size * ctx_pg->entries;
7327 init = &ctx->mem_init[BNXT_CTX_MEM_INIT_VNIC];
7328 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, init);
7333 ctx_pg = &ctx->stat_mem;
7334 ctx_pg->entries = ctx->stat_max_entries;
7335 if (ctx->stat_entry_size) {
7336 mem_size = ctx->stat_entry_size * ctx_pg->entries;
7337 init = &ctx->mem_init[BNXT_CTX_MEM_INIT_STAT];
7338 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, init);
7344 if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
7347 ctx_pg = &ctx->mrav_mem;
7348 /* 128K extra is needed to accommodate static AH context
7349 * allocation by f/w.
7351 num_mr = 1024 * 256;
7352 num_ah = 1024 * 128;
7353 ctx_pg->entries = num_mr + num_ah;
7354 if (ctx->mrav_entry_size) {
7355 mem_size = ctx->mrav_entry_size * ctx_pg->entries;
7356 init = &ctx->mem_init[BNXT_CTX_MEM_INIT_MRAV];
7357 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 2, init);
7361 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
7362 if (ctx->mrav_num_entries_units)
7364 ((num_mr / ctx->mrav_num_entries_units) << 16) |
7365 (num_ah / ctx->mrav_num_entries_units);
7367 ctx_pg = &ctx->tim_mem;
7368 ctx_pg->entries = ctx->qp_mem.entries;
7369 if (ctx->tim_entry_size) {
7370 mem_size = ctx->tim_entry_size * ctx_pg->entries;
7371 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, NULL);
7375 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
7378 min = ctx->tqm_min_entries_per_ring;
7379 entries_sp = ctx->vnic_max_vnic_entries + ctx->qp_max_l2_entries +
7380 2 * (extra_qps + ctx->qp_min_qp1_entries) + min;
7381 entries_sp = roundup(entries_sp, ctx->tqm_entries_multiple);
7382 entries = ctx->qp_max_l2_entries + 2 * (extra_qps + ctx->qp_min_qp1_entries);
7383 entries = roundup(entries, ctx->tqm_entries_multiple);
7384 entries = clamp_t(u32, entries, min, ctx->tqm_max_entries_per_ring);
7385 for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++) {
7386 ctx_pg = ctx->tqm_mem[i];
7387 ctx_pg->entries = i ? entries : entries_sp;
7388 if (ctx->tqm_entry_size) {
7389 mem_size = ctx->tqm_entry_size * ctx_pg->entries;
7390 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1,
7395 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
7397 ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
7398 rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
7400 netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
7404 ctx->flags |= BNXT_CTX_FLAG_INITED;
7408 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
7410 struct hwrm_func_resource_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
7411 struct hwrm_func_resource_qcaps_input req = {0};
7412 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7415 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESOURCE_QCAPS, -1, -1);
7416 req.fid = cpu_to_le16(0xffff);
7418 mutex_lock(&bp->hwrm_cmd_lock);
7419 rc = _hwrm_send_message_silent(bp, &req, sizeof(req),
7422 goto hwrm_func_resc_qcaps_exit;
7424 hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
7426 goto hwrm_func_resc_qcaps_exit;
7428 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
7429 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
7430 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
7431 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
7432 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
7433 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
7434 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
7435 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
7436 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
7437 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
7438 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
7439 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
7440 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
7441 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
7442 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
7443 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
7445 if (bp->flags & BNXT_FLAG_CHIP_P5) {
7446 u16 max_msix = le16_to_cpu(resp->max_msix);
7448 hw_resc->max_nqs = max_msix;
7449 hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
7453 struct bnxt_pf_info *pf = &bp->pf;
7455 pf->vf_resv_strategy =
7456 le16_to_cpu(resp->vf_reservation_strategy);
7457 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
7458 pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
7460 hwrm_func_resc_qcaps_exit:
7461 mutex_unlock(&bp->hwrm_cmd_lock);
7465 /* bp->hwrm_cmd_lock already held. */
7466 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
7468 struct hwrm_port_mac_ptp_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
7469 struct hwrm_port_mac_ptp_qcfg_input req = {0};
7470 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
7474 if (bp->hwrm_spec_code < 0x10801) {
7479 req.port_id = cpu_to_le16(bp->pf.port_id);
7480 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_MAC_PTP_QCFG, -1, -1);
7481 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7485 flags = resp->flags;
7486 if (!(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
7491 ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
7497 if (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK) {
7498 ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
7499 ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
7500 } else if (bp->flags & BNXT_FLAG_CHIP_P5) {
7501 ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
7502 ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
7515 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
7518 struct hwrm_func_qcaps_input req = {0};
7519 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
7520 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7521 u32 flags, flags_ext;
7523 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
7524 req.fid = cpu_to_le16(0xffff);
7526 mutex_lock(&bp->hwrm_cmd_lock);
7527 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7529 goto hwrm_func_qcaps_exit;
7531 flags = le32_to_cpu(resp->flags);
7532 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
7533 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
7534 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
7535 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
7536 if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
7537 bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
7538 if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
7539 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
7540 if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
7541 bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
7542 if (flags & FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
7543 bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
7544 if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
7545 bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
7546 if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
7547 bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
7549 flags_ext = le32_to_cpu(resp->flags_ext);
7550 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
7551 bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
7553 bp->tx_push_thresh = 0;
7554 if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
7555 BNXT_FW_MAJ(bp) > 217)
7556 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
7558 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
7559 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
7560 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
7561 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
7562 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
7563 if (!hw_resc->max_hw_ring_grps)
7564 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
7565 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
7566 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
7567 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
7570 struct bnxt_pf_info *pf = &bp->pf;
7572 pf->fw_fid = le16_to_cpu(resp->fid);
7573 pf->port_id = le16_to_cpu(resp->port_id);
7574 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
7575 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
7576 pf->max_vfs = le16_to_cpu(resp->max_vfs);
7577 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
7578 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
7579 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
7580 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
7581 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
7582 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
7583 bp->flags &= ~BNXT_FLAG_WOL_CAP;
7584 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
7585 bp->flags |= BNXT_FLAG_WOL_CAP;
7586 if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
7587 __bnxt_hwrm_ptp_qcfg(bp);
7593 #ifdef CONFIG_BNXT_SRIOV
7594 struct bnxt_vf_info *vf = &bp->vf;
7596 vf->fw_fid = le16_to_cpu(resp->fid);
7597 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
7601 hwrm_func_qcaps_exit:
7602 mutex_unlock(&bp->hwrm_cmd_lock);
7606 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
7608 static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
7612 rc = __bnxt_hwrm_func_qcaps(bp);
7615 rc = bnxt_hwrm_queue_qportcfg(bp);
7617 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
7620 if (bp->hwrm_spec_code >= 0x10803) {
7621 rc = bnxt_alloc_ctx_mem(bp);
7624 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
7626 bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
7631 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
7633 struct hwrm_cfa_adv_flow_mgnt_qcaps_input req = {0};
7634 struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
7638 if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
7641 resp = bp->hwrm_cmd_resp_addr;
7642 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS, -1, -1);
7644 mutex_lock(&bp->hwrm_cmd_lock);
7645 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7647 goto hwrm_cfa_adv_qcaps_exit;
7649 flags = le32_to_cpu(resp->flags);
7651 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
7652 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
7654 hwrm_cfa_adv_qcaps_exit:
7655 mutex_unlock(&bp->hwrm_cmd_lock);
7659 static int __bnxt_alloc_fw_health(struct bnxt *bp)
7664 bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
7671 static int bnxt_alloc_fw_health(struct bnxt *bp)
7675 if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
7676 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
7679 rc = __bnxt_alloc_fw_health(bp);
7681 bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
7682 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
7689 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
7691 writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
7692 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
7693 BNXT_FW_HEALTH_WIN_MAP_OFF);
7696 bool bnxt_is_fw_healthy(struct bnxt *bp)
7698 if (bp->fw_health && bp->fw_health->status_reliable) {
7701 fw_status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
7702 if (fw_status && !BNXT_FW_IS_HEALTHY(fw_status))
7709 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
7711 struct bnxt_fw_health *fw_health = bp->fw_health;
7714 if (!fw_health || !fw_health->status_reliable)
7717 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
7718 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
7719 fw_health->status_reliable = false;
7722 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
7730 bp->fw_health->status_reliable = false;
7732 __bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
7733 hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
7735 sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
7736 if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
7737 if (!bp->chip_num) {
7738 __bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
7739 bp->chip_num = readl(bp->bar0 +
7740 BNXT_FW_HEALTH_WIN_BASE +
7741 BNXT_GRC_REG_CHIP_NUM);
7743 if (!BNXT_CHIP_P5(bp))
7746 status_loc = BNXT_GRC_REG_STATUS_P5 |
7747 BNXT_FW_HEALTH_REG_TYPE_BAR0;
7749 status_loc = readl(hs + offsetof(struct hcomm_status,
7753 if (__bnxt_alloc_fw_health(bp)) {
7754 netdev_warn(bp->dev, "no memory for firmware status checks\n");
7758 bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
7759 reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
7760 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
7761 __bnxt_map_fw_health_reg(bp, status_loc);
7762 bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
7763 BNXT_FW_HEALTH_WIN_OFF(status_loc);
7766 bp->fw_health->status_reliable = true;
7769 static int bnxt_map_fw_health_regs(struct bnxt *bp)
7771 struct bnxt_fw_health *fw_health = bp->fw_health;
7772 u32 reg_base = 0xffffffff;
7775 bp->fw_health->status_reliable = false;
7776 /* Only pre-map the monitoring GRC registers using window 3 */
7777 for (i = 0; i < 4; i++) {
7778 u32 reg = fw_health->regs[i];
7780 if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
7782 if (reg_base == 0xffffffff)
7783 reg_base = reg & BNXT_GRC_BASE_MASK;
7784 if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
7786 fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
7788 bp->fw_health->status_reliable = true;
7789 if (reg_base == 0xffffffff)
7792 __bnxt_map_fw_health_reg(bp, reg_base);
7796 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
7798 struct hwrm_error_recovery_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
7799 struct bnxt_fw_health *fw_health = bp->fw_health;
7800 struct hwrm_error_recovery_qcfg_input req = {0};
7803 if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
7806 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_ERROR_RECOVERY_QCFG, -1, -1);
7807 mutex_lock(&bp->hwrm_cmd_lock);
7808 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7810 goto err_recovery_out;
7811 fw_health->flags = le32_to_cpu(resp->flags);
7812 if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
7813 !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
7815 goto err_recovery_out;
7817 fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
7818 fw_health->master_func_wait_dsecs =
7819 le32_to_cpu(resp->master_func_wait_period);
7820 fw_health->normal_func_wait_dsecs =
7821 le32_to_cpu(resp->normal_func_wait_period);
7822 fw_health->post_reset_wait_dsecs =
7823 le32_to_cpu(resp->master_func_wait_period_after_reset);
7824 fw_health->post_reset_max_wait_dsecs =
7825 le32_to_cpu(resp->max_bailout_time_after_reset);
7826 fw_health->regs[BNXT_FW_HEALTH_REG] =
7827 le32_to_cpu(resp->fw_health_status_reg);
7828 fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
7829 le32_to_cpu(resp->fw_heartbeat_reg);
7830 fw_health->regs[BNXT_FW_RESET_CNT_REG] =
7831 le32_to_cpu(resp->fw_reset_cnt_reg);
7832 fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
7833 le32_to_cpu(resp->reset_inprogress_reg);
7834 fw_health->fw_reset_inprog_reg_mask =
7835 le32_to_cpu(resp->reset_inprogress_reg_mask);
7836 fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
7837 if (fw_health->fw_reset_seq_cnt >= 16) {
7839 goto err_recovery_out;
7841 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
7842 fw_health->fw_reset_seq_regs[i] =
7843 le32_to_cpu(resp->reset_reg[i]);
7844 fw_health->fw_reset_seq_vals[i] =
7845 le32_to_cpu(resp->reset_reg_val[i]);
7846 fw_health->fw_reset_seq_delay_msec[i] =
7847 resp->delay_after_reset[i];
7850 mutex_unlock(&bp->hwrm_cmd_lock);
7852 rc = bnxt_map_fw_health_regs(bp);
7854 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
7858 static int bnxt_hwrm_func_reset(struct bnxt *bp)
7860 struct hwrm_func_reset_input req = {0};
7862 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
7865 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
7868 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
7870 struct hwrm_nvm_get_dev_info_output nvm_info;
7872 if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
7873 snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
7874 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
7875 nvm_info.nvm_cfg_ver_upd);
7878 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
7881 struct hwrm_queue_qportcfg_input req = {0};
7882 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
7886 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
7888 mutex_lock(&bp->hwrm_cmd_lock);
7889 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7893 if (!resp->max_configurable_queues) {
7897 bp->max_tc = resp->max_configurable_queues;
7898 bp->max_lltc = resp->max_configurable_lossless_queues;
7899 if (bp->max_tc > BNXT_MAX_QUEUE)
7900 bp->max_tc = BNXT_MAX_QUEUE;
7902 no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
7903 qptr = &resp->queue_id0;
7904 for (i = 0, j = 0; i < bp->max_tc; i++) {
7905 bp->q_info[j].queue_id = *qptr;
7906 bp->q_ids[i] = *qptr++;
7907 bp->q_info[j].queue_profile = *qptr++;
7908 bp->tc_to_qidx[j] = j;
7909 if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
7910 (no_rdma && BNXT_PF(bp)))
7913 bp->max_q = bp->max_tc;
7914 bp->max_tc = max_t(u8, j, 1);
7916 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
7919 if (bp->max_lltc > bp->max_tc)
7920 bp->max_lltc = bp->max_tc;
7923 mutex_unlock(&bp->hwrm_cmd_lock);
7927 static int __bnxt_hwrm_ver_get(struct bnxt *bp, bool silent)
7929 struct hwrm_ver_get_input req = {0};
7932 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
7933 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
7934 req.hwrm_intf_min = HWRM_VERSION_MINOR;
7935 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
7937 rc = bnxt_hwrm_do_send_msg(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT,
7942 static int bnxt_hwrm_ver_get(struct bnxt *bp)
7944 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
7945 u16 fw_maj, fw_min, fw_bld, fw_rsv;
7946 u32 dev_caps_cfg, hwrm_ver;
7949 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
7950 mutex_lock(&bp->hwrm_cmd_lock);
7951 rc = __bnxt_hwrm_ver_get(bp, false);
7953 goto hwrm_ver_get_exit;
7955 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
7957 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
7958 resp->hwrm_intf_min_8b << 8 |
7959 resp->hwrm_intf_upd_8b;
7960 if (resp->hwrm_intf_maj_8b < 1) {
7961 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
7962 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
7963 resp->hwrm_intf_upd_8b);
7964 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
7967 hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
7968 HWRM_VERSION_UPDATE;
7970 if (bp->hwrm_spec_code > hwrm_ver)
7971 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
7972 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
7973 HWRM_VERSION_UPDATE);
7975 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
7976 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
7977 resp->hwrm_intf_upd_8b);
7979 fw_maj = le16_to_cpu(resp->hwrm_fw_major);
7980 if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
7981 fw_min = le16_to_cpu(resp->hwrm_fw_minor);
7982 fw_bld = le16_to_cpu(resp->hwrm_fw_build);
7983 fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
7984 len = FW_VER_STR_LEN;
7986 fw_maj = resp->hwrm_fw_maj_8b;
7987 fw_min = resp->hwrm_fw_min_8b;
7988 fw_bld = resp->hwrm_fw_bld_8b;
7989 fw_rsv = resp->hwrm_fw_rsvd_8b;
7990 len = BC_HWRM_STR_LEN;
7992 bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
7993 snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
7996 if (strlen(resp->active_pkg_name)) {
7997 int fw_ver_len = strlen(bp->fw_ver_str);
7999 snprintf(bp->fw_ver_str + fw_ver_len,
8000 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
8001 resp->active_pkg_name);
8002 bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
8005 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
8006 if (!bp->hwrm_cmd_timeout)
8007 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
8009 if (resp->hwrm_intf_maj_8b >= 1) {
8010 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
8011 bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
8013 if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
8014 bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
8016 bp->chip_num = le16_to_cpu(resp->chip_num);
8017 bp->chip_rev = resp->chip_rev;
8018 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
8020 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
8022 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
8023 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
8024 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
8025 bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
8027 if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
8028 bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
8031 VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
8032 bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
8035 VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
8036 bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
8039 VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
8040 bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
8043 mutex_unlock(&bp->hwrm_cmd_lock);
8047 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
8049 struct hwrm_fw_set_time_input req = {0};
8051 time64_t now = ktime_get_real_seconds();
8053 if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
8054 bp->hwrm_spec_code < 0x10400)
8057 time64_to_tm(now, 0, &tm);
8058 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
8059 req.year = cpu_to_le16(1900 + tm.tm_year);
8060 req.month = 1 + tm.tm_mon;
8061 req.day = tm.tm_mday;
8062 req.hour = tm.tm_hour;
8063 req.minute = tm.tm_min;
8064 req.second = tm.tm_sec;
8065 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8068 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
8073 sw_tmp = (*sw & ~mask) | hw;
8074 if (hw < (*sw & mask))
8076 WRITE_ONCE(*sw, sw_tmp);
8079 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
8080 int count, bool ignore_zero)
8084 for (i = 0; i < count; i++) {
8085 u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
8087 if (ignore_zero && !hw)
8090 if (masks[i] == -1ULL)
8093 bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
8097 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
8099 if (!stats->hw_stats)
8102 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
8103 stats->hw_masks, stats->len / 8, false);
8106 static void bnxt_accumulate_all_stats(struct bnxt *bp)
8108 struct bnxt_stats_mem *ring0_stats;
8109 bool ignore_zero = false;
8112 /* Chip bug. Counter intermittently becomes 0. */
8113 if (bp->flags & BNXT_FLAG_CHIP_P5)
8116 for (i = 0; i < bp->cp_nr_rings; i++) {
8117 struct bnxt_napi *bnapi = bp->bnapi[i];
8118 struct bnxt_cp_ring_info *cpr;
8119 struct bnxt_stats_mem *stats;
8121 cpr = &bnapi->cp_ring;
8122 stats = &cpr->stats;
8124 ring0_stats = stats;
8125 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
8126 ring0_stats->hw_masks,
8127 ring0_stats->len / 8, ignore_zero);
8129 if (bp->flags & BNXT_FLAG_PORT_STATS) {
8130 struct bnxt_stats_mem *stats = &bp->port_stats;
8131 __le64 *hw_stats = stats->hw_stats;
8132 u64 *sw_stats = stats->sw_stats;
8133 u64 *masks = stats->hw_masks;
8136 cnt = sizeof(struct rx_port_stats) / 8;
8137 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
8139 hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
8140 sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
8141 masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
8142 cnt = sizeof(struct tx_port_stats) / 8;
8143 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
8145 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
8146 bnxt_accumulate_stats(&bp->rx_port_stats_ext);
8147 bnxt_accumulate_stats(&bp->tx_port_stats_ext);
8151 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
8153 struct bnxt_pf_info *pf = &bp->pf;
8154 struct hwrm_port_qstats_input req = {0};
8156 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
8159 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
8163 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
8164 req.port_id = cpu_to_le16(pf->port_id);
8165 req.tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
8166 BNXT_TX_PORT_STATS_BYTE_OFFSET);
8167 req.rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
8168 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8171 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
8173 struct hwrm_port_qstats_ext_output *resp = bp->hwrm_cmd_resp_addr;
8174 struct hwrm_queue_pri2cos_qcfg_input req2 = {0};
8175 struct hwrm_port_qstats_ext_input req = {0};
8176 struct bnxt_pf_info *pf = &bp->pf;
8180 if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
8183 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
8186 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS_EXT, -1, -1);
8188 req.port_id = cpu_to_le16(pf->port_id);
8189 req.rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
8190 req.rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
8191 tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
8192 sizeof(struct tx_port_stats_ext) : 0;
8193 req.tx_stat_size = cpu_to_le16(tx_stat_size);
8194 req.tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
8195 mutex_lock(&bp->hwrm_cmd_lock);
8196 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8198 bp->fw_rx_stats_ext_size = le16_to_cpu(resp->rx_stat_size) / 8;
8199 bp->fw_tx_stats_ext_size = tx_stat_size ?
8200 le16_to_cpu(resp->tx_stat_size) / 8 : 0;
8202 bp->fw_rx_stats_ext_size = 0;
8203 bp->fw_tx_stats_ext_size = 0;
8208 if (bp->fw_tx_stats_ext_size <=
8209 offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
8210 mutex_unlock(&bp->hwrm_cmd_lock);
8211 bp->pri2cos_valid = 0;
8215 bnxt_hwrm_cmd_hdr_init(bp, &req2, HWRM_QUEUE_PRI2COS_QCFG, -1, -1);
8216 req2.flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
8218 rc = _hwrm_send_message(bp, &req2, sizeof(req2), HWRM_CMD_TIMEOUT);
8220 struct hwrm_queue_pri2cos_qcfg_output *resp2;
8224 resp2 = bp->hwrm_cmd_resp_addr;
8225 pri2cos = &resp2->pri0_cos_queue_id;
8226 for (i = 0; i < 8; i++) {
8227 u8 queue_id = pri2cos[i];
8230 /* Per port queue IDs start from 0, 10, 20, etc */
8231 queue_idx = queue_id % 10;
8232 if (queue_idx > BNXT_MAX_QUEUE) {
8233 bp->pri2cos_valid = false;
8236 for (j = 0; j < bp->max_q; j++) {
8237 if (bp->q_ids[j] == queue_id)
8238 bp->pri2cos_idx[i] = queue_idx;
8241 bp->pri2cos_valid = 1;
8244 mutex_unlock(&bp->hwrm_cmd_lock);
8248 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
8250 if (bp->vxlan_fw_dst_port_id != INVALID_HW_RING_ID)
8251 bnxt_hwrm_tunnel_dst_port_free(
8252 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
8253 if (bp->nge_fw_dst_port_id != INVALID_HW_RING_ID)
8254 bnxt_hwrm_tunnel_dst_port_free(
8255 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
8258 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
8264 tpa_flags = bp->flags & BNXT_FLAG_TPA;
8265 else if (BNXT_NO_FW_ACCESS(bp))
8267 for (i = 0; i < bp->nr_vnics; i++) {
8268 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
8270 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
8278 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
8282 for (i = 0; i < bp->nr_vnics; i++)
8283 bnxt_hwrm_vnic_set_rss(bp, i, false);
8286 static void bnxt_clear_vnic(struct bnxt *bp)
8291 bnxt_hwrm_clear_vnic_filter(bp);
8292 if (!(bp->flags & BNXT_FLAG_CHIP_P5)) {
8293 /* clear all RSS setting before free vnic ctx */
8294 bnxt_hwrm_clear_vnic_rss(bp);
8295 bnxt_hwrm_vnic_ctx_free(bp);
8297 /* before free the vnic, undo the vnic tpa settings */
8298 if (bp->flags & BNXT_FLAG_TPA)
8299 bnxt_set_tpa(bp, false);
8300 bnxt_hwrm_vnic_free(bp);
8301 if (bp->flags & BNXT_FLAG_CHIP_P5)
8302 bnxt_hwrm_vnic_ctx_free(bp);
8305 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
8308 bnxt_clear_vnic(bp);
8309 bnxt_hwrm_ring_free(bp, close_path);
8310 bnxt_hwrm_ring_grp_free(bp);
8312 bnxt_hwrm_stat_ctx_free(bp);
8313 bnxt_hwrm_free_tunnel_ports(bp);
8317 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
8319 struct hwrm_func_cfg_input req = {0};
8321 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
8322 req.fid = cpu_to_le16(0xffff);
8323 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
8324 if (br_mode == BRIDGE_MODE_VEB)
8325 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
8326 else if (br_mode == BRIDGE_MODE_VEPA)
8327 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
8330 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8333 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
8335 struct hwrm_func_cfg_input req = {0};
8337 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
8340 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
8341 req.fid = cpu_to_le16(0xffff);
8342 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
8343 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
8345 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
8347 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8350 static int __bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
8352 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
8355 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
8358 /* allocate context for vnic */
8359 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
8361 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
8363 goto vnic_setup_err;
8365 bp->rsscos_nr_ctxs++;
8367 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8368 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
8370 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
8372 goto vnic_setup_err;
8374 bp->rsscos_nr_ctxs++;
8378 /* configure default vnic, ring grp */
8379 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
8381 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
8383 goto vnic_setup_err;
8386 /* Enable RSS hashing on vnic */
8387 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
8389 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
8391 goto vnic_setup_err;
8394 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8395 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
8397 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
8406 static int __bnxt_setup_vnic_p5(struct bnxt *bp, u16 vnic_id)
8410 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
8411 for (i = 0; i < nr_ctxs; i++) {
8412 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, i);
8414 netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
8418 bp->rsscos_nr_ctxs++;
8423 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic_id, true);
8425 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
8429 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
8431 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
8435 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8436 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
8438 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
8445 static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
8447 if (bp->flags & BNXT_FLAG_CHIP_P5)
8448 return __bnxt_setup_vnic_p5(bp, vnic_id);
8450 return __bnxt_setup_vnic(bp, vnic_id);
8453 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
8455 #ifdef CONFIG_RFS_ACCEL
8458 if (bp->flags & BNXT_FLAG_CHIP_P5)
8461 for (i = 0; i < bp->rx_nr_rings; i++) {
8462 struct bnxt_vnic_info *vnic;
8463 u16 vnic_id = i + 1;
8466 if (vnic_id >= bp->nr_vnics)
8469 vnic = &bp->vnic_info[vnic_id];
8470 vnic->flags |= BNXT_VNIC_RFS_FLAG;
8471 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
8472 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
8473 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
8475 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
8479 rc = bnxt_setup_vnic(bp, vnic_id);
8489 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
8490 static bool bnxt_promisc_ok(struct bnxt *bp)
8492 #ifdef CONFIG_BNXT_SRIOV
8493 if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
8499 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
8501 unsigned int rc = 0;
8503 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
8505 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
8510 rc = bnxt_hwrm_vnic_cfg(bp, 1);
8512 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
8519 static int bnxt_cfg_rx_mode(struct bnxt *);
8520 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
8522 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
8524 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8526 unsigned int rx_nr_rings = bp->rx_nr_rings;
8529 rc = bnxt_hwrm_stat_ctx_alloc(bp);
8531 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
8537 rc = bnxt_hwrm_ring_alloc(bp);
8539 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
8543 rc = bnxt_hwrm_ring_grp_alloc(bp);
8545 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
8549 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8552 /* default vnic 0 */
8553 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
8555 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
8559 rc = bnxt_setup_vnic(bp, 0);
8563 if (bp->flags & BNXT_FLAG_RFS) {
8564 rc = bnxt_alloc_rfs_vnics(bp);
8569 if (bp->flags & BNXT_FLAG_TPA) {
8570 rc = bnxt_set_tpa(bp, true);
8576 bnxt_update_vf_mac(bp);
8578 /* Filter for default vnic 0 */
8579 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
8581 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
8584 vnic->uc_filter_count = 1;
8587 if (bp->dev->flags & IFF_BROADCAST)
8588 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
8590 if (bp->dev->flags & IFF_PROMISC)
8591 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
8593 if (bp->dev->flags & IFF_ALLMULTI) {
8594 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
8595 vnic->mc_list_count = 0;
8599 bnxt_mc_list_updated(bp, &mask);
8600 vnic->rx_mask |= mask;
8603 rc = bnxt_cfg_rx_mode(bp);
8607 rc = bnxt_hwrm_set_coal(bp);
8609 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
8612 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8613 rc = bnxt_setup_nitroa0_vnic(bp);
8615 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
8620 bnxt_hwrm_func_qcfg(bp);
8621 netdev_update_features(bp->dev);
8627 bnxt_hwrm_resource_free(bp, 0, true);
8632 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
8634 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
8638 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
8640 bnxt_init_cp_rings(bp);
8641 bnxt_init_rx_rings(bp);
8642 bnxt_init_tx_rings(bp);
8643 bnxt_init_ring_grps(bp, irq_re_init);
8644 bnxt_init_vnics(bp);
8646 return bnxt_init_chip(bp, irq_re_init);
8649 static int bnxt_set_real_num_queues(struct bnxt *bp)
8652 struct net_device *dev = bp->dev;
8654 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
8655 bp->tx_nr_rings_xdp);
8659 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
8663 #ifdef CONFIG_RFS_ACCEL
8664 if (bp->flags & BNXT_FLAG_RFS)
8665 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
8671 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
8674 int _rx = *rx, _tx = *tx;
8677 *rx = min_t(int, _rx, max);
8678 *tx = min_t(int, _tx, max);
8683 while (_rx + _tx > max) {
8684 if (_rx > _tx && _rx > 1)
8695 static void bnxt_setup_msix(struct bnxt *bp)
8697 const int len = sizeof(bp->irq_tbl[0].name);
8698 struct net_device *dev = bp->dev;
8701 tcs = netdev_get_num_tc(dev);
8705 for (i = 0; i < tcs; i++) {
8706 count = bp->tx_nr_rings_per_tc;
8708 netdev_set_tc_queue(dev, i, count, off);
8712 for (i = 0; i < bp->cp_nr_rings; i++) {
8713 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
8716 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8718 else if (i < bp->rx_nr_rings)
8723 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
8725 bp->irq_tbl[map_idx].handler = bnxt_msix;
8729 static void bnxt_setup_inta(struct bnxt *bp)
8731 const int len = sizeof(bp->irq_tbl[0].name);
8733 if (netdev_get_num_tc(bp->dev))
8734 netdev_reset_tc(bp->dev);
8736 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
8738 bp->irq_tbl[0].handler = bnxt_inta;
8741 static int bnxt_init_int_mode(struct bnxt *bp);
8743 static int bnxt_setup_int_mode(struct bnxt *bp)
8748 rc = bnxt_init_int_mode(bp);
8749 if (rc || !bp->irq_tbl)
8750 return rc ?: -ENODEV;
8753 if (bp->flags & BNXT_FLAG_USING_MSIX)
8754 bnxt_setup_msix(bp);
8756 bnxt_setup_inta(bp);
8758 rc = bnxt_set_real_num_queues(bp);
8762 #ifdef CONFIG_RFS_ACCEL
8763 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
8765 return bp->hw_resc.max_rsscos_ctxs;
8768 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
8770 return bp->hw_resc.max_vnics;
8774 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
8776 return bp->hw_resc.max_stat_ctxs;
8779 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
8781 return bp->hw_resc.max_cp_rings;
8784 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
8786 unsigned int cp = bp->hw_resc.max_cp_rings;
8788 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
8789 cp -= bnxt_get_ulp_msix_num(bp);
8794 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
8796 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8798 if (bp->flags & BNXT_FLAG_CHIP_P5)
8799 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
8801 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
8804 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
8806 bp->hw_resc.max_irqs = max_irqs;
8809 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
8813 cp = bnxt_get_max_func_cp_rings_for_en(bp);
8814 if (bp->flags & BNXT_FLAG_CHIP_P5)
8815 return cp - bp->rx_nr_rings - bp->tx_nr_rings;
8817 return cp - bp->cp_nr_rings;
8820 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
8822 return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
8825 int bnxt_get_avail_msix(struct bnxt *bp, int num)
8827 int max_cp = bnxt_get_max_func_cp_rings(bp);
8828 int max_irq = bnxt_get_max_func_irqs(bp);
8829 int total_req = bp->cp_nr_rings + num;
8830 int max_idx, avail_msix;
8832 max_idx = bp->total_irqs;
8833 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
8834 max_idx = min_t(int, bp->total_irqs, max_cp);
8835 avail_msix = max_idx - bp->cp_nr_rings;
8836 if (!BNXT_NEW_RM(bp) || avail_msix >= num)
8839 if (max_irq < total_req) {
8840 num = max_irq - bp->cp_nr_rings;
8847 static int bnxt_get_num_msix(struct bnxt *bp)
8849 if (!BNXT_NEW_RM(bp))
8850 return bnxt_get_max_func_irqs(bp);
8852 return bnxt_nq_rings_in_use(bp);
8855 static int bnxt_init_msix(struct bnxt *bp)
8857 int i, total_vecs, max, rc = 0, min = 1, ulp_msix;
8858 struct msix_entry *msix_ent;
8860 total_vecs = bnxt_get_num_msix(bp);
8861 max = bnxt_get_max_func_irqs(bp);
8862 if (total_vecs > max)
8868 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
8872 for (i = 0; i < total_vecs; i++) {
8873 msix_ent[i].entry = i;
8874 msix_ent[i].vector = 0;
8877 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
8880 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
8881 ulp_msix = bnxt_get_ulp_msix_num(bp);
8882 if (total_vecs < 0 || total_vecs < ulp_msix) {
8884 goto msix_setup_exit;
8887 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
8889 for (i = 0; i < total_vecs; i++)
8890 bp->irq_tbl[i].vector = msix_ent[i].vector;
8892 bp->total_irqs = total_vecs;
8893 /* Trim rings based upon num of vectors allocated */
8894 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
8895 total_vecs - ulp_msix, min == 1);
8897 goto msix_setup_exit;
8899 bp->cp_nr_rings = (min == 1) ?
8900 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
8901 bp->tx_nr_rings + bp->rx_nr_rings;
8905 goto msix_setup_exit;
8907 bp->flags |= BNXT_FLAG_USING_MSIX;
8912 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
8915 pci_disable_msix(bp->pdev);
8920 static int bnxt_init_inta(struct bnxt *bp)
8922 bp->irq_tbl = kzalloc(sizeof(struct bnxt_irq), GFP_KERNEL);
8927 bp->rx_nr_rings = 1;
8928 bp->tx_nr_rings = 1;
8929 bp->cp_nr_rings = 1;
8930 bp->flags |= BNXT_FLAG_SHARED_RINGS;
8931 bp->irq_tbl[0].vector = bp->pdev->irq;
8935 static int bnxt_init_int_mode(struct bnxt *bp)
8939 if (bp->flags & BNXT_FLAG_MSIX_CAP)
8940 rc = bnxt_init_msix(bp);
8942 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
8943 /* fallback to INTA */
8944 rc = bnxt_init_inta(bp);
8949 static void bnxt_clear_int_mode(struct bnxt *bp)
8951 if (bp->flags & BNXT_FLAG_USING_MSIX)
8952 pci_disable_msix(bp->pdev);
8956 bp->flags &= ~BNXT_FLAG_USING_MSIX;
8959 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
8961 int tcs = netdev_get_num_tc(bp->dev);
8962 bool irq_cleared = false;
8965 if (!bnxt_need_reserve_rings(bp))
8968 if (irq_re_init && BNXT_NEW_RM(bp) &&
8969 bnxt_get_num_msix(bp) != bp->total_irqs) {
8970 bnxt_ulp_irq_stop(bp);
8971 bnxt_clear_int_mode(bp);
8974 rc = __bnxt_reserve_rings(bp);
8977 rc = bnxt_init_int_mode(bp);
8978 bnxt_ulp_irq_restart(bp, rc);
8981 netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
8984 if (tcs && (bp->tx_nr_rings_per_tc * tcs != bp->tx_nr_rings)) {
8985 netdev_err(bp->dev, "tx ring reservation failure\n");
8986 netdev_reset_tc(bp->dev);
8987 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
8993 static void bnxt_free_irq(struct bnxt *bp)
8995 struct bnxt_irq *irq;
8998 #ifdef CONFIG_RFS_ACCEL
8999 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
9000 bp->dev->rx_cpu_rmap = NULL;
9002 if (!bp->irq_tbl || !bp->bnapi)
9005 for (i = 0; i < bp->cp_nr_rings; i++) {
9006 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
9008 irq = &bp->irq_tbl[map_idx];
9009 if (irq->requested) {
9010 if (irq->have_cpumask) {
9011 irq_set_affinity_hint(irq->vector, NULL);
9012 free_cpumask_var(irq->cpu_mask);
9013 irq->have_cpumask = 0;
9015 free_irq(irq->vector, bp->bnapi[i]);
9022 static int bnxt_request_irq(struct bnxt *bp)
9025 unsigned long flags = 0;
9026 #ifdef CONFIG_RFS_ACCEL
9027 struct cpu_rmap *rmap;
9030 rc = bnxt_setup_int_mode(bp);
9032 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
9036 #ifdef CONFIG_RFS_ACCEL
9037 rmap = bp->dev->rx_cpu_rmap;
9039 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
9040 flags = IRQF_SHARED;
9042 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
9043 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
9044 struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
9046 #ifdef CONFIG_RFS_ACCEL
9047 if (rmap && bp->bnapi[i]->rx_ring) {
9048 rc = irq_cpu_rmap_add(rmap, irq->vector);
9050 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
9055 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
9062 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
9063 int numa_node = dev_to_node(&bp->pdev->dev);
9065 irq->have_cpumask = 1;
9066 cpumask_set_cpu(cpumask_local_spread(i, numa_node),
9068 rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
9070 netdev_warn(bp->dev,
9071 "Set affinity failed, IRQ = %d\n",
9080 static void bnxt_del_napi(struct bnxt *bp)
9087 for (i = 0; i < bp->cp_nr_rings; i++) {
9088 struct bnxt_napi *bnapi = bp->bnapi[i];
9090 __netif_napi_del(&bnapi->napi);
9092 /* We called __netif_napi_del(), we need
9093 * to respect an RCU grace period before freeing napi structures.
9098 static void bnxt_init_napi(struct bnxt *bp)
9101 unsigned int cp_nr_rings = bp->cp_nr_rings;
9102 struct bnxt_napi *bnapi;
9104 if (bp->flags & BNXT_FLAG_USING_MSIX) {
9105 int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
9107 if (bp->flags & BNXT_FLAG_CHIP_P5)
9108 poll_fn = bnxt_poll_p5;
9109 else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
9111 for (i = 0; i < cp_nr_rings; i++) {
9112 bnapi = bp->bnapi[i];
9113 netif_napi_add(bp->dev, &bnapi->napi, poll_fn, 64);
9115 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
9116 bnapi = bp->bnapi[cp_nr_rings];
9117 netif_napi_add(bp->dev, &bnapi->napi,
9118 bnxt_poll_nitroa0, 64);
9121 bnapi = bp->bnapi[0];
9122 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
9126 static void bnxt_disable_napi(struct bnxt *bp)
9131 test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
9134 for (i = 0; i < bp->cp_nr_rings; i++) {
9135 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
9137 if (bp->bnapi[i]->rx_ring)
9138 cancel_work_sync(&cpr->dim.work);
9140 napi_disable(&bp->bnapi[i]->napi);
9144 static void bnxt_enable_napi(struct bnxt *bp)
9148 clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
9149 for (i = 0; i < bp->cp_nr_rings; i++) {
9150 struct bnxt_napi *bnapi = bp->bnapi[i];
9151 struct bnxt_cp_ring_info *cpr;
9153 cpr = &bnapi->cp_ring;
9154 if (bnapi->in_reset)
9155 cpr->sw_stats.rx.rx_resets++;
9156 bnapi->in_reset = false;
9158 if (bnapi->rx_ring) {
9159 INIT_WORK(&cpr->dim.work, bnxt_dim_work);
9160 cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
9162 napi_enable(&bnapi->napi);
9166 void bnxt_tx_disable(struct bnxt *bp)
9169 struct bnxt_tx_ring_info *txr;
9172 for (i = 0; i < bp->tx_nr_rings; i++) {
9173 txr = &bp->tx_ring[i];
9174 WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
9177 /* Make sure napi polls see @dev_state change */
9179 /* Drop carrier first to prevent TX timeout */
9180 netif_carrier_off(bp->dev);
9181 /* Stop all TX queues */
9182 netif_tx_disable(bp->dev);
9185 void bnxt_tx_enable(struct bnxt *bp)
9188 struct bnxt_tx_ring_info *txr;
9190 for (i = 0; i < bp->tx_nr_rings; i++) {
9191 txr = &bp->tx_ring[i];
9192 WRITE_ONCE(txr->dev_state, 0);
9194 /* Make sure napi polls see @dev_state change */
9196 netif_tx_wake_all_queues(bp->dev);
9197 if (bp->link_info.link_up)
9198 netif_carrier_on(bp->dev);
9201 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
9203 u8 active_fec = link_info->active_fec_sig_mode &
9204 PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
9206 switch (active_fec) {
9208 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
9210 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
9211 return "Clause 74 BaseR";
9212 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
9213 return "Clause 91 RS(528,514)";
9214 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
9215 return "Clause 91 RS544_1XN";
9216 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
9217 return "Clause 91 RS(544,514)";
9218 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
9219 return "Clause 91 RS272_1XN";
9220 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
9221 return "Clause 91 RS(272,257)";
9225 static void bnxt_report_link(struct bnxt *bp)
9227 if (bp->link_info.link_up) {
9228 const char *signal = "";
9229 const char *flow_ctrl;
9234 netif_carrier_on(bp->dev);
9235 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
9236 if (speed == SPEED_UNKNOWN) {
9237 netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
9240 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
9244 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
9245 flow_ctrl = "ON - receive & transmit";
9246 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
9247 flow_ctrl = "ON - transmit";
9248 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
9249 flow_ctrl = "ON - receive";
9252 if (bp->link_info.phy_qcfg_resp.option_flags &
9253 PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
9254 u8 sig_mode = bp->link_info.active_fec_sig_mode &
9255 PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
9257 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
9260 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
9267 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
9268 speed, signal, duplex, flow_ctrl);
9269 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
9270 netdev_info(bp->dev, "EEE is %s\n",
9271 bp->eee.eee_active ? "active" :
9273 fec = bp->link_info.fec_cfg;
9274 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
9275 netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
9276 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
9277 bnxt_report_fec(&bp->link_info));
9279 netif_carrier_off(bp->dev);
9280 netdev_err(bp->dev, "NIC Link is Down\n");
9284 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
9286 if (!resp->supported_speeds_auto_mode &&
9287 !resp->supported_speeds_force_mode &&
9288 !resp->supported_pam4_speeds_auto_mode &&
9289 !resp->supported_pam4_speeds_force_mode)
9294 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
9297 struct hwrm_port_phy_qcaps_input req = {0};
9298 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
9299 struct bnxt_link_info *link_info = &bp->link_info;
9301 if (bp->hwrm_spec_code < 0x10201)
9304 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
9306 mutex_lock(&bp->hwrm_cmd_lock);
9307 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9309 goto hwrm_phy_qcaps_exit;
9311 bp->phy_flags = resp->flags;
9312 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
9313 struct ethtool_eee *eee = &bp->eee;
9314 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
9316 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9317 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
9318 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
9319 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
9320 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
9323 if (bp->hwrm_spec_code >= 0x10a01) {
9324 if (bnxt_phy_qcaps_no_speed(resp)) {
9325 link_info->phy_state = BNXT_PHY_STATE_DISABLED;
9326 netdev_warn(bp->dev, "Ethernet link disabled\n");
9327 } else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
9328 link_info->phy_state = BNXT_PHY_STATE_ENABLED;
9329 netdev_info(bp->dev, "Ethernet link enabled\n");
9330 /* Phy re-enabled, reprobe the speeds */
9331 link_info->support_auto_speeds = 0;
9332 link_info->support_pam4_auto_speeds = 0;
9335 if (resp->supported_speeds_auto_mode)
9336 link_info->support_auto_speeds =
9337 le16_to_cpu(resp->supported_speeds_auto_mode);
9338 if (resp->supported_pam4_speeds_auto_mode)
9339 link_info->support_pam4_auto_speeds =
9340 le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
9342 bp->port_count = resp->port_cnt;
9344 hwrm_phy_qcaps_exit:
9345 mutex_unlock(&bp->hwrm_cmd_lock);
9349 static bool bnxt_support_dropped(u16 advertising, u16 supported)
9351 u16 diff = advertising ^ supported;
9353 return ((supported | diff) != supported);
9356 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
9359 struct bnxt_link_info *link_info = &bp->link_info;
9360 struct hwrm_port_phy_qcfg_input req = {0};
9361 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
9362 u8 link_up = link_info->link_up;
9363 bool support_changed = false;
9365 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
9367 mutex_lock(&bp->hwrm_cmd_lock);
9368 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9370 mutex_unlock(&bp->hwrm_cmd_lock);
9374 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
9375 link_info->phy_link_status = resp->link;
9376 link_info->duplex = resp->duplex_cfg;
9377 if (bp->hwrm_spec_code >= 0x10800)
9378 link_info->duplex = resp->duplex_state;
9379 link_info->pause = resp->pause;
9380 link_info->auto_mode = resp->auto_mode;
9381 link_info->auto_pause_setting = resp->auto_pause;
9382 link_info->lp_pause = resp->link_partner_adv_pause;
9383 link_info->force_pause_setting = resp->force_pause;
9384 link_info->duplex_setting = resp->duplex_cfg;
9385 if (link_info->phy_link_status == BNXT_LINK_LINK)
9386 link_info->link_speed = le16_to_cpu(resp->link_speed);
9388 link_info->link_speed = 0;
9389 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
9390 link_info->force_pam4_link_speed =
9391 le16_to_cpu(resp->force_pam4_link_speed);
9392 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
9393 link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
9394 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
9395 link_info->auto_pam4_link_speeds =
9396 le16_to_cpu(resp->auto_pam4_link_speed_mask);
9397 link_info->lp_auto_link_speeds =
9398 le16_to_cpu(resp->link_partner_adv_speeds);
9399 link_info->lp_auto_pam4_link_speeds =
9400 resp->link_partner_pam4_adv_speeds;
9401 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
9402 link_info->phy_ver[0] = resp->phy_maj;
9403 link_info->phy_ver[1] = resp->phy_min;
9404 link_info->phy_ver[2] = resp->phy_bld;
9405 link_info->media_type = resp->media_type;
9406 link_info->phy_type = resp->phy_type;
9407 link_info->transceiver = resp->xcvr_pkg_type;
9408 link_info->phy_addr = resp->eee_config_phy_addr &
9409 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
9410 link_info->module_status = resp->module_status;
9412 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
9413 struct ethtool_eee *eee = &bp->eee;
9416 eee->eee_active = 0;
9417 if (resp->eee_config_phy_addr &
9418 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
9419 eee->eee_active = 1;
9420 fw_speeds = le16_to_cpu(
9421 resp->link_partner_adv_eee_link_speed_mask);
9422 eee->lp_advertised =
9423 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9426 /* Pull initial EEE config */
9427 if (!chng_link_state) {
9428 if (resp->eee_config_phy_addr &
9429 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
9430 eee->eee_enabled = 1;
9432 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
9434 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9436 if (resp->eee_config_phy_addr &
9437 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
9440 eee->tx_lpi_enabled = 1;
9441 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
9442 eee->tx_lpi_timer = le32_to_cpu(tmr) &
9443 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
9448 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
9449 if (bp->hwrm_spec_code >= 0x10504) {
9450 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
9451 link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
9453 /* TODO: need to add more logic to report VF link */
9454 if (chng_link_state) {
9455 if (link_info->phy_link_status == BNXT_LINK_LINK)
9456 link_info->link_up = 1;
9458 link_info->link_up = 0;
9459 if (link_up != link_info->link_up)
9460 bnxt_report_link(bp);
9462 /* alwasy link down if not require to update link state */
9463 link_info->link_up = 0;
9465 mutex_unlock(&bp->hwrm_cmd_lock);
9467 if (!BNXT_PHY_CFG_ABLE(bp))
9470 /* Check if any advertised speeds are no longer supported. The caller
9471 * holds the link_lock mutex, so we can modify link_info settings.
9473 if (bnxt_support_dropped(link_info->advertising,
9474 link_info->support_auto_speeds)) {
9475 link_info->advertising = link_info->support_auto_speeds;
9476 support_changed = true;
9478 if (bnxt_support_dropped(link_info->advertising_pam4,
9479 link_info->support_pam4_auto_speeds)) {
9480 link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
9481 support_changed = true;
9483 if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
9484 bnxt_hwrm_set_link_setting(bp, true, false);
9488 static void bnxt_get_port_module_status(struct bnxt *bp)
9490 struct bnxt_link_info *link_info = &bp->link_info;
9491 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
9494 if (bnxt_update_link(bp, true))
9497 module_status = link_info->module_status;
9498 switch (module_status) {
9499 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
9500 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
9501 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
9502 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
9504 if (bp->hwrm_spec_code >= 0x10201) {
9505 netdev_warn(bp->dev, "Module part number %s\n",
9506 resp->phy_vendor_partnumber);
9508 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
9509 netdev_warn(bp->dev, "TX is disabled\n");
9510 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
9511 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
9516 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
9518 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
9519 if (bp->hwrm_spec_code >= 0x10201)
9521 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
9522 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
9523 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
9524 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
9525 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
9527 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
9529 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
9530 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
9531 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
9532 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
9534 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
9535 if (bp->hwrm_spec_code >= 0x10201) {
9536 req->auto_pause = req->force_pause;
9537 req->enables |= cpu_to_le32(
9538 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
9543 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
9545 if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
9546 req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
9547 if (bp->link_info.advertising) {
9548 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
9549 req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
9551 if (bp->link_info.advertising_pam4) {
9553 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
9554 req->auto_link_pam4_speed_mask =
9555 cpu_to_le16(bp->link_info.advertising_pam4);
9557 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
9558 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
9560 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
9561 if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
9562 req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
9563 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
9565 req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
9569 /* tell chimp that the setting takes effect immediately */
9570 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
9573 int bnxt_hwrm_set_pause(struct bnxt *bp)
9575 struct hwrm_port_phy_cfg_input req = {0};
9578 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
9579 bnxt_hwrm_set_pause_common(bp, &req);
9581 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
9582 bp->link_info.force_link_chng)
9583 bnxt_hwrm_set_link_common(bp, &req);
9585 mutex_lock(&bp->hwrm_cmd_lock);
9586 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9587 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
9588 /* since changing of pause setting doesn't trigger any link
9589 * change event, the driver needs to update the current pause
9590 * result upon successfully return of the phy_cfg command
9592 bp->link_info.pause =
9593 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
9594 bp->link_info.auto_pause_setting = 0;
9595 if (!bp->link_info.force_link_chng)
9596 bnxt_report_link(bp);
9598 bp->link_info.force_link_chng = false;
9599 mutex_unlock(&bp->hwrm_cmd_lock);
9603 static void bnxt_hwrm_set_eee(struct bnxt *bp,
9604 struct hwrm_port_phy_cfg_input *req)
9606 struct ethtool_eee *eee = &bp->eee;
9608 if (eee->eee_enabled) {
9610 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
9612 if (eee->tx_lpi_enabled)
9613 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
9615 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
9617 req->flags |= cpu_to_le32(flags);
9618 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
9619 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
9620 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
9622 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
9626 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
9628 struct hwrm_port_phy_cfg_input req = {0};
9630 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
9632 bnxt_hwrm_set_pause_common(bp, &req);
9634 bnxt_hwrm_set_link_common(bp, &req);
9637 bnxt_hwrm_set_eee(bp, &req);
9638 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9641 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
9643 struct hwrm_port_phy_cfg_input req = {0};
9645 if (!BNXT_SINGLE_PF(bp))
9648 if (pci_num_vf(bp->pdev) &&
9649 !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
9652 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
9653 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
9654 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9657 static int bnxt_fw_init_one(struct bnxt *bp);
9659 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
9661 #ifdef CONFIG_TEE_BNXT_FW
9662 int rc = tee_bnxt_fw_load();
9665 netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
9669 netdev_err(bp->dev, "OP-TEE not supported\n");
9674 static int bnxt_try_recover_fw(struct bnxt *bp)
9676 if (bp->fw_health && bp->fw_health->status_reliable) {
9680 mutex_lock(&bp->hwrm_cmd_lock);
9682 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
9683 rc = __bnxt_hwrm_ver_get(bp, true);
9684 if (!BNXT_FW_IS_BOOTING(sts) &&
9685 !BNXT_FW_IS_RECOVERING(sts))
9688 } while (rc == -EBUSY && retry < BNXT_FW_RETRY);
9689 mutex_unlock(&bp->hwrm_cmd_lock);
9691 if (!BNXT_FW_IS_HEALTHY(sts)) {
9693 "Firmware not responding, status: 0x%x\n",
9697 if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
9698 netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
9699 return bnxt_fw_reset_via_optee(bp);
9707 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
9709 struct hwrm_func_drv_if_change_output *resp = bp->hwrm_cmd_resp_addr;
9710 struct hwrm_func_drv_if_change_input req = {0};
9711 bool fw_reset = !bp->irq_tbl;
9712 bool resc_reinit = false;
9716 if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
9719 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_IF_CHANGE, -1, -1);
9721 req.flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
9722 mutex_lock(&bp->hwrm_cmd_lock);
9723 while (retry < BNXT_FW_IF_RETRY) {
9724 rc = _hwrm_send_message(bp, &req, sizeof(req),
9733 flags = le32_to_cpu(resp->flags);
9734 mutex_unlock(&bp->hwrm_cmd_lock);
9739 rc = bnxt_try_recover_fw(bp);
9746 bnxt_inv_fw_health_reg(bp);
9750 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
9752 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE)
9754 else if (bp->fw_health && !bp->fw_health->status_reliable)
9755 bnxt_try_map_fw_health_reg(bp);
9757 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
9758 netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
9759 set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
9762 if (resc_reinit || fw_reset) {
9764 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
9765 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
9767 bnxt_free_ctx_mem(bp);
9771 rc = bnxt_fw_init_one(bp);
9773 clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
9774 set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
9777 bnxt_clear_int_mode(bp);
9778 rc = bnxt_init_int_mode(bp);
9780 clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
9781 netdev_err(bp->dev, "init int mode failed\n");
9785 if (BNXT_NEW_RM(bp)) {
9786 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9788 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9790 netdev_err(bp->dev, "resc_qcaps failed\n");
9792 hw_resc->resv_cp_rings = 0;
9793 hw_resc->resv_stat_ctxs = 0;
9794 hw_resc->resv_irqs = 0;
9795 hw_resc->resv_tx_rings = 0;
9796 hw_resc->resv_rx_rings = 0;
9797 hw_resc->resv_hw_ring_grps = 0;
9798 hw_resc->resv_vnics = 0;
9800 bp->tx_nr_rings = 0;
9801 bp->rx_nr_rings = 0;
9808 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
9810 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
9811 struct hwrm_port_led_qcaps_input req = {0};
9812 struct bnxt_pf_info *pf = &bp->pf;
9816 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
9819 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
9820 req.port_id = cpu_to_le16(pf->port_id);
9821 mutex_lock(&bp->hwrm_cmd_lock);
9822 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9824 mutex_unlock(&bp->hwrm_cmd_lock);
9827 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
9830 bp->num_leds = resp->num_leds;
9831 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
9833 for (i = 0; i < bp->num_leds; i++) {
9834 struct bnxt_led_info *led = &bp->leds[i];
9835 __le16 caps = led->led_state_caps;
9837 if (!led->led_group_id ||
9838 !BNXT_LED_ALT_BLINK_CAP(caps)) {
9844 mutex_unlock(&bp->hwrm_cmd_lock);
9848 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
9850 struct hwrm_wol_filter_alloc_input req = {0};
9851 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
9854 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
9855 req.port_id = cpu_to_le16(bp->pf.port_id);
9856 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
9857 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
9858 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
9859 mutex_lock(&bp->hwrm_cmd_lock);
9860 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9862 bp->wol_filter_id = resp->wol_filter_id;
9863 mutex_unlock(&bp->hwrm_cmd_lock);
9867 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
9869 struct hwrm_wol_filter_free_input req = {0};
9871 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
9872 req.port_id = cpu_to_le16(bp->pf.port_id);
9873 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
9874 req.wol_filter_id = bp->wol_filter_id;
9875 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9878 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
9880 struct hwrm_wol_filter_qcfg_input req = {0};
9881 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
9882 u16 next_handle = 0;
9885 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
9886 req.port_id = cpu_to_le16(bp->pf.port_id);
9887 req.handle = cpu_to_le16(handle);
9888 mutex_lock(&bp->hwrm_cmd_lock);
9889 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9891 next_handle = le16_to_cpu(resp->next_handle);
9892 if (next_handle != 0) {
9893 if (resp->wol_type ==
9894 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
9896 bp->wol_filter_id = resp->wol_filter_id;
9900 mutex_unlock(&bp->hwrm_cmd_lock);
9904 static void bnxt_get_wol_settings(struct bnxt *bp)
9909 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
9913 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
9914 } while (handle && handle != 0xffff);
9917 #ifdef CONFIG_BNXT_HWMON
9918 static ssize_t bnxt_show_temp(struct device *dev,
9919 struct device_attribute *devattr, char *buf)
9921 struct hwrm_temp_monitor_query_input req = {0};
9922 struct hwrm_temp_monitor_query_output *resp;
9923 struct bnxt *bp = dev_get_drvdata(dev);
9927 resp = bp->hwrm_cmd_resp_addr;
9928 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TEMP_MONITOR_QUERY, -1, -1);
9929 mutex_lock(&bp->hwrm_cmd_lock);
9930 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9932 len = sprintf(buf, "%u\n", resp->temp * 1000); /* display millidegree */
9933 mutex_unlock(&bp->hwrm_cmd_lock);
9938 static SENSOR_DEVICE_ATTR(temp1_input, 0444, bnxt_show_temp, NULL, 0);
9940 static struct attribute *bnxt_attrs[] = {
9941 &sensor_dev_attr_temp1_input.dev_attr.attr,
9944 ATTRIBUTE_GROUPS(bnxt);
9946 static void bnxt_hwmon_close(struct bnxt *bp)
9948 if (bp->hwmon_dev) {
9949 hwmon_device_unregister(bp->hwmon_dev);
9950 bp->hwmon_dev = NULL;
9954 static void bnxt_hwmon_open(struct bnxt *bp)
9956 struct hwrm_temp_monitor_query_input req = {0};
9957 struct pci_dev *pdev = bp->pdev;
9960 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TEMP_MONITOR_QUERY, -1, -1);
9961 rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9962 if (rc == -EACCES || rc == -EOPNOTSUPP) {
9963 bnxt_hwmon_close(bp);
9970 bp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev,
9971 DRV_MODULE_NAME, bp,
9973 if (IS_ERR(bp->hwmon_dev)) {
9974 bp->hwmon_dev = NULL;
9975 dev_warn(&pdev->dev, "Cannot register hwmon device\n");
9979 static void bnxt_hwmon_close(struct bnxt *bp)
9983 static void bnxt_hwmon_open(struct bnxt *bp)
9988 static bool bnxt_eee_config_ok(struct bnxt *bp)
9990 struct ethtool_eee *eee = &bp->eee;
9991 struct bnxt_link_info *link_info = &bp->link_info;
9993 if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
9996 if (eee->eee_enabled) {
9998 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
10000 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
10001 eee->eee_enabled = 0;
10004 if (eee->advertised & ~advertising) {
10005 eee->advertised = advertising & eee->supported;
10012 static int bnxt_update_phy_setting(struct bnxt *bp)
10015 bool update_link = false;
10016 bool update_pause = false;
10017 bool update_eee = false;
10018 struct bnxt_link_info *link_info = &bp->link_info;
10020 rc = bnxt_update_link(bp, true);
10022 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
10026 if (!BNXT_SINGLE_PF(bp))
10029 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
10030 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
10031 link_info->req_flow_ctrl)
10032 update_pause = true;
10033 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
10034 link_info->force_pause_setting != link_info->req_flow_ctrl)
10035 update_pause = true;
10036 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
10037 if (BNXT_AUTO_MODE(link_info->auto_mode))
10038 update_link = true;
10039 if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
10040 link_info->req_link_speed != link_info->force_link_speed)
10041 update_link = true;
10042 else if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
10043 link_info->req_link_speed != link_info->force_pam4_link_speed)
10044 update_link = true;
10045 if (link_info->req_duplex != link_info->duplex_setting)
10046 update_link = true;
10048 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
10049 update_link = true;
10050 if (link_info->advertising != link_info->auto_link_speeds ||
10051 link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
10052 update_link = true;
10055 /* The last close may have shutdown the link, so need to call
10056 * PHY_CFG to bring it back up.
10058 if (!bp->link_info.link_up)
10059 update_link = true;
10061 if (!bnxt_eee_config_ok(bp))
10065 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
10066 else if (update_pause)
10067 rc = bnxt_hwrm_set_pause(bp);
10069 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
10077 /* Common routine to pre-map certain register block to different GRC window.
10078 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
10079 * in PF and 3 windows in VF that can be customized to map in different
10082 static void bnxt_preset_reg_win(struct bnxt *bp)
10085 /* CAG registers map to GRC window #4 */
10086 writel(BNXT_CAG_REG_BASE,
10087 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
10091 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
10093 static int bnxt_reinit_after_abort(struct bnxt *bp)
10097 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
10100 if (bp->dev->reg_state == NETREG_UNREGISTERED)
10103 rc = bnxt_fw_init_one(bp);
10105 bnxt_clear_int_mode(bp);
10106 rc = bnxt_init_int_mode(bp);
10108 clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10109 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
10115 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10119 bnxt_preset_reg_win(bp);
10120 netif_carrier_off(bp->dev);
10122 /* Reserve rings now if none were reserved at driver probe. */
10123 rc = bnxt_init_dflt_ring_mode(bp);
10125 netdev_err(bp->dev, "Failed to reserve default rings at open\n");
10129 rc = bnxt_reserve_rings(bp, irq_re_init);
10132 if ((bp->flags & BNXT_FLAG_RFS) &&
10133 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
10134 /* disable RFS if falling back to INTA */
10135 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
10136 bp->flags &= ~BNXT_FLAG_RFS;
10139 rc = bnxt_alloc_mem(bp, irq_re_init);
10141 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
10142 goto open_err_free_mem;
10146 bnxt_init_napi(bp);
10147 rc = bnxt_request_irq(bp);
10149 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
10154 rc = bnxt_init_nic(bp, irq_re_init);
10156 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
10160 bnxt_enable_napi(bp);
10161 bnxt_debug_dev_init(bp);
10163 if (link_re_init) {
10164 mutex_lock(&bp->link_lock);
10165 rc = bnxt_update_phy_setting(bp);
10166 mutex_unlock(&bp->link_lock);
10168 netdev_warn(bp->dev, "failed to update phy settings\n");
10169 if (BNXT_SINGLE_PF(bp)) {
10170 bp->link_info.phy_retry = true;
10171 bp->link_info.phy_retry_expires =
10178 udp_tunnel_nic_reset_ntf(bp->dev);
10180 set_bit(BNXT_STATE_OPEN, &bp->state);
10181 bnxt_enable_int(bp);
10182 /* Enable TX queues */
10183 bnxt_tx_enable(bp);
10184 mod_timer(&bp->timer, jiffies + bp->current_interval);
10185 /* Poll link status and check for SFP+ module status */
10186 bnxt_get_port_module_status(bp);
10188 /* VF-reps may need to be re-opened after the PF is re-opened */
10190 bnxt_vf_reps_open(bp);
10197 bnxt_free_skbs(bp);
10199 bnxt_free_mem(bp, true);
10203 /* rtnl_lock held */
10204 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10208 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
10211 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
10213 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
10214 dev_close(bp->dev);
10219 /* rtnl_lock held, open the NIC half way by allocating all resources, but
10220 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
10223 int bnxt_half_open_nic(struct bnxt *bp)
10227 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
10228 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
10230 goto half_open_err;
10233 rc = bnxt_alloc_mem(bp, false);
10235 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
10236 goto half_open_err;
10238 rc = bnxt_init_nic(bp, false);
10240 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
10241 goto half_open_err;
10246 bnxt_free_skbs(bp);
10247 bnxt_free_mem(bp, false);
10248 dev_close(bp->dev);
10252 /* rtnl_lock held, this call can only be made after a previous successful
10253 * call to bnxt_half_open_nic().
10255 void bnxt_half_close_nic(struct bnxt *bp)
10257 bnxt_hwrm_resource_free(bp, false, false);
10258 bnxt_free_skbs(bp);
10259 bnxt_free_mem(bp, false);
10262 static void bnxt_reenable_sriov(struct bnxt *bp)
10265 struct bnxt_pf_info *pf = &bp->pf;
10266 int n = pf->active_vfs;
10269 bnxt_cfg_hw_sriov(bp, &n, true);
10273 static int bnxt_open(struct net_device *dev)
10275 struct bnxt *bp = netdev_priv(dev);
10278 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
10279 rc = bnxt_reinit_after_abort(bp);
10282 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
10284 netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
10289 rc = bnxt_hwrm_if_change(bp, true);
10293 if (bnxt_ptp_init(bp)) {
10294 netdev_warn(dev, "PTP initialization failed.\n");
10295 kfree(bp->ptp_cfg);
10296 bp->ptp_cfg = NULL;
10298 rc = __bnxt_open_nic(bp, true, true);
10300 bnxt_hwrm_if_change(bp, false);
10301 bnxt_ptp_clear(bp);
10303 if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
10304 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
10305 bnxt_ulp_start(bp, 0);
10306 bnxt_reenable_sriov(bp);
10309 bnxt_hwmon_open(bp);
10315 static bool bnxt_drv_busy(struct bnxt *bp)
10317 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
10318 test_bit(BNXT_STATE_READ_STATS, &bp->state));
10321 static void bnxt_get_ring_stats(struct bnxt *bp,
10322 struct rtnl_link_stats64 *stats);
10324 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
10327 /* Close the VF-reps before closing PF */
10329 bnxt_vf_reps_close(bp);
10331 /* Change device state to avoid TX queue wake up's */
10332 bnxt_tx_disable(bp);
10334 clear_bit(BNXT_STATE_OPEN, &bp->state);
10335 smp_mb__after_atomic();
10336 while (bnxt_drv_busy(bp))
10339 /* Flush rings and and disable interrupts */
10340 bnxt_shutdown_nic(bp, irq_re_init);
10342 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
10344 bnxt_debug_dev_exit(bp);
10345 bnxt_disable_napi(bp);
10346 del_timer_sync(&bp->timer);
10347 bnxt_free_skbs(bp);
10349 /* Save ring stats before shutdown */
10350 if (bp->bnapi && irq_re_init)
10351 bnxt_get_ring_stats(bp, &bp->net_stats_prev);
10356 bnxt_free_mem(bp, irq_re_init);
10359 int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10363 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
10364 /* If we get here, it means firmware reset is in progress
10365 * while we are trying to close. We can safely proceed with
10366 * the close because we are holding rtnl_lock(). Some firmware
10367 * messages may fail as we proceed to close. We set the
10368 * ABORT_ERR flag here so that the FW reset thread will later
10369 * abort when it gets the rtnl_lock() and sees the flag.
10371 netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
10372 set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10375 #ifdef CONFIG_BNXT_SRIOV
10376 if (bp->sriov_cfg) {
10377 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
10379 BNXT_SRIOV_CFG_WAIT_TMO);
10381 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
10384 __bnxt_close_nic(bp, irq_re_init, link_re_init);
10388 static int bnxt_close(struct net_device *dev)
10390 struct bnxt *bp = netdev_priv(dev);
10392 bnxt_ptp_clear(bp);
10393 bnxt_hwmon_close(bp);
10394 bnxt_close_nic(bp, true, true);
10395 bnxt_hwrm_shutdown_link(bp);
10396 bnxt_hwrm_if_change(bp, false);
10400 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
10403 struct hwrm_port_phy_mdio_read_output *resp = bp->hwrm_cmd_resp_addr;
10404 struct hwrm_port_phy_mdio_read_input req = {0};
10407 if (bp->hwrm_spec_code < 0x10a00)
10408 return -EOPNOTSUPP;
10410 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_MDIO_READ, -1, -1);
10411 req.port_id = cpu_to_le16(bp->pf.port_id);
10412 req.phy_addr = phy_addr;
10413 req.reg_addr = cpu_to_le16(reg & 0x1f);
10414 if (mdio_phy_id_is_c45(phy_addr)) {
10416 req.phy_addr = mdio_phy_id_prtad(phy_addr);
10417 req.dev_addr = mdio_phy_id_devad(phy_addr);
10418 req.reg_addr = cpu_to_le16(reg);
10421 mutex_lock(&bp->hwrm_cmd_lock);
10422 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10424 *val = le16_to_cpu(resp->reg_data);
10425 mutex_unlock(&bp->hwrm_cmd_lock);
10429 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
10432 struct hwrm_port_phy_mdio_write_input req = {0};
10434 if (bp->hwrm_spec_code < 0x10a00)
10435 return -EOPNOTSUPP;
10437 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_MDIO_WRITE, -1, -1);
10438 req.port_id = cpu_to_le16(bp->pf.port_id);
10439 req.phy_addr = phy_addr;
10440 req.reg_addr = cpu_to_le16(reg & 0x1f);
10441 if (mdio_phy_id_is_c45(phy_addr)) {
10443 req.phy_addr = mdio_phy_id_prtad(phy_addr);
10444 req.dev_addr = mdio_phy_id_devad(phy_addr);
10445 req.reg_addr = cpu_to_le16(reg);
10447 req.reg_data = cpu_to_le16(val);
10449 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10452 /* rtnl_lock held */
10453 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10455 struct mii_ioctl_data *mdio = if_mii(ifr);
10456 struct bnxt *bp = netdev_priv(dev);
10461 mdio->phy_id = bp->link_info.phy_addr;
10464 case SIOCGMIIREG: {
10465 u16 mii_regval = 0;
10467 if (!netif_running(dev))
10470 rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
10472 mdio->val_out = mii_regval;
10477 if (!netif_running(dev))
10480 return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
10483 case SIOCSHWTSTAMP:
10484 return bnxt_hwtstamp_set(dev, ifr);
10486 case SIOCGHWTSTAMP:
10487 return bnxt_hwtstamp_get(dev, ifr);
10493 return -EOPNOTSUPP;
10496 static void bnxt_get_ring_stats(struct bnxt *bp,
10497 struct rtnl_link_stats64 *stats)
10501 for (i = 0; i < bp->cp_nr_rings; i++) {
10502 struct bnxt_napi *bnapi = bp->bnapi[i];
10503 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
10504 u64 *sw = cpr->stats.sw_stats;
10506 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
10507 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
10508 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
10510 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
10511 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
10512 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
10514 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
10515 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
10516 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
10518 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
10519 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
10520 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
10522 stats->rx_missed_errors +=
10523 BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
10525 stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
10527 stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
10531 static void bnxt_add_prev_stats(struct bnxt *bp,
10532 struct rtnl_link_stats64 *stats)
10534 struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
10536 stats->rx_packets += prev_stats->rx_packets;
10537 stats->tx_packets += prev_stats->tx_packets;
10538 stats->rx_bytes += prev_stats->rx_bytes;
10539 stats->tx_bytes += prev_stats->tx_bytes;
10540 stats->rx_missed_errors += prev_stats->rx_missed_errors;
10541 stats->multicast += prev_stats->multicast;
10542 stats->tx_dropped += prev_stats->tx_dropped;
10546 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
10548 struct bnxt *bp = netdev_priv(dev);
10550 set_bit(BNXT_STATE_READ_STATS, &bp->state);
10551 /* Make sure bnxt_close_nic() sees that we are reading stats before
10552 * we check the BNXT_STATE_OPEN flag.
10554 smp_mb__after_atomic();
10555 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10556 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
10557 *stats = bp->net_stats_prev;
10561 bnxt_get_ring_stats(bp, stats);
10562 bnxt_add_prev_stats(bp, stats);
10564 if (bp->flags & BNXT_FLAG_PORT_STATS) {
10565 u64 *rx = bp->port_stats.sw_stats;
10566 u64 *tx = bp->port_stats.sw_stats +
10567 BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10569 stats->rx_crc_errors =
10570 BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
10571 stats->rx_frame_errors =
10572 BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
10573 stats->rx_length_errors =
10574 BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
10575 BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
10576 BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
10578 BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
10579 BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
10580 stats->collisions =
10581 BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
10582 stats->tx_fifo_errors =
10583 BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
10584 stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
10586 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
10589 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
10591 struct net_device *dev = bp->dev;
10592 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10593 struct netdev_hw_addr *ha;
10596 bool update = false;
10599 netdev_for_each_mc_addr(ha, dev) {
10600 if (mc_count >= BNXT_MAX_MC_ADDRS) {
10601 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10602 vnic->mc_list_count = 0;
10606 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
10607 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
10614 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
10616 if (mc_count != vnic->mc_list_count) {
10617 vnic->mc_list_count = mc_count;
10623 static bool bnxt_uc_list_updated(struct bnxt *bp)
10625 struct net_device *dev = bp->dev;
10626 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10627 struct netdev_hw_addr *ha;
10630 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
10633 netdev_for_each_uc_addr(ha, dev) {
10634 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
10642 static void bnxt_set_rx_mode(struct net_device *dev)
10644 struct bnxt *bp = netdev_priv(dev);
10645 struct bnxt_vnic_info *vnic;
10646 bool mc_update = false;
10650 if (!test_bit(BNXT_STATE_OPEN, &bp->state))
10653 vnic = &bp->vnic_info[0];
10654 mask = vnic->rx_mask;
10655 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
10656 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
10657 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
10658 CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
10660 if (dev->flags & IFF_PROMISC)
10661 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10663 uc_update = bnxt_uc_list_updated(bp);
10665 if (dev->flags & IFF_BROADCAST)
10666 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10667 if (dev->flags & IFF_ALLMULTI) {
10668 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10669 vnic->mc_list_count = 0;
10671 mc_update = bnxt_mc_list_updated(bp, &mask);
10674 if (mask != vnic->rx_mask || uc_update || mc_update) {
10675 vnic->rx_mask = mask;
10677 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
10678 bnxt_queue_sp_work(bp);
10682 static int bnxt_cfg_rx_mode(struct bnxt *bp)
10684 struct net_device *dev = bp->dev;
10685 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10686 struct netdev_hw_addr *ha;
10687 int i, off = 0, rc;
10690 netif_addr_lock_bh(dev);
10691 uc_update = bnxt_uc_list_updated(bp);
10692 netif_addr_unlock_bh(dev);
10697 mutex_lock(&bp->hwrm_cmd_lock);
10698 for (i = 1; i < vnic->uc_filter_count; i++) {
10699 struct hwrm_cfa_l2_filter_free_input req = {0};
10701 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
10704 req.l2_filter_id = vnic->fw_l2_filter_id[i];
10706 rc = _hwrm_send_message(bp, &req, sizeof(req),
10709 mutex_unlock(&bp->hwrm_cmd_lock);
10711 vnic->uc_filter_count = 1;
10713 netif_addr_lock_bh(dev);
10714 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
10715 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10717 netdev_for_each_uc_addr(ha, dev) {
10718 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
10720 vnic->uc_filter_count++;
10723 netif_addr_unlock_bh(dev);
10725 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
10726 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
10728 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
10730 vnic->uc_filter_count = i;
10736 if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
10737 !bnxt_promisc_ok(bp))
10738 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10739 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
10740 if (rc && vnic->mc_list_count) {
10741 netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
10743 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10744 vnic->mc_list_count = 0;
10745 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
10748 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
10754 static bool bnxt_can_reserve_rings(struct bnxt *bp)
10756 #ifdef CONFIG_BNXT_SRIOV
10757 if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
10758 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10760 /* No minimum rings were provisioned by the PF. Don't
10761 * reserve rings by default when device is down.
10763 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
10766 if (!netif_running(bp->dev))
10773 /* If the chip and firmware supports RFS */
10774 static bool bnxt_rfs_supported(struct bnxt *bp)
10776 if (bp->flags & BNXT_FLAG_CHIP_P5) {
10777 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
10781 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
10783 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
10788 /* If runtime conditions support RFS */
10789 static bool bnxt_rfs_capable(struct bnxt *bp)
10791 #ifdef CONFIG_RFS_ACCEL
10792 int vnics, max_vnics, max_rss_ctxs;
10794 if (bp->flags & BNXT_FLAG_CHIP_P5)
10795 return bnxt_rfs_supported(bp);
10796 if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp))
10799 vnics = 1 + bp->rx_nr_rings;
10800 max_vnics = bnxt_get_max_func_vnics(bp);
10801 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
10803 /* RSS contexts not a limiting factor */
10804 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
10805 max_rss_ctxs = max_vnics;
10806 if (vnics > max_vnics || vnics > max_rss_ctxs) {
10807 if (bp->rx_nr_rings > 1)
10808 netdev_warn(bp->dev,
10809 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
10810 min(max_rss_ctxs - 1, max_vnics - 1));
10814 if (!BNXT_NEW_RM(bp))
10817 if (vnics == bp->hw_resc.resv_vnics)
10820 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, vnics);
10821 if (vnics <= bp->hw_resc.resv_vnics)
10824 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
10825 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, 1);
10832 static netdev_features_t bnxt_fix_features(struct net_device *dev,
10833 netdev_features_t features)
10835 struct bnxt *bp = netdev_priv(dev);
10836 netdev_features_t vlan_features;
10838 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
10839 features &= ~NETIF_F_NTUPLE;
10841 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
10842 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
10844 if (!(features & NETIF_F_GRO))
10845 features &= ~NETIF_F_GRO_HW;
10847 if (features & NETIF_F_GRO_HW)
10848 features &= ~NETIF_F_LRO;
10850 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
10851 * turned on or off together.
10853 vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
10854 if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
10855 if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
10856 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
10857 else if (vlan_features)
10858 features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
10860 #ifdef CONFIG_BNXT_SRIOV
10861 if (BNXT_VF(bp) && bp->vf.vlan)
10862 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
10867 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
10869 struct bnxt *bp = netdev_priv(dev);
10870 u32 flags = bp->flags;
10873 bool re_init = false;
10874 bool update_tpa = false;
10876 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
10877 if (features & NETIF_F_GRO_HW)
10878 flags |= BNXT_FLAG_GRO;
10879 else if (features & NETIF_F_LRO)
10880 flags |= BNXT_FLAG_LRO;
10882 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
10883 flags &= ~BNXT_FLAG_TPA;
10885 if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
10886 flags |= BNXT_FLAG_STRIP_VLAN;
10888 if (features & NETIF_F_NTUPLE)
10889 flags |= BNXT_FLAG_RFS;
10891 changes = flags ^ bp->flags;
10892 if (changes & BNXT_FLAG_TPA) {
10894 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
10895 (flags & BNXT_FLAG_TPA) == 0 ||
10896 (bp->flags & BNXT_FLAG_CHIP_P5))
10900 if (changes & ~BNXT_FLAG_TPA)
10903 if (flags != bp->flags) {
10904 u32 old_flags = bp->flags;
10906 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10909 bnxt_set_ring_params(bp);
10914 bnxt_close_nic(bp, false, false);
10917 bnxt_set_ring_params(bp);
10919 return bnxt_open_nic(bp, false, false);
10923 rc = bnxt_set_tpa(bp,
10924 (flags & BNXT_FLAG_TPA) ?
10927 bp->flags = old_flags;
10933 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
10936 struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
10941 /* Check that there are at most 2 IPv6 extension headers, no
10942 * fragment header, and each is <= 64 bytes.
10944 start = nw_off + sizeof(*ip6h);
10945 nexthdr = &ip6h->nexthdr;
10946 while (ipv6_ext_hdr(*nexthdr)) {
10947 struct ipv6_opt_hdr *hp;
10950 if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
10951 *nexthdr == NEXTHDR_FRAGMENT)
10953 hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
10954 skb_headlen(skb), NULL);
10957 if (*nexthdr == NEXTHDR_AUTH)
10958 hdrlen = ipv6_authlen(hp);
10960 hdrlen = ipv6_optlen(hp);
10964 nexthdr = &hp->nexthdr;
10969 /* Caller will check inner protocol */
10970 if (skb->encapsulation) {
10976 /* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
10977 return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
10980 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
10981 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
10983 struct udphdr *uh = udp_hdr(skb);
10984 __be16 udp_port = uh->dest;
10986 if (udp_port != bp->vxlan_port && udp_port != bp->nge_port)
10988 if (skb->inner_protocol_type == ENCAP_TYPE_ETHER) {
10989 struct ethhdr *eh = inner_eth_hdr(skb);
10991 switch (eh->h_proto) {
10992 case htons(ETH_P_IP):
10994 case htons(ETH_P_IPV6):
10995 return bnxt_exthdr_check(bp, skb,
10996 skb_inner_network_offset(skb),
11003 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
11005 switch (l4_proto) {
11007 return bnxt_udp_tunl_check(bp, skb);
11010 case IPPROTO_GRE: {
11011 switch (skb->inner_protocol) {
11014 case htons(ETH_P_IP):
11016 case htons(ETH_P_IPV6):
11021 /* Check ext headers of inner ipv6 */
11022 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
11028 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
11029 struct net_device *dev,
11030 netdev_features_t features)
11032 struct bnxt *bp = netdev_priv(dev);
11035 features = vlan_features_check(skb, features);
11036 switch (vlan_get_protocol(skb)) {
11037 case htons(ETH_P_IP):
11038 if (!skb->encapsulation)
11040 l4_proto = &ip_hdr(skb)->protocol;
11041 if (bnxt_tunl_check(bp, skb, *l4_proto))
11044 case htons(ETH_P_IPV6):
11045 if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
11048 if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
11052 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
11055 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
11058 struct hwrm_dbg_read_direct_output *resp = bp->hwrm_cmd_resp_addr;
11059 struct hwrm_dbg_read_direct_input req = {0};
11060 __le32 *dbg_reg_buf;
11061 dma_addr_t mapping;
11064 dbg_reg_buf = dma_alloc_coherent(&bp->pdev->dev, num_words * 4,
11065 &mapping, GFP_KERNEL);
11068 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_READ_DIRECT, -1, -1);
11069 req.host_dest_addr = cpu_to_le64(mapping);
11070 req.read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
11071 req.read_len32 = cpu_to_le32(num_words);
11072 mutex_lock(&bp->hwrm_cmd_lock);
11073 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
11074 if (rc || resp->error_code) {
11076 goto dbg_rd_reg_exit;
11078 for (i = 0; i < num_words; i++)
11079 reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
11082 mutex_unlock(&bp->hwrm_cmd_lock);
11083 dma_free_coherent(&bp->pdev->dev, num_words * 4, dbg_reg_buf, mapping);
11087 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
11088 u32 ring_id, u32 *prod, u32 *cons)
11090 struct hwrm_dbg_ring_info_get_output *resp = bp->hwrm_cmd_resp_addr;
11091 struct hwrm_dbg_ring_info_get_input req = {0};
11094 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_RING_INFO_GET, -1, -1);
11095 req.ring_type = ring_type;
11096 req.fw_ring_id = cpu_to_le32(ring_id);
11097 mutex_lock(&bp->hwrm_cmd_lock);
11098 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
11100 *prod = le32_to_cpu(resp->producer_index);
11101 *cons = le32_to_cpu(resp->consumer_index);
11103 mutex_unlock(&bp->hwrm_cmd_lock);
11107 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
11109 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
11110 int i = bnapi->index;
11115 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
11116 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
11120 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
11122 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
11123 int i = bnapi->index;
11128 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",
11129 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
11130 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
11131 rxr->rx_sw_agg_prod);
11134 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
11136 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
11137 int i = bnapi->index;
11139 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
11140 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
11143 static void bnxt_dbg_dump_states(struct bnxt *bp)
11146 struct bnxt_napi *bnapi;
11148 for (i = 0; i < bp->cp_nr_rings; i++) {
11149 bnapi = bp->bnapi[i];
11150 if (netif_msg_drv(bp)) {
11151 bnxt_dump_tx_sw_state(bnapi);
11152 bnxt_dump_rx_sw_state(bnapi);
11153 bnxt_dump_cp_sw_state(bnapi);
11158 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
11160 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
11161 struct hwrm_ring_reset_input req = {0};
11162 struct bnxt_napi *bnapi = rxr->bnapi;
11163 struct bnxt_cp_ring_info *cpr;
11166 cpr = &bnapi->cp_ring;
11167 cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
11168 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_RESET, cp_ring_id, -1);
11169 req.ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
11170 req.ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
11171 return hwrm_send_message_silent(bp, &req, sizeof(req),
11175 static void bnxt_reset_task(struct bnxt *bp, bool silent)
11178 bnxt_dbg_dump_states(bp);
11179 if (netif_running(bp->dev)) {
11183 bnxt_close_nic(bp, false, false);
11184 bnxt_open_nic(bp, false, false);
11187 bnxt_close_nic(bp, true, false);
11188 rc = bnxt_open_nic(bp, true, false);
11189 bnxt_ulp_start(bp, rc);
11194 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
11196 struct bnxt *bp = netdev_priv(dev);
11198 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
11199 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
11200 bnxt_queue_sp_work(bp);
11203 static void bnxt_fw_health_check(struct bnxt *bp)
11205 struct bnxt_fw_health *fw_health = bp->fw_health;
11208 if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11211 if (fw_health->tmr_counter) {
11212 fw_health->tmr_counter--;
11216 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
11217 if (val == fw_health->last_fw_heartbeat)
11220 fw_health->last_fw_heartbeat = val;
11222 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
11223 if (val != fw_health->last_fw_reset_cnt)
11226 fw_health->tmr_counter = fw_health->tmr_multiplier;
11230 set_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event);
11231 bnxt_queue_sp_work(bp);
11234 static void bnxt_timer(struct timer_list *t)
11236 struct bnxt *bp = from_timer(bp, t, timer);
11237 struct net_device *dev = bp->dev;
11239 if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
11242 if (atomic_read(&bp->intr_sem) != 0)
11243 goto bnxt_restart_timer;
11245 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
11246 bnxt_fw_health_check(bp);
11248 if (bp->link_info.link_up && bp->stats_coal_ticks) {
11249 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
11250 bnxt_queue_sp_work(bp);
11253 if (bnxt_tc_flower_enabled(bp)) {
11254 set_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event);
11255 bnxt_queue_sp_work(bp);
11258 #ifdef CONFIG_RFS_ACCEL
11259 if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count) {
11260 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
11261 bnxt_queue_sp_work(bp);
11263 #endif /*CONFIG_RFS_ACCEL*/
11265 if (bp->link_info.phy_retry) {
11266 if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
11267 bp->link_info.phy_retry = false;
11268 netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
11270 set_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event);
11271 bnxt_queue_sp_work(bp);
11275 if ((bp->flags & BNXT_FLAG_CHIP_P5) && !bp->chip_rev &&
11276 netif_carrier_ok(dev)) {
11277 set_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event);
11278 bnxt_queue_sp_work(bp);
11280 bnxt_restart_timer:
11281 mod_timer(&bp->timer, jiffies + bp->current_interval);
11284 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
11286 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
11287 * set. If the device is being closed, bnxt_close() may be holding
11288 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
11289 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
11291 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11295 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
11297 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11301 /* Only called from bnxt_sp_task() */
11302 static void bnxt_reset(struct bnxt *bp, bool silent)
11304 bnxt_rtnl_lock_sp(bp);
11305 if (test_bit(BNXT_STATE_OPEN, &bp->state))
11306 bnxt_reset_task(bp, silent);
11307 bnxt_rtnl_unlock_sp(bp);
11310 /* Only called from bnxt_sp_task() */
11311 static void bnxt_rx_ring_reset(struct bnxt *bp)
11315 bnxt_rtnl_lock_sp(bp);
11316 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
11317 bnxt_rtnl_unlock_sp(bp);
11320 /* Disable and flush TPA before resetting the RX ring */
11321 if (bp->flags & BNXT_FLAG_TPA)
11322 bnxt_set_tpa(bp, false);
11323 for (i = 0; i < bp->rx_nr_rings; i++) {
11324 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
11325 struct bnxt_cp_ring_info *cpr;
11328 if (!rxr->bnapi->in_reset)
11331 rc = bnxt_hwrm_rx_ring_reset(bp, i);
11333 if (rc == -EINVAL || rc == -EOPNOTSUPP)
11334 netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
11336 netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
11338 bnxt_reset_task(bp, true);
11341 bnxt_free_one_rx_ring_skbs(bp, i);
11343 rxr->rx_agg_prod = 0;
11344 rxr->rx_sw_agg_prod = 0;
11345 rxr->rx_next_cons = 0;
11346 rxr->bnapi->in_reset = false;
11347 bnxt_alloc_one_rx_ring(bp, i);
11348 cpr = &rxr->bnapi->cp_ring;
11349 cpr->sw_stats.rx.rx_resets++;
11350 if (bp->flags & BNXT_FLAG_AGG_RINGS)
11351 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
11352 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
11354 if (bp->flags & BNXT_FLAG_TPA)
11355 bnxt_set_tpa(bp, true);
11356 bnxt_rtnl_unlock_sp(bp);
11359 static void bnxt_fw_reset_close(struct bnxt *bp)
11362 /* When firmware is in fatal state, quiesce device and disable
11363 * bus master to prevent any potential bad DMAs before freeing
11366 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
11369 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
11371 bp->fw_reset_min_dsecs = 0;
11372 bnxt_tx_disable(bp);
11373 bnxt_disable_napi(bp);
11374 bnxt_disable_int_sync(bp);
11376 bnxt_clear_int_mode(bp);
11377 pci_disable_device(bp->pdev);
11379 bnxt_ptp_clear(bp);
11380 __bnxt_close_nic(bp, true, false);
11381 bnxt_vf_reps_free(bp);
11382 bnxt_clear_int_mode(bp);
11383 bnxt_hwrm_func_drv_unrgtr(bp);
11384 if (pci_is_enabled(bp->pdev))
11385 pci_disable_device(bp->pdev);
11386 bnxt_free_ctx_mem(bp);
11391 static bool is_bnxt_fw_ok(struct bnxt *bp)
11393 struct bnxt_fw_health *fw_health = bp->fw_health;
11394 bool no_heartbeat = false, has_reset = false;
11397 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
11398 if (val == fw_health->last_fw_heartbeat)
11399 no_heartbeat = true;
11401 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
11402 if (val != fw_health->last_fw_reset_cnt)
11405 if (!no_heartbeat && has_reset)
11411 /* rtnl_lock is acquired before calling this function */
11412 static void bnxt_force_fw_reset(struct bnxt *bp)
11414 struct bnxt_fw_health *fw_health = bp->fw_health;
11417 if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
11418 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11421 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11422 bnxt_fw_reset_close(bp);
11423 wait_dsecs = fw_health->master_func_wait_dsecs;
11424 if (fw_health->master) {
11425 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
11427 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
11429 bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
11430 wait_dsecs = fw_health->normal_func_wait_dsecs;
11431 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11434 bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
11435 bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
11436 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
11439 void bnxt_fw_exception(struct bnxt *bp)
11441 netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
11442 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
11443 bnxt_rtnl_lock_sp(bp);
11444 bnxt_force_fw_reset(bp);
11445 bnxt_rtnl_unlock_sp(bp);
11448 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
11451 static int bnxt_get_registered_vfs(struct bnxt *bp)
11453 #ifdef CONFIG_BNXT_SRIOV
11459 rc = bnxt_hwrm_func_qcfg(bp);
11461 netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
11464 if (bp->pf.registered_vfs)
11465 return bp->pf.registered_vfs;
11472 void bnxt_fw_reset(struct bnxt *bp)
11474 bnxt_rtnl_lock_sp(bp);
11475 if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
11476 !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
11479 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11480 if (bp->pf.active_vfs &&
11481 !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
11482 n = bnxt_get_registered_vfs(bp);
11484 netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
11486 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11487 dev_close(bp->dev);
11488 goto fw_reset_exit;
11489 } else if (n > 0) {
11490 u16 vf_tmo_dsecs = n * 10;
11492 if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
11493 bp->fw_reset_max_dsecs = vf_tmo_dsecs;
11494 bp->fw_reset_state =
11495 BNXT_FW_RESET_STATE_POLL_VF;
11496 bnxt_queue_fw_reset_work(bp, HZ / 10);
11497 goto fw_reset_exit;
11499 bnxt_fw_reset_close(bp);
11500 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
11501 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
11504 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11505 tmo = bp->fw_reset_min_dsecs * HZ / 10;
11507 bnxt_queue_fw_reset_work(bp, tmo);
11510 bnxt_rtnl_unlock_sp(bp);
11513 static void bnxt_chk_missed_irq(struct bnxt *bp)
11517 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
11520 for (i = 0; i < bp->cp_nr_rings; i++) {
11521 struct bnxt_napi *bnapi = bp->bnapi[i];
11522 struct bnxt_cp_ring_info *cpr;
11529 cpr = &bnapi->cp_ring;
11530 for (j = 0; j < 2; j++) {
11531 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
11534 if (!cpr2 || cpr2->has_more_work ||
11535 !bnxt_has_work(bp, cpr2))
11538 if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
11539 cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
11542 fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
11543 bnxt_dbg_hwrm_ring_info_get(bp,
11544 DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
11545 fw_ring_id, &val[0], &val[1]);
11546 cpr->sw_stats.cmn.missed_irqs++;
11551 static void bnxt_cfg_ntp_filters(struct bnxt *);
11553 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
11555 struct bnxt_link_info *link_info = &bp->link_info;
11557 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
11558 link_info->autoneg = BNXT_AUTONEG_SPEED;
11559 if (bp->hwrm_spec_code >= 0x10201) {
11560 if (link_info->auto_pause_setting &
11561 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
11562 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
11564 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
11566 link_info->advertising = link_info->auto_link_speeds;
11567 link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
11569 link_info->req_link_speed = link_info->force_link_speed;
11570 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
11571 if (link_info->force_pam4_link_speed) {
11572 link_info->req_link_speed =
11573 link_info->force_pam4_link_speed;
11574 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
11576 link_info->req_duplex = link_info->duplex_setting;
11578 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
11579 link_info->req_flow_ctrl =
11580 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
11582 link_info->req_flow_ctrl = link_info->force_pause_setting;
11585 static void bnxt_fw_echo_reply(struct bnxt *bp)
11587 struct bnxt_fw_health *fw_health = bp->fw_health;
11588 struct hwrm_func_echo_response_input req = {0};
11590 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_ECHO_RESPONSE, -1, -1);
11591 req.event_data1 = cpu_to_le32(fw_health->echo_req_data1);
11592 req.event_data2 = cpu_to_le32(fw_health->echo_req_data2);
11593 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
11596 static void bnxt_sp_task(struct work_struct *work)
11598 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
11600 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11601 smp_mb__after_atomic();
11602 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
11603 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11607 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
11608 bnxt_cfg_rx_mode(bp);
11610 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
11611 bnxt_cfg_ntp_filters(bp);
11612 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
11613 bnxt_hwrm_exec_fwd_req(bp);
11614 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
11615 bnxt_hwrm_port_qstats(bp, 0);
11616 bnxt_hwrm_port_qstats_ext(bp, 0);
11617 bnxt_accumulate_all_stats(bp);
11620 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
11623 mutex_lock(&bp->link_lock);
11624 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
11626 bnxt_hwrm_phy_qcaps(bp);
11628 rc = bnxt_update_link(bp, true);
11630 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
11633 if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
11635 bnxt_init_ethtool_link_settings(bp);
11636 mutex_unlock(&bp->link_lock);
11638 if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
11641 mutex_lock(&bp->link_lock);
11642 rc = bnxt_update_phy_setting(bp);
11643 mutex_unlock(&bp->link_lock);
11645 netdev_warn(bp->dev, "update phy settings retry failed\n");
11647 bp->link_info.phy_retry = false;
11648 netdev_info(bp->dev, "update phy settings retry succeeded\n");
11651 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
11652 mutex_lock(&bp->link_lock);
11653 bnxt_get_port_module_status(bp);
11654 mutex_unlock(&bp->link_lock);
11657 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
11658 bnxt_tc_flow_stats_work(bp);
11660 if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
11661 bnxt_chk_missed_irq(bp);
11663 if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
11664 bnxt_fw_echo_reply(bp);
11666 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
11667 * must be the last functions to be called before exiting.
11669 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
11670 bnxt_reset(bp, false);
11672 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
11673 bnxt_reset(bp, true);
11675 if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
11676 bnxt_rx_ring_reset(bp);
11678 if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event))
11679 bnxt_devlink_health_report(bp, BNXT_FW_RESET_NOTIFY_SP_EVENT);
11681 if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
11682 if (!is_bnxt_fw_ok(bp))
11683 bnxt_devlink_health_report(bp,
11684 BNXT_FW_EXCEPTION_SP_EVENT);
11687 smp_mb__before_atomic();
11688 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11691 /* Under rtnl_lock */
11692 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
11695 int max_rx, max_tx, tx_sets = 1;
11696 int tx_rings_needed, stats;
11703 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
11710 tx_rings_needed = tx * tx_sets + tx_xdp;
11711 if (max_tx < tx_rings_needed)
11715 if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
11718 if (bp->flags & BNXT_FLAG_AGG_RINGS)
11720 cp = sh ? max_t(int, tx_rings_needed, rx) : tx_rings_needed + rx;
11722 if (BNXT_NEW_RM(bp)) {
11723 cp += bnxt_get_ulp_msix_num(bp);
11724 stats += bnxt_get_ulp_stat_ctxs(bp);
11726 return bnxt_hwrm_check_rings(bp, tx_rings_needed, rx_rings, rx, cp,
11730 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
11733 pci_iounmap(pdev, bp->bar2);
11738 pci_iounmap(pdev, bp->bar1);
11743 pci_iounmap(pdev, bp->bar0);
11748 static void bnxt_cleanup_pci(struct bnxt *bp)
11750 bnxt_unmap_bars(bp, bp->pdev);
11751 pci_release_regions(bp->pdev);
11752 if (pci_is_enabled(bp->pdev))
11753 pci_disable_device(bp->pdev);
11756 static void bnxt_init_dflt_coal(struct bnxt *bp)
11758 struct bnxt_coal *coal;
11760 /* Tick values in micro seconds.
11761 * 1 coal_buf x bufs_per_record = 1 completion record.
11763 coal = &bp->rx_coal;
11764 coal->coal_ticks = 10;
11765 coal->coal_bufs = 30;
11766 coal->coal_ticks_irq = 1;
11767 coal->coal_bufs_irq = 2;
11768 coal->idle_thresh = 50;
11769 coal->bufs_per_record = 2;
11770 coal->budget = 64; /* NAPI budget */
11772 coal = &bp->tx_coal;
11773 coal->coal_ticks = 28;
11774 coal->coal_bufs = 30;
11775 coal->coal_ticks_irq = 2;
11776 coal->coal_bufs_irq = 2;
11777 coal->bufs_per_record = 1;
11779 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
11782 static int bnxt_fw_init_one_p1(struct bnxt *bp)
11787 rc = bnxt_hwrm_ver_get(bp);
11788 bnxt_try_map_fw_health_reg(bp);
11790 rc = bnxt_try_recover_fw(bp);
11793 rc = bnxt_hwrm_ver_get(bp);
11798 if (bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL) {
11799 rc = bnxt_alloc_kong_hwrm_resources(bp);
11801 bp->fw_cap &= ~BNXT_FW_CAP_KONG_MB_CHNL;
11804 if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
11805 bp->hwrm_max_ext_req_len > BNXT_HWRM_MAX_REQ_LEN) {
11806 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
11810 bnxt_nvm_cfg_ver_get(bp);
11812 rc = bnxt_hwrm_func_reset(bp);
11816 bnxt_hwrm_fw_set_time(bp);
11820 static int bnxt_fw_init_one_p2(struct bnxt *bp)
11824 /* Get the MAX capabilities for this function */
11825 rc = bnxt_hwrm_func_qcaps(bp);
11827 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
11832 rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
11834 netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
11837 if (bnxt_alloc_fw_health(bp)) {
11838 netdev_warn(bp->dev, "no memory for firmware error recovery\n");
11840 rc = bnxt_hwrm_error_recovery_qcfg(bp);
11842 netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
11846 rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
11850 bnxt_hwrm_func_qcfg(bp);
11851 bnxt_hwrm_vnic_qcaps(bp);
11852 bnxt_hwrm_port_led_qcaps(bp);
11853 bnxt_ethtool_init(bp);
11858 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
11860 bp->flags &= ~BNXT_FLAG_UDP_RSS_CAP;
11861 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
11862 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
11863 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
11864 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
11865 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
11866 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
11867 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
11868 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
11872 static void bnxt_set_dflt_rfs(struct bnxt *bp)
11874 struct net_device *dev = bp->dev;
11876 dev->hw_features &= ~NETIF_F_NTUPLE;
11877 dev->features &= ~NETIF_F_NTUPLE;
11878 bp->flags &= ~BNXT_FLAG_RFS;
11879 if (bnxt_rfs_supported(bp)) {
11880 dev->hw_features |= NETIF_F_NTUPLE;
11881 if (bnxt_rfs_capable(bp)) {
11882 bp->flags |= BNXT_FLAG_RFS;
11883 dev->features |= NETIF_F_NTUPLE;
11888 static void bnxt_fw_init_one_p3(struct bnxt *bp)
11890 struct pci_dev *pdev = bp->pdev;
11892 bnxt_set_dflt_rss_hash_type(bp);
11893 bnxt_set_dflt_rfs(bp);
11895 bnxt_get_wol_settings(bp);
11896 if (bp->flags & BNXT_FLAG_WOL_CAP)
11897 device_set_wakeup_enable(&pdev->dev, bp->wol);
11899 device_set_wakeup_capable(&pdev->dev, false);
11901 bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
11902 bnxt_hwrm_coal_params_qcaps(bp);
11905 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
11907 static int bnxt_fw_init_one(struct bnxt *bp)
11911 rc = bnxt_fw_init_one_p1(bp);
11913 netdev_err(bp->dev, "Firmware init phase 1 failed\n");
11916 rc = bnxt_fw_init_one_p2(bp);
11918 netdev_err(bp->dev, "Firmware init phase 2 failed\n");
11921 rc = bnxt_probe_phy(bp, false);
11924 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
11928 /* In case fw capabilities have changed, destroy the unneeded
11929 * reporters and create newly capable ones.
11931 bnxt_dl_fw_reporters_destroy(bp, false);
11932 bnxt_dl_fw_reporters_create(bp);
11933 bnxt_fw_init_one_p3(bp);
11937 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
11939 struct bnxt_fw_health *fw_health = bp->fw_health;
11940 u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
11941 u32 val = fw_health->fw_reset_seq_vals[reg_idx];
11942 u32 reg_type, reg_off, delay_msecs;
11944 delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
11945 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
11946 reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
11947 switch (reg_type) {
11948 case BNXT_FW_HEALTH_REG_TYPE_CFG:
11949 pci_write_config_dword(bp->pdev, reg_off, val);
11951 case BNXT_FW_HEALTH_REG_TYPE_GRC:
11952 writel(reg_off & BNXT_GRC_BASE_MASK,
11953 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
11954 reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
11956 case BNXT_FW_HEALTH_REG_TYPE_BAR0:
11957 writel(val, bp->bar0 + reg_off);
11959 case BNXT_FW_HEALTH_REG_TYPE_BAR1:
11960 writel(val, bp->bar1 + reg_off);
11964 pci_read_config_dword(bp->pdev, 0, &val);
11965 msleep(delay_msecs);
11969 static void bnxt_reset_all(struct bnxt *bp)
11971 struct bnxt_fw_health *fw_health = bp->fw_health;
11974 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
11975 bnxt_fw_reset_via_optee(bp);
11976 bp->fw_reset_timestamp = jiffies;
11980 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
11981 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
11982 bnxt_fw_reset_writel(bp, i);
11983 } else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
11984 struct hwrm_fw_reset_input req = {0};
11986 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
11987 req.resp_addr = cpu_to_le64(bp->hwrm_cmd_kong_resp_dma_addr);
11988 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
11989 req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
11990 req.flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
11991 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
11993 netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
11995 bp->fw_reset_timestamp = jiffies;
11998 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
12000 return time_after(jiffies, bp->fw_reset_timestamp +
12001 (bp->fw_reset_max_dsecs * HZ / 10));
12004 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
12006 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12007 if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF) {
12008 bnxt_ulp_start(bp, rc);
12009 bnxt_dl_health_status_update(bp, false);
12011 bp->fw_reset_state = 0;
12012 dev_close(bp->dev);
12015 static void bnxt_fw_reset_task(struct work_struct *work)
12017 struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
12020 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12021 netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
12025 switch (bp->fw_reset_state) {
12026 case BNXT_FW_RESET_STATE_POLL_VF: {
12027 int n = bnxt_get_registered_vfs(bp);
12031 netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
12032 n, jiffies_to_msecs(jiffies -
12033 bp->fw_reset_timestamp));
12034 goto fw_reset_abort;
12035 } else if (n > 0) {
12036 if (bnxt_fw_reset_timeout(bp)) {
12037 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12038 bp->fw_reset_state = 0;
12039 netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
12043 bnxt_queue_fw_reset_work(bp, HZ / 10);
12046 bp->fw_reset_timestamp = jiffies;
12048 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12049 bnxt_fw_reset_abort(bp, rc);
12053 bnxt_fw_reset_close(bp);
12054 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
12055 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
12058 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
12059 tmo = bp->fw_reset_min_dsecs * HZ / 10;
12062 bnxt_queue_fw_reset_work(bp, tmo);
12065 case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
12068 val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12069 if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
12070 !bnxt_fw_reset_timeout(bp)) {
12071 bnxt_queue_fw_reset_work(bp, HZ / 5);
12075 if (!bp->fw_health->master) {
12076 u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
12078 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
12079 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
12082 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
12085 case BNXT_FW_RESET_STATE_RESET_FW:
12086 bnxt_reset_all(bp);
12087 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
12088 bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
12090 case BNXT_FW_RESET_STATE_ENABLE_DEV:
12091 bnxt_inv_fw_health_reg(bp);
12092 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
12093 !bp->fw_reset_min_dsecs) {
12096 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
12097 if (val == 0xffff) {
12098 if (bnxt_fw_reset_timeout(bp)) {
12099 netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
12101 goto fw_reset_abort;
12103 bnxt_queue_fw_reset_work(bp, HZ / 1000);
12107 clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
12108 if (pci_enable_device(bp->pdev)) {
12109 netdev_err(bp->dev, "Cannot re-enable PCI device\n");
12111 goto fw_reset_abort;
12113 pci_set_master(bp->pdev);
12114 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
12116 case BNXT_FW_RESET_STATE_POLL_FW:
12117 bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
12118 rc = __bnxt_hwrm_ver_get(bp, true);
12120 if (bnxt_fw_reset_timeout(bp)) {
12121 netdev_err(bp->dev, "Firmware reset aborted\n");
12122 goto fw_reset_abort_status;
12124 bnxt_queue_fw_reset_work(bp, HZ / 5);
12127 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
12128 bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
12130 case BNXT_FW_RESET_STATE_OPENING:
12131 while (!rtnl_trylock()) {
12132 bnxt_queue_fw_reset_work(bp, HZ / 10);
12135 rc = bnxt_open(bp->dev);
12137 netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
12138 bnxt_fw_reset_abort(bp, rc);
12143 bp->fw_reset_state = 0;
12144 /* Make sure fw_reset_state is 0 before clearing the flag */
12145 smp_mb__before_atomic();
12146 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12147 bnxt_ulp_start(bp, 0);
12148 bnxt_reenable_sriov(bp);
12149 bnxt_vf_reps_alloc(bp);
12150 bnxt_vf_reps_open(bp);
12151 bnxt_dl_health_recovery_done(bp);
12152 bnxt_dl_health_status_update(bp, true);
12158 fw_reset_abort_status:
12159 if (bp->fw_health->status_reliable ||
12160 (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
12161 u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12163 netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
12167 bnxt_fw_reset_abort(bp, rc);
12171 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
12174 struct bnxt *bp = netdev_priv(dev);
12176 SET_NETDEV_DEV(dev, &pdev->dev);
12178 /* enable device (incl. PCI PM wakeup), and bus-mastering */
12179 rc = pci_enable_device(pdev);
12181 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
12185 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
12186 dev_err(&pdev->dev,
12187 "Cannot find PCI device base address, aborting\n");
12189 goto init_err_disable;
12192 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
12194 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
12195 goto init_err_disable;
12198 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
12199 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
12200 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
12202 goto init_err_release;
12205 pci_set_master(pdev);
12210 /* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
12211 * determines the BAR size.
12213 bp->bar0 = pci_ioremap_bar(pdev, 0);
12215 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
12217 goto init_err_release;
12220 bp->bar2 = pci_ioremap_bar(pdev, 4);
12222 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
12224 goto init_err_release;
12227 pci_enable_pcie_error_reporting(pdev);
12229 INIT_WORK(&bp->sp_task, bnxt_sp_task);
12230 INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
12232 spin_lock_init(&bp->ntp_fltr_lock);
12233 #if BITS_PER_LONG == 32
12234 spin_lock_init(&bp->db_lock);
12237 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
12238 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
12240 bnxt_init_dflt_coal(bp);
12242 timer_setup(&bp->timer, bnxt_timer, 0);
12243 bp->current_interval = BNXT_TIMER_INTERVAL;
12245 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
12246 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
12248 clear_bit(BNXT_STATE_OPEN, &bp->state);
12252 bnxt_unmap_bars(bp, pdev);
12253 pci_release_regions(pdev);
12256 pci_disable_device(pdev);
12262 /* rtnl_lock held */
12263 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
12265 struct sockaddr *addr = p;
12266 struct bnxt *bp = netdev_priv(dev);
12269 if (!is_valid_ether_addr(addr->sa_data))
12270 return -EADDRNOTAVAIL;
12272 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
12275 rc = bnxt_approve_mac(bp, addr->sa_data, true);
12279 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
12280 if (netif_running(dev)) {
12281 bnxt_close_nic(bp, false, false);
12282 rc = bnxt_open_nic(bp, false, false);
12288 /* rtnl_lock held */
12289 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
12291 struct bnxt *bp = netdev_priv(dev);
12293 if (netif_running(dev))
12294 bnxt_close_nic(bp, true, false);
12296 dev->mtu = new_mtu;
12297 bnxt_set_ring_params(bp);
12299 if (netif_running(dev))
12300 return bnxt_open_nic(bp, true, false);
12305 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
12307 struct bnxt *bp = netdev_priv(dev);
12311 if (tc > bp->max_tc) {
12312 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
12317 if (netdev_get_num_tc(dev) == tc)
12320 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
12323 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
12324 sh, tc, bp->tx_nr_rings_xdp);
12328 /* Needs to close the device and do hw resource re-allocations */
12329 if (netif_running(bp->dev))
12330 bnxt_close_nic(bp, true, false);
12333 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
12334 netdev_set_num_tc(dev, tc);
12336 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
12337 netdev_reset_tc(dev);
12339 bp->tx_nr_rings += bp->tx_nr_rings_xdp;
12340 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
12341 bp->tx_nr_rings + bp->rx_nr_rings;
12343 if (netif_running(bp->dev))
12344 return bnxt_open_nic(bp, true, false);
12349 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
12352 struct bnxt *bp = cb_priv;
12354 if (!bnxt_tc_flower_enabled(bp) ||
12355 !tc_cls_can_offload_and_chain0(bp->dev, type_data))
12356 return -EOPNOTSUPP;
12359 case TC_SETUP_CLSFLOWER:
12360 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
12362 return -EOPNOTSUPP;
12366 LIST_HEAD(bnxt_block_cb_list);
12368 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
12371 struct bnxt *bp = netdev_priv(dev);
12374 case TC_SETUP_BLOCK:
12375 return flow_block_cb_setup_simple(type_data,
12376 &bnxt_block_cb_list,
12377 bnxt_setup_tc_block_cb,
12379 case TC_SETUP_QDISC_MQPRIO: {
12380 struct tc_mqprio_qopt *mqprio = type_data;
12382 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
12384 return bnxt_setup_mq_tc(dev, mqprio->num_tc);
12387 return -EOPNOTSUPP;
12391 #ifdef CONFIG_RFS_ACCEL
12392 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
12393 struct bnxt_ntuple_filter *f2)
12395 struct flow_keys *keys1 = &f1->fkeys;
12396 struct flow_keys *keys2 = &f2->fkeys;
12398 if (keys1->basic.n_proto != keys2->basic.n_proto ||
12399 keys1->basic.ip_proto != keys2->basic.ip_proto)
12402 if (keys1->basic.n_proto == htons(ETH_P_IP)) {
12403 if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
12404 keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst)
12407 if (memcmp(&keys1->addrs.v6addrs.src, &keys2->addrs.v6addrs.src,
12408 sizeof(keys1->addrs.v6addrs.src)) ||
12409 memcmp(&keys1->addrs.v6addrs.dst, &keys2->addrs.v6addrs.dst,
12410 sizeof(keys1->addrs.v6addrs.dst)))
12414 if (keys1->ports.ports == keys2->ports.ports &&
12415 keys1->control.flags == keys2->control.flags &&
12416 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
12417 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
12423 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
12424 u16 rxq_index, u32 flow_id)
12426 struct bnxt *bp = netdev_priv(dev);
12427 struct bnxt_ntuple_filter *fltr, *new_fltr;
12428 struct flow_keys *fkeys;
12429 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
12430 int rc = 0, idx, bit_id, l2_idx = 0;
12431 struct hlist_head *head;
12434 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
12435 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
12438 netif_addr_lock_bh(dev);
12439 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
12440 if (ether_addr_equal(eth->h_dest,
12441 vnic->uc_list + off)) {
12446 netif_addr_unlock_bh(dev);
12450 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
12454 fkeys = &new_fltr->fkeys;
12455 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
12456 rc = -EPROTONOSUPPORT;
12460 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
12461 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
12462 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
12463 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
12464 rc = -EPROTONOSUPPORT;
12467 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
12468 bp->hwrm_spec_code < 0x10601) {
12469 rc = -EPROTONOSUPPORT;
12472 flags = fkeys->control.flags;
12473 if (((flags & FLOW_DIS_ENCAPSULATION) &&
12474 bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
12475 rc = -EPROTONOSUPPORT;
12479 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
12480 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
12482 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
12483 head = &bp->ntp_fltr_hash_tbl[idx];
12485 hlist_for_each_entry_rcu(fltr, head, hash) {
12486 if (bnxt_fltr_match(fltr, new_fltr)) {
12494 spin_lock_bh(&bp->ntp_fltr_lock);
12495 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
12496 BNXT_NTP_FLTR_MAX_FLTR, 0);
12498 spin_unlock_bh(&bp->ntp_fltr_lock);
12503 new_fltr->sw_id = (u16)bit_id;
12504 new_fltr->flow_id = flow_id;
12505 new_fltr->l2_fltr_idx = l2_idx;
12506 new_fltr->rxq = rxq_index;
12507 hlist_add_head_rcu(&new_fltr->hash, head);
12508 bp->ntp_fltr_count++;
12509 spin_unlock_bh(&bp->ntp_fltr_lock);
12511 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
12512 bnxt_queue_sp_work(bp);
12514 return new_fltr->sw_id;
12521 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
12525 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
12526 struct hlist_head *head;
12527 struct hlist_node *tmp;
12528 struct bnxt_ntuple_filter *fltr;
12531 head = &bp->ntp_fltr_hash_tbl[i];
12532 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
12535 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
12536 if (rps_may_expire_flow(bp->dev, fltr->rxq,
12539 bnxt_hwrm_cfa_ntuple_filter_free(bp,
12544 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
12549 set_bit(BNXT_FLTR_VALID, &fltr->state);
12553 spin_lock_bh(&bp->ntp_fltr_lock);
12554 hlist_del_rcu(&fltr->hash);
12555 bp->ntp_fltr_count--;
12556 spin_unlock_bh(&bp->ntp_fltr_lock);
12558 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
12563 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
12564 netdev_info(bp->dev, "Receive PF driver unload event!\n");
12569 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
12573 #endif /* CONFIG_RFS_ACCEL */
12575 static int bnxt_udp_tunnel_sync(struct net_device *netdev, unsigned int table)
12577 struct bnxt *bp = netdev_priv(netdev);
12578 struct udp_tunnel_info ti;
12581 udp_tunnel_nic_get_port(netdev, table, 0, &ti);
12582 if (ti.type == UDP_TUNNEL_TYPE_VXLAN) {
12583 bp->vxlan_port = ti.port;
12584 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
12586 bp->nge_port = ti.port;
12587 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
12591 return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti.port, cmd);
12593 return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
12596 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
12597 .sync_table = bnxt_udp_tunnel_sync,
12598 .flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
12599 UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
12601 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, },
12602 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
12606 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12607 struct net_device *dev, u32 filter_mask,
12610 struct bnxt *bp = netdev_priv(dev);
12612 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
12613 nlflags, filter_mask, NULL);
12616 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
12617 u16 flags, struct netlink_ext_ack *extack)
12619 struct bnxt *bp = netdev_priv(dev);
12620 struct nlattr *attr, *br_spec;
12623 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
12624 return -EOPNOTSUPP;
12626 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12630 nla_for_each_nested(attr, br_spec, rem) {
12633 if (nla_type(attr) != IFLA_BRIDGE_MODE)
12636 if (nla_len(attr) < sizeof(mode))
12639 mode = nla_get_u16(attr);
12640 if (mode == bp->br_mode)
12643 rc = bnxt_hwrm_set_br_mode(bp, mode);
12645 bp->br_mode = mode;
12651 int bnxt_get_port_parent_id(struct net_device *dev,
12652 struct netdev_phys_item_id *ppid)
12654 struct bnxt *bp = netdev_priv(dev);
12656 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
12657 return -EOPNOTSUPP;
12659 /* The PF and it's VF-reps only support the switchdev framework */
12660 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
12661 return -EOPNOTSUPP;
12663 ppid->id_len = sizeof(bp->dsn);
12664 memcpy(ppid->id, bp->dsn, ppid->id_len);
12669 static struct devlink_port *bnxt_get_devlink_port(struct net_device *dev)
12671 struct bnxt *bp = netdev_priv(dev);
12673 return &bp->dl_port;
12676 static const struct net_device_ops bnxt_netdev_ops = {
12677 .ndo_open = bnxt_open,
12678 .ndo_start_xmit = bnxt_start_xmit,
12679 .ndo_stop = bnxt_close,
12680 .ndo_get_stats64 = bnxt_get_stats64,
12681 .ndo_set_rx_mode = bnxt_set_rx_mode,
12682 .ndo_do_ioctl = bnxt_ioctl,
12683 .ndo_validate_addr = eth_validate_addr,
12684 .ndo_set_mac_address = bnxt_change_mac_addr,
12685 .ndo_change_mtu = bnxt_change_mtu,
12686 .ndo_fix_features = bnxt_fix_features,
12687 .ndo_set_features = bnxt_set_features,
12688 .ndo_features_check = bnxt_features_check,
12689 .ndo_tx_timeout = bnxt_tx_timeout,
12690 #ifdef CONFIG_BNXT_SRIOV
12691 .ndo_get_vf_config = bnxt_get_vf_config,
12692 .ndo_set_vf_mac = bnxt_set_vf_mac,
12693 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
12694 .ndo_set_vf_rate = bnxt_set_vf_bw,
12695 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
12696 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
12697 .ndo_set_vf_trust = bnxt_set_vf_trust,
12699 .ndo_setup_tc = bnxt_setup_tc,
12700 #ifdef CONFIG_RFS_ACCEL
12701 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
12703 .ndo_bpf = bnxt_xdp,
12704 .ndo_xdp_xmit = bnxt_xdp_xmit,
12705 .ndo_bridge_getlink = bnxt_bridge_getlink,
12706 .ndo_bridge_setlink = bnxt_bridge_setlink,
12707 .ndo_get_devlink_port = bnxt_get_devlink_port,
12710 static void bnxt_remove_one(struct pci_dev *pdev)
12712 struct net_device *dev = pci_get_drvdata(pdev);
12713 struct bnxt *bp = netdev_priv(dev);
12716 bnxt_sriov_disable(bp);
12719 devlink_port_type_clear(&bp->dl_port);
12721 pci_disable_pcie_error_reporting(pdev);
12722 unregister_netdev(dev);
12723 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12724 /* Flush any pending tasks */
12725 cancel_work_sync(&bp->sp_task);
12726 cancel_delayed_work_sync(&bp->fw_reset_task);
12729 bnxt_dl_fw_reporters_destroy(bp, true);
12730 bnxt_dl_unregister(bp);
12731 bnxt_shutdown_tc(bp);
12733 bnxt_clear_int_mode(bp);
12734 bnxt_hwrm_func_drv_unrgtr(bp);
12735 bnxt_free_hwrm_resources(bp);
12736 bnxt_free_hwrm_short_cmd_req(bp);
12737 bnxt_ethtool_free(bp);
12741 kfree(bp->ptp_cfg);
12742 bp->ptp_cfg = NULL;
12743 kfree(bp->fw_health);
12744 bp->fw_health = NULL;
12745 bnxt_cleanup_pci(bp);
12746 bnxt_free_ctx_mem(bp);
12749 kfree(bp->rss_indir_tbl);
12750 bp->rss_indir_tbl = NULL;
12751 bnxt_free_port_stats(bp);
12755 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
12758 struct bnxt_link_info *link_info = &bp->link_info;
12761 rc = bnxt_hwrm_phy_qcaps(bp);
12763 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
12767 if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
12768 bp->dev->priv_flags |= IFF_SUPP_NOFCS;
12770 bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
12774 rc = bnxt_update_link(bp, false);
12776 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
12781 /* Older firmware does not have supported_auto_speeds, so assume
12782 * that all supported speeds can be autonegotiated.
12784 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
12785 link_info->support_auto_speeds = link_info->support_speeds;
12787 bnxt_init_ethtool_link_settings(bp);
12791 static int bnxt_get_max_irq(struct pci_dev *pdev)
12795 if (!pdev->msix_cap)
12798 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
12799 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
12802 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
12805 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12806 int max_ring_grps = 0, max_irq;
12808 *max_tx = hw_resc->max_tx_rings;
12809 *max_rx = hw_resc->max_rx_rings;
12810 *max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
12811 max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
12812 bnxt_get_ulp_msix_num(bp),
12813 hw_resc->max_stat_ctxs - bnxt_get_ulp_stat_ctxs(bp));
12814 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
12815 *max_cp = min_t(int, *max_cp, max_irq);
12816 max_ring_grps = hw_resc->max_hw_ring_grps;
12817 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
12821 if (bp->flags & BNXT_FLAG_AGG_RINGS)
12823 if (bp->flags & BNXT_FLAG_CHIP_P5) {
12824 bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
12825 /* On P5 chips, max_cp output param should be available NQs */
12828 *max_rx = min_t(int, *max_rx, max_ring_grps);
12831 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
12835 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
12838 if (!rx || !tx || !cp)
12841 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
12844 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
12849 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
12850 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
12851 /* Not enough rings, try disabling agg rings. */
12852 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
12853 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
12855 /* set BNXT_FLAG_AGG_RINGS back for consistency */
12856 bp->flags |= BNXT_FLAG_AGG_RINGS;
12859 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
12860 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12861 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12862 bnxt_set_ring_params(bp);
12865 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
12866 int max_cp, max_stat, max_irq;
12868 /* Reserve minimum resources for RoCE */
12869 max_cp = bnxt_get_max_func_cp_rings(bp);
12870 max_stat = bnxt_get_max_func_stat_ctxs(bp);
12871 max_irq = bnxt_get_max_func_irqs(bp);
12872 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
12873 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
12874 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
12877 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
12878 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
12879 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
12880 max_cp = min_t(int, max_cp, max_irq);
12881 max_cp = min_t(int, max_cp, max_stat);
12882 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
12889 /* In initial default shared ring setting, each shared ring must have a
12892 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
12894 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
12895 bp->rx_nr_rings = bp->cp_nr_rings;
12896 bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
12897 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
12900 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
12902 int dflt_rings, max_rx_rings, max_tx_rings, rc;
12904 if (!bnxt_can_reserve_rings(bp))
12908 bp->flags |= BNXT_FLAG_SHARED_RINGS;
12909 dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
12910 /* Reduce default rings on multi-port cards so that total default
12911 * rings do not exceed CPU count.
12913 if (bp->port_count > 1) {
12915 max_t(int, num_online_cpus() / bp->port_count, 1);
12917 dflt_rings = min_t(int, dflt_rings, max_rings);
12919 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
12922 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
12923 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
12925 bnxt_trim_dflt_sh_rings(bp);
12927 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
12928 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
12930 rc = __bnxt_reserve_rings(bp);
12932 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
12933 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
12935 bnxt_trim_dflt_sh_rings(bp);
12937 /* Rings may have been trimmed, re-reserve the trimmed rings. */
12938 if (bnxt_need_reserve_rings(bp)) {
12939 rc = __bnxt_reserve_rings(bp);
12941 netdev_warn(bp->dev, "2nd rings reservation failed.\n");
12942 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
12944 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
12949 bp->tx_nr_rings = 0;
12950 bp->rx_nr_rings = 0;
12955 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
12959 if (bp->tx_nr_rings)
12962 bnxt_ulp_irq_stop(bp);
12963 bnxt_clear_int_mode(bp);
12964 rc = bnxt_set_dflt_rings(bp, true);
12966 netdev_err(bp->dev, "Not enough rings available.\n");
12967 goto init_dflt_ring_err;
12969 rc = bnxt_init_int_mode(bp);
12971 goto init_dflt_ring_err;
12973 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
12974 if (bnxt_rfs_supported(bp) && bnxt_rfs_capable(bp)) {
12975 bp->flags |= BNXT_FLAG_RFS;
12976 bp->dev->features |= NETIF_F_NTUPLE;
12978 init_dflt_ring_err:
12979 bnxt_ulp_irq_restart(bp, rc);
12983 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
12988 bnxt_hwrm_func_qcaps(bp);
12990 if (netif_running(bp->dev))
12991 __bnxt_close_nic(bp, true, false);
12993 bnxt_ulp_irq_stop(bp);
12994 bnxt_clear_int_mode(bp);
12995 rc = bnxt_init_int_mode(bp);
12996 bnxt_ulp_irq_restart(bp, rc);
12998 if (netif_running(bp->dev)) {
13000 dev_close(bp->dev);
13002 rc = bnxt_open_nic(bp, true, false);
13008 static int bnxt_init_mac_addr(struct bnxt *bp)
13013 memcpy(bp->dev->dev_addr, bp->pf.mac_addr, ETH_ALEN);
13015 #ifdef CONFIG_BNXT_SRIOV
13016 struct bnxt_vf_info *vf = &bp->vf;
13017 bool strict_approval = true;
13019 if (is_valid_ether_addr(vf->mac_addr)) {
13020 /* overwrite netdev dev_addr with admin VF MAC */
13021 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
13022 /* Older PF driver or firmware may not approve this
13025 strict_approval = false;
13027 eth_hw_addr_random(bp->dev);
13029 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
13035 #define BNXT_VPD_LEN 512
13036 static void bnxt_vpd_read_info(struct bnxt *bp)
13038 struct pci_dev *pdev = bp->pdev;
13039 int i, len, pos, ro_size, size;
13043 vpd_data = kmalloc(BNXT_VPD_LEN, GFP_KERNEL);
13047 vpd_size = pci_read_vpd(pdev, 0, BNXT_VPD_LEN, vpd_data);
13048 if (vpd_size <= 0) {
13049 netdev_err(bp->dev, "Unable to read VPD\n");
13053 i = pci_vpd_find_tag(vpd_data, vpd_size, PCI_VPD_LRDT_RO_DATA);
13055 netdev_err(bp->dev, "VPD READ-Only not found\n");
13059 ro_size = pci_vpd_lrdt_size(&vpd_data[i]);
13060 i += PCI_VPD_LRDT_TAG_SIZE;
13061 if (i + ro_size > vpd_size)
13064 pos = pci_vpd_find_info_keyword(vpd_data, i, ro_size,
13065 PCI_VPD_RO_KEYWORD_PARTNO);
13069 len = pci_vpd_info_field_size(&vpd_data[pos]);
13070 pos += PCI_VPD_INFO_FLD_HDR_SIZE;
13071 if (len + pos > vpd_size)
13074 size = min(len, BNXT_VPD_FLD_LEN - 1);
13075 memcpy(bp->board_partno, &vpd_data[pos], size);
13078 pos = pci_vpd_find_info_keyword(vpd_data, i, ro_size,
13079 PCI_VPD_RO_KEYWORD_SERIALNO);
13083 len = pci_vpd_info_field_size(&vpd_data[pos]);
13084 pos += PCI_VPD_INFO_FLD_HDR_SIZE;
13085 if (len + pos > vpd_size)
13088 size = min(len, BNXT_VPD_FLD_LEN - 1);
13089 memcpy(bp->board_serialno, &vpd_data[pos], size);
13094 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
13096 struct pci_dev *pdev = bp->pdev;
13099 qword = pci_get_dsn(pdev);
13101 netdev_info(bp->dev, "Unable to read adapter's DSN\n");
13102 return -EOPNOTSUPP;
13105 put_unaligned_le64(qword, dsn);
13107 bp->flags |= BNXT_FLAG_DSN_VALID;
13111 static int bnxt_map_db_bar(struct bnxt *bp)
13115 bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
13121 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
13123 struct net_device *dev;
13127 if (pci_is_bridge(pdev))
13130 /* Clear any pending DMA transactions from crash kernel
13131 * while loading driver in capture kernel.
13133 if (is_kdump_kernel()) {
13134 pci_clear_master(pdev);
13138 max_irqs = bnxt_get_max_irq(pdev);
13139 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
13143 bp = netdev_priv(dev);
13144 bp->msg_enable = BNXT_DEF_MSG_ENABLE;
13145 bnxt_set_max_func_irqs(bp, max_irqs);
13147 if (bnxt_vf_pciid(ent->driver_data))
13148 bp->flags |= BNXT_FLAG_VF;
13150 if (pdev->msix_cap)
13151 bp->flags |= BNXT_FLAG_MSIX_CAP;
13153 rc = bnxt_init_board(pdev, dev);
13155 goto init_err_free;
13157 dev->netdev_ops = &bnxt_netdev_ops;
13158 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
13159 dev->ethtool_ops = &bnxt_ethtool_ops;
13160 pci_set_drvdata(pdev, dev);
13162 rc = bnxt_alloc_hwrm_resources(bp);
13164 goto init_err_pci_clean;
13166 mutex_init(&bp->hwrm_cmd_lock);
13167 mutex_init(&bp->link_lock);
13169 rc = bnxt_fw_init_one_p1(bp);
13171 goto init_err_pci_clean;
13174 bnxt_vpd_read_info(bp);
13176 if (BNXT_CHIP_P5(bp)) {
13177 bp->flags |= BNXT_FLAG_CHIP_P5;
13178 if (BNXT_CHIP_SR2(bp))
13179 bp->flags |= BNXT_FLAG_CHIP_SR2;
13182 rc = bnxt_alloc_rss_indir_tbl(bp);
13184 goto init_err_pci_clean;
13186 rc = bnxt_fw_init_one_p2(bp);
13188 goto init_err_pci_clean;
13190 rc = bnxt_map_db_bar(bp);
13192 dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
13194 goto init_err_pci_clean;
13197 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
13198 NETIF_F_TSO | NETIF_F_TSO6 |
13199 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
13200 NETIF_F_GSO_IPXIP4 |
13201 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
13202 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
13203 NETIF_F_RXCSUM | NETIF_F_GRO;
13205 if (BNXT_SUPPORTS_TPA(bp))
13206 dev->hw_features |= NETIF_F_LRO;
13208 dev->hw_enc_features =
13209 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
13210 NETIF_F_TSO | NETIF_F_TSO6 |
13211 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
13212 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
13213 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
13214 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
13216 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
13217 NETIF_F_GSO_GRE_CSUM;
13218 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
13219 if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
13220 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13221 if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
13222 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
13223 if (BNXT_SUPPORTS_TPA(bp))
13224 dev->hw_features |= NETIF_F_GRO_HW;
13225 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
13226 if (dev->features & NETIF_F_GRO_HW)
13227 dev->features &= ~NETIF_F_LRO;
13228 dev->priv_flags |= IFF_UNICAST_FLT;
13230 #ifdef CONFIG_BNXT_SRIOV
13231 init_waitqueue_head(&bp->sriov_cfg_wait);
13232 mutex_init(&bp->sriov_lock);
13234 if (BNXT_SUPPORTS_TPA(bp)) {
13235 bp->gro_func = bnxt_gro_func_5730x;
13236 if (BNXT_CHIP_P4(bp))
13237 bp->gro_func = bnxt_gro_func_5731x;
13238 else if (BNXT_CHIP_P5(bp))
13239 bp->gro_func = bnxt_gro_func_5750x;
13241 if (!BNXT_CHIP_P4_PLUS(bp))
13242 bp->flags |= BNXT_FLAG_DOUBLE_DB;
13244 rc = bnxt_init_mac_addr(bp);
13246 dev_err(&pdev->dev, "Unable to initialize mac address.\n");
13247 rc = -EADDRNOTAVAIL;
13248 goto init_err_pci_clean;
13252 /* Read the adapter's DSN to use as the eswitch switch_id */
13253 rc = bnxt_pcie_dsn_get(bp, bp->dsn);
13256 /* MTU range: 60 - FW defined max */
13257 dev->min_mtu = ETH_ZLEN;
13258 dev->max_mtu = bp->max_mtu;
13260 rc = bnxt_probe_phy(bp, true);
13262 goto init_err_pci_clean;
13264 bnxt_set_rx_skb_mode(bp, false);
13265 bnxt_set_tpa_flags(bp);
13266 bnxt_set_ring_params(bp);
13267 rc = bnxt_set_dflt_rings(bp, true);
13269 netdev_err(bp->dev, "Not enough rings available.\n");
13271 goto init_err_pci_clean;
13274 bnxt_fw_init_one_p3(bp);
13276 if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13277 bp->flags |= BNXT_FLAG_STRIP_VLAN;
13279 rc = bnxt_init_int_mode(bp);
13281 goto init_err_pci_clean;
13283 /* No TC has been set yet and rings may have been trimmed due to
13284 * limited MSIX, so we re-initialize the TX rings per TC.
13286 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13291 create_singlethread_workqueue("bnxt_pf_wq");
13293 dev_err(&pdev->dev, "Unable to create workqueue.\n");
13295 goto init_err_pci_clean;
13298 rc = bnxt_init_tc(bp);
13300 netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
13304 bnxt_inv_fw_health_reg(bp);
13305 bnxt_dl_register(bp);
13307 rc = register_netdev(dev);
13309 goto init_err_cleanup;
13312 devlink_port_type_eth_set(&bp->dl_port, bp->dev);
13313 bnxt_dl_fw_reporters_create(bp);
13315 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
13316 board_info[ent->driver_data].name,
13317 (long)pci_resource_start(pdev, 0), dev->dev_addr);
13318 pcie_print_link_status(pdev);
13320 pci_save_state(pdev);
13324 bnxt_dl_unregister(bp);
13325 bnxt_shutdown_tc(bp);
13326 bnxt_clear_int_mode(bp);
13328 init_err_pci_clean:
13329 bnxt_hwrm_func_drv_unrgtr(bp);
13330 bnxt_free_hwrm_short_cmd_req(bp);
13331 bnxt_free_hwrm_resources(bp);
13332 bnxt_ethtool_free(bp);
13333 kfree(bp->ptp_cfg);
13334 bp->ptp_cfg = NULL;
13335 kfree(bp->fw_health);
13336 bp->fw_health = NULL;
13337 bnxt_cleanup_pci(bp);
13338 bnxt_free_ctx_mem(bp);
13341 kfree(bp->rss_indir_tbl);
13342 bp->rss_indir_tbl = NULL;
13349 static void bnxt_shutdown(struct pci_dev *pdev)
13351 struct net_device *dev = pci_get_drvdata(pdev);
13358 bp = netdev_priv(dev);
13360 goto shutdown_exit;
13362 if (netif_running(dev))
13365 bnxt_ulp_shutdown(bp);
13366 bnxt_clear_int_mode(bp);
13367 pci_disable_device(pdev);
13369 if (system_state == SYSTEM_POWER_OFF) {
13370 pci_wake_from_d3(pdev, bp->wol);
13371 pci_set_power_state(pdev, PCI_D3hot);
13378 #ifdef CONFIG_PM_SLEEP
13379 static int bnxt_suspend(struct device *device)
13381 struct net_device *dev = dev_get_drvdata(device);
13382 struct bnxt *bp = netdev_priv(dev);
13387 if (netif_running(dev)) {
13388 netif_device_detach(dev);
13389 rc = bnxt_close(dev);
13391 bnxt_hwrm_func_drv_unrgtr(bp);
13392 pci_disable_device(bp->pdev);
13393 bnxt_free_ctx_mem(bp);
13400 static int bnxt_resume(struct device *device)
13402 struct net_device *dev = dev_get_drvdata(device);
13403 struct bnxt *bp = netdev_priv(dev);
13407 rc = pci_enable_device(bp->pdev);
13409 netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
13413 pci_set_master(bp->pdev);
13414 if (bnxt_hwrm_ver_get(bp)) {
13418 rc = bnxt_hwrm_func_reset(bp);
13424 rc = bnxt_hwrm_func_qcaps(bp);
13428 if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
13433 bnxt_get_wol_settings(bp);
13434 if (netif_running(dev)) {
13435 rc = bnxt_open(dev);
13437 netif_device_attach(dev);
13441 bnxt_ulp_start(bp, rc);
13443 bnxt_reenable_sriov(bp);
13448 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
13449 #define BNXT_PM_OPS (&bnxt_pm_ops)
13453 #define BNXT_PM_OPS NULL
13455 #endif /* CONFIG_PM_SLEEP */
13458 * bnxt_io_error_detected - called when PCI error is detected
13459 * @pdev: Pointer to PCI device
13460 * @state: The current pci connection state
13462 * This function is called after a PCI bus error affecting
13463 * this device has been detected.
13465 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
13466 pci_channel_state_t state)
13468 struct net_device *netdev = pci_get_drvdata(pdev);
13469 struct bnxt *bp = netdev_priv(netdev);
13471 netdev_info(netdev, "PCI I/O error detected\n");
13474 netif_device_detach(netdev);
13478 if (state == pci_channel_io_perm_failure) {
13480 return PCI_ERS_RESULT_DISCONNECT;
13483 if (state == pci_channel_io_frozen)
13484 set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
13486 if (netif_running(netdev))
13487 bnxt_close(netdev);
13489 if (pci_is_enabled(pdev))
13490 pci_disable_device(pdev);
13491 bnxt_free_ctx_mem(bp);
13496 /* Request a slot slot reset. */
13497 return PCI_ERS_RESULT_NEED_RESET;
13501 * bnxt_io_slot_reset - called after the pci bus has been reset.
13502 * @pdev: Pointer to PCI device
13504 * Restart the card from scratch, as if from a cold-boot.
13505 * At this point, the card has exprienced a hard reset,
13506 * followed by fixups by BIOS, and has its config space
13507 * set up identically to what it was at cold boot.
13509 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
13511 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
13512 struct net_device *netdev = pci_get_drvdata(pdev);
13513 struct bnxt *bp = netdev_priv(netdev);
13516 netdev_info(bp->dev, "PCI Slot Reset\n");
13520 if (pci_enable_device(pdev)) {
13521 dev_err(&pdev->dev,
13522 "Cannot re-enable PCI device after reset.\n");
13524 pci_set_master(pdev);
13525 /* Upon fatal error, our device internal logic that latches to
13526 * BAR value is getting reset and will restore only upon
13527 * rewritting the BARs.
13529 * As pci_restore_state() does not re-write the BARs if the
13530 * value is same as saved value earlier, driver needs to
13531 * write the BARs to 0 to force restore, in case of fatal error.
13533 if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
13535 for (off = PCI_BASE_ADDRESS_0;
13536 off <= PCI_BASE_ADDRESS_5; off += 4)
13537 pci_write_config_dword(bp->pdev, off, 0);
13539 pci_restore_state(pdev);
13540 pci_save_state(pdev);
13542 err = bnxt_hwrm_func_reset(bp);
13544 result = PCI_ERS_RESULT_RECOVERED;
13553 * bnxt_io_resume - called when traffic can start flowing again.
13554 * @pdev: Pointer to PCI device
13556 * This callback is called when the error recovery driver tells
13557 * us that its OK to resume normal operation.
13559 static void bnxt_io_resume(struct pci_dev *pdev)
13561 struct net_device *netdev = pci_get_drvdata(pdev);
13562 struct bnxt *bp = netdev_priv(netdev);
13565 netdev_info(bp->dev, "PCI Slot Resume\n");
13568 err = bnxt_hwrm_func_qcaps(bp);
13569 if (!err && netif_running(netdev))
13570 err = bnxt_open(netdev);
13572 bnxt_ulp_start(bp, err);
13574 bnxt_reenable_sriov(bp);
13575 netif_device_attach(netdev);
13581 static const struct pci_error_handlers bnxt_err_handler = {
13582 .error_detected = bnxt_io_error_detected,
13583 .slot_reset = bnxt_io_slot_reset,
13584 .resume = bnxt_io_resume
13587 static struct pci_driver bnxt_pci_driver = {
13588 .name = DRV_MODULE_NAME,
13589 .id_table = bnxt_pci_tbl,
13590 .probe = bnxt_init_one,
13591 .remove = bnxt_remove_one,
13592 .shutdown = bnxt_shutdown,
13593 .driver.pm = BNXT_PM_OPS,
13594 .err_handler = &bnxt_err_handler,
13595 #if defined(CONFIG_BNXT_SRIOV)
13596 .sriov_configure = bnxt_sriov_configure,
13600 static int __init bnxt_init(void)
13603 return pci_register_driver(&bnxt_pci_driver);
13606 static void __exit bnxt_exit(void)
13608 pci_unregister_driver(&bnxt_pci_driver);
13610 destroy_workqueue(bnxt_pf_wq);
13614 module_init(bnxt_init);
13615 module_exit(bnxt_exit);