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>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <linux/hwmon.h>
56 #include <linux/hwmon-sysfs.h>
57 #include <net/page_pool.h>
58 #include <linux/align.h>
59 #include <net/netdev_queues.h>
63 #include "bnxt_hwrm.h"
65 #include "bnxt_sriov.h"
66 #include "bnxt_ethtool.h"
72 #include "bnxt_devlink.h"
73 #include "bnxt_debugfs.h"
75 #define BNXT_TX_TIMEOUT (5 * HZ)
76 #define BNXT_DEF_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_HW | \
79 MODULE_LICENSE("GPL");
80 MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
82 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
83 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
84 #define BNXT_RX_COPY_THRESH 256
86 #define BNXT_TX_PUSH_THRESH 164
88 /* indexed by enum board_idx */
92 [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
93 [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
94 [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
95 [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
96 [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
97 [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
98 [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
99 [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
100 [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
101 [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
102 [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
103 [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
104 [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
105 [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
106 [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
107 [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
108 [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
109 [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
110 [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
111 [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
112 [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
113 [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
114 [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
115 [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
116 [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
117 [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
118 [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
119 [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
120 [BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
121 [BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
122 [BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
123 [BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
124 [BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
125 [BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
126 [BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
127 [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
128 [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
129 [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
130 [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
131 [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
132 [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
133 [NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
134 [NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
135 [NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
136 [NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
139 static const struct pci_device_id bnxt_pci_tbl[] = {
140 { PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
141 { PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
142 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
143 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
144 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
145 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
146 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
147 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
148 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
149 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
150 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
151 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
152 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
153 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
154 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
155 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
156 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
157 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
158 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
159 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
160 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
161 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
162 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
163 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
164 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
165 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
166 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
167 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
168 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
169 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
170 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
171 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
172 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
173 { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
174 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
175 { PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
176 { PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
177 { PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
178 { PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
179 { PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
180 { PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
181 { PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
182 { PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
183 { PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
184 { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
185 { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
186 #ifdef CONFIG_BNXT_SRIOV
187 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
188 { PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
189 { PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
190 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
191 { PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
192 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
193 { PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
194 { PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
195 { PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
196 { PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
197 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
198 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
199 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
200 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
201 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
202 { PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
203 { PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
204 { PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
205 { PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
206 { PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
207 { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
212 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
214 static const u16 bnxt_vf_req_snif[] = {
218 HWRM_CFA_L2_FILTER_ALLOC,
221 static const u16 bnxt_async_events_arr[] = {
222 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
223 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
224 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
225 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
226 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
227 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
228 ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
229 ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
230 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
231 ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
232 ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
233 ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
234 ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
235 ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
236 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
237 ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
240 static struct workqueue_struct *bnxt_pf_wq;
242 static bool bnxt_vf_pciid(enum board_idx idx)
244 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
245 idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
246 idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
247 idx == NETXTREME_E_P5_VF_HV);
250 #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
251 #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
252 #define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
254 #define BNXT_CP_DB_IRQ_DIS(db) \
255 writel(DB_CP_IRQ_DIS_FLAGS, db)
257 #define BNXT_DB_CQ(db, idx) \
258 writel(DB_CP_FLAGS | RING_CMP(idx), (db)->doorbell)
260 #define BNXT_DB_NQ_P5(db, idx) \
261 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | RING_CMP(idx), \
264 #define BNXT_DB_CQ_ARM(db, idx) \
265 writel(DB_CP_REARM_FLAGS | RING_CMP(idx), (db)->doorbell)
267 #define BNXT_DB_NQ_ARM_P5(db, idx) \
268 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM | RING_CMP(idx),\
271 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
273 if (bp->flags & BNXT_FLAG_CHIP_P5)
274 BNXT_DB_NQ_P5(db, idx);
279 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
281 if (bp->flags & BNXT_FLAG_CHIP_P5)
282 BNXT_DB_NQ_ARM_P5(db, idx);
284 BNXT_DB_CQ_ARM(db, idx);
287 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
289 if (bp->flags & BNXT_FLAG_CHIP_P5)
290 bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
291 RING_CMP(idx), db->doorbell);
296 const u16 bnxt_lhint_arr[] = {
297 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
298 TX_BD_FLAGS_LHINT_512_TO_1023,
299 TX_BD_FLAGS_LHINT_1024_TO_2047,
300 TX_BD_FLAGS_LHINT_1024_TO_2047,
301 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
302 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
303 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
304 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
305 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
306 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
307 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
308 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
309 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
310 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
311 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
312 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
313 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
314 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
315 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
318 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
320 struct metadata_dst *md_dst = skb_metadata_dst(skb);
322 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
325 return md_dst->u.port_info.port_id;
328 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
331 bnxt_db_write(bp, &txr->tx_db, prod);
332 txr->kick_pending = 0;
335 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
337 struct bnxt *bp = netdev_priv(dev);
339 struct tx_bd_ext *txbd1;
340 struct netdev_queue *txq;
343 unsigned int length, pad = 0;
344 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
346 struct pci_dev *pdev = bp->pdev;
347 struct bnxt_tx_ring_info *txr;
348 struct bnxt_sw_tx_bd *tx_buf;
351 i = skb_get_queue_mapping(skb);
352 if (unlikely(i >= bp->tx_nr_rings)) {
353 dev_kfree_skb_any(skb);
354 dev_core_stats_tx_dropped_inc(dev);
358 txq = netdev_get_tx_queue(dev, i);
359 txr = &bp->tx_ring[bp->tx_ring_map[i]];
362 free_size = bnxt_tx_avail(bp, txr);
363 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
364 /* We must have raced with NAPI cleanup */
365 if (net_ratelimit() && txr->kick_pending)
366 netif_warn(bp, tx_err, dev,
367 "bnxt: ring busy w/ flush pending!\n");
368 if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
370 return NETDEV_TX_BUSY;
373 if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
377 len = skb_headlen(skb);
378 last_frag = skb_shinfo(skb)->nr_frags;
380 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
382 txbd->tx_bd_opaque = prod;
384 tx_buf = &txr->tx_buf_ring[prod];
386 tx_buf->nr_frags = last_frag;
389 cfa_action = bnxt_xmit_get_cfa_action(skb);
390 if (skb_vlan_tag_present(skb)) {
391 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
392 skb_vlan_tag_get(skb);
393 /* Currently supports 8021Q, 8021AD vlan offloads
394 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
396 if (skb->vlan_proto == htons(ETH_P_8021Q))
397 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
400 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
401 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
403 if (ptp && ptp->tx_tstamp_en && !skb_is_gso(skb) &&
404 atomic_dec_if_positive(&ptp->tx_avail) >= 0) {
405 if (!bnxt_ptp_parse(skb, &ptp->tx_seqid,
408 ptp->tx_hdr_off += VLAN_HLEN;
409 lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
410 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
412 atomic_inc(&bp->ptp_cfg->tx_avail);
417 if (unlikely(skb->no_fcs))
418 lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
420 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
422 struct tx_push_buffer *tx_push_buf = txr->tx_push;
423 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
424 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
425 void __iomem *db = txr->tx_db.doorbell;
426 void *pdata = tx_push_buf->data;
430 /* Set COAL_NOW to be ready quickly for the next push */
431 tx_push->tx_bd_len_flags_type =
432 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
433 TX_BD_TYPE_LONG_TX_BD |
434 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
435 TX_BD_FLAGS_COAL_NOW |
436 TX_BD_FLAGS_PACKET_END |
437 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
439 if (skb->ip_summed == CHECKSUM_PARTIAL)
440 tx_push1->tx_bd_hsize_lflags =
441 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
443 tx_push1->tx_bd_hsize_lflags = 0;
445 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
446 tx_push1->tx_bd_cfa_action =
447 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
449 end = pdata + length;
450 end = PTR_ALIGN(end, 8) - 1;
453 skb_copy_from_linear_data(skb, pdata, len);
455 for (j = 0; j < last_frag; j++) {
456 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
459 fptr = skb_frag_address_safe(frag);
463 memcpy(pdata, fptr, skb_frag_size(frag));
464 pdata += skb_frag_size(frag);
467 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
468 txbd->tx_bd_haddr = txr->data_mapping;
469 prod = NEXT_TX(prod);
470 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
471 memcpy(txbd, tx_push1, sizeof(*txbd));
472 prod = NEXT_TX(prod);
474 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
475 WRITE_ONCE(txr->tx_prod, prod);
478 netdev_tx_sent_queue(txq, skb->len);
479 wmb(); /* Sync is_push and byte queue before pushing data */
481 push_len = (length + sizeof(*tx_push) + 7) / 8;
483 __iowrite64_copy(db, tx_push_buf, 16);
484 __iowrite32_copy(db + 4, tx_push_buf + 1,
485 (push_len - 16) << 1);
487 __iowrite64_copy(db, tx_push_buf, push_len);
494 if (length < BNXT_MIN_PKT_SIZE) {
495 pad = BNXT_MIN_PKT_SIZE - length;
496 if (skb_pad(skb, pad))
497 /* SKB already freed. */
498 goto tx_kick_pending;
499 length = BNXT_MIN_PKT_SIZE;
502 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
504 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
507 dma_unmap_addr_set(tx_buf, mapping, mapping);
508 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
509 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
511 txbd->tx_bd_haddr = cpu_to_le64(mapping);
513 prod = NEXT_TX(prod);
514 txbd1 = (struct tx_bd_ext *)
515 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
517 txbd1->tx_bd_hsize_lflags = lflags;
518 if (skb_is_gso(skb)) {
521 if (skb->encapsulation)
522 hdr_len = skb_inner_tcp_all_headers(skb);
524 hdr_len = skb_tcp_all_headers(skb);
526 txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
528 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
529 length = skb_shinfo(skb)->gso_size;
530 txbd1->tx_bd_mss = cpu_to_le32(length);
532 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
533 txbd1->tx_bd_hsize_lflags |=
534 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
535 txbd1->tx_bd_mss = 0;
539 if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
540 dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
545 flags |= bnxt_lhint_arr[length];
546 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
548 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
549 txbd1->tx_bd_cfa_action =
550 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
551 for (i = 0; i < last_frag; i++) {
552 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
554 prod = NEXT_TX(prod);
555 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
557 len = skb_frag_size(frag);
558 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
561 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
564 tx_buf = &txr->tx_buf_ring[prod];
565 dma_unmap_addr_set(tx_buf, mapping, mapping);
567 txbd->tx_bd_haddr = cpu_to_le64(mapping);
569 flags = len << TX_BD_LEN_SHIFT;
570 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
574 txbd->tx_bd_len_flags_type =
575 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
576 TX_BD_FLAGS_PACKET_END);
578 netdev_tx_sent_queue(txq, skb->len);
580 skb_tx_timestamp(skb);
582 /* Sync BD data before updating doorbell */
585 prod = NEXT_TX(prod);
586 WRITE_ONCE(txr->tx_prod, prod);
588 if (!netdev_xmit_more() || netif_xmit_stopped(txq))
589 bnxt_txr_db_kick(bp, txr, prod);
591 txr->kick_pending = 1;
595 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
596 if (netdev_xmit_more() && !tx_buf->is_push)
597 bnxt_txr_db_kick(bp, txr, prod);
599 netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
605 if (BNXT_TX_PTP_IS_SET(lflags))
606 atomic_inc(&bp->ptp_cfg->tx_avail);
610 /* start back at beginning and unmap skb */
612 tx_buf = &txr->tx_buf_ring[prod];
613 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
614 skb_headlen(skb), DMA_TO_DEVICE);
615 prod = NEXT_TX(prod);
617 /* unmap remaining mapped pages */
618 for (i = 0; i < last_frag; i++) {
619 prod = NEXT_TX(prod);
620 tx_buf = &txr->tx_buf_ring[prod];
621 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
622 skb_frag_size(&skb_shinfo(skb)->frags[i]),
627 dev_kfree_skb_any(skb);
629 if (txr->kick_pending)
630 bnxt_txr_db_kick(bp, txr, txr->tx_prod);
631 txr->tx_buf_ring[txr->tx_prod].skb = NULL;
632 dev_core_stats_tx_dropped_inc(dev);
636 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
638 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
639 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
640 u16 cons = txr->tx_cons;
641 struct pci_dev *pdev = bp->pdev;
643 unsigned int tx_bytes = 0;
645 for (i = 0; i < nr_pkts; i++) {
646 struct bnxt_sw_tx_bd *tx_buf;
650 tx_buf = &txr->tx_buf_ring[cons];
651 cons = NEXT_TX(cons);
655 tx_bytes += skb->len;
657 if (tx_buf->is_push) {
662 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
663 skb_headlen(skb), DMA_TO_DEVICE);
664 last = tx_buf->nr_frags;
666 for (j = 0; j < last; j++) {
667 cons = NEXT_TX(cons);
668 tx_buf = &txr->tx_buf_ring[cons];
671 dma_unmap_addr(tx_buf, mapping),
672 skb_frag_size(&skb_shinfo(skb)->frags[j]),
675 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
676 if (bp->flags & BNXT_FLAG_CHIP_P5) {
677 /* PTP worker takes ownership of the skb */
678 if (!bnxt_get_tx_ts_p5(bp, skb))
681 atomic_inc(&bp->ptp_cfg->tx_avail);
686 cons = NEXT_TX(cons);
688 dev_kfree_skb_any(skb);
691 WRITE_ONCE(txr->tx_cons, cons);
693 __netif_txq_completed_wake(txq, nr_pkts, tx_bytes,
694 bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
695 READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
698 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
699 struct bnxt_rx_ring_info *rxr,
702 struct device *dev = &bp->pdev->dev;
705 page = page_pool_dev_alloc_pages(rxr->page_pool);
709 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
710 DMA_ATTR_WEAK_ORDERING);
711 if (dma_mapping_error(dev, *mapping)) {
712 page_pool_recycle_direct(rxr->page_pool, page);
718 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
722 struct pci_dev *pdev = bp->pdev;
724 if (gfp == GFP_ATOMIC)
725 data = napi_alloc_frag(bp->rx_buf_size);
727 data = netdev_alloc_frag(bp->rx_buf_size);
731 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
732 bp->rx_buf_use_size, bp->rx_dir,
733 DMA_ATTR_WEAK_ORDERING);
735 if (dma_mapping_error(&pdev->dev, *mapping)) {
742 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
745 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
746 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
749 if (BNXT_RX_PAGE_MODE(bp)) {
751 __bnxt_alloc_rx_page(bp, &mapping, rxr, gfp);
756 mapping += bp->rx_dma_offset;
758 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
760 u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, gfp);
766 rx_buf->data_ptr = data + bp->rx_offset;
768 rx_buf->mapping = mapping;
770 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
774 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
776 u16 prod = rxr->rx_prod;
777 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
778 struct rx_bd *cons_bd, *prod_bd;
780 prod_rx_buf = &rxr->rx_buf_ring[prod];
781 cons_rx_buf = &rxr->rx_buf_ring[cons];
783 prod_rx_buf->data = data;
784 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
786 prod_rx_buf->mapping = cons_rx_buf->mapping;
788 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
789 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
791 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
794 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
796 u16 next, max = rxr->rx_agg_bmap_size;
798 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
800 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
804 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
805 struct bnxt_rx_ring_info *rxr,
809 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
810 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
811 struct pci_dev *pdev = bp->pdev;
814 u16 sw_prod = rxr->rx_sw_agg_prod;
815 unsigned int offset = 0;
817 if (BNXT_RX_PAGE_MODE(bp)) {
818 page = __bnxt_alloc_rx_page(bp, &mapping, rxr, gfp);
824 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
827 page = alloc_page(gfp);
831 rxr->rx_page_offset = 0;
833 offset = rxr->rx_page_offset;
834 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
835 if (rxr->rx_page_offset == PAGE_SIZE)
840 page = alloc_page(gfp);
845 mapping = dma_map_page_attrs(&pdev->dev, page, offset,
846 BNXT_RX_PAGE_SIZE, DMA_FROM_DEVICE,
847 DMA_ATTR_WEAK_ORDERING);
848 if (dma_mapping_error(&pdev->dev, mapping)) {
854 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
855 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
857 __set_bit(sw_prod, rxr->rx_agg_bmap);
858 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
859 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
861 rx_agg_buf->page = page;
862 rx_agg_buf->offset = offset;
863 rx_agg_buf->mapping = mapping;
864 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
865 rxbd->rx_bd_opaque = sw_prod;
869 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
870 struct bnxt_cp_ring_info *cpr,
871 u16 cp_cons, u16 curr)
873 struct rx_agg_cmp *agg;
875 cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
876 agg = (struct rx_agg_cmp *)
877 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
881 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
882 struct bnxt_rx_ring_info *rxr,
883 u16 agg_id, u16 curr)
885 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
887 return &tpa_info->agg_arr[curr];
890 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
891 u16 start, u32 agg_bufs, bool tpa)
893 struct bnxt_napi *bnapi = cpr->bnapi;
894 struct bnxt *bp = bnapi->bp;
895 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
896 u16 prod = rxr->rx_agg_prod;
897 u16 sw_prod = rxr->rx_sw_agg_prod;
901 if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
904 for (i = 0; i < agg_bufs; i++) {
906 struct rx_agg_cmp *agg;
907 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
908 struct rx_bd *prod_bd;
912 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
914 agg = bnxt_get_agg(bp, cpr, idx, start + i);
915 cons = agg->rx_agg_cmp_opaque;
916 __clear_bit(cons, rxr->rx_agg_bmap);
918 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
919 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
921 __set_bit(sw_prod, rxr->rx_agg_bmap);
922 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
923 cons_rx_buf = &rxr->rx_agg_ring[cons];
925 /* It is possible for sw_prod to be equal to cons, so
926 * set cons_rx_buf->page to NULL first.
928 page = cons_rx_buf->page;
929 cons_rx_buf->page = NULL;
930 prod_rx_buf->page = page;
931 prod_rx_buf->offset = cons_rx_buf->offset;
933 prod_rx_buf->mapping = cons_rx_buf->mapping;
935 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
937 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
938 prod_bd->rx_bd_opaque = sw_prod;
940 prod = NEXT_RX_AGG(prod);
941 sw_prod = NEXT_RX_AGG(sw_prod);
943 rxr->rx_agg_prod = prod;
944 rxr->rx_sw_agg_prod = sw_prod;
947 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
948 struct bnxt_rx_ring_info *rxr,
949 u16 cons, void *data, u8 *data_ptr,
951 unsigned int offset_and_len)
953 unsigned int len = offset_and_len & 0xffff;
954 struct page *page = data;
955 u16 prod = rxr->rx_prod;
959 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
961 bnxt_reuse_rx_data(rxr, cons, data);
964 dma_addr -= bp->rx_dma_offset;
965 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
966 DMA_ATTR_WEAK_ORDERING);
967 skb = build_skb(page_address(page), PAGE_SIZE);
969 page_pool_recycle_direct(rxr->page_pool, page);
972 skb_mark_for_recycle(skb);
973 skb_reserve(skb, bp->rx_dma_offset);
979 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
980 struct bnxt_rx_ring_info *rxr,
981 u16 cons, void *data, u8 *data_ptr,
983 unsigned int offset_and_len)
985 unsigned int payload = offset_and_len >> 16;
986 unsigned int len = offset_and_len & 0xffff;
988 struct page *page = data;
989 u16 prod = rxr->rx_prod;
993 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
995 bnxt_reuse_rx_data(rxr, cons, data);
998 dma_addr -= bp->rx_dma_offset;
999 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
1000 DMA_ATTR_WEAK_ORDERING);
1002 if (unlikely(!payload))
1003 payload = eth_get_headlen(bp->dev, data_ptr, len);
1005 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1007 page_pool_recycle_direct(rxr->page_pool, page);
1011 skb_mark_for_recycle(skb);
1012 off = (void *)data_ptr - page_address(page);
1013 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
1014 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1015 payload + NET_IP_ALIGN);
1017 frag = &skb_shinfo(skb)->frags[0];
1018 skb_frag_size_sub(frag, payload);
1019 skb_frag_off_add(frag, payload);
1020 skb->data_len -= payload;
1021 skb->tail += payload;
1026 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1027 struct bnxt_rx_ring_info *rxr, u16 cons,
1028 void *data, u8 *data_ptr,
1029 dma_addr_t dma_addr,
1030 unsigned int offset_and_len)
1032 u16 prod = rxr->rx_prod;
1033 struct sk_buff *skb;
1036 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1037 if (unlikely(err)) {
1038 bnxt_reuse_rx_data(rxr, cons, data);
1042 skb = build_skb(data, bp->rx_buf_size);
1043 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1044 bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
1046 skb_free_frag(data);
1050 skb_reserve(skb, bp->rx_offset);
1051 skb_put(skb, offset_and_len & 0xffff);
1055 static u32 __bnxt_rx_agg_pages(struct bnxt *bp,
1056 struct bnxt_cp_ring_info *cpr,
1057 struct skb_shared_info *shinfo,
1058 u16 idx, u32 agg_bufs, bool tpa,
1059 struct xdp_buff *xdp)
1061 struct bnxt_napi *bnapi = cpr->bnapi;
1062 struct pci_dev *pdev = bp->pdev;
1063 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1064 u16 prod = rxr->rx_agg_prod;
1065 u32 i, total_frag_len = 0;
1066 bool p5_tpa = false;
1068 if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
1071 for (i = 0; i < agg_bufs; i++) {
1072 skb_frag_t *frag = &shinfo->frags[i];
1074 struct rx_agg_cmp *agg;
1075 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1080 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1082 agg = bnxt_get_agg(bp, cpr, idx, i);
1083 cons = agg->rx_agg_cmp_opaque;
1084 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1085 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1087 cons_rx_buf = &rxr->rx_agg_ring[cons];
1088 skb_frag_off_set(frag, cons_rx_buf->offset);
1089 skb_frag_size_set(frag, frag_len);
1090 __skb_frag_set_page(frag, cons_rx_buf->page);
1091 shinfo->nr_frags = i + 1;
1092 __clear_bit(cons, rxr->rx_agg_bmap);
1094 /* It is possible for bnxt_alloc_rx_page() to allocate
1095 * a sw_prod index that equals the cons index, so we
1096 * need to clear the cons entry now.
1098 mapping = cons_rx_buf->mapping;
1099 page = cons_rx_buf->page;
1100 cons_rx_buf->page = NULL;
1102 if (xdp && page_is_pfmemalloc(page))
1103 xdp_buff_set_frag_pfmemalloc(xdp);
1105 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1106 unsigned int nr_frags;
1108 nr_frags = --shinfo->nr_frags;
1109 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
1110 cons_rx_buf->page = page;
1112 /* Update prod since possibly some pages have been
1113 * allocated already.
1115 rxr->rx_agg_prod = prod;
1116 bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1120 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1122 DMA_ATTR_WEAK_ORDERING);
1124 total_frag_len += frag_len;
1125 prod = NEXT_RX_AGG(prod);
1127 rxr->rx_agg_prod = prod;
1128 return total_frag_len;
1131 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp,
1132 struct bnxt_cp_ring_info *cpr,
1133 struct sk_buff *skb, u16 idx,
1134 u32 agg_bufs, bool tpa)
1136 struct skb_shared_info *shinfo = skb_shinfo(skb);
1137 u32 total_frag_len = 0;
1139 total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx,
1140 agg_bufs, tpa, NULL);
1141 if (!total_frag_len) {
1146 skb->data_len += total_frag_len;
1147 skb->len += total_frag_len;
1148 skb->truesize += PAGE_SIZE * agg_bufs;
1152 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp,
1153 struct bnxt_cp_ring_info *cpr,
1154 struct xdp_buff *xdp, u16 idx,
1155 u32 agg_bufs, bool tpa)
1157 struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1158 u32 total_frag_len = 0;
1160 if (!xdp_buff_has_frags(xdp))
1161 shinfo->nr_frags = 0;
1163 total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo,
1164 idx, agg_bufs, tpa, xdp);
1165 if (total_frag_len) {
1166 xdp_buff_set_frags_flag(xdp);
1167 shinfo->nr_frags = agg_bufs;
1168 shinfo->xdp_frags_size = total_frag_len;
1170 return total_frag_len;
1173 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1174 u8 agg_bufs, u32 *raw_cons)
1177 struct rx_agg_cmp *agg;
1179 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1180 last = RING_CMP(*raw_cons);
1181 agg = (struct rx_agg_cmp *)
1182 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1183 return RX_AGG_CMP_VALID(agg, *raw_cons);
1186 static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1190 struct bnxt *bp = bnapi->bp;
1191 struct pci_dev *pdev = bp->pdev;
1192 struct sk_buff *skb;
1194 skb = napi_alloc_skb(&bnapi->napi, len);
1198 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1201 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1202 len + NET_IP_ALIGN);
1204 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1211 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1212 u32 *raw_cons, void *cmp)
1214 struct rx_cmp *rxcmp = cmp;
1215 u32 tmp_raw_cons = *raw_cons;
1216 u8 cmp_type, agg_bufs = 0;
1218 cmp_type = RX_CMP_TYPE(rxcmp);
1220 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1221 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1223 RX_CMP_AGG_BUFS_SHIFT;
1224 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1225 struct rx_tpa_end_cmp *tpa_end = cmp;
1227 if (bp->flags & BNXT_FLAG_CHIP_P5)
1230 agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1234 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1237 *raw_cons = tmp_raw_cons;
1241 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
1243 if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
1247 queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
1249 schedule_delayed_work(&bp->fw_reset_task, delay);
1252 static void bnxt_queue_sp_work(struct bnxt *bp)
1255 queue_work(bnxt_pf_wq, &bp->sp_task);
1257 schedule_work(&bp->sp_task);
1260 static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1262 if (!rxr->bnapi->in_reset) {
1263 rxr->bnapi->in_reset = true;
1264 if (bp->flags & BNXT_FLAG_CHIP_P5)
1265 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1267 set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
1268 bnxt_queue_sp_work(bp);
1270 rxr->rx_next_cons = 0xffff;
1273 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1275 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1276 u16 idx = agg_id & MAX_TPA_P5_MASK;
1278 if (test_bit(idx, map->agg_idx_bmap))
1279 idx = find_first_zero_bit(map->agg_idx_bmap,
1280 BNXT_AGG_IDX_BMAP_SIZE);
1281 __set_bit(idx, map->agg_idx_bmap);
1282 map->agg_id_tbl[agg_id] = idx;
1286 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1288 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1290 __clear_bit(idx, map->agg_idx_bmap);
1293 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1295 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1297 return map->agg_id_tbl[agg_id];
1300 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1301 struct rx_tpa_start_cmp *tpa_start,
1302 struct rx_tpa_start_cmp_ext *tpa_start1)
1304 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1305 struct bnxt_tpa_info *tpa_info;
1306 u16 cons, prod, agg_id;
1307 struct rx_bd *prod_bd;
1310 if (bp->flags & BNXT_FLAG_CHIP_P5) {
1311 agg_id = TPA_START_AGG_ID_P5(tpa_start);
1312 agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1314 agg_id = TPA_START_AGG_ID(tpa_start);
1316 cons = tpa_start->rx_tpa_start_cmp_opaque;
1317 prod = rxr->rx_prod;
1318 cons_rx_buf = &rxr->rx_buf_ring[cons];
1319 prod_rx_buf = &rxr->rx_buf_ring[prod];
1320 tpa_info = &rxr->rx_tpa[agg_id];
1322 if (unlikely(cons != rxr->rx_next_cons ||
1323 TPA_START_ERROR(tpa_start))) {
1324 netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1325 cons, rxr->rx_next_cons,
1326 TPA_START_ERROR_CODE(tpa_start1));
1327 bnxt_sched_reset(bp, rxr);
1330 /* Store cfa_code in tpa_info to use in tpa_end
1331 * completion processing.
1333 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1334 prod_rx_buf->data = tpa_info->data;
1335 prod_rx_buf->data_ptr = tpa_info->data_ptr;
1337 mapping = tpa_info->mapping;
1338 prod_rx_buf->mapping = mapping;
1340 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1342 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1344 tpa_info->data = cons_rx_buf->data;
1345 tpa_info->data_ptr = cons_rx_buf->data_ptr;
1346 cons_rx_buf->data = NULL;
1347 tpa_info->mapping = cons_rx_buf->mapping;
1350 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1351 RX_TPA_START_CMP_LEN_SHIFT;
1352 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1353 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1355 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1356 tpa_info->gso_type = SKB_GSO_TCPV4;
1357 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1358 if (hash_type == 3 || TPA_START_IS_IPV6(tpa_start1))
1359 tpa_info->gso_type = SKB_GSO_TCPV6;
1360 tpa_info->rss_hash =
1361 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1363 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1364 tpa_info->gso_type = 0;
1365 netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1367 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1368 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1369 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1370 tpa_info->agg_count = 0;
1372 rxr->rx_prod = NEXT_RX(prod);
1373 cons = NEXT_RX(cons);
1374 rxr->rx_next_cons = NEXT_RX(cons);
1375 cons_rx_buf = &rxr->rx_buf_ring[cons];
1377 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1378 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1379 cons_rx_buf->data = NULL;
1382 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1385 bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1389 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1391 struct udphdr *uh = NULL;
1393 if (ip_proto == htons(ETH_P_IP)) {
1394 struct iphdr *iph = (struct iphdr *)skb->data;
1396 if (iph->protocol == IPPROTO_UDP)
1397 uh = (struct udphdr *)(iph + 1);
1399 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1401 if (iph->nexthdr == IPPROTO_UDP)
1402 uh = (struct udphdr *)(iph + 1);
1406 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1408 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1413 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1414 int payload_off, int tcp_ts,
1415 struct sk_buff *skb)
1420 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1421 u32 hdr_info = tpa_info->hdr_info;
1422 bool loopback = false;
1424 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1425 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1426 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1428 /* If the packet is an internal loopback packet, the offsets will
1429 * have an extra 4 bytes.
1431 if (inner_mac_off == 4) {
1433 } else if (inner_mac_off > 4) {
1434 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1437 /* We only support inner iPv4/ipv6. If we don't see the
1438 * correct protocol ID, it must be a loopback packet where
1439 * the offsets are off by 4.
1441 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1445 /* internal loopback packet, subtract all offsets by 4 */
1451 nw_off = inner_ip_off - ETH_HLEN;
1452 skb_set_network_header(skb, nw_off);
1453 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1454 struct ipv6hdr *iph = ipv6_hdr(skb);
1456 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1457 len = skb->len - skb_transport_offset(skb);
1459 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1461 struct iphdr *iph = ip_hdr(skb);
1463 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1464 len = skb->len - skb_transport_offset(skb);
1466 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1469 if (inner_mac_off) { /* tunnel */
1470 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1473 bnxt_gro_tunnel(skb, proto);
1479 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1480 int payload_off, int tcp_ts,
1481 struct sk_buff *skb)
1484 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1485 u32 hdr_info = tpa_info->hdr_info;
1486 int iphdr_len, nw_off;
1488 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1489 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1490 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1492 nw_off = inner_ip_off - ETH_HLEN;
1493 skb_set_network_header(skb, nw_off);
1494 iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1495 sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1496 skb_set_transport_header(skb, nw_off + iphdr_len);
1498 if (inner_mac_off) { /* tunnel */
1499 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1502 bnxt_gro_tunnel(skb, proto);
1508 #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1509 #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1511 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1512 int payload_off, int tcp_ts,
1513 struct sk_buff *skb)
1517 int len, nw_off, tcp_opt_len = 0;
1522 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1525 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1527 skb_set_network_header(skb, nw_off);
1529 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1530 len = skb->len - skb_transport_offset(skb);
1532 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1533 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1534 struct ipv6hdr *iph;
1536 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1538 skb_set_network_header(skb, nw_off);
1539 iph = ipv6_hdr(skb);
1540 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1541 len = skb->len - skb_transport_offset(skb);
1543 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1545 dev_kfree_skb_any(skb);
1549 if (nw_off) /* tunnel */
1550 bnxt_gro_tunnel(skb, skb->protocol);
1555 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1556 struct bnxt_tpa_info *tpa_info,
1557 struct rx_tpa_end_cmp *tpa_end,
1558 struct rx_tpa_end_cmp_ext *tpa_end1,
1559 struct sk_buff *skb)
1565 segs = TPA_END_TPA_SEGS(tpa_end);
1569 NAPI_GRO_CB(skb)->count = segs;
1570 skb_shinfo(skb)->gso_size =
1571 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1572 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1573 if (bp->flags & BNXT_FLAG_CHIP_P5)
1574 payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1576 payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1577 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1579 tcp_gro_complete(skb);
1584 /* Given the cfa_code of a received packet determine which
1585 * netdev (vf-rep or PF) the packet is destined to.
1587 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1589 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1591 /* if vf-rep dev is NULL, the must belongs to the PF */
1592 return dev ? dev : bp->dev;
1595 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1596 struct bnxt_cp_ring_info *cpr,
1598 struct rx_tpa_end_cmp *tpa_end,
1599 struct rx_tpa_end_cmp_ext *tpa_end1,
1602 struct bnxt_napi *bnapi = cpr->bnapi;
1603 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1604 u8 *data_ptr, agg_bufs;
1606 struct bnxt_tpa_info *tpa_info;
1608 struct sk_buff *skb;
1609 u16 idx = 0, agg_id;
1613 if (unlikely(bnapi->in_reset)) {
1614 int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1617 return ERR_PTR(-EBUSY);
1621 if (bp->flags & BNXT_FLAG_CHIP_P5) {
1622 agg_id = TPA_END_AGG_ID_P5(tpa_end);
1623 agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1624 agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1625 tpa_info = &rxr->rx_tpa[agg_id];
1626 if (unlikely(agg_bufs != tpa_info->agg_count)) {
1627 netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1628 agg_bufs, tpa_info->agg_count);
1629 agg_bufs = tpa_info->agg_count;
1631 tpa_info->agg_count = 0;
1632 *event |= BNXT_AGG_EVENT;
1633 bnxt_free_agg_idx(rxr, agg_id);
1635 gro = !!(bp->flags & BNXT_FLAG_GRO);
1637 agg_id = TPA_END_AGG_ID(tpa_end);
1638 agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1639 tpa_info = &rxr->rx_tpa[agg_id];
1640 idx = RING_CMP(*raw_cons);
1642 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1643 return ERR_PTR(-EBUSY);
1645 *event |= BNXT_AGG_EVENT;
1646 idx = NEXT_CMP(idx);
1648 gro = !!TPA_END_GRO(tpa_end);
1650 data = tpa_info->data;
1651 data_ptr = tpa_info->data_ptr;
1653 len = tpa_info->len;
1654 mapping = tpa_info->mapping;
1656 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1657 bnxt_abort_tpa(cpr, idx, agg_bufs);
1658 if (agg_bufs > MAX_SKB_FRAGS)
1659 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1660 agg_bufs, (int)MAX_SKB_FRAGS);
1664 if (len <= bp->rx_copy_thresh) {
1665 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1667 bnxt_abort_tpa(cpr, idx, agg_bufs);
1668 cpr->sw_stats.rx.rx_oom_discards += 1;
1673 dma_addr_t new_mapping;
1675 new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, GFP_ATOMIC);
1677 bnxt_abort_tpa(cpr, idx, agg_bufs);
1678 cpr->sw_stats.rx.rx_oom_discards += 1;
1682 tpa_info->data = new_data;
1683 tpa_info->data_ptr = new_data + bp->rx_offset;
1684 tpa_info->mapping = new_mapping;
1686 skb = build_skb(data, bp->rx_buf_size);
1687 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1688 bp->rx_buf_use_size, bp->rx_dir,
1689 DMA_ATTR_WEAK_ORDERING);
1692 skb_free_frag(data);
1693 bnxt_abort_tpa(cpr, idx, agg_bufs);
1694 cpr->sw_stats.rx.rx_oom_discards += 1;
1697 skb_reserve(skb, bp->rx_offset);
1702 skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true);
1704 /* Page reuse already handled by bnxt_rx_pages(). */
1705 cpr->sw_stats.rx.rx_oom_discards += 1;
1711 eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
1713 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1714 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1716 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1717 (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1718 __be16 vlan_proto = htons(tpa_info->metadata >>
1719 RX_CMP_FLAGS2_METADATA_TPID_SFT);
1720 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1722 if (eth_type_vlan(vlan_proto)) {
1723 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1730 skb_checksum_none_assert(skb);
1731 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1732 skb->ip_summed = CHECKSUM_UNNECESSARY;
1734 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1738 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1743 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1744 struct rx_agg_cmp *rx_agg)
1746 u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1747 struct bnxt_tpa_info *tpa_info;
1749 agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1750 tpa_info = &rxr->rx_tpa[agg_id];
1751 BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1752 tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1755 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1756 struct sk_buff *skb)
1758 if (skb->dev != bp->dev) {
1759 /* this packet belongs to a vf-rep */
1760 bnxt_vf_rep_rx(bp, skb);
1763 skb_record_rx_queue(skb, bnapi->index);
1764 napi_gro_receive(&bnapi->napi, skb);
1767 /* returns the following:
1768 * 1 - 1 packet successfully received
1769 * 0 - successful TPA_START, packet not completed yet
1770 * -EBUSY - completion ring does not have all the agg buffers yet
1771 * -ENOMEM - packet aborted due to out of memory
1772 * -EIO - packet aborted due to hw error indicated in BD
1774 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1775 u32 *raw_cons, u8 *event)
1777 struct bnxt_napi *bnapi = cpr->bnapi;
1778 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1779 struct net_device *dev = bp->dev;
1780 struct rx_cmp *rxcmp;
1781 struct rx_cmp_ext *rxcmp1;
1782 u32 tmp_raw_cons = *raw_cons;
1783 u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1784 struct bnxt_sw_rx_bd *rx_buf;
1786 u8 *data_ptr, agg_bufs, cmp_type;
1787 bool xdp_active = false;
1788 dma_addr_t dma_addr;
1789 struct sk_buff *skb;
1790 struct xdp_buff xdp;
1795 rxcmp = (struct rx_cmp *)
1796 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1798 cmp_type = RX_CMP_TYPE(rxcmp);
1800 if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
1801 bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
1802 goto next_rx_no_prod_no_len;
1805 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1806 cp_cons = RING_CMP(tmp_raw_cons);
1807 rxcmp1 = (struct rx_cmp_ext *)
1808 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1810 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1813 /* The valid test of the entry must be done first before
1814 * reading any further.
1817 prod = rxr->rx_prod;
1819 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1820 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1821 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1823 *event |= BNXT_RX_EVENT;
1824 goto next_rx_no_prod_no_len;
1826 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1827 skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
1828 (struct rx_tpa_end_cmp *)rxcmp,
1829 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
1836 bnxt_deliver_skb(bp, bnapi, skb);
1839 *event |= BNXT_RX_EVENT;
1840 goto next_rx_no_prod_no_len;
1843 cons = rxcmp->rx_cmp_opaque;
1844 if (unlikely(cons != rxr->rx_next_cons)) {
1845 int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
1847 /* 0xffff is forced error, don't print it */
1848 if (rxr->rx_next_cons != 0xffff)
1849 netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
1850 cons, rxr->rx_next_cons);
1851 bnxt_sched_reset(bp, rxr);
1854 goto next_rx_no_prod_no_len;
1856 rx_buf = &rxr->rx_buf_ring[cons];
1857 data = rx_buf->data;
1858 data_ptr = rx_buf->data_ptr;
1861 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1862 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
1865 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1868 cp_cons = NEXT_CMP(cp_cons);
1869 *event |= BNXT_AGG_EVENT;
1871 *event |= BNXT_RX_EVENT;
1873 rx_buf->data = NULL;
1874 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1875 u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
1877 bnxt_reuse_rx_data(rxr, cons, data);
1879 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
1883 if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
1884 bnapi->cp_ring.sw_stats.rx.rx_buf_errors++;
1885 if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
1886 !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
1887 netdev_warn_once(bp->dev, "RX buffer error %x\n",
1889 bnxt_sched_reset(bp, rxr);
1892 goto next_rx_no_len;
1895 flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
1896 len = flags >> RX_CMP_LEN_SHIFT;
1897 dma_addr = rx_buf->mapping;
1899 if (bnxt_xdp_attached(bp, rxr)) {
1900 bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
1902 u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
1906 cpr->sw_stats.rx.rx_oom_discards += 1;
1915 if (bnxt_rx_xdp(bp, rxr, cons, xdp, data, &data_ptr, &len, event)) {
1921 if (len <= bp->rx_copy_thresh) {
1922 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
1923 bnxt_reuse_rx_data(rxr, cons, data);
1927 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
1930 bnxt_xdp_buff_frags_free(rxr, &xdp);
1932 cpr->sw_stats.rx.rx_oom_discards += 1;
1939 if (rx_buf->data_ptr == data_ptr)
1940 payload = misc & RX_CMP_PAYLOAD_OFFSET;
1943 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
1946 cpr->sw_stats.rx.rx_oom_discards += 1;
1954 skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
1956 cpr->sw_stats.rx.rx_oom_discards += 1;
1961 skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1);
1963 /* we should be able to free the old skb here */
1964 bnxt_xdp_buff_frags_free(rxr, &xdp);
1965 cpr->sw_stats.rx.rx_oom_discards += 1;
1972 if (RX_CMP_HASH_VALID(rxcmp)) {
1973 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1974 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1976 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1977 if (hash_type != 1 && hash_type != 3)
1978 type = PKT_HASH_TYPE_L3;
1979 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1982 cfa_code = RX_CMP_CFA_CODE(rxcmp1);
1983 skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
1985 if ((rxcmp1->rx_cmp_flags2 &
1986 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1987 (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1988 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1989 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1990 __be16 vlan_proto = htons(meta_data >>
1991 RX_CMP_FLAGS2_METADATA_TPID_SFT);
1993 if (eth_type_vlan(vlan_proto)) {
1994 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2001 skb_checksum_none_assert(skb);
2002 if (RX_CMP_L4_CS_OK(rxcmp1)) {
2003 if (dev->features & NETIF_F_RXCSUM) {
2004 skb->ip_summed = CHECKSUM_UNNECESSARY;
2005 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2008 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2009 if (dev->features & NETIF_F_RXCSUM)
2010 bnapi->cp_ring.sw_stats.rx.rx_l4_csum_errors++;
2014 if (unlikely((flags & RX_CMP_FLAGS_ITYPES_MASK) ==
2015 RX_CMP_FLAGS_ITYPE_PTP_W_TS) || bp->ptp_all_rx_tstamp) {
2016 if (bp->flags & BNXT_FLAG_CHIP_P5) {
2017 u32 cmpl_ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
2020 if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2021 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2023 spin_lock_bh(&ptp->ptp_lock);
2024 ns = timecounter_cyc2time(&ptp->tc, ts);
2025 spin_unlock_bh(&ptp->ptp_lock);
2026 memset(skb_hwtstamps(skb), 0,
2027 sizeof(*skb_hwtstamps(skb)));
2028 skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2032 bnxt_deliver_skb(bp, bnapi, skb);
2036 cpr->rx_packets += 1;
2037 cpr->rx_bytes += len;
2040 rxr->rx_prod = NEXT_RX(prod);
2041 rxr->rx_next_cons = NEXT_RX(cons);
2043 next_rx_no_prod_no_len:
2044 *raw_cons = tmp_raw_cons;
2049 /* In netpoll mode, if we are using a combined completion ring, we need to
2050 * discard the rx packets and recycle the buffers.
2052 static int bnxt_force_rx_discard(struct bnxt *bp,
2053 struct bnxt_cp_ring_info *cpr,
2054 u32 *raw_cons, u8 *event)
2056 u32 tmp_raw_cons = *raw_cons;
2057 struct rx_cmp_ext *rxcmp1;
2058 struct rx_cmp *rxcmp;
2063 cp_cons = RING_CMP(tmp_raw_cons);
2064 rxcmp = (struct rx_cmp *)
2065 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2067 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2068 cp_cons = RING_CMP(tmp_raw_cons);
2069 rxcmp1 = (struct rx_cmp_ext *)
2070 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2072 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2075 /* The valid test of the entry must be done first before
2076 * reading any further.
2079 cmp_type = RX_CMP_TYPE(rxcmp);
2080 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
2081 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2082 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2083 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2084 struct rx_tpa_end_cmp_ext *tpa_end1;
2086 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2087 tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2088 cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2090 rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2091 if (rc && rc != -EBUSY)
2092 cpr->sw_stats.rx.rx_netpoll_discards += 1;
2096 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2098 struct bnxt_fw_health *fw_health = bp->fw_health;
2099 u32 reg = fw_health->regs[reg_idx];
2100 u32 reg_type, reg_off, val = 0;
2102 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2103 reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2105 case BNXT_FW_HEALTH_REG_TYPE_CFG:
2106 pci_read_config_dword(bp->pdev, reg_off, &val);
2108 case BNXT_FW_HEALTH_REG_TYPE_GRC:
2109 reg_off = fw_health->mapped_regs[reg_idx];
2111 case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2112 val = readl(bp->bar0 + reg_off);
2114 case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2115 val = readl(bp->bar1 + reg_off);
2118 if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2119 val &= fw_health->fw_reset_inprog_reg_mask;
2123 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2127 for (i = 0; i < bp->rx_nr_rings; i++) {
2128 u16 grp_idx = bp->rx_ring[i].bnapi->index;
2129 struct bnxt_ring_grp_info *grp_info;
2131 grp_info = &bp->grp_info[grp_idx];
2132 if (grp_info->agg_fw_ring_id == ring_id)
2135 return INVALID_HW_RING_ID;
2138 static void bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2140 u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2143 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2144 netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2145 BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2147 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2148 netdev_warn(bp->dev, "Pause Storm detected!\n");
2150 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2151 netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2154 netdev_err(bp->dev, "FW reported unknown error type %u\n",
2160 #define BNXT_GET_EVENT_PORT(data) \
2162 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2164 #define BNXT_EVENT_RING_TYPE(data2) \
2166 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2168 #define BNXT_EVENT_RING_TYPE_RX(data2) \
2169 (BNXT_EVENT_RING_TYPE(data2) == \
2170 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2172 #define BNXT_EVENT_PHC_EVENT_TYPE(data1) \
2173 (((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2174 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2176 #define BNXT_EVENT_PHC_RTC_UPDATE(data1) \
2177 (((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2178 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2180 #define BNXT_PHC_BITS 48
2182 static int bnxt_async_event_process(struct bnxt *bp,
2183 struct hwrm_async_event_cmpl *cmpl)
2185 u16 event_id = le16_to_cpu(cmpl->event_id);
2186 u32 data1 = le32_to_cpu(cmpl->event_data1);
2187 u32 data2 = le32_to_cpu(cmpl->event_data2);
2189 netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2190 event_id, data1, data2);
2192 /* TODO CHIMP_FW: Define event id's for link change, error etc */
2194 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2195 struct bnxt_link_info *link_info = &bp->link_info;
2198 goto async_event_process_exit;
2200 /* print unsupported speed warning in forced speed mode only */
2201 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2202 (data1 & 0x20000)) {
2203 u16 fw_speed = link_info->force_link_speed;
2204 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2206 if (speed != SPEED_UNKNOWN)
2207 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2210 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2213 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2214 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2215 set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2217 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2218 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2220 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2221 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2223 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2224 u16 port_id = BNXT_GET_EVENT_PORT(data1);
2229 if (bp->pf.port_id != port_id)
2232 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2235 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2237 goto async_event_process_exit;
2238 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2240 case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2241 char *type_str = "Solicited";
2244 goto async_event_process_exit;
2246 bp->fw_reset_timestamp = jiffies;
2247 bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2248 if (!bp->fw_reset_min_dsecs)
2249 bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2250 bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2251 if (!bp->fw_reset_max_dsecs)
2252 bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2253 if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2254 set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2255 } else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2257 bp->fw_health->fatalities++;
2258 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2259 } else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2260 EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2261 type_str = "Non-fatal";
2262 bp->fw_health->survivals++;
2263 set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2265 netif_warn(bp, hw, bp->dev,
2266 "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2267 type_str, data1, data2,
2268 bp->fw_reset_min_dsecs * 100,
2269 bp->fw_reset_max_dsecs * 100);
2270 set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2273 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2274 struct bnxt_fw_health *fw_health = bp->fw_health;
2275 char *status_desc = "healthy";
2279 goto async_event_process_exit;
2281 if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2282 fw_health->enabled = false;
2283 netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2286 fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2287 fw_health->tmr_multiplier =
2288 DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2289 bp->current_interval * 10);
2290 fw_health->tmr_counter = fw_health->tmr_multiplier;
2291 if (!fw_health->enabled)
2292 fw_health->last_fw_heartbeat =
2293 bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2294 fw_health->last_fw_reset_cnt =
2295 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2296 status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2297 if (status != BNXT_FW_STATUS_HEALTHY)
2298 status_desc = "unhealthy";
2299 netif_info(bp, drv, bp->dev,
2300 "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2301 fw_health->primary ? "primary" : "backup", status,
2302 status_desc, fw_health->last_fw_reset_cnt);
2303 if (!fw_health->enabled) {
2304 /* Make sure tmr_counter is set and visible to
2305 * bnxt_health_check() before setting enabled to true.
2308 fw_health->enabled = true;
2310 goto async_event_process_exit;
2312 case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2313 netif_notice(bp, hw, bp->dev,
2314 "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2316 goto async_event_process_exit;
2317 case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2318 struct bnxt_rx_ring_info *rxr;
2321 if (bp->flags & BNXT_FLAG_CHIP_P5)
2322 goto async_event_process_exit;
2324 netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2325 BNXT_EVENT_RING_TYPE(data2), data1);
2326 if (!BNXT_EVENT_RING_TYPE_RX(data2))
2327 goto async_event_process_exit;
2329 grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2330 if (grp_idx == INVALID_HW_RING_ID) {
2331 netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2333 goto async_event_process_exit;
2335 rxr = bp->bnapi[grp_idx]->rx_ring;
2336 bnxt_sched_reset(bp, rxr);
2337 goto async_event_process_exit;
2339 case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2340 struct bnxt_fw_health *fw_health = bp->fw_health;
2342 netif_notice(bp, hw, bp->dev,
2343 "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2346 fw_health->echo_req_data1 = data1;
2347 fw_health->echo_req_data2 = data2;
2348 set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2351 goto async_event_process_exit;
2353 case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2354 bnxt_ptp_pps_event(bp, data1, data2);
2355 goto async_event_process_exit;
2357 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2358 bnxt_event_error_report(bp, data1, data2);
2359 goto async_event_process_exit;
2361 case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2362 switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2363 case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2364 if (BNXT_PTP_USE_RTC(bp)) {
2365 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2369 goto async_event_process_exit;
2371 spin_lock_bh(&ptp->ptp_lock);
2372 bnxt_ptp_update_current_time(bp);
2373 ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2374 BNXT_PHC_BITS) | ptp->current_time);
2375 bnxt_ptp_rtc_timecounter_init(ptp, ns);
2376 spin_unlock_bh(&ptp->ptp_lock);
2380 goto async_event_process_exit;
2382 case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2383 u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2385 hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2386 goto async_event_process_exit;
2389 goto async_event_process_exit;
2391 bnxt_queue_sp_work(bp);
2392 async_event_process_exit:
2396 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2398 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2399 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2400 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2401 (struct hwrm_fwd_req_cmpl *)txcmp;
2403 switch (cmpl_type) {
2404 case CMPL_BASE_TYPE_HWRM_DONE:
2405 seq_id = le16_to_cpu(h_cmpl->sequence_id);
2406 hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2409 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2410 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2412 if ((vf_id < bp->pf.first_vf_id) ||
2413 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2414 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2419 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2420 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
2421 bnxt_queue_sp_work(bp);
2424 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2425 bnxt_async_event_process(bp,
2426 (struct hwrm_async_event_cmpl *)txcmp);
2436 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2438 struct bnxt_napi *bnapi = dev_instance;
2439 struct bnxt *bp = bnapi->bp;
2440 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2441 u32 cons = RING_CMP(cpr->cp_raw_cons);
2444 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2445 napi_schedule(&bnapi->napi);
2449 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2451 u32 raw_cons = cpr->cp_raw_cons;
2452 u16 cons = RING_CMP(raw_cons);
2453 struct tx_cmp *txcmp;
2455 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2457 return TX_CMP_VALID(txcmp, raw_cons);
2460 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
2462 struct bnxt_napi *bnapi = dev_instance;
2463 struct bnxt *bp = bnapi->bp;
2464 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2465 u32 cons = RING_CMP(cpr->cp_raw_cons);
2468 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2470 if (!bnxt_has_work(bp, cpr)) {
2471 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
2472 /* return if erroneous interrupt */
2473 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
2477 /* disable ring IRQ */
2478 BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
2480 /* Return here if interrupt is shared and is disabled. */
2481 if (unlikely(atomic_read(&bp->intr_sem) != 0))
2484 napi_schedule(&bnapi->napi);
2488 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2491 struct bnxt_napi *bnapi = cpr->bnapi;
2492 u32 raw_cons = cpr->cp_raw_cons;
2497 struct tx_cmp *txcmp;
2499 cpr->has_more_work = 0;
2500 cpr->had_work_done = 1;
2504 cons = RING_CMP(raw_cons);
2505 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2507 if (!TX_CMP_VALID(txcmp, raw_cons))
2510 /* The valid test of the entry must be done first before
2511 * reading any further.
2514 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
2516 /* return full budget so NAPI will complete. */
2517 if (unlikely(tx_pkts >= bp->tx_wake_thresh)) {
2519 raw_cons = NEXT_RAW_CMP(raw_cons);
2521 cpr->has_more_work = 1;
2524 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2526 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2528 rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2530 if (likely(rc >= 0))
2532 /* Increment rx_pkts when rc is -ENOMEM to count towards
2533 * the NAPI budget. Otherwise, we may potentially loop
2534 * here forever if we consistently cannot allocate
2537 else if (rc == -ENOMEM && budget)
2539 else if (rc == -EBUSY) /* partial completion */
2541 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
2542 CMPL_BASE_TYPE_HWRM_DONE) ||
2543 (TX_CMP_TYPE(txcmp) ==
2544 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
2545 (TX_CMP_TYPE(txcmp) ==
2546 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
2547 bnxt_hwrm_handler(bp, txcmp);
2549 raw_cons = NEXT_RAW_CMP(raw_cons);
2551 if (rx_pkts && rx_pkts == budget) {
2552 cpr->has_more_work = 1;
2557 if (event & BNXT_REDIRECT_EVENT)
2560 if (event & BNXT_TX_EVENT) {
2561 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
2562 u16 prod = txr->tx_prod;
2564 /* Sync BD data before updating doorbell */
2567 bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
2570 cpr->cp_raw_cons = raw_cons;
2571 bnapi->tx_pkts += tx_pkts;
2572 bnapi->events |= event;
2576 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi)
2578 if (bnapi->tx_pkts) {
2579 bnapi->tx_int(bp, bnapi, bnapi->tx_pkts);
2583 if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
2584 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2586 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2588 if (bnapi->events & BNXT_AGG_EVENT) {
2589 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2591 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2596 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2599 struct bnxt_napi *bnapi = cpr->bnapi;
2602 rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2604 /* ACK completion ring before freeing tx ring and producing new
2605 * buffers in rx/agg rings to prevent overflowing the completion
2608 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
2610 __bnxt_poll_work_done(bp, bnapi);
2614 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
2616 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2617 struct bnxt *bp = bnapi->bp;
2618 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2619 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2620 struct tx_cmp *txcmp;
2621 struct rx_cmp_ext *rxcmp1;
2622 u32 cp_cons, tmp_raw_cons;
2623 u32 raw_cons = cpr->cp_raw_cons;
2630 cp_cons = RING_CMP(raw_cons);
2631 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2633 if (!TX_CMP_VALID(txcmp, raw_cons))
2636 /* The valid test of the entry must be done first before
2637 * reading any further.
2640 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2641 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
2642 cp_cons = RING_CMP(tmp_raw_cons);
2643 rxcmp1 = (struct rx_cmp_ext *)
2644 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2646 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2649 /* force an error to recycle the buffer */
2650 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2651 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2653 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2654 if (likely(rc == -EIO) && budget)
2656 else if (rc == -EBUSY) /* partial completion */
2658 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
2659 CMPL_BASE_TYPE_HWRM_DONE)) {
2660 bnxt_hwrm_handler(bp, txcmp);
2663 "Invalid completion received on special ring\n");
2665 raw_cons = NEXT_RAW_CMP(raw_cons);
2667 if (rx_pkts == budget)
2671 cpr->cp_raw_cons = raw_cons;
2672 BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
2673 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2675 if (event & BNXT_AGG_EVENT)
2676 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2678 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
2679 napi_complete_done(napi, rx_pkts);
2680 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2685 static int bnxt_poll(struct napi_struct *napi, int budget)
2687 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2688 struct bnxt *bp = bnapi->bp;
2689 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2692 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
2693 napi_complete(napi);
2697 work_done += bnxt_poll_work(bp, cpr, budget - work_done);
2699 if (work_done >= budget) {
2701 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2705 if (!bnxt_has_work(bp, cpr)) {
2706 if (napi_complete_done(napi, work_done))
2707 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2711 if (bp->flags & BNXT_FLAG_DIM) {
2712 struct dim_sample dim_sample = {};
2714 dim_update_sample(cpr->event_ctr,
2718 net_dim(&cpr->dim, dim_sample);
2723 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
2725 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2726 int i, work_done = 0;
2728 for (i = 0; i < 2; i++) {
2729 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2732 work_done += __bnxt_poll_work(bp, cpr2,
2733 budget - work_done);
2734 cpr->has_more_work |= cpr2->has_more_work;
2740 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2743 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2746 for (i = 0; i < 2; i++) {
2747 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2748 struct bnxt_db_info *db;
2750 if (cpr2 && cpr2->had_work_done) {
2752 bnxt_writeq(bp, db->db_key64 | dbr_type |
2753 RING_CMP(cpr2->cp_raw_cons), db->doorbell);
2754 cpr2->had_work_done = 0;
2757 __bnxt_poll_work_done(bp, bnapi);
2760 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
2762 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2763 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2764 struct bnxt_cp_ring_info *cpr_rx;
2765 u32 raw_cons = cpr->cp_raw_cons;
2766 struct bnxt *bp = bnapi->bp;
2767 struct nqe_cn *nqcmp;
2771 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
2772 napi_complete(napi);
2775 if (cpr->has_more_work) {
2776 cpr->has_more_work = 0;
2777 work_done = __bnxt_poll_cqs(bp, bnapi, budget);
2780 cons = RING_CMP(raw_cons);
2781 nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2783 if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
2784 if (cpr->has_more_work)
2787 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL);
2788 cpr->cp_raw_cons = raw_cons;
2789 if (napi_complete_done(napi, work_done))
2790 BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
2795 /* The valid test of the entry must be done first before
2796 * reading any further.
2800 if (nqcmp->type == cpu_to_le16(NQ_CN_TYPE_CQ_NOTIFICATION)) {
2801 u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
2802 struct bnxt_cp_ring_info *cpr2;
2804 /* No more budget for RX work */
2805 if (budget && work_done >= budget && idx == BNXT_RX_HDL)
2808 cpr2 = cpr->cp_ring_arr[idx];
2809 work_done += __bnxt_poll_work(bp, cpr2,
2810 budget - work_done);
2811 cpr->has_more_work |= cpr2->has_more_work;
2813 bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
2815 raw_cons = NEXT_RAW_CMP(raw_cons);
2817 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ);
2818 if (raw_cons != cpr->cp_raw_cons) {
2819 cpr->cp_raw_cons = raw_cons;
2820 BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
2823 cpr_rx = cpr->cp_ring_arr[BNXT_RX_HDL];
2824 if (cpr_rx && (bp->flags & BNXT_FLAG_DIM)) {
2825 struct dim_sample dim_sample = {};
2827 dim_update_sample(cpr->event_ctr,
2831 net_dim(&cpr->dim, dim_sample);
2836 static void bnxt_free_tx_skbs(struct bnxt *bp)
2839 struct pci_dev *pdev = bp->pdev;
2844 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
2845 for (i = 0; i < bp->tx_nr_rings; i++) {
2846 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2849 if (!txr->tx_buf_ring)
2852 for (j = 0; j < max_idx;) {
2853 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
2854 struct sk_buff *skb;
2857 if (i < bp->tx_nr_rings_xdp &&
2858 tx_buf->action == XDP_REDIRECT) {
2859 dma_unmap_single(&pdev->dev,
2860 dma_unmap_addr(tx_buf, mapping),
2861 dma_unmap_len(tx_buf, len),
2863 xdp_return_frame(tx_buf->xdpf);
2865 tx_buf->xdpf = NULL;
2878 if (tx_buf->is_push) {
2884 dma_unmap_single(&pdev->dev,
2885 dma_unmap_addr(tx_buf, mapping),
2889 last = tx_buf->nr_frags;
2891 for (k = 0; k < last; k++, j++) {
2892 int ring_idx = j & bp->tx_ring_mask;
2893 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2895 tx_buf = &txr->tx_buf_ring[ring_idx];
2898 dma_unmap_addr(tx_buf, mapping),
2899 skb_frag_size(frag), DMA_TO_DEVICE);
2903 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
2907 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr)
2909 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
2910 struct pci_dev *pdev = bp->pdev;
2911 struct bnxt_tpa_idx_map *map;
2912 int i, max_idx, max_agg_idx;
2914 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2915 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2917 goto skip_rx_tpa_free;
2919 for (i = 0; i < bp->max_tpa; i++) {
2920 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
2921 u8 *data = tpa_info->data;
2926 dma_unmap_single_attrs(&pdev->dev, tpa_info->mapping,
2927 bp->rx_buf_use_size, bp->rx_dir,
2928 DMA_ATTR_WEAK_ORDERING);
2930 tpa_info->data = NULL;
2932 skb_free_frag(data);
2936 if (!rxr->rx_buf_ring)
2937 goto skip_rx_buf_free;
2939 for (i = 0; i < max_idx; i++) {
2940 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
2941 dma_addr_t mapping = rx_buf->mapping;
2942 void *data = rx_buf->data;
2947 rx_buf->data = NULL;
2948 if (BNXT_RX_PAGE_MODE(bp)) {
2949 mapping -= bp->rx_dma_offset;
2950 dma_unmap_page_attrs(&pdev->dev, mapping, PAGE_SIZE,
2952 DMA_ATTR_WEAK_ORDERING);
2953 page_pool_recycle_direct(rxr->page_pool, data);
2955 dma_unmap_single_attrs(&pdev->dev, mapping,
2956 bp->rx_buf_use_size, bp->rx_dir,
2957 DMA_ATTR_WEAK_ORDERING);
2958 skb_free_frag(data);
2963 if (!rxr->rx_agg_ring)
2964 goto skip_rx_agg_free;
2966 for (i = 0; i < max_agg_idx; i++) {
2967 struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
2968 struct page *page = rx_agg_buf->page;
2973 if (BNXT_RX_PAGE_MODE(bp)) {
2974 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2975 BNXT_RX_PAGE_SIZE, bp->rx_dir,
2976 DMA_ATTR_WEAK_ORDERING);
2977 rx_agg_buf->page = NULL;
2978 __clear_bit(i, rxr->rx_agg_bmap);
2980 page_pool_recycle_direct(rxr->page_pool, page);
2982 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2983 BNXT_RX_PAGE_SIZE, DMA_FROM_DEVICE,
2984 DMA_ATTR_WEAK_ORDERING);
2985 rx_agg_buf->page = NULL;
2986 __clear_bit(i, rxr->rx_agg_bmap);
2994 __free_page(rxr->rx_page);
2995 rxr->rx_page = NULL;
2997 map = rxr->rx_tpa_idx_map;
2999 memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3002 static void bnxt_free_rx_skbs(struct bnxt *bp)
3009 for (i = 0; i < bp->rx_nr_rings; i++)
3010 bnxt_free_one_rx_ring_skbs(bp, i);
3013 static void bnxt_free_skbs(struct bnxt *bp)
3015 bnxt_free_tx_skbs(bp);
3016 bnxt_free_rx_skbs(bp);
3019 static void bnxt_init_ctx_mem(struct bnxt_mem_init *mem_init, void *p, int len)
3021 u8 init_val = mem_init->init_val;
3022 u16 offset = mem_init->offset;
3028 if (offset == BNXT_MEM_INVALID_OFFSET) {
3029 memset(p, init_val, len);
3032 for (i = 0; i < len; i += mem_init->size)
3033 *(p2 + i + offset) = init_val;
3036 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3038 struct pci_dev *pdev = bp->pdev;
3044 for (i = 0; i < rmem->nr_pages; i++) {
3045 if (!rmem->pg_arr[i])
3048 dma_free_coherent(&pdev->dev, rmem->page_size,
3049 rmem->pg_arr[i], rmem->dma_arr[i]);
3051 rmem->pg_arr[i] = NULL;
3055 size_t pg_tbl_size = rmem->nr_pages * 8;
3057 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3058 pg_tbl_size = rmem->page_size;
3059 dma_free_coherent(&pdev->dev, pg_tbl_size,
3060 rmem->pg_tbl, rmem->pg_tbl_map);
3061 rmem->pg_tbl = NULL;
3063 if (rmem->vmem_size && *rmem->vmem) {
3069 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3071 struct pci_dev *pdev = bp->pdev;
3075 if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3076 valid_bit = PTU_PTE_VALID;
3077 if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3078 size_t pg_tbl_size = rmem->nr_pages * 8;
3080 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3081 pg_tbl_size = rmem->page_size;
3082 rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3089 for (i = 0; i < rmem->nr_pages; i++) {
3090 u64 extra_bits = valid_bit;
3092 rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3096 if (!rmem->pg_arr[i])
3100 bnxt_init_ctx_mem(rmem->mem_init, rmem->pg_arr[i],
3102 if (rmem->nr_pages > 1 || rmem->depth > 0) {
3103 if (i == rmem->nr_pages - 2 &&
3104 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3105 extra_bits |= PTU_PTE_NEXT_TO_LAST;
3106 else if (i == rmem->nr_pages - 1 &&
3107 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3108 extra_bits |= PTU_PTE_LAST;
3110 cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3114 if (rmem->vmem_size) {
3115 *rmem->vmem = vzalloc(rmem->vmem_size);
3122 static void bnxt_free_tpa_info(struct bnxt *bp)
3126 for (i = 0; i < bp->rx_nr_rings; i++) {
3127 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3129 kfree(rxr->rx_tpa_idx_map);
3130 rxr->rx_tpa_idx_map = NULL;
3132 for (j = 0; j < bp->max_tpa; j++) {
3133 kfree(rxr->rx_tpa[j].agg_arr);
3134 rxr->rx_tpa[j].agg_arr = NULL;
3142 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3146 bp->max_tpa = MAX_TPA;
3147 if (bp->flags & BNXT_FLAG_CHIP_P5) {
3148 if (!bp->max_tpa_v2)
3150 bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3153 for (i = 0; i < bp->rx_nr_rings; i++) {
3154 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3155 struct rx_agg_cmp *agg;
3157 rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3162 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
3164 for (j = 0; j < bp->max_tpa; j++) {
3165 agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3168 rxr->rx_tpa[j].agg_arr = agg;
3170 rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3172 if (!rxr->rx_tpa_idx_map)
3178 static void bnxt_free_rx_rings(struct bnxt *bp)
3185 bnxt_free_tpa_info(bp);
3186 for (i = 0; i < bp->rx_nr_rings; i++) {
3187 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3188 struct bnxt_ring_struct *ring;
3191 bpf_prog_put(rxr->xdp_prog);
3193 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3194 xdp_rxq_info_unreg(&rxr->xdp_rxq);
3196 page_pool_destroy(rxr->page_pool);
3197 rxr->page_pool = NULL;
3199 kfree(rxr->rx_agg_bmap);
3200 rxr->rx_agg_bmap = NULL;
3202 ring = &rxr->rx_ring_struct;
3203 bnxt_free_ring(bp, &ring->ring_mem);
3205 ring = &rxr->rx_agg_ring_struct;
3206 bnxt_free_ring(bp, &ring->ring_mem);
3210 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3211 struct bnxt_rx_ring_info *rxr)
3213 struct page_pool_params pp = { 0 };
3215 pp.pool_size = bp->rx_ring_size;
3216 pp.nid = dev_to_node(&bp->pdev->dev);
3217 pp.napi = &rxr->bnapi->napi;
3218 pp.dev = &bp->pdev->dev;
3219 pp.dma_dir = DMA_BIDIRECTIONAL;
3221 rxr->page_pool = page_pool_create(&pp);
3222 if (IS_ERR(rxr->page_pool)) {
3223 int err = PTR_ERR(rxr->page_pool);
3225 rxr->page_pool = NULL;
3231 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3233 int i, rc = 0, agg_rings = 0;
3238 if (bp->flags & BNXT_FLAG_AGG_RINGS)
3241 for (i = 0; i < bp->rx_nr_rings; i++) {
3242 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3243 struct bnxt_ring_struct *ring;
3245 ring = &rxr->rx_ring_struct;
3247 rc = bnxt_alloc_rx_page_pool(bp, rxr);
3251 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3255 rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3259 xdp_rxq_info_unreg(&rxr->xdp_rxq);
3263 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3271 ring = &rxr->rx_agg_ring_struct;
3272 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3277 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3278 mem_size = rxr->rx_agg_bmap_size / 8;
3279 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3280 if (!rxr->rx_agg_bmap)
3284 if (bp->flags & BNXT_FLAG_TPA)
3285 rc = bnxt_alloc_tpa_info(bp);
3289 static void bnxt_free_tx_rings(struct bnxt *bp)
3292 struct pci_dev *pdev = bp->pdev;
3297 for (i = 0; i < bp->tx_nr_rings; i++) {
3298 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3299 struct bnxt_ring_struct *ring;
3302 dma_free_coherent(&pdev->dev, bp->tx_push_size,
3303 txr->tx_push, txr->tx_push_mapping);
3304 txr->tx_push = NULL;
3307 ring = &txr->tx_ring_struct;
3309 bnxt_free_ring(bp, &ring->ring_mem);
3313 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3316 struct pci_dev *pdev = bp->pdev;
3318 bp->tx_push_size = 0;
3319 if (bp->tx_push_thresh) {
3322 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3323 bp->tx_push_thresh);
3325 if (push_size > 256) {
3327 bp->tx_push_thresh = 0;
3330 bp->tx_push_size = push_size;
3333 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3334 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3335 struct bnxt_ring_struct *ring;
3338 ring = &txr->tx_ring_struct;
3340 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3344 ring->grp_idx = txr->bnapi->index;
3345 if (bp->tx_push_size) {
3348 /* One pre-allocated DMA buffer to backup
3351 txr->tx_push = dma_alloc_coherent(&pdev->dev,
3353 &txr->tx_push_mapping,
3359 mapping = txr->tx_push_mapping +
3360 sizeof(struct tx_push_bd);
3361 txr->data_mapping = cpu_to_le64(mapping);
3363 qidx = bp->tc_to_qidx[j];
3364 ring->queue_id = bp->q_info[qidx].queue_id;
3365 spin_lock_init(&txr->xdp_tx_lock);
3366 if (i < bp->tx_nr_rings_xdp)
3368 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
3374 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3376 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3378 kfree(cpr->cp_desc_ring);
3379 cpr->cp_desc_ring = NULL;
3380 ring->ring_mem.pg_arr = NULL;
3381 kfree(cpr->cp_desc_mapping);
3382 cpr->cp_desc_mapping = NULL;
3383 ring->ring_mem.dma_arr = NULL;
3386 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3388 cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3389 if (!cpr->cp_desc_ring)
3391 cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3393 if (!cpr->cp_desc_mapping)
3398 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3404 for (i = 0; i < bp->cp_nr_rings; i++) {
3405 struct bnxt_napi *bnapi = bp->bnapi[i];
3409 bnxt_free_cp_arrays(&bnapi->cp_ring);
3413 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3415 int i, n = bp->cp_nr_pages;
3417 for (i = 0; i < bp->cp_nr_rings; i++) {
3418 struct bnxt_napi *bnapi = bp->bnapi[i];
3423 rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3430 static void bnxt_free_cp_rings(struct bnxt *bp)
3437 for (i = 0; i < bp->cp_nr_rings; i++) {
3438 struct bnxt_napi *bnapi = bp->bnapi[i];
3439 struct bnxt_cp_ring_info *cpr;
3440 struct bnxt_ring_struct *ring;
3446 cpr = &bnapi->cp_ring;
3447 ring = &cpr->cp_ring_struct;
3449 bnxt_free_ring(bp, &ring->ring_mem);
3451 for (j = 0; j < 2; j++) {
3452 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3455 ring = &cpr2->cp_ring_struct;
3456 bnxt_free_ring(bp, &ring->ring_mem);
3457 bnxt_free_cp_arrays(cpr2);
3459 cpr->cp_ring_arr[j] = NULL;
3465 static struct bnxt_cp_ring_info *bnxt_alloc_cp_sub_ring(struct bnxt *bp)
3467 struct bnxt_ring_mem_info *rmem;
3468 struct bnxt_ring_struct *ring;
3469 struct bnxt_cp_ring_info *cpr;
3472 cpr = kzalloc(sizeof(*cpr), GFP_KERNEL);
3476 rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
3478 bnxt_free_cp_arrays(cpr);
3482 ring = &cpr->cp_ring_struct;
3483 rmem = &ring->ring_mem;
3484 rmem->nr_pages = bp->cp_nr_pages;
3485 rmem->page_size = HW_CMPD_RING_SIZE;
3486 rmem->pg_arr = (void **)cpr->cp_desc_ring;
3487 rmem->dma_arr = cpr->cp_desc_mapping;
3488 rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
3489 rc = bnxt_alloc_ring(bp, rmem);
3491 bnxt_free_ring(bp, rmem);
3492 bnxt_free_cp_arrays(cpr);
3499 static int bnxt_alloc_cp_rings(struct bnxt *bp)
3501 bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
3502 int i, rc, ulp_base_vec, ulp_msix;
3504 ulp_msix = bnxt_get_ulp_msix_num(bp);
3505 ulp_base_vec = bnxt_get_ulp_msix_base(bp);
3506 for (i = 0; i < bp->cp_nr_rings; i++) {
3507 struct bnxt_napi *bnapi = bp->bnapi[i];
3508 struct bnxt_cp_ring_info *cpr;
3509 struct bnxt_ring_struct *ring;
3514 cpr = &bnapi->cp_ring;
3516 ring = &cpr->cp_ring_struct;
3518 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3522 if (ulp_msix && i >= ulp_base_vec)
3523 ring->map_idx = i + ulp_msix;
3527 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
3530 if (i < bp->rx_nr_rings) {
3531 struct bnxt_cp_ring_info *cpr2 =
3532 bnxt_alloc_cp_sub_ring(bp);
3534 cpr->cp_ring_arr[BNXT_RX_HDL] = cpr2;
3537 cpr2->bnapi = bnapi;
3539 if ((sh && i < bp->tx_nr_rings) ||
3540 (!sh && i >= bp->rx_nr_rings)) {
3541 struct bnxt_cp_ring_info *cpr2 =
3542 bnxt_alloc_cp_sub_ring(bp);
3544 cpr->cp_ring_arr[BNXT_TX_HDL] = cpr2;
3547 cpr2->bnapi = bnapi;
3553 static void bnxt_init_ring_struct(struct bnxt *bp)
3557 for (i = 0; i < bp->cp_nr_rings; i++) {
3558 struct bnxt_napi *bnapi = bp->bnapi[i];
3559 struct bnxt_ring_mem_info *rmem;
3560 struct bnxt_cp_ring_info *cpr;
3561 struct bnxt_rx_ring_info *rxr;
3562 struct bnxt_tx_ring_info *txr;
3563 struct bnxt_ring_struct *ring;
3568 cpr = &bnapi->cp_ring;
3569 ring = &cpr->cp_ring_struct;
3570 rmem = &ring->ring_mem;
3571 rmem->nr_pages = bp->cp_nr_pages;
3572 rmem->page_size = HW_CMPD_RING_SIZE;
3573 rmem->pg_arr = (void **)cpr->cp_desc_ring;
3574 rmem->dma_arr = cpr->cp_desc_mapping;
3575 rmem->vmem_size = 0;
3577 rxr = bnapi->rx_ring;
3581 ring = &rxr->rx_ring_struct;
3582 rmem = &ring->ring_mem;
3583 rmem->nr_pages = bp->rx_nr_pages;
3584 rmem->page_size = HW_RXBD_RING_SIZE;
3585 rmem->pg_arr = (void **)rxr->rx_desc_ring;
3586 rmem->dma_arr = rxr->rx_desc_mapping;
3587 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
3588 rmem->vmem = (void **)&rxr->rx_buf_ring;
3590 ring = &rxr->rx_agg_ring_struct;
3591 rmem = &ring->ring_mem;
3592 rmem->nr_pages = bp->rx_agg_nr_pages;
3593 rmem->page_size = HW_RXBD_RING_SIZE;
3594 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
3595 rmem->dma_arr = rxr->rx_agg_desc_mapping;
3596 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
3597 rmem->vmem = (void **)&rxr->rx_agg_ring;
3600 txr = bnapi->tx_ring;
3604 ring = &txr->tx_ring_struct;
3605 rmem = &ring->ring_mem;
3606 rmem->nr_pages = bp->tx_nr_pages;
3607 rmem->page_size = HW_RXBD_RING_SIZE;
3608 rmem->pg_arr = (void **)txr->tx_desc_ring;
3609 rmem->dma_arr = txr->tx_desc_mapping;
3610 rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
3611 rmem->vmem = (void **)&txr->tx_buf_ring;
3615 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
3619 struct rx_bd **rx_buf_ring;
3621 rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
3622 for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
3626 rxbd = rx_buf_ring[i];
3630 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
3631 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
3632 rxbd->rx_bd_opaque = prod;
3637 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
3639 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
3640 struct net_device *dev = bp->dev;
3644 prod = rxr->rx_prod;
3645 for (i = 0; i < bp->rx_ring_size; i++) {
3646 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
3647 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
3648 ring_nr, i, bp->rx_ring_size);
3651 prod = NEXT_RX(prod);
3653 rxr->rx_prod = prod;
3655 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
3658 prod = rxr->rx_agg_prod;
3659 for (i = 0; i < bp->rx_agg_ring_size; i++) {
3660 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
3661 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
3662 ring_nr, i, bp->rx_ring_size);
3665 prod = NEXT_RX_AGG(prod);
3667 rxr->rx_agg_prod = prod;
3673 for (i = 0; i < bp->max_tpa; i++) {
3674 data = __bnxt_alloc_rx_frag(bp, &mapping, GFP_KERNEL);
3678 rxr->rx_tpa[i].data = data;
3679 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
3680 rxr->rx_tpa[i].mapping = mapping;
3686 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
3688 struct bnxt_rx_ring_info *rxr;
3689 struct bnxt_ring_struct *ring;
3692 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
3693 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
3695 if (NET_IP_ALIGN == 2)
3696 type |= RX_BD_FLAGS_SOP;
3698 rxr = &bp->rx_ring[ring_nr];
3699 ring = &rxr->rx_ring_struct;
3700 bnxt_init_rxbd_pages(ring, type);
3702 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
3703 bpf_prog_add(bp->xdp_prog, 1);
3704 rxr->xdp_prog = bp->xdp_prog;
3706 ring->fw_ring_id = INVALID_HW_RING_ID;
3708 ring = &rxr->rx_agg_ring_struct;
3709 ring->fw_ring_id = INVALID_HW_RING_ID;
3711 if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
3712 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
3713 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
3715 bnxt_init_rxbd_pages(ring, type);
3718 return bnxt_alloc_one_rx_ring(bp, ring_nr);
3721 static void bnxt_init_cp_rings(struct bnxt *bp)
3725 for (i = 0; i < bp->cp_nr_rings; i++) {
3726 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
3727 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3729 ring->fw_ring_id = INVALID_HW_RING_ID;
3730 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3731 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3732 for (j = 0; j < 2; j++) {
3733 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3738 ring = &cpr2->cp_ring_struct;
3739 ring->fw_ring_id = INVALID_HW_RING_ID;
3740 cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3741 cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3746 static int bnxt_init_rx_rings(struct bnxt *bp)
3750 if (BNXT_RX_PAGE_MODE(bp)) {
3751 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
3752 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
3754 bp->rx_offset = BNXT_RX_OFFSET;
3755 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
3758 for (i = 0; i < bp->rx_nr_rings; i++) {
3759 rc = bnxt_init_one_rx_ring(bp, i);
3767 static int bnxt_init_tx_rings(struct bnxt *bp)
3771 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
3772 BNXT_MIN_TX_DESC_CNT);
3774 for (i = 0; i < bp->tx_nr_rings; i++) {
3775 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3776 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
3778 ring->fw_ring_id = INVALID_HW_RING_ID;
3784 static void bnxt_free_ring_grps(struct bnxt *bp)
3786 kfree(bp->grp_info);
3787 bp->grp_info = NULL;
3790 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
3795 bp->grp_info = kcalloc(bp->cp_nr_rings,
3796 sizeof(struct bnxt_ring_grp_info),
3801 for (i = 0; i < bp->cp_nr_rings; i++) {
3803 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
3804 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3805 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
3806 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
3807 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
3812 static void bnxt_free_vnics(struct bnxt *bp)
3814 kfree(bp->vnic_info);
3815 bp->vnic_info = NULL;
3819 static int bnxt_alloc_vnics(struct bnxt *bp)
3823 #ifdef CONFIG_RFS_ACCEL
3824 if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
3825 num_vnics += bp->rx_nr_rings;
3828 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3831 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
3836 bp->nr_vnics = num_vnics;
3840 static void bnxt_init_vnics(struct bnxt *bp)
3844 for (i = 0; i < bp->nr_vnics; i++) {
3845 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3848 vnic->fw_vnic_id = INVALID_HW_RING_ID;
3849 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
3850 vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
3852 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
3854 if (bp->vnic_info[i].rss_hash_key) {
3856 get_random_bytes(vnic->rss_hash_key,
3859 memcpy(vnic->rss_hash_key,
3860 bp->vnic_info[0].rss_hash_key,
3866 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
3870 pages = ring_size / desc_per_pg;
3877 while (pages & (pages - 1))
3883 void bnxt_set_tpa_flags(struct bnxt *bp)
3885 bp->flags &= ~BNXT_FLAG_TPA;
3886 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
3888 if (bp->dev->features & NETIF_F_LRO)
3889 bp->flags |= BNXT_FLAG_LRO;
3890 else if (bp->dev->features & NETIF_F_GRO_HW)
3891 bp->flags |= BNXT_FLAG_GRO;
3894 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
3897 void bnxt_set_ring_params(struct bnxt *bp)
3899 u32 ring_size, rx_size, rx_space, max_rx_cmpl;
3900 u32 agg_factor = 0, agg_ring_size = 0;
3902 /* 8 for CRC and VLAN */
3903 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
3905 rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
3906 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3908 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
3909 ring_size = bp->rx_ring_size;
3910 bp->rx_agg_ring_size = 0;
3911 bp->rx_agg_nr_pages = 0;
3913 if (bp->flags & BNXT_FLAG_TPA)
3914 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
3916 bp->flags &= ~BNXT_FLAG_JUMBO;
3917 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
3920 bp->flags |= BNXT_FLAG_JUMBO;
3921 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
3922 if (jumbo_factor > agg_factor)
3923 agg_factor = jumbo_factor;
3926 if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
3927 ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
3928 netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
3929 bp->rx_ring_size, ring_size);
3930 bp->rx_ring_size = ring_size;
3932 agg_ring_size = ring_size * agg_factor;
3934 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
3936 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
3937 u32 tmp = agg_ring_size;
3939 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
3940 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
3941 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
3942 tmp, agg_ring_size);
3944 bp->rx_agg_ring_size = agg_ring_size;
3945 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
3947 if (BNXT_RX_PAGE_MODE(bp)) {
3948 rx_space = PAGE_SIZE;
3949 rx_size = PAGE_SIZE -
3950 ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
3951 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3953 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
3954 rx_space = rx_size + NET_SKB_PAD +
3955 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3959 bp->rx_buf_use_size = rx_size;
3960 bp->rx_buf_size = rx_space;
3962 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
3963 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
3965 ring_size = bp->tx_ring_size;
3966 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
3967 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
3969 max_rx_cmpl = bp->rx_ring_size;
3970 /* MAX TPA needs to be added because TPA_START completions are
3971 * immediately recycled, so the TPA completions are not bound by
3974 if (bp->flags & BNXT_FLAG_TPA)
3975 max_rx_cmpl += bp->max_tpa;
3976 /* RX and TPA completions are 32-byte, all others are 16-byte */
3977 ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
3978 bp->cp_ring_size = ring_size;
3980 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
3981 if (bp->cp_nr_pages > MAX_CP_PAGES) {
3982 bp->cp_nr_pages = MAX_CP_PAGES;
3983 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
3984 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
3985 ring_size, bp->cp_ring_size);
3987 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
3988 bp->cp_ring_mask = bp->cp_bit - 1;
3991 /* Changing allocation mode of RX rings.
3992 * TODO: Update when extending xdp_rxq_info to support allocation modes.
3994 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
3997 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
3998 bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4000 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4001 bp->flags |= BNXT_FLAG_JUMBO;
4002 bp->rx_skb_func = bnxt_rx_multi_page_skb;
4004 min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4006 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4007 bp->rx_skb_func = bnxt_rx_page_skb;
4009 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4011 bp->rx_dir = DMA_BIDIRECTIONAL;
4012 /* Disable LRO or GRO_HW */
4013 netdev_update_features(bp->dev);
4015 bp->dev->max_mtu = bp->max_mtu;
4016 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4017 bp->rx_dir = DMA_FROM_DEVICE;
4018 bp->rx_skb_func = bnxt_rx_skb;
4023 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4026 struct bnxt_vnic_info *vnic;
4027 struct pci_dev *pdev = bp->pdev;
4032 for (i = 0; i < bp->nr_vnics; i++) {
4033 vnic = &bp->vnic_info[i];
4035 kfree(vnic->fw_grp_ids);
4036 vnic->fw_grp_ids = NULL;
4038 kfree(vnic->uc_list);
4039 vnic->uc_list = NULL;
4041 if (vnic->mc_list) {
4042 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4043 vnic->mc_list, vnic->mc_list_mapping);
4044 vnic->mc_list = NULL;
4047 if (vnic->rss_table) {
4048 dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4050 vnic->rss_table_dma_addr);
4051 vnic->rss_table = NULL;
4054 vnic->rss_hash_key = NULL;
4059 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4061 int i, rc = 0, size;
4062 struct bnxt_vnic_info *vnic;
4063 struct pci_dev *pdev = bp->pdev;
4066 for (i = 0; i < bp->nr_vnics; i++) {
4067 vnic = &bp->vnic_info[i];
4069 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4070 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4073 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4074 if (!vnic->uc_list) {
4081 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4082 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4084 dma_alloc_coherent(&pdev->dev,
4086 &vnic->mc_list_mapping,
4088 if (!vnic->mc_list) {
4094 if (bp->flags & BNXT_FLAG_CHIP_P5)
4095 goto vnic_skip_grps;
4097 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4098 max_rings = bp->rx_nr_rings;
4102 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4103 if (!vnic->fw_grp_ids) {
4108 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
4109 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4112 /* Allocate rss table and hash key */
4113 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4114 if (bp->flags & BNXT_FLAG_CHIP_P5)
4115 size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4117 vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4118 vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4119 vnic->rss_table_size,
4120 &vnic->rss_table_dma_addr,
4122 if (!vnic->rss_table) {
4127 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4128 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4136 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4138 struct bnxt_hwrm_wait_token *token;
4140 dma_pool_destroy(bp->hwrm_dma_pool);
4141 bp->hwrm_dma_pool = NULL;
4144 hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4145 WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4149 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4151 bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4153 BNXT_HWRM_DMA_ALIGN, 0);
4154 if (!bp->hwrm_dma_pool)
4157 INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4162 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4164 kfree(stats->hw_masks);
4165 stats->hw_masks = NULL;
4166 kfree(stats->sw_stats);
4167 stats->sw_stats = NULL;
4168 if (stats->hw_stats) {
4169 dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4170 stats->hw_stats_map);
4171 stats->hw_stats = NULL;
4175 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4178 stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4179 &stats->hw_stats_map, GFP_KERNEL);
4180 if (!stats->hw_stats)
4183 stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4184 if (!stats->sw_stats)
4188 stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4189 if (!stats->hw_masks)
4195 bnxt_free_stats_mem(bp, stats);
4199 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4203 for (i = 0; i < count; i++)
4207 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4211 for (i = 0; i < count; i++)
4212 mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4215 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4216 struct bnxt_stats_mem *stats)
4218 struct hwrm_func_qstats_ext_output *resp;
4219 struct hwrm_func_qstats_ext_input *req;
4223 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4224 !(bp->flags & BNXT_FLAG_CHIP_P5))
4227 rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4231 req->fid = cpu_to_le16(0xffff);
4232 req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4234 resp = hwrm_req_hold(bp, req);
4235 rc = hwrm_req_send(bp, req);
4237 hw_masks = &resp->rx_ucast_pkts;
4238 bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4240 hwrm_req_drop(bp, req);
4244 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4245 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4247 static void bnxt_init_stats(struct bnxt *bp)
4249 struct bnxt_napi *bnapi = bp->bnapi[0];
4250 struct bnxt_cp_ring_info *cpr;
4251 struct bnxt_stats_mem *stats;
4252 __le64 *rx_stats, *tx_stats;
4253 int rc, rx_count, tx_count;
4254 u64 *rx_masks, *tx_masks;
4258 cpr = &bnapi->cp_ring;
4259 stats = &cpr->stats;
4260 rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4262 if (bp->flags & BNXT_FLAG_CHIP_P5)
4263 mask = (1ULL << 48) - 1;
4266 bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
4268 if (bp->flags & BNXT_FLAG_PORT_STATS) {
4269 stats = &bp->port_stats;
4270 rx_stats = stats->hw_stats;
4271 rx_masks = stats->hw_masks;
4272 rx_count = sizeof(struct rx_port_stats) / 8;
4273 tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4274 tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
4275 tx_count = sizeof(struct tx_port_stats) / 8;
4277 flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
4278 rc = bnxt_hwrm_port_qstats(bp, flags);
4280 mask = (1ULL << 40) - 1;
4282 bnxt_fill_masks(rx_masks, mask, rx_count);
4283 bnxt_fill_masks(tx_masks, mask, tx_count);
4285 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4286 bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
4287 bnxt_hwrm_port_qstats(bp, 0);
4290 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
4291 stats = &bp->rx_port_stats_ext;
4292 rx_stats = stats->hw_stats;
4293 rx_masks = stats->hw_masks;
4294 rx_count = sizeof(struct rx_port_stats_ext) / 8;
4295 stats = &bp->tx_port_stats_ext;
4296 tx_stats = stats->hw_stats;
4297 tx_masks = stats->hw_masks;
4298 tx_count = sizeof(struct tx_port_stats_ext) / 8;
4300 flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4301 rc = bnxt_hwrm_port_qstats_ext(bp, flags);
4303 mask = (1ULL << 40) - 1;
4305 bnxt_fill_masks(rx_masks, mask, rx_count);
4307 bnxt_fill_masks(tx_masks, mask, tx_count);
4309 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4311 bnxt_copy_hw_masks(tx_masks, tx_stats,
4313 bnxt_hwrm_port_qstats_ext(bp, 0);
4318 static void bnxt_free_port_stats(struct bnxt *bp)
4320 bp->flags &= ~BNXT_FLAG_PORT_STATS;
4321 bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
4323 bnxt_free_stats_mem(bp, &bp->port_stats);
4324 bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
4325 bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
4328 static void bnxt_free_ring_stats(struct bnxt *bp)
4335 for (i = 0; i < bp->cp_nr_rings; i++) {
4336 struct bnxt_napi *bnapi = bp->bnapi[i];
4337 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4339 bnxt_free_stats_mem(bp, &cpr->stats);
4343 static int bnxt_alloc_stats(struct bnxt *bp)
4348 size = bp->hw_ring_stats_size;
4350 for (i = 0; i < bp->cp_nr_rings; i++) {
4351 struct bnxt_napi *bnapi = bp->bnapi[i];
4352 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4354 cpr->stats.len = size;
4355 rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
4359 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4362 if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
4365 if (bp->port_stats.hw_stats)
4366 goto alloc_ext_stats;
4368 bp->port_stats.len = BNXT_PORT_STATS_SIZE;
4369 rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
4373 bp->flags |= BNXT_FLAG_PORT_STATS;
4376 /* Display extended statistics only if FW supports it */
4377 if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
4378 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
4381 if (bp->rx_port_stats_ext.hw_stats)
4382 goto alloc_tx_ext_stats;
4384 bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
4385 rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
4386 /* Extended stats are optional */
4391 if (bp->tx_port_stats_ext.hw_stats)
4394 if (bp->hwrm_spec_code >= 0x10902 ||
4395 (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
4396 bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
4397 rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
4398 /* Extended stats are optional */
4402 bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
4406 static void bnxt_clear_ring_indices(struct bnxt *bp)
4413 for (i = 0; i < bp->cp_nr_rings; i++) {
4414 struct bnxt_napi *bnapi = bp->bnapi[i];
4415 struct bnxt_cp_ring_info *cpr;
4416 struct bnxt_rx_ring_info *rxr;
4417 struct bnxt_tx_ring_info *txr;
4422 cpr = &bnapi->cp_ring;
4423 cpr->cp_raw_cons = 0;
4425 txr = bnapi->tx_ring;
4431 rxr = bnapi->rx_ring;
4434 rxr->rx_agg_prod = 0;
4435 rxr->rx_sw_agg_prod = 0;
4436 rxr->rx_next_cons = 0;
4441 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
4443 #ifdef CONFIG_RFS_ACCEL
4446 /* Under rtnl_lock and all our NAPIs have been disabled. It's
4447 * safe to delete the hash table.
4449 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
4450 struct hlist_head *head;
4451 struct hlist_node *tmp;
4452 struct bnxt_ntuple_filter *fltr;
4454 head = &bp->ntp_fltr_hash_tbl[i];
4455 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
4456 hlist_del(&fltr->hash);
4461 bitmap_free(bp->ntp_fltr_bmap);
4462 bp->ntp_fltr_bmap = NULL;
4464 bp->ntp_fltr_count = 0;
4468 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
4470 #ifdef CONFIG_RFS_ACCEL
4473 if (!(bp->flags & BNXT_FLAG_RFS))
4476 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
4477 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
4479 bp->ntp_fltr_count = 0;
4480 bp->ntp_fltr_bmap = bitmap_zalloc(BNXT_NTP_FLTR_MAX_FLTR, GFP_KERNEL);
4482 if (!bp->ntp_fltr_bmap)
4491 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
4493 bnxt_free_vnic_attributes(bp);
4494 bnxt_free_tx_rings(bp);
4495 bnxt_free_rx_rings(bp);
4496 bnxt_free_cp_rings(bp);
4497 bnxt_free_all_cp_arrays(bp);
4498 bnxt_free_ntp_fltrs(bp, irq_re_init);
4500 bnxt_free_ring_stats(bp);
4501 if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
4502 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
4503 bnxt_free_port_stats(bp);
4504 bnxt_free_ring_grps(bp);
4505 bnxt_free_vnics(bp);
4506 kfree(bp->tx_ring_map);
4507 bp->tx_ring_map = NULL;
4515 bnxt_clear_ring_indices(bp);
4519 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
4521 int i, j, rc, size, arr_size;
4525 /* Allocate bnapi mem pointer array and mem block for
4528 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
4530 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
4531 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
4537 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
4538 bp->bnapi[i] = bnapi;
4539 bp->bnapi[i]->index = i;
4540 bp->bnapi[i]->bp = bp;
4541 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4542 struct bnxt_cp_ring_info *cpr =
4543 &bp->bnapi[i]->cp_ring;
4545 cpr->cp_ring_struct.ring_mem.flags =
4546 BNXT_RMEM_RING_PTE_FLAG;
4550 bp->rx_ring = kcalloc(bp->rx_nr_rings,
4551 sizeof(struct bnxt_rx_ring_info),
4556 for (i = 0; i < bp->rx_nr_rings; i++) {
4557 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4559 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4560 rxr->rx_ring_struct.ring_mem.flags =
4561 BNXT_RMEM_RING_PTE_FLAG;
4562 rxr->rx_agg_ring_struct.ring_mem.flags =
4563 BNXT_RMEM_RING_PTE_FLAG;
4565 rxr->bnapi = bp->bnapi[i];
4566 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
4569 bp->tx_ring = kcalloc(bp->tx_nr_rings,
4570 sizeof(struct bnxt_tx_ring_info),
4575 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
4578 if (!bp->tx_ring_map)
4581 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4584 j = bp->rx_nr_rings;
4586 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
4587 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4589 if (bp->flags & BNXT_FLAG_CHIP_P5)
4590 txr->tx_ring_struct.ring_mem.flags =
4591 BNXT_RMEM_RING_PTE_FLAG;
4592 txr->bnapi = bp->bnapi[j];
4593 bp->bnapi[j]->tx_ring = txr;
4594 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
4595 if (i >= bp->tx_nr_rings_xdp) {
4596 txr->txq_index = i - bp->tx_nr_rings_xdp;
4597 bp->bnapi[j]->tx_int = bnxt_tx_int;
4599 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
4600 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
4604 rc = bnxt_alloc_stats(bp);
4607 bnxt_init_stats(bp);
4609 rc = bnxt_alloc_ntp_fltrs(bp);
4613 rc = bnxt_alloc_vnics(bp);
4618 rc = bnxt_alloc_all_cp_arrays(bp);
4622 bnxt_init_ring_struct(bp);
4624 rc = bnxt_alloc_rx_rings(bp);
4628 rc = bnxt_alloc_tx_rings(bp);
4632 rc = bnxt_alloc_cp_rings(bp);
4636 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
4637 BNXT_VNIC_UCAST_FLAG;
4638 rc = bnxt_alloc_vnic_attributes(bp);
4644 bnxt_free_mem(bp, true);
4648 static void bnxt_disable_int(struct bnxt *bp)
4655 for (i = 0; i < bp->cp_nr_rings; i++) {
4656 struct bnxt_napi *bnapi = bp->bnapi[i];
4657 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4658 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4660 if (ring->fw_ring_id != INVALID_HW_RING_ID)
4661 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
4665 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
4667 struct bnxt_napi *bnapi = bp->bnapi[n];
4668 struct bnxt_cp_ring_info *cpr;
4670 cpr = &bnapi->cp_ring;
4671 return cpr->cp_ring_struct.map_idx;
4674 static void bnxt_disable_int_sync(struct bnxt *bp)
4681 atomic_inc(&bp->intr_sem);
4683 bnxt_disable_int(bp);
4684 for (i = 0; i < bp->cp_nr_rings; i++) {
4685 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
4687 synchronize_irq(bp->irq_tbl[map_idx].vector);
4691 static void bnxt_enable_int(struct bnxt *bp)
4695 atomic_set(&bp->intr_sem, 0);
4696 for (i = 0; i < bp->cp_nr_rings; i++) {
4697 struct bnxt_napi *bnapi = bp->bnapi[i];
4698 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4700 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
4704 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
4707 DECLARE_BITMAP(async_events_bmap, 256);
4708 u32 *events = (u32 *)async_events_bmap;
4709 struct hwrm_func_drv_rgtr_output *resp;
4710 struct hwrm_func_drv_rgtr_input *req;
4714 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
4718 req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
4719 FUNC_DRV_RGTR_REQ_ENABLES_VER |
4720 FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4722 req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
4723 flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
4724 if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
4725 flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
4726 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
4727 flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
4728 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
4729 req->flags = cpu_to_le32(flags);
4730 req->ver_maj_8b = DRV_VER_MAJ;
4731 req->ver_min_8b = DRV_VER_MIN;
4732 req->ver_upd_8b = DRV_VER_UPD;
4733 req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
4734 req->ver_min = cpu_to_le16(DRV_VER_MIN);
4735 req->ver_upd = cpu_to_le16(DRV_VER_UPD);
4741 memset(data, 0, sizeof(data));
4742 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
4743 u16 cmd = bnxt_vf_req_snif[i];
4744 unsigned int bit, idx;
4748 data[idx] |= 1 << bit;
4751 for (i = 0; i < 8; i++)
4752 req->vf_req_fwd[i] = cpu_to_le32(data[i]);
4755 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
4758 if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
4759 req->flags |= cpu_to_le32(
4760 FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
4762 memset(async_events_bmap, 0, sizeof(async_events_bmap));
4763 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
4764 u16 event_id = bnxt_async_events_arr[i];
4766 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
4767 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
4769 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
4772 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
4774 if (bmap && bmap_size) {
4775 for (i = 0; i < bmap_size; i++) {
4776 if (test_bit(i, bmap))
4777 __set_bit(i, async_events_bmap);
4780 for (i = 0; i < 8; i++)
4781 req->async_event_fwd[i] |= cpu_to_le32(events[i]);
4785 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4787 resp = hwrm_req_hold(bp, req);
4788 rc = hwrm_req_send(bp, req);
4790 set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
4792 cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
4793 bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
4795 hwrm_req_drop(bp, req);
4799 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
4801 struct hwrm_func_drv_unrgtr_input *req;
4804 if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
4807 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
4810 return hwrm_req_send(bp, req);
4813 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
4815 struct hwrm_tunnel_dst_port_free_input *req;
4818 if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
4819 bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
4821 if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
4822 bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
4825 rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
4829 req->tunnel_type = tunnel_type;
4831 switch (tunnel_type) {
4832 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
4833 req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
4835 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
4837 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
4838 req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
4840 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
4846 rc = hwrm_req_send(bp, req);
4848 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
4853 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
4856 struct hwrm_tunnel_dst_port_alloc_output *resp;
4857 struct hwrm_tunnel_dst_port_alloc_input *req;
4860 rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
4864 req->tunnel_type = tunnel_type;
4865 req->tunnel_dst_port_val = port;
4867 resp = hwrm_req_hold(bp, req);
4868 rc = hwrm_req_send(bp, req);
4870 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
4875 switch (tunnel_type) {
4876 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
4877 bp->vxlan_port = port;
4878 bp->vxlan_fw_dst_port_id =
4879 le16_to_cpu(resp->tunnel_dst_port_id);
4881 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
4882 bp->nge_port = port;
4883 bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
4890 hwrm_req_drop(bp, req);
4894 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
4896 struct hwrm_cfa_l2_set_rx_mask_input *req;
4897 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4900 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
4904 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4905 if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
4906 req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
4907 req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
4909 req->mask = cpu_to_le32(vnic->rx_mask);
4910 return hwrm_req_send_silent(bp, req);
4913 #ifdef CONFIG_RFS_ACCEL
4914 static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
4915 struct bnxt_ntuple_filter *fltr)
4917 struct hwrm_cfa_ntuple_filter_free_input *req;
4920 rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
4924 req->ntuple_filter_id = fltr->filter_id;
4925 return hwrm_req_send(bp, req);
4928 #define BNXT_NTP_FLTR_FLAGS \
4929 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
4930 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
4931 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
4932 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
4933 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
4934 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
4935 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
4936 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
4937 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
4938 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
4939 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
4940 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
4941 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
4942 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
4944 #define BNXT_NTP_TUNNEL_FLTR_FLAG \
4945 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
4947 static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
4948 struct bnxt_ntuple_filter *fltr)
4950 struct hwrm_cfa_ntuple_filter_alloc_output *resp;
4951 struct hwrm_cfa_ntuple_filter_alloc_input *req;
4952 struct flow_keys *keys = &fltr->fkeys;
4953 struct bnxt_vnic_info *vnic;
4957 rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
4961 req->l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
4963 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
4964 flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
4965 req->dst_id = cpu_to_le16(fltr->rxq);
4967 vnic = &bp->vnic_info[fltr->rxq + 1];
4968 req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
4970 req->flags = cpu_to_le32(flags);
4971 req->enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
4973 req->ethertype = htons(ETH_P_IP);
4974 memcpy(req->src_macaddr, fltr->src_mac_addr, ETH_ALEN);
4975 req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
4976 req->ip_protocol = keys->basic.ip_proto;
4978 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
4981 req->ethertype = htons(ETH_P_IPV6);
4983 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
4984 *(struct in6_addr *)&req->src_ipaddr[0] =
4985 keys->addrs.v6addrs.src;
4986 *(struct in6_addr *)&req->dst_ipaddr[0] =
4987 keys->addrs.v6addrs.dst;
4988 for (i = 0; i < 4; i++) {
4989 req->src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4990 req->dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4993 req->src_ipaddr[0] = keys->addrs.v4addrs.src;
4994 req->src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4995 req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
4996 req->dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4998 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
4999 req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
5001 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
5004 req->src_port = keys->ports.src;
5005 req->src_port_mask = cpu_to_be16(0xffff);
5006 req->dst_port = keys->ports.dst;
5007 req->dst_port_mask = cpu_to_be16(0xffff);
5009 resp = hwrm_req_hold(bp, req);
5010 rc = hwrm_req_send(bp, req);
5012 fltr->filter_id = resp->ntuple_filter_id;
5013 hwrm_req_drop(bp, req);
5018 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
5021 struct hwrm_cfa_l2_filter_alloc_output *resp;
5022 struct hwrm_cfa_l2_filter_alloc_input *req;
5025 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
5029 req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
5030 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
5032 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
5033 req->dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
5035 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
5036 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
5037 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
5038 memcpy(req->l2_addr, mac_addr, ETH_ALEN);
5039 req->l2_addr_mask[0] = 0xff;
5040 req->l2_addr_mask[1] = 0xff;
5041 req->l2_addr_mask[2] = 0xff;
5042 req->l2_addr_mask[3] = 0xff;
5043 req->l2_addr_mask[4] = 0xff;
5044 req->l2_addr_mask[5] = 0xff;
5046 resp = hwrm_req_hold(bp, req);
5047 rc = hwrm_req_send(bp, req);
5049 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
5051 hwrm_req_drop(bp, req);
5055 static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
5057 struct hwrm_cfa_l2_filter_free_input *req;
5058 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
5061 /* Any associated ntuple filters will also be cleared by firmware. */
5062 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
5065 hwrm_req_hold(bp, req);
5066 for (i = 0; i < num_of_vnics; i++) {
5067 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
5069 for (j = 0; j < vnic->uc_filter_count; j++) {
5070 req->l2_filter_id = vnic->fw_l2_filter_id[j];
5072 rc = hwrm_req_send(bp, req);
5074 vnic->uc_filter_count = 0;
5076 hwrm_req_drop(bp, req);
5080 static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
5082 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5083 u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
5084 struct hwrm_vnic_tpa_cfg_input *req;
5087 if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
5090 rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
5095 u16 mss = bp->dev->mtu - 40;
5096 u32 nsegs, n, segs = 0, flags;
5098 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
5099 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
5100 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
5101 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
5102 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
5103 if (tpa_flags & BNXT_FLAG_GRO)
5104 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
5106 req->flags = cpu_to_le32(flags);
5109 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
5110 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
5111 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
5113 /* Number of segs are log2 units, and first packet is not
5114 * included as part of this units.
5116 if (mss <= BNXT_RX_PAGE_SIZE) {
5117 n = BNXT_RX_PAGE_SIZE / mss;
5118 nsegs = (MAX_SKB_FRAGS - 1) * n;
5120 n = mss / BNXT_RX_PAGE_SIZE;
5121 if (mss & (BNXT_RX_PAGE_SIZE - 1))
5123 nsegs = (MAX_SKB_FRAGS - n) / n;
5126 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5127 segs = MAX_TPA_SEGS_P5;
5128 max_aggs = bp->max_tpa;
5130 segs = ilog2(nsegs);
5132 req->max_agg_segs = cpu_to_le16(segs);
5133 req->max_aggs = cpu_to_le16(max_aggs);
5135 req->min_agg_len = cpu_to_le32(512);
5137 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5139 return hwrm_req_send(bp, req);
5142 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
5144 struct bnxt_ring_grp_info *grp_info;
5146 grp_info = &bp->grp_info[ring->grp_idx];
5147 return grp_info->cp_fw_ring_id;
5150 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
5152 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5153 struct bnxt_napi *bnapi = rxr->bnapi;
5154 struct bnxt_cp_ring_info *cpr;
5156 cpr = bnapi->cp_ring.cp_ring_arr[BNXT_RX_HDL];
5157 return cpr->cp_ring_struct.fw_ring_id;
5159 return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
5163 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
5165 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5166 struct bnxt_napi *bnapi = txr->bnapi;
5167 struct bnxt_cp_ring_info *cpr;
5169 cpr = bnapi->cp_ring.cp_ring_arr[BNXT_TX_HDL];
5170 return cpr->cp_ring_struct.fw_ring_id;
5172 return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
5176 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
5180 if (bp->flags & BNXT_FLAG_CHIP_P5)
5181 entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
5183 entries = HW_HASH_INDEX_SIZE;
5185 bp->rss_indir_tbl_entries = entries;
5186 bp->rss_indir_tbl = kmalloc_array(entries, sizeof(*bp->rss_indir_tbl),
5188 if (!bp->rss_indir_tbl)
5193 static void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp)
5195 u16 max_rings, max_entries, pad, i;
5197 if (!bp->rx_nr_rings)
5200 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5201 max_rings = bp->rx_nr_rings - 1;
5203 max_rings = bp->rx_nr_rings;
5205 max_entries = bnxt_get_rxfh_indir_size(bp->dev);
5207 for (i = 0; i < max_entries; i++)
5208 bp->rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
5210 pad = bp->rss_indir_tbl_entries - max_entries;
5212 memset(&bp->rss_indir_tbl[i], 0, pad * sizeof(u16));
5215 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
5217 u16 i, tbl_size, max_ring = 0;
5219 if (!bp->rss_indir_tbl)
5222 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
5223 for (i = 0; i < tbl_size; i++)
5224 max_ring = max(max_ring, bp->rss_indir_tbl[i]);
5228 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
5230 if (bp->flags & BNXT_FLAG_CHIP_P5)
5231 return DIV_ROUND_UP(rx_rings, BNXT_RSS_TABLE_ENTRIES_P5);
5232 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5237 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
5239 bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
5242 /* Fill the RSS indirection table with ring group ids */
5243 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
5245 j = bp->rss_indir_tbl[i];
5246 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
5250 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
5251 struct bnxt_vnic_info *vnic)
5253 __le16 *ring_tbl = vnic->rss_table;
5254 struct bnxt_rx_ring_info *rxr;
5257 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
5259 for (i = 0; i < tbl_size; i++) {
5262 j = bp->rss_indir_tbl[i];
5263 rxr = &bp->rx_ring[j];
5265 ring_id = rxr->rx_ring_struct.fw_ring_id;
5266 *ring_tbl++ = cpu_to_le16(ring_id);
5267 ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5268 *ring_tbl++ = cpu_to_le16(ring_id);
5273 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
5274 struct bnxt_vnic_info *vnic)
5276 if (bp->flags & BNXT_FLAG_CHIP_P5)
5277 bnxt_fill_hw_rss_tbl_p5(bp, vnic);
5279 bnxt_fill_hw_rss_tbl(bp, vnic);
5281 if (bp->rss_hash_delta) {
5282 req->hash_type = cpu_to_le32(bp->rss_hash_delta);
5283 if (bp->rss_hash_cfg & bp->rss_hash_delta)
5284 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
5286 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
5288 req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
5290 req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
5291 req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
5292 req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
5295 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
5297 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5298 struct hwrm_vnic_rss_cfg_input *req;
5301 if ((bp->flags & BNXT_FLAG_CHIP_P5) ||
5302 vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
5305 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
5310 __bnxt_hwrm_vnic_set_rss(bp, req, vnic);
5311 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5312 return hwrm_req_send(bp, req);
5315 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, u16 vnic_id, bool set_rss)
5317 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5318 struct hwrm_vnic_rss_cfg_input *req;
5319 dma_addr_t ring_tbl_map;
5323 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
5327 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5329 return hwrm_req_send(bp, req);
5331 __bnxt_hwrm_vnic_set_rss(bp, req, vnic);
5332 ring_tbl_map = vnic->rss_table_dma_addr;
5333 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
5335 hwrm_req_hold(bp, req);
5336 for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
5337 req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
5338 req->ring_table_pair_index = i;
5339 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
5340 rc = hwrm_req_send(bp, req);
5346 hwrm_req_drop(bp, req);
5350 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
5352 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5353 struct hwrm_vnic_rss_qcfg_output *resp;
5354 struct hwrm_vnic_rss_qcfg_input *req;
5356 if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
5359 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5360 /* all contexts configured to same hash_type, zero always exists */
5361 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5362 resp = hwrm_req_hold(bp, req);
5363 if (!hwrm_req_send(bp, req)) {
5364 bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
5365 bp->rss_hash_delta = 0;
5367 hwrm_req_drop(bp, req);
5370 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
5372 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5373 struct hwrm_vnic_plcmodes_cfg_input *req;
5376 rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
5380 req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
5381 req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
5383 if (BNXT_RX_PAGE_MODE(bp)) {
5384 req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
5386 req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
5387 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
5389 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
5390 req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
5391 req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
5393 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5394 return hwrm_req_send(bp, req);
5397 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
5400 struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
5402 if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
5405 req->rss_cos_lb_ctx_id =
5406 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
5408 hwrm_req_send(bp, req);
5409 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
5412 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
5416 for (i = 0; i < bp->nr_vnics; i++) {
5417 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
5419 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
5420 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
5421 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
5424 bp->rsscos_nr_ctxs = 0;
5427 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
5429 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
5430 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
5433 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
5437 resp = hwrm_req_hold(bp, req);
5438 rc = hwrm_req_send(bp, req);
5440 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
5441 le16_to_cpu(resp->rss_cos_lb_ctx_id);
5442 hwrm_req_drop(bp, req);
5447 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
5449 if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
5450 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
5451 return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
5454 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
5456 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5457 struct hwrm_vnic_cfg_input *req;
5458 unsigned int ring = 0, grp_idx;
5462 rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
5466 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5467 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
5469 req->default_rx_ring_id =
5470 cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
5471 req->default_cmpl_ring_id =
5472 cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
5474 cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
5475 VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
5478 req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
5479 /* Only RSS support for now TBD: COS & LB */
5480 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
5481 req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5482 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5483 VNIC_CFG_REQ_ENABLES_MRU);
5484 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
5486 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
5487 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5488 VNIC_CFG_REQ_ENABLES_MRU);
5489 req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
5491 req->rss_rule = cpu_to_le16(0xffff);
5494 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
5495 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
5496 req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
5497 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
5499 req->cos_rule = cpu_to_le16(0xffff);
5502 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
5504 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
5506 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
5507 ring = bp->rx_nr_rings - 1;
5509 grp_idx = bp->rx_ring[ring].bnapi->index;
5510 req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
5511 req->lb_rule = cpu_to_le16(0xffff);
5513 req->mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + VLAN_HLEN);
5515 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5516 #ifdef CONFIG_BNXT_SRIOV
5518 def_vlan = bp->vf.vlan;
5520 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
5521 req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
5522 if (!vnic_id && bnxt_ulp_registered(bp->edev))
5523 req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
5525 return hwrm_req_send(bp, req);
5528 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
5530 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
5531 struct hwrm_vnic_free_input *req;
5533 if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
5537 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
5539 hwrm_req_send(bp, req);
5540 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
5544 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
5548 for (i = 0; i < bp->nr_vnics; i++)
5549 bnxt_hwrm_vnic_free_one(bp, i);
5552 static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
5553 unsigned int start_rx_ring_idx,
5554 unsigned int nr_rings)
5556 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
5557 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5558 struct hwrm_vnic_alloc_output *resp;
5559 struct hwrm_vnic_alloc_input *req;
5562 rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
5566 if (bp->flags & BNXT_FLAG_CHIP_P5)
5567 goto vnic_no_ring_grps;
5569 /* map ring groups to this vnic */
5570 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
5571 grp_idx = bp->rx_ring[i].bnapi->index;
5572 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
5573 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
5577 vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
5581 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
5582 vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
5584 req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
5586 resp = hwrm_req_hold(bp, req);
5587 rc = hwrm_req_send(bp, req);
5589 vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
5590 hwrm_req_drop(bp, req);
5594 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
5596 struct hwrm_vnic_qcaps_output *resp;
5597 struct hwrm_vnic_qcaps_input *req;
5600 bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
5601 bp->flags &= ~(BNXT_FLAG_NEW_RSS_CAP | BNXT_FLAG_ROCE_MIRROR_CAP);
5602 if (bp->hwrm_spec_code < 0x10600)
5605 rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
5609 resp = hwrm_req_hold(bp, req);
5610 rc = hwrm_req_send(bp, req);
5612 u32 flags = le32_to_cpu(resp->flags);
5614 if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
5615 (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
5616 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
5618 VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
5619 bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
5621 /* Older P5 fw before EXT_HW_STATS support did not set
5622 * VLAN_STRIP_CAP properly.
5624 if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
5625 (BNXT_CHIP_P5_THOR(bp) &&
5626 !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
5627 bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
5628 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
5629 bp->fw_cap |= BNXT_FW_CAP_RSS_HASH_TYPE_DELTA;
5630 bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
5631 if (bp->max_tpa_v2) {
5632 if (BNXT_CHIP_P5_THOR(bp))
5633 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
5635 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5_SR2;
5638 hwrm_req_drop(bp, req);
5642 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
5644 struct hwrm_ring_grp_alloc_output *resp;
5645 struct hwrm_ring_grp_alloc_input *req;
5649 if (bp->flags & BNXT_FLAG_CHIP_P5)
5652 rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
5656 resp = hwrm_req_hold(bp, req);
5657 for (i = 0; i < bp->rx_nr_rings; i++) {
5658 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
5660 req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
5661 req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
5662 req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
5663 req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
5665 rc = hwrm_req_send(bp, req);
5670 bp->grp_info[grp_idx].fw_grp_id =
5671 le32_to_cpu(resp->ring_group_id);
5673 hwrm_req_drop(bp, req);
5677 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
5679 struct hwrm_ring_grp_free_input *req;
5682 if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5))
5685 if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
5688 hwrm_req_hold(bp, req);
5689 for (i = 0; i < bp->cp_nr_rings; i++) {
5690 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
5692 req->ring_group_id =
5693 cpu_to_le32(bp->grp_info[i].fw_grp_id);
5695 hwrm_req_send(bp, req);
5696 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
5698 hwrm_req_drop(bp, req);
5701 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
5702 struct bnxt_ring_struct *ring,
5703 u32 ring_type, u32 map_index)
5705 struct hwrm_ring_alloc_output *resp;
5706 struct hwrm_ring_alloc_input *req;
5707 struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
5708 struct bnxt_ring_grp_info *grp_info;
5712 rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
5717 if (rmem->nr_pages > 1) {
5718 req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
5719 /* Page size is in log2 units */
5720 req->page_size = BNXT_PAGE_SHIFT;
5721 req->page_tbl_depth = 1;
5723 req->page_tbl_addr = cpu_to_le64(rmem->dma_arr[0]);
5726 /* Association of ring index with doorbell index and MSIX number */
5727 req->logical_id = cpu_to_le16(map_index);
5729 switch (ring_type) {
5730 case HWRM_RING_ALLOC_TX: {
5731 struct bnxt_tx_ring_info *txr;
5733 txr = container_of(ring, struct bnxt_tx_ring_info,
5735 req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
5736 /* Association of transmit ring with completion ring */
5737 grp_info = &bp->grp_info[ring->grp_idx];
5738 req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
5739 req->length = cpu_to_le32(bp->tx_ring_mask + 1);
5740 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5741 req->queue_id = cpu_to_le16(ring->queue_id);
5744 case HWRM_RING_ALLOC_RX:
5745 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5746 req->length = cpu_to_le32(bp->rx_ring_mask + 1);
5747 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5750 /* Association of rx ring with stats context */
5751 grp_info = &bp->grp_info[ring->grp_idx];
5752 req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
5753 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5754 req->enables |= cpu_to_le32(
5755 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5756 if (NET_IP_ALIGN == 2)
5757 flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
5758 req->flags = cpu_to_le16(flags);
5761 case HWRM_RING_ALLOC_AGG:
5762 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5763 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
5764 /* Association of agg ring with rx ring */
5765 grp_info = &bp->grp_info[ring->grp_idx];
5766 req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
5767 req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
5768 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5769 req->enables |= cpu_to_le32(
5770 RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
5771 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5773 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5775 req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
5777 case HWRM_RING_ALLOC_CMPL:
5778 req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
5779 req->length = cpu_to_le32(bp->cp_ring_mask + 1);
5780 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5781 /* Association of cp ring with nq */
5782 grp_info = &bp->grp_info[map_index];
5783 req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
5784 req->cq_handle = cpu_to_le64(ring->handle);
5785 req->enables |= cpu_to_le32(
5786 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
5787 } else if (bp->flags & BNXT_FLAG_USING_MSIX) {
5788 req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5791 case HWRM_RING_ALLOC_NQ:
5792 req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
5793 req->length = cpu_to_le32(bp->cp_ring_mask + 1);
5794 if (bp->flags & BNXT_FLAG_USING_MSIX)
5795 req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5798 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
5803 resp = hwrm_req_hold(bp, req);
5804 rc = hwrm_req_send(bp, req);
5805 err = le16_to_cpu(resp->error_code);
5806 ring_id = le16_to_cpu(resp->ring_id);
5807 hwrm_req_drop(bp, req);
5811 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
5812 ring_type, rc, err);
5815 ring->fw_ring_id = ring_id;
5819 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
5824 struct hwrm_func_cfg_input *req;
5826 rc = hwrm_req_init(bp, req, HWRM_FUNC_CFG);
5830 req->fid = cpu_to_le16(0xffff);
5831 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5832 req->async_event_cr = cpu_to_le16(idx);
5833 return hwrm_req_send(bp, req);
5835 struct hwrm_func_vf_cfg_input *req;
5837 rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
5842 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5843 req->async_event_cr = cpu_to_le16(idx);
5844 return hwrm_req_send(bp, req);
5848 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
5849 u32 map_idx, u32 xid)
5851 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5853 db->doorbell = bp->bar1 + DB_PF_OFFSET_P5;
5855 db->doorbell = bp->bar1 + DB_VF_OFFSET_P5;
5856 switch (ring_type) {
5857 case HWRM_RING_ALLOC_TX:
5858 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
5860 case HWRM_RING_ALLOC_RX:
5861 case HWRM_RING_ALLOC_AGG:
5862 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
5864 case HWRM_RING_ALLOC_CMPL:
5865 db->db_key64 = DBR_PATH_L2;
5867 case HWRM_RING_ALLOC_NQ:
5868 db->db_key64 = DBR_PATH_L2;
5871 db->db_key64 |= (u64)xid << DBR_XID_SFT;
5873 db->doorbell = bp->bar1 + map_idx * 0x80;
5874 switch (ring_type) {
5875 case HWRM_RING_ALLOC_TX:
5876 db->db_key32 = DB_KEY_TX;
5878 case HWRM_RING_ALLOC_RX:
5879 case HWRM_RING_ALLOC_AGG:
5880 db->db_key32 = DB_KEY_RX;
5882 case HWRM_RING_ALLOC_CMPL:
5883 db->db_key32 = DB_KEY_CP;
5889 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
5891 bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
5895 if (bp->flags & BNXT_FLAG_CHIP_P5)
5896 type = HWRM_RING_ALLOC_NQ;
5898 type = HWRM_RING_ALLOC_CMPL;
5899 for (i = 0; i < bp->cp_nr_rings; i++) {
5900 struct bnxt_napi *bnapi = bp->bnapi[i];
5901 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5902 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5903 u32 map_idx = ring->map_idx;
5904 unsigned int vector;
5906 vector = bp->irq_tbl[map_idx].vector;
5907 disable_irq_nosync(vector);
5908 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5913 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
5914 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5916 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
5919 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
5921 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
5925 type = HWRM_RING_ALLOC_TX;
5926 for (i = 0; i < bp->tx_nr_rings; i++) {
5927 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5928 struct bnxt_ring_struct *ring;
5931 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5932 struct bnxt_napi *bnapi = txr->bnapi;
5933 struct bnxt_cp_ring_info *cpr, *cpr2;
5934 u32 type2 = HWRM_RING_ALLOC_CMPL;
5936 cpr = &bnapi->cp_ring;
5937 cpr2 = cpr->cp_ring_arr[BNXT_TX_HDL];
5938 ring = &cpr2->cp_ring_struct;
5939 ring->handle = BNXT_TX_HDL;
5940 map_idx = bnapi->index;
5941 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5944 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5946 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5948 ring = &txr->tx_ring_struct;
5950 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5953 bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
5956 type = HWRM_RING_ALLOC_RX;
5957 for (i = 0; i < bp->rx_nr_rings; i++) {
5958 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5959 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5960 struct bnxt_napi *bnapi = rxr->bnapi;
5961 u32 map_idx = bnapi->index;
5963 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5966 bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
5967 /* If we have agg rings, post agg buffers first. */
5969 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
5970 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
5971 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5972 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5973 u32 type2 = HWRM_RING_ALLOC_CMPL;
5974 struct bnxt_cp_ring_info *cpr2;
5976 cpr2 = cpr->cp_ring_arr[BNXT_RX_HDL];
5977 ring = &cpr2->cp_ring_struct;
5978 ring->handle = BNXT_RX_HDL;
5979 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5982 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5984 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5989 type = HWRM_RING_ALLOC_AGG;
5990 for (i = 0; i < bp->rx_nr_rings; i++) {
5991 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5992 struct bnxt_ring_struct *ring =
5993 &rxr->rx_agg_ring_struct;
5994 u32 grp_idx = ring->grp_idx;
5995 u32 map_idx = grp_idx + bp->rx_nr_rings;
5997 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
6001 bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
6003 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
6004 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
6005 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
6012 static int hwrm_ring_free_send_msg(struct bnxt *bp,
6013 struct bnxt_ring_struct *ring,
6014 u32 ring_type, int cmpl_ring_id)
6016 struct hwrm_ring_free_output *resp;
6017 struct hwrm_ring_free_input *req;
6021 if (BNXT_NO_FW_ACCESS(bp))
6024 rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
6028 req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
6029 req->ring_type = ring_type;
6030 req->ring_id = cpu_to_le16(ring->fw_ring_id);
6032 resp = hwrm_req_hold(bp, req);
6033 rc = hwrm_req_send(bp, req);
6034 error_code = le16_to_cpu(resp->error_code);
6035 hwrm_req_drop(bp, req);
6037 if (rc || error_code) {
6038 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
6039 ring_type, rc, error_code);
6045 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
6053 for (i = 0; i < bp->tx_nr_rings; i++) {
6054 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
6055 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
6057 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6058 u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
6060 hwrm_ring_free_send_msg(bp, ring,
6061 RING_FREE_REQ_RING_TYPE_TX,
6062 close_path ? cmpl_ring_id :
6063 INVALID_HW_RING_ID);
6064 ring->fw_ring_id = INVALID_HW_RING_ID;
6068 for (i = 0; i < bp->rx_nr_rings; i++) {
6069 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6070 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
6071 u32 grp_idx = rxr->bnapi->index;
6073 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6074 u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6076 hwrm_ring_free_send_msg(bp, ring,
6077 RING_FREE_REQ_RING_TYPE_RX,
6078 close_path ? cmpl_ring_id :
6079 INVALID_HW_RING_ID);
6080 ring->fw_ring_id = INVALID_HW_RING_ID;
6081 bp->grp_info[grp_idx].rx_fw_ring_id =
6086 if (bp->flags & BNXT_FLAG_CHIP_P5)
6087 type = RING_FREE_REQ_RING_TYPE_RX_AGG;
6089 type = RING_FREE_REQ_RING_TYPE_RX;
6090 for (i = 0; i < bp->rx_nr_rings; i++) {
6091 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
6092 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
6093 u32 grp_idx = rxr->bnapi->index;
6095 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6096 u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6098 hwrm_ring_free_send_msg(bp, ring, type,
6099 close_path ? cmpl_ring_id :
6100 INVALID_HW_RING_ID);
6101 ring->fw_ring_id = INVALID_HW_RING_ID;
6102 bp->grp_info[grp_idx].agg_fw_ring_id =
6107 /* The completion rings are about to be freed. After that the
6108 * IRQ doorbell will not work anymore. So we need to disable
6111 bnxt_disable_int_sync(bp);
6113 if (bp->flags & BNXT_FLAG_CHIP_P5)
6114 type = RING_FREE_REQ_RING_TYPE_NQ;
6116 type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
6117 for (i = 0; i < bp->cp_nr_rings; i++) {
6118 struct bnxt_napi *bnapi = bp->bnapi[i];
6119 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6120 struct bnxt_ring_struct *ring;
6123 for (j = 0; j < 2; j++) {
6124 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
6127 ring = &cpr2->cp_ring_struct;
6128 if (ring->fw_ring_id == INVALID_HW_RING_ID)
6130 hwrm_ring_free_send_msg(bp, ring,
6131 RING_FREE_REQ_RING_TYPE_L2_CMPL,
6132 INVALID_HW_RING_ID);
6133 ring->fw_ring_id = INVALID_HW_RING_ID;
6136 ring = &cpr->cp_ring_struct;
6137 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
6138 hwrm_ring_free_send_msg(bp, ring, type,
6139 INVALID_HW_RING_ID);
6140 ring->fw_ring_id = INVALID_HW_RING_ID;
6141 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
6146 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
6149 static int bnxt_hwrm_get_rings(struct bnxt *bp)
6151 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6152 struct hwrm_func_qcfg_output *resp;
6153 struct hwrm_func_qcfg_input *req;
6156 if (bp->hwrm_spec_code < 0x10601)
6159 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
6163 req->fid = cpu_to_le16(0xffff);
6164 resp = hwrm_req_hold(bp, req);
6165 rc = hwrm_req_send(bp, req);
6167 hwrm_req_drop(bp, req);
6171 hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
6172 if (BNXT_NEW_RM(bp)) {
6175 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
6176 hw_resc->resv_hw_ring_grps =
6177 le32_to_cpu(resp->alloc_hw_ring_grps);
6178 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
6179 cp = le16_to_cpu(resp->alloc_cmpl_rings);
6180 stats = le16_to_cpu(resp->alloc_stat_ctx);
6181 hw_resc->resv_irqs = cp;
6182 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6183 int rx = hw_resc->resv_rx_rings;
6184 int tx = hw_resc->resv_tx_rings;
6186 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6188 if (cp < (rx + tx)) {
6189 bnxt_trim_rings(bp, &rx, &tx, cp, false);
6190 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6192 hw_resc->resv_rx_rings = rx;
6193 hw_resc->resv_tx_rings = tx;
6195 hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
6196 hw_resc->resv_hw_ring_grps = rx;
6198 hw_resc->resv_cp_rings = cp;
6199 hw_resc->resv_stat_ctxs = stats;
6201 hwrm_req_drop(bp, req);
6205 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
6207 struct hwrm_func_qcfg_output *resp;
6208 struct hwrm_func_qcfg_input *req;
6211 if (bp->hwrm_spec_code < 0x10601)
6214 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
6218 req->fid = cpu_to_le16(fid);
6219 resp = hwrm_req_hold(bp, req);
6220 rc = hwrm_req_send(bp, req);
6222 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
6224 hwrm_req_drop(bp, req);
6228 static bool bnxt_rfs_supported(struct bnxt *bp);
6230 static struct hwrm_func_cfg_input *
6231 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6232 int ring_grps, int cp_rings, int stats, int vnics)
6234 struct hwrm_func_cfg_input *req;
6237 if (hwrm_req_init(bp, req, HWRM_FUNC_CFG))
6240 req->fid = cpu_to_le16(0xffff);
6241 enables |= tx_rings ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
6242 req->num_tx_rings = cpu_to_le16(tx_rings);
6243 if (BNXT_NEW_RM(bp)) {
6244 enables |= rx_rings ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
6245 enables |= stats ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
6246 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6247 enables |= cp_rings ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
6248 enables |= tx_rings + ring_grps ?
6249 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6250 enables |= rx_rings ?
6251 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6253 enables |= cp_rings ?
6254 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6255 enables |= ring_grps ?
6256 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS |
6257 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6259 enables |= vnics ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
6261 req->num_rx_rings = cpu_to_le16(rx_rings);
6262 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6263 req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
6264 req->num_msix = cpu_to_le16(cp_rings);
6265 req->num_rsscos_ctxs =
6266 cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
6268 req->num_cmpl_rings = cpu_to_le16(cp_rings);
6269 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
6270 req->num_rsscos_ctxs = cpu_to_le16(1);
6271 if (!(bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
6272 bnxt_rfs_supported(bp))
6273 req->num_rsscos_ctxs =
6274 cpu_to_le16(ring_grps + 1);
6276 req->num_stat_ctxs = cpu_to_le16(stats);
6277 req->num_vnics = cpu_to_le16(vnics);
6279 req->enables = cpu_to_le32(enables);
6283 static struct hwrm_func_vf_cfg_input *
6284 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6285 int ring_grps, int cp_rings, int stats, int vnics)
6287 struct hwrm_func_vf_cfg_input *req;
6290 if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
6293 enables |= tx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
6294 enables |= rx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
6295 FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6296 enables |= stats ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
6297 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6298 enables |= tx_rings + ring_grps ?
6299 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6301 enables |= cp_rings ?
6302 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6303 enables |= ring_grps ?
6304 FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
6306 enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
6307 enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
6309 req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
6310 req->num_tx_rings = cpu_to_le16(tx_rings);
6311 req->num_rx_rings = cpu_to_le16(rx_rings);
6312 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6313 req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
6314 req->num_rsscos_ctxs = cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
6316 req->num_cmpl_rings = cpu_to_le16(cp_rings);
6317 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
6318 req->num_rsscos_ctxs = cpu_to_le16(BNXT_VF_MAX_RSS_CTX);
6320 req->num_stat_ctxs = cpu_to_le16(stats);
6321 req->num_vnics = cpu_to_le16(vnics);
6323 req->enables = cpu_to_le32(enables);
6328 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6329 int ring_grps, int cp_rings, int stats, int vnics)
6331 struct hwrm_func_cfg_input *req;
6334 req = __bnxt_hwrm_reserve_pf_rings(bp, tx_rings, rx_rings, ring_grps,
6335 cp_rings, stats, vnics);
6339 if (!req->enables) {
6340 hwrm_req_drop(bp, req);
6344 rc = hwrm_req_send(bp, req);
6348 if (bp->hwrm_spec_code < 0x10601)
6349 bp->hw_resc.resv_tx_rings = tx_rings;
6351 return bnxt_hwrm_get_rings(bp);
6355 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6356 int ring_grps, int cp_rings, int stats, int vnics)
6358 struct hwrm_func_vf_cfg_input *req;
6361 if (!BNXT_NEW_RM(bp)) {
6362 bp->hw_resc.resv_tx_rings = tx_rings;
6366 req = __bnxt_hwrm_reserve_vf_rings(bp, tx_rings, rx_rings, ring_grps,
6367 cp_rings, stats, vnics);
6371 rc = hwrm_req_send(bp, req);
6375 return bnxt_hwrm_get_rings(bp);
6378 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, int tx, int rx, int grp,
6379 int cp, int stat, int vnic)
6382 return bnxt_hwrm_reserve_pf_rings(bp, tx, rx, grp, cp, stat,
6385 return bnxt_hwrm_reserve_vf_rings(bp, tx, rx, grp, cp, stat,
6389 int bnxt_nq_rings_in_use(struct bnxt *bp)
6391 int cp = bp->cp_nr_rings;
6392 int ulp_msix, ulp_base;
6394 ulp_msix = bnxt_get_ulp_msix_num(bp);
6396 ulp_base = bnxt_get_ulp_msix_base(bp);
6398 if ((ulp_base + ulp_msix) > cp)
6399 cp = ulp_base + ulp_msix;
6404 static int bnxt_cp_rings_in_use(struct bnxt *bp)
6408 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6409 return bnxt_nq_rings_in_use(bp);
6411 cp = bp->tx_nr_rings + bp->rx_nr_rings;
6415 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
6417 int ulp_stat = bnxt_get_ulp_stat_ctxs(bp);
6418 int cp = bp->cp_nr_rings;
6423 if (bnxt_nq_rings_in_use(bp) > cp + bnxt_get_ulp_msix_num(bp))
6424 return bnxt_get_ulp_msix_base(bp) + ulp_stat;
6426 return cp + ulp_stat;
6429 /* Check if a default RSS map needs to be setup. This function is only
6430 * used on older firmware that does not require reserving RX rings.
6432 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
6434 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6436 /* The RSS map is valid for RX rings set to resv_rx_rings */
6437 if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
6438 hw_resc->resv_rx_rings = bp->rx_nr_rings;
6439 if (!netif_is_rxfh_configured(bp->dev))
6440 bnxt_set_dflt_rss_indir_tbl(bp);
6444 static bool bnxt_need_reserve_rings(struct bnxt *bp)
6446 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6447 int cp = bnxt_cp_rings_in_use(bp);
6448 int nq = bnxt_nq_rings_in_use(bp);
6449 int rx = bp->rx_nr_rings, stat;
6450 int vnic = 1, grp = rx;
6452 if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
6453 bp->hwrm_spec_code >= 0x10601)
6456 /* Old firmware does not need RX ring reservations but we still
6457 * need to setup a default RSS map when needed. With new firmware
6458 * we go through RX ring reservations first and then set up the
6459 * RSS map for the successfully reserved RX rings when needed.
6461 if (!BNXT_NEW_RM(bp)) {
6462 bnxt_check_rss_tbl_no_rmgr(bp);
6465 if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
6467 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6469 stat = bnxt_get_func_stat_ctxs(bp);
6470 if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
6471 hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
6472 (hw_resc->resv_hw_ring_grps != grp &&
6473 !(bp->flags & BNXT_FLAG_CHIP_P5)))
6475 if ((bp->flags & BNXT_FLAG_CHIP_P5) && BNXT_PF(bp) &&
6476 hw_resc->resv_irqs != nq)
6481 static int __bnxt_reserve_rings(struct bnxt *bp)
6483 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6484 int cp = bnxt_nq_rings_in_use(bp);
6485 int tx = bp->tx_nr_rings;
6486 int rx = bp->rx_nr_rings;
6487 int grp, rx_rings, rc;
6491 if (!bnxt_need_reserve_rings(bp))
6494 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6496 if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
6498 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6500 grp = bp->rx_nr_rings;
6501 stat = bnxt_get_func_stat_ctxs(bp);
6503 rc = bnxt_hwrm_reserve_rings(bp, tx, rx, grp, cp, stat, vnic);
6507 tx = hw_resc->resv_tx_rings;
6508 if (BNXT_NEW_RM(bp)) {
6509 rx = hw_resc->resv_rx_rings;
6510 cp = hw_resc->resv_irqs;
6511 grp = hw_resc->resv_hw_ring_grps;
6512 vnic = hw_resc->resv_vnics;
6513 stat = hw_resc->resv_stat_ctxs;
6517 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
6521 if (netif_running(bp->dev))
6524 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
6525 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
6526 bp->dev->hw_features &= ~NETIF_F_LRO;
6527 bp->dev->features &= ~NETIF_F_LRO;
6528 bnxt_set_ring_params(bp);
6531 rx_rings = min_t(int, rx_rings, grp);
6532 cp = min_t(int, cp, bp->cp_nr_rings);
6533 if (stat > bnxt_get_ulp_stat_ctxs(bp))
6534 stat -= bnxt_get_ulp_stat_ctxs(bp);
6535 cp = min_t(int, cp, stat);
6536 rc = bnxt_trim_rings(bp, &rx_rings, &tx, cp, sh);
6537 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6539 cp = sh ? max_t(int, tx, rx_rings) : tx + rx_rings;
6540 bp->tx_nr_rings = tx;
6542 /* If we cannot reserve all the RX rings, reset the RSS map only
6543 * if absolutely necessary
6545 if (rx_rings != bp->rx_nr_rings) {
6546 netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
6547 rx_rings, bp->rx_nr_rings);
6548 if (netif_is_rxfh_configured(bp->dev) &&
6549 (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
6550 bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
6551 bnxt_get_max_rss_ring(bp) >= rx_rings)) {
6552 netdev_warn(bp->dev, "RSS table entries reverting to default\n");
6553 bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
6556 bp->rx_nr_rings = rx_rings;
6557 bp->cp_nr_rings = cp;
6559 if (!tx || !rx || !cp || !grp || !vnic || !stat)
6562 if (!netif_is_rxfh_configured(bp->dev))
6563 bnxt_set_dflt_rss_indir_tbl(bp);
6568 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6569 int ring_grps, int cp_rings, int stats,
6572 struct hwrm_func_vf_cfg_input *req;
6575 if (!BNXT_NEW_RM(bp))
6578 req = __bnxt_hwrm_reserve_vf_rings(bp, tx_rings, rx_rings, ring_grps,
6579 cp_rings, stats, vnics);
6580 flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
6581 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6582 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6583 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6584 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
6585 FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
6586 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6587 flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6589 req->flags = cpu_to_le32(flags);
6590 return hwrm_req_send_silent(bp, req);
6593 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6594 int ring_grps, int cp_rings, int stats,
6597 struct hwrm_func_cfg_input *req;
6600 req = __bnxt_hwrm_reserve_pf_rings(bp, tx_rings, rx_rings, ring_grps,
6601 cp_rings, stats, vnics);
6602 flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
6603 if (BNXT_NEW_RM(bp)) {
6604 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6605 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6606 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6607 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
6608 if (bp->flags & BNXT_FLAG_CHIP_P5)
6609 flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
6610 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
6612 flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6615 req->flags = cpu_to_le32(flags);
6616 return hwrm_req_send_silent(bp, req);
6619 static int bnxt_hwrm_check_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6620 int ring_grps, int cp_rings, int stats,
6623 if (bp->hwrm_spec_code < 0x10801)
6627 return bnxt_hwrm_check_pf_rings(bp, tx_rings, rx_rings,
6628 ring_grps, cp_rings, stats,
6631 return bnxt_hwrm_check_vf_rings(bp, tx_rings, rx_rings, ring_grps,
6632 cp_rings, stats, vnics);
6635 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
6637 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6638 struct hwrm_ring_aggint_qcaps_output *resp;
6639 struct hwrm_ring_aggint_qcaps_input *req;
6642 coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
6643 coal_cap->num_cmpl_dma_aggr_max = 63;
6644 coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
6645 coal_cap->cmpl_aggr_dma_tmr_max = 65535;
6646 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
6647 coal_cap->int_lat_tmr_min_max = 65535;
6648 coal_cap->int_lat_tmr_max_max = 65535;
6649 coal_cap->num_cmpl_aggr_int_max = 65535;
6650 coal_cap->timer_units = 80;
6652 if (bp->hwrm_spec_code < 0x10902)
6655 if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
6658 resp = hwrm_req_hold(bp, req);
6659 rc = hwrm_req_send_silent(bp, req);
6661 coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
6662 coal_cap->nq_params = le32_to_cpu(resp->nq_params);
6663 coal_cap->num_cmpl_dma_aggr_max =
6664 le16_to_cpu(resp->num_cmpl_dma_aggr_max);
6665 coal_cap->num_cmpl_dma_aggr_during_int_max =
6666 le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
6667 coal_cap->cmpl_aggr_dma_tmr_max =
6668 le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
6669 coal_cap->cmpl_aggr_dma_tmr_during_int_max =
6670 le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
6671 coal_cap->int_lat_tmr_min_max =
6672 le16_to_cpu(resp->int_lat_tmr_min_max);
6673 coal_cap->int_lat_tmr_max_max =
6674 le16_to_cpu(resp->int_lat_tmr_max_max);
6675 coal_cap->num_cmpl_aggr_int_max =
6676 le16_to_cpu(resp->num_cmpl_aggr_int_max);
6677 coal_cap->timer_units = le16_to_cpu(resp->timer_units);
6679 hwrm_req_drop(bp, req);
6682 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
6684 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6686 return usec * 1000 / coal_cap->timer_units;
6689 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
6690 struct bnxt_coal *hw_coal,
6691 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
6693 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6694 u16 val, tmr, max, flags = hw_coal->flags;
6695 u32 cmpl_params = coal_cap->cmpl_params;
6697 max = hw_coal->bufs_per_record * 128;
6698 if (hw_coal->budget)
6699 max = hw_coal->bufs_per_record * hw_coal->budget;
6700 max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
6702 val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
6703 req->num_cmpl_aggr_int = cpu_to_le16(val);
6705 val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
6706 req->num_cmpl_dma_aggr = cpu_to_le16(val);
6708 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
6709 coal_cap->num_cmpl_dma_aggr_during_int_max);
6710 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
6712 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
6713 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
6714 req->int_lat_tmr_max = cpu_to_le16(tmr);
6716 /* min timer set to 1/2 of interrupt timer */
6717 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
6719 val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
6720 req->int_lat_tmr_min = cpu_to_le16(val);
6721 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6724 /* buf timer set to 1/4 of interrupt timer */
6725 val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
6726 req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
6729 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
6730 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
6731 val = clamp_t(u16, tmr, 1,
6732 coal_cap->cmpl_aggr_dma_tmr_during_int_max);
6733 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
6735 cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
6738 if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
6739 hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
6740 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
6741 req->flags = cpu_to_le16(flags);
6742 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
6745 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
6746 struct bnxt_coal *hw_coal)
6748 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
6749 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6750 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6751 u32 nq_params = coal_cap->nq_params;
6755 if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
6758 rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
6762 req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
6764 cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
6766 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
6767 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
6768 req->int_lat_tmr_min = cpu_to_le16(tmr);
6769 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6770 return hwrm_req_send(bp, req);
6773 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
6775 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
6776 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6777 struct bnxt_coal coal;
6780 /* Tick values in micro seconds.
6781 * 1 coal_buf x bufs_per_record = 1 completion record.
6783 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
6785 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
6786 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
6788 if (!bnapi->rx_ring)
6791 rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
6795 bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
6797 req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
6799 return hwrm_req_send(bp, req_rx);
6802 int bnxt_hwrm_set_coal(struct bnxt *bp)
6804 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx,
6808 rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
6812 rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
6814 hwrm_req_drop(bp, req_rx);
6818 bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
6819 bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
6821 hwrm_req_hold(bp, req_rx);
6822 hwrm_req_hold(bp, req_tx);
6823 for (i = 0; i < bp->cp_nr_rings; i++) {
6824 struct bnxt_napi *bnapi = bp->bnapi[i];
6825 struct bnxt_coal *hw_coal;
6829 if (!bnapi->rx_ring) {
6830 ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6833 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
6835 req->ring_id = cpu_to_le16(ring_id);
6837 rc = hwrm_req_send(bp, req);
6841 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6844 if (bnapi->rx_ring && bnapi->tx_ring) {
6846 ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6847 req->ring_id = cpu_to_le16(ring_id);
6848 rc = hwrm_req_send(bp, req);
6853 hw_coal = &bp->rx_coal;
6855 hw_coal = &bp->tx_coal;
6856 __bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
6858 hwrm_req_drop(bp, req_rx);
6859 hwrm_req_drop(bp, req_tx);
6863 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
6865 struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
6866 struct hwrm_stat_ctx_free_input *req;
6872 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6875 if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
6877 if (BNXT_FW_MAJ(bp) <= 20) {
6878 if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
6879 hwrm_req_drop(bp, req);
6882 hwrm_req_hold(bp, req0);
6884 hwrm_req_hold(bp, req);
6885 for (i = 0; i < bp->cp_nr_rings; i++) {
6886 struct bnxt_napi *bnapi = bp->bnapi[i];
6887 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6889 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
6890 req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
6892 req0->stat_ctx_id = req->stat_ctx_id;
6893 hwrm_req_send(bp, req0);
6895 hwrm_req_send(bp, req);
6897 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
6900 hwrm_req_drop(bp, req);
6902 hwrm_req_drop(bp, req0);
6905 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
6907 struct hwrm_stat_ctx_alloc_output *resp;
6908 struct hwrm_stat_ctx_alloc_input *req;
6911 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6914 rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
6918 req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
6919 req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
6921 resp = hwrm_req_hold(bp, req);
6922 for (i = 0; i < bp->cp_nr_rings; i++) {
6923 struct bnxt_napi *bnapi = bp->bnapi[i];
6924 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6926 req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
6928 rc = hwrm_req_send(bp, req);
6932 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
6934 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
6936 hwrm_req_drop(bp, req);
6940 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
6942 struct hwrm_func_qcfg_output *resp;
6943 struct hwrm_func_qcfg_input *req;
6944 u32 min_db_offset = 0;
6948 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
6952 req->fid = cpu_to_le16(0xffff);
6953 resp = hwrm_req_hold(bp, req);
6954 rc = hwrm_req_send(bp, req);
6956 goto func_qcfg_exit;
6958 #ifdef CONFIG_BNXT_SRIOV
6960 struct bnxt_vf_info *vf = &bp->vf;
6962 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
6964 bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
6967 flags = le16_to_cpu(resp->flags);
6968 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
6969 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
6970 bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
6971 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
6972 bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
6974 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
6975 bp->flags |= BNXT_FLAG_MULTI_HOST;
6977 if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
6978 bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
6980 switch (resp->port_partition_type) {
6981 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
6982 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
6983 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
6984 bp->port_partition_type = resp->port_partition_type;
6987 if (bp->hwrm_spec_code < 0x10707 ||
6988 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
6989 bp->br_mode = BRIDGE_MODE_VEB;
6990 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
6991 bp->br_mode = BRIDGE_MODE_VEPA;
6993 bp->br_mode = BRIDGE_MODE_UNDEF;
6995 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
6997 bp->max_mtu = BNXT_MAX_MTU;
7000 goto func_qcfg_exit;
7002 if (bp->flags & BNXT_FLAG_CHIP_P5) {
7004 min_db_offset = DB_PF_OFFSET_P5;
7006 min_db_offset = DB_VF_OFFSET_P5;
7008 bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
7010 if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
7011 bp->db_size <= min_db_offset)
7012 bp->db_size = pci_resource_len(bp->pdev, 2);
7015 hwrm_req_drop(bp, req);
7019 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_info *ctx,
7020 struct hwrm_func_backing_store_qcaps_output *resp)
7022 struct bnxt_mem_init *mem_init;
7028 init_val = resp->ctx_kind_initializer;
7029 init_mask = le16_to_cpu(resp->ctx_init_mask);
7030 offset = &resp->qp_init_offset;
7031 mem_init = &ctx->mem_init[BNXT_CTX_MEM_INIT_QP];
7032 for (i = 0; i < BNXT_CTX_MEM_INIT_MAX; i++, mem_init++, offset++) {
7033 mem_init->init_val = init_val;
7034 mem_init->offset = BNXT_MEM_INVALID_OFFSET;
7037 if (i == BNXT_CTX_MEM_INIT_STAT)
7038 offset = &resp->stat_init_offset;
7039 if (init_mask & (1 << i))
7040 mem_init->offset = *offset * 4;
7042 mem_init->init_val = 0;
7044 ctx->mem_init[BNXT_CTX_MEM_INIT_QP].size = ctx->qp_entry_size;
7045 ctx->mem_init[BNXT_CTX_MEM_INIT_SRQ].size = ctx->srq_entry_size;
7046 ctx->mem_init[BNXT_CTX_MEM_INIT_CQ].size = ctx->cq_entry_size;
7047 ctx->mem_init[BNXT_CTX_MEM_INIT_VNIC].size = ctx->vnic_entry_size;
7048 ctx->mem_init[BNXT_CTX_MEM_INIT_STAT].size = ctx->stat_entry_size;
7049 ctx->mem_init[BNXT_CTX_MEM_INIT_MRAV].size = ctx->mrav_entry_size;
7052 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
7054 struct hwrm_func_backing_store_qcaps_output *resp;
7055 struct hwrm_func_backing_store_qcaps_input *req;
7058 if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
7061 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
7065 resp = hwrm_req_hold(bp, req);
7066 rc = hwrm_req_send_silent(bp, req);
7068 struct bnxt_ctx_pg_info *ctx_pg;
7069 struct bnxt_ctx_mem_info *ctx;
7072 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
7077 ctx->qp_max_entries = le32_to_cpu(resp->qp_max_entries);
7078 ctx->qp_min_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
7079 ctx->qp_max_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
7080 ctx->qp_entry_size = le16_to_cpu(resp->qp_entry_size);
7081 ctx->srq_max_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
7082 ctx->srq_max_entries = le32_to_cpu(resp->srq_max_entries);
7083 ctx->srq_entry_size = le16_to_cpu(resp->srq_entry_size);
7084 ctx->cq_max_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
7085 ctx->cq_max_entries = le32_to_cpu(resp->cq_max_entries);
7086 ctx->cq_entry_size = le16_to_cpu(resp->cq_entry_size);
7087 ctx->vnic_max_vnic_entries =
7088 le16_to_cpu(resp->vnic_max_vnic_entries);
7089 ctx->vnic_max_ring_table_entries =
7090 le16_to_cpu(resp->vnic_max_ring_table_entries);
7091 ctx->vnic_entry_size = le16_to_cpu(resp->vnic_entry_size);
7092 ctx->stat_max_entries = le32_to_cpu(resp->stat_max_entries);
7093 ctx->stat_entry_size = le16_to_cpu(resp->stat_entry_size);
7094 ctx->tqm_entry_size = le16_to_cpu(resp->tqm_entry_size);
7095 ctx->tqm_min_entries_per_ring =
7096 le32_to_cpu(resp->tqm_min_entries_per_ring);
7097 ctx->tqm_max_entries_per_ring =
7098 le32_to_cpu(resp->tqm_max_entries_per_ring);
7099 ctx->tqm_entries_multiple = resp->tqm_entries_multiple;
7100 if (!ctx->tqm_entries_multiple)
7101 ctx->tqm_entries_multiple = 1;
7102 ctx->mrav_max_entries = le32_to_cpu(resp->mrav_max_entries);
7103 ctx->mrav_entry_size = le16_to_cpu(resp->mrav_entry_size);
7104 ctx->mrav_num_entries_units =
7105 le16_to_cpu(resp->mrav_num_entries_units);
7106 ctx->tim_entry_size = le16_to_cpu(resp->tim_entry_size);
7107 ctx->tim_max_entries = le32_to_cpu(resp->tim_max_entries);
7109 bnxt_init_ctx_initializer(ctx, resp);
7111 ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
7112 if (!ctx->tqm_fp_rings_count)
7113 ctx->tqm_fp_rings_count = bp->max_q;
7114 else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
7115 ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
7117 tqm_rings = ctx->tqm_fp_rings_count + BNXT_MAX_TQM_SP_RINGS;
7118 ctx_pg = kcalloc(tqm_rings, sizeof(*ctx_pg), GFP_KERNEL);
7124 for (i = 0; i < tqm_rings; i++, ctx_pg++)
7125 ctx->tqm_mem[i] = ctx_pg;
7131 hwrm_req_drop(bp, req);
7135 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
7138 if (!rmem->nr_pages)
7141 BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
7142 if (rmem->depth >= 1) {
7143 if (rmem->depth == 2)
7147 *pg_dir = cpu_to_le64(rmem->pg_tbl_map);
7149 *pg_dir = cpu_to_le64(rmem->dma_arr[0]);
7153 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES \
7154 (FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP | \
7155 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ | \
7156 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ | \
7157 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC | \
7158 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
7160 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
7162 struct hwrm_func_backing_store_cfg_input *req;
7163 struct bnxt_ctx_mem_info *ctx = bp->ctx;
7164 struct bnxt_ctx_pg_info *ctx_pg;
7165 void **__req = (void **)&req;
7166 u32 req_len = sizeof(*req);
7167 __le32 *num_entries;
7178 if (req_len > bp->hwrm_max_ext_req_len)
7179 req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
7180 rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
7184 req->enables = cpu_to_le32(enables);
7185 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
7186 ctx_pg = &ctx->qp_mem;
7187 req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
7188 req->qp_num_qp1_entries = cpu_to_le16(ctx->qp_min_qp1_entries);
7189 req->qp_num_l2_entries = cpu_to_le16(ctx->qp_max_l2_entries);
7190 req->qp_entry_size = cpu_to_le16(ctx->qp_entry_size);
7191 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7192 &req->qpc_pg_size_qpc_lvl,
7193 &req->qpc_page_dir);
7195 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
7196 ctx_pg = &ctx->srq_mem;
7197 req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
7198 req->srq_num_l2_entries = cpu_to_le16(ctx->srq_max_l2_entries);
7199 req->srq_entry_size = cpu_to_le16(ctx->srq_entry_size);
7200 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7201 &req->srq_pg_size_srq_lvl,
7202 &req->srq_page_dir);
7204 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
7205 ctx_pg = &ctx->cq_mem;
7206 req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
7207 req->cq_num_l2_entries = cpu_to_le16(ctx->cq_max_l2_entries);
7208 req->cq_entry_size = cpu_to_le16(ctx->cq_entry_size);
7209 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7210 &req->cq_pg_size_cq_lvl,
7213 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
7214 ctx_pg = &ctx->vnic_mem;
7215 req->vnic_num_vnic_entries =
7216 cpu_to_le16(ctx->vnic_max_vnic_entries);
7217 req->vnic_num_ring_table_entries =
7218 cpu_to_le16(ctx->vnic_max_ring_table_entries);
7219 req->vnic_entry_size = cpu_to_le16(ctx->vnic_entry_size);
7220 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7221 &req->vnic_pg_size_vnic_lvl,
7222 &req->vnic_page_dir);
7224 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
7225 ctx_pg = &ctx->stat_mem;
7226 req->stat_num_entries = cpu_to_le32(ctx->stat_max_entries);
7227 req->stat_entry_size = cpu_to_le16(ctx->stat_entry_size);
7228 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7229 &req->stat_pg_size_stat_lvl,
7230 &req->stat_page_dir);
7232 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
7233 ctx_pg = &ctx->mrav_mem;
7234 req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
7235 if (ctx->mrav_num_entries_units)
7237 FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
7238 req->mrav_entry_size = cpu_to_le16(ctx->mrav_entry_size);
7239 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7240 &req->mrav_pg_size_mrav_lvl,
7241 &req->mrav_page_dir);
7243 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
7244 ctx_pg = &ctx->tim_mem;
7245 req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
7246 req->tim_entry_size = cpu_to_le16(ctx->tim_entry_size);
7247 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7248 &req->tim_pg_size_tim_lvl,
7249 &req->tim_page_dir);
7251 for (i = 0, num_entries = &req->tqm_sp_num_entries,
7252 pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
7253 pg_dir = &req->tqm_sp_page_dir,
7254 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP;
7255 i < BNXT_MAX_TQM_RINGS;
7256 i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
7257 if (!(enables & ena))
7260 req->tqm_entry_size = cpu_to_le16(ctx->tqm_entry_size);
7261 ctx_pg = ctx->tqm_mem[i];
7262 *num_entries = cpu_to_le32(ctx_pg->entries);
7263 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
7265 req->flags = cpu_to_le32(flags);
7266 return hwrm_req_send(bp, req);
7269 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
7270 struct bnxt_ctx_pg_info *ctx_pg)
7272 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7274 rmem->page_size = BNXT_PAGE_SIZE;
7275 rmem->pg_arr = ctx_pg->ctx_pg_arr;
7276 rmem->dma_arr = ctx_pg->ctx_dma_arr;
7277 rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
7278 if (rmem->depth >= 1)
7279 rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
7280 return bnxt_alloc_ring(bp, rmem);
7283 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
7284 struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
7285 u8 depth, struct bnxt_mem_init *mem_init)
7287 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7293 ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
7294 if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
7295 ctx_pg->nr_pages = 0;
7298 if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
7302 ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
7304 if (!ctx_pg->ctx_pg_tbl)
7306 nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
7307 rmem->nr_pages = nr_tbls;
7308 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
7311 for (i = 0; i < nr_tbls; i++) {
7312 struct bnxt_ctx_pg_info *pg_tbl;
7314 pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
7317 ctx_pg->ctx_pg_tbl[i] = pg_tbl;
7318 rmem = &pg_tbl->ring_mem;
7319 rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
7320 rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
7322 rmem->nr_pages = MAX_CTX_PAGES;
7323 rmem->mem_init = mem_init;
7324 if (i == (nr_tbls - 1)) {
7325 int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
7328 rmem->nr_pages = rem;
7330 rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
7335 rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
7336 if (rmem->nr_pages > 1 || depth)
7338 rmem->mem_init = mem_init;
7339 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
7344 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
7345 struct bnxt_ctx_pg_info *ctx_pg)
7347 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7349 if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
7350 ctx_pg->ctx_pg_tbl) {
7351 int i, nr_tbls = rmem->nr_pages;
7353 for (i = 0; i < nr_tbls; i++) {
7354 struct bnxt_ctx_pg_info *pg_tbl;
7355 struct bnxt_ring_mem_info *rmem2;
7357 pg_tbl = ctx_pg->ctx_pg_tbl[i];
7360 rmem2 = &pg_tbl->ring_mem;
7361 bnxt_free_ring(bp, rmem2);
7362 ctx_pg->ctx_pg_arr[i] = NULL;
7364 ctx_pg->ctx_pg_tbl[i] = NULL;
7366 kfree(ctx_pg->ctx_pg_tbl);
7367 ctx_pg->ctx_pg_tbl = NULL;
7369 bnxt_free_ring(bp, rmem);
7370 ctx_pg->nr_pages = 0;
7373 void bnxt_free_ctx_mem(struct bnxt *bp)
7375 struct bnxt_ctx_mem_info *ctx = bp->ctx;
7381 if (ctx->tqm_mem[0]) {
7382 for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
7383 bnxt_free_ctx_pg_tbls(bp, ctx->tqm_mem[i]);
7384 kfree(ctx->tqm_mem[0]);
7385 ctx->tqm_mem[0] = NULL;
7388 bnxt_free_ctx_pg_tbls(bp, &ctx->tim_mem);
7389 bnxt_free_ctx_pg_tbls(bp, &ctx->mrav_mem);
7390 bnxt_free_ctx_pg_tbls(bp, &ctx->stat_mem);
7391 bnxt_free_ctx_pg_tbls(bp, &ctx->vnic_mem);
7392 bnxt_free_ctx_pg_tbls(bp, &ctx->cq_mem);
7393 bnxt_free_ctx_pg_tbls(bp, &ctx->srq_mem);
7394 bnxt_free_ctx_pg_tbls(bp, &ctx->qp_mem);
7395 ctx->flags &= ~BNXT_CTX_FLAG_INITED;
7398 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
7400 struct bnxt_ctx_pg_info *ctx_pg;
7401 struct bnxt_ctx_mem_info *ctx;
7402 struct bnxt_mem_init *init;
7403 u32 mem_size, ena, entries;
7404 u32 entries_sp, min;
7411 rc = bnxt_hwrm_func_backing_store_qcaps(bp);
7413 netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
7418 if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
7421 if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
7427 ctx_pg = &ctx->qp_mem;
7428 ctx_pg->entries = ctx->qp_min_qp1_entries + ctx->qp_max_l2_entries +
7430 if (ctx->qp_entry_size) {
7431 mem_size = ctx->qp_entry_size * ctx_pg->entries;
7432 init = &ctx->mem_init[BNXT_CTX_MEM_INIT_QP];
7433 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init);
7438 ctx_pg = &ctx->srq_mem;
7439 ctx_pg->entries = ctx->srq_max_l2_entries + extra_srqs;
7440 if (ctx->srq_entry_size) {
7441 mem_size = ctx->srq_entry_size * ctx_pg->entries;
7442 init = &ctx->mem_init[BNXT_CTX_MEM_INIT_SRQ];
7443 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init);
7448 ctx_pg = &ctx->cq_mem;
7449 ctx_pg->entries = ctx->cq_max_l2_entries + extra_qps * 2;
7450 if (ctx->cq_entry_size) {
7451 mem_size = ctx->cq_entry_size * ctx_pg->entries;
7452 init = &ctx->mem_init[BNXT_CTX_MEM_INIT_CQ];
7453 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init);
7458 ctx_pg = &ctx->vnic_mem;
7459 ctx_pg->entries = ctx->vnic_max_vnic_entries +
7460 ctx->vnic_max_ring_table_entries;
7461 if (ctx->vnic_entry_size) {
7462 mem_size = ctx->vnic_entry_size * ctx_pg->entries;
7463 init = &ctx->mem_init[BNXT_CTX_MEM_INIT_VNIC];
7464 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, init);
7469 ctx_pg = &ctx->stat_mem;
7470 ctx_pg->entries = ctx->stat_max_entries;
7471 if (ctx->stat_entry_size) {
7472 mem_size = ctx->stat_entry_size * ctx_pg->entries;
7473 init = &ctx->mem_init[BNXT_CTX_MEM_INIT_STAT];
7474 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, init);
7480 if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
7483 ctx_pg = &ctx->mrav_mem;
7484 /* 128K extra is needed to accommodate static AH context
7485 * allocation by f/w.
7487 num_mr = 1024 * 256;
7488 num_ah = 1024 * 128;
7489 ctx_pg->entries = num_mr + num_ah;
7490 if (ctx->mrav_entry_size) {
7491 mem_size = ctx->mrav_entry_size * ctx_pg->entries;
7492 init = &ctx->mem_init[BNXT_CTX_MEM_INIT_MRAV];
7493 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 2, init);
7497 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
7498 if (ctx->mrav_num_entries_units)
7500 ((num_mr / ctx->mrav_num_entries_units) << 16) |
7501 (num_ah / ctx->mrav_num_entries_units);
7503 ctx_pg = &ctx->tim_mem;
7504 ctx_pg->entries = ctx->qp_mem.entries;
7505 if (ctx->tim_entry_size) {
7506 mem_size = ctx->tim_entry_size * ctx_pg->entries;
7507 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, NULL);
7511 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
7514 min = ctx->tqm_min_entries_per_ring;
7515 entries_sp = ctx->vnic_max_vnic_entries + ctx->qp_max_l2_entries +
7516 2 * (extra_qps + ctx->qp_min_qp1_entries) + min;
7517 entries_sp = roundup(entries_sp, ctx->tqm_entries_multiple);
7518 entries = ctx->qp_max_l2_entries + 2 * (extra_qps + ctx->qp_min_qp1_entries);
7519 entries = roundup(entries, ctx->tqm_entries_multiple);
7520 entries = clamp_t(u32, entries, min, ctx->tqm_max_entries_per_ring);
7521 for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++) {
7522 ctx_pg = ctx->tqm_mem[i];
7523 ctx_pg->entries = i ? entries : entries_sp;
7524 if (ctx->tqm_entry_size) {
7525 mem_size = ctx->tqm_entry_size * ctx_pg->entries;
7526 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1,
7531 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
7533 ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
7534 rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
7536 netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
7540 ctx->flags |= BNXT_CTX_FLAG_INITED;
7544 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
7546 struct hwrm_func_resource_qcaps_output *resp;
7547 struct hwrm_func_resource_qcaps_input *req;
7548 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7551 rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
7555 req->fid = cpu_to_le16(0xffff);
7556 resp = hwrm_req_hold(bp, req);
7557 rc = hwrm_req_send_silent(bp, req);
7559 goto hwrm_func_resc_qcaps_exit;
7561 hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
7563 goto hwrm_func_resc_qcaps_exit;
7565 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
7566 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
7567 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
7568 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
7569 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
7570 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
7571 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
7572 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
7573 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
7574 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
7575 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
7576 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
7577 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
7578 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
7579 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
7580 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
7582 if (bp->flags & BNXT_FLAG_CHIP_P5) {
7583 u16 max_msix = le16_to_cpu(resp->max_msix);
7585 hw_resc->max_nqs = max_msix;
7586 hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
7590 struct bnxt_pf_info *pf = &bp->pf;
7592 pf->vf_resv_strategy =
7593 le16_to_cpu(resp->vf_reservation_strategy);
7594 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
7595 pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
7597 hwrm_func_resc_qcaps_exit:
7598 hwrm_req_drop(bp, req);
7602 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
7604 struct hwrm_port_mac_ptp_qcfg_output *resp;
7605 struct hwrm_port_mac_ptp_qcfg_input *req;
7606 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
7611 if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_THOR(bp)) {
7616 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
7620 req->port_id = cpu_to_le16(bp->pf.port_id);
7621 resp = hwrm_req_hold(bp, req);
7622 rc = hwrm_req_send(bp, req);
7626 flags = resp->flags;
7627 if (!(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
7632 ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
7640 if (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK) {
7641 ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
7642 ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
7643 } else if (bp->flags & BNXT_FLAG_CHIP_P5) {
7644 ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
7645 ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
7650 phc_cfg = (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
7651 rc = bnxt_ptp_init(bp, phc_cfg);
7653 netdev_warn(bp->dev, "PTP initialization failed.\n");
7655 hwrm_req_drop(bp, req);
7666 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
7668 struct hwrm_func_qcaps_output *resp;
7669 struct hwrm_func_qcaps_input *req;
7670 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7671 u32 flags, flags_ext, flags_ext2;
7674 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
7678 req->fid = cpu_to_le16(0xffff);
7679 resp = hwrm_req_hold(bp, req);
7680 rc = hwrm_req_send(bp, req);
7682 goto hwrm_func_qcaps_exit;
7684 flags = le32_to_cpu(resp->flags);
7685 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
7686 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
7687 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
7688 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
7689 if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
7690 bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
7691 if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
7692 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
7693 if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
7694 bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
7695 if (flags & FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
7696 bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
7697 if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
7698 bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
7699 if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
7700 bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
7701 if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
7702 bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
7704 flags_ext = le32_to_cpu(resp->flags_ext);
7705 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
7706 bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
7707 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
7708 bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
7709 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
7710 bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
7711 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
7712 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
7713 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
7714 bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
7716 flags_ext2 = le32_to_cpu(resp->flags_ext2);
7717 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
7718 bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
7720 bp->tx_push_thresh = 0;
7721 if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
7722 BNXT_FW_MAJ(bp) > 217)
7723 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
7725 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
7726 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
7727 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
7728 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
7729 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
7730 if (!hw_resc->max_hw_ring_grps)
7731 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
7732 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
7733 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
7734 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
7737 struct bnxt_pf_info *pf = &bp->pf;
7739 pf->fw_fid = le16_to_cpu(resp->fid);
7740 pf->port_id = le16_to_cpu(resp->port_id);
7741 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
7742 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
7743 pf->max_vfs = le16_to_cpu(resp->max_vfs);
7744 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
7745 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
7746 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
7747 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
7748 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
7749 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
7750 bp->flags &= ~BNXT_FLAG_WOL_CAP;
7751 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
7752 bp->flags |= BNXT_FLAG_WOL_CAP;
7753 if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
7754 bp->fw_cap |= BNXT_FW_CAP_PTP;
7761 #ifdef CONFIG_BNXT_SRIOV
7762 struct bnxt_vf_info *vf = &bp->vf;
7764 vf->fw_fid = le16_to_cpu(resp->fid);
7765 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
7769 hwrm_func_qcaps_exit:
7770 hwrm_req_drop(bp, req);
7774 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
7776 struct hwrm_dbg_qcaps_output *resp;
7777 struct hwrm_dbg_qcaps_input *req;
7781 if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
7784 rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
7788 req->fid = cpu_to_le16(0xffff);
7789 resp = hwrm_req_hold(bp, req);
7790 rc = hwrm_req_send(bp, req);
7792 goto hwrm_dbg_qcaps_exit;
7794 bp->fw_dbg_cap = le32_to_cpu(resp->flags);
7796 hwrm_dbg_qcaps_exit:
7797 hwrm_req_drop(bp, req);
7800 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
7802 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
7806 rc = __bnxt_hwrm_func_qcaps(bp);
7810 bnxt_hwrm_dbg_qcaps(bp);
7812 rc = bnxt_hwrm_queue_qportcfg(bp);
7814 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
7817 if (bp->hwrm_spec_code >= 0x10803) {
7818 rc = bnxt_alloc_ctx_mem(bp);
7821 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
7823 bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
7828 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
7830 struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
7831 struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
7835 if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
7838 rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
7842 resp = hwrm_req_hold(bp, req);
7843 rc = hwrm_req_send(bp, req);
7845 goto hwrm_cfa_adv_qcaps_exit;
7847 flags = le32_to_cpu(resp->flags);
7849 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
7850 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
7852 hwrm_cfa_adv_qcaps_exit:
7853 hwrm_req_drop(bp, req);
7857 static int __bnxt_alloc_fw_health(struct bnxt *bp)
7862 bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
7866 mutex_init(&bp->fw_health->lock);
7870 static int bnxt_alloc_fw_health(struct bnxt *bp)
7874 if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
7875 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
7878 rc = __bnxt_alloc_fw_health(bp);
7880 bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
7881 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
7888 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
7890 writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
7891 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
7892 BNXT_FW_HEALTH_WIN_MAP_OFF);
7895 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
7897 struct bnxt_fw_health *fw_health = bp->fw_health;
7903 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
7904 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
7905 fw_health->status_reliable = false;
7907 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
7908 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
7909 fw_health->resets_reliable = false;
7912 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
7920 bp->fw_health->status_reliable = false;
7922 __bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
7923 hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
7925 sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
7926 if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
7927 if (!bp->chip_num) {
7928 __bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
7929 bp->chip_num = readl(bp->bar0 +
7930 BNXT_FW_HEALTH_WIN_BASE +
7931 BNXT_GRC_REG_CHIP_NUM);
7933 if (!BNXT_CHIP_P5(bp))
7936 status_loc = BNXT_GRC_REG_STATUS_P5 |
7937 BNXT_FW_HEALTH_REG_TYPE_BAR0;
7939 status_loc = readl(hs + offsetof(struct hcomm_status,
7943 if (__bnxt_alloc_fw_health(bp)) {
7944 netdev_warn(bp->dev, "no memory for firmware status checks\n");
7948 bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
7949 reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
7950 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
7951 __bnxt_map_fw_health_reg(bp, status_loc);
7952 bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
7953 BNXT_FW_HEALTH_WIN_OFF(status_loc);
7956 bp->fw_health->status_reliable = true;
7959 static int bnxt_map_fw_health_regs(struct bnxt *bp)
7961 struct bnxt_fw_health *fw_health = bp->fw_health;
7962 u32 reg_base = 0xffffffff;
7965 bp->fw_health->status_reliable = false;
7966 bp->fw_health->resets_reliable = false;
7967 /* Only pre-map the monitoring GRC registers using window 3 */
7968 for (i = 0; i < 4; i++) {
7969 u32 reg = fw_health->regs[i];
7971 if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
7973 if (reg_base == 0xffffffff)
7974 reg_base = reg & BNXT_GRC_BASE_MASK;
7975 if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
7977 fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
7979 bp->fw_health->status_reliable = true;
7980 bp->fw_health->resets_reliable = true;
7981 if (reg_base == 0xffffffff)
7984 __bnxt_map_fw_health_reg(bp, reg_base);
7988 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
7993 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
7994 bp->fw_health->status_reliable = true;
7995 bp->fw_health->resets_reliable = true;
7997 bnxt_try_map_fw_health_reg(bp);
8001 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
8003 struct bnxt_fw_health *fw_health = bp->fw_health;
8004 struct hwrm_error_recovery_qcfg_output *resp;
8005 struct hwrm_error_recovery_qcfg_input *req;
8008 if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
8011 rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
8015 resp = hwrm_req_hold(bp, req);
8016 rc = hwrm_req_send(bp, req);
8018 goto err_recovery_out;
8019 fw_health->flags = le32_to_cpu(resp->flags);
8020 if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
8021 !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
8023 goto err_recovery_out;
8025 fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
8026 fw_health->master_func_wait_dsecs =
8027 le32_to_cpu(resp->master_func_wait_period);
8028 fw_health->normal_func_wait_dsecs =
8029 le32_to_cpu(resp->normal_func_wait_period);
8030 fw_health->post_reset_wait_dsecs =
8031 le32_to_cpu(resp->master_func_wait_period_after_reset);
8032 fw_health->post_reset_max_wait_dsecs =
8033 le32_to_cpu(resp->max_bailout_time_after_reset);
8034 fw_health->regs[BNXT_FW_HEALTH_REG] =
8035 le32_to_cpu(resp->fw_health_status_reg);
8036 fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
8037 le32_to_cpu(resp->fw_heartbeat_reg);
8038 fw_health->regs[BNXT_FW_RESET_CNT_REG] =
8039 le32_to_cpu(resp->fw_reset_cnt_reg);
8040 fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
8041 le32_to_cpu(resp->reset_inprogress_reg);
8042 fw_health->fw_reset_inprog_reg_mask =
8043 le32_to_cpu(resp->reset_inprogress_reg_mask);
8044 fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
8045 if (fw_health->fw_reset_seq_cnt >= 16) {
8047 goto err_recovery_out;
8049 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
8050 fw_health->fw_reset_seq_regs[i] =
8051 le32_to_cpu(resp->reset_reg[i]);
8052 fw_health->fw_reset_seq_vals[i] =
8053 le32_to_cpu(resp->reset_reg_val[i]);
8054 fw_health->fw_reset_seq_delay_msec[i] =
8055 resp->delay_after_reset[i];
8058 hwrm_req_drop(bp, req);
8060 rc = bnxt_map_fw_health_regs(bp);
8062 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
8066 static int bnxt_hwrm_func_reset(struct bnxt *bp)
8068 struct hwrm_func_reset_input *req;
8071 rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
8076 hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
8077 return hwrm_req_send(bp, req);
8080 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
8082 struct hwrm_nvm_get_dev_info_output nvm_info;
8084 if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
8085 snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
8086 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
8087 nvm_info.nvm_cfg_ver_upd);
8090 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
8092 struct hwrm_queue_qportcfg_output *resp;
8093 struct hwrm_queue_qportcfg_input *req;
8098 rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
8102 resp = hwrm_req_hold(bp, req);
8103 rc = hwrm_req_send(bp, req);
8107 if (!resp->max_configurable_queues) {
8111 bp->max_tc = resp->max_configurable_queues;
8112 bp->max_lltc = resp->max_configurable_lossless_queues;
8113 if (bp->max_tc > BNXT_MAX_QUEUE)
8114 bp->max_tc = BNXT_MAX_QUEUE;
8116 no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
8117 qptr = &resp->queue_id0;
8118 for (i = 0, j = 0; i < bp->max_tc; i++) {
8119 bp->q_info[j].queue_id = *qptr;
8120 bp->q_ids[i] = *qptr++;
8121 bp->q_info[j].queue_profile = *qptr++;
8122 bp->tc_to_qidx[j] = j;
8123 if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
8124 (no_rdma && BNXT_PF(bp)))
8127 bp->max_q = bp->max_tc;
8128 bp->max_tc = max_t(u8, j, 1);
8130 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
8133 if (bp->max_lltc > bp->max_tc)
8134 bp->max_lltc = bp->max_tc;
8137 hwrm_req_drop(bp, req);
8141 static int bnxt_hwrm_poll(struct bnxt *bp)
8143 struct hwrm_ver_get_input *req;
8146 rc = hwrm_req_init(bp, req, HWRM_VER_GET);
8150 req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
8151 req->hwrm_intf_min = HWRM_VERSION_MINOR;
8152 req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
8154 hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
8155 rc = hwrm_req_send(bp, req);
8159 static int bnxt_hwrm_ver_get(struct bnxt *bp)
8161 struct hwrm_ver_get_output *resp;
8162 struct hwrm_ver_get_input *req;
8163 u16 fw_maj, fw_min, fw_bld, fw_rsv;
8164 u32 dev_caps_cfg, hwrm_ver;
8167 rc = hwrm_req_init(bp, req, HWRM_VER_GET);
8171 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
8172 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
8173 req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
8174 req->hwrm_intf_min = HWRM_VERSION_MINOR;
8175 req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
8177 resp = hwrm_req_hold(bp, req);
8178 rc = hwrm_req_send(bp, req);
8180 goto hwrm_ver_get_exit;
8182 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
8184 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
8185 resp->hwrm_intf_min_8b << 8 |
8186 resp->hwrm_intf_upd_8b;
8187 if (resp->hwrm_intf_maj_8b < 1) {
8188 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
8189 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
8190 resp->hwrm_intf_upd_8b);
8191 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
8194 hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
8195 HWRM_VERSION_UPDATE;
8197 if (bp->hwrm_spec_code > hwrm_ver)
8198 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
8199 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
8200 HWRM_VERSION_UPDATE);
8202 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
8203 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
8204 resp->hwrm_intf_upd_8b);
8206 fw_maj = le16_to_cpu(resp->hwrm_fw_major);
8207 if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
8208 fw_min = le16_to_cpu(resp->hwrm_fw_minor);
8209 fw_bld = le16_to_cpu(resp->hwrm_fw_build);
8210 fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
8211 len = FW_VER_STR_LEN;
8213 fw_maj = resp->hwrm_fw_maj_8b;
8214 fw_min = resp->hwrm_fw_min_8b;
8215 fw_bld = resp->hwrm_fw_bld_8b;
8216 fw_rsv = resp->hwrm_fw_rsvd_8b;
8217 len = BC_HWRM_STR_LEN;
8219 bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
8220 snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
8223 if (strlen(resp->active_pkg_name)) {
8224 int fw_ver_len = strlen(bp->fw_ver_str);
8226 snprintf(bp->fw_ver_str + fw_ver_len,
8227 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
8228 resp->active_pkg_name);
8229 bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
8232 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
8233 if (!bp->hwrm_cmd_timeout)
8234 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
8235 bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
8236 if (!bp->hwrm_cmd_max_timeout)
8237 bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
8238 else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
8239 netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
8240 bp->hwrm_cmd_max_timeout / 1000);
8242 if (resp->hwrm_intf_maj_8b >= 1) {
8243 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
8244 bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
8246 if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
8247 bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
8249 bp->chip_num = le16_to_cpu(resp->chip_num);
8250 bp->chip_rev = resp->chip_rev;
8251 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
8253 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
8255 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
8256 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
8257 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
8258 bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
8260 if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
8261 bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
8264 VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
8265 bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
8268 VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
8269 bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
8272 VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
8273 bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
8276 hwrm_req_drop(bp, req);
8280 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
8282 struct hwrm_fw_set_time_input *req;
8284 time64_t now = ktime_get_real_seconds();
8287 if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
8288 bp->hwrm_spec_code < 0x10400)
8291 time64_to_tm(now, 0, &tm);
8292 rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
8296 req->year = cpu_to_le16(1900 + tm.tm_year);
8297 req->month = 1 + tm.tm_mon;
8298 req->day = tm.tm_mday;
8299 req->hour = tm.tm_hour;
8300 req->minute = tm.tm_min;
8301 req->second = tm.tm_sec;
8302 return hwrm_req_send(bp, req);
8305 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
8310 sw_tmp = (*sw & ~mask) | hw;
8311 if (hw < (*sw & mask))
8313 WRITE_ONCE(*sw, sw_tmp);
8316 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
8317 int count, bool ignore_zero)
8321 for (i = 0; i < count; i++) {
8322 u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
8324 if (ignore_zero && !hw)
8327 if (masks[i] == -1ULL)
8330 bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
8334 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
8336 if (!stats->hw_stats)
8339 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
8340 stats->hw_masks, stats->len / 8, false);
8343 static void bnxt_accumulate_all_stats(struct bnxt *bp)
8345 struct bnxt_stats_mem *ring0_stats;
8346 bool ignore_zero = false;
8349 /* Chip bug. Counter intermittently becomes 0. */
8350 if (bp->flags & BNXT_FLAG_CHIP_P5)
8353 for (i = 0; i < bp->cp_nr_rings; i++) {
8354 struct bnxt_napi *bnapi = bp->bnapi[i];
8355 struct bnxt_cp_ring_info *cpr;
8356 struct bnxt_stats_mem *stats;
8358 cpr = &bnapi->cp_ring;
8359 stats = &cpr->stats;
8361 ring0_stats = stats;
8362 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
8363 ring0_stats->hw_masks,
8364 ring0_stats->len / 8, ignore_zero);
8366 if (bp->flags & BNXT_FLAG_PORT_STATS) {
8367 struct bnxt_stats_mem *stats = &bp->port_stats;
8368 __le64 *hw_stats = stats->hw_stats;
8369 u64 *sw_stats = stats->sw_stats;
8370 u64 *masks = stats->hw_masks;
8373 cnt = sizeof(struct rx_port_stats) / 8;
8374 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
8376 hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
8377 sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
8378 masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
8379 cnt = sizeof(struct tx_port_stats) / 8;
8380 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
8382 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
8383 bnxt_accumulate_stats(&bp->rx_port_stats_ext);
8384 bnxt_accumulate_stats(&bp->tx_port_stats_ext);
8388 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
8390 struct hwrm_port_qstats_input *req;
8391 struct bnxt_pf_info *pf = &bp->pf;
8394 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
8397 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
8400 rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
8405 req->port_id = cpu_to_le16(pf->port_id);
8406 req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
8407 BNXT_TX_PORT_STATS_BYTE_OFFSET);
8408 req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
8409 return hwrm_req_send(bp, req);
8412 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
8414 struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
8415 struct hwrm_queue_pri2cos_qcfg_input *req_qc;
8416 struct hwrm_port_qstats_ext_output *resp_qs;
8417 struct hwrm_port_qstats_ext_input *req_qs;
8418 struct bnxt_pf_info *pf = &bp->pf;
8422 if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
8425 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
8428 rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
8432 req_qs->flags = flags;
8433 req_qs->port_id = cpu_to_le16(pf->port_id);
8434 req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
8435 req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
8436 tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
8437 sizeof(struct tx_port_stats_ext) : 0;
8438 req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
8439 req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
8440 resp_qs = hwrm_req_hold(bp, req_qs);
8441 rc = hwrm_req_send(bp, req_qs);
8443 bp->fw_rx_stats_ext_size =
8444 le16_to_cpu(resp_qs->rx_stat_size) / 8;
8445 if (BNXT_FW_MAJ(bp) < 220 &&
8446 bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
8447 bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
8449 bp->fw_tx_stats_ext_size = tx_stat_size ?
8450 le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
8452 bp->fw_rx_stats_ext_size = 0;
8453 bp->fw_tx_stats_ext_size = 0;
8455 hwrm_req_drop(bp, req_qs);
8460 if (bp->fw_tx_stats_ext_size <=
8461 offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
8462 bp->pri2cos_valid = 0;
8466 rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
8470 req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
8472 resp_qc = hwrm_req_hold(bp, req_qc);
8473 rc = hwrm_req_send(bp, req_qc);
8478 pri2cos = &resp_qc->pri0_cos_queue_id;
8479 for (i = 0; i < 8; i++) {
8480 u8 queue_id = pri2cos[i];
8483 /* Per port queue IDs start from 0, 10, 20, etc */
8484 queue_idx = queue_id % 10;
8485 if (queue_idx > BNXT_MAX_QUEUE) {
8486 bp->pri2cos_valid = false;
8487 hwrm_req_drop(bp, req_qc);
8490 for (j = 0; j < bp->max_q; j++) {
8491 if (bp->q_ids[j] == queue_id)
8492 bp->pri2cos_idx[i] = queue_idx;
8495 bp->pri2cos_valid = true;
8497 hwrm_req_drop(bp, req_qc);
8502 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
8504 bnxt_hwrm_tunnel_dst_port_free(bp,
8505 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
8506 bnxt_hwrm_tunnel_dst_port_free(bp,
8507 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
8510 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
8516 tpa_flags = bp->flags & BNXT_FLAG_TPA;
8517 else if (BNXT_NO_FW_ACCESS(bp))
8519 for (i = 0; i < bp->nr_vnics; i++) {
8520 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
8522 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
8530 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
8534 for (i = 0; i < bp->nr_vnics; i++)
8535 bnxt_hwrm_vnic_set_rss(bp, i, false);
8538 static void bnxt_clear_vnic(struct bnxt *bp)
8543 bnxt_hwrm_clear_vnic_filter(bp);
8544 if (!(bp->flags & BNXT_FLAG_CHIP_P5)) {
8545 /* clear all RSS setting before free vnic ctx */
8546 bnxt_hwrm_clear_vnic_rss(bp);
8547 bnxt_hwrm_vnic_ctx_free(bp);
8549 /* before free the vnic, undo the vnic tpa settings */
8550 if (bp->flags & BNXT_FLAG_TPA)
8551 bnxt_set_tpa(bp, false);
8552 bnxt_hwrm_vnic_free(bp);
8553 if (bp->flags & BNXT_FLAG_CHIP_P5)
8554 bnxt_hwrm_vnic_ctx_free(bp);
8557 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
8560 bnxt_clear_vnic(bp);
8561 bnxt_hwrm_ring_free(bp, close_path);
8562 bnxt_hwrm_ring_grp_free(bp);
8564 bnxt_hwrm_stat_ctx_free(bp);
8565 bnxt_hwrm_free_tunnel_ports(bp);
8569 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
8571 struct hwrm_func_cfg_input *req;
8575 if (br_mode == BRIDGE_MODE_VEB)
8576 evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
8577 else if (br_mode == BRIDGE_MODE_VEPA)
8578 evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
8582 rc = hwrm_req_init(bp, req, HWRM_FUNC_CFG);
8586 req->fid = cpu_to_le16(0xffff);
8587 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
8588 req->evb_mode = evb_mode;
8589 return hwrm_req_send(bp, req);
8592 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
8594 struct hwrm_func_cfg_input *req;
8597 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
8600 rc = hwrm_req_init(bp, req, HWRM_FUNC_CFG);
8604 req->fid = cpu_to_le16(0xffff);
8605 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
8606 req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
8608 req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
8610 return hwrm_req_send(bp, req);
8613 static int __bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
8615 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
8618 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
8621 /* allocate context for vnic */
8622 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
8624 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
8626 goto vnic_setup_err;
8628 bp->rsscos_nr_ctxs++;
8630 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8631 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
8633 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
8635 goto vnic_setup_err;
8637 bp->rsscos_nr_ctxs++;
8641 /* configure default vnic, ring grp */
8642 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
8644 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
8646 goto vnic_setup_err;
8649 /* Enable RSS hashing on vnic */
8650 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
8652 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
8654 goto vnic_setup_err;
8657 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8658 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
8660 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
8669 static int __bnxt_setup_vnic_p5(struct bnxt *bp, u16 vnic_id)
8673 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
8674 for (i = 0; i < nr_ctxs; i++) {
8675 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, i);
8677 netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
8681 bp->rsscos_nr_ctxs++;
8686 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic_id, true);
8688 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
8692 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
8694 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
8698 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8699 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
8701 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
8708 static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
8710 if (bp->flags & BNXT_FLAG_CHIP_P5)
8711 return __bnxt_setup_vnic_p5(bp, vnic_id);
8713 return __bnxt_setup_vnic(bp, vnic_id);
8716 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
8718 #ifdef CONFIG_RFS_ACCEL
8721 if (bp->flags & BNXT_FLAG_CHIP_P5)
8724 for (i = 0; i < bp->rx_nr_rings; i++) {
8725 struct bnxt_vnic_info *vnic;
8726 u16 vnic_id = i + 1;
8729 if (vnic_id >= bp->nr_vnics)
8732 vnic = &bp->vnic_info[vnic_id];
8733 vnic->flags |= BNXT_VNIC_RFS_FLAG;
8734 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
8735 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
8736 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
8738 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
8742 rc = bnxt_setup_vnic(bp, vnic_id);
8752 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
8753 static bool bnxt_promisc_ok(struct bnxt *bp)
8755 #ifdef CONFIG_BNXT_SRIOV
8756 if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
8762 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
8764 unsigned int rc = 0;
8766 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
8768 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
8773 rc = bnxt_hwrm_vnic_cfg(bp, 1);
8775 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
8782 static int bnxt_cfg_rx_mode(struct bnxt *);
8783 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
8785 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
8787 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8789 unsigned int rx_nr_rings = bp->rx_nr_rings;
8792 rc = bnxt_hwrm_stat_ctx_alloc(bp);
8794 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
8800 rc = bnxt_hwrm_ring_alloc(bp);
8802 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
8806 rc = bnxt_hwrm_ring_grp_alloc(bp);
8808 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
8812 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8815 /* default vnic 0 */
8816 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
8818 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
8823 bnxt_hwrm_func_qcfg(bp);
8825 rc = bnxt_setup_vnic(bp, 0);
8828 if (bp->fw_cap & BNXT_FW_CAP_RSS_HASH_TYPE_DELTA)
8829 bnxt_hwrm_update_rss_hash_cfg(bp);
8831 if (bp->flags & BNXT_FLAG_RFS) {
8832 rc = bnxt_alloc_rfs_vnics(bp);
8837 if (bp->flags & BNXT_FLAG_TPA) {
8838 rc = bnxt_set_tpa(bp, true);
8844 bnxt_update_vf_mac(bp);
8846 /* Filter for default vnic 0 */
8847 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
8849 if (BNXT_VF(bp) && rc == -ENODEV)
8850 netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
8852 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
8855 vnic->uc_filter_count = 1;
8858 if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
8861 if (bp->dev->flags & IFF_BROADCAST)
8862 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
8864 if (bp->dev->flags & IFF_PROMISC)
8865 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
8867 if (bp->dev->flags & IFF_ALLMULTI) {
8868 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
8869 vnic->mc_list_count = 0;
8870 } else if (bp->dev->flags & IFF_MULTICAST) {
8873 bnxt_mc_list_updated(bp, &mask);
8874 vnic->rx_mask |= mask;
8877 rc = bnxt_cfg_rx_mode(bp);
8882 rc = bnxt_hwrm_set_coal(bp);
8884 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
8887 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8888 rc = bnxt_setup_nitroa0_vnic(bp);
8890 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
8895 bnxt_hwrm_func_qcfg(bp);
8896 netdev_update_features(bp->dev);
8902 bnxt_hwrm_resource_free(bp, 0, true);
8907 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
8909 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
8913 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
8915 bnxt_init_cp_rings(bp);
8916 bnxt_init_rx_rings(bp);
8917 bnxt_init_tx_rings(bp);
8918 bnxt_init_ring_grps(bp, irq_re_init);
8919 bnxt_init_vnics(bp);
8921 return bnxt_init_chip(bp, irq_re_init);
8924 static int bnxt_set_real_num_queues(struct bnxt *bp)
8927 struct net_device *dev = bp->dev;
8929 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
8930 bp->tx_nr_rings_xdp);
8934 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
8938 #ifdef CONFIG_RFS_ACCEL
8939 if (bp->flags & BNXT_FLAG_RFS)
8940 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
8946 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
8949 int _rx = *rx, _tx = *tx;
8952 *rx = min_t(int, _rx, max);
8953 *tx = min_t(int, _tx, max);
8958 while (_rx + _tx > max) {
8959 if (_rx > _tx && _rx > 1)
8970 static void bnxt_setup_msix(struct bnxt *bp)
8972 const int len = sizeof(bp->irq_tbl[0].name);
8973 struct net_device *dev = bp->dev;
8976 tcs = netdev_get_num_tc(dev);
8980 for (i = 0; i < tcs; i++) {
8981 count = bp->tx_nr_rings_per_tc;
8983 netdev_set_tc_queue(dev, i, count, off);
8987 for (i = 0; i < bp->cp_nr_rings; i++) {
8988 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
8991 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8993 else if (i < bp->rx_nr_rings)
8998 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
9000 bp->irq_tbl[map_idx].handler = bnxt_msix;
9004 static void bnxt_setup_inta(struct bnxt *bp)
9006 const int len = sizeof(bp->irq_tbl[0].name);
9008 if (netdev_get_num_tc(bp->dev))
9009 netdev_reset_tc(bp->dev);
9011 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
9013 bp->irq_tbl[0].handler = bnxt_inta;
9016 static int bnxt_init_int_mode(struct bnxt *bp);
9018 static int bnxt_setup_int_mode(struct bnxt *bp)
9023 rc = bnxt_init_int_mode(bp);
9024 if (rc || !bp->irq_tbl)
9025 return rc ?: -ENODEV;
9028 if (bp->flags & BNXT_FLAG_USING_MSIX)
9029 bnxt_setup_msix(bp);
9031 bnxt_setup_inta(bp);
9033 rc = bnxt_set_real_num_queues(bp);
9037 #ifdef CONFIG_RFS_ACCEL
9038 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
9040 return bp->hw_resc.max_rsscos_ctxs;
9043 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
9045 return bp->hw_resc.max_vnics;
9049 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
9051 return bp->hw_resc.max_stat_ctxs;
9054 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
9056 return bp->hw_resc.max_cp_rings;
9059 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
9061 unsigned int cp = bp->hw_resc.max_cp_rings;
9063 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
9064 cp -= bnxt_get_ulp_msix_num(bp);
9069 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
9071 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9073 if (bp->flags & BNXT_FLAG_CHIP_P5)
9074 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
9076 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
9079 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
9081 bp->hw_resc.max_irqs = max_irqs;
9084 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
9088 cp = bnxt_get_max_func_cp_rings_for_en(bp);
9089 if (bp->flags & BNXT_FLAG_CHIP_P5)
9090 return cp - bp->rx_nr_rings - bp->tx_nr_rings;
9092 return cp - bp->cp_nr_rings;
9095 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
9097 return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
9100 int bnxt_get_avail_msix(struct bnxt *bp, int num)
9102 int max_cp = bnxt_get_max_func_cp_rings(bp);
9103 int max_irq = bnxt_get_max_func_irqs(bp);
9104 int total_req = bp->cp_nr_rings + num;
9105 int max_idx, avail_msix;
9107 max_idx = bp->total_irqs;
9108 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
9109 max_idx = min_t(int, bp->total_irqs, max_cp);
9110 avail_msix = max_idx - bp->cp_nr_rings;
9111 if (!BNXT_NEW_RM(bp) || avail_msix >= num)
9114 if (max_irq < total_req) {
9115 num = max_irq - bp->cp_nr_rings;
9122 static int bnxt_get_num_msix(struct bnxt *bp)
9124 if (!BNXT_NEW_RM(bp))
9125 return bnxt_get_max_func_irqs(bp);
9127 return bnxt_nq_rings_in_use(bp);
9130 static int bnxt_init_msix(struct bnxt *bp)
9132 int i, total_vecs, max, rc = 0, min = 1, ulp_msix;
9133 struct msix_entry *msix_ent;
9135 total_vecs = bnxt_get_num_msix(bp);
9136 max = bnxt_get_max_func_irqs(bp);
9137 if (total_vecs > max)
9143 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
9147 for (i = 0; i < total_vecs; i++) {
9148 msix_ent[i].entry = i;
9149 msix_ent[i].vector = 0;
9152 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
9155 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
9156 ulp_msix = bnxt_get_ulp_msix_num(bp);
9157 if (total_vecs < 0 || total_vecs < ulp_msix) {
9159 goto msix_setup_exit;
9162 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
9164 for (i = 0; i < total_vecs; i++)
9165 bp->irq_tbl[i].vector = msix_ent[i].vector;
9167 bp->total_irqs = total_vecs;
9168 /* Trim rings based upon num of vectors allocated */
9169 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
9170 total_vecs - ulp_msix, min == 1);
9172 goto msix_setup_exit;
9174 bp->cp_nr_rings = (min == 1) ?
9175 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
9176 bp->tx_nr_rings + bp->rx_nr_rings;
9180 goto msix_setup_exit;
9182 bp->flags |= BNXT_FLAG_USING_MSIX;
9187 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
9190 pci_disable_msix(bp->pdev);
9195 static int bnxt_init_inta(struct bnxt *bp)
9197 bp->irq_tbl = kzalloc(sizeof(struct bnxt_irq), GFP_KERNEL);
9202 bp->rx_nr_rings = 1;
9203 bp->tx_nr_rings = 1;
9204 bp->cp_nr_rings = 1;
9205 bp->flags |= BNXT_FLAG_SHARED_RINGS;
9206 bp->irq_tbl[0].vector = bp->pdev->irq;
9210 static int bnxt_init_int_mode(struct bnxt *bp)
9214 if (bp->flags & BNXT_FLAG_MSIX_CAP)
9215 rc = bnxt_init_msix(bp);
9217 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
9218 /* fallback to INTA */
9219 rc = bnxt_init_inta(bp);
9224 static void bnxt_clear_int_mode(struct bnxt *bp)
9226 if (bp->flags & BNXT_FLAG_USING_MSIX)
9227 pci_disable_msix(bp->pdev);
9231 bp->flags &= ~BNXT_FLAG_USING_MSIX;
9234 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
9236 int tcs = netdev_get_num_tc(bp->dev);
9237 bool irq_cleared = false;
9240 if (!bnxt_need_reserve_rings(bp))
9243 if (irq_re_init && BNXT_NEW_RM(bp) &&
9244 bnxt_get_num_msix(bp) != bp->total_irqs) {
9245 bnxt_ulp_irq_stop(bp);
9246 bnxt_clear_int_mode(bp);
9249 rc = __bnxt_reserve_rings(bp);
9252 rc = bnxt_init_int_mode(bp);
9253 bnxt_ulp_irq_restart(bp, rc);
9256 netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
9259 if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
9260 bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
9261 netdev_err(bp->dev, "tx ring reservation failure\n");
9262 netdev_reset_tc(bp->dev);
9263 if (bp->tx_nr_rings_xdp)
9264 bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
9266 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
9272 static void bnxt_free_irq(struct bnxt *bp)
9274 struct bnxt_irq *irq;
9277 #ifdef CONFIG_RFS_ACCEL
9278 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
9279 bp->dev->rx_cpu_rmap = NULL;
9281 if (!bp->irq_tbl || !bp->bnapi)
9284 for (i = 0; i < bp->cp_nr_rings; i++) {
9285 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
9287 irq = &bp->irq_tbl[map_idx];
9288 if (irq->requested) {
9289 if (irq->have_cpumask) {
9290 irq_set_affinity_hint(irq->vector, NULL);
9291 free_cpumask_var(irq->cpu_mask);
9292 irq->have_cpumask = 0;
9294 free_irq(irq->vector, bp->bnapi[i]);
9301 static int bnxt_request_irq(struct bnxt *bp)
9304 unsigned long flags = 0;
9305 #ifdef CONFIG_RFS_ACCEL
9306 struct cpu_rmap *rmap;
9309 rc = bnxt_setup_int_mode(bp);
9311 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
9315 #ifdef CONFIG_RFS_ACCEL
9316 rmap = bp->dev->rx_cpu_rmap;
9318 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
9319 flags = IRQF_SHARED;
9321 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
9322 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
9323 struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
9325 #ifdef CONFIG_RFS_ACCEL
9326 if (rmap && bp->bnapi[i]->rx_ring) {
9327 rc = irq_cpu_rmap_add(rmap, irq->vector);
9329 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
9334 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
9341 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
9342 int numa_node = dev_to_node(&bp->pdev->dev);
9344 irq->have_cpumask = 1;
9345 cpumask_set_cpu(cpumask_local_spread(i, numa_node),
9347 rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
9349 netdev_warn(bp->dev,
9350 "Set affinity failed, IRQ = %d\n",
9359 static void bnxt_del_napi(struct bnxt *bp)
9366 for (i = 0; i < bp->cp_nr_rings; i++) {
9367 struct bnxt_napi *bnapi = bp->bnapi[i];
9369 __netif_napi_del(&bnapi->napi);
9371 /* We called __netif_napi_del(), we need
9372 * to respect an RCU grace period before freeing napi structures.
9377 static void bnxt_init_napi(struct bnxt *bp)
9380 unsigned int cp_nr_rings = bp->cp_nr_rings;
9381 struct bnxt_napi *bnapi;
9383 if (bp->flags & BNXT_FLAG_USING_MSIX) {
9384 int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
9386 if (bp->flags & BNXT_FLAG_CHIP_P5)
9387 poll_fn = bnxt_poll_p5;
9388 else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
9390 for (i = 0; i < cp_nr_rings; i++) {
9391 bnapi = bp->bnapi[i];
9392 netif_napi_add(bp->dev, &bnapi->napi, poll_fn);
9394 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
9395 bnapi = bp->bnapi[cp_nr_rings];
9396 netif_napi_add(bp->dev, &bnapi->napi,
9400 bnapi = bp->bnapi[0];
9401 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll);
9405 static void bnxt_disable_napi(struct bnxt *bp)
9410 test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
9413 for (i = 0; i < bp->cp_nr_rings; i++) {
9414 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
9416 napi_disable(&bp->bnapi[i]->napi);
9417 if (bp->bnapi[i]->rx_ring)
9418 cancel_work_sync(&cpr->dim.work);
9422 static void bnxt_enable_napi(struct bnxt *bp)
9426 clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
9427 for (i = 0; i < bp->cp_nr_rings; i++) {
9428 struct bnxt_napi *bnapi = bp->bnapi[i];
9429 struct bnxt_cp_ring_info *cpr;
9431 cpr = &bnapi->cp_ring;
9432 if (bnapi->in_reset)
9433 cpr->sw_stats.rx.rx_resets++;
9434 bnapi->in_reset = false;
9436 if (bnapi->rx_ring) {
9437 INIT_WORK(&cpr->dim.work, bnxt_dim_work);
9438 cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
9440 napi_enable(&bnapi->napi);
9444 void bnxt_tx_disable(struct bnxt *bp)
9447 struct bnxt_tx_ring_info *txr;
9450 for (i = 0; i < bp->tx_nr_rings; i++) {
9451 txr = &bp->tx_ring[i];
9452 WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
9455 /* Make sure napi polls see @dev_state change */
9457 /* Drop carrier first to prevent TX timeout */
9458 netif_carrier_off(bp->dev);
9459 /* Stop all TX queues */
9460 netif_tx_disable(bp->dev);
9463 void bnxt_tx_enable(struct bnxt *bp)
9466 struct bnxt_tx_ring_info *txr;
9468 for (i = 0; i < bp->tx_nr_rings; i++) {
9469 txr = &bp->tx_ring[i];
9470 WRITE_ONCE(txr->dev_state, 0);
9472 /* Make sure napi polls see @dev_state change */
9474 netif_tx_wake_all_queues(bp->dev);
9475 if (BNXT_LINK_IS_UP(bp))
9476 netif_carrier_on(bp->dev);
9479 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
9481 u8 active_fec = link_info->active_fec_sig_mode &
9482 PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
9484 switch (active_fec) {
9486 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
9488 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
9489 return "Clause 74 BaseR";
9490 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
9491 return "Clause 91 RS(528,514)";
9492 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
9493 return "Clause 91 RS544_1XN";
9494 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
9495 return "Clause 91 RS(544,514)";
9496 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
9497 return "Clause 91 RS272_1XN";
9498 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
9499 return "Clause 91 RS(272,257)";
9503 void bnxt_report_link(struct bnxt *bp)
9505 if (BNXT_LINK_IS_UP(bp)) {
9506 const char *signal = "";
9507 const char *flow_ctrl;
9512 netif_carrier_on(bp->dev);
9513 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
9514 if (speed == SPEED_UNKNOWN) {
9515 netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
9518 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
9522 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
9523 flow_ctrl = "ON - receive & transmit";
9524 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
9525 flow_ctrl = "ON - transmit";
9526 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
9527 flow_ctrl = "ON - receive";
9530 if (bp->link_info.phy_qcfg_resp.option_flags &
9531 PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
9532 u8 sig_mode = bp->link_info.active_fec_sig_mode &
9533 PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
9535 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
9538 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
9545 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
9546 speed, signal, duplex, flow_ctrl);
9547 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
9548 netdev_info(bp->dev, "EEE is %s\n",
9549 bp->eee.eee_active ? "active" :
9551 fec = bp->link_info.fec_cfg;
9552 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
9553 netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
9554 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
9555 bnxt_report_fec(&bp->link_info));
9557 netif_carrier_off(bp->dev);
9558 netdev_err(bp->dev, "NIC Link is Down\n");
9562 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
9564 if (!resp->supported_speeds_auto_mode &&
9565 !resp->supported_speeds_force_mode &&
9566 !resp->supported_pam4_speeds_auto_mode &&
9567 !resp->supported_pam4_speeds_force_mode)
9572 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
9574 struct bnxt_link_info *link_info = &bp->link_info;
9575 struct hwrm_port_phy_qcaps_output *resp;
9576 struct hwrm_port_phy_qcaps_input *req;
9579 if (bp->hwrm_spec_code < 0x10201)
9582 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
9586 resp = hwrm_req_hold(bp, req);
9587 rc = hwrm_req_send(bp, req);
9589 goto hwrm_phy_qcaps_exit;
9591 bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
9592 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
9593 struct ethtool_eee *eee = &bp->eee;
9594 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
9596 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9597 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
9598 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
9599 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
9600 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
9603 if (bp->hwrm_spec_code >= 0x10a01) {
9604 if (bnxt_phy_qcaps_no_speed(resp)) {
9605 link_info->phy_state = BNXT_PHY_STATE_DISABLED;
9606 netdev_warn(bp->dev, "Ethernet link disabled\n");
9607 } else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
9608 link_info->phy_state = BNXT_PHY_STATE_ENABLED;
9609 netdev_info(bp->dev, "Ethernet link enabled\n");
9610 /* Phy re-enabled, reprobe the speeds */
9611 link_info->support_auto_speeds = 0;
9612 link_info->support_pam4_auto_speeds = 0;
9615 if (resp->supported_speeds_auto_mode)
9616 link_info->support_auto_speeds =
9617 le16_to_cpu(resp->supported_speeds_auto_mode);
9618 if (resp->supported_pam4_speeds_auto_mode)
9619 link_info->support_pam4_auto_speeds =
9620 le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
9622 bp->port_count = resp->port_cnt;
9624 hwrm_phy_qcaps_exit:
9625 hwrm_req_drop(bp, req);
9629 static bool bnxt_support_dropped(u16 advertising, u16 supported)
9631 u16 diff = advertising ^ supported;
9633 return ((supported | diff) != supported);
9636 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
9638 struct bnxt_link_info *link_info = &bp->link_info;
9639 struct hwrm_port_phy_qcfg_output *resp;
9640 struct hwrm_port_phy_qcfg_input *req;
9641 u8 link_state = link_info->link_state;
9642 bool support_changed = false;
9645 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
9649 resp = hwrm_req_hold(bp, req);
9650 rc = hwrm_req_send(bp, req);
9652 hwrm_req_drop(bp, req);
9653 if (BNXT_VF(bp) && rc == -ENODEV) {
9654 netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
9660 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
9661 link_info->phy_link_status = resp->link;
9662 link_info->duplex = resp->duplex_cfg;
9663 if (bp->hwrm_spec_code >= 0x10800)
9664 link_info->duplex = resp->duplex_state;
9665 link_info->pause = resp->pause;
9666 link_info->auto_mode = resp->auto_mode;
9667 link_info->auto_pause_setting = resp->auto_pause;
9668 link_info->lp_pause = resp->link_partner_adv_pause;
9669 link_info->force_pause_setting = resp->force_pause;
9670 link_info->duplex_setting = resp->duplex_cfg;
9671 if (link_info->phy_link_status == BNXT_LINK_LINK)
9672 link_info->link_speed = le16_to_cpu(resp->link_speed);
9674 link_info->link_speed = 0;
9675 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
9676 link_info->force_pam4_link_speed =
9677 le16_to_cpu(resp->force_pam4_link_speed);
9678 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
9679 link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
9680 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
9681 link_info->auto_pam4_link_speeds =
9682 le16_to_cpu(resp->auto_pam4_link_speed_mask);
9683 link_info->lp_auto_link_speeds =
9684 le16_to_cpu(resp->link_partner_adv_speeds);
9685 link_info->lp_auto_pam4_link_speeds =
9686 resp->link_partner_pam4_adv_speeds;
9687 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
9688 link_info->phy_ver[0] = resp->phy_maj;
9689 link_info->phy_ver[1] = resp->phy_min;
9690 link_info->phy_ver[2] = resp->phy_bld;
9691 link_info->media_type = resp->media_type;
9692 link_info->phy_type = resp->phy_type;
9693 link_info->transceiver = resp->xcvr_pkg_type;
9694 link_info->phy_addr = resp->eee_config_phy_addr &
9695 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
9696 link_info->module_status = resp->module_status;
9698 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
9699 struct ethtool_eee *eee = &bp->eee;
9702 eee->eee_active = 0;
9703 if (resp->eee_config_phy_addr &
9704 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
9705 eee->eee_active = 1;
9706 fw_speeds = le16_to_cpu(
9707 resp->link_partner_adv_eee_link_speed_mask);
9708 eee->lp_advertised =
9709 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9712 /* Pull initial EEE config */
9713 if (!chng_link_state) {
9714 if (resp->eee_config_phy_addr &
9715 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
9716 eee->eee_enabled = 1;
9718 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
9720 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9722 if (resp->eee_config_phy_addr &
9723 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
9726 eee->tx_lpi_enabled = 1;
9727 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
9728 eee->tx_lpi_timer = le32_to_cpu(tmr) &
9729 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
9734 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
9735 if (bp->hwrm_spec_code >= 0x10504) {
9736 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
9737 link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
9739 /* TODO: need to add more logic to report VF link */
9740 if (chng_link_state) {
9741 if (link_info->phy_link_status == BNXT_LINK_LINK)
9742 link_info->link_state = BNXT_LINK_STATE_UP;
9744 link_info->link_state = BNXT_LINK_STATE_DOWN;
9745 if (link_state != link_info->link_state)
9746 bnxt_report_link(bp);
9748 /* always link down if not require to update link state */
9749 link_info->link_state = BNXT_LINK_STATE_DOWN;
9751 hwrm_req_drop(bp, req);
9753 if (!BNXT_PHY_CFG_ABLE(bp))
9756 /* Check if any advertised speeds are no longer supported. The caller
9757 * holds the link_lock mutex, so we can modify link_info settings.
9759 if (bnxt_support_dropped(link_info->advertising,
9760 link_info->support_auto_speeds)) {
9761 link_info->advertising = link_info->support_auto_speeds;
9762 support_changed = true;
9764 if (bnxt_support_dropped(link_info->advertising_pam4,
9765 link_info->support_pam4_auto_speeds)) {
9766 link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
9767 support_changed = true;
9769 if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
9770 bnxt_hwrm_set_link_setting(bp, true, false);
9774 static void bnxt_get_port_module_status(struct bnxt *bp)
9776 struct bnxt_link_info *link_info = &bp->link_info;
9777 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
9780 if (bnxt_update_link(bp, true))
9783 module_status = link_info->module_status;
9784 switch (module_status) {
9785 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
9786 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
9787 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
9788 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
9790 if (bp->hwrm_spec_code >= 0x10201) {
9791 netdev_warn(bp->dev, "Module part number %s\n",
9792 resp->phy_vendor_partnumber);
9794 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
9795 netdev_warn(bp->dev, "TX is disabled\n");
9796 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
9797 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
9802 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
9804 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
9805 if (bp->hwrm_spec_code >= 0x10201)
9807 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
9808 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
9809 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
9810 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
9811 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
9813 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
9815 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
9816 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
9817 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
9818 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
9820 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
9821 if (bp->hwrm_spec_code >= 0x10201) {
9822 req->auto_pause = req->force_pause;
9823 req->enables |= cpu_to_le32(
9824 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
9829 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
9831 if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
9832 req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
9833 if (bp->link_info.advertising) {
9834 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
9835 req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
9837 if (bp->link_info.advertising_pam4) {
9839 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
9840 req->auto_link_pam4_speed_mask =
9841 cpu_to_le16(bp->link_info.advertising_pam4);
9843 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
9844 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
9846 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
9847 if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
9848 req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
9849 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
9851 req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
9855 /* tell chimp that the setting takes effect immediately */
9856 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
9859 int bnxt_hwrm_set_pause(struct bnxt *bp)
9861 struct hwrm_port_phy_cfg_input *req;
9864 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
9868 bnxt_hwrm_set_pause_common(bp, req);
9870 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
9871 bp->link_info.force_link_chng)
9872 bnxt_hwrm_set_link_common(bp, req);
9874 rc = hwrm_req_send(bp, req);
9875 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
9876 /* since changing of pause setting doesn't trigger any link
9877 * change event, the driver needs to update the current pause
9878 * result upon successfully return of the phy_cfg command
9880 bp->link_info.pause =
9881 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
9882 bp->link_info.auto_pause_setting = 0;
9883 if (!bp->link_info.force_link_chng)
9884 bnxt_report_link(bp);
9886 bp->link_info.force_link_chng = false;
9890 static void bnxt_hwrm_set_eee(struct bnxt *bp,
9891 struct hwrm_port_phy_cfg_input *req)
9893 struct ethtool_eee *eee = &bp->eee;
9895 if (eee->eee_enabled) {
9897 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
9899 if (eee->tx_lpi_enabled)
9900 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
9902 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
9904 req->flags |= cpu_to_le32(flags);
9905 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
9906 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
9907 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
9909 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
9913 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
9915 struct hwrm_port_phy_cfg_input *req;
9918 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
9923 bnxt_hwrm_set_pause_common(bp, req);
9925 bnxt_hwrm_set_link_common(bp, req);
9928 bnxt_hwrm_set_eee(bp, req);
9929 return hwrm_req_send(bp, req);
9932 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
9934 struct hwrm_port_phy_cfg_input *req;
9937 if (!BNXT_SINGLE_PF(bp))
9940 if (pci_num_vf(bp->pdev) &&
9941 !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
9944 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
9948 req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
9949 rc = hwrm_req_send(bp, req);
9951 mutex_lock(&bp->link_lock);
9952 /* Device is not obliged link down in certain scenarios, even
9953 * when forced. Setting the state unknown is consistent with
9954 * driver startup and will force link state to be reported
9955 * during subsequent open based on PORT_PHY_QCFG.
9957 bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
9958 mutex_unlock(&bp->link_lock);
9963 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
9965 #ifdef CONFIG_TEE_BNXT_FW
9966 int rc = tee_bnxt_fw_load();
9969 netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
9973 netdev_err(bp->dev, "OP-TEE not supported\n");
9978 static int bnxt_try_recover_fw(struct bnxt *bp)
9980 if (bp->fw_health && bp->fw_health->status_reliable) {
9985 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
9986 rc = bnxt_hwrm_poll(bp);
9987 if (!BNXT_FW_IS_BOOTING(sts) &&
9988 !BNXT_FW_IS_RECOVERING(sts))
9991 } while (rc == -EBUSY && retry < BNXT_FW_RETRY);
9993 if (!BNXT_FW_IS_HEALTHY(sts)) {
9995 "Firmware not responding, status: 0x%x\n",
9999 if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
10000 netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
10001 return bnxt_fw_reset_via_optee(bp);
10009 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
10011 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10013 if (!BNXT_NEW_RM(bp))
10014 return; /* no resource reservations required */
10016 hw_resc->resv_cp_rings = 0;
10017 hw_resc->resv_stat_ctxs = 0;
10018 hw_resc->resv_irqs = 0;
10019 hw_resc->resv_tx_rings = 0;
10020 hw_resc->resv_rx_rings = 0;
10021 hw_resc->resv_hw_ring_grps = 0;
10022 hw_resc->resv_vnics = 0;
10024 bp->tx_nr_rings = 0;
10025 bp->rx_nr_rings = 0;
10029 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
10033 if (!BNXT_NEW_RM(bp))
10034 return 0; /* no resource reservations required */
10036 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
10038 netdev_err(bp->dev, "resc_qcaps failed\n");
10040 bnxt_clear_reservations(bp, fw_reset);
10045 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
10047 struct hwrm_func_drv_if_change_output *resp;
10048 struct hwrm_func_drv_if_change_input *req;
10049 bool fw_reset = !bp->irq_tbl;
10050 bool resc_reinit = false;
10054 if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
10057 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
10062 req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
10063 resp = hwrm_req_hold(bp, req);
10065 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10066 while (retry < BNXT_FW_IF_RETRY) {
10067 rc = hwrm_req_send(bp, req);
10075 if (rc == -EAGAIN) {
10076 hwrm_req_drop(bp, req);
10079 flags = le32_to_cpu(resp->flags);
10081 rc = bnxt_try_recover_fw(bp);
10084 hwrm_req_drop(bp, req);
10089 bnxt_inv_fw_health_reg(bp);
10093 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
10094 resc_reinit = true;
10095 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
10096 test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
10099 bnxt_remap_fw_health_regs(bp);
10101 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
10102 netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
10103 set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10106 if (resc_reinit || fw_reset) {
10108 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
10109 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
10111 bnxt_free_ctx_mem(bp);
10115 rc = bnxt_fw_init_one(bp);
10117 clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
10118 set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10121 bnxt_clear_int_mode(bp);
10122 rc = bnxt_init_int_mode(bp);
10124 clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
10125 netdev_err(bp->dev, "init int mode failed\n");
10129 rc = bnxt_cancel_reservations(bp, fw_reset);
10134 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
10136 struct hwrm_port_led_qcaps_output *resp;
10137 struct hwrm_port_led_qcaps_input *req;
10138 struct bnxt_pf_info *pf = &bp->pf;
10142 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
10145 rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
10149 req->port_id = cpu_to_le16(pf->port_id);
10150 resp = hwrm_req_hold(bp, req);
10151 rc = hwrm_req_send(bp, req);
10153 hwrm_req_drop(bp, req);
10156 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
10159 bp->num_leds = resp->num_leds;
10160 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
10162 for (i = 0; i < bp->num_leds; i++) {
10163 struct bnxt_led_info *led = &bp->leds[i];
10164 __le16 caps = led->led_state_caps;
10166 if (!led->led_group_id ||
10167 !BNXT_LED_ALT_BLINK_CAP(caps)) {
10173 hwrm_req_drop(bp, req);
10177 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
10179 struct hwrm_wol_filter_alloc_output *resp;
10180 struct hwrm_wol_filter_alloc_input *req;
10183 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
10187 req->port_id = cpu_to_le16(bp->pf.port_id);
10188 req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
10189 req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
10190 memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
10192 resp = hwrm_req_hold(bp, req);
10193 rc = hwrm_req_send(bp, req);
10195 bp->wol_filter_id = resp->wol_filter_id;
10196 hwrm_req_drop(bp, req);
10200 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
10202 struct hwrm_wol_filter_free_input *req;
10205 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
10209 req->port_id = cpu_to_le16(bp->pf.port_id);
10210 req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
10211 req->wol_filter_id = bp->wol_filter_id;
10213 return hwrm_req_send(bp, req);
10216 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
10218 struct hwrm_wol_filter_qcfg_output *resp;
10219 struct hwrm_wol_filter_qcfg_input *req;
10220 u16 next_handle = 0;
10223 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
10227 req->port_id = cpu_to_le16(bp->pf.port_id);
10228 req->handle = cpu_to_le16(handle);
10229 resp = hwrm_req_hold(bp, req);
10230 rc = hwrm_req_send(bp, req);
10232 next_handle = le16_to_cpu(resp->next_handle);
10233 if (next_handle != 0) {
10234 if (resp->wol_type ==
10235 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
10237 bp->wol_filter_id = resp->wol_filter_id;
10241 hwrm_req_drop(bp, req);
10242 return next_handle;
10245 static void bnxt_get_wol_settings(struct bnxt *bp)
10250 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
10254 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
10255 } while (handle && handle != 0xffff);
10258 #ifdef CONFIG_BNXT_HWMON
10259 static ssize_t bnxt_show_temp(struct device *dev,
10260 struct device_attribute *devattr, char *buf)
10262 struct hwrm_temp_monitor_query_output *resp;
10263 struct hwrm_temp_monitor_query_input *req;
10264 struct bnxt *bp = dev_get_drvdata(dev);
10268 rc = hwrm_req_init(bp, req, HWRM_TEMP_MONITOR_QUERY);
10271 resp = hwrm_req_hold(bp, req);
10272 rc = hwrm_req_send(bp, req);
10274 len = sprintf(buf, "%u\n", resp->temp * 1000); /* display millidegree */
10275 hwrm_req_drop(bp, req);
10280 static SENSOR_DEVICE_ATTR(temp1_input, 0444, bnxt_show_temp, NULL, 0);
10282 static struct attribute *bnxt_attrs[] = {
10283 &sensor_dev_attr_temp1_input.dev_attr.attr,
10286 ATTRIBUTE_GROUPS(bnxt);
10288 static void bnxt_hwmon_close(struct bnxt *bp)
10290 if (bp->hwmon_dev) {
10291 hwmon_device_unregister(bp->hwmon_dev);
10292 bp->hwmon_dev = NULL;
10296 static void bnxt_hwmon_open(struct bnxt *bp)
10298 struct hwrm_temp_monitor_query_input *req;
10299 struct pci_dev *pdev = bp->pdev;
10302 rc = hwrm_req_init(bp, req, HWRM_TEMP_MONITOR_QUERY);
10304 rc = hwrm_req_send_silent(bp, req);
10305 if (rc == -EACCES || rc == -EOPNOTSUPP) {
10306 bnxt_hwmon_close(bp);
10313 bp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev,
10314 DRV_MODULE_NAME, bp,
10316 if (IS_ERR(bp->hwmon_dev)) {
10317 bp->hwmon_dev = NULL;
10318 dev_warn(&pdev->dev, "Cannot register hwmon device\n");
10322 static void bnxt_hwmon_close(struct bnxt *bp)
10326 static void bnxt_hwmon_open(struct bnxt *bp)
10331 static bool bnxt_eee_config_ok(struct bnxt *bp)
10333 struct ethtool_eee *eee = &bp->eee;
10334 struct bnxt_link_info *link_info = &bp->link_info;
10336 if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
10339 if (eee->eee_enabled) {
10341 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
10343 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
10344 eee->eee_enabled = 0;
10347 if (eee->advertised & ~advertising) {
10348 eee->advertised = advertising & eee->supported;
10355 static int bnxt_update_phy_setting(struct bnxt *bp)
10358 bool update_link = false;
10359 bool update_pause = false;
10360 bool update_eee = false;
10361 struct bnxt_link_info *link_info = &bp->link_info;
10363 rc = bnxt_update_link(bp, true);
10365 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
10369 if (!BNXT_SINGLE_PF(bp))
10372 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
10373 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
10374 link_info->req_flow_ctrl)
10375 update_pause = true;
10376 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
10377 link_info->force_pause_setting != link_info->req_flow_ctrl)
10378 update_pause = true;
10379 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
10380 if (BNXT_AUTO_MODE(link_info->auto_mode))
10381 update_link = true;
10382 if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
10383 link_info->req_link_speed != link_info->force_link_speed)
10384 update_link = true;
10385 else if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
10386 link_info->req_link_speed != link_info->force_pam4_link_speed)
10387 update_link = true;
10388 if (link_info->req_duplex != link_info->duplex_setting)
10389 update_link = true;
10391 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
10392 update_link = true;
10393 if (link_info->advertising != link_info->auto_link_speeds ||
10394 link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
10395 update_link = true;
10398 /* The last close may have shutdown the link, so need to call
10399 * PHY_CFG to bring it back up.
10401 if (!BNXT_LINK_IS_UP(bp))
10402 update_link = true;
10404 if (!bnxt_eee_config_ok(bp))
10408 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
10409 else if (update_pause)
10410 rc = bnxt_hwrm_set_pause(bp);
10412 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
10420 /* Common routine to pre-map certain register block to different GRC window.
10421 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
10422 * in PF and 3 windows in VF that can be customized to map in different
10425 static void bnxt_preset_reg_win(struct bnxt *bp)
10428 /* CAG registers map to GRC window #4 */
10429 writel(BNXT_CAG_REG_BASE,
10430 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
10434 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
10436 static int bnxt_reinit_after_abort(struct bnxt *bp)
10440 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
10443 if (bp->dev->reg_state == NETREG_UNREGISTERED)
10446 rc = bnxt_fw_init_one(bp);
10448 bnxt_clear_int_mode(bp);
10449 rc = bnxt_init_int_mode(bp);
10451 clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10452 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
10458 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10462 bnxt_preset_reg_win(bp);
10463 netif_carrier_off(bp->dev);
10465 /* Reserve rings now if none were reserved at driver probe. */
10466 rc = bnxt_init_dflt_ring_mode(bp);
10468 netdev_err(bp->dev, "Failed to reserve default rings at open\n");
10472 rc = bnxt_reserve_rings(bp, irq_re_init);
10475 if ((bp->flags & BNXT_FLAG_RFS) &&
10476 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
10477 /* disable RFS if falling back to INTA */
10478 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
10479 bp->flags &= ~BNXT_FLAG_RFS;
10482 rc = bnxt_alloc_mem(bp, irq_re_init);
10484 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
10485 goto open_err_free_mem;
10489 bnxt_init_napi(bp);
10490 rc = bnxt_request_irq(bp);
10492 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
10497 rc = bnxt_init_nic(bp, irq_re_init);
10499 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
10503 bnxt_enable_napi(bp);
10504 bnxt_debug_dev_init(bp);
10506 if (link_re_init) {
10507 mutex_lock(&bp->link_lock);
10508 rc = bnxt_update_phy_setting(bp);
10509 mutex_unlock(&bp->link_lock);
10511 netdev_warn(bp->dev, "failed to update phy settings\n");
10512 if (BNXT_SINGLE_PF(bp)) {
10513 bp->link_info.phy_retry = true;
10514 bp->link_info.phy_retry_expires =
10521 udp_tunnel_nic_reset_ntf(bp->dev);
10523 if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
10524 if (!static_key_enabled(&bnxt_xdp_locking_key))
10525 static_branch_enable(&bnxt_xdp_locking_key);
10526 } else if (static_key_enabled(&bnxt_xdp_locking_key)) {
10527 static_branch_disable(&bnxt_xdp_locking_key);
10529 set_bit(BNXT_STATE_OPEN, &bp->state);
10530 bnxt_enable_int(bp);
10531 /* Enable TX queues */
10532 bnxt_tx_enable(bp);
10533 mod_timer(&bp->timer, jiffies + bp->current_interval);
10534 /* Poll link status and check for SFP+ module status */
10535 mutex_lock(&bp->link_lock);
10536 bnxt_get_port_module_status(bp);
10537 mutex_unlock(&bp->link_lock);
10539 /* VF-reps may need to be re-opened after the PF is re-opened */
10541 bnxt_vf_reps_open(bp);
10542 bnxt_ptp_init_rtc(bp, true);
10543 bnxt_ptp_cfg_tstamp_filters(bp);
10550 bnxt_free_skbs(bp);
10552 bnxt_free_mem(bp, true);
10556 /* rtnl_lock held */
10557 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10561 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
10564 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
10566 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
10567 dev_close(bp->dev);
10572 /* rtnl_lock held, open the NIC half way by allocating all resources, but
10573 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
10576 int bnxt_half_open_nic(struct bnxt *bp)
10580 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
10581 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
10583 goto half_open_err;
10586 rc = bnxt_alloc_mem(bp, true);
10588 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
10589 goto half_open_err;
10591 set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
10592 rc = bnxt_init_nic(bp, true);
10594 clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
10595 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
10596 goto half_open_err;
10601 bnxt_free_skbs(bp);
10602 bnxt_free_mem(bp, true);
10603 dev_close(bp->dev);
10607 /* rtnl_lock held, this call can only be made after a previous successful
10608 * call to bnxt_half_open_nic().
10610 void bnxt_half_close_nic(struct bnxt *bp)
10612 bnxt_hwrm_resource_free(bp, false, true);
10613 bnxt_free_skbs(bp);
10614 bnxt_free_mem(bp, true);
10615 clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
10618 void bnxt_reenable_sriov(struct bnxt *bp)
10621 struct bnxt_pf_info *pf = &bp->pf;
10622 int n = pf->active_vfs;
10625 bnxt_cfg_hw_sriov(bp, &n, true);
10629 static int bnxt_open(struct net_device *dev)
10631 struct bnxt *bp = netdev_priv(dev);
10634 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
10635 rc = bnxt_reinit_after_abort(bp);
10638 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
10640 netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
10645 rc = bnxt_hwrm_if_change(bp, true);
10649 rc = __bnxt_open_nic(bp, true, true);
10651 bnxt_hwrm_if_change(bp, false);
10653 if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
10654 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
10655 bnxt_ulp_start(bp, 0);
10656 bnxt_reenable_sriov(bp);
10659 bnxt_hwmon_open(bp);
10665 static bool bnxt_drv_busy(struct bnxt *bp)
10667 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
10668 test_bit(BNXT_STATE_READ_STATS, &bp->state));
10671 static void bnxt_get_ring_stats(struct bnxt *bp,
10672 struct rtnl_link_stats64 *stats);
10674 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
10677 /* Close the VF-reps before closing PF */
10679 bnxt_vf_reps_close(bp);
10681 /* Change device state to avoid TX queue wake up's */
10682 bnxt_tx_disable(bp);
10684 clear_bit(BNXT_STATE_OPEN, &bp->state);
10685 smp_mb__after_atomic();
10686 while (bnxt_drv_busy(bp))
10689 /* Flush rings and disable interrupts */
10690 bnxt_shutdown_nic(bp, irq_re_init);
10692 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
10694 bnxt_debug_dev_exit(bp);
10695 bnxt_disable_napi(bp);
10696 del_timer_sync(&bp->timer);
10697 bnxt_free_skbs(bp);
10699 /* Save ring stats before shutdown */
10700 if (bp->bnapi && irq_re_init)
10701 bnxt_get_ring_stats(bp, &bp->net_stats_prev);
10706 bnxt_free_mem(bp, irq_re_init);
10709 int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10713 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
10714 /* If we get here, it means firmware reset is in progress
10715 * while we are trying to close. We can safely proceed with
10716 * the close because we are holding rtnl_lock(). Some firmware
10717 * messages may fail as we proceed to close. We set the
10718 * ABORT_ERR flag here so that the FW reset thread will later
10719 * abort when it gets the rtnl_lock() and sees the flag.
10721 netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
10722 set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10725 #ifdef CONFIG_BNXT_SRIOV
10726 if (bp->sriov_cfg) {
10727 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
10729 BNXT_SRIOV_CFG_WAIT_TMO);
10731 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
10734 __bnxt_close_nic(bp, irq_re_init, link_re_init);
10738 static int bnxt_close(struct net_device *dev)
10740 struct bnxt *bp = netdev_priv(dev);
10742 bnxt_hwmon_close(bp);
10743 bnxt_close_nic(bp, true, true);
10744 bnxt_hwrm_shutdown_link(bp);
10745 bnxt_hwrm_if_change(bp, false);
10749 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
10752 struct hwrm_port_phy_mdio_read_output *resp;
10753 struct hwrm_port_phy_mdio_read_input *req;
10756 if (bp->hwrm_spec_code < 0x10a00)
10757 return -EOPNOTSUPP;
10759 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
10763 req->port_id = cpu_to_le16(bp->pf.port_id);
10764 req->phy_addr = phy_addr;
10765 req->reg_addr = cpu_to_le16(reg & 0x1f);
10766 if (mdio_phy_id_is_c45(phy_addr)) {
10767 req->cl45_mdio = 1;
10768 req->phy_addr = mdio_phy_id_prtad(phy_addr);
10769 req->dev_addr = mdio_phy_id_devad(phy_addr);
10770 req->reg_addr = cpu_to_le16(reg);
10773 resp = hwrm_req_hold(bp, req);
10774 rc = hwrm_req_send(bp, req);
10776 *val = le16_to_cpu(resp->reg_data);
10777 hwrm_req_drop(bp, req);
10781 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
10784 struct hwrm_port_phy_mdio_write_input *req;
10787 if (bp->hwrm_spec_code < 0x10a00)
10788 return -EOPNOTSUPP;
10790 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
10794 req->port_id = cpu_to_le16(bp->pf.port_id);
10795 req->phy_addr = phy_addr;
10796 req->reg_addr = cpu_to_le16(reg & 0x1f);
10797 if (mdio_phy_id_is_c45(phy_addr)) {
10798 req->cl45_mdio = 1;
10799 req->phy_addr = mdio_phy_id_prtad(phy_addr);
10800 req->dev_addr = mdio_phy_id_devad(phy_addr);
10801 req->reg_addr = cpu_to_le16(reg);
10803 req->reg_data = cpu_to_le16(val);
10805 return hwrm_req_send(bp, req);
10808 /* rtnl_lock held */
10809 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10811 struct mii_ioctl_data *mdio = if_mii(ifr);
10812 struct bnxt *bp = netdev_priv(dev);
10817 mdio->phy_id = bp->link_info.phy_addr;
10820 case SIOCGMIIREG: {
10821 u16 mii_regval = 0;
10823 if (!netif_running(dev))
10826 rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
10828 mdio->val_out = mii_regval;
10833 if (!netif_running(dev))
10836 return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
10839 case SIOCSHWTSTAMP:
10840 return bnxt_hwtstamp_set(dev, ifr);
10842 case SIOCGHWTSTAMP:
10843 return bnxt_hwtstamp_get(dev, ifr);
10849 return -EOPNOTSUPP;
10852 static void bnxt_get_ring_stats(struct bnxt *bp,
10853 struct rtnl_link_stats64 *stats)
10857 for (i = 0; i < bp->cp_nr_rings; i++) {
10858 struct bnxt_napi *bnapi = bp->bnapi[i];
10859 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
10860 u64 *sw = cpr->stats.sw_stats;
10862 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
10863 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
10864 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
10866 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
10867 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
10868 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
10870 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
10871 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
10872 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
10874 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
10875 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
10876 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
10878 stats->rx_missed_errors +=
10879 BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
10881 stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
10883 stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
10885 stats->rx_dropped +=
10886 cpr->sw_stats.rx.rx_netpoll_discards +
10887 cpr->sw_stats.rx.rx_oom_discards;
10891 static void bnxt_add_prev_stats(struct bnxt *bp,
10892 struct rtnl_link_stats64 *stats)
10894 struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
10896 stats->rx_packets += prev_stats->rx_packets;
10897 stats->tx_packets += prev_stats->tx_packets;
10898 stats->rx_bytes += prev_stats->rx_bytes;
10899 stats->tx_bytes += prev_stats->tx_bytes;
10900 stats->rx_missed_errors += prev_stats->rx_missed_errors;
10901 stats->multicast += prev_stats->multicast;
10902 stats->rx_dropped += prev_stats->rx_dropped;
10903 stats->tx_dropped += prev_stats->tx_dropped;
10907 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
10909 struct bnxt *bp = netdev_priv(dev);
10911 set_bit(BNXT_STATE_READ_STATS, &bp->state);
10912 /* Make sure bnxt_close_nic() sees that we are reading stats before
10913 * we check the BNXT_STATE_OPEN flag.
10915 smp_mb__after_atomic();
10916 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10917 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
10918 *stats = bp->net_stats_prev;
10922 bnxt_get_ring_stats(bp, stats);
10923 bnxt_add_prev_stats(bp, stats);
10925 if (bp->flags & BNXT_FLAG_PORT_STATS) {
10926 u64 *rx = bp->port_stats.sw_stats;
10927 u64 *tx = bp->port_stats.sw_stats +
10928 BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10930 stats->rx_crc_errors =
10931 BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
10932 stats->rx_frame_errors =
10933 BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
10934 stats->rx_length_errors =
10935 BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
10936 BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
10937 BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
10939 BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
10940 BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
10941 stats->collisions =
10942 BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
10943 stats->tx_fifo_errors =
10944 BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
10945 stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
10947 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
10950 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
10952 struct net_device *dev = bp->dev;
10953 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10954 struct netdev_hw_addr *ha;
10957 bool update = false;
10960 netdev_for_each_mc_addr(ha, dev) {
10961 if (mc_count >= BNXT_MAX_MC_ADDRS) {
10962 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10963 vnic->mc_list_count = 0;
10967 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
10968 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
10975 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
10977 if (mc_count != vnic->mc_list_count) {
10978 vnic->mc_list_count = mc_count;
10984 static bool bnxt_uc_list_updated(struct bnxt *bp)
10986 struct net_device *dev = bp->dev;
10987 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10988 struct netdev_hw_addr *ha;
10991 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
10994 netdev_for_each_uc_addr(ha, dev) {
10995 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
11003 static void bnxt_set_rx_mode(struct net_device *dev)
11005 struct bnxt *bp = netdev_priv(dev);
11006 struct bnxt_vnic_info *vnic;
11007 bool mc_update = false;
11011 if (!test_bit(BNXT_STATE_OPEN, &bp->state))
11014 vnic = &bp->vnic_info[0];
11015 mask = vnic->rx_mask;
11016 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
11017 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
11018 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
11019 CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
11021 if (dev->flags & IFF_PROMISC)
11022 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11024 uc_update = bnxt_uc_list_updated(bp);
11026 if (dev->flags & IFF_BROADCAST)
11027 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11028 if (dev->flags & IFF_ALLMULTI) {
11029 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11030 vnic->mc_list_count = 0;
11031 } else if (dev->flags & IFF_MULTICAST) {
11032 mc_update = bnxt_mc_list_updated(bp, &mask);
11035 if (mask != vnic->rx_mask || uc_update || mc_update) {
11036 vnic->rx_mask = mask;
11038 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
11039 bnxt_queue_sp_work(bp);
11043 static int bnxt_cfg_rx_mode(struct bnxt *bp)
11045 struct net_device *dev = bp->dev;
11046 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
11047 struct hwrm_cfa_l2_filter_free_input *req;
11048 struct netdev_hw_addr *ha;
11049 int i, off = 0, rc;
11052 netif_addr_lock_bh(dev);
11053 uc_update = bnxt_uc_list_updated(bp);
11054 netif_addr_unlock_bh(dev);
11059 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
11062 hwrm_req_hold(bp, req);
11063 for (i = 1; i < vnic->uc_filter_count; i++) {
11064 req->l2_filter_id = vnic->fw_l2_filter_id[i];
11066 rc = hwrm_req_send(bp, req);
11068 hwrm_req_drop(bp, req);
11070 vnic->uc_filter_count = 1;
11072 netif_addr_lock_bh(dev);
11073 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
11074 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11076 netdev_for_each_uc_addr(ha, dev) {
11077 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
11079 vnic->uc_filter_count++;
11082 netif_addr_unlock_bh(dev);
11084 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
11085 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
11087 if (BNXT_VF(bp) && rc == -ENODEV) {
11088 if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
11089 netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
11091 netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
11094 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
11096 vnic->uc_filter_count = i;
11100 if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
11101 netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
11104 if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
11105 !bnxt_promisc_ok(bp))
11106 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11107 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
11108 if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
11109 netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
11111 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
11112 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11113 vnic->mc_list_count = 0;
11114 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
11117 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
11123 static bool bnxt_can_reserve_rings(struct bnxt *bp)
11125 #ifdef CONFIG_BNXT_SRIOV
11126 if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
11127 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11129 /* No minimum rings were provisioned by the PF. Don't
11130 * reserve rings by default when device is down.
11132 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
11135 if (!netif_running(bp->dev))
11142 /* If the chip and firmware supports RFS */
11143 static bool bnxt_rfs_supported(struct bnxt *bp)
11145 if (bp->flags & BNXT_FLAG_CHIP_P5) {
11146 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
11150 /* 212 firmware is broken for aRFS */
11151 if (BNXT_FW_MAJ(bp) == 212)
11153 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
11155 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
11160 /* If runtime conditions support RFS */
11161 static bool bnxt_rfs_capable(struct bnxt *bp)
11163 #ifdef CONFIG_RFS_ACCEL
11164 int vnics, max_vnics, max_rss_ctxs;
11166 if (bp->flags & BNXT_FLAG_CHIP_P5)
11167 return bnxt_rfs_supported(bp);
11168 if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
11171 vnics = 1 + bp->rx_nr_rings;
11172 max_vnics = bnxt_get_max_func_vnics(bp);
11173 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
11175 /* RSS contexts not a limiting factor */
11176 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
11177 max_rss_ctxs = max_vnics;
11178 if (vnics > max_vnics || vnics > max_rss_ctxs) {
11179 if (bp->rx_nr_rings > 1)
11180 netdev_warn(bp->dev,
11181 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
11182 min(max_rss_ctxs - 1, max_vnics - 1));
11186 if (!BNXT_NEW_RM(bp))
11189 if (vnics == bp->hw_resc.resv_vnics)
11192 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, vnics);
11193 if (vnics <= bp->hw_resc.resv_vnics)
11196 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
11197 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, 1);
11204 static netdev_features_t bnxt_fix_features(struct net_device *dev,
11205 netdev_features_t features)
11207 struct bnxt *bp = netdev_priv(dev);
11208 netdev_features_t vlan_features;
11210 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
11211 features &= ~NETIF_F_NTUPLE;
11213 if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
11214 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
11216 if (!(features & NETIF_F_GRO))
11217 features &= ~NETIF_F_GRO_HW;
11219 if (features & NETIF_F_GRO_HW)
11220 features &= ~NETIF_F_LRO;
11222 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
11223 * turned on or off together.
11225 vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
11226 if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
11227 if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
11228 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
11229 else if (vlan_features)
11230 features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
11232 #ifdef CONFIG_BNXT_SRIOV
11233 if (BNXT_VF(bp) && bp->vf.vlan)
11234 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
11239 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
11241 struct bnxt *bp = netdev_priv(dev);
11242 u32 flags = bp->flags;
11245 bool re_init = false;
11246 bool update_tpa = false;
11248 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
11249 if (features & NETIF_F_GRO_HW)
11250 flags |= BNXT_FLAG_GRO;
11251 else if (features & NETIF_F_LRO)
11252 flags |= BNXT_FLAG_LRO;
11254 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
11255 flags &= ~BNXT_FLAG_TPA;
11257 if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
11258 flags |= BNXT_FLAG_STRIP_VLAN;
11260 if (features & NETIF_F_NTUPLE)
11261 flags |= BNXT_FLAG_RFS;
11263 changes = flags ^ bp->flags;
11264 if (changes & BNXT_FLAG_TPA) {
11266 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
11267 (flags & BNXT_FLAG_TPA) == 0 ||
11268 (bp->flags & BNXT_FLAG_CHIP_P5))
11272 if (changes & ~BNXT_FLAG_TPA)
11275 if (flags != bp->flags) {
11276 u32 old_flags = bp->flags;
11278 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
11281 bnxt_set_ring_params(bp);
11286 bnxt_close_nic(bp, false, false);
11289 bnxt_set_ring_params(bp);
11291 return bnxt_open_nic(bp, false, false);
11295 rc = bnxt_set_tpa(bp,
11296 (flags & BNXT_FLAG_TPA) ?
11299 bp->flags = old_flags;
11305 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
11308 struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
11309 struct hop_jumbo_hdr *jhdr;
11314 /* Check that there are at most 2 IPv6 extension headers, no
11315 * fragment header, and each is <= 64 bytes.
11317 start = nw_off + sizeof(*ip6h);
11318 nexthdr = &ip6h->nexthdr;
11319 while (ipv6_ext_hdr(*nexthdr)) {
11320 struct ipv6_opt_hdr *hp;
11323 if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
11324 *nexthdr == NEXTHDR_FRAGMENT)
11326 hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
11327 skb_headlen(skb), NULL);
11330 if (*nexthdr == NEXTHDR_AUTH)
11331 hdrlen = ipv6_authlen(hp);
11333 hdrlen = ipv6_optlen(hp);
11338 /* The ext header may be a hop-by-hop header inserted for
11339 * big TCP purposes. This will be removed before sending
11340 * from NIC, so do not count it.
11342 if (*nexthdr == NEXTHDR_HOP) {
11343 if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
11344 goto increment_hdr;
11346 jhdr = (struct hop_jumbo_hdr *)hp;
11347 if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
11348 jhdr->nexthdr != IPPROTO_TCP)
11349 goto increment_hdr;
11356 nexthdr = &hp->nexthdr;
11360 /* Caller will check inner protocol */
11361 if (skb->encapsulation) {
11367 /* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
11368 return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
11371 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
11372 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
11374 struct udphdr *uh = udp_hdr(skb);
11375 __be16 udp_port = uh->dest;
11377 if (udp_port != bp->vxlan_port && udp_port != bp->nge_port)
11379 if (skb->inner_protocol_type == ENCAP_TYPE_ETHER) {
11380 struct ethhdr *eh = inner_eth_hdr(skb);
11382 switch (eh->h_proto) {
11383 case htons(ETH_P_IP):
11385 case htons(ETH_P_IPV6):
11386 return bnxt_exthdr_check(bp, skb,
11387 skb_inner_network_offset(skb),
11394 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
11396 switch (l4_proto) {
11398 return bnxt_udp_tunl_check(bp, skb);
11401 case IPPROTO_GRE: {
11402 switch (skb->inner_protocol) {
11405 case htons(ETH_P_IP):
11407 case htons(ETH_P_IPV6):
11412 /* Check ext headers of inner ipv6 */
11413 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
11419 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
11420 struct net_device *dev,
11421 netdev_features_t features)
11423 struct bnxt *bp = netdev_priv(dev);
11426 features = vlan_features_check(skb, features);
11427 switch (vlan_get_protocol(skb)) {
11428 case htons(ETH_P_IP):
11429 if (!skb->encapsulation)
11431 l4_proto = &ip_hdr(skb)->protocol;
11432 if (bnxt_tunl_check(bp, skb, *l4_proto))
11435 case htons(ETH_P_IPV6):
11436 if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
11439 if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
11443 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
11446 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
11449 struct hwrm_dbg_read_direct_output *resp;
11450 struct hwrm_dbg_read_direct_input *req;
11451 __le32 *dbg_reg_buf;
11452 dma_addr_t mapping;
11455 rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
11459 dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
11461 if (!dbg_reg_buf) {
11463 goto dbg_rd_reg_exit;
11466 req->host_dest_addr = cpu_to_le64(mapping);
11468 resp = hwrm_req_hold(bp, req);
11469 req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
11470 req->read_len32 = cpu_to_le32(num_words);
11472 rc = hwrm_req_send(bp, req);
11473 if (rc || resp->error_code) {
11475 goto dbg_rd_reg_exit;
11477 for (i = 0; i < num_words; i++)
11478 reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
11481 hwrm_req_drop(bp, req);
11485 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
11486 u32 ring_id, u32 *prod, u32 *cons)
11488 struct hwrm_dbg_ring_info_get_output *resp;
11489 struct hwrm_dbg_ring_info_get_input *req;
11492 rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
11496 req->ring_type = ring_type;
11497 req->fw_ring_id = cpu_to_le32(ring_id);
11498 resp = hwrm_req_hold(bp, req);
11499 rc = hwrm_req_send(bp, req);
11501 *prod = le32_to_cpu(resp->producer_index);
11502 *cons = le32_to_cpu(resp->consumer_index);
11504 hwrm_req_drop(bp, req);
11508 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
11510 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
11511 int i = bnapi->index;
11516 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
11517 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
11521 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
11523 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
11524 int i = bnapi->index;
11529 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",
11530 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
11531 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
11532 rxr->rx_sw_agg_prod);
11535 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
11537 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
11538 int i = bnapi->index;
11540 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
11541 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
11544 static void bnxt_dbg_dump_states(struct bnxt *bp)
11547 struct bnxt_napi *bnapi;
11549 for (i = 0; i < bp->cp_nr_rings; i++) {
11550 bnapi = bp->bnapi[i];
11551 if (netif_msg_drv(bp)) {
11552 bnxt_dump_tx_sw_state(bnapi);
11553 bnxt_dump_rx_sw_state(bnapi);
11554 bnxt_dump_cp_sw_state(bnapi);
11559 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
11561 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
11562 struct hwrm_ring_reset_input *req;
11563 struct bnxt_napi *bnapi = rxr->bnapi;
11564 struct bnxt_cp_ring_info *cpr;
11568 rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
11572 cpr = &bnapi->cp_ring;
11573 cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
11574 req->cmpl_ring = cpu_to_le16(cp_ring_id);
11575 req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
11576 req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
11577 return hwrm_req_send_silent(bp, req);
11580 static void bnxt_reset_task(struct bnxt *bp, bool silent)
11583 bnxt_dbg_dump_states(bp);
11584 if (netif_running(bp->dev)) {
11588 bnxt_close_nic(bp, false, false);
11589 bnxt_open_nic(bp, false, false);
11592 bnxt_close_nic(bp, true, false);
11593 rc = bnxt_open_nic(bp, true, false);
11594 bnxt_ulp_start(bp, rc);
11599 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
11601 struct bnxt *bp = netdev_priv(dev);
11603 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
11604 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
11605 bnxt_queue_sp_work(bp);
11608 static void bnxt_fw_health_check(struct bnxt *bp)
11610 struct bnxt_fw_health *fw_health = bp->fw_health;
11611 struct pci_dev *pdev = bp->pdev;
11614 if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11617 /* Make sure it is enabled before checking the tmr_counter. */
11619 if (fw_health->tmr_counter) {
11620 fw_health->tmr_counter--;
11624 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
11625 if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
11626 fw_health->arrests++;
11630 fw_health->last_fw_heartbeat = val;
11632 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
11633 if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
11634 fw_health->discoveries++;
11638 fw_health->tmr_counter = fw_health->tmr_multiplier;
11642 set_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event);
11643 bnxt_queue_sp_work(bp);
11646 static void bnxt_timer(struct timer_list *t)
11648 struct bnxt *bp = from_timer(bp, t, timer);
11649 struct net_device *dev = bp->dev;
11651 if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
11654 if (atomic_read(&bp->intr_sem) != 0)
11655 goto bnxt_restart_timer;
11657 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
11658 bnxt_fw_health_check(bp);
11660 if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks) {
11661 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
11662 bnxt_queue_sp_work(bp);
11665 if (bnxt_tc_flower_enabled(bp)) {
11666 set_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event);
11667 bnxt_queue_sp_work(bp);
11670 #ifdef CONFIG_RFS_ACCEL
11671 if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count) {
11672 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
11673 bnxt_queue_sp_work(bp);
11675 #endif /*CONFIG_RFS_ACCEL*/
11677 if (bp->link_info.phy_retry) {
11678 if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
11679 bp->link_info.phy_retry = false;
11680 netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
11682 set_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event);
11683 bnxt_queue_sp_work(bp);
11687 if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) {
11688 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
11689 bnxt_queue_sp_work(bp);
11692 if ((bp->flags & BNXT_FLAG_CHIP_P5) && !bp->chip_rev &&
11693 netif_carrier_ok(dev)) {
11694 set_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event);
11695 bnxt_queue_sp_work(bp);
11697 bnxt_restart_timer:
11698 mod_timer(&bp->timer, jiffies + bp->current_interval);
11701 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
11703 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
11704 * set. If the device is being closed, bnxt_close() may be holding
11705 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
11706 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
11708 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11712 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
11714 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11718 /* Only called from bnxt_sp_task() */
11719 static void bnxt_reset(struct bnxt *bp, bool silent)
11721 bnxt_rtnl_lock_sp(bp);
11722 if (test_bit(BNXT_STATE_OPEN, &bp->state))
11723 bnxt_reset_task(bp, silent);
11724 bnxt_rtnl_unlock_sp(bp);
11727 /* Only called from bnxt_sp_task() */
11728 static void bnxt_rx_ring_reset(struct bnxt *bp)
11732 bnxt_rtnl_lock_sp(bp);
11733 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
11734 bnxt_rtnl_unlock_sp(bp);
11737 /* Disable and flush TPA before resetting the RX ring */
11738 if (bp->flags & BNXT_FLAG_TPA)
11739 bnxt_set_tpa(bp, false);
11740 for (i = 0; i < bp->rx_nr_rings; i++) {
11741 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
11742 struct bnxt_cp_ring_info *cpr;
11745 if (!rxr->bnapi->in_reset)
11748 rc = bnxt_hwrm_rx_ring_reset(bp, i);
11750 if (rc == -EINVAL || rc == -EOPNOTSUPP)
11751 netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
11753 netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
11755 bnxt_reset_task(bp, true);
11758 bnxt_free_one_rx_ring_skbs(bp, i);
11760 rxr->rx_agg_prod = 0;
11761 rxr->rx_sw_agg_prod = 0;
11762 rxr->rx_next_cons = 0;
11763 rxr->bnapi->in_reset = false;
11764 bnxt_alloc_one_rx_ring(bp, i);
11765 cpr = &rxr->bnapi->cp_ring;
11766 cpr->sw_stats.rx.rx_resets++;
11767 if (bp->flags & BNXT_FLAG_AGG_RINGS)
11768 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
11769 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
11771 if (bp->flags & BNXT_FLAG_TPA)
11772 bnxt_set_tpa(bp, true);
11773 bnxt_rtnl_unlock_sp(bp);
11776 static void bnxt_fw_reset_close(struct bnxt *bp)
11779 /* When firmware is in fatal state, quiesce device and disable
11780 * bus master to prevent any potential bad DMAs before freeing
11783 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
11786 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
11788 bp->fw_reset_min_dsecs = 0;
11789 bnxt_tx_disable(bp);
11790 bnxt_disable_napi(bp);
11791 bnxt_disable_int_sync(bp);
11793 bnxt_clear_int_mode(bp);
11794 pci_disable_device(bp->pdev);
11796 __bnxt_close_nic(bp, true, false);
11797 bnxt_vf_reps_free(bp);
11798 bnxt_clear_int_mode(bp);
11799 bnxt_hwrm_func_drv_unrgtr(bp);
11800 if (pci_is_enabled(bp->pdev))
11801 pci_disable_device(bp->pdev);
11802 bnxt_free_ctx_mem(bp);
11807 static bool is_bnxt_fw_ok(struct bnxt *bp)
11809 struct bnxt_fw_health *fw_health = bp->fw_health;
11810 bool no_heartbeat = false, has_reset = false;
11813 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
11814 if (val == fw_health->last_fw_heartbeat)
11815 no_heartbeat = true;
11817 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
11818 if (val != fw_health->last_fw_reset_cnt)
11821 if (!no_heartbeat && has_reset)
11827 /* rtnl_lock is acquired before calling this function */
11828 static void bnxt_force_fw_reset(struct bnxt *bp)
11830 struct bnxt_fw_health *fw_health = bp->fw_health;
11831 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
11834 if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
11835 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
11839 spin_lock_bh(&ptp->ptp_lock);
11840 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11841 spin_unlock_bh(&ptp->ptp_lock);
11843 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11845 bnxt_fw_reset_close(bp);
11846 wait_dsecs = fw_health->master_func_wait_dsecs;
11847 if (fw_health->primary) {
11848 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
11850 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
11852 bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
11853 wait_dsecs = fw_health->normal_func_wait_dsecs;
11854 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11857 bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
11858 bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
11859 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
11862 void bnxt_fw_exception(struct bnxt *bp)
11864 netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
11865 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
11866 bnxt_rtnl_lock_sp(bp);
11867 bnxt_force_fw_reset(bp);
11868 bnxt_rtnl_unlock_sp(bp);
11871 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
11874 static int bnxt_get_registered_vfs(struct bnxt *bp)
11876 #ifdef CONFIG_BNXT_SRIOV
11882 rc = bnxt_hwrm_func_qcfg(bp);
11884 netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
11887 if (bp->pf.registered_vfs)
11888 return bp->pf.registered_vfs;
11895 void bnxt_fw_reset(struct bnxt *bp)
11897 bnxt_rtnl_lock_sp(bp);
11898 if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
11899 !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
11900 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
11904 spin_lock_bh(&ptp->ptp_lock);
11905 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11906 spin_unlock_bh(&ptp->ptp_lock);
11908 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11910 if (bp->pf.active_vfs &&
11911 !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
11912 n = bnxt_get_registered_vfs(bp);
11914 netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
11916 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11917 dev_close(bp->dev);
11918 goto fw_reset_exit;
11919 } else if (n > 0) {
11920 u16 vf_tmo_dsecs = n * 10;
11922 if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
11923 bp->fw_reset_max_dsecs = vf_tmo_dsecs;
11924 bp->fw_reset_state =
11925 BNXT_FW_RESET_STATE_POLL_VF;
11926 bnxt_queue_fw_reset_work(bp, HZ / 10);
11927 goto fw_reset_exit;
11929 bnxt_fw_reset_close(bp);
11930 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
11931 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
11934 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11935 tmo = bp->fw_reset_min_dsecs * HZ / 10;
11937 bnxt_queue_fw_reset_work(bp, tmo);
11940 bnxt_rtnl_unlock_sp(bp);
11943 static void bnxt_chk_missed_irq(struct bnxt *bp)
11947 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
11950 for (i = 0; i < bp->cp_nr_rings; i++) {
11951 struct bnxt_napi *bnapi = bp->bnapi[i];
11952 struct bnxt_cp_ring_info *cpr;
11959 cpr = &bnapi->cp_ring;
11960 for (j = 0; j < 2; j++) {
11961 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
11964 if (!cpr2 || cpr2->has_more_work ||
11965 !bnxt_has_work(bp, cpr2))
11968 if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
11969 cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
11972 fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
11973 bnxt_dbg_hwrm_ring_info_get(bp,
11974 DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
11975 fw_ring_id, &val[0], &val[1]);
11976 cpr->sw_stats.cmn.missed_irqs++;
11981 static void bnxt_cfg_ntp_filters(struct bnxt *);
11983 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
11985 struct bnxt_link_info *link_info = &bp->link_info;
11987 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
11988 link_info->autoneg = BNXT_AUTONEG_SPEED;
11989 if (bp->hwrm_spec_code >= 0x10201) {
11990 if (link_info->auto_pause_setting &
11991 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
11992 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
11994 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
11996 link_info->advertising = link_info->auto_link_speeds;
11997 link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
11999 link_info->req_link_speed = link_info->force_link_speed;
12000 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
12001 if (link_info->force_pam4_link_speed) {
12002 link_info->req_link_speed =
12003 link_info->force_pam4_link_speed;
12004 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
12006 link_info->req_duplex = link_info->duplex_setting;
12008 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
12009 link_info->req_flow_ctrl =
12010 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
12012 link_info->req_flow_ctrl = link_info->force_pause_setting;
12015 static void bnxt_fw_echo_reply(struct bnxt *bp)
12017 struct bnxt_fw_health *fw_health = bp->fw_health;
12018 struct hwrm_func_echo_response_input *req;
12021 rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
12024 req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
12025 req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
12026 hwrm_req_send(bp, req);
12029 static void bnxt_sp_task(struct work_struct *work)
12031 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
12033 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
12034 smp_mb__after_atomic();
12035 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12036 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
12040 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
12041 bnxt_cfg_rx_mode(bp);
12043 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
12044 bnxt_cfg_ntp_filters(bp);
12045 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
12046 bnxt_hwrm_exec_fwd_req(bp);
12047 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
12048 bnxt_hwrm_port_qstats(bp, 0);
12049 bnxt_hwrm_port_qstats_ext(bp, 0);
12050 bnxt_accumulate_all_stats(bp);
12053 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
12056 mutex_lock(&bp->link_lock);
12057 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
12059 bnxt_hwrm_phy_qcaps(bp);
12061 rc = bnxt_update_link(bp, true);
12063 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
12066 if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
12068 bnxt_init_ethtool_link_settings(bp);
12069 mutex_unlock(&bp->link_lock);
12071 if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
12074 mutex_lock(&bp->link_lock);
12075 rc = bnxt_update_phy_setting(bp);
12076 mutex_unlock(&bp->link_lock);
12078 netdev_warn(bp->dev, "update phy settings retry failed\n");
12080 bp->link_info.phy_retry = false;
12081 netdev_info(bp->dev, "update phy settings retry succeeded\n");
12084 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
12085 mutex_lock(&bp->link_lock);
12086 bnxt_get_port_module_status(bp);
12087 mutex_unlock(&bp->link_lock);
12090 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
12091 bnxt_tc_flow_stats_work(bp);
12093 if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
12094 bnxt_chk_missed_irq(bp);
12096 if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
12097 bnxt_fw_echo_reply(bp);
12099 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
12100 * must be the last functions to be called before exiting.
12102 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
12103 bnxt_reset(bp, false);
12105 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
12106 bnxt_reset(bp, true);
12108 if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
12109 bnxt_rx_ring_reset(bp);
12111 if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
12112 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
12113 test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
12114 bnxt_devlink_health_fw_report(bp);
12119 if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
12120 if (!is_bnxt_fw_ok(bp))
12121 bnxt_devlink_health_fw_report(bp);
12124 smp_mb__before_atomic();
12125 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
12128 /* Under rtnl_lock */
12129 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
12132 int max_rx, max_tx, tx_sets = 1;
12133 int tx_rings_needed, stats;
12140 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
12147 tx_rings_needed = tx * tx_sets + tx_xdp;
12148 if (max_tx < tx_rings_needed)
12152 if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
12155 if (bp->flags & BNXT_FLAG_AGG_RINGS)
12157 cp = sh ? max_t(int, tx_rings_needed, rx) : tx_rings_needed + rx;
12159 if (BNXT_NEW_RM(bp)) {
12160 cp += bnxt_get_ulp_msix_num(bp);
12161 stats += bnxt_get_ulp_stat_ctxs(bp);
12163 return bnxt_hwrm_check_rings(bp, tx_rings_needed, rx_rings, rx, cp,
12167 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
12170 pci_iounmap(pdev, bp->bar2);
12175 pci_iounmap(pdev, bp->bar1);
12180 pci_iounmap(pdev, bp->bar0);
12185 static void bnxt_cleanup_pci(struct bnxt *bp)
12187 bnxt_unmap_bars(bp, bp->pdev);
12188 pci_release_regions(bp->pdev);
12189 if (pci_is_enabled(bp->pdev))
12190 pci_disable_device(bp->pdev);
12193 static void bnxt_init_dflt_coal(struct bnxt *bp)
12195 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
12196 struct bnxt_coal *coal;
12199 if (coal_cap->cmpl_params &
12200 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
12201 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
12203 /* Tick values in micro seconds.
12204 * 1 coal_buf x bufs_per_record = 1 completion record.
12206 coal = &bp->rx_coal;
12207 coal->coal_ticks = 10;
12208 coal->coal_bufs = 30;
12209 coal->coal_ticks_irq = 1;
12210 coal->coal_bufs_irq = 2;
12211 coal->idle_thresh = 50;
12212 coal->bufs_per_record = 2;
12213 coal->budget = 64; /* NAPI budget */
12214 coal->flags = flags;
12216 coal = &bp->tx_coal;
12217 coal->coal_ticks = 28;
12218 coal->coal_bufs = 30;
12219 coal->coal_ticks_irq = 2;
12220 coal->coal_bufs_irq = 2;
12221 coal->bufs_per_record = 1;
12222 coal->flags = flags;
12224 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
12227 static int bnxt_fw_init_one_p1(struct bnxt *bp)
12232 rc = bnxt_hwrm_ver_get(bp);
12233 bnxt_try_map_fw_health_reg(bp);
12235 rc = bnxt_try_recover_fw(bp);
12238 rc = bnxt_hwrm_ver_get(bp);
12243 bnxt_nvm_cfg_ver_get(bp);
12245 rc = bnxt_hwrm_func_reset(bp);
12249 bnxt_hwrm_fw_set_time(bp);
12253 static int bnxt_fw_init_one_p2(struct bnxt *bp)
12257 /* Get the MAX capabilities for this function */
12258 rc = bnxt_hwrm_func_qcaps(bp);
12260 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
12265 rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
12267 netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
12270 if (bnxt_alloc_fw_health(bp)) {
12271 netdev_warn(bp->dev, "no memory for firmware error recovery\n");
12273 rc = bnxt_hwrm_error_recovery_qcfg(bp);
12275 netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
12279 rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
12283 bnxt_hwrm_func_qcfg(bp);
12284 bnxt_hwrm_vnic_qcaps(bp);
12285 bnxt_hwrm_port_led_qcaps(bp);
12286 bnxt_ethtool_init(bp);
12287 if (bp->fw_cap & BNXT_FW_CAP_PTP)
12288 __bnxt_hwrm_ptp_qcfg(bp);
12293 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
12295 bp->flags &= ~BNXT_FLAG_UDP_RSS_CAP;
12296 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
12297 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
12298 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
12299 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
12300 if (bp->fw_cap & BNXT_FW_CAP_RSS_HASH_TYPE_DELTA)
12301 bp->rss_hash_delta = bp->rss_hash_cfg;
12302 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
12303 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
12304 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
12305 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
12309 static void bnxt_set_dflt_rfs(struct bnxt *bp)
12311 struct net_device *dev = bp->dev;
12313 dev->hw_features &= ~NETIF_F_NTUPLE;
12314 dev->features &= ~NETIF_F_NTUPLE;
12315 bp->flags &= ~BNXT_FLAG_RFS;
12316 if (bnxt_rfs_supported(bp)) {
12317 dev->hw_features |= NETIF_F_NTUPLE;
12318 if (bnxt_rfs_capable(bp)) {
12319 bp->flags |= BNXT_FLAG_RFS;
12320 dev->features |= NETIF_F_NTUPLE;
12325 static void bnxt_fw_init_one_p3(struct bnxt *bp)
12327 struct pci_dev *pdev = bp->pdev;
12329 bnxt_set_dflt_rss_hash_type(bp);
12330 bnxt_set_dflt_rfs(bp);
12332 bnxt_get_wol_settings(bp);
12333 if (bp->flags & BNXT_FLAG_WOL_CAP)
12334 device_set_wakeup_enable(&pdev->dev, bp->wol);
12336 device_set_wakeup_capable(&pdev->dev, false);
12338 bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
12339 bnxt_hwrm_coal_params_qcaps(bp);
12342 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
12344 int bnxt_fw_init_one(struct bnxt *bp)
12348 rc = bnxt_fw_init_one_p1(bp);
12350 netdev_err(bp->dev, "Firmware init phase 1 failed\n");
12353 rc = bnxt_fw_init_one_p2(bp);
12355 netdev_err(bp->dev, "Firmware init phase 2 failed\n");
12358 rc = bnxt_probe_phy(bp, false);
12361 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
12365 bnxt_fw_init_one_p3(bp);
12369 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
12371 struct bnxt_fw_health *fw_health = bp->fw_health;
12372 u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
12373 u32 val = fw_health->fw_reset_seq_vals[reg_idx];
12374 u32 reg_type, reg_off, delay_msecs;
12376 delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
12377 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
12378 reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
12379 switch (reg_type) {
12380 case BNXT_FW_HEALTH_REG_TYPE_CFG:
12381 pci_write_config_dword(bp->pdev, reg_off, val);
12383 case BNXT_FW_HEALTH_REG_TYPE_GRC:
12384 writel(reg_off & BNXT_GRC_BASE_MASK,
12385 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
12386 reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
12388 case BNXT_FW_HEALTH_REG_TYPE_BAR0:
12389 writel(val, bp->bar0 + reg_off);
12391 case BNXT_FW_HEALTH_REG_TYPE_BAR1:
12392 writel(val, bp->bar1 + reg_off);
12396 pci_read_config_dword(bp->pdev, 0, &val);
12397 msleep(delay_msecs);
12401 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
12403 struct hwrm_func_qcfg_output *resp;
12404 struct hwrm_func_qcfg_input *req;
12405 bool result = true; /* firmware will enforce if unknown */
12407 if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
12410 if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
12413 req->fid = cpu_to_le16(0xffff);
12414 resp = hwrm_req_hold(bp, req);
12415 if (!hwrm_req_send(bp, req))
12416 result = !!(le16_to_cpu(resp->flags) &
12417 FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
12418 hwrm_req_drop(bp, req);
12422 static void bnxt_reset_all(struct bnxt *bp)
12424 struct bnxt_fw_health *fw_health = bp->fw_health;
12427 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
12428 bnxt_fw_reset_via_optee(bp);
12429 bp->fw_reset_timestamp = jiffies;
12433 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
12434 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
12435 bnxt_fw_reset_writel(bp, i);
12436 } else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
12437 struct hwrm_fw_reset_input *req;
12439 rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
12441 req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
12442 req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
12443 req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
12444 req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
12445 rc = hwrm_req_send(bp, req);
12448 netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
12450 bp->fw_reset_timestamp = jiffies;
12453 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
12455 return time_after(jiffies, bp->fw_reset_timestamp +
12456 (bp->fw_reset_max_dsecs * HZ / 10));
12459 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
12461 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12462 if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF) {
12463 bnxt_ulp_start(bp, rc);
12464 bnxt_dl_health_fw_status_update(bp, false);
12466 bp->fw_reset_state = 0;
12467 dev_close(bp->dev);
12470 static void bnxt_fw_reset_task(struct work_struct *work)
12472 struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
12475 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12476 netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
12480 switch (bp->fw_reset_state) {
12481 case BNXT_FW_RESET_STATE_POLL_VF: {
12482 int n = bnxt_get_registered_vfs(bp);
12486 netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
12487 n, jiffies_to_msecs(jiffies -
12488 bp->fw_reset_timestamp));
12489 goto fw_reset_abort;
12490 } else if (n > 0) {
12491 if (bnxt_fw_reset_timeout(bp)) {
12492 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12493 bp->fw_reset_state = 0;
12494 netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
12498 bnxt_queue_fw_reset_work(bp, HZ / 10);
12501 bp->fw_reset_timestamp = jiffies;
12503 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12504 bnxt_fw_reset_abort(bp, rc);
12508 bnxt_fw_reset_close(bp);
12509 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
12510 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
12513 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
12514 tmo = bp->fw_reset_min_dsecs * HZ / 10;
12517 bnxt_queue_fw_reset_work(bp, tmo);
12520 case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
12523 val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12524 if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
12525 !bnxt_fw_reset_timeout(bp)) {
12526 bnxt_queue_fw_reset_work(bp, HZ / 5);
12530 if (!bp->fw_health->primary) {
12531 u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
12533 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
12534 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
12537 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
12540 case BNXT_FW_RESET_STATE_RESET_FW:
12541 bnxt_reset_all(bp);
12542 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
12543 bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
12545 case BNXT_FW_RESET_STATE_ENABLE_DEV:
12546 bnxt_inv_fw_health_reg(bp);
12547 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
12548 !bp->fw_reset_min_dsecs) {
12551 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
12552 if (val == 0xffff) {
12553 if (bnxt_fw_reset_timeout(bp)) {
12554 netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
12556 goto fw_reset_abort;
12558 bnxt_queue_fw_reset_work(bp, HZ / 1000);
12562 clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
12563 clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
12564 if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
12565 !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
12566 bnxt_dl_remote_reload(bp);
12567 if (pci_enable_device(bp->pdev)) {
12568 netdev_err(bp->dev, "Cannot re-enable PCI device\n");
12570 goto fw_reset_abort;
12572 pci_set_master(bp->pdev);
12573 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
12575 case BNXT_FW_RESET_STATE_POLL_FW:
12576 bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
12577 rc = bnxt_hwrm_poll(bp);
12579 if (bnxt_fw_reset_timeout(bp)) {
12580 netdev_err(bp->dev, "Firmware reset aborted\n");
12581 goto fw_reset_abort_status;
12583 bnxt_queue_fw_reset_work(bp, HZ / 5);
12586 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
12587 bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
12589 case BNXT_FW_RESET_STATE_OPENING:
12590 while (!rtnl_trylock()) {
12591 bnxt_queue_fw_reset_work(bp, HZ / 10);
12594 rc = bnxt_open(bp->dev);
12596 netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
12597 bnxt_fw_reset_abort(bp, rc);
12602 if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
12603 bp->fw_health->enabled) {
12604 bp->fw_health->last_fw_reset_cnt =
12605 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
12607 bp->fw_reset_state = 0;
12608 /* Make sure fw_reset_state is 0 before clearing the flag */
12609 smp_mb__before_atomic();
12610 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12611 bnxt_ulp_start(bp, 0);
12612 bnxt_reenable_sriov(bp);
12613 bnxt_vf_reps_alloc(bp);
12614 bnxt_vf_reps_open(bp);
12615 bnxt_ptp_reapply_pps(bp);
12616 clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
12617 if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
12618 bnxt_dl_health_fw_recovery_done(bp);
12619 bnxt_dl_health_fw_status_update(bp, true);
12626 fw_reset_abort_status:
12627 if (bp->fw_health->status_reliable ||
12628 (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
12629 u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12631 netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
12635 bnxt_fw_reset_abort(bp, rc);
12639 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
12642 struct bnxt *bp = netdev_priv(dev);
12644 SET_NETDEV_DEV(dev, &pdev->dev);
12646 /* enable device (incl. PCI PM wakeup), and bus-mastering */
12647 rc = pci_enable_device(pdev);
12649 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
12653 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
12654 dev_err(&pdev->dev,
12655 "Cannot find PCI device base address, aborting\n");
12657 goto init_err_disable;
12660 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
12662 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
12663 goto init_err_disable;
12666 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
12667 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
12668 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
12670 goto init_err_release;
12673 pci_set_master(pdev);
12678 /* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
12679 * determines the BAR size.
12681 bp->bar0 = pci_ioremap_bar(pdev, 0);
12683 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
12685 goto init_err_release;
12688 bp->bar2 = pci_ioremap_bar(pdev, 4);
12690 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
12692 goto init_err_release;
12695 INIT_WORK(&bp->sp_task, bnxt_sp_task);
12696 INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
12698 spin_lock_init(&bp->ntp_fltr_lock);
12699 #if BITS_PER_LONG == 32
12700 spin_lock_init(&bp->db_lock);
12703 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
12704 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
12706 timer_setup(&bp->timer, bnxt_timer, 0);
12707 bp->current_interval = BNXT_TIMER_INTERVAL;
12709 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
12710 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
12712 clear_bit(BNXT_STATE_OPEN, &bp->state);
12716 bnxt_unmap_bars(bp, pdev);
12717 pci_release_regions(pdev);
12720 pci_disable_device(pdev);
12726 /* rtnl_lock held */
12727 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
12729 struct sockaddr *addr = p;
12730 struct bnxt *bp = netdev_priv(dev);
12733 if (!is_valid_ether_addr(addr->sa_data))
12734 return -EADDRNOTAVAIL;
12736 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
12739 rc = bnxt_approve_mac(bp, addr->sa_data, true);
12743 eth_hw_addr_set(dev, addr->sa_data);
12744 if (netif_running(dev)) {
12745 bnxt_close_nic(bp, false, false);
12746 rc = bnxt_open_nic(bp, false, false);
12752 /* rtnl_lock held */
12753 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
12755 struct bnxt *bp = netdev_priv(dev);
12757 if (netif_running(dev))
12758 bnxt_close_nic(bp, true, false);
12760 dev->mtu = new_mtu;
12761 bnxt_set_ring_params(bp);
12763 if (netif_running(dev))
12764 return bnxt_open_nic(bp, true, false);
12769 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
12771 struct bnxt *bp = netdev_priv(dev);
12775 if (tc > bp->max_tc) {
12776 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
12781 if (netdev_get_num_tc(dev) == tc)
12784 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
12787 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
12788 sh, tc, bp->tx_nr_rings_xdp);
12792 /* Needs to close the device and do hw resource re-allocations */
12793 if (netif_running(bp->dev))
12794 bnxt_close_nic(bp, true, false);
12797 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
12798 netdev_set_num_tc(dev, tc);
12800 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
12801 netdev_reset_tc(dev);
12803 bp->tx_nr_rings += bp->tx_nr_rings_xdp;
12804 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
12805 bp->tx_nr_rings + bp->rx_nr_rings;
12807 if (netif_running(bp->dev))
12808 return bnxt_open_nic(bp, true, false);
12813 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
12816 struct bnxt *bp = cb_priv;
12818 if (!bnxt_tc_flower_enabled(bp) ||
12819 !tc_cls_can_offload_and_chain0(bp->dev, type_data))
12820 return -EOPNOTSUPP;
12823 case TC_SETUP_CLSFLOWER:
12824 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
12826 return -EOPNOTSUPP;
12830 LIST_HEAD(bnxt_block_cb_list);
12832 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
12835 struct bnxt *bp = netdev_priv(dev);
12838 case TC_SETUP_BLOCK:
12839 return flow_block_cb_setup_simple(type_data,
12840 &bnxt_block_cb_list,
12841 bnxt_setup_tc_block_cb,
12843 case TC_SETUP_QDISC_MQPRIO: {
12844 struct tc_mqprio_qopt *mqprio = type_data;
12846 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
12848 return bnxt_setup_mq_tc(dev, mqprio->num_tc);
12851 return -EOPNOTSUPP;
12855 #ifdef CONFIG_RFS_ACCEL
12856 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
12857 struct bnxt_ntuple_filter *f2)
12859 struct flow_keys *keys1 = &f1->fkeys;
12860 struct flow_keys *keys2 = &f2->fkeys;
12862 if (keys1->basic.n_proto != keys2->basic.n_proto ||
12863 keys1->basic.ip_proto != keys2->basic.ip_proto)
12866 if (keys1->basic.n_proto == htons(ETH_P_IP)) {
12867 if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
12868 keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst)
12871 if (memcmp(&keys1->addrs.v6addrs.src, &keys2->addrs.v6addrs.src,
12872 sizeof(keys1->addrs.v6addrs.src)) ||
12873 memcmp(&keys1->addrs.v6addrs.dst, &keys2->addrs.v6addrs.dst,
12874 sizeof(keys1->addrs.v6addrs.dst)))
12878 if (keys1->ports.ports == keys2->ports.ports &&
12879 keys1->control.flags == keys2->control.flags &&
12880 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
12881 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
12887 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
12888 u16 rxq_index, u32 flow_id)
12890 struct bnxt *bp = netdev_priv(dev);
12891 struct bnxt_ntuple_filter *fltr, *new_fltr;
12892 struct flow_keys *fkeys;
12893 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
12894 int rc = 0, idx, bit_id, l2_idx = 0;
12895 struct hlist_head *head;
12898 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
12899 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
12902 netif_addr_lock_bh(dev);
12903 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
12904 if (ether_addr_equal(eth->h_dest,
12905 vnic->uc_list + off)) {
12910 netif_addr_unlock_bh(dev);
12914 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
12918 fkeys = &new_fltr->fkeys;
12919 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
12920 rc = -EPROTONOSUPPORT;
12924 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
12925 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
12926 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
12927 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
12928 rc = -EPROTONOSUPPORT;
12931 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
12932 bp->hwrm_spec_code < 0x10601) {
12933 rc = -EPROTONOSUPPORT;
12936 flags = fkeys->control.flags;
12937 if (((flags & FLOW_DIS_ENCAPSULATION) &&
12938 bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
12939 rc = -EPROTONOSUPPORT;
12943 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
12944 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
12946 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
12947 head = &bp->ntp_fltr_hash_tbl[idx];
12949 hlist_for_each_entry_rcu(fltr, head, hash) {
12950 if (bnxt_fltr_match(fltr, new_fltr)) {
12958 spin_lock_bh(&bp->ntp_fltr_lock);
12959 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
12960 BNXT_NTP_FLTR_MAX_FLTR, 0);
12962 spin_unlock_bh(&bp->ntp_fltr_lock);
12967 new_fltr->sw_id = (u16)bit_id;
12968 new_fltr->flow_id = flow_id;
12969 new_fltr->l2_fltr_idx = l2_idx;
12970 new_fltr->rxq = rxq_index;
12971 hlist_add_head_rcu(&new_fltr->hash, head);
12972 bp->ntp_fltr_count++;
12973 spin_unlock_bh(&bp->ntp_fltr_lock);
12975 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
12976 bnxt_queue_sp_work(bp);
12978 return new_fltr->sw_id;
12985 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
12989 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
12990 struct hlist_head *head;
12991 struct hlist_node *tmp;
12992 struct bnxt_ntuple_filter *fltr;
12995 head = &bp->ntp_fltr_hash_tbl[i];
12996 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
12999 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
13000 if (rps_may_expire_flow(bp->dev, fltr->rxq,
13003 bnxt_hwrm_cfa_ntuple_filter_free(bp,
13008 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
13013 set_bit(BNXT_FLTR_VALID, &fltr->state);
13017 spin_lock_bh(&bp->ntp_fltr_lock);
13018 hlist_del_rcu(&fltr->hash);
13019 bp->ntp_fltr_count--;
13020 spin_unlock_bh(&bp->ntp_fltr_lock);
13022 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
13027 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
13028 netdev_info(bp->dev, "Receive PF driver unload event!\n");
13033 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
13037 #endif /* CONFIG_RFS_ACCEL */
13039 static int bnxt_udp_tunnel_sync(struct net_device *netdev, unsigned int table)
13041 struct bnxt *bp = netdev_priv(netdev);
13042 struct udp_tunnel_info ti;
13045 udp_tunnel_nic_get_port(netdev, table, 0, &ti);
13046 if (ti.type == UDP_TUNNEL_TYPE_VXLAN)
13047 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
13049 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
13052 return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti.port, cmd);
13054 return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
13057 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
13058 .sync_table = bnxt_udp_tunnel_sync,
13059 .flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
13060 UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
13062 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, },
13063 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
13067 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13068 struct net_device *dev, u32 filter_mask,
13071 struct bnxt *bp = netdev_priv(dev);
13073 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
13074 nlflags, filter_mask, NULL);
13077 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
13078 u16 flags, struct netlink_ext_ack *extack)
13080 struct bnxt *bp = netdev_priv(dev);
13081 struct nlattr *attr, *br_spec;
13084 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
13085 return -EOPNOTSUPP;
13087 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13091 nla_for_each_nested(attr, br_spec, rem) {
13094 if (nla_type(attr) != IFLA_BRIDGE_MODE)
13097 if (nla_len(attr) < sizeof(mode))
13100 mode = nla_get_u16(attr);
13101 if (mode == bp->br_mode)
13104 rc = bnxt_hwrm_set_br_mode(bp, mode);
13106 bp->br_mode = mode;
13112 int bnxt_get_port_parent_id(struct net_device *dev,
13113 struct netdev_phys_item_id *ppid)
13115 struct bnxt *bp = netdev_priv(dev);
13117 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
13118 return -EOPNOTSUPP;
13120 /* The PF and it's VF-reps only support the switchdev framework */
13121 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
13122 return -EOPNOTSUPP;
13124 ppid->id_len = sizeof(bp->dsn);
13125 memcpy(ppid->id, bp->dsn, ppid->id_len);
13130 static const struct net_device_ops bnxt_netdev_ops = {
13131 .ndo_open = bnxt_open,
13132 .ndo_start_xmit = bnxt_start_xmit,
13133 .ndo_stop = bnxt_close,
13134 .ndo_get_stats64 = bnxt_get_stats64,
13135 .ndo_set_rx_mode = bnxt_set_rx_mode,
13136 .ndo_eth_ioctl = bnxt_ioctl,
13137 .ndo_validate_addr = eth_validate_addr,
13138 .ndo_set_mac_address = bnxt_change_mac_addr,
13139 .ndo_change_mtu = bnxt_change_mtu,
13140 .ndo_fix_features = bnxt_fix_features,
13141 .ndo_set_features = bnxt_set_features,
13142 .ndo_features_check = bnxt_features_check,
13143 .ndo_tx_timeout = bnxt_tx_timeout,
13144 #ifdef CONFIG_BNXT_SRIOV
13145 .ndo_get_vf_config = bnxt_get_vf_config,
13146 .ndo_set_vf_mac = bnxt_set_vf_mac,
13147 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
13148 .ndo_set_vf_rate = bnxt_set_vf_bw,
13149 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
13150 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
13151 .ndo_set_vf_trust = bnxt_set_vf_trust,
13153 .ndo_setup_tc = bnxt_setup_tc,
13154 #ifdef CONFIG_RFS_ACCEL
13155 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
13157 .ndo_bpf = bnxt_xdp,
13158 .ndo_xdp_xmit = bnxt_xdp_xmit,
13159 .ndo_bridge_getlink = bnxt_bridge_getlink,
13160 .ndo_bridge_setlink = bnxt_bridge_setlink,
13163 static void bnxt_remove_one(struct pci_dev *pdev)
13165 struct net_device *dev = pci_get_drvdata(pdev);
13166 struct bnxt *bp = netdev_priv(dev);
13169 bnxt_sriov_disable(bp);
13171 bnxt_rdma_aux_device_uninit(bp);
13173 bnxt_ptp_clear(bp);
13174 unregister_netdev(dev);
13175 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13176 /* Flush any pending tasks */
13177 cancel_work_sync(&bp->sp_task);
13178 cancel_delayed_work_sync(&bp->fw_reset_task);
13181 bnxt_dl_fw_reporters_destroy(bp);
13182 bnxt_dl_unregister(bp);
13183 bnxt_shutdown_tc(bp);
13185 bnxt_clear_int_mode(bp);
13186 bnxt_hwrm_func_drv_unrgtr(bp);
13187 bnxt_free_hwrm_resources(bp);
13188 bnxt_ethtool_free(bp);
13190 kfree(bp->ptp_cfg);
13191 bp->ptp_cfg = NULL;
13192 kfree(bp->fw_health);
13193 bp->fw_health = NULL;
13194 bnxt_cleanup_pci(bp);
13195 bnxt_free_ctx_mem(bp);
13198 kfree(bp->rss_indir_tbl);
13199 bp->rss_indir_tbl = NULL;
13200 bnxt_free_port_stats(bp);
13204 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
13207 struct bnxt_link_info *link_info = &bp->link_info;
13210 rc = bnxt_hwrm_phy_qcaps(bp);
13212 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
13216 if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
13217 bp->dev->priv_flags |= IFF_SUPP_NOFCS;
13219 bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
13223 mutex_lock(&bp->link_lock);
13224 rc = bnxt_update_link(bp, false);
13226 mutex_unlock(&bp->link_lock);
13227 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
13232 /* Older firmware does not have supported_auto_speeds, so assume
13233 * that all supported speeds can be autonegotiated.
13235 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
13236 link_info->support_auto_speeds = link_info->support_speeds;
13238 bnxt_init_ethtool_link_settings(bp);
13239 mutex_unlock(&bp->link_lock);
13243 static int bnxt_get_max_irq(struct pci_dev *pdev)
13247 if (!pdev->msix_cap)
13250 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
13251 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
13254 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
13257 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13258 int max_ring_grps = 0, max_irq;
13260 *max_tx = hw_resc->max_tx_rings;
13261 *max_rx = hw_resc->max_rx_rings;
13262 *max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
13263 max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
13264 bnxt_get_ulp_msix_num(bp),
13265 hw_resc->max_stat_ctxs - bnxt_get_ulp_stat_ctxs(bp));
13266 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
13267 *max_cp = min_t(int, *max_cp, max_irq);
13268 max_ring_grps = hw_resc->max_hw_ring_grps;
13269 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
13273 if (bp->flags & BNXT_FLAG_AGG_RINGS)
13275 if (bp->flags & BNXT_FLAG_CHIP_P5) {
13276 bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
13277 /* On P5 chips, max_cp output param should be available NQs */
13280 *max_rx = min_t(int, *max_rx, max_ring_grps);
13283 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
13287 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
13290 if (!rx || !tx || !cp)
13293 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
13296 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
13301 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
13302 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
13303 /* Not enough rings, try disabling agg rings. */
13304 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
13305 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
13307 /* set BNXT_FLAG_AGG_RINGS back for consistency */
13308 bp->flags |= BNXT_FLAG_AGG_RINGS;
13311 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
13312 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13313 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13314 bnxt_set_ring_params(bp);
13317 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
13318 int max_cp, max_stat, max_irq;
13320 /* Reserve minimum resources for RoCE */
13321 max_cp = bnxt_get_max_func_cp_rings(bp);
13322 max_stat = bnxt_get_max_func_stat_ctxs(bp);
13323 max_irq = bnxt_get_max_func_irqs(bp);
13324 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
13325 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
13326 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
13329 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
13330 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
13331 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
13332 max_cp = min_t(int, max_cp, max_irq);
13333 max_cp = min_t(int, max_cp, max_stat);
13334 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
13341 /* In initial default shared ring setting, each shared ring must have a
13344 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
13346 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
13347 bp->rx_nr_rings = bp->cp_nr_rings;
13348 bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
13349 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
13352 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
13354 int dflt_rings, max_rx_rings, max_tx_rings, rc;
13356 if (!bnxt_can_reserve_rings(bp))
13360 bp->flags |= BNXT_FLAG_SHARED_RINGS;
13361 dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
13362 /* Reduce default rings on multi-port cards so that total default
13363 * rings do not exceed CPU count.
13365 if (bp->port_count > 1) {
13367 max_t(int, num_online_cpus() / bp->port_count, 1);
13369 dflt_rings = min_t(int, dflt_rings, max_rings);
13371 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
13374 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
13375 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
13377 bnxt_trim_dflt_sh_rings(bp);
13379 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
13380 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
13382 rc = __bnxt_reserve_rings(bp);
13383 if (rc && rc != -ENODEV)
13384 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
13385 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13387 bnxt_trim_dflt_sh_rings(bp);
13389 /* Rings may have been trimmed, re-reserve the trimmed rings. */
13390 if (bnxt_need_reserve_rings(bp)) {
13391 rc = __bnxt_reserve_rings(bp);
13392 if (rc && rc != -ENODEV)
13393 netdev_warn(bp->dev, "2nd rings reservation failed.\n");
13394 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13396 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
13401 bp->tx_nr_rings = 0;
13402 bp->rx_nr_rings = 0;
13407 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
13411 if (bp->tx_nr_rings)
13414 bnxt_ulp_irq_stop(bp);
13415 bnxt_clear_int_mode(bp);
13416 rc = bnxt_set_dflt_rings(bp, true);
13418 if (BNXT_VF(bp) && rc == -ENODEV)
13419 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
13421 netdev_err(bp->dev, "Not enough rings available.\n");
13422 goto init_dflt_ring_err;
13424 rc = bnxt_init_int_mode(bp);
13426 goto init_dflt_ring_err;
13428 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13430 bnxt_set_dflt_rfs(bp);
13432 init_dflt_ring_err:
13433 bnxt_ulp_irq_restart(bp, rc);
13437 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
13442 bnxt_hwrm_func_qcaps(bp);
13444 if (netif_running(bp->dev))
13445 __bnxt_close_nic(bp, true, false);
13447 bnxt_ulp_irq_stop(bp);
13448 bnxt_clear_int_mode(bp);
13449 rc = bnxt_init_int_mode(bp);
13450 bnxt_ulp_irq_restart(bp, rc);
13452 if (netif_running(bp->dev)) {
13454 dev_close(bp->dev);
13456 rc = bnxt_open_nic(bp, true, false);
13462 static int bnxt_init_mac_addr(struct bnxt *bp)
13467 eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
13469 #ifdef CONFIG_BNXT_SRIOV
13470 struct bnxt_vf_info *vf = &bp->vf;
13471 bool strict_approval = true;
13473 if (is_valid_ether_addr(vf->mac_addr)) {
13474 /* overwrite netdev dev_addr with admin VF MAC */
13475 eth_hw_addr_set(bp->dev, vf->mac_addr);
13476 /* Older PF driver or firmware may not approve this
13479 strict_approval = false;
13481 eth_hw_addr_random(bp->dev);
13483 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
13489 static void bnxt_vpd_read_info(struct bnxt *bp)
13491 struct pci_dev *pdev = bp->pdev;
13492 unsigned int vpd_size, kw_len;
13496 vpd_data = pci_vpd_alloc(pdev, &vpd_size);
13497 if (IS_ERR(vpd_data)) {
13498 pci_warn(pdev, "Unable to read VPD\n");
13502 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
13503 PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
13507 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
13508 memcpy(bp->board_partno, &vpd_data[pos], size);
13511 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
13512 PCI_VPD_RO_KEYWORD_SERIALNO,
13517 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
13518 memcpy(bp->board_serialno, &vpd_data[pos], size);
13523 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
13525 struct pci_dev *pdev = bp->pdev;
13528 qword = pci_get_dsn(pdev);
13530 netdev_info(bp->dev, "Unable to read adapter's DSN\n");
13531 return -EOPNOTSUPP;
13534 put_unaligned_le64(qword, dsn);
13536 bp->flags |= BNXT_FLAG_DSN_VALID;
13540 static int bnxt_map_db_bar(struct bnxt *bp)
13544 bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
13550 void bnxt_print_device_info(struct bnxt *bp)
13552 netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
13553 board_info[bp->board_idx].name,
13554 (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
13556 pcie_print_link_status(bp->pdev);
13559 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
13561 struct net_device *dev;
13565 if (pci_is_bridge(pdev))
13568 /* Clear any pending DMA transactions from crash kernel
13569 * while loading driver in capture kernel.
13571 if (is_kdump_kernel()) {
13572 pci_clear_master(pdev);
13576 max_irqs = bnxt_get_max_irq(pdev);
13577 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
13581 bp = netdev_priv(dev);
13582 bp->board_idx = ent->driver_data;
13583 bp->msg_enable = BNXT_DEF_MSG_ENABLE;
13584 bnxt_set_max_func_irqs(bp, max_irqs);
13586 if (bnxt_vf_pciid(bp->board_idx))
13587 bp->flags |= BNXT_FLAG_VF;
13589 /* No devlink port registration in case of a VF */
13591 SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
13593 if (pdev->msix_cap)
13594 bp->flags |= BNXT_FLAG_MSIX_CAP;
13596 rc = bnxt_init_board(pdev, dev);
13598 goto init_err_free;
13600 dev->netdev_ops = &bnxt_netdev_ops;
13601 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
13602 dev->ethtool_ops = &bnxt_ethtool_ops;
13603 pci_set_drvdata(pdev, dev);
13605 rc = bnxt_alloc_hwrm_resources(bp);
13607 goto init_err_pci_clean;
13609 mutex_init(&bp->hwrm_cmd_lock);
13610 mutex_init(&bp->link_lock);
13612 rc = bnxt_fw_init_one_p1(bp);
13614 goto init_err_pci_clean;
13617 bnxt_vpd_read_info(bp);
13619 if (BNXT_CHIP_P5(bp)) {
13620 bp->flags |= BNXT_FLAG_CHIP_P5;
13621 if (BNXT_CHIP_SR2(bp))
13622 bp->flags |= BNXT_FLAG_CHIP_SR2;
13625 rc = bnxt_alloc_rss_indir_tbl(bp);
13627 goto init_err_pci_clean;
13629 rc = bnxt_fw_init_one_p2(bp);
13631 goto init_err_pci_clean;
13633 rc = bnxt_map_db_bar(bp);
13635 dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
13637 goto init_err_pci_clean;
13640 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
13641 NETIF_F_TSO | NETIF_F_TSO6 |
13642 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
13643 NETIF_F_GSO_IPXIP4 |
13644 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
13645 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
13646 NETIF_F_RXCSUM | NETIF_F_GRO;
13648 if (BNXT_SUPPORTS_TPA(bp))
13649 dev->hw_features |= NETIF_F_LRO;
13651 dev->hw_enc_features =
13652 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
13653 NETIF_F_TSO | NETIF_F_TSO6 |
13654 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
13655 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
13656 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
13657 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
13659 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
13660 NETIF_F_GSO_GRE_CSUM;
13661 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
13662 if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
13663 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13664 if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
13665 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
13666 if (BNXT_SUPPORTS_TPA(bp))
13667 dev->hw_features |= NETIF_F_GRO_HW;
13668 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
13669 if (dev->features & NETIF_F_GRO_HW)
13670 dev->features &= ~NETIF_F_LRO;
13671 dev->priv_flags |= IFF_UNICAST_FLT;
13673 netif_set_tso_max_size(dev, GSO_MAX_SIZE);
13675 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
13676 NETDEV_XDP_ACT_RX_SG;
13678 #ifdef CONFIG_BNXT_SRIOV
13679 init_waitqueue_head(&bp->sriov_cfg_wait);
13681 if (BNXT_SUPPORTS_TPA(bp)) {
13682 bp->gro_func = bnxt_gro_func_5730x;
13683 if (BNXT_CHIP_P4(bp))
13684 bp->gro_func = bnxt_gro_func_5731x;
13685 else if (BNXT_CHIP_P5(bp))
13686 bp->gro_func = bnxt_gro_func_5750x;
13688 if (!BNXT_CHIP_P4_PLUS(bp))
13689 bp->flags |= BNXT_FLAG_DOUBLE_DB;
13691 rc = bnxt_init_mac_addr(bp);
13693 dev_err(&pdev->dev, "Unable to initialize mac address.\n");
13694 rc = -EADDRNOTAVAIL;
13695 goto init_err_pci_clean;
13699 /* Read the adapter's DSN to use as the eswitch switch_id */
13700 rc = bnxt_pcie_dsn_get(bp, bp->dsn);
13703 /* MTU range: 60 - FW defined max */
13704 dev->min_mtu = ETH_ZLEN;
13705 dev->max_mtu = bp->max_mtu;
13707 rc = bnxt_probe_phy(bp, true);
13709 goto init_err_pci_clean;
13711 bnxt_set_rx_skb_mode(bp, false);
13712 bnxt_set_tpa_flags(bp);
13713 bnxt_set_ring_params(bp);
13714 rc = bnxt_set_dflt_rings(bp, true);
13716 if (BNXT_VF(bp) && rc == -ENODEV) {
13717 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
13719 netdev_err(bp->dev, "Not enough rings available.\n");
13722 goto init_err_pci_clean;
13725 bnxt_fw_init_one_p3(bp);
13727 bnxt_init_dflt_coal(bp);
13729 if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13730 bp->flags |= BNXT_FLAG_STRIP_VLAN;
13732 rc = bnxt_init_int_mode(bp);
13734 goto init_err_pci_clean;
13736 /* No TC has been set yet and rings may have been trimmed due to
13737 * limited MSIX, so we re-initialize the TX rings per TC.
13739 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
13744 create_singlethread_workqueue("bnxt_pf_wq");
13746 dev_err(&pdev->dev, "Unable to create workqueue.\n");
13748 goto init_err_pci_clean;
13751 rc = bnxt_init_tc(bp);
13753 netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
13757 bnxt_inv_fw_health_reg(bp);
13758 rc = bnxt_dl_register(bp);
13762 rc = register_netdev(dev);
13764 goto init_err_cleanup;
13766 bnxt_dl_fw_reporters_create(bp);
13768 bnxt_rdma_aux_device_init(bp);
13770 bnxt_print_device_info(bp);
13772 pci_save_state(pdev);
13776 bnxt_dl_unregister(bp);
13778 bnxt_shutdown_tc(bp);
13779 bnxt_clear_int_mode(bp);
13781 init_err_pci_clean:
13782 bnxt_hwrm_func_drv_unrgtr(bp);
13783 bnxt_free_hwrm_resources(bp);
13784 bnxt_ethtool_free(bp);
13785 bnxt_ptp_clear(bp);
13786 kfree(bp->ptp_cfg);
13787 bp->ptp_cfg = NULL;
13788 kfree(bp->fw_health);
13789 bp->fw_health = NULL;
13790 bnxt_cleanup_pci(bp);
13791 bnxt_free_ctx_mem(bp);
13794 kfree(bp->rss_indir_tbl);
13795 bp->rss_indir_tbl = NULL;
13802 static void bnxt_shutdown(struct pci_dev *pdev)
13804 struct net_device *dev = pci_get_drvdata(pdev);
13811 bp = netdev_priv(dev);
13813 goto shutdown_exit;
13815 if (netif_running(dev))
13818 bnxt_clear_int_mode(bp);
13819 pci_disable_device(pdev);
13821 if (system_state == SYSTEM_POWER_OFF) {
13822 pci_wake_from_d3(pdev, bp->wol);
13823 pci_set_power_state(pdev, PCI_D3hot);
13830 #ifdef CONFIG_PM_SLEEP
13831 static int bnxt_suspend(struct device *device)
13833 struct net_device *dev = dev_get_drvdata(device);
13834 struct bnxt *bp = netdev_priv(dev);
13839 if (netif_running(dev)) {
13840 netif_device_detach(dev);
13841 rc = bnxt_close(dev);
13843 bnxt_hwrm_func_drv_unrgtr(bp);
13844 pci_disable_device(bp->pdev);
13845 bnxt_free_ctx_mem(bp);
13852 static int bnxt_resume(struct device *device)
13854 struct net_device *dev = dev_get_drvdata(device);
13855 struct bnxt *bp = netdev_priv(dev);
13859 rc = pci_enable_device(bp->pdev);
13861 netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
13865 pci_set_master(bp->pdev);
13866 if (bnxt_hwrm_ver_get(bp)) {
13870 rc = bnxt_hwrm_func_reset(bp);
13876 rc = bnxt_hwrm_func_qcaps(bp);
13880 if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
13885 bnxt_get_wol_settings(bp);
13886 if (netif_running(dev)) {
13887 rc = bnxt_open(dev);
13889 netif_device_attach(dev);
13893 bnxt_ulp_start(bp, rc);
13895 bnxt_reenable_sriov(bp);
13900 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
13901 #define BNXT_PM_OPS (&bnxt_pm_ops)
13905 #define BNXT_PM_OPS NULL
13907 #endif /* CONFIG_PM_SLEEP */
13910 * bnxt_io_error_detected - called when PCI error is detected
13911 * @pdev: Pointer to PCI device
13912 * @state: The current pci connection state
13914 * This function is called after a PCI bus error affecting
13915 * this device has been detected.
13917 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
13918 pci_channel_state_t state)
13920 struct net_device *netdev = pci_get_drvdata(pdev);
13921 struct bnxt *bp = netdev_priv(netdev);
13923 netdev_info(netdev, "PCI I/O error detected\n");
13926 netif_device_detach(netdev);
13930 if (state == pci_channel_io_perm_failure) {
13932 return PCI_ERS_RESULT_DISCONNECT;
13935 if (state == pci_channel_io_frozen)
13936 set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
13938 if (netif_running(netdev))
13939 bnxt_close(netdev);
13941 if (pci_is_enabled(pdev))
13942 pci_disable_device(pdev);
13943 bnxt_free_ctx_mem(bp);
13948 /* Request a slot slot reset. */
13949 return PCI_ERS_RESULT_NEED_RESET;
13953 * bnxt_io_slot_reset - called after the pci bus has been reset.
13954 * @pdev: Pointer to PCI device
13956 * Restart the card from scratch, as if from a cold-boot.
13957 * At this point, the card has exprienced a hard reset,
13958 * followed by fixups by BIOS, and has its config space
13959 * set up identically to what it was at cold boot.
13961 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
13963 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
13964 struct net_device *netdev = pci_get_drvdata(pdev);
13965 struct bnxt *bp = netdev_priv(netdev);
13970 netdev_info(bp->dev, "PCI Slot Reset\n");
13974 if (pci_enable_device(pdev)) {
13975 dev_err(&pdev->dev,
13976 "Cannot re-enable PCI device after reset.\n");
13978 pci_set_master(pdev);
13979 /* Upon fatal error, our device internal logic that latches to
13980 * BAR value is getting reset and will restore only upon
13981 * rewritting the BARs.
13983 * As pci_restore_state() does not re-write the BARs if the
13984 * value is same as saved value earlier, driver needs to
13985 * write the BARs to 0 to force restore, in case of fatal error.
13987 if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
13989 for (off = PCI_BASE_ADDRESS_0;
13990 off <= PCI_BASE_ADDRESS_5; off += 4)
13991 pci_write_config_dword(bp->pdev, off, 0);
13993 pci_restore_state(pdev);
13994 pci_save_state(pdev);
13996 bnxt_inv_fw_health_reg(bp);
13997 bnxt_try_map_fw_health_reg(bp);
13999 /* In some PCIe AER scenarios, firmware may take up to
14000 * 10 seconds to become ready in the worst case.
14003 err = bnxt_try_recover_fw(bp);
14007 } while (retry < BNXT_FW_SLOT_RESET_RETRY);
14010 dev_err(&pdev->dev, "Firmware not ready\n");
14014 err = bnxt_hwrm_func_reset(bp);
14016 result = PCI_ERS_RESULT_RECOVERED;
14018 bnxt_ulp_irq_stop(bp);
14019 bnxt_clear_int_mode(bp);
14020 err = bnxt_init_int_mode(bp);
14021 bnxt_ulp_irq_restart(bp, err);
14025 bnxt_clear_reservations(bp, true);
14032 * bnxt_io_resume - called when traffic can start flowing again.
14033 * @pdev: Pointer to PCI device
14035 * This callback is called when the error recovery driver tells
14036 * us that its OK to resume normal operation.
14038 static void bnxt_io_resume(struct pci_dev *pdev)
14040 struct net_device *netdev = pci_get_drvdata(pdev);
14041 struct bnxt *bp = netdev_priv(netdev);
14044 netdev_info(bp->dev, "PCI Slot Resume\n");
14047 err = bnxt_hwrm_func_qcaps(bp);
14048 if (!err && netif_running(netdev))
14049 err = bnxt_open(netdev);
14051 bnxt_ulp_start(bp, err);
14053 bnxt_reenable_sriov(bp);
14054 netif_device_attach(netdev);
14060 static const struct pci_error_handlers bnxt_err_handler = {
14061 .error_detected = bnxt_io_error_detected,
14062 .slot_reset = bnxt_io_slot_reset,
14063 .resume = bnxt_io_resume
14066 static struct pci_driver bnxt_pci_driver = {
14067 .name = DRV_MODULE_NAME,
14068 .id_table = bnxt_pci_tbl,
14069 .probe = bnxt_init_one,
14070 .remove = bnxt_remove_one,
14071 .shutdown = bnxt_shutdown,
14072 .driver.pm = BNXT_PM_OPS,
14073 .err_handler = &bnxt_err_handler,
14074 #if defined(CONFIG_BNXT_SRIOV)
14075 .sriov_configure = bnxt_sriov_configure,
14079 static int __init bnxt_init(void)
14084 err = pci_register_driver(&bnxt_pci_driver);
14093 static void __exit bnxt_exit(void)
14095 pci_unregister_driver(&bnxt_pci_driver);
14097 destroy_workqueue(bnxt_pf_wq);
14101 module_init(bnxt_init);
14102 module_exit(bnxt_exit);